/**********************************************************************************************
* 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.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.GlobalUconnectExtActivation.ActivationRequest;
import com.fca.uconnect.global.GlobalUconnectExtActivation.ActivationResponse;
import com.google.protobuf.ByteString;
import com.google.protobuf.ExtensionRegistry;
/**
* This example class demonstrates how to use the AWS IoT mqtt client to send a Uconnect Activation 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 TBMExampleForActivation {
private String clientId; // The clientId is unique to the device and obtained from the client X.509 certificate CN.
private ByteString sessionId; // The sessionId is assigned by the SDP to maintain a session independent of connection protocols.
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 Semaphore semaphore;//The semaphore objects is used to wait for a response.
List messages ;
/**
* Constructor - initialize protocol buffer extension registry for parsing incoming Uconnect Messages.
*/
public TBMExampleForActivation() {
extRegistry = ExtensionRegistry.newInstance();
extRegistry.add(GlobalUconnectExtActivation.activationResponse);
extRegistry.add(GlobalUconnectExtProvision.provisioningPush);
this.semaphore = new Semaphore();
}
/**
* This method generates a Protocol Buffer Uconnect Message given the required inputs for Activation.
*
* @param vehicleId
* @param firstName
* @param lastName
* @param emailAddress
* @param acceptTandC
* @return UconnectMessage Protocol Buffer
*/
public UconnectMessage getActivationReqMsg(String vehicleId,String firstName,String lastName,
String emailAddress, boolean acceptTandC) {
this.messageId++;
sessionId = ByteString.copyFrom(new byte[18]);
GlobalUconnectExtActivation.ActivationRequest.TCIndicatorEnum indicatorEnum;
if (acceptTandC) {
indicatorEnum = GlobalUconnectExtActivation.ActivationRequest.TCIndicatorEnum.AGREE;
}
else {
indicatorEnum = GlobalUconnectExtActivation.ActivationRequest.TCIndicatorEnum.DISAGREE;
}
ActivationRequest activationReq = ActivationRequest.newBuilder()
.setVehicleId(vehicleId)
.setEmailAddress(emailAddress)
.setFirstName(firstName)
.setLastName(lastName)
.setTcIndicator(indicatorEnum)
.build();
UconnectMessage msg = UconnectMessage.newBuilder()
.setTimestamp(System.currentTimeMillis())
.setMessageId(messageId)
.addMessages(UconnectAny.newBuilder().
setExtension(GlobalUconnectExtActivation.activationRequest, activationReq))
.build();
return msg;
}
/**
* This method demonstrates connecting to the AWS IoT mqtt broker and publishing an Activation message to the UACT topic.
*/
public List process() {
mqttClient = new AWSIotMqttClient("endpoint", clientId, "awsAccessKeyId", "awsSecretAccessKey");
try {
mqttClient.connect();
iotTopic = new AWSIotTopic(clientId+"/UACT/", AWSIotQos.QOS1) {
//this code will be executed upon receiving an ActivationRespose message.
public void onMessage(AWSIotMessage msg) {
try{
UconnectMessage msgIn;
msgIn = UconnectMessage.parseFrom(msg.getPayload(),extRegistry);
if (msgIn.getCorrelationId()==messageId && msgIn.getMessages(0).hasExtension(GlobalUconnectExtActivation.activationResponse)) {
if(messages==null)
messages = new ArrayList();
messages.add(msgIn.getMessages(0));
ActivationResponse activationMsg = msgIn.getMessages(0).getExtension(GlobalUconnectExtActivation.activationResponse);
semaphore.unlock();// Releasing the lock as it received response
}else if(messages!=null && messages.size()>0){
receivedResponse=true;
messages.add(msgIn.getMessages(0));
}
} catch( Exception ex) {
ex.printStackTrace();
System.out.println("Err processing message: "+ex+", try parsing Activation.");
}
}
};
mqttClient.subscribe(iotTopic);
UconnectMessage msg = getActivationReqMsg("562X4423AE122","fname","lname","flname@google.com",true);
AWSIotMessage message = new MessagePublisherListener("/UACT/"+clientId,AWSIotQos.QOS1,msg.toByteArray());
mqttClient.publish(message);
//This is just to simulate a delay between sending Activation request and receiving Activation response and its just a place holder ,The onMessage method Handles callback
this.semaphore.lock();
if(messages!=null)
return messages;
} catch (AWSIotException ex) {
System.out.println("Caught AWS Exception "+ex);
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Example main to launch example.
*
* @param args
*/
public static void main(String[] args) {
TBMExampleForActivation example = new TBMExampleForActivation();
example.process();
}
}
Here's an example of how Service Connects to AWS IOT MQTT for Activation topics and process Activation Request and send Provision Push.
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.DRM;
import com.fca.uconnect.global.GlobalUconnectControl.UconnectAny;
import com.fca.uconnect.global.GlobalUconnectControl.UconnectMessage;
import com.fca.uconnect.global.GlobalUconnectExtActivation.ActivationResponse;
import com.fca.uconnect.global.GlobalUconnectExtProvision.ProvisioningPush;
import com.google.protobuf.ByteString;
import com.google.protobuf.ExtensionRegistry;
public class SDPExampleForActivation {
private String clientId; // The clientId is unique to the device and obtained from the client X.509 certificate CN.
private ByteString sessionId; // The sessionId is assigned by the SDP to maintain a session independent of connection protocols.
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 boolean termsAndConditions;// flag to check whether User agreed or not for Terms and Condition.
private int correlationId = 0;// The correlationId is a sender messageId so that Sender can recognize the message once its received response.
public SDPExampleForActivation() {
extRegistry = ExtensionRegistry.newInstance();
extRegistry.add(GlobalUconnectExtActivation.activationRequest);
extRegistry.add(GlobalUconnectExtActivation.activationResponse);
extRegistry.add(GlobalUconnectExtProvision.provisioningPush);
}
public static void main(String[] args) {
SDPExampleForActivation m = new SDPExampleForActivation();
m.process();
}
public UconnectMessage getActivationRespMsg(int correlationId,boolean isFromException) {
messageId++;
GlobalUconnectExtActivation.ActivationResponse.ResponseEnum resp = null;
if(termsAndConditions){
resp = GlobalUconnectExtActivation.ActivationResponse.ResponseEnum.ACTIVATED;
}else if(!isFromException){
resp = GlobalUconnectExtActivation.ActivationResponse.ResponseEnum.FAILURE;
}else{
resp = GlobalUconnectExtActivation.ActivationResponse.ResponseEnum.RETRY_ABLE_FAILURE;
}
ActivationResponse aresp = ActivationResponse.newBuilder()
.setResponseEnum(resp)
.build();
UconnectMessage msg = UconnectMessage.newBuilder()
.setTimestamp(System.currentTimeMillis())
.setMessageId(messageId)
.setCorrelationId(correlationId)
.addMessages(UconnectAny.newBuilder().
setExtension(GlobalUconnectExtActivation.activationResponse, aresp))
.build();
return msg;
}
public void disconnect(){
try {
mqttClient.disconnect();
} catch (AWSIotException e) {
e.printStackTrace();
}
}
public void process() {
mqttClient = new AWSIotMqttClient("endpoint", clientId, "awsAccessKeyId", "awsSecretAccessKey");
try {
mqttClient.connect();
iotTopic = new AWSIotTopic("/UACT/#",AWSIotQos.QOS1){
public void onMessage(AWSIotMessage msg) {
try {
UconnectMessage msgIn = UconnectMessage.parseFrom(msg.getPayload(),extRegistry);
GlobalUconnectExtActivation.ActivationRequest activationReqMsg = msgIn.getMessages(0).getExtension(GlobalUconnectExtActivation.activationRequest);
if (activationReqMsg.getVehicleId()!=null && activationReqMsg.getVehicleId().trim().length()>0
&&activationReqMsg.getEmailAddress()!=null &&
activationReqMsg.getEmailAddress().trim().length()>0 && validateEmail(activationReqMsg.getEmailAddress())) {
correlationId=msgIn.getMessageId();
if(activationReqMsg.getTcIndicator().equals(GlobalUconnectExtActivation.ActivationRequest.TCIndicatorEnum.AGREE))
termsAndConditions=true;
else
termsAndConditions=false;
sendMessage(getActivationRespMsg(correlationId,false));
}
try {
Thread.sleep(1000);//simulating a delay between the login response message and pushing an updated provisioning message to the vehicle. The delay represents additional processing time to lookup the services for the use.
} catch (InterruptedException e) {
}
sendMessage(getProvisionPushMsg( sessionId));
} catch( Exception ex) {
ex.printStackTrace();
System.out.println("Err processing message: "+ex+", try parsing Activation.");
sendMessage(getActivationRespMsg(correlationId,true));
}
}
};
mqttClient.subscribe(iotTopic);
} catch (AWSIotException e) {
System.out.println("Caught Exception "+e);
e.printStackTrace();
}
}
public boolean validateEmail(String emailAddress){
// this needs to be implemented with the appropriate logic to meet the requirements documented in the SFS.
return true;
}
public UconnectMessage getProvisionPushMsg(ByteString sessionId) {
messageId++;
ByteString drmFile = ByteString.copyFrom(new byte[18]);
DRM drm = DRM.newBuilder()
.setDrmFile(drmFile)
.build();
ProvisioningPush provisioningPush = ProvisioningPush.newBuilder()
.setDrm(drm)
.build();
UconnectMessage msg = UconnectMessage.newBuilder()
.setTimestamp(System.currentTimeMillis())
.setMessageId(messageId)
.addMessages(UconnectAny.newBuilder().
setExtension(GlobalUconnectExtProvision.provisioningPush, provisioningPush))
.build();
return msg;
}
public void sendMessage(UconnectMessage msg) {
final byte[] msgOut = msg.toByteArray();
new Thread(new Runnable() {
@Override
public void run() {
try {
mqttClient.publish(clientId+"/UACT/", AWSIotQos.QOS1, msgOut);
} catch (AWSIotException e) {
System.out.println("Caught Exception "+e);
e.printStackTrace();
}
}
}).start();
}
}
The example code demonstrates how to connect with AWS IoT for publishing and subscribing to the Activation topic.
Download Client Example Code here
Download Service Example Code here