17 #ifndef THINCLOUD_EMBEDDED_C_SDK_ 18 #define THINCLOUD_EMBEDDED_C_SDK_ 21 #define TC_EMBEDDED_C_SDK_VERSION_MAJOR 1 22 #define TC_EMBEDDED_C_SDK_VERSION_MINOR 1 23 #define TC_EMBEDDED_C_SDK_VERSION_PATCH 0 37 #include <json-c/json.h> 39 #include "aws_iot_log.h" 40 #include "aws_iot_error.h" 41 #include "aws_iot_mqtt_client_interface.h" 46 #define TC_ID_LENGTH 37 51 #define MAX_TOPIC_LENGTH 257 53 #define REQUEST_METHOD_GET "GET" 54 #define REQUEST_METHOD_PUT "PUT" 55 #define REQUEST_METHOD_POST "POST" 56 #define REQUEST_METHOD_DELETE "DELETE" 81 if (deviceType == NULL || physicalId == NULL)
83 FUNC_EXIT_RC(NULL_VALUE_ERROR);
86 sprintf(buffer,
"thincloud/registration/%s_%s/requests", deviceType, physicalId);
88 FUNC_EXIT_RC(SUCCESS);
111 if (deviceType == NULL || physicalId == NULL || requestId == NULL)
113 FUNC_EXIT_RC(NULL_VALUE_ERROR);
116 sprintf(buffer,
"thincloud/registration/%s_%s/requests/%s/response", deviceType, physicalId, requestId);
118 FUNC_EXIT_RC(SUCCESS);
139 if (deviceId == NULL)
141 FUNC_EXIT_RC(NULL_VALUE_ERROR);
144 sprintf(buffer,
"thincloud/devices/%s/command", deviceId);
146 FUNC_EXIT_RC(SUCCESS);
168 if (deviceId == NULL || commandId == NULL)
170 FUNC_EXIT_RC(NULL_VALUE_ERROR);
173 sprintf(buffer,
"thincloud/devices/%s/command/%s/response", deviceId, commandId);
175 FUNC_EXIT_RC(SUCCESS);
196 if (deviceId == NULL)
198 FUNC_EXIT_RC(NULL_VALUE_ERROR);
201 sprintf(buffer,
"thincloud/devices/%s/requests", deviceId);
203 FUNC_EXIT_RC(SUCCESS);
225 if (deviceId == NULL || requestId == NULL)
227 FUNC_EXIT_RC(NULL_VALUE_ERROR);
230 sprintf(buffer,
"thincloud/devices/%s/requests/%s/response", deviceId, requestId);
232 FUNC_EXIT_RC(SUCCESS);
249 IoT_Error_t
commissioning_request(
char *buffer,
const char *requestId,
const char *deviceType,
const char *physicalId,
char **relatedDeviceIds, uint32_t idsSize)
251 if (deviceType == NULL || physicalId == NULL)
253 FUNC_EXIT_RC(NULL_VALUE_ERROR);
256 json_object *obj = json_object_new_object();
257 if (requestId != NULL)
259 json_object *value = json_object_new_string(requestId);
260 json_object_object_add(obj,
"id", value);
263 json_object *method = json_object_new_string(
"commission");
264 json_object_object_add(obj,
"method", method);
266 json_object *params = json_object_new_array();
267 json_object *data = json_object_new_object();
268 json_object *dataObj = json_object_new_object();
270 json_object *deviceTypeValue = json_object_new_string(deviceType);
271 json_object *physicalIdValue = json_object_new_string(physicalId);
272 json_object_object_add(data,
"deviceType", deviceTypeValue);
273 json_object_object_add(data,
"physicalId", physicalIdValue);
275 if (relatedDeviceIds != NULL && idsSize > 0)
277 json_object *relatedDevices = json_object_new_array();
278 for (uint32_t i = 0; i < idsSize; i++)
280 const char *
id = relatedDeviceIds[i];
281 if (
id == NULL || strlen(
id) <= 0)
286 json_object *deviceId = json_object_new_string(
id);
287 json_object *deviceIdObj = json_object_new_object();
288 json_object_object_add(deviceIdObj,
"deviceId", deviceId);
289 json_object_array_add(relatedDevices, deviceIdObj);
292 json_object_object_add(data,
"relatedDevices", relatedDevices);
295 json_object_object_add(dataObj,
"data", data);
296 json_object_array_add(params, dataObj);
298 json_object_object_add(obj,
"params", params);
300 const char *str = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN);
304 json_object_put(obj);
306 FUNC_EXIT_RC(SUCCESS);
322 IoT_Error_t
commissioning_response(
char *deviceId, uint16_t *statusCode,
char *requestId,
char *payload, uint16_t payloadLen)
324 if (payload == NULL || payloadLen == 0)
326 FUNC_EXIT_RC(SUCCESS);
329 json_tokener *tok = json_tokener_new();
331 json_object *obj = json_tokener_parse_ex(tok, payload, payloadLen);
334 FUNC_EXIT_RC(JSON_PARSE_ERROR);
337 if (requestId != NULL)
339 const char *requestIdValue = json_object_get_string(json_object_object_get(obj,
"id"));
340 if (requestIdValue != NULL)
342 strcpy(requestId, requestIdValue);
346 json_object *result = json_object_object_get(obj,
"result");
350 if (statusCode != NULL)
352 *statusCode = json_object_get_int(json_object_object_get(result,
"statusCode"));
355 if (deviceId != NULL)
357 const char *deviceIdValue = json_object_get_string(json_object_object_get(result,
"deviceId"));
358 if (deviceIdValue != NULL)
360 strcpy(deviceId, deviceIdValue);
365 json_tokener_free(tok);
366 json_object_put(obj);
368 FUNC_EXIT_RC(SUCCESS);
385 IoT_Error_t
command_response(
char *buffer,
const char *requestId, uint16_t statusCode,
bool isErrorResponse,
char *errorMessage, json_object *body)
387 json_object *obj = json_object_new_object();
388 if (requestId != NULL)
390 json_object *value = json_object_new_string(requestId);
391 json_object_object_add(obj,
"id", value);
394 json_object *statusCodeValue = json_object_new_int(statusCode);
398 json_object *error = json_object_new_object();
399 json_object_object_add(error,
"statusCode", statusCodeValue);
400 if (errorMessage != NULL)
402 json_object *errorMessageValue = json_object_new_string(errorMessage);
403 json_object_object_add(error,
"message", errorMessageValue);
406 json_object_object_add(obj,
"error", error);
410 json_object *result = json_object_new_object();
411 json_object_object_add(result,
"statusCode", statusCodeValue);
415 json_object_object_add(result,
"body", body);
418 json_object_object_add(obj,
"result", result);
421 const char *str = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN);
425 json_object_put(obj);
427 FUNC_EXIT_RC(SUCCESS);
443 IoT_Error_t
command_request(
char *requestId,
char *method, json_object **params,
const char *payload,
const unsigned int payloadLen)
445 if (payload == NULL || payloadLen == 0)
447 FUNC_EXIT_RC(SUCCESS);
450 json_tokener *tok = json_tokener_new();
452 json_object *obj = json_tokener_parse_ex(tok, payload, payloadLen);
455 FUNC_EXIT_RC(JSON_PARSE_ERROR);
458 if (requestId != NULL)
460 const char *requestIdValue = json_object_get_string(json_object_object_get(obj,
"id"));
461 if (requestIdValue != NULL)
463 strcpy(requestId, requestIdValue);
469 const char *methodValue = json_object_get_string(json_object_object_get(obj,
"method"));
470 if (methodValue != NULL)
472 strcpy(method, methodValue);
476 json_object *pParams = json_object_object_get(obj,
"params");
478 if (pParams != NULL && params != NULL)
480 const int rc = json_object_deep_copy(pParams, params, NULL);
483 FUNC_EXIT_RC(JSON_PARSE_ERROR);
487 json_tokener_free(tok);
488 json_object_put(obj);
490 FUNC_EXIT_RC(SUCCESS);
505 IoT_Error_t
service_request(
char *buffer,
const char *requestId,
const char *method, json_object *params)
507 json_object *obj = json_object_new_object();
508 if (requestId != NULL)
510 json_object *value = json_object_new_string(requestId);
511 json_object_object_add(obj,
"id", value);
514 json_object *methodValue = json_object_new_string(method);
515 json_object_object_add(obj,
"method", methodValue);
519 json_object_object_add(obj,
"params", params);
522 const char *str = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN);
526 json_object_put(obj);
528 FUNC_EXIT_RC(SUCCESS);
544 IoT_Error_t
service_response(
char *requestId, uint16_t *statusCode, json_object **data,
const char *payload,
const unsigned int payloadLen)
546 if (payload == NULL || payloadLen == 0)
548 FUNC_EXIT_RC(SUCCESS);
551 json_tokener *tok = json_tokener_new();
553 json_object *obj = json_tokener_parse_ex(tok, payload, payloadLen);
556 FUNC_EXIT_RC(JSON_PARSE_ERROR);
559 if (requestId != NULL)
561 const char *requestIdValue = json_object_get_string(json_object_object_get(obj,
"id"));
562 if (requestIdValue != NULL)
565 strcpy(requestId, requestIdValue);
569 json_object *result = json_object_object_get(obj,
"result");
573 if (statusCode != NULL)
575 *statusCode = json_object_get_int(json_object_object_get(result,
"statusCode"));
578 json_object *body = json_object_object_get(result,
"body");
579 if (body != NULL && data != NULL)
581 const int rc = json_object_deep_copy(body, data, NULL);
584 FUNC_EXIT_RC(JSON_PARSE_ERROR);
589 json_tokener_free(tok);
590 json_object_put(obj);
592 FUNC_EXIT_RC(SUCCESS);
610 IoT_Error_t
send_command_response(AWS_IoT_Client *client,
const char *deviceId,
const char *commandId, uint16_t statusCode,
bool isErrorResponse,
char *errorMessage, json_object *body)
620 char payload[MAX_JSON_TOKEN_EXPECTED];
622 rc =
command_response(payload, commandId, statusCode, isErrorResponse, errorMessage, body);
629 IoT_Publish_Message_Params params;
631 params.isRetained =
false;
632 params.payload = (
void *)payload;
633 params.payloadLen = strlen(payload);
635 return aws_iot_mqtt_publish(client, topic, strlen(topic), ¶ms);
652 IoT_Error_t
send_commissioning_request(AWS_IoT_Client *client,
const char *requestId,
const char *deviceType,
const char *physicalId,
char **relatedDeviceIds, uint32_t idsSize)
662 char payload[MAX_JSON_TOKEN_EXPECTED];
671 IoT_Publish_Message_Params params;
673 params.isRetained =
false;
674 params.payload = (
void *)payload;
675 params.payloadLen = strlen(payload);
677 return aws_iot_mqtt_publish(client, topic, strlen(topic), ¶ms);
693 IoT_Error_t
send_service_request(AWS_IoT_Client *client,
const char *requestId,
const char *deviceId,
const char *method, json_object *reqParams)
704 char payload[MAX_JSON_TOKEN_EXPECTED];
713 IoT_Publish_Message_Params params;
715 params.isRetained =
false;
716 params.payload = (
void *)payload;
717 params.payloadLen = strlen(payload);
719 return aws_iot_mqtt_publish(client, topic, strlen(topic), ¶ms);
734 IoT_Error_t
subscribe_to_commissioning_response(AWS_IoT_Client *client,
const char *requestId,
const char *deviceType,
const char *physicalId, pApplicationHandler_t handler,
void *subscribeData)
779 IoT_Error_t
subscribe_to_service_response(AWS_IoT_Client *client,
const char *deviceId,
const char *requestId, pApplicationHandler_t handler,
void *subscribeData)
806 IoT_Error_t
tc_init(AWS_IoT_Client *client,
char *hostAddr,
char *rootCAPath,
char *clientCRTPath,
char *clientKeyPath, iot_disconnect_handler handler,
void *disconnectData)
808 IoT_Client_Init_Params params = iotClientInitParamsDefault;
810 params.enableAutoReconnect =
false;
811 params.pHostURL = hostAddr;
813 params.pRootCALocation = rootCAPath;
814 params.pDeviceCertLocation = clientCRTPath;
815 params.pDevicePrivateKeyLocation = clientKeyPath;
816 params.mqttCommandTimeout_ms = 20000;
817 params.tlsHandshakeTimeout_ms = 5000;
818 params.isSSLHostnameVerify =
true;
819 params.disconnectHandler = handler;
820 params.disconnectHandlerData = disconnectData;
822 return aws_iot_mqtt_init(client, ¶ms);
837 IoT_Error_t
tc_connect(AWS_IoT_Client *client,
char *clientId,
bool autoReconnect)
839 IoT_Client_Connect_Params params = iotClientConnectParamsDefault;
841 params.keepAliveIntervalInSec = 600;
842 params.isCleanSession =
true;
843 params.MQTTVersion = MQTT_3_1_1;
844 params.pClientID = clientId;
845 params.clientIDLen = (uint16_t)strlen(clientId);
846 params.isWillMsgPresent =
false;
848 IoT_Error_t rc = aws_iot_mqtt_connect(client, ¶ms);
856 rc = aws_iot_mqtt_autoreconnect_set_status(client,
true);
863 FUNC_EXIT_RC(SUCCESS);
IoT_Error_t tc_connect(AWS_IoT_Client *client, char *clientId, bool autoReconnect)
Start MQTT connection to ThinCloud host.
Definition: thincloud.h:837
IoT_Error_t tc_init(AWS_IoT_Client *client, char *hostAddr, char *rootCAPath, char *clientCRTPath, char *clientKeyPath, iot_disconnect_handler handler, void *disconnectData)
Initialize an AWS IoT Client.
Definition: thincloud.h:806
IoT_Error_t command_request_topic(char *buffer, const char *deviceId)
Build a command request topic.
Definition: thincloud.h:132
IoT_Error_t service_response(char *requestId, uint16_t *statusCode, json_object **data, const char *payload, const unsigned int payloadLen)
Unmarshall a service response payload.
Definition: thincloud.h:544
IoT_Error_t command_request(char *requestId, char *method, json_object **params, const char *payload, const unsigned int payloadLen)
Unmarshall a command request payload.
Definition: thincloud.h:443
IoT_Error_t commission_response_topic(char *buffer, const char *deviceType, const char *physicalId, const char *requestId)
Build a commission response topic.
Definition: thincloud.h:104
IoT_Error_t commissioning_request(char *buffer, const char *requestId, const char *deviceType, const char *physicalId, char **relatedDeviceIds, uint32_t idsSize)
Build a commissioning request.
Definition: thincloud.h:249
#define MAX_TOPIC_LENGTH
Definition: thincloud.h:51
IoT_Error_t command_response_topic(char *buffer, const char *deviceId, const char *commandId)
Build a command response topic.
Definition: thincloud.h:161
IoT_Error_t service_request(char *buffer, const char *requestId, const char *method, json_object *params)
Marshal a service request.
Definition: thincloud.h:505
IoT_Error_t subscribe_to_commissioning_response(AWS_IoT_Client *client, const char *requestId, const char *deviceType, const char *physicalId, pApplicationHandler_t handler, void *subscribeData)
Subscribe to commissioning respones.
Definition: thincloud.h:734
char SERVICE_RESPONSE_TOPIC_BUFFER[MAX_TOPIC_LENGTH]
Definition: thincloud.h:60
char COMMISSIONING_RESPONSE_TOPIC_BUFFER[MAX_TOPIC_LENGTH]
Definition: thincloud.h:58
IoT_Error_t send_commissioning_request(AWS_IoT_Client *client, const char *requestId, const char *deviceType, const char *physicalId, char **relatedDeviceIds, uint32_t idsSize)
Send commissioning request.
Definition: thincloud.h:652
IoT_Error_t command_response(char *buffer, const char *requestId, uint16_t statusCode, bool isErrorResponse, char *errorMessage, json_object *body)
Marshal a command response.
Definition: thincloud.h:385
char COMMAND_TOPIC_BUFFER[MAX_TOPIC_LENGTH]
Definition: thincloud.h:59
IoT_Error_t send_service_request(AWS_IoT_Client *client, const char *requestId, const char *deviceId, const char *method, json_object *reqParams)
Send service request.
Definition: thincloud.h:693
IoT_Error_t commission_request_topic(char *buffer, const char *deviceType, const char *physicalId)
Build a commission request topic.
Definition: thincloud.h:74
IoT_Error_t service_response_topic(char *buffer, const char *deviceId, const char *requestId)
Build a service response topic.
Definition: thincloud.h:218
IoT_Error_t commissioning_response(char *deviceId, uint16_t *statusCode, char *requestId, char *payload, uint16_t payloadLen)
Unmarshall a commissioning response.
Definition: thincloud.h:322
IoT_Error_t subscribe_to_command_request(AWS_IoT_Client *client, const char *deviceId, pApplicationHandler_t handler, void *subscribeData)
Subscribe to command requests.
Definition: thincloud.h:756
IoT_Error_t send_command_response(AWS_IoT_Client *client, const char *deviceId, const char *commandId, uint16_t statusCode, bool isErrorResponse, char *errorMessage, json_object *body)
Send a command response.
Definition: thincloud.h:610
IoT_Error_t subscribe_to_service_response(AWS_IoT_Client *client, const char *deviceId, const char *requestId, pApplicationHandler_t handler, void *subscribeData)
Subscribe to service responses.
Definition: thincloud.h:779
IoT_Error_t service_request_topic(char *buffer, const char *deviceId)
Build a service request topic.
Definition: thincloud.h:189