/**********************************************************************************************
 * 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 java.util.List;

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.GlobalUconnectExtVehicleMessaging.VehicleMessageAck;
import com.fca.uconnect.global.GlobalUconnectExtVehicleMessaging.VehicleMessageAck.ResponseEnum;
import com.fca.uconnect.global.GlobalUconnectExtVehicleMessaging.VehicleMessageDispositionPublish;
import com.fca.uconnect.global.GlobalUconnectExtVehicleMessaging.VehicleMessageDispositionPublish.MessageDispositionEnum;
import com.google.protobuf.ByteString;
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 In Vehicle message Acknowledgement to the SDP .   
 * This code is only for reference and is furnished without warranty.....
 * 
 * @author FCA Global V2C API Team
 *
 */
public class TBMExampleForVehMessaging {

	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.
	private int correlationId = 0;// The correlationId is a sender messageId so that Sender can recognize the message once its received response.
	List messages ;//This is for Junit Tests.
	private Semaphore semaphore;//The semaphore object is used to wait for a response.

	/**
	 * Constructor - initialize protocol buffer extension registry for parsing incoming Uconnect Messages.
	 */
	public TBMExampleForVehMessaging() {
		extRegistry = ExtensionRegistry.newInstance();
		extRegistry.add(GlobalUconnectExtVehicleMessaging.vehicleMessagePublish);
		this.semaphore = new Semaphore();
	}
	
	/**
	 * This method generates a Protocol Buffer Uconnect Message given the required inputs for VehicleMessage Ack
	 * @param response
	 * @param vehMsgId
	 * @return
	 */
	public UconnectMessage getVehicleMessagingAckMsg(ResponseEnum response,ByteString vehMsgId) {
		this.messageId++;
		VehicleMessageAck ackMsg = VehicleMessageAck.newBuilder()
				.setStatus(response)
				.setVehicleMessageID(vehMsgId)
				.setMessageReceiptTimestamp(System.currentTimeMillis())
				.build();
		UconnectMessage msg = UconnectMessage.newBuilder()
			.setTimestamp(System.currentTimeMillis())
			.setMessageId(messageId)
			.setCorrelationId(correlationId)
			.addMessages(UconnectAny.newBuilder().
			 setExtension(GlobalUconnectExtVehicleMessaging.vehicleMessageAck,ackMsg))
			.build();
		return msg;		
	}
	public UconnectMessage getVehicleMessagingDispPubMsg(long messageDisplayTimestamp,
			long messageHMIDispositionEventTimestamp,ByteString vehicleMessageID,MessageDispositionEnum dispEnum) {
		this.messageId++;
		VehicleMessageDispositionPublish vehDispPubMsg = VehicleMessageDispositionPublish.newBuilder()
				.setMessageDisplayTimestamp(messageDisplayTimestamp)
				.setMessageHMIDispositionEventTimestamp(messageHMIDispositionEventTimestamp)
				.setVehicleMessageID(vehicleMessageID)
				.setDisposition(dispEnum)
				.build();
		UconnectMessage msg = UconnectMessage.newBuilder()
			.setTimestamp(System.currentTimeMillis())
			.setMessageId(messageId)
			.setCorrelationId(correlationId)
			.addMessages(UconnectAny.newBuilder().
			 setExtension(GlobalUconnectExtVehicleMessaging.vehicleMessageDispositionPublish, vehDispPubMsg))
			.build();
		return msg;		
	}
	
	/**
	 * This method demonstrates connecting to the AWS IoT mqtt broker and publishing an Vehicle message Ack to the In Vehicle Message topic.
	 */
	public void process() {
		
		mqttClient = new AWSIotMqttClient("endpoint", clientId, "awsAccessKeyId", "awsSecretAccessKey");
		try {			
			mqttClient.connect();						
			iotTopic = new AWSIotTopic(clientId+"/IVM/", AWSIotQos.QOS1) {
				public void onMessage(AWSIotMessage msg) {
					UconnectMessage msgIn = null;
					try {
						msgIn = UconnectMessage.parseFrom(msg.getPayload(),extRegistry);
					} catch (InvalidProtocolBufferException e) {					
					}
					if(msgIn.getMessages(0).hasExtension(GlobalUconnectExtVehicleMessaging.vehicleMessagePublish)){
						semaphore.unlock();// Releasing the lock
						GlobalUconnectExtVehicleMessaging.VehicleMessagePublish msgToDisplay = msgIn.getMessages(0).getExtension(GlobalUconnectExtVehicleMessaging.vehicleMessagePublish);
						correlationId = msgIn.getMessageId();
						ByteString vehMsgId = ByteString.copyFrom(new byte[18]);//This is just an example
						sendMessage(getVehicleMessagingAckMsg(ResponseEnum.MESSAGE_STAGED_FOR_DISPLAY,vehMsgId));
						long dispTime = System.currentTimeMillis();
						
						try {
							Thread.sleep(10000);
						} catch (InterruptedException e) {
						}
						long dispositionTime = System.currentTimeMillis();
						sendMessage(getVehicleMessagingDispPubMsg(dispTime,
						dispositionTime,vehMsgId,MessageDispositionEnum.MESSAGE_DISPLAY_CANCELLED));
				}
			}};
			mqttClient.subscribe(iotTopic);
		} catch (AWSIotException ex) {
			System.out.println("Caught AWS Exception "+ex);
			ex.printStackTrace();
		}  catch (Exception e) {
			e.printStackTrace();
		}
		try {
			this.semaphore.lock();// lock thread
		} catch (InterruptedException e) {
		}	
	}
	/**
	 * This method publishes the message to VehicleMessaging topic
	 */
	public void sendMessage(UconnectMessage msg) {
		final byte[] msgOut = msg.toByteArray();
		new Thread(new Runnable() {

		    @Override
		public void run() {
		try {						
			AWSIotMessage message = new 
					MessagePublisherListener("/IVM/"+clientId,AWSIotQos.QOS1,msgOut);
			mqttClient.publish(message);

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

	/**
	 * Example main to launch example.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		TBMExampleForVehMessaging example = new TBMExampleForVehMessaging();
		example.process();
	}
}
      
      

Here's an example of how Service Connects to AWS IOT MQTT for InVehicleMessaging topics and Send Vehicle Messages and receive Acknowledgement from Vehicle.

Service Example Code (SDP):

      
package com.fca.uconnect.global;
import java.util.ArrayList;
import java.util.List;

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.GlobalUconnectExtVehicleMessaging.VehicleMessagePublish;
import com.fca.uconnect.global.GlobalUconnectExtVehicleMessaging.VehicleMessagePublish.LanguageString;
import com.fca.uconnect.global.GlobalUconnectExtVehicleMessaging.VehicleMessagePublish.MessageType;
import com.google.protobuf.ExtensionRegistry;

public class SDPExampleForVehMessaging {
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.
private List messages;//This is for Junit Tests.


public SDPExampleForVehMessaging() {
	extRegistry = ExtensionRegistry.newInstance();
	extRegistry.add(GlobalUconnectExtVehicleMessaging.vehicleMessageAck);
	extRegistry.add(GlobalUconnectExtVehicleMessaging.vehicleMessageDispositionPublish);
}
public UconnectMessage getVehicleMessagingPubMsg(String vin,MessageType messageType,LanguageString lang) {
	messageId++;
	VehicleMessagePublish pubMsg = VehicleMessagePublish.newBuilder()
			.setVin(vin)
			.setMessageType(messageType)
			.addMessage(lang)
			.build();
	
	UconnectMessage msg = UconnectMessage.newBuilder()
		.setTimestamp(System.currentTimeMillis())
		.setMessageId(messageId)
		.addMessages(UconnectAny.newBuilder().
		 setExtension(GlobalUconnectExtVehicleMessaging.vehicleMessagePublish,pubMsg))
		.build();

	return msg;		
}

/**
 * This method demonstrates connecting to the AWS IoT mqtt broker and Subscribing  an Parental Control message to the VehicleMessaging topic.
 */
public void process() {

	mqttClient = new AWSIotMqttClient("endpoint", clientId, "awsAccessKeyId", "awsSecretAccessKey");
try {			
mqttClient.connect();						
 iotTopic = new AWSIotTopic("/IVM/#",AWSIotQos.QOS1){
	public void onMessage(AWSIotMessage msg) {
try {
	UconnectMessage	msgIn = UconnectMessage.parseFrom(msg.getPayload(),extRegistry);			
	if(msgIn.getMessages(0).hasExtension(GlobalUconnectExtVehicleMessaging.vehicleMessageAck)){
		GlobalUconnectExtVehicleMessaging.VehicleMessageAck acknowledgement= msgIn.getMessages(0).getExtension(GlobalUconnectExtVehicleMessaging.vehicleMessageAck);
		if(messages==null)
			messages = new ArrayList();
		messages.add(msgIn.getMessages(0));
	}
} catch( Exception ex) {
	ex.printStackTrace();
}
}
};
LanguageString lang = LanguageString.newBuilder()
.setLanguage("ENGLISH")
.setMessageText("Your Vehicle is Due for Service")
.build();

sendMessage(getVehicleMessagingPubMsg("1HGCM82633A004352",MessageType.SERVICE_NOTICE,lang));
mqttClient.subscribe(iotTopic);

} 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 {						
	AWSIotMessage message = new 
			MessagePublisherListener(clientId+"/IVM/",AWSIotQos.QOS1,msgOut);
	mqttClient.publish(message);

} catch (AWSIotException e) {
	System.out.println("Caught Exception "+e);
				e.printStackTrace();
			}		    	
	    }
	}).start();
}
public static void main(String[] args) {
	SDPExampleForVehMessaging m = new SDPExampleForVehMessaging();
	m.process();
}
}
      
      

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

Download Client Example Code here

Download Service Example Code here