Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

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

Committer:
AzureIoTClient
Date:
Mon Sep 25 13:37:53 2017 -0700
Revision:
76:943524fee0b7
Parent:
71:0d498da5ece1
Child:
78:74a8d3068204
1.1.24

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 16:deba40344375 865 if (iotHubClientInstance->ThreadHandle != NULL)
AzureIoTClient 16:deba40344375 866 {
AzureIoTClient 42:448eecc3676e 867 iotHubClientInstance->StopThread = 1;
AzureIoTClient 42:448eecc3676e 868 okToJoin = true;
AzureIoTClient 42:448eecc3676e 869 }
AzureIoTClient 42:448eecc3676e 870 else
AzureIoTClient 42:448eecc3676e 871 {
AzureIoTClient 42:448eecc3676e 872 okToJoin = false;
AzureIoTClient 16:deba40344375 873 }
Azure.IoT Build 37:18310e4d888d 874
AzureIoTClient 42:448eecc3676e 875 /*Codes_SRS_IOTHUBCLIENT_02_045: [ IoTHubClient_Destroy shall unlock the serializing lock. ]*/
AzureIoTClient 42:448eecc3676e 876 if (Unlock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 877 {
AzureIoTClient 42:448eecc3676e 878 LogError("unable to Unlock");
AzureIoTClient 42:448eecc3676e 879 }
Azure.IoT Build 37:18310e4d888d 880
AzureIoTClient 42:448eecc3676e 881 if (okToJoin == true)
AzureIoTClient 42:448eecc3676e 882 {
AzureIoTClient 42:448eecc3676e 883 if (iotHubClientInstance->ThreadHandle != NULL)
AzureIoTClient 42:448eecc3676e 884 {
AzureIoTClient 42:448eecc3676e 885 int res;
AzureIoTClient 42:448eecc3676e 886 /*Codes_SRS_IOTHUBCLIENT_01_007: [ The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetNotificationMessageCallback shall be joined. ]*/
AzureIoTClient 42:448eecc3676e 887 if (ThreadAPI_Join(iotHubClientInstance->ThreadHandle, &res) != THREADAPI_OK)
AzureIoTClient 42:448eecc3676e 888 {
AzureIoTClient 42:448eecc3676e 889 LogError("ThreadAPI_Join failed");
AzureIoTClient 42:448eecc3676e 890 }
AzureIoTClient 42:448eecc3676e 891 }
Azure.IoT.Build 54:6dcad9019a64 892
AzureIoTClient 42:448eecc3676e 893 if (iotHubClientInstance->TransportHandle != NULL)
AzureIoTClient 42:448eecc3676e 894 {
AzureIoTClient 42:448eecc3676e 895 /*Codes_SRS_IOTHUBCLIENT_01_007: [ The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetNotificationMessageCallback shall be joined. ]*/
AzureIoTClient 42:448eecc3676e 896 IoTHubTransport_JoinWorkerThread(iotHubClientInstance->TransportHandle, iotHubClientHandle);
AzureIoTClient 42:448eecc3676e 897 }
AzureIoTClient 42:448eecc3676e 898 }
AzureIoTClient 42:448eecc3676e 899
AzureIoTClient 76:943524fee0b7 900
AzureIoTClient 76:943524fee0b7 901 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 76:943524fee0b7 902 {
AzureIoTClient 76:943524fee0b7 903 LogError("unable to Lock - - will still proceed to try to end the thread without locking");
AzureIoTClient 76:943524fee0b7 904 }
AzureIoTClient 76:943524fee0b7 905
AzureIoTClient 76:943524fee0b7 906 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 76:943524fee0b7 907 /*Codes_SRS_IOTHUBCLIENT_02_069: [ IoTHubClient_Destroy shall free all data created by IoTHubClient_UploadToBlobAsync ]*/
AzureIoTClient 76:943524fee0b7 908 /*wait for all uploading threads to finish*/
AzureIoTClient 76:943524fee0b7 909 while (singlylinkedlist_get_head_item(iotHubClientInstance->savedDataToBeCleaned) != NULL)
AzureIoTClient 76:943524fee0b7 910 {
AzureIoTClient 76:943524fee0b7 911 garbageCollectorImpl(iotHubClientInstance);
AzureIoTClient 76:943524fee0b7 912 }
AzureIoTClient 76:943524fee0b7 913
AzureIoTClient 76:943524fee0b7 914 if (iotHubClientInstance->savedDataToBeCleaned != NULL)
AzureIoTClient 76:943524fee0b7 915 {
AzureIoTClient 76:943524fee0b7 916 singlylinkedlist_destroy(iotHubClientInstance->savedDataToBeCleaned);
AzureIoTClient 76:943524fee0b7 917 }
AzureIoTClient 76:943524fee0b7 918 #endif
AzureIoTClient 76:943524fee0b7 919
AzureIoTClient 76:943524fee0b7 920 /* Codes_SRS_IOTHUBCLIENT_01_006: [That includes destroying the IoTHubClient_LL instance by calling IoTHubClient_LL_Destroy.] */
AzureIoTClient 76:943524fee0b7 921 IoTHubClient_LL_Destroy(iotHubClientInstance->IoTHubClientLLHandle);
AzureIoTClient 76:943524fee0b7 922
AzureIoTClient 76:943524fee0b7 923 if (Unlock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 76:943524fee0b7 924 {
AzureIoTClient 76:943524fee0b7 925 LogError("unable to Unlock");
AzureIoTClient 76:943524fee0b7 926 }
AzureIoTClient 76:943524fee0b7 927
AzureIoTClient 76:943524fee0b7 928
AzureIoTClient 55:59b527ab3452 929 vector_size = VECTOR_size(iotHubClientInstance->saved_user_callback_list);
AzureIoTClient 57:4524910c6445 930 size_t index = 0;
AzureIoTClient 57:4524910c6445 931 for (index = 0; index < vector_size; index++)
AzureIoTClient 55:59b527ab3452 932 {
AzureIoTClient 55:59b527ab3452 933 USER_CALLBACK_INFO* queue_cb_info = (USER_CALLBACK_INFO*)VECTOR_element(iotHubClientInstance->saved_user_callback_list, index);
AzureIoTClient 55:59b527ab3452 934 if (queue_cb_info != NULL)
AzureIoTClient 55:59b527ab3452 935 {
AzureIoTClient 62:5a4cdacf5090 936 if ((queue_cb_info->type == CALLBACK_TYPE_DEVICE_METHOD) || (queue_cb_info->type == CALLBACK_TYPE_INBOUD_DEVICE_METHOD))
AzureIoTClient 55:59b527ab3452 937 {
AzureIoTClient 55:59b527ab3452 938 STRING_delete(queue_cb_info->iothub_callback.method_cb_info.method_name);
AzureIoTClient 55:59b527ab3452 939 BUFFER_delete(queue_cb_info->iothub_callback.method_cb_info.payload);
AzureIoTClient 55:59b527ab3452 940 }
AzureIoTClient 55:59b527ab3452 941 else if (queue_cb_info->type == CALLBACK_TYPE_DEVICE_TWIN)
AzureIoTClient 55:59b527ab3452 942 {
AzureIoTClient 55:59b527ab3452 943 if (queue_cb_info->iothub_callback.dev_twin_cb_info.payLoad != NULL)
AzureIoTClient 55:59b527ab3452 944 {
AzureIoTClient 55:59b527ab3452 945 free(queue_cb_info->iothub_callback.dev_twin_cb_info.payLoad);
AzureIoTClient 55:59b527ab3452 946 }
AzureIoTClient 55:59b527ab3452 947 }
AzureIoTClient 59:7e89be231352 948 else if (queue_cb_info->type == CALLBACK_TYPE_EVENT_CONFIRM)
AzureIoTClient 59:7e89be231352 949 {
AzureIoTClient 59:7e89be231352 950 if (iotHubClientInstance->event_confirm_callback)
AzureIoTClient 59:7e89be231352 951 {
AzureIoTClient 59:7e89be231352 952 iotHubClientInstance->event_confirm_callback(queue_cb_info->iothub_callback.event_confirm_cb_info.confirm_result, queue_cb_info->userContextCallback);
AzureIoTClient 59:7e89be231352 953 }
AzureIoTClient 59:7e89be231352 954 }
AzureIoTClient 55:59b527ab3452 955 }
AzureIoTClient 55:59b527ab3452 956 }
Azure.IoT.Build 54:6dcad9019a64 957 VECTOR_destroy(iotHubClientInstance->saved_user_callback_list);
Azure.IoT.Build 54:6dcad9019a64 958
AzureIoTClient 42:448eecc3676e 959 if (iotHubClientInstance->TransportHandle == NULL)
AzureIoTClient 42:448eecc3676e 960 {
AzureIoTClient 42:448eecc3676e 961 /* Codes_SRS_IOTHUBCLIENT_01_032: [If the lock was allocated in IoTHubClient_Create, it shall be also freed..] */
AzureIoTClient 42:448eecc3676e 962 Lock_Deinit(iotHubClientInstance->LockHandle);
AzureIoTClient 42:448eecc3676e 963 }
Azure.IoT.Build 54:6dcad9019a64 964 if (iotHubClientInstance->devicetwin_user_context != NULL)
Azure.IoT.Build 54:6dcad9019a64 965 {
Azure.IoT.Build 54:6dcad9019a64 966 free(iotHubClientInstance->devicetwin_user_context);
Azure.IoT.Build 54:6dcad9019a64 967 }
AzureIoTClient 55:59b527ab3452 968 if (iotHubClientInstance->connection_status_user_context != NULL)
AzureIoTClient 55:59b527ab3452 969 {
AzureIoTClient 55:59b527ab3452 970 free(iotHubClientInstance->connection_status_user_context);
AzureIoTClient 55:59b527ab3452 971 }
AzureIoTClient 61:8b85a4e797cf 972 if (iotHubClientInstance->message_user_context != NULL)
AzureIoTClient 61:8b85a4e797cf 973 {
AzureIoTClient 61:8b85a4e797cf 974 free(iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 975 }
AzureIoTClient 62:5a4cdacf5090 976 if (iotHubClientInstance->method_user_context != NULL)
AzureIoTClient 62:5a4cdacf5090 977 {
AzureIoTClient 62:5a4cdacf5090 978 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 979 }
AzureIoTClient 16:deba40344375 980 free(iotHubClientInstance);
AzureIoTClient 16:deba40344375 981 }
AzureIoTClient 16:deba40344375 982 }
AzureIoTClient 16:deba40344375 983
AzureIoTClient 16:deba40344375 984 IOTHUB_CLIENT_RESULT IoTHubClient_SendEventAsync(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_MESSAGE_HANDLE eventMessageHandle, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK eventConfirmationCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 985 {
AzureIoTClient 16:deba40344375 986 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 987
AzureIoTClient 16:deba40344375 988 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 989 {
AzureIoTClient 16:deba40344375 990 /* Codes_SRS_IOTHUBCLIENT_01_011: [If iotHubClientHandle is NULL, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 991 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 992 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 993 }
AzureIoTClient 16:deba40344375 994 else
AzureIoTClient 16:deba40344375 995 {
AzureIoTClient 16:deba40344375 996 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 997
AzureIoTClient 66:a419827cb051 998 /* Codes_SRS_IOTHUBCLIENT_01_009: [IoTHubClient_SendEventAsync shall start the worker thread if it was not previously started.] */
AzureIoTClient 66:a419827cb051 999 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 16:deba40344375 1000 {
AzureIoTClient 66:a419827cb051 1001 /* Codes_SRS_IOTHUBCLIENT_01_010: [If starting the thread fails, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1002 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1003 LogError("Could not start worker thread");
AzureIoTClient 16:deba40344375 1004 }
AzureIoTClient 16:deba40344375 1005 else
AzureIoTClient 16:deba40344375 1006 {
Azure.IoT.Build 54:6dcad9019a64 1007 if (iotHubClientInstance->created_with_transport_handle == 0)
Azure.IoT.Build 54:6dcad9019a64 1008 {
Azure.IoT.Build 54:6dcad9019a64 1009 iotHubClientInstance->event_confirm_callback = eventConfirmationCallback;
Azure.IoT.Build 54:6dcad9019a64 1010 }
AzureIoTClient 66:a419827cb051 1011
AzureIoTClient 66:a419827cb051 1012 /* Codes_SRS_IOTHUBCLIENT_01_025: [IoTHubClient_SendEventAsync shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 1013 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 1014 {
AzureIoTClient 66:a419827cb051 1015 /* Codes_SRS_IOTHUBCLIENT_01_026: [If acquiring the lock fails, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1016 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1017 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 1018 }
AzureIoTClient 16:deba40344375 1019 else
AzureIoTClient 16:deba40344375 1020 {
Azure.IoT.Build 54:6dcad9019a64 1021 if (iotHubClientInstance->created_with_transport_handle != 0 || eventConfirmationCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 1022 {
Azure.IoT.Build 54:6dcad9019a64 1023 result = IoTHubClient_LL_SendEventAsync(iotHubClientInstance->IoTHubClientLLHandle, eventMessageHandle, eventConfirmationCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 1024 }
Azure.IoT.Build 54:6dcad9019a64 1025 else
Azure.IoT.Build 54:6dcad9019a64 1026 {
Azure.IoT.Build 54:6dcad9019a64 1027 /* 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 1028 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
Azure.IoT.Build 54:6dcad9019a64 1029 if (queue_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1030 {
Azure.IoT.Build 54:6dcad9019a64 1031 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1032 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1033 }
Azure.IoT.Build 54:6dcad9019a64 1034 else
Azure.IoT.Build 54:6dcad9019a64 1035 {
Azure.IoT.Build 54:6dcad9019a64 1036 queue_context->iotHubClientHandle = iotHubClientInstance;
Azure.IoT.Build 54:6dcad9019a64 1037 queue_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1038 /* 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 1039 /* 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 1040 result = IoTHubClient_LL_SendEventAsync(iotHubClientInstance->IoTHubClientLLHandle, eventMessageHandle, iothub_ll_event_confirm_callback, queue_context);
Azure.IoT.Build 54:6dcad9019a64 1041 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1042 {
Azure.IoT.Build 54:6dcad9019a64 1043 LogError("IoTHubClient_LL_SendEventAsync failed");
Azure.IoT.Build 54:6dcad9019a64 1044 free(queue_context);
Azure.IoT.Build 54:6dcad9019a64 1045 }
Azure.IoT.Build 54:6dcad9019a64 1046 }
Azure.IoT.Build 54:6dcad9019a64 1047 }
AzureIoTClient 66:a419827cb051 1048
AzureIoTClient 66:a419827cb051 1049 /* Codes_SRS_IOTHUBCLIENT_01_025: [IoTHubClient_SendEventAsync shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 1050 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1051 }
AzureIoTClient 16:deba40344375 1052 }
AzureIoTClient 16:deba40344375 1053 }
AzureIoTClient 16:deba40344375 1054
AzureIoTClient 16:deba40344375 1055 return result;
AzureIoTClient 16:deba40344375 1056 }
AzureIoTClient 16:deba40344375 1057
AzureIoTClient 16:deba40344375 1058 IOTHUB_CLIENT_RESULT IoTHubClient_GetSendStatus(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 16:deba40344375 1059 {
AzureIoTClient 16:deba40344375 1060 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1061
AzureIoTClient 16:deba40344375 1062 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 1063 {
AzureIoTClient 16:deba40344375 1064 /* Codes_SRS_IOTHUBCLIENT_01_023: [If iotHubClientHandle is NULL, IoTHubClient_ GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 1065 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 1066 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 1067 }
AzureIoTClient 16:deba40344375 1068 else
AzureIoTClient 16:deba40344375 1069 {
AzureIoTClient 16:deba40344375 1070 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1071
AzureIoTClient 16:deba40344375 1072 /* Codes_SRS_IOTHUBCLIENT_01_033: [IoTHubClient_GetSendStatus shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 1073 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 1074 {
AzureIoTClient 16:deba40344375 1075 /* Codes_SRS_IOTHUBCLIENT_01_034: [If acquiring the lock fails, IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1076 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 39:2719651a5bee 1077 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 1078 }
AzureIoTClient 16:deba40344375 1079 else
AzureIoTClient 16:deba40344375 1080 {
AzureIoTClient 16:deba40344375 1081 /* 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 1082 /* Codes_SRS_IOTHUBCLIENT_01_024: [Otherwise, IoTHubClient_GetSendStatus shall return the result of IoTHubClient_LL_GetSendStatus.] */
AzureIoTClient 16:deba40344375 1083 result = IoTHubClient_LL_GetSendStatus(iotHubClientInstance->IoTHubClientLLHandle, iotHubClientStatus);
AzureIoTClient 16:deba40344375 1084
AzureIoTClient 16:deba40344375 1085 /* Codes_SRS_IOTHUBCLIENT_01_033: [IoTHubClient_GetSendStatus shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 1086 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1087 }
AzureIoTClient 16:deba40344375 1088 }
AzureIoTClient 16:deba40344375 1089
AzureIoTClient 16:deba40344375 1090 return result;
AzureIoTClient 16:deba40344375 1091 }
AzureIoTClient 16:deba40344375 1092
AzureIoTClient 16:deba40344375 1093 IOTHUB_CLIENT_RESULT IoTHubClient_SetMessageCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 1094 {
AzureIoTClient 16:deba40344375 1095 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1096
AzureIoTClient 16:deba40344375 1097 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 1098 {
AzureIoTClient 16:deba40344375 1099 /* Codes_SRS_IOTHUBCLIENT_01_016: [If iotHubClientHandle is NULL, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 1100 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 1101 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 1102 }
AzureIoTClient 16:deba40344375 1103 else
AzureIoTClient 16:deba40344375 1104 {
AzureIoTClient 16:deba40344375 1105 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1106
AzureIoTClient 66:a419827cb051 1107 /* Codes_SRS_IOTHUBCLIENT_01_014: [IoTHubClient_SetMessageCallback shall start the worker thread if it was not previously started.] */
AzureIoTClient 66:a419827cb051 1108 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 16:deba40344375 1109 {
AzureIoTClient 66:a419827cb051 1110 /* Codes_SRS_IOTHUBCLIENT_01_015: [If starting the thread fails, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1111 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1112 LogError("Could not start worker thread");
AzureIoTClient 16:deba40344375 1113 }
AzureIoTClient 16:deba40344375 1114 else
AzureIoTClient 16:deba40344375 1115 {
AzureIoTClient 61:8b85a4e797cf 1116 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 61:8b85a4e797cf 1117 {
AzureIoTClient 61:8b85a4e797cf 1118 iotHubClientInstance->message_callback = messageCallback;
AzureIoTClient 61:8b85a4e797cf 1119 }
AzureIoTClient 61:8b85a4e797cf 1120
AzureIoTClient 66:a419827cb051 1121 /* Codes_SRS_IOTHUBCLIENT_01_027: [IoTHubClient_SetMessageCallback shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 1122 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 1123 {
AzureIoTClient 66:a419827cb051 1124 /* Codes_SRS_IOTHUBCLIENT_01_028: [If acquiring the lock fails, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1125 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1126 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 1127 }
AzureIoTClient 16:deba40344375 1128 else
AzureIoTClient 16:deba40344375 1129 {
AzureIoTClient 61:8b85a4e797cf 1130 if (iotHubClientInstance->message_user_context != NULL)
AzureIoTClient 61:8b85a4e797cf 1131 {
AzureIoTClient 61:8b85a4e797cf 1132 free(iotHubClientInstance->message_user_context);
AzureIoTClient 62:5a4cdacf5090 1133 iotHubClientInstance->message_user_context = NULL;
AzureIoTClient 61:8b85a4e797cf 1134 }
AzureIoTClient 61:8b85a4e797cf 1135 if (messageCallback == NULL)
AzureIoTClient 61:8b85a4e797cf 1136 {
AzureIoTClient 62:5a4cdacf5090 1137 result = IoTHubClient_LL_SetMessageCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, NULL, iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 1138 }
AzureIoTClient 61:8b85a4e797cf 1139 else if (iotHubClientInstance->created_with_transport_handle != 0)
AzureIoTClient 61:8b85a4e797cf 1140 {
AzureIoTClient 61:8b85a4e797cf 1141 result = IoTHubClient_LL_SetMessageCallback(iotHubClientInstance->IoTHubClientLLHandle, messageCallback, userContextCallback);
AzureIoTClient 61:8b85a4e797cf 1142 }
AzureIoTClient 61:8b85a4e797cf 1143 else
AzureIoTClient 61:8b85a4e797cf 1144 {
AzureIoTClient 61:8b85a4e797cf 1145 iotHubClientInstance->message_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 61:8b85a4e797cf 1146 if (iotHubClientInstance->message_user_context == NULL)
AzureIoTClient 61:8b85a4e797cf 1147 {
AzureIoTClient 61:8b85a4e797cf 1148 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 61:8b85a4e797cf 1149 LogError("Failed allocating QUEUE_CONTEXT");
AzureIoTClient 61:8b85a4e797cf 1150 }
AzureIoTClient 61:8b85a4e797cf 1151 else
AzureIoTClient 61:8b85a4e797cf 1152 {
AzureIoTClient 61:8b85a4e797cf 1153 iotHubClientInstance->message_user_context->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 61:8b85a4e797cf 1154 iotHubClientInstance->message_user_context->userContextCallback = userContextCallback;
AzureIoTClient 61:8b85a4e797cf 1155
AzureIoTClient 62:5a4cdacf5090 1156 /* 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 1157 /* 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 1158 result = IoTHubClient_LL_SetMessageCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_message_callback, iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 1159 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 61:8b85a4e797cf 1160 {
AzureIoTClient 61:8b85a4e797cf 1161 LogError("IoTHubClient_LL_SetMessageCallback failed");
AzureIoTClient 61:8b85a4e797cf 1162 free(iotHubClientInstance->message_user_context);
AzureIoTClient 61:8b85a4e797cf 1163 iotHubClientInstance->message_user_context = NULL;
AzureIoTClient 61:8b85a4e797cf 1164 }
AzureIoTClient 61:8b85a4e797cf 1165 }
AzureIoTClient 61:8b85a4e797cf 1166 }
AzureIoTClient 66:a419827cb051 1167
AzureIoTClient 66:a419827cb051 1168 /* Codes_SRS_IOTHUBCLIENT_01_027: [IoTHubClient_SetMessageCallback shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 66:a419827cb051 1169 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1170 }
AzureIoTClient 16:deba40344375 1171 }
AzureIoTClient 16:deba40344375 1172 }
AzureIoTClient 16:deba40344375 1173
AzureIoTClient 16:deba40344375 1174 return result;
AzureIoTClient 16:deba40344375 1175 }
AzureIoTClient 16:deba40344375 1176
AzureIoTClient 53:1e5a1ca1f274 1177 IOTHUB_CLIENT_RESULT IoTHubClient_SetConnectionStatusCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK connectionStatusCallback, void * userContextCallback)
AzureIoTClient 52:1cc3c6d07cad 1178 {
AzureIoTClient 53:1e5a1ca1f274 1179 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1180
AzureIoTClient 53:1e5a1ca1f274 1181 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1182 {
AzureIoTClient 53:1e5a1ca1f274 1183 /* Codes_SRS_IOTHUBCLIENT_25_076: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ] */
AzureIoTClient 53:1e5a1ca1f274 1184 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1185 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 1186 }
AzureIoTClient 53:1e5a1ca1f274 1187 else
AzureIoTClient 53:1e5a1ca1f274 1188 {
AzureIoTClient 53:1e5a1ca1f274 1189 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 52:1cc3c6d07cad 1190
AzureIoTClient 66:a419827cb051 1191 /* Codes_SRS_IOTHUBCLIENT_25_081: [ `IoTHubClient_SetConnectionStatusCallback` shall start the worker thread if it was not previously started. ]*/
AzureIoTClient 66:a419827cb051 1192 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1193 {
AzureIoTClient 66:a419827cb051 1194 /* Codes_SRS_IOTHUBCLIENT_25_083: [ If starting the thread fails, `IoTHubClient_SetConnectionStatusCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1195 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1196 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1197 }
AzureIoTClient 53:1e5a1ca1f274 1198 else
AzureIoTClient 53:1e5a1ca1f274 1199 {
AzureIoTClient 66:a419827cb051 1200 /* Codes_SRS_IOTHUBCLIENT_25_087: [ `IoTHubClient_SetConnectionStatusCallback` shall be made thread-safe by using the lock created in `IoTHubClient_Create`. ] */
AzureIoTClient 66:a419827cb051 1201 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
Azure.IoT.Build 54:6dcad9019a64 1202 {
AzureIoTClient 66:a419827cb051 1203 /* Codes_SRS_IOTHUBCLIENT_25_088: [ If acquiring the lock fails, `IoTHubClient_SetConnectionStatusCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1204 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1205 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1206 }
AzureIoTClient 53:1e5a1ca1f274 1207 else
AzureIoTClient 53:1e5a1ca1f274 1208 {
AzureIoTClient 66:a419827cb051 1209 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 66:a419827cb051 1210 {
AzureIoTClient 66:a419827cb051 1211 iotHubClientInstance->connection_status_callback = connectionStatusCallback;
AzureIoTClient 66:a419827cb051 1212 }
AzureIoTClient 66:a419827cb051 1213
Azure.IoT.Build 54:6dcad9019a64 1214 if (iotHubClientInstance->created_with_transport_handle != 0 || connectionStatusCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 1215 {
Azure.IoT.Build 54:6dcad9019a64 1216 /* 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 1217 result = IoTHubClient_LL_SetConnectionStatusCallback(iotHubClientInstance->IoTHubClientLLHandle, connectionStatusCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 1218 }
Azure.IoT.Build 54:6dcad9019a64 1219 else
Azure.IoT.Build 54:6dcad9019a64 1220 {
AzureIoTClient 55:59b527ab3452 1221 if (iotHubClientInstance->connection_status_user_context != NULL)
AzureIoTClient 55:59b527ab3452 1222 {
AzureIoTClient 55:59b527ab3452 1223 free(iotHubClientInstance->connection_status_user_context);
AzureIoTClient 55:59b527ab3452 1224 }
AzureIoTClient 55:59b527ab3452 1225 iotHubClientInstance->connection_status_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 55:59b527ab3452 1226 if (iotHubClientInstance->connection_status_user_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1227 {
Azure.IoT.Build 54:6dcad9019a64 1228 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1229 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1230 }
Azure.IoT.Build 54:6dcad9019a64 1231 else
Azure.IoT.Build 54:6dcad9019a64 1232 {
AzureIoTClient 55:59b527ab3452 1233 iotHubClientInstance->connection_status_user_context->iotHubClientHandle = iotHubClientInstance;
AzureIoTClient 55:59b527ab3452 1234 iotHubClientInstance->connection_status_user_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1235
Azure.IoT.Build 54:6dcad9019a64 1236 /* 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 1237 result = IoTHubClient_LL_SetConnectionStatusCallback(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_connection_status_callback, iotHubClientInstance->connection_status_user_context);
Azure.IoT.Build 54:6dcad9019a64 1238 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1239 {
Azure.IoT.Build 54:6dcad9019a64 1240 LogError("IoTHubClient_LL_SetConnectionStatusCallback failed");
AzureIoTClient 55:59b527ab3452 1241 free(iotHubClientInstance->connection_status_user_context);
AzureIoTClient 61:8b85a4e797cf 1242 iotHubClientInstance->connection_status_user_context = NULL;
Azure.IoT.Build 54:6dcad9019a64 1243 }
Azure.IoT.Build 54:6dcad9019a64 1244 }
Azure.IoT.Build 54:6dcad9019a64 1245 }
AzureIoTClient 66:a419827cb051 1246 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1247 }
AzureIoTClient 53:1e5a1ca1f274 1248 }
AzureIoTClient 53:1e5a1ca1f274 1249 }
AzureIoTClient 52:1cc3c6d07cad 1250 return result;
AzureIoTClient 52:1cc3c6d07cad 1251 }
AzureIoTClient 52:1cc3c6d07cad 1252
AzureIoTClient 53:1e5a1ca1f274 1253 IOTHUB_CLIENT_RESULT IoTHubClient_SetRetryPolicy(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1254 {
AzureIoTClient 53:1e5a1ca1f274 1255 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1256
AzureIoTClient 53:1e5a1ca1f274 1257 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1258 {
AzureIoTClient 53:1e5a1ca1f274 1259 /* Codes_SRS_IOTHUBCLIENT_25_076: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ] */
AzureIoTClient 53:1e5a1ca1f274 1260 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1261 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 1262 }
AzureIoTClient 53:1e5a1ca1f274 1263 else
AzureIoTClient 53:1e5a1ca1f274 1264 {
AzureIoTClient 53:1e5a1ca1f274 1265 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1266
AzureIoTClient 66:a419827cb051 1267 /* Codes_SRS_IOTHUBCLIENT_25_073: [ `IoTHubClient_SetRetryPolicy` shall start the worker thread if it was not previously started. ] */
AzureIoTClient 66:a419827cb051 1268 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1269 {
AzureIoTClient 66:a419827cb051 1270 /* Codes_SRS_IOTHUBCLIENT_25_075: [ If starting the thread fails, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1271 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1272 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1273 }
AzureIoTClient 53:1e5a1ca1f274 1274 else
AzureIoTClient 53:1e5a1ca1f274 1275 {
AzureIoTClient 66:a419827cb051 1276 /* Codes_SRS_IOTHUBCLIENT_25_079: [ `IoTHubClient_SetRetryPolicy` shall be made thread-safe by using the lock created in `IoTHubClient_Create`.] */
AzureIoTClient 66:a419827cb051 1277 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1278 {
AzureIoTClient 66:a419827cb051 1279 /* Codes_SRS_IOTHUBCLIENT_25_080: [ If acquiring the lock fails, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1280 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1281 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1282 }
AzureIoTClient 53:1e5a1ca1f274 1283 else
AzureIoTClient 53:1e5a1ca1f274 1284 {
AzureIoTClient 53:1e5a1ca1f274 1285 /* 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 1286 result = IoTHubClient_LL_SetRetryPolicy(iotHubClientInstance->IoTHubClientLLHandle, retryPolicy, retryTimeoutLimitInSeconds);
AzureIoTClient 66:a419827cb051 1287 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1288 }
AzureIoTClient 53:1e5a1ca1f274 1289
AzureIoTClient 53:1e5a1ca1f274 1290 }
AzureIoTClient 53:1e5a1ca1f274 1291 }
AzureIoTClient 52:1cc3c6d07cad 1292
AzureIoTClient 52:1cc3c6d07cad 1293 return result;
AzureIoTClient 52:1cc3c6d07cad 1294 }
AzureIoTClient 52:1cc3c6d07cad 1295
Azure.IoT.Build 54:6dcad9019a64 1296 IOTHUB_CLIENT_RESULT IoTHubClient_GetRetryPolicy(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY* retryPolicy, size_t* retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1297 {
AzureIoTClient 53:1e5a1ca1f274 1298 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1299
AzureIoTClient 53:1e5a1ca1f274 1300 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1301 {
AzureIoTClient 53:1e5a1ca1f274 1302 /* Codes_SRS_IOTHUBCLIENT_25_092: [ If `iotHubClientHandle` is `NULL`, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1303 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1304 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 1305 }
AzureIoTClient 53:1e5a1ca1f274 1306 else
AzureIoTClient 53:1e5a1ca1f274 1307 {
AzureIoTClient 53:1e5a1ca1f274 1308 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1309
AzureIoTClient 66:a419827cb051 1310 /* Codes_SRS_IOTHUBCLIENT_25_089: [ `IoTHubClient_GetRetryPolicy` shall start the worker thread if it was not previously started.]*/
AzureIoTClient 66:a419827cb051 1311 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1312 {
AzureIoTClient 66:a419827cb051 1313 /* Codes_SRS_IOTHUBCLIENT_25_091: [ If starting the thread fails, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`.]*/
AzureIoTClient 53:1e5a1ca1f274 1314 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1315 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1316 }
AzureIoTClient 53:1e5a1ca1f274 1317 else
AzureIoTClient 53:1e5a1ca1f274 1318 {
AzureIoTClient 66:a419827cb051 1319 /* Codes_SRS_IOTHUBCLIENT_25_095: [ `IoTHubClient_GetRetryPolicy` shall be made thread-safe by using the lock created in `IoTHubClient_Create`. ]*/
AzureIoTClient 66:a419827cb051 1320 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1321 {
AzureIoTClient 66:a419827cb051 1322 /* Codes_SRS_IOTHUBCLIENT_25_096: [ If acquiring the lock fails, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1323 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1324 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1325 }
AzureIoTClient 53:1e5a1ca1f274 1326 else
AzureIoTClient 53:1e5a1ca1f274 1327 {
AzureIoTClient 53:1e5a1ca1f274 1328 /* 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 1329 result = IoTHubClient_LL_GetRetryPolicy(iotHubClientInstance->IoTHubClientLLHandle, retryPolicy, retryTimeoutLimitInSeconds);
AzureIoTClient 66:a419827cb051 1330 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1331 }
AzureIoTClient 53:1e5a1ca1f274 1332 }
AzureIoTClient 53:1e5a1ca1f274 1333 }
AzureIoTClient 52:1cc3c6d07cad 1334
AzureIoTClient 52:1cc3c6d07cad 1335 return result;
AzureIoTClient 52:1cc3c6d07cad 1336 }
AzureIoTClient 52:1cc3c6d07cad 1337
AzureIoTClient 16:deba40344375 1338 IOTHUB_CLIENT_RESULT IoTHubClient_GetLastMessageReceiveTime(IOTHUB_CLIENT_HANDLE iotHubClientHandle, time_t* lastMessageReceiveTime)
AzureIoTClient 16:deba40344375 1339 {
AzureIoTClient 16:deba40344375 1340 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1341
AzureIoTClient 16:deba40344375 1342 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 1343 {
AzureIoTClient 16:deba40344375 1344 /* Codes_SRS_IOTHUBCLIENT_01_020: [If iotHubClientHandle is NULL, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 1345 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 1346 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 1347 }
AzureIoTClient 16:deba40344375 1348 else
AzureIoTClient 16:deba40344375 1349 {
AzureIoTClient 16:deba40344375 1350 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1351
AzureIoTClient 16:deba40344375 1352 /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 1353 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 1354 {
AzureIoTClient 16:deba40344375 1355 /* Codes_SRS_IOTHUBCLIENT_01_036: [If acquiring the lock fails, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 1356 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 39:2719651a5bee 1357 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 1358 }
AzureIoTClient 16:deba40344375 1359 else
AzureIoTClient 16:deba40344375 1360 {
AzureIoTClient 16:deba40344375 1361 /* 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 1362 /* Codes_SRS_IOTHUBCLIENT_01_021: [Otherwise, IoTHubClient_GetLastMessageReceiveTime shall return the result of IoTHubClient_LL_GetLastMessageReceiveTime.] */
AzureIoTClient 16:deba40344375 1363 result = IoTHubClient_LL_GetLastMessageReceiveTime(iotHubClientInstance->IoTHubClientLLHandle, lastMessageReceiveTime);
AzureIoTClient 16:deba40344375 1364
AzureIoTClient 16:deba40344375 1365 /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 53:1e5a1ca1f274 1366 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 1367 }
AzureIoTClient 16:deba40344375 1368 }
AzureIoTClient 16:deba40344375 1369
AzureIoTClient 16:deba40344375 1370 return result;
AzureIoTClient 16:deba40344375 1371 }
AzureIoTClient 16:deba40344375 1372
AzureIoTClient 16:deba40344375 1373 IOTHUB_CLIENT_RESULT IoTHubClient_SetOption(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const char* optionName, const void* value)
AzureIoTClient 16:deba40344375 1374 {
AzureIoTClient 16:deba40344375 1375 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 1376 /*Codes_SRS_IOTHUBCLIENT_02_034: [If parameter iotHubClientHandle is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 42:448eecc3676e 1377 /*Codes_SRS_IOTHUBCLIENT_02_035: [ If parameter optionName is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1378 /*Codes_SRS_IOTHUBCLIENT_02_036: [ If parameter value is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 16:deba40344375 1379 if (
AzureIoTClient 16:deba40344375 1380 (iotHubClientHandle == NULL) ||
AzureIoTClient 16:deba40344375 1381 (optionName == NULL) ||
AzureIoTClient 16:deba40344375 1382 (value == NULL)
AzureIoTClient 16:deba40344375 1383 )
AzureIoTClient 16:deba40344375 1384 {
AzureIoTClient 16:deba40344375 1385 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1386 LogError("invalid arg (NULL)");
AzureIoTClient 16:deba40344375 1387 }
AzureIoTClient 16:deba40344375 1388 else
AzureIoTClient 16:deba40344375 1389 {
AzureIoTClient 16:deba40344375 1390 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1391
AzureIoTClient 40:1a94db9139ea 1392 /* Codes_SRS_IOTHUBCLIENT_01_041: [ IoTHubClient_SetOption shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 40:1a94db9139ea 1393 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 40:1a94db9139ea 1394 {
AzureIoTClient 66:a419827cb051 1395 /* Codes_SRS_IOTHUBCLIENT_01_042: [ If acquiring the lock fails, IoTHubClient_SetOption shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 40:1a94db9139ea 1396 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 40:1a94db9139ea 1397 LogError("Could not acquire lock");
AzureIoTClient 40:1a94db9139ea 1398 }
AzureIoTClient 40:1a94db9139ea 1399 else
AzureIoTClient 16:deba40344375 1400 {
AzureIoTClient 40:1a94db9139ea 1401 /*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 1402 result = IoTHubClient_LL_SetOption(iotHubClientInstance->IoTHubClientLLHandle, optionName, value);
AzureIoTClient 40:1a94db9139ea 1403 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 40:1a94db9139ea 1404 {
AzureIoTClient 40:1a94db9139ea 1405 LogError("IoTHubClient_LL_SetOption failed");
AzureIoTClient 40:1a94db9139ea 1406 }
AzureIoTClient 40:1a94db9139ea 1407
AzureIoTClient 53:1e5a1ca1f274 1408 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1409 }
AzureIoTClient 53:1e5a1ca1f274 1410 }
AzureIoTClient 53:1e5a1ca1f274 1411 return result;
AzureIoTClient 53:1e5a1ca1f274 1412 }
AzureIoTClient 53:1e5a1ca1f274 1413
AzureIoTClient 53:1e5a1ca1f274 1414 IOTHUB_CLIENT_RESULT IoTHubClient_SetDeviceTwinCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1415 {
AzureIoTClient 53:1e5a1ca1f274 1416 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1417
AzureIoTClient 53:1e5a1ca1f274 1418 /*Codes_SRS_IOTHUBCLIENT_10_001: [** `IoTHubClient_SetDeviceTwinCallback` shall fail and return `IOTHUB_CLIENT_INVALID_ARG` if parameter `iotHubClientHandle` is `NULL`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1419 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1420 {
AzureIoTClient 53:1e5a1ca1f274 1421 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1422 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 1423 }
AzureIoTClient 53:1e5a1ca1f274 1424 else
AzureIoTClient 53:1e5a1ca1f274 1425 {
AzureIoTClient 53:1e5a1ca1f274 1426 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1427
AzureIoTClient 66:a419827cb051 1428 /*Codes_SRS_IOTHUBCLIENT_10_003: [** If the transport connection is shared, the thread shall be started by calling `IoTHubTransport_StartWorkerThread`. ]*/
AzureIoTClient 66:a419827cb051 1429 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1430 {
AzureIoTClient 66:a419827cb051 1431 /*Codes_SRS_IOTHUBCLIENT_10_004: [** If starting the thread fails, `IoTHubClient_SetDeviceTwinCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1432 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1433 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1434 }
AzureIoTClient 53:1e5a1ca1f274 1435 else
AzureIoTClient 53:1e5a1ca1f274 1436 {
AzureIoTClient 66:a419827cb051 1437 /*Codes_SRS_IOTHUBCLIENT_10_020: [** `IoTHubClient_SetDeviceTwinCallback` shall be made thread - safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1438 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1439 {
AzureIoTClient 66:a419827cb051 1440 /*Codes_SRS_IOTHUBCLIENT_10_002: [** If acquiring the lock fails, `IoTHubClient_SetDeviceTwinCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1441 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1442 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1443 }
AzureIoTClient 53:1e5a1ca1f274 1444 else
AzureIoTClient 53:1e5a1ca1f274 1445 {
AzureIoTClient 67:ecebc2a41159 1446 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 67:ecebc2a41159 1447 {
AzureIoTClient 67:ecebc2a41159 1448 iotHubClientInstance->desired_state_callback = deviceTwinCallback;
AzureIoTClient 67:ecebc2a41159 1449 }
AzureIoTClient 67:ecebc2a41159 1450
Azure.IoT.Build 54:6dcad9019a64 1451 if (iotHubClientInstance->created_with_transport_handle != 0 || deviceTwinCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 1452 {
Azure.IoT.Build 54:6dcad9019a64 1453 /*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 1454 result = IoTHubClient_LL_SetDeviceTwinCallback(iotHubClientInstance->IoTHubClientLLHandle, deviceTwinCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 1455 }
Azure.IoT.Build 54:6dcad9019a64 1456 else
AzureIoTClient 53:1e5a1ca1f274 1457 {
Azure.IoT.Build 54:6dcad9019a64 1458 if (iotHubClientInstance->devicetwin_user_context != NULL)
Azure.IoT.Build 54:6dcad9019a64 1459 {
Azure.IoT.Build 54:6dcad9019a64 1460 free(iotHubClientInstance->devicetwin_user_context);
Azure.IoT.Build 54:6dcad9019a64 1461 }
Azure.IoT.Build 54:6dcad9019a64 1462
Azure.IoT.Build 54:6dcad9019a64 1463 /*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 1464 iotHubClientInstance->devicetwin_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
Azure.IoT.Build 54:6dcad9019a64 1465 if (iotHubClientInstance->devicetwin_user_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1466 {
Azure.IoT.Build 54:6dcad9019a64 1467 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1468 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1469 }
Azure.IoT.Build 54:6dcad9019a64 1470 else
Azure.IoT.Build 54:6dcad9019a64 1471 {
Azure.IoT.Build 54:6dcad9019a64 1472 /*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 1473 iotHubClientInstance->devicetwin_user_context->iotHubClientHandle = iotHubClientInstance;
AzureIoTClient 55:59b527ab3452 1474 iotHubClientInstance->devicetwin_user_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1475 result = IoTHubClient_LL_SetDeviceTwinCallback(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_device_twin_callback, iotHubClientInstance->devicetwin_user_context);
Azure.IoT.Build 54:6dcad9019a64 1476 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1477 {
Azure.IoT.Build 54:6dcad9019a64 1478 LogError("IoTHubClient_LL_SetDeviceTwinCallback failed");
Azure.IoT.Build 54:6dcad9019a64 1479 free(iotHubClientInstance->devicetwin_user_context);
AzureIoTClient 61:8b85a4e797cf 1480 iotHubClientInstance->devicetwin_user_context = NULL;
Azure.IoT.Build 54:6dcad9019a64 1481 }
Azure.IoT.Build 54:6dcad9019a64 1482 }
AzureIoTClient 53:1e5a1ca1f274 1483 }
AzureIoTClient 66:a419827cb051 1484
AzureIoTClient 66:a419827cb051 1485 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1486 }
AzureIoTClient 16:deba40344375 1487 }
AzureIoTClient 16:deba40344375 1488 }
AzureIoTClient 16:deba40344375 1489 return result;
AzureIoTClient 16:deba40344375 1490 }
AzureIoTClient 42:448eecc3676e 1491
AzureIoTClient 53:1e5a1ca1f274 1492 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 1493 {
AzureIoTClient 53:1e5a1ca1f274 1494 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1495
AzureIoTClient 53:1e5a1ca1f274 1496 /*Codes_SRS_IOTHUBCLIENT_10_013: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1497 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1498 {
AzureIoTClient 53:1e5a1ca1f274 1499 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1500 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 1501 }
AzureIoTClient 53:1e5a1ca1f274 1502 else
AzureIoTClient 53:1e5a1ca1f274 1503 {
AzureIoTClient 53:1e5a1ca1f274 1504 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1505
AzureIoTClient 66:a419827cb051 1506 /*Codes_SRS_IOTHUBCLIENT_10_015: [** If the transport connection is shared, the thread shall be started by calling `IoTHubTransport_StartWorkerThread`. ]*/
AzureIoTClient 66:a419827cb051 1507 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1508 {
AzureIoTClient 66:a419827cb051 1509 /*Codes_SRS_IOTHUBCLIENT_10_016: [** If starting the thread fails, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1510 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1511 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1512 }
AzureIoTClient 53:1e5a1ca1f274 1513 else
AzureIoTClient 53:1e5a1ca1f274 1514 {
Azure.IoT.Build 54:6dcad9019a64 1515 if (iotHubClientInstance->created_with_transport_handle == 0)
Azure.IoT.Build 54:6dcad9019a64 1516 {
Azure.IoT.Build 54:6dcad9019a64 1517 iotHubClientInstance->reported_state_callback = reportedStateCallback;
Azure.IoT.Build 54:6dcad9019a64 1518 }
AzureIoTClient 66:a419827cb051 1519
AzureIoTClient 66:a419827cb051 1520 /*Codes_SRS_IOTHUBCLIENT_10_021: [** `IoTHubClient_SendReportedState` shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1521 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1522 {
AzureIoTClient 66:a419827cb051 1523 /*Codes_SRS_IOTHUBCLIENT_10_014: [** If acquiring the lock fails, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 1524 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1525 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1526 }
AzureIoTClient 53:1e5a1ca1f274 1527 else
AzureIoTClient 53:1e5a1ca1f274 1528 {
Azure.IoT.Build 54:6dcad9019a64 1529 if (iotHubClientInstance->created_with_transport_handle != 0 || reportedStateCallback == NULL)
Azure.IoT.Build 54:6dcad9019a64 1530 {
Azure.IoT.Build 54:6dcad9019a64 1531 /*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 1532 /*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 1533 result = IoTHubClient_LL_SendReportedState(iotHubClientInstance->IoTHubClientLLHandle, reportedState, size, reportedStateCallback, userContextCallback);
Azure.IoT.Build 54:6dcad9019a64 1534 }
Azure.IoT.Build 54:6dcad9019a64 1535 else
AzureIoTClient 53:1e5a1ca1f274 1536 {
Azure.IoT.Build 54:6dcad9019a64 1537 /* 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 1538 IOTHUB_QUEUE_CONTEXT* queue_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
Azure.IoT.Build 54:6dcad9019a64 1539 if (queue_context == NULL)
Azure.IoT.Build 54:6dcad9019a64 1540 {
Azure.IoT.Build 54:6dcad9019a64 1541 result = IOTHUB_CLIENT_ERROR;
Azure.IoT.Build 54:6dcad9019a64 1542 LogError("Failed allocating QUEUE_CONTEXT");
Azure.IoT.Build 54:6dcad9019a64 1543 }
Azure.IoT.Build 54:6dcad9019a64 1544 else
Azure.IoT.Build 54:6dcad9019a64 1545 {
Azure.IoT.Build 54:6dcad9019a64 1546 queue_context->iotHubClientHandle = iotHubClientInstance;
Azure.IoT.Build 54:6dcad9019a64 1547 queue_context->userContextCallback = userContextCallback;
Azure.IoT.Build 54:6dcad9019a64 1548 /*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 1549 /*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 1550 result = IoTHubClient_LL_SendReportedState(iotHubClientInstance->IoTHubClientLLHandle, reportedState, size, iothub_ll_reported_state_callback, queue_context);
Azure.IoT.Build 54:6dcad9019a64 1551 if (result != IOTHUB_CLIENT_OK)
Azure.IoT.Build 54:6dcad9019a64 1552 {
Azure.IoT.Build 54:6dcad9019a64 1553 LogError("IoTHubClient_LL_SendReportedState failed");
Azure.IoT.Build 54:6dcad9019a64 1554 free(queue_context);
Azure.IoT.Build 54:6dcad9019a64 1555 }
Azure.IoT.Build 54:6dcad9019a64 1556 }
AzureIoTClient 53:1e5a1ca1f274 1557 }
AzureIoTClient 66:a419827cb051 1558
AzureIoTClient 66:a419827cb051 1559 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1560 }
AzureIoTClient 53:1e5a1ca1f274 1561 }
AzureIoTClient 53:1e5a1ca1f274 1562 }
AzureIoTClient 53:1e5a1ca1f274 1563 return result;
AzureIoTClient 53:1e5a1ca1f274 1564 }
AzureIoTClient 53:1e5a1ca1f274 1565
AzureIoTClient 53:1e5a1ca1f274 1566 IOTHUB_CLIENT_RESULT IoTHubClient_SetDeviceMethodCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC deviceMethodCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1567 {
AzureIoTClient 53:1e5a1ca1f274 1568 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1569
AzureIoTClient 53:1e5a1ca1f274 1570 /*Codes_SRS_IOTHUBCLIENT_12_012: [ If iotHubClientHandle is NULL, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 53:1e5a1ca1f274 1571 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1572 {
AzureIoTClient 53:1e5a1ca1f274 1573 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1574 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 1575 }
AzureIoTClient 53:1e5a1ca1f274 1576 else
AzureIoTClient 53:1e5a1ca1f274 1577 {
AzureIoTClient 53:1e5a1ca1f274 1578 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1579
AzureIoTClient 66:a419827cb051 1580 /*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 1581 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 1582 {
AzureIoTClient 66:a419827cb051 1583 /*Codes_SRS_IOTHUBCLIENT_12_015: [ If starting the thread fails, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 53:1e5a1ca1f274 1584 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1585 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 1586 }
AzureIoTClient 53:1e5a1ca1f274 1587 else
AzureIoTClient 53:1e5a1ca1f274 1588 {
AzureIoTClient 62:5a4cdacf5090 1589 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 62:5a4cdacf5090 1590 {
AzureIoTClient 62:5a4cdacf5090 1591 iotHubClientInstance->device_method_callback = deviceMethodCallback;
AzureIoTClient 62:5a4cdacf5090 1592 }
AzureIoTClient 66:a419827cb051 1593
AzureIoTClient 66:a419827cb051 1594 /*Codes_SRS_IOTHUBCLIENT_12_018: [ IoTHubClient_SetDeviceMethodCallback shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1595 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 1596 {
AzureIoTClient 66:a419827cb051 1597 /*Codes_SRS_IOTHUBCLIENT_12_013: [ If acquiring the lock fails, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 53:1e5a1ca1f274 1598 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1599 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 1600 }
AzureIoTClient 53:1e5a1ca1f274 1601 else
AzureIoTClient 53:1e5a1ca1f274 1602 {
AzureIoTClient 62:5a4cdacf5090 1603 if (iotHubClientInstance->method_user_context)
AzureIoTClient 62:5a4cdacf5090 1604 {
AzureIoTClient 62:5a4cdacf5090 1605 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1606 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 62:5a4cdacf5090 1607 }
AzureIoTClient 62:5a4cdacf5090 1608 if (deviceMethodCallback == NULL)
AzureIoTClient 62:5a4cdacf5090 1609 {
AzureIoTClient 62:5a4cdacf5090 1610 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, NULL, NULL);
AzureIoTClient 62:5a4cdacf5090 1611 }
AzureIoTClient 62:5a4cdacf5090 1612 else
AzureIoTClient 53:1e5a1ca1f274 1613 {
AzureIoTClient 62:5a4cdacf5090 1614 iotHubClientInstance->method_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 62:5a4cdacf5090 1615 if (iotHubClientInstance->method_user_context == NULL)
AzureIoTClient 62:5a4cdacf5090 1616 {
AzureIoTClient 62:5a4cdacf5090 1617 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 62:5a4cdacf5090 1618 LogError("Failed allocating QUEUE_CONTEXT");
AzureIoTClient 62:5a4cdacf5090 1619 }
AzureIoTClient 62:5a4cdacf5090 1620 else
AzureIoTClient 62:5a4cdacf5090 1621 {
AzureIoTClient 62:5a4cdacf5090 1622 iotHubClientInstance->method_user_context->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 62:5a4cdacf5090 1623 iotHubClientInstance->method_user_context->userContextCallback = userContextCallback;
AzureIoTClient 62:5a4cdacf5090 1624
AzureIoTClient 62:5a4cdacf5090 1625 /*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 1626 /*Codes_SRS_IOTHUBCLIENT_12_017: [ When IoTHubClient_LL_SetDeviceMethodCallback is called, IoTHubClient_SetDeviceMethodCallback shall return the result of IoTHubClient_LL_SetDeviceMethodCallback. ]*/
AzureIoTClient 62:5a4cdacf5090 1627 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_device_method_callback, iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1628 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 62:5a4cdacf5090 1629 {
AzureIoTClient 62:5a4cdacf5090 1630 LogError("IoTHubClient_LL_SetDeviceMethodCallback_Ex failed");
AzureIoTClient 62:5a4cdacf5090 1631 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1632 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 62:5a4cdacf5090 1633 }
AzureIoTClient 66:a419827cb051 1634 else
AzureIoTClient 66:a419827cb051 1635 {
AzureIoTClient 66:a419827cb051 1636 iotHubClientInstance->device_method_callback = deviceMethodCallback;
AzureIoTClient 66:a419827cb051 1637 }
AzureIoTClient 62:5a4cdacf5090 1638 }
AzureIoTClient 53:1e5a1ca1f274 1639 }
AzureIoTClient 66:a419827cb051 1640
AzureIoTClient 66:a419827cb051 1641 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 1642 }
AzureIoTClient 53:1e5a1ca1f274 1643
AzureIoTClient 53:1e5a1ca1f274 1644 }
AzureIoTClient 53:1e5a1ca1f274 1645 }
AzureIoTClient 53:1e5a1ca1f274 1646 return result;
AzureIoTClient 53:1e5a1ca1f274 1647 }
AzureIoTClient 53:1e5a1ca1f274 1648
AzureIoTClient 55:59b527ab3452 1649 IOTHUB_CLIENT_RESULT IoTHubClient_SetDeviceMethodCallback_Ex(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK inboundDeviceMethodCallback, void* userContextCallback)
AzureIoTClient 55:59b527ab3452 1650 {
AzureIoTClient 55:59b527ab3452 1651 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1652
AzureIoTClient 55:59b527ab3452 1653 /*Codes_SRS_IOTHUBCLIENT_07_001: [ If iotHubClientHandle is NULL, IoTHubClient_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 55:59b527ab3452 1654 if (iotHubClientHandle == NULL)
AzureIoTClient 55:59b527ab3452 1655 {
AzureIoTClient 55:59b527ab3452 1656 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1657 LogError("invalid arg (NULL)");
AzureIoTClient 55:59b527ab3452 1658 }
AzureIoTClient 55:59b527ab3452 1659 else
AzureIoTClient 55:59b527ab3452 1660 {
AzureIoTClient 55:59b527ab3452 1661 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1662
AzureIoTClient 66:a419827cb051 1663 /*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 1664 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 55:59b527ab3452 1665 {
AzureIoTClient 66:a419827cb051 1666 /*Codes_SRS_IOTHUBCLIENT_07_004: [ If starting the thread fails, IoTHubClient_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 55:59b527ab3452 1667 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1668 LogError("Could not start worker thread");
AzureIoTClient 55:59b527ab3452 1669 }
AzureIoTClient 55:59b527ab3452 1670 else
AzureIoTClient 55:59b527ab3452 1671 {
AzureIoTClient 55:59b527ab3452 1672 if (iotHubClientInstance->created_with_transport_handle == 0)
AzureIoTClient 55:59b527ab3452 1673 {
AzureIoTClient 62:5a4cdacf5090 1674 iotHubClientInstance->inbound_device_method_callback = inboundDeviceMethodCallback;
AzureIoTClient 55:59b527ab3452 1675 }
AzureIoTClient 66:a419827cb051 1676
AzureIoTClient 66:a419827cb051 1677 /*Codes_SRS_IOTHUBCLIENT_07_007: [ IoTHubClient_SetDeviceMethodCallback_Ex shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 66:a419827cb051 1678 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 55:59b527ab3452 1679 {
AzureIoTClient 66:a419827cb051 1680 /*Codes_SRS_IOTHUBCLIENT_07_002: [ If acquiring the lock fails, IoTHubClient_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 55:59b527ab3452 1681 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 66:a419827cb051 1682 LogError("Could not acquire lock");
AzureIoTClient 55:59b527ab3452 1683 }
AzureIoTClient 55:59b527ab3452 1684 else
AzureIoTClient 55:59b527ab3452 1685 {
AzureIoTClient 62:5a4cdacf5090 1686 if (iotHubClientInstance->method_user_context)
AzureIoTClient 62:5a4cdacf5090 1687 {
AzureIoTClient 62:5a4cdacf5090 1688 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1689 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 62:5a4cdacf5090 1690 }
AzureIoTClient 62:5a4cdacf5090 1691 if (inboundDeviceMethodCallback == NULL)
AzureIoTClient 55:59b527ab3452 1692 {
AzureIoTClient 55:59b527ab3452 1693 /* 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 1694 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, NULL, NULL);
AzureIoTClient 55:59b527ab3452 1695 }
AzureIoTClient 55:59b527ab3452 1696 else
AzureIoTClient 55:59b527ab3452 1697 {
AzureIoTClient 62:5a4cdacf5090 1698 iotHubClientInstance->method_user_context = (IOTHUB_QUEUE_CONTEXT*)malloc(sizeof(IOTHUB_QUEUE_CONTEXT));
AzureIoTClient 62:5a4cdacf5090 1699 if (iotHubClientInstance->method_user_context == NULL)
AzureIoTClient 55:59b527ab3452 1700 {
AzureIoTClient 55:59b527ab3452 1701 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1702 LogError("Failed allocating QUEUE_CONTEXT");
AzureIoTClient 55:59b527ab3452 1703 }
AzureIoTClient 55:59b527ab3452 1704 else
AzureIoTClient 55:59b527ab3452 1705 {
AzureIoTClient 55:59b527ab3452 1706 /*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 1707 iotHubClientInstance->method_user_context->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 62:5a4cdacf5090 1708 iotHubClientInstance->method_user_context->userContextCallback = userContextCallback;
AzureIoTClient 62:5a4cdacf5090 1709
AzureIoTClient 55:59b527ab3452 1710 /* 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 1711 result = IoTHubClient_LL_SetDeviceMethodCallback_Ex(iotHubClientInstance->IoTHubClientLLHandle, iothub_ll_inbound_device_method_callback, iotHubClientInstance->method_user_context);
AzureIoTClient 55:59b527ab3452 1712 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 55:59b527ab3452 1713 {
AzureIoTClient 62:5a4cdacf5090 1714 LogError("IoTHubClient_LL_SetDeviceMethodCallback_Ex failed");
AzureIoTClient 62:5a4cdacf5090 1715 free(iotHubClientInstance->method_user_context);
AzureIoTClient 62:5a4cdacf5090 1716 iotHubClientInstance->method_user_context = NULL;
AzureIoTClient 55:59b527ab3452 1717 }
AzureIoTClient 66:a419827cb051 1718 else
AzureIoTClient 66:a419827cb051 1719 {
AzureIoTClient 66:a419827cb051 1720 iotHubClientInstance->inbound_device_method_callback = inboundDeviceMethodCallback;
AzureIoTClient 66:a419827cb051 1721 }
AzureIoTClient 55:59b527ab3452 1722 }
AzureIoTClient 55:59b527ab3452 1723 }
AzureIoTClient 66:a419827cb051 1724
AzureIoTClient 66:a419827cb051 1725 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 55:59b527ab3452 1726 }
AzureIoTClient 55:59b527ab3452 1727 }
AzureIoTClient 55:59b527ab3452 1728 }
AzureIoTClient 55:59b527ab3452 1729 return result;
AzureIoTClient 55:59b527ab3452 1730 }
AzureIoTClient 55:59b527ab3452 1731
AzureIoTClient 55:59b527ab3452 1732 IOTHUB_CLIENT_RESULT IoTHubClient_DeviceMethodResponse(IOTHUB_CLIENT_HANDLE iotHubClientHandle, METHOD_HANDLE methodId, const unsigned char* response, size_t respSize, int statusCode)
AzureIoTClient 55:59b527ab3452 1733 {
AzureIoTClient 55:59b527ab3452 1734 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1735
AzureIoTClient 55:59b527ab3452 1736 /*Codes_SRS_IOTHUBCLIENT_12_012: [ If iotHubClientHandle is NULL, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 55:59b527ab3452 1737 if (iotHubClientHandle == NULL)
AzureIoTClient 55:59b527ab3452 1738 {
AzureIoTClient 55:59b527ab3452 1739 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1740 LogError("invalid arg (NULL)");
AzureIoTClient 55:59b527ab3452 1741 }
AzureIoTClient 55:59b527ab3452 1742 else
AzureIoTClient 55:59b527ab3452 1743 {
AzureIoTClient 55:59b527ab3452 1744 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1745
AzureIoTClient 55:59b527ab3452 1746 /*Codes_SRS_IOTHUBCLIENT_12_018: [ IoTHubClient_SetDeviceMethodCallback shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 55:59b527ab3452 1747 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 55:59b527ab3452 1748 {
AzureIoTClient 55:59b527ab3452 1749 /*Codes_SRS_IOTHUBCLIENT_12_013: [ If acquiring the lock fails, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 55:59b527ab3452 1750 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1751 LogError("Could not acquire lock");
AzureIoTClient 55:59b527ab3452 1752 }
AzureIoTClient 55:59b527ab3452 1753 else
AzureIoTClient 55:59b527ab3452 1754 {
AzureIoTClient 55:59b527ab3452 1755 result = IoTHubClient_LL_DeviceMethodResponse(iotHubClientInstance->IoTHubClientLLHandle, methodId, response, respSize, statusCode);
AzureIoTClient 55:59b527ab3452 1756 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 55:59b527ab3452 1757 {
AzureIoTClient 55:59b527ab3452 1758 LogError("IoTHubClient_LL_DeviceMethodResponse failed");
AzureIoTClient 55:59b527ab3452 1759 }
AzureIoTClient 55:59b527ab3452 1760 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 55:59b527ab3452 1761 }
AzureIoTClient 55:59b527ab3452 1762 }
AzureIoTClient 55:59b527ab3452 1763 return result;
AzureIoTClient 55:59b527ab3452 1764 }
AzureIoTClient 53:1e5a1ca1f274 1765
AzureIoTClient 44:33dd78697616 1766 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 1767 static int uploadingThread(void *data)
AzureIoTClient 42:448eecc3676e 1768 {
AzureIoTClient 42:448eecc3676e 1769 UPLOADTOBLOB_SAVED_DATA* savedData = (UPLOADTOBLOB_SAVED_DATA*)data;
AzureIoTClient 42:448eecc3676e 1770
AzureIoTClient 62:5a4cdacf5090 1771 if (Lock(savedData->iotHubClientHandle->LockHandle) == LOCK_OK)
AzureIoTClient 42:448eecc3676e 1772 {
AzureIoTClient 62:5a4cdacf5090 1773 IOTHUB_CLIENT_FILE_UPLOAD_RESULT upload_result;
AzureIoTClient 62:5a4cdacf5090 1774 /*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 1775 /*not having it protected means multiple simultaneous uploads can happen*/
AzureIoTClient 62:5a4cdacf5090 1776 /*Codes_SRS_IOTHUBCLIENT_02_054: [ The thread shall call IoTHubClient_LL_UploadToBlob passing the information packed in the structure. ]*/
AzureIoTClient 62:5a4cdacf5090 1777 if (IoTHubClient_LL_UploadToBlob(savedData->iotHubClientHandle->IoTHubClientLLHandle, savedData->destinationFileName, savedData->source, savedData->size) == IOTHUB_CLIENT_OK)
AzureIoTClient 62:5a4cdacf5090 1778 {
AzureIoTClient 62:5a4cdacf5090 1779 upload_result = FILE_UPLOAD_OK;
AzureIoTClient 62:5a4cdacf5090 1780 }
AzureIoTClient 62:5a4cdacf5090 1781 else
AzureIoTClient 62:5a4cdacf5090 1782 {
AzureIoTClient 62:5a4cdacf5090 1783 LogError("unable to IoTHubClient_LL_UploadToBlob");
AzureIoTClient 62:5a4cdacf5090 1784 upload_result = FILE_UPLOAD_ERROR;
AzureIoTClient 62:5a4cdacf5090 1785 }
AzureIoTClient 62:5a4cdacf5090 1786 (void)Unlock(savedData->iotHubClientHandle->LockHandle);
AzureIoTClient 62:5a4cdacf5090 1787
AzureIoTClient 42:448eecc3676e 1788 if (savedData->iotHubClientFileUploadCallback != NULL)
AzureIoTClient 42:448eecc3676e 1789 {
AzureIoTClient 42:448eecc3676e 1790 /*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 1791 savedData->iotHubClientFileUploadCallback(upload_result, savedData->context);
AzureIoTClient 42:448eecc3676e 1792 }
AzureIoTClient 42:448eecc3676e 1793 }
AzureIoTClient 42:448eecc3676e 1794 else
AzureIoTClient 42:448eecc3676e 1795 {
AzureIoTClient 62:5a4cdacf5090 1796 LogError("Lock failed");
AzureIoTClient 42:448eecc3676e 1797 }
AzureIoTClient 42:448eecc3676e 1798
AzureIoTClient 42:448eecc3676e 1799 /*Codes_SRS_IOTHUBCLIENT_02_071: [ The thread shall mark itself as disposable. ]*/
AzureIoTClient 42:448eecc3676e 1800 if (Lock(savedData->lockGarbage) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 1801 {
AzureIoTClient 42:448eecc3676e 1802 LogError("unable to Lock - trying anyway");
AzureIoTClient 42:448eecc3676e 1803 savedData->canBeGarbageCollected = 1;
AzureIoTClient 42:448eecc3676e 1804 }
AzureIoTClient 42:448eecc3676e 1805 else
AzureIoTClient 42:448eecc3676e 1806 {
AzureIoTClient 42:448eecc3676e 1807 savedData->canBeGarbageCollected = 1;
AzureIoTClient 42:448eecc3676e 1808
AzureIoTClient 42:448eecc3676e 1809 if (Unlock(savedData->lockGarbage) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 1810 {
AzureIoTClient 42:448eecc3676e 1811 LogError("unable to Unlock after locking");
AzureIoTClient 42:448eecc3676e 1812 }
AzureIoTClient 42:448eecc3676e 1813 }
AzureIoTClient 71:0d498da5ece1 1814
AzureIoTClient 71:0d498da5ece1 1815 ThreadAPI_Exit(0);
AzureIoTClient 42:448eecc3676e 1816 return 0;
AzureIoTClient 42:448eecc3676e 1817 }
AzureIoTClient 43:038d8511e817 1818 #endif
AzureIoTClient 42:448eecc3676e 1819
AzureIoTClient 44:33dd78697616 1820 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 1821 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 1822 {
AzureIoTClient 42:448eecc3676e 1823 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1824 /*Codes_SRS_IOTHUBCLIENT_02_047: [ If iotHubClientHandle is NULL then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1825 /*Codes_SRS_IOTHUBCLIENT_02_048: [ If destinationFileName is NULL then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1826 /*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 1827 if (
AzureIoTClient 42:448eecc3676e 1828 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 1829 (destinationFileName == NULL) ||
AzureIoTClient 42:448eecc3676e 1830 ((source == NULL) && (size > 0))
AzureIoTClient 42:448eecc3676e 1831 )
AzureIoTClient 42:448eecc3676e 1832 {
AzureIoTClient 42:448eecc3676e 1833 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 1834 iotHubClientHandle,
AzureIoTClient 42:448eecc3676e 1835 destinationFileName,
AzureIoTClient 42:448eecc3676e 1836 source,
AzureIoTClient 42:448eecc3676e 1837 size,
AzureIoTClient 42:448eecc3676e 1838 iotHubClientFileUploadCallback,
AzureIoTClient 42:448eecc3676e 1839 context
AzureIoTClient 42:448eecc3676e 1840 );
AzureIoTClient 42:448eecc3676e 1841 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 1842 }
AzureIoTClient 42:448eecc3676e 1843 else
AzureIoTClient 42:448eecc3676e 1844 {
AzureIoTClient 42:448eecc3676e 1845 /*Codes_SRS_IOTHUBCLIENT_02_051: [IoTHubClient_UploadToBlobAsync shall copy the souce, size, iotHubClientFileUploadCallback, context into a structure.]*/
AzureIoTClient 42:448eecc3676e 1846 UPLOADTOBLOB_SAVED_DATA *savedData = (UPLOADTOBLOB_SAVED_DATA *)malloc(sizeof(UPLOADTOBLOB_SAVED_DATA));
AzureIoTClient 42:448eecc3676e 1847 if (savedData == NULL)
AzureIoTClient 42:448eecc3676e 1848 {
AzureIoTClient 42:448eecc3676e 1849 /*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 1850 LogError("unable to malloc - oom");
AzureIoTClient 42:448eecc3676e 1851 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1852 }
AzureIoTClient 42:448eecc3676e 1853 else
AzureIoTClient 42:448eecc3676e 1854 {
AzureIoTClient 42:448eecc3676e 1855 if (mallocAndStrcpy_s((char**)&savedData->destinationFileName, destinationFileName) != 0)
AzureIoTClient 42:448eecc3676e 1856 {
AzureIoTClient 42:448eecc3676e 1857 /*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 1858 LogError("unable to mallocAndStrcpy_s");
AzureIoTClient 42:448eecc3676e 1859 free(savedData);
AzureIoTClient 42:448eecc3676e 1860 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1861 }
AzureIoTClient 42:448eecc3676e 1862 else
AzureIoTClient 42:448eecc3676e 1863 {
AzureIoTClient 42:448eecc3676e 1864 savedData->size = size;
AzureIoTClient 42:448eecc3676e 1865 int sourceCloned;
AzureIoTClient 42:448eecc3676e 1866 if (size == 0)
AzureIoTClient 42:448eecc3676e 1867 {
AzureIoTClient 42:448eecc3676e 1868 savedData->source = NULL;
AzureIoTClient 42:448eecc3676e 1869 sourceCloned = 1;
AzureIoTClient 42:448eecc3676e 1870 }
AzureIoTClient 42:448eecc3676e 1871 else
AzureIoTClient 42:448eecc3676e 1872 {
AzureIoTClient 42:448eecc3676e 1873 savedData->source = (unsigned char*)malloc(size);
AzureIoTClient 42:448eecc3676e 1874 if (savedData->source == NULL)
AzureIoTClient 42:448eecc3676e 1875 {
AzureIoTClient 42:448eecc3676e 1876 /*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 1877 LogError("unable to malloc - oom");
AzureIoTClient 42:448eecc3676e 1878 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1879 free(savedData);
AzureIoTClient 42:448eecc3676e 1880 sourceCloned = 0;
AzureIoTClient 42:448eecc3676e 1881 }
AzureIoTClient 42:448eecc3676e 1882 else
AzureIoTClient 42:448eecc3676e 1883 {
AzureIoTClient 42:448eecc3676e 1884 sourceCloned = 1;
AzureIoTClient 42:448eecc3676e 1885 }
AzureIoTClient 42:448eecc3676e 1886 }
AzureIoTClient 42:448eecc3676e 1887
AzureIoTClient 42:448eecc3676e 1888 if (sourceCloned == 0)
AzureIoTClient 42:448eecc3676e 1889 {
AzureIoTClient 42:448eecc3676e 1890 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1891 }
AzureIoTClient 42:448eecc3676e 1892 else
AzureIoTClient 42:448eecc3676e 1893 {
Azure.IoT.Build 54:6dcad9019a64 1894 IOTHUB_CLIENT_INSTANCE* iotHubClientHandleData = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
Azure.IoT.Build 54:6dcad9019a64 1895
AzureIoTClient 42:448eecc3676e 1896 savedData->iotHubClientFileUploadCallback = iotHubClientFileUploadCallback;
AzureIoTClient 42:448eecc3676e 1897 savedData->context = context;
AzureIoTClient 58:15b0d29b2667 1898 (void)memcpy(savedData->source, source, size);
Azure.IoT.Build 54:6dcad9019a64 1899
AzureIoTClient 66:a419827cb051 1900 if ((result = StartWorkerThreadIfNeeded(iotHubClientHandleData)) != IOTHUB_CLIENT_OK)
AzureIoTClient 42:448eecc3676e 1901 {
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 66:a419827cb051 1906 LogError("Could not start worker thread");
AzureIoTClient 42:448eecc3676e 1907 }
AzureIoTClient 42:448eecc3676e 1908 else
AzureIoTClient 42:448eecc3676e 1909 {
AzureIoTClient 66:a419827cb051 1910 if (Lock(iotHubClientHandleData->LockHandle) != LOCK_OK) /*locking because the next statement is changing blobThreadsToBeJoined*/
AzureIoTClient 42:448eecc3676e 1911 {
AzureIoTClient 66:a419827cb051 1912 LogError("unable to lock");
AzureIoTClient 42:448eecc3676e 1913 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1914 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1915 free(savedData);
AzureIoTClient 42:448eecc3676e 1916 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1917 }
AzureIoTClient 42:448eecc3676e 1918 else
AzureIoTClient 42:448eecc3676e 1919 {
AzureIoTClient 42:448eecc3676e 1920 /*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 1921 LIST_ITEM_HANDLE item = singlylinkedlist_add(iotHubClientHandleData->savedDataToBeCleaned, savedData);
AzureIoTClient 42:448eecc3676e 1922 if (item == NULL)
AzureIoTClient 42:448eecc3676e 1923 {
AzureIoTClient 52:1cc3c6d07cad 1924 LogError("unable to singlylinkedlist_add");
AzureIoTClient 42:448eecc3676e 1925 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1926 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1927 free(savedData);
AzureIoTClient 42:448eecc3676e 1928 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1929 }
AzureIoTClient 42:448eecc3676e 1930 else
AzureIoTClient 42:448eecc3676e 1931 {
AzureIoTClient 42:448eecc3676e 1932 savedData->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 1933 savedData->canBeGarbageCollected = 0;
AzureIoTClient 42:448eecc3676e 1934 if ((savedData->lockGarbage = Lock_Init()) == NULL)
AzureIoTClient 42:448eecc3676e 1935 {
AzureIoTClient 52:1cc3c6d07cad 1936 (void)singlylinkedlist_remove(iotHubClientHandleData->savedDataToBeCleaned, item);
AzureIoTClient 42:448eecc3676e 1937 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1938 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1939 free(savedData);
AzureIoTClient 42:448eecc3676e 1940 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1941 LogError("unable to Lock_Init");
AzureIoTClient 42:448eecc3676e 1942 }
AzureIoTClient 42:448eecc3676e 1943 else
AzureIoTClient 42:448eecc3676e 1944 {
AzureIoTClient 42:448eecc3676e 1945 /*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 1946 if (ThreadAPI_Create(&savedData->uploadingThreadHandle, uploadingThread, savedData) != THREADAPI_OK)
AzureIoTClient 42:448eecc3676e 1947 {
AzureIoTClient 42:448eecc3676e 1948 /*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 1949 LogError("unablet to ThreadAPI_Create");
AzureIoTClient 42:448eecc3676e 1950 (void)Lock_Deinit(savedData->lockGarbage);
AzureIoTClient 52:1cc3c6d07cad 1951 (void)singlylinkedlist_remove(iotHubClientHandleData->savedDataToBeCleaned, item);
AzureIoTClient 42:448eecc3676e 1952 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1953 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1954 free(savedData);
AzureIoTClient 42:448eecc3676e 1955 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1956 }
AzureIoTClient 42:448eecc3676e 1957 else
AzureIoTClient 42:448eecc3676e 1958 {
AzureIoTClient 42:448eecc3676e 1959 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 1960 }
AzureIoTClient 42:448eecc3676e 1961 }
AzureIoTClient 42:448eecc3676e 1962 }
AzureIoTClient 66:a419827cb051 1963
AzureIoTClient 66:a419827cb051 1964 (void)Unlock(iotHubClientHandleData->LockHandle);
AzureIoTClient 42:448eecc3676e 1965 }
AzureIoTClient 42:448eecc3676e 1966 }
AzureIoTClient 42:448eecc3676e 1967 }
AzureIoTClient 42:448eecc3676e 1968 }
AzureIoTClient 42:448eecc3676e 1969 }
AzureIoTClient 42:448eecc3676e 1970 }
AzureIoTClient 42:448eecc3676e 1971 return result;
AzureIoTClient 42:448eecc3676e 1972 }
AzureIoTClient 44:33dd78697616 1973 #endif /*DONT_USE_UPLOADTOBLOB*/