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:
Fri Aug 11 14:02:31 2017 -0700
Revision:
73:c4d9077dd0a1
Parent:
70:875388a7d419
Child:
75:86205ca63a59
1.1.21

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 73:c4d9077dd0a1 328 // Codes_SRS_IOTHUBCLIENT_LL_09_010: [ If any failure occurs `IoTHubClient_LL_Create` shall destroy the `transportHandle` only if it has created it ]
AzureIoTClient 73:c4d9077dd0a1 329 if (!result->isSharedTransport)
AzureIoTClient 73:c4d9077dd0a1 330 {
AzureIoTClient 73:c4d9077dd0a1 331 result->IoTHubTransport_Destroy(result->transportHandle);
AzureIoTClient 73:c4d9077dd0a1 332 }
AzureIoTClient 62:5a4cdacf5090 333 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 66:a419827cb051 334 STRING_delete(product_info);
AzureIoTClient 62:5a4cdacf5090 335 free(result);
AzureIoTClient 62:5a4cdacf5090 336 result = NULL;
AzureIoTClient 62:5a4cdacf5090 337 }
AzureIoTClient 62:5a4cdacf5090 338 else
AzureIoTClient 62:5a4cdacf5090 339 {
AzureIoTClient 66:a419827cb051 340 if ((result->tickCounter = tickcounter_create()) == NULL)
AzureIoTClient 62:5a4cdacf5090 341 {
AzureIoTClient 66:a419827cb051 342 LogError("unable to get a tickcounter");
AzureIoTClient 73:c4d9077dd0a1 343 // Codes_SRS_IOTHUBCLIENT_LL_09_010: [ If any failure occurs `IoTHubClient_LL_Create` shall destroy the `transportHandle` only if it has created it ]
AzureIoTClient 73:c4d9077dd0a1 344 if (!result->isSharedTransport)
AzureIoTClient 73:c4d9077dd0a1 345 {
AzureIoTClient 73:c4d9077dd0a1 346 result->IoTHubTransport_Destroy(result->transportHandle);
AzureIoTClient 73:c4d9077dd0a1 347 }
AzureIoTClient 66:a419827cb051 348 destroy_blob_upload_module(result);
AzureIoTClient 62:5a4cdacf5090 349 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 66:a419827cb051 350 STRING_delete(product_info);
AzureIoTClient 62:5a4cdacf5090 351 free(result);
AzureIoTClient 62:5a4cdacf5090 352 result = NULL;
AzureIoTClient 62:5a4cdacf5090 353 }
AzureIoTClient 62:5a4cdacf5090 354 else
AzureIoTClient 62:5a4cdacf5090 355 {
AzureIoTClient 66:a419827cb051 356 /*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 357 DList_InitializeListHead(&(result->waitingToSend));
AzureIoTClient 66:a419827cb051 358 DList_InitializeListHead(&(result->iot_msg_queue));
AzureIoTClient 66:a419827cb051 359 DList_InitializeListHead(&(result->iot_ack_queue));
AzureIoTClient 66:a419827cb051 360 result->messageCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 66:a419827cb051 361 result->lastMessageReceiveTime = INDEFINITE_TIME;
AzureIoTClient 66:a419827cb051 362 result->data_msg_id = 1;
AzureIoTClient 66:a419827cb051 363 result->product_info = product_info;
AzureIoTClient 66:a419827cb051 364
AzureIoTClient 66:a419827cb051 365 IOTHUB_DEVICE_CONFIG deviceConfig;
AzureIoTClient 66:a419827cb051 366 deviceConfig.deviceId = config->deviceId;
AzureIoTClient 66:a419827cb051 367 deviceConfig.deviceKey = config->deviceKey;
AzureIoTClient 66:a419827cb051 368 deviceConfig.deviceSasToken = config->deviceSasToken;
AzureIoTClient 66:a419827cb051 369 deviceConfig.authorization_module = result->authorization_module;
AzureIoTClient 66:a419827cb051 370
AzureIoTClient 66:a419827cb051 371 /*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 372 if ((result->deviceHandle = result->IoTHubTransport_Register(result->transportHandle, &deviceConfig, result, &(result->waitingToSend))) == NULL)
AzureIoTClient 62:5a4cdacf5090 373 {
AzureIoTClient 66:a419827cb051 374 LogError("Registering device in transport failed");
AzureIoTClient 62:5a4cdacf5090 375 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 73:c4d9077dd0a1 376 // Codes_SRS_IOTHUBCLIENT_LL_09_010: [ If any failure occurs `IoTHubClient_LL_Create` shall destroy the `transportHandle` only if it has created it ]
AzureIoTClient 73:c4d9077dd0a1 377 if (!result->isSharedTransport)
AzureIoTClient 73:c4d9077dd0a1 378 {
AzureIoTClient 73:c4d9077dd0a1 379 result->IoTHubTransport_Destroy(result->transportHandle);
AzureIoTClient 73:c4d9077dd0a1 380 }
AzureIoTClient 62:5a4cdacf5090 381 destroy_blob_upload_module(result);
AzureIoTClient 62:5a4cdacf5090 382 tickcounter_destroy(result->tickCounter);
AzureIoTClient 66:a419827cb051 383 STRING_delete(product_info);
AzureIoTClient 62:5a4cdacf5090 384 free(result);
AzureIoTClient 62:5a4cdacf5090 385 result = NULL;
AzureIoTClient 62:5a4cdacf5090 386 }
AzureIoTClient 66:a419827cb051 387 else
AzureIoTClient 66:a419827cb051 388 {
AzureIoTClient 66:a419827cb051 389 /*Codes_SRS_IOTHUBCLIENT_LL_02_042: [ By default, messages shall not timeout. ]*/
AzureIoTClient 66:a419827cb051 390 result->currentMessageTimeout = 0;
AzureIoTClient 66:a419827cb051 391 result->current_device_twin_timeout = 0;
AzureIoTClient 66:a419827cb051 392 /*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 393 if (IoTHubClient_LL_SetRetryPolicy(result, IOTHUB_CLIENT_RETRY_EXPONENTIAL_BACKOFF_WITH_JITTER, 0) != IOTHUB_CLIENT_OK)
AzureIoTClient 66:a419827cb051 394 {
AzureIoTClient 66:a419827cb051 395 LogError("Setting default retry policy in transport failed");
AzureIoTClient 66:a419827cb051 396 result->IoTHubTransport_Unregister(result->deviceHandle);
AzureIoTClient 66:a419827cb051 397 IoTHubClient_Auth_Destroy(result->authorization_module);
AzureIoTClient 73:c4d9077dd0a1 398 // Codes_SRS_IOTHUBCLIENT_LL_09_010: [ If any failure occurs `IoTHubClient_LL_Create` shall destroy the `transportHandle` only if it has created it ]
AzureIoTClient 73:c4d9077dd0a1 399 if (!result->isSharedTransport)
AzureIoTClient 73:c4d9077dd0a1 400 {
AzureIoTClient 73:c4d9077dd0a1 401 result->IoTHubTransport_Destroy(result->transportHandle);
AzureIoTClient 73:c4d9077dd0a1 402 }
AzureIoTClient 66:a419827cb051 403 destroy_blob_upload_module(result);
AzureIoTClient 66:a419827cb051 404 tickcounter_destroy(result->tickCounter);
AzureIoTClient 66:a419827cb051 405 STRING_delete(product_info);
AzureIoTClient 66:a419827cb051 406 free(result);
AzureIoTClient 66:a419827cb051 407 result = NULL;
AzureIoTClient 66:a419827cb051 408 }
AzureIoTClient 66:a419827cb051 409 }
AzureIoTClient 62:5a4cdacf5090 410 }
AzureIoTClient 62:5a4cdacf5090 411 }
AzureIoTClient 62:5a4cdacf5090 412 }
AzureIoTClient 66:a419827cb051 413 if (IoTHubName)
AzureIoTClient 66:a419827cb051 414 {
AzureIoTClient 66:a419827cb051 415 free(IoTHubName);
AzureIoTClient 66:a419827cb051 416 }
AzureIoTClient 66:a419827cb051 417 if (transport_hostname)
AzureIoTClient 66:a419827cb051 418 {
AzureIoTClient 66:a419827cb051 419 STRING_delete(transport_hostname);
AzureIoTClient 66:a419827cb051 420 }
AzureIoTClient 62:5a4cdacf5090 421 }
AzureIoTClient 62:5a4cdacf5090 422 }
AzureIoTClient 62:5a4cdacf5090 423 return result;
AzureIoTClient 62:5a4cdacf5090 424 }
AzureIoTClient 62:5a4cdacf5090 425
AzureIoTClient 53:1e5a1ca1f274 426 static uint32_t get_next_item_id(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData)
AzureIoTClient 53:1e5a1ca1f274 427 {
AzureIoTClient 53:1e5a1ca1f274 428 if (handleData->data_msg_id+1 >= UINT32_MAX)
AzureIoTClient 53:1e5a1ca1f274 429 {
AzureIoTClient 53:1e5a1ca1f274 430 handleData->data_msg_id = 1;
AzureIoTClient 53:1e5a1ca1f274 431 }
AzureIoTClient 53:1e5a1ca1f274 432 else
AzureIoTClient 53:1e5a1ca1f274 433 {
AzureIoTClient 53:1e5a1ca1f274 434 handleData->data_msg_id++;
AzureIoTClient 53:1e5a1ca1f274 435 }
AzureIoTClient 53:1e5a1ca1f274 436 return handleData->data_msg_id;
AzureIoTClient 53:1e5a1ca1f274 437 }
AzureIoTClient 53:1e5a1ca1f274 438
AzureIoTClient 53:1e5a1ca1f274 439 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 440 {
AzureIoTClient 53:1e5a1ca1f274 441 IOTHUB_DEVICE_TWIN* result = (IOTHUB_DEVICE_TWIN*)malloc(sizeof(IOTHUB_DEVICE_TWIN) );
AzureIoTClient 53:1e5a1ca1f274 442 if (result != NULL)
AzureIoTClient 53:1e5a1ca1f274 443 {
AzureIoTClient 53:1e5a1ca1f274 444 result->report_data_handle = CONSTBUFFER_Create(reportedState, size);
AzureIoTClient 53:1e5a1ca1f274 445 if (result->report_data_handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 446 {
AzureIoTClient 53:1e5a1ca1f274 447 LogError("Failure allocating reported state data");
AzureIoTClient 53:1e5a1ca1f274 448 free(result);
AzureIoTClient 53:1e5a1ca1f274 449 result = NULL;
AzureIoTClient 53:1e5a1ca1f274 450 }
AzureIoTClient 53:1e5a1ca1f274 451 else if (tickcounter_get_current_ms(handleData->tickCounter, &result->ms_timesOutAfter) != 0)
AzureIoTClient 53:1e5a1ca1f274 452 {
AzureIoTClient 53:1e5a1ca1f274 453 LogError("Failure getting tickcount info");
AzureIoTClient 53:1e5a1ca1f274 454 CONSTBUFFER_Destroy(result->report_data_handle);
AzureIoTClient 53:1e5a1ca1f274 455 free(result);
AzureIoTClient 53:1e5a1ca1f274 456 result = NULL;
AzureIoTClient 53:1e5a1ca1f274 457 }
AzureIoTClient 53:1e5a1ca1f274 458 else
AzureIoTClient 53:1e5a1ca1f274 459 {
AzureIoTClient 53:1e5a1ca1f274 460 result->item_id = id;
AzureIoTClient 53:1e5a1ca1f274 461 result->ms_timesOutAfter = 0;
AzureIoTClient 53:1e5a1ca1f274 462 result->context = userContextCallback;
AzureIoTClient 53:1e5a1ca1f274 463 result->reported_state_callback = reportedStateCallback;
AzureIoTClient 73:c4d9077dd0a1 464 result->client_handle = handleData;
AzureIoTClient 73:c4d9077dd0a1 465 result->device_handle = handleData->deviceHandle;
AzureIoTClient 53:1e5a1ca1f274 466 }
AzureIoTClient 53:1e5a1ca1f274 467 }
AzureIoTClient 53:1e5a1ca1f274 468 else
AzureIoTClient 53:1e5a1ca1f274 469 {
AzureIoTClient 53:1e5a1ca1f274 470 LogError("Failure allocating device twin information");
AzureIoTClient 53:1e5a1ca1f274 471 }
AzureIoTClient 53:1e5a1ca1f274 472 return result;
AzureIoTClient 53:1e5a1ca1f274 473 }
AzureIoTClient 53:1e5a1ca1f274 474
AzureIoTClient 16:deba40344375 475 IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_CreateFromConnectionString(const char* connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol)
AzureIoTClient 16:deba40344375 476 {
AzureIoTClient 53:1e5a1ca1f274 477 IOTHUB_CLIENT_LL_HANDLE result;
AzureIoTClient 16:deba40344375 478
AzureIoTClient 42:448eecc3676e 479 /*Codes_SRS_IOTHUBCLIENT_LL_05_001: [IoTHubClient_LL_CreateFromConnectionString shall obtain the version string by a call to IoTHubClient_GetVersionString.]*/
AzureIoTClient 42:448eecc3676e 480 /*Codes_SRS_IOTHUBCLIENT_LL_05_002: [IoTHubClient_LL_CreateFromConnectionString shall print the version string to standard output.]*/
AzureIoTClient 42:448eecc3676e 481 LogInfo("IoT Hub SDK for C, version %s", IoTHubClient_GetVersionString());
AzureIoTClient 16:deba40344375 482
AzureIoTClient 42:448eecc3676e 483 /* 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 484 if (connectionString == NULL)
AzureIoTClient 42:448eecc3676e 485 {
AzureIoTClient 42:448eecc3676e 486 LogError("Input parameter is NULL: connectionString");
AzureIoTClient 53:1e5a1ca1f274 487 result = NULL;
AzureIoTClient 42:448eecc3676e 488 }
AzureIoTClient 42:448eecc3676e 489 else if (protocol == NULL)
AzureIoTClient 42:448eecc3676e 490 {
AzureIoTClient 42:448eecc3676e 491 LogError("Input parameter is NULL: protocol");
AzureIoTClient 53:1e5a1ca1f274 492 result = NULL;
AzureIoTClient 42:448eecc3676e 493 }
AzureIoTClient 42:448eecc3676e 494 else
AzureIoTClient 42:448eecc3676e 495 {
AzureIoTClient 42:448eecc3676e 496 /* Codes_SRS_IOTHUBCLIENT_LL_12_004: [IoTHubClient_LL_CreateFromConnectionString shall allocate IOTHUB_CLIENT_CONFIG structure] */
AzureIoTClient 53:1e5a1ca1f274 497 IOTHUB_CLIENT_CONFIG* config = (IOTHUB_CLIENT_CONFIG*) malloc(sizeof(IOTHUB_CLIENT_CONFIG));
AzureIoTClient 42:448eecc3676e 498 if (config == NULL)
AzureIoTClient 42:448eecc3676e 499 {
AzureIoTClient 42:448eecc3676e 500 /* Codes_SRS_IOTHUBCLIENT_LL_12_012: [If the allocation failed IoTHubClient_LL_CreateFromConnectionString returns NULL] */
AzureIoTClient 42:448eecc3676e 501 LogError("Malloc failed");
AzureIoTClient 53:1e5a1ca1f274 502 result = NULL;
AzureIoTClient 42:448eecc3676e 503 }
AzureIoTClient 42:448eecc3676e 504 else
AzureIoTClient 42:448eecc3676e 505 {
AzureIoTClient 42:448eecc3676e 506 STRING_TOKENIZER_HANDLE tokenizer1 = NULL;
AzureIoTClient 42:448eecc3676e 507 STRING_HANDLE connString = NULL;
AzureIoTClient 42:448eecc3676e 508 STRING_HANDLE tokenString = NULL;
AzureIoTClient 42:448eecc3676e 509 STRING_HANDLE valueString = NULL;
AzureIoTClient 42:448eecc3676e 510 STRING_HANDLE hostNameString = NULL;
AzureIoTClient 42:448eecc3676e 511 STRING_HANDLE hostSuffixString = NULL;
AzureIoTClient 42:448eecc3676e 512 STRING_HANDLE deviceIdString = NULL;
AzureIoTClient 42:448eecc3676e 513 STRING_HANDLE deviceKeyString = NULL;
AzureIoTClient 42:448eecc3676e 514 STRING_HANDLE deviceSasTokenString = NULL;
AzureIoTClient 42:448eecc3676e 515 STRING_HANDLE protocolGateway = NULL;
AzureIoTClient 16:deba40344375 516
AzureIoTClient 42:448eecc3676e 517 config->protocol = protocol;
AzureIoTClient 16:deba40344375 518
AzureIoTClient 42:448eecc3676e 519 config->iotHubName = NULL;
AzureIoTClient 42:448eecc3676e 520 config->iotHubSuffix = NULL;
AzureIoTClient 42:448eecc3676e 521 config->deviceId = NULL;
AzureIoTClient 42:448eecc3676e 522 config->deviceKey = NULL;
AzureIoTClient 42:448eecc3676e 523 config->deviceSasToken = NULL;
AzureIoTClient 40:1a94db9139ea 524
AzureIoTClient 42:448eecc3676e 525 /* Codes_SRS_IOTHUBCLIENT_LL_04_002: [If it does not, it shall pass the protocolGatewayHostName NULL.] */
AzureIoTClient 42:448eecc3676e 526 config->protocolGatewayHostName = NULL;
AzureIoTClient 16:deba40344375 527
AzureIoTClient 42:448eecc3676e 528 if ((connString = STRING_construct(connectionString)) == NULL)
AzureIoTClient 42:448eecc3676e 529 {
AzureIoTClient 42:448eecc3676e 530 LogError("Error constructing connectiong String");
AzureIoTClient 53:1e5a1ca1f274 531 result = NULL;
AzureIoTClient 42:448eecc3676e 532 }
AzureIoTClient 42:448eecc3676e 533 else if ((tokenizer1 = STRING_TOKENIZER_create(connString)) == NULL)
AzureIoTClient 42:448eecc3676e 534 {
AzureIoTClient 42:448eecc3676e 535 LogError("Error creating Tokenizer");
AzureIoTClient 53:1e5a1ca1f274 536 result = NULL;
AzureIoTClient 42:448eecc3676e 537 }
AzureIoTClient 42:448eecc3676e 538 else if ((tokenString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 539 {
AzureIoTClient 42:448eecc3676e 540 LogError("Error creating Token String");
AzureIoTClient 53:1e5a1ca1f274 541 result = NULL;
AzureIoTClient 42:448eecc3676e 542 }
AzureIoTClient 42:448eecc3676e 543 else if ((valueString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 544 {
AzureIoTClient 42:448eecc3676e 545 LogError("Error creating Value String");
AzureIoTClient 53:1e5a1ca1f274 546 result = NULL;
AzureIoTClient 42:448eecc3676e 547 }
AzureIoTClient 42:448eecc3676e 548 else if ((hostNameString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 549 {
AzureIoTClient 42:448eecc3676e 550 LogError("Error creating HostName String");
AzureIoTClient 53:1e5a1ca1f274 551 result = NULL;
AzureIoTClient 42:448eecc3676e 552 }
AzureIoTClient 42:448eecc3676e 553 else if ((hostSuffixString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 554 {
AzureIoTClient 42:448eecc3676e 555 LogError("Error creating HostSuffix String");
AzureIoTClient 53:1e5a1ca1f274 556 result = NULL;
AzureIoTClient 42:448eecc3676e 557 }
AzureIoTClient 42:448eecc3676e 558 /* 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 559 /* 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 560 else
AzureIoTClient 42:448eecc3676e 561 {
Azure.IoT Build 45:54c11b1b1407 562 int isx509found = 0;
AzureIoTClient 42:448eecc3676e 563 while ((STRING_TOKENIZER_get_next_token(tokenizer1, tokenString, "=") == 0))
AzureIoTClient 42:448eecc3676e 564 {
AzureIoTClient 42:448eecc3676e 565 if (STRING_TOKENIZER_get_next_token(tokenizer1, valueString, ";") != 0)
AzureIoTClient 42:448eecc3676e 566 {
AzureIoTClient 42:448eecc3676e 567 LogError("Tokenizer error");
AzureIoTClient 42:448eecc3676e 568 break;
AzureIoTClient 42:448eecc3676e 569 }
AzureIoTClient 42:448eecc3676e 570 else
AzureIoTClient 42:448eecc3676e 571 {
AzureIoTClient 42:448eecc3676e 572 if (tokenString != NULL)
AzureIoTClient 42:448eecc3676e 573 {
AzureIoTClient 42:448eecc3676e 574 /* 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 575 const char* s_token = STRING_c_str(tokenString);
AzureIoTClient 42:448eecc3676e 576 if (strcmp(s_token, HOSTNAME_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 577 {
AzureIoTClient 42:448eecc3676e 578 /* 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 579 STRING_TOKENIZER_HANDLE tokenizer2 = NULL;
AzureIoTClient 42:448eecc3676e 580 if ((tokenizer2 = STRING_TOKENIZER_create(valueString)) == NULL)
AzureIoTClient 42:448eecc3676e 581 {
AzureIoTClient 42:448eecc3676e 582 LogError("Error creating Tokenizer");
AzureIoTClient 42:448eecc3676e 583 break;
AzureIoTClient 42:448eecc3676e 584 }
AzureIoTClient 42:448eecc3676e 585 else
AzureIoTClient 42:448eecc3676e 586 {
AzureIoTClient 42:448eecc3676e 587 /* Codes_SRS_IOTHUBCLIENT_LL_12_015: [If the string split failed, IoTHubClient_LL_CreateFromConnectionString returns NULL ] */
AzureIoTClient 42:448eecc3676e 588 if (STRING_TOKENIZER_get_next_token(tokenizer2, hostNameString, ".") != 0)
AzureIoTClient 42:448eecc3676e 589 {
AzureIoTClient 42:448eecc3676e 590 LogError("Tokenizer error");
AzureIoTClient 42:448eecc3676e 591 STRING_TOKENIZER_destroy(tokenizer2);
AzureIoTClient 42:448eecc3676e 592 break;
AzureIoTClient 42:448eecc3676e 593 }
AzureIoTClient 42:448eecc3676e 594 else
AzureIoTClient 42:448eecc3676e 595 {
AzureIoTClient 42:448eecc3676e 596 config->iotHubName = STRING_c_str(hostNameString);
AzureIoTClient 42:448eecc3676e 597 if (STRING_TOKENIZER_get_next_token(tokenizer2, hostSuffixString, ";") != 0)
AzureIoTClient 42:448eecc3676e 598 {
AzureIoTClient 42:448eecc3676e 599 LogError("Tokenizer error");
AzureIoTClient 42:448eecc3676e 600 STRING_TOKENIZER_destroy(tokenizer2);
AzureIoTClient 42:448eecc3676e 601 break;
AzureIoTClient 42:448eecc3676e 602 }
AzureIoTClient 42:448eecc3676e 603 else
AzureIoTClient 42:448eecc3676e 604 {
AzureIoTClient 42:448eecc3676e 605 config->iotHubSuffix = STRING_c_str(hostSuffixString);
AzureIoTClient 42:448eecc3676e 606 }
AzureIoTClient 42:448eecc3676e 607 }
AzureIoTClient 42:448eecc3676e 608 STRING_TOKENIZER_destroy(tokenizer2);
AzureIoTClient 42:448eecc3676e 609 }
AzureIoTClient 42:448eecc3676e 610 }
AzureIoTClient 42:448eecc3676e 611 else if (strcmp(s_token, DEVICEID_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 612 {
AzureIoTClient 42:448eecc3676e 613 deviceIdString = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 614 if (deviceIdString != NULL)
AzureIoTClient 42:448eecc3676e 615 {
AzureIoTClient 42:448eecc3676e 616 config->deviceId = STRING_c_str(deviceIdString);
AzureIoTClient 42:448eecc3676e 617 }
AzureIoTClient 53:1e5a1ca1f274 618 else
AzureIoTClient 53:1e5a1ca1f274 619 {
AzureIoTClient 53:1e5a1ca1f274 620 LogError("Failure cloning device id string");
AzureIoTClient 53:1e5a1ca1f274 621 break;
AzureIoTClient 53:1e5a1ca1f274 622 }
AzureIoTClient 42:448eecc3676e 623 }
AzureIoTClient 42:448eecc3676e 624 else if (strcmp(s_token, DEVICEKEY_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 625 {
AzureIoTClient 42:448eecc3676e 626 deviceKeyString = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 627 if (deviceKeyString != NULL)
AzureIoTClient 42:448eecc3676e 628 {
AzureIoTClient 42:448eecc3676e 629 config->deviceKey = STRING_c_str(deviceKeyString);
AzureIoTClient 42:448eecc3676e 630 }
AzureIoTClient 53:1e5a1ca1f274 631 else
AzureIoTClient 53:1e5a1ca1f274 632 {
AzureIoTClient 53:1e5a1ca1f274 633 LogError("Failure cloning device key string");
AzureIoTClient 53:1e5a1ca1f274 634 break;
AzureIoTClient 53:1e5a1ca1f274 635 }
AzureIoTClient 42:448eecc3676e 636 }
AzureIoTClient 42:448eecc3676e 637 else if (strcmp(s_token, DEVICESAS_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 638 {
AzureIoTClient 42:448eecc3676e 639 deviceSasTokenString = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 640 if (deviceSasTokenString != NULL)
AzureIoTClient 42:448eecc3676e 641 {
AzureIoTClient 42:448eecc3676e 642 config->deviceSasToken = STRING_c_str(deviceSasTokenString);
AzureIoTClient 42:448eecc3676e 643 }
AzureIoTClient 53:1e5a1ca1f274 644 else
AzureIoTClient 53:1e5a1ca1f274 645 {
AzureIoTClient 53:1e5a1ca1f274 646 LogError("Failure cloning device sasToken string");
AzureIoTClient 53:1e5a1ca1f274 647 break;
AzureIoTClient 53:1e5a1ca1f274 648 }
AzureIoTClient 42:448eecc3676e 649 }
Azure.IoT Build 45:54c11b1b1407 650 else if (strcmp(s_token, X509_TOKEN) == 0)
Azure.IoT Build 45:54c11b1b1407 651 {
Azure.IoT Build 45:54c11b1b1407 652 if (strcmp(STRING_c_str(valueString), X509_TOKEN_ONLY_ACCEPTABLE_VALUE) != 0)
Azure.IoT Build 45:54c11b1b1407 653 {
Azure.IoT Build 45:54c11b1b1407 654 LogError("x509 option has wrong value, the only acceptable one is \"true\"");
AzureIoTClient 53:1e5a1ca1f274 655 break;
Azure.IoT Build 45:54c11b1b1407 656 }
Azure.IoT Build 45:54c11b1b1407 657 else
Azure.IoT Build 45:54c11b1b1407 658 {
Azure.IoT Build 45:54c11b1b1407 659 isx509found = 1;
Azure.IoT Build 45:54c11b1b1407 660 }
Azure.IoT Build 45:54c11b1b1407 661 }
AzureIoTClient 42:448eecc3676e 662 /* 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 663 else if (strcmp(s_token, PROTOCOL_GATEWAY_HOST) == 0)
AzureIoTClient 42:448eecc3676e 664 {
AzureIoTClient 42:448eecc3676e 665 protocolGateway = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 666 if (protocolGateway != NULL)
AzureIoTClient 42:448eecc3676e 667 {
AzureIoTClient 42:448eecc3676e 668 config->protocolGatewayHostName = STRING_c_str(protocolGateway);
AzureIoTClient 42:448eecc3676e 669 }
AzureIoTClient 53:1e5a1ca1f274 670 else
AzureIoTClient 53:1e5a1ca1f274 671 {
AzureIoTClient 53:1e5a1ca1f274 672 LogError("Failure cloning protocol Gateway Name");
AzureIoTClient 53:1e5a1ca1f274 673 break;
AzureIoTClient 53:1e5a1ca1f274 674 }
AzureIoTClient 42:448eecc3676e 675 }
AzureIoTClient 42:448eecc3676e 676 }
AzureIoTClient 42:448eecc3676e 677 }
AzureIoTClient 42:448eecc3676e 678 }
AzureIoTClient 42:448eecc3676e 679 /* parsing is done - check the result */
AzureIoTClient 42:448eecc3676e 680 if (config->iotHubName == NULL)
AzureIoTClient 42:448eecc3676e 681 {
AzureIoTClient 42:448eecc3676e 682 LogError("iotHubName is not found");
AzureIoTClient 53:1e5a1ca1f274 683 result = NULL;
AzureIoTClient 42:448eecc3676e 684 }
AzureIoTClient 42:448eecc3676e 685 else if (config->iotHubSuffix == NULL)
AzureIoTClient 42:448eecc3676e 686 {
AzureIoTClient 42:448eecc3676e 687 LogError("iotHubSuffix is not found");
AzureIoTClient 53:1e5a1ca1f274 688 result = NULL;
AzureIoTClient 42:448eecc3676e 689 }
AzureIoTClient 42:448eecc3676e 690 else if (config->deviceId == NULL)
AzureIoTClient 42:448eecc3676e 691 {
AzureIoTClient 42:448eecc3676e 692 LogError("deviceId is not found");
AzureIoTClient 53:1e5a1ca1f274 693 result = NULL;
AzureIoTClient 42:448eecc3676e 694 }
Azure.IoT Build 45:54c11b1b1407 695 else if (!(
Azure.IoT Build 45:54c11b1b1407 696 ((!isx509found) && (config->deviceSasToken == NULL) ^ (config->deviceKey == NULL)) ||
Azure.IoT Build 45:54c11b1b1407 697 ((isx509found) && (config->deviceSasToken == NULL) && (config->deviceKey == NULL))
Azure.IoT Build 45:54c11b1b1407 698 ))
AzureIoTClient 42:448eecc3676e 699 {
Azure.IoT Build 45:54c11b1b1407 700 LogError("invalid combination of x509, deviceSasToken and deviceKey");
AzureIoTClient 53:1e5a1ca1f274 701 result = NULL;
AzureIoTClient 42:448eecc3676e 702 }
AzureIoTClient 42:448eecc3676e 703 else
AzureIoTClient 42:448eecc3676e 704 {
AzureIoTClient 42:448eecc3676e 705 /* 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 706 result = initialize_iothub_client(config, NULL);
AzureIoTClient 42:448eecc3676e 707 if (result == NULL)
AzureIoTClient 42:448eecc3676e 708 {
AzureIoTClient 42:448eecc3676e 709 LogError("IoTHubClient_LL_Create failed");
AzureIoTClient 42:448eecc3676e 710 }
AzureIoTClient 42:448eecc3676e 711 else
AzureIoTClient 42:448eecc3676e 712 {
AzureIoTClient 42:448eecc3676e 713 /*return as is*/
AzureIoTClient 42:448eecc3676e 714 }
AzureIoTClient 42:448eecc3676e 715 }
AzureIoTClient 42:448eecc3676e 716 }
AzureIoTClient 42:448eecc3676e 717 if (deviceSasTokenString != NULL)
AzureIoTClient 42:448eecc3676e 718 STRING_delete(deviceSasTokenString);
AzureIoTClient 42:448eecc3676e 719 if (deviceKeyString != NULL)
AzureIoTClient 42:448eecc3676e 720 STRING_delete(deviceKeyString);
AzureIoTClient 42:448eecc3676e 721 if (deviceIdString != NULL)
AzureIoTClient 42:448eecc3676e 722 STRING_delete(deviceIdString);
AzureIoTClient 42:448eecc3676e 723 if (hostSuffixString != NULL)
AzureIoTClient 42:448eecc3676e 724 STRING_delete(hostSuffixString);
AzureIoTClient 42:448eecc3676e 725 if (hostNameString != NULL)
AzureIoTClient 42:448eecc3676e 726 STRING_delete(hostNameString);
AzureIoTClient 42:448eecc3676e 727 if (valueString != NULL)
AzureIoTClient 42:448eecc3676e 728 STRING_delete(valueString);
AzureIoTClient 42:448eecc3676e 729 if (tokenString != NULL)
AzureIoTClient 42:448eecc3676e 730 STRING_delete(tokenString);
AzureIoTClient 42:448eecc3676e 731 if (connString != NULL)
AzureIoTClient 42:448eecc3676e 732 STRING_delete(connString);
AzureIoTClient 42:448eecc3676e 733 if (protocolGateway != NULL)
AzureIoTClient 42:448eecc3676e 734 STRING_delete(protocolGateway);
AzureIoTClient 16:deba40344375 735
AzureIoTClient 42:448eecc3676e 736 if (tokenizer1 != NULL)
AzureIoTClient 42:448eecc3676e 737 STRING_TOKENIZER_destroy(tokenizer1);
AzureIoTClient 16:deba40344375 738
AzureIoTClient 42:448eecc3676e 739 free(config);
AzureIoTClient 42:448eecc3676e 740 }
AzureIoTClient 42:448eecc3676e 741 }
AzureIoTClient 42:448eecc3676e 742 return result;
AzureIoTClient 16:deba40344375 743 }
AzureIoTClient 16:deba40344375 744
AzureIoTClient 16:deba40344375 745 IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_Create(const IOTHUB_CLIENT_CONFIG* config)
AzureIoTClient 16:deba40344375 746 {
AzureIoTClient 42:448eecc3676e 747 IOTHUB_CLIENT_LL_HANDLE result;
AzureIoTClient 42:448eecc3676e 748 /*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 749 if(
AzureIoTClient 42:448eecc3676e 750 (config == NULL) ||
AzureIoTClient 42:448eecc3676e 751 (config->protocol == NULL)
AzureIoTClient 42:448eecc3676e 752 )
AzureIoTClient 42:448eecc3676e 753 {
AzureIoTClient 42:448eecc3676e 754 result = NULL;
AzureIoTClient 42:448eecc3676e 755 LogError("invalid configuration (NULL detected)");
AzureIoTClient 42:448eecc3676e 756 }
AzureIoTClient 42:448eecc3676e 757 else
AzureIoTClient 42:448eecc3676e 758 {
AzureIoTClient 62:5a4cdacf5090 759 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = initialize_iothub_client(config, NULL);
AzureIoTClient 42:448eecc3676e 760 if (handleData == NULL)
AzureIoTClient 42:448eecc3676e 761 {
AzureIoTClient 62:5a4cdacf5090 762 LogError("initialize iothub client");
AzureIoTClient 42:448eecc3676e 763 result = NULL;
AzureIoTClient 42:448eecc3676e 764 }
AzureIoTClient 42:448eecc3676e 765 else
AzureIoTClient 42:448eecc3676e 766 {
AzureIoTClient 62:5a4cdacf5090 767 result = handleData;
AzureIoTClient 42:448eecc3676e 768 }
AzureIoTClient 42:448eecc3676e 769 }
AzureIoTClient 16:deba40344375 770
AzureIoTClient 42:448eecc3676e 771 return result;
AzureIoTClient 16:deba40344375 772 }
AzureIoTClient 16:deba40344375 773
Azure.IoT Build 36:67300d5a4c1f 774 IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_CreateWithTransport(const IOTHUB_CLIENT_DEVICE_CONFIG * config)
Azure.IoT Build 36:67300d5a4c1f 775 {
AzureIoTClient 42:448eecc3676e 776 IOTHUB_CLIENT_LL_HANDLE result;
AzureIoTClient 42:448eecc3676e 777 /*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 778 if (
AzureIoTClient 42:448eecc3676e 779 (config == NULL) ||
AzureIoTClient 42:448eecc3676e 780 (config->protocol == NULL) ||
Azure.IoT Build 45:54c11b1b1407 781 (config->transportHandle == NULL) ||
Azure.IoT Build 45:54c11b1b1407 782 /*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 783 ((config->deviceKey == NULL) && (config->deviceSasToken == NULL))
AzureIoTClient 42:448eecc3676e 784 )
AzureIoTClient 42:448eecc3676e 785 {
AzureIoTClient 42:448eecc3676e 786 result = NULL;
AzureIoTClient 42:448eecc3676e 787 LogError("invalid configuration (NULL detected)");
AzureIoTClient 42:448eecc3676e 788 }
AzureIoTClient 42:448eecc3676e 789 else
AzureIoTClient 42:448eecc3676e 790 {
AzureIoTClient 62:5a4cdacf5090 791 result = initialize_iothub_client(NULL, config);
AzureIoTClient 42:448eecc3676e 792 }
AzureIoTClient 42:448eecc3676e 793 return result;
Azure.IoT Build 36:67300d5a4c1f 794 }
Azure.IoT Build 36:67300d5a4c1f 795
AzureIoTClient 16:deba40344375 796 void IoTHubClient_LL_Destroy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 16:deba40344375 797 {
AzureIoTClient 42:448eecc3676e 798 /*Codes_SRS_IOTHUBCLIENT_LL_02_009: [IoTHubClient_LL_Destroy shall do nothing if parameter iotHubClientHandle is NULL.]*/
AzureIoTClient 42:448eecc3676e 799 if (iotHubClientHandle != NULL)
AzureIoTClient 42:448eecc3676e 800 {
AzureIoTClient 42:448eecc3676e 801 PDLIST_ENTRY unsend;
AzureIoTClient 42:448eecc3676e 802 /*Codes_SRS_IOTHUBCLIENT_LL_17_010: [IoTHubClient_LL_Destroy shall call the underlaying layer's _Unregister function] */
AzureIoTClient 42:448eecc3676e 803 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 804 handleData->IoTHubTransport_Unregister(handleData->deviceHandle);
AzureIoTClient 42:448eecc3676e 805 if (handleData->isSharedTransport == false)
AzureIoTClient 42:448eecc3676e 806 {
AzureIoTClient 42:448eecc3676e 807 /*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 808 handleData->IoTHubTransport_Destroy(handleData->transportHandle);
AzureIoTClient 42:448eecc3676e 809 }
AzureIoTClient 42:448eecc3676e 810 /*if any, remove the items currently not send*/
AzureIoTClient 42:448eecc3676e 811 while ((unsend = DList_RemoveHeadList(&(handleData->waitingToSend))) != &(handleData->waitingToSend))
AzureIoTClient 42:448eecc3676e 812 {
AzureIoTClient 42:448eecc3676e 813 IOTHUB_MESSAGE_LIST* temp = containingRecord(unsend, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 42:448eecc3676e 814 /*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 815 if (temp->callback != NULL)
AzureIoTClient 42:448eecc3676e 816 {
AzureIoTClient 42:448eecc3676e 817 temp->callback(IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY, temp->context);
AzureIoTClient 42:448eecc3676e 818 }
AzureIoTClient 42:448eecc3676e 819 IoTHubMessage_Destroy(temp->messageHandle);
AzureIoTClient 42:448eecc3676e 820 free(temp);
AzureIoTClient 42:448eecc3676e 821 }
AzureIoTClient 53:1e5a1ca1f274 822
AzureIoTClient 53:1e5a1ca1f274 823 /* Codes_SRS_IOTHUBCLIENT_LL_07_007: [ IoTHubClient_LL_Destroy shall iterate the device twin queues and destroy any remaining items. ] */
AzureIoTClient 53:1e5a1ca1f274 824 while ((unsend = DList_RemoveHeadList(&(handleData->iot_msg_queue))) != &(handleData->iot_msg_queue))
AzureIoTClient 53:1e5a1ca1f274 825 {
AzureIoTClient 53:1e5a1ca1f274 826 IOTHUB_DEVICE_TWIN* temp = containingRecord(unsend, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 827 device_twin_data_destroy(temp);
AzureIoTClient 53:1e5a1ca1f274 828 }
AzureIoTClient 53:1e5a1ca1f274 829 while ((unsend = DList_RemoveHeadList(&(handleData->iot_ack_queue))) != &(handleData->iot_ack_queue))
AzureIoTClient 53:1e5a1ca1f274 830 {
AzureIoTClient 53:1e5a1ca1f274 831 IOTHUB_DEVICE_TWIN* temp = containingRecord(unsend, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 832 device_twin_data_destroy(temp);
AzureIoTClient 53:1e5a1ca1f274 833 }
AzureIoTClient 53:1e5a1ca1f274 834
AzureIoTClient 42:448eecc3676e 835 /*Codes_SRS_IOTHUBCLIENT_LL_17_011: [IoTHubClient_LL_Destroy shall free the resources allocated by IoTHubClient (if any).] */
AzureIoTClient 62:5a4cdacf5090 836 IoTHubClient_Auth_Destroy(handleData->authorization_module);
AzureIoTClient 42:448eecc3676e 837 tickcounter_destroy(handleData->tickCounter);
AzureIoTClient 44:33dd78697616 838 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 839 IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle);
AzureIoTClient 43:038d8511e817 840 #endif
AzureIoTClient 66:a419827cb051 841 STRING_delete(handleData->product_info);
AzureIoTClient 42:448eecc3676e 842 free(handleData);
AzureIoTClient 42:448eecc3676e 843 }
AzureIoTClient 16:deba40344375 844 }
AzureIoTClient 16:deba40344375 845
Azure.IoT Build 36:67300d5a4c1f 846 /*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 847 /*returns 0 on success, any other value is error*/
Azure.IoT Build 36:67300d5a4c1f 848 static int attach_ms_timesOutAfter(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData, IOTHUB_MESSAGE_LIST *newEntry)
Azure.IoT Build 36:67300d5a4c1f 849 {
AzureIoTClient 42:448eecc3676e 850 int result;
AzureIoTClient 42:448eecc3676e 851 /*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 852 if (handleData->currentMessageTimeout == 0)
AzureIoTClient 42:448eecc3676e 853 {
AzureIoTClient 42:448eecc3676e 854 newEntry->ms_timesOutAfter = 0; /*do not timeout*/
AzureIoTClient 42:448eecc3676e 855 result = 0;
AzureIoTClient 42:448eecc3676e 856 }
AzureIoTClient 42:448eecc3676e 857 else
AzureIoTClient 42:448eecc3676e 858 {
AzureIoTClient 42:448eecc3676e 859 /*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 860 if (tickcounter_get_current_ms(handleData->tickCounter, &newEntry->ms_timesOutAfter) != 0)
AzureIoTClient 42:448eecc3676e 861 {
AzureIoTClient 60:41648c4e7036 862 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 863 LogError("unable to get the current relative tickcount");
AzureIoTClient 42:448eecc3676e 864 }
AzureIoTClient 42:448eecc3676e 865 else
AzureIoTClient 42:448eecc3676e 866 {
AzureIoTClient 42:448eecc3676e 867 newEntry->ms_timesOutAfter += handleData->currentMessageTimeout;
AzureIoTClient 42:448eecc3676e 868 result = 0;
AzureIoTClient 42:448eecc3676e 869 }
AzureIoTClient 42:448eecc3676e 870 }
AzureIoTClient 42:448eecc3676e 871 return result;
Azure.IoT Build 36:67300d5a4c1f 872 }
Azure.IoT Build 36:67300d5a4c1f 873
AzureIoTClient 16:deba40344375 874 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 875 {
AzureIoTClient 42:448eecc3676e 876 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 877 /*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 878 if (
AzureIoTClient 42:448eecc3676e 879 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 880 (eventMessageHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 881 /*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 882 ((eventConfirmationCallback == NULL) && (userContextCallback != NULL))
AzureIoTClient 42:448eecc3676e 883 )
AzureIoTClient 42:448eecc3676e 884 {
AzureIoTClient 42:448eecc3676e 885 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 45:54c11b1b1407 886 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 887 }
AzureIoTClient 42:448eecc3676e 888 else
AzureIoTClient 42:448eecc3676e 889 {
AzureIoTClient 42:448eecc3676e 890 IOTHUB_MESSAGE_LIST *newEntry = (IOTHUB_MESSAGE_LIST*)malloc(sizeof(IOTHUB_MESSAGE_LIST));
AzureIoTClient 42:448eecc3676e 891 if (newEntry == NULL)
AzureIoTClient 42:448eecc3676e 892 {
AzureIoTClient 42:448eecc3676e 893 result = IOTHUB_CLIENT_ERROR;
Azure.IoT Build 45:54c11b1b1407 894 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 895 }
AzureIoTClient 42:448eecc3676e 896 else
AzureIoTClient 42:448eecc3676e 897 {
AzureIoTClient 42:448eecc3676e 898 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 40:1a94db9139ea 899
AzureIoTClient 42:448eecc3676e 900 if (attach_ms_timesOutAfter(handleData, newEntry) != 0)
AzureIoTClient 42:448eecc3676e 901 {
AzureIoTClient 42:448eecc3676e 902 result = IOTHUB_CLIENT_ERROR;
Azure.IoT Build 45:54c11b1b1407 903 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 904 free(newEntry);
AzureIoTClient 42:448eecc3676e 905 }
AzureIoTClient 42:448eecc3676e 906 else
AzureIoTClient 42:448eecc3676e 907 {
AzureIoTClient 62:5a4cdacf5090 908 /*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 909 if ((newEntry->messageHandle = IoTHubMessage_Clone(eventMessageHandle)) == NULL)
AzureIoTClient 42:448eecc3676e 910 {
AzureIoTClient 42:448eecc3676e 911 /*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 912 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 913 free(newEntry);
Azure.IoT Build 45:54c11b1b1407 914 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 915 }
AzureIoTClient 42:448eecc3676e 916 else
AzureIoTClient 42:448eecc3676e 917 {
AzureIoTClient 62:5a4cdacf5090 918 /*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 919 newEntry->callback = eventConfirmationCallback;
AzureIoTClient 42:448eecc3676e 920 newEntry->context = userContextCallback;
AzureIoTClient 46:6a69294b6119 921 DList_InsertTailList(&(iotHubClientHandle->waitingToSend), &(newEntry->entry));
AzureIoTClient 42:448eecc3676e 922 /*Codes_SRS_IOTHUBCLIENT_LL_02_015: [Otherwise IoTHubClient_LL_SendEventAsync shall succeed and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 42:448eecc3676e 923 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 924 }
AzureIoTClient 42:448eecc3676e 925 }
AzureIoTClient 42:448eecc3676e 926 }
AzureIoTClient 42:448eecc3676e 927 }
AzureIoTClient 42:448eecc3676e 928 return result;
AzureIoTClient 16:deba40344375 929 }
AzureIoTClient 16:deba40344375 930
AzureIoTClient 16:deba40344375 931 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetMessageCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 932 {
AzureIoTClient 42:448eecc3676e 933 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 934 if (iotHubClientHandle == NULL)
AzureIoTClient 42:448eecc3676e 935 {
AzureIoTClient 61:8b85a4e797cf 936 /*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 937 LogError("Invalid argument - iotHubClientHandle is NULL");
AzureIoTClient 42:448eecc3676e 938 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 939 }
AzureIoTClient 42:448eecc3676e 940 else
AzureIoTClient 42:448eecc3676e 941 {
AzureIoTClient 42:448eecc3676e 942 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 943 if (messageCallback == NULL)
AzureIoTClient 42:448eecc3676e 944 {
AzureIoTClient 62:5a4cdacf5090 945 if (handleData->messageCallback.type == CALLBACK_TYPE_NONE)
AzureIoTClient 61:8b85a4e797cf 946 {
AzureIoTClient 61:8b85a4e797cf 947 /*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 948 LogError("not currently set to accept or process incoming messages.");
AzureIoTClient 61:8b85a4e797cf 949 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 950 }
AzureIoTClient 62:5a4cdacf5090 951 else if (handleData->messageCallback.type == CALLBACK_TYPE_ASYNC)
AzureIoTClient 61:8b85a4e797cf 952 {
AzureIoTClient 61:8b85a4e797cf 953 /*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 954 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetMessageCallback_Ex function.");
AzureIoTClient 61:8b85a4e797cf 955 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 956 }
AzureIoTClient 61:8b85a4e797cf 957 else
AzureIoTClient 61:8b85a4e797cf 958 {
AzureIoTClient 61:8b85a4e797cf 959 /*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 960 handleData->IoTHubTransport_Unsubscribe(handleData->deviceHandle);
AzureIoTClient 62:5a4cdacf5090 961 handleData->messageCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 61:8b85a4e797cf 962 handleData->messageCallback.callbackSync = NULL;
AzureIoTClient 61:8b85a4e797cf 963 handleData->messageCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 964 handleData->messageCallback.userContextCallback = NULL;
AzureIoTClient 61:8b85a4e797cf 965 result = IOTHUB_CLIENT_OK;
AzureIoTClient 61:8b85a4e797cf 966 }
AzureIoTClient 42:448eecc3676e 967 }
AzureIoTClient 42:448eecc3676e 968 else
AzureIoTClient 42:448eecc3676e 969 {
AzureIoTClient 62:5a4cdacf5090 970 if (handleData->messageCallback.type == CALLBACK_TYPE_ASYNC)
AzureIoTClient 42:448eecc3676e 971 {
AzureIoTClient 62:5a4cdacf5090 972 /* 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 973 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetMessageCallback_Ex function before subscribing with MessageCallback.");
AzureIoTClient 61:8b85a4e797cf 974 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 975 }
AzureIoTClient 42:448eecc3676e 976 else
AzureIoTClient 42:448eecc3676e 977 {
AzureIoTClient 61:8b85a4e797cf 978 if (handleData->IoTHubTransport_Subscribe(handleData->deviceHandle) == 0)
AzureIoTClient 61:8b85a4e797cf 979 {
AzureIoTClient 61:8b85a4e797cf 980 /*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 981 handleData->messageCallback.type = CALLBACK_TYPE_SYNC;
AzureIoTClient 61:8b85a4e797cf 982 handleData->messageCallback.callbackSync = messageCallback;
AzureIoTClient 62:5a4cdacf5090 983 handleData->messageCallback.userContextCallback = userContextCallback;
AzureIoTClient 61:8b85a4e797cf 984 result = IOTHUB_CLIENT_OK;
AzureIoTClient 61:8b85a4e797cf 985 }
AzureIoTClient 61:8b85a4e797cf 986 else
AzureIoTClient 61:8b85a4e797cf 987 {
AzureIoTClient 61:8b85a4e797cf 988 /*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 989 LogError("IoTHubTransport_Subscribe failed");
AzureIoTClient 62:5a4cdacf5090 990 handleData->messageCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 61:8b85a4e797cf 991 handleData->messageCallback.callbackSync = NULL;
AzureIoTClient 61:8b85a4e797cf 992 handleData->messageCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 993 handleData->messageCallback.userContextCallback = NULL;
AzureIoTClient 61:8b85a4e797cf 994 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 995 }
AzureIoTClient 42:448eecc3676e 996 }
AzureIoTClient 42:448eecc3676e 997 }
AzureIoTClient 42:448eecc3676e 998 }
AzureIoTClient 61:8b85a4e797cf 999 return result;
AzureIoTClient 61:8b85a4e797cf 1000 }
AzureIoTClient 40:1a94db9139ea 1001
AzureIoTClient 62:5a4cdacf5090 1002 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetMessageCallback_Ex(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC_EX messageCallback, void* userContextCallback)
AzureIoTClient 61:8b85a4e797cf 1003 {
AzureIoTClient 61:8b85a4e797cf 1004 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 61:8b85a4e797cf 1005 if (iotHubClientHandle == NULL)
AzureIoTClient 61:8b85a4e797cf 1006 {
AzureIoTClient 62:5a4cdacf5090 1007 /*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 1008 LogError("Invalid argument - iotHubClientHandle is NULL");
AzureIoTClient 61:8b85a4e797cf 1009 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 61:8b85a4e797cf 1010 }
AzureIoTClient 61:8b85a4e797cf 1011 else
AzureIoTClient 61:8b85a4e797cf 1012 {
AzureIoTClient 61:8b85a4e797cf 1013 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 61:8b85a4e797cf 1014 if (messageCallback == NULL)
AzureIoTClient 61:8b85a4e797cf 1015 {
AzureIoTClient 62:5a4cdacf5090 1016 if (handleData->messageCallback.type == CALLBACK_TYPE_NONE)
AzureIoTClient 61:8b85a4e797cf 1017 {
AzureIoTClient 62:5a4cdacf5090 1018 /*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 1019 LogError("not currently set to accept or process incoming messages.");
AzureIoTClient 61:8b85a4e797cf 1020 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 1021 }
AzureIoTClient 62:5a4cdacf5090 1022 else if (handleData->messageCallback.type == CALLBACK_TYPE_SYNC)
AzureIoTClient 61:8b85a4e797cf 1023 {
AzureIoTClient 62:5a4cdacf5090 1024 /*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 1025 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetMessageCallback function.");
AzureIoTClient 61:8b85a4e797cf 1026 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 1027 }
AzureIoTClient 61:8b85a4e797cf 1028 else
AzureIoTClient 61:8b85a4e797cf 1029 {
AzureIoTClient 62:5a4cdacf5090 1030 /*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 1031 handleData->IoTHubTransport_Unsubscribe(handleData->deviceHandle);
AzureIoTClient 62:5a4cdacf5090 1032 handleData->messageCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 61:8b85a4e797cf 1033 handleData->messageCallback.callbackSync = NULL;
AzureIoTClient 61:8b85a4e797cf 1034 handleData->messageCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1035 handleData->messageCallback.userContextCallback = NULL;
AzureIoTClient 61:8b85a4e797cf 1036 result = IOTHUB_CLIENT_OK;
AzureIoTClient 61:8b85a4e797cf 1037 }
AzureIoTClient 61:8b85a4e797cf 1038 }
AzureIoTClient 61:8b85a4e797cf 1039 else
AzureIoTClient 61:8b85a4e797cf 1040 {
AzureIoTClient 62:5a4cdacf5090 1041 if (handleData->messageCallback.type == CALLBACK_TYPE_SYNC)
AzureIoTClient 61:8b85a4e797cf 1042 {
AzureIoTClient 62:5a4cdacf5090 1043 /*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 1044 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_MessageCallbackEx function before subscribing with MessageCallback.");
AzureIoTClient 61:8b85a4e797cf 1045 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 1046 }
AzureIoTClient 61:8b85a4e797cf 1047 else
AzureIoTClient 61:8b85a4e797cf 1048 {
AzureIoTClient 61:8b85a4e797cf 1049 if (handleData->IoTHubTransport_Subscribe(handleData->deviceHandle) == 0)
AzureIoTClient 61:8b85a4e797cf 1050 {
AzureIoTClient 62:5a4cdacf5090 1051 /*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 1052 handleData->messageCallback.type = CALLBACK_TYPE_ASYNC;
AzureIoTClient 61:8b85a4e797cf 1053 handleData->messageCallback.callbackAsync = messageCallback;
AzureIoTClient 62:5a4cdacf5090 1054 handleData->messageCallback.userContextCallback = userContextCallback;
AzureIoTClient 61:8b85a4e797cf 1055 result = IOTHUB_CLIENT_OK;
AzureIoTClient 61:8b85a4e797cf 1056 }
AzureIoTClient 61:8b85a4e797cf 1057 else
AzureIoTClient 61:8b85a4e797cf 1058 {
AzureIoTClient 62:5a4cdacf5090 1059 /*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 1060 LogError("IoTHubTransport_Subscribe failed");
AzureIoTClient 62:5a4cdacf5090 1061 handleData->messageCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 61:8b85a4e797cf 1062 handleData->messageCallback.callbackSync = NULL;
AzureIoTClient 61:8b85a4e797cf 1063 handleData->messageCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1064 handleData->messageCallback.userContextCallback = NULL;
AzureIoTClient 61:8b85a4e797cf 1065 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 1066 }
AzureIoTClient 61:8b85a4e797cf 1067 }
AzureIoTClient 61:8b85a4e797cf 1068 }
AzureIoTClient 61:8b85a4e797cf 1069 }
AzureIoTClient 61:8b85a4e797cf 1070 return result;
AzureIoTClient 61:8b85a4e797cf 1071 }
AzureIoTClient 61:8b85a4e797cf 1072
AzureIoTClient 61:8b85a4e797cf 1073 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SendMessageDisposition(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, MESSAGE_CALLBACK_INFO* message_data, IOTHUBMESSAGE_DISPOSITION_RESULT disposition)
AzureIoTClient 61:8b85a4e797cf 1074 {
AzureIoTClient 61:8b85a4e797cf 1075 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 61:8b85a4e797cf 1076 if ((iotHubClientHandle == NULL) || (message_data == NULL))
AzureIoTClient 61:8b85a4e797cf 1077 {
AzureIoTClient 61:8b85a4e797cf 1078 /*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 1079 LogError("Invalid argument handle=%p, message=%p", iotHubClientHandle, message_data);
AzureIoTClient 61:8b85a4e797cf 1080 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 61:8b85a4e797cf 1081 }
AzureIoTClient 61:8b85a4e797cf 1082 else
AzureIoTClient 61:8b85a4e797cf 1083 {
AzureIoTClient 61:8b85a4e797cf 1084 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 61:8b85a4e797cf 1085 /*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 1086 result = handleData->IoTHubTransport_SendMessageDisposition(message_data, disposition);
AzureIoTClient 61:8b85a4e797cf 1087 }
AzureIoTClient 42:448eecc3676e 1088 return result;
AzureIoTClient 16:deba40344375 1089 }
AzureIoTClient 16:deba40344375 1090
Azure.IoT Build 36:67300d5a4c1f 1091 static void DoTimeouts(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData)
Azure.IoT Build 36:67300d5a4c1f 1092 {
Azure.IoT.Build 54:6dcad9019a64 1093 tickcounter_ms_t nowTick;
AzureIoTClient 42:448eecc3676e 1094 if (tickcounter_get_current_ms(handleData->tickCounter, &nowTick) != 0)
AzureIoTClient 42:448eecc3676e 1095 {
AzureIoTClient 42:448eecc3676e 1096 LogError("unable to get the current ms, timeouts will not be processed");
AzureIoTClient 42:448eecc3676e 1097 }
AzureIoTClient 42:448eecc3676e 1098 else
AzureIoTClient 42:448eecc3676e 1099 {
AzureIoTClient 42:448eecc3676e 1100 DLIST_ENTRY* currentItemInWaitingToSend = handleData->waitingToSend.Flink;
AzureIoTClient 42:448eecc3676e 1101 while (currentItemInWaitingToSend != &(handleData->waitingToSend)) /*while we are not at the end of the list*/
AzureIoTClient 42:448eecc3676e 1102 {
AzureIoTClient 42:448eecc3676e 1103 IOTHUB_MESSAGE_LIST* fullEntry = containingRecord(currentItemInWaitingToSend, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 42:448eecc3676e 1104 /*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 1105 if ((fullEntry->ms_timesOutAfter != 0) && (fullEntry->ms_timesOutAfter < nowTick))
AzureIoTClient 42:448eecc3676e 1106 {
AzureIoTClient 42:448eecc3676e 1107 PDLIST_ENTRY theNext = currentItemInWaitingToSend->Flink; /*need to save the next item, because the below operations are destructive*/
AzureIoTClient 42:448eecc3676e 1108 DList_RemoveEntryList(currentItemInWaitingToSend);
AzureIoTClient 42:448eecc3676e 1109 if (fullEntry->callback != NULL)
AzureIoTClient 42:448eecc3676e 1110 {
AzureIoTClient 42:448eecc3676e 1111 fullEntry->callback(IOTHUB_CLIENT_CONFIRMATION_MESSAGE_TIMEOUT, fullEntry->context);
AzureIoTClient 42:448eecc3676e 1112 }
AzureIoTClient 42:448eecc3676e 1113 IoTHubMessage_Destroy(fullEntry->messageHandle); /*because it has been cloned*/
AzureIoTClient 42:448eecc3676e 1114 free(fullEntry);
AzureIoTClient 42:448eecc3676e 1115 currentItemInWaitingToSend = theNext;
AzureIoTClient 42:448eecc3676e 1116 }
AzureIoTClient 42:448eecc3676e 1117 else
AzureIoTClient 42:448eecc3676e 1118 {
AzureIoTClient 42:448eecc3676e 1119 currentItemInWaitingToSend = currentItemInWaitingToSend->Flink;
AzureIoTClient 42:448eecc3676e 1120 }
AzureIoTClient 42:448eecc3676e 1121 }
AzureIoTClient 42:448eecc3676e 1122 }
Azure.IoT Build 36:67300d5a4c1f 1123 }
Azure.IoT Build 36:67300d5a4c1f 1124
AzureIoTClient 16:deba40344375 1125 void IoTHubClient_LL_DoWork(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 16:deba40344375 1126 {
AzureIoTClient 42:448eecc3676e 1127 /*Codes_SRS_IOTHUBCLIENT_LL_02_020: [If parameter iotHubClientHandle is NULL then IoTHubClient_LL_DoWork shall not perform any action.] */
AzureIoTClient 42:448eecc3676e 1128 if (iotHubClientHandle != NULL)
AzureIoTClient 42:448eecc3676e 1129 {
AzureIoTClient 42:448eecc3676e 1130 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 1131 DoTimeouts(handleData);
AzureIoTClient 42:448eecc3676e 1132
AzureIoTClient 53:1e5a1ca1f274 1133 /*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 1134 DLIST_ENTRY* client_item = handleData->iot_msg_queue.Flink;
AzureIoTClient 53:1e5a1ca1f274 1135 while (client_item != &(handleData->iot_msg_queue)) /*while we are not at the end of the list*/
AzureIoTClient 53:1e5a1ca1f274 1136 {
AzureIoTClient 53:1e5a1ca1f274 1137 PDLIST_ENTRY next_item = client_item->Flink;
AzureIoTClient 53:1e5a1ca1f274 1138
AzureIoTClient 53:1e5a1ca1f274 1139 IOTHUB_DEVICE_TWIN* queue_data = containingRecord(client_item, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 1140 IOTHUB_IDENTITY_INFO identity_info;
AzureIoTClient 53:1e5a1ca1f274 1141 identity_info.device_twin = queue_data;
AzureIoTClient 53:1e5a1ca1f274 1142 IOTHUB_PROCESS_ITEM_RESULT process_results = handleData->IoTHubTransport_ProcessItem(handleData->transportHandle, IOTHUB_TYPE_DEVICE_TWIN, &identity_info);
AzureIoTClient 53:1e5a1ca1f274 1143 if (process_results == IOTHUB_PROCESS_CONTINUE || process_results == IOTHUB_PROCESS_NOT_CONNECTED)
AzureIoTClient 53:1e5a1ca1f274 1144 {
AzureIoTClient 53:1e5a1ca1f274 1145 /*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 1146 break;
AzureIoTClient 53:1e5a1ca1f274 1147 }
AzureIoTClient 53:1e5a1ca1f274 1148 else
AzureIoTClient 53:1e5a1ca1f274 1149 {
AzureIoTClient 53:1e5a1ca1f274 1150 DList_RemoveEntryList(client_item);
AzureIoTClient 53:1e5a1ca1f274 1151 if (process_results == IOTHUB_PROCESS_OK)
AzureIoTClient 53:1e5a1ca1f274 1152 {
AzureIoTClient 53:1e5a1ca1f274 1153 /*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 1154 DList_InsertTailList(&(iotHubClientHandle->iot_ack_queue), &(queue_data->entry));
AzureIoTClient 53:1e5a1ca1f274 1155 }
AzureIoTClient 53:1e5a1ca1f274 1156 else
AzureIoTClient 53:1e5a1ca1f274 1157 {
AzureIoTClient 53:1e5a1ca1f274 1158 /*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 1159 LogError("Failure queue processing item");
AzureIoTClient 53:1e5a1ca1f274 1160 device_twin_data_destroy(queue_data);
AzureIoTClient 53:1e5a1ca1f274 1161 }
AzureIoTClient 53:1e5a1ca1f274 1162 }
AzureIoTClient 53:1e5a1ca1f274 1163 // Move along to the next item
AzureIoTClient 53:1e5a1ca1f274 1164 client_item = next_item;
AzureIoTClient 53:1e5a1ca1f274 1165 }
AzureIoTClient 53:1e5a1ca1f274 1166
AzureIoTClient 42:448eecc3676e 1167 /*Codes_SRS_IOTHUBCLIENT_LL_02_021: [Otherwise, IoTHubClient_LL_DoWork shall invoke the underlaying layer's _DoWork function.]*/
AzureIoTClient 42:448eecc3676e 1168 handleData->IoTHubTransport_DoWork(handleData->transportHandle, iotHubClientHandle);
AzureIoTClient 42:448eecc3676e 1169 }
AzureIoTClient 16:deba40344375 1170 }
AzureIoTClient 16:deba40344375 1171
AzureIoTClient 16:deba40344375 1172 IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetSendStatus(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 16:deba40344375 1173 {
AzureIoTClient 42:448eecc3676e 1174 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1175
AzureIoTClient 42:448eecc3676e 1176 /* Codes_SRS_IOTHUBCLIENT_09_007: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter] */
AzureIoTClient 42:448eecc3676e 1177 if (iotHubClientHandle == NULL || iotHubClientStatus == NULL)
AzureIoTClient 42:448eecc3676e 1178 {
AzureIoTClient 42:448eecc3676e 1179 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 45:54c11b1b1407 1180 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 1181 }
AzureIoTClient 42:448eecc3676e 1182 else
AzureIoTClient 42:448eecc3676e 1183 {
AzureIoTClient 42:448eecc3676e 1184 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1185
AzureIoTClient 42:448eecc3676e 1186 /* 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 1187 /* 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 1188 result = handleData->IoTHubTransport_GetSendStatus(handleData->deviceHandle, iotHubClientStatus);
AzureIoTClient 42:448eecc3676e 1189 }
AzureIoTClient 16:deba40344375 1190
AzureIoTClient 42:448eecc3676e 1191 return result;
AzureIoTClient 16:deba40344375 1192 }
AzureIoTClient 16:deba40344375 1193
Azure.IoT Build 45:54c11b1b1407 1194 void IoTHubClient_LL_SendComplete(IOTHUB_CLIENT_LL_HANDLE handle, PDLIST_ENTRY completed, IOTHUB_CLIENT_CONFIRMATION_RESULT result)
AzureIoTClient 16:deba40344375 1195 {
AzureIoTClient 42:448eecc3676e 1196 /*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 1197 if (
AzureIoTClient 42:448eecc3676e 1198 (handle == NULL) ||
AzureIoTClient 42:448eecc3676e 1199 (completed == NULL)
AzureIoTClient 42:448eecc3676e 1200 )
AzureIoTClient 42:448eecc3676e 1201 {
AzureIoTClient 42:448eecc3676e 1202 /*"shall return"*/
AzureIoTClient 42:448eecc3676e 1203 LogError("invalid arg");
AzureIoTClient 42:448eecc3676e 1204 }
AzureIoTClient 42:448eecc3676e 1205 else
AzureIoTClient 42:448eecc3676e 1206 {
Azure.IoT Build 45:54c11b1b1407 1207 /*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 1208 /*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 1209 PDLIST_ENTRY oldest;
AzureIoTClient 42:448eecc3676e 1210 while ((oldest = DList_RemoveHeadList(completed)) != completed)
AzureIoTClient 42:448eecc3676e 1211 {
AzureIoTClient 42:448eecc3676e 1212 IOTHUB_MESSAGE_LIST* messageList = (IOTHUB_MESSAGE_LIST*)containingRecord(oldest, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 42:448eecc3676e 1213 /*Codes_SRS_IOTHUBCLIENT_LL_02_026: [If any callback is NULL then there shall not be a callback call.]*/
AzureIoTClient 42:448eecc3676e 1214 if (messageList->callback != NULL)
AzureIoTClient 42:448eecc3676e 1215 {
Azure.IoT Build 45:54c11b1b1407 1216 messageList->callback(result, messageList->context);
AzureIoTClient 42:448eecc3676e 1217 }
AzureIoTClient 42:448eecc3676e 1218 IoTHubMessage_Destroy(messageList->messageHandle);
AzureIoTClient 42:448eecc3676e 1219 free(messageList);
AzureIoTClient 42:448eecc3676e 1220 }
AzureIoTClient 42:448eecc3676e 1221 }
AzureIoTClient 16:deba40344375 1222 }
AzureIoTClient 16:deba40344375 1223
AzureIoTClient 55:59b527ab3452 1224 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 1225 {
AzureIoTClient 53:1e5a1ca1f274 1226 int result;
AzureIoTClient 55:59b527ab3452 1227 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1228 {
AzureIoTClient 53:1e5a1ca1f274 1229 /* Codes_SRS_IOTHUBCLIENT_LL_07_017: [ If handle or response is NULL then IoTHubClient_LL_DeviceMethodComplete shall return 500. ] */
AzureIoTClient 53:1e5a1ca1f274 1230 LogError("Invalid argument handle=%p", handle);
AzureIoTClient 60:41648c4e7036 1231 result = __FAILURE__;
AzureIoTClient 53:1e5a1ca1f274 1232 }
AzureIoTClient 53:1e5a1ca1f274 1233 else
AzureIoTClient 53:1e5a1ca1f274 1234 {
AzureIoTClient 53:1e5a1ca1f274 1235 /* Codes_SRS_IOTHUBCLIENT_LL_07_018: [ If deviceMethodCallback is not NULL IoTHubClient_LL_DeviceMethodComplete shall execute deviceMethodCallback and return the status. ] */
AzureIoTClient 53:1e5a1ca1f274 1236 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 62:5a4cdacf5090 1237 switch (handleData->methodCallback.type)
AzureIoTClient 53:1e5a1ca1f274 1238 {
AzureIoTClient 62:5a4cdacf5090 1239 case CALLBACK_TYPE_SYNC:
AzureIoTClient 53:1e5a1ca1f274 1240 {
AzureIoTClient 62:5a4cdacf5090 1241 unsigned char* payload_resp = NULL;
AzureIoTClient 62:5a4cdacf5090 1242 size_t response_size = 0;
AzureIoTClient 62:5a4cdacf5090 1243 result = handleData->methodCallback.callbackSync(method_name, payLoad, size, &payload_resp, &response_size, handleData->methodCallback.userContextCallback);
AzureIoTClient 62:5a4cdacf5090 1244 /* 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 1245 if (payload_resp != NULL && response_size > 0)
AzureIoTClient 62:5a4cdacf5090 1246 {
AzureIoTClient 62:5a4cdacf5090 1247 result = handleData->IoTHubTransport_DeviceMethod_Response(handleData->deviceHandle, response_id, payload_resp, response_size, result);
AzureIoTClient 62:5a4cdacf5090 1248 }
AzureIoTClient 62:5a4cdacf5090 1249 else
AzureIoTClient 62:5a4cdacf5090 1250 {
AzureIoTClient 62:5a4cdacf5090 1251 result = __FAILURE__;
AzureIoTClient 62:5a4cdacf5090 1252 }
AzureIoTClient 62:5a4cdacf5090 1253 if (payload_resp != NULL)
AzureIoTClient 62:5a4cdacf5090 1254 {
AzureIoTClient 62:5a4cdacf5090 1255 free(payload_resp);
AzureIoTClient 62:5a4cdacf5090 1256 }
AzureIoTClient 62:5a4cdacf5090 1257 break;
AzureIoTClient 53:1e5a1ca1f274 1258 }
AzureIoTClient 62:5a4cdacf5090 1259 case CALLBACK_TYPE_ASYNC:
AzureIoTClient 62:5a4cdacf5090 1260 result = handleData->methodCallback.callbackAsync(method_name, payLoad, size, response_id, handleData->methodCallback.userContextCallback);
AzureIoTClient 62:5a4cdacf5090 1261 break;
AzureIoTClient 62:5a4cdacf5090 1262 default:
AzureIoTClient 62:5a4cdacf5090 1263 /* Codes_SRS_IOTHUBCLIENT_LL_07_019: [ If deviceMethodCallback is NULL IoTHubClient_LL_DeviceMethodComplete shall return 404. ] */
AzureIoTClient 62:5a4cdacf5090 1264 result = 0;
AzureIoTClient 62:5a4cdacf5090 1265 break;
AzureIoTClient 53:1e5a1ca1f274 1266 }
AzureIoTClient 53:1e5a1ca1f274 1267 }
AzureIoTClient 53:1e5a1ca1f274 1268 return result;
AzureIoTClient 53:1e5a1ca1f274 1269 }
AzureIoTClient 53:1e5a1ca1f274 1270
AzureIoTClient 53:1e5a1ca1f274 1271 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 1272 {
AzureIoTClient 53:1e5a1ca1f274 1273 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1274 {
AzureIoTClient 53:1e5a1ca1f274 1275 /* Codes_SRS_IOTHUBCLIENT_LL_07_013: [ If handle is NULL then IoTHubClient_LL_RetrievePropertyComplete shall do nothing.] */
AzureIoTClient 53:1e5a1ca1f274 1276 LogError("Invalid argument handle=%p", handle);
AzureIoTClient 53:1e5a1ca1f274 1277 }
AzureIoTClient 53:1e5a1ca1f274 1278 else
AzureIoTClient 53:1e5a1ca1f274 1279 {
AzureIoTClient 53:1e5a1ca1f274 1280 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 53:1e5a1ca1f274 1281 /* Codes_SRS_IOTHUBCLIENT_LL_07_014: [ If deviceTwinCallback is NULL then IoTHubClient_LL_RetrievePropertyComplete shall do nothing.] */
AzureIoTClient 53:1e5a1ca1f274 1282 if (handleData->deviceTwinCallback)
AzureIoTClient 53:1e5a1ca1f274 1283 {
AzureIoTClient 53:1e5a1ca1f274 1284 /* 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 1285 if (update_state == DEVICE_TWIN_UPDATE_COMPLETE)
AzureIoTClient 53:1e5a1ca1f274 1286 {
AzureIoTClient 53:1e5a1ca1f274 1287 handleData->complete_twin_update_encountered = true;
AzureIoTClient 53:1e5a1ca1f274 1288 }
AzureIoTClient 53:1e5a1ca1f274 1289 if (handleData->complete_twin_update_encountered)
AzureIoTClient 53:1e5a1ca1f274 1290 {
AzureIoTClient 53:1e5a1ca1f274 1291 /* 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 1292 handleData->deviceTwinCallback(update_state, payLoad, size, handleData->deviceTwinContextCallback);
AzureIoTClient 53:1e5a1ca1f274 1293 }
AzureIoTClient 53:1e5a1ca1f274 1294 }
AzureIoTClient 53:1e5a1ca1f274 1295 }
AzureIoTClient 53:1e5a1ca1f274 1296 }
AzureIoTClient 53:1e5a1ca1f274 1297
AzureIoTClient 53:1e5a1ca1f274 1298 void IoTHubClient_LL_ReportedStateComplete(IOTHUB_CLIENT_LL_HANDLE handle, uint32_t item_id, int status_code)
AzureIoTClient 53:1e5a1ca1f274 1299 {
AzureIoTClient 53:1e5a1ca1f274 1300 /* Codes_SRS_IOTHUBCLIENT_LL_07_002: [ if handle or queue_handle are NULL then IoTHubClient_LL_ReportedStateComplete shall do nothing. ] */
AzureIoTClient 53:1e5a1ca1f274 1301 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1302 {
AzureIoTClient 53:1e5a1ca1f274 1303 /*"shall return"*/
AzureIoTClient 53:1e5a1ca1f274 1304 LogError("Invalid argument handle=%p", handle);
AzureIoTClient 53:1e5a1ca1f274 1305 }
AzureIoTClient 53:1e5a1ca1f274 1306 else
AzureIoTClient 53:1e5a1ca1f274 1307 {
AzureIoTClient 53:1e5a1ca1f274 1308 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 53:1e5a1ca1f274 1309
AzureIoTClient 53:1e5a1ca1f274 1310 /* Codes_SRS_IOTHUBCLIENT_LL_07_003: [ IoTHubClient_LL_ReportedStateComplete shall enumerate through the IOTHUB_DEVICE_TWIN structures in queue_handle. ]*/
AzureIoTClient 53:1e5a1ca1f274 1311 DLIST_ENTRY* client_item = handleData->iot_ack_queue.Flink;
AzureIoTClient 53:1e5a1ca1f274 1312 while (client_item != &(handleData->iot_ack_queue)) /*while we are not at the end of the list*/
AzureIoTClient 53:1e5a1ca1f274 1313 {
AzureIoTClient 53:1e5a1ca1f274 1314 PDLIST_ENTRY next_item = client_item->Flink;
AzureIoTClient 53:1e5a1ca1f274 1315 IOTHUB_DEVICE_TWIN* queue_data = containingRecord(client_item, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 1316 if (queue_data->item_id == item_id)
AzureIoTClient 53:1e5a1ca1f274 1317 {
AzureIoTClient 53:1e5a1ca1f274 1318 if (queue_data->reported_state_callback != NULL)
AzureIoTClient 53:1e5a1ca1f274 1319 {
AzureIoTClient 53:1e5a1ca1f274 1320 queue_data->reported_state_callback(status_code, queue_data->context);
AzureIoTClient 53:1e5a1ca1f274 1321 }
AzureIoTClient 53:1e5a1ca1f274 1322 /*Codes_SRS_IOTHUBCLIENT_LL_07_009: [ IoTHubClient_LL_ReportedStateComplete shall remove the IOTHUB_DEVICE_TWIN item from the ack queue.]*/
AzureIoTClient 53:1e5a1ca1f274 1323 DList_RemoveEntryList(client_item);
AzureIoTClient 53:1e5a1ca1f274 1324 device_twin_data_destroy(queue_data);
AzureIoTClient 53:1e5a1ca1f274 1325 break;
AzureIoTClient 53:1e5a1ca1f274 1326 }
AzureIoTClient 53:1e5a1ca1f274 1327 client_item = next_item;
AzureIoTClient 53:1e5a1ca1f274 1328 }
AzureIoTClient 53:1e5a1ca1f274 1329 }
AzureIoTClient 53:1e5a1ca1f274 1330 }
AzureIoTClient 53:1e5a1ca1f274 1331
AzureIoTClient 61:8b85a4e797cf 1332 bool IoTHubClient_LL_MessageCallback(IOTHUB_CLIENT_LL_HANDLE handle, MESSAGE_CALLBACK_INFO* messageData)
AzureIoTClient 16:deba40344375 1333 {
AzureIoTClient 61:8b85a4e797cf 1334 bool result;
AzureIoTClient 61:8b85a4e797cf 1335 if ((handle == NULL) || messageData == NULL)
AzureIoTClient 42:448eecc3676e 1336 {
AzureIoTClient 61:8b85a4e797cf 1337 /*Codes_SRS_IOTHUBCLIENT_LL_02_029: [If parameter handle is NULL then IoTHubClient_LL_MessageCallback shall return IOTHUBMESSAGE_ABANDONED.] */
AzureIoTClient 61:8b85a4e797cf 1338 LogError("invalid argument: handle(%p), messageData(%p)", handle, messageData);
AzureIoTClient 61:8b85a4e797cf 1339 result = false;
AzureIoTClient 61:8b85a4e797cf 1340 }
AzureIoTClient 61:8b85a4e797cf 1341 else if (messageData->messageHandle == NULL)
AzureIoTClient 61:8b85a4e797cf 1342 {
AzureIoTClient 61:8b85a4e797cf 1343 /*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 1344 LogError("invalid argument messageData->messageHandle(NULL)");
AzureIoTClient 61:8b85a4e797cf 1345 result = false;
AzureIoTClient 42:448eecc3676e 1346 }
AzureIoTClient 42:448eecc3676e 1347 else
AzureIoTClient 42:448eecc3676e 1348 {
AzureIoTClient 42:448eecc3676e 1349 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 16:deba40344375 1350
AzureIoTClient 42:448eecc3676e 1351 /* Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClient_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] */
AzureIoTClient 42:448eecc3676e 1352 handleData->lastMessageReceiveTime = get_time(NULL);
AzureIoTClient 62:5a4cdacf5090 1353 switch (handleData->messageCallback.type)
AzureIoTClient 42:448eecc3676e 1354 {
AzureIoTClient 62:5a4cdacf5090 1355 case CALLBACK_TYPE_NONE:
AzureIoTClient 61:8b85a4e797cf 1356 {
AzureIoTClient 61:8b85a4e797cf 1357 /*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 1358 LogError("Invalid workflow - not currently set up to accept messages");
AzureIoTClient 61:8b85a4e797cf 1359 result = false;
AzureIoTClient 61:8b85a4e797cf 1360 break;
AzureIoTClient 61:8b85a4e797cf 1361 }
AzureIoTClient 62:5a4cdacf5090 1362 case CALLBACK_TYPE_SYNC:
AzureIoTClient 61:8b85a4e797cf 1363 {
AzureIoTClient 61:8b85a4e797cf 1364 /*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 1365 IOTHUBMESSAGE_DISPOSITION_RESULT cb_result = handleData->messageCallback.callbackSync(messageData->messageHandle, handleData->messageCallback.userContextCallback);
AzureIoTClient 61:8b85a4e797cf 1366
AzureIoTClient 61:8b85a4e797cf 1367 /*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 1368 if (handleData->IoTHubTransport_SendMessageDisposition(messageData, cb_result) != IOTHUB_CLIENT_OK)
AzureIoTClient 61:8b85a4e797cf 1369 {
AzureIoTClient 61:8b85a4e797cf 1370 LogError("IoTHubTransport_SendMessageDisposition failed");
AzureIoTClient 61:8b85a4e797cf 1371 }
AzureIoTClient 61:8b85a4e797cf 1372 result = true;
AzureIoTClient 61:8b85a4e797cf 1373 break;
AzureIoTClient 61:8b85a4e797cf 1374 }
AzureIoTClient 62:5a4cdacf5090 1375 case CALLBACK_TYPE_ASYNC:
AzureIoTClient 61:8b85a4e797cf 1376 {
AzureIoTClient 61:8b85a4e797cf 1377 /* Codes_SRS_IOTHUBCLIENT_LL_10_009: [If messageCallbackType is ASYNC then IoTHubClient_LL_MessageCallback shall return what messageCallbacEx returns.] */
AzureIoTClient 62:5a4cdacf5090 1378 result = handleData->messageCallback.callbackAsync(messageData, handleData->messageCallback.userContextCallback);
AzureIoTClient 61:8b85a4e797cf 1379 if (!result)
AzureIoTClient 61:8b85a4e797cf 1380 {
AzureIoTClient 61:8b85a4e797cf 1381 LogError("messageCallbackEx failed");
AzureIoTClient 61:8b85a4e797cf 1382 }
AzureIoTClient 61:8b85a4e797cf 1383 break;
AzureIoTClient 61:8b85a4e797cf 1384 }
AzureIoTClient 61:8b85a4e797cf 1385 default:
AzureIoTClient 61:8b85a4e797cf 1386 {
AzureIoTClient 61:8b85a4e797cf 1387 LogError("Invalid state");
AzureIoTClient 61:8b85a4e797cf 1388 result = false;
AzureIoTClient 61:8b85a4e797cf 1389 break;
AzureIoTClient 61:8b85a4e797cf 1390 }
AzureIoTClient 42:448eecc3676e 1391 }
AzureIoTClient 42:448eecc3676e 1392 }
AzureIoTClient 61:8b85a4e797cf 1393 return result;
AzureIoTClient 16:deba40344375 1394 }
AzureIoTClient 16:deba40344375 1395
AzureIoTClient 61:8b85a4e797cf 1396 void IoTHubClient_LL_ConnectionStatusCallBack(IOTHUB_CLIENT_LL_HANDLE handle, IOTHUB_CLIENT_CONNECTION_STATUS status, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason)
AzureIoTClient 52:1cc3c6d07cad 1397 {
AzureIoTClient 61:8b85a4e797cf 1398 /*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 1399 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1400 {
AzureIoTClient 53:1e5a1ca1f274 1401 /*"shall return"*/
AzureIoTClient 53:1e5a1ca1f274 1402 LogError("invalid arg");
AzureIoTClient 53:1e5a1ca1f274 1403 }
AzureIoTClient 53:1e5a1ca1f274 1404 else
AzureIoTClient 53:1e5a1ca1f274 1405 {
AzureIoTClient 53:1e5a1ca1f274 1406 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 53:1e5a1ca1f274 1407
AzureIoTClient 61:8b85a4e797cf 1408 /*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 1409 if (handleData->conStatusCallback != NULL)
AzureIoTClient 53:1e5a1ca1f274 1410 {
AzureIoTClient 53:1e5a1ca1f274 1411 handleData->conStatusCallback(status, reason, handleData->conStatusUserContextCallback);
AzureIoTClient 53:1e5a1ca1f274 1412 }
AzureIoTClient 53:1e5a1ca1f274 1413 }
AzureIoTClient 53:1e5a1ca1f274 1414
AzureIoTClient 52:1cc3c6d07cad 1415 }
AzureIoTClient 52:1cc3c6d07cad 1416
AzureIoTClient 52:1cc3c6d07cad 1417 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetConnectionStatusCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK connectionStatusCallback, void * userContextCallback)
AzureIoTClient 52:1cc3c6d07cad 1418 {
AzureIoTClient 53:1e5a1ca1f274 1419 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1420 /*Codes_SRS_IOTHUBCLIENT_LL_25_111: [IoTHubClient_LL_SetConnectionStatusCallback shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter iotHubClientHandle**]** */
AzureIoTClient 53:1e5a1ca1f274 1421 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1422 {
AzureIoTClient 53:1e5a1ca1f274 1423 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1424 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1425 }
AzureIoTClient 53:1e5a1ca1f274 1426 else
AzureIoTClient 53:1e5a1ca1f274 1427 {
AzureIoTClient 53:1e5a1ca1f274 1428 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1429 /*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 1430 handleData->conStatusCallback = connectionStatusCallback;
AzureIoTClient 53:1e5a1ca1f274 1431 handleData->conStatusUserContextCallback = userContextCallback;
AzureIoTClient 53:1e5a1ca1f274 1432 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1433 }
AzureIoTClient 52:1cc3c6d07cad 1434
AzureIoTClient 52:1cc3c6d07cad 1435 return result;
AzureIoTClient 52:1cc3c6d07cad 1436 }
AzureIoTClient 52:1cc3c6d07cad 1437
AzureIoTClient 53:1e5a1ca1f274 1438 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetRetryPolicy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1439 {
AzureIoTClient 53:1e5a1ca1f274 1440 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1441 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 52:1cc3c6d07cad 1442
AzureIoTClient 62:5a4cdacf5090 1443 /* 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 1444 if (handleData == NULL)
AzureIoTClient 53:1e5a1ca1f274 1445 {
AzureIoTClient 53:1e5a1ca1f274 1446 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1447 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1448 }
AzureIoTClient 53:1e5a1ca1f274 1449 else
AzureIoTClient 53:1e5a1ca1f274 1450 {
AzureIoTClient 53:1e5a1ca1f274 1451 if (handleData->transportHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1452 {
AzureIoTClient 53:1e5a1ca1f274 1453 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1454 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1455 }
AzureIoTClient 53:1e5a1ca1f274 1456 else
AzureIoTClient 53:1e5a1ca1f274 1457 {
AzureIoTClient 53:1e5a1ca1f274 1458 if (handleData->IoTHubTransport_SetRetryPolicy(handleData->transportHandle, retryPolicy, retryTimeoutLimitInSeconds) != 0)
AzureIoTClient 53:1e5a1ca1f274 1459 {
AzureIoTClient 53:1e5a1ca1f274 1460 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1461 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1462 }
AzureIoTClient 53:1e5a1ca1f274 1463 else
AzureIoTClient 53:1e5a1ca1f274 1464 {
AzureIoTClient 62:5a4cdacf5090 1465 /* 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 1466 /* 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 1467 handleData->retryPolicy = retryPolicy;
AzureIoTClient 53:1e5a1ca1f274 1468 handleData->retryTimeoutLimitInSeconds = retryTimeoutLimitInSeconds;
AzureIoTClient 53:1e5a1ca1f274 1469 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1470 }
AzureIoTClient 53:1e5a1ca1f274 1471 }
AzureIoTClient 53:1e5a1ca1f274 1472 }
AzureIoTClient 52:1cc3c6d07cad 1473 return result;
AzureIoTClient 52:1cc3c6d07cad 1474 }
AzureIoTClient 52:1cc3c6d07cad 1475
AzureIoTClient 53:1e5a1ca1f274 1476 IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetRetryPolicy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY* retryPolicy, size_t* retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1477 {
AzureIoTClient 62:5a4cdacf5090 1478 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1479
AzureIoTClient 53:1e5a1ca1f274 1480 /* 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 1481 if (iotHubClientHandle == NULL || retryPolicy == NULL || retryTimeoutLimitInSeconds == NULL)
AzureIoTClient 53:1e5a1ca1f274 1482 {
AzureIoTClient 62:5a4cdacf5090 1483 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 1484 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1485 }
AzureIoTClient 53:1e5a1ca1f274 1486 else
AzureIoTClient 53:1e5a1ca1f274 1487 {
AzureIoTClient 62:5a4cdacf5090 1488 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 62:5a4cdacf5090 1489
AzureIoTClient 53:1e5a1ca1f274 1490 *retryPolicy = handleData->retryPolicy;
AzureIoTClient 53:1e5a1ca1f274 1491 *retryTimeoutLimitInSeconds = handleData->retryTimeoutLimitInSeconds;
AzureIoTClient 53:1e5a1ca1f274 1492 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1493 }
AzureIoTClient 52:1cc3c6d07cad 1494
AzureIoTClient 52:1cc3c6d07cad 1495 return result;
AzureIoTClient 52:1cc3c6d07cad 1496 }
AzureIoTClient 52:1cc3c6d07cad 1497
AzureIoTClient 16:deba40344375 1498 IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetLastMessageReceiveTime(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, time_t* lastMessageReceiveTime)
AzureIoTClient 16:deba40344375 1499 {
AzureIoTClient 42:448eecc3676e 1500 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1501 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1502
AzureIoTClient 42:448eecc3676e 1503 /* 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 1504 if (handleData == NULL || lastMessageReceiveTime == NULL)
AzureIoTClient 42:448eecc3676e 1505 {
AzureIoTClient 42:448eecc3676e 1506 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 45:54c11b1b1407 1507 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 1508 }
AzureIoTClient 42:448eecc3676e 1509 else
AzureIoTClient 42:448eecc3676e 1510 {
AzureIoTClient 42:448eecc3676e 1511 /* 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 1512 if (handleData->lastMessageReceiveTime == INDEFINITE_TIME)
AzureIoTClient 42:448eecc3676e 1513 {
AzureIoTClient 42:448eecc3676e 1514 result = IOTHUB_CLIENT_INDEFINITE_TIME;
Azure.IoT Build 45:54c11b1b1407 1515 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 1516 }
AzureIoTClient 42:448eecc3676e 1517 else
AzureIoTClient 42:448eecc3676e 1518 {
AzureIoTClient 42:448eecc3676e 1519 /* 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 1520 /* Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClient_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] */
AzureIoTClient 42:448eecc3676e 1521 *lastMessageReceiveTime = handleData->lastMessageReceiveTime;
AzureIoTClient 42:448eecc3676e 1522 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 1523 }
AzureIoTClient 42:448eecc3676e 1524 }
AzureIoTClient 16:deba40344375 1525
AzureIoTClient 42:448eecc3676e 1526 return result;
AzureIoTClient 16:deba40344375 1527 }
AzureIoTClient 16:deba40344375 1528
AzureIoTClient 16:deba40344375 1529 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetOption(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* optionName, const void* value)
AzureIoTClient 16:deba40344375 1530 {
AzureIoTClient 40:1a94db9139ea 1531
AzureIoTClient 42:448eecc3676e 1532 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1533 /*Codes_SRS_IOTHUBCLIENT_LL_02_034: [If iotHubClientHandle is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]*/
AzureIoTClient 42:448eecc3676e 1534 /*Codes_SRS_IOTHUBCLIENT_LL_02_035: [If optionName is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 42:448eecc3676e 1535 /*Codes_SRS_IOTHUBCLIENT_LL_02_036: [If value is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 42:448eecc3676e 1536 if (
AzureIoTClient 42:448eecc3676e 1537 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 1538 (optionName == NULL) ||
AzureIoTClient 42:448eecc3676e 1539 (value == NULL)
AzureIoTClient 42:448eecc3676e 1540 )
AzureIoTClient 42:448eecc3676e 1541 {
AzureIoTClient 42:448eecc3676e 1542 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 1543 LogError("invalid argument (NULL)");
AzureIoTClient 42:448eecc3676e 1544 }
AzureIoTClient 42:448eecc3676e 1545 else
AzureIoTClient 42:448eecc3676e 1546 {
AzureIoTClient 42:448eecc3676e 1547 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 1548
AzureIoTClient 42:448eecc3676e 1549 /*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 70:875388a7d419 1550 if (strcmp(optionName, OPTION_MESSAGE_TIMEOUT) == 0)
AzureIoTClient 42:448eecc3676e 1551 {
AzureIoTClient 42:448eecc3676e 1552 /*this is an option handled by IoTHubClient_LL*/
AzureIoTClient 42:448eecc3676e 1553 /*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 1554 handleData->currentMessageTimeout = *(const tickcounter_ms_t*)value;
AzureIoTClient 42:448eecc3676e 1555 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 1556 }
AzureIoTClient 66:a419827cb051 1557 else if (strcmp(optionName, OPTION_PRODUCT_INFO) == 0)
AzureIoTClient 66:a419827cb051 1558 {
AzureIoTClient 66:a419827cb051 1559 /*Codes_SRS_IOTHUBCLIENT_LL_10_033: [repeat calls with "product_info" will erase the previously set product information if applicatble. ]*/
AzureIoTClient 66:a419827cb051 1560 if (handleData->product_info != NULL)
AzureIoTClient 66:a419827cb051 1561 {
AzureIoTClient 66:a419827cb051 1562 STRING_delete(handleData->product_info);
AzureIoTClient 66:a419827cb051 1563 handleData->product_info = NULL;
AzureIoTClient 66:a419827cb051 1564 }
AzureIoTClient 66:a419827cb051 1565
AzureIoTClient 66:a419827cb051 1566 /*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 1567 handleData->product_info = make_product_info((const char*)value);
AzureIoTClient 66:a419827cb051 1568 if (handleData->product_info == NULL)
AzureIoTClient 66:a419827cb051 1569 {
AzureIoTClient 66:a419827cb051 1570 LogError("STRING_construct_sprintf failed");
AzureIoTClient 66:a419827cb051 1571 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1572 }
AzureIoTClient 66:a419827cb051 1573 else
AzureIoTClient 66:a419827cb051 1574 {
AzureIoTClient 66:a419827cb051 1575 result = IOTHUB_CLIENT_OK;
AzureIoTClient 66:a419827cb051 1576 }
AzureIoTClient 66:a419827cb051 1577 }
AzureIoTClient 42:448eecc3676e 1578 else
AzureIoTClient 42:448eecc3676e 1579 {
AzureIoTClient 46:6a69294b6119 1580
AzureIoTClient 46:6a69294b6119 1581 /*Codes_SRS_IOTHUBCLIENT_LL_02_099: [ IoTHubClient_LL_SetOption shall return according to the table below ]*/
AzureIoTClient 46:6a69294b6119 1582 IOTHUB_CLIENT_RESULT uploadToBlob_result;
AzureIoTClient 46:6a69294b6119 1583 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 46:6a69294b6119 1584 uploadToBlob_result = IoTHubClient_LL_UploadToBlob_SetOption(handleData->uploadToBlobHandle, optionName, value);
AzureIoTClient 46:6a69294b6119 1585 if(uploadToBlob_result == IOTHUB_CLIENT_ERROR)
AzureIoTClient 46:6a69294b6119 1586 {
AzureIoTClient 46:6a69294b6119 1587 LogError("unable to IoTHubClient_LL_UploadToBlob_SetOption");
AzureIoTClient 46:6a69294b6119 1588 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 46:6a69294b6119 1589 }
AzureIoTClient 46:6a69294b6119 1590 #else
AzureIoTClient 46:6a69294b6119 1591 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 1592 #endif /*DONT_USE_UPLOADTOBLOB*/
AzureIoTClient 16:deba40344375 1593
AzureIoTClient 70:875388a7d419 1594 /*Codes_SRS_IOTHUBCLIENT_LL_12_023: [** `c2d_keep_alive_freq_secs` - shall set the cloud to device keep alive frequency(in seconds) for the connection. Zero means keep alive will not be sent. ]*/
AzureIoTClient 46:6a69294b6119 1595 result =
AzureIoTClient 46:6a69294b6119 1596 /*based on uploadToBlob_result value this is what happens:*/
AzureIoTClient 46:6a69294b6119 1597 /*IOTHUB_CLIENT_INVALID_ARG always returns what IoTHubTransport_SetOption returns*/
AzureIoTClient 46:6a69294b6119 1598 /*IOTHUB_CLIENT_ERROR always returns IOTHUB_CLIENT_ERROR */
AzureIoTClient 46:6a69294b6119 1599 /*IOTHUB_CLIENT_OK returns OK
AzureIoTClient 46:6a69294b6119 1600 IOTHUB_CLIENT_OK if IoTHubTransport_SetOption returns OK or INVALID_ARG
AzureIoTClient 46:6a69294b6119 1601 IOTHUB_CLIENT_ERROR if IoTHubTransport_SetOption returns ERROR*/
AzureIoTClient 46:6a69294b6119 1602
AzureIoTClient 46:6a69294b6119 1603 (uploadToBlob_result == IOTHUB_CLIENT_INVALID_ARG) ? handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value) :
AzureIoTClient 46:6a69294b6119 1604 (uploadToBlob_result == IOTHUB_CLIENT_ERROR) ? IOTHUB_CLIENT_ERROR :
AzureIoTClient 46:6a69294b6119 1605 (handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value) == IOTHUB_CLIENT_ERROR) ? IOTHUB_CLIENT_ERROR : IOTHUB_CLIENT_OK;
AzureIoTClient 46:6a69294b6119 1606
AzureIoTClient 47:aaa262b5f898 1607 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 47:aaa262b5f898 1608 {
AzureIoTClient 47:aaa262b5f898 1609 LogError("underlying transport failed, returned = %s", ENUM_TO_STRING(IOTHUB_CLIENT_RESULT, result));
AzureIoTClient 42:448eecc3676e 1610 }
AzureIoTClient 47:aaa262b5f898 1611 }
AzureIoTClient 42:448eecc3676e 1612 }
AzureIoTClient 42:448eecc3676e 1613 return result;
AzureIoTClient 42:448eecc3676e 1614 }
AzureIoTClient 16:deba40344375 1615
AzureIoTClient 66:a419827cb051 1616 IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetOption(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* optionName, void** value)
AzureIoTClient 66:a419827cb051 1617 {
AzureIoTClient 66:a419827cb051 1618 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 66:a419827cb051 1619
AzureIoTClient 66:a419827cb051 1620 if ((iotHubClientHandle == NULL) || (optionName == NULL) || (value == NULL))
AzureIoTClient 66:a419827cb051 1621 {
AzureIoTClient 66:a419827cb051 1622 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 66:a419827cb051 1623 LogError("invalid argument iotHubClientHandle(%p); optionName(%p); value(%p)", iotHubClientHandle, optionName, value);
AzureIoTClient 66:a419827cb051 1624 }
AzureIoTClient 66:a419827cb051 1625 else if (strcmp(optionName, OPTION_PRODUCT_INFO) == 0)
AzureIoTClient 66:a419827cb051 1626 {
AzureIoTClient 66:a419827cb051 1627 result = IOTHUB_CLIENT_OK;
AzureIoTClient 66:a419827cb051 1628 *value = iotHubClientHandle->product_info;
AzureIoTClient 66:a419827cb051 1629 }
AzureIoTClient 66:a419827cb051 1630 else
AzureIoTClient 66:a419827cb051 1631 {
AzureIoTClient 66:a419827cb051 1632 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 66:a419827cb051 1633 LogError("invalid argument (%s)", optionName);
AzureIoTClient 66:a419827cb051 1634 }
AzureIoTClient 66:a419827cb051 1635 return result;
AzureIoTClient 66:a419827cb051 1636 }
AzureIoTClient 66:a419827cb051 1637
AzureIoTClient 53:1e5a1ca1f274 1638 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetDeviceTwinCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1639 {
AzureIoTClient 53:1e5a1ca1f274 1640 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1641 /* 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 1642 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1643 {
AzureIoTClient 53:1e5a1ca1f274 1644 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1645 LogError("Invalid argument specified iothubClientHandle=%p", iotHubClientHandle);
AzureIoTClient 53:1e5a1ca1f274 1646 }
AzureIoTClient 53:1e5a1ca1f274 1647 else
AzureIoTClient 53:1e5a1ca1f274 1648 {
AzureIoTClient 53:1e5a1ca1f274 1649 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1650 if (deviceTwinCallback == NULL)
AzureIoTClient 53:1e5a1ca1f274 1651 {
AzureIoTClient 53:1e5a1ca1f274 1652 /* 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 1653 handleData->IoTHubTransport_Unsubscribe_DeviceTwin(handleData->transportHandle);
AzureIoTClient 53:1e5a1ca1f274 1654 handleData->deviceTwinCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 1655 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1656 }
AzureIoTClient 53:1e5a1ca1f274 1657 else
AzureIoTClient 53:1e5a1ca1f274 1658 {
AzureIoTClient 53:1e5a1ca1f274 1659 /* 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 1660 if (handleData->IoTHubTransport_Subscribe_DeviceTwin(handleData->transportHandle) == 0)
AzureIoTClient 53:1e5a1ca1f274 1661 {
AzureIoTClient 53:1e5a1ca1f274 1662 handleData->deviceTwinCallback = deviceTwinCallback;
AzureIoTClient 53:1e5a1ca1f274 1663 handleData->deviceTwinContextCallback = userContextCallback;
AzureIoTClient 53:1e5a1ca1f274 1664 /* Codes_SRS_IOTHUBCLIENT_LL_10_005: [ Otherwise IoTHubClient_LL_SetDeviceTwinCallback shall succeed and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 53:1e5a1ca1f274 1665 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1666 }
AzureIoTClient 53:1e5a1ca1f274 1667 else
AzureIoTClient 53:1e5a1ca1f274 1668 {
AzureIoTClient 53:1e5a1ca1f274 1669 /* 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 1670 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1671 }
AzureIoTClient 53:1e5a1ca1f274 1672 }
AzureIoTClient 53:1e5a1ca1f274 1673 }
AzureIoTClient 53:1e5a1ca1f274 1674 return result;
AzureIoTClient 53:1e5a1ca1f274 1675 }
AzureIoTClient 53:1e5a1ca1f274 1676
AzureIoTClient 53:1e5a1ca1f274 1677 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 1678 {
AzureIoTClient 53:1e5a1ca1f274 1679 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1680 /* 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 1681 /* 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 1682 /* 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 1683 if (iotHubClientHandle == NULL || (reportedState == NULL || size == 0) )
AzureIoTClient 53:1e5a1ca1f274 1684 {
AzureIoTClient 53:1e5a1ca1f274 1685 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1686 LogError("Invalid argument specified iothubClientHandle=%p, reportedState=%p, size=%zu", iotHubClientHandle, reportedState, size);
AzureIoTClient 53:1e5a1ca1f274 1687 }
AzureIoTClient 53:1e5a1ca1f274 1688 else
AzureIoTClient 53:1e5a1ca1f274 1689 {
AzureIoTClient 53:1e5a1ca1f274 1690 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1691 /* 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 1692 IOTHUB_DEVICE_TWIN* client_data = dev_twin_data_create(handleData, get_next_item_id(handleData), reportedState, size, reportedStateCallback, userContextCallback);
AzureIoTClient 53:1e5a1ca1f274 1693 if (client_data == NULL)
AzureIoTClient 53:1e5a1ca1f274 1694 {
AzureIoTClient 53:1e5a1ca1f274 1695 /* Codes_SRS_IOTHUBCLIENT_LL_10_015: [If any error is encountered IoTHubClient_LL_SendReportedState shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 53:1e5a1ca1f274 1696 LogError("Failure constructing device twin data");
AzureIoTClient 53:1e5a1ca1f274 1697 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1698 }
AzureIoTClient 53:1e5a1ca1f274 1699 else
AzureIoTClient 53:1e5a1ca1f274 1700 {
AzureIoTClient 53:1e5a1ca1f274 1701 if (handleData->IoTHubTransport_Subscribe_DeviceTwin(handleData->transportHandle) != 0)
AzureIoTClient 53:1e5a1ca1f274 1702 {
AzureIoTClient 53:1e5a1ca1f274 1703 LogError("Failure adding device twin data to queue");
AzureIoTClient 53:1e5a1ca1f274 1704 device_twin_data_destroy(client_data);
AzureIoTClient 53:1e5a1ca1f274 1705 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1706 }
AzureIoTClient 53:1e5a1ca1f274 1707 else
AzureIoTClient 53:1e5a1ca1f274 1708 {
AzureIoTClient 53:1e5a1ca1f274 1709 /* 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 1710 DList_InsertTailList(&(iotHubClientHandle->iot_msg_queue), &(client_data->entry));
AzureIoTClient 53:1e5a1ca1f274 1711
AzureIoTClient 53:1e5a1ca1f274 1712 /* Codes_SRS_IOTHUBCLIENT_LL_10_016: [ Otherwise IoTHubClient_LL_SendReportedState shall succeed and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 53:1e5a1ca1f274 1713 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1714 }
AzureIoTClient 53:1e5a1ca1f274 1715 }
AzureIoTClient 53:1e5a1ca1f274 1716 }
AzureIoTClient 53:1e5a1ca1f274 1717 return result;
AzureIoTClient 53:1e5a1ca1f274 1718 }
AzureIoTClient 53:1e5a1ca1f274 1719
AzureIoTClient 53:1e5a1ca1f274 1720 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetDeviceMethodCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC deviceMethodCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1721 {
AzureIoTClient 53:1e5a1ca1f274 1722 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1723
AzureIoTClient 53:1e5a1ca1f274 1724 /*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 1725 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1726 {
AzureIoTClient 53:1e5a1ca1f274 1727 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1728 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1729 }
AzureIoTClient 53:1e5a1ca1f274 1730 else
AzureIoTClient 53:1e5a1ca1f274 1731 {
AzureIoTClient 53:1e5a1ca1f274 1732 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1733 if (deviceMethodCallback == NULL)
AzureIoTClient 53:1e5a1ca1f274 1734 {
AzureIoTClient 62:5a4cdacf5090 1735 if (handleData->methodCallback.type == CALLBACK_TYPE_NONE)
AzureIoTClient 62:5a4cdacf5090 1736 {
AzureIoTClient 62:5a4cdacf5090 1737 /* 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 1738 LogError("not currently set to accept or process incoming messages.");
AzureIoTClient 62:5a4cdacf5090 1739 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1740 }
AzureIoTClient 62:5a4cdacf5090 1741 else if (handleData->methodCallback.type == CALLBACK_TYPE_ASYNC)
AzureIoTClient 62:5a4cdacf5090 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.");
AzureIoTClient 62:5a4cdacf5090 1745 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1746 }
AzureIoTClient 62:5a4cdacf5090 1747 else
AzureIoTClient 62:5a4cdacf5090 1748 {
AzureIoTClient 62:5a4cdacf5090 1749 /*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 1750 /*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 1751 /*Codes_SRS_IOTHUBCLIENT_LL_12_022: [ Otherwise IoTHubClient_LL_SetDeviceMethodCallback shall succeed and return IOTHUB_CLIENT_OK. ]*/
AzureIoTClient 62:5a4cdacf5090 1752 handleData->IoTHubTransport_Unsubscribe_DeviceMethod(handleData->transportHandle);
AzureIoTClient 62:5a4cdacf5090 1753 handleData->methodCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 62:5a4cdacf5090 1754 handleData->methodCallback.callbackSync = NULL;
AzureIoTClient 62:5a4cdacf5090 1755 handleData->methodCallback.userContextCallback = NULL;
AzureIoTClient 62:5a4cdacf5090 1756 result = IOTHUB_CLIENT_OK;
AzureIoTClient 62:5a4cdacf5090 1757 }
AzureIoTClient 53:1e5a1ca1f274 1758 }
AzureIoTClient 53:1e5a1ca1f274 1759 else
AzureIoTClient 53:1e5a1ca1f274 1760 {
AzureIoTClient 62:5a4cdacf5090 1761 if (handleData->methodCallback.type == CALLBACK_TYPE_ASYNC)
AzureIoTClient 53:1e5a1ca1f274 1762 {
AzureIoTClient 62:5a4cdacf5090 1763 /* 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 1764 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetDeviceMethodCallback_Ex function before subscribing with IoTHubClient_LL_SetDeviceMethodCallback.");
AzureIoTClient 62:5a4cdacf5090 1765 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1766 }
AzureIoTClient 53:1e5a1ca1f274 1767 else
AzureIoTClient 53:1e5a1ca1f274 1768 {
AzureIoTClient 62:5a4cdacf5090 1769 /*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 1770 if (handleData->IoTHubTransport_Subscribe_DeviceMethod(handleData->deviceHandle) == 0)
AzureIoTClient 62:5a4cdacf5090 1771 {
AzureIoTClient 62:5a4cdacf5090 1772 /*Codes_SRS_IOTHUBCLIENT_LL_12_022: [ Otherwise IoTHubClient_LL_SetDeviceMethodCallback shall succeed and return IOTHUB_CLIENT_OK. ]*/
AzureIoTClient 62:5a4cdacf5090 1773 handleData->methodCallback.type = CALLBACK_TYPE_SYNC;
AzureIoTClient 62:5a4cdacf5090 1774 handleData->methodCallback.callbackSync = deviceMethodCallback;
AzureIoTClient 62:5a4cdacf5090 1775 handleData->methodCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1776 handleData->methodCallback.userContextCallback = userContextCallback;
AzureIoTClient 62:5a4cdacf5090 1777 result = IOTHUB_CLIENT_OK;
AzureIoTClient 62:5a4cdacf5090 1778 }
AzureIoTClient 62:5a4cdacf5090 1779 else
AzureIoTClient 62:5a4cdacf5090 1780 {
AzureIoTClient 62:5a4cdacf5090 1781 /*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 1782 /*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 1783 LogError("IoTHubTransport_Subscribe_DeviceMethod failed");
AzureIoTClient 62:5a4cdacf5090 1784 handleData->methodCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 62:5a4cdacf5090 1785 handleData->methodCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1786 handleData->methodCallback.callbackSync = NULL;
AzureIoTClient 62:5a4cdacf5090 1787 handleData->methodCallback.userContextCallback = NULL;
AzureIoTClient 62:5a4cdacf5090 1788 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1789 }
AzureIoTClient 53:1e5a1ca1f274 1790 }
AzureIoTClient 53:1e5a1ca1f274 1791 }
AzureIoTClient 53:1e5a1ca1f274 1792 }
AzureIoTClient 53:1e5a1ca1f274 1793 return result;
AzureIoTClient 53:1e5a1ca1f274 1794 }
AzureIoTClient 53:1e5a1ca1f274 1795
AzureIoTClient 55:59b527ab3452 1796 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetDeviceMethodCallback_Ex(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK inboundDeviceMethodCallback, void* userContextCallback)
AzureIoTClient 55:59b527ab3452 1797 {
AzureIoTClient 55:59b527ab3452 1798 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1799 /* Codes_SRS_IOTHUBCLIENT_LL_07_021: [ If handle is NULL then IoTHubClient_LL_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 55:59b527ab3452 1800 if (iotHubClientHandle == NULL)
AzureIoTClient 55:59b527ab3452 1801 {
AzureIoTClient 55:59b527ab3452 1802 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1803 LOG_ERROR_RESULT;
AzureIoTClient 55:59b527ab3452 1804 }
AzureIoTClient 55:59b527ab3452 1805 else
AzureIoTClient 55:59b527ab3452 1806 {
AzureIoTClient 55:59b527ab3452 1807 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1808 if (inboundDeviceMethodCallback == NULL)
AzureIoTClient 55:59b527ab3452 1809 {
AzureIoTClient 62:5a4cdacf5090 1810 if (handleData->methodCallback.type == CALLBACK_TYPE_NONE)
AzureIoTClient 62:5a4cdacf5090 1811 {
AzureIoTClient 62:5a4cdacf5090 1812 /* 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 1813 LogError("not currently set to accept or process incoming messages.");
AzureIoTClient 62:5a4cdacf5090 1814 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1815 }
AzureIoTClient 62:5a4cdacf5090 1816 else if (handleData->methodCallback.type == CALLBACK_TYPE_SYNC)
AzureIoTClient 62:5a4cdacf5090 1817 {
AzureIoTClient 62:5a4cdacf5090 1818 /* 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 1819 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetDeviceMethodCallback function.");
AzureIoTClient 62:5a4cdacf5090 1820 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1821 }
AzureIoTClient 62:5a4cdacf5090 1822 else
AzureIoTClient 62:5a4cdacf5090 1823 {
AzureIoTClient 62:5a4cdacf5090 1824 /* 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 1825 handleData->IoTHubTransport_Unsubscribe_DeviceMethod(handleData->transportHandle);
AzureIoTClient 62:5a4cdacf5090 1826 handleData->methodCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 62:5a4cdacf5090 1827 handleData->methodCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1828 handleData->methodCallback.userContextCallback = NULL;
AzureIoTClient 62:5a4cdacf5090 1829 result = IOTHUB_CLIENT_OK;
AzureIoTClient 62:5a4cdacf5090 1830 }
AzureIoTClient 55:59b527ab3452 1831 }
AzureIoTClient 55:59b527ab3452 1832 else
AzureIoTClient 55:59b527ab3452 1833 {
AzureIoTClient 62:5a4cdacf5090 1834 if (handleData->methodCallback.type == CALLBACK_TYPE_SYNC)
AzureIoTClient 55:59b527ab3452 1835 {
AzureIoTClient 62:5a4cdacf5090 1836 /* 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 1837 LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClient_LL_SetDeviceMethodCallback function before subscribing with IoTHubClient_LL_SetDeviceMethodCallback_Ex.");
AzureIoTClient 62:5a4cdacf5090 1838 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1839 }
AzureIoTClient 55:59b527ab3452 1840 else
AzureIoTClient 55:59b527ab3452 1841 {
AzureIoTClient 62:5a4cdacf5090 1842 /* 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 1843 if (handleData->IoTHubTransport_Subscribe_DeviceMethod(handleData->deviceHandle) == 0)
AzureIoTClient 62:5a4cdacf5090 1844 {
AzureIoTClient 62:5a4cdacf5090 1845 handleData->methodCallback.type = CALLBACK_TYPE_ASYNC;
AzureIoTClient 62:5a4cdacf5090 1846 handleData->methodCallback.callbackAsync = inboundDeviceMethodCallback;
AzureIoTClient 62:5a4cdacf5090 1847 handleData->methodCallback.callbackSync = NULL;
AzureIoTClient 62:5a4cdacf5090 1848 handleData->methodCallback.userContextCallback = userContextCallback;
AzureIoTClient 62:5a4cdacf5090 1849 result = IOTHUB_CLIENT_OK;
AzureIoTClient 62:5a4cdacf5090 1850 }
AzureIoTClient 62:5a4cdacf5090 1851 else
AzureIoTClient 62:5a4cdacf5090 1852 {
AzureIoTClient 62:5a4cdacf5090 1853 /* Codes_SRS_IOTHUBCLIENT_LL_07_025: [ If any error is encountered then IoTHubClient_LL_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 62:5a4cdacf5090 1854 LogError("IoTHubTransport_Subscribe_DeviceMethod failed");
AzureIoTClient 62:5a4cdacf5090 1855 handleData->methodCallback.type = CALLBACK_TYPE_NONE;
AzureIoTClient 62:5a4cdacf5090 1856 handleData->methodCallback.callbackAsync = NULL;
AzureIoTClient 62:5a4cdacf5090 1857 handleData->methodCallback.callbackSync = NULL;
AzureIoTClient 62:5a4cdacf5090 1858 handleData->methodCallback.userContextCallback = NULL;
AzureIoTClient 62:5a4cdacf5090 1859 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1860 }
AzureIoTClient 55:59b527ab3452 1861 }
AzureIoTClient 55:59b527ab3452 1862 }
AzureIoTClient 55:59b527ab3452 1863 }
AzureIoTClient 55:59b527ab3452 1864 return result;
AzureIoTClient 55:59b527ab3452 1865 }
AzureIoTClient 55:59b527ab3452 1866
AzureIoTClient 55:59b527ab3452 1867 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 1868 {
AzureIoTClient 55:59b527ab3452 1869 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1870 /* 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 1871 if (iotHubClientHandle == NULL || methodId == NULL)
AzureIoTClient 55:59b527ab3452 1872 {
AzureIoTClient 55:59b527ab3452 1873 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1874 LOG_ERROR_RESULT;
AzureIoTClient 55:59b527ab3452 1875 }
AzureIoTClient 55:59b527ab3452 1876 else
AzureIoTClient 55:59b527ab3452 1877 {
AzureIoTClient 55:59b527ab3452 1878 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1879 /* Codes_SRS_IOTHUBCLIENT_LL_07_027: [ IoTHubClient_LL_DeviceMethodResponse shall call the IoTHubTransport_DeviceMethod_Response transport function.] */
AzureIoTClient 55:59b527ab3452 1880 if (handleData->IoTHubTransport_DeviceMethod_Response(handleData->deviceHandle, methodId, response, response_size, status_response) != 0)
AzureIoTClient 55:59b527ab3452 1881 {
AzureIoTClient 55:59b527ab3452 1882 LogError("IoTHubTransport_DeviceMethod_Response failed");
AzureIoTClient 55:59b527ab3452 1883 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1884 }
AzureIoTClient 55:59b527ab3452 1885 else
AzureIoTClient 55:59b527ab3452 1886 {
AzureIoTClient 55:59b527ab3452 1887 result = IOTHUB_CLIENT_OK;
AzureIoTClient 55:59b527ab3452 1888 }
AzureIoTClient 55:59b527ab3452 1889 }
AzureIoTClient 55:59b527ab3452 1890 return result;
AzureIoTClient 55:59b527ab3452 1891 }
AzureIoTClient 55:59b527ab3452 1892
AzureIoTClient 44:33dd78697616 1893 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 1894 IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadToBlob(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* destinationFileName, const unsigned char* source, size_t size)
AzureIoTClient 42:448eecc3676e 1895 {
AzureIoTClient 42:448eecc3676e 1896 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1897 /*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 1898 /*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 1899 if (
AzureIoTClient 42:448eecc3676e 1900 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 1901 (destinationFileName == NULL) ||
AzureIoTClient 42:448eecc3676e 1902 ((source == NULL) && (size >0))
AzureIoTClient 42:448eecc3676e 1903 )
AzureIoTClient 42:448eecc3676e 1904 {
AzureIoTClient 42:448eecc3676e 1905 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 1906 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 1907 }
AzureIoTClient 42:448eecc3676e 1908 else
AzureIoTClient 42:448eecc3676e 1909 {
AzureIoTClient 42:448eecc3676e 1910 result = IoTHubClient_LL_UploadToBlob_Impl(iotHubClientHandle->uploadToBlobHandle, destinationFileName, source, size);
AzureIoTClient 42:448eecc3676e 1911 }
AzureIoTClient 42:448eecc3676e 1912 return result;
AzureIoTClient 16:deba40344375 1913 }
AzureIoTClient 43:038d8511e817 1914 #endif