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