/**********************************************************************************************
 * This example code is provided for illustrative purposes only and provided 'AS IS' without 
 * warranty of any kind.
 * 
 * Copyright ©2014 Chrysler Group LLC.  All Rights Reserved.  All information contained herein is,
 * and remains the property of Chrysler Group LLC and its suppliers, if any.  The intellectual and
 * technical concepts contained herein are proprietary to Chrysler Group LLC and its suppliers
 * and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade
 * secret or copyright law. Dissemination of this information or reproduction of this material is
 * strictly forbidden unless prior written permission is obtained from Chrysler Group LLC.
 *
 * Chrysler, Jeep, Dodge, Ram, SRT, Mopar and the Pentastar logo are registered trademarks of
 * Chrysler Group LLC. FIAT is a registered trademark of Fiat group Marketing & Corporate
 * Communication S.p.A. used under license by Chrysler Group LLC.
 *********************************************************************************************/
package com.fca.uconnect.global;


import com.amazonaws.services.iot.client.AWSIotException;
import com.amazonaws.services.iot.client.AWSIotMessage;
import com.amazonaws.services.iot.client.AWSIotMqttClient;
import com.amazonaws.services.iot.client.AWSIotQos;
import com.amazonaws.services.iot.client.AWSIotTopic;
import com.fca.uconnect.global.GlobalUconnectControl.DRM.AppDrm;
import com.fca.uconnect.global.GlobalUconnectControl.DRM.AppDrm.DRMContent;
import com.fca.uconnect.global.GlobalUconnectControl.DRM.AppDrm.DRMRegistration;
import com.fca.uconnect.global.GlobalUconnectControl.DRM.AppDrm.DRMRegistration.OperationEnum;
import com.fca.uconnect.global.GlobalUconnectControl.DRM.AppDrm.DRMRegistration.RegistrationStatusEnum;
import com.fca.uconnect.global.GlobalUconnectControl.DRMService;
import com.fca.uconnect.global.GlobalUconnectControl.DRM;
import com.fca.uconnect.global.GlobalUconnectControl.UconnectAny;
import com.fca.uconnect.global.GlobalUconnectControl.UconnectMessage;
import com.fca.uconnect.global.GlobalUconnectExtAOTA.DiscrepancyCheck;
import com.fca.uconnect.global.GlobalUconnectExtAOTA.UpdateStatusNotification;
import com.fca.uconnect.global.GlobalUconnectExtAOTA.UpdateStatusNotification.StatusEnum;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.InvalidProtocolBufferException;

/**
 * This example class demonstrates how to use the AWS IoT mqtt client to send a Uconnect AOTA message to the SDP and wait for a response message.   
 * This code is only for reference and is furnished without warranty.....
 * 
 * @author FCA Global V2C API Team
 *
 */
public class TBMExampleForAOTA {

	private String clientId; // The clientId is unique to the device and obtained from the client X.509 certificate CN.
	private int messageId; // The messageId is a sequential number incremented and assigned to each message sent from the client.
	private AWSIotMqttClient mqttClient; // The mqtt client object from the AWS IoT client library. 
	private boolean receivedResponse = false; // flag to wait for the response as part of this example.
	private AWSIotTopic iotTopic; // The Topic object to subscribe for inbound messages.
	private ExtensionRegistry extRegistry; // Google protocol buffers extension registry instance.
	private int correlationId = 0;// The correlationId is a sender messageId so that Sender can recognize the message once its received response.
	/**
	 * Constructor - initialize protocol buffer extension registry for parsing incoming Uconnect Messages.
	 */
	public TBMExampleForAOTA() {
		extRegistry = ExtensionRegistry.newInstance();
		extRegistry.add(GlobalUconnectExtAOTA.applicationUpdateNotification);
		extRegistry.add(GlobalUconnectExtAOTA.initiateDiscrepancyCheck);
	}
	public void disconnect(){
		try {
			mqttClient.disconnect();
		} catch (AWSIotException e) {
			e.printStackTrace();
		}
	}
	/**
	 * This method generates a Protocol Buffer Uconnect Message given the required inputs for AOTA Response.
	 *
	 * @return
	 */
	public UconnectMessage getAOTARespMsg(StatusEnum repsonse){
			messageId++;
			UpdateStatusNotification respMsg = UpdateStatusNotification.newBuilder()
					.setStatus(repsonse)
					.build();
		UconnectMessage msg = UconnectMessage.newBuilder()
			.setTimestamp(System.currentTimeMillis())
			.setMessageId(messageId)
			.setCorrelationId(correlationId)
			.addMessages(UconnectAny.newBuilder().
			 setExtension(GlobalUconnectExtAOTA.updateStatusNotification, respMsg))
			.build();

		return msg;		
	}
	
	public UconnectMessage getDescCheckMsg(){
		messageId++;
		DRMRegistration regiMsg = DRMRegistration.newBuilder()
				.setOperation(OperationEnum.RESET)
				.setRegistrationStatus(RegistrationStatusEnum.BASIC_STAGE)
				.build();
		//Only few fields added in this example
				DRMService drmService = DRMService.newBuilder()
						.setAppIdentifier("APPIDENTIFIER")
						.setEmailId("EMAILID")
						.setFileName("FILENAME")
						.setInstallerType("INSTALLER TYPE")
						.build();
		DRMContent drmContent = DRMContent.newBuilder()
				.setDrmService(0, drmService)
				.setApplicationType("APPTYPE")
				.setMsisdn("MSISDN")
				.build();
		AppDrm appDrm = AppDrm.newBuilder()
				.setDrmRegistration(regiMsg)
				.setAmsDRM(drmContent)
				.build();
		DRM drm = DRM.newBuilder()
				.setAppDrm(appDrm)
				.build();
		DiscrepancyCheck descCheckMsg = DiscrepancyCheck.newBuilder()
				.setDrm(drm)
				.build();
		
	UconnectMessage msg = UconnectMessage.newBuilder()
		.setTimestamp(System.currentTimeMillis())
		.setMessageId(messageId)
		.setCorrelationId(correlationId)
		.addMessages(UconnectAny.newBuilder().
		 setExtension(GlobalUconnectExtAOTA.discrepancyCheck, descCheckMsg))
		.build();
	return msg;		
}
	/**
	 * This method demonstrates connecting to the AWS IoT mqtt broker and publishing an AOTA message to the AOTA topic.
	 */
	public void process() {
		
		mqttClient = new AWSIotMqttClient("endpoint", clientId, "awsAccessKeyId", "awsSecretAccessKey");
		try {			
			mqttClient.connect();						
						
			iotTopic = new AWSIotTopic(clientId+"/AOTA/", AWSIotQos.QOS1) {
				public void onMessage(AWSIotMessage msg) {
					UconnectMessage msgIn = null;
					try {
						msgIn = UconnectMessage.parseFrom(msg.getPayload(),extRegistry);
					} catch (InvalidProtocolBufferException e) {
						e.printStackTrace();
					}			
					if(msgIn.getMessages(0).hasExtension(GlobalUconnectExtAOTA.initiateDiscrepancyCheck)){
						//receive Initiate  and send DRM to SDP
						correlationId=msgIn.getMessageId();
						sendMessage(getDescCheckMsg());
					}else if ((msgIn.getMessages(0).hasExtension(GlobalUconnectExtAOTA.applicationUpdateNotification))){
						
						correlationId=msgIn.getMessageId();
						sendMessage(getAOTARespMsg(StatusEnum.SUCCESS));
					}
				}	
			};
			mqttClient.subscribe(iotTopic);
		} catch (AWSIotException ex) {
			System.out.println("Caught AWS Exception "+ex);
			ex.printStackTrace();
		} 
	}
	/**
	 * This method publishes the message to AOTA topic
	 */
	public void sendMessage(UconnectMessage msg) {
		final byte[] msgOut = msg.toByteArray();
		new Thread(new Runnable() {

		    @Override
		public void run() {
		try {						
			mqttClient.publish("/AOTA/"+clientId, AWSIotQos.QOS1, msgOut);
		} catch (AWSIotException e) {
			System.out.println("Caught Exception "+e);
						e.printStackTrace();
					}		    	
			    }
			}).start();
		}		
	/**
	 * Example main to launch example.
	 * 
	 */
	public static void main(String[] args) {
		TBMExampleForAOTA example = new TBMExampleForAOTA();
		example.process();
	}
}
      
      

Here's an example of how SDP Connects to AWS IOT MQTT for AOTA topics.

Service Example Code (SDP):

      
package com.fca.uconnect.global;

import com.amazonaws.services.iot.client.AWSIotException;
import com.amazonaws.services.iot.client.AWSIotMessage;
import com.amazonaws.services.iot.client.AWSIotMqttClient;
import com.amazonaws.services.iot.client.AWSIotQos;
import com.amazonaws.services.iot.client.AWSIotTopic;
import com.fca.uconnect.global.GlobalUconnectControl.UconnectAny;
import com.fca.uconnect.global.GlobalUconnectControl.UconnectMessage;
import com.fca.uconnect.global.GlobalUconnectExtAOTA.ApplicationUpdateNotification;
import com.fca.uconnect.global.GlobalUconnectExtAOTA.InitiateDiscrepancyCheck;
import com.google.protobuf.ExtensionRegistry;

public class SDPExampleForAOTA {
private String clientId; // The clientId is unique to the device and obtained from the client X.509 certificate CN.
private int messageId; // The messageId is a sequential number incremented and assigned to each message sent from the client.
private AWSIotMqttClient mqttClient; // The mqtt client object from the AWS IoT client library. 
private AWSIotTopic iotTopic; // The Topic object to subscribe for inbound messages.
private ExtensionRegistry extRegistry; // Google protocol buffers extension registry instance.  

public SDPExampleForAOTA() {
	extRegistry = ExtensionRegistry.newInstance();
	extRegistry.add(GlobalUconnectExtAOTA.discrepancyCheck);
	extRegistry.add(GlobalUconnectExtAOTA.updateStatusNotification);
}
//Disconnects MQTT 
public void disconnect(){
	try {
		mqttClient.disconnect();
	} catch (AWSIotException e) {
		e.printStackTrace();
	}
}
public UconnectMessage getInitiateDescMsg(){
	messageId++;
	InitiateDiscrepancyCheck descCheckmsg = InitiateDiscrepancyCheck.newBuilder()
			.build();
	UconnectMessage msg = UconnectMessage.newBuilder()
			.setTimestamp(System.currentTimeMillis())
			.setMessageId(messageId)
			.addMessages(UconnectAny.newBuilder().
			 setExtension(GlobalUconnectExtAOTA.initiateDiscrepancyCheck, descCheckmsg))
			.build();

		return msg;		
}
public UconnectMessage getAppUpdateNotifyMsg(String appId,String description,long expireTime,
			int size,String version,String checkSum,String url) {
	messageId++;
	
	ApplicationUpdateNotification updateNotifyMsg = ApplicationUpdateNotification.newBuilder()
			.setAppId(appId)
			.setDescription(description)
			.setExpireDate(expireTime)
			.setPackageSize(size)
			.setChecksum(checkSum)
			.setVersion(version)
			.setUrl(url)
			.build();

	UconnectMessage msg = UconnectMessage.newBuilder()
		.setTimestamp(System.currentTimeMillis())
		.setMessageId(messageId)
		.addMessages(UconnectAny.newBuilder().
		 setExtension(GlobalUconnectExtAOTA.applicationUpdateNotification, updateNotifyMsg))
		.build();

	return msg;		
}

public void process() {
	mqttClient = new AWSIotMqttClient("endpoint", clientId, "awsAccessKeyId", "awsSecretAccessKey");
try {			
mqttClient.connect();						
 iotTopic = new AWSIotTopic("/AOTA/#",AWSIotQos.QOS1){
	public void onMessage(AWSIotMessage msg) {
try {
	UconnectMessage	msgIn = UconnectMessage.parseFrom(msg.getPayload(),extRegistry);
	if(msgIn.getMessages(0).hasExtension(GlobalUconnectExtAOTA.discrepancyCheck)){
		//Receive Discrepancy Check Message
		//send app update
		long expireTime = System.currentTimeMillis()+1000000;
		sendMessage(getAppUpdateNotifyMsg("APPID","DESCRPTION",expireTime,32,"VERSION","CHECKSUM","URL"));
    }
	
} catch( Exception ex) {
	ex.printStackTrace();
	System.out.println("Err processing message: "+ex+", try parsing AOTA.");
}
}
};
mqttClient.subscribe(iotTopic);
sendMessage(getInitiateDescMsg());

} catch (AWSIotException e) {
	System.out.println("Caught Exception "+e);
	e.printStackTrace();
}
}

public void sendMessage(UconnectMessage msg) {
final byte[] msgOut = msg.toByteArray();
new Thread(new Runnable() {

    @Override
public void run() {
try {						
	mqttClient.publish(clientId+"/AOTA/", AWSIotQos.QOS1, msgOut);
} catch (AWSIotException e) {
	System.out.println("Caught Exception "+e);
				e.printStackTrace();
			}		    	
	    }
	}).start();
}
public static void main(String[] args) {
	SDPExampleForAOTA m = new SDPExampleForAOTA();
	m.process();
}
}
      
      

The example code demonstrates how to connect with AWS IoT for publishing and subscribing to the AOTA topic.

Download Client Example Code here

Download Service Example Code here