Jim Flynn
/
aws-iot-device-sdk-mbed-c
Changes to enabled on-line compiler
src/aws_iot_shadow.c@0:082731ede69f, 2018-05-30 (annotated)
- Committer:
- JMF
- Date:
- Wed May 30 20:59:51 2018 +0000
- Revision:
- 0:082731ede69f
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:082731ede69f | 1 | /* |
JMF | 0:082731ede69f | 2 | * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
JMF | 0:082731ede69f | 3 | * |
JMF | 0:082731ede69f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
JMF | 0:082731ede69f | 5 | * You may not use this file except in compliance with the License. |
JMF | 0:082731ede69f | 6 | * A copy of the License is located at |
JMF | 0:082731ede69f | 7 | * |
JMF | 0:082731ede69f | 8 | * http://aws.amazon.com/apache2.0 |
JMF | 0:082731ede69f | 9 | * |
JMF | 0:082731ede69f | 10 | * or in the "license" file accompanying this file. This file is distributed |
JMF | 0:082731ede69f | 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
JMF | 0:082731ede69f | 12 | * express or implied. See the License for the specific language governing |
JMF | 0:082731ede69f | 13 | * permissions and limitations under the License. |
JMF | 0:082731ede69f | 14 | */ |
JMF | 0:082731ede69f | 15 | |
JMF | 0:082731ede69f | 16 | /** |
JMF | 0:082731ede69f | 17 | * @file aws_iot_shadow.c |
JMF | 0:082731ede69f | 18 | * @brief Shadow client API definitions |
JMF | 0:082731ede69f | 19 | */ |
JMF | 0:082731ede69f | 20 | |
JMF | 0:082731ede69f | 21 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 22 | extern "C" { |
JMF | 0:082731ede69f | 23 | #endif |
JMF | 0:082731ede69f | 24 | |
JMF | 0:082731ede69f | 25 | #include <string.h> |
JMF | 0:082731ede69f | 26 | #include "aws_iot_mqtt_client_interface.h" |
JMF | 0:082731ede69f | 27 | #include "aws_iot_shadow_interface.h" |
JMF | 0:082731ede69f | 28 | #include "aws_iot_error.h" |
JMF | 0:082731ede69f | 29 | #include "aws_iot_log.h" |
JMF | 0:082731ede69f | 30 | #include "aws_iot_shadow_actions.h" |
JMF | 0:082731ede69f | 31 | #include "aws_iot_shadow_json.h" |
JMF | 0:082731ede69f | 32 | #include "aws_iot_shadow_key.h" |
JMF | 0:082731ede69f | 33 | #include "aws_iot_shadow_records.h" |
JMF | 0:082731ede69f | 34 | |
JMF | 0:082731ede69f | 35 | const ShadowInitParameters_t ShadowInitParametersDefault = {(char *) AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT, NULL, NULL, |
JMF | 0:082731ede69f | 36 | NULL, false, NULL}; |
JMF | 0:082731ede69f | 37 | |
JMF | 0:082731ede69f | 38 | const ShadowConnectParameters_t ShadowConnectParametersDefault = {(char *) AWS_IOT_MY_THING_NAME, |
JMF | 0:082731ede69f | 39 | (char *) AWS_IOT_MQTT_CLIENT_ID, 0, NULL}; |
JMF | 0:082731ede69f | 40 | |
JMF | 0:082731ede69f | 41 | static char deleteAcceptedTopic[MAX_SHADOW_TOPIC_LENGTH_BYTES]; |
JMF | 0:082731ede69f | 42 | |
JMF | 0:082731ede69f | 43 | void aws_iot_shadow_reset_last_received_version(void) { |
JMF | 0:082731ede69f | 44 | shadowJsonVersionNum = 0; |
JMF | 0:082731ede69f | 45 | } |
JMF | 0:082731ede69f | 46 | |
JMF | 0:082731ede69f | 47 | uint32_t aws_iot_shadow_get_last_received_version(void) { |
JMF | 0:082731ede69f | 48 | return shadowJsonVersionNum; |
JMF | 0:082731ede69f | 49 | } |
JMF | 0:082731ede69f | 50 | |
JMF | 0:082731ede69f | 51 | void aws_iot_shadow_enable_discard_old_delta_msgs(void) { |
JMF | 0:082731ede69f | 52 | shadowDiscardOldDeltaFlag = true; |
JMF | 0:082731ede69f | 53 | } |
JMF | 0:082731ede69f | 54 | |
JMF | 0:082731ede69f | 55 | void aws_iot_shadow_disable_discard_old_delta_msgs(void) { |
JMF | 0:082731ede69f | 56 | shadowDiscardOldDeltaFlag = false; |
JMF | 0:082731ede69f | 57 | } |
JMF | 0:082731ede69f | 58 | |
JMF | 0:082731ede69f | 59 | IoT_Error_t aws_iot_shadow_free(AWS_IoT_Client *pClient) |
JMF | 0:082731ede69f | 60 | { |
JMF | 0:082731ede69f | 61 | IoT_Error_t rc; |
JMF | 0:082731ede69f | 62 | |
JMF | 0:082731ede69f | 63 | if (NULL == pClient) { |
JMF | 0:082731ede69f | 64 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 65 | } |
JMF | 0:082731ede69f | 66 | |
JMF | 0:082731ede69f | 67 | rc = aws_iot_mqtt_free(pClient); |
JMF | 0:082731ede69f | 68 | |
JMF | 0:082731ede69f | 69 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 70 | } |
JMF | 0:082731ede69f | 71 | |
JMF | 0:082731ede69f | 72 | IoT_Error_t aws_iot_shadow_init(AWS_IoT_Client *pClient, ShadowInitParameters_t *pParams) { |
JMF | 0:082731ede69f | 73 | IoT_Client_Init_Params mqttInitParams = IoT_Client_Init_Params_initializer; |
JMF | 0:082731ede69f | 74 | IoT_Error_t rc; |
JMF | 0:082731ede69f | 75 | |
JMF | 0:082731ede69f | 76 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 77 | |
JMF | 0:082731ede69f | 78 | if(NULL == pClient || NULL == pParams) { |
JMF | 0:082731ede69f | 79 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 80 | } |
JMF | 0:082731ede69f | 81 | |
JMF | 0:082731ede69f | 82 | mqttInitParams.enableAutoReconnect = pParams->enableAutoReconnect; |
JMF | 0:082731ede69f | 83 | mqttInitParams.pHostURL = pParams->pHost; |
JMF | 0:082731ede69f | 84 | mqttInitParams.port = pParams->port; |
JMF | 0:082731ede69f | 85 | mqttInitParams.pRootCALocation = pParams->pRootCA; |
JMF | 0:082731ede69f | 86 | mqttInitParams.pDeviceCertLocation = pParams->pClientCRT; |
JMF | 0:082731ede69f | 87 | mqttInitParams.pDevicePrivateKeyLocation = pParams->pClientKey; |
JMF | 0:082731ede69f | 88 | mqttInitParams.mqttPacketTimeout_ms = 5000; |
JMF | 0:082731ede69f | 89 | mqttInitParams.mqttCommandTimeout_ms = 20000; |
JMF | 0:082731ede69f | 90 | mqttInitParams.tlsHandshakeTimeout_ms = 5000; |
JMF | 0:082731ede69f | 91 | mqttInitParams.isSSLHostnameVerify = true; |
JMF | 0:082731ede69f | 92 | mqttInitParams.disconnectHandler = pParams->disconnectHandler; |
JMF | 0:082731ede69f | 93 | |
JMF | 0:082731ede69f | 94 | rc = aws_iot_mqtt_init(pClient, &mqttInitParams); |
JMF | 0:082731ede69f | 95 | if(AWS_SUCCESS != rc) { |
JMF | 0:082731ede69f | 96 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 97 | } |
JMF | 0:082731ede69f | 98 | |
JMF | 0:082731ede69f | 99 | resetClientTokenSequenceNum(); |
JMF | 0:082731ede69f | 100 | aws_iot_shadow_reset_last_received_version(); |
JMF | 0:082731ede69f | 101 | initDeltaTokens(); |
JMF | 0:082731ede69f | 102 | |
JMF | 0:082731ede69f | 103 | FUNC_EXIT_RC(AWS_SUCCESS); |
JMF | 0:082731ede69f | 104 | } |
JMF | 0:082731ede69f | 105 | |
JMF | 0:082731ede69f | 106 | IoT_Error_t aws_iot_shadow_connect(AWS_IoT_Client *pClient, ShadowConnectParameters_t *pParams) { |
JMF | 0:082731ede69f | 107 | IoT_Error_t rc = AWS_SUCCESS; |
JMF | 0:082731ede69f | 108 | uint16_t deleteAcceptedTopicLen; |
JMF | 0:082731ede69f | 109 | IoT_Client_Connect_Params ConnectParams = iotClientConnectParamsDefault; |
JMF | 0:082731ede69f | 110 | |
JMF | 0:082731ede69f | 111 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 112 | |
JMF | 0:082731ede69f | 113 | if(NULL == pClient || NULL == pParams || NULL == pParams->pMqttClientId) { |
JMF | 0:082731ede69f | 114 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 115 | } |
JMF | 0:082731ede69f | 116 | |
JMF | 0:082731ede69f | 117 | snprintf(myThingName, MAX_SIZE_OF_THING_NAME, "%s", pParams->pMyThingName); |
JMF | 0:082731ede69f | 118 | snprintf(mqttClientID, MAX_SIZE_OF_UNIQUE_CLIENT_ID_BYTES, "%s", pParams->pMqttClientId); |
JMF | 0:082731ede69f | 119 | |
JMF | 0:082731ede69f | 120 | ConnectParams.keepAliveIntervalInSec = 600; // NOTE: Temporary fix |
JMF | 0:082731ede69f | 121 | ConnectParams.MQTTVersion = MQTT_3_1_1; |
JMF | 0:082731ede69f | 122 | ConnectParams.isCleanSession = true; |
JMF | 0:082731ede69f | 123 | ConnectParams.isWillMsgPresent = false; |
JMF | 0:082731ede69f | 124 | ConnectParams.pClientID = pParams->pMqttClientId; |
JMF | 0:082731ede69f | 125 | ConnectParams.clientIDLen = pParams->mqttClientIdLen; |
JMF | 0:082731ede69f | 126 | ConnectParams.pPassword = NULL; |
JMF | 0:082731ede69f | 127 | ConnectParams.pUsername = NULL; |
JMF | 0:082731ede69f | 128 | |
JMF | 0:082731ede69f | 129 | rc = aws_iot_mqtt_connect(pClient, &ConnectParams); |
JMF | 0:082731ede69f | 130 | |
JMF | 0:082731ede69f | 131 | if(AWS_SUCCESS != rc) { |
JMF | 0:082731ede69f | 132 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 133 | } |
JMF | 0:082731ede69f | 134 | |
JMF | 0:082731ede69f | 135 | initializeRecords(pClient); |
JMF | 0:082731ede69f | 136 | |
JMF | 0:082731ede69f | 137 | if(NULL != pParams->deleteActionHandler) { |
JMF | 0:082731ede69f | 138 | snprintf(deleteAcceptedTopic, MAX_SHADOW_TOPIC_LENGTH_BYTES, |
JMF | 0:082731ede69f | 139 | "$aws/things/%s/shadow/delete/accepted", myThingName); |
JMF | 0:082731ede69f | 140 | deleteAcceptedTopicLen = (uint16_t) strlen(deleteAcceptedTopic); |
JMF | 0:082731ede69f | 141 | rc = aws_iot_mqtt_subscribe(pClient, deleteAcceptedTopic, deleteAcceptedTopicLen, QOS1, |
JMF | 0:082731ede69f | 142 | pParams->deleteActionHandler, (void *) myThingName); |
JMF | 0:082731ede69f | 143 | } |
JMF | 0:082731ede69f | 144 | |
JMF | 0:082731ede69f | 145 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 146 | } |
JMF | 0:082731ede69f | 147 | |
JMF | 0:082731ede69f | 148 | IoT_Error_t aws_iot_shadow_register_delta(AWS_IoT_Client *pMqttClient, jsonStruct_t *pStruct) { |
JMF | 0:082731ede69f | 149 | if(NULL == pMqttClient || NULL == pStruct) { |
JMF | 0:082731ede69f | 150 | return NULL_VALUE_ERROR; |
JMF | 0:082731ede69f | 151 | } |
JMF | 0:082731ede69f | 152 | |
JMF | 0:082731ede69f | 153 | if(!aws_iot_mqtt_is_client_connected(pMqttClient)) { |
JMF | 0:082731ede69f | 154 | return MQTT_CONNECTION_ERROR; |
JMF | 0:082731ede69f | 155 | } |
JMF | 0:082731ede69f | 156 | |
JMF | 0:082731ede69f | 157 | return registerJsonTokenOnDelta(pStruct); |
JMF | 0:082731ede69f | 158 | } |
JMF | 0:082731ede69f | 159 | |
JMF | 0:082731ede69f | 160 | IoT_Error_t aws_iot_shadow_yield(AWS_IoT_Client *pClient, uint32_t timeout) { |
JMF | 0:082731ede69f | 161 | if(NULL == pClient) { |
JMF | 0:082731ede69f | 162 | return NULL_VALUE_ERROR; |
JMF | 0:082731ede69f | 163 | } |
JMF | 0:082731ede69f | 164 | |
JMF | 0:082731ede69f | 165 | HandleExpiredResponseCallbacks(); |
JMF | 0:082731ede69f | 166 | return aws_iot_mqtt_yield(pClient, timeout); |
JMF | 0:082731ede69f | 167 | } |
JMF | 0:082731ede69f | 168 | |
JMF | 0:082731ede69f | 169 | IoT_Error_t aws_iot_shadow_disconnect(AWS_IoT_Client *pClient) { |
JMF | 0:082731ede69f | 170 | return aws_iot_mqtt_disconnect(pClient); |
JMF | 0:082731ede69f | 171 | } |
JMF | 0:082731ede69f | 172 | |
JMF | 0:082731ede69f | 173 | IoT_Error_t aws_iot_shadow_update(AWS_IoT_Client *pClient, const char *pThingName, char *pJsonString, |
JMF | 0:082731ede69f | 174 | fpActionCallback_t callback, void *pContextData, uint8_t timeout_seconds, |
JMF | 0:082731ede69f | 175 | bool isPersistentSubscribe) { |
JMF | 0:082731ede69f | 176 | IoT_Error_t rc; |
JMF | 0:082731ede69f | 177 | |
JMF | 0:082731ede69f | 178 | if(NULL == pClient) { |
JMF | 0:082731ede69f | 179 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 180 | } |
JMF | 0:082731ede69f | 181 | |
JMF | 0:082731ede69f | 182 | if(!aws_iot_mqtt_is_client_connected(pClient)) { |
JMF | 0:082731ede69f | 183 | FUNC_EXIT_RC(MQTT_CONNECTION_ERROR); |
JMF | 0:082731ede69f | 184 | } |
JMF | 0:082731ede69f | 185 | |
JMF | 0:082731ede69f | 186 | rc = aws_iot_shadow_internal_action(pThingName, SHADOW_UPDATE, pJsonString, strlen(pJsonString), callback, pContextData, |
JMF | 0:082731ede69f | 187 | timeout_seconds, isPersistentSubscribe); |
JMF | 0:082731ede69f | 188 | |
JMF | 0:082731ede69f | 189 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 190 | } |
JMF | 0:082731ede69f | 191 | |
JMF | 0:082731ede69f | 192 | IoT_Error_t aws_iot_shadow_delete(AWS_IoT_Client *pClient, const char *pThingName, fpActionCallback_t callback, |
JMF | 0:082731ede69f | 193 | void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe) { |
JMF | 0:082731ede69f | 194 | char deleteRequestJsonBuf[MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE]; |
JMF | 0:082731ede69f | 195 | IoT_Error_t rc; |
JMF | 0:082731ede69f | 196 | |
JMF | 0:082731ede69f | 197 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 198 | |
JMF | 0:082731ede69f | 199 | if(NULL == pClient) { |
JMF | 0:082731ede69f | 200 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 201 | } |
JMF | 0:082731ede69f | 202 | |
JMF | 0:082731ede69f | 203 | if(!aws_iot_mqtt_is_client_connected(pClient)) { |
JMF | 0:082731ede69f | 204 | FUNC_EXIT_RC(MQTT_CONNECTION_ERROR); |
JMF | 0:082731ede69f | 205 | } |
JMF | 0:082731ede69f | 206 | |
JMF | 0:082731ede69f | 207 | rc = aws_iot_shadow_internal_delete_request_json(deleteRequestJsonBuf, MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE ); |
JMF | 0:082731ede69f | 208 | if ( AWS_SUCCESS != rc ) { |
JMF | 0:082731ede69f | 209 | FUNC_EXIT_RC( rc ); |
JMF | 0:082731ede69f | 210 | } |
JMF | 0:082731ede69f | 211 | |
JMF | 0:082731ede69f | 212 | rc = aws_iot_shadow_internal_action(pThingName, SHADOW_DELETE, deleteRequestJsonBuf, MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE, callback, pContextData, |
JMF | 0:082731ede69f | 213 | timeout_seconds, isPersistentSubscribe); |
JMF | 0:082731ede69f | 214 | |
JMF | 0:082731ede69f | 215 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 216 | } |
JMF | 0:082731ede69f | 217 | |
JMF | 0:082731ede69f | 218 | IoT_Error_t aws_iot_shadow_get(AWS_IoT_Client *pClient, const char *pThingName, fpActionCallback_t callback, |
JMF | 0:082731ede69f | 219 | void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe) { |
JMF | 0:082731ede69f | 220 | char getRequestJsonBuf[MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE]; |
JMF | 0:082731ede69f | 221 | IoT_Error_t rc; |
JMF | 0:082731ede69f | 222 | |
JMF | 0:082731ede69f | 223 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 224 | |
JMF | 0:082731ede69f | 225 | if(NULL == pClient) { |
JMF | 0:082731ede69f | 226 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 227 | } |
JMF | 0:082731ede69f | 228 | |
JMF | 0:082731ede69f | 229 | if(!aws_iot_mqtt_is_client_connected(pClient)) { |
JMF | 0:082731ede69f | 230 | FUNC_EXIT_RC(MQTT_CONNECTION_ERROR); |
JMF | 0:082731ede69f | 231 | } |
JMF | 0:082731ede69f | 232 | |
JMF | 0:082731ede69f | 233 | rc = aws_iot_shadow_internal_get_request_json(getRequestJsonBuf, MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE ); |
JMF | 0:082731ede69f | 234 | if (AWS_SUCCESS != rc) { |
JMF | 0:082731ede69f | 235 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 236 | } |
JMF | 0:082731ede69f | 237 | |
JMF | 0:082731ede69f | 238 | rc = aws_iot_shadow_internal_action(pThingName, SHADOW_GET, getRequestJsonBuf, MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE, callback, pContextData, |
JMF | 0:082731ede69f | 239 | timeout_seconds, isPersistentSubscribe); |
JMF | 0:082731ede69f | 240 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 241 | } |
JMF | 0:082731ede69f | 242 | |
JMF | 0:082731ede69f | 243 | IoT_Error_t aws_iot_shadow_set_autoreconnect_status(AWS_IoT_Client *pClient, bool newStatus) { |
JMF | 0:082731ede69f | 244 | return aws_iot_mqtt_autoreconnect_set_status(pClient, newStatus); |
JMF | 0:082731ede69f | 245 | } |
JMF | 0:082731ede69f | 246 | |
JMF | 0:082731ede69f | 247 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 248 | } |
JMF | 0:082731ede69f | 249 | #endif |
JMF | 0:082731ede69f | 250 |