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:
Tue Sep 11 11:13:11 2018 -0700
Revision:
92:97148cf9aa2a
Parent:
91:bbf806070c5f
1.2.9

Who changed what in which revision?

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