Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

This library implements the Microsoft Azure IoTHub client library. The code is replicated from https://github.com/Azure/azure-iot-sdks

Committer:
AzureIoTClient
Date:
Mon May 08 10:50:31 2017 -0700
Revision:
66:a419827cb051
Parent:
63:1bf1c2d60aab
Child:
70:875388a7d419
1.1.14

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 16:deba40344375 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 16:deba40344375 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 16:deba40344375 3
AzureIoTClient 16:deba40344375 4 #include <stdlib.h>
Azure.IoT Build 38:a05929a75111 5 #include <string.h>
AzureIoTClient 60:41648c4e7036 6 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 38:a05929a75111 7 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 38:a05929a75111 8 #include "azure_c_shared_utility/string_tokenizer.h"
Azure.IoT Build 38:a05929a75111 9 #include "azure_c_shared_utility/doublylinkedlist.h"
Azure.IoT Build 45:54c11b1b1407 10 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 38:a05929a75111 11 #include "azure_c_shared_utility/tickcounter.h"
AzureIoTClient 53:1e5a1ca1f274 12 #include "azure_c_shared_utility/constbuffer.h"
AzureIoTClient 66:a419827cb051 13 #include "azure_c_shared_utility/platform.h"
AzureIoTClient 16:deba40344375 14
AzureIoTClient 62:5a4cdacf5090 15 #include "iothub_client_authorization.h"
AzureIoTClient 16:deba40344375 16 #include "iothub_client_ll.h"
AzureIoTClient 61:8b85a4e797cf 17 #include "iothub_transport_ll.h"
AzureIoTClient 16:deba40344375 18 #include "iothub_client_private.h"
AzureIoTClient 66:a419827cb051 19 #include "iothub_client_options.h"
AzureIoTClient 33:b372b0efcd20 20 #include "iothub_client_version.h"
AzureIoTClient 53:1e5a1ca1f274 21 #include <stdint.h>
AzureIoTClient 43:038d8511e817 22
AzureIoTClient 44:33dd78697616 23 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 24 #include "iothub_client_ll_uploadtoblob.h"
AzureIoTClient 43:038d8511e817 25 #endif
Azure.IoT Build 36:67300d5a4c1f 26
Azure.IoT Build 45:54c11b1b1407 27 #define LOG_ERROR_RESULT LogError("result = %s", ENUM_TO_STRING(IOTHUB_CLIENT_RESULT, result));
AzureIoTClient 16:deba40344375 28 #define INDEFINITE_TIME ((time_t)(-1))
AzureIoTClient 16:deba40344375 29
AzureIoTClient 16:deba40344375 30 DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_RESULT_VALUES);
Azure.IoT Build 45:54c11b1b1407 31 DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES);
AzureIoTClient 16:deba40344375 32
AzureIoTClient 62:5a4cdacf5090 33 #define CALLBACK_TYPE_VALUES \
AzureIoTClient 62:5a4cdacf5090 34 CALLBACK_TYPE_NONE, \
AzureIoTClient 62:5a4cdacf5090 35 CALLBACK_TYPE_SYNC, \
AzureIoTClient 62:5a4cdacf5090 36 CALLBACK_TYPE_ASYNC
AzureIoTClient 62:5a4cdacf5090 37
AzureIoTClient 62:5a4cdacf5090 38 DEFINE_ENUM(CALLBACK_TYPE, CALLBACK_TYPE_VALUES)
AzureIoTClient 62:5a4cdacf5090 39 DEFINE_ENUM_STRINGS(CALLBACK_TYPE, CALLBACK_TYPE_VALUES)
AzureIoTClient 61:8b85a4e797cf 40
AzureIoTClient 62:5a4cdacf5090 41 typedef struct IOTHUB_METHOD_CALLBACK_DATA_TAG
AzureIoTClient 62:5a4cdacf5090 42 {
AzureIoTClient 62:5a4cdacf5090 43 CALLBACK_TYPE type;
AzureIoTClient 62:5a4cdacf5090 44 IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC callbackSync;
AzureIoTClient 62:5a4cdacf5090 45 IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK callbackAsync;
AzureIoTClient 62:5a4cdacf5090 46 void* userContextCallback;
AzureIoTClient 62:5a4cdacf5090 47 }IOTHUB_METHOD_CALLBACK_DATA;
AzureIoTClient 61:8b85a4e797cf 48
AzureIoTClient 61:8b85a4e797cf 49 typedef struct IOTHUB_MESSAGE_CALLBACK_DATA_TAG
AzureIoTClient 61:8b85a4e797cf 50 {
AzureIoTClient 62:5a4cdacf5090 51 CALLBACK_TYPE type;
AzureIoTClient 61:8b85a4e797cf 52 IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC callbackSync;
AzureIoTClient 61:8b85a4e797cf 53 IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC_EX callbackAsync;
AzureIoTClient 62:5a4cdacf5090 54 void* userContextCallback;
AzureIoTClient 61:8b85a4e797cf 55 }IOTHUB_MESSAGE_CALLBACK_DATA;
AzureIoTClient 61:8b85a4e797cf 56
AzureIoTClient 16:deba40344375 57 typedef struct IOTHUB_CLIENT_LL_HANDLE_DATA_TAG
AzureIoTClient 16:deba40344375 58 {
AzureIoTClient 42:448eecc3676e 59 DLIST_ENTRY waitingToSend;
AzureIoTClient 53:1e5a1ca1f274 60 DLIST_ENTRY iot_msg_queue;
AzureIoTClient 53:1e5a1ca1f274 61 DLIST_ENTRY iot_ack_queue;
AzureIoTClient 42:448eecc3676e 62 TRANSPORT_LL_HANDLE transportHandle;
AzureIoTClient 42:448eecc3676e 63 bool isSharedTransport;
AzureIoTClient 42:448eecc3676e 64 IOTHUB_DEVICE_HANDLE deviceHandle;
AzureIoTClient 42:448eecc3676e 65 TRANSPORT_PROVIDER_FIELDS;
AzureIoTClient 61:8b85a4e797cf 66 IOTHUB_MESSAGE_CALLBACK_DATA messageCallback;
AzureIoTClient 62:5a4cdacf5090 67 IOTHUB_METHOD_CALLBACK_DATA methodCallback;
AzureIoTClient 53:1e5a1ca1f274 68 IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK conStatusCallback;
AzureIoTClient 53:1e5a1ca1f274 69 void* conStatusUserContextCallback;
AzureIoTClient 42:448eecc3676e 70 time_t lastMessageReceiveTime;
AzureIoTClient 42:448eecc3676e 71 TICK_COUNTER_HANDLE tickCounter; /*shared tickcounter used to track message timeouts in waitingToSend list*/
Azure.IoT.Build 54:6dcad9019a64 72 tickcounter_ms_t currentMessageTimeout;
AzureIoTClient 53:1e5a1ca1f274 73 uint64_t current_device_twin_timeout;
AzureIoTClient 53:1e5a1ca1f274 74 IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback;
AzureIoTClient 53:1e5a1ca1f274 75 void* deviceTwinContextCallback;
AzureIoTClient 52:1cc3c6d07cad 76 IOTHUB_CLIENT_RETRY_POLICY retryPolicy;
AzureIoTClient 53:1e5a1ca1f274 77 size_t retryTimeoutLimitInSeconds;
AzureIoTClient 44:33dd78697616 78 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 79 IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE uploadToBlobHandle;
AzureIoTClient 43:038d8511e817 80 #endif
AzureIoTClient 53:1e5a1ca1f274 81 uint32_t data_msg_id;
AzureIoTClient 53:1e5a1ca1f274 82 bool complete_twin_update_encountered;
AzureIoTClient 62:5a4cdacf5090 83 IOTHUB_AUTHORIZATION_HANDLE authorization_module;
AzureIoTClient 66:a419827cb051 84 STRING_HANDLE product_info;
AzureIoTClient 16:deba40344375 85 }IOTHUB_CLIENT_LL_HANDLE_DATA;
AzureIoTClient 16:deba40344375 86
AzureIoTClient 16:deba40344375 87 static const char HOSTNAME_TOKEN[] = "HostName";
AzureIoTClient 16:deba40344375 88 static const char DEVICEID_TOKEN[] = "DeviceId";
Azure.IoT Build 45:54c11b1b1407 89 static const char X509_TOKEN[] = "x509";
Azure.IoT Build 45:54c11b1b1407 90 static const char X509_TOKEN_ONLY_ACCEPTABLE_VALUE[] = "true";
AzureIoTClient 16:deba40344375 91 static const char DEVICEKEY_TOKEN[] = "SharedAccessKey";
AzureIoTClient 40:1a94db9139ea 92 static const char DEVICESAS_TOKEN[] = "SharedAccessSignature";
AzureIoTClient 16:deba40344375 93 static const char PROTOCOL_GATEWAY_HOST[] = "GatewayHostName";
AzureIoTClient 16:deba40344375 94
AzureIoTClient 62:5a4cdacf5090 95 static void setTransportProtocol(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData, TRANSPORT_PROVIDER* protocol)
AzureIoTClient 62:5a4cdacf5090 96 {
AzureIoTClient 62:5a4cdacf5090 97 handleData->IoTHubTransport_SendMessageDisposition = protocol->IoTHubTransport_SendMessageDisposition;
AzureIoTClient 62:5a4cdacf5090 98 handleData->IoTHubTransport_GetHostname = protocol->IoTHubTransport_GetHostname;
AzureIoTClient 62:5a4cdacf5090 99 handleData->IoTHubTransport_SetOption = protocol->IoTHubTransport_SetOption;
AzureIoTClient 62:5a4cdacf5090 100 handleData->IoTHubTransport_Create = protocol->IoTHubTransport_Create;
AzureIoTClient 62:5a4cdacf5090 101 handleData->IoTHubTransport_Destroy = protocol->IoTHubTransport_Destroy;
AzureIoTClient 62:5a4cdacf5090 102 handleData->IoTHubTransport_Register = protocol->IoTHubTransport_Register;
AzureIoTClient 62:5a4cdacf5090 103 handleData->IoTHubTransport_Unregister = protocol->IoTHubTransport_Unregister;
AzureIoTClient 62:5a4cdacf5090 104 handleData->IoTHubTransport_Subscribe = protocol->IoTHubTransport_Subscribe;
AzureIoTClient 62:5a4cdacf5090 105 handleData->IoTHubTransport_Unsubscribe = protocol->IoTHubTransport_Unsubscribe;
AzureIoTClient 62:5a4cdacf5090 106 handleData->IoTHubTransport_DoWork = protocol->IoTHubTransport_DoWork;
AzureIoTClient 62:5a4cdacf5090 107 handleData->IoTHubTransport_SetRetryPolicy = protocol->IoTHubTransport_SetRetryPolicy;
AzureIoTClient 62:5a4cdacf5090 108 handleData->IoTHubTransport_GetSendStatus = protocol->IoTHubTransport_GetSendStatus;
AzureIoTClient 62:5a4cdacf5090 109 handleData->IoTHubTransport_ProcessItem = protocol->IoTHubTransport_ProcessItem;
AzureIoTClient 62:5a4cdacf5090 110 handleData->IoTHubTransport_Subscribe_DeviceTwin = protocol->IoTHubTransport_Subscribe_DeviceTwin;
AzureIoTClient 62:5a4cdacf5090 111 handleData->IoTHubTransport_Unsubscribe_DeviceTwin = protocol->IoTHubTransport_Unsubscribe_DeviceTwin;
AzureIoTClient 62:5a4cdacf5090 112 handleData->IoTHubTransport_Subscribe_DeviceMethod = protocol->IoTHubTransport_Subscribe_DeviceMethod;
AzureIoTClient 62:5a4cdacf5090 113 handleData->IoTHubTransport_Unsubscribe_DeviceMethod = protocol->IoTHubTransport_Unsubscribe_DeviceMethod;
AzureIoTClient 62:5a4cdacf5090 114 handleData->IoTHubTransport_DeviceMethod_Response = protocol->IoTHubTransport_DeviceMethod_Response;
AzureIoTClient 62:5a4cdacf5090 115 }
AzureIoTClient 62:5a4cdacf5090 116
AzureIoTClient 53:1e5a1ca1f274 117 static void device_twin_data_destroy(IOTHUB_DEVICE_TWIN* client_item)
AzureIoTClient 53:1e5a1ca1f274 118 {
AzureIoTClient 53:1e5a1ca1f274 119 CONSTBUFFER_Destroy(client_item->report_data_handle);
AzureIoTClient 53:1e5a1ca1f274 120 free(client_item);
AzureIoTClient 53:1e5a1ca1f274 121 }
AzureIoTClient 53:1e5a1ca1f274 122
AzureIoTClient 62:5a4cdacf5090 123 static int create_blob_upload_module(IOTHUB_CLIENT_LL_HANDLE_DATA* handle_data, const IOTHUB_CLIENT_CONFIG* config)
AzureIoTClient 62:5a4cdacf5090 124 {
AzureIoTClient 62:5a4cdacf5090 125 int result;
AzureIoTClient 63:1bf1c2d60aab 126 (void)handle_data;
AzureIoTClient 63:1bf1c2d60aab 127 (void)config;
AzureIoTClient 62:5a4cdacf5090 128 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 62:5a4cdacf5090 129 handle_data->uploadToBlobHandle = IoTHubClient_LL_UploadToBlob_Create(config);
AzureIoTClient 62:5a4cdacf5090 130 if (handle_data->uploadToBlobHandle == NULL)
AzureIoTClient 62:5a4cdacf5090 131 {
AzureIoTClient 62:5a4cdacf5090 132 LogError("unable to IoTHubClient_LL_UploadToBlob_Create");
AzureIoTClient 62:5a4cdacf5090 133 result = __FAILURE__;
AzureIoTClient 62:5a4cdacf5090 134 }
AzureIoTClient 62:5a4cdacf5090 135 else
AzureIoTClient 62:5a4cdacf5090 136 {
AzureIoTClient 62:5a4cdacf5090 137 result = 0;
AzureIoTClient 62:5a4cdacf5090 138 }
AzureIoTClient 62:5a4cdacf5090 139 #else
AzureIoTClient 62:5a4cdacf5090 140 result = 0;
AzureIoTClient 62:5a4cdacf5090 141 #endif
AzureIoTClient 62:5a4cdacf5090 142 return result;
AzureIoTClient 62:5a4cdacf5090 143 }
AzureIoTClient 62:5a4cdacf5090 144
AzureIoTClient 62:5a4cdacf5090 145 static void destroy_blob_upload_module(IOTHUB_CLIENT_LL_HANDLE_DATA* handle_data)
AzureIoTClient 62:5a4cdacf5090 146 {
AzureIoTClient 63:1bf1c2d60aab 147 (void)handle_data;
AzureIoTClient 62:5a4cdacf5090 148 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 62:5a4cdacf5090 149 /*Codes_SRS_IOTHUBCLIENT_LL_02_046: [ If creating the TICK_COUNTER_HANDLE fails then IoTHubClient_LL_Create shall fail and return NULL. ]*/
AzureIoTClient 62:5a4cdacf5090 150 IoTHubClient_LL_UploadToBlob_Destroy(handle_data->uploadToBlobHandle);
AzureIoTClient 62:5a4cdacf5090 151 #endif
AzureIoTClient 62:5a4cdacf5090 152 }
AzureIoTClient 62:5a4cdacf5090 153
AzureIoTClient 66:a419827cb051 154 /*Codes_SRS_IOTHUBCLIENT_LL_10_032: ["product_info" - takes a char string as an argument to specify the product information(e.g. `"ProductName/ProductVersion"`). ]*/
AzureIoTClient 66:a419827cb051 155 /*Codes_SRS_IOTHUBCLIENT_LL_10_034: ["product_info" - shall store the given string concatenated with the sdk information and the platform information in the form(ProductInfo DeviceSDKName / DeviceSDKVersion(OSName OSVersion; Architecture). ]*/
AzureIoTClient 66:a419827cb051 156 static STRING_HANDLE make_product_info(const char* product)
AzureIoTClient 62:5a4cdacf5090 157 {
AzureIoTClient 66:a419827cb051 158 STRING_HANDLE result;
AzureIoTClient 66:a419827cb051 159 STRING_HANDLE pfi = platform_get_platform_info();
AzureIoTClient 66:a419827cb051 160 if (pfi == NULL)
AzureIoTClient 62:5a4cdacf5090 161 {
AzureIoTClient 66:a419827cb051 162 result = NULL;
AzureIoTClient 62:5a4cdacf5090 163 }
AzureIoTClient 62:5a4cdacf5090 164 else
AzureIoTClient 62:5a4cdacf5090 165 {
AzureIoTClient 66:a419827cb051 166 if (product == NULL)
AzureIoTClient 66:a419827cb051 167 {
AzureIoTClient 66:a419827cb051 168 result = STRING_construct_sprintf("%s %s", CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION, STRING_c_str(pfi));
AzureIoTClient 66:a419827cb051 169 }
AzureIoTClient 66:a419827cb051 170 else
AzureIoTClient 66:a419827cb051 171 {
AzureIoTClient 66:a419827cb051 172 result = STRING_construct_sprintf("%s %s %s", product, CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION, STRING_c_str(pfi));
AzureIoTClient 66:a419827cb051 173 }
AzureIoTClient 66:a419827cb051 174 STRING_delete(pfi);
AzureIoTClient 66:a419827cb051 175 }
AzureIoTClient 66:a419827cb051 176 return result;
AzureIoTClient 66:a419827cb051 177 }
AzureIoTClient 62:5a4cdacf5090 178
AzureIoTClient 66:a419827cb051 179 static IOTHUB_CLIENT_LL_HANDLE_DATA* initialize_iothub_client(const IOTHUB_CLIENT_CONFIG* client_config, const IOTHUB_CLIENT_DEVICE_CONFIG* device_config)
AzureIoTClient 66:a419827cb051 180 {
AzureIoTClient 66:a419827cb051 181 IOTHUB_CLIENT_LL_HANDLE_DATA* result;
AzureIoTClient 66:a419827cb051 182 STRING_HANDLE product_info = make_product_info(NULL);
AzureIoTClient 66:a419827cb051 183 if (product_info == NULL)
AzureIoTClient 66:a419827cb051 184 {
AzureIoTClient 66:a419827cb051 185 LogError("failed to initialize product info");
AzureIoTClient 66:a419827cb051 186 result = NULL;
AzureIoTClient 66:a419827cb051 187 }
AzureIoTClient 66:a419827cb051 188 else
AzureIoTClient 66:a419827cb051 189 {
AzureIoTClient 66:a419827cb051 190 result = (IOTHUB_CLIENT_LL_HANDLE_DATA*)malloc(sizeof(IOTHUB_CLIENT_LL_HANDLE_DATA));
AzureIoTClient 66:a419827cb051 191 if (result == NULL)
AzureIoTClient 63:1bf1c2d60aab 192 {
AzureIoTClient 66:a419827cb051 193 LogError("failure allocating IOTHUB_CLIENT_LL_HANDLE_DATA");
AzureIoTClient 66:a419827cb051 194 STRING_delete(product_info);
AzureIoTClient 63:1bf1c2d60aab 195 }
AzureIoTClient 63:1bf1c2d60aab 196 else
AzureIoTClient 63:1bf1c2d60aab 197 {
AzureIoTClient 66:a419827cb051 198 IOTHUB_CLIENT_CONFIG actual_config;
AzureIoTClient 66:a419827cb051 199 const IOTHUB_CLIENT_CONFIG* config = NULL;
AzureIoTClient 66:a419827cb051 200 char* IoTHubName = NULL;
AzureIoTClient 66:a419827cb051 201 STRING_HANDLE transport_hostname = NULL;
AzureIoTClient 66:a419827cb051 202
AzureIoTClient 66:a419827cb051 203 memset(result, 0, sizeof(IOTHUB_CLIENT_LL_HANDLE_DATA));
AzureIoTClient 63:1bf1c2d60aab 204
AzureIoTClient 66:a419827cb051 205 const char* device_key;
AzureIoTClient 66:a419827cb051 206 const char* device_id;
AzureIoTClient 66:a419827cb051 207 const char* sas_token;
AzureIoTClient 62:5a4cdacf5090 208
AzureIoTClient 66:a419827cb051 209 if (device_config == NULL)
AzureIoTClient 62:5a4cdacf5090 210 {
AzureIoTClient 66:a419827cb051 211 device_key = client_config->deviceKey;
AzureIoTClient 66:a419827cb051 212 device_id = client_config->deviceId;
AzureIoTClient 66:a419827cb051 213 sas_token = client_config->deviceSasToken;
AzureIoTClient 62:5a4cdacf5090 214 }
AzureIoTClient 62:5a4cdacf5090 215 else
AzureIoTClient 62:5a4cdacf5090 216 {
AzureIoTClient 66:a419827cb051 217 device_key = device_config->deviceKey;
AzureIoTClient 66:a419827cb051 218 device_id = device_config->deviceId;
AzureIoTClient 66:a419827cb051 219 sas_token = device_config->deviceSasToken;
AzureIoTClient 66:a419827cb051 220 }
AzureIoTClient 66:a419827cb051 221
AzureIoTClient 66:a419827cb051 222 /* Codes_SRS_IOTHUBCLIENT_LL_07_029: [ IoTHubClient_LL_Create shall create the Auth module with the device_key, device_id, and/or deviceSasToken values ] */
AzureIoTClient 66:a419827cb051 223 if ((result->authorization_module = IoTHubClient_Auth_Create(device_key, device_id, sas_token)) == NULL)
AzureIoTClient 66:a419827cb051 224 {
AzureIoTClient 66:a419827cb051 225 LogError("Failed create authorization module");
AzureIoTClient 66:a419827cb051 226 free(result);
AzureIoTClient 66:a419827cb051 227 STRING_delete(product_info);
AzureIoTClient 66:a419827cb051 228 result = NULL;
AzureIoTClient 62:5a4cdacf5090 229 }
AzureIoTClient 66:a419827cb051 230 else if (client_config != NULL)
AzureIoTClient 66:a419827cb051 231 {
AzureIoTClient 66:a419827cb051 232 IOTHUBTRANSPORT_CONFIG lowerLayerConfig;
AzureIoTClient 66:a419827cb051 233 memset(&lowerLayerConfig, 0, sizeof(IOTHUBTRANSPORT_CONFIG));
AzureIoTClient 66:a419827cb051 234 /*Codes_SRS_IOTHUBCLIENT_LL_02_006: [IoTHubClient_LL_Create shall populate a structure of type IOTHUBTRANSPORT_CONFIG with the information from config parameter and the previous DLIST and shall pass that to the underlying layer _Create function.]*/
AzureIoTClient 66:a419827cb051 235 lowerLayerConfig.upperConfig = client_config;
AzureIoTClient 66:a419827cb051 236 lowerLayerConfig.waitingToSend = &(result->waitingToSend);
AzureIoTClient 66:a419827cb051 237 lowerLayerConfig.auth_module_handle = result->authorization_module;
AzureIoTClient 62:5a4cdacf5090 238
AzureIoTClient 66:a419827cb051 239 setTransportProtocol(result, (TRANSPORT_PROVIDER*)client_config->protocol());
AzureIoTClient 66:a419827cb051 240 if ((result->transportHandle = result->IoTHubTransport_Create(&lowerLayerConfig)) == NULL)
AzureIoTClient 66:a419827cb051 241 {
AzureIoTClient 66:a419827cb051 242 /*Codes_SRS_IOTHUBCLIENT_LL_02_007: [If the underlaying layer _Create function fails them IoTHubClient_LL_Create shall fail and return NULL.] */
AzureIoTClient 66:a419827cb051 243 LogError("underlying transport failed");
AzureIoTClient 66:a419827cb051 244 destroy_blob_upload_module(result);
AzureIoTClient 66:a419827cb051 245 tickcounter_destroy(result->tickCounter);
AzureIoTClient 66:a419827cb051 246 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 66:a419827cb051 247 STRING_delete(product_info);
AzureIoTClient 66:a419827cb051 248 free(result);
AzureIoTClient 66:a419827cb051 249 result = NULL;
AzureIoTClient 66:a419827cb051 250 }
AzureIoTClient 66:a419827cb051 251 else
AzureIoTClient 66:a419827cb051 252 {
AzureIoTClient 66:a419827cb051 253 /*Codes_SRS_IOTHUBCLIENT_LL_02_008: [Otherwise, IoTHubClient_LL_Create shall succeed and return a non-NULL handle.] */
AzureIoTClient 66:a419827cb051 254 result->isSharedTransport = false;
AzureIoTClient 66:a419827cb051 255 config = client_config;
AzureIoTClient 66:a419827cb051 256 }
AzureIoTClient 62:5a4cdacf5090 257 }
AzureIoTClient 62:5a4cdacf5090 258 else
AzureIoTClient 62:5a4cdacf5090 259 {
AzureIoTClient 66:a419827cb051 260 result->transportHandle = device_config->transportHandle;
AzureIoTClient 66:a419827cb051 261 setTransportProtocol(result, (TRANSPORT_PROVIDER*)device_config->protocol());
AzureIoTClient 66:a419827cb051 262
AzureIoTClient 66:a419827cb051 263 transport_hostname = result->IoTHubTransport_GetHostname(result->transportHandle);
AzureIoTClient 66:a419827cb051 264 if (transport_hostname == NULL)
AzureIoTClient 62:5a4cdacf5090 265 {
AzureIoTClient 62:5a4cdacf5090 266 /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_CreateWithTransport shall fail and return NULL. ]*/
AzureIoTClient 66:a419827cb051 267 LogError("unable to determine the transport IoTHub name");
AzureIoTClient 63:1bf1c2d60aab 268 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 66:a419827cb051 269 STRING_delete(product_info);
AzureIoTClient 62:5a4cdacf5090 270 free(result);
AzureIoTClient 62:5a4cdacf5090 271 result = NULL;
AzureIoTClient 62:5a4cdacf5090 272 }
AzureIoTClient 62:5a4cdacf5090 273 else
AzureIoTClient 62:5a4cdacf5090 274 {
AzureIoTClient 66:a419827cb051 275 const char* hostname = STRING_c_str(transport_hostname);
AzureIoTClient 62:5a4cdacf5090 276 /*Codes_SRS_IOTHUBCLIENT_LL_02_096: [ IoTHubClient_LL_CreateWithTransport shall create the data structures needed to instantiate a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE. ]*/
AzureIoTClient 66:a419827cb051 277 /*the first '.' says where the iothubname finishes*/
AzureIoTClient 66:a419827cb051 278 const char* whereIsDot = strchr(hostname, '.');
AzureIoTClient 66:a419827cb051 279 if (whereIsDot == NULL)
AzureIoTClient 62:5a4cdacf5090 280 {
AzureIoTClient 62:5a4cdacf5090 281 /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_CreateWithTransport shall fail and return NULL. ]*/
AzureIoTClient 66:a419827cb051 282 LogError("unable to determine the IoTHub name");
AzureIoTClient 63:1bf1c2d60aab 283 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 66:a419827cb051 284 STRING_delete(product_info);
AzureIoTClient 62:5a4cdacf5090 285 free(result);
AzureIoTClient 62:5a4cdacf5090 286 result = NULL;
AzureIoTClient 62:5a4cdacf5090 287 }
AzureIoTClient 62:5a4cdacf5090 288 else
AzureIoTClient 62:5a4cdacf5090 289 {
AzureIoTClient 66:a419827cb051 290 /*Codes_SRS_IOTHUBCLIENT_LL_02_096: [ IoTHubClient_LL_CreateWithTransport shall create the data structures needed to instantiate a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE. ]*/
AzureIoTClient 66:a419827cb051 291 IoTHubName = (char*)malloc(whereIsDot - hostname + 1);
AzureIoTClient 66:a419827cb051 292 if (IoTHubName == NULL)
AzureIoTClient 66:a419827cb051 293 {
AzureIoTClient 66:a419827cb051 294 /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_CreateWithTransport shall fail and return NULL. ]*/
AzureIoTClient 66:a419827cb051 295 LogError("unable to malloc");
AzureIoTClient 66:a419827cb051 296 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 66:a419827cb051 297 STRING_delete(product_info);
AzureIoTClient 66:a419827cb051 298 free(result);
AzureIoTClient 66:a419827cb051 299 result = NULL;
AzureIoTClient 66:a419827cb051 300 }
AzureIoTClient 66:a419827cb051 301 else
AzureIoTClient 66:a419827cb051 302 {
AzureIoTClient 66:a419827cb051 303 const char* IotHubSuffix = whereIsDot + 1;
AzureIoTClient 66:a419827cb051 304 (void)memcpy(IoTHubName, hostname, whereIsDot - hostname);
AzureIoTClient 66:a419827cb051 305 IoTHubName[whereIsDot - hostname] = '\0';
AzureIoTClient 62:5a4cdacf5090 306
AzureIoTClient 66:a419827cb051 307 actual_config.deviceId = device_config->deviceId;
AzureIoTClient 66:a419827cb051 308 actual_config.deviceKey = device_config->deviceKey;
AzureIoTClient 66:a419827cb051 309 actual_config.deviceSasToken = device_config->deviceSasToken;
AzureIoTClient 66:a419827cb051 310 actual_config.iotHubName = IoTHubName;
AzureIoTClient 66:a419827cb051 311 actual_config.iotHubSuffix = IotHubSuffix;
AzureIoTClient 66:a419827cb051 312 actual_config.protocol = NULL; /*irrelevant to IoTHubClient_LL_UploadToBlob*/
AzureIoTClient 66:a419827cb051 313 actual_config.protocolGatewayHostName = NULL; /*irrelevant to IoTHubClient_LL_UploadToBlob*/
AzureIoTClient 62:5a4cdacf5090 314
AzureIoTClient 66:a419827cb051 315 config = &actual_config;
AzureIoTClient 62:5a4cdacf5090 316
AzureIoTClient 66:a419827cb051 317 /*Codes_SRS_IOTHUBCLIENT_LL_02_008: [Otherwise, IoTHubClient_LL_Create shall succeed and return a non-NULL handle.] */
AzureIoTClient 66:a419827cb051 318 result->isSharedTransport = true;
AzureIoTClient 66:a419827cb051 319 }
AzureIoTClient 62:5a4cdacf5090 320 }
AzureIoTClient 62:5a4cdacf5090 321 }
AzureIoTClient 62:5a4cdacf5090 322 }
AzureIoTClient 66:a419827cb051 323 if (result != NULL)
AzureIoTClient 62:5a4cdacf5090 324 {
AzureIoTClient 66:a419827cb051 325 if (create_blob_upload_module(result, config) != 0)
AzureIoTClient 62:5a4cdacf5090 326 {
AzureIoTClient 66:a419827cb051 327 LogError("unable to create blob upload");
AzureIoTClient 62:5a4cdacf5090 328 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 66:a419827cb051 329 STRING_delete(product_info);
AzureIoTClient 62:5a4cdacf5090 330 free(result);
AzureIoTClient 62:5a4cdacf5090 331 result = NULL;
AzureIoTClient 62:5a4cdacf5090 332 }
AzureIoTClient 62:5a4cdacf5090 333 else
AzureIoTClient 62:5a4cdacf5090 334 {
AzureIoTClient 66:a419827cb051 335 if ((result->tickCounter = tickcounter_create()) == NULL)
AzureIoTClient 62:5a4cdacf5090 336 {
AzureIoTClient 66:a419827cb051 337 LogError("unable to get a tickcounter");
AzureIoTClient 66:a419827cb051 338 destroy_blob_upload_module(result);
AzureIoTClient 62:5a4cdacf5090 339 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 66:a419827cb051 340 STRING_delete(product_info);
AzureIoTClient 62:5a4cdacf5090 341 free(result);
AzureIoTClient 62:5a4cdacf5090 342 result = NULL;
AzureIoTClient 62:5a4cdacf5090 343 }
AzureIoTClient 62:5a4cdacf5090 344 else
AzureIoTClient 62:5a4cdacf5090 345 {
AzureIoTClient 66:a419827cb051 346 /*Codes_SRS_IOTHUBCLIENT_LL_02_004: [Otherwise IoTHubClient_LL_Create shall initialize a new DLIST (further called "waitingToSend") containing records with fields of the following types: IOTHUB_MESSAGE_HANDLE, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK, void*.]*/
AzureIoTClient 66:a419827cb051 347 DList_InitializeListHead(&(result->waitingToSend));
AzureIoTClient 66:a419827cb051 348 DList_InitializeListHead(&(result->iot_msg_queue));
AzureIoTClient 66:a419827cb051 349 DList_InitializeListHead(&(result->iot_ack_queue));
AzureIoTClient 66:a419827cb051 350 result->messageCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 66:a419827cb051 351 result->lastMessageReceiveTime = INDEFINITE_TIME;
AzureIoTClient 66:a419827cb051 352 result->data_msg_id = 1;
AzureIoTClient 66:a419827cb051 353 result->product_info = product_info;
AzureIoTClient 66:a419827cb051 354
AzureIoTClient 66:a419827cb051 355 IOTHUB_DEVICE_CONFIG deviceConfig;
AzureIoTClient 66:a419827cb051 356 deviceConfig.deviceId = config->deviceId;
AzureIoTClient 66:a419827cb051 357 deviceConfig.deviceKey = config->deviceKey;
AzureIoTClient 66:a419827cb051 358 deviceConfig.deviceSasToken = config->deviceSasToken;
AzureIoTClient 66:a419827cb051 359 deviceConfig.authorization_module = result->authorization_module;
AzureIoTClient 66:a419827cb051 360
AzureIoTClient 66:a419827cb051 361 /*Codes_SRS_IOTHUBCLIENT_LL_17_008: [IoTHubClient_LL_Create shall call the transport _Register function with a populated structure of type IOTHUB_DEVICE_CONFIG and waitingToSend list.] */
AzureIoTClient 66:a419827cb051 362 if ((result->deviceHandle = result->IoTHubTransport_Register(result->transportHandle, &deviceConfig, result, &(result->waitingToSend))) == NULL)
AzureIoTClient 62:5a4cdacf5090 363 {
AzureIoTClient 66:a419827cb051 364 LogError("Registering device in transport failed");
AzureIoTClient 62:5a4cdacf5090 365 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 62:5a4cdacf5090 366 result->IoTHubTransport_Destroy(result->transportHandle);
AzureIoTClient 62:5a4cdacf5090 367 destroy_blob_upload_module(result);
AzureIoTClient 62:5a4cdacf5090 368 tickcounter_destroy(result->tickCounter);
AzureIoTClient 66:a419827cb051 369 STRING_delete(product_info);
AzureIoTClient 62:5a4cdacf5090 370 free(result);
AzureIoTClient 62:5a4cdacf5090 371 result = NULL;
AzureIoTClient 62:5a4cdacf5090 372 }
AzureIoTClient 66:a419827cb051 373 else
AzureIoTClient 66:a419827cb051 374 {
AzureIoTClient 66:a419827cb051 375 /*Codes_SRS_IOTHUBCLIENT_LL_02_042: [ By default, messages shall not timeout. ]*/
AzureIoTClient 66:a419827cb051 376 result->currentMessageTimeout = 0;
AzureIoTClient 66:a419827cb051 377 result->current_device_twin_timeout = 0;
AzureIoTClient 66:a419827cb051 378 /*Codes_SRS_IOTHUBCLIENT_LL_25_124: [ `IoTHubClient_LL_Create` shall set the default retry policy as Exponential backoff with jitter and if succeed and return a `non-NULL` handle. ]*/
AzureIoTClient 66:a419827cb051 379 if (IoTHubClient_LL_SetRetryPolicy(result, IOTHUB_CLIENT_RETRY_EXPONENTIAL_BACKOFF_WITH_JITTER, 0) != IOTHUB_CLIENT_OK)
AzureIoTClient 66:a419827cb051 380 {
AzureIoTClient 66:a419827cb051 381 LogError("Setting default retry policy in transport failed");
AzureIoTClient 66:a419827cb051 382 result->IoTHubTransport_Unregister(result->deviceHandle);
AzureIoTClient 66:a419827cb051 383 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 66:a419827cb051 384 result->IoTHubTransport_Destroy(result->transportHandle);
AzureIoTClient 66:a419827cb051 385 destroy_blob_upload_module(result);
AzureIoTClient 66:a419827cb051 386 tickcounter_destroy(result->tickCounter);
AzureIoTClient 66:a419827cb051 387 STRING_delete(product_info);
AzureIoTClient 66:a419827cb051 388 free(result);
AzureIoTClient 66:a419827cb051 389 result = NULL;
AzureIoTClient 66:a419827cb051 390 }
AzureIoTClient 66:a419827cb051 391 }
AzureIoTClient 62:5a4cdacf5090 392 }
AzureIoTClient 62:5a4cdacf5090 393 }
AzureIoTClient 62:5a4cdacf5090 394 }
AzureIoTClient 66:a419827cb051 395 if (IoTHubName)
AzureIoTClient 66:a419827cb051 396 {
AzureIoTClient 66:a419827cb051 397 free(IoTHubName);
AzureIoTClient 66:a419827cb051 398 }
AzureIoTClient 66:a419827cb051 399 if (transport_hostname)
AzureIoTClient 66:a419827cb051 400 {
AzureIoTClient 66:a419827cb051 401 STRING_delete(transport_hostname);
AzureIoTClient 66:a419827cb051 402 }
AzureIoTClient 62:5a4cdacf5090 403 }
AzureIoTClient 62:5a4cdacf5090 404 }
AzureIoTClient 62:5a4cdacf5090 405 return result;
AzureIoTClient 62:5a4cdacf5090 406 }
AzureIoTClient 62:5a4cdacf5090 407
AzureIoTClient 53:1e5a1ca1f274 408 static uint32_t get_next_item_id(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData)
AzureIoTClient 53:1e5a1ca1f274 409 {
AzureIoTClient 53:1e5a1ca1f274 410 if (handleData->data_msg_id+1 >= UINT32_MAX)
AzureIoTClient 53:1e5a1ca1f274 411 {
AzureIoTClient 53:1e5a1ca1f274 412 handleData->data_msg_id = 1;
AzureIoTClient 53:1e5a1ca1f274 413 }
AzureIoTClient 53:1e5a1ca1f274 414 else
AzureIoTClient 53:1e5a1ca1f274 415 {
AzureIoTClient 53:1e5a1ca1f274 416 handleData->data_msg_id++;
AzureIoTClient 53:1e5a1ca1f274 417 }
AzureIoTClient 53:1e5a1ca1f274 418 return handleData->data_msg_id;
AzureIoTClient 53:1e5a1ca1f274 419 }
AzureIoTClient 53:1e5a1ca1f274 420
AzureIoTClient 53:1e5a1ca1f274 421 static IOTHUB_DEVICE_TWIN* dev_twin_data_create(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData, uint32_t id, const unsigned char* reportedState, size_t size, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK reportedStateCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 422 {
AzureIoTClient 53:1e5a1ca1f274 423 IOTHUB_DEVICE_TWIN* result = (IOTHUB_DEVICE_TWIN*)malloc(sizeof(IOTHUB_DEVICE_TWIN) );
AzureIoTClient 53:1e5a1ca1f274 424 if (result != NULL)
AzureIoTClient 53:1e5a1ca1f274 425 {
AzureIoTClient 53:1e5a1ca1f274 426 result->report_data_handle = CONSTBUFFER_Create(reportedState, size);
AzureIoTClient 53:1e5a1ca1f274 427 if (result->report_data_handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 428 {
AzureIoTClient 53:1e5a1ca1f274 429 LogError("Failure allocating reported state data");
AzureIoTClient 53:1e5a1ca1f274 430 free(result);
AzureIoTClient 53:1e5a1ca1f274 431 result = NULL;
AzureIoTClient 53:1e5a1ca1f274 432 }
AzureIoTClient 53:1e5a1ca1f274 433 else if (tickcounter_get_current_ms(handleData->tickCounter, &result->ms_timesOutAfter) != 0)
AzureIoTClient 53:1e5a1ca1f274 434 {
AzureIoTClient 53:1e5a1ca1f274 435 LogError("Failure getting tickcount info");
AzureIoTClient 53:1e5a1ca1f274 436 CONSTBUFFER_Destroy(result->report_data_handle);
AzureIoTClient 53:1e5a1ca1f274 437 free(result);
AzureIoTClient 53:1e5a1ca1f274 438 result = NULL;
AzureIoTClient 53:1e5a1ca1f274 439 }
AzureIoTClient 53:1e5a1ca1f274 440 else
AzureIoTClient 53:1e5a1ca1f274 441 {
AzureIoTClient 53:1e5a1ca1f274 442 result->item_id = id;
AzureIoTClient 53:1e5a1ca1f274 443 result->ms_timesOutAfter = 0;
AzureIoTClient 53:1e5a1ca1f274 444 result->context = userContextCallback;
AzureIoTClient 53:1e5a1ca1f274 445 result->reported_state_callback = reportedStateCallback;
AzureIoTClient 53:1e5a1ca1f274 446 }
AzureIoTClient 53:1e5a1ca1f274 447 }
AzureIoTClient 53:1e5a1ca1f274 448 else
AzureIoTClient 53:1e5a1ca1f274 449 {
AzureIoTClient 53:1e5a1ca1f274 450 LogError("Failure allocating device twin information");
AzureIoTClient 53:1e5a1ca1f274 451 }
AzureIoTClient 53:1e5a1ca1f274 452 return result;
AzureIoTClient 53:1e5a1ca1f274 453 }
AzureIoTClient 53:1e5a1ca1f274 454
AzureIoTClient 16:deba40344375 455 IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_CreateFromConnectionString(const char* connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol)
AzureIoTClient 16:deba40344375 456 {
AzureIoTClient 53:1e5a1ca1f274 457 IOTHUB_CLIENT_LL_HANDLE result;
AzureIoTClient 16:deba40344375 458
AzureIoTClient 42:448eecc3676e 459 /*Codes_SRS_IOTHUBCLIENT_LL_05_001: [IoTHubClient_LL_CreateFromConnectionString shall obtain the version string by a call to IoTHubClient_GetVersionString.]*/
AzureIoTClient 42:448eecc3676e 460 /*Codes_SRS_IOTHUBCLIENT_LL_05_002: [IoTHubClient_LL_CreateFromConnectionString shall print the version string to standard output.]*/
AzureIoTClient 42:448eecc3676e 461 LogInfo("IoT Hub SDK for C, version %s", IoTHubClient_GetVersionString());
AzureIoTClient 16:deba40344375 462
AzureIoTClient 42:448eecc3676e 463 /* Codes_SRS_IOTHUBCLIENT_LL_12_003: [IoTHubClient_LL_CreateFromConnectionString shall verify the input parameter and if it is NULL then return NULL] */
AzureIoTClient 42:448eecc3676e 464 if (connectionString == NULL)
AzureIoTClient 42:448eecc3676e 465 {
AzureIoTClient 42:448eecc3676e 466 LogError("Input parameter is NULL: connectionString");
AzureIoTClient 53:1e5a1ca1f274 467 result = NULL;
AzureIoTClient 42:448eecc3676e 468 }
AzureIoTClient 42:448eecc3676e 469 else if (protocol == NULL)
AzureIoTClient 42:448eecc3676e 470 {
AzureIoTClient 42:448eecc3676e 471 LogError("Input parameter is NULL: protocol");
AzureIoTClient 53:1e5a1ca1f274 472 result = NULL;
AzureIoTClient 42:448eecc3676e 473 }
AzureIoTClient 42:448eecc3676e 474 else
AzureIoTClient 42:448eecc3676e 475 {
AzureIoTClient 42:448eecc3676e 476 /* Codes_SRS_IOTHUBCLIENT_LL_12_004: [IoTHubClient_LL_CreateFromConnectionString shall allocate IOTHUB_CLIENT_CONFIG structure] */
AzureIoTClient 53:1e5a1ca1f274 477 IOTHUB_CLIENT_CONFIG* config = (IOTHUB_CLIENT_CONFIG*) malloc(sizeof(IOTHUB_CLIENT_CONFIG));
AzureIoTClient 42:448eecc3676e 478 if (config == NULL)
AzureIoTClient 42:448eecc3676e 479 {
AzureIoTClient 42:448eecc3676e 480 /* Codes_SRS_IOTHUBCLIENT_LL_12_012: [If the allocation failed IoTHubClient_LL_CreateFromConnectionString returns NULL] */
AzureIoTClient 42:448eecc3676e 481 LogError("Malloc failed");
AzureIoTClient 53:1e5a1ca1f274 482 result = NULL;
AzureIoTClient 42:448eecc3676e 483 }
AzureIoTClient 42:448eecc3676e 484 else
AzureIoTClient 42:448eecc3676e 485 {
AzureIoTClient 42:448eecc3676e 486 STRING_TOKENIZER_HANDLE tokenizer1 = NULL;
AzureIoTClient 42:448eecc3676e 487 STRING_HANDLE connString = NULL;
AzureIoTClient 42:448eecc3676e 488 STRING_HANDLE tokenString = NULL;
AzureIoTClient 42:448eecc3676e 489 STRING_HANDLE valueString = NULL;
AzureIoTClient 42:448eecc3676e 490 STRING_HANDLE hostNameString = NULL;
AzureIoTClient 42:448eecc3676e 491 STRING_HANDLE hostSuffixString = NULL;
AzureIoTClient 42:448eecc3676e 492 STRING_HANDLE deviceIdString = NULL;
AzureIoTClient 42:448eecc3676e 493 STRING_HANDLE deviceKeyString = NULL;
AzureIoTClient 42:448eecc3676e 494 STRING_HANDLE deviceSasTokenString = NULL;
AzureIoTClient 42:448eecc3676e 495 STRING_HANDLE protocolGateway = NULL;
AzureIoTClient 16:deba40344375 496
AzureIoTClient 42:448eecc3676e 497 config->protocol = protocol;
AzureIoTClient 16:deba40344375 498
AzureIoTClient 42:448eecc3676e 499 config->iotHubName = NULL;
AzureIoTClient 42:448eecc3676e 500 config->iotHubSuffix = NULL;
AzureIoTClient 42:448eecc3676e 501 config->deviceId = NULL;
AzureIoTClient 42:448eecc3676e 502 config->deviceKey = NULL;
AzureIoTClient 42:448eecc3676e 503 config->deviceSasToken = NULL;
AzureIoTClient 40:1a94db9139ea 504
AzureIoTClient 42:448eecc3676e 505 /* Codes_SRS_IOTHUBCLIENT_LL_04_002: [If it does not, it shall pass the protocolGatewayHostName NULL.] */
AzureIoTClient 42:448eecc3676e 506 config->protocolGatewayHostName = NULL;
AzureIoTClient 16:deba40344375 507
AzureIoTClient 42:448eecc3676e 508 if ((connString = STRING_construct(connectionString)) == NULL)
AzureIoTClient 42:448eecc3676e 509 {
AzureIoTClient 42:448eecc3676e 510 LogError("Error constructing connectiong String");
AzureIoTClient 53:1e5a1ca1f274 511 result = NULL;
AzureIoTClient 42:448eecc3676e 512 }
AzureIoTClient 42:448eecc3676e 513 else if ((tokenizer1 = STRING_TOKENIZER_create(connString)) == NULL)
AzureIoTClient 42:448eecc3676e 514 {
AzureIoTClient 42:448eecc3676e 515 LogError("Error creating Tokenizer");
AzureIoTClient 53:1e5a1ca1f274 516 result = NULL;
AzureIoTClient 42:448eecc3676e 517 }
AzureIoTClient 42:448eecc3676e 518 else if ((tokenString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 519 {
AzureIoTClient 42:448eecc3676e 520 LogError("Error creating Token String");
AzureIoTClient 53:1e5a1ca1f274 521 result = NULL;
AzureIoTClient 42:448eecc3676e 522 }
AzureIoTClient 42:448eecc3676e 523 else if ((valueString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 524 {
AzureIoTClient 42:448eecc3676e 525 LogError("Error creating Value String");
AzureIoTClient 53:1e5a1ca1f274 526 result = NULL;
AzureIoTClient 42:448eecc3676e 527 }
AzureIoTClient 42:448eecc3676e 528 else if ((hostNameString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 529 {
AzureIoTClient 42:448eecc3676e 530 LogError("Error creating HostName String");
AzureIoTClient 53:1e5a1ca1f274 531 result = NULL;
AzureIoTClient 42:448eecc3676e 532 }
AzureIoTClient 42:448eecc3676e 533 else if ((hostSuffixString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 534 {
AzureIoTClient 42:448eecc3676e 535 LogError("Error creating HostSuffix String");
AzureIoTClient 53:1e5a1ca1f274 536 result = NULL;
AzureIoTClient 42:448eecc3676e 537 }
AzureIoTClient 42:448eecc3676e 538 /* Codes_SRS_IOTHUBCLIENT_LL_12_005: [IoTHubClient_LL_CreateFromConnectionString shall try to parse the connectionString input parameter for the following structure: "Key1=value1;key2=value2;key3=value3..."] */
Azure.IoT Build 45:54c11b1b1407 539 /* Codes_SRS_IOTHUBCLIENT_LL_12_006: [IoTHubClient_LL_CreateFromConnectionString shall verify the existence of the following Key/Value pairs in the connection string: HostName, DeviceId, SharedAccessKey, SharedAccessSignature or x509] */
AzureIoTClient 42:448eecc3676e 540 else
AzureIoTClient 42:448eecc3676e 541 {
Azure.IoT Build 45:54c11b1b1407 542 int isx509found = 0;
AzureIoTClient 42:448eecc3676e 543 while ((STRING_TOKENIZER_get_next_token(tokenizer1, tokenString, "=") == 0))
AzureIoTClient 42:448eecc3676e 544 {
AzureIoTClient 42:448eecc3676e 545 if (STRING_TOKENIZER_get_next_token(tokenizer1, valueString, ";") != 0)
AzureIoTClient 42:448eecc3676e 546 {
AzureIoTClient 42:448eecc3676e 547 LogError("Tokenizer error");
AzureIoTClient 42:448eecc3676e 548 break;
AzureIoTClient 42:448eecc3676e 549 }
AzureIoTClient 42:448eecc3676e 550 else
AzureIoTClient 42:448eecc3676e 551 {
AzureIoTClient 42:448eecc3676e 552 if (tokenString != NULL)
AzureIoTClient 42:448eecc3676e 553 {
AzureIoTClient 42:448eecc3676e 554 /* Codes_SRS_IOTHUBCLIENT_LL_12_010: [IoTHubClient_LL_CreateFromConnectionString shall fill up the IOTHUB_CLIENT_CONFIG structure using the following mapping: iotHubName = Name, iotHubSuffix = Suffix, deviceId = DeviceId, deviceKey = SharedAccessKey or deviceSasToken = SharedAccessSignature] */
AzureIoTClient 42:448eecc3676e 555 const char* s_token = STRING_c_str(tokenString);
AzureIoTClient 42:448eecc3676e 556 if (strcmp(s_token, HOSTNAME_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 557 {
AzureIoTClient 42:448eecc3676e 558 /* Codes_SRS_IOTHUBCLIENT_LL_12_009: [IoTHubClient_LL_CreateFromConnectionString shall split the value of HostName to Name and Suffix using the first "." as a separator] */
AzureIoTClient 42:448eecc3676e 559 STRING_TOKENIZER_HANDLE tokenizer2 = NULL;
AzureIoTClient 42:448eecc3676e 560 if ((tokenizer2 = STRING_TOKENIZER_create(valueString)) == NULL)
AzureIoTClient 42:448eecc3676e 561 {
AzureIoTClient 42:448eecc3676e 562 LogError("Error creating Tokenizer");
AzureIoTClient 42:448eecc3676e 563 break;
AzureIoTClient 42:448eecc3676e 564 }
AzureIoTClient 42:448eecc3676e 565 else
AzureIoTClient 42:448eecc3676e 566 {
AzureIoTClient 42:448eecc3676e 567 /* Codes_SRS_IOTHUBCLIENT_LL_12_015: [If the string split failed, IoTHubClient_LL_CreateFromConnectionString returns NULL ] */
AzureIoTClient 42:448eecc3676e 568 if (STRING_TOKENIZER_get_next_token(tokenizer2, hostNameString, ".") != 0)
AzureIoTClient 42:448eecc3676e 569 {
AzureIoTClient 42:448eecc3676e 570 LogError("Tokenizer error");
AzureIoTClient 42:448eecc3676e 571 STRING_TOKENIZER_destroy(tokenizer2);
AzureIoTClient 42:448eecc3676e 572 break;
AzureIoTClient 42:448eecc3676e 573 }
AzureIoTClient 42:448eecc3676e 574 else
AzureIoTClient 42:448eecc3676e 575 {
AzureIoTClient 42:448eecc3676e 576 config->iotHubName = STRING_c_str(hostNameString);
AzureIoTClient 42:448eecc3676e 577 if (STRING_TOKENIZER_get_next_token(tokenizer2, hostSuffixString, ";") != 0)
AzureIoTClient 42:448eecc3676e 578 {
AzureIoTClient 42:448eecc3676e 579 LogError("Tokenizer error");
AzureIoTClient 42:448eecc3676e 580 STRING_TOKENIZER_destroy(tokenizer2);
AzureIoTClient 42:448eecc3676e 581 break;
AzureIoTClient 42:448eecc3676e 582 }
AzureIoTClient 42:448eecc3676e 583 else
AzureIoTClient 42:448eecc3676e 584 {
AzureIoTClient 42:448eecc3676e 585 config->iotHubSuffix = STRING_c_str(hostSuffixString);
AzureIoTClient 42:448eecc3676e 586 }
AzureIoTClient 42:448eecc3676e 587 }
AzureIoTClient 42:448eecc3676e 588 STRING_TOKENIZER_destroy(tokenizer2);
AzureIoTClient 42:448eecc3676e 589 }
AzureIoTClient 42:448eecc3676e 590 }
AzureIoTClient 42:448eecc3676e 591 else if (strcmp(s_token, DEVICEID_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 592 {
AzureIoTClient 42:448eecc3676e 593 deviceIdString = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 594 if (deviceIdString != NULL)
AzureIoTClient 42:448eecc3676e 595 {
AzureIoTClient 42:448eecc3676e 596 config->deviceId = STRING_c_str(deviceIdString);
AzureIoTClient 42:448eecc3676e 597 }
AzureIoTClient 53:1e5a1ca1f274 598 else
AzureIoTClient 53:1e5a1ca1f274 599 {
AzureIoTClient 53:1e5a1ca1f274 600 LogError("Failure cloning device id string");
AzureIoTClient 53:1e5a1ca1f274 601 break;
AzureIoTClient 53:1e5a1ca1f274 602 }
AzureIoTClient 42:448eecc3676e 603 }
AzureIoTClient 42:448eecc3676e 604 else if (strcmp(s_token, DEVICEKEY_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 605 {
AzureIoTClient 42:448eecc3676e 606 deviceKeyString = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 607 if (deviceKeyString != NULL)
AzureIoTClient 42:448eecc3676e 608 {
AzureIoTClient 42:448eecc3676e 609 config->deviceKey = STRING_c_str(deviceKeyString);
AzureIoTClient 42:448eecc3676e 610 }
AzureIoTClient 53:1e5a1ca1f274 611 else
AzureIoTClient 53:1e5a1ca1f274 612 {
AzureIoTClient 53:1e5a1ca1f274 613 LogError("Failure cloning device key string");
AzureIoTClient 53:1e5a1ca1f274 614 break;
AzureIoTClient 53:1e5a1ca1f274 615 }
AzureIoTClient 42:448eecc3676e 616 }
AzureIoTClient 42:448eecc3676e 617 else if (strcmp(s_token, DEVICESAS_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 618 {
AzureIoTClient 42:448eecc3676e 619 deviceSasTokenString = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 620 if (deviceSasTokenString != NULL)
AzureIoTClient 42:448eecc3676e 621 {
AzureIoTClient 42:448eecc3676e 622 config->deviceSasToken = STRING_c_str(deviceSasTokenString);
AzureIoTClient 42:448eecc3676e 623 }
AzureIoTClient 53:1e5a1ca1f274 624 else
AzureIoTClient 53:1e5a1ca1f274 625 {
AzureIoTClient 53:1e5a1ca1f274 626 LogError("Failure cloning device sasToken string");
AzureIoTClient 53:1e5a1ca1f274 627 break;
AzureIoTClient 53:1e5a1ca1f274 628 }
AzureIoTClient 42:448eecc3676e 629 }
Azure.IoT Build 45:54c11b1b1407 630 else if (strcmp(s_token, X509_TOKEN) == 0)
Azure.IoT Build 45:54c11b1b1407 631 {
Azure.IoT Build 45:54c11b1b1407 632 if (strcmp(STRING_c_str(valueString), X509_TOKEN_ONLY_ACCEPTABLE_VALUE) != 0)
Azure.IoT Build 45:54c11b1b1407 633 {
Azure.IoT Build 45:54c11b1b1407 634 LogError("x509 option has wrong value, the only acceptable one is \"true\"");
AzureIoTClient 53:1e5a1ca1f274 635 break;
Azure.IoT Build 45:54c11b1b1407 636 }
Azure.IoT Build 45:54c11b1b1407 637 else
Azure.IoT Build 45:54c11b1b1407 638 {
Azure.IoT Build 45:54c11b1b1407 639 isx509found = 1;
Azure.IoT Build 45:54c11b1b1407 640 }
Azure.IoT Build 45:54c11b1b1407 641 }
AzureIoTClient 42:448eecc3676e 642 /* Codes_SRS_IOTHUBCLIENT_LL_04_001: [IoTHubClient_LL_CreateFromConnectionString shall verify the existence of key/value pair GatewayHostName. If it does exist it shall pass the value to IoTHubClient_LL_Create API.] */
AzureIoTClient 42:448eecc3676e 643 else if (strcmp(s_token, PROTOCOL_GATEWAY_HOST) == 0)
AzureIoTClient 42:448eecc3676e 644 {
AzureIoTClient 42:448eecc3676e 645 protocolGateway = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 646 if (protocolGateway != NULL)
AzureIoTClient 42:448eecc3676e 647 {
AzureIoTClient 42:448eecc3676e 648 config->protocolGatewayHostName = STRING_c_str(protocolGateway);
AzureIoTClient 42:448eecc3676e 649 }
AzureIoTClient 53:1e5a1ca1f274 650 else
AzureIoTClient 53:1e5a1ca1f274 651 {
AzureIoTClient 53:1e5a1ca1f274 652 LogError("Failure cloning protocol Gateway Name");
AzureIoTClient 53:1e5a1ca1f274 653 break;
AzureIoTClient 53:1e5a1ca1f274 654 }
AzureIoTClient 42:448eecc3676e 655 }
AzureIoTClient 42:448eecc3676e 656 }
AzureIoTClient 42:448eecc3676e 657 }
AzureIoTClient 42:448eecc3676e 658 }
AzureIoTClient 42:448eecc3676e 659 /* parsing is done - check the result */
AzureIoTClient 42:448eecc3676e 660 if (config->iotHubName == NULL)
AzureIoTClient 42:448eecc3676e 661 {
AzureIoTClient 42:448eecc3676e 662 LogError("iotHubName is not found");
AzureIoTClient 53:1e5a1ca1f274 663 result = NULL;
AzureIoTClient 42:448eecc3676e 664 }
AzureIoTClient 42:448eecc3676e 665 else if (config->iotHubSuffix == NULL)
AzureIoTClient 42:448eecc3676e 666 {
AzureIoTClient 42:448eecc3676e 667 LogError("iotHubSuffix is not found");
AzureIoTClient 53:1e5a1ca1f274 668 result = NULL;
AzureIoTClient 42:448eecc3676e 669 }
AzureIoTClient 42:448eecc3676e 670 else if (config->deviceId == NULL)
AzureIoTClient 42:448eecc3676e 671 {
AzureIoTClient 42:448eecc3676e 672 LogError("deviceId is not found");
AzureIoTClient 53:1e5a1ca1f274 673 result = NULL;
AzureIoTClient 42:448eecc3676e 674 }
Azure.IoT Build 45:54c11b1b1407 675 else if (!(
Azure.IoT Build 45:54c11b1b1407 676 ((!isx509found) && (config->deviceSasToken == NULL) ^ (config->deviceKey == NULL)) ||
Azure.IoT Build 45:54c11b1b1407 677 ((isx509found) && (config->deviceSasToken == NULL) && (config->deviceKey == NULL))
Azure.IoT Build 45:54c11b1b1407 678 ))
AzureIoTClient 42:448eecc3676e 679 {
Azure.IoT Build 45:54c11b1b1407 680 LogError("invalid combination of x509, deviceSasToken and deviceKey");
AzureIoTClient 53:1e5a1ca1f274 681 result = NULL;
AzureIoTClient 42:448eecc3676e 682 }
AzureIoTClient 42:448eecc3676e 683 else
AzureIoTClient 42:448eecc3676e 684 {
AzureIoTClient 42:448eecc3676e 685 /* Codes_SRS_IOTHUBCLIENT_LL_12_011: [IoTHubClient_LL_CreateFromConnectionString shall call into the IoTHubClient_LL_Create API with the current structure and returns with the return value of it] */
AzureIoTClient 62:5a4cdacf5090 686 result = initialize_iothub_client(config, NULL);
AzureIoTClient 42:448eecc3676e 687 if (result == NULL)
AzureIoTClient 42:448eecc3676e 688 {
AzureIoTClient 42:448eecc3676e 689 LogError("IoTHubClient_LL_Create failed");
AzureIoTClient 42:448eecc3676e 690 }
AzureIoTClient 42:448eecc3676e 691 else
AzureIoTClient 42:448eecc3676e 692 {
AzureIoTClient 42:448eecc3676e 693 /*return as is*/
AzureIoTClient 42:448eecc3676e 694 }
AzureIoTClient 42:448eecc3676e 695 }
AzureIoTClient 42:448eecc3676e 696 }
AzureIoTClient 42:448eecc3676e 697 if (deviceSasTokenString != NULL)
AzureIoTClient 42:448eecc3676e 698 STRING_delete(deviceSasTokenString);
AzureIoTClient 42:448eecc3676e 699 if (deviceKeyString != NULL)
AzureIoTClient 42:448eecc3676e 700 STRING_delete(deviceKeyString);
AzureIoTClient 42:448eecc3676e 701 if (deviceIdString != NULL)
AzureIoTClient 42:448eecc3676e 702 STRING_delete(deviceIdString);
AzureIoTClient 42:448eecc3676e 703 if (hostSuffixString != NULL)
AzureIoTClient 42:448eecc3676e 704 STRING_delete(hostSuffixString);
AzureIoTClient 42:448eecc3676e 705 if (hostNameString != NULL)
AzureIoTClient 42:448eecc3676e 706 STRING_delete(hostNameString);
AzureIoTClient 42:448eecc3676e 707 if (valueString != NULL)
AzureIoTClient 42:448eecc3676e 708 STRING_delete(valueString);
AzureIoTClient 42:448eecc3676e 709 if (tokenString != NULL)
AzureIoTClient 42:448eecc3676e 710 STRING_delete(tokenString);
AzureIoTClient 42:448eecc3676e 711 if (connString != NULL)
AzureIoTClient 42:448eecc3676e 712 STRING_delete(connString);
AzureIoTClient 42:448eecc3676e 713 if (protocolGateway != NULL)
AzureIoTClient 42:448eecc3676e 714 STRING_delete(protocolGateway);
AzureIoTClient 16:deba40344375 715
AzureIoTClient 42:448eecc3676e 716 if (tokenizer1 != NULL)
AzureIoTClient 42:448eecc3676e 717 STRING_TOKENIZER_destroy(tokenizer1);
AzureIoTClient 16:deba40344375 718
AzureIoTClient 42:448eecc3676e 719 free(config);
AzureIoTClient 42:448eecc3676e 720 }
AzureIoTClient 42:448eecc3676e 721 }
AzureIoTClient 42:448eecc3676e 722 return result;
AzureIoTClient 16:deba40344375 723 }
AzureIoTClient 16:deba40344375 724
AzureIoTClient 16:deba40344375 725 IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_Create(const IOTHUB_CLIENT_CONFIG* config)
AzureIoTClient 16:deba40344375 726 {
AzureIoTClient 42:448eecc3676e 727 IOTHUB_CLIENT_LL_HANDLE result;
AzureIoTClient 42:448eecc3676e 728 /*Codes_SRS_IOTHUBCLIENT_LL_02_001: [IoTHubClient_LL_Create shall return NULL if config parameter is NULL or protocol field is NULL.]*/
AzureIoTClient 42:448eecc3676e 729 if(
AzureIoTClient 42:448eecc3676e 730 (config == NULL) ||
AzureIoTClient 42:448eecc3676e 731 (config->protocol == NULL)
AzureIoTClient 42:448eecc3676e 732 )
AzureIoTClient 42:448eecc3676e 733 {
AzureIoTClient 42:448eecc3676e 734 result = NULL;
AzureIoTClient 42:448eecc3676e 735 LogError("invalid configuration (NULL detected)");
AzureIoTClient 42:448eecc3676e 736 }
AzureIoTClient 42:448eecc3676e 737 else
AzureIoTClient 42:448eecc3676e 738 {
AzureIoTClient 62:5a4cdacf5090 739 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = initialize_iothub_client(config, NULL);
AzureIoTClient 42:448eecc3676e 740 if (handleData == NULL)
AzureIoTClient 42:448eecc3676e 741 {
AzureIoTClient 62:5a4cdacf5090 742 LogError("initialize iothub client");
AzureIoTClient 42:448eecc3676e 743 result = NULL;
AzureIoTClient 42:448eecc3676e 744 }
AzureIoTClient 42:448eecc3676e 745 else
AzureIoTClient 42:448eecc3676e 746 {
AzureIoTClient 62:5a4cdacf5090 747 result = handleData;
AzureIoTClient 42:448eecc3676e 748 }
AzureIoTClient 42:448eecc3676e 749 }
AzureIoTClient 16:deba40344375 750
AzureIoTClient 42:448eecc3676e 751 return result;
AzureIoTClient 16:deba40344375 752 }
AzureIoTClient 16:deba40344375 753
Azure.IoT Build 36:67300d5a4c1f 754 IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_CreateWithTransport(const IOTHUB_CLIENT_DEVICE_CONFIG * config)
Azure.IoT Build 36:67300d5a4c1f 755 {
AzureIoTClient 42:448eecc3676e 756 IOTHUB_CLIENT_LL_HANDLE result;
AzureIoTClient 42:448eecc3676e 757 /*Codes_SRS_IOTHUBCLIENT_LL_17_001: [IoTHubClient_LL_CreateWithTransport shall return NULL if config parameter is NULL, or protocol field is NULL or transportHandle is NULL.]*/
AzureIoTClient 42:448eecc3676e 758 if (
AzureIoTClient 42:448eecc3676e 759 (config == NULL) ||
AzureIoTClient 42:448eecc3676e 760 (config->protocol == NULL) ||
Azure.IoT Build 45:54c11b1b1407 761 (config->transportHandle == NULL) ||
Azure.IoT Build 45:54c11b1b1407 762 /*Codes_SRS_IOTHUBCLIENT_LL_02_098: [ IoTHubClient_LL_CreateWithTransport shall fail and return NULL if both config->deviceKey AND config->deviceSasToken are NULL. ]*/
Azure.IoT Build 45:54c11b1b1407 763 ((config->deviceKey == NULL) && (config->deviceSasToken == NULL))
AzureIoTClient 42:448eecc3676e 764 )
AzureIoTClient 42:448eecc3676e 765 {
AzureIoTClient 42:448eecc3676e 766 result = NULL;
AzureIoTClient 42:448eecc3676e 767 LogError("invalid configuration (NULL detected)");
AzureIoTClient 42:448eecc3676e 768 }
AzureIoTClient 42:448eecc3676e 769 else
AzureIoTClient 42:448eecc3676e 770 {
AzureIoTClient 62:5a4cdacf5090 771 result = initialize_iothub_client(NULL, config);
AzureIoTClient 42:448eecc3676e 772 }
AzureIoTClient 42:448eecc3676e 773 return result;
Azure.IoT Build 36:67300d5a4c1f 774 }
Azure.IoT Build 36:67300d5a4c1f 775
AzureIoTClient 16:deba40344375 776 void IoTHubClient_LL_Destroy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 16:deba40344375 777 {
AzureIoTClient 42:448eecc3676e 778 /*Codes_SRS_IOTHUBCLIENT_LL_02_009: [IoTHubClient_LL_Destroy shall do nothing if parameter iotHubClientHandle is NULL.]*/
AzureIoTClient 42:448eecc3676e 779 if (iotHubClientHandle != NULL)
AzureIoTClient 42:448eecc3676e 780 {
AzureIoTClient 42:448eecc3676e 781 PDLIST_ENTRY unsend;
AzureIoTClient 42:448eecc3676e 782 /*Codes_SRS_IOTHUBCLIENT_LL_17_010: [IoTHubClient_LL_Destroy shall call the underlaying layer's _Unregister function] */
AzureIoTClient 42:448eecc3676e 783 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 784 handleData->IoTHubTransport_Unregister(handleData->deviceHandle);
AzureIoTClient 42:448eecc3676e 785 if (handleData->isSharedTransport == false)
AzureIoTClient 42:448eecc3676e 786 {
AzureIoTClient 42:448eecc3676e 787 /*Codes_SRS_IOTHUBCLIENT_LL_02_010: [If iotHubClientHandle was not created by IoTHubClient_LL_CreateWithTransport, IoTHubClient_LL_Destroy shall call the underlaying layer's _Destroy function.] */
AzureIoTClient 42:448eecc3676e 788 handleData->IoTHubTransport_Destroy(handleData->transportHandle);
AzureIoTClient 42:448eecc3676e 789 }
AzureIoTClient 42:448eecc3676e 790 /*if any, remove the items currently not send*/
AzureIoTClient 42:448eecc3676e 791 while ((unsend = DList_RemoveHeadList(&(handleData->waitingToSend))) != &(handleData->waitingToSend))
AzureIoTClient 42:448eecc3676e 792 {
AzureIoTClient 42:448eecc3676e 793 IOTHUB_MESSAGE_LIST* temp = containingRecord(unsend, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 42:448eecc3676e 794 /*Codes_SRS_IOTHUBCLIENT_LL_02_033: [Otherwise, IoTHubClient_LL_Destroy shall complete all the event message callbacks that are in the waitingToSend list with the result IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY.] */
AzureIoTClient 42:448eecc3676e 795 if (temp->callback != NULL)
AzureIoTClient 42:448eecc3676e 796 {
AzureIoTClient 42:448eecc3676e 797 temp->callback(IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY, temp->context);
AzureIoTClient 42:448eecc3676e 798 }
AzureIoTClient 42:448eecc3676e 799 IoTHubMessage_Destroy(temp->messageHandle);
AzureIoTClient 42:448eecc3676e 800 free(temp);
AzureIoTClient 42:448eecc3676e 801 }
AzureIoTClient 53:1e5a1ca1f274 802
AzureIoTClient 53:1e5a1ca1f274 803 /* Codes_SRS_IOTHUBCLIENT_LL_07_007: [ IoTHubClient_LL_Destroy shall iterate the device twin queues and destroy any remaining items. ] */
AzureIoTClient 53:1e5a1ca1f274 804 while ((unsend = DList_RemoveHeadList(&(handleData->iot_msg_queue))) != &(handleData->iot_msg_queue))
AzureIoTClient 53:1e5a1ca1f274 805 {
AzureIoTClient 53:1e5a1ca1f274 806 IOTHUB_DEVICE_TWIN* temp = containingRecord(unsend, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 807 device_twin_data_destroy(temp);
AzureIoTClient 53:1e5a1ca1f274 808 }
AzureIoTClient 53:1e5a1ca1f274 809 while ((unsend = DList_RemoveHeadList(&(handleData->iot_ack_queue))) != &(handleData->iot_ack_queue))
AzureIoTClient 53:1e5a1ca1f274 810 {
AzureIoTClient 53:1e5a1ca1f274 811 IOTHUB_DEVICE_TWIN* temp = containingRecord(unsend, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 812 device_twin_data_destroy(temp);
AzureIoTClient 53:1e5a1ca1f274 813 }
AzureIoTClient 53:1e5a1ca1f274 814
AzureIoTClient 42:448eecc3676e 815 /*Codes_SRS_IOTHUBCLIENT_LL_17_011: [IoTHubClient_LL_Destroy shall free the resources allocated by IoTHubClient (if any).] */
AzureIoTClient 62:5a4cdacf5090 816 IoTHubClient_Auth_Destroy(handleData->authorization_module);
AzureIoTClient 42:448eecc3676e 817 tickcounter_destroy(handleData->tickCounter);
AzureIoTClient 44:33dd78697616 818 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 819 IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle);
AzureIoTClient 43:038d8511e817 820 #endif
AzureIoTClient 66:a419827cb051 821 STRING_delete(handleData->product_info);
AzureIoTClient 42:448eecc3676e 822 free(handleData);
AzureIoTClient 42:448eecc3676e 823 }
AzureIoTClient 16:deba40344375 824 }
AzureIoTClient 16:deba40344375 825
Azure.IoT Build 36:67300d5a4c1f 826 /*Codes_SRS_IOTHUBCLIENT_LL_02_044: [ Messages already delivered to IoTHubClient_LL shall not have their timeouts modified by a new call to IoTHubClient_LL_SetOption. ]*/
Azure.IoT Build 36:67300d5a4c1f 827 /*returns 0 on success, any other value is error*/
Azure.IoT Build 36:67300d5a4c1f 828 static int attach_ms_timesOutAfter(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData, IOTHUB_MESSAGE_LIST *newEntry)
Azure.IoT Build 36:67300d5a4c1f 829 {
AzureIoTClient 42:448eecc3676e 830 int result;
AzureIoTClient 42:448eecc3676e 831 /*Codes_SRS_IOTHUBCLIENT_LL_02_043: [ Calling IoTHubClient_LL_SetOption with value set to "0" shall disable the timeout mechanism for all new messages. ]*/
AzureIoTClient 42:448eecc3676e 832 if (handleData->currentMessageTimeout == 0)
AzureIoTClient 42:448eecc3676e 833 {
AzureIoTClient 42:448eecc3676e 834 newEntry->ms_timesOutAfter = 0; /*do not timeout*/
AzureIoTClient 42:448eecc3676e 835 result = 0;
AzureIoTClient 42:448eecc3676e 836 }
AzureIoTClient 42:448eecc3676e 837 else
AzureIoTClient 42:448eecc3676e 838 {
AzureIoTClient 42:448eecc3676e 839 /*Codes_SRS_IOTHUBCLIENT_LL_02_039: [ "messageTimeout" - once IoTHubClient_LL_SendEventAsync is called the message shall timeout after value miliseconds. Value is a pointer to a uint64. ]*/
AzureIoTClient 42:448eecc3676e 840 if (tickcounter_get_current_ms(handleData->tickCounter, &newEntry->ms_timesOutAfter) != 0)
AzureIoTClient 42:448eecc3676e 841 {
AzureIoTClient 60:41648c4e7036 842 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 843 LogError("unable to get the current relative tickcount");
AzureIoTClient 42:448eecc3676e 844 }
AzureIoTClient 42:448eecc3676e 845 else
AzureIoTClient 42:448eecc3676e 846 {
AzureIoTClient 42:448eecc3676e 847 newEntry->ms_timesOutAfter += handleData->currentMessageTimeout;
AzureIoTClient 42:448eecc3676e 848 result = 0;
AzureIoTClient 42:448eecc3676e 849 }
AzureIoTClient 42:448eecc3676e 850 }
AzureIoTClient 42:448eecc3676e 851 return result;
Azure.IoT Build 36:67300d5a4c1f 852 }
Azure.IoT Build 36:67300d5a4c1f 853
AzureIoTClient 16:deba40344375 854 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SendEventAsync(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_MESSAGE_HANDLE eventMessageHandle, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK eventConfirmationCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 855 {
AzureIoTClient 42:448eecc3676e 856 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 857 /*Codes_SRS_IOTHUBCLIENT_LL_02_011: [IoTHubClient_LL_SendEventAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle or eventMessageHandle is NULL.]*/
AzureIoTClient 42:448eecc3676e 858 if (
AzureIoTClient 42:448eecc3676e 859 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 860 (eventMessageHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 861 /*Codes_SRS_IOTHUBCLIENT_LL_02_012: [IoTHubClient_LL_SendEventAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter eventConfirmationCallback is NULL and userContextCallback is not NULL.] */
AzureIoTClient 42:448eecc3676e 862 ((eventConfirmationCallback == NULL) && (userContextCallback != NULL))
AzureIoTClient 42:448eecc3676e 863 )
AzureIoTClient 42:448eecc3676e 864 {
AzureIoTClient 42:448eecc3676e 865 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 45:54c11b1b1407 866 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 867 }
AzureIoTClient 42:448eecc3676e 868 else
AzureIoTClient 42:448eecc3676e 869 {
AzureIoTClient 42:448eecc3676e 870 IOTHUB_MESSAGE_LIST *newEntry = (IOTHUB_MESSAGE_LIST*)malloc(sizeof(IOTHUB_MESSAGE_LIST));
AzureIoTClient 42:448eecc3676e 871 if (newEntry == NULL)
AzureIoTClient 42:448eecc3676e 872 {
AzureIoTClient 42:448eecc3676e 873 result = IOTHUB_CLIENT_ERROR;
Azure.IoT Build 45:54c11b1b1407 874 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 875 }
AzureIoTClient 42:448eecc3676e 876 else
AzureIoTClient 42:448eecc3676e 877 {
AzureIoTClient 42:448eecc3676e 878 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 40:1a94db9139ea 879
AzureIoTClient 42:448eecc3676e 880 if (attach_ms_timesOutAfter(handleData, newEntry) != 0)
AzureIoTClient 42:448eecc3676e 881 {
AzureIoTClient 42:448eecc3676e 882 result = IOTHUB_CLIENT_ERROR;
Azure.IoT Build 45:54c11b1b1407 883 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 884 free(newEntry);
AzureIoTClient 42:448eecc3676e 885 }
AzureIoTClient 42:448eecc3676e 886 else
AzureIoTClient 42:448eecc3676e 887 {
AzureIoTClient 62:5a4cdacf5090 888 /*Codes_SRS_IOTHUBCLIENT_LL_02_013: [IoTHubClient_LL_SendEventAsync shall add the DLIST waitingToSend a new record cloning the information from eventMessageHandle, eventConfirmationCallback, userContextCallback.]*/
AzureIoTClient 42:448eecc3676e 889 if ((newEntry->messageHandle = IoTHubMessage_Clone(eventMessageHandle)) == NULL)
AzureIoTClient 42:448eecc3676e 890 {
AzureIoTClient 42:448eecc3676e 891 /*Codes_SRS_IOTHUBCLIENT_LL_02_014: [If cloning and/or adding the information fails for any reason, IoTHubClient_LL_SendEventAsync shall fail and return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 42:448eecc3676e 892 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 893 free(newEntry);
Azure.IoT Build 45:54c11b1b1407 894 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 895 }
AzureIoTClient 42:448eecc3676e 896 else
AzureIoTClient 42:448eecc3676e 897 {
AzureIoTClient 62:5a4cdacf5090 898 /*Codes_SRS_IOTHUBCLIENT_LL_02_013: [IoTHubClient_LL_SendEventAsync shall add the DLIST waitingToSend a new record cloning the information from eventMessageHandle, eventConfirmationCallback, userContextCallback.]*/
AzureIoTClient 42:448eecc3676e 899 newEntry->callback = eventConfirmationCallback;
AzureIoTClient 42:448eecc3676e 900 newEntry->context = userContextCallback;
AzureIoTClient 46:6a69294b6119 901 DList_InsertTailList(&(iotHubClientHandle->waitingToSend), &(newEntry->entry));
AzureIoTClient 42:448eecc3676e 902 /*Codes_SRS_IOTHUBCLIENT_LL_02_015: [Otherwise IoTHubClient_LL_SendEventAsync shall succeed and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 42:448eecc3676e 903 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 904 }
AzureIoTClient 42:448eecc3676e 905 }
AzureIoTClient 42:448eecc3676e 906 }
AzureIoTClient 42:448eecc3676e 907 }
AzureIoTClient 42:448eecc3676e 908 return result;
AzureIoTClient 16:deba40344375 909 }
AzureIoTClient 16:deba40344375 910
AzureIoTClient 16:deba40344375 911 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetMessageCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 912 {
AzureIoTClient 42:448eecc3676e 913 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 914 if (iotHubClientHandle == NULL)
AzureIoTClient 42:448eecc3676e 915 {
AzureIoTClient 61:8b85a4e797cf 916 /*Codes_SRS_IOTHUBCLIENT_LL_02_016: [IoTHubClient_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.] */
AzureIoTClient 61:8b85a4e797cf 917 LogError("Invalid argument - iotHubClientHandle is NULL");
AzureIoTClient 42:448eecc3676e 918 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 919 }
AzureIoTClient 42:448eecc3676e 920 else
AzureIoTClient 42:448eecc3676e 921 {
AzureIoTClient 42:448eecc3676e 922 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 923 if (messageCallback == NULL)
AzureIoTClient 42:448eecc3676e 924 {
AzureIoTClient 62:5a4cdacf5090 925 if (handleData->messageCallback.type == CALLBACK_TYPE_NONE)
AzureIoTClient 61:8b85a4e797cf 926 {
AzureIoTClient 61:8b85a4e797cf 927 /*Codes_SRS_IOTHUBCLIENT_LL_10_010: [If parameter messageCallback is NULL and the _SetMessageCallback had not been called to subscribe for messages, then IoTHubClient_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 61:8b85a4e797cf 928 LogError("not currently set to accept or process incoming messages.");
AzureIoTClient 61:8b85a4e797cf 929 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 930 }
AzureIoTClient 62:5a4cdacf5090 931 else if (handleData->messageCallback.type == CALLBACK_TYPE_ASYNC)
AzureIoTClient 61:8b85a4e797cf 932 {
AzureIoTClient 61:8b85a4e797cf 933 /*Codes_SRS_IOTHUBCLIENT_LL_10_010: [If parameter messageCallback is NULL and the _SetMessageCallback had not been called to subscribe for messages, then IoTHubClient_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 62:5a4cdacf5090 934 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetMessageCallback_Ex function.");
AzureIoTClient 61:8b85a4e797cf 935 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 936 }
AzureIoTClient 61:8b85a4e797cf 937 else
AzureIoTClient 61:8b85a4e797cf 938 {
AzureIoTClient 61:8b85a4e797cf 939 /*Codes_SRS_IOTHUBCLIENT_LL_02_019: [If parameter messageCallback is NULL then IoTHubClient_LL_SetMessageCallback shall call the underlying layer's _Unsubscribe function and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 61:8b85a4e797cf 940 handleData->IoTHubTransport_Unsubscribe(handleData->deviceHandle);
AzureIoTClient 62:5a4cdacf5090 941 handleData->messageCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 61:8b85a4e797cf 942 handleData->messageCallback.callbackSync = NULL;
AzureIoTClient 61:8b85a4e797cf 943 handleData->messageCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 944 handleData->messageCallback.userContextCallback = NULL;
AzureIoTClient 61:8b85a4e797cf 945 result = IOTHUB_CLIENT_OK;
AzureIoTClient 61:8b85a4e797cf 946 }
AzureIoTClient 42:448eecc3676e 947 }
AzureIoTClient 42:448eecc3676e 948 else
AzureIoTClient 42:448eecc3676e 949 {
AzureIoTClient 62:5a4cdacf5090 950 if (handleData->messageCallback.type == CALLBACK_TYPE_ASYNC)
AzureIoTClient 42:448eecc3676e 951 {
AzureIoTClient 62:5a4cdacf5090 952 /* Codes_SRS_IOTHUBCLIENT_LL_10_011: [If parameter messageCallback is non-NULL and the _SetMessageCallback_Ex had been used to susbscribe for messages, then IoTHubClient_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 62:5a4cdacf5090 953 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetMessageCallback_Ex function before subscribing with MessageCallback.");
AzureIoTClient 61:8b85a4e797cf 954 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 955 }
AzureIoTClient 42:448eecc3676e 956 else
AzureIoTClient 42:448eecc3676e 957 {
AzureIoTClient 61:8b85a4e797cf 958 if (handleData->IoTHubTransport_Subscribe(handleData->deviceHandle) == 0)
AzureIoTClient 61:8b85a4e797cf 959 {
AzureIoTClient 61:8b85a4e797cf 960 /*Codes_SRS_IOTHUBCLIENT_LL_02_017: [If parameter messageCallback is non-NULL then IoTHubClient_LL_SetMessageCallback shall call the underlying layer's _Subscribe function.]*/
AzureIoTClient 62:5a4cdacf5090 961 handleData->messageCallback.type = CALLBACK_TYPE_SYNC;
AzureIoTClient 61:8b85a4e797cf 962 handleData->messageCallback.callbackSync = messageCallback;
AzureIoTClient 62:5a4cdacf5090 963 handleData->messageCallback.userContextCallback = userContextCallback;
AzureIoTClient 61:8b85a4e797cf 964 result = IOTHUB_CLIENT_OK;
AzureIoTClient 61:8b85a4e797cf 965 }
AzureIoTClient 61:8b85a4e797cf 966 else
AzureIoTClient 61:8b85a4e797cf 967 {
AzureIoTClient 61:8b85a4e797cf 968 /*Codes_SRS_IOTHUBCLIENT_LL_02_018: [If the underlying layer's _Subscribe function fails, then IoTHubClient_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_ERROR. Otherwise IoTHubClient_LL_SetMessageCallback shall succeed and return IOTHUB_CLIENT_OK.]*/
AzureIoTClient 62:5a4cdacf5090 969 LogError("IoTHubTransport_Subscribe failed");
AzureIoTClient 62:5a4cdacf5090 970 handleData->messageCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 61:8b85a4e797cf 971 handleData->messageCallback.callbackSync = NULL;
AzureIoTClient 61:8b85a4e797cf 972 handleData->messageCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 973 handleData->messageCallback.userContextCallback = NULL;
AzureIoTClient 61:8b85a4e797cf 974 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 975 }
AzureIoTClient 42:448eecc3676e 976 }
AzureIoTClient 42:448eecc3676e 977 }
AzureIoTClient 42:448eecc3676e 978 }
AzureIoTClient 61:8b85a4e797cf 979 return result;
AzureIoTClient 61:8b85a4e797cf 980 }
AzureIoTClient 40:1a94db9139ea 981
AzureIoTClient 62:5a4cdacf5090 982 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetMessageCallback_Ex(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC_EX messageCallback, void* userContextCallback)
AzureIoTClient 61:8b85a4e797cf 983 {
AzureIoTClient 61:8b85a4e797cf 984 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 61:8b85a4e797cf 985 if (iotHubClientHandle == NULL)
AzureIoTClient 61:8b85a4e797cf 986 {
AzureIoTClient 62:5a4cdacf5090 987 /*Codes_SRS_IOTHUBCLIENT_LL_10_021: [IoTHubClient_LL_SetMessageCallback_Ex shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.]*/
AzureIoTClient 61:8b85a4e797cf 988 LogError("Invalid argument - iotHubClientHandle is NULL");
AzureIoTClient 61:8b85a4e797cf 989 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 61:8b85a4e797cf 990 }
AzureIoTClient 61:8b85a4e797cf 991 else
AzureIoTClient 61:8b85a4e797cf 992 {
AzureIoTClient 61:8b85a4e797cf 993 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 61:8b85a4e797cf 994 if (messageCallback == NULL)
AzureIoTClient 61:8b85a4e797cf 995 {
AzureIoTClient 62:5a4cdacf5090 996 if (handleData->messageCallback.type == CALLBACK_TYPE_NONE)
AzureIoTClient 61:8b85a4e797cf 997 {
AzureIoTClient 62:5a4cdacf5090 998 /*Codes_SRS_IOTHUBCLIENT_LL_10_018: [If parameter messageCallback is NULL and IoTHubClient_LL_SetMessageCallback_Ex had not been used to subscribe for messages, then IoTHubClient_LL_SetMessageCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 61:8b85a4e797cf 999 LogError("not currently set to accept or process incoming messages.");
AzureIoTClient 61:8b85a4e797cf 1000 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 1001 }
AzureIoTClient 62:5a4cdacf5090 1002 else if (handleData->messageCallback.type == CALLBACK_TYPE_SYNC)
AzureIoTClient 61:8b85a4e797cf 1003 {
AzureIoTClient 62:5a4cdacf5090 1004 /*Codes_SRS_IOTHUBCLIENT_LL_10_019: [If parameter messageCallback is NULL and IoTHubClient_LL_SetMessageCallback had been used to subscribe for messages, then IoTHubClient_LL_SetMessageCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 61:8b85a4e797cf 1005 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetMessageCallback function.");
AzureIoTClient 61:8b85a4e797cf 1006 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 1007 }
AzureIoTClient 61:8b85a4e797cf 1008 else
AzureIoTClient 61:8b85a4e797cf 1009 {
AzureIoTClient 62:5a4cdacf5090 1010 /*Codes_SRS_IOTHUBCLIENT_LL_10_023: [If parameter messageCallback is NULL then IoTHubClient_LL_SetMessageCallback_Ex shall call the underlying layer's _Unsubscribe function and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 61:8b85a4e797cf 1011 handleData->IoTHubTransport_Unsubscribe(handleData->deviceHandle);
AzureIoTClient 62:5a4cdacf5090 1012 handleData->messageCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 61:8b85a4e797cf 1013 handleData->messageCallback.callbackSync = NULL;
AzureIoTClient 61:8b85a4e797cf 1014 handleData->messageCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1015 handleData->messageCallback.userContextCallback = NULL;
AzureIoTClient 61:8b85a4e797cf 1016 result = IOTHUB_CLIENT_OK;
AzureIoTClient 61:8b85a4e797cf 1017 }
AzureIoTClient 61:8b85a4e797cf 1018 }
AzureIoTClient 61:8b85a4e797cf 1019 else
AzureIoTClient 61:8b85a4e797cf 1020 {
AzureIoTClient 62:5a4cdacf5090 1021 if (handleData->messageCallback.type == CALLBACK_TYPE_SYNC)
AzureIoTClient 61:8b85a4e797cf 1022 {
AzureIoTClient 62:5a4cdacf5090 1023 /*Codes_SRS_IOTHUBCLIENT_LL_10_020: [If parameter messageCallback is non-NULL, and IoTHubClient_LL_SetMessageCallback had been used to subscribe for messages, then IoTHubClient_LL_SetMessageCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 61:8b85a4e797cf 1024 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_MessageCallbackEx function before subscribing with MessageCallback.");
AzureIoTClient 61:8b85a4e797cf 1025 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 1026 }
AzureIoTClient 61:8b85a4e797cf 1027 else
AzureIoTClient 61:8b85a4e797cf 1028 {
AzureIoTClient 61:8b85a4e797cf 1029 if (handleData->IoTHubTransport_Subscribe(handleData->deviceHandle) == 0)
AzureIoTClient 61:8b85a4e797cf 1030 {
AzureIoTClient 62:5a4cdacf5090 1031 /*Codes_SRS_IOTHUBCLIENT_LL_10_024: [If parameter messageCallback is non-NULL then IoTHubClient_LL_SetMessageCallback_Ex shall call the underlying layer's _Subscribe function.]*/
AzureIoTClient 62:5a4cdacf5090 1032 handleData->messageCallback.type = CALLBACK_TYPE_ASYNC;
AzureIoTClient 61:8b85a4e797cf 1033 handleData->messageCallback.callbackAsync = messageCallback;
AzureIoTClient 62:5a4cdacf5090 1034 handleData->messageCallback.userContextCallback = userContextCallback;
AzureIoTClient 61:8b85a4e797cf 1035 result = IOTHUB_CLIENT_OK;
AzureIoTClient 61:8b85a4e797cf 1036 }
AzureIoTClient 61:8b85a4e797cf 1037 else
AzureIoTClient 61:8b85a4e797cf 1038 {
AzureIoTClient 62:5a4cdacf5090 1039 /*Codes_SRS_IOTHUBCLIENT_LL_10_025: [If the underlying layer's _Subscribe function fails, then IoTHubClient_LL_SetMessageCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR. Otherwise IoTHubClient_LL_SetMessageCallback_Ex shall succeed and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 62:5a4cdacf5090 1040 LogError("IoTHubTransport_Subscribe failed");
AzureIoTClient 62:5a4cdacf5090 1041 handleData->messageCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 61:8b85a4e797cf 1042 handleData->messageCallback.callbackSync = NULL;
AzureIoTClient 61:8b85a4e797cf 1043 handleData->messageCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1044 handleData->messageCallback.userContextCallback = NULL;
AzureIoTClient 61:8b85a4e797cf 1045 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 1046 }
AzureIoTClient 61:8b85a4e797cf 1047 }
AzureIoTClient 61:8b85a4e797cf 1048 }
AzureIoTClient 61:8b85a4e797cf 1049 }
AzureIoTClient 61:8b85a4e797cf 1050 return result;
AzureIoTClient 61:8b85a4e797cf 1051 }
AzureIoTClient 61:8b85a4e797cf 1052
AzureIoTClient 61:8b85a4e797cf 1053 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SendMessageDisposition(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, MESSAGE_CALLBACK_INFO* message_data, IOTHUBMESSAGE_DISPOSITION_RESULT disposition)
AzureIoTClient 61:8b85a4e797cf 1054 {
AzureIoTClient 61:8b85a4e797cf 1055 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 61:8b85a4e797cf 1056 if ((iotHubClientHandle == NULL) || (message_data == NULL))
AzureIoTClient 61:8b85a4e797cf 1057 {
AzureIoTClient 61:8b85a4e797cf 1058 /*Codes_SRS_IOTHUBCLIENT_LL_10_026: [IoTHubClient_LL_SendMessageDisposition shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.]*/
AzureIoTClient 61:8b85a4e797cf 1059 LogError("Invalid argument handle=%p, message=%p", iotHubClientHandle, message_data);
AzureIoTClient 61:8b85a4e797cf 1060 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 61:8b85a4e797cf 1061 }
AzureIoTClient 61:8b85a4e797cf 1062 else
AzureIoTClient 61:8b85a4e797cf 1063 {
AzureIoTClient 61:8b85a4e797cf 1064 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 61:8b85a4e797cf 1065 /*Codes_SRS_IOTHUBCLIENT_LL_10_027: [IoTHubClient_LL_SendMessageDisposition shall return the result from calling the underlying layer's _Send_Message_Disposition.]*/
AzureIoTClient 61:8b85a4e797cf 1066 result = handleData->IoTHubTransport_SendMessageDisposition(message_data, disposition);
AzureIoTClient 61:8b85a4e797cf 1067 }
AzureIoTClient 42:448eecc3676e 1068 return result;
AzureIoTClient 16:deba40344375 1069 }
AzureIoTClient 16:deba40344375 1070
Azure.IoT Build 36:67300d5a4c1f 1071 static void DoTimeouts(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData)
Azure.IoT Build 36:67300d5a4c1f 1072 {
Azure.IoT.Build 54:6dcad9019a64 1073 tickcounter_ms_t nowTick;
AzureIoTClient 42:448eecc3676e 1074 if (tickcounter_get_current_ms(handleData->tickCounter, &nowTick) != 0)
AzureIoTClient 42:448eecc3676e 1075 {
AzureIoTClient 42:448eecc3676e 1076 LogError("unable to get the current ms, timeouts will not be processed");
AzureIoTClient 42:448eecc3676e 1077 }
AzureIoTClient 42:448eecc3676e 1078 else
AzureIoTClient 42:448eecc3676e 1079 {
AzureIoTClient 42:448eecc3676e 1080 DLIST_ENTRY* currentItemInWaitingToSend = handleData->waitingToSend.Flink;
AzureIoTClient 42:448eecc3676e 1081 while (currentItemInWaitingToSend != &(handleData->waitingToSend)) /*while we are not at the end of the list*/
AzureIoTClient 42:448eecc3676e 1082 {
AzureIoTClient 42:448eecc3676e 1083 IOTHUB_MESSAGE_LIST* fullEntry = containingRecord(currentItemInWaitingToSend, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 42:448eecc3676e 1084 /*Codes_SRS_IOTHUBCLIENT_LL_02_041: [ If more than value miliseconds have passed since the call to IoTHubClient_LL_SendEventAsync then the message callback shall be called with a status code of IOTHUB_CLIENT_CONFIRMATION_TIMEOUT. ]*/
AzureIoTClient 42:448eecc3676e 1085 if ((fullEntry->ms_timesOutAfter != 0) && (fullEntry->ms_timesOutAfter < nowTick))
AzureIoTClient 42:448eecc3676e 1086 {
AzureIoTClient 42:448eecc3676e 1087 PDLIST_ENTRY theNext = currentItemInWaitingToSend->Flink; /*need to save the next item, because the below operations are destructive*/
AzureIoTClient 42:448eecc3676e 1088 DList_RemoveEntryList(currentItemInWaitingToSend);
AzureIoTClient 42:448eecc3676e 1089 if (fullEntry->callback != NULL)
AzureIoTClient 42:448eecc3676e 1090 {
AzureIoTClient 42:448eecc3676e 1091 fullEntry->callback(IOTHUB_CLIENT_CONFIRMATION_MESSAGE_TIMEOUT, fullEntry->context);
AzureIoTClient 42:448eecc3676e 1092 }
AzureIoTClient 42:448eecc3676e 1093 IoTHubMessage_Destroy(fullEntry->messageHandle); /*because it has been cloned*/
AzureIoTClient 42:448eecc3676e 1094 free(fullEntry);
AzureIoTClient 42:448eecc3676e 1095 currentItemInWaitingToSend = theNext;
AzureIoTClient 42:448eecc3676e 1096 }
AzureIoTClient 42:448eecc3676e 1097 else
AzureIoTClient 42:448eecc3676e 1098 {
AzureIoTClient 42:448eecc3676e 1099 currentItemInWaitingToSend = currentItemInWaitingToSend->Flink;
AzureIoTClient 42:448eecc3676e 1100 }
AzureIoTClient 42:448eecc3676e 1101 }
AzureIoTClient 42:448eecc3676e 1102 }
Azure.IoT Build 36:67300d5a4c1f 1103 }
Azure.IoT Build 36:67300d5a4c1f 1104
AzureIoTClient 16:deba40344375 1105 void IoTHubClient_LL_DoWork(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 16:deba40344375 1106 {
AzureIoTClient 42:448eecc3676e 1107 /*Codes_SRS_IOTHUBCLIENT_LL_02_020: [If parameter iotHubClientHandle is NULL then IoTHubClient_LL_DoWork shall not perform any action.] */
AzureIoTClient 42:448eecc3676e 1108 if (iotHubClientHandle != NULL)
AzureIoTClient 42:448eecc3676e 1109 {
AzureIoTClient 42:448eecc3676e 1110 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 1111 DoTimeouts(handleData);
AzureIoTClient 42:448eecc3676e 1112
AzureIoTClient 53:1e5a1ca1f274 1113 /*Codes_SRS_IOTHUBCLIENT_LL_07_008: [ IoTHubClient_LL_DoWork shall iterate the message queue and execute the underlying transports IoTHubTransport_ProcessItem function for each item. ] */
AzureIoTClient 53:1e5a1ca1f274 1114 DLIST_ENTRY* client_item = handleData->iot_msg_queue.Flink;
AzureIoTClient 53:1e5a1ca1f274 1115 while (client_item != &(handleData->iot_msg_queue)) /*while we are not at the end of the list*/
AzureIoTClient 53:1e5a1ca1f274 1116 {
AzureIoTClient 53:1e5a1ca1f274 1117 PDLIST_ENTRY next_item = client_item->Flink;
AzureIoTClient 53:1e5a1ca1f274 1118
AzureIoTClient 53:1e5a1ca1f274 1119 IOTHUB_DEVICE_TWIN* queue_data = containingRecord(client_item, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 1120 IOTHUB_IDENTITY_INFO identity_info;
AzureIoTClient 53:1e5a1ca1f274 1121 identity_info.device_twin = queue_data;
AzureIoTClient 53:1e5a1ca1f274 1122 IOTHUB_PROCESS_ITEM_RESULT process_results = handleData->IoTHubTransport_ProcessItem(handleData->transportHandle, IOTHUB_TYPE_DEVICE_TWIN, &identity_info);
AzureIoTClient 53:1e5a1ca1f274 1123 if (process_results == IOTHUB_PROCESS_CONTINUE || process_results == IOTHUB_PROCESS_NOT_CONNECTED)
AzureIoTClient 53:1e5a1ca1f274 1124 {
AzureIoTClient 53:1e5a1ca1f274 1125 /*Codes_SRS_IOTHUBCLIENT_LL_07_010: [ If 'IoTHubTransport_ProcessItem' returns IOTHUB_PROCESS_CONTINUE or IOTHUB_PROCESS_NOT_CONNECTED IoTHubClient_LL_DoWork shall continue on to call the underlaying layer's _DoWork function. ]*/
AzureIoTClient 53:1e5a1ca1f274 1126 break;
AzureIoTClient 53:1e5a1ca1f274 1127 }
AzureIoTClient 53:1e5a1ca1f274 1128 else
AzureIoTClient 53:1e5a1ca1f274 1129 {
AzureIoTClient 53:1e5a1ca1f274 1130 DList_RemoveEntryList(client_item);
AzureIoTClient 53:1e5a1ca1f274 1131 if (process_results == IOTHUB_PROCESS_OK)
AzureIoTClient 53:1e5a1ca1f274 1132 {
AzureIoTClient 53:1e5a1ca1f274 1133 /*Codes_SRS_IOTHUBCLIENT_LL_07_011: [ If 'IoTHubTransport_ProcessItem' returns IOTHUB_PROCESS_OK IoTHubClient_LL_DoWork shall add the IOTHUB_DEVICE_TWIN to the ack queue. ]*/
AzureIoTClient 53:1e5a1ca1f274 1134 DList_InsertTailList(&(iotHubClientHandle->iot_ack_queue), &(queue_data->entry));
AzureIoTClient 53:1e5a1ca1f274 1135 }
AzureIoTClient 53:1e5a1ca1f274 1136 else
AzureIoTClient 53:1e5a1ca1f274 1137 {
AzureIoTClient 53:1e5a1ca1f274 1138 /*Codes_SRS_IOTHUBCLIENT_LL_07_012: [ If 'IoTHubTransport_ProcessItem' returns any other value IoTHubClient_LL_DoWork shall destroy the IOTHUB_DEVICE_TWIN item. ]*/
AzureIoTClient 53:1e5a1ca1f274 1139 LogError("Failure queue processing item");
AzureIoTClient 53:1e5a1ca1f274 1140 device_twin_data_destroy(queue_data);
AzureIoTClient 53:1e5a1ca1f274 1141 }
AzureIoTClient 53:1e5a1ca1f274 1142 }
AzureIoTClient 53:1e5a1ca1f274 1143 // Move along to the next item
AzureIoTClient 53:1e5a1ca1f274 1144 client_item = next_item;
AzureIoTClient 53:1e5a1ca1f274 1145 }
AzureIoTClient 53:1e5a1ca1f274 1146
AzureIoTClient 42:448eecc3676e 1147 /*Codes_SRS_IOTHUBCLIENT_LL_02_021: [Otherwise, IoTHubClient_LL_DoWork shall invoke the underlaying layer's _DoWork function.]*/
AzureIoTClient 42:448eecc3676e 1148 handleData->IoTHubTransport_DoWork(handleData->transportHandle, iotHubClientHandle);
AzureIoTClient 42:448eecc3676e 1149 }
AzureIoTClient 16:deba40344375 1150 }
AzureIoTClient 16:deba40344375 1151
AzureIoTClient 16:deba40344375 1152 IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetSendStatus(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 16:deba40344375 1153 {
AzureIoTClient 42:448eecc3676e 1154 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1155
AzureIoTClient 42:448eecc3676e 1156 /* Codes_SRS_IOTHUBCLIENT_09_007: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter] */
AzureIoTClient 42:448eecc3676e 1157 if (iotHubClientHandle == NULL || iotHubClientStatus == NULL)
AzureIoTClient 42:448eecc3676e 1158 {
AzureIoTClient 42:448eecc3676e 1159 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 45:54c11b1b1407 1160 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 1161 }
AzureIoTClient 42:448eecc3676e 1162 else
AzureIoTClient 42:448eecc3676e 1163 {
AzureIoTClient 42:448eecc3676e 1164 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1165
AzureIoTClient 42:448eecc3676e 1166 /* Codes_SRS_IOTHUBCLIENT_09_008: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_IDLE if there is currently no items to be sent] */
AzureIoTClient 42:448eecc3676e 1167 /* Codes_SRS_IOTHUBCLIENT_09_009: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_BUSY if there are currently items to be sent] */
AzureIoTClient 42:448eecc3676e 1168 result = handleData->IoTHubTransport_GetSendStatus(handleData->deviceHandle, iotHubClientStatus);
AzureIoTClient 42:448eecc3676e 1169 }
AzureIoTClient 16:deba40344375 1170
AzureIoTClient 42:448eecc3676e 1171 return result;
AzureIoTClient 16:deba40344375 1172 }
AzureIoTClient 16:deba40344375 1173
Azure.IoT Build 45:54c11b1b1407 1174 void IoTHubClient_LL_SendComplete(IOTHUB_CLIENT_LL_HANDLE handle, PDLIST_ENTRY completed, IOTHUB_CLIENT_CONFIRMATION_RESULT result)
AzureIoTClient 16:deba40344375 1175 {
AzureIoTClient 42:448eecc3676e 1176 /*Codes_SRS_IOTHUBCLIENT_LL_02_022: [If parameter completed is NULL, or parameter handle is NULL then IoTHubClient_LL_SendBatch shall return.]*/
AzureIoTClient 42:448eecc3676e 1177 if (
AzureIoTClient 42:448eecc3676e 1178 (handle == NULL) ||
AzureIoTClient 42:448eecc3676e 1179 (completed == NULL)
AzureIoTClient 42:448eecc3676e 1180 )
AzureIoTClient 42:448eecc3676e 1181 {
AzureIoTClient 42:448eecc3676e 1182 /*"shall return"*/
AzureIoTClient 42:448eecc3676e 1183 LogError("invalid arg");
AzureIoTClient 42:448eecc3676e 1184 }
AzureIoTClient 42:448eecc3676e 1185 else
AzureIoTClient 42:448eecc3676e 1186 {
Azure.IoT Build 45:54c11b1b1407 1187 /*Codes_SRS_IOTHUBCLIENT_LL_02_027: [If parameter result is IOTHUB_CLIENT_CONFIRMATION_ERROR then IoTHubClient_LL_SendComplete shall call all the non-NULL callbacks with the result parameter set to IOTHUB_CLIENT_CONFIRMATION_ERROR and the context set to the context passed originally in the SendEventAsync call.] */
Azure.IoT Build 45:54c11b1b1407 1188 /*Codes_SRS_IOTHUBCLIENT_LL_02_025: [If parameter result is IOTHUB_CLIENT_CONFIRMATION_OK then IoTHubClient_LL_SendComplete shall call all the non-NULL callbacks with the result parameter set to IOTHUB_CLIENT_CONFIRMATION_OK and the context set to the context passed originally in the SendEventAsync call.]*/
AzureIoTClient 42:448eecc3676e 1189 PDLIST_ENTRY oldest;
AzureIoTClient 42:448eecc3676e 1190 while ((oldest = DList_RemoveHeadList(completed)) != completed)
AzureIoTClient 42:448eecc3676e 1191 {
AzureIoTClient 42:448eecc3676e 1192 IOTHUB_MESSAGE_LIST* messageList = (IOTHUB_MESSAGE_LIST*)containingRecord(oldest, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 42:448eecc3676e 1193 /*Codes_SRS_IOTHUBCLIENT_LL_02_026: [If any callback is NULL then there shall not be a callback call.]*/
AzureIoTClient 42:448eecc3676e 1194 if (messageList->callback != NULL)
AzureIoTClient 42:448eecc3676e 1195 {
Azure.IoT Build 45:54c11b1b1407 1196 messageList->callback(result, messageList->context);
AzureIoTClient 42:448eecc3676e 1197 }
AzureIoTClient 42:448eecc3676e 1198 IoTHubMessage_Destroy(messageList->messageHandle);
AzureIoTClient 42:448eecc3676e 1199 free(messageList);
AzureIoTClient 42:448eecc3676e 1200 }
AzureIoTClient 42:448eecc3676e 1201 }
AzureIoTClient 16:deba40344375 1202 }
AzureIoTClient 16:deba40344375 1203
AzureIoTClient 55:59b527ab3452 1204 int IoTHubClient_LL_DeviceMethodComplete(IOTHUB_CLIENT_LL_HANDLE handle, const char* method_name, const unsigned char* payLoad, size_t size, METHOD_HANDLE response_id)
AzureIoTClient 53:1e5a1ca1f274 1205 {
AzureIoTClient 53:1e5a1ca1f274 1206 int result;
AzureIoTClient 55:59b527ab3452 1207 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1208 {
AzureIoTClient 53:1e5a1ca1f274 1209 /* Codes_SRS_IOTHUBCLIENT_LL_07_017: [ If handle or response is NULL then IoTHubClient_LL_DeviceMethodComplete shall return 500. ] */
AzureIoTClient 53:1e5a1ca1f274 1210 LogError("Invalid argument handle=%p", handle);
AzureIoTClient 60:41648c4e7036 1211 result = __FAILURE__;
AzureIoTClient 53:1e5a1ca1f274 1212 }
AzureIoTClient 53:1e5a1ca1f274 1213 else
AzureIoTClient 53:1e5a1ca1f274 1214 {
AzureIoTClient 53:1e5a1ca1f274 1215 /* Codes_SRS_IOTHUBCLIENT_LL_07_018: [ If deviceMethodCallback is not NULL IoTHubClient_LL_DeviceMethodComplete shall execute deviceMethodCallback and return the status. ] */
AzureIoTClient 53:1e5a1ca1f274 1216 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 62:5a4cdacf5090 1217 switch (handleData->methodCallback.type)
AzureIoTClient 53:1e5a1ca1f274 1218 {
AzureIoTClient 62:5a4cdacf5090 1219 case CALLBACK_TYPE_SYNC:
AzureIoTClient 53:1e5a1ca1f274 1220 {
AzureIoTClient 62:5a4cdacf5090 1221 unsigned char* payload_resp = NULL;
AzureIoTClient 62:5a4cdacf5090 1222 size_t response_size = 0;
AzureIoTClient 62:5a4cdacf5090 1223 result = handleData->methodCallback.callbackSync(method_name, payLoad, size, &payload_resp, &response_size, handleData->methodCallback.userContextCallback);
AzureIoTClient 62:5a4cdacf5090 1224 /* Codes_SRS_IOTHUBCLIENT_LL_07_020: [ deviceMethodCallback shall build the BUFFER_HANDLE with the response payload from the IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC callback. ] */
AzureIoTClient 62:5a4cdacf5090 1225 if (payload_resp != NULL && response_size > 0)
AzureIoTClient 62:5a4cdacf5090 1226 {
AzureIoTClient 62:5a4cdacf5090 1227 result = handleData->IoTHubTransport_DeviceMethod_Response(handleData->deviceHandle, response_id, payload_resp, response_size, result);
AzureIoTClient 62:5a4cdacf5090 1228 }
AzureIoTClient 62:5a4cdacf5090 1229 else
AzureIoTClient 62:5a4cdacf5090 1230 {
AzureIoTClient 62:5a4cdacf5090 1231 result = __FAILURE__;
AzureIoTClient 62:5a4cdacf5090 1232 }
AzureIoTClient 62:5a4cdacf5090 1233 if (payload_resp != NULL)
AzureIoTClient 62:5a4cdacf5090 1234 {
AzureIoTClient 62:5a4cdacf5090 1235 free(payload_resp);
AzureIoTClient 62:5a4cdacf5090 1236 }
AzureIoTClient 62:5a4cdacf5090 1237 break;
AzureIoTClient 53:1e5a1ca1f274 1238 }
AzureIoTClient 62:5a4cdacf5090 1239 case CALLBACK_TYPE_ASYNC:
AzureIoTClient 62:5a4cdacf5090 1240 result = handleData->methodCallback.callbackAsync(method_name, payLoad, size, response_id, handleData->methodCallback.userContextCallback);
AzureIoTClient 62:5a4cdacf5090 1241 break;
AzureIoTClient 62:5a4cdacf5090 1242 default:
AzureIoTClient 62:5a4cdacf5090 1243 /* Codes_SRS_IOTHUBCLIENT_LL_07_019: [ If deviceMethodCallback is NULL IoTHubClient_LL_DeviceMethodComplete shall return 404. ] */
AzureIoTClient 62:5a4cdacf5090 1244 result = 0;
AzureIoTClient 62:5a4cdacf5090 1245 break;
AzureIoTClient 53:1e5a1ca1f274 1246 }
AzureIoTClient 53:1e5a1ca1f274 1247 }
AzureIoTClient 53:1e5a1ca1f274 1248 return result;
AzureIoTClient 53:1e5a1ca1f274 1249 }
AzureIoTClient 53:1e5a1ca1f274 1250
AzureIoTClient 53:1e5a1ca1f274 1251 void IoTHubClient_LL_RetrievePropertyComplete(IOTHUB_CLIENT_LL_HANDLE handle, DEVICE_TWIN_UPDATE_STATE update_state, const unsigned char* payLoad, size_t size)
AzureIoTClient 53:1e5a1ca1f274 1252 {
AzureIoTClient 53:1e5a1ca1f274 1253 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1254 {
AzureIoTClient 53:1e5a1ca1f274 1255 /* Codes_SRS_IOTHUBCLIENT_LL_07_013: [ If handle is NULL then IoTHubClient_LL_RetrievePropertyComplete shall do nothing.] */
AzureIoTClient 53:1e5a1ca1f274 1256 LogError("Invalid argument handle=%p", handle);
AzureIoTClient 53:1e5a1ca1f274 1257 }
AzureIoTClient 53:1e5a1ca1f274 1258 else
AzureIoTClient 53:1e5a1ca1f274 1259 {
AzureIoTClient 53:1e5a1ca1f274 1260 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 53:1e5a1ca1f274 1261 /* Codes_SRS_IOTHUBCLIENT_LL_07_014: [ If deviceTwinCallback is NULL then IoTHubClient_LL_RetrievePropertyComplete shall do nothing.] */
AzureIoTClient 53:1e5a1ca1f274 1262 if (handleData->deviceTwinCallback)
AzureIoTClient 53:1e5a1ca1f274 1263 {
AzureIoTClient 53:1e5a1ca1f274 1264 /* Codes_SRS_IOTHUBCLIENT_LL_07_015: [ If the the update_state parameter is DEVICE_TWIN_UPDATE_PARTIAL and a DEVICE_TWIN_UPDATE_COMPLETE has not been previously recieved then IoTHubClient_LL_RetrievePropertyComplete shall do nothing.] */
AzureIoTClient 53:1e5a1ca1f274 1265 if (update_state == DEVICE_TWIN_UPDATE_COMPLETE)
AzureIoTClient 53:1e5a1ca1f274 1266 {
AzureIoTClient 53:1e5a1ca1f274 1267 handleData->complete_twin_update_encountered = true;
AzureIoTClient 53:1e5a1ca1f274 1268 }
AzureIoTClient 53:1e5a1ca1f274 1269 if (handleData->complete_twin_update_encountered)
AzureIoTClient 53:1e5a1ca1f274 1270 {
AzureIoTClient 53:1e5a1ca1f274 1271 /* Codes_SRS_IOTHUBCLIENT_LL_07_016: [ If deviceTwinCallback is set and DEVICE_TWIN_UPDATE_COMPLETE has been encountered then IoTHubClient_LL_RetrievePropertyComplete shall call deviceTwinCallback.] */
AzureIoTClient 53:1e5a1ca1f274 1272 handleData->deviceTwinCallback(update_state, payLoad, size, handleData->deviceTwinContextCallback);
AzureIoTClient 53:1e5a1ca1f274 1273 }
AzureIoTClient 53:1e5a1ca1f274 1274 }
AzureIoTClient 53:1e5a1ca1f274 1275 }
AzureIoTClient 53:1e5a1ca1f274 1276 }
AzureIoTClient 53:1e5a1ca1f274 1277
AzureIoTClient 53:1e5a1ca1f274 1278 void IoTHubClient_LL_ReportedStateComplete(IOTHUB_CLIENT_LL_HANDLE handle, uint32_t item_id, int status_code)
AzureIoTClient 53:1e5a1ca1f274 1279 {
AzureIoTClient 53:1e5a1ca1f274 1280 /* Codes_SRS_IOTHUBCLIENT_LL_07_002: [ if handle or queue_handle are NULL then IoTHubClient_LL_ReportedStateComplete shall do nothing. ] */
AzureIoTClient 53:1e5a1ca1f274 1281 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1282 {
AzureIoTClient 53:1e5a1ca1f274 1283 /*"shall return"*/
AzureIoTClient 53:1e5a1ca1f274 1284 LogError("Invalid argument handle=%p", handle);
AzureIoTClient 53:1e5a1ca1f274 1285 }
AzureIoTClient 53:1e5a1ca1f274 1286 else
AzureIoTClient 53:1e5a1ca1f274 1287 {
AzureIoTClient 53:1e5a1ca1f274 1288 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 53:1e5a1ca1f274 1289
AzureIoTClient 53:1e5a1ca1f274 1290 /* Codes_SRS_IOTHUBCLIENT_LL_07_003: [ IoTHubClient_LL_ReportedStateComplete shall enumerate through the IOTHUB_DEVICE_TWIN structures in queue_handle. ]*/
AzureIoTClient 53:1e5a1ca1f274 1291 DLIST_ENTRY* client_item = handleData->iot_ack_queue.Flink;
AzureIoTClient 53:1e5a1ca1f274 1292 while (client_item != &(handleData->iot_ack_queue)) /*while we are not at the end of the list*/
AzureIoTClient 53:1e5a1ca1f274 1293 {
AzureIoTClient 53:1e5a1ca1f274 1294 PDLIST_ENTRY next_item = client_item->Flink;
AzureIoTClient 53:1e5a1ca1f274 1295 IOTHUB_DEVICE_TWIN* queue_data = containingRecord(client_item, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 1296 if (queue_data->item_id == item_id)
AzureIoTClient 53:1e5a1ca1f274 1297 {
AzureIoTClient 53:1e5a1ca1f274 1298 if (queue_data->reported_state_callback != NULL)
AzureIoTClient 53:1e5a1ca1f274 1299 {
AzureIoTClient 53:1e5a1ca1f274 1300 queue_data->reported_state_callback(status_code, queue_data->context);
AzureIoTClient 53:1e5a1ca1f274 1301 }
AzureIoTClient 53:1e5a1ca1f274 1302 /*Codes_SRS_IOTHUBCLIENT_LL_07_009: [ IoTHubClient_LL_ReportedStateComplete shall remove the IOTHUB_DEVICE_TWIN item from the ack queue.]*/
AzureIoTClient 53:1e5a1ca1f274 1303 DList_RemoveEntryList(client_item);
AzureIoTClient 53:1e5a1ca1f274 1304 device_twin_data_destroy(queue_data);
AzureIoTClient 53:1e5a1ca1f274 1305 break;
AzureIoTClient 53:1e5a1ca1f274 1306 }
AzureIoTClient 53:1e5a1ca1f274 1307 client_item = next_item;
AzureIoTClient 53:1e5a1ca1f274 1308 }
AzureIoTClient 53:1e5a1ca1f274 1309 }
AzureIoTClient 53:1e5a1ca1f274 1310 }
AzureIoTClient 53:1e5a1ca1f274 1311
AzureIoTClient 61:8b85a4e797cf 1312 bool IoTHubClient_LL_MessageCallback(IOTHUB_CLIENT_LL_HANDLE handle, MESSAGE_CALLBACK_INFO* messageData)
AzureIoTClient 16:deba40344375 1313 {
AzureIoTClient 61:8b85a4e797cf 1314 bool result;
AzureIoTClient 61:8b85a4e797cf 1315 if ((handle == NULL) || messageData == NULL)
AzureIoTClient 42:448eecc3676e 1316 {
AzureIoTClient 61:8b85a4e797cf 1317 /*Codes_SRS_IOTHUBCLIENT_LL_02_029: [If parameter handle is NULL then IoTHubClient_LL_MessageCallback shall return IOTHUBMESSAGE_ABANDONED.] */
AzureIoTClient 61:8b85a4e797cf 1318 LogError("invalid argument: handle(%p), messageData(%p)", handle, messageData);
AzureIoTClient 61:8b85a4e797cf 1319 result = false;
AzureIoTClient 61:8b85a4e797cf 1320 }
AzureIoTClient 61:8b85a4e797cf 1321 else if (messageData->messageHandle == NULL)
AzureIoTClient 61:8b85a4e797cf 1322 {
AzureIoTClient 61:8b85a4e797cf 1323 /*Codes_SRS_IOTHUBCLIENT_LL_10_004: [If messageHandle field of paramger messageData is NULL then IoTHubClient_LL_MessageCallback shall return IOTHUBMESSAGE_ABANDONED.] */
AzureIoTClient 61:8b85a4e797cf 1324 LogError("invalid argument messageData->messageHandle(NULL)");
AzureIoTClient 61:8b85a4e797cf 1325 result = false;
AzureIoTClient 42:448eecc3676e 1326 }
AzureIoTClient 42:448eecc3676e 1327 else
AzureIoTClient 42:448eecc3676e 1328 {
AzureIoTClient 42:448eecc3676e 1329 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 16:deba40344375 1330
AzureIoTClient 42:448eecc3676e 1331 /* Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClient_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] */
AzureIoTClient 42:448eecc3676e 1332 handleData->lastMessageReceiveTime = get_time(NULL);
AzureIoTClient 62:5a4cdacf5090 1333 switch (handleData->messageCallback.type)
AzureIoTClient 42:448eecc3676e 1334 {
AzureIoTClient 62:5a4cdacf5090 1335 case CALLBACK_TYPE_NONE:
AzureIoTClient 61:8b85a4e797cf 1336 {
AzureIoTClient 61:8b85a4e797cf 1337 /*Codes_SRS_IOTHUBCLIENT_LL_02_032: [If the client is not subscribed to receive messages then IoTHubClient_LL_MessageCallback shall return false.] */
AzureIoTClient 61:8b85a4e797cf 1338 LogError("Invalid workflow - not currently set up to accept messages");
AzureIoTClient 61:8b85a4e797cf 1339 result = false;
AzureIoTClient 61:8b85a4e797cf 1340 break;
AzureIoTClient 61:8b85a4e797cf 1341 }
AzureIoTClient 62:5a4cdacf5090 1342 case CALLBACK_TYPE_SYNC:
AzureIoTClient 61:8b85a4e797cf 1343 {
AzureIoTClient 61:8b85a4e797cf 1344 /*Codes_SRS_IOTHUBCLIENT_LL_02_030: [If messageCallbackType is LEGACY then IoTHubClient_LL_MessageCallback shall invoke the last callback function (the parameter messageCallback to IoTHubClient_LL_SetMessageCallback) passing the message and the passed userContextCallback.]*/
AzureIoTClient 62:5a4cdacf5090 1345 IOTHUBMESSAGE_DISPOSITION_RESULT cb_result = handleData->messageCallback.callbackSync(messageData->messageHandle, handleData->messageCallback.userContextCallback);
AzureIoTClient 61:8b85a4e797cf 1346
AzureIoTClient 61:8b85a4e797cf 1347 /*Codes_SRS_IOTHUBCLIENT_LL_10_007: [If messageCallbackType is LEGACY then IoTHubClient_LL_MessageCallback shall send the message disposition as returned by the client to the underlying layer.] */
AzureIoTClient 61:8b85a4e797cf 1348 if (handleData->IoTHubTransport_SendMessageDisposition(messageData, cb_result) != IOTHUB_CLIENT_OK)
AzureIoTClient 61:8b85a4e797cf 1349 {
AzureIoTClient 61:8b85a4e797cf 1350 LogError("IoTHubTransport_SendMessageDisposition failed");
AzureIoTClient 61:8b85a4e797cf 1351 }
AzureIoTClient 61:8b85a4e797cf 1352 result = true;
AzureIoTClient 61:8b85a4e797cf 1353 break;
AzureIoTClient 61:8b85a4e797cf 1354 }
AzureIoTClient 62:5a4cdacf5090 1355 case CALLBACK_TYPE_ASYNC:
AzureIoTClient 61:8b85a4e797cf 1356 {
AzureIoTClient 61:8b85a4e797cf 1357 /* Codes_SRS_IOTHUBCLIENT_LL_10_009: [If messageCallbackType is ASYNC then IoTHubClient_LL_MessageCallback shall return what messageCallbacEx returns.] */
AzureIoTClient 62:5a4cdacf5090 1358 result = handleData->messageCallback.callbackAsync(messageData, handleData->messageCallback.userContextCallback);
AzureIoTClient 61:8b85a4e797cf 1359 if (!result)
AzureIoTClient 61:8b85a4e797cf 1360 {
AzureIoTClient 61:8b85a4e797cf 1361 LogError("messageCallbackEx failed");
AzureIoTClient 61:8b85a4e797cf 1362 }
AzureIoTClient 61:8b85a4e797cf 1363 break;
AzureIoTClient 61:8b85a4e797cf 1364 }
AzureIoTClient 61:8b85a4e797cf 1365 default:
AzureIoTClient 61:8b85a4e797cf 1366 {
AzureIoTClient 61:8b85a4e797cf 1367 LogError("Invalid state");
AzureIoTClient 61:8b85a4e797cf 1368 result = false;
AzureIoTClient 61:8b85a4e797cf 1369 break;
AzureIoTClient 61:8b85a4e797cf 1370 }
AzureIoTClient 42:448eecc3676e 1371 }
AzureIoTClient 42:448eecc3676e 1372 }
AzureIoTClient 61:8b85a4e797cf 1373 return result;
AzureIoTClient 16:deba40344375 1374 }
AzureIoTClient 16:deba40344375 1375
AzureIoTClient 61:8b85a4e797cf 1376 void IoTHubClient_LL_ConnectionStatusCallBack(IOTHUB_CLIENT_LL_HANDLE handle, IOTHUB_CLIENT_CONNECTION_STATUS status, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason)
AzureIoTClient 52:1cc3c6d07cad 1377 {
AzureIoTClient 61:8b85a4e797cf 1378 /*Codes_SRS_IOTHUBCLIENT_LL_25_113: [If parameter connectionStatus is NULL or parameter handle is NULL then IoTHubClient_LL_ConnectionStatusCallBack shall return.]*/
AzureIoTClient 53:1e5a1ca1f274 1379 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1380 {
AzureIoTClient 53:1e5a1ca1f274 1381 /*"shall return"*/
AzureIoTClient 53:1e5a1ca1f274 1382 LogError("invalid arg");
AzureIoTClient 53:1e5a1ca1f274 1383 }
AzureIoTClient 53:1e5a1ca1f274 1384 else
AzureIoTClient 53:1e5a1ca1f274 1385 {
AzureIoTClient 53:1e5a1ca1f274 1386 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 53:1e5a1ca1f274 1387
AzureIoTClient 61:8b85a4e797cf 1388 /*Codes_SRS_IOTHUBCLIENT_LL_25_114: [IoTHubClient_LL_ConnectionStatusCallBack shall call non-callback set by the user from IoTHubClient_LL_SetConnectionStatusCallback passing the status, reason and the passed userContextCallback.]*/
AzureIoTClient 53:1e5a1ca1f274 1389 if (handleData->conStatusCallback != NULL)
AzureIoTClient 53:1e5a1ca1f274 1390 {
AzureIoTClient 53:1e5a1ca1f274 1391 handleData->conStatusCallback(status, reason, handleData->conStatusUserContextCallback);
AzureIoTClient 53:1e5a1ca1f274 1392 }
AzureIoTClient 53:1e5a1ca1f274 1393 }
AzureIoTClient 53:1e5a1ca1f274 1394
AzureIoTClient 52:1cc3c6d07cad 1395 }
AzureIoTClient 52:1cc3c6d07cad 1396
AzureIoTClient 52:1cc3c6d07cad 1397 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetConnectionStatusCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK connectionStatusCallback, void * userContextCallback)
AzureIoTClient 52:1cc3c6d07cad 1398 {
AzureIoTClient 53:1e5a1ca1f274 1399 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1400 /*Codes_SRS_IOTHUBCLIENT_LL_25_111: [IoTHubClient_LL_SetConnectionStatusCallback shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter iotHubClientHandle**]** */
AzureIoTClient 53:1e5a1ca1f274 1401 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1402 {
AzureIoTClient 53:1e5a1ca1f274 1403 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1404 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1405 }
AzureIoTClient 53:1e5a1ca1f274 1406 else
AzureIoTClient 53:1e5a1ca1f274 1407 {
AzureIoTClient 53:1e5a1ca1f274 1408 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1409 /*Codes_SRS_IOTHUBCLIENT_LL_25_112: [IoTHubClient_LL_SetConnectionStatusCallback shall return IOTHUB_CLIENT_OK and save the callback and userContext as a member of the handle.] */
AzureIoTClient 53:1e5a1ca1f274 1410 handleData->conStatusCallback = connectionStatusCallback;
AzureIoTClient 53:1e5a1ca1f274 1411 handleData->conStatusUserContextCallback = userContextCallback;
AzureIoTClient 53:1e5a1ca1f274 1412 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1413 }
AzureIoTClient 52:1cc3c6d07cad 1414
AzureIoTClient 52:1cc3c6d07cad 1415 return result;
AzureIoTClient 52:1cc3c6d07cad 1416 }
AzureIoTClient 52:1cc3c6d07cad 1417
AzureIoTClient 53:1e5a1ca1f274 1418 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetRetryPolicy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1419 {
AzureIoTClient 53:1e5a1ca1f274 1420 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1421 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 52:1cc3c6d07cad 1422
AzureIoTClient 62:5a4cdacf5090 1423 /* Codes_SRS_IOTHUBCLIENT_LL_25_116: [If iotHubClientHandle, retryPolicy or retryTimeoutLimitinSeconds is NULL, IoTHubClient_LL_GetRetryPolicy shall return IOTHUB_CLIENT_INVALID_ARG]*/
AzureIoTClient 53:1e5a1ca1f274 1424 if (handleData == NULL)
AzureIoTClient 53:1e5a1ca1f274 1425 {
AzureIoTClient 53:1e5a1ca1f274 1426 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1427 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1428 }
AzureIoTClient 53:1e5a1ca1f274 1429 else
AzureIoTClient 53:1e5a1ca1f274 1430 {
AzureIoTClient 53:1e5a1ca1f274 1431 if (handleData->transportHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1432 {
AzureIoTClient 53:1e5a1ca1f274 1433 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1434 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1435 }
AzureIoTClient 53:1e5a1ca1f274 1436 else
AzureIoTClient 53:1e5a1ca1f274 1437 {
AzureIoTClient 53:1e5a1ca1f274 1438 if (handleData->IoTHubTransport_SetRetryPolicy(handleData->transportHandle, retryPolicy, retryTimeoutLimitInSeconds) != 0)
AzureIoTClient 53:1e5a1ca1f274 1439 {
AzureIoTClient 53:1e5a1ca1f274 1440 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1441 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1442 }
AzureIoTClient 53:1e5a1ca1f274 1443 else
AzureIoTClient 53:1e5a1ca1f274 1444 {
AzureIoTClient 62:5a4cdacf5090 1445 /* Codes_SRS_IOTHUBCLIENT_LL_25_118: [IoTHubClient_LL_SetRetryPolicy shall save connection retry policies specified by the user to retryPolicy in struct IOTHUB_CLIENT_LL_HANDLE_DATA] */
AzureIoTClient 62:5a4cdacf5090 1446 /* Codes_SRS_IOTHUBCLIENT_LL_25_119: [IoTHubClient_LL_SetRetryPolicy shall save retryTimeoutLimitInSeconds in seconds to retryTimeout in struct IOTHUB_CLIENT_LL_HANDLE_DATA] */
AzureIoTClient 53:1e5a1ca1f274 1447 handleData->retryPolicy = retryPolicy;
AzureIoTClient 53:1e5a1ca1f274 1448 handleData->retryTimeoutLimitInSeconds = retryTimeoutLimitInSeconds;
AzureIoTClient 53:1e5a1ca1f274 1449 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1450 }
AzureIoTClient 53:1e5a1ca1f274 1451 }
AzureIoTClient 53:1e5a1ca1f274 1452 }
AzureIoTClient 52:1cc3c6d07cad 1453 return result;
AzureIoTClient 52:1cc3c6d07cad 1454 }
AzureIoTClient 52:1cc3c6d07cad 1455
AzureIoTClient 53:1e5a1ca1f274 1456 IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetRetryPolicy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY* retryPolicy, size_t* retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1457 {
AzureIoTClient 62:5a4cdacf5090 1458 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1459
AzureIoTClient 53:1e5a1ca1f274 1460 /* Codes_SRS_IOTHUBCLIENT_LL_09_001: [IoTHubClient_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG if any of the arguments is NULL] */
AzureIoTClient 62:5a4cdacf5090 1461 if (iotHubClientHandle == NULL || retryPolicy == NULL || retryTimeoutLimitInSeconds == NULL)
AzureIoTClient 53:1e5a1ca1f274 1462 {
AzureIoTClient 62:5a4cdacf5090 1463 LogError("Invalid parameter IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = %p, IOTHUB_CLIENT_RETRY_POLICY* retryPolicy = %p, size_t* retryTimeoutLimitInSeconds = %p", iotHubClientHandle, retryPolicy, retryTimeoutLimitInSeconds);
AzureIoTClient 53:1e5a1ca1f274 1464 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1465 }
AzureIoTClient 53:1e5a1ca1f274 1466 else
AzureIoTClient 53:1e5a1ca1f274 1467 {
AzureIoTClient 62:5a4cdacf5090 1468 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 62:5a4cdacf5090 1469
AzureIoTClient 53:1e5a1ca1f274 1470 *retryPolicy = handleData->retryPolicy;
AzureIoTClient 53:1e5a1ca1f274 1471 *retryTimeoutLimitInSeconds = handleData->retryTimeoutLimitInSeconds;
AzureIoTClient 53:1e5a1ca1f274 1472 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1473 }
AzureIoTClient 52:1cc3c6d07cad 1474
AzureIoTClient 52:1cc3c6d07cad 1475 return result;
AzureIoTClient 52:1cc3c6d07cad 1476 }
AzureIoTClient 52:1cc3c6d07cad 1477
AzureIoTClient 16:deba40344375 1478 IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetLastMessageReceiveTime(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, time_t* lastMessageReceiveTime)
AzureIoTClient 16:deba40344375 1479 {
AzureIoTClient 42:448eecc3676e 1480 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1481 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1482
AzureIoTClient 42:448eecc3676e 1483 /* Codes_SRS_IOTHUBCLIENT_LL_09_001: [IoTHubClient_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG if any of the arguments is NULL] */
AzureIoTClient 42:448eecc3676e 1484 if (handleData == NULL || lastMessageReceiveTime == NULL)
AzureIoTClient 42:448eecc3676e 1485 {
AzureIoTClient 42:448eecc3676e 1486 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 45:54c11b1b1407 1487 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 1488 }
AzureIoTClient 42:448eecc3676e 1489 else
AzureIoTClient 42:448eecc3676e 1490 {
AzureIoTClient 42:448eecc3676e 1491 /* Codes_SRS_IOTHUBCLIENT_LL_09_002: [IoTHubClient_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INDEFINITE_TIME - and not set 'lastMessageReceiveTime' - if it is unable to provide the time for the last commands] */
AzureIoTClient 42:448eecc3676e 1492 if (handleData->lastMessageReceiveTime == INDEFINITE_TIME)
AzureIoTClient 42:448eecc3676e 1493 {
AzureIoTClient 42:448eecc3676e 1494 result = IOTHUB_CLIENT_INDEFINITE_TIME;
Azure.IoT Build 45:54c11b1b1407 1495 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 1496 }
AzureIoTClient 42:448eecc3676e 1497 else
AzureIoTClient 42:448eecc3676e 1498 {
AzureIoTClient 42:448eecc3676e 1499 /* Codes_SRS_IOTHUBCLIENT_LL_09_003: [IoTHubClient_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_OK if it wrote in the lastMessageReceiveTime the time when the last command was received] */
AzureIoTClient 42:448eecc3676e 1500 /* Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClient_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] */
AzureIoTClient 42:448eecc3676e 1501 *lastMessageReceiveTime = handleData->lastMessageReceiveTime;
AzureIoTClient 42:448eecc3676e 1502 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 1503 }
AzureIoTClient 42:448eecc3676e 1504 }
AzureIoTClient 16:deba40344375 1505
AzureIoTClient 42:448eecc3676e 1506 return result;
AzureIoTClient 16:deba40344375 1507 }
AzureIoTClient 16:deba40344375 1508
AzureIoTClient 16:deba40344375 1509 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetOption(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* optionName, const void* value)
AzureIoTClient 16:deba40344375 1510 {
AzureIoTClient 40:1a94db9139ea 1511
AzureIoTClient 42:448eecc3676e 1512 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1513 /*Codes_SRS_IOTHUBCLIENT_LL_02_034: [If iotHubClientHandle is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]*/
AzureIoTClient 42:448eecc3676e 1514 /*Codes_SRS_IOTHUBCLIENT_LL_02_035: [If optionName is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 42:448eecc3676e 1515 /*Codes_SRS_IOTHUBCLIENT_LL_02_036: [If value is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 42:448eecc3676e 1516 if (
AzureIoTClient 42:448eecc3676e 1517 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 1518 (optionName == NULL) ||
AzureIoTClient 42:448eecc3676e 1519 (value == NULL)
AzureIoTClient 42:448eecc3676e 1520 )
AzureIoTClient 42:448eecc3676e 1521 {
AzureIoTClient 42:448eecc3676e 1522 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 1523 LogError("invalid argument (NULL)");
AzureIoTClient 42:448eecc3676e 1524 }
AzureIoTClient 42:448eecc3676e 1525 else
AzureIoTClient 42:448eecc3676e 1526 {
AzureIoTClient 42:448eecc3676e 1527 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 1528
AzureIoTClient 42:448eecc3676e 1529 /*Codes_SRS_IOTHUBCLIENT_LL_02_039: [ "messageTimeout" - once IoTHubClient_LL_SendEventAsync is called the message shall timeout after value miliseconds. Value is a pointer to a uint64. ]*/
AzureIoTClient 42:448eecc3676e 1530 if (strcmp(optionName, "messageTimeout") == 0)
AzureIoTClient 42:448eecc3676e 1531 {
AzureIoTClient 42:448eecc3676e 1532 /*this is an option handled by IoTHubClient_LL*/
AzureIoTClient 42:448eecc3676e 1533 /*Codes_SRS_IOTHUBCLIENT_LL_02_043: [ Calling IoTHubClient_LL_SetOption with value set to "0" shall disable the timeout mechanism for all new messages. ]*/
Azure.IoT.Build 54:6dcad9019a64 1534 handleData->currentMessageTimeout = *(const tickcounter_ms_t*)value;
AzureIoTClient 42:448eecc3676e 1535 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 1536 }
AzureIoTClient 66:a419827cb051 1537 else if (strcmp(optionName, OPTION_PRODUCT_INFO) == 0)
AzureIoTClient 66:a419827cb051 1538 {
AzureIoTClient 66:a419827cb051 1539 /*Codes_SRS_IOTHUBCLIENT_LL_10_033: [repeat calls with "product_info" will erase the previously set product information if applicatble. ]*/
AzureIoTClient 66:a419827cb051 1540 if (handleData->product_info != NULL)
AzureIoTClient 66:a419827cb051 1541 {
AzureIoTClient 66:a419827cb051 1542 STRING_delete(handleData->product_info);
AzureIoTClient 66:a419827cb051 1543 handleData->product_info = NULL;
AzureIoTClient 66:a419827cb051 1544 }
AzureIoTClient 66:a419827cb051 1545
AzureIoTClient 66:a419827cb051 1546 /*Codes_SRS_IOTHUBCLIENT_LL_10_035: [If string concatenation fails, `IoTHubClient_LL_SetOption` shall return `IOTHUB_CLIENT_ERRROR`. Otherwise, `IOTHUB_CLIENT_OK` shall be returned. ]*/
AzureIoTClient 66:a419827cb051 1547 handleData->product_info = make_product_info((const char*)value);
AzureIoTClient 66:a419827cb051 1548 if (handleData->product_info == NULL)
AzureIoTClient 66:a419827cb051 1549 {
AzureIoTClient 66:a419827cb051 1550 LogError("STRING_construct_sprintf failed");
AzureIoTClient 66:a419827cb051 1551 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1552 }
AzureIoTClient 66:a419827cb051 1553 else
AzureIoTClient 66:a419827cb051 1554 {
AzureIoTClient 66:a419827cb051 1555 result = IOTHUB_CLIENT_OK;
AzureIoTClient 66:a419827cb051 1556 }
AzureIoTClient 66:a419827cb051 1557 }
AzureIoTClient 42:448eecc3676e 1558 else
AzureIoTClient 42:448eecc3676e 1559 {
AzureIoTClient 46:6a69294b6119 1560
AzureIoTClient 46:6a69294b6119 1561 /*Codes_SRS_IOTHUBCLIENT_LL_02_099: [ IoTHubClient_LL_SetOption shall return according to the table below ]*/
AzureIoTClient 46:6a69294b6119 1562 IOTHUB_CLIENT_RESULT uploadToBlob_result;
AzureIoTClient 46:6a69294b6119 1563 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 46:6a69294b6119 1564 uploadToBlob_result = IoTHubClient_LL_UploadToBlob_SetOption(handleData->uploadToBlobHandle, optionName, value);
AzureIoTClient 46:6a69294b6119 1565 if(uploadToBlob_result == IOTHUB_CLIENT_ERROR)
AzureIoTClient 46:6a69294b6119 1566 {
AzureIoTClient 46:6a69294b6119 1567 LogError("unable to IoTHubClient_LL_UploadToBlob_SetOption");
AzureIoTClient 46:6a69294b6119 1568 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 46:6a69294b6119 1569 }
AzureIoTClient 46:6a69294b6119 1570 #else
AzureIoTClient 46:6a69294b6119 1571 uploadToBlob_result = IOTHUB_CLIENT_INVALID_ARG; /*harmless value (IOTHUB_CLIENT_INVALID_ARG)in the case when uploadtoblob is not compiled in, otherwise whatever IoTHubClient_LL_UploadToBlob_SetOption returned*/
AzureIoTClient 46:6a69294b6119 1572 #endif /*DONT_USE_UPLOADTOBLOB*/
AzureIoTClient 16:deba40344375 1573
AzureIoTClient 46:6a69294b6119 1574
AzureIoTClient 46:6a69294b6119 1575 result =
AzureIoTClient 46:6a69294b6119 1576 /*based on uploadToBlob_result value this is what happens:*/
AzureIoTClient 46:6a69294b6119 1577 /*IOTHUB_CLIENT_INVALID_ARG always returns what IoTHubTransport_SetOption returns*/
AzureIoTClient 46:6a69294b6119 1578 /*IOTHUB_CLIENT_ERROR always returns IOTHUB_CLIENT_ERROR */
AzureIoTClient 46:6a69294b6119 1579 /*IOTHUB_CLIENT_OK returns OK
AzureIoTClient 46:6a69294b6119 1580 IOTHUB_CLIENT_OK if IoTHubTransport_SetOption returns OK or INVALID_ARG
AzureIoTClient 46:6a69294b6119 1581 IOTHUB_CLIENT_ERROR if IoTHubTransport_SetOption returns ERROR*/
AzureIoTClient 46:6a69294b6119 1582
AzureIoTClient 46:6a69294b6119 1583 (uploadToBlob_result == IOTHUB_CLIENT_INVALID_ARG) ? handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value) :
AzureIoTClient 46:6a69294b6119 1584 (uploadToBlob_result == IOTHUB_CLIENT_ERROR) ? IOTHUB_CLIENT_ERROR :
AzureIoTClient 46:6a69294b6119 1585 (handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value) == IOTHUB_CLIENT_ERROR) ? IOTHUB_CLIENT_ERROR : IOTHUB_CLIENT_OK;
AzureIoTClient 46:6a69294b6119 1586
AzureIoTClient 47:aaa262b5f898 1587 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 47:aaa262b5f898 1588 {
AzureIoTClient 47:aaa262b5f898 1589 LogError("underlying transport failed, returned = %s", ENUM_TO_STRING(IOTHUB_CLIENT_RESULT, result));
AzureIoTClient 42:448eecc3676e 1590 }
AzureIoTClient 47:aaa262b5f898 1591 }
AzureIoTClient 42:448eecc3676e 1592 }
AzureIoTClient 42:448eecc3676e 1593 return result;
AzureIoTClient 42:448eecc3676e 1594 }
AzureIoTClient 16:deba40344375 1595
AzureIoTClient 66:a419827cb051 1596 IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetOption(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* optionName, void** value)
AzureIoTClient 66:a419827cb051 1597 {
AzureIoTClient 66:a419827cb051 1598 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 66:a419827cb051 1599
AzureIoTClient 66:a419827cb051 1600 if ((iotHubClientHandle == NULL) || (optionName == NULL) || (value == NULL))
AzureIoTClient 66:a419827cb051 1601 {
AzureIoTClient 66:a419827cb051 1602 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 66:a419827cb051 1603 LogError("invalid argument iotHubClientHandle(%p); optionName(%p); value(%p)", iotHubClientHandle, optionName, value);
AzureIoTClient 66:a419827cb051 1604 }
AzureIoTClient 66:a419827cb051 1605 else if (strcmp(optionName, OPTION_PRODUCT_INFO) == 0)
AzureIoTClient 66:a419827cb051 1606 {
AzureIoTClient 66:a419827cb051 1607 result = IOTHUB_CLIENT_OK;
AzureIoTClient 66:a419827cb051 1608 *value = iotHubClientHandle->product_info;
AzureIoTClient 66:a419827cb051 1609 }
AzureIoTClient 66:a419827cb051 1610 else
AzureIoTClient 66:a419827cb051 1611 {
AzureIoTClient 66:a419827cb051 1612 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 66:a419827cb051 1613 LogError("invalid argument (%s)", optionName);
AzureIoTClient 66:a419827cb051 1614 }
AzureIoTClient 66:a419827cb051 1615 return result;
AzureIoTClient 66:a419827cb051 1616 }
AzureIoTClient 66:a419827cb051 1617
AzureIoTClient 53:1e5a1ca1f274 1618 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetDeviceTwinCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1619 {
AzureIoTClient 53:1e5a1ca1f274 1620 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1621 /* Codes_SRS_IOTHUBCLIENT_LL_10_001: [ IoTHubClient_LL_SetDeviceTwinCallback shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.] */
AzureIoTClient 53:1e5a1ca1f274 1622 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1623 {
AzureIoTClient 53:1e5a1ca1f274 1624 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1625 LogError("Invalid argument specified iothubClientHandle=%p", iotHubClientHandle);
AzureIoTClient 53:1e5a1ca1f274 1626 }
AzureIoTClient 53:1e5a1ca1f274 1627 else
AzureIoTClient 53:1e5a1ca1f274 1628 {
AzureIoTClient 53:1e5a1ca1f274 1629 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1630 if (deviceTwinCallback == NULL)
AzureIoTClient 53:1e5a1ca1f274 1631 {
AzureIoTClient 53:1e5a1ca1f274 1632 /* Codes_SRS_IOTHUBCLIENT_LL_10_006: [ If deviceTwinCallback is NULL, then IoTHubClient_LL_SetDeviceTwinCallback shall call the underlying layer's _Unsubscribe function and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 53:1e5a1ca1f274 1633 handleData->IoTHubTransport_Unsubscribe_DeviceTwin(handleData->transportHandle);
AzureIoTClient 53:1e5a1ca1f274 1634 handleData->deviceTwinCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 1635 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1636 }
AzureIoTClient 53:1e5a1ca1f274 1637 else
AzureIoTClient 53:1e5a1ca1f274 1638 {
AzureIoTClient 53:1e5a1ca1f274 1639 /* Codes_SRS_IOTHUBCLIENT_LL_10_002: [ If deviceTwinCallback is not NULL, then IoTHubClient_LL_SetDeviceTwinCallback shall call the underlying layer's _Subscribe function.] */
AzureIoTClient 53:1e5a1ca1f274 1640 if (handleData->IoTHubTransport_Subscribe_DeviceTwin(handleData->transportHandle) == 0)
AzureIoTClient 53:1e5a1ca1f274 1641 {
AzureIoTClient 53:1e5a1ca1f274 1642 handleData->deviceTwinCallback = deviceTwinCallback;
AzureIoTClient 53:1e5a1ca1f274 1643 handleData->deviceTwinContextCallback = userContextCallback;
AzureIoTClient 53:1e5a1ca1f274 1644 /* Codes_SRS_IOTHUBCLIENT_LL_10_005: [ Otherwise IoTHubClient_LL_SetDeviceTwinCallback shall succeed and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 53:1e5a1ca1f274 1645 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1646 }
AzureIoTClient 53:1e5a1ca1f274 1647 else
AzureIoTClient 53:1e5a1ca1f274 1648 {
AzureIoTClient 53:1e5a1ca1f274 1649 /* Codes_SRS_IOTHUBCLIENT_LL_10_003: [ If the underlying layer's _Subscribe function fails, then IoTHubClient_LL_SetDeviceTwinCallback shall fail and return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 53:1e5a1ca1f274 1650 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1651 }
AzureIoTClient 53:1e5a1ca1f274 1652 }
AzureIoTClient 53:1e5a1ca1f274 1653 }
AzureIoTClient 53:1e5a1ca1f274 1654 return result;
AzureIoTClient 53:1e5a1ca1f274 1655 }
AzureIoTClient 53:1e5a1ca1f274 1656
AzureIoTClient 53:1e5a1ca1f274 1657 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SendReportedState(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* reportedState, size_t size, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK reportedStateCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1658 {
AzureIoTClient 53:1e5a1ca1f274 1659 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1660 /* Codes_SRS_IOTHUBCLIENT_LL_10_012: [ IoTHubClient_LL_SendReportedState shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL. ] */
AzureIoTClient 53:1e5a1ca1f274 1661 /* Codes_SRS_IOTHUBCLIENT_LL_10_013: [ IoTHubClient_LL_SendReportedState shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter reportedState is NULL] */
AzureIoTClient 53:1e5a1ca1f274 1662 /* Codes_SRS_IOTHUBCLIENT_LL_07_005: [ IoTHubClient_LL_SendReportedState shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter size is equal to 0. ] */
AzureIoTClient 53:1e5a1ca1f274 1663 if (iotHubClientHandle == NULL || (reportedState == NULL || size == 0) )
AzureIoTClient 53:1e5a1ca1f274 1664 {
AzureIoTClient 53:1e5a1ca1f274 1665 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1666 LogError("Invalid argument specified iothubClientHandle=%p, reportedState=%p, size=%zu", iotHubClientHandle, reportedState, size);
AzureIoTClient 53:1e5a1ca1f274 1667 }
AzureIoTClient 53:1e5a1ca1f274 1668 else
AzureIoTClient 53:1e5a1ca1f274 1669 {
AzureIoTClient 53:1e5a1ca1f274 1670 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1671 /* Codes_SRS_IOTHUBCLIENT_LL_10_014: [IoTHubClient_LL_SendReportedState shall construct and queue the reported a Device_Twin structure for transmition by the underlying transport.] */
AzureIoTClient 53:1e5a1ca1f274 1672 IOTHUB_DEVICE_TWIN* client_data = dev_twin_data_create(handleData, get_next_item_id(handleData), reportedState, size, reportedStateCallback, userContextCallback);
AzureIoTClient 53:1e5a1ca1f274 1673 if (client_data == NULL)
AzureIoTClient 53:1e5a1ca1f274 1674 {
AzureIoTClient 53:1e5a1ca1f274 1675 /* Codes_SRS_IOTHUBCLIENT_LL_10_015: [If any error is encountered IoTHubClient_LL_SendReportedState shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 53:1e5a1ca1f274 1676 LogError("Failure constructing device twin data");
AzureIoTClient 53:1e5a1ca1f274 1677 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1678 }
AzureIoTClient 53:1e5a1ca1f274 1679 else
AzureIoTClient 53:1e5a1ca1f274 1680 {
AzureIoTClient 53:1e5a1ca1f274 1681 if (handleData->IoTHubTransport_Subscribe_DeviceTwin(handleData->transportHandle) != 0)
AzureIoTClient 53:1e5a1ca1f274 1682 {
AzureIoTClient 53:1e5a1ca1f274 1683 LogError("Failure adding device twin data to queue");
AzureIoTClient 53:1e5a1ca1f274 1684 device_twin_data_destroy(client_data);
AzureIoTClient 53:1e5a1ca1f274 1685 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1686 }
AzureIoTClient 53:1e5a1ca1f274 1687 else
AzureIoTClient 53:1e5a1ca1f274 1688 {
AzureIoTClient 53:1e5a1ca1f274 1689 /* Codes_SRS_IOTHUBCLIENT_LL_07_001: [ IoTHubClient_LL_SendReportedState shall queue the constructed reportedState data to be consumed by the targeted transport. ] */
AzureIoTClient 53:1e5a1ca1f274 1690 DList_InsertTailList(&(iotHubClientHandle->iot_msg_queue), &(client_data->entry));
AzureIoTClient 53:1e5a1ca1f274 1691
AzureIoTClient 53:1e5a1ca1f274 1692 /* Codes_SRS_IOTHUBCLIENT_LL_10_016: [ Otherwise IoTHubClient_LL_SendReportedState shall succeed and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 53:1e5a1ca1f274 1693 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1694 }
AzureIoTClient 53:1e5a1ca1f274 1695 }
AzureIoTClient 53:1e5a1ca1f274 1696 }
AzureIoTClient 53:1e5a1ca1f274 1697 return result;
AzureIoTClient 53:1e5a1ca1f274 1698 }
AzureIoTClient 53:1e5a1ca1f274 1699
AzureIoTClient 53:1e5a1ca1f274 1700 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetDeviceMethodCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC deviceMethodCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1701 {
AzureIoTClient 53:1e5a1ca1f274 1702 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1703
AzureIoTClient 53:1e5a1ca1f274 1704 /*Codes_SRS_IOTHUBCLIENT_LL_12_017: [ IoTHubClient_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL. ] */
AzureIoTClient 53:1e5a1ca1f274 1705 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1706 {
AzureIoTClient 53:1e5a1ca1f274 1707 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1708 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1709 }
AzureIoTClient 53:1e5a1ca1f274 1710 else
AzureIoTClient 53:1e5a1ca1f274 1711 {
AzureIoTClient 53:1e5a1ca1f274 1712 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1713 if (deviceMethodCallback == NULL)
AzureIoTClient 53:1e5a1ca1f274 1714 {
AzureIoTClient 62:5a4cdacf5090 1715 if (handleData->methodCallback.type == CALLBACK_TYPE_NONE)
AzureIoTClient 62:5a4cdacf5090 1716 {
AzureIoTClient 62:5a4cdacf5090 1717 /* Codes_SRS_IOTHUBCLIENT_LL_10_029: [ If deviceMethodCallback is NULL and the client is not subscribed to receive method calls, IoTHubClient_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ] */
AzureIoTClient 62:5a4cdacf5090 1718 LogError("not currently set to accept or process incoming messages.");
AzureIoTClient 62:5a4cdacf5090 1719 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1720 }
AzureIoTClient 62:5a4cdacf5090 1721 else if (handleData->methodCallback.type == CALLBACK_TYPE_ASYNC)
AzureIoTClient 62:5a4cdacf5090 1722 {
AzureIoTClient 62:5a4cdacf5090 1723 /* Codes_SRS_IOTHUBCLIENT_LL_10_028: [If the user has subscribed using IoTHubClient_LL_SetDeviceMethodCallback_Ex, IoTHubClient_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ] */
AzureIoTClient 62:5a4cdacf5090 1724 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetDeviceMethodCallback_Ex function.");
AzureIoTClient 62:5a4cdacf5090 1725 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1726 }
AzureIoTClient 62:5a4cdacf5090 1727 else
AzureIoTClient 62:5a4cdacf5090 1728 {
AzureIoTClient 62:5a4cdacf5090 1729 /*Codes_SRS_IOTHUBCLIENT_LL_02_019: [If parameter messageCallback is NULL then IoTHubClient_LL_SetMessageCallback shall call the underlying layer's _Unsubscribe function and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 62:5a4cdacf5090 1730 /*Codes_SRS_IOTHUBCLIENT_LL_12_018: [If deviceMethodCallback is NULL, then IoTHubClient_LL_SetDeviceMethodCallback shall call the underlying layer's IoTHubTransport_Unsubscribe_DeviceMethod function and return IOTHUB_CLIENT_OK. ] */
AzureIoTClient 62:5a4cdacf5090 1731 /*Codes_SRS_IOTHUBCLIENT_LL_12_022: [ Otherwise IoTHubClient_LL_SetDeviceMethodCallback shall succeed and return IOTHUB_CLIENT_OK. ]*/
AzureIoTClient 62:5a4cdacf5090 1732 handleData->IoTHubTransport_Unsubscribe_DeviceMethod(handleData->transportHandle);
AzureIoTClient 62:5a4cdacf5090 1733 handleData->methodCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 62:5a4cdacf5090 1734 handleData->methodCallback.callbackSync = NULL;
AzureIoTClient 62:5a4cdacf5090 1735 handleData->methodCallback.userContextCallback = NULL;
AzureIoTClient 62:5a4cdacf5090 1736 result = IOTHUB_CLIENT_OK;
AzureIoTClient 62:5a4cdacf5090 1737 }
AzureIoTClient 53:1e5a1ca1f274 1738 }
AzureIoTClient 53:1e5a1ca1f274 1739 else
AzureIoTClient 53:1e5a1ca1f274 1740 {
AzureIoTClient 62:5a4cdacf5090 1741 if (handleData->methodCallback.type == CALLBACK_TYPE_ASYNC)
AzureIoTClient 53:1e5a1ca1f274 1742 {
AzureIoTClient 62:5a4cdacf5090 1743 /* Codes_SRS_IOTHUBCLIENT_LL_10_028: [If the user has subscribed using IoTHubClient_LL_SetDeviceMethodCallback_Ex, IoTHubClient_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ] */
AzureIoTClient 62:5a4cdacf5090 1744 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetDeviceMethodCallback_Ex function before subscribing with IoTHubClient_LL_SetDeviceMethodCallback.");
AzureIoTClient 62:5a4cdacf5090 1745 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1746 }
AzureIoTClient 53:1e5a1ca1f274 1747 else
AzureIoTClient 53:1e5a1ca1f274 1748 {
AzureIoTClient 62:5a4cdacf5090 1749 /*Codes_SRS_IOTHUBCLIENT_LL_12_019: [ If deviceMethodCallback is not NULL, then IoTHubClient_LL_SetDeviceMethodCallback shall call the underlying layer's IoTHubTransport_Subscribe_DeviceMethod function. ]*/
AzureIoTClient 62:5a4cdacf5090 1750 if (handleData->IoTHubTransport_Subscribe_DeviceMethod(handleData->deviceHandle) == 0)
AzureIoTClient 62:5a4cdacf5090 1751 {
AzureIoTClient 62:5a4cdacf5090 1752 /*Codes_SRS_IOTHUBCLIENT_LL_12_022: [ Otherwise IoTHubClient_LL_SetDeviceMethodCallback shall succeed and return IOTHUB_CLIENT_OK. ]*/
AzureIoTClient 62:5a4cdacf5090 1753 handleData->methodCallback.type = CALLBACK_TYPE_SYNC;
AzureIoTClient 62:5a4cdacf5090 1754 handleData->methodCallback.callbackSync = deviceMethodCallback;
AzureIoTClient 62:5a4cdacf5090 1755 handleData->methodCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1756 handleData->methodCallback.userContextCallback = userContextCallback;
AzureIoTClient 62:5a4cdacf5090 1757 result = IOTHUB_CLIENT_OK;
AzureIoTClient 62:5a4cdacf5090 1758 }
AzureIoTClient 62:5a4cdacf5090 1759 else
AzureIoTClient 62:5a4cdacf5090 1760 {
AzureIoTClient 62:5a4cdacf5090 1761 /*Codes_SRS_IOTHUBCLIENT_LL_12_020: [ If the underlying layer's IoTHubTransport_Subscribe_DeviceMethod function fails, then IoTHubClient_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 62:5a4cdacf5090 1762 /*Codes_SRS_IOTHUBCLIENT_LL_12_021: [ If adding the information fails for any reason, IoTHubClient_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 62:5a4cdacf5090 1763 LogError("IoTHubTransport_Subscribe_DeviceMethod failed");
AzureIoTClient 62:5a4cdacf5090 1764 handleData->methodCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 62:5a4cdacf5090 1765 handleData->methodCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1766 handleData->methodCallback.callbackSync = NULL;
AzureIoTClient 62:5a4cdacf5090 1767 handleData->methodCallback.userContextCallback = NULL;
AzureIoTClient 62:5a4cdacf5090 1768 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1769 }
AzureIoTClient 53:1e5a1ca1f274 1770 }
AzureIoTClient 53:1e5a1ca1f274 1771 }
AzureIoTClient 53:1e5a1ca1f274 1772 }
AzureIoTClient 53:1e5a1ca1f274 1773 return result;
AzureIoTClient 53:1e5a1ca1f274 1774 }
AzureIoTClient 53:1e5a1ca1f274 1775
AzureIoTClient 55:59b527ab3452 1776 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetDeviceMethodCallback_Ex(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK inboundDeviceMethodCallback, void* userContextCallback)
AzureIoTClient 55:59b527ab3452 1777 {
AzureIoTClient 55:59b527ab3452 1778 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1779 /* Codes_SRS_IOTHUBCLIENT_LL_07_021: [ If handle is NULL then IoTHubClient_LL_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 55:59b527ab3452 1780 if (iotHubClientHandle == NULL)
AzureIoTClient 55:59b527ab3452 1781 {
AzureIoTClient 55:59b527ab3452 1782 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1783 LOG_ERROR_RESULT;
AzureIoTClient 55:59b527ab3452 1784 }
AzureIoTClient 55:59b527ab3452 1785 else
AzureIoTClient 55:59b527ab3452 1786 {
AzureIoTClient 55:59b527ab3452 1787 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1788 if (inboundDeviceMethodCallback == NULL)
AzureIoTClient 55:59b527ab3452 1789 {
AzureIoTClient 62:5a4cdacf5090 1790 if (handleData->methodCallback.type == CALLBACK_TYPE_NONE)
AzureIoTClient 62:5a4cdacf5090 1791 {
AzureIoTClient 62:5a4cdacf5090 1792 /* Codes_SRS_IOTHUBCLIENT_LL_10_030: [ If deviceMethodCallback is NULL and the client is not subscribed to receive method calls, IoTHubClient_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ] */
AzureIoTClient 62:5a4cdacf5090 1793 LogError("not currently set to accept or process incoming messages.");
AzureIoTClient 62:5a4cdacf5090 1794 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1795 }
AzureIoTClient 62:5a4cdacf5090 1796 else if (handleData->methodCallback.type == CALLBACK_TYPE_SYNC)
AzureIoTClient 62:5a4cdacf5090 1797 {
AzureIoTClient 62:5a4cdacf5090 1798 /* Codes_SRS_IOTHUBCLIENT_LL_10_031: [If the user has subscribed using IoTHubClient_LL_SetDeviceMethodCallback, IoTHubClient_LL_SetDeviceMethodCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR. ] */
AzureIoTClient 62:5a4cdacf5090 1799 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetDeviceMethodCallback function.");
AzureIoTClient 62:5a4cdacf5090 1800 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1801 }
AzureIoTClient 62:5a4cdacf5090 1802 else
AzureIoTClient 62:5a4cdacf5090 1803 {
AzureIoTClient 62:5a4cdacf5090 1804 /* Codes_SRS_IOTHUBCLIENT_LL_07_022: [ If inboundDeviceMethodCallback is NULL then IoTHubClient_LL_SetDeviceMethodCallback_Ex shall call the underlying layer's IoTHubTransport_Unsubscribe_DeviceMethod function and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 62:5a4cdacf5090 1805 handleData->IoTHubTransport_Unsubscribe_DeviceMethod(handleData->transportHandle);
AzureIoTClient 62:5a4cdacf5090 1806 handleData->methodCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 62:5a4cdacf5090 1807 handleData->methodCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1808 handleData->methodCallback.userContextCallback = NULL;
AzureIoTClient 62:5a4cdacf5090 1809 result = IOTHUB_CLIENT_OK;
AzureIoTClient 62:5a4cdacf5090 1810 }
AzureIoTClient 55:59b527ab3452 1811 }
AzureIoTClient 55:59b527ab3452 1812 else
AzureIoTClient 55:59b527ab3452 1813 {
AzureIoTClient 62:5a4cdacf5090 1814 if (handleData->methodCallback.type == CALLBACK_TYPE_SYNC)
AzureIoTClient 55:59b527ab3452 1815 {
AzureIoTClient 62:5a4cdacf5090 1816 /* Codes_SRS_IOTHUBCLIENT_LL_10_031: [If the user has subscribed using IoTHubClient_LL_SetDeviceMethodCallback, IoTHubClient_LL_SetDeviceMethodCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR. ] */
AzureIoTClient 62:5a4cdacf5090 1817 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetDeviceMethodCallback function before subscribing with IoTHubClient_LL_SetDeviceMethodCallback_Ex.");
AzureIoTClient 62:5a4cdacf5090 1818 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1819 }
AzureIoTClient 55:59b527ab3452 1820 else
AzureIoTClient 55:59b527ab3452 1821 {
AzureIoTClient 62:5a4cdacf5090 1822 /* Codes_SRS_IOTHUBCLIENT_LL_07_023: [ If inboundDeviceMethodCallback is non-NULL then IoTHubClient_LL_SetDeviceMethodCallback_Ex shall call the underlying layer's IoTHubTransport_Subscribe_DeviceMethod function.]*/
AzureIoTClient 62:5a4cdacf5090 1823 if (handleData->IoTHubTransport_Subscribe_DeviceMethod(handleData->deviceHandle) == 0)
AzureIoTClient 62:5a4cdacf5090 1824 {
AzureIoTClient 62:5a4cdacf5090 1825 handleData->methodCallback.type = CALLBACK_TYPE_ASYNC;
AzureIoTClient 62:5a4cdacf5090 1826 handleData->methodCallback.callbackAsync = inboundDeviceMethodCallback;
AzureIoTClient 62:5a4cdacf5090 1827 handleData->methodCallback.callbackSync = NULL;
AzureIoTClient 62:5a4cdacf5090 1828 handleData->methodCallback.userContextCallback = userContextCallback;
AzureIoTClient 62:5a4cdacf5090 1829 result = IOTHUB_CLIENT_OK;
AzureIoTClient 62:5a4cdacf5090 1830 }
AzureIoTClient 62:5a4cdacf5090 1831 else
AzureIoTClient 62:5a4cdacf5090 1832 {
AzureIoTClient 62:5a4cdacf5090 1833 /* Codes_SRS_IOTHUBCLIENT_LL_07_025: [ If any error is encountered then IoTHubClient_LL_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 62:5a4cdacf5090 1834 LogError("IoTHubTransport_Subscribe_DeviceMethod failed");
AzureIoTClient 62:5a4cdacf5090 1835 handleData->methodCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 62:5a4cdacf5090 1836 handleData->methodCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1837 handleData->methodCallback.callbackSync = NULL;
AzureIoTClient 62:5a4cdacf5090 1838 handleData->methodCallback.userContextCallback = NULL;
AzureIoTClient 62:5a4cdacf5090 1839 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1840 }
AzureIoTClient 55:59b527ab3452 1841 }
AzureIoTClient 55:59b527ab3452 1842 }
AzureIoTClient 55:59b527ab3452 1843 }
AzureIoTClient 55:59b527ab3452 1844 return result;
AzureIoTClient 55:59b527ab3452 1845 }
AzureIoTClient 55:59b527ab3452 1846
AzureIoTClient 55:59b527ab3452 1847 IOTHUB_CLIENT_RESULT IoTHubClient_LL_DeviceMethodResponse(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, METHOD_HANDLE methodId, const unsigned char* response, size_t response_size, int status_response)
AzureIoTClient 55:59b527ab3452 1848 {
AzureIoTClient 55:59b527ab3452 1849 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1850 /* Codes_SRS_IOTHUBCLIENT_LL_07_026: [ If handle or methodId is NULL then IoTHubClient_LL_DeviceMethodResponse shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 55:59b527ab3452 1851 if (iotHubClientHandle == NULL || methodId == NULL)
AzureIoTClient 55:59b527ab3452 1852 {
AzureIoTClient 55:59b527ab3452 1853 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1854 LOG_ERROR_RESULT;
AzureIoTClient 55:59b527ab3452 1855 }
AzureIoTClient 55:59b527ab3452 1856 else
AzureIoTClient 55:59b527ab3452 1857 {
AzureIoTClient 55:59b527ab3452 1858 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1859 /* Codes_SRS_IOTHUBCLIENT_LL_07_027: [ IoTHubClient_LL_DeviceMethodResponse shall call the IoTHubTransport_DeviceMethod_Response transport function.] */
AzureIoTClient 55:59b527ab3452 1860 if (handleData->IoTHubTransport_DeviceMethod_Response(handleData->deviceHandle, methodId, response, response_size, status_response) != 0)
AzureIoTClient 55:59b527ab3452 1861 {
AzureIoTClient 55:59b527ab3452 1862 LogError("IoTHubTransport_DeviceMethod_Response failed");
AzureIoTClient 55:59b527ab3452 1863 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1864 }
AzureIoTClient 55:59b527ab3452 1865 else
AzureIoTClient 55:59b527ab3452 1866 {
AzureIoTClient 55:59b527ab3452 1867 result = IOTHUB_CLIENT_OK;
AzureIoTClient 55:59b527ab3452 1868 }
AzureIoTClient 55:59b527ab3452 1869 }
AzureIoTClient 55:59b527ab3452 1870 return result;
AzureIoTClient 55:59b527ab3452 1871 }
AzureIoTClient 55:59b527ab3452 1872
AzureIoTClient 44:33dd78697616 1873 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 1874 IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadToBlob(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* destinationFileName, const unsigned char* source, size_t size)
AzureIoTClient 42:448eecc3676e 1875 {
AzureIoTClient 42:448eecc3676e 1876 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1877 /*Codes_SRS_IOTHUBCLIENT_LL_02_061: [ If iotHubClientHandle is NULL then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1878 /*Codes_SRS_IOTHUBCLIENT_LL_02_062: [ If destinationFileName is NULL then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1879 if (
AzureIoTClient 42:448eecc3676e 1880 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 1881 (destinationFileName == NULL) ||
AzureIoTClient 42:448eecc3676e 1882 ((source == NULL) && (size >0))
AzureIoTClient 42:448eecc3676e 1883 )
AzureIoTClient 42:448eecc3676e 1884 {
AzureIoTClient 42:448eecc3676e 1885 LogError("invalid parameters IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle=%p, const char* destinationFileName=%s, const unsigned char* source=%p, size_t size=%zu", iotHubClientHandle, destinationFileName, source, size);
AzureIoTClient 42:448eecc3676e 1886 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 1887 }
AzureIoTClient 42:448eecc3676e 1888 else
AzureIoTClient 42:448eecc3676e 1889 {
AzureIoTClient 42:448eecc3676e 1890 result = IoTHubClient_LL_UploadToBlob_Impl(iotHubClientHandle->uploadToBlobHandle, destinationFileName, source, size);
AzureIoTClient 42:448eecc3676e 1891 }
AzureIoTClient 42:448eecc3676e 1892 return result;
AzureIoTClient 16:deba40344375 1893 }
AzureIoTClient 43:038d8511e817 1894 #endif