Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

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

Committer:
AzureIoTClient
Date:
Fri Jul 14 16:37:50 2017 -0700
Revision:
71:0d498da5ece1
Parent:
67:ecebc2a41159
Child:
76:943524fee0b7
1.1.19

Who changed what in which revision?

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