Pierre-Marie Ancèle
/
AWS-test
test
Embed:
(wiki syntax)
Show/hide line numbers
aws_iot_shadow_interface.h
Go to the documentation of this file.
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 #ifndef AWS_IOT_SDK_SRC_IOT_SHADOW_H_ 00016 #define AWS_IOT_SDK_SRC_IOT_SHADOW_H_ 00017 00018 #ifdef __cplusplus 00019 extern "C" { 00020 #endif 00021 00022 00023 /** 00024 * @file aws_iot_shadow_interface.h 00025 * @brief Interface for thing shadow 00026 * 00027 * These are the functions and structs to manage/interact the Thing Shadow(in the cloud). 00028 * This SDK will let you interact with your own thing shadow or any other shadow using its Thing Name. 00029 * There are totally 3 actions a device can perform on the shadow - Get, Update and Delete. 00030 * 00031 * Currently the device should use MQTT/S underneath. In the future this will also support other protocols. As it supports MQTT, the shadow needs to connect and disconnect. 00032 * It will also work on the pub/sub model. On performing any action, the acknowledgment will be received in either accepted or rejected. For Example: 00033 * If we want to perform a GET on the thing shadow the following messages will be sent and received: 00034 * 1. A MQTT Publish on the topic - $aws/things/{thingName}/shadow/get 00035 * 2. Subscribe to MQTT topics - $aws/things/{thingName}/shadow/get/accepted and $aws/things/{thingName}/shadow/get/rejected. 00036 * If the request was successful we will receive the things json document in the accepted topic. 00037 * 00038 * 00039 */ 00040 #include "aws_iot_mqtt_client_interface.h" 00041 #include "aws_iot_shadow_json_data.h" 00042 00043 /*! 00044 * @brief Shadow Initialization parameters 00045 * 00046 * As the Shadow SDK uses MQTT underneath, it could be connected and disconnected on events to save some battery. 00047 * @note Always use the \c ShadowIniTParametersDefault to initialize this struct 00048 * 00049 * 00050 * 00051 */ 00052 typedef struct { 00053 char *pHost; ///< This will be unique to a customer and can be retrieved from the console 00054 uint16_t port; ///< By default the port is 8883 00055 char *pRootCA; ///< Location with the Filename of the Root CA 00056 char *pClientCRT; ///< Location of Device certs signed by AWS IoT service 00057 char *pClientKey; ///< Location of Device private key 00058 bool enableAutoReconnect; ///< Set to true to enable auto reconnect 00059 iot_disconnect_handler disconnectHandler; ///< Callback to be invoked upon connection loss. 00060 } ShadowInitParameters_t; 00061 00062 /*! 00063 * @brief Shadow Connect parameters 00064 * 00065 * As the Shadow SDK uses MQTT underneath, it could be connected and disconnected on events to save some battery. 00066 * @note Always use the \c ShadowConnectParametersDefault to initialize this struct 00067 * 00068 *d 00069 * 00070 */ 00071 typedef struct { 00072 char *pMyThingName; ///< Every device has a Thing Shadow and this is the placeholder for name 00073 char *pMqttClientId; ///< Currently the Shadow uses MQTT to connect and it is important to ensure we have unique client id 00074 uint16_t mqttClientIdLen; ///< Currently the Shadow uses MQTT to connect and it is important to ensure we have unique client id 00075 pApplicationHandler_t deleteActionHandler; ///< Callback to be invoked when Thing shadow for this device is deleted 00076 } ShadowConnectParameters_t; 00077 00078 /*! 00079 * @brief This is set to defaults from the configuration file 00080 * The certs are set to NULL because they need the path to the file. shadow_sample.c file demonstrates on how to get the relative path 00081 * 00082 * \relates ShadowInitParameters_t 00083 */ 00084 extern const ShadowInitParameters_t ShadowInitParametersDefault; 00085 00086 /*! 00087 * @brief This is set to defaults from the configuration file 00088 * The length of the client id is initialized as 0. This is due to C language limitations of using constant literals 00089 * only for creating const variables. The client id will be assigned using the value from aws_iot_config.h but the 00090 * length needs to be assigned in code. shadow_sample.c file demonstrates this. 00091 * 00092 * \relates ShadowConnectParameters_t 00093 */ 00094 extern const ShadowConnectParameters_t ShadowConnectParametersDefault; 00095 00096 00097 /** 00098 * @brief Initialize the Thing Shadow before use 00099 * 00100 * This function takes care of initializing the internal book-keeping data structures and initializing the IoT client. 00101 * 00102 * @param pClient A new MQTT Client to be used as the protocol layer. Will be initialized with pParams. 00103 * @return An IoT Error Type defining successful/failed Initialization 00104 */ 00105 IoT_Error_t aws_iot_shadow_init(AWS_IoT_Client *pClient, ShadowInitParameters_t *pParams); 00106 00107 /** 00108 * @brief Connect to the AWS IoT Thing Shadow service over MQTT 00109 * 00110 * This function does the TLSv1.2 handshake and establishes the MQTT connection 00111 * 00112 * @param pClient MQTT Client used as the protocol layer 00113 * @param pParams Shadow Conenction parameters like TLS cert location 00114 * @return An IoT Error Type defining successful/failed Connection 00115 */ 00116 IoT_Error_t aws_iot_shadow_connect(AWS_IoT_Client *pClient, ShadowConnectParameters_t *pParams); 00117 00118 /** 00119 * @brief Yield function to let the background tasks of MQTT and Shadow 00120 * 00121 * This function could be use in a separate thread waiting for the incoming messages, ensuring the connection is kept alive with the AWS Service. 00122 * It also ensures the expired requests of Shadow actions are cleared and Timeout callback is executed. 00123 * @note All callbacks ever used in the SDK will be executed in the context of this function. 00124 * 00125 * @param pClient MQTT Client used as the protocol layer 00126 * @param timeout in milliseconds, This is the maximum time the yield function will wait for a message and/or read the messages from the TLS buffer 00127 * @return An IoT Error Type defining successful/failed Yield 00128 */ 00129 IoT_Error_t aws_iot_shadow_yield(AWS_IoT_Client *pClient, uint32_t timeout); 00130 00131 /** 00132 * @brief Disconnect from the AWS IoT Thing Shadow service over MQTT 00133 * 00134 * This will close the underlying TCP connection, MQTT connection will also be closed 00135 * 00136 * @param pClient MQTT Client used as the protocol layer 00137 * @return An IoT Error Type defining successful/failed disconnect status 00138 */ 00139 IoT_Error_t aws_iot_shadow_disconnect(AWS_IoT_Client *pClient); 00140 00141 /** 00142 * @brief Thing Shadow Acknowledgment enum 00143 * 00144 * This enum type is use in the callback for the action response 00145 * 00146 */ 00147 typedef enum { 00148 SHADOW_ACK_TIMEOUT, SHADOW_ACK_REJECTED, SHADOW_ACK_ACCEPTED 00149 } Shadow_Ack_Status_t; 00150 00151 /** 00152 * @brief Thing Shadow Action type enum 00153 * 00154 * This enum type is use in the callback for the action response 00155 * 00156 */ 00157 typedef enum { 00158 SHADOW_GET, SHADOW_UPDATE, SHADOW_DELETE 00159 } ShadowActions_t; 00160 00161 00162 /** 00163 * @brief Function Pointer typedef used as the callback for every action 00164 * 00165 * This function will be called from the context of \c aws_iot_shadow_yield() context 00166 * 00167 * @param pThingName Thing Name of the response received 00168 * @param action The response of the action 00169 * @param status Informs if the action was Accepted/Rejected or Timed out 00170 * @param pReceivedJsonDocument Received JSON document 00171 * @param pContextData the void* data passed in during the action call(update, get or delete) 00172 * 00173 */ 00174 typedef void (*fpActionCallback_t)(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status, 00175 const char *pReceivedJsonDocument, void *pContextData); 00176 00177 /** 00178 * @brief This function is the one used to perform an Update action to a Thing Name's Shadow. 00179 * 00180 * update is one of the most frequently used functionality by a device. In most cases the device may be just reporting few params to update the thing shadow in the cloud 00181 * Update Action if no callback or if the JSON document does not have a client token then will just publish the update and not track it. 00182 * 00183 * @note The update has to subscribe to two topics update/accepted and update/rejected. This function waits 2 seconds to ensure the subscriptions are registered before publishing the update message. 00184 * The following steps are performed on using this function: 00185 * 1. Subscribe to Shadow topics - $aws/things/{thingName}/shadow/update/accepted and $aws/things/{thingName}/shadow/update/rejected 00186 * 2. wait for 2 seconds for the subscription to take effect 00187 * 3. Publish on the update topic - $aws/things/{thingName}/shadow/update 00188 * 4. In the \c aws_iot_shadow_yield() function the response will be handled. In case of timeout or if the response is received, the subscription to shadow response topics are un-subscribed from. 00189 * On the contrary if the persistent subscription is set to true then the un-subscribe will not be done. The topics will always be listened to. 00190 * 00191 * @param pClient MQTT Client used as the protocol layer 00192 * @param pThingName Thing Name of the shadow that needs to be Updated 00193 * @param pJsonString The update action expects a JSON document to send. The JSON String should be a null terminated string. This JSON document should adhere to the AWS IoT Thing Shadow specification. To help in the process of creating this document- SDK provides apis in \c aws_iot_shadow_json_data.h 00194 * @param callback This is the callback that will be used to inform the caller of the response from the AWS IoT Shadow service.Callback could be set to NULL if response is not important 00195 * @param pContextData This is an extra parameter that could be passed along with the callback. It should be set to NULL if not used 00196 * @param timeout_seconds It is the time the SDK will wait for the response on either accepted/rejected before declaring timeout on the action 00197 * @param isPersistentSubscribe As mentioned above, every time if a device updates the same shadow then this should be set to true to avoid repeated subscription and unsubscription. If the Thing Name is one off update then this should be set to false 00198 * @return An IoT Error Type defining successful/failed update action 00199 */ 00200 IoT_Error_t aws_iot_shadow_update(AWS_IoT_Client *pClient, const char *pThingName, char *pJsonString, 00201 fpActionCallback_t callback, void *pContextData, uint8_t timeout_seconds, 00202 bool isPersistentSubscribe); 00203 00204 /** 00205 * @brief This function is the one used to perform an Get action to a Thing Name's Shadow. 00206 * 00207 * One use of this function is usually to get the config of a device at boot up. 00208 * It is similar to the Update function internally except it does not take a JSON document as the input. The entire JSON document will be sent over the accepted topic 00209 * 00210 * @param pClient MQTT Client used as the protocol layer 00211 * @param pThingName Thing Name of the JSON document that is needed 00212 * @param callback This is the callback that will be used to inform the caller of the response from the AWS IoT Shadow service.Callback could be set to NULL if response is not important 00213 * @param pContextData This is an extra parameter that could be passed along with the callback. It should be set to NULL if not used 00214 * @param timeout_seconds It is the time the SDK will wait for the response on either accepted/rejected before declaring timeout on the action 00215 * @param isPersistentSubscribe As mentioned above, every time if a device gets the same Sahdow (JSON document) then this should be set to true to avoid repeated subscription and un-subscription. If the Thing Name is one off get then this should be set to false 00216 * @return An IoT Error Type defining successful/failed get action 00217 */ 00218 IoT_Error_t aws_iot_shadow_get(AWS_IoT_Client *pClient, const char *pThingName, fpActionCallback_t callback, 00219 void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe); 00220 00221 /** 00222 * @brief This function is the one used to perform an Delete action to a Thing Name's Shadow. 00223 * 00224 * This is not a very common use case for device. It is generally the responsibility of the accompanying app to do the delete. 00225 * It is similar to the Update function internally except it does not take a JSON document as the input. The Thing Shadow referred by the ThingName will be deleted. 00226 * 00227 * @param pClient MQTT Client used as the protocol layer 00228 * @param pThingName Thing Name of the Shadow that should be deleted 00229 * @param callback This is the callback that will be used to inform the caller of the response from the AWS IoT Shadow service.Callback could be set to NULL if response is not important 00230 * @param pContextData This is an extra parameter that could be passed along with the callback. It should be set to NULL if not used 00231 * @param timeout_seconds It is the time the SDK will wait for the response on either accepted/rejected before declaring timeout on the action 00232 * @param isPersistentSubscribe As mentioned above, every time if a device deletes the same Shadow (JSON document) then this should be set to true to avoid repeated subscription and un-subscription. If the Thing Name is one off delete then this should be set to false 00233 * @return An IoT Error Type defining successful/failed delete action 00234 */ 00235 IoT_Error_t aws_iot_shadow_delete(AWS_IoT_Client *pClient, const char *pThingName, fpActionCallback_t callback, 00236 void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscriptions); 00237 00238 /** 00239 * @brief This function is used to listen on the delta topic of #AWS_IOT_MY_THING_NAME mentioned in the aws_iot_config.h file. 00240 * 00241 * Any time a delta is published the Json document will be delivered to the pStruct->cb. If you don't want the parsing done by the SDK then use the jsonStruct_t key set to "state". A good example of this is displayed in the sample_apps/shadow_console_echo.c 00242 * 00243 * @param pClient MQTT Client used as the protocol layer 00244 * @param pStruct The struct used to parse JSON value 00245 * @return An IoT Error Type defining successful/failed delta registering 00246 */ 00247 IoT_Error_t aws_iot_shadow_register_delta(AWS_IoT_Client *pClient, jsonStruct_t *pStruct); 00248 00249 /** 00250 * @brief Reset the last received version number to zero. 00251 * This will be useful if the Thing Shadow is deleted and would like to to reset the local version 00252 * @return no return values 00253 * 00254 */ 00255 void aws_iot_shadow_reset_last_received_version(void); 00256 00257 /** 00258 * @brief Version of a document is received with every accepted/rejected and the SDK keeps track of the last received version of the JSON document of #AWS_IOT_MY_THING_NAME shadow 00259 * 00260 * One exception to this version tracking is that, the SDK will ignore the version from update/accepted topic. Rest of the responses will be scanned to update the version number. 00261 * Accepting version change for update/accepted may cause version conflicts for delta message if the update message is received before the delta. 00262 * 00263 * @return version number of the last received response 00264 * 00265 */ 00266 uint32_t aws_iot_shadow_get_last_received_version(void); 00267 00268 /** 00269 * @brief Enable the ignoring of delta messages with old version number 00270 * 00271 * As we use MQTT underneath, there could be more than 1 of the same message if we use QoS 0. To avoid getting called for the same message, this functionality should be enabled. All the old message will be ignored 00272 */ 00273 void aws_iot_shadow_enable_discard_old_delta_msgs(void); 00274 00275 /** 00276 * @brief Disable the ignoring of delta messages with old version number 00277 */ 00278 void aws_iot_shadow_disable_discard_old_delta_msgs(void); 00279 00280 /** 00281 * @brief This function is used to enable or disable autoreconnect 00282 * 00283 * Any time a disconnect happens the underlying MQTT client attempts to reconnect if this is set to true 00284 * 00285 * @param pClient MQTT Client used as the protocol layer 00286 * @param newStatus The new status to set the autoreconnect option to 00287 * 00288 * @return An IoT Error Type defining successful/failed operation 00289 */ 00290 IoT_Error_t aws_iot_shadow_set_autoreconnect_status(AWS_IoT_Client *pClient, bool newStatus); 00291 00292 #ifdef __cplusplus 00293 } 00294 #endif 00295 00296 #endif //AWS_IOT_SDK_SRC_IOT_SHADOW_H_
Generated on Tue Jul 12 2022 20:38:50 by 1.7.2