Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

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

Committer:
AzureIoTClient
Date:
Mon May 22 10:35:09 2017 -0700
Revision:
67:ecebc2a41159
Parent:
66:a419827cb051
Child:
71:0d498da5ece1
1.1.15

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