![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
V.06 11/3
Dependencies: FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts
Fork of ATT_AWS_IoT_demo by
AWS_openssl/aws_iot_src/shadow/aws_iot_shadow.cpp@29:f71a0be59b99, 2017-11-03 (annotated)
- Committer:
- jilee
- Date:
- Fri Nov 03 20:28:02 2017 +0000
- Revision:
- 29:f71a0be59b99
- Parent:
- 15:6f2798e45099
v.06 11/03/2016
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ampembeng | 15:6f2798e45099 | 1 | /* |
ampembeng | 15:6f2798e45099 | 2 | * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
ampembeng | 15:6f2798e45099 | 3 | * |
ampembeng | 15:6f2798e45099 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
ampembeng | 15:6f2798e45099 | 5 | * You may not use this file except in compliance with the License. |
ampembeng | 15:6f2798e45099 | 6 | * A copy of the License is located at |
ampembeng | 15:6f2798e45099 | 7 | * |
ampembeng | 15:6f2798e45099 | 8 | * http://aws.amazon.com/apache2.0 |
ampembeng | 15:6f2798e45099 | 9 | * |
ampembeng | 15:6f2798e45099 | 10 | * or in the "license" file accompanying this file. This file is distributed |
ampembeng | 15:6f2798e45099 | 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
ampembeng | 15:6f2798e45099 | 12 | * express or implied. See the License for the specific language governing |
ampembeng | 15:6f2798e45099 | 13 | * permissions and limitations under the License. |
ampembeng | 15:6f2798e45099 | 14 | */ |
ampembeng | 15:6f2798e45099 | 15 | |
ampembeng | 15:6f2798e45099 | 16 | #include "aws_iot_error.h" |
ampembeng | 15:6f2798e45099 | 17 | #include "aws_iot_log.h" |
ampembeng | 15:6f2798e45099 | 18 | #include "aws_iot_shadow_actions.h" |
ampembeng | 15:6f2798e45099 | 19 | #include "aws_iot_shadow_json.h" |
ampembeng | 15:6f2798e45099 | 20 | #include "aws_iot_shadow_key.h" |
ampembeng | 15:6f2798e45099 | 21 | #include "aws_iot_shadow_records.h" |
ampembeng | 15:6f2798e45099 | 22 | |
jilee | 29:f71a0be59b99 | 23 | extern char *aws_iot_mqtt_host; |
jilee | 29:f71a0be59b99 | 24 | |
ampembeng | 15:6f2798e45099 | 25 | const ShadowParameters_t ShadowParametersDefault = { |
ampembeng | 15:6f2798e45099 | 26 | .pMyThingName = AWS_IOT_MY_THING_NAME, |
ampembeng | 15:6f2798e45099 | 27 | .pMqttClientId = AWS_IOT_MQTT_CLIENT_ID, |
jilee | 29:f71a0be59b99 | 28 | //.pHost = AWS_IOT_MQTT_HOST, |
jilee | 29:f71a0be59b99 | 29 | .pHost = aws_iot_mqtt_host, |
ampembeng | 15:6f2798e45099 | 30 | .port = AWS_IOT_MQTT_PORT, |
ampembeng | 15:6f2798e45099 | 31 | .pRootCA = NULL, |
ampembeng | 15:6f2798e45099 | 32 | .pClientCRT = NULL, |
ampembeng | 15:6f2798e45099 | 33 | .pClientKey = NULL |
ampembeng | 15:6f2798e45099 | 34 | }; |
ampembeng | 15:6f2798e45099 | 35 | |
ampembeng | 15:6f2798e45099 | 36 | void aws_iot_shadow_reset_last_received_version(void) { |
ampembeng | 15:6f2798e45099 | 37 | shadowJsonVersionNum = 0; |
ampembeng | 15:6f2798e45099 | 38 | } |
ampembeng | 15:6f2798e45099 | 39 | |
ampembeng | 15:6f2798e45099 | 40 | uint32_t aws_iot_shadow_get_last_received_version(void) { |
ampembeng | 15:6f2798e45099 | 41 | return shadowJsonVersionNum; |
ampembeng | 15:6f2798e45099 | 42 | } |
ampembeng | 15:6f2798e45099 | 43 | |
ampembeng | 15:6f2798e45099 | 44 | void aws_iot_shadow_enable_discard_old_delta_msgs(void) { |
ampembeng | 15:6f2798e45099 | 45 | shadowDiscardOldDeltaFlag = true; |
ampembeng | 15:6f2798e45099 | 46 | } |
ampembeng | 15:6f2798e45099 | 47 | |
ampembeng | 15:6f2798e45099 | 48 | void aws_iot_shadow_disable_discard_old_delta_msgs(void) { |
ampembeng | 15:6f2798e45099 | 49 | shadowDiscardOldDeltaFlag = false; |
ampembeng | 15:6f2798e45099 | 50 | } |
ampembeng | 15:6f2798e45099 | 51 | |
ampembeng | 15:6f2798e45099 | 52 | IoT_Error_t aws_iot_shadow_init(MQTTClient_t *pClient) { |
ampembeng | 15:6f2798e45099 | 53 | |
ampembeng | 15:6f2798e45099 | 54 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 55 | |
ampembeng | 15:6f2798e45099 | 56 | if (pClient == NULL) { |
ampembeng | 15:6f2798e45099 | 57 | return NULL_VALUE_ERROR; |
ampembeng | 15:6f2798e45099 | 58 | } |
ampembeng | 15:6f2798e45099 | 59 | |
ampembeng | 15:6f2798e45099 | 60 | resetClientTokenSequenceNum(); |
ampembeng | 15:6f2798e45099 | 61 | aws_iot_shadow_reset_last_received_version(); |
ampembeng | 15:6f2798e45099 | 62 | initDeltaTokens(); |
ampembeng | 15:6f2798e45099 | 63 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 64 | } |
ampembeng | 15:6f2798e45099 | 65 | |
ampembeng | 15:6f2798e45099 | 66 | IoT_Error_t aws_iot_shadow_connect(MQTTClient_t *pClient, ShadowParameters_t *pParams) { |
ampembeng | 15:6f2798e45099 | 67 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 68 | |
ampembeng | 15:6f2798e45099 | 69 | MQTTConnectParams ConnectParams = MQTTConnectParamsDefault; |
ampembeng | 15:6f2798e45099 | 70 | if (pClient == NULL) { |
ampembeng | 15:6f2798e45099 | 71 | return NULL_VALUE_ERROR; |
ampembeng | 15:6f2798e45099 | 72 | } |
ampembeng | 15:6f2798e45099 | 73 | |
ampembeng | 15:6f2798e45099 | 74 | if (pClient->connect == NULL) { |
ampembeng | 15:6f2798e45099 | 75 | return NULL_VALUE_ERROR; |
ampembeng | 15:6f2798e45099 | 76 | } |
ampembeng | 15:6f2798e45099 | 77 | |
ampembeng | 15:6f2798e45099 | 78 | snprintf(myThingName, MAX_SIZE_OF_THING_NAME, "%s", pParams->pMyThingName ); |
ampembeng | 15:6f2798e45099 | 79 | snprintf(mqttClientID, MAX_SIZE_OF_UNIQUE_CLIENT_ID_BYTES, "%s", pParams->pMqttClientId ); |
ampembeng | 15:6f2798e45099 | 80 | |
ampembeng | 15:6f2798e45099 | 81 | INFO("...Thing Name %s", myThingName); |
ampembeng | 15:6f2798e45099 | 82 | INFO("...MQTT Client ID %s", mqttClientID); |
ampembeng | 15:6f2798e45099 | 83 | |
ampembeng | 15:6f2798e45099 | 84 | ConnectParams.KeepAliveInterval_sec = 10; |
ampembeng | 15:6f2798e45099 | 85 | ConnectParams.MQTTVersion = MQTT_3_1_1; |
ampembeng | 15:6f2798e45099 | 86 | ConnectParams.mqttCommandTimeout_ms = 10000;//2000; |
ampembeng | 15:6f2798e45099 | 87 | ConnectParams.tlsHandshakeTimeout_ms = 10000; |
ampembeng | 15:6f2798e45099 | 88 | ConnectParams.isCleansession = true; |
ampembeng | 15:6f2798e45099 | 89 | ConnectParams.isSSLHostnameVerify = true; |
ampembeng | 15:6f2798e45099 | 90 | ConnectParams.isWillMsgPresent = false; |
ampembeng | 15:6f2798e45099 | 91 | ConnectParams.pClientID = pParams->pMqttClientId; |
ampembeng | 15:6f2798e45099 | 92 | ConnectParams.pDeviceCertLocation = pParams->pClientCRT; |
ampembeng | 15:6f2798e45099 | 93 | ConnectParams.pDevicePrivateKeyLocation = pParams->pClientKey; |
ampembeng | 15:6f2798e45099 | 94 | ConnectParams.pRootCALocation = pParams->pRootCA; |
ampembeng | 15:6f2798e45099 | 95 | ConnectParams.pPassword = NULL; |
ampembeng | 15:6f2798e45099 | 96 | ConnectParams.pUserName = NULL; |
ampembeng | 15:6f2798e45099 | 97 | ConnectParams.pHostURL = pParams->pHost; |
ampembeng | 15:6f2798e45099 | 98 | ConnectParams.port = pParams->port; |
ampembeng | 15:6f2798e45099 | 99 | ConnectParams.disconnectHandler = NULL; |
ampembeng | 15:6f2798e45099 | 100 | |
ampembeng | 15:6f2798e45099 | 101 | rc = pClient->connect(&ConnectParams); |
ampembeng | 15:6f2798e45099 | 102 | |
ampembeng | 15:6f2798e45099 | 103 | if(rc == NONE_ERROR){ |
ampembeng | 15:6f2798e45099 | 104 | initializeRecords(pClient); |
ampembeng | 15:6f2798e45099 | 105 | } |
ampembeng | 15:6f2798e45099 | 106 | |
ampembeng | 15:6f2798e45099 | 107 | return rc; |
ampembeng | 15:6f2798e45099 | 108 | } |
ampembeng | 15:6f2798e45099 | 109 | |
ampembeng | 15:6f2798e45099 | 110 | IoT_Error_t aws_iot_shadow_register_delta(MQTTClient_t *pClient, jsonStruct_t *pStruct) { |
ampembeng | 15:6f2798e45099 | 111 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 112 | |
ampembeng | 15:6f2798e45099 | 113 | if (!(pClient->isConnected())) { |
ampembeng | 15:6f2798e45099 | 114 | return CONNECTION_ERROR; |
ampembeng | 15:6f2798e45099 | 115 | } |
ampembeng | 15:6f2798e45099 | 116 | |
ampembeng | 15:6f2798e45099 | 117 | rc = registerJsonTokenOnDelta(pStruct); |
ampembeng | 15:6f2798e45099 | 118 | |
ampembeng | 15:6f2798e45099 | 119 | return rc; |
ampembeng | 15:6f2798e45099 | 120 | } |
ampembeng | 15:6f2798e45099 | 121 | |
ampembeng | 15:6f2798e45099 | 122 | IoT_Error_t aws_iot_shadow_yield(MQTTClient_t *pClient, int timeout) { |
ampembeng | 15:6f2798e45099 | 123 | HandleExpiredResponseCallbacks(); |
ampembeng | 15:6f2798e45099 | 124 | return pClient->yield(timeout); |
ampembeng | 15:6f2798e45099 | 125 | } |
ampembeng | 15:6f2798e45099 | 126 | |
ampembeng | 15:6f2798e45099 | 127 | IoT_Error_t aws_iot_shadow_disconnect(MQTTClient_t *pClient) { |
ampembeng | 15:6f2798e45099 | 128 | return pClient->disconnect(); |
ampembeng | 15:6f2798e45099 | 129 | } |
ampembeng | 15:6f2798e45099 | 130 | |
ampembeng | 15:6f2798e45099 | 131 | IoT_Error_t aws_iot_shadow_update(MQTTClient_t *pClient, const char *pThingName, char *pJsonString, |
ampembeng | 15:6f2798e45099 | 132 | fpActionCallback_t callback, void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe) { |
ampembeng | 15:6f2798e45099 | 133 | |
ampembeng | 15:6f2798e45099 | 134 | IoT_Error_t ret_val = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 135 | |
ampembeng | 15:6f2798e45099 | 136 | if (!(pClient->isConnected())) { |
ampembeng | 15:6f2798e45099 | 137 | return CONNECTION_ERROR; |
ampembeng | 15:6f2798e45099 | 138 | } |
ampembeng | 15:6f2798e45099 | 139 | |
ampembeng | 15:6f2798e45099 | 140 | ret_val = iot_shadow_action(pClient, pThingName, SHADOW_UPDATE, pJsonString, callback, pContextData, |
ampembeng | 15:6f2798e45099 | 141 | timeout_seconds, isPersistentSubscribe); |
ampembeng | 15:6f2798e45099 | 142 | |
ampembeng | 15:6f2798e45099 | 143 | return ret_val; |
ampembeng | 15:6f2798e45099 | 144 | } |
ampembeng | 15:6f2798e45099 | 145 | |
ampembeng | 15:6f2798e45099 | 146 | IoT_Error_t aws_iot_shadow_delete(MQTTClient_t *pClient, const char *pThingName, fpActionCallback_t callback, |
ampembeng | 15:6f2798e45099 | 147 | void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe) { |
ampembeng | 15:6f2798e45099 | 148 | IoT_Error_t ret_val = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 149 | |
ampembeng | 15:6f2798e45099 | 150 | if (!(pClient->isConnected())) { |
ampembeng | 15:6f2798e45099 | 151 | return CONNECTION_ERROR; |
ampembeng | 15:6f2798e45099 | 152 | } |
ampembeng | 15:6f2798e45099 | 153 | |
ampembeng | 15:6f2798e45099 | 154 | char deleteRequestJsonBuf[MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE]; |
ampembeng | 15:6f2798e45099 | 155 | iot_shadow_delete_request_json(deleteRequestJsonBuf); |
ampembeng | 15:6f2798e45099 | 156 | ret_val = iot_shadow_action(pClient, pThingName, SHADOW_DELETE, deleteRequestJsonBuf, callback, pContextData, |
ampembeng | 15:6f2798e45099 | 157 | timeout_seconds, isPersistentSubscribe); |
ampembeng | 15:6f2798e45099 | 158 | |
ampembeng | 15:6f2798e45099 | 159 | return ret_val; |
ampembeng | 15:6f2798e45099 | 160 | } |
ampembeng | 15:6f2798e45099 | 161 | |
ampembeng | 15:6f2798e45099 | 162 | IoT_Error_t aws_iot_shadow_get(MQTTClient_t *pClient, const char *pThingName, fpActionCallback_t callback, |
ampembeng | 15:6f2798e45099 | 163 | void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe) { |
ampembeng | 15:6f2798e45099 | 164 | |
ampembeng | 15:6f2798e45099 | 165 | IoT_Error_t ret_val = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 166 | |
ampembeng | 15:6f2798e45099 | 167 | if (!(pClient->isConnected())) { |
ampembeng | 15:6f2798e45099 | 168 | return CONNECTION_ERROR; |
ampembeng | 15:6f2798e45099 | 169 | } |
ampembeng | 15:6f2798e45099 | 170 | |
ampembeng | 15:6f2798e45099 | 171 | char getRequestJsonBuf[MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE]; |
ampembeng | 15:6f2798e45099 | 172 | |
ampembeng | 15:6f2798e45099 | 173 | iot_shadow_get_request_json(getRequestJsonBuf); |
ampembeng | 15:6f2798e45099 | 174 | |
ampembeng | 15:6f2798e45099 | 175 | ret_val = iot_shadow_action(pClient, pThingName, SHADOW_GET, getRequestJsonBuf, callback, pContextData, |
ampembeng | 15:6f2798e45099 | 176 | timeout_seconds, isPersistentSubscribe); |
ampembeng | 15:6f2798e45099 | 177 | |
ampembeng | 15:6f2798e45099 | 178 | return ret_val; |
ampembeng | 15:6f2798e45099 | 179 | } |
ampembeng | 15:6f2798e45099 | 180 |