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:
Thu Feb 15 11:35:22 2018 -0800
Revision:
83:18ccaa6567d0
Parent:
82:f94e6bed4495
Child:
88:248736be106e
1.1.32

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