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 08 10:50:31 2017 -0700
Revision:
66:a419827cb051
Parent:
62:5a4cdacf5090
Child:
67:ecebc2a41159
1.1.14

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 61:8b85a4e797cf 417 if (iotHubClientInstance->desired_state_callback)
AzureIoTClient 61:8b85a4e797cf 418 {
AzureIoTClient 61:8b85a4e797cf 419 iotHubClientInstance->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 61:8b85a4e797cf 420 }
AzureIoTClient 61:8b85a4e797cf 421 if (queued_cb->iothub_callback.dev_twin_cb_info.payLoad)
AzureIoTClient 61:8b85a4e797cf 422 {
AzureIoTClient 61:8b85a4e797cf 423 free(queued_cb->iothub_callback.dev_twin_cb_info.payLoad);
AzureIoTClient 61:8b85a4e797cf 424 }
AzureIoTClient 61:8b85a4e797cf 425 break;
AzureIoTClient 61:8b85a4e797cf 426 case CALLBACK_TYPE_EVENT_CONFIRM:
AzureIoTClient 61:8b85a4e797cf 427 if (iotHubClientInstance->event_confirm_callback)
AzureIoTClient 61:8b85a4e797cf 428 {
AzureIoTClient 61:8b85a4e797cf 429 iotHubClientInstance->event_confirm_callback(queued_cb->iothub_callback.event_confirm_cb_info.confirm_result, queued_cb->userContextCallback);
AzureIoTClient 61:8b85a4e797cf 430 }
AzureIoTClient 61:8b85a4e797cf 431 break;
AzureIoTClient 61:8b85a4e797cf 432 case CALLBACK_TYPE_REPORTED_STATE:
AzureIoTClient 61:8b85a4e797cf 433 if (iotHubClientInstance->reported_state_callback)
AzureIoTClient 61:8b85a4e797cf 434 {
AzureIoTClient 61:8b85a4e797cf 435 iotHubClientInstance->reported_state_callback(queued_cb->iothub_callback.reported_state_cb_info.status_code, queued_cb->userContextCallback);
AzureIoTClient 61:8b85a4e797cf 436 }
AzureIoTClient 61:8b85a4e797cf 437 break;
AzureIoTClient 61:8b85a4e797cf 438 case CALLBACK_TYPE_CONNECTION_STATUS:
AzureIoTClient 61:8b85a4e797cf 439 if (iotHubClientInstance->connection_status_callback)
AzureIoTClient 61:8b85a4e797cf 440 {
AzureIoTClient 61:8b85a4e797cf 441 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 442 }
AzureIoTClient 61:8b85a4e797cf 443 break;
AzureIoTClient 61:8b85a4e797cf 444 case CALLBACK_TYPE_DEVICE_METHOD:
AzureIoTClient 61:8b85a4e797cf 445 if (iotHubClientInstance->device_method_callback)
AzureIoTClient 61:8b85a4e797cf 446 {
AzureIoTClient 61:8b85a4e797cf 447 const char* method_name = STRING_c_str(queued_cb->iothub_callback.method_cb_info.method_name);
AzureIoTClient 61:8b85a4e797cf 448 const unsigned char* payload = BUFFER_u_char(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 61:8b85a4e797cf 449 size_t payload_len = BUFFER_length(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 62:5a4cdacf5090 450
AzureIoTClient 62:5a4cdacf5090 451 unsigned char* payload_resp = NULL;
AzureIoTClient 62:5a4cdacf5090 452 size_t response_size = 0;
AzureIoTClient 62:5a4cdacf5090 453 int status = iotHubClientInstance->device_method_callback(method_name, payload, payload_len, &payload_resp, &response_size, queued_cb->userContextCallback);
AzureIoTClient 62:5a4cdacf5090 454
AzureIoTClient 62:5a4cdacf5090 455 if (payload_resp && (response_size > 0))
AzureIoTClient 62:5a4cdacf5090 456 {
AzureIoTClient 62:5a4cdacf5090 457 IOTHUB_CLIENT_HANDLE handle = iotHubClientInstance->method_user_context->iotHubClientHandle;
AzureIoTClient 62:5a4cdacf5090 458 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 459 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 62:5a4cdacf5090 460 {
AzureIoTClient 62:5a4cdacf5090 461 LogError("IoTHubClient_LL_DeviceMethodResponse failed");
AzureIoTClient 62:5a4cdacf5090 462 }
AzureIoTClient 62:5a4cdacf5090 463 }
AzureIoTClient 62:5a4cdacf5090 464
AzureIoTClient 62:5a4cdacf5090 465 BUFFER_delete(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 62:5a4cdacf5090 466 STRING_delete(queued_cb->iothub_callback.method_cb_info.method_name);
AzureIoTClient 66:a419827cb051 467
AzureIoTClient 66:a419827cb051 468 if (payload_resp)
AzureIoTClient 66:a419827cb051 469 {
AzureIoTClient 62:5a4cdacf5090 470 free(payload_resp);
AzureIoTClient 66:a419827cb051 471 }
AzureIoTClient 62:5a4cdacf5090 472 }
AzureIoTClient 62:5a4cdacf5090 473 break;
AzureIoTClient 62:5a4cdacf5090 474 case CALLBACK_TYPE_INBOUD_DEVICE_METHOD:
AzureIoTClient 62:5a4cdacf5090 475 if (iotHubClientInstance->inbound_device_method_callback)
AzureIoTClient 62:5a4cdacf5090 476 {
AzureIoTClient 62:5a4cdacf5090 477 const char* method_name = STRING_c_str(queued_cb->iothub_callback.method_cb_info.method_name);
AzureIoTClient 62:5a4cdacf5090 478 const unsigned char* payload = BUFFER_u_char(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 62:5a4cdacf5090 479 size_t payload_len = BUFFER_length(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 62:5a4cdacf5090 480
AzureIoTClient 62:5a4cdacf5090 481 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 482
AzureIoTClient 61:8b85a4e797cf 483 BUFFER_delete(queued_cb->iothub_callback.method_cb_info.payload);
AzureIoTClient 61:8b85a4e797cf 484 STRING_delete(queued_cb->iothub_callback.method_cb_info.method_name);
AzureIoTClient 61:8b85a4e797cf 485 }
AzureIoTClient 61:8b85a4e797cf 486 break;
AzureIoTClient 61:8b85a4e797cf 487 case CALLBACK_TYPE_MESSAGE:
AzureIoTClient 61:8b85a4e797cf 488 if (iotHubClientInstance->message_callback)
AzureIoTClient 61:8b85a4e797cf 489 {
AzureIoTClient 61:8b85a4e797cf 490 IOTHUBMESSAGE_DISPOSITION_RESULT disposition = iotHubClientInstance->message_callback(queued_cb->iothub_callback.message_cb_info->messageHandle, queued_cb->userContextCallback);
AzureIoTClient 61:8b85a4e797cf 491 IOTHUB_CLIENT_HANDLE handle = iotHubClientInstance->message_user_context->iotHubClientHandle;
AzureIoTClient 61:8b85a4e797cf 492
AzureIoTClient 61:8b85a4e797cf 493 if (Lock(handle->LockHandle) == LOCK_OK)
Azure.IoT.Build 54:6dcad9019a64 494 {
AzureIoTClient 61:8b85a4e797cf 495 IOTHUB_CLIENT_RESULT result = IoTHubClient_LL_SendMessageDisposition(handle->IoTHubClientLLHandle, queued_cb->iothub_callback.message_cb_info, disposition);
AzureIoTClient 62:5a4cdacf5090 496 (void)Unlock(handle->LockHandle);
AzureIoTClient 61:8b85a4e797cf 497 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 498 {
AzureIoTClient 62:5a4cdacf5090 499 LogError("IoTHubClient_LL_SendMessageDisposition failed");
Azure.IoT.Build 54:6dcad9019a64 500 }
Azure.IoT.Build 54:6dcad9019a64 501 }
AzureIoTClient 61:8b85a4e797cf 502 else
Azure.IoT.Build 54:6dcad9019a64 503 {
AzureIoTClient 61:8b85a4e797cf 504 LogError("Lock failed");
Azure.IoT.Build 54:6dcad9019a64 505 }
AzureIoTClient 61:8b85a4e797cf 506 }
AzureIoTClient 61:8b85a4e797cf 507 break;
AzureIoTClient 61:8b85a4e797cf 508 default:
AzureIoTClient 61:8b85a4e797cf 509 LogError("Invalid callback type '%s'", ENUM_TO_STRING(USER_CALLBACK_TYPE, queued_cb->type));
AzureIoTClient 61:8b85a4e797cf 510 break;
Azure.IoT.Build 54:6dcad9019a64 511 }
Azure.IoT.Build 54:6dcad9019a64 512 }
Azure.IoT.Build 54:6dcad9019a64 513 }
AzureIoTClient 61:8b85a4e797cf 514 VECTOR_destroy(call_backs);
Azure.IoT.Build 54:6dcad9019a64 515 }
Azure.IoT.Build 54:6dcad9019a64 516
AzureIoTClient 66:a419827cb051 517 static void ScheduleWork_Thread_ForMultiplexing(void* iotHubClientHandle)
AzureIoTClient 66:a419827cb051 518 {
AzureIoTClient 66:a419827cb051 519 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 66:a419827cb051 520
AzureIoTClient 66:a419827cb051 521 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 66:a419827cb051 522 garbageCollectorImpl(iotHubClientInstance);
AzureIoTClient 66:a419827cb051 523 #endif
AzureIoTClient 66:a419827cb051 524 if (Lock(iotHubClientInstance->LockHandle) == LOCK_OK)
AzureIoTClient 66:a419827cb051 525 {
AzureIoTClient 66:a419827cb051 526 VECTOR_HANDLE call_backs = VECTOR_move(iotHubClientInstance->saved_user_callback_list);
AzureIoTClient 66:a419827cb051 527 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 66:a419827cb051 528
AzureIoTClient 66:a419827cb051 529 if (call_backs == NULL)
AzureIoTClient 66:a419827cb051 530 {
AzureIoTClient 66:a419827cb051 531 LogError("Failed moving user callbacks");
AzureIoTClient 66:a419827cb051 532 }
AzureIoTClient 66:a419827cb051 533 else
AzureIoTClient 66:a419827cb051 534 {
AzureIoTClient 66:a419827cb051 535 dispatch_user_callbacks(iotHubClientInstance, call_backs);
AzureIoTClient 66:a419827cb051 536 }
AzureIoTClient 66:a419827cb051 537 }
AzureIoTClient 66:a419827cb051 538 else
AzureIoTClient 66:a419827cb051 539 {
AzureIoTClient 66:a419827cb051 540 LogError("failed locking for ScheduleWork_Thread_ForMultiplexing");
AzureIoTClient 66:a419827cb051 541 }
AzureIoTClient 66:a419827cb051 542 }
AzureIoTClient 66:a419827cb051 543
AzureIoTClient 16:deba40344375 544 static int ScheduleWork_Thread(void* threadArgument)
AzureIoTClient 16:deba40344375 545 {
AzureIoTClient 16:deba40344375 546 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)threadArgument;
AzureIoTClient 42:448eecc3676e 547
Azure.IoT Build 37:18310e4d888d 548 while (1)
AzureIoTClient 16:deba40344375 549 {
AzureIoTClient 16:deba40344375 550 if (Lock(iotHubClientInstance->LockHandle) == LOCK_OK)
AzureIoTClient 16:deba40344375 551 {
Azure.IoT Build 37:18310e4d888d 552 /*Codes_SRS_IOTHUBCLIENT_01_038: [ The thread shall exit when IoTHubClient_Destroy is called. ]*/
Azure.IoT Build 37:18310e4d888d 553 if (iotHubClientInstance->StopThread)
Azure.IoT Build 37:18310e4d888d 554 {
Azure.IoT Build 37:18310e4d888d 555 (void)Unlock(iotHubClientInstance->LockHandle);
Azure.IoT Build 37:18310e4d888d 556 break; /*gets out of the thread*/
Azure.IoT Build 37:18310e4d888d 557 }
Azure.IoT Build 37:18310e4d888d 558 else
Azure.IoT Build 37:18310e4d888d 559 {
Azure.IoT Build 37:18310e4d888d 560 /* 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 561 /* 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 562 IoTHubClient_LL_DoWork(iotHubClientInstance->IoTHubClientLLHandle);
AzureIoTClient 42:448eecc3676e 563
AzureIoTClient 44:33dd78697616 564 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 565 garbageCollectorImpl(iotHubClientInstance);
AzureIoTClient 43:038d8511e817 566 #endif
AzureIoTClient 61:8b85a4e797cf 567 VECTOR_HANDLE call_backs = VECTOR_move(iotHubClientInstance->saved_user_callback_list);
Azure.IoT Build 37:18310e4d888d 568 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 61:8b85a4e797cf 569 if (call_backs == NULL)
AzureIoTClient 61:8b85a4e797cf 570 {
AzureIoTClient 61:8b85a4e797cf 571 LogError("VECTOR_move failed");
AzureIoTClient 61:8b85a4e797cf 572 }
AzureIoTClient 61:8b85a4e797cf 573 else
AzureIoTClient 61:8b85a4e797cf 574 {
AzureIoTClient 61:8b85a4e797cf 575 dispatch_user_callbacks(iotHubClientInstance, call_backs);
AzureIoTClient 61:8b85a4e797cf 576 }
Azure.IoT Build 37:18310e4d888d 577 }
AzureIoTClient 16:deba40344375 578 }
Azure.IoT Build 37:18310e4d888d 579 else
Azure.IoT Build 37:18310e4d888d 580 {
Azure.IoT Build 37:18310e4d888d 581 /*Codes_SRS_IOTHUBCLIENT_01_040: [If acquiring the lock fails, IoTHubClient_LL_DoWork shall not be called.]*/
Azure.IoT Build 37:18310e4d888d 582 /*no code, shall retry*/
Azure.IoT Build 37:18310e4d888d 583 }
Azure.IoT Build 37:18310e4d888d 584 (void)ThreadAPI_Sleep(1);
AzureIoTClient 16:deba40344375 585 }
AzureIoTClient 42:448eecc3676e 586
AzureIoTClient 16:deba40344375 587 return 0;
AzureIoTClient 16:deba40344375 588 }
AzureIoTClient 16:deba40344375 589
Azure.IoT Build 37:18310e4d888d 590 static IOTHUB_CLIENT_RESULT StartWorkerThreadIfNeeded(IOTHUB_CLIENT_INSTANCE* iotHubClientInstance)
AzureIoTClient 16:deba40344375 591 {
AzureIoTClient 42:448eecc3676e 592 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 593 if (iotHubClientInstance->TransportHandle == NULL)
AzureIoTClient 42:448eecc3676e 594 {
AzureIoTClient 42:448eecc3676e 595 if (iotHubClientInstance->ThreadHandle == NULL)
AzureIoTClient 42:448eecc3676e 596 {
AzureIoTClient 42:448eecc3676e 597 iotHubClientInstance->StopThread = 0;
AzureIoTClient 42:448eecc3676e 598 if (ThreadAPI_Create(&iotHubClientInstance->ThreadHandle, ScheduleWork_Thread, iotHubClientInstance) != THREADAPI_OK)
AzureIoTClient 42:448eecc3676e 599 {
AzureIoTClient 61:8b85a4e797cf 600 LogError("ThreadAPI_Create failed");
AzureIoTClient 42:448eecc3676e 601 iotHubClientInstance->ThreadHandle = NULL;
AzureIoTClient 42:448eecc3676e 602 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 603 }
AzureIoTClient 42:448eecc3676e 604 else
AzureIoTClient 42:448eecc3676e 605 {
AzureIoTClient 42:448eecc3676e 606 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 607 }
AzureIoTClient 42:448eecc3676e 608 }
AzureIoTClient 42:448eecc3676e 609 else
AzureIoTClient 42:448eecc3676e 610 {
AzureIoTClient 42:448eecc3676e 611 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 612 }
AzureIoTClient 42:448eecc3676e 613 }
AzureIoTClient 42:448eecc3676e 614 else
AzureIoTClient 42:448eecc3676e 615 {
AzureIoTClient 42:448eecc3676e 616 /*Codes_SRS_IOTHUBCLIENT_17_012: [ If the transport connection is shared, the thread shall be started by calling IoTHubTransport_StartWorkerThread. ]*/
AzureIoTClient 42:448eecc3676e 617 /*Codes_SRS_IOTHUBCLIENT_17_011: [ If the transport connection is shared, the thread shall be started by calling IoTHubTransport_StartWorkerThread*/
AzureIoTClient 66:a419827cb051 618 result = IoTHubTransport_StartWorkerThread(iotHubClientInstance->TransportHandle, iotHubClientInstance, ScheduleWork_Thread_ForMultiplexing);
AzureIoTClient 42:448eecc3676e 619 }
AzureIoTClient 42:448eecc3676e 620 return result;
AzureIoTClient 16:deba40344375 621 }
AzureIoTClient 16:deba40344375 622
Azure.IoT.Build 54:6dcad9019a64 623 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 624 {
AzureIoTClient 16:deba40344375 625 IOTHUB_CLIENT_INSTANCE* result = (IOTHUB_CLIENT_INSTANCE*)malloc(sizeof(IOTHUB_CLIENT_INSTANCE));
AzureIoTClient 16:deba40344375 626
AzureIoTClient 16:deba40344375 627 /* Codes_SRS_IOTHUBCLIENT_01_004: [If allocating memory for the new IoTHubClient instance fails, then IoTHubClient_Create shall return NULL.] */
AzureIoTClient 16:deba40344375 628 if (result != NULL)
AzureIoTClient 16:deba40344375 629 {
AzureIoTClient 16:deba40344375 630 /* 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 631 if ( (result->saved_user_callback_list = VECTOR_create(sizeof(USER_CALLBACK_INFO)) ) == NULL)
AzureIoTClient 16:deba40344375 632 {
Azure.IoT.Build 54:6dcad9019a64 633 LogError("Failed creating VECTOR");
AzureIoTClient 16:deba40344375 634 free(result);
AzureIoTClient 16:deba40344375 635 result = NULL;
AzureIoTClient 16:deba40344375 636 }
AzureIoTClient 16:deba40344375 637 else
AzureIoTClient 16:deba40344375 638 {
AzureIoTClient 44:33dd78697616 639 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 52:1cc3c6d07cad 640 /*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 641 if ((result->savedDataToBeCleaned = singlylinkedlist_create()) == NULL)
AzureIoTClient 16:deba40344375 642 {
AzureIoTClient 52:1cc3c6d07cad 643 /*Codes_SRS_IOTHUBCLIENT_02_061: [ If creating the SINGLYLINKEDLIST_HANDLE fails then IoTHubClient_Create shall fail and return NULL. ]*/
AzureIoTClient 52:1cc3c6d07cad 644 LogError("unable to singlylinkedlist_create");
Azure.IoT.Build 54:6dcad9019a64 645 VECTOR_destroy(result->saved_user_callback_list);
AzureIoTClient 16:deba40344375 646 free(result);
AzureIoTClient 16:deba40344375 647 result = NULL;
AzureIoTClient 16:deba40344375 648 }
AzureIoTClient 42:448eecc3676e 649 else
AzureIoTClient 43:038d8511e817 650 #endif
AzureIoTClient 42:448eecc3676e 651 {
Azure.IoT.Build 54:6dcad9019a64 652 result->TransportHandle = transportHandle;
Azure.IoT.Build 54:6dcad9019a64 653 result->created_with_transport_handle = 0;
Azure.IoT.Build 54:6dcad9019a64 654 if (config != NULL)
Azure.IoT.Build 54:6dcad9019a64 655 {
Azure.IoT.Build 54:6dcad9019a64 656 if (transportHandle != NULL)
Azure.IoT.Build 54:6dcad9019a64 657 {
Azure.IoT.Build 54:6dcad9019a64 658 /*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 659 result->LockHandle = IoTHubTransport_GetLock(transportHandle);
Azure.IoT.Build 54:6dcad9019a64 660 if (result->LockHandle == NULL)
Azure.IoT.Build 54:6dcad9019a64 661 {
Azure.IoT.Build 54:6dcad9019a64 662 LogError("unable to IoTHubTransport_GetLock");
Azure.IoT.Build 54:6dcad9019a64 663 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 664 }
Azure.IoT.Build 54:6dcad9019a64 665 else
Azure.IoT.Build 54:6dcad9019a64 666 {
Azure.IoT.Build 54:6dcad9019a64 667 IOTHUB_CLIENT_DEVICE_CONFIG deviceConfig;
Azure.IoT.Build 54:6dcad9019a64 668 deviceConfig.deviceId = config->deviceId;
Azure.IoT.Build 54:6dcad9019a64 669 deviceConfig.deviceKey = config->deviceKey;
Azure.IoT.Build 54:6dcad9019a64 670 deviceConfig.protocol = config->protocol;
Azure.IoT.Build 54:6dcad9019a64 671 deviceConfig.deviceSasToken = config->deviceSasToken;
Azure.IoT.Build 54:6dcad9019a64 672
Azure.IoT.Build 54:6dcad9019a64 673 /*Codes_SRS_IOTHUBCLIENT_17_003: [ IoTHubClient_CreateWithTransport shall call IoTHubTransport_GetLLTransport on transportHandle to get lower layer transport. ]*/
Azure.IoT.Build 54:6dcad9019a64 674 deviceConfig.transportHandle = IoTHubTransport_GetLLTransport(transportHandle);
Azure.IoT.Build 54:6dcad9019a64 675 if (deviceConfig.transportHandle == NULL)
Azure.IoT.Build 54:6dcad9019a64 676 {
Azure.IoT.Build 54:6dcad9019a64 677 LogError("unable to IoTHubTransport_GetLLTransport");
Azure.IoT.Build 54:6dcad9019a64 678 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 679 }
Azure.IoT.Build 54:6dcad9019a64 680 else
Azure.IoT.Build 54:6dcad9019a64 681 {
Azure.IoT.Build 54:6dcad9019a64 682 if (Lock(result->LockHandle) != LOCK_OK)
Azure.IoT.Build 54:6dcad9019a64 683 {
Azure.IoT.Build 54:6dcad9019a64 684 LogError("unable to Lock");
Azure.IoT.Build 54:6dcad9019a64 685 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 686 }
Azure.IoT.Build 54:6dcad9019a64 687 else
Azure.IoT.Build 54:6dcad9019a64 688 {
Azure.IoT.Build 54:6dcad9019a64 689 /*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 690 result->IoTHubClientLLHandle = IoTHubClient_LL_CreateWithTransport(&deviceConfig);
Azure.IoT.Build 54:6dcad9019a64 691 result->created_with_transport_handle = 1;
Azure.IoT.Build 54:6dcad9019a64 692 if (Unlock(result->LockHandle) != LOCK_OK)
Azure.IoT.Build 54:6dcad9019a64 693 {
Azure.IoT.Build 54:6dcad9019a64 694 LogError("unable to Unlock");
Azure.IoT.Build 54:6dcad9019a64 695 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 696 }
Azure.IoT.Build 54:6dcad9019a64 697 }
Azure.IoT.Build 54:6dcad9019a64 698 }
Azure.IoT.Build 54:6dcad9019a64 699 }
Azure.IoT.Build 54:6dcad9019a64 700 }
Azure.IoT.Build 54:6dcad9019a64 701 else
Azure.IoT.Build 54:6dcad9019a64 702 {
Azure.IoT.Build 54:6dcad9019a64 703 result->LockHandle = Lock_Init();
Azure.IoT.Build 54:6dcad9019a64 704 if (result->LockHandle == NULL)
Azure.IoT.Build 54:6dcad9019a64 705 {
Azure.IoT.Build 54:6dcad9019a64 706 /* Codes_SRS_IOTHUBCLIENT_01_030: [If creating the lock fails, then IoTHubClient_Create shall return NULL.] */
Azure.IoT.Build 54:6dcad9019a64 707 /* Codes_SRS_IOTHUBCLIENT_01_031: [If IoTHubClient_Create fails, all resources allocated by it shall be freed.] */
Azure.IoT.Build 54:6dcad9019a64 708 LogError("Failure creating Lock object");
Azure.IoT.Build 54:6dcad9019a64 709 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 710 }
Azure.IoT.Build 54:6dcad9019a64 711 else
Azure.IoT.Build 54:6dcad9019a64 712 {
Azure.IoT.Build 54:6dcad9019a64 713 /* 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 714 result->IoTHubClientLLHandle = IoTHubClient_LL_Create(config);
Azure.IoT.Build 54:6dcad9019a64 715 }
Azure.IoT.Build 54:6dcad9019a64 716 }
Azure.IoT.Build 54:6dcad9019a64 717 }
Azure.IoT.Build 54:6dcad9019a64 718 else
Azure.IoT.Build 54:6dcad9019a64 719 {
Azure.IoT.Build 54:6dcad9019a64 720 result->LockHandle = Lock_Init();
Azure.IoT.Build 54:6dcad9019a64 721 if (result->LockHandle == NULL)
Azure.IoT.Build 54:6dcad9019a64 722 {
Azure.IoT.Build 54:6dcad9019a64 723 /* Codes_SRS_IOTHUBCLIENT_01_030: [If creating the lock fails, then IoTHubClient_Create shall return NULL.] */
Azure.IoT.Build 54:6dcad9019a64 724 /* Codes_SRS_IOTHUBCLIENT_01_031: [If IoTHubClient_Create fails, all resources allocated by it shall be freed.] */
Azure.IoT.Build 54:6dcad9019a64 725 LogError("Failure creating Lock object");
Azure.IoT.Build 54:6dcad9019a64 726 result->IoTHubClientLLHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 727 }
Azure.IoT.Build 54:6dcad9019a64 728 else
Azure.IoT.Build 54:6dcad9019a64 729 {
Azure.IoT.Build 54:6dcad9019a64 730 /* 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 731 result->IoTHubClientLLHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, protocol);
Azure.IoT.Build 54:6dcad9019a64 732 }
Azure.IoT.Build 54:6dcad9019a64 733 }
Azure.IoT.Build 54:6dcad9019a64 734
AzureIoTClient 42:448eecc3676e 735 if (result->IoTHubClientLLHandle == NULL)
AzureIoTClient 42:448eecc3676e 736 {
AzureIoTClient 42:448eecc3676e 737 /* Codes_SRS_IOTHUBCLIENT_01_003: [If IoTHubClient_LL_Create fails, then IoTHubClient_Create shall return NULL.] */
AzureIoTClient 42:448eecc3676e 738 /* Codes_SRS_IOTHUBCLIENT_01_031: [If IoTHubClient_Create fails, all resources allocated by it shall be freed.] */
Azure.IoT.Build 54:6dcad9019a64 739 /* Codes_SRS_IOTHUBCLIENT_17_006: [ If IoTHubTransport_GetLock fails, then IoTHubClient_CreateWithTransport shall return NULL. ]*/
Azure.IoT.Build 54:6dcad9019a64 740 if (transportHandle == NULL)
Azure.IoT.Build 54:6dcad9019a64 741 {
Azure.IoT.Build 54:6dcad9019a64 742 Lock_Deinit(result->LockHandle);
Azure.IoT.Build 54:6dcad9019a64 743 }
AzureIoTClient 44:33dd78697616 744 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 52:1cc3c6d07cad 745 singlylinkedlist_destroy(result->savedDataToBeCleaned);
AzureIoTClient 43:038d8511e817 746 #endif
Azure.IoT.Build 54:6dcad9019a64 747 LogError("Failure creating iothub handle");
Azure.IoT.Build 54:6dcad9019a64 748 VECTOR_destroy(result->saved_user_callback_list);
AzureIoTClient 42:448eecc3676e 749 free(result);
AzureIoTClient 42:448eecc3676e 750 result = NULL;
AzureIoTClient 42:448eecc3676e 751 }
AzureIoTClient 42:448eecc3676e 752 else
AzureIoTClient 42:448eecc3676e 753 {
AzureIoTClient 42:448eecc3676e 754 result->ThreadHandle = NULL;
Azure.IoT.Build 54:6dcad9019a64 755 result->desired_state_callback = NULL;
Azure.IoT.Build 54:6dcad9019a64 756 result->event_confirm_callback = NULL;
Azure.IoT.Build 54:6dcad9019a64 757 result->reported_state_callback = NULL;
Azure.IoT.Build 54:6dcad9019a64 758 result->devicetwin_user_context = NULL;
AzureIoTClient 55:59b527ab3452 759 result->connection_status_callback = NULL;
AzureIoTClient 55:59b527ab3452 760 result->connection_status_user_context = NULL;
AzureIoTClient 61:8b85a4e797cf 761 result->message_callback = NULL;
AzureIoTClient 61:8b85a4e797cf 762 result->message_user_context = NULL;
AzureIoTClient 62:5a4cdacf5090 763 result->method_user_context = NULL;
AzureIoTClient 42:448eecc3676e 764 }
AzureIoTClient 42:448eecc3676e 765 }
AzureIoTClient 16:deba40344375 766 }
AzureIoTClient 16:deba40344375 767 }
Azure.IoT.Build 54:6dcad9019a64 768 return result;
Azure.IoT.Build 54:6dcad9019a64 769 }
AzureIoTClient 16:deba40344375 770
Azure.IoT.Build 54:6dcad9019a64 771 IOTHUB_CLIENT_HANDLE IoTHubClient_CreateFromConnectionString(const char* connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol)
Azure.IoT.Build 54:6dcad9019a64 772 {
Azure.IoT.Build 54:6dcad9019a64 773 IOTHUB_CLIENT_INSTANCE* result;
Azure.IoT.Build 54:6dcad9019a64 774
Azure.IoT.Build 54:6dcad9019a64 775 /* 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 776 if (connectionString == NULL)
Azure.IoT.Build 54:6dcad9019a64 777 {
Azure.IoT.Build 54:6dcad9019a64 778 LogError("Input parameter is NULL: connectionString");
Azure.IoT.Build 54:6dcad9019a64 779 result = NULL;
Azure.IoT.Build 54:6dcad9019a64 780 }
Azure.IoT.Build 54:6dcad9019a64 781 else if (protocol == NULL)
Azure.IoT.Build 54:6dcad9019a64 782 {
Azure.IoT.Build 54:6dcad9019a64 783 LogError("Input parameter is NULL: protocol");
Azure.IoT.Build 54:6dcad9019a64 784 result = NULL;
Azure.IoT.Build 54:6dcad9019a64 785 }
Azure.IoT.Build 54:6dcad9019a64 786 else
Azure.IoT.Build 54:6dcad9019a64 787 {
Azure.IoT.Build 54:6dcad9019a64 788 result = create_iothub_instance(NULL, NULL, connectionString, protocol);
Azure.IoT.Build 54:6dcad9019a64 789 }
Azure.IoT.Build 54:6dcad9019a64 790 return result;
Azure.IoT.Build 54:6dcad9019a64 791 }
Azure.IoT.Build 54:6dcad9019a64 792
Azure.IoT.Build 54:6dcad9019a64 793 IOTHUB_CLIENT_HANDLE IoTHubClient_Create(const IOTHUB_CLIENT_CONFIG* config)
Azure.IoT.Build 54:6dcad9019a64 794 {
Azure.IoT.Build 54:6dcad9019a64 795 IOTHUB_CLIENT_INSTANCE* result;
Azure.IoT.Build 54:6dcad9019a64 796 if (config == NULL)
Azure.IoT.Build 54:6dcad9019a64 797 {
Azure.IoT.Build 54:6dcad9019a64 798 LogError("Input parameter is NULL: IOTHUB_CLIENT_CONFIG");
Azure.IoT.Build 54:6dcad9019a64 799 result = NULL;
Azure.IoT.Build 54:6dcad9019a64 800 }
Azure.IoT.Build 54:6dcad9019a64 801 else
Azure.IoT.Build 54:6dcad9019a64 802 {
Azure.IoT.Build 54:6dcad9019a64 803 result = create_iothub_instance(config, NULL, NULL, NULL);
Azure.IoT.Build 54:6dcad9019a64 804 }
AzureIoTClient 16:deba40344375 805 return result;
AzureIoTClient 16:deba40344375 806 }
AzureIoTClient 16:deba40344375 807
Azure.IoT Build 37:18310e4d888d 808 IOTHUB_CLIENT_HANDLE IoTHubClient_CreateWithTransport(TRANSPORT_HANDLE transportHandle, const IOTHUB_CLIENT_CONFIG* config)
Azure.IoT Build 37:18310e4d888d 809 {
AzureIoTClient 42:448eecc3676e 810 IOTHUB_CLIENT_INSTANCE* result;
AzureIoTClient 42:448eecc3676e 811 /*Codes_SRS_IOTHUBCLIENT_17_013: [ IoTHubClient_CreateWithTransport shall return NULL if transportHandle is NULL. ]*/
AzureIoTClient 42:448eecc3676e 812 /*Codes_SRS_IOTHUBCLIENT_17_014: [ IoTHubClient_CreateWithTransport shall return NULL if config is NULL. ]*/
AzureIoTClient 42:448eecc3676e 813 if (transportHandle == NULL || config == NULL)
AzureIoTClient 42:448eecc3676e 814 {
AzureIoTClient 43:038d8511e817 815 LogError("invalid parameter TRANSPORT_HANDLE transportHandle=%p, const IOTHUB_CLIENT_CONFIG* config=%p", transportHandle, config);
AzureIoTClient 42:448eecc3676e 816 result = NULL;
AzureIoTClient 42:448eecc3676e 817 }
AzureIoTClient 42:448eecc3676e 818 else
AzureIoTClient 42:448eecc3676e 819 {
Azure.IoT.Build 54:6dcad9019a64 820 result = create_iothub_instance(config, transportHandle, NULL, NULL);
AzureIoTClient 42:448eecc3676e 821 }
AzureIoTClient 42:448eecc3676e 822 return result;
Azure.IoT Build 37:18310e4d888d 823 }
Azure.IoT Build 37:18310e4d888d 824
AzureIoTClient 16:deba40344375 825 /* Codes_SRS_IOTHUBCLIENT_01_005: [IoTHubClient_Destroy shall free all resources associated with the iotHubClientHandle instance.] */
AzureIoTClient 18:1e9adb15c645 826 void IoTHubClient_Destroy(IOTHUB_CLIENT_HANDLE iotHubClientHandle)
AzureIoTClient 16:deba40344375 827 {
AzureIoTClient 16:deba40344375 828 /* Codes_SRS_IOTHUBCLIENT_01_008: [IoTHubClient_Destroy shall do nothing if parameter iotHubClientHandle is NULL.] */
AzureIoTClient 16:deba40344375 829 if (iotHubClientHandle != NULL)
AzureIoTClient 16:deba40344375 830 {
AzureIoTClient 42:448eecc3676e 831 bool okToJoin;
AzureIoTClient 62:5a4cdacf5090 832 size_t vector_size;
Azure.IoT Build 37:18310e4d888d 833
AzureIoTClient 16:deba40344375 834 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 835
AzureIoTClient 66:a419827cb051 836 if (iotHubClientInstance->TransportHandle != NULL)
AzureIoTClient 66:a419827cb051 837 {
AzureIoTClient 66:a419827cb051 838 /*Codes_SRS_IOTHUBCLIENT_01_007: [ The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetNotificationMessageCallback shall be joined. ]*/
AzureIoTClient 66:a419827cb051 839 okToJoin = IoTHubTransport_SignalEndWorkerThread(iotHubClientInstance->TransportHandle, iotHubClientHandle);
AzureIoTClient 66:a419827cb051 840 }
AzureIoTClient 66:a419827cb051 841
AzureIoTClient 42:448eecc3676e 842 /*Codes_SRS_IOTHUBCLIENT_02_043: [ IoTHubClient_Destroy shall lock the serializing lock and signal the worker thread (if any) to end ]*/
AzureIoTClient 42:448eecc3676e 843 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 844 {
AzureIoTClient 42:448eecc3676e 845 LogError("unable to Lock - - will still proceed to try to end the thread without locking");
AzureIoTClient 42:448eecc3676e 846 }
AzureIoTClient 42:448eecc3676e 847
AzureIoTClient 44:33dd78697616 848 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 849 /*Codes_SRS_IOTHUBCLIENT_02_069: [ IoTHubClient_Destroy shall free all data created by IoTHubClient_UploadToBlobAsync ]*/
AzureIoTClient 42:448eecc3676e 850 /*wait for all uploading threads to finish*/
AzureIoTClient 52:1cc3c6d07cad 851 while (singlylinkedlist_get_head_item(iotHubClientInstance->savedDataToBeCleaned) != NULL)
AzureIoTClient 42:448eecc3676e 852 {
AzureIoTClient 42:448eecc3676e 853 garbageCollectorImpl(iotHubClientInstance);
AzureIoTClient 42:448eecc3676e 854 }
AzureIoTClient 43:038d8511e817 855 #endif
AzureIoTClient 16:deba40344375 856 if (iotHubClientInstance->ThreadHandle != NULL)
AzureIoTClient 16:deba40344375 857 {
AzureIoTClient 42:448eecc3676e 858 iotHubClientInstance->StopThread = 1;
AzureIoTClient 42:448eecc3676e 859 okToJoin = true;
AzureIoTClient 42:448eecc3676e 860 }
AzureIoTClient 42:448eecc3676e 861 else
AzureIoTClient 42:448eecc3676e 862 {
AzureIoTClient 42:448eecc3676e 863 okToJoin = false;
AzureIoTClient 16:deba40344375 864 }
Azure.IoT Build 37:18310e4d888d 865
AzureIoTClient 16:deba40344375 866 /* Codes_SRS_IOTHUBCLIENT_01_006: [That includes destroying the IoTHubClient_LL instance by calling IoTHubClient_LL_Destroy.] */
AzureIoTClient 16:deba40344375 867 IoTHubClient_LL_Destroy(iotHubClientInstance->IoTHubClientLLHandle);
AzureIoTClient 16:deba40344375 868
AzureIoTClient 44:33dd78697616 869 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 43:038d8511e817 870 if (iotHubClientInstance->savedDataToBeCleaned != NULL)
AzureIoTClient 43:038d8511e817 871 {
AzureIoTClient 52:1cc3c6d07cad 872 singlylinkedlist_destroy(iotHubClientInstance->savedDataToBeCleaned);
AzureIoTClient 43:038d8511e817 873 }
AzureIoTClient 43:038d8511e817 874 #endif
AzureIoTClient 43:038d8511e817 875
AzureIoTClient 42:448eecc3676e 876 /*Codes_SRS_IOTHUBCLIENT_02_045: [ IoTHubClient_Destroy shall unlock the serializing lock. ]*/
AzureIoTClient 42:448eecc3676e 877 if (Unlock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 878 {
AzureIoTClient 42:448eecc3676e 879 LogError("unable to Unlock");
AzureIoTClient 42:448eecc3676e 880 }
Azure.IoT Build 37:18310e4d888d 881
AzureIoTClient 42:448eecc3676e 882 if (okToJoin == true)
AzureIoTClient 42:448eecc3676e 883 {
AzureIoTClient 42:448eecc3676e 884 if (iotHubClientInstance->ThreadHandle != NULL)
AzureIoTClient 42:448eecc3676e 885 {
AzureIoTClient 42:448eecc3676e 886 int res;
AzureIoTClient 42:448eecc3676e 887 /*Codes_SRS_IOTHUBCLIENT_01_007: [ The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetNotificationMessageCallback shall be joined. ]*/
AzureIoTClient 42:448eecc3676e 888 if (ThreadAPI_Join(iotHubClientInstance->ThreadHandle, &res) != THREADAPI_OK)
AzureIoTClient 42:448eecc3676e 889 {
AzureIoTClient 42:448eecc3676e 890 LogError("ThreadAPI_Join failed");
AzureIoTClient 42:448eecc3676e 891 }
AzureIoTClient 42:448eecc3676e 892 }
Azure.IoT.Build 54:6dcad9019a64 893
AzureIoTClient 42:448eecc3676e 894 if (iotHubClientInstance->TransportHandle != NULL)
AzureIoTClient 42:448eecc3676e 895 {
AzureIoTClient 42:448eecc3676e 896 /*Codes_SRS_IOTHUBCLIENT_01_007: [ The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetNotificationMessageCallback shall be joined. ]*/
AzureIoTClient 42:448eecc3676e 897 IoTHubTransport_JoinWorkerThread(iotHubClientInstance->TransportHandle, iotHubClientHandle);
AzureIoTClient 42:448eecc3676e 898 }
AzureIoTClient 42:448eecc3676e 899 }
AzureIoTClient 42:448eecc3676e 900
AzureIoTClient 55:59b527ab3452 901 vector_size = VECTOR_size(iotHubClientInstance->saved_user_callback_list);
AzureIoTClient 57:4524910c6445 902 size_t index = 0;
AzureIoTClient 57:4524910c6445 903 for (index = 0; index < vector_size; index++)
AzureIoTClient 55:59b527ab3452 904 {
AzureIoTClient 55:59b527ab3452 905 USER_CALLBACK_INFO* queue_cb_info = (USER_CALLBACK_INFO*)VECTOR_element(iotHubClientInstance->saved_user_callback_list, index);
AzureIoTClient 55:59b527ab3452 906 if (queue_cb_info != NULL)
AzureIoTClient 55:59b527ab3452 907 {
AzureIoTClient 62:5a4cdacf5090 908 if ((queue_cb_info->type == CALLBACK_TYPE_DEVICE_METHOD) || (queue_cb_info->type == CALLBACK_TYPE_INBOUD_DEVICE_METHOD))
AzureIoTClient 55:59b527ab3452 909 {
AzureIoTClient 55:59b527ab3452 910 STRING_delete(queue_cb_info->iothub_callback.method_cb_info.method_name);
AzureIoTClient 55:59b527ab3452 911 BUFFER_delete(queue_cb_info->iothub_callback.method_cb_info.payload);
AzureIoTClient 55:59b527ab3452 912 }
AzureIoTClient 55:59b527ab3452 913 else if (queue_cb_info->type == CALLBACK_TYPE_DEVICE_TWIN)
AzureIoTClient 55:59b527ab3452 914 {
AzureIoTClient 55:59b527ab3452 915 if (queue_cb_info->iothub_callback.dev_twin_cb_info.payLoad != NULL)
AzureIoTClient 55:59b527ab3452 916 {
AzureIoTClient 55:59b527ab3452 917 free(queue_cb_info->iothub_callback.dev_twin_cb_info.payLoad);
AzureIoTClient 55:59b527ab3452 918 }
AzureIoTClient 55:59b527ab3452 919 }
AzureIoTClient 59:7e89be231352 920 else if (queue_cb_info->type == CALLBACK_TYPE_EVENT_CONFIRM)
AzureIoTClient 59:7e89be231352 921 {
AzureIoTClient 59:7e89be231352 922 if (iotHubClientInstance->event_confirm_callback)
AzureIoTClient 59:7e89be231352 923 {
AzureIoTClient 59:7e89be231352 924 iotHubClientInstance->event_confirm_callback(queue_cb_info->iothub_callback.event_confirm_cb_info.confirm_result, queue_cb_info->userContextCallback);
AzureIoTClient 59:7e89be231352 925 }
AzureIoTClient 59:7e89be231352 926 }
AzureIoTClient 55:59b527ab3452 927 }
AzureIoTClient 55:59b527ab3452 928 }
Azure.IoT.Build 54:6dcad9019a64 929 VECTOR_destroy(iotHubClientInstance->saved_user_callback_list);
Azure.IoT.Build 54:6dcad9019a64 930
AzureIoTClient 42:448eecc3676e 931 if (iotHubClientInstance->TransportHandle == NULL)
AzureIoTClient 42:448eecc3676e 932 {
AzureIoTClient 42:448eecc3676e 933 /* Codes_SRS_IOTHUBCLIENT_01_032: [If the lock was allocated in IoTHubClient_Create, it shall be also freed..] */
AzureIoTClient 42:448eecc3676e 934 Lock_Deinit(iotHubClientInstance->LockHandle);
AzureIoTClient 42:448eecc3676e 935 }
Azure.IoT.Build 54:6dcad9019a64 936 if (iotHubClientInstance->devicetwin_user_context != NULL)
Azure.IoT.Build 54:6dcad9019a64 937 {
Azure.IoT.Build 54:6dcad9019a64 938 free(iotHubClientInstance->devicetwin_user_context);
Azure.IoT.Build 54:6dcad9019a64 939 }
AzureIoTClient 55:59b527ab3452 940 if (iotHubClientInstance->connection_status_user_context != NULL)
AzureIoTClient 55:59b527ab3452 941 {
AzureIoTClient 55:59b527ab3452 942 free(iotHubClientInstance->connection_status_user_context);
AzureIoTClient 55:59b527ab3452 943 }
AzureIoTClient 61:8b85a4e797cf 944 if (iotHubClientInstance->message_user_context != NULL)
AzureIoTClient 61:8b85a4e797cf 945 {
AzureIoTClient 61:8b85a4e797cf 946 free(iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 947 }
AzureIoTClient 62:5a4cdacf5090 948 if (iotHubClientInstance->method_user_context != NULL)
AzureIoTClient 62:5a4cdacf5090 949 {
AzureIoTClient 62:5a4cdacf5090 950 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 951 }
AzureIoTClient 16:deba40344375 952 free(iotHubClientInstance);
AzureIoTClient 16:deba40344375 953 }
AzureIoTClient 16:deba40344375 954 }
AzureIoTClient 16:deba40344375 955
AzureIoTClient 16:deba40344375 956 IOTHUB_CLIENT_RESULT IoTHubClient_SendEventAsync(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_MESSAGE_HANDLE eventMessageHandle, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK eventConfirmationCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 957 {
AzureIoTClient 16:deba40344375 958 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 959
AzureIoTClient 16:deba40344375 960 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 961 {
AzureIoTClient 16:deba40344375 962 /* Codes_SRS_IOTHUBCLIENT_01_011: [If iotHubClientHandle is NULL, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 963 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 964 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 965 }
AzureIoTClient 16:deba40344375 966 else
AzureIoTClient 16:deba40344375 967 {
AzureIoTClient 16:deba40344375 968 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 969
AzureIoTClient 66:a419827cb051 970 /* Codes_SRS_IOTHUBCLIENT_01_009: [IoTHubClient_SendEventAsync shall start the worker thread if it was not previously started.] */
AzureIoTClient 66:a419827cb051 971 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 16:deba40344375 972 {
AzureIoTClient 66:a419827cb051 973 /* Codes_SRS_IOTHUBCLIENT_01_010: [If starting the thread fails, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 974 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 975 LogError("Could not start worker thread");
AzureIoTClient 16:deba40344375 976 }
AzureIoTClient 16:deba40344375 977 else
AzureIoTClient 16:deba40344375 978 {
Azure.IoT.Build 54:6dcad9019a64 979 if (iotHubClientInstance->created_with_transport_handle == 0)
Azure.IoT.Build 54:6dcad9019a64 980 {
Azure.IoT.Build 54:6dcad9019a64 981 iotHubClientInstance->event_confirm_callback = eventConfirmationCallback;
Azure.IoT.Build 54:6dcad9019a64 982 }
AzureIoTClient 66:a419827cb051 983
AzureIoTClient 66:a419827cb051 984 /* Codes_SRS_IOTHUBCLIENT_01_025: [IoTHubClient_SendEventAsync shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 985 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 986 {
AzureIoTClient 66:a419827cb051 987 /* Codes_SRS_IOTHUBCLIENT_01_026: [If acquiring the lock fails, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 988 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 989 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 990 }
AzureIoTClient 16:deba40344375 991 else
AzureIoTClient 16:deba40344375 992 {
Azure.IoT.Build 54:6dcad9019a64 993 if (iotHubClientInstance->created_with_transport_handle != 0 || eventConfirmationCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 994 {
Azure.IoT.Build 54:6dcad9019a64 995 result = IoTHubClient_LL_SendEventAsync(iotHubClientInstance->IoTHubClientLLHandle, eventMessageHandle, eventConfirmationCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 996 }
Azure.IoT.Build 54:6dcad9019a64 997 else
Azure.IoT.Build 54:6dcad9019a64 998 {
Azure.IoT.Build 54:6dcad9019a64 999 /* 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 1000 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
Azure.IoT.Build 54:6dcad9019a64 1001 if (queue_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1002 {
Azure.IoT.Build 54:6dcad9019a64 1003 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1004 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1005 }
Azure.IoT.Build 54:6dcad9019a64 1006 else
Azure.IoT.Build 54:6dcad9019a64 1007 {
Azure.IoT.Build 54:6dcad9019a64 1008 queue_context->iotHubClientHandle = iotHubClientInstance;
Azure.IoT.Build 54:6dcad9019a64 1009 queue_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1010 /* 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 1011 /* 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 1012 result = IoTHubClient_LL_SendEventAsync(iotHubClientInstance->IoTHubClientLLHandle, eventMessageHandle, iothub_ll_event_confirm_callback, queue_context);
Azure.IoT.Build 54:6dcad9019a64 1013 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1014 {
Azure.IoT.Build 54:6dcad9019a64 1015 LogError("IoTHubClient_LL_SendEventAsync failed");
Azure.IoT.Build 54:6dcad9019a64 1016 free(queue_context);
Azure.IoT.Build 54:6dcad9019a64 1017 }
Azure.IoT.Build 54:6dcad9019a64 1018 }
Azure.IoT.Build 54:6dcad9019a64 1019 }
AzureIoTClient 66:a419827cb051 1020
AzureIoTClient 66:a419827cb051 1021 /* Codes_SRS_IOTHUBCLIENT_01_025: [IoTHubClient_SendEventAsync shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 1022 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1023 }
AzureIoTClient 16:deba40344375 1024 }
AzureIoTClient 16:deba40344375 1025 }
AzureIoTClient 16:deba40344375 1026
AzureIoTClient 16:deba40344375 1027 return result;
AzureIoTClient 16:deba40344375 1028 }
AzureIoTClient 16:deba40344375 1029
AzureIoTClient 16:deba40344375 1030 IOTHUB_CLIENT_RESULT IoTHubClient_GetSendStatus(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 16:deba40344375 1031 {
AzureIoTClient 16:deba40344375 1032 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1033
AzureIoTClient 16:deba40344375 1034 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 1035 {
AzureIoTClient 16:deba40344375 1036 /* Codes_SRS_IOTHUBCLIENT_01_023: [If iotHubClientHandle is NULL, IoTHubClient_ GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 1037 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 1038 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 1039 }
AzureIoTClient 16:deba40344375 1040 else
AzureIoTClient 16:deba40344375 1041 {
AzureIoTClient 16:deba40344375 1042 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1043
AzureIoTClient 16:deba40344375 1044 /* Codes_SRS_IOTHUBCLIENT_01_033: [IoTHubClient_GetSendStatus shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 1045 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 1046 {
AzureIoTClient 16:deba40344375 1047 /* Codes_SRS_IOTHUBCLIENT_01_034: [If acquiring the lock fails, IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1048 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 39:2719651a5bee 1049 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 1050 }
AzureIoTClient 16:deba40344375 1051 else
AzureIoTClient 16:deba40344375 1052 {
AzureIoTClient 16:deba40344375 1053 /* 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 1054 /* Codes_SRS_IOTHUBCLIENT_01_024: [Otherwise, IoTHubClient_GetSendStatus shall return the result of IoTHubClient_LL_GetSendStatus.] */
AzureIoTClient 16:deba40344375 1055 result = IoTHubClient_LL_GetSendStatus(iotHubClientInstance->IoTHubClientLLHandle, iotHubClientStatus);
AzureIoTClient 16:deba40344375 1056
AzureIoTClient 16:deba40344375 1057 /* Codes_SRS_IOTHUBCLIENT_01_033: [IoTHubClient_GetSendStatus shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 1058 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1059 }
AzureIoTClient 16:deba40344375 1060 }
AzureIoTClient 16:deba40344375 1061
AzureIoTClient 16:deba40344375 1062 return result;
AzureIoTClient 16:deba40344375 1063 }
AzureIoTClient 16:deba40344375 1064
AzureIoTClient 16:deba40344375 1065 IOTHUB_CLIENT_RESULT IoTHubClient_SetMessageCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 1066 {
AzureIoTClient 16:deba40344375 1067 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1068
AzureIoTClient 16:deba40344375 1069 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 1070 {
AzureIoTClient 16:deba40344375 1071 /* Codes_SRS_IOTHUBCLIENT_01_016: [If iotHubClientHandle is NULL, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 1072 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 1073 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 1074 }
AzureIoTClient 16:deba40344375 1075 else
AzureIoTClient 16:deba40344375 1076 {
AzureIoTClient 16:deba40344375 1077 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1078
AzureIoTClient 66:a419827cb051 1079 /* Codes_SRS_IOTHUBCLIENT_01_014: [IoTHubClient_SetMessageCallback shall start the worker thread if it was not previously started.] */
AzureIoTClient 66:a419827cb051 1080 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 16:deba40344375 1081 {
AzureIoTClient 66:a419827cb051 1082 /* Codes_SRS_IOTHUBCLIENT_01_015: [If starting the thread fails, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1083 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1084 LogError("Could not start worker thread");
AzureIoTClient 16:deba40344375 1085 }
AzureIoTClient 16:deba40344375 1086 else
AzureIoTClient 16:deba40344375 1087 {
AzureIoTClient 61:8b85a4e797cf 1088 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 61:8b85a4e797cf 1089 {
AzureIoTClient 61:8b85a4e797cf 1090 iotHubClientInstance->message_callback = messageCallback;
AzureIoTClient 61:8b85a4e797cf 1091 }
AzureIoTClient 61:8b85a4e797cf 1092
AzureIoTClient 66:a419827cb051 1093 /* Codes_SRS_IOTHUBCLIENT_01_027: [IoTHubClient_SetMessageCallback shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 1094 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 1095 {
AzureIoTClient 66:a419827cb051 1096 /* Codes_SRS_IOTHUBCLIENT_01_028: [If acquiring the lock fails, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1097 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1098 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 1099 }
AzureIoTClient 16:deba40344375 1100 else
AzureIoTClient 16:deba40344375 1101 {
AzureIoTClient 61:8b85a4e797cf 1102 if (iotHubClientInstance->message_user_context != NULL)
AzureIoTClient 61:8b85a4e797cf 1103 {
AzureIoTClient 61:8b85a4e797cf 1104 free(iotHubClientInstance->message_user_context);
AzureIoTClient 62:5a4cdacf5090 1105 iotHubClientInstance->message_user_context = NULL;
AzureIoTClient 61:8b85a4e797cf 1106 }
AzureIoTClient 61:8b85a4e797cf 1107 if (messageCallback == NULL)
AzureIoTClient 61:8b85a4e797cf 1108 {
AzureIoTClient 62:5a4cdacf5090 1109 result = IoTHubClient_LL_SetMessageCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, NULL, iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 1110 }
AzureIoTClient 61:8b85a4e797cf 1111 else if (iotHubClientInstance->created_with_transport_handle != 0)
AzureIoTClient 61:8b85a4e797cf 1112 {
AzureIoTClient 61:8b85a4e797cf 1113 result = IoTHubClient_LL_SetMessageCallback(iotHubClientInstance->IoTHubClientLLHandle, messageCallback, userContextCallback);
AzureIoTClient 61:8b85a4e797cf 1114 }
AzureIoTClient 61:8b85a4e797cf 1115 else
AzureIoTClient 61:8b85a4e797cf 1116 {
AzureIoTClient 61:8b85a4e797cf 1117 iotHubClientInstance->message_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 61:8b85a4e797cf 1118 if (iotHubClientInstance->message_user_context == NULL)
AzureIoTClient 61:8b85a4e797cf 1119 {
AzureIoTClient 61:8b85a4e797cf 1120 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 1121 LogError("Failed allocating QUEUE_CONTEXT");
AzureIoTClient 61:8b85a4e797cf 1122 }
AzureIoTClient 61:8b85a4e797cf 1123 else
AzureIoTClient 61:8b85a4e797cf 1124 {
AzureIoTClient 61:8b85a4e797cf 1125 iotHubClientInstance->message_user_context->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 61:8b85a4e797cf 1126 iotHubClientInstance->message_user_context->userContextCallback = userContextCallback;
AzureIoTClient 61:8b85a4e797cf 1127
AzureIoTClient 62:5a4cdacf5090 1128 /* 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 1129 /* 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 1130 result = IoTHubClient_LL_SetMessageCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_message_callback, iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 1131 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 61:8b85a4e797cf 1132 {
AzureIoTClient 61:8b85a4e797cf 1133 LogError("IoTHubClient_LL_SetMessageCallback failed");
AzureIoTClient 61:8b85a4e797cf 1134 free(iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 1135 iotHubClientInstance->message_user_context = NULL;
AzureIoTClient 61:8b85a4e797cf 1136 }
AzureIoTClient 61:8b85a4e797cf 1137 }
AzureIoTClient 61:8b85a4e797cf 1138 }
AzureIoTClient 66:a419827cb051 1139
AzureIoTClient 66:a419827cb051 1140 /* Codes_SRS_IOTHUBCLIENT_01_027: [IoTHubClient_SetMessageCallback shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 1141 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1142 }
AzureIoTClient 16:deba40344375 1143 }
AzureIoTClient 16:deba40344375 1144 }
AzureIoTClient 16:deba40344375 1145
AzureIoTClient 16:deba40344375 1146 return result;
AzureIoTClient 16:deba40344375 1147 }
AzureIoTClient 16:deba40344375 1148
AzureIoTClient 53:1e5a1ca1f274 1149 IOTHUB_CLIENT_RESULT IoTHubClient_SetConnectionStatusCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK connectionStatusCallback, void * userContextCallback)
AzureIoTClient 52:1cc3c6d07cad 1150 {
AzureIoTClient 53:1e5a1ca1f274 1151 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1152
AzureIoTClient 53:1e5a1ca1f274 1153 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1154 {
AzureIoTClient 53:1e5a1ca1f274 1155 /* Codes_SRS_IOTHUBCLIENT_25_076: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ] */
AzureIoTClient 53:1e5a1ca1f274 1156 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1157 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 1158 }
AzureIoTClient 53:1e5a1ca1f274 1159 else
AzureIoTClient 53:1e5a1ca1f274 1160 {
AzureIoTClient 53:1e5a1ca1f274 1161 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 52:1cc3c6d07cad 1162
AzureIoTClient 66:a419827cb051 1163 /* Codes_SRS_IOTHUBCLIENT_25_081: [ `IoTHubClient_SetConnectionStatusCallback` shall start the worker thread if it was not previously started. ]*/
AzureIoTClient 66:a419827cb051 1164 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1165 {
AzureIoTClient 66:a419827cb051 1166 /* Codes_SRS_IOTHUBCLIENT_25_083: [ If starting the thread fails, `IoTHubClient_SetConnectionStatusCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1167 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1168 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1169 }
AzureIoTClient 53:1e5a1ca1f274 1170 else
AzureIoTClient 53:1e5a1ca1f274 1171 {
AzureIoTClient 66:a419827cb051 1172 /* Codes_SRS_IOTHUBCLIENT_25_087: [ `IoTHubClient_SetConnectionStatusCallback` shall be made thread-safe by using the lock created in `IoTHubClient_Create`. ] */
AzureIoTClient 66:a419827cb051 1173 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
Azure.IoT.Build 54:6dcad9019a64 1174 {
AzureIoTClient 66:a419827cb051 1175 /* Codes_SRS_IOTHUBCLIENT_25_088: [ If acquiring the lock fails, `IoTHubClient_SetConnectionStatusCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1176 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1177 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1178 }
AzureIoTClient 53:1e5a1ca1f274 1179 else
AzureIoTClient 53:1e5a1ca1f274 1180 {
AzureIoTClient 66:a419827cb051 1181 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 66:a419827cb051 1182 {
AzureIoTClient 66:a419827cb051 1183 iotHubClientInstance->connection_status_callback = connectionStatusCallback;
AzureIoTClient 66:a419827cb051 1184 }
AzureIoTClient 66:a419827cb051 1185
Azure.IoT.Build 54:6dcad9019a64 1186 if (iotHubClientInstance->created_with_transport_handle != 0 || connectionStatusCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 1187 {
Azure.IoT.Build 54:6dcad9019a64 1188 /* 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 1189 result = IoTHubClient_LL_SetConnectionStatusCallback(iotHubClientInstance->IoTHubClientLLHandle, connectionStatusCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 1190 }
Azure.IoT.Build 54:6dcad9019a64 1191 else
Azure.IoT.Build 54:6dcad9019a64 1192 {
AzureIoTClient 55:59b527ab3452 1193 if (iotHubClientInstance->connection_status_user_context != NULL)
AzureIoTClient 55:59b527ab3452 1194 {
AzureIoTClient 55:59b527ab3452 1195 free(iotHubClientInstance->connection_status_user_context);
AzureIoTClient 55:59b527ab3452 1196 }
AzureIoTClient 55:59b527ab3452 1197 iotHubClientInstance->connection_status_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 55:59b527ab3452 1198 if (iotHubClientInstance->connection_status_user_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1199 {
Azure.IoT.Build 54:6dcad9019a64 1200 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1201 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1202 }
Azure.IoT.Build 54:6dcad9019a64 1203 else
Azure.IoT.Build 54:6dcad9019a64 1204 {
AzureIoTClient 55:59b527ab3452 1205 iotHubClientInstance->connection_status_user_context->iotHubClientHandle = iotHubClientInstance;
AzureIoTClient 55:59b527ab3452 1206 iotHubClientInstance->connection_status_user_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1207
Azure.IoT.Build 54:6dcad9019a64 1208 /* 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 1209 result = IoTHubClient_LL_SetConnectionStatusCallback(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_connection_status_callback, iotHubClientInstance->connection_status_user_context);
Azure.IoT.Build 54:6dcad9019a64 1210 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1211 {
Azure.IoT.Build 54:6dcad9019a64 1212 LogError("IoTHubClient_LL_SetConnectionStatusCallback failed");
AzureIoTClient 55:59b527ab3452 1213 free(iotHubClientInstance->connection_status_user_context);
AzureIoTClient 61:8b85a4e797cf 1214 iotHubClientInstance->connection_status_user_context = NULL;
Azure.IoT.Build 54:6dcad9019a64 1215 }
Azure.IoT.Build 54:6dcad9019a64 1216 }
Azure.IoT.Build 54:6dcad9019a64 1217 }
AzureIoTClient 66:a419827cb051 1218 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1219 }
AzureIoTClient 53:1e5a1ca1f274 1220 }
AzureIoTClient 53:1e5a1ca1f274 1221 }
AzureIoTClient 52:1cc3c6d07cad 1222 return result;
AzureIoTClient 52:1cc3c6d07cad 1223 }
AzureIoTClient 52:1cc3c6d07cad 1224
AzureIoTClient 53:1e5a1ca1f274 1225 IOTHUB_CLIENT_RESULT IoTHubClient_SetRetryPolicy(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1226 {
AzureIoTClient 53:1e5a1ca1f274 1227 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1228
AzureIoTClient 53:1e5a1ca1f274 1229 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1230 {
AzureIoTClient 53:1e5a1ca1f274 1231 /* Codes_SRS_IOTHUBCLIENT_25_076: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ] */
AzureIoTClient 53:1e5a1ca1f274 1232 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1233 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 1234 }
AzureIoTClient 53:1e5a1ca1f274 1235 else
AzureIoTClient 53:1e5a1ca1f274 1236 {
AzureIoTClient 53:1e5a1ca1f274 1237 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1238
AzureIoTClient 66:a419827cb051 1239 /* Codes_SRS_IOTHUBCLIENT_25_073: [ `IoTHubClient_SetRetryPolicy` shall start the worker thread if it was not previously started. ] */
AzureIoTClient 66:a419827cb051 1240 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1241 {
AzureIoTClient 66:a419827cb051 1242 /* Codes_SRS_IOTHUBCLIENT_25_075: [ If starting the thread fails, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1243 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1244 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1245 }
AzureIoTClient 53:1e5a1ca1f274 1246 else
AzureIoTClient 53:1e5a1ca1f274 1247 {
AzureIoTClient 66:a419827cb051 1248 /* Codes_SRS_IOTHUBCLIENT_25_079: [ `IoTHubClient_SetRetryPolicy` shall be made thread-safe by using the lock created in `IoTHubClient_Create`.] */
AzureIoTClient 66:a419827cb051 1249 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1250 {
AzureIoTClient 66:a419827cb051 1251 /* Codes_SRS_IOTHUBCLIENT_25_080: [ If acquiring the lock fails, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1252 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1253 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1254 }
AzureIoTClient 53:1e5a1ca1f274 1255 else
AzureIoTClient 53:1e5a1ca1f274 1256 {
AzureIoTClient 53:1e5a1ca1f274 1257 /* 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 1258 result = IoTHubClient_LL_SetRetryPolicy(iotHubClientInstance->IoTHubClientLLHandle, retryPolicy, retryTimeoutLimitInSeconds);
AzureIoTClient 66:a419827cb051 1259 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1260 }
AzureIoTClient 53:1e5a1ca1f274 1261
AzureIoTClient 53:1e5a1ca1f274 1262 }
AzureIoTClient 53:1e5a1ca1f274 1263 }
AzureIoTClient 52:1cc3c6d07cad 1264
AzureIoTClient 52:1cc3c6d07cad 1265 return result;
AzureIoTClient 52:1cc3c6d07cad 1266 }
AzureIoTClient 52:1cc3c6d07cad 1267
Azure.IoT.Build 54:6dcad9019a64 1268 IOTHUB_CLIENT_RESULT IoTHubClient_GetRetryPolicy(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY* retryPolicy, size_t* retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1269 {
AzureIoTClient 53:1e5a1ca1f274 1270 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1271
AzureIoTClient 53:1e5a1ca1f274 1272 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1273 {
AzureIoTClient 53:1e5a1ca1f274 1274 /* Codes_SRS_IOTHUBCLIENT_25_092: [ If `iotHubClientHandle` is `NULL`, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1275 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1276 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 1277 }
AzureIoTClient 53:1e5a1ca1f274 1278 else
AzureIoTClient 53:1e5a1ca1f274 1279 {
AzureIoTClient 53:1e5a1ca1f274 1280 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1281
AzureIoTClient 66:a419827cb051 1282 /* Codes_SRS_IOTHUBCLIENT_25_089: [ `IoTHubClient_GetRetryPolicy` shall start the worker thread if it was not previously started.]*/
AzureIoTClient 66:a419827cb051 1283 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1284 {
AzureIoTClient 66:a419827cb051 1285 /* Codes_SRS_IOTHUBCLIENT_25_091: [ If starting the thread fails, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`.]*/
AzureIoTClient 53:1e5a1ca1f274 1286 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1287 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1288 }
AzureIoTClient 53:1e5a1ca1f274 1289 else
AzureIoTClient 53:1e5a1ca1f274 1290 {
AzureIoTClient 66:a419827cb051 1291 /* Codes_SRS_IOTHUBCLIENT_25_095: [ `IoTHubClient_GetRetryPolicy` shall be made thread-safe by using the lock created in `IoTHubClient_Create`. ]*/
AzureIoTClient 66:a419827cb051 1292 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1293 {
AzureIoTClient 66:a419827cb051 1294 /* Codes_SRS_IOTHUBCLIENT_25_096: [ If acquiring the lock fails, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1295 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1296 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1297 }
AzureIoTClient 53:1e5a1ca1f274 1298 else
AzureIoTClient 53:1e5a1ca1f274 1299 {
AzureIoTClient 53:1e5a1ca1f274 1300 /* 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 1301 result = IoTHubClient_LL_GetRetryPolicy(iotHubClientInstance->IoTHubClientLLHandle, retryPolicy, retryTimeoutLimitInSeconds);
AzureIoTClient 66:a419827cb051 1302 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1303 }
AzureIoTClient 53:1e5a1ca1f274 1304 }
AzureIoTClient 53:1e5a1ca1f274 1305 }
AzureIoTClient 52:1cc3c6d07cad 1306
AzureIoTClient 52:1cc3c6d07cad 1307 return result;
AzureIoTClient 52:1cc3c6d07cad 1308 }
AzureIoTClient 52:1cc3c6d07cad 1309
AzureIoTClient 16:deba40344375 1310 IOTHUB_CLIENT_RESULT IoTHubClient_GetLastMessageReceiveTime(IOTHUB_CLIENT_HANDLE iotHubClientHandle, time_t* lastMessageReceiveTime)
AzureIoTClient 16:deba40344375 1311 {
AzureIoTClient 16:deba40344375 1312 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1313
AzureIoTClient 16:deba40344375 1314 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 1315 {
AzureIoTClient 16:deba40344375 1316 /* Codes_SRS_IOTHUBCLIENT_01_020: [If iotHubClientHandle is NULL, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 1317 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 1318 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 1319 }
AzureIoTClient 16:deba40344375 1320 else
AzureIoTClient 16:deba40344375 1321 {
AzureIoTClient 16:deba40344375 1322 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1323
AzureIoTClient 16:deba40344375 1324 /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 1325 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 1326 {
AzureIoTClient 16:deba40344375 1327 /* Codes_SRS_IOTHUBCLIENT_01_036: [If acquiring the lock fails, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1328 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 39:2719651a5bee 1329 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 1330 }
AzureIoTClient 16:deba40344375 1331 else
AzureIoTClient 16:deba40344375 1332 {
AzureIoTClient 16:deba40344375 1333 /* 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 1334 /* Codes_SRS_IOTHUBCLIENT_01_021: [Otherwise, IoTHubClient_GetLastMessageReceiveTime shall return the result of IoTHubClient_LL_GetLastMessageReceiveTime.] */
AzureIoTClient 16:deba40344375 1335 result = IoTHubClient_LL_GetLastMessageReceiveTime(iotHubClientInstance->IoTHubClientLLHandle, lastMessageReceiveTime);
AzureIoTClient 16:deba40344375 1336
AzureIoTClient 16:deba40344375 1337 /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 53:1e5a1ca1f274 1338 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1339 }
AzureIoTClient 16:deba40344375 1340 }
AzureIoTClient 16:deba40344375 1341
AzureIoTClient 16:deba40344375 1342 return result;
AzureIoTClient 16:deba40344375 1343 }
AzureIoTClient 16:deba40344375 1344
AzureIoTClient 16:deba40344375 1345 IOTHUB_CLIENT_RESULT IoTHubClient_SetOption(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const char* optionName, const void* value)
AzureIoTClient 16:deba40344375 1346 {
AzureIoTClient 16:deba40344375 1347 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1348 /*Codes_SRS_IOTHUBCLIENT_02_034: [If parameter iotHubClientHandle is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 42:448eecc3676e 1349 /*Codes_SRS_IOTHUBCLIENT_02_035: [ If parameter optionName is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1350 /*Codes_SRS_IOTHUBCLIENT_02_036: [ If parameter value is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 16:deba40344375 1351 if (
AzureIoTClient 16:deba40344375 1352 (iotHubClientHandle == NULL) ||
AzureIoTClient 16:deba40344375 1353 (optionName == NULL) ||
AzureIoTClient 16:deba40344375 1354 (value == NULL)
AzureIoTClient 16:deba40344375 1355 )
AzureIoTClient 16:deba40344375 1356 {
AzureIoTClient 16:deba40344375 1357 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1358 LogError("invalid arg (NULL)");
AzureIoTClient 16:deba40344375 1359 }
AzureIoTClient 16:deba40344375 1360 else
AzureIoTClient 16:deba40344375 1361 {
AzureIoTClient 16:deba40344375 1362 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1363
AzureIoTClient 40:1a94db9139ea 1364 /* Codes_SRS_IOTHUBCLIENT_01_041: [ IoTHubClient_SetOption shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 40:1a94db9139ea 1365 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 40:1a94db9139ea 1366 {
AzureIoTClient 66:a419827cb051 1367 /* Codes_SRS_IOTHUBCLIENT_01_042: [ If acquiring the lock fails, IoTHubClient_SetOption shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 40:1a94db9139ea 1368 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 40:1a94db9139ea 1369 LogError("Could not acquire lock");
AzureIoTClient 40:1a94db9139ea 1370 }
AzureIoTClient 40:1a94db9139ea 1371 else
AzureIoTClient 16:deba40344375 1372 {
AzureIoTClient 40:1a94db9139ea 1373 /*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 1374 result = IoTHubClient_LL_SetOption(iotHubClientInstance->IoTHubClientLLHandle, optionName, value);
AzureIoTClient 40:1a94db9139ea 1375 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 40:1a94db9139ea 1376 {
AzureIoTClient 40:1a94db9139ea 1377 LogError("IoTHubClient_LL_SetOption failed");
AzureIoTClient 40:1a94db9139ea 1378 }
AzureIoTClient 40:1a94db9139ea 1379
AzureIoTClient 53:1e5a1ca1f274 1380 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1381 }
AzureIoTClient 53:1e5a1ca1f274 1382 }
AzureIoTClient 53:1e5a1ca1f274 1383 return result;
AzureIoTClient 53:1e5a1ca1f274 1384 }
AzureIoTClient 53:1e5a1ca1f274 1385
AzureIoTClient 53:1e5a1ca1f274 1386 IOTHUB_CLIENT_RESULT IoTHubClient_SetDeviceTwinCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1387 {
AzureIoTClient 53:1e5a1ca1f274 1388 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1389
AzureIoTClient 53:1e5a1ca1f274 1390 /*Codes_SRS_IOTHUBCLIENT_10_001: [** `IoTHubClient_SetDeviceTwinCallback` shall fail and return `IOTHUB_CLIENT_INVALID_ARG` if parameter `iotHubClientHandle` is `NULL`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1391 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1392 {
AzureIoTClient 53:1e5a1ca1f274 1393 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1394 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 1395 }
AzureIoTClient 53:1e5a1ca1f274 1396 else
AzureIoTClient 53:1e5a1ca1f274 1397 {
AzureIoTClient 53:1e5a1ca1f274 1398 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1399
AzureIoTClient 66:a419827cb051 1400 /*Codes_SRS_IOTHUBCLIENT_10_003: [** If the transport connection is shared, the thread shall be started by calling `IoTHubTransport_StartWorkerThread`. ]*/
AzureIoTClient 66:a419827cb051 1401 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1402 {
AzureIoTClient 66:a419827cb051 1403 /*Codes_SRS_IOTHUBCLIENT_10_004: [** If starting the thread fails, `IoTHubClient_SetDeviceTwinCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1404 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1405 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1406 }
AzureIoTClient 53:1e5a1ca1f274 1407 else
AzureIoTClient 53:1e5a1ca1f274 1408 {
Azure.IoT.Build 54:6dcad9019a64 1409 if (iotHubClientInstance->created_with_transport_handle == 0)
Azure.IoT.Build 54:6dcad9019a64 1410 {
Azure.IoT.Build 54:6dcad9019a64 1411 iotHubClientInstance->desired_state_callback = deviceTwinCallback;
Azure.IoT.Build 54:6dcad9019a64 1412 }
AzureIoTClient 66:a419827cb051 1413
AzureIoTClient 66:a419827cb051 1414 /*Codes_SRS_IOTHUBCLIENT_10_020: [** `IoTHubClient_SetDeviceTwinCallback` shall be made thread - safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1415 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1416 {
AzureIoTClient 66:a419827cb051 1417 /*Codes_SRS_IOTHUBCLIENT_10_002: [** If acquiring the lock fails, `IoTHubClient_SetDeviceTwinCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1418 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1419 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1420 }
AzureIoTClient 53:1e5a1ca1f274 1421 else
AzureIoTClient 53:1e5a1ca1f274 1422 {
Azure.IoT.Build 54:6dcad9019a64 1423 if (iotHubClientInstance->created_with_transport_handle != 0 || deviceTwinCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 1424 {
Azure.IoT.Build 54:6dcad9019a64 1425 /*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 1426 result = IoTHubClient_LL_SetDeviceTwinCallback(iotHubClientInstance->IoTHubClientLLHandle, deviceTwinCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 1427 }
Azure.IoT.Build 54:6dcad9019a64 1428 else
AzureIoTClient 53:1e5a1ca1f274 1429 {
Azure.IoT.Build 54:6dcad9019a64 1430 if (iotHubClientInstance->devicetwin_user_context != NULL)
Azure.IoT.Build 54:6dcad9019a64 1431 {
Azure.IoT.Build 54:6dcad9019a64 1432 free(iotHubClientInstance->devicetwin_user_context);
Azure.IoT.Build 54:6dcad9019a64 1433 }
Azure.IoT.Build 54:6dcad9019a64 1434
Azure.IoT.Build 54:6dcad9019a64 1435 /*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 1436 iotHubClientInstance->devicetwin_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
Azure.IoT.Build 54:6dcad9019a64 1437 if (iotHubClientInstance->devicetwin_user_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1438 {
Azure.IoT.Build 54:6dcad9019a64 1439 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1440 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1441 }
Azure.IoT.Build 54:6dcad9019a64 1442 else
Azure.IoT.Build 54:6dcad9019a64 1443 {
Azure.IoT.Build 54:6dcad9019a64 1444 /*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 1445 iotHubClientInstance->devicetwin_user_context->iotHubClientHandle = iotHubClientInstance;
AzureIoTClient 55:59b527ab3452 1446 iotHubClientInstance->devicetwin_user_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1447 result = IoTHubClient_LL_SetDeviceTwinCallback(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_device_twin_callback, iotHubClientInstance->devicetwin_user_context);
Azure.IoT.Build 54:6dcad9019a64 1448 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1449 {
Azure.IoT.Build 54:6dcad9019a64 1450 LogError("IoTHubClient_LL_SetDeviceTwinCallback failed");
Azure.IoT.Build 54:6dcad9019a64 1451 free(iotHubClientInstance->devicetwin_user_context);
AzureIoTClient 61:8b85a4e797cf 1452 iotHubClientInstance->devicetwin_user_context = NULL;
Azure.IoT.Build 54:6dcad9019a64 1453 }
Azure.IoT.Build 54:6dcad9019a64 1454 }
AzureIoTClient 53:1e5a1ca1f274 1455 }
AzureIoTClient 66:a419827cb051 1456
AzureIoTClient 66:a419827cb051 1457 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1458 }
AzureIoTClient 16:deba40344375 1459 }
AzureIoTClient 16:deba40344375 1460 }
AzureIoTClient 16:deba40344375 1461 return result;
AzureIoTClient 16:deba40344375 1462 }
AzureIoTClient 42:448eecc3676e 1463
AzureIoTClient 53:1e5a1ca1f274 1464 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 1465 {
AzureIoTClient 53:1e5a1ca1f274 1466 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1467
AzureIoTClient 53:1e5a1ca1f274 1468 /*Codes_SRS_IOTHUBCLIENT_10_013: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1469 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1470 {
AzureIoTClient 53:1e5a1ca1f274 1471 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1472 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 1473 }
AzureIoTClient 53:1e5a1ca1f274 1474 else
AzureIoTClient 53:1e5a1ca1f274 1475 {
AzureIoTClient 53:1e5a1ca1f274 1476 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1477
AzureIoTClient 66:a419827cb051 1478 /*Codes_SRS_IOTHUBCLIENT_10_015: [** If the transport connection is shared, the thread shall be started by calling `IoTHubTransport_StartWorkerThread`. ]*/
AzureIoTClient 66:a419827cb051 1479 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1480 {
AzureIoTClient 66:a419827cb051 1481 /*Codes_SRS_IOTHUBCLIENT_10_016: [** If starting the thread fails, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1482 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1483 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1484 }
AzureIoTClient 53:1e5a1ca1f274 1485 else
AzureIoTClient 53:1e5a1ca1f274 1486 {
Azure.IoT.Build 54:6dcad9019a64 1487 if (iotHubClientInstance->created_with_transport_handle == 0)
Azure.IoT.Build 54:6dcad9019a64 1488 {
Azure.IoT.Build 54:6dcad9019a64 1489 iotHubClientInstance->reported_state_callback = reportedStateCallback;
Azure.IoT.Build 54:6dcad9019a64 1490 }
AzureIoTClient 66:a419827cb051 1491
AzureIoTClient 66:a419827cb051 1492 /*Codes_SRS_IOTHUBCLIENT_10_021: [** `IoTHubClient_SendReportedState` shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1493 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1494 {
AzureIoTClient 66:a419827cb051 1495 /*Codes_SRS_IOTHUBCLIENT_10_014: [** If acquiring the lock fails, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1496 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1497 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1498 }
AzureIoTClient 53:1e5a1ca1f274 1499 else
AzureIoTClient 53:1e5a1ca1f274 1500 {
Azure.IoT.Build 54:6dcad9019a64 1501 if (iotHubClientInstance->created_with_transport_handle != 0 || reportedStateCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 1502 {
Azure.IoT.Build 54:6dcad9019a64 1503 /*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 1504 /*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 1505 result = IoTHubClient_LL_SendReportedState(iotHubClientInstance->IoTHubClientLLHandle, reportedState, size, reportedStateCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 1506 }
Azure.IoT.Build 54:6dcad9019a64 1507 else
AzureIoTClient 53:1e5a1ca1f274 1508 {
Azure.IoT.Build 54:6dcad9019a64 1509 /* 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 1510 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
Azure.IoT.Build 54:6dcad9019a64 1511 if (queue_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1512 {
Azure.IoT.Build 54:6dcad9019a64 1513 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1514 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1515 }
Azure.IoT.Build 54:6dcad9019a64 1516 else
Azure.IoT.Build 54:6dcad9019a64 1517 {
Azure.IoT.Build 54:6dcad9019a64 1518 queue_context->iotHubClientHandle = iotHubClientInstance;
Azure.IoT.Build 54:6dcad9019a64 1519 queue_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1520 /*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 1521 /*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 1522 result = IoTHubClient_LL_SendReportedState(iotHubClientInstance->IoTHubClientLLHandle, reportedState, size, iothub_ll_reported_state_callback, queue_context);
Azure.IoT.Build 54:6dcad9019a64 1523 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1524 {
Azure.IoT.Build 54:6dcad9019a64 1525 LogError("IoTHubClient_LL_SendReportedState failed");
Azure.IoT.Build 54:6dcad9019a64 1526 free(queue_context);
Azure.IoT.Build 54:6dcad9019a64 1527 }
Azure.IoT.Build 54:6dcad9019a64 1528 }
AzureIoTClient 53:1e5a1ca1f274 1529 }
AzureIoTClient 66:a419827cb051 1530
AzureIoTClient 66:a419827cb051 1531 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1532 }
AzureIoTClient 53:1e5a1ca1f274 1533 }
AzureIoTClient 53:1e5a1ca1f274 1534 }
AzureIoTClient 53:1e5a1ca1f274 1535 return result;
AzureIoTClient 53:1e5a1ca1f274 1536 }
AzureIoTClient 53:1e5a1ca1f274 1537
AzureIoTClient 53:1e5a1ca1f274 1538 IOTHUB_CLIENT_RESULT IoTHubClient_SetDeviceMethodCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC deviceMethodCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1539 {
AzureIoTClient 53:1e5a1ca1f274 1540 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1541
AzureIoTClient 53:1e5a1ca1f274 1542 /*Codes_SRS_IOTHUBCLIENT_12_012: [ If iotHubClientHandle is NULL, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 53:1e5a1ca1f274 1543 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1544 {
AzureIoTClient 53:1e5a1ca1f274 1545 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1546 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 1547 }
AzureIoTClient 53:1e5a1ca1f274 1548 else
AzureIoTClient 53:1e5a1ca1f274 1549 {
AzureIoTClient 53:1e5a1ca1f274 1550 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1551
AzureIoTClient 66:a419827cb051 1552 /*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 1553 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1554 {
AzureIoTClient 66:a419827cb051 1555 /*Codes_SRS_IOTHUBCLIENT_12_015: [ If starting the thread fails, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 53:1e5a1ca1f274 1556 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1557 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1558 }
AzureIoTClient 53:1e5a1ca1f274 1559 else
AzureIoTClient 53:1e5a1ca1f274 1560 {
AzureIoTClient 62:5a4cdacf5090 1561 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 62:5a4cdacf5090 1562 {
AzureIoTClient 62:5a4cdacf5090 1563 iotHubClientInstance->device_method_callback = deviceMethodCallback;
AzureIoTClient 62:5a4cdacf5090 1564 }
AzureIoTClient 66:a419827cb051 1565
AzureIoTClient 66:a419827cb051 1566 /*Codes_SRS_IOTHUBCLIENT_12_018: [ IoTHubClient_SetDeviceMethodCallback shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1567 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1568 {
AzureIoTClient 66:a419827cb051 1569 /*Codes_SRS_IOTHUBCLIENT_12_013: [ If acquiring the lock fails, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 53:1e5a1ca1f274 1570 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1571 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1572 }
AzureIoTClient 53:1e5a1ca1f274 1573 else
AzureIoTClient 53:1e5a1ca1f274 1574 {
AzureIoTClient 62:5a4cdacf5090 1575 if (iotHubClientInstance->method_user_context)
AzureIoTClient 62:5a4cdacf5090 1576 {
AzureIoTClient 62:5a4cdacf5090 1577 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1578 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 62:5a4cdacf5090 1579 }
AzureIoTClient 62:5a4cdacf5090 1580 if (deviceMethodCallback == NULL)
AzureIoTClient 62:5a4cdacf5090 1581 {
AzureIoTClient 62:5a4cdacf5090 1582 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, NULL, NULL);
AzureIoTClient 62:5a4cdacf5090 1583 }
AzureIoTClient 62:5a4cdacf5090 1584 else
AzureIoTClient 53:1e5a1ca1f274 1585 {
AzureIoTClient 62:5a4cdacf5090 1586 iotHubClientInstance->method_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 62:5a4cdacf5090 1587 if (iotHubClientInstance->method_user_context == NULL)
AzureIoTClient 62:5a4cdacf5090 1588 {
AzureIoTClient 62:5a4cdacf5090 1589 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1590 LogError("Failed allocating QUEUE_CONTEXT");
AzureIoTClient 62:5a4cdacf5090 1591 }
AzureIoTClient 62:5a4cdacf5090 1592 else
AzureIoTClient 62:5a4cdacf5090 1593 {
AzureIoTClient 62:5a4cdacf5090 1594 iotHubClientInstance->method_user_context->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 62:5a4cdacf5090 1595 iotHubClientInstance->method_user_context->userContextCallback = userContextCallback;
AzureIoTClient 62:5a4cdacf5090 1596
AzureIoTClient 62:5a4cdacf5090 1597 /*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 1598 /*Codes_SRS_IOTHUBCLIENT_12_017: [ When IoTHubClient_LL_SetDeviceMethodCallback is called, IoTHubClient_SetDeviceMethodCallback shall return the result of IoTHubClient_LL_SetDeviceMethodCallback. ]*/
AzureIoTClient 62:5a4cdacf5090 1599 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_device_method_callback, iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1600 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 62:5a4cdacf5090 1601 {
AzureIoTClient 62:5a4cdacf5090 1602 LogError("IoTHubClient_LL_SetDeviceMethodCallback_Ex failed");
AzureIoTClient 62:5a4cdacf5090 1603 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1604 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 62:5a4cdacf5090 1605 }
AzureIoTClient 66:a419827cb051 1606 else
AzureIoTClient 66:a419827cb051 1607 {
AzureIoTClient 66:a419827cb051 1608 iotHubClientInstance->device_method_callback = deviceMethodCallback;
AzureIoTClient 66:a419827cb051 1609 }
AzureIoTClient 62:5a4cdacf5090 1610 }
AzureIoTClient 53:1e5a1ca1f274 1611 }
AzureIoTClient 66:a419827cb051 1612
AzureIoTClient 66:a419827cb051 1613 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1614 }
AzureIoTClient 53:1e5a1ca1f274 1615
AzureIoTClient 53:1e5a1ca1f274 1616 }
AzureIoTClient 53:1e5a1ca1f274 1617 }
AzureIoTClient 53:1e5a1ca1f274 1618 return result;
AzureIoTClient 53:1e5a1ca1f274 1619 }
AzureIoTClient 53:1e5a1ca1f274 1620
AzureIoTClient 55:59b527ab3452 1621 IOTHUB_CLIENT_RESULT IoTHubClient_SetDeviceMethodCallback_Ex(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK inboundDeviceMethodCallback, void* userContextCallback)
AzureIoTClient 55:59b527ab3452 1622 {
AzureIoTClient 55:59b527ab3452 1623 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1624
AzureIoTClient 55:59b527ab3452 1625 /*Codes_SRS_IOTHUBCLIENT_07_001: [ If iotHubClientHandle is NULL, IoTHubClient_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 55:59b527ab3452 1626 if (iotHubClientHandle == NULL)
AzureIoTClient 55:59b527ab3452 1627 {
AzureIoTClient 55:59b527ab3452 1628 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1629 LogError("invalid arg (NULL)");
AzureIoTClient 55:59b527ab3452 1630 }
AzureIoTClient 55:59b527ab3452 1631 else
AzureIoTClient 55:59b527ab3452 1632 {
AzureIoTClient 55:59b527ab3452 1633 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1634
AzureIoTClient 66:a419827cb051 1635 /*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 1636 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 55:59b527ab3452 1637 {
AzureIoTClient 66:a419827cb051 1638 /*Codes_SRS_IOTHUBCLIENT_07_004: [ If starting the thread fails, IoTHubClient_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 55:59b527ab3452 1639 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1640 LogError("Could not start worker thread");
AzureIoTClient 55:59b527ab3452 1641 }
AzureIoTClient 55:59b527ab3452 1642 else
AzureIoTClient 55:59b527ab3452 1643 {
AzureIoTClient 55:59b527ab3452 1644 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 55:59b527ab3452 1645 {
AzureIoTClient 62:5a4cdacf5090 1646 iotHubClientInstance->inbound_device_method_callback = inboundDeviceMethodCallback;
AzureIoTClient 55:59b527ab3452 1647 }
AzureIoTClient 66:a419827cb051 1648
AzureIoTClient 66:a419827cb051 1649 /*Codes_SRS_IOTHUBCLIENT_07_007: [ IoTHubClient_SetDeviceMethodCallback_Ex shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1650 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 55:59b527ab3452 1651 {
AzureIoTClient 66:a419827cb051 1652 /*Codes_SRS_IOTHUBCLIENT_07_002: [ If acquiring the lock fails, IoTHubClient_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 55:59b527ab3452 1653 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1654 LogError("Could not acquire lock");
AzureIoTClient 55:59b527ab3452 1655 }
AzureIoTClient 55:59b527ab3452 1656 else
AzureIoTClient 55:59b527ab3452 1657 {
AzureIoTClient 62:5a4cdacf5090 1658 if (iotHubClientInstance->method_user_context)
AzureIoTClient 62:5a4cdacf5090 1659 {
AzureIoTClient 62:5a4cdacf5090 1660 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1661 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 62:5a4cdacf5090 1662 }
AzureIoTClient 62:5a4cdacf5090 1663 if (inboundDeviceMethodCallback == NULL)
AzureIoTClient 55:59b527ab3452 1664 {
AzureIoTClient 55:59b527ab3452 1665 /* 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 1666 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, NULL, NULL);
AzureIoTClient 55:59b527ab3452 1667 }
AzureIoTClient 55:59b527ab3452 1668 else
AzureIoTClient 55:59b527ab3452 1669 {
AzureIoTClient 62:5a4cdacf5090 1670 iotHubClientInstance->method_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 62:5a4cdacf5090 1671 if (iotHubClientInstance->method_user_context == NULL)
AzureIoTClient 55:59b527ab3452 1672 {
AzureIoTClient 55:59b527ab3452 1673 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1674 LogError("Failed allocating QUEUE_CONTEXT");
AzureIoTClient 55:59b527ab3452 1675 }
AzureIoTClient 55:59b527ab3452 1676 else
AzureIoTClient 55:59b527ab3452 1677 {
AzureIoTClient 55:59b527ab3452 1678 /*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 1679 iotHubClientInstance->method_user_context->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 62:5a4cdacf5090 1680 iotHubClientInstance->method_user_context->userContextCallback = userContextCallback;
AzureIoTClient 62:5a4cdacf5090 1681
AzureIoTClient 55:59b527ab3452 1682 /* 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 1683 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_inbound_device_method_callback, iotHubClientInstance->method_user_context);
AzureIoTClient 55:59b527ab3452 1684 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 55:59b527ab3452 1685 {
AzureIoTClient 62:5a4cdacf5090 1686 LogError("IoTHubClient_LL_SetDeviceMethodCallback_Ex failed");
AzureIoTClient 62:5a4cdacf5090 1687 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1688 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 55:59b527ab3452 1689 }
AzureIoTClient 66:a419827cb051 1690 else
AzureIoTClient 66:a419827cb051 1691 {
AzureIoTClient 66:a419827cb051 1692 iotHubClientInstance->inbound_device_method_callback = inboundDeviceMethodCallback;
AzureIoTClient 66:a419827cb051 1693 }
AzureIoTClient 55:59b527ab3452 1694 }
AzureIoTClient 55:59b527ab3452 1695 }
AzureIoTClient 66:a419827cb051 1696
AzureIoTClient 66:a419827cb051 1697 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 55:59b527ab3452 1698 }
AzureIoTClient 55:59b527ab3452 1699 }
AzureIoTClient 55:59b527ab3452 1700 }
AzureIoTClient 55:59b527ab3452 1701 return result;
AzureIoTClient 55:59b527ab3452 1702 }
AzureIoTClient 55:59b527ab3452 1703
AzureIoTClient 55:59b527ab3452 1704 IOTHUB_CLIENT_RESULT IoTHubClient_DeviceMethodResponse(IOTHUB_CLIENT_HANDLE iotHubClientHandle, METHOD_HANDLE methodId, const unsigned char* response, size_t respSize, int statusCode)
AzureIoTClient 55:59b527ab3452 1705 {
AzureIoTClient 55:59b527ab3452 1706 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1707
AzureIoTClient 55:59b527ab3452 1708 /*Codes_SRS_IOTHUBCLIENT_12_012: [ If iotHubClientHandle is NULL, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 55:59b527ab3452 1709 if (iotHubClientHandle == NULL)
AzureIoTClient 55:59b527ab3452 1710 {
AzureIoTClient 55:59b527ab3452 1711 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1712 LogError("invalid arg (NULL)");
AzureIoTClient 55:59b527ab3452 1713 }
AzureIoTClient 55:59b527ab3452 1714 else
AzureIoTClient 55:59b527ab3452 1715 {
AzureIoTClient 55:59b527ab3452 1716 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1717
AzureIoTClient 55:59b527ab3452 1718 /*Codes_SRS_IOTHUBCLIENT_12_018: [ IoTHubClient_SetDeviceMethodCallback shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 55:59b527ab3452 1719 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 55:59b527ab3452 1720 {
AzureIoTClient 55:59b527ab3452 1721 /*Codes_SRS_IOTHUBCLIENT_12_013: [ If acquiring the lock fails, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 55:59b527ab3452 1722 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1723 LogError("Could not acquire lock");
AzureIoTClient 55:59b527ab3452 1724 }
AzureIoTClient 55:59b527ab3452 1725 else
AzureIoTClient 55:59b527ab3452 1726 {
AzureIoTClient 55:59b527ab3452 1727 result = IoTHubClient_LL_DeviceMethodResponse(iotHubClientInstance->IoTHubClientLLHandle, methodId, response, respSize, statusCode);
AzureIoTClient 55:59b527ab3452 1728 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 55:59b527ab3452 1729 {
AzureIoTClient 55:59b527ab3452 1730 LogError("IoTHubClient_LL_DeviceMethodResponse failed");
AzureIoTClient 55:59b527ab3452 1731 }
AzureIoTClient 55:59b527ab3452 1732 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 55:59b527ab3452 1733 }
AzureIoTClient 55:59b527ab3452 1734 }
AzureIoTClient 55:59b527ab3452 1735 return result;
AzureIoTClient 55:59b527ab3452 1736 }
AzureIoTClient 53:1e5a1ca1f274 1737
AzureIoTClient 44:33dd78697616 1738 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 1739 static int uploadingThread(void *data)
AzureIoTClient 42:448eecc3676e 1740 {
AzureIoTClient 42:448eecc3676e 1741 UPLOADTOBLOB_SAVED_DATA* savedData = (UPLOADTOBLOB_SAVED_DATA*)data;
AzureIoTClient 42:448eecc3676e 1742
AzureIoTClient 62:5a4cdacf5090 1743 if (Lock(savedData->iotHubClientHandle->LockHandle) == LOCK_OK)
AzureIoTClient 42:448eecc3676e 1744 {
AzureIoTClient 62:5a4cdacf5090 1745 IOTHUB_CLIENT_FILE_UPLOAD_RESULT upload_result;
AzureIoTClient 62:5a4cdacf5090 1746 /*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 1747 /*not having it protected means multiple simultaneous uploads can happen*/
AzureIoTClient 62:5a4cdacf5090 1748 /*Codes_SRS_IOTHUBCLIENT_02_054: [ The thread shall call IoTHubClient_LL_UploadToBlob passing the information packed in the structure. ]*/
AzureIoTClient 62:5a4cdacf5090 1749 if (IoTHubClient_LL_UploadToBlob(savedData->iotHubClientHandle->IoTHubClientLLHandle, savedData->destinationFileName, savedData->source, savedData->size) == IOTHUB_CLIENT_OK)
AzureIoTClient 62:5a4cdacf5090 1750 {
AzureIoTClient 62:5a4cdacf5090 1751 upload_result = FILE_UPLOAD_OK;
AzureIoTClient 62:5a4cdacf5090 1752 }
AzureIoTClient 62:5a4cdacf5090 1753 else
AzureIoTClient 62:5a4cdacf5090 1754 {
AzureIoTClient 62:5a4cdacf5090 1755 LogError("unable to IoTHubClient_LL_UploadToBlob");
AzureIoTClient 62:5a4cdacf5090 1756 upload_result = FILE_UPLOAD_ERROR;
AzureIoTClient 62:5a4cdacf5090 1757 }
AzureIoTClient 62:5a4cdacf5090 1758 (void)Unlock(savedData->iotHubClientHandle->LockHandle);
AzureIoTClient 62:5a4cdacf5090 1759
AzureIoTClient 42:448eecc3676e 1760 if (savedData->iotHubClientFileUploadCallback != NULL)
AzureIoTClient 42:448eecc3676e 1761 {
AzureIoTClient 42:448eecc3676e 1762 /*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 1763 savedData->iotHubClientFileUploadCallback(upload_result, savedData->context);
AzureIoTClient 42:448eecc3676e 1764 }
AzureIoTClient 42:448eecc3676e 1765 }
AzureIoTClient 42:448eecc3676e 1766 else
AzureIoTClient 42:448eecc3676e 1767 {
AzureIoTClient 62:5a4cdacf5090 1768 LogError("Lock failed");
AzureIoTClient 42:448eecc3676e 1769 }
AzureIoTClient 42:448eecc3676e 1770
AzureIoTClient 42:448eecc3676e 1771 /*Codes_SRS_IOTHUBCLIENT_02_071: [ The thread shall mark itself as disposable. ]*/
AzureIoTClient 42:448eecc3676e 1772 if (Lock(savedData->lockGarbage) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 1773 {
AzureIoTClient 42:448eecc3676e 1774 LogError("unable to Lock - trying anyway");
AzureIoTClient 42:448eecc3676e 1775 savedData->canBeGarbageCollected = 1;
AzureIoTClient 42:448eecc3676e 1776 }
AzureIoTClient 42:448eecc3676e 1777 else
AzureIoTClient 42:448eecc3676e 1778 {
AzureIoTClient 42:448eecc3676e 1779 savedData->canBeGarbageCollected = 1;
AzureIoTClient 42:448eecc3676e 1780
AzureIoTClient 42:448eecc3676e 1781 if (Unlock(savedData->lockGarbage) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 1782 {
AzureIoTClient 42:448eecc3676e 1783 LogError("unable to Unlock after locking");
AzureIoTClient 42:448eecc3676e 1784 }
AzureIoTClient 42:448eecc3676e 1785 }
AzureIoTClient 42:448eecc3676e 1786 return 0;
AzureIoTClient 42:448eecc3676e 1787 }
AzureIoTClient 43:038d8511e817 1788 #endif
AzureIoTClient 42:448eecc3676e 1789
AzureIoTClient 44:33dd78697616 1790 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 1791 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 1792 {
AzureIoTClient 42:448eecc3676e 1793 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1794 /*Codes_SRS_IOTHUBCLIENT_02_047: [ If iotHubClientHandle is NULL then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1795 /*Codes_SRS_IOTHUBCLIENT_02_048: [ If destinationFileName is NULL then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1796 /*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 1797 if (
AzureIoTClient 42:448eecc3676e 1798 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 1799 (destinationFileName == NULL) ||
AzureIoTClient 42:448eecc3676e 1800 ((source == NULL) && (size > 0))
AzureIoTClient 42:448eecc3676e 1801 )
AzureIoTClient 42:448eecc3676e 1802 {
AzureIoTClient 42:448eecc3676e 1803 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 1804 iotHubClientHandle,
AzureIoTClient 42:448eecc3676e 1805 destinationFileName,
AzureIoTClient 42:448eecc3676e 1806 source,
AzureIoTClient 42:448eecc3676e 1807 size,
AzureIoTClient 42:448eecc3676e 1808 iotHubClientFileUploadCallback,
AzureIoTClient 42:448eecc3676e 1809 context
AzureIoTClient 42:448eecc3676e 1810 );
AzureIoTClient 42:448eecc3676e 1811 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 1812 }
AzureIoTClient 42:448eecc3676e 1813 else
AzureIoTClient 42:448eecc3676e 1814 {
AzureIoTClient 42:448eecc3676e 1815 /*Codes_SRS_IOTHUBCLIENT_02_051: [IoTHubClient_UploadToBlobAsync shall copy the souce, size, iotHubClientFileUploadCallback, context into a structure.]*/
AzureIoTClient 42:448eecc3676e 1816 UPLOADTOBLOB_SAVED_DATA *savedData = (UPLOADTOBLOB_SAVED_DATA *)malloc(sizeof(UPLOADTOBLOB_SAVED_DATA));
AzureIoTClient 42:448eecc3676e 1817 if (savedData == NULL)
AzureIoTClient 42:448eecc3676e 1818 {
AzureIoTClient 42:448eecc3676e 1819 /*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 1820 LogError("unable to malloc - oom");
AzureIoTClient 42:448eecc3676e 1821 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1822 }
AzureIoTClient 42:448eecc3676e 1823 else
AzureIoTClient 42:448eecc3676e 1824 {
AzureIoTClient 42:448eecc3676e 1825 if (mallocAndStrcpy_s((char**)&savedData->destinationFileName, destinationFileName) != 0)
AzureIoTClient 42:448eecc3676e 1826 {
AzureIoTClient 42:448eecc3676e 1827 /*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 1828 LogError("unable to mallocAndStrcpy_s");
AzureIoTClient 42:448eecc3676e 1829 free(savedData);
AzureIoTClient 42:448eecc3676e 1830 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1831 }
AzureIoTClient 42:448eecc3676e 1832 else
AzureIoTClient 42:448eecc3676e 1833 {
AzureIoTClient 42:448eecc3676e 1834 savedData->size = size;
AzureIoTClient 42:448eecc3676e 1835 int sourceCloned;
AzureIoTClient 42:448eecc3676e 1836 if (size == 0)
AzureIoTClient 42:448eecc3676e 1837 {
AzureIoTClient 42:448eecc3676e 1838 savedData->source = NULL;
AzureIoTClient 42:448eecc3676e 1839 sourceCloned = 1;
AzureIoTClient 42:448eecc3676e 1840 }
AzureIoTClient 42:448eecc3676e 1841 else
AzureIoTClient 42:448eecc3676e 1842 {
AzureIoTClient 42:448eecc3676e 1843 savedData->source = (unsigned char*)malloc(size);
AzureIoTClient 42:448eecc3676e 1844 if (savedData->source == NULL)
AzureIoTClient 42:448eecc3676e 1845 {
AzureIoTClient 42:448eecc3676e 1846 /*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 1847 LogError("unable to malloc - oom");
AzureIoTClient 42:448eecc3676e 1848 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1849 free(savedData);
AzureIoTClient 42:448eecc3676e 1850 sourceCloned = 0;
AzureIoTClient 42:448eecc3676e 1851 }
AzureIoTClient 42:448eecc3676e 1852 else
AzureIoTClient 42:448eecc3676e 1853 {
AzureIoTClient 42:448eecc3676e 1854 sourceCloned = 1;
AzureIoTClient 42:448eecc3676e 1855 }
AzureIoTClient 42:448eecc3676e 1856 }
AzureIoTClient 42:448eecc3676e 1857
AzureIoTClient 42:448eecc3676e 1858 if (sourceCloned == 0)
AzureIoTClient 42:448eecc3676e 1859 {
AzureIoTClient 42:448eecc3676e 1860 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1861 }
AzureIoTClient 42:448eecc3676e 1862 else
AzureIoTClient 42:448eecc3676e 1863 {
Azure.IoT.Build 54:6dcad9019a64 1864 IOTHUB_CLIENT_INSTANCE* iotHubClientHandleData = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
Azure.IoT.Build 54:6dcad9019a64 1865
AzureIoTClient 42:448eecc3676e 1866 savedData->iotHubClientFileUploadCallback = iotHubClientFileUploadCallback;
AzureIoTClient 42:448eecc3676e 1867 savedData->context = context;
AzureIoTClient 58:15b0d29b2667 1868 (void)memcpy(savedData->source, source, size);
Azure.IoT.Build 54:6dcad9019a64 1869
AzureIoTClient 66:a419827cb051 1870 if ((result = StartWorkerThreadIfNeeded(iotHubClientHandleData)) != IOTHUB_CLIENT_OK)
AzureIoTClient 42:448eecc3676e 1871 {
AzureIoTClient 42:448eecc3676e 1872 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1873 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1874 free(savedData);
AzureIoTClient 42:448eecc3676e 1875 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1876 LogError("Could not start worker thread");
AzureIoTClient 42:448eecc3676e 1877 }
AzureIoTClient 42:448eecc3676e 1878 else
AzureIoTClient 42:448eecc3676e 1879 {
AzureIoTClient 66:a419827cb051 1880 if (Lock(iotHubClientHandleData->LockHandle) != LOCK_OK) /*locking because the next statement is changing blobThreadsToBeJoined*/
AzureIoTClient 42:448eecc3676e 1881 {
AzureIoTClient 66:a419827cb051 1882 LogError("unable to lock");
AzureIoTClient 42:448eecc3676e 1883 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1884 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1885 free(savedData);
AzureIoTClient 42:448eecc3676e 1886 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1887 }
AzureIoTClient 42:448eecc3676e 1888 else
AzureIoTClient 42:448eecc3676e 1889 {
AzureIoTClient 42:448eecc3676e 1890 /*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 1891 LIST_ITEM_HANDLE item = singlylinkedlist_add(iotHubClientHandleData->savedDataToBeCleaned, savedData);
AzureIoTClient 42:448eecc3676e 1892 if (item == NULL)
AzureIoTClient 42:448eecc3676e 1893 {
AzureIoTClient 52:1cc3c6d07cad 1894 LogError("unable to singlylinkedlist_add");
AzureIoTClient 42:448eecc3676e 1895 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1896 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1897 free(savedData);
AzureIoTClient 42:448eecc3676e 1898 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1899 }
AzureIoTClient 42:448eecc3676e 1900 else
AzureIoTClient 42:448eecc3676e 1901 {
AzureIoTClient 42:448eecc3676e 1902 savedData->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 1903 savedData->canBeGarbageCollected = 0;
AzureIoTClient 42:448eecc3676e 1904 if ((savedData->lockGarbage = Lock_Init()) == NULL)
AzureIoTClient 42:448eecc3676e 1905 {
AzureIoTClient 52:1cc3c6d07cad 1906 (void)singlylinkedlist_remove(iotHubClientHandleData->savedDataToBeCleaned, item);
AzureIoTClient 42:448eecc3676e 1907 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1908 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1909 free(savedData);
AzureIoTClient 42:448eecc3676e 1910 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1911 LogError("unable to Lock_Init");
AzureIoTClient 42:448eecc3676e 1912 }
AzureIoTClient 42:448eecc3676e 1913 else
AzureIoTClient 42:448eecc3676e 1914 {
AzureIoTClient 42:448eecc3676e 1915 /*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 1916 if (ThreadAPI_Create(&savedData->uploadingThreadHandle, uploadingThread, savedData) != THREADAPI_OK)
AzureIoTClient 42:448eecc3676e 1917 {
AzureIoTClient 42:448eecc3676e 1918 /*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 1919 LogError("unablet to ThreadAPI_Create");
AzureIoTClient 42:448eecc3676e 1920 (void)Lock_Deinit(savedData->lockGarbage);
AzureIoTClient 52:1cc3c6d07cad 1921 (void)singlylinkedlist_remove(iotHubClientHandleData->savedDataToBeCleaned, item);
AzureIoTClient 42:448eecc3676e 1922 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1923 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1924 free(savedData);
AzureIoTClient 42:448eecc3676e 1925 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1926 }
AzureIoTClient 42:448eecc3676e 1927 else
AzureIoTClient 42:448eecc3676e 1928 {
AzureIoTClient 42:448eecc3676e 1929 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 1930 }
AzureIoTClient 42:448eecc3676e 1931 }
AzureIoTClient 42:448eecc3676e 1932 }
AzureIoTClient 66:a419827cb051 1933
AzureIoTClient 66:a419827cb051 1934 (void)Unlock(iotHubClientHandleData->LockHandle);
AzureIoTClient 42:448eecc3676e 1935 }
AzureIoTClient 42:448eecc3676e 1936 }
AzureIoTClient 42:448eecc3676e 1937 }
AzureIoTClient 42:448eecc3676e 1938 }
AzureIoTClient 42:448eecc3676e 1939 }
AzureIoTClient 42:448eecc3676e 1940 }
AzureIoTClient 42:448eecc3676e 1941 return result;
AzureIoTClient 42:448eecc3676e 1942 }
AzureIoTClient 44:33dd78697616 1943 #endif /*DONT_USE_UPLOADTOBLOB*/