Client Example Code (HU/TBM):
/**********************************************************************************************
* 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.UconnectAny;
import com.fca.uconnect.global.GlobalUconnectControl.UconnectMessage;
import com.fca.uconnect.global.GlobalUconnectExtWIFIHotSpot.DataPackageStatusRequest;
import com.fca.uconnect.global.GlobalUconnectExtWIFIHotSpot.DataPackageStatusResponse;
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 DataPackageStatus 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 TBMExampleForWifiHotSpot {
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 Semaphore semaphore;//The semaphore object is used to wait for a response.
/**
* Constructor - initialize protocol buffer extension registry for parsing incoming Uconnect Messages.
*/
public TBMExampleForWifiHotSpot() {
extRegistry = ExtensionRegistry.newInstance();
extRegistry.add(GlobalUconnectExtWIFIHotSpot.dataPackageStatusResponse);
this.semaphore = new Semaphore();
}
/**
* This method generates a Protocol Buffer Uconnect Message given the empty message for Wifi.
*
* @return UconnectMessage Protocol Buffer
*/
public UconnectMessage getWifiReqMsg() {
this.messageId++;
DataPackageStatusRequest dataReq = DataPackageStatusRequest.newBuilder()
.build();
UconnectMessage msg = UconnectMessage.newBuilder()
.setTimestamp(System.currentTimeMillis())
.setMessageId(messageId)
.addMessages(UconnectAny.newBuilder().
setExtension(GlobalUconnectExtWIFIHotSpot.dataPackageStatusRequest, dataReq))
.build();
return msg;
}
/**
* This method demonstrates connecting to the AWS IoT mqtt broker and publishing an DataPackageStatus message to the WIFI topic.
*/
public void process() {
mqttClient = new AWSIotMqttClient("endpoint", clientId, "awsAccessKeyId", "awsSecretAccessKey");
try {
mqttClient.connect();
iotTopic = new AWSIotTopic(clientId+"/WIFI/", 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(GlobalUconnectExtWIFIHotSpot.dataPackageStatusResponse)) {
DataPackageStatusResponse dataRespMsg = msgIn.getMessages(0).getExtension(GlobalUconnectExtWIFIHotSpot.dataPackageStatusResponse);
semaphore.unlock();// Releasing the lock as it received response
}
}
};
mqttClient.subscribe(iotTopic);
UconnectMessage msg = getWifiReqMsg();
AWSIotMessage message = new
MessagePublisherListener("/WIFI/"+clientId,AWSIotQos.QOS1,msg.toByteArray());
mqttClient.publish(message);
this.semaphore.lock(); //Wait for response, lock thread
} catch (AWSIotException ex) {
System.out.println("Caught AWS Exception "+ex);
ex.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* Example main to launch example.
*
* @param args
*/
public static void main(String[] args) {
TBMExampleForWifiHotSpot example = new TBMExampleForWifiHotSpot();
example.process();
}
}
Here's an example of how Service Connects to AWS IOT MQTT for Wifi topics and process WIFI Request , send WIFIData 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.UconnectAny;
import com.fca.uconnect.global.GlobalUconnectControl.UconnectMessage;
import com.fca.uconnect.global.GlobalUconnectExtWIFIHotSpot.DataPackageStatusResponse;
import com.fca.uconnect.global.GlobalUconnectExtWIFIHotSpot.WifiPackageDataStatus;
import com.google.protobuf.ByteString;
import com.google.protobuf.ExtensionRegistry;
public class SDPExampleForWifiHotSpot {
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 int correlationId = 0;// The correlationId is a sender messageId so that Sender can recognize the message once its received response.
private boolean messageReceived = false; // flag to wait for the message as part of this example.
public SDPExampleForWifiHotSpot() {
extRegistry = ExtensionRegistry.newInstance();
extRegistry.add(GlobalUconnectExtWIFIHotSpot.dataPackageStatusRequest);
extRegistry.add(GlobalUconnectExtWIFIHotSpot.dataPackageStatusResponse);
extRegistry.add(GlobalUconnectExtWIFIHotSpot.wifiPackageDataStatus);
}
public UconnectMessage getWifiRespMsg(int correlationId,boolean isFromException) {
messageId++;
GlobalUconnectExtWIFIHotSpot.DataPackageStatusResponse.ResponseEnum resp = null;
if(isFromException)
resp = GlobalUconnectExtWIFIHotSpot.DataPackageStatusResponse.ResponseEnum.FAILURE;
else
resp = GlobalUconnectExtWIFIHotSpot.DataPackageStatusResponse.ResponseEnum.SUCCESS;
WifiPackageDataStatus wifiPackageDataStatus = WifiPackageDataStatus.newBuilder()
.setIsLowAlertFlag(false)
.setTotalVolume(1000000) //this is in KB
.setUsedVolume(500000)
.setUsedTiming(20) //days
.setTotalTiming(30)
.build();
DataPackageStatusResponse dataPackageStatusResponse = DataPackageStatusResponse.newBuilder()
.setResponseEnum(resp)
.setPackageStatus(wifiPackageDataStatus)
.build();
UconnectMessage msg = UconnectMessage.newBuilder()
.setTimestamp(System.currentTimeMillis())
.setSessionId(sessionId)
.setMessageId(messageId)
.setCorrelationId(correlationId)
.addMessages(UconnectAny.newBuilder().
setExtension(GlobalUconnectExtWIFIHotSpot.dataPackageStatusResponse, dataPackageStatusResponse))
.build();
return msg;
}
public void process() {
mqttClient = new AWSIotMqttClient("endpoint", clientId, "awsAccessKeyId", "awsSecretAccessKey");
try {
mqttClient.connect();
iotTopic = new AWSIotTopic("/WIFI/#",AWSIotQos.QOS1){
public void onMessage(AWSIotMessage msg) {
try {
UconnectMessage msgIn = UconnectMessage.parseFrom(msg.getPayload(),extRegistry);
GlobalUconnectExtWIFIHotSpot.DataPackageStatusRequest wifiReqMsg = msgIn.getMessages(0).getExtension(GlobalUconnectExtWIFIHotSpot.dataPackageStatusRequest);
correlationId=msgIn.getMessageId();
sendMessage(getWifiRespMsg(correlationId,false));
try {
Thread.sleep(1000);//wait and send WifiDataPush
} catch (InterruptedException e) {
}
sendMessage(getWifiLowAlertPushMsg(sessionId));
} catch( Exception ex) {
System.out.println("Err processing message: "+ex+", try parsing Activation.");
sendMessage(getWifiRespMsg(correlationId,true));
}
}
};
mqttClient.subscribe(iotTopic);
} catch (AWSIotException e) {
System.out.println("Caught Exception "+e);
e.printStackTrace();
}
}
public UconnectMessage getWifiLowAlertPushMsg(ByteString sessionId) {
messageId++;
WifiPackageDataStatus packagStatus = WifiPackageDataStatus.newBuilder()
.setIsLowAlertFlag(true)
.setTotalVolume(1000000) //this is in KB
.setUsedVolume(900000)
.setUsedTiming(20)
.setTotalTiming(30)
.build();
UconnectMessage msg = UconnectMessage.newBuilder()
.setTimestamp(System.currentTimeMillis())
.setMessageId(messageId)
.addMessages(UconnectAny.newBuilder().setExtension(GlobalUconnectExtWIFIHotSpot.wifiPackageDataStatus, packagStatus))
.build();
return msg;
}
public void sendMessage(UconnectMessage msg) {
final byte[] msgOut = msg.toByteArray();
new Thread(new Runnable() {
@Override
public void run() {
try {
AWSIotMessage message = new
MessagePublisherListener(clientId+"/WIFI/",AWSIotQos.QOS1,msgOut);
mqttClient.publish(message);
} catch (AWSIotException e) {
System.out.println("Caught Exception "+e);
e.printStackTrace();
}
}
}).start();
}
public static void main(String[] args) {
SDPExampleForWifiHotSpot m = new SDPExampleForWifiHotSpot();
m.process();
}
}
The example code demonstrates how to connect with AWS IoT for publishing and subscribing to the Wifi topic.
Download Client Example Code here
Download Service Example Code here