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