Simple sample that demonstrates reading the FXOS8700CQ accelerometer, convert the data to JSON and send to an Azure IoT Hub.
Dependencies: azure_umqtt_c iothub_mqtt_transport mbed-rtos mbed wolfSSL Socket lwip-eth lwip-sys lwip
iothub_client.h
00001 // Copyright (c) Microsoft. All rights reserved. 00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 00003 00004 /** @file iothub_client.h 00005 * @brief Extends the IoTHubCLient_LL module with additional features. 00006 * 00007 * @details IoTHubClient is a module that extends the IoTHubCLient_LL 00008 * module with 2 features: 00009 * - scheduling the work for the IoTHubCLient from a 00010 * thread, so that the user does not need to create their 00011 * own thread 00012 * - thread-safe APIs 00013 */ 00014 00015 #ifndef IOTHUB_CLIENT_H 00016 #define IOTHUB_CLIENT_H 00017 00018 typedef struct IOTHUB_CLIENT_INSTANCE_TAG* IOTHUB_CLIENT_HANDLE; 00019 00020 00021 #include "iothubtransport.h" 00022 #include <stddef.h> 00023 #include <stdint.h> 00024 00025 #include "iothub_client_ll.h" 00026 #include "azure_c_shared_utility/umock_c_prod.h" 00027 00028 #ifdef __cplusplus 00029 extern "C" 00030 { 00031 #endif 00032 00033 #define IOTHUB_CLIENT_FILE_UPLOAD_RESULT_VALUES \ 00034 FILE_UPLOAD_OK ,\ 00035 FILE_UPLOAD_ERROR 00036 00037 DEFINE_ENUM(IOTHUB_CLIENT_FILE_UPLOAD_RESULT, IOTHUB_CLIENT_FILE_UPLOAD_RESULT_VALUES) 00038 typedef void(*IOTHUB_CLIENT_FILE_UPLOAD_CALLBACK)(IOTHUB_CLIENT_FILE_UPLOAD_RESULT result, void* userContextCallback); 00039 00040 /** 00041 * @brief Creates a IoT Hub client for communication with an existing 00042 * IoT Hub using the specified connection string parameter. 00043 * 00044 * @param connectionString Pointer to a character string 00045 * @param protocol Function pointer for protocol implementation 00046 * 00047 * Sample connection string: 00048 * <blockquote> 00049 * <pre>HostName=[IoT Hub name goes here].[IoT Hub suffix goes here, e.g., private.azure-devices-int.net];DeviceId=[Device ID goes here];SharedAccessKey=[Device key goes here];</pre> 00050 * <pre>HostName=[IoT Hub name goes here].[IoT Hub suffix goes here, e.g., private.azure-devices-int.net];DeviceId=[Device ID goes here];SharedAccessSignature=SharedAccessSignature sr=[IoT Hub name goes here].[IoT Hub suffix goes here, e.g., private.azure-devices-int.net]/devices/[Device ID goes here]&sig=[SAS Token goes here]&se=[Expiry Time goes here];</pre> 00051 * </blockquote> 00052 * 00053 * @return A non-NULL @c IOTHUB_CLIENT_HANDLE value that is used when 00054 * invoking other functions for IoT Hub client and @c NULL on failure. 00055 */ 00056 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_HANDLE, IoTHubClient_CreateFromConnectionString, const char*, connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER, protocol); 00057 00058 /** 00059 * @brief Creates a IoT Hub client for communication with an existing IoT 00060 * Hub using the specified parameters. 00061 * 00062 * @param config Pointer to an @c IOTHUB_CLIENT_CONFIG structure 00063 * 00064 * The API does not allow sharing of a connection across multiple 00065 * devices. This is a blocking call. 00066 * 00067 * @return A non-NULL @c IOTHUB_CLIENT_HANDLE value that is used when 00068 * invoking other functions for IoT Hub client and @c NULL on failure. 00069 */ 00070 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_HANDLE, IoTHubClient_Create, const IOTHUB_CLIENT_CONFIG*, config); 00071 00072 /** 00073 * @brief Creates a IoT Hub client for communication with an existing IoT 00074 * Hub using the specified parameters. 00075 * 00076 * @param transportHandle TRANSPORT_HANDLE which represents a connection. 00077 * @param config Pointer to an @c IOTHUB_CLIENT_CONFIG structure 00078 * 00079 * The API allows sharing of a connection across multiple 00080 * devices. This is a blocking call. 00081 * 00082 * @return A non-NULL @c IOTHUB_CLIENT_HANDLE value that is used when 00083 * invoking other functions for IoT Hub client and @c NULL on failure. 00084 */ 00085 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_HANDLE, IoTHubClient_CreateWithTransport, TRANSPORT_HANDLE, transportHandle, const IOTHUB_CLIENT_CONFIG*, config); 00086 00087 /** 00088 * @brief Disposes of resources allocated by the IoT Hub client. This is a 00089 * blocking call. 00090 * 00091 * @param iotHubClientHandle The handle created by a call to the create function. 00092 */ 00093 MOCKABLE_FUNCTION(, void, IoTHubClient_Destroy, IOTHUB_CLIENT_HANDLE, iotHubClientHandle); 00094 00095 /** 00096 * @brief Asynchronous call to send the message specified by @p eventMessageHandle. 00097 * 00098 * @param iotHubClientHandle The handle created by a call to the create function. 00099 * @param eventMessageHandle The handle to an IoT Hub message. 00100 * @param eventConfirmationCallback The callback specified by the device for receiving 00101 * confirmation of the delivery of the IoT Hub message. 00102 * This callback can be expected to invoke the 00103 * ::IoTHubClient_SendEventAsync function for the 00104 * same message in an attempt to retry sending a failing 00105 * message. The user can specify a @c NULL value here to 00106 * indicate that no callback is required. 00107 * @param userContextCallback User specified context that will be provided to the 00108 * callback. This can be @c NULL. 00109 * 00110 * @b NOTE: The application behavior is undefined if the user calls 00111 * the ::IoTHubClient_Destroy function from within any callback. 00112 * 00113 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00114 */ 00115 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_SendEventAsync, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, IOTHUB_MESSAGE_HANDLE, eventMessageHandle, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK, eventConfirmationCallback, void*, userContextCallback); 00116 00117 /** 00118 * @brief This function returns the current sending status for IoTHubClient. 00119 * 00120 * @param iotHubClientHandle The handle created by a call to the create function. 00121 * @param iotHubClientStatus The sending state is populated at the address pointed 00122 * at by this parameter. The value will be set to 00123 * @c IOTHUBCLIENT_SENDSTATUS_IDLE if there is currently 00124 * no item to be sent and @c IOTHUBCLIENT_SENDSTATUS_BUSY 00125 * if there are. 00126 * 00127 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00128 */ 00129 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_GetSendStatus, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, IOTHUB_CLIENT_STATUS*, iotHubClientStatus); 00130 00131 /** 00132 * @brief Sets up the message callback to be invoked when IoT Hub issues a 00133 * message to the device. This is a blocking call. 00134 * 00135 * @param iotHubClientHandle The handle created by a call to the create function. 00136 * @param messageCallback The callback specified by the device for receiving 00137 * messages from IoT Hub. 00138 * @param userContextCallback User specified context that will be provided to the 00139 * callback. This can be @c NULL. 00140 * 00141 * @b NOTE: The application behavior is undefined if the user calls 00142 * the ::IoTHubClient_Destroy function from within any callback. 00143 * 00144 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00145 */ 00146 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_SetMessageCallback, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC, messageCallback, void*, userContextCallback); 00147 00148 /** 00149 * @brief Sets up the connection status callback to be invoked representing the status of 00150 * the connection to IOT Hub. This is a blocking call. 00151 * 00152 * @param iotHubClientHandle The handle created by a call to the create function. 00153 * @param connectionStatusCallback The callback specified by the device for receiving 00154 * updates about the status of the connection to IoT Hub. 00155 * @param userContextCallback User specified context that will be provided to the 00156 * callback. This can be @c NULL. 00157 * 00158 * @b NOTE: The application behavior is undefined if the user calls 00159 * the ::IoTHubClient_LL_Destroy function from within any callback. 00160 * 00161 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00162 */ 00163 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_SetConnectionStatusCallback, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK, connectionStatusCallback, void*, userContextCallback); 00164 00165 /** 00166 * @brief Sets up the connection status callback to be invoked representing the status of 00167 * the connection to IOT Hub. This is a blocking call. 00168 * 00169 * @param iotHubClientHandle The handle created by a call to the create function. 00170 * @param retryPolicy The policy to use to reconnect to IoT Hub when a 00171 * connection drops. 00172 * @param retryTimeoutLimitInSeconds Maximum amount of time(seconds) to attempt reconnection when a 00173 * connection drops to IOT Hub. 00174 * 00175 * @b NOTE: The application behavior is undefined if the user calls 00176 * the ::IoTHubClient_LL_Destroy function from within any callback. 00177 * 00178 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00179 */ 00180 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_SetRetryPolicy, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY, retryPolicy, size_t, retryTimeoutLimitInSeconds); 00181 00182 /** 00183 * @brief Sets up the connection status callback to be invoked representing the status of 00184 * the connection to IOT Hub. This is a blocking call. 00185 * 00186 * @param iotHubClientHandle The handle created by a call to the create function. 00187 * @param retryPolicy Out parameter containing the policy to use to reconnect to IoT Hub. 00188 * @param retryTimeoutLimitInSeconds Out parameter containing maximum amount of time in seconds to attempt reconnection 00189 to IOT Hub. 00190 * 00191 * @b NOTE: The application behavior is undefined if the user calls 00192 * the ::IoTHubClient_LL_Destroy function from within any callback. 00193 * 00194 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00195 */ 00196 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_GetRetryPolicy, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY*, retryPolicy, size_t*, retryTimeoutLimitInSeconds); 00197 00198 /** 00199 * @brief This function returns in the out parameter @p lastMessageReceiveTime 00200 * what was the value of the @c time function when the last message was 00201 * received at the client. 00202 * 00203 * @param iotHubClientHandle The handle created by a call to the create function. 00204 * @param lastMessageReceiveTime Out parameter containing the value of @c time function 00205 * when the last message was received. 00206 * 00207 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00208 */ 00209 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_GetLastMessageReceiveTime, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, time_t*, lastMessageReceiveTime); 00210 00211 /** 00212 * @brief This API sets a runtime option identified by parameter @p optionName 00213 * to a value pointed to by @p value. @p optionName and the data type 00214 * @p value is pointing to are specific for every option. 00215 * 00216 * @param iotHubClientHandle The handle created by a call to the create function. 00217 * @param optionName Name of the option. 00218 * @param value The value. 00219 * 00220 * The options that can be set via this API are: 00221 * - @b timeout - the maximum time in milliseconds a communication is 00222 * allowed to use. @p value is a pointer to an @c unsigned @c int with 00223 * the timeout value in milliseconds. This is only supported for the HTTP 00224 * protocol as of now. When the HTTP protocol uses CURL, the meaning of 00225 * the parameter is <em>total request time</em>. When the HTTP protocol uses 00226 * winhttp, the meaning is the same as the @c dwSendTimeout and 00227 * @c dwReceiveTimeout parameters of the 00228 * <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa384116(v=vs.85).aspx"> 00229 * WinHttpSetTimeouts</a> API. 00230 * - @b CURLOPT_LOW_SPEED_LIMIT - only available for HTTP protocol and only 00231 * when CURL is used. It has the same meaning as CURL's option with the same 00232 * name. @p value is pointer to a long. 00233 * - @b CURLOPT_LOW_SPEED_TIME - only available for HTTP protocol and only 00234 * when CURL is used. It has the same meaning as CURL's option with the same 00235 * name. @p value is pointer to a long. 00236 * - @b CURLOPT_FORBID_REUSE - only available for HTTP protocol and only 00237 * when CURL is used. It has the same meaning as CURL's option with the same 00238 * name. @p value is pointer to a long. 00239 * - @b CURLOPT_FRESH_CONNECT - only available for HTTP protocol and only 00240 * when CURL is used. It has the same meaning as CURL's option with the same 00241 * name. @p value is pointer to a long. 00242 * - @b CURLOPT_VERBOSE - only available for HTTP protocol and only 00243 * when CURL is used. It has the same meaning as CURL's option with the same 00244 * name. @p value is pointer to a long. 00245 * - @b messageTimeout - the maximum time in milliseconds until a message 00246 * is timeouted. The time starts at IoTHubClient_SendEventAsync. By default, 00247 * messages do not expire. @p is a pointer to a uint64_t 00248 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00249 */ 00250 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_SetOption, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, const char*, optionName, const void*, value); 00251 00252 /** 00253 * @brief This API specifies a call back to be used when the device receives a state update. 00254 * 00255 * @param iotHubClientHandle The handle created by a call to the create function. 00256 * @param deviceTwinCallback The callback specified by the device client to be used for updating 00257 * the desired state. The callback will be called in response to a 00258 * request send by the IoTHub services. The payload will be passed to the 00259 * callback, along with two version numbers: 00260 * - Desired: 00261 * - LastSeenReported: 00262 * @param userContextCallback User specified context that will be provided to the 00263 * callback. This can be @c NULL. 00264 * 00265 * @b NOTE: The application behavior is undefined if the user calls 00266 * the ::IoTHubClient_Destroy function from within any callback. 00267 * 00268 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00269 */ 00270 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_SetDeviceTwinCallback, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK, deviceTwinCallback, void*, userContextCallback); 00271 00272 /** 00273 * @brief This API sends a report of the device's properties and their current values. 00274 * 00275 * @param iotHubClientHandle The handle created by a call to the create function. 00276 * @param reportedState The current device property values to be 'reported' to the IoTHub. 00277 * @param reportedStateCallback The callback specified by the device client to be called with the 00278 * result of the transaction. 00279 * @param userContextCallback User specified context that will be provided to the 00280 * callback. This can be @c NULL. 00281 * 00282 * @b NOTE: The application behavior is undefined if the user calls 00283 * the ::IoTHubClient_Destroy function from within any callback. 00284 * 00285 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00286 */ 00287 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_SendReportedState, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, const unsigned char*, reportedState, size_t, size, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK, reportedStateCallback, void*, userContextCallback); 00288 00289 /** 00290 * @brief This API sets callback for cloud to device method call. 00291 * 00292 * @param iotHubClientHandle The handle created by a call to the create function. 00293 * @param deviceMethodCallback The callback which will be called by IoTHub. 00294 * @param userContextCallback User specified context that will be provided to the 00295 * callback. This can be @c NULL. 00296 * 00297 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00298 */ 00299 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_SetDeviceMethodCallback, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC, deviceMethodCallback, void*, userContextCallback); 00300 00301 #ifndef DONT_USE_UPLOADTOBLOB 00302 /** 00303 * @brief IoTHubClient_UploadToBlobAsync uploads data from memory to a file in Azure Blob Storage. 00304 * 00305 * @param iotHubClientHandle The handle created by a call to the IoTHubClient_Create function. 00306 * @param destinationFileName The name of the file to be created in Azure Blob Storage. 00307 * @param source The source of data. 00308 * @param size The size of data. 00309 * @param iotHubClientFileUploadCallback A callback to be invoked when the file upload operation has finished. 00310 * @param context A user-provided context to be passed to the file upload callback. 00311 * 00312 * @return IOTHUB_CLIENT_OK upon success or an error code upon failure. 00313 */ 00314 MOCKABLE_FUNCTION(, IOTHUB_CLIENT_RESULT, IoTHubClient_UploadToBlobAsync, IOTHUB_CLIENT_HANDLE, iotHubClientHandle, const char*, destinationFileName, const unsigned char*, source, size_t, size, IOTHUB_CLIENT_FILE_UPLOAD_CALLBACK, iotHubClientFileUploadCallback, void*, context); 00315 #endif 00316 #ifdef __cplusplus 00317 } 00318 #endif 00319 00320 #endif /* IOTHUB_CLIENT_H */
Generated on Wed Jul 13 2022 07:19:04 by 1.7.2