Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of AWS-test by
aws_iot_shadow.cpp
00001 /* 00002 * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"). 00005 * You may not use this file except in compliance with the License. 00006 * A copy of the License is located at 00007 * 00008 * http://aws.amazon.com/apache2.0 00009 * 00010 * or in the "license" file accompanying this file. This file is distributed 00011 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 00012 * express or implied. See the License for the specific language governing 00013 * permissions and limitations under the License. 00014 */ 00015 00016 /** 00017 * @file aws_iot_shadow.c 00018 * @brief Shadow client API definitions 00019 */ 00020 00021 #ifdef __cplusplus 00022 extern "C" { 00023 #endif 00024 00025 #include <string.h> 00026 #include "aws_iot_mqtt_client_interface.h" 00027 #include "aws_iot_shadow_interface.h" 00028 #include "aws_iot_error.h" 00029 #include "aws_iot_log.h" 00030 #include "aws_iot_shadow_actions.h" 00031 #include "aws_iot_shadow_json.h" 00032 #include "aws_iot_shadow_key.h" 00033 #include "aws_iot_shadow_records.h" 00034 00035 const ShadowInitParameters_t ShadowInitParametersDefault = {(char *) AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT, NULL, NULL, 00036 NULL, false, NULL}; 00037 00038 const ShadowConnectParameters_t ShadowConnectParametersDefault = {(char *) AWS_IOT_MY_THING_NAME, 00039 (char *) AWS_IOT_MQTT_CLIENT_ID, 0}; 00040 00041 void aws_iot_shadow_reset_last_received_version(void) { 00042 shadowJsonVersionNum = 0; 00043 } 00044 00045 uint32_t aws_iot_shadow_get_last_received_version(void) { 00046 return shadowJsonVersionNum; 00047 } 00048 00049 void aws_iot_shadow_enable_discard_old_delta_msgs(void) { 00050 shadowDiscardOldDeltaFlag = true; 00051 } 00052 00053 void aws_iot_shadow_disable_discard_old_delta_msgs(void) { 00054 shadowDiscardOldDeltaFlag = false; 00055 } 00056 00057 IoT_Error_t aws_iot_shadow_init(AWS_IoT_Client *pClient, ShadowInitParameters_t *pParams) { 00058 IoT_Client_Init_Params mqttInitParams; 00059 IoT_Error_t rc; 00060 00061 FUNC_ENTRY; 00062 00063 if(NULL == pClient || NULL == pParams) { 00064 FUNC_EXIT_RC(NULL_VALUE_ERROR); 00065 } 00066 00067 mqttInitParams.enableAutoReconnect = pParams->enableAutoReconnect; 00068 mqttInitParams.pHostURL = pParams->pHost; 00069 mqttInitParams.port = pParams->port; 00070 mqttInitParams.pRootCALocation = pParams->pRootCA; 00071 mqttInitParams.pDeviceCertLocation = pParams->pClientCRT; 00072 mqttInitParams.pDevicePrivateKeyLocation = pParams->pClientKey; 00073 mqttInitParams.mqttCommandTimeout_ms = 20000; 00074 mqttInitParams.tlsHandshakeTimeout_ms = 5000; 00075 mqttInitParams.isSSLHostnameVerify = true; 00076 mqttInitParams.disconnectHandler = pParams->disconnectHandler; 00077 00078 rc = aws_iot_mqtt_init(pClient, &mqttInitParams); 00079 if(IOT_SUCCESS != rc) { 00080 FUNC_EXIT_RC(rc); 00081 } 00082 00083 resetClientTokenSequenceNum(); 00084 aws_iot_shadow_reset_last_received_version(); 00085 initDeltaTokens(); 00086 00087 FUNC_EXIT_RC(IOT_SUCCESS); 00088 } 00089 00090 IoT_Error_t aws_iot_shadow_connect(AWS_IoT_Client *pClient, ShadowConnectParameters_t *pParams) { 00091 IoT_Error_t rc = IOT_SUCCESS; 00092 char deleteAcceptedTopic[MAX_SHADOW_TOPIC_LENGTH_BYTES]; 00093 uint16_t deleteAcceptedTopicLen; 00094 IoT_Client_Connect_Params ConnectParams = iotClientConnectParamsDefault; 00095 00096 FUNC_ENTRY; 00097 00098 if(NULL == pClient || NULL == pParams || NULL == pParams->pMqttClientId) { 00099 FUNC_EXIT_RC(NULL_VALUE_ERROR); 00100 } 00101 00102 snprintf(myThingName, MAX_SIZE_OF_THING_NAME, "%s", pParams->pMyThingName); 00103 snprintf(mqttClientID, MAX_SIZE_OF_UNIQUE_CLIENT_ID_BYTES, "%s", pParams->pMqttClientId); 00104 00105 ConnectParams.keepAliveIntervalInSec = 10; 00106 ConnectParams.MQTTVersion = MQTT_3_1_1; 00107 ConnectParams.isCleanSession = true; 00108 ConnectParams.isWillMsgPresent = false; 00109 ConnectParams.pClientID = pParams->pMqttClientId; 00110 ConnectParams.clientIDLen = pParams->mqttClientIdLen; 00111 ConnectParams.pPassword = NULL; 00112 ConnectParams.pUsername = NULL; 00113 00114 rc = aws_iot_mqtt_connect(pClient, &ConnectParams); 00115 00116 if(IOT_SUCCESS == rc) { 00117 initializeRecords(pClient); 00118 } 00119 00120 if(NULL != pParams->deleteActionHandler) { 00121 snprintf(deleteAcceptedTopic, MAX_SHADOW_TOPIC_LENGTH_BYTES, 00122 "$aws/things/%s/shadow/delete/accepted", myThingName); 00123 deleteAcceptedTopicLen = (uint16_t) strlen(deleteAcceptedTopic); 00124 rc = aws_iot_mqtt_subscribe(pClient, deleteAcceptedTopic, deleteAcceptedTopicLen, QOS1, 00125 pParams->deleteActionHandler, (void *) myThingName); 00126 } 00127 00128 FUNC_EXIT_RC(rc); 00129 } 00130 00131 IoT_Error_t aws_iot_shadow_register_delta(AWS_IoT_Client *pMqttClient, jsonStruct_t *pStruct) { 00132 if(NULL == pMqttClient || NULL == pStruct) { 00133 return NULL_VALUE_ERROR; 00134 } 00135 00136 if(!aws_iot_mqtt_is_client_connected(pMqttClient)) { 00137 return MQTT_CONNECTION_ERROR; 00138 } 00139 00140 return registerJsonTokenOnDelta(pStruct); 00141 } 00142 00143 IoT_Error_t aws_iot_shadow_yield(AWS_IoT_Client *pClient, uint32_t timeout) { 00144 if(NULL == pClient) { 00145 return NULL_VALUE_ERROR; 00146 } 00147 00148 HandleExpiredResponseCallbacks(); 00149 return aws_iot_mqtt_yield(pClient, timeout); 00150 } 00151 00152 IoT_Error_t aws_iot_shadow_disconnect(AWS_IoT_Client *pClient) { 00153 return aws_iot_mqtt_disconnect(pClient); 00154 } 00155 00156 IoT_Error_t aws_iot_shadow_update(AWS_IoT_Client *pClient, const char *pThingName, char *pJsonString, 00157 fpActionCallback_t callback, void *pContextData, uint8_t timeout_seconds, 00158 bool isPersistentSubscribe) { 00159 IoT_Error_t rc; 00160 00161 if(NULL == pClient) { 00162 FUNC_EXIT_RC(NULL_VALUE_ERROR); 00163 } 00164 00165 if(!aws_iot_mqtt_is_client_connected(pClient)) { 00166 FUNC_EXIT_RC(MQTT_CONNECTION_ERROR); 00167 } 00168 00169 rc = aws_iot_shadow_internal_action(pThingName, SHADOW_UPDATE, pJsonString, callback, pContextData, 00170 timeout_seconds, isPersistentSubscribe); 00171 00172 FUNC_EXIT_RC(rc); 00173 } 00174 00175 IoT_Error_t aws_iot_shadow_delete(AWS_IoT_Client *pClient, const char *pThingName, fpActionCallback_t callback, 00176 void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe) { 00177 char deleteRequestJsonBuf[MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE]; 00178 IoT_Error_t rc; 00179 00180 FUNC_ENTRY; 00181 00182 if(NULL == pClient) { 00183 FUNC_EXIT_RC(NULL_VALUE_ERROR); 00184 } 00185 00186 if(!aws_iot_mqtt_is_client_connected(pClient)) { 00187 FUNC_EXIT_RC(MQTT_CONNECTION_ERROR); 00188 } 00189 00190 aws_iot_shadow_internal_delete_request_json(deleteRequestJsonBuf); 00191 rc = aws_iot_shadow_internal_action(pThingName, SHADOW_DELETE, deleteRequestJsonBuf, callback, pContextData, 00192 timeout_seconds, isPersistentSubscribe); 00193 00194 FUNC_EXIT_RC(rc); 00195 } 00196 00197 IoT_Error_t aws_iot_shadow_get(AWS_IoT_Client *pClient, const char *pThingName, fpActionCallback_t callback, 00198 void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe) { 00199 char getRequestJsonBuf[MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE]; 00200 IoT_Error_t rc; 00201 00202 FUNC_ENTRY; 00203 00204 if(NULL == pClient) { 00205 FUNC_EXIT_RC(NULL_VALUE_ERROR); 00206 } 00207 00208 if(!aws_iot_mqtt_is_client_connected(pClient)) { 00209 FUNC_EXIT_RC(MQTT_CONNECTION_ERROR); 00210 } 00211 00212 aws_iot_shadow_internal_get_request_json(getRequestJsonBuf); 00213 rc = aws_iot_shadow_internal_action(pThingName, SHADOW_GET, getRequestJsonBuf, callback, pContextData, 00214 timeout_seconds, isPersistentSubscribe); 00215 FUNC_EXIT_RC(rc); 00216 } 00217 00218 IoT_Error_t aws_iot_shadow_set_autoreconnect_status(AWS_IoT_Client *pClient, bool newStatus) { 00219 return aws_iot_mqtt_autoreconnect_set_status(pClient, newStatus); 00220 } 00221 00222 #ifdef __cplusplus 00223 } 00224 #endif 00225
Generated on Tue Jul 12 2022 11:16:37 by
