Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

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

Committer:
AzureIoTClient
Date:
Mon Sep 11 09:22:55 2017 -0700
Revision:
75:86205ca63a59
Parent:
73:c4d9077dd0a1
Child:
77:e4e36df9caee
1.1.23

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