Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

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

Committer:
AzureIoTClient
Date:
Fri Dec 15 14:09:20 2017 -0800
Revision:
80:db5f5237bc95
Parent:
78:74a8d3068204
Child:
82:f94e6bed4495
1.1.29

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 54:6dcad9019a64 5 #include "azure_c_shared_utility/umock_c_prod.h"
Azure.IoT Build 38:a05929a75111 6 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 16:deba40344375 7
AzureIoTClient 16:deba40344375 8 #include <signal.h>
Azure.IoT Build 35:ceed20da4ba6 9 #include <stddef.h>
AzureIoTClient 60:41648c4e7036 10 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 38:a05929a75111 11 #include "azure_c_shared_utility/crt_abstractions.h"
AzureIoTClient 16:deba40344375 12 #include "iothub_client.h"
AzureIoTClient 16:deba40344375 13 #include "iothub_client_ll.h"
AzureIoTClient 61:8b85a4e797cf 14 #include "iothub_client_private.h"
Azure.IoT Build 37:18310e4d888d 15 #include "iothubtransport.h"
Azure.IoT Build 38:a05929a75111 16 #include "azure_c_shared_utility/threadapi.h"
Azure.IoT Build 38:a05929a75111 17 #include "azure_c_shared_utility/lock.h"
Azure.IoT Build 45:54c11b1b1407 18 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 52:1cc3c6d07cad 19 #include "azure_c_shared_utility/singlylinkedlist.h"
Azure.IoT.Build 54:6dcad9019a64 20 #include "azure_c_shared_utility/vector.h"
AzureIoTClient 78:74a8d3068204 21 #ifdef USE_PROV_MODULE
AzureIoTClient 78:74a8d3068204 22 #include "iothub_client_hsm_ll.h"
AzureIoTClient 78:74a8d3068204 23 #endif
AzureIoTClient 16:deba40344375 24
AzureIoTClient 55:59b527ab3452 25 struct IOTHUB_QUEUE_CONTEXT_TAG;
AzureIoTClient 55:59b527ab3452 26
AzureIoTClient 16:deba40344375 27 typedef struct IOTHUB_CLIENT_INSTANCE_TAG
AzureIoTClient 16:deba40344375 28 {
AzureIoTClient 16:deba40344375 29 IOTHUB_CLIENT_LL_HANDLE IoTHubClientLLHandle;
AzureIoTClient 42:448eecc3676e 30 TRANSPORT_HANDLE TransportHandle;
AzureIoTClient 16:deba40344375 31 THREAD_HANDLE ThreadHandle;
AzureIoTClient 16:deba40344375 32 LOCK_HANDLE LockHandle;
AzureIoTClient 16:deba40344375 33 sig_atomic_t StopThread;
AzureIoTClient 44:33dd78697616 34 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 52:1cc3c6d07cad 35 SINGLYLINKEDLIST_HANDLE savedDataToBeCleaned; /*list containing UPLOADTOBLOB_SAVED_DATA*/
AzureIoTClient 43:038d8511e817 36 #endif
Azure.IoT.Build 54:6dcad9019a64 37 int created_with_transport_handle;
Azure.IoT.Build 54:6dcad9019a64 38 VECTOR_HANDLE saved_user_callback_list;
Azure.IoT.Build 54:6dcad9019a64 39 IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK desired_state_callback;
Azure.IoT.Build 54:6dcad9019a64 40 IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK event_confirm_callback;
Azure.IoT.Build 54:6dcad9019a64 41 IOTHUB_CLIENT_REPORTED_STATE_CALLBACK reported_state_callback;
Azure.IoT.Build 54:6dcad9019a64 42 IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK connection_status_callback;
AzureIoTClient 62:5a4cdacf5090 43 IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC device_method_callback;
AzureIoTClient 62:5a4cdacf5090 44 IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK inbound_device_method_callback;
AzureIoTClient 61:8b85a4e797cf 45 IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC message_callback;
AzureIoTClient 55:59b527ab3452 46 struct IOTHUB_QUEUE_CONTEXT_TAG* devicetwin_user_context;
AzureIoTClient 55:59b527ab3452 47 struct IOTHUB_QUEUE_CONTEXT_TAG* connection_status_user_context;
AzureIoTClient 61:8b85a4e797cf 48 struct IOTHUB_QUEUE_CONTEXT_TAG* message_user_context;
AzureIoTClient 62:5a4cdacf5090 49 struct IOTHUB_QUEUE_CONTEXT_TAG* method_user_context;
AzureIoTClient 16:deba40344375 50 } IOTHUB_CLIENT_INSTANCE;
AzureIoTClient 16:deba40344375 51
AzureIoTClient 44:33dd78697616 52 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 53 typedef struct UPLOADTOBLOB_SAVED_DATA_TAG
AzureIoTClient 42:448eecc3676e 54 {
AzureIoTClient 42:448eecc3676e 55 unsigned char* source;
AzureIoTClient 42:448eecc3676e 56 size_t size;
AzureIoTClient 42:448eecc3676e 57 char* destinationFileName;
AzureIoTClient 42:448eecc3676e 58 IOTHUB_CLIENT_FILE_UPLOAD_CALLBACK iotHubClientFileUploadCallback;
AzureIoTClient 42:448eecc3676e 59 void* context;
AzureIoTClient 42:448eecc3676e 60 THREAD_HANDLE uploadingThreadHandle;
AzureIoTClient 42:448eecc3676e 61 IOTHUB_CLIENT_HANDLE iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 62 LOCK_HANDLE lockGarbage;
AzureIoTClient 42:448eecc3676e 63 int canBeGarbageCollected; /*flag indicating that the UPLOADTOBLOB_SAVED_DATA structure can be freed because the thread deadling with it finished*/
AzureIoTClient 42:448eecc3676e 64 }UPLOADTOBLOB_SAVED_DATA;
AzureIoTClient 80:db5f5237bc95 65
AzureIoTClient 80:db5f5237bc95 66 typedef struct UPLOADTOBLOB_MULTIBLOCK_SAVED_DATA_TAG
AzureIoTClient 80:db5f5237bc95 67 {
AzureIoTClient 80:db5f5237bc95 68 char* destinationFileName;
AzureIoTClient 80:db5f5237bc95 69 IOTHUB_CLIENT_FILE_UPLOAD_GET_DATA_CALLBACK getDataCallback;
AzureIoTClient 80:db5f5237bc95 70 void* context;
AzureIoTClient 80:db5f5237bc95 71 THREAD_HANDLE uploadingThreadHandle;
AzureIoTClient 80:db5f5237bc95 72 IOTHUB_CLIENT_HANDLE iotHubClientHandle;
AzureIoTClient 80:db5f5237bc95 73 }UPLOADTOBLOB_MULTIBLOCK_SAVED_DATA;
AzureIoTClient 43:038d8511e817 74 #endif
AzureIoTClient 42:448eecc3676e 75
AzureIoTClient 62:5a4cdacf5090 76 #define USER_CALLBACK_TYPE_VALUES \
AzureIoTClient 62:5a4cdacf5090 77 CALLBACK_TYPE_DEVICE_TWIN, \
AzureIoTClient 62:5a4cdacf5090 78 CALLBACK_TYPE_EVENT_CONFIRM, \
AzureIoTClient 62:5a4cdacf5090 79 CALLBACK_TYPE_REPORTED_STATE, \
AzureIoTClient 62:5a4cdacf5090 80 CALLBACK_TYPE_CONNECTION_STATUS, \
AzureIoTClient 62:5a4cdacf5090 81 CALLBACK_TYPE_DEVICE_METHOD, \
AzureIoTClient 62:5a4cdacf5090 82 CALLBACK_TYPE_INBOUD_DEVICE_METHOD, \
AzureIoTClient 61:8b85a4e797cf 83 CALLBACK_TYPE_MESSAGE
Azure.IoT.Build 54:6dcad9019a64 84
Azure.IoT.Build 54:6dcad9019a64 85 DEFINE_ENUM(USER_CALLBACK_TYPE, USER_CALLBACK_TYPE_VALUES)
AzureIoTClient 56:fdda9c1244e4 86 DEFINE_ENUM_STRINGS(USER_CALLBACK_TYPE, USER_CALLBACK_TYPE_VALUES)
Azure.IoT.Build 54:6dcad9019a64 87
Azure.IoT.Build 54:6dcad9019a64 88 typedef struct DEVICE_TWIN_CALLBACK_INFO_TAG
Azure.IoT.Build 54:6dcad9019a64 89 {
Azure.IoT.Build 54:6dcad9019a64 90 DEVICE_TWIN_UPDATE_STATE update_state;
Azure.IoT.Build 54:6dcad9019a64 91 unsigned char* payLoad;
Azure.IoT.Build 54:6dcad9019a64 92 size_t size;
Azure.IoT.Build 54:6dcad9019a64 93 } DEVICE_TWIN_CALLBACK_INFO;
Azure.IoT.Build 54:6dcad9019a64 94
Azure.IoT.Build 54:6dcad9019a64 95 typedef struct EVENT_CONFIRM_CALLBACK_INFO_TAG
Azure.IoT.Build 54:6dcad9019a64 96 {
Azure.IoT.Build 54:6dcad9019a64 97 IOTHUB_CLIENT_CONFIRMATION_RESULT confirm_result;
Azure.IoT.Build 54:6dcad9019a64 98 } EVENT_CONFIRM_CALLBACK_INFO;
Azure.IoT.Build 54:6dcad9019a64 99
Azure.IoT.Build 54:6dcad9019a64 100 typedef struct REPORTED_STATE_CALLBACK_INFO_TAG
Azure.IoT.Build 54:6dcad9019a64 101 {
Azure.IoT.Build 54:6dcad9019a64 102 int status_code;
Azure.IoT.Build 54:6dcad9019a64 103 } REPORTED_STATE_CALLBACK_INFO;
Azure.IoT.Build 54:6dcad9019a64 104
Azure.IoT.Build 54:6dcad9019a64 105 typedef struct CONNECTION_STATUS_CALLBACK_INFO_TAG
Azure.IoT.Build 54:6dcad9019a64 106 {
Azure.IoT.Build 54:6dcad9019a64 107 IOTHUB_CLIENT_CONNECTION_STATUS connection_status;
Azure.IoT.Build 54:6dcad9019a64 108 IOTHUB_CLIENT_CONNECTION_STATUS_REASON status_reason;
Azure.IoT.Build 54:6dcad9019a64 109 } CONNECTION_STATUS_CALLBACK_INFO;
Azure.IoT.Build 54:6dcad9019a64 110
Azure.IoT.Build 54:6dcad9019a64 111 typedef struct METHOD_CALLBACK_INFO_TAG
Azure.IoT.Build 54:6dcad9019a64 112 {
AzureIoTClient 55:59b527ab3452 113 STRING_HANDLE method_name;
AzureIoTClient 55:59b527ab3452 114 BUFFER_HANDLE payload;
AzureIoTClient 55:59b527ab3452 115 METHOD_HANDLE method_id;
Azure.IoT.Build 54:6dcad9019a64 116 } METHOD_CALLBACK_INFO;
Azure.IoT.Build 54:6dcad9019a64 117
Azure.IoT.Build 54:6dcad9019a64 118 typedef struct USER_CALLBACK_INFO_TAG
Azure.IoT.Build 54:6dcad9019a64 119 {
Azure.IoT.Build 54:6dcad9019a64 120 USER_CALLBACK_TYPE type;
Azure.IoT.Build 54:6dcad9019a64 121 void* userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 122 union IOTHUB_CALLBACK
Azure.IoT.Build 54:6dcad9019a64 123 {
Azure.IoT.Build 54:6dcad9019a64 124 DEVICE_TWIN_CALLBACK_INFO dev_twin_cb_info;
Azure.IoT.Build 54:6dcad9019a64 125 EVENT_CONFIRM_CALLBACK_INFO event_confirm_cb_info;
Azure.IoT.Build 54:6dcad9019a64 126 REPORTED_STATE_CALLBACK_INFO reported_state_cb_info;
Azure.IoT.Build 54:6dcad9019a64 127 CONNECTION_STATUS_CALLBACK_INFO connection_status_cb_info;
AzureIoTClient 55:59b527ab3452 128 METHOD_CALLBACK_INFO method_cb_info;
AzureIoTClient 61:8b85a4e797cf 129 MESSAGE_CALLBACK_INFO* message_cb_info;
Azure.IoT.Build 54:6dcad9019a64 130 } iothub_callback;
Azure.IoT.Build 54:6dcad9019a64 131 } USER_CALLBACK_INFO;
Azure.IoT.Build 54:6dcad9019a64 132
Azure.IoT.Build 54:6dcad9019a64 133 typedef struct IOTHUB_QUEUE_CONTEXT_TAG
Azure.IoT.Build 54:6dcad9019a64 134 {
Azure.IoT.Build 54:6dcad9019a64 135 IOTHUB_CLIENT_INSTANCE* iotHubClientHandle;
Azure.IoT.Build 54:6dcad9019a64 136 void* userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 137 } IOTHUB_QUEUE_CONTEXT;
Azure.IoT.Build 54:6dcad9019a64 138
Azure.IoT Build 35:ceed20da4ba6 139 /*used by unittests only*/
Azure.IoT Build 35:ceed20da4ba6 140 const size_t IoTHubClient_ThreadTerminationOffset = offsetof(IOTHUB_CLIENT_INSTANCE, StopThread);
Azure.IoT Build 35:ceed20da4ba6 141
AzureIoTClient 44:33dd78697616 142 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 143 /*this function is called from _Destroy and from ScheduleWork_Thread to join finished blobUpload threads and free that memory*/
AzureIoTClient 42:448eecc3676e 144 static void garbageCollectorImpl(IOTHUB_CLIENT_INSTANCE* iotHubClientInstance)
AzureIoTClient 42:448eecc3676e 145 {
AzureIoTClient 42:448eecc3676e 146 /*see if any savedData structures can be disposed of*/
AzureIoTClient 42:448eecc3676e 147 /*Codes_SRS_IOTHUBCLIENT_02_072: [ All threads marked as disposable (upon completion of a file upload) shall be joined and the data structures build for them shall be freed. ]*/
AzureIoTClient 52:1cc3c6d07cad 148 LIST_ITEM_HANDLE item = singlylinkedlist_get_head_item(iotHubClientInstance->savedDataToBeCleaned);
AzureIoTClient 42:448eecc3676e 149 while (item != NULL)
AzureIoTClient 42:448eecc3676e 150 {
AzureIoTClient 52:1cc3c6d07cad 151 const UPLOADTOBLOB_SAVED_DATA* savedData = (const UPLOADTOBLOB_SAVED_DATA*)singlylinkedlist_item_get_value(item);
AzureIoTClient 42:448eecc3676e 152 LIST_ITEM_HANDLE old_item = item;
AzureIoTClient 52:1cc3c6d07cad 153 item = singlylinkedlist_get_next_item(item);
AzureIoTClient 42:448eecc3676e 154
AzureIoTClient 42:448eecc3676e 155 if (Lock(savedData->lockGarbage) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 156 {
AzureIoTClient 43:038d8511e817 157 LogError("unable to Lock");
AzureIoTClient 42:448eecc3676e 158 }
AzureIoTClient 42:448eecc3676e 159 else
AzureIoTClient 42:448eecc3676e 160 {
AzureIoTClient 42:448eecc3676e 161 if (savedData->canBeGarbageCollected == 1)
AzureIoTClient 42:448eecc3676e 162 {
AzureIoTClient 42:448eecc3676e 163 int notUsed;
AzureIoTClient 42:448eecc3676e 164 if (ThreadAPI_Join(savedData->uploadingThreadHandle, &notUsed) != THREADAPI_OK)
AzureIoTClient 42:448eecc3676e 165 {
AzureIoTClient 42:448eecc3676e 166 LogError("unable to ThreadAPI_Join");
AzureIoTClient 42:448eecc3676e 167 }
AzureIoTClient 52:1cc3c6d07cad 168 (void)singlylinkedlist_remove(iotHubClientInstance->savedDataToBeCleaned, old_item);
AzureIoTClient 42:448eecc3676e 169 free((void*)savedData->source);
AzureIoTClient 42:448eecc3676e 170 free((void*)savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 171
AzureIoTClient 42:448eecc3676e 172 if (Unlock(savedData->lockGarbage) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 173 {
AzureIoTClient 42:448eecc3676e 174 LogError("unable to unlock after locking");
AzureIoTClient 42:448eecc3676e 175 }
AzureIoTClient 42:448eecc3676e 176 (void)Lock_Deinit(savedData->lockGarbage);
AzureIoTClient 42:448eecc3676e 177 free((void*)savedData);
AzureIoTClient 42:448eecc3676e 178 }
AzureIoTClient 42:448eecc3676e 179 else
AzureIoTClient 42:448eecc3676e 180 {
AzureIoTClient 42:448eecc3676e 181 if (Unlock(savedData->lockGarbage) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 182 {
AzureIoTClient 42:448eecc3676e 183 LogError("unable to unlock after locking");
AzureIoTClient 42:448eecc3676e 184 }
AzureIoTClient 42:448eecc3676e 185 }
AzureIoTClient 42:448eecc3676e 186 }
AzureIoTClient 42:448eecc3676e 187 }
AzureIoTClient 42:448eecc3676e 188 }
AzureIoTClient 43:038d8511e817 189 #endif
AzureIoTClient 42:448eecc3676e 190
AzureIoTClient 61:8b85a4e797cf 191 static bool iothub_ll_message_callback(MESSAGE_CALLBACK_INFO* messageData, void* userContextCallback)
Azure.IoT.Build 54:6dcad9019a64 192 {
AzureIoTClient 61:8b85a4e797cf 193 bool result;
AzureIoTClient 61:8b85a4e797cf 194 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)userContextCallback;
AzureIoTClient 61:8b85a4e797cf 195 if (queue_context == NULL)
AzureIoTClient 61:8b85a4e797cf 196 {
AzureIoTClient 61:8b85a4e797cf 197 LogError("invalid parameter userContextCallback(NULL)");
AzureIoTClient 61:8b85a4e797cf 198 result = false;
AzureIoTClient 61:8b85a4e797cf 199 }
AzureIoTClient 61:8b85a4e797cf 200 else
AzureIoTClient 61:8b85a4e797cf 201 {
AzureIoTClient 61:8b85a4e797cf 202 USER_CALLBACK_INFO queue_cb_info;
AzureIoTClient 61:8b85a4e797cf 203 queue_cb_info.type = CALLBACK_TYPE_MESSAGE;
AzureIoTClient 61:8b85a4e797cf 204 queue_cb_info.userContextCallback = queue_context->userContextCallback;
AzureIoTClient 61:8b85a4e797cf 205 queue_cb_info.iothub_callback.message_cb_info = messageData;
AzureIoTClient 61:8b85a4e797cf 206 if (VECTOR_push_back(queue_context->iotHubClientHandle->saved_user_callback_list, &queue_cb_info, 1) == 0)
AzureIoTClient 61:8b85a4e797cf 207 {
AzureIoTClient 61:8b85a4e797cf 208 result = true;
AzureIoTClient 61:8b85a4e797cf 209 }
AzureIoTClient 61:8b85a4e797cf 210 else
AzureIoTClient 61:8b85a4e797cf 211 {
AzureIoTClient 61:8b85a4e797cf 212 LogError("message callback vector push failed.");
AzureIoTClient 61:8b85a4e797cf 213 result = false;
AzureIoTClient 61:8b85a4e797cf 214 }
AzureIoTClient 61:8b85a4e797cf 215 }
AzureIoTClient 61:8b85a4e797cf 216 return result;
Azure.IoT.Build 54:6dcad9019a64 217 }
Azure.IoT.Build 54:6dcad9019a64 218
AzureIoTClient 62:5a4cdacf5090 219 static int make_method_calback_queue_context(USER_CALLBACK_INFO* queue_cb_info, const char* method_name, const unsigned char* payload, size_t size, METHOD_HANDLE method_id, IOTHUB_QUEUE_CONTEXT* queue_context)
Azure.IoT.Build 54:6dcad9019a64 220 {
AzureIoTClient 55:59b527ab3452 221 int result;
AzureIoTClient 62:5a4cdacf5090 222 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_002: [ IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK shall copy the method_name and payload. ] */
AzureIoTClient 62:5a4cdacf5090 223 queue_cb_info->userContextCallback = queue_context->userContextCallback;
AzureIoTClient 62:5a4cdacf5090 224 queue_cb_info->iothub_callback.method_cb_info.method_id = method_id;
AzureIoTClient 62:5a4cdacf5090 225 if ((queue_cb_info->iothub_callback.method_cb_info.method_name = STRING_construct(method_name)) == NULL)
AzureIoTClient 55:59b527ab3452 226 {
AzureIoTClient 62:5a4cdacf5090 227 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [ If a failure is encountered IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK shall return a non-NULL value. ]*/
AzureIoTClient 62:5a4cdacf5090 228 LogError("STRING_construct failed");
AzureIoTClient 62:5a4cdacf5090 229 result = __FAILURE__;
AzureIoTClient 62:5a4cdacf5090 230 }
AzureIoTClient 62:5a4cdacf5090 231 else
AzureIoTClient 62:5a4cdacf5090 232 {
AzureIoTClient 62:5a4cdacf5090 233 if ((queue_cb_info->iothub_callback.method_cb_info.payload = BUFFER_create(payload, size)) == NULL)
AzureIoTClient 55:59b527ab3452 234 {
AzureIoTClient 62:5a4cdacf5090 235 STRING_delete(queue_cb_info->iothub_callback.method_cb_info.method_name);
AzureIoTClient 55:59b527ab3452 236 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [ If a failure is encountered IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK shall return a non-NULL value. ]*/
AzureIoTClient 62:5a4cdacf5090 237 LogError("BUFFER_create failed");
AzureIoTClient 60:41648c4e7036 238 result = __FAILURE__;
AzureIoTClient 55:59b527ab3452 239 }
AzureIoTClient 55:59b527ab3452 240 else
AzureIoTClient 55:59b527ab3452 241 {
AzureIoTClient 62:5a4cdacf5090 242 if (VECTOR_push_back(queue_context->iotHubClientHandle->saved_user_callback_list, queue_cb_info, 1) == 0)
AzureIoTClient 62:5a4cdacf5090 243 {
AzureIoTClient 62:5a4cdacf5090 244 result = 0;
AzureIoTClient 62:5a4cdacf5090 245 }
AzureIoTClient 62:5a4cdacf5090 246 else
AzureIoTClient 62:5a4cdacf5090 247 {
AzureIoTClient 62:5a4cdacf5090 248 STRING_delete(queue_cb_info->iothub_callback.method_cb_info.method_name);
AzureIoTClient 62:5a4cdacf5090 249 BUFFER_delete(queue_cb_info->iothub_callback.method_cb_info.payload);
AzureIoTClient 62:5a4cdacf5090 250 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [ If a failure is encountered IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK shall return a non-NULL value. ]*/
AzureIoTClient 62:5a4cdacf5090 251 LogError("VECTOR_push_back failed");
AzureIoTClient 62:5a4cdacf5090 252 result = __FAILURE__;
AzureIoTClient 62:5a4cdacf5090 253 }
AzureIoTClient 55:59b527ab3452 254 }
AzureIoTClient 62:5a4cdacf5090 255 }
AzureIoTClient 62:5a4cdacf5090 256 return result;
AzureIoTClient 62:5a4cdacf5090 257 }
AzureIoTClient 62:5a4cdacf5090 258
AzureIoTClient 62:5a4cdacf5090 259 static int iothub_ll_device_method_callback(const char* method_name, const unsigned char* payload, size_t size, METHOD_HANDLE method_id, void* userContextCallback)
AzureIoTClient 62:5a4cdacf5090 260 {
AzureIoTClient 62:5a4cdacf5090 261 int result;
AzureIoTClient 62:5a4cdacf5090 262 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_001: [ if userContextCallback is NULL, IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK shall return a nonNULL value. ] */
AzureIoTClient 62:5a4cdacf5090 263 if (userContextCallback == NULL)
AzureIoTClient 62:5a4cdacf5090 264 {
AzureIoTClient 62:5a4cdacf5090 265 LogError("invalid parameter userContextCallback(NULL)");
AzureIoTClient 62:5a4cdacf5090 266 result = __FAILURE__;
AzureIoTClient 55:59b527ab3452 267 }
AzureIoTClient 55:59b527ab3452 268 else
AzureIoTClient 55:59b527ab3452 269 {
AzureIoTClient 62:5a4cdacf5090 270 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)userContextCallback;
AzureIoTClient 62:5a4cdacf5090 271
AzureIoTClient 62:5a4cdacf5090 272 USER_CALLBACK_INFO queue_cb_info;
AzureIoTClient 62:5a4cdacf5090 273 queue_cb_info.type = CALLBACK_TYPE_DEVICE_METHOD;
AzureIoTClient 62:5a4cdacf5090 274
AzureIoTClient 62:5a4cdacf5090 275 result = make_method_calback_queue_context(&queue_cb_info, method_name, payload, size, method_id, queue_context);
AzureIoTClient 62:5a4cdacf5090 276 if (result != 0)
AzureIoTClient 62:5a4cdacf5090 277 {
AzureIoTClient 62:5a4cdacf5090 278 LogError("construction of method calback queue context failed");
AzureIoTClient 62:5a4cdacf5090 279 result = __FAILURE__;
AzureIoTClient 62:5a4cdacf5090 280 }
AzureIoTClient 62:5a4cdacf5090 281 }
AzureIoTClient 62:5a4cdacf5090 282 return result;
AzureIoTClient 62:5a4cdacf5090 283 }
AzureIoTClient 62:5a4cdacf5090 284
AzureIoTClient 62:5a4cdacf5090 285 static int iothub_ll_inbound_device_method_callback(const char* method_name, const unsigned char* payload, size_t size, METHOD_HANDLE method_id, void* userContextCallback)
AzureIoTClient 62:5a4cdacf5090 286 {
AzureIoTClient 62:5a4cdacf5090 287 int result;
AzureIoTClient 62:5a4cdacf5090 288 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_001: [ if userContextCallback is NULL, IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK shall return a nonNULL value. ] */
AzureIoTClient 62:5a4cdacf5090 289 if (userContextCallback == NULL)
AzureIoTClient 62:5a4cdacf5090 290 {
AzureIoTClient 62:5a4cdacf5090 291 LogError("invalid parameter userContextCallback(NULL)");
AzureIoTClient 60:41648c4e7036 292 result = __FAILURE__;
AzureIoTClient 55:59b527ab3452 293 }
AzureIoTClient 62:5a4cdacf5090 294 else
AzureIoTClient 62:5a4cdacf5090 295 {
AzureIoTClient 62:5a4cdacf5090 296 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)userContextCallback;
AzureIoTClient 62:5a4cdacf5090 297
AzureIoTClient 62:5a4cdacf5090 298 USER_CALLBACK_INFO queue_cb_info;
AzureIoTClient 62:5a4cdacf5090 299 queue_cb_info.type = CALLBACK_TYPE_INBOUD_DEVICE_METHOD;
AzureIoTClient 62:5a4cdacf5090 300
AzureIoTClient 62:5a4cdacf5090 301 result = make_method_calback_queue_context(&queue_cb_info, method_name, payload, size, method_id, queue_context);
AzureIoTClient 62:5a4cdacf5090 302 if (result != 0)
AzureIoTClient 62:5a4cdacf5090 303 {
AzureIoTClient 62:5a4cdacf5090 304 LogError("construction of method calback queue context failed");
AzureIoTClient 62:5a4cdacf5090 305 result = __FAILURE__;
AzureIoTClient 62:5a4cdacf5090 306 }
AzureIoTClient 62:5a4cdacf5090 307 }
AzureIoTClient 55:59b527ab3452 308 return result;
Azure.IoT.Build 54:6dcad9019a64 309 }
Azure.IoT.Build 54:6dcad9019a64 310
Azure.IoT.Build 54:6dcad9019a64 311 static void iothub_ll_connection_status_callback(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void* userContextCallback)
Azure.IoT.Build 54:6dcad9019a64 312 {
Azure.IoT.Build 54:6dcad9019a64 313 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 314 if (queue_context != NULL)
Azure.IoT.Build 54:6dcad9019a64 315 {
Azure.IoT.Build 54:6dcad9019a64 316 USER_CALLBACK_INFO queue_cb_info;
Azure.IoT.Build 54:6dcad9019a64 317 queue_cb_info.type = CALLBACK_TYPE_CONNECTION_STATUS;
Azure.IoT.Build 54:6dcad9019a64 318 queue_cb_info.userContextCallback = queue_context->userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 319 queue_cb_info.iothub_callback.connection_status_cb_info.status_reason = reason;
Azure.IoT.Build 54:6dcad9019a64 320 queue_cb_info.iothub_callback.connection_status_cb_info.connection_status = result;
Azure.IoT.Build 54:6dcad9019a64 321 if (VECTOR_push_back(queue_context->iotHubClientHandle->saved_user_callback_list, &queue_cb_info, 1) != 0)
Azure.IoT.Build 54:6dcad9019a64 322 {
Azure.IoT.Build 54:6dcad9019a64 323 LogError("connection status callback vector push failed.");
Azure.IoT.Build 54:6dcad9019a64 324 }
Azure.IoT.Build 54:6dcad9019a64 325 }
Azure.IoT.Build 54:6dcad9019a64 326 }
Azure.IoT.Build 54:6dcad9019a64 327
Azure.IoT.Build 54:6dcad9019a64 328 static void iothub_ll_event_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
Azure.IoT.Build 54:6dcad9019a64 329 {
Azure.IoT.Build 54:6dcad9019a64 330 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 331 if (queue_context != NULL)
Azure.IoT.Build 54:6dcad9019a64 332 {
Azure.IoT.Build 54:6dcad9019a64 333 USER_CALLBACK_INFO queue_cb_info;
Azure.IoT.Build 54:6dcad9019a64 334 queue_cb_info.type = CALLBACK_TYPE_EVENT_CONFIRM;
Azure.IoT.Build 54:6dcad9019a64 335 queue_cb_info.userContextCallback = queue_context->userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 336 queue_cb_info.iothub_callback.event_confirm_cb_info.confirm_result = result;
Azure.IoT.Build 54:6dcad9019a64 337 if (VECTOR_push_back(queue_context->iotHubClientHandle->saved_user_callback_list, &queue_cb_info, 1) != 0)
Azure.IoT.Build 54:6dcad9019a64 338 {
Azure.IoT.Build 54:6dcad9019a64 339 LogError("event confirm callback vector push failed.");
Azure.IoT.Build 54:6dcad9019a64 340 }
Azure.IoT.Build 54:6dcad9019a64 341 free(queue_context);
Azure.IoT.Build 54:6dcad9019a64 342 }
Azure.IoT.Build 54:6dcad9019a64 343 }
Azure.IoT.Build 54:6dcad9019a64 344
Azure.IoT.Build 54:6dcad9019a64 345 static void iothub_ll_reported_state_callback(int status_code, void* userContextCallback)
Azure.IoT.Build 54:6dcad9019a64 346 {
Azure.IoT.Build 54:6dcad9019a64 347 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 348 if (queue_context != NULL)
Azure.IoT.Build 54:6dcad9019a64 349 {
Azure.IoT.Build 54:6dcad9019a64 350 USER_CALLBACK_INFO queue_cb_info;
Azure.IoT.Build 54:6dcad9019a64 351 queue_cb_info.type = CALLBACK_TYPE_REPORTED_STATE;
Azure.IoT.Build 54:6dcad9019a64 352 queue_cb_info.userContextCallback = queue_context->userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 353 queue_cb_info.iothub_callback.reported_state_cb_info.status_code = status_code;
Azure.IoT.Build 54:6dcad9019a64 354 if (VECTOR_push_back(queue_context->iotHubClientHandle->saved_user_callback_list, &queue_cb_info, 1) != 0)
Azure.IoT.Build 54:6dcad9019a64 355 {
Azure.IoT.Build 54:6dcad9019a64 356 LogError("reported state callback vector push failed.");
Azure.IoT.Build 54:6dcad9019a64 357 }
Azure.IoT.Build 54:6dcad9019a64 358 free(queue_context);
Azure.IoT.Build 54:6dcad9019a64 359 }
Azure.IoT.Build 54:6dcad9019a64 360 }
Azure.IoT.Build 54:6dcad9019a64 361
Azure.IoT.Build 54:6dcad9019a64 362 static void iothub_ll_device_twin_callback(DEVICE_TWIN_UPDATE_STATE update_state, const unsigned char* payLoad, size_t size, void* userContextCallback)
Azure.IoT.Build 54:6dcad9019a64 363 {
Azure.IoT.Build 54:6dcad9019a64 364 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 365 if (queue_context != NULL)
Azure.IoT.Build 54:6dcad9019a64 366 {
AzureIoTClient 55:59b527ab3452 367 int push_to_vector;
AzureIoTClient 55:59b527ab3452 368
Azure.IoT.Build 54:6dcad9019a64 369 USER_CALLBACK_INFO queue_cb_info;
Azure.IoT.Build 54:6dcad9019a64 370 queue_cb_info.type = CALLBACK_TYPE_DEVICE_TWIN;
Azure.IoT.Build 54:6dcad9019a64 371 queue_cb_info.userContextCallback = queue_context->userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 372 queue_cb_info.iothub_callback.dev_twin_cb_info.update_state = update_state;
Azure.IoT.Build 54:6dcad9019a64 373 if (payLoad == NULL)
Azure.IoT.Build 54:6dcad9019a64 374 {
Azure.IoT.Build 54:6dcad9019a64 375 queue_cb_info.iothub_callback.dev_twin_cb_info.payLoad = NULL;
Azure.IoT.Build 54:6dcad9019a64 376 queue_cb_info.iothub_callback.dev_twin_cb_info.size = 0;
AzureIoTClient 55:59b527ab3452 377 push_to_vector = 0;
Azure.IoT.Build 54:6dcad9019a64 378 }
Azure.IoT.Build 54:6dcad9019a64 379 else
Azure.IoT.Build 54:6dcad9019a64 380 {
Azure.IoT.Build 54:6dcad9019a64 381 queue_cb_info.iothub_callback.dev_twin_cb_info.payLoad = (unsigned char*)malloc(size);
Azure.IoT.Build 54:6dcad9019a64 382 if (queue_cb_info.iothub_callback.dev_twin_cb_info.payLoad == NULL)
Azure.IoT.Build 54:6dcad9019a64 383 {
Azure.IoT.Build 54:6dcad9019a64 384 LogError("failure allocating payload in device twin callback.");
Azure.IoT.Build 54:6dcad9019a64 385 queue_cb_info.iothub_callback.dev_twin_cb_info.size = 0;
AzureIoTClient 60:41648c4e7036 386 push_to_vector = __FAILURE__;
Azure.IoT.Build 54:6dcad9019a64 387 }
Azure.IoT.Build 54:6dcad9019a64 388 else
Azure.IoT.Build 54:6dcad9019a64 389 {
AzureIoTClient 55:59b527ab3452 390 (void)memcpy(queue_cb_info.iothub_callback.dev_twin_cb_info.payLoad, payLoad, size);
Azure.IoT.Build 54:6dcad9019a64 391 queue_cb_info.iothub_callback.dev_twin_cb_info.size = size;
AzureIoTClient 55:59b527ab3452 392 push_to_vector = 0;
Azure.IoT.Build 54:6dcad9019a64 393 }
Azure.IoT.Build 54:6dcad9019a64 394 }
AzureIoTClient 55:59b527ab3452 395 if (push_to_vector == 0)
Azure.IoT.Build 54:6dcad9019a64 396 {
AzureIoTClient 55:59b527ab3452 397 if (VECTOR_push_back(queue_context->iotHubClientHandle->saved_user_callback_list, &queue_cb_info, 1) != 0)
AzureIoTClient 55:59b527ab3452 398 {
AzureIoTClient 55:59b527ab3452 399 if (queue_cb_info.iothub_callback.dev_twin_cb_info.payLoad != NULL)
AzureIoTClient 55:59b527ab3452 400 {
AzureIoTClient 55:59b527ab3452 401 free(queue_cb_info.iothub_callback.dev_twin_cb_info.payLoad);
AzureIoTClient 55:59b527ab3452 402 }
AzureIoTClient 55:59b527ab3452 403 LogError("device twin callback userContextCallback vector push failed.");
AzureIoTClient 55:59b527ab3452 404 }
Azure.IoT.Build 54:6dcad9019a64 405 }
Azure.IoT.Build 54:6dcad9019a64 406 }
Azure.IoT.Build 54:6dcad9019a64 407 else
Azure.IoT.Build 54:6dcad9019a64 408 {
Azure.IoT.Build 54:6dcad9019a64 409 LogError("device twin callback userContextCallback NULL");
Azure.IoT.Build 54:6dcad9019a64 410 }
Azure.IoT.Build 54:6dcad9019a64 411 }
Azure.IoT.Build 54:6dcad9019a64 412
AzureIoTClient 61:8b85a4e797cf 413 static void dispatch_user_callbacks(IOTHUB_CLIENT_INSTANCE* iotHubClientInstance, VECTOR_HANDLE call_backs)
Azure.IoT.Build 54:6dcad9019a64 414 {
AzureIoTClient 61:8b85a4e797cf 415 size_t callbacks_length = VECTOR_size(call_backs);
AzureIoTClient 61:8b85a4e797cf 416 size_t index;
AzureIoTClient 80:db5f5237bc95 417
AzureIoTClient 80:db5f5237bc95 418 IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK desired_state_callback = NULL;
AzureIoTClient 80:db5f5237bc95 419 IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK event_confirm_callback = NULL;
AzureIoTClient 80:db5f5237bc95 420 IOTHUB_CLIENT_REPORTED_STATE_CALLBACK reported_state_callback = NULL;
AzureIoTClient 80:db5f5237bc95 421 IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK connection_status_callback = NULL;
AzureIoTClient 80:db5f5237bc95 422 IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC device_method_callback = NULL;
AzureIoTClient 80:db5f5237bc95 423 IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK inbound_device_method_callback = NULL;
AzureIoTClient 80:db5f5237bc95 424 IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC message_callback = NULL;
AzureIoTClient 80:db5f5237bc95 425 IOTHUB_CLIENT_HANDLE message_user_context_handle = NULL;
AzureIoTClient 80:db5f5237bc95 426 IOTHUB_CLIENT_HANDLE method_user_context_handle = NULL;
AzureIoTClient 80:db5f5237bc95 427
AzureIoTClient 80:db5f5237bc95 428 // Make a local copy of these callbacks, as we don't run with a lock held and iotHubClientInstance may change mid-run.
AzureIoTClient 80:db5f5237bc95 429 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 80:db5f5237bc95 430 {
AzureIoTClient 80:db5f5237bc95 431 LogError("failed locking for dispatch_user_callbacks");
AzureIoTClient 80:db5f5237bc95 432 }
AzureIoTClient 80:db5f5237bc95 433 else
AzureIoTClient 80:db5f5237bc95 434 {
AzureIoTClient 80:db5f5237bc95 435 desired_state_callback = iotHubClientInstance->desired_state_callback;
AzureIoTClient 80:db5f5237bc95 436 event_confirm_callback = iotHubClientInstance->event_confirm_callback;
AzureIoTClient 80:db5f5237bc95 437 reported_state_callback = iotHubClientInstance->reported_state_callback;
AzureIoTClient 80:db5f5237bc95 438 connection_status_callback = iotHubClientInstance->connection_status_callback;
AzureIoTClient 80:db5f5237bc95 439 device_method_callback = iotHubClientInstance->device_method_callback;
AzureIoTClient 80:db5f5237bc95 440 inbound_device_method_callback = iotHubClientInstance->inbound_device_method_callback;
AzureIoTClient 80:db5f5237bc95 441 message_callback = iotHubClientInstance->message_callback;
AzureIoTClient 80:db5f5237bc95 442 if (iotHubClientInstance->method_user_context)
AzureIoTClient 80:db5f5237bc95 443 {
AzureIoTClient 80:db5f5237bc95 444 method_user_context_handle = iotHubClientInstance->method_user_context->iotHubClientHandle;
AzureIoTClient 80:db5f5237bc95 445 }
AzureIoTClient 80:db5f5237bc95 446 if (iotHubClientInstance->message_user_context)
AzureIoTClient 80:db5f5237bc95 447 {
AzureIoTClient 80:db5f5237bc95 448 message_user_context_handle = iotHubClientInstance->message_user_context->iotHubClientHandle;
AzureIoTClient 80:db5f5237bc95 449 }
AzureIoTClient 80:db5f5237bc95 450
AzureIoTClient 80:db5f5237bc95 451 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 80:db5f5237bc95 452 }
AzureIoTClient 80:db5f5237bc95 453
AzureIoTClient 80:db5f5237bc95 454
AzureIoTClient 61:8b85a4e797cf 455 for (index = 0; index < callbacks_length; index++)
Azure.IoT.Build 54:6dcad9019a64 456 {
AzureIoTClient 61:8b85a4e797cf 457 USER_CALLBACK_INFO* queued_cb = (USER_CALLBACK_INFO*)VECTOR_element(call_backs, index);
AzureIoTClient 61:8b85a4e797cf 458 if (queued_cb == NULL)
AzureIoTClient 61:8b85a4e797cf 459 {
AzureIoTClient 61:8b85a4e797cf 460 LogError("VECTOR_element at index %zd is NULL.", index);
AzureIoTClient 61:8b85a4e797cf 461 }
AzureIoTClient 61:8b85a4e797cf 462 else
Azure.IoT.Build 54:6dcad9019a64 463 {
AzureIoTClient 61:8b85a4e797cf 464 switch (queued_cb->type)
Azure.IoT.Build 54:6dcad9019a64 465 {
AzureIoTClient 61:8b85a4e797cf 466 case CALLBACK_TYPE_DEVICE_TWIN:
AzureIoTClient 67:ecebc2a41159 467 {
AzureIoTClient 67:ecebc2a41159 468 if (desired_state_callback)
AzureIoTClient 67:ecebc2a41159 469 {
AzureIoTClient 67:ecebc2a41159 470 desired_state_callback(queued_cb->iothub_callback.dev_twin_cb_info.update_state, queued_cb->iothub_callback.dev_twin_cb_info.payLoad, queued_cb->iothub_callback.dev_twin_cb_info.size, queued_cb->userContextCallback);
AzureIoTClient 67:ecebc2a41159 471 }
AzureIoTClient 67:ecebc2a41159 472
AzureIoTClient 61:8b85a4e797cf 473 if (queued_cb->iothub_callback.dev_twin_cb_info.payLoad)
AzureIoTClient 61:8b85a4e797cf 474 {
AzureIoTClient 61:8b85a4e797cf 475 free(queued_cb->iothub_callback.dev_twin_cb_info.payLoad);
AzureIoTClient 61:8b85a4e797cf 476 }
AzureIoTClient 61:8b85a4e797cf 477 break;
AzureIoTClient 67:ecebc2a41159 478 }
AzureIoTClient 61:8b85a4e797cf 479 case CALLBACK_TYPE_EVENT_CONFIRM:
AzureIoTClient 80:db5f5237bc95 480 if (event_confirm_callback)
AzureIoTClient 61:8b85a4e797cf 481 {
AzureIoTClient 80:db5f5237bc95 482 event_confirm_callback(queued_cb->iothub_callback.event_confirm_cb_info.confirm_result, queued_cb->userContextCallback);
AzureIoTClient 61:8b85a4e797cf 483 }
AzureIoTClient 61:8b85a4e797cf 484 break;
AzureIoTClient 61:8b85a4e797cf 485 case CALLBACK_TYPE_REPORTED_STATE:
AzureIoTClient 80:db5f5237bc95 486 if (reported_state_callback)
AzureIoTClient 61:8b85a4e797cf 487 {
AzureIoTClient 80:db5f5237bc95 488 reported_state_callback(queued_cb->iothub_callback.reported_state_cb_info.status_code, queued_cb->userContextCallback);
AzureIoTClient 61:8b85a4e797cf 489 }
AzureIoTClient 61:8b85a4e797cf 490 break;
AzureIoTClient 61:8b85a4e797cf 491 case CALLBACK_TYPE_CONNECTION_STATUS:
AzureIoTClient 80:db5f5237bc95 492 if (connection_status_callback)
AzureIoTClient 61:8b85a4e797cf 493 {
AzureIoTClient 80:db5f5237bc95 494 connection_status_callback(queued_cb->iothub_callback.connection_status_cb_info.connection_status, queued_cb->iothub_callback.connection_status_cb_info.status_reason, queued_cb->userContextCallback);
AzureIoTClient 61:8b85a4e797cf 495 }
AzureIoTClient 61:8b85a4e797cf 496 break;
AzureIoTClient 61:8b85a4e797cf 497 case CALLBACK_TYPE_DEVICE_METHOD:
AzureIoTClient 80:db5f5237bc95 498 if (device_method_callback)
AzureIoTClient 61:8b85a4e797cf 499 {
AzureIoTClient 61:8b85a4e797cf 500 const char* method_name = STRING_c_str(queued_cb->iothub_callback.method_cb_info.method_name);
AzureIoTClient 61:8b85a4e797cf 501 const unsigned char* payload = BUFFER_u_char(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 61:8b85a4e797cf 502 size_t payload_len = BUFFER_length(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 62:5a4cdacf5090 503
AzureIoTClient 62:5a4cdacf5090 504 unsigned char* payload_resp = NULL;
AzureIoTClient 62:5a4cdacf5090 505 size_t response_size = 0;
AzureIoTClient 80:db5f5237bc95 506 int status = device_method_callback(method_name, payload, payload_len, &payload_resp, &response_size, queued_cb->userContextCallback);
AzureIoTClient 62:5a4cdacf5090 507
AzureIoTClient 62:5a4cdacf5090 508 if (payload_resp && (response_size > 0))
AzureIoTClient 62:5a4cdacf5090 509 {
AzureIoTClient 80:db5f5237bc95 510 IOTHUB_CLIENT_RESULT result = IoTHubClient_DeviceMethodResponse(method_user_context_handle, queued_cb->iothub_callback.method_cb_info.method_id, (const unsigned char*)payload_resp, response_size, status);
AzureIoTClient 62:5a4cdacf5090 511 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 62:5a4cdacf5090 512 {
AzureIoTClient 62:5a4cdacf5090 513 LogError("IoTHubClient_LL_DeviceMethodResponse failed");
AzureIoTClient 62:5a4cdacf5090 514 }
AzureIoTClient 62:5a4cdacf5090 515 }
AzureIoTClient 62:5a4cdacf5090 516
AzureIoTClient 62:5a4cdacf5090 517 BUFFER_delete(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 62:5a4cdacf5090 518 STRING_delete(queued_cb->iothub_callback.method_cb_info.method_name);
AzureIoTClient 66:a419827cb051 519
AzureIoTClient 66:a419827cb051 520 if (payload_resp)
AzureIoTClient 66:a419827cb051 521 {
AzureIoTClient 62:5a4cdacf5090 522 free(payload_resp);
AzureIoTClient 66:a419827cb051 523 }
AzureIoTClient 62:5a4cdacf5090 524 }
AzureIoTClient 62:5a4cdacf5090 525 break;
AzureIoTClient 62:5a4cdacf5090 526 case CALLBACK_TYPE_INBOUD_DEVICE_METHOD:
AzureIoTClient 80:db5f5237bc95 527 if (inbound_device_method_callback)
AzureIoTClient 62:5a4cdacf5090 528 {
AzureIoTClient 62:5a4cdacf5090 529 const char* method_name = STRING_c_str(queued_cb->iothub_callback.method_cb_info.method_name);
AzureIoTClient 62:5a4cdacf5090 530 const unsigned char* payload = BUFFER_u_char(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 62:5a4cdacf5090 531 size_t payload_len = BUFFER_length(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 62:5a4cdacf5090 532
AzureIoTClient 80:db5f5237bc95 533 inbound_device_method_callback(method_name, payload, payload_len, queued_cb->iothub_callback.method_cb_info.method_id, queued_cb->userContextCallback);
AzureIoTClient 61:8b85a4e797cf 534
AzureIoTClient 61:8b85a4e797cf 535 BUFFER_delete(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 61:8b85a4e797cf 536 STRING_delete(queued_cb->iothub_callback.method_cb_info.method_name);
AzureIoTClient 61:8b85a4e797cf 537 }
AzureIoTClient 61:8b85a4e797cf 538 break;
AzureIoTClient 61:8b85a4e797cf 539 case CALLBACK_TYPE_MESSAGE:
AzureIoTClient 80:db5f5237bc95 540 if (message_callback)
AzureIoTClient 61:8b85a4e797cf 541 {
AzureIoTClient 80:db5f5237bc95 542 IOTHUBMESSAGE_DISPOSITION_RESULT disposition = message_callback(queued_cb->iothub_callback.message_cb_info->messageHandle, queued_cb->userContextCallback);
AzureIoTClient 61:8b85a4e797cf 543
AzureIoTClient 80:db5f5237bc95 544 if (Lock(message_user_context_handle->LockHandle) == LOCK_OK)
Azure.IoT.Build 54:6dcad9019a64 545 {
AzureIoTClient 80:db5f5237bc95 546 IOTHUB_CLIENT_RESULT result = IoTHubClient_LL_SendMessageDisposition(message_user_context_handle->IoTHubClientLLHandle, queued_cb->iothub_callback.message_cb_info, disposition);
AzureIoTClient 80:db5f5237bc95 547 (void)Unlock(message_user_context_handle->LockHandle);
AzureIoTClient 61:8b85a4e797cf 548 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 549 {
AzureIoTClient 62:5a4cdacf5090 550 LogError("IoTHubClient_LL_SendMessageDisposition failed");
Azure.IoT.Build 54:6dcad9019a64 551 }
Azure.IoT.Build 54:6dcad9019a64 552 }
AzureIoTClient 61:8b85a4e797cf 553 else
Azure.IoT.Build 54:6dcad9019a64 554 {
AzureIoTClient 61:8b85a4e797cf 555 LogError("Lock failed");
Azure.IoT.Build 54:6dcad9019a64 556 }
AzureIoTClient 61:8b85a4e797cf 557 }
AzureIoTClient 61:8b85a4e797cf 558 break;
AzureIoTClient 61:8b85a4e797cf 559 default:
AzureIoTClient 61:8b85a4e797cf 560 LogError("Invalid callback type '%s'", ENUM_TO_STRING(USER_CALLBACK_TYPE, queued_cb->type));
AzureIoTClient 61:8b85a4e797cf 561 break;
Azure.IoT.Build 54:6dcad9019a64 562 }
Azure.IoT.Build 54:6dcad9019a64 563 }
Azure.IoT.Build 54:6dcad9019a64 564 }
AzureIoTClient 61:8b85a4e797cf 565 VECTOR_destroy(call_backs);
Azure.IoT.Build 54:6dcad9019a64 566 }
Azure.IoT.Build 54:6dcad9019a64 567
AzureIoTClient 66:a419827cb051 568 static void ScheduleWork_Thread_ForMultiplexing(void* iotHubClientHandle)
AzureIoTClient 66:a419827cb051 569 {
AzureIoTClient 66:a419827cb051 570 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 66:a419827cb051 571
AzureIoTClient 66:a419827cb051 572 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 66:a419827cb051 573 garbageCollectorImpl(iotHubClientInstance);
AzureIoTClient 66:a419827cb051 574 #endif
AzureIoTClient 66:a419827cb051 575 if (Lock(iotHubClientInstance->LockHandle) == LOCK_OK)
AzureIoTClient 66:a419827cb051 576 {
AzureIoTClient 66:a419827cb051 577 VECTOR_HANDLE call_backs = VECTOR_move(iotHubClientInstance->saved_user_callback_list);
AzureIoTClient 66:a419827cb051 578 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 66:a419827cb051 579
AzureIoTClient 66:a419827cb051 580 if (call_backs == NULL)
AzureIoTClient 66:a419827cb051 581 {
AzureIoTClient 66:a419827cb051 582 LogError("Failed moving user callbacks");
AzureIoTClient 66:a419827cb051 583 }
AzureIoTClient 66:a419827cb051 584 else
AzureIoTClient 66:a419827cb051 585 {
AzureIoTClient 66:a419827cb051 586 dispatch_user_callbacks(iotHubClientInstance, call_backs);
AzureIoTClient 66:a419827cb051 587 }
AzureIoTClient 66:a419827cb051 588 }
AzureIoTClient 66:a419827cb051 589 else
AzureIoTClient 66:a419827cb051 590 {
AzureIoTClient 66:a419827cb051 591 LogError("failed locking for ScheduleWork_Thread_ForMultiplexing");
AzureIoTClient 66:a419827cb051 592 }
AzureIoTClient 66:a419827cb051 593 }
AzureIoTClient 66:a419827cb051 594
AzureIoTClient 16:deba40344375 595 static int ScheduleWork_Thread(void* threadArgument)
AzureIoTClient 16:deba40344375 596 {
AzureIoTClient 16:deba40344375 597 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)threadArgument;
AzureIoTClient 42:448eecc3676e 598
Azure.IoT Build 37:18310e4d888d 599 while (1)
AzureIoTClient 16:deba40344375 600 {
AzureIoTClient 16:deba40344375 601 if (Lock(iotHubClientInstance->LockHandle) == LOCK_OK)
AzureIoTClient 16:deba40344375 602 {
Azure.IoT Build 37:18310e4d888d 603 /*Codes_SRS_IOTHUBCLIENT_01_038: [ The thread shall exit when IoTHubClient_Destroy is called. ]*/
Azure.IoT Build 37:18310e4d888d 604 if (iotHubClientInstance->StopThread)
Azure.IoT Build 37:18310e4d888d 605 {
Azure.IoT Build 37:18310e4d888d 606 (void)Unlock(iotHubClientInstance->LockHandle);
Azure.IoT Build 37:18310e4d888d 607 break; /*gets out of the thread*/
Azure.IoT Build 37:18310e4d888d 608 }
Azure.IoT Build 37:18310e4d888d 609 else
Azure.IoT Build 37:18310e4d888d 610 {
Azure.IoT Build 37:18310e4d888d 611 /* Codes_SRS_IOTHUBCLIENT_01_037: [The thread created by IoTHubClient_SendEvent or IoTHubClient_SetMessageCallback shall call IoTHubClient_LL_DoWork every 1 ms.] */
Azure.IoT Build 37:18310e4d888d 612 /* Codes_SRS_IOTHUBCLIENT_01_039: [All calls to IoTHubClient_LL_DoWork shall be protected by the lock created in IotHubClient_Create.] */
Azure.IoT Build 37:18310e4d888d 613 IoTHubClient_LL_DoWork(iotHubClientInstance->IoTHubClientLLHandle);
AzureIoTClient 42:448eecc3676e 614
AzureIoTClient 44:33dd78697616 615 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 616 garbageCollectorImpl(iotHubClientInstance);
AzureIoTClient 43:038d8511e817 617 #endif
AzureIoTClient 61:8b85a4e797cf 618 VECTOR_HANDLE call_backs = VECTOR_move(iotHubClientInstance->saved_user_callback_list);
Azure.IoT Build 37:18310e4d888d 619 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 61:8b85a4e797cf 620 if (call_backs == NULL)
AzureIoTClient 61:8b85a4e797cf 621 {
AzureIoTClient 61:8b85a4e797cf 622 LogError("VECTOR_move failed");
AzureIoTClient 61:8b85a4e797cf 623 }
AzureIoTClient 61:8b85a4e797cf 624 else
AzureIoTClient 61:8b85a4e797cf 625 {
AzureIoTClient 61:8b85a4e797cf 626 dispatch_user_callbacks(iotHubClientInstance, call_backs);
AzureIoTClient 61:8b85a4e797cf 627 }
Azure.IoT Build 37:18310e4d888d 628 }
AzureIoTClient 16:deba40344375 629 }
Azure.IoT Build 37:18310e4d888d 630 else
Azure.IoT Build 37:18310e4d888d 631 {
Azure.IoT Build 37:18310e4d888d 632 /*Codes_SRS_IOTHUBCLIENT_01_040: [If acquiring the lock fails, IoTHubClient_LL_DoWork shall not be called.]*/
Azure.IoT Build 37:18310e4d888d 633 /*no code, shall retry*/
Azure.IoT Build 37:18310e4d888d 634 }
Azure.IoT Build 37:18310e4d888d 635 (void)ThreadAPI_Sleep(1);
AzureIoTClient 16:deba40344375 636 }
AzureIoTClient 42:448eecc3676e 637
AzureIoTClient 71:0d498da5ece1 638 ThreadAPI_Exit(0);
AzureIoTClient 16:deba40344375 639 return 0;
AzureIoTClient 16:deba40344375 640 }
AzureIoTClient 16:deba40344375 641
Azure.IoT Build 37:18310e4d888d 642 static IOTHUB_CLIENT_RESULT StartWorkerThreadIfNeeded(IOTHUB_CLIENT_INSTANCE* iotHubClientInstance)
AzureIoTClient 16:deba40344375 643 {
AzureIoTClient 42:448eecc3676e 644 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 645 if (iotHubClientInstance->TransportHandle == NULL)
AzureIoTClient 42:448eecc3676e 646 {
AzureIoTClient 42:448eecc3676e 647 if (iotHubClientInstance->ThreadHandle == NULL)
AzureIoTClient 42:448eecc3676e 648 {
AzureIoTClient 42:448eecc3676e 649 iotHubClientInstance->StopThread = 0;
AzureIoTClient 42:448eecc3676e 650 if (ThreadAPI_Create(&iotHubClientInstance->ThreadHandle, ScheduleWork_Thread, iotHubClientInstance) != THREADAPI_OK)
AzureIoTClient 42:448eecc3676e 651 {
AzureIoTClient 61:8b85a4e797cf 652 LogError("ThreadAPI_Create failed");
AzureIoTClient 42:448eecc3676e 653 iotHubClientInstance->ThreadHandle = NULL;
AzureIoTClient 42:448eecc3676e 654 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 655 }
AzureIoTClient 42:448eecc3676e 656 else
AzureIoTClient 42:448eecc3676e 657 {
AzureIoTClient 42:448eecc3676e 658 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 659 }
AzureIoTClient 42:448eecc3676e 660 }
AzureIoTClient 42:448eecc3676e 661 else
AzureIoTClient 42:448eecc3676e 662 {
AzureIoTClient 42:448eecc3676e 663 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 664 }
AzureIoTClient 42:448eecc3676e 665 }
AzureIoTClient 42:448eecc3676e 666 else
AzureIoTClient 42:448eecc3676e 667 {
AzureIoTClient 42:448eecc3676e 668 /*Codes_SRS_IOTHUBCLIENT_17_012: [ If the transport connection is shared, the thread shall be started by calling IoTHubTransport_StartWorkerThread. ]*/
AzureIoTClient 42:448eecc3676e 669 /*Codes_SRS_IOTHUBCLIENT_17_011: [ If the transport connection is shared, the thread shall be started by calling IoTHubTransport_StartWorkerThread*/
AzureIoTClient 66:a419827cb051 670 result = IoTHubTransport_StartWorkerThread(iotHubClientInstance->TransportHandle, iotHubClientInstance, ScheduleWork_Thread_ForMultiplexing);
AzureIoTClient 42:448eecc3676e 671 }
AzureIoTClient 42:448eecc3676e 672 return result;
AzureIoTClient 16:deba40344375 673 }
AzureIoTClient 16:deba40344375 674
AzureIoTClient 78:74a8d3068204 675 static IOTHUB_CLIENT_INSTANCE* create_iothub_instance(const IOTHUB_CLIENT_CONFIG* config, TRANSPORT_HANDLE transportHandle, const char* connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol, const char* iothub_uri, const char* device_id)
AzureIoTClient 16:deba40344375 676 {
AzureIoTClient 78:74a8d3068204 677 /* Codes_SRS_IOTHUBCLIENT_12_020: [** `IoTHubClient_CreateFromDeviceAuth` shall allocate a new `IoTHubClient` instance. **] */
AzureIoTClient 16:deba40344375 678 IOTHUB_CLIENT_INSTANCE* result = (IOTHUB_CLIENT_INSTANCE*)malloc(sizeof(IOTHUB_CLIENT_INSTANCE));
AzureIoTClient 16:deba40344375 679
AzureIoTClient 78:74a8d3068204 680 /* Codes_SRS_IOTHUBCLIENT_12_021: [** If allocating memory for the new `IoTHubClient` instance fails, then `IoTHubClient_CreateFromDeviceAuth` shall return `NULL`. **] */
AzureIoTClient 16:deba40344375 681 /* Codes_SRS_IOTHUBCLIENT_01_004: [If allocating memory for the new IoTHubClient instance fails, then IoTHubClient_Create shall return NULL.] */
AzureIoTClient 16:deba40344375 682 if (result != NULL)
AzureIoTClient 16:deba40344375 683 {
AzureIoTClient 16:deba40344375 684 /* Codes_SRS_IOTHUBCLIENT_01_029: [IoTHubClient_Create shall create a lock object to be used later for serializing IoTHubClient calls.] */
Azure.IoT.Build 54:6dcad9019a64 685 if ( (result->saved_user_callback_list = VECTOR_create(sizeof(USER_CALLBACK_INFO)) ) == NULL)
AzureIoTClient 16:deba40344375 686 {
Azure.IoT.Build 54:6dcad9019a64 687 LogError("Failed creating VECTOR");
AzureIoTClient 16:deba40344375 688 free(result);
AzureIoTClient 16:deba40344375 689 result = NULL;
AzureIoTClient 16:deba40344375 690 }
AzureIoTClient 16:deba40344375 691 else
AzureIoTClient 16:deba40344375 692 {
AzureIoTClient 44:33dd78697616 693 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 52:1cc3c6d07cad 694 /*Codes_SRS_IOTHUBCLIENT_02_060: [ IoTHubClient_Create shall create a SINGLYLINKEDLIST_HANDLE containing THREAD_HANDLE (created by future calls to IoTHubClient_UploadToBlobAsync). ]*/
AzureIoTClient 52:1cc3c6d07cad 695 if ((result->savedDataToBeCleaned = singlylinkedlist_create()) == NULL)
AzureIoTClient 16:deba40344375 696 {
AzureIoTClient 52:1cc3c6d07cad 697 /*Codes_SRS_IOTHUBCLIENT_02_061: [ If creating the SINGLYLINKEDLIST_HANDLE fails then IoTHubClient_Create shall fail and return NULL. ]*/
AzureIoTClient 52:1cc3c6d07cad 698 LogError("unable to singlylinkedlist_create");
Azure.IoT.Build 54:6dcad9019a64 699 VECTOR_destroy(result->saved_user_callback_list);
AzureIoTClient 16:deba40344375 700 free(result);
AzureIoTClient 16:deba40344375 701 result = NULL;
AzureIoTClient 16:deba40344375 702 }
AzureIoTClient 42:448eecc3676e 703 else
AzureIoTClient 43:038d8511e817 704 #endif
AzureIoTClient 42:448eecc3676e 705 {
Azure.IoT.Build 54:6dcad9019a64 706 result->TransportHandle = transportHandle;
Azure.IoT.Build 54:6dcad9019a64 707 result->created_with_transport_handle = 0;
Azure.IoT.Build 54:6dcad9019a64 708 if (config != NULL)
Azure.IoT.Build 54:6dcad9019a64 709 {
Azure.IoT.Build 54:6dcad9019a64 710 if (transportHandle != NULL)
Azure.IoT.Build 54:6dcad9019a64 711 {
Azure.IoT.Build 54:6dcad9019a64 712 /*Codes_SRS_IOTHUBCLIENT_17_005: [ IoTHubClient_CreateWithTransport shall call IoTHubTransport_GetLock to get the transport lock to be used later for serializing IoTHubClient calls. ]*/
Azure.IoT.Build 54:6dcad9019a64 713 result->LockHandle = IoTHubTransport_GetLock(transportHandle);
Azure.IoT.Build 54:6dcad9019a64 714 if (result->LockHandle == NULL)
Azure.IoT.Build 54:6dcad9019a64 715 {
Azure.IoT.Build 54:6dcad9019a64 716 LogError("unable to IoTHubTransport_GetLock");
Azure.IoT.Build 54:6dcad9019a64 717 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 718 }
Azure.IoT.Build 54:6dcad9019a64 719 else
Azure.IoT.Build 54:6dcad9019a64 720 {
Azure.IoT.Build 54:6dcad9019a64 721 IOTHUB_CLIENT_DEVICE_CONFIG deviceConfig;
Azure.IoT.Build 54:6dcad9019a64 722 deviceConfig.deviceId = config->deviceId;
Azure.IoT.Build 54:6dcad9019a64 723 deviceConfig.deviceKey = config->deviceKey;
Azure.IoT.Build 54:6dcad9019a64 724 deviceConfig.protocol = config->protocol;
Azure.IoT.Build 54:6dcad9019a64 725 deviceConfig.deviceSasToken = config->deviceSasToken;
Azure.IoT.Build 54:6dcad9019a64 726
Azure.IoT.Build 54:6dcad9019a64 727 /*Codes_SRS_IOTHUBCLIENT_17_003: [ IoTHubClient_CreateWithTransport shall call IoTHubTransport_GetLLTransport on transportHandle to get lower layer transport. ]*/
Azure.IoT.Build 54:6dcad9019a64 728 deviceConfig.transportHandle = IoTHubTransport_GetLLTransport(transportHandle);
Azure.IoT.Build 54:6dcad9019a64 729 if (deviceConfig.transportHandle == NULL)
Azure.IoT.Build 54:6dcad9019a64 730 {
Azure.IoT.Build 54:6dcad9019a64 731 LogError("unable to IoTHubTransport_GetLLTransport");
Azure.IoT.Build 54:6dcad9019a64 732 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 733 }
Azure.IoT.Build 54:6dcad9019a64 734 else
Azure.IoT.Build 54:6dcad9019a64 735 {
Azure.IoT.Build 54:6dcad9019a64 736 if (Lock(result->LockHandle) != LOCK_OK)
Azure.IoT.Build 54:6dcad9019a64 737 {
Azure.IoT.Build 54:6dcad9019a64 738 LogError("unable to Lock");
Azure.IoT.Build 54:6dcad9019a64 739 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 740 }
Azure.IoT.Build 54:6dcad9019a64 741 else
Azure.IoT.Build 54:6dcad9019a64 742 {
Azure.IoT.Build 54:6dcad9019a64 743 /*Codes_SRS_IOTHUBCLIENT_17_007: [ IoTHubClient_CreateWithTransport shall instantiate a new IoTHubClient_LL instance by calling IoTHubClient_LL_CreateWithTransport and passing the lower layer transport and config argument. ]*/
Azure.IoT.Build 54:6dcad9019a64 744 result->IoTHubClientLLHandle = IoTHubClient_LL_CreateWithTransport(&deviceConfig);
Azure.IoT.Build 54:6dcad9019a64 745 result->created_with_transport_handle = 1;
Azure.IoT.Build 54:6dcad9019a64 746 if (Unlock(result->LockHandle) != LOCK_OK)
Azure.IoT.Build 54:6dcad9019a64 747 {
Azure.IoT.Build 54:6dcad9019a64 748 LogError("unable to Unlock");
Azure.IoT.Build 54:6dcad9019a64 749 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 750 }
Azure.IoT.Build 54:6dcad9019a64 751 }
Azure.IoT.Build 54:6dcad9019a64 752 }
Azure.IoT.Build 54:6dcad9019a64 753 }
Azure.IoT.Build 54:6dcad9019a64 754 }
Azure.IoT.Build 54:6dcad9019a64 755 else
Azure.IoT.Build 54:6dcad9019a64 756 {
Azure.IoT.Build 54:6dcad9019a64 757 result->LockHandle = Lock_Init();
Azure.IoT.Build 54:6dcad9019a64 758 if (result->LockHandle == NULL)
Azure.IoT.Build 54:6dcad9019a64 759 {
Azure.IoT.Build 54:6dcad9019a64 760 /* Codes_SRS_IOTHUBCLIENT_01_030: [If creating the lock fails, then IoTHubClient_Create shall return NULL.] */
Azure.IoT.Build 54:6dcad9019a64 761 /* Codes_SRS_IOTHUBCLIENT_01_031: [If IoTHubClient_Create fails, all resources allocated by it shall be freed.] */
Azure.IoT.Build 54:6dcad9019a64 762 LogError("Failure creating Lock object");
Azure.IoT.Build 54:6dcad9019a64 763 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 764 }
Azure.IoT.Build 54:6dcad9019a64 765 else
Azure.IoT.Build 54:6dcad9019a64 766 {
Azure.IoT.Build 54:6dcad9019a64 767 /* Codes_SRS_IOTHUBCLIENT_01_002: [IoTHubClient_Create shall instantiate a new IoTHubClient_LL instance by calling IoTHubClient_LL_Create and passing the config argument.] */
Azure.IoT.Build 54:6dcad9019a64 768 result->IoTHubClientLLHandle = IoTHubClient_LL_Create(config);
Azure.IoT.Build 54:6dcad9019a64 769 }
Azure.IoT.Build 54:6dcad9019a64 770 }
Azure.IoT.Build 54:6dcad9019a64 771 }
AzureIoTClient 78:74a8d3068204 772 else if (iothub_uri != NULL)
AzureIoTClient 78:74a8d3068204 773 {
AzureIoTClient 78:74a8d3068204 774 #ifdef USE_PROV_MODULE
AzureIoTClient 78:74a8d3068204 775 /* Codes_SRS_IOTHUBCLIENT_12_022: [** `IoTHubClient_CreateFromDeviceAuth` shall create a lock object to be used later for serializing IoTHubClient calls. **] */
AzureIoTClient 78:74a8d3068204 776 result->LockHandle = Lock_Init();
AzureIoTClient 78:74a8d3068204 777 if (result->LockHandle == NULL)
AzureIoTClient 78:74a8d3068204 778 {
AzureIoTClient 78:74a8d3068204 779 /* Codes_SRS_IOTHUBCLIENT_12_023: [** If creating the lock fails, then IoTHubClient_CreateFromDeviceAuth shall return NULL. **] */
AzureIoTClient 78:74a8d3068204 780 LogError("Failure creating Lock object");
AzureIoTClient 78:74a8d3068204 781 result->IoTHubClientLLHandle = NULL;
AzureIoTClient 78:74a8d3068204 782 }
AzureIoTClient 78:74a8d3068204 783 else
AzureIoTClient 78:74a8d3068204 784 {
AzureIoTClient 78:74a8d3068204 785 /* Codes_SRS_IOTHUBCLIENT_12_025: [** `IoTHubClient_CreateFromDeviceAuth` shall instantiate a new `IoTHubClient_LL` instance by calling `IoTHubClient_LL_CreateFromDeviceAuth` and passing iothub_uri, device_id and protocol argument. **] */
AzureIoTClient 78:74a8d3068204 786 result->IoTHubClientLLHandle = IoTHubClient_LL_CreateFromDeviceAuth(iothub_uri, device_id, protocol);
AzureIoTClient 78:74a8d3068204 787 }
AzureIoTClient 78:74a8d3068204 788 #else
AzureIoTClient 78:74a8d3068204 789 (void)device_id;
AzureIoTClient 78:74a8d3068204 790 LogError("Provisioning is not enabled for the build");
AzureIoTClient 78:74a8d3068204 791 result->IoTHubClientLLHandle = NULL;
AzureIoTClient 78:74a8d3068204 792 #endif
AzureIoTClient 78:74a8d3068204 793 }
Azure.IoT.Build 54:6dcad9019a64 794 else
Azure.IoT.Build 54:6dcad9019a64 795 {
Azure.IoT.Build 54:6dcad9019a64 796 result->LockHandle = Lock_Init();
Azure.IoT.Build 54:6dcad9019a64 797 if (result->LockHandle == NULL)
Azure.IoT.Build 54:6dcad9019a64 798 {
Azure.IoT.Build 54:6dcad9019a64 799 /* Codes_SRS_IOTHUBCLIENT_01_030: [If creating the lock fails, then IoTHubClient_Create shall return NULL.] */
Azure.IoT.Build 54:6dcad9019a64 800 /* Codes_SRS_IOTHUBCLIENT_01_031: [If IoTHubClient_Create fails, all resources allocated by it shall be freed.] */
Azure.IoT.Build 54:6dcad9019a64 801 LogError("Failure creating Lock object");
Azure.IoT.Build 54:6dcad9019a64 802 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 803 }
Azure.IoT.Build 54:6dcad9019a64 804 else
Azure.IoT.Build 54:6dcad9019a64 805 {
Azure.IoT.Build 54:6dcad9019a64 806 /* Codes_SRS_IOTHUBCLIENT_12_006: [IoTHubClient_CreateFromConnectionString shall instantiate a new IoTHubClient_LL instance by calling IoTHubClient_LL_CreateFromConnectionString and passing the connectionString] */
Azure.IoT.Build 54:6dcad9019a64 807 result->IoTHubClientLLHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, protocol);
Azure.IoT.Build 54:6dcad9019a64 808 }
Azure.IoT.Build 54:6dcad9019a64 809 }
Azure.IoT.Build 54:6dcad9019a64 810
AzureIoTClient 42:448eecc3676e 811 if (result->IoTHubClientLLHandle == NULL)
AzureIoTClient 42:448eecc3676e 812 {
AzureIoTClient 42:448eecc3676e 813 /* Codes_SRS_IOTHUBCLIENT_01_003: [If IoTHubClient_LL_Create fails, then IoTHubClient_Create shall return NULL.] */
AzureIoTClient 42:448eecc3676e 814 /* Codes_SRS_IOTHUBCLIENT_01_031: [If IoTHubClient_Create fails, all resources allocated by it shall be freed.] */
Azure.IoT.Build 54:6dcad9019a64 815 /* Codes_SRS_IOTHUBCLIENT_17_006: [ If IoTHubTransport_GetLock fails, then IoTHubClient_CreateWithTransport shall return NULL. ]*/
Azure.IoT.Build 54:6dcad9019a64 816 if (transportHandle == NULL)
Azure.IoT.Build 54:6dcad9019a64 817 {
Azure.IoT.Build 54:6dcad9019a64 818 Lock_Deinit(result->LockHandle);
Azure.IoT.Build 54:6dcad9019a64 819 }
AzureIoTClient 44:33dd78697616 820 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 52:1cc3c6d07cad 821 singlylinkedlist_destroy(result->savedDataToBeCleaned);
AzureIoTClient 43:038d8511e817 822 #endif
Azure.IoT.Build 54:6dcad9019a64 823 LogError("Failure creating iothub handle");
Azure.IoT.Build 54:6dcad9019a64 824 VECTOR_destroy(result->saved_user_callback_list);
AzureIoTClient 42:448eecc3676e 825 free(result);
AzureIoTClient 42:448eecc3676e 826 result = NULL;
AzureIoTClient 42:448eecc3676e 827 }
AzureIoTClient 42:448eecc3676e 828 else
AzureIoTClient 42:448eecc3676e 829 {
AzureIoTClient 42:448eecc3676e 830 result->ThreadHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 831 result->desired_state_callback = NULL;
Azure.IoT.Build 54:6dcad9019a64 832 result->event_confirm_callback = NULL;
Azure.IoT.Build 54:6dcad9019a64 833 result->reported_state_callback = NULL;
Azure.IoT.Build 54:6dcad9019a64 834 result->devicetwin_user_context = NULL;
AzureIoTClient 55:59b527ab3452 835 result->connection_status_callback = NULL;
AzureIoTClient 55:59b527ab3452 836 result->connection_status_user_context = NULL;
AzureIoTClient 61:8b85a4e797cf 837 result->message_callback = NULL;
AzureIoTClient 61:8b85a4e797cf 838 result->message_user_context = NULL;
AzureIoTClient 62:5a4cdacf5090 839 result->method_user_context = NULL;
AzureIoTClient 42:448eecc3676e 840 }
AzureIoTClient 42:448eecc3676e 841 }
AzureIoTClient 16:deba40344375 842 }
AzureIoTClient 16:deba40344375 843 }
Azure.IoT.Build 54:6dcad9019a64 844 return result;
Azure.IoT.Build 54:6dcad9019a64 845 }
AzureIoTClient 16:deba40344375 846
Azure.IoT.Build 54:6dcad9019a64 847 IOTHUB_CLIENT_HANDLE IoTHubClient_CreateFromConnectionString(const char* connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol)
Azure.IoT.Build 54:6dcad9019a64 848 {
Azure.IoT.Build 54:6dcad9019a64 849 IOTHUB_CLIENT_INSTANCE* result;
Azure.IoT.Build 54:6dcad9019a64 850
Azure.IoT.Build 54:6dcad9019a64 851 /* Codes_SRS_IOTHUBCLIENT_12_003: [IoTHubClient_CreateFromConnectionString shall verify all input parameters and if any is NULL then return NULL] */
Azure.IoT.Build 54:6dcad9019a64 852 if (connectionString == NULL)
Azure.IoT.Build 54:6dcad9019a64 853 {
Azure.IoT.Build 54:6dcad9019a64 854 LogError("Input parameter is NULL: connectionString");
Azure.IoT.Build 54:6dcad9019a64 855 result = NULL;
Azure.IoT.Build 54:6dcad9019a64 856 }
Azure.IoT.Build 54:6dcad9019a64 857 else if (protocol == NULL)
Azure.IoT.Build 54:6dcad9019a64 858 {
Azure.IoT.Build 54:6dcad9019a64 859 LogError("Input parameter is NULL: protocol");
Azure.IoT.Build 54:6dcad9019a64 860 result = NULL;
Azure.IoT.Build 54:6dcad9019a64 861 }
Azure.IoT.Build 54:6dcad9019a64 862 else
Azure.IoT.Build 54:6dcad9019a64 863 {
AzureIoTClient 78:74a8d3068204 864 result = create_iothub_instance(NULL, NULL, connectionString, protocol, NULL, NULL);
Azure.IoT.Build 54:6dcad9019a64 865 }
Azure.IoT.Build 54:6dcad9019a64 866 return result;
Azure.IoT.Build 54:6dcad9019a64 867 }
Azure.IoT.Build 54:6dcad9019a64 868
Azure.IoT.Build 54:6dcad9019a64 869 IOTHUB_CLIENT_HANDLE IoTHubClient_Create(const IOTHUB_CLIENT_CONFIG* config)
Azure.IoT.Build 54:6dcad9019a64 870 {
Azure.IoT.Build 54:6dcad9019a64 871 IOTHUB_CLIENT_INSTANCE* result;
Azure.IoT.Build 54:6dcad9019a64 872 if (config == NULL)
Azure.IoT.Build 54:6dcad9019a64 873 {
Azure.IoT.Build 54:6dcad9019a64 874 LogError("Input parameter is NULL: IOTHUB_CLIENT_CONFIG");
Azure.IoT.Build 54:6dcad9019a64 875 result = NULL;
Azure.IoT.Build 54:6dcad9019a64 876 }
Azure.IoT.Build 54:6dcad9019a64 877 else
Azure.IoT.Build 54:6dcad9019a64 878 {
AzureIoTClient 78:74a8d3068204 879 result = create_iothub_instance(config, NULL, NULL, NULL, NULL, NULL);
Azure.IoT.Build 54:6dcad9019a64 880 }
AzureIoTClient 16:deba40344375 881 return result;
AzureIoTClient 16:deba40344375 882 }
AzureIoTClient 16:deba40344375 883
Azure.IoT Build 37:18310e4d888d 884 IOTHUB_CLIENT_HANDLE IoTHubClient_CreateWithTransport(TRANSPORT_HANDLE transportHandle, const IOTHUB_CLIENT_CONFIG* config)
Azure.IoT Build 37:18310e4d888d 885 {
AzureIoTClient 42:448eecc3676e 886 IOTHUB_CLIENT_INSTANCE* result;
AzureIoTClient 42:448eecc3676e 887 /*Codes_SRS_IOTHUBCLIENT_17_013: [ IoTHubClient_CreateWithTransport shall return NULL if transportHandle is NULL. ]*/
AzureIoTClient 42:448eecc3676e 888 /*Codes_SRS_IOTHUBCLIENT_17_014: [ IoTHubClient_CreateWithTransport shall return NULL if config is NULL. ]*/
AzureIoTClient 42:448eecc3676e 889 if (transportHandle == NULL || config == NULL)
AzureIoTClient 42:448eecc3676e 890 {
AzureIoTClient 43:038d8511e817 891 LogError("invalid parameter TRANSPORT_HANDLE transportHandle=%p, const IOTHUB_CLIENT_CONFIG* config=%p", transportHandle, config);
AzureIoTClient 42:448eecc3676e 892 result = NULL;
AzureIoTClient 42:448eecc3676e 893 }
AzureIoTClient 42:448eecc3676e 894 else
AzureIoTClient 42:448eecc3676e 895 {
AzureIoTClient 78:74a8d3068204 896 result = create_iothub_instance(config, transportHandle, NULL, NULL, NULL, NULL);
AzureIoTClient 78:74a8d3068204 897 }
AzureIoTClient 78:74a8d3068204 898 return result;
AzureIoTClient 78:74a8d3068204 899 }
AzureIoTClient 78:74a8d3068204 900
AzureIoTClient 78:74a8d3068204 901 IOTHUB_CLIENT_HANDLE IoTHubClient_CreateFromDeviceAuth(const char* iothub_uri, const char* device_id, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol)
AzureIoTClient 78:74a8d3068204 902 {
AzureIoTClient 78:74a8d3068204 903 IOTHUB_CLIENT_INSTANCE* result;
AzureIoTClient 78:74a8d3068204 904
AzureIoTClient 78:74a8d3068204 905 /* Codes_SRS_IOTHUBCLIENT_12_019: [** `IoTHubClient_CreateFromDeviceAuth` shall verify the input parameters and if any of them `NULL` then return `NULL`. **] */
AzureIoTClient 78:74a8d3068204 906 if (iothub_uri == NULL)
AzureIoTClient 78:74a8d3068204 907 {
AzureIoTClient 78:74a8d3068204 908 LogError("Input parameter is NULL: iothub_uri");
AzureIoTClient 78:74a8d3068204 909 result = NULL;
AzureIoTClient 78:74a8d3068204 910 }
AzureIoTClient 78:74a8d3068204 911 else if (device_id == NULL)
AzureIoTClient 78:74a8d3068204 912 {
AzureIoTClient 78:74a8d3068204 913 LogError("Input parameter is NULL: device_id");
AzureIoTClient 78:74a8d3068204 914 result = NULL;
AzureIoTClient 78:74a8d3068204 915 }
AzureIoTClient 78:74a8d3068204 916 else if (protocol == NULL)
AzureIoTClient 78:74a8d3068204 917 {
AzureIoTClient 78:74a8d3068204 918 LogError("Input parameter is NULL: protocol");
AzureIoTClient 78:74a8d3068204 919 result = NULL;
AzureIoTClient 78:74a8d3068204 920 }
AzureIoTClient 78:74a8d3068204 921 else
AzureIoTClient 78:74a8d3068204 922 {
AzureIoTClient 78:74a8d3068204 923 /* Codes_SRS_IOTHUBCLIENT_12_020: [** `IoTHubClient_CreateFromDeviceAuth` shall allocate a new `IoTHubClient` instance. **] */
AzureIoTClient 78:74a8d3068204 924 /* Codes_SRS_IOTHUBCLIENT_12_021: [** If allocating memory for the new `IoTHubClient` instance fails, then `IoTHubClient_CreateFromDeviceAuth` shall return `NULL`. **] */
AzureIoTClient 78:74a8d3068204 925 /* Codes_SRS_IOTHUBCLIENT_12_022: [** `IoTHubClient_CreateFromDeviceAuth` shall create a lock object to be used later for serializing IoTHubClient calls. **] */
AzureIoTClient 78:74a8d3068204 926 /* Codes_SRS_IOTHUBCLIENT_12_023: [** If creating the lock fails, then IoTHubClient_CreateFromDeviceAuth shall return NULL. **] */
AzureIoTClient 78:74a8d3068204 927 /* Codes_SRS_IOTHUBCLIENT_12_024: [** If IoTHubClient_CreateFromDeviceAuth fails, all resources allocated by it shall be freed. **] */
AzureIoTClient 78:74a8d3068204 928 /* Codes_SRS_IOTHUBCLIENT_12_025: [** `IoTHubClient_CreateFromDeviceAuth` shall instantiate a new `IoTHubClient_LL` instance by calling `IoTHubClient_LL_CreateFromDeviceAuth` and passing iothub_uri, device_id and protocol argument. **] */
AzureIoTClient 78:74a8d3068204 929 result = create_iothub_instance(NULL, NULL, NULL, protocol, iothub_uri, device_id);
AzureIoTClient 42:448eecc3676e 930 }
AzureIoTClient 42:448eecc3676e 931 return result;
Azure.IoT Build 37:18310e4d888d 932 }
Azure.IoT Build 37:18310e4d888d 933
AzureIoTClient 16:deba40344375 934 /* Codes_SRS_IOTHUBCLIENT_01_005: [IoTHubClient_Destroy shall free all resources associated with the iotHubClientHandle instance.] */
AzureIoTClient 18:1e9adb15c645 935 void IoTHubClient_Destroy(IOTHUB_CLIENT_HANDLE iotHubClientHandle)
AzureIoTClient 16:deba40344375 936 {
AzureIoTClient 16:deba40344375 937 /* Codes_SRS_IOTHUBCLIENT_01_008: [IoTHubClient_Destroy shall do nothing if parameter iotHubClientHandle is NULL.] */
AzureIoTClient 16:deba40344375 938 if (iotHubClientHandle != NULL)
AzureIoTClient 16:deba40344375 939 {
AzureIoTClient 78:74a8d3068204 940 bool joinClientThread;
AzureIoTClient 78:74a8d3068204 941 bool joinTransportThread;
AzureIoTClient 62:5a4cdacf5090 942 size_t vector_size;
Azure.IoT Build 37:18310e4d888d 943
AzureIoTClient 16:deba40344375 944 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 945
AzureIoTClient 66:a419827cb051 946 if (iotHubClientInstance->TransportHandle != NULL)
AzureIoTClient 66:a419827cb051 947 {
AzureIoTClient 66:a419827cb051 948 /*Codes_SRS_IOTHUBCLIENT_01_007: [ The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetNotificationMessageCallback shall be joined. ]*/
AzureIoTClient 78:74a8d3068204 949 joinTransportThread = IoTHubTransport_SignalEndWorkerThread(iotHubClientInstance->TransportHandle, iotHubClientHandle);
AzureIoTClient 78:74a8d3068204 950 }
AzureIoTClient 78:74a8d3068204 951 else
AzureIoTClient 78:74a8d3068204 952 {
AzureIoTClient 78:74a8d3068204 953 joinTransportThread = false;
AzureIoTClient 66:a419827cb051 954 }
AzureIoTClient 66:a419827cb051 955
AzureIoTClient 42:448eecc3676e 956 /*Codes_SRS_IOTHUBCLIENT_02_043: [ IoTHubClient_Destroy shall lock the serializing lock and signal the worker thread (if any) to end ]*/
AzureIoTClient 42:448eecc3676e 957 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 958 {
AzureIoTClient 42:448eecc3676e 959 LogError("unable to Lock - - will still proceed to try to end the thread without locking");
AzureIoTClient 42:448eecc3676e 960 }
AzureIoTClient 42:448eecc3676e 961
AzureIoTClient 16:deba40344375 962 if (iotHubClientInstance->ThreadHandle != NULL)
AzureIoTClient 16:deba40344375 963 {
AzureIoTClient 42:448eecc3676e 964 iotHubClientInstance->StopThread = 1;
AzureIoTClient 78:74a8d3068204 965 joinClientThread = true;
AzureIoTClient 42:448eecc3676e 966 }
AzureIoTClient 42:448eecc3676e 967 else
AzureIoTClient 42:448eecc3676e 968 {
AzureIoTClient 78:74a8d3068204 969 joinClientThread = false;
AzureIoTClient 16:deba40344375 970 }
Azure.IoT Build 37:18310e4d888d 971
AzureIoTClient 42:448eecc3676e 972 /*Codes_SRS_IOTHUBCLIENT_02_045: [ IoTHubClient_Destroy shall unlock the serializing lock. ]*/
AzureIoTClient 42:448eecc3676e 973 if (Unlock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 974 {
AzureIoTClient 42:448eecc3676e 975 LogError("unable to Unlock");
AzureIoTClient 42:448eecc3676e 976 }
Azure.IoT Build 37:18310e4d888d 977
AzureIoTClient 78:74a8d3068204 978 if (joinClientThread == true)
AzureIoTClient 42:448eecc3676e 979 {
AzureIoTClient 78:74a8d3068204 980 int res;
AzureIoTClient 78:74a8d3068204 981 /*Codes_SRS_IOTHUBCLIENT_01_007: [ The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetNotificationMessageCallback shall be joined. ]*/
AzureIoTClient 78:74a8d3068204 982 if (ThreadAPI_Join(iotHubClientInstance->ThreadHandle, &res) != THREADAPI_OK)
AzureIoTClient 42:448eecc3676e 983 {
AzureIoTClient 78:74a8d3068204 984 LogError("ThreadAPI_Join failed");
AzureIoTClient 42:448eecc3676e 985 }
AzureIoTClient 42:448eecc3676e 986 }
AzureIoTClient 42:448eecc3676e 987
AzureIoTClient 78:74a8d3068204 988 if (joinTransportThread == true)
AzureIoTClient 78:74a8d3068204 989 {
AzureIoTClient 78:74a8d3068204 990 /*Codes_SRS_IOTHUBCLIENT_01_007: [ The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetNotificationMessageCallback shall be joined. ]*/
AzureIoTClient 78:74a8d3068204 991 IoTHubTransport_JoinWorkerThread(iotHubClientInstance->TransportHandle, iotHubClientHandle);
AzureIoTClient 78:74a8d3068204 992 }
AzureIoTClient 76:943524fee0b7 993
AzureIoTClient 76:943524fee0b7 994 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 76:943524fee0b7 995 {
AzureIoTClient 76:943524fee0b7 996 LogError("unable to Lock - - will still proceed to try to end the thread without locking");
AzureIoTClient 76:943524fee0b7 997 }
AzureIoTClient 76:943524fee0b7 998
AzureIoTClient 76:943524fee0b7 999 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 76:943524fee0b7 1000 /*Codes_SRS_IOTHUBCLIENT_02_069: [ IoTHubClient_Destroy shall free all data created by IoTHubClient_UploadToBlobAsync ]*/
AzureIoTClient 76:943524fee0b7 1001 /*wait for all uploading threads to finish*/
AzureIoTClient 76:943524fee0b7 1002 while (singlylinkedlist_get_head_item(iotHubClientInstance->savedDataToBeCleaned) != NULL)
AzureIoTClient 76:943524fee0b7 1003 {
AzureIoTClient 76:943524fee0b7 1004 garbageCollectorImpl(iotHubClientInstance);
AzureIoTClient 76:943524fee0b7 1005 }
AzureIoTClient 76:943524fee0b7 1006
AzureIoTClient 76:943524fee0b7 1007 if (iotHubClientInstance->savedDataToBeCleaned != NULL)
AzureIoTClient 76:943524fee0b7 1008 {
AzureIoTClient 76:943524fee0b7 1009 singlylinkedlist_destroy(iotHubClientInstance->savedDataToBeCleaned);
AzureIoTClient 76:943524fee0b7 1010 }
AzureIoTClient 76:943524fee0b7 1011 #endif
AzureIoTClient 76:943524fee0b7 1012
AzureIoTClient 76:943524fee0b7 1013 /* Codes_SRS_IOTHUBCLIENT_01_006: [That includes destroying the IoTHubClient_LL instance by calling IoTHubClient_LL_Destroy.] */
AzureIoTClient 76:943524fee0b7 1014 IoTHubClient_LL_Destroy(iotHubClientInstance->IoTHubClientLLHandle);
AzureIoTClient 76:943524fee0b7 1015
AzureIoTClient 76:943524fee0b7 1016 if (Unlock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 76:943524fee0b7 1017 {
AzureIoTClient 76:943524fee0b7 1018 LogError("unable to Unlock");
AzureIoTClient 76:943524fee0b7 1019 }
AzureIoTClient 76:943524fee0b7 1020
AzureIoTClient 76:943524fee0b7 1021
AzureIoTClient 55:59b527ab3452 1022 vector_size = VECTOR_size(iotHubClientInstance->saved_user_callback_list);
AzureIoTClient 57:4524910c6445 1023 size_t index = 0;
AzureIoTClient 57:4524910c6445 1024 for (index = 0; index < vector_size; index++)
AzureIoTClient 55:59b527ab3452 1025 {
AzureIoTClient 55:59b527ab3452 1026 USER_CALLBACK_INFO* queue_cb_info = (USER_CALLBACK_INFO*)VECTOR_element(iotHubClientInstance->saved_user_callback_list, index);
AzureIoTClient 55:59b527ab3452 1027 if (queue_cb_info != NULL)
AzureIoTClient 55:59b527ab3452 1028 {
AzureIoTClient 62:5a4cdacf5090 1029 if ((queue_cb_info->type == CALLBACK_TYPE_DEVICE_METHOD) || (queue_cb_info->type == CALLBACK_TYPE_INBOUD_DEVICE_METHOD))
AzureIoTClient 55:59b527ab3452 1030 {
AzureIoTClient 55:59b527ab3452 1031 STRING_delete(queue_cb_info->iothub_callback.method_cb_info.method_name);
AzureIoTClient 55:59b527ab3452 1032 BUFFER_delete(queue_cb_info->iothub_callback.method_cb_info.payload);
AzureIoTClient 55:59b527ab3452 1033 }
AzureIoTClient 55:59b527ab3452 1034 else if (queue_cb_info->type == CALLBACK_TYPE_DEVICE_TWIN)
AzureIoTClient 55:59b527ab3452 1035 {
AzureIoTClient 55:59b527ab3452 1036 if (queue_cb_info->iothub_callback.dev_twin_cb_info.payLoad != NULL)
AzureIoTClient 55:59b527ab3452 1037 {
AzureIoTClient 55:59b527ab3452 1038 free(queue_cb_info->iothub_callback.dev_twin_cb_info.payLoad);
AzureIoTClient 55:59b527ab3452 1039 }
AzureIoTClient 55:59b527ab3452 1040 }
AzureIoTClient 59:7e89be231352 1041 else if (queue_cb_info->type == CALLBACK_TYPE_EVENT_CONFIRM)
AzureIoTClient 59:7e89be231352 1042 {
AzureIoTClient 59:7e89be231352 1043 if (iotHubClientInstance->event_confirm_callback)
AzureIoTClient 59:7e89be231352 1044 {
AzureIoTClient 59:7e89be231352 1045 iotHubClientInstance->event_confirm_callback(queue_cb_info->iothub_callback.event_confirm_cb_info.confirm_result, queue_cb_info->userContextCallback);
AzureIoTClient 59:7e89be231352 1046 }
AzureIoTClient 59:7e89be231352 1047 }
AzureIoTClient 55:59b527ab3452 1048 }
AzureIoTClient 55:59b527ab3452 1049 }
Azure.IoT.Build 54:6dcad9019a64 1050 VECTOR_destroy(iotHubClientInstance->saved_user_callback_list);
Azure.IoT.Build 54:6dcad9019a64 1051
AzureIoTClient 42:448eecc3676e 1052 if (iotHubClientInstance->TransportHandle == NULL)
AzureIoTClient 42:448eecc3676e 1053 {
AzureIoTClient 42:448eecc3676e 1054 /* Codes_SRS_IOTHUBCLIENT_01_032: [If the lock was allocated in IoTHubClient_Create, it shall be also freed..] */
AzureIoTClient 42:448eecc3676e 1055 Lock_Deinit(iotHubClientInstance->LockHandle);
AzureIoTClient 42:448eecc3676e 1056 }
Azure.IoT.Build 54:6dcad9019a64 1057 if (iotHubClientInstance->devicetwin_user_context != NULL)
Azure.IoT.Build 54:6dcad9019a64 1058 {
Azure.IoT.Build 54:6dcad9019a64 1059 free(iotHubClientInstance->devicetwin_user_context);
Azure.IoT.Build 54:6dcad9019a64 1060 }
AzureIoTClient 55:59b527ab3452 1061 if (iotHubClientInstance->connection_status_user_context != NULL)
AzureIoTClient 55:59b527ab3452 1062 {
AzureIoTClient 55:59b527ab3452 1063 free(iotHubClientInstance->connection_status_user_context);
AzureIoTClient 55:59b527ab3452 1064 }
AzureIoTClient 61:8b85a4e797cf 1065 if (iotHubClientInstance->message_user_context != NULL)
AzureIoTClient 61:8b85a4e797cf 1066 {
AzureIoTClient 61:8b85a4e797cf 1067 free(iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 1068 }
AzureIoTClient 62:5a4cdacf5090 1069 if (iotHubClientInstance->method_user_context != NULL)
AzureIoTClient 62:5a4cdacf5090 1070 {
AzureIoTClient 62:5a4cdacf5090 1071 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1072 }
AzureIoTClient 16:deba40344375 1073 free(iotHubClientInstance);
AzureIoTClient 16:deba40344375 1074 }
AzureIoTClient 16:deba40344375 1075 }
AzureIoTClient 16:deba40344375 1076
AzureIoTClient 16:deba40344375 1077 IOTHUB_CLIENT_RESULT IoTHubClient_SendEventAsync(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_MESSAGE_HANDLE eventMessageHandle, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK eventConfirmationCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 1078 {
AzureIoTClient 16:deba40344375 1079 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1080
AzureIoTClient 16:deba40344375 1081 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 1082 {
AzureIoTClient 16:deba40344375 1083 /* Codes_SRS_IOTHUBCLIENT_01_011: [If iotHubClientHandle is NULL, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 1084 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 1085 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 1086 }
AzureIoTClient 16:deba40344375 1087 else
AzureIoTClient 16:deba40344375 1088 {
AzureIoTClient 16:deba40344375 1089 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1090
AzureIoTClient 66:a419827cb051 1091 /* Codes_SRS_IOTHUBCLIENT_01_009: [IoTHubClient_SendEventAsync shall start the worker thread if it was not previously started.] */
AzureIoTClient 66:a419827cb051 1092 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 16:deba40344375 1093 {
AzureIoTClient 66:a419827cb051 1094 /* Codes_SRS_IOTHUBCLIENT_01_010: [If starting the thread fails, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1095 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1096 LogError("Could not start worker thread");
AzureIoTClient 16:deba40344375 1097 }
AzureIoTClient 16:deba40344375 1098 else
AzureIoTClient 16:deba40344375 1099 {
AzureIoTClient 66:a419827cb051 1100 /* Codes_SRS_IOTHUBCLIENT_01_025: [IoTHubClient_SendEventAsync shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 1101 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 1102 {
AzureIoTClient 66:a419827cb051 1103 /* Codes_SRS_IOTHUBCLIENT_01_026: [If acquiring the lock fails, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1104 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1105 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 1106 }
AzureIoTClient 16:deba40344375 1107 else
AzureIoTClient 16:deba40344375 1108 {
AzureIoTClient 80:db5f5237bc95 1109 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 80:db5f5237bc95 1110 {
AzureIoTClient 80:db5f5237bc95 1111 iotHubClientInstance->event_confirm_callback = eventConfirmationCallback;
AzureIoTClient 80:db5f5237bc95 1112 }
AzureIoTClient 80:db5f5237bc95 1113
Azure.IoT.Build 54:6dcad9019a64 1114 if (iotHubClientInstance->created_with_transport_handle != 0 || eventConfirmationCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 1115 {
Azure.IoT.Build 54:6dcad9019a64 1116 result = IoTHubClient_LL_SendEventAsync(iotHubClientInstance->IoTHubClientLLHandle, eventMessageHandle, eventConfirmationCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 1117 }
Azure.IoT.Build 54:6dcad9019a64 1118 else
Azure.IoT.Build 54:6dcad9019a64 1119 {
Azure.IoT.Build 54:6dcad9019a64 1120 /* Codes_SRS_IOTHUBCLIENT_07_001: [ IoTHubClient_SendEventAsync shall allocate a IOTHUB_QUEUE_CONTEXT object to be sent to the IoTHubClient_LL_SendEventAsync function as a user context. ] */
Azure.IoT.Build 54:6dcad9019a64 1121 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
Azure.IoT.Build 54:6dcad9019a64 1122 if (queue_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1123 {
Azure.IoT.Build 54:6dcad9019a64 1124 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1125 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1126 }
Azure.IoT.Build 54:6dcad9019a64 1127 else
Azure.IoT.Build 54:6dcad9019a64 1128 {
Azure.IoT.Build 54:6dcad9019a64 1129 queue_context->iotHubClientHandle = iotHubClientInstance;
Azure.IoT.Build 54:6dcad9019a64 1130 queue_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1131 /* Codes_SRS_IOTHUBCLIENT_01_012: [IoTHubClient_SendEventAsync shall call IoTHubClient_LL_SendEventAsync, while passing the IoTHubClient_LL handle created by IoTHubClient_Create and the parameters eventMessageHandle, eventConfirmationCallback and userContextCallback.] */
Azure.IoT.Build 54:6dcad9019a64 1132 /* Codes_SRS_IOTHUBCLIENT_01_013: [When IoTHubClient_LL_SendEventAsync is called, IoTHubClient_SendEventAsync shall return the result of IoTHubClient_LL_SendEventAsync.] */
Azure.IoT.Build 54:6dcad9019a64 1133 result = IoTHubClient_LL_SendEventAsync(iotHubClientInstance->IoTHubClientLLHandle, eventMessageHandle, iothub_ll_event_confirm_callback, queue_context);
Azure.IoT.Build 54:6dcad9019a64 1134 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1135 {
Azure.IoT.Build 54:6dcad9019a64 1136 LogError("IoTHubClient_LL_SendEventAsync failed");
Azure.IoT.Build 54:6dcad9019a64 1137 free(queue_context);
Azure.IoT.Build 54:6dcad9019a64 1138 }
Azure.IoT.Build 54:6dcad9019a64 1139 }
Azure.IoT.Build 54:6dcad9019a64 1140 }
AzureIoTClient 66:a419827cb051 1141
AzureIoTClient 66:a419827cb051 1142 /* Codes_SRS_IOTHUBCLIENT_01_025: [IoTHubClient_SendEventAsync shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 1143 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1144 }
AzureIoTClient 16:deba40344375 1145 }
AzureIoTClient 16:deba40344375 1146 }
AzureIoTClient 16:deba40344375 1147
AzureIoTClient 16:deba40344375 1148 return result;
AzureIoTClient 16:deba40344375 1149 }
AzureIoTClient 16:deba40344375 1150
AzureIoTClient 16:deba40344375 1151 IOTHUB_CLIENT_RESULT IoTHubClient_GetSendStatus(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 16:deba40344375 1152 {
AzureIoTClient 16:deba40344375 1153 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1154
AzureIoTClient 16:deba40344375 1155 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 1156 {
AzureIoTClient 16:deba40344375 1157 /* Codes_SRS_IOTHUBCLIENT_01_023: [If iotHubClientHandle is NULL, IoTHubClient_ GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 1158 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 1159 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 1160 }
AzureIoTClient 16:deba40344375 1161 else
AzureIoTClient 16:deba40344375 1162 {
AzureIoTClient 16:deba40344375 1163 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1164
AzureIoTClient 16:deba40344375 1165 /* Codes_SRS_IOTHUBCLIENT_01_033: [IoTHubClient_GetSendStatus shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 1166 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 1167 {
AzureIoTClient 16:deba40344375 1168 /* Codes_SRS_IOTHUBCLIENT_01_034: [If acquiring the lock fails, IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1169 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 39:2719651a5bee 1170 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 1171 }
AzureIoTClient 16:deba40344375 1172 else
AzureIoTClient 16:deba40344375 1173 {
AzureIoTClient 16:deba40344375 1174 /* Codes_SRS_IOTHUBCLIENT_01_022: [IoTHubClient_GetSendStatus shall call IoTHubClient_LL_GetSendStatus, while passing the IoTHubClient_LL handle created by IoTHubClient_Create and the parameter iotHubClientStatus.] */
AzureIoTClient 16:deba40344375 1175 /* Codes_SRS_IOTHUBCLIENT_01_024: [Otherwise, IoTHubClient_GetSendStatus shall return the result of IoTHubClient_LL_GetSendStatus.] */
AzureIoTClient 16:deba40344375 1176 result = IoTHubClient_LL_GetSendStatus(iotHubClientInstance->IoTHubClientLLHandle, iotHubClientStatus);
AzureIoTClient 16:deba40344375 1177
AzureIoTClient 16:deba40344375 1178 /* Codes_SRS_IOTHUBCLIENT_01_033: [IoTHubClient_GetSendStatus shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 1179 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1180 }
AzureIoTClient 16:deba40344375 1181 }
AzureIoTClient 16:deba40344375 1182
AzureIoTClient 16:deba40344375 1183 return result;
AzureIoTClient 16:deba40344375 1184 }
AzureIoTClient 16:deba40344375 1185
AzureIoTClient 16:deba40344375 1186 IOTHUB_CLIENT_RESULT IoTHubClient_SetMessageCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 1187 {
AzureIoTClient 16:deba40344375 1188 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1189
AzureIoTClient 16:deba40344375 1190 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 1191 {
AzureIoTClient 16:deba40344375 1192 /* Codes_SRS_IOTHUBCLIENT_01_016: [If iotHubClientHandle is NULL, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 1193 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 1194 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 1195 }
AzureIoTClient 16:deba40344375 1196 else
AzureIoTClient 16:deba40344375 1197 {
AzureIoTClient 16:deba40344375 1198 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1199
AzureIoTClient 66:a419827cb051 1200 /* Codes_SRS_IOTHUBCLIENT_01_014: [IoTHubClient_SetMessageCallback shall start the worker thread if it was not previously started.] */
AzureIoTClient 66:a419827cb051 1201 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 16:deba40344375 1202 {
AzureIoTClient 66:a419827cb051 1203 /* Codes_SRS_IOTHUBCLIENT_01_015: [If starting the thread fails, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1204 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1205 LogError("Could not start worker thread");
AzureIoTClient 16:deba40344375 1206 }
AzureIoTClient 16:deba40344375 1207 else
AzureIoTClient 16:deba40344375 1208 {
AzureIoTClient 66:a419827cb051 1209 /* Codes_SRS_IOTHUBCLIENT_01_027: [IoTHubClient_SetMessageCallback shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 1210 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 1211 {
AzureIoTClient 66:a419827cb051 1212 /* Codes_SRS_IOTHUBCLIENT_01_028: [If acquiring the lock fails, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1213 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1214 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 1215 }
AzureIoTClient 16:deba40344375 1216 else
AzureIoTClient 16:deba40344375 1217 {
AzureIoTClient 80:db5f5237bc95 1218 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 80:db5f5237bc95 1219 {
AzureIoTClient 80:db5f5237bc95 1220 iotHubClientInstance->message_callback = messageCallback;
AzureIoTClient 80:db5f5237bc95 1221 }
AzureIoTClient 61:8b85a4e797cf 1222 if (iotHubClientInstance->message_user_context != NULL)
AzureIoTClient 61:8b85a4e797cf 1223 {
AzureIoTClient 61:8b85a4e797cf 1224 free(iotHubClientInstance->message_user_context);
AzureIoTClient 62:5a4cdacf5090 1225 iotHubClientInstance->message_user_context = NULL;
AzureIoTClient 61:8b85a4e797cf 1226 }
AzureIoTClient 61:8b85a4e797cf 1227 if (messageCallback == NULL)
AzureIoTClient 61:8b85a4e797cf 1228 {
AzureIoTClient 62:5a4cdacf5090 1229 result = IoTHubClient_LL_SetMessageCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, NULL, iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 1230 }
AzureIoTClient 61:8b85a4e797cf 1231 else if (iotHubClientInstance->created_with_transport_handle != 0)
AzureIoTClient 61:8b85a4e797cf 1232 {
AzureIoTClient 61:8b85a4e797cf 1233 result = IoTHubClient_LL_SetMessageCallback(iotHubClientInstance->IoTHubClientLLHandle, messageCallback, userContextCallback);
AzureIoTClient 61:8b85a4e797cf 1234 }
AzureIoTClient 61:8b85a4e797cf 1235 else
AzureIoTClient 61:8b85a4e797cf 1236 {
AzureIoTClient 61:8b85a4e797cf 1237 iotHubClientInstance->message_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 61:8b85a4e797cf 1238 if (iotHubClientInstance->message_user_context == NULL)
AzureIoTClient 61:8b85a4e797cf 1239 {
AzureIoTClient 61:8b85a4e797cf 1240 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 1241 LogError("Failed allocating QUEUE_CONTEXT");
AzureIoTClient 61:8b85a4e797cf 1242 }
AzureIoTClient 61:8b85a4e797cf 1243 else
AzureIoTClient 61:8b85a4e797cf 1244 {
AzureIoTClient 61:8b85a4e797cf 1245 iotHubClientInstance->message_user_context->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 61:8b85a4e797cf 1246 iotHubClientInstance->message_user_context->userContextCallback = userContextCallback;
AzureIoTClient 61:8b85a4e797cf 1247
AzureIoTClient 62:5a4cdacf5090 1248 /* Codes_SRS_IOTHUBCLIENT_01_017: [IoTHubClient_SetMessageCallback shall call IoTHubClient_LL_SetMessageCallback_Ex, while passing the IoTHubClient_LL handle created by IoTHubClient_Create and the local iothub_ll_message_callback wrapper of messageCallback and userContextCallback.] */
AzureIoTClient 62:5a4cdacf5090 1249 /* Codes_SRS_IOTHUBCLIENT_01_018: [When IoTHubClient_LL_SetMessageCallback_Ex is called, IoTHubClient_SetMessageCallback shall return the result of IoTHubClient_LL_SetMessageCallback_Ex.] */
AzureIoTClient 62:5a4cdacf5090 1250 result = IoTHubClient_LL_SetMessageCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_message_callback, iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 1251 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 61:8b85a4e797cf 1252 {
AzureIoTClient 61:8b85a4e797cf 1253 LogError("IoTHubClient_LL_SetMessageCallback failed");
AzureIoTClient 61:8b85a4e797cf 1254 free(iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 1255 iotHubClientInstance->message_user_context = NULL;
AzureIoTClient 61:8b85a4e797cf 1256 }
AzureIoTClient 61:8b85a4e797cf 1257 }
AzureIoTClient 61:8b85a4e797cf 1258 }
AzureIoTClient 66:a419827cb051 1259
AzureIoTClient 66:a419827cb051 1260 /* Codes_SRS_IOTHUBCLIENT_01_027: [IoTHubClient_SetMessageCallback shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 1261 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1262 }
AzureIoTClient 16:deba40344375 1263 }
AzureIoTClient 16:deba40344375 1264 }
AzureIoTClient 16:deba40344375 1265
AzureIoTClient 16:deba40344375 1266 return result;
AzureIoTClient 16:deba40344375 1267 }
AzureIoTClient 16:deba40344375 1268
AzureIoTClient 53:1e5a1ca1f274 1269 IOTHUB_CLIENT_RESULT IoTHubClient_SetConnectionStatusCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK connectionStatusCallback, void * userContextCallback)
AzureIoTClient 52:1cc3c6d07cad 1270 {
AzureIoTClient 53:1e5a1ca1f274 1271 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1272
AzureIoTClient 53:1e5a1ca1f274 1273 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1274 {
AzureIoTClient 53:1e5a1ca1f274 1275 /* Codes_SRS_IOTHUBCLIENT_25_076: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ] */
AzureIoTClient 53:1e5a1ca1f274 1276 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1277 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 1278 }
AzureIoTClient 53:1e5a1ca1f274 1279 else
AzureIoTClient 53:1e5a1ca1f274 1280 {
AzureIoTClient 53:1e5a1ca1f274 1281 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 52:1cc3c6d07cad 1282
AzureIoTClient 66:a419827cb051 1283 /* Codes_SRS_IOTHUBCLIENT_25_081: [ `IoTHubClient_SetConnectionStatusCallback` shall start the worker thread if it was not previously started. ]*/
AzureIoTClient 66:a419827cb051 1284 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1285 {
AzureIoTClient 66:a419827cb051 1286 /* Codes_SRS_IOTHUBCLIENT_25_083: [ If starting the thread fails, `IoTHubClient_SetConnectionStatusCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1287 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1288 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1289 }
AzureIoTClient 53:1e5a1ca1f274 1290 else
AzureIoTClient 53:1e5a1ca1f274 1291 {
AzureIoTClient 66:a419827cb051 1292 /* Codes_SRS_IOTHUBCLIENT_25_087: [ `IoTHubClient_SetConnectionStatusCallback` shall be made thread-safe by using the lock created in `IoTHubClient_Create`. ] */
AzureIoTClient 66:a419827cb051 1293 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
Azure.IoT.Build 54:6dcad9019a64 1294 {
AzureIoTClient 66:a419827cb051 1295 /* Codes_SRS_IOTHUBCLIENT_25_088: [ If acquiring the lock fails, `IoTHubClient_SetConnectionStatusCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1296 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1297 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1298 }
AzureIoTClient 53:1e5a1ca1f274 1299 else
AzureIoTClient 53:1e5a1ca1f274 1300 {
AzureIoTClient 66:a419827cb051 1301 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 66:a419827cb051 1302 {
AzureIoTClient 66:a419827cb051 1303 iotHubClientInstance->connection_status_callback = connectionStatusCallback;
AzureIoTClient 66:a419827cb051 1304 }
AzureIoTClient 66:a419827cb051 1305
Azure.IoT.Build 54:6dcad9019a64 1306 if (iotHubClientInstance->created_with_transport_handle != 0 || connectionStatusCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 1307 {
Azure.IoT.Build 54:6dcad9019a64 1308 /* Codes_SRS_IOTHUBCLIENT_25_085: [ `IoTHubClient_SetConnectionStatusCallback` shall call `IoTHubClient_LL_SetConnectionStatusCallback`, while passing the `IoTHubClient_LL` handle created by `IoTHubClient_Create` and the parameters `connectionStatusCallback` and `userContextCallback`. ]*/
Azure.IoT.Build 54:6dcad9019a64 1309 result = IoTHubClient_LL_SetConnectionStatusCallback(iotHubClientInstance->IoTHubClientLLHandle, connectionStatusCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 1310 }
Azure.IoT.Build 54:6dcad9019a64 1311 else
Azure.IoT.Build 54:6dcad9019a64 1312 {
AzureIoTClient 55:59b527ab3452 1313 if (iotHubClientInstance->connection_status_user_context != NULL)
AzureIoTClient 55:59b527ab3452 1314 {
AzureIoTClient 55:59b527ab3452 1315 free(iotHubClientInstance->connection_status_user_context);
AzureIoTClient 55:59b527ab3452 1316 }
AzureIoTClient 55:59b527ab3452 1317 iotHubClientInstance->connection_status_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 55:59b527ab3452 1318 if (iotHubClientInstance->connection_status_user_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1319 {
Azure.IoT.Build 54:6dcad9019a64 1320 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1321 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1322 }
Azure.IoT.Build 54:6dcad9019a64 1323 else
Azure.IoT.Build 54:6dcad9019a64 1324 {
AzureIoTClient 55:59b527ab3452 1325 iotHubClientInstance->connection_status_user_context->iotHubClientHandle = iotHubClientInstance;
AzureIoTClient 55:59b527ab3452 1326 iotHubClientInstance->connection_status_user_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1327
Azure.IoT.Build 54:6dcad9019a64 1328 /* Codes_SRS_IOTHUBCLIENT_25_085: [ `IoTHubClient_SetConnectionStatusCallback` shall call `IoTHubClient_LL_SetConnectionStatusCallback`, while passing the `IoTHubClient_LL` handle created by `IoTHubClient_Create` and the parameters `connectionStatusCallback` and `userContextCallback`. ]*/
AzureIoTClient 55:59b527ab3452 1329 result = IoTHubClient_LL_SetConnectionStatusCallback(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_connection_status_callback, iotHubClientInstance->connection_status_user_context);
Azure.IoT.Build 54:6dcad9019a64 1330 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1331 {
Azure.IoT.Build 54:6dcad9019a64 1332 LogError("IoTHubClient_LL_SetConnectionStatusCallback failed");
AzureIoTClient 55:59b527ab3452 1333 free(iotHubClientInstance->connection_status_user_context);
AzureIoTClient 61:8b85a4e797cf 1334 iotHubClientInstance->connection_status_user_context = NULL;
Azure.IoT.Build 54:6dcad9019a64 1335 }
Azure.IoT.Build 54:6dcad9019a64 1336 }
Azure.IoT.Build 54:6dcad9019a64 1337 }
AzureIoTClient 66:a419827cb051 1338 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1339 }
AzureIoTClient 53:1e5a1ca1f274 1340 }
AzureIoTClient 53:1e5a1ca1f274 1341 }
AzureIoTClient 52:1cc3c6d07cad 1342 return result;
AzureIoTClient 52:1cc3c6d07cad 1343 }
AzureIoTClient 52:1cc3c6d07cad 1344
AzureIoTClient 53:1e5a1ca1f274 1345 IOTHUB_CLIENT_RESULT IoTHubClient_SetRetryPolicy(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1346 {
AzureIoTClient 53:1e5a1ca1f274 1347 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1348
AzureIoTClient 53:1e5a1ca1f274 1349 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1350 {
AzureIoTClient 53:1e5a1ca1f274 1351 /* Codes_SRS_IOTHUBCLIENT_25_076: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ] */
AzureIoTClient 53:1e5a1ca1f274 1352 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1353 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 1354 }
AzureIoTClient 53:1e5a1ca1f274 1355 else
AzureIoTClient 53:1e5a1ca1f274 1356 {
AzureIoTClient 53:1e5a1ca1f274 1357 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1358
AzureIoTClient 66:a419827cb051 1359 /* Codes_SRS_IOTHUBCLIENT_25_073: [ `IoTHubClient_SetRetryPolicy` shall start the worker thread if it was not previously started. ] */
AzureIoTClient 66:a419827cb051 1360 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1361 {
AzureIoTClient 66:a419827cb051 1362 /* Codes_SRS_IOTHUBCLIENT_25_075: [ If starting the thread fails, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1363 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1364 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1365 }
AzureIoTClient 53:1e5a1ca1f274 1366 else
AzureIoTClient 53:1e5a1ca1f274 1367 {
AzureIoTClient 66:a419827cb051 1368 /* Codes_SRS_IOTHUBCLIENT_25_079: [ `IoTHubClient_SetRetryPolicy` shall be made thread-safe by using the lock created in `IoTHubClient_Create`.] */
AzureIoTClient 66:a419827cb051 1369 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1370 {
AzureIoTClient 66:a419827cb051 1371 /* Codes_SRS_IOTHUBCLIENT_25_080: [ If acquiring the lock fails, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1372 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1373 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1374 }
AzureIoTClient 53:1e5a1ca1f274 1375 else
AzureIoTClient 53:1e5a1ca1f274 1376 {
AzureIoTClient 53:1e5a1ca1f274 1377 /* Codes_SRS_IOTHUBCLIENT_25_077: [ `IoTHubClient_SetRetryPolicy` shall call `IoTHubClient_LL_SetRetryPolicy`, while passing the `IoTHubClient_LL` handle created by `IoTHubClient_Create` and the parameters `retryPolicy` and `retryTimeoutLimitinSeconds`.]*/
AzureIoTClient 53:1e5a1ca1f274 1378 result = IoTHubClient_LL_SetRetryPolicy(iotHubClientInstance->IoTHubClientLLHandle, retryPolicy, retryTimeoutLimitInSeconds);
AzureIoTClient 66:a419827cb051 1379 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1380 }
AzureIoTClient 53:1e5a1ca1f274 1381
AzureIoTClient 53:1e5a1ca1f274 1382 }
AzureIoTClient 53:1e5a1ca1f274 1383 }
AzureIoTClient 52:1cc3c6d07cad 1384
AzureIoTClient 52:1cc3c6d07cad 1385 return result;
AzureIoTClient 52:1cc3c6d07cad 1386 }
AzureIoTClient 52:1cc3c6d07cad 1387
Azure.IoT.Build 54:6dcad9019a64 1388 IOTHUB_CLIENT_RESULT IoTHubClient_GetRetryPolicy(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY* retryPolicy, size_t* retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1389 {
AzureIoTClient 53:1e5a1ca1f274 1390 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1391
AzureIoTClient 53:1e5a1ca1f274 1392 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1393 {
AzureIoTClient 53:1e5a1ca1f274 1394 /* Codes_SRS_IOTHUBCLIENT_25_092: [ If `iotHubClientHandle` is `NULL`, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1395 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1396 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 1397 }
AzureIoTClient 53:1e5a1ca1f274 1398 else
AzureIoTClient 53:1e5a1ca1f274 1399 {
AzureIoTClient 53:1e5a1ca1f274 1400 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1401
AzureIoTClient 66:a419827cb051 1402 /* Codes_SRS_IOTHUBCLIENT_25_089: [ `IoTHubClient_GetRetryPolicy` shall start the worker thread if it was not previously started.]*/
AzureIoTClient 66:a419827cb051 1403 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1404 {
AzureIoTClient 66:a419827cb051 1405 /* Codes_SRS_IOTHUBCLIENT_25_091: [ If starting the thread fails, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`.]*/
AzureIoTClient 53:1e5a1ca1f274 1406 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1407 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1408 }
AzureIoTClient 53:1e5a1ca1f274 1409 else
AzureIoTClient 53:1e5a1ca1f274 1410 {
AzureIoTClient 66:a419827cb051 1411 /* Codes_SRS_IOTHUBCLIENT_25_095: [ `IoTHubClient_GetRetryPolicy` shall be made thread-safe by using the lock created in `IoTHubClient_Create`. ]*/
AzureIoTClient 66:a419827cb051 1412 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1413 {
AzureIoTClient 66:a419827cb051 1414 /* Codes_SRS_IOTHUBCLIENT_25_096: [ If acquiring the lock fails, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1415 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1416 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1417 }
AzureIoTClient 53:1e5a1ca1f274 1418 else
AzureIoTClient 53:1e5a1ca1f274 1419 {
AzureIoTClient 53:1e5a1ca1f274 1420 /* Codes_SRS_IOTHUBCLIENT_25_093: [ `IoTHubClient_GetRetryPolicy` shall call `IoTHubClient_LL_GetRetryPolicy`, while passing the `IoTHubClient_LL` handle created by `IoTHubClient_Create` and the parameters `connectionStatusCallback` and `userContextCallback`.]*/
AzureIoTClient 53:1e5a1ca1f274 1421 result = IoTHubClient_LL_GetRetryPolicy(iotHubClientInstance->IoTHubClientLLHandle, retryPolicy, retryTimeoutLimitInSeconds);
AzureIoTClient 66:a419827cb051 1422 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1423 }
AzureIoTClient 53:1e5a1ca1f274 1424 }
AzureIoTClient 53:1e5a1ca1f274 1425 }
AzureIoTClient 52:1cc3c6d07cad 1426
AzureIoTClient 52:1cc3c6d07cad 1427 return result;
AzureIoTClient 52:1cc3c6d07cad 1428 }
AzureIoTClient 52:1cc3c6d07cad 1429
AzureIoTClient 16:deba40344375 1430 IOTHUB_CLIENT_RESULT IoTHubClient_GetLastMessageReceiveTime(IOTHUB_CLIENT_HANDLE iotHubClientHandle, time_t* lastMessageReceiveTime)
AzureIoTClient 16:deba40344375 1431 {
AzureIoTClient 16:deba40344375 1432 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1433
AzureIoTClient 16:deba40344375 1434 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 1435 {
AzureIoTClient 16:deba40344375 1436 /* Codes_SRS_IOTHUBCLIENT_01_020: [If iotHubClientHandle is NULL, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 1437 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 1438 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 1439 }
AzureIoTClient 16:deba40344375 1440 else
AzureIoTClient 16:deba40344375 1441 {
AzureIoTClient 16:deba40344375 1442 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1443
AzureIoTClient 16:deba40344375 1444 /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 1445 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 1446 {
AzureIoTClient 16:deba40344375 1447 /* Codes_SRS_IOTHUBCLIENT_01_036: [If acquiring the lock fails, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1448 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 39:2719651a5bee 1449 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 1450 }
AzureIoTClient 16:deba40344375 1451 else
AzureIoTClient 16:deba40344375 1452 {
AzureIoTClient 16:deba40344375 1453 /* Codes_SRS_IOTHUBCLIENT_01_019: [IoTHubClient_GetLastMessageReceiveTime shall call IoTHubClient_LL_GetLastMessageReceiveTime, while passing the IoTHubClient_LL handle created by IoTHubClient_Create and the parameter lastMessageReceiveTime.] */
AzureIoTClient 16:deba40344375 1454 /* Codes_SRS_IOTHUBCLIENT_01_021: [Otherwise, IoTHubClient_GetLastMessageReceiveTime shall return the result of IoTHubClient_LL_GetLastMessageReceiveTime.] */
AzureIoTClient 16:deba40344375 1455 result = IoTHubClient_LL_GetLastMessageReceiveTime(iotHubClientInstance->IoTHubClientLLHandle, lastMessageReceiveTime);
AzureIoTClient 16:deba40344375 1456
AzureIoTClient 16:deba40344375 1457 /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 53:1e5a1ca1f274 1458 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1459 }
AzureIoTClient 16:deba40344375 1460 }
AzureIoTClient 16:deba40344375 1461
AzureIoTClient 16:deba40344375 1462 return result;
AzureIoTClient 16:deba40344375 1463 }
AzureIoTClient 16:deba40344375 1464
AzureIoTClient 16:deba40344375 1465 IOTHUB_CLIENT_RESULT IoTHubClient_SetOption(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const char* optionName, const void* value)
AzureIoTClient 16:deba40344375 1466 {
AzureIoTClient 16:deba40344375 1467 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1468 /*Codes_SRS_IOTHUBCLIENT_02_034: [If parameter iotHubClientHandle is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 42:448eecc3676e 1469 /*Codes_SRS_IOTHUBCLIENT_02_035: [ If parameter optionName is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1470 /*Codes_SRS_IOTHUBCLIENT_02_036: [ If parameter value is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 16:deba40344375 1471 if (
AzureIoTClient 16:deba40344375 1472 (iotHubClientHandle == NULL) ||
AzureIoTClient 16:deba40344375 1473 (optionName == NULL) ||
AzureIoTClient 16:deba40344375 1474 (value == NULL)
AzureIoTClient 16:deba40344375 1475 )
AzureIoTClient 16:deba40344375 1476 {
AzureIoTClient 16:deba40344375 1477 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1478 LogError("invalid arg (NULL)");
AzureIoTClient 16:deba40344375 1479 }
AzureIoTClient 16:deba40344375 1480 else
AzureIoTClient 16:deba40344375 1481 {
AzureIoTClient 16:deba40344375 1482 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1483
AzureIoTClient 40:1a94db9139ea 1484 /* Codes_SRS_IOTHUBCLIENT_01_041: [ IoTHubClient_SetOption shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 40:1a94db9139ea 1485 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 40:1a94db9139ea 1486 {
AzureIoTClient 66:a419827cb051 1487 /* Codes_SRS_IOTHUBCLIENT_01_042: [ If acquiring the lock fails, IoTHubClient_SetOption shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 40:1a94db9139ea 1488 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 40:1a94db9139ea 1489 LogError("Could not acquire lock");
AzureIoTClient 40:1a94db9139ea 1490 }
AzureIoTClient 40:1a94db9139ea 1491 else
AzureIoTClient 16:deba40344375 1492 {
AzureIoTClient 40:1a94db9139ea 1493 /*Codes_SRS_IOTHUBCLIENT_02_038: [If optionName doesn't match one of the options handled by this module then IoTHubClient_SetOption shall call IoTHubClient_LL_SetOption passing the same parameters and return what IoTHubClient_LL_SetOption returns.] */
AzureIoTClient 40:1a94db9139ea 1494 result = IoTHubClient_LL_SetOption(iotHubClientInstance->IoTHubClientLLHandle, optionName, value);
AzureIoTClient 40:1a94db9139ea 1495 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 40:1a94db9139ea 1496 {
AzureIoTClient 40:1a94db9139ea 1497 LogError("IoTHubClient_LL_SetOption failed");
AzureIoTClient 40:1a94db9139ea 1498 }
AzureIoTClient 40:1a94db9139ea 1499
AzureIoTClient 53:1e5a1ca1f274 1500 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1501 }
AzureIoTClient 53:1e5a1ca1f274 1502 }
AzureIoTClient 53:1e5a1ca1f274 1503 return result;
AzureIoTClient 53:1e5a1ca1f274 1504 }
AzureIoTClient 53:1e5a1ca1f274 1505
AzureIoTClient 53:1e5a1ca1f274 1506 IOTHUB_CLIENT_RESULT IoTHubClient_SetDeviceTwinCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1507 {
AzureIoTClient 53:1e5a1ca1f274 1508 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1509
AzureIoTClient 53:1e5a1ca1f274 1510 /*Codes_SRS_IOTHUBCLIENT_10_001: [** `IoTHubClient_SetDeviceTwinCallback` shall fail and return `IOTHUB_CLIENT_INVALID_ARG` if parameter `iotHubClientHandle` is `NULL`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1511 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1512 {
AzureIoTClient 53:1e5a1ca1f274 1513 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1514 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 1515 }
AzureIoTClient 53:1e5a1ca1f274 1516 else
AzureIoTClient 53:1e5a1ca1f274 1517 {
AzureIoTClient 53:1e5a1ca1f274 1518 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1519
AzureIoTClient 66:a419827cb051 1520 /*Codes_SRS_IOTHUBCLIENT_10_003: [** If the transport connection is shared, the thread shall be started by calling `IoTHubTransport_StartWorkerThread`. ]*/
AzureIoTClient 66:a419827cb051 1521 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1522 {
AzureIoTClient 66:a419827cb051 1523 /*Codes_SRS_IOTHUBCLIENT_10_004: [** If starting the thread fails, `IoTHubClient_SetDeviceTwinCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1524 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1525 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1526 }
AzureIoTClient 53:1e5a1ca1f274 1527 else
AzureIoTClient 53:1e5a1ca1f274 1528 {
AzureIoTClient 66:a419827cb051 1529 /*Codes_SRS_IOTHUBCLIENT_10_020: [** `IoTHubClient_SetDeviceTwinCallback` shall be made thread - safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1530 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1531 {
AzureIoTClient 66:a419827cb051 1532 /*Codes_SRS_IOTHUBCLIENT_10_002: [** If acquiring the lock fails, `IoTHubClient_SetDeviceTwinCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1533 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1534 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1535 }
AzureIoTClient 53:1e5a1ca1f274 1536 else
AzureIoTClient 53:1e5a1ca1f274 1537 {
AzureIoTClient 67:ecebc2a41159 1538 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 67:ecebc2a41159 1539 {
AzureIoTClient 67:ecebc2a41159 1540 iotHubClientInstance->desired_state_callback = deviceTwinCallback;
AzureIoTClient 67:ecebc2a41159 1541 }
AzureIoTClient 67:ecebc2a41159 1542
Azure.IoT.Build 54:6dcad9019a64 1543 if (iotHubClientInstance->created_with_transport_handle != 0 || deviceTwinCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 1544 {
Azure.IoT.Build 54:6dcad9019a64 1545 /*Codes_SRS_IOTHUBCLIENT_10_005: [** `IoTHubClient_LL_SetDeviceTwinCallback` shall call `IoTHubClient_LL_SetDeviceTwinCallback`, while passing the `IoTHubClient_LL handle` created by `IoTHubClient_LL_Create` along with the parameters `reportedStateCallback` and `userContextCallback`. ]*/
Azure.IoT.Build 54:6dcad9019a64 1546 result = IoTHubClient_LL_SetDeviceTwinCallback(iotHubClientInstance->IoTHubClientLLHandle, deviceTwinCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 1547 }
Azure.IoT.Build 54:6dcad9019a64 1548 else
AzureIoTClient 53:1e5a1ca1f274 1549 {
Azure.IoT.Build 54:6dcad9019a64 1550 if (iotHubClientInstance->devicetwin_user_context != NULL)
Azure.IoT.Build 54:6dcad9019a64 1551 {
Azure.IoT.Build 54:6dcad9019a64 1552 free(iotHubClientInstance->devicetwin_user_context);
Azure.IoT.Build 54:6dcad9019a64 1553 }
Azure.IoT.Build 54:6dcad9019a64 1554
Azure.IoT.Build 54:6dcad9019a64 1555 /*Codes_SRS_IOTHUBCLIENT_07_002: [ IoTHubClient_SetDeviceTwinCallback shall allocate a IOTHUB_QUEUE_CONTEXT object to be sent to the IoTHubClient_LL_SetDeviceTwinCallback function as a user context. ]*/
AzureIoTClient 55:59b527ab3452 1556 iotHubClientInstance->devicetwin_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
Azure.IoT.Build 54:6dcad9019a64 1557 if (iotHubClientInstance->devicetwin_user_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1558 {
Azure.IoT.Build 54:6dcad9019a64 1559 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1560 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1561 }
Azure.IoT.Build 54:6dcad9019a64 1562 else
Azure.IoT.Build 54:6dcad9019a64 1563 {
Azure.IoT.Build 54:6dcad9019a64 1564 /*Codes_SRS_IOTHUBCLIENT_10_005: [** `IoTHubClient_LL_SetDeviceTwinCallback` shall call `IoTHubClient_LL_SetDeviceTwinCallback`, while passing the `IoTHubClient_LL handle` created by `IoTHubClient_LL_Create` along with the parameters `iothub_ll_device_twin_callback` and IOTHUB_QUEUE_CONTEXT variable. ]*/
AzureIoTClient 55:59b527ab3452 1565 iotHubClientInstance->devicetwin_user_context->iotHubClientHandle = iotHubClientInstance;
AzureIoTClient 55:59b527ab3452 1566 iotHubClientInstance->devicetwin_user_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1567 result = IoTHubClient_LL_SetDeviceTwinCallback(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_device_twin_callback, iotHubClientInstance->devicetwin_user_context);
Azure.IoT.Build 54:6dcad9019a64 1568 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1569 {
Azure.IoT.Build 54:6dcad9019a64 1570 LogError("IoTHubClient_LL_SetDeviceTwinCallback failed");
Azure.IoT.Build 54:6dcad9019a64 1571 free(iotHubClientInstance->devicetwin_user_context);
AzureIoTClient 61:8b85a4e797cf 1572 iotHubClientInstance->devicetwin_user_context = NULL;
Azure.IoT.Build 54:6dcad9019a64 1573 }
Azure.IoT.Build 54:6dcad9019a64 1574 }
AzureIoTClient 53:1e5a1ca1f274 1575 }
AzureIoTClient 66:a419827cb051 1576
AzureIoTClient 66:a419827cb051 1577 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1578 }
AzureIoTClient 16:deba40344375 1579 }
AzureIoTClient 16:deba40344375 1580 }
AzureIoTClient 16:deba40344375 1581 return result;
AzureIoTClient 16:deba40344375 1582 }
AzureIoTClient 42:448eecc3676e 1583
AzureIoTClient 53:1e5a1ca1f274 1584 IOTHUB_CLIENT_RESULT IoTHubClient_SendReportedState(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const unsigned char* reportedState, size_t size, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK reportedStateCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1585 {
AzureIoTClient 53:1e5a1ca1f274 1586 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1587
AzureIoTClient 53:1e5a1ca1f274 1588 /*Codes_SRS_IOTHUBCLIENT_10_013: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1589 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1590 {
AzureIoTClient 53:1e5a1ca1f274 1591 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1592 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 1593 }
AzureIoTClient 53:1e5a1ca1f274 1594 else
AzureIoTClient 53:1e5a1ca1f274 1595 {
AzureIoTClient 53:1e5a1ca1f274 1596 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1597
AzureIoTClient 66:a419827cb051 1598 /*Codes_SRS_IOTHUBCLIENT_10_015: [** If the transport connection is shared, the thread shall be started by calling `IoTHubTransport_StartWorkerThread`. ]*/
AzureIoTClient 66:a419827cb051 1599 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1600 {
AzureIoTClient 66:a419827cb051 1601 /*Codes_SRS_IOTHUBCLIENT_10_016: [** If starting the thread fails, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1602 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1603 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1604 }
AzureIoTClient 53:1e5a1ca1f274 1605 else
AzureIoTClient 53:1e5a1ca1f274 1606 {
AzureIoTClient 66:a419827cb051 1607 /*Codes_SRS_IOTHUBCLIENT_10_021: [** `IoTHubClient_SendReportedState` shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1608 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1609 {
AzureIoTClient 66:a419827cb051 1610 /*Codes_SRS_IOTHUBCLIENT_10_014: [** If acquiring the lock fails, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1611 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1612 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1613 }
AzureIoTClient 53:1e5a1ca1f274 1614 else
AzureIoTClient 53:1e5a1ca1f274 1615 {
AzureIoTClient 80:db5f5237bc95 1616 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 80:db5f5237bc95 1617 {
AzureIoTClient 80:db5f5237bc95 1618 iotHubClientInstance->reported_state_callback = reportedStateCallback;
AzureIoTClient 80:db5f5237bc95 1619 }
AzureIoTClient 80:db5f5237bc95 1620
Azure.IoT.Build 54:6dcad9019a64 1621 if (iotHubClientInstance->created_with_transport_handle != 0 || reportedStateCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 1622 {
Azure.IoT.Build 54:6dcad9019a64 1623 /*Codes_SRS_IOTHUBCLIENT_10_017: [** `IoTHubClient_SendReportedState` shall call `IoTHubClient_LL_SendReportedState`, while passing the `IoTHubClient_LL handle` created by `IoTHubClient_LL_Create` along with the parameters `reportedState`, `size`, `reportedStateCallback`, and `userContextCallback`. ]*/
Azure.IoT.Build 54:6dcad9019a64 1624 /*Codes_SRS_IOTHUBCLIENT_10_018: [** When `IoTHubClient_LL_SendReportedState` is called, `IoTHubClient_SendReportedState` shall return the result of `IoTHubClient_LL_SendReportedState`. **]*/
Azure.IoT.Build 54:6dcad9019a64 1625 result = IoTHubClient_LL_SendReportedState(iotHubClientInstance->IoTHubClientLLHandle, reportedState, size, reportedStateCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 1626 }
Azure.IoT.Build 54:6dcad9019a64 1627 else
AzureIoTClient 53:1e5a1ca1f274 1628 {
Azure.IoT.Build 54:6dcad9019a64 1629 /* Codes_SRS_IOTHUBCLIENT_07_003: [ IoTHubClient_SendReportedState shall allocate a IOTHUB_QUEUE_CONTEXT object to be sent to the IoTHubClient_LL_SendReportedState function as a user context. ] */
Azure.IoT.Build 54:6dcad9019a64 1630 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
Azure.IoT.Build 54:6dcad9019a64 1631 if (queue_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1632 {
Azure.IoT.Build 54:6dcad9019a64 1633 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1634 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1635 }
Azure.IoT.Build 54:6dcad9019a64 1636 else
Azure.IoT.Build 54:6dcad9019a64 1637 {
Azure.IoT.Build 54:6dcad9019a64 1638 queue_context->iotHubClientHandle = iotHubClientInstance;
Azure.IoT.Build 54:6dcad9019a64 1639 queue_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1640 /*Codes_SRS_IOTHUBCLIENT_10_017: [** `IoTHubClient_SendReportedState` shall call `IoTHubClient_LL_SendReportedState`, while passing the `IoTHubClient_LL handle` created by `IoTHubClient_LL_Create` along with the parameters `reportedState`, `size`, `iothub_ll_reported_state_callback` and IOTHUB_QUEUE_CONTEXT variable. ]*/
Azure.IoT.Build 54:6dcad9019a64 1641 /*Codes_SRS_IOTHUBCLIENT_10_018: [** When `IoTHubClient_LL_SendReportedState` is called, `IoTHubClient_SendReportedState` shall return the result of `IoTHubClient_LL_SendReportedState`. **]*/
Azure.IoT.Build 54:6dcad9019a64 1642 result = IoTHubClient_LL_SendReportedState(iotHubClientInstance->IoTHubClientLLHandle, reportedState, size, iothub_ll_reported_state_callback, queue_context);
Azure.IoT.Build 54:6dcad9019a64 1643 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1644 {
Azure.IoT.Build 54:6dcad9019a64 1645 LogError("IoTHubClient_LL_SendReportedState failed");
Azure.IoT.Build 54:6dcad9019a64 1646 free(queue_context);
Azure.IoT.Build 54:6dcad9019a64 1647 }
Azure.IoT.Build 54:6dcad9019a64 1648 }
AzureIoTClient 53:1e5a1ca1f274 1649 }
AzureIoTClient 66:a419827cb051 1650
AzureIoTClient 66:a419827cb051 1651 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1652 }
AzureIoTClient 53:1e5a1ca1f274 1653 }
AzureIoTClient 53:1e5a1ca1f274 1654 }
AzureIoTClient 53:1e5a1ca1f274 1655 return result;
AzureIoTClient 53:1e5a1ca1f274 1656 }
AzureIoTClient 53:1e5a1ca1f274 1657
AzureIoTClient 53:1e5a1ca1f274 1658 IOTHUB_CLIENT_RESULT IoTHubClient_SetDeviceMethodCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC deviceMethodCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1659 {
AzureIoTClient 53:1e5a1ca1f274 1660 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1661
AzureIoTClient 53:1e5a1ca1f274 1662 /*Codes_SRS_IOTHUBCLIENT_12_012: [ If iotHubClientHandle is NULL, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 53:1e5a1ca1f274 1663 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1664 {
AzureIoTClient 53:1e5a1ca1f274 1665 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1666 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 1667 }
AzureIoTClient 53:1e5a1ca1f274 1668 else
AzureIoTClient 53:1e5a1ca1f274 1669 {
AzureIoTClient 53:1e5a1ca1f274 1670 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1671
AzureIoTClient 66:a419827cb051 1672 /*Codes_SRS_IOTHUBCLIENT_12_014: [ If the transport handle is null and the worker thread is not initialized, the thread shall be started by calling IoTHubTransport_StartWorkerThread. ]*/
AzureIoTClient 66:a419827cb051 1673 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1674 {
AzureIoTClient 66:a419827cb051 1675 /*Codes_SRS_IOTHUBCLIENT_12_015: [ If starting the thread fails, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 53:1e5a1ca1f274 1676 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1677 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1678 }
AzureIoTClient 53:1e5a1ca1f274 1679 else
AzureIoTClient 53:1e5a1ca1f274 1680 {
AzureIoTClient 66:a419827cb051 1681 /*Codes_SRS_IOTHUBCLIENT_12_018: [ IoTHubClient_SetDeviceMethodCallback shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1682 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1683 {
AzureIoTClient 66:a419827cb051 1684 /*Codes_SRS_IOTHUBCLIENT_12_013: [ If acquiring the lock fails, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 53:1e5a1ca1f274 1685 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1686 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1687 }
AzureIoTClient 53:1e5a1ca1f274 1688 else
AzureIoTClient 53:1e5a1ca1f274 1689 {
AzureIoTClient 80:db5f5237bc95 1690 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 80:db5f5237bc95 1691 {
AzureIoTClient 80:db5f5237bc95 1692 iotHubClientInstance->device_method_callback = deviceMethodCallback;
AzureIoTClient 80:db5f5237bc95 1693 }
AzureIoTClient 80:db5f5237bc95 1694
AzureIoTClient 62:5a4cdacf5090 1695 if (iotHubClientInstance->method_user_context)
AzureIoTClient 62:5a4cdacf5090 1696 {
AzureIoTClient 62:5a4cdacf5090 1697 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1698 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 62:5a4cdacf5090 1699 }
AzureIoTClient 62:5a4cdacf5090 1700 if (deviceMethodCallback == NULL)
AzureIoTClient 62:5a4cdacf5090 1701 {
AzureIoTClient 62:5a4cdacf5090 1702 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, NULL, NULL);
AzureIoTClient 62:5a4cdacf5090 1703 }
AzureIoTClient 62:5a4cdacf5090 1704 else
AzureIoTClient 53:1e5a1ca1f274 1705 {
AzureIoTClient 62:5a4cdacf5090 1706 iotHubClientInstance->method_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 62:5a4cdacf5090 1707 if (iotHubClientInstance->method_user_context == NULL)
AzureIoTClient 62:5a4cdacf5090 1708 {
AzureIoTClient 62:5a4cdacf5090 1709 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1710 LogError("Failed allocating QUEUE_CONTEXT");
AzureIoTClient 62:5a4cdacf5090 1711 }
AzureIoTClient 62:5a4cdacf5090 1712 else
AzureIoTClient 62:5a4cdacf5090 1713 {
AzureIoTClient 62:5a4cdacf5090 1714 iotHubClientInstance->method_user_context->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 62:5a4cdacf5090 1715 iotHubClientInstance->method_user_context->userContextCallback = userContextCallback;
AzureIoTClient 62:5a4cdacf5090 1716
AzureIoTClient 62:5a4cdacf5090 1717 /*Codes_SRS_IOTHUBCLIENT_12_016: [ IoTHubClient_SetDeviceMethodCallback shall call IoTHubClient_LL_SetDeviceMethodCallback, while passing the IoTHubClient_LL_handle created by IoTHubClient_LL_Create along with the parameters deviceMethodCallback and userContextCallback. ]*/
AzureIoTClient 62:5a4cdacf5090 1718 /*Codes_SRS_IOTHUBCLIENT_12_017: [ When IoTHubClient_LL_SetDeviceMethodCallback is called, IoTHubClient_SetDeviceMethodCallback shall return the result of IoTHubClient_LL_SetDeviceMethodCallback. ]*/
AzureIoTClient 62:5a4cdacf5090 1719 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_device_method_callback, iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1720 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 62:5a4cdacf5090 1721 {
AzureIoTClient 62:5a4cdacf5090 1722 LogError("IoTHubClient_LL_SetDeviceMethodCallback_Ex failed");
AzureIoTClient 62:5a4cdacf5090 1723 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1724 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 62:5a4cdacf5090 1725 }
AzureIoTClient 66:a419827cb051 1726 else
AzureIoTClient 66:a419827cb051 1727 {
AzureIoTClient 66:a419827cb051 1728 iotHubClientInstance->device_method_callback = deviceMethodCallback;
AzureIoTClient 66:a419827cb051 1729 }
AzureIoTClient 62:5a4cdacf5090 1730 }
AzureIoTClient 53:1e5a1ca1f274 1731 }
AzureIoTClient 66:a419827cb051 1732
AzureIoTClient 66:a419827cb051 1733 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1734 }
AzureIoTClient 53:1e5a1ca1f274 1735
AzureIoTClient 53:1e5a1ca1f274 1736 }
AzureIoTClient 53:1e5a1ca1f274 1737 }
AzureIoTClient 53:1e5a1ca1f274 1738 return result;
AzureIoTClient 53:1e5a1ca1f274 1739 }
AzureIoTClient 53:1e5a1ca1f274 1740
AzureIoTClient 55:59b527ab3452 1741 IOTHUB_CLIENT_RESULT IoTHubClient_SetDeviceMethodCallback_Ex(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK inboundDeviceMethodCallback, void* userContextCallback)
AzureIoTClient 55:59b527ab3452 1742 {
AzureIoTClient 55:59b527ab3452 1743 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1744
AzureIoTClient 55:59b527ab3452 1745 /*Codes_SRS_IOTHUBCLIENT_07_001: [ If iotHubClientHandle is NULL, IoTHubClient_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 55:59b527ab3452 1746 if (iotHubClientHandle == NULL)
AzureIoTClient 55:59b527ab3452 1747 {
AzureIoTClient 55:59b527ab3452 1748 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1749 LogError("invalid arg (NULL)");
AzureIoTClient 55:59b527ab3452 1750 }
AzureIoTClient 55:59b527ab3452 1751 else
AzureIoTClient 55:59b527ab3452 1752 {
AzureIoTClient 55:59b527ab3452 1753 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1754
AzureIoTClient 66:a419827cb051 1755 /*Codes_SRS_IOTHUBCLIENT_07_003: [ If the transport handle is NULL and the worker thread is not initialized, the thread shall be started by calling IoTHubTransport_StartWorkerThread. ]*/
AzureIoTClient 66:a419827cb051 1756 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 55:59b527ab3452 1757 {
AzureIoTClient 66:a419827cb051 1758 /*Codes_SRS_IOTHUBCLIENT_07_004: [ If starting the thread fails, IoTHubClient_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 55:59b527ab3452 1759 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1760 LogError("Could not start worker thread");
AzureIoTClient 55:59b527ab3452 1761 }
AzureIoTClient 55:59b527ab3452 1762 else
AzureIoTClient 55:59b527ab3452 1763 {
AzureIoTClient 66:a419827cb051 1764 /*Codes_SRS_IOTHUBCLIENT_07_007: [ IoTHubClient_SetDeviceMethodCallback_Ex shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1765 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 55:59b527ab3452 1766 {
AzureIoTClient 66:a419827cb051 1767 /*Codes_SRS_IOTHUBCLIENT_07_002: [ If acquiring the lock fails, IoTHubClient_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 55:59b527ab3452 1768 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1769 LogError("Could not acquire lock");
AzureIoTClient 55:59b527ab3452 1770 }
AzureIoTClient 55:59b527ab3452 1771 else
AzureIoTClient 55:59b527ab3452 1772 {
AzureIoTClient 80:db5f5237bc95 1773 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 80:db5f5237bc95 1774 {
AzureIoTClient 80:db5f5237bc95 1775 iotHubClientInstance->inbound_device_method_callback = inboundDeviceMethodCallback;
AzureIoTClient 80:db5f5237bc95 1776 }
AzureIoTClient 80:db5f5237bc95 1777
AzureIoTClient 62:5a4cdacf5090 1778 if (iotHubClientInstance->method_user_context)
AzureIoTClient 62:5a4cdacf5090 1779 {
AzureIoTClient 62:5a4cdacf5090 1780 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1781 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 62:5a4cdacf5090 1782 }
AzureIoTClient 62:5a4cdacf5090 1783 if (inboundDeviceMethodCallback == NULL)
AzureIoTClient 55:59b527ab3452 1784 {
AzureIoTClient 55:59b527ab3452 1785 /* Codes_SRS_IOTHUBCLIENT_07_008: [ If inboundDeviceMethodCallback is NULL, IoTHubClient_SetDeviceMethodCallback_Ex shall call IoTHubClient_LL_SetDeviceMethodCallback_Ex, passing NULL for the iothub_ll_inbound_device_method_callback. ] */
AzureIoTClient 55:59b527ab3452 1786 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, NULL, NULL);
AzureIoTClient 55:59b527ab3452 1787 }
AzureIoTClient 55:59b527ab3452 1788 else
AzureIoTClient 55:59b527ab3452 1789 {
AzureIoTClient 62:5a4cdacf5090 1790 iotHubClientInstance->method_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 62:5a4cdacf5090 1791 if (iotHubClientInstance->method_user_context == NULL)
AzureIoTClient 55:59b527ab3452 1792 {
AzureIoTClient 55:59b527ab3452 1793 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1794 LogError("Failed allocating QUEUE_CONTEXT");
AzureIoTClient 55:59b527ab3452 1795 }
AzureIoTClient 55:59b527ab3452 1796 else
AzureIoTClient 55:59b527ab3452 1797 {
AzureIoTClient 55:59b527ab3452 1798 /*Codes_SRS_IOTHUBCLIENT_07_005: [ IoTHubClient_SetDeviceMethodCallback_Ex shall call IoTHubClient_LL_SetDeviceMethodCallback_Ex, while passing the IoTHubClient_LL_handle created by IoTHubClient_LL_Create along with the parameters iothub_ll_inbound_device_method_callback and IOTHUB_QUEUE_CONTEXT. ]*/
AzureIoTClient 62:5a4cdacf5090 1799 iotHubClientInstance->method_user_context->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 62:5a4cdacf5090 1800 iotHubClientInstance->method_user_context->userContextCallback = userContextCallback;
AzureIoTClient 62:5a4cdacf5090 1801
AzureIoTClient 55:59b527ab3452 1802 /* Codes_SRS_IOTHUBCLIENT_07_006: [ When IoTHubClient_LL_SetDeviceMethodCallback_Ex is called, IoTHubClient_SetDeviceMethodCallback_Ex shall return the result of IoTHubClient_LL_SetDeviceMethodCallback_Ex. ] */
AzureIoTClient 62:5a4cdacf5090 1803 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_inbound_device_method_callback, iotHubClientInstance->method_user_context);
AzureIoTClient 55:59b527ab3452 1804 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 55:59b527ab3452 1805 {
AzureIoTClient 62:5a4cdacf5090 1806 LogError("IoTHubClient_LL_SetDeviceMethodCallback_Ex failed");
AzureIoTClient 62:5a4cdacf5090 1807 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1808 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 55:59b527ab3452 1809 }
AzureIoTClient 66:a419827cb051 1810 else
AzureIoTClient 66:a419827cb051 1811 {
AzureIoTClient 66:a419827cb051 1812 iotHubClientInstance->inbound_device_method_callback = inboundDeviceMethodCallback;
AzureIoTClient 66:a419827cb051 1813 }
AzureIoTClient 55:59b527ab3452 1814 }
AzureIoTClient 55:59b527ab3452 1815 }
AzureIoTClient 66:a419827cb051 1816
AzureIoTClient 66:a419827cb051 1817 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 55:59b527ab3452 1818 }
AzureIoTClient 55:59b527ab3452 1819 }
AzureIoTClient 55:59b527ab3452 1820 }
AzureIoTClient 55:59b527ab3452 1821 return result;
AzureIoTClient 55:59b527ab3452 1822 }
AzureIoTClient 55:59b527ab3452 1823
AzureIoTClient 55:59b527ab3452 1824 IOTHUB_CLIENT_RESULT IoTHubClient_DeviceMethodResponse(IOTHUB_CLIENT_HANDLE iotHubClientHandle, METHOD_HANDLE methodId, const unsigned char* response, size_t respSize, int statusCode)
AzureIoTClient 55:59b527ab3452 1825 {
AzureIoTClient 55:59b527ab3452 1826 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1827
AzureIoTClient 55:59b527ab3452 1828 /*Codes_SRS_IOTHUBCLIENT_12_012: [ If iotHubClientHandle is NULL, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 55:59b527ab3452 1829 if (iotHubClientHandle == NULL)
AzureIoTClient 55:59b527ab3452 1830 {
AzureIoTClient 55:59b527ab3452 1831 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1832 LogError("invalid arg (NULL)");
AzureIoTClient 55:59b527ab3452 1833 }
AzureIoTClient 55:59b527ab3452 1834 else
AzureIoTClient 55:59b527ab3452 1835 {
AzureIoTClient 55:59b527ab3452 1836 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1837
AzureIoTClient 55:59b527ab3452 1838 /*Codes_SRS_IOTHUBCLIENT_12_018: [ IoTHubClient_SetDeviceMethodCallback shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 55:59b527ab3452 1839 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 55:59b527ab3452 1840 {
AzureIoTClient 55:59b527ab3452 1841 /*Codes_SRS_IOTHUBCLIENT_12_013: [ If acquiring the lock fails, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 55:59b527ab3452 1842 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1843 LogError("Could not acquire lock");
AzureIoTClient 55:59b527ab3452 1844 }
AzureIoTClient 55:59b527ab3452 1845 else
AzureIoTClient 55:59b527ab3452 1846 {
AzureIoTClient 55:59b527ab3452 1847 result = IoTHubClient_LL_DeviceMethodResponse(iotHubClientInstance->IoTHubClientLLHandle, methodId, response, respSize, statusCode);
AzureIoTClient 55:59b527ab3452 1848 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 55:59b527ab3452 1849 {
AzureIoTClient 55:59b527ab3452 1850 LogError("IoTHubClient_LL_DeviceMethodResponse failed");
AzureIoTClient 55:59b527ab3452 1851 }
AzureIoTClient 55:59b527ab3452 1852 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 55:59b527ab3452 1853 }
AzureIoTClient 55:59b527ab3452 1854 }
AzureIoTClient 55:59b527ab3452 1855 return result;
AzureIoTClient 55:59b527ab3452 1856 }
AzureIoTClient 53:1e5a1ca1f274 1857
AzureIoTClient 44:33dd78697616 1858 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 1859 static int uploadingThread(void *data)
AzureIoTClient 42:448eecc3676e 1860 {
AzureIoTClient 42:448eecc3676e 1861 UPLOADTOBLOB_SAVED_DATA* savedData = (UPLOADTOBLOB_SAVED_DATA*)data;
AzureIoTClient 42:448eecc3676e 1862
AzureIoTClient 62:5a4cdacf5090 1863 if (Lock(savedData->iotHubClientHandle->LockHandle) == LOCK_OK)
AzureIoTClient 42:448eecc3676e 1864 {
AzureIoTClient 62:5a4cdacf5090 1865 IOTHUB_CLIENT_FILE_UPLOAD_RESULT upload_result;
AzureIoTClient 62:5a4cdacf5090 1866 /*it so happens that IoTHubClient_LL_UploadToBlob is thread-safe because there's no saved state in the handle and there are no globals, so no need to protect it*/
AzureIoTClient 62:5a4cdacf5090 1867 /*not having it protected means multiple simultaneous uploads can happen*/
AzureIoTClient 62:5a4cdacf5090 1868 /*Codes_SRS_IOTHUBCLIENT_02_054: [ The thread shall call IoTHubClient_LL_UploadToBlob passing the information packed in the structure. ]*/
AzureIoTClient 62:5a4cdacf5090 1869 if (IoTHubClient_LL_UploadToBlob(savedData->iotHubClientHandle->IoTHubClientLLHandle, savedData->destinationFileName, savedData->source, savedData->size) == IOTHUB_CLIENT_OK)
AzureIoTClient 62:5a4cdacf5090 1870 {
AzureIoTClient 62:5a4cdacf5090 1871 upload_result = FILE_UPLOAD_OK;
AzureIoTClient 62:5a4cdacf5090 1872 }
AzureIoTClient 62:5a4cdacf5090 1873 else
AzureIoTClient 62:5a4cdacf5090 1874 {
AzureIoTClient 62:5a4cdacf5090 1875 LogError("unable to IoTHubClient_LL_UploadToBlob");
AzureIoTClient 62:5a4cdacf5090 1876 upload_result = FILE_UPLOAD_ERROR;
AzureIoTClient 62:5a4cdacf5090 1877 }
AzureIoTClient 62:5a4cdacf5090 1878 (void)Unlock(savedData->iotHubClientHandle->LockHandle);
AzureIoTClient 62:5a4cdacf5090 1879
AzureIoTClient 42:448eecc3676e 1880 if (savedData->iotHubClientFileUploadCallback != NULL)
AzureIoTClient 42:448eecc3676e 1881 {
AzureIoTClient 42:448eecc3676e 1882 /*Codes_SRS_IOTHUBCLIENT_02_055: [ If IoTHubClient_LL_UploadToBlob fails then the thread shall call iotHubClientFileUploadCallbackInternal passing as result FILE_UPLOAD_ERROR and as context the structure from SRS IOTHUBCLIENT 02 051. ]*/
AzureIoTClient 62:5a4cdacf5090 1883 savedData->iotHubClientFileUploadCallback(upload_result, savedData->context);
AzureIoTClient 42:448eecc3676e 1884 }
AzureIoTClient 42:448eecc3676e 1885 }
AzureIoTClient 42:448eecc3676e 1886 else
AzureIoTClient 42:448eecc3676e 1887 {
AzureIoTClient 62:5a4cdacf5090 1888 LogError("Lock failed");
AzureIoTClient 42:448eecc3676e 1889 }
AzureIoTClient 42:448eecc3676e 1890
AzureIoTClient 42:448eecc3676e 1891 /*Codes_SRS_IOTHUBCLIENT_02_071: [ The thread shall mark itself as disposable. ]*/
AzureIoTClient 42:448eecc3676e 1892 if (Lock(savedData->lockGarbage) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 1893 {
AzureIoTClient 42:448eecc3676e 1894 LogError("unable to Lock - trying anyway");
AzureIoTClient 42:448eecc3676e 1895 savedData->canBeGarbageCollected = 1;
AzureIoTClient 42:448eecc3676e 1896 }
AzureIoTClient 42:448eecc3676e 1897 else
AzureIoTClient 42:448eecc3676e 1898 {
AzureIoTClient 42:448eecc3676e 1899 savedData->canBeGarbageCollected = 1;
AzureIoTClient 42:448eecc3676e 1900
AzureIoTClient 42:448eecc3676e 1901 if (Unlock(savedData->lockGarbage) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 1902 {
AzureIoTClient 42:448eecc3676e 1903 LogError("unable to Unlock after locking");
AzureIoTClient 42:448eecc3676e 1904 }
AzureIoTClient 42:448eecc3676e 1905 }
AzureIoTClient 71:0d498da5ece1 1906
AzureIoTClient 71:0d498da5ece1 1907 ThreadAPI_Exit(0);
AzureIoTClient 42:448eecc3676e 1908 return 0;
AzureIoTClient 42:448eecc3676e 1909 }
AzureIoTClient 43:038d8511e817 1910 #endif
AzureIoTClient 42:448eecc3676e 1911
AzureIoTClient 44:33dd78697616 1912 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 1913 IOTHUB_CLIENT_RESULT IoTHubClient_UploadToBlobAsync(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const char* destinationFileName, const unsigned char* source, size_t size, IOTHUB_CLIENT_FILE_UPLOAD_CALLBACK iotHubClientFileUploadCallback, void* context)
AzureIoTClient 42:448eecc3676e 1914 {
AzureIoTClient 42:448eecc3676e 1915 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1916 /*Codes_SRS_IOTHUBCLIENT_02_047: [ If iotHubClientHandle is NULL then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1917 /*Codes_SRS_IOTHUBCLIENT_02_048: [ If destinationFileName is NULL then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1918 /*Codes_SRS_IOTHUBCLIENT_02_049: [ If source is NULL and size is greated than 0 then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1919 if (
AzureIoTClient 42:448eecc3676e 1920 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 1921 (destinationFileName == NULL) ||
AzureIoTClient 42:448eecc3676e 1922 ((source == NULL) && (size > 0))
AzureIoTClient 42:448eecc3676e 1923 )
AzureIoTClient 42:448eecc3676e 1924 {
AzureIoTClient 42:448eecc3676e 1925 LogError("invalid parameters IOTHUB_CLIENT_HANDLE iotHubClientHandle = %p , const char* destinationFileName = %s, const unsigned char* source= %p, size_t size = %zu, IOTHUB_CLIENT_FILE_UPLOAD_CALLBACK iotHubClientFileUploadCallback = %p, void* context = %p",
AzureIoTClient 42:448eecc3676e 1926 iotHubClientHandle,
AzureIoTClient 42:448eecc3676e 1927 destinationFileName,
AzureIoTClient 42:448eecc3676e 1928 source,
AzureIoTClient 42:448eecc3676e 1929 size,
AzureIoTClient 42:448eecc3676e 1930 iotHubClientFileUploadCallback,
AzureIoTClient 42:448eecc3676e 1931 context
AzureIoTClient 42:448eecc3676e 1932 );
AzureIoTClient 42:448eecc3676e 1933 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 1934 }
AzureIoTClient 42:448eecc3676e 1935 else
AzureIoTClient 42:448eecc3676e 1936 {
AzureIoTClient 42:448eecc3676e 1937 /*Codes_SRS_IOTHUBCLIENT_02_051: [IoTHubClient_UploadToBlobAsync shall copy the souce, size, iotHubClientFileUploadCallback, context into a structure.]*/
AzureIoTClient 42:448eecc3676e 1938 UPLOADTOBLOB_SAVED_DATA *savedData = (UPLOADTOBLOB_SAVED_DATA *)malloc(sizeof(UPLOADTOBLOB_SAVED_DATA));
AzureIoTClient 42:448eecc3676e 1939 if (savedData == NULL)
AzureIoTClient 42:448eecc3676e 1940 {
AzureIoTClient 42:448eecc3676e 1941 /*Codes_SRS_IOTHUBCLIENT_02_053: [ If copying to the structure or spawning the thread fails, then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 1942 LogError("unable to malloc - oom");
AzureIoTClient 42:448eecc3676e 1943 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1944 }
AzureIoTClient 42:448eecc3676e 1945 else
AzureIoTClient 42:448eecc3676e 1946 {
AzureIoTClient 42:448eecc3676e 1947 if (mallocAndStrcpy_s((char**)&savedData->destinationFileName, destinationFileName) != 0)
AzureIoTClient 42:448eecc3676e 1948 {
AzureIoTClient 42:448eecc3676e 1949 /*Codes_SRS_IOTHUBCLIENT_02_053: [ If copying to the structure or spawning the thread fails, then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 1950 LogError("unable to mallocAndStrcpy_s");
AzureIoTClient 42:448eecc3676e 1951 free(savedData);
AzureIoTClient 42:448eecc3676e 1952 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1953 }
AzureIoTClient 42:448eecc3676e 1954 else
AzureIoTClient 42:448eecc3676e 1955 {
AzureIoTClient 42:448eecc3676e 1956 savedData->size = size;
AzureIoTClient 42:448eecc3676e 1957 int sourceCloned;
AzureIoTClient 42:448eecc3676e 1958 if (size == 0)
AzureIoTClient 42:448eecc3676e 1959 {
AzureIoTClient 42:448eecc3676e 1960 savedData->source = NULL;
AzureIoTClient 42:448eecc3676e 1961 sourceCloned = 1;
AzureIoTClient 42:448eecc3676e 1962 }
AzureIoTClient 42:448eecc3676e 1963 else
AzureIoTClient 42:448eecc3676e 1964 {
AzureIoTClient 42:448eecc3676e 1965 savedData->source = (unsigned char*)malloc(size);
AzureIoTClient 42:448eecc3676e 1966 if (savedData->source == NULL)
AzureIoTClient 42:448eecc3676e 1967 {
AzureIoTClient 42:448eecc3676e 1968 /*Codes_SRS_IOTHUBCLIENT_02_053: [ If copying to the structure or spawning the thread fails, then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 1969 LogError("unable to malloc - oom");
AzureIoTClient 42:448eecc3676e 1970 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1971 free(savedData);
AzureIoTClient 42:448eecc3676e 1972 sourceCloned = 0;
AzureIoTClient 42:448eecc3676e 1973 }
AzureIoTClient 42:448eecc3676e 1974 else
AzureIoTClient 42:448eecc3676e 1975 {
AzureIoTClient 42:448eecc3676e 1976 sourceCloned = 1;
AzureIoTClient 42:448eecc3676e 1977 }
AzureIoTClient 42:448eecc3676e 1978 }
AzureIoTClient 42:448eecc3676e 1979
AzureIoTClient 42:448eecc3676e 1980 if (sourceCloned == 0)
AzureIoTClient 42:448eecc3676e 1981 {
AzureIoTClient 42:448eecc3676e 1982 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1983 }
AzureIoTClient 42:448eecc3676e 1984 else
AzureIoTClient 42:448eecc3676e 1985 {
Azure.IoT.Build 54:6dcad9019a64 1986 IOTHUB_CLIENT_INSTANCE* iotHubClientHandleData = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
Azure.IoT.Build 54:6dcad9019a64 1987
AzureIoTClient 42:448eecc3676e 1988 savedData->iotHubClientFileUploadCallback = iotHubClientFileUploadCallback;
AzureIoTClient 42:448eecc3676e 1989 savedData->context = context;
AzureIoTClient 58:15b0d29b2667 1990 (void)memcpy(savedData->source, source, size);
Azure.IoT.Build 54:6dcad9019a64 1991
AzureIoTClient 66:a419827cb051 1992 if ((result = StartWorkerThreadIfNeeded(iotHubClientHandleData)) != IOTHUB_CLIENT_OK)
AzureIoTClient 42:448eecc3676e 1993 {
AzureIoTClient 42:448eecc3676e 1994 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1995 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1996 free(savedData);
AzureIoTClient 42:448eecc3676e 1997 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1998 LogError("Could not start worker thread");
AzureIoTClient 42:448eecc3676e 1999 }
AzureIoTClient 42:448eecc3676e 2000 else
AzureIoTClient 42:448eecc3676e 2001 {
AzureIoTClient 66:a419827cb051 2002 if (Lock(iotHubClientHandleData->LockHandle) != LOCK_OK) /*locking because the next statement is changing blobThreadsToBeJoined*/
AzureIoTClient 42:448eecc3676e 2003 {
AzureIoTClient 66:a419827cb051 2004 LogError("unable to lock");
AzureIoTClient 42:448eecc3676e 2005 free(savedData->source);
AzureIoTClient 42:448eecc3676e 2006 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 2007 free(savedData);
AzureIoTClient 42:448eecc3676e 2008 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 2009 }
AzureIoTClient 42:448eecc3676e 2010 else
AzureIoTClient 42:448eecc3676e 2011 {
AzureIoTClient 42:448eecc3676e 2012 /*Codes_SRS_IOTHUBCLIENT_02_058: [ IoTHubClient_UploadToBlobAsync shall add the structure to the list of structures that need to be cleaned once file upload finishes. ]*/
AzureIoTClient 52:1cc3c6d07cad 2013 LIST_ITEM_HANDLE item = singlylinkedlist_add(iotHubClientHandleData->savedDataToBeCleaned, savedData);
AzureIoTClient 42:448eecc3676e 2014 if (item == NULL)
AzureIoTClient 42:448eecc3676e 2015 {
AzureIoTClient 52:1cc3c6d07cad 2016 LogError("unable to singlylinkedlist_add");
AzureIoTClient 42:448eecc3676e 2017 free(savedData->source);
AzureIoTClient 42:448eecc3676e 2018 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 2019 free(savedData);
AzureIoTClient 42:448eecc3676e 2020 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 2021 }
AzureIoTClient 42:448eecc3676e 2022 else
AzureIoTClient 42:448eecc3676e 2023 {
AzureIoTClient 42:448eecc3676e 2024 savedData->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 2025 savedData->canBeGarbageCollected = 0;
AzureIoTClient 42:448eecc3676e 2026 if ((savedData->lockGarbage = Lock_Init()) == NULL)
AzureIoTClient 42:448eecc3676e 2027 {
AzureIoTClient 52:1cc3c6d07cad 2028 (void)singlylinkedlist_remove(iotHubClientHandleData->savedDataToBeCleaned, item);
AzureIoTClient 42:448eecc3676e 2029 free(savedData->source);
AzureIoTClient 42:448eecc3676e 2030 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 2031 free(savedData);
AzureIoTClient 42:448eecc3676e 2032 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 2033 LogError("unable to Lock_Init");
AzureIoTClient 42:448eecc3676e 2034 }
AzureIoTClient 42:448eecc3676e 2035 else
AzureIoTClient 42:448eecc3676e 2036 {
AzureIoTClient 42:448eecc3676e 2037 /*Codes_SRS_IOTHUBCLIENT_02_052: [ IoTHubClient_UploadToBlobAsync shall spawn a thread passing the structure build in SRS IOTHUBCLIENT 02 051 as thread data.]*/
AzureIoTClient 42:448eecc3676e 2038 if (ThreadAPI_Create(&savedData->uploadingThreadHandle, uploadingThread, savedData) != THREADAPI_OK)
AzureIoTClient 42:448eecc3676e 2039 {
AzureIoTClient 42:448eecc3676e 2040 /*Codes_SRS_IOTHUBCLIENT_02_053: [ If copying to the structure or spawning the thread fails, then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 2041 LogError("unablet to ThreadAPI_Create");
AzureIoTClient 42:448eecc3676e 2042 (void)Lock_Deinit(savedData->lockGarbage);
AzureIoTClient 52:1cc3c6d07cad 2043 (void)singlylinkedlist_remove(iotHubClientHandleData->savedDataToBeCleaned, item);
AzureIoTClient 42:448eecc3676e 2044 free(savedData->source);
AzureIoTClient 42:448eecc3676e 2045 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 2046 free(savedData);
AzureIoTClient 42:448eecc3676e 2047 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 2048 }
AzureIoTClient 42:448eecc3676e 2049 else
AzureIoTClient 42:448eecc3676e 2050 {
AzureIoTClient 42:448eecc3676e 2051 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 2052 }
AzureIoTClient 42:448eecc3676e 2053 }
AzureIoTClient 42:448eecc3676e 2054 }
AzureIoTClient 66:a419827cb051 2055
AzureIoTClient 66:a419827cb051 2056 (void)Unlock(iotHubClientHandleData->LockHandle);
AzureIoTClient 42:448eecc3676e 2057 }
AzureIoTClient 42:448eecc3676e 2058 }
AzureIoTClient 42:448eecc3676e 2059 }
AzureIoTClient 42:448eecc3676e 2060 }
AzureIoTClient 42:448eecc3676e 2061 }
AzureIoTClient 42:448eecc3676e 2062 }
AzureIoTClient 42:448eecc3676e 2063 return result;
AzureIoTClient 42:448eecc3676e 2064 }
AzureIoTClient 80:db5f5237bc95 2065
AzureIoTClient 80:db5f5237bc95 2066 static int uploadMultipleBlock_thread(void* data)
AzureIoTClient 80:db5f5237bc95 2067 {
AzureIoTClient 80:db5f5237bc95 2068 UPLOADTOBLOB_MULTIBLOCK_SAVED_DATA *blocksData = (UPLOADTOBLOB_MULTIBLOCK_SAVED_DATA *)data;
AzureIoTClient 80:db5f5237bc95 2069 IOTHUB_CLIENT_LL_HANDLE llHandle = blocksData->iotHubClientHandle->IoTHubClientLLHandle;
AzureIoTClient 80:db5f5237bc95 2070
AzureIoTClient 80:db5f5237bc95 2071 /*Codes_SRS_IOTHUBCLIENT_99_078: [ The thread shall call `IoTHubClient_LL_UploadMultipleBlocksToBlob` passing the information packed in the structure. ]*/
AzureIoTClient 80:db5f5237bc95 2072 IOTHUB_CLIENT_RESULT result = IoTHubClient_LL_UploadMultipleBlocksToBlob(llHandle, blocksData->destinationFileName, blocksData->getDataCallback, blocksData->context);
AzureIoTClient 80:db5f5237bc95 2073
AzureIoTClient 80:db5f5237bc95 2074 // Clean resources
AzureIoTClient 80:db5f5237bc95 2075 free(blocksData->destinationFileName);
AzureIoTClient 80:db5f5237bc95 2076 free(blocksData);
AzureIoTClient 80:db5f5237bc95 2077 ThreadAPI_Exit(result);
AzureIoTClient 80:db5f5237bc95 2078 return 0;
AzureIoTClient 80:db5f5237bc95 2079 }
AzureIoTClient 80:db5f5237bc95 2080
AzureIoTClient 80:db5f5237bc95 2081 IOTHUB_CLIENT_RESULT IoTHubClient_UploadMultipleBlocksToBlobAsync(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const char* destinationFileName, IOTHUB_CLIENT_FILE_UPLOAD_GET_DATA_CALLBACK getDataCallback, void* context)
AzureIoTClient 80:db5f5237bc95 2082 {
AzureIoTClient 80:db5f5237bc95 2083 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 80:db5f5237bc95 2084
AzureIoTClient 80:db5f5237bc95 2085 /*Codes_SRS_IOTHUBCLIENT_99_072: [ If `iotHubClientHandle` is `NULL` then `IoTHubClient_UploadMultipleBlocksToBlobAsync` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 80:db5f5237bc95 2086 /*Codes_SRS_IOTHUBCLIENT_99_073: [ If `destinationFileName` is `NULL` then `IoTHubClient_UploadMultipleBlocksToBlobAsync` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 80:db5f5237bc95 2087 /*Codes_SRS_IOTHUBCLIENT_99_074: [ If `getDataCallback` is `NULL` then `IoTHubClient_UploadMultipleBlocksToBlobAsync` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 80:db5f5237bc95 2088 if (
AzureIoTClient 80:db5f5237bc95 2089 (iotHubClientHandle == NULL) ||
AzureIoTClient 80:db5f5237bc95 2090 (destinationFileName == NULL) ||
AzureIoTClient 80:db5f5237bc95 2091 (getDataCallback == NULL)
AzureIoTClient 80:db5f5237bc95 2092 )
AzureIoTClient 80:db5f5237bc95 2093 {
AzureIoTClient 80:db5f5237bc95 2094 LogError("invalid parameters iotHubClientHandle = %p , destinationFileName = %p, getDataCallback = %p",
AzureIoTClient 80:db5f5237bc95 2095 iotHubClientHandle,
AzureIoTClient 80:db5f5237bc95 2096 destinationFileName,
AzureIoTClient 80:db5f5237bc95 2097 getDataCallback
AzureIoTClient 80:db5f5237bc95 2098 );
AzureIoTClient 80:db5f5237bc95 2099 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 80:db5f5237bc95 2100 }
AzureIoTClient 80:db5f5237bc95 2101 else
AzureIoTClient 80:db5f5237bc95 2102 {
AzureIoTClient 80:db5f5237bc95 2103 /*Codes_SRS_IOTHUBCLIENT_99_075: [ `IoTHubClient_UploadMultipleBlocksToBlobAsync` shall copy the `destinationFileName`, `getDataCallback`, `context` and `iotHubClientHandle` into a structure. ]*/
AzureIoTClient 80:db5f5237bc95 2104 UPLOADTOBLOB_MULTIBLOCK_SAVED_DATA *blocksData = (UPLOADTOBLOB_MULTIBLOCK_SAVED_DATA *)malloc(sizeof(UPLOADTOBLOB_MULTIBLOCK_SAVED_DATA));
AzureIoTClient 80:db5f5237bc95 2105 if (blocksData == NULL)
AzureIoTClient 80:db5f5237bc95 2106 {
AzureIoTClient 80:db5f5237bc95 2107 /*Codes_SRS_IOTHUBCLIENT_99_077: [ If copying to the structure or spawning the thread fails, then `IoTHubClient_UploadMultipleBlocksToBlobAsync` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 80:db5f5237bc95 2108 LogError("unable to malloc");
AzureIoTClient 80:db5f5237bc95 2109 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 80:db5f5237bc95 2110 }
AzureIoTClient 80:db5f5237bc95 2111 else
AzureIoTClient 80:db5f5237bc95 2112 {
AzureIoTClient 80:db5f5237bc95 2113 /*Codes_SRS_IOTHUBCLIENT_99_075: [ `IoTHubClient_UploadMultipleBlocksToBlobAsync` shall copy the `destinationFileName`, `getDataCallback`, `context` and `iotHubClientHandle` into a structure. ]*/
AzureIoTClient 80:db5f5237bc95 2114 if (mallocAndStrcpy_s((char**)&blocksData->destinationFileName, destinationFileName) != 0)
AzureIoTClient 80:db5f5237bc95 2115 {
AzureIoTClient 80:db5f5237bc95 2116 /*Codes_SRS_IOTHUBCLIENT_99_077: [ If copying to the structure or spawning the thread fails, then `IoTHubClient_UploadMultipleBlocksToBlobAsync` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 80:db5f5237bc95 2117 LogError("unable to mallocAndStrcpy_s");
AzureIoTClient 80:db5f5237bc95 2118 free(blocksData);
AzureIoTClient 80:db5f5237bc95 2119 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 80:db5f5237bc95 2120 }
AzureIoTClient 80:db5f5237bc95 2121 else
AzureIoTClient 80:db5f5237bc95 2122 {
AzureIoTClient 80:db5f5237bc95 2123 /*Codes_SRS_IOTHUBCLIENT_99_075: [ `IoTHubClient_UploadMultipleBlocksToBlobAsync` shall copy the `destinationFileName`, `getDataCallback`, `context` and `iotHubClientHandle` into a structure. ]*/
AzureIoTClient 80:db5f5237bc95 2124 blocksData->getDataCallback = getDataCallback;
AzureIoTClient 80:db5f5237bc95 2125 blocksData->context = context;
AzureIoTClient 80:db5f5237bc95 2126 blocksData->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 80:db5f5237bc95 2127 if (ThreadAPI_Create(&blocksData->uploadingThreadHandle, uploadMultipleBlock_thread, blocksData) != THREADAPI_OK)
AzureIoTClient 80:db5f5237bc95 2128 {
AzureIoTClient 80:db5f5237bc95 2129 /*Codes_SRS_IOTHUBCLIENT_99_077: [ If copying to the structure or spawning the thread fails, then `IoTHubClient_UploadMultipleBlocksToBlobAsync` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 80:db5f5237bc95 2130 LogError("unable to ThreadAPI_Create");
AzureIoTClient 80:db5f5237bc95 2131 free(blocksData->destinationFileName);
AzureIoTClient 80:db5f5237bc95 2132 free(blocksData);
AzureIoTClient 80:db5f5237bc95 2133 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 80:db5f5237bc95 2134 }
AzureIoTClient 80:db5f5237bc95 2135 else
AzureIoTClient 80:db5f5237bc95 2136 {
AzureIoTClient 80:db5f5237bc95 2137 /*Codes_SRS_IOTHUBCLIENT_99_077: [ If copying to the structure and spawning the thread succeeds, then `IoTHubClient_UploadMultipleBlocksToBlobAsync` shall return `IOTHUB_CLIENT_OK`. ]*/
AzureIoTClient 80:db5f5237bc95 2138 result = IOTHUB_CLIENT_OK;
AzureIoTClient 80:db5f5237bc95 2139 }
AzureIoTClient 80:db5f5237bc95 2140 }
AzureIoTClient 80:db5f5237bc95 2141 }
AzureIoTClient 80:db5f5237bc95 2142 }
AzureIoTClient 80:db5f5237bc95 2143
AzureIoTClient 80:db5f5237bc95 2144 return result;
AzureIoTClient 80:db5f5237bc95 2145 }
AzureIoTClient 80:db5f5237bc95 2146
AzureIoTClient 44:33dd78697616 2147 #endif /*DONT_USE_UPLOADTOBLOB*/