/**********************************************************************************************
* 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.GlobalUconnectExtFOTA.FirmwareUpdateResponse;
import com.fca.uconnect.global.GlobalUconnectExtFOTA.FirmwareUpdateResponse.ResponseEnum;
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 FOTA 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 TBMExampleForFOTA {
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 List messages;// This can be used for JUNIT tests.
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 TBMExampleForFOTA() {
extRegistry = ExtensionRegistry.newInstance();
extRegistry.add(GlobalUconnectExtFOTA.firmwareUpdateNotification);
}
public void disconnect(){
try {
mqttClient.disconnect();
} catch (AWSIotException e) {
e.printStackTrace();
}
}
/**
* This method generates a Protocol Buffer Uconnect Message given the required inputs for FOTA Response.
*
* @return
*/
public UconnectMessage getFOTARespMsg(ResponseEnum repsonse){
messageId++;
FirmwareUpdateResponse respMsg = FirmwareUpdateResponse.newBuilder()
.setResponseEnum(repsonse)
.build();
UconnectMessage msg = UconnectMessage.newBuilder()
.setTimestamp(System.currentTimeMillis())
.setMessageId(messageId)
.setCorrelationId(correlationId)
.addMessages(UconnectAny.newBuilder().
setExtension(GlobalUconnectExtFOTA.firmwareUpdateResponse, respMsg))
.build();
return msg;
}
/**
* This method demonstrates connecting to the AWS IoT mqtt broker and publishing an FOTA message to the FOTA topic.
*/
public void process() {
mqttClient = new AWSIotMqttClient("endpoint", clientId, "awsAccessKeyId", "awsSecretAccessKey");
try {
mqttClient.connect();
iotTopic = new AWSIotTopic(clientId+"/FOTA/", AWSIotQos.QOS0) {
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(GlobalUconnectExtFOTA.firmwareUpdateNotification)){
//receive notify and update firmware and send response to SDP
correlationId=msgIn.getMessageId();
sendMessage(getFOTARespMsg(ResponseEnum.SUCCESS));
}
}
};
mqttClient.subscribe(iotTopic);
} catch (AWSIotException ex) {
System.out.println("Caught AWS Exception "+ex);
ex.printStackTrace();
}
}
/**
* This method publishes the message to FOTA topic
*/
public void sendMessage(UconnectMessage msg) {
final byte[] msgOut = msg.toByteArray();
new Thread(new Runnable() {
@Override
public void run() {
try {
mqttClient.publish("/FOTA/"+clientId, AWSIotQos.QOS0, msgOut);
} catch (AWSIotException e) {
System.out.println("Caught Exception "+e);
e.printStackTrace();
}
}
}).start();
}
/**
* Example main to launch example.
*
*/
public static void main(String[] args) {
TBMExampleForFOTA example = new TBMExampleForFOTA();
example.process();
}
}
Here's an example of how SDP Connects to AWS IOT MQTT for FOTA topics.
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.GlobalUconnectExtFOTA.FirmwareUpdateNotification;
import com.fca.uconnect.global.GlobalUconnectExtFOTA.FirmwareUpdateNotification.FirmwareDescriptor;
import com.google.protobuf.ExtensionRegistry;
public class SDPExampleForFOTA {
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;
public SDPExampleForFOTA() {
extRegistry = ExtensionRegistry.newInstance();
extRegistry.add(GlobalUconnectExtFOTA.firmwareUpdateResponse);
}
//Disconnects MQTT
public void disconnect(){
try {
mqttClient.disconnect();
} catch (AWSIotException e) {
e.printStackTrace();
}
}
public UconnectMessage getFirmwareNotifyMsg(String taskId,List firmwareDescriptors) {
messageId++;
FirmwareUpdateNotification updateNotifyMsg = FirmwareUpdateNotification.newBuilder()
.setFotaTaskId(taskId)
.addAllFirmwareDescriptor(firmwareDescriptors)
.build();
UconnectMessage msg = UconnectMessage.newBuilder()
.setTimestamp(System.currentTimeMillis())
.setMessageId(messageId)
.addMessages(UconnectAny.newBuilder().
setExtension(GlobalUconnectExtFOTA.firmwareUpdateNotification, updateNotifyMsg))
.build();
return msg;
}
public List getFOTADataMsg(){
while(messages.isEmpty()){
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
}
}
return messages;
}
public void process() {
mqttClient = new AWSIotMqttClient("endpoint", clientId, "awsAccessKeyId", "awsSecretAccessKey");
try {
mqttClient.connect();
iotTopic = new AWSIotTopic("/FOTA/#",AWSIotQos.QOS0){
public void onMessage(AWSIotMessage msg) {
try {
UconnectMessage msgIn = UconnectMessage.parseFrom(msg.getPayload(),extRegistry);
if(msgIn.getMessages(0).hasExtension(GlobalUconnectExtFOTA.firmwareUpdateResponse)){
//Receive Firmware update response
}
} catch( Exception ex) {
ex.printStackTrace();
System.out.println("Err processing message: "+ex+", try parsing FOTA.");
}
}
};
mqttClient.subscribe(iotTopic);
FirmwareDescriptor firmwareDescriptor = FirmwareDescriptor.newBuilder()
.setChecksum("CE114E4501D2F4E2DCEA3E17B546F339")
.setDescription("Update for ABC")
.setEcuAddress(956488)
.setExpireDate(456899)
.setPackageSize(10000)
.setFirmwareVersion("2.0")
.setUrl("https://abc.com")
.build();
List descriptors = new ArrayList<>();
descriptors.add(firmwareDescriptor);
sendMessage(getFirmwareNotifyMsg("AXCSEA1234",descriptors));
} 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+"/FOTA/", AWSIotQos.QOS0, msgOut);
} catch (AWSIotException e) {
System.out.println("Caught Exception "+e);
e.printStackTrace();
}
}
}).start();
}
public static void main(String[] args) {
SDPExampleForFOTA m = new SDPExampleForFOTA();
m.process();
}
}
The example code demonstrates how to connect with AWS IoT for publishing and subscribing to the FOTA topic.
Download Client Example Code here
Download Service Example Code here