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