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@45:54c11b1b1407, 2016-07-01 (annotated)
- Committer:
- Azure.IoT Build
- Date:
- Fri Jul 01 10:42:36 2016 -0700
- Revision:
- 45:54c11b1b1407
- Parent:
- 44:33dd78697616
- Child:
- 47:aaa262b5f898
1.0.10
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> |
AzureIoTClient | 16:deba40344375 | 5 | #ifdef _CRTDBG_MAP_ALLOC |
AzureIoTClient | 16:deba40344375 | 6 | #include <crtdbg.h> |
AzureIoTClient | 16:deba40344375 | 7 | #endif |
Azure.IoT Build | 38:a05929a75111 | 8 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 16:deba40344375 | 9 | |
AzureIoTClient | 16:deba40344375 | 10 | #include <stdlib.h> |
AzureIoTClient | 16:deba40344375 | 11 | #include <signal.h> |
Azure.IoT Build | 35:ceed20da4ba6 | 12 | #include <stddef.h> |
Azure.IoT Build | 38:a05929a75111 | 13 | #include "azure_c_shared_utility/crt_abstractions.h" |
AzureIoTClient | 16:deba40344375 | 14 | #include "iothub_client.h" |
AzureIoTClient | 16:deba40344375 | 15 | #include "iothub_client_ll.h" |
Azure.IoT Build | 37:18310e4d888d | 16 | #include "iothubtransport.h" |
Azure.IoT Build | 38:a05929a75111 | 17 | #include "azure_c_shared_utility/threadapi.h" |
Azure.IoT Build | 38:a05929a75111 | 18 | #include "azure_c_shared_utility/lock.h" |
Azure.IoT Build | 45:54c11b1b1407 | 19 | #include "azure_c_shared_utility/xlogging.h" |
AzureIoTClient | 42:448eecc3676e | 20 | #include "azure_c_shared_utility/list.h" |
AzureIoTClient | 16:deba40344375 | 21 | |
AzureIoTClient | 16:deba40344375 | 22 | typedef struct IOTHUB_CLIENT_INSTANCE_TAG |
AzureIoTClient | 16:deba40344375 | 23 | { |
AzureIoTClient | 16:deba40344375 | 24 | IOTHUB_CLIENT_LL_HANDLE IoTHubClientLLHandle; |
AzureIoTClient | 42:448eecc3676e | 25 | TRANSPORT_HANDLE TransportHandle; |
AzureIoTClient | 16:deba40344375 | 26 | THREAD_HANDLE ThreadHandle; |
AzureIoTClient | 16:deba40344375 | 27 | LOCK_HANDLE LockHandle; |
AzureIoTClient | 16:deba40344375 | 28 | sig_atomic_t StopThread; |
AzureIoTClient | 44:33dd78697616 | 29 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 42:448eecc3676e | 30 | LIST_HANDLE savedDataToBeCleaned; /*list containing UPLOADTOBLOB_SAVED_DATA*/ |
AzureIoTClient | 43:038d8511e817 | 31 | #endif |
AzureIoTClient | 16:deba40344375 | 32 | } IOTHUB_CLIENT_INSTANCE; |
AzureIoTClient | 16:deba40344375 | 33 | |
AzureIoTClient | 44:33dd78697616 | 34 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 42:448eecc3676e | 35 | typedef struct UPLOADTOBLOB_SAVED_DATA_TAG |
AzureIoTClient | 42:448eecc3676e | 36 | { |
AzureIoTClient | 42:448eecc3676e | 37 | unsigned char* source; |
AzureIoTClient | 42:448eecc3676e | 38 | size_t size; |
AzureIoTClient | 42:448eecc3676e | 39 | char* destinationFileName; |
AzureIoTClient | 42:448eecc3676e | 40 | IOTHUB_CLIENT_FILE_UPLOAD_CALLBACK iotHubClientFileUploadCallback; |
AzureIoTClient | 42:448eecc3676e | 41 | void* context; |
AzureIoTClient | 42:448eecc3676e | 42 | THREAD_HANDLE uploadingThreadHandle; |
AzureIoTClient | 42:448eecc3676e | 43 | IOTHUB_CLIENT_HANDLE iotHubClientHandle; |
AzureIoTClient | 42:448eecc3676e | 44 | LOCK_HANDLE lockGarbage; |
AzureIoTClient | 42:448eecc3676e | 45 | int canBeGarbageCollected; /*flag indicating that the UPLOADTOBLOB_SAVED_DATA structure can be freed because the thread deadling with it finished*/ |
AzureIoTClient | 42:448eecc3676e | 46 | }UPLOADTOBLOB_SAVED_DATA; |
AzureIoTClient | 43:038d8511e817 | 47 | #endif |
AzureIoTClient | 42:448eecc3676e | 48 | |
Azure.IoT Build | 35:ceed20da4ba6 | 49 | /*used by unittests only*/ |
Azure.IoT Build | 35:ceed20da4ba6 | 50 | const size_t IoTHubClient_ThreadTerminationOffset = offsetof(IOTHUB_CLIENT_INSTANCE, StopThread); |
Azure.IoT Build | 35:ceed20da4ba6 | 51 | |
AzureIoTClient | 44:33dd78697616 | 52 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 42:448eecc3676e | 53 | /*this function is called from _Destroy and from ScheduleWork_Thread to join finished blobUpload threads and free that memory*/ |
AzureIoTClient | 42:448eecc3676e | 54 | static void garbageCollectorImpl(IOTHUB_CLIENT_INSTANCE* iotHubClientInstance) |
AzureIoTClient | 42:448eecc3676e | 55 | { |
AzureIoTClient | 42:448eecc3676e | 56 | /*see if any savedData structures can be disposed of*/ |
AzureIoTClient | 42:448eecc3676e | 57 | /*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 | 42:448eecc3676e | 58 | LIST_ITEM_HANDLE item = list_get_head_item(iotHubClientInstance->savedDataToBeCleaned); |
AzureIoTClient | 42:448eecc3676e | 59 | while (item != NULL) |
AzureIoTClient | 42:448eecc3676e | 60 | { |
AzureIoTClient | 42:448eecc3676e | 61 | const UPLOADTOBLOB_SAVED_DATA* savedData = (const UPLOADTOBLOB_SAVED_DATA*)list_item_get_value(item); |
AzureIoTClient | 42:448eecc3676e | 62 | LIST_ITEM_HANDLE old_item = item; |
AzureIoTClient | 42:448eecc3676e | 63 | item = list_get_next_item(item); |
AzureIoTClient | 42:448eecc3676e | 64 | |
AzureIoTClient | 42:448eecc3676e | 65 | if (Lock(savedData->lockGarbage) != LOCK_OK) |
AzureIoTClient | 42:448eecc3676e | 66 | { |
AzureIoTClient | 43:038d8511e817 | 67 | LogError("unable to Lock"); |
AzureIoTClient | 42:448eecc3676e | 68 | } |
AzureIoTClient | 42:448eecc3676e | 69 | else |
AzureIoTClient | 42:448eecc3676e | 70 | { |
AzureIoTClient | 42:448eecc3676e | 71 | if (savedData->canBeGarbageCollected == 1) |
AzureIoTClient | 42:448eecc3676e | 72 | { |
AzureIoTClient | 42:448eecc3676e | 73 | int notUsed; |
AzureIoTClient | 42:448eecc3676e | 74 | if (ThreadAPI_Join(savedData->uploadingThreadHandle, ¬Used) != THREADAPI_OK) |
AzureIoTClient | 42:448eecc3676e | 75 | { |
AzureIoTClient | 42:448eecc3676e | 76 | LogError("unable to ThreadAPI_Join"); |
AzureIoTClient | 42:448eecc3676e | 77 | } |
AzureIoTClient | 42:448eecc3676e | 78 | (void)list_remove(iotHubClientInstance->savedDataToBeCleaned, old_item); |
AzureIoTClient | 42:448eecc3676e | 79 | free((void*)savedData->source); |
AzureIoTClient | 42:448eecc3676e | 80 | free((void*)savedData->destinationFileName); |
AzureIoTClient | 42:448eecc3676e | 81 | |
AzureIoTClient | 42:448eecc3676e | 82 | if (Unlock(savedData->lockGarbage) != LOCK_OK) |
AzureIoTClient | 42:448eecc3676e | 83 | { |
AzureIoTClient | 42:448eecc3676e | 84 | LogError("unable to unlock after locking"); |
AzureIoTClient | 42:448eecc3676e | 85 | } |
AzureIoTClient | 42:448eecc3676e | 86 | (void)Lock_Deinit(savedData->lockGarbage); |
AzureIoTClient | 42:448eecc3676e | 87 | free((void*)savedData); |
AzureIoTClient | 42:448eecc3676e | 88 | } |
AzureIoTClient | 42:448eecc3676e | 89 | else |
AzureIoTClient | 42:448eecc3676e | 90 | { |
AzureIoTClient | 42:448eecc3676e | 91 | if (Unlock(savedData->lockGarbage) != LOCK_OK) |
AzureIoTClient | 42:448eecc3676e | 92 | { |
AzureIoTClient | 42:448eecc3676e | 93 | LogError("unable to unlock after locking"); |
AzureIoTClient | 42:448eecc3676e | 94 | } |
AzureIoTClient | 42:448eecc3676e | 95 | } |
AzureIoTClient | 42:448eecc3676e | 96 | } |
AzureIoTClient | 42:448eecc3676e | 97 | } |
AzureIoTClient | 42:448eecc3676e | 98 | } |
AzureIoTClient | 43:038d8511e817 | 99 | #endif |
AzureIoTClient | 42:448eecc3676e | 100 | |
AzureIoTClient | 16:deba40344375 | 101 | static int ScheduleWork_Thread(void* threadArgument) |
AzureIoTClient | 16:deba40344375 | 102 | { |
AzureIoTClient | 16:deba40344375 | 103 | IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)threadArgument; |
AzureIoTClient | 42:448eecc3676e | 104 | |
Azure.IoT Build | 37:18310e4d888d | 105 | while (1) |
AzureIoTClient | 16:deba40344375 | 106 | { |
AzureIoTClient | 16:deba40344375 | 107 | if (Lock(iotHubClientInstance->LockHandle) == LOCK_OK) |
AzureIoTClient | 16:deba40344375 | 108 | { |
Azure.IoT Build | 37:18310e4d888d | 109 | /*Codes_SRS_IOTHUBCLIENT_01_038: [ The thread shall exit when IoTHubClient_Destroy is called. ]*/ |
Azure.IoT Build | 37:18310e4d888d | 110 | if (iotHubClientInstance->StopThread) |
Azure.IoT Build | 37:18310e4d888d | 111 | { |
Azure.IoT Build | 37:18310e4d888d | 112 | (void)Unlock(iotHubClientInstance->LockHandle); |
Azure.IoT Build | 37:18310e4d888d | 113 | break; /*gets out of the thread*/ |
Azure.IoT Build | 37:18310e4d888d | 114 | } |
Azure.IoT Build | 37:18310e4d888d | 115 | else |
Azure.IoT Build | 37:18310e4d888d | 116 | { |
Azure.IoT Build | 37:18310e4d888d | 117 | /* 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 | 118 | /* 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 | 119 | IoTHubClient_LL_DoWork(iotHubClientInstance->IoTHubClientLLHandle); |
AzureIoTClient | 42:448eecc3676e | 120 | |
AzureIoTClient | 44:33dd78697616 | 121 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 42:448eecc3676e | 122 | garbageCollectorImpl(iotHubClientInstance); |
AzureIoTClient | 43:038d8511e817 | 123 | #endif |
Azure.IoT Build | 37:18310e4d888d | 124 | (void)Unlock(iotHubClientInstance->LockHandle); |
Azure.IoT Build | 37:18310e4d888d | 125 | } |
AzureIoTClient | 16:deba40344375 | 126 | } |
Azure.IoT Build | 37:18310e4d888d | 127 | else |
Azure.IoT Build | 37:18310e4d888d | 128 | { |
Azure.IoT Build | 37:18310e4d888d | 129 | /*Codes_SRS_IOTHUBCLIENT_01_040: [If acquiring the lock fails, IoTHubClient_LL_DoWork shall not be called.]*/ |
Azure.IoT Build | 37:18310e4d888d | 130 | /*no code, shall retry*/ |
Azure.IoT Build | 37:18310e4d888d | 131 | } |
Azure.IoT Build | 37:18310e4d888d | 132 | (void)ThreadAPI_Sleep(1); |
AzureIoTClient | 16:deba40344375 | 133 | } |
AzureIoTClient | 42:448eecc3676e | 134 | |
AzureIoTClient | 16:deba40344375 | 135 | return 0; |
AzureIoTClient | 16:deba40344375 | 136 | } |
AzureIoTClient | 16:deba40344375 | 137 | |
Azure.IoT Build | 37:18310e4d888d | 138 | static IOTHUB_CLIENT_RESULT StartWorkerThreadIfNeeded(IOTHUB_CLIENT_INSTANCE* iotHubClientInstance) |
AzureIoTClient | 16:deba40344375 | 139 | { |
AzureIoTClient | 42:448eecc3676e | 140 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 42:448eecc3676e | 141 | if (iotHubClientInstance->TransportHandle == NULL) |
AzureIoTClient | 42:448eecc3676e | 142 | { |
AzureIoTClient | 42:448eecc3676e | 143 | if (iotHubClientInstance->ThreadHandle == NULL) |
AzureIoTClient | 42:448eecc3676e | 144 | { |
AzureIoTClient | 42:448eecc3676e | 145 | iotHubClientInstance->StopThread = 0; |
AzureIoTClient | 42:448eecc3676e | 146 | if (ThreadAPI_Create(&iotHubClientInstance->ThreadHandle, ScheduleWork_Thread, iotHubClientInstance) != THREADAPI_OK) |
AzureIoTClient | 42:448eecc3676e | 147 | { |
AzureIoTClient | 42:448eecc3676e | 148 | iotHubClientInstance->ThreadHandle = NULL; |
AzureIoTClient | 42:448eecc3676e | 149 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 42:448eecc3676e | 150 | } |
AzureIoTClient | 42:448eecc3676e | 151 | else |
AzureIoTClient | 42:448eecc3676e | 152 | { |
AzureIoTClient | 42:448eecc3676e | 153 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 42:448eecc3676e | 154 | } |
AzureIoTClient | 42:448eecc3676e | 155 | } |
AzureIoTClient | 42:448eecc3676e | 156 | else |
AzureIoTClient | 42:448eecc3676e | 157 | { |
AzureIoTClient | 42:448eecc3676e | 158 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 42:448eecc3676e | 159 | } |
AzureIoTClient | 42:448eecc3676e | 160 | } |
AzureIoTClient | 42:448eecc3676e | 161 | else |
AzureIoTClient | 42:448eecc3676e | 162 | { |
AzureIoTClient | 42:448eecc3676e | 163 | /*Codes_SRS_IOTHUBCLIENT_17_012: [ If the transport connection is shared, the thread shall be started by calling IoTHubTransport_StartWorkerThread. ]*/ |
AzureIoTClient | 42:448eecc3676e | 164 | /*Codes_SRS_IOTHUBCLIENT_17_011: [ If the transport connection is shared, the thread shall be started by calling IoTHubTransport_StartWorkerThread*/ |
Azure.IoT Build | 37:18310e4d888d | 165 | |
AzureIoTClient | 42:448eecc3676e | 166 | result = IoTHubTransport_StartWorkerThread(iotHubClientInstance->TransportHandle, iotHubClientInstance); |
AzureIoTClient | 42:448eecc3676e | 167 | } |
AzureIoTClient | 42:448eecc3676e | 168 | return result; |
AzureIoTClient | 16:deba40344375 | 169 | } |
AzureIoTClient | 16:deba40344375 | 170 | |
AzureIoTClient | 16:deba40344375 | 171 | IOTHUB_CLIENT_HANDLE IoTHubClient_CreateFromConnectionString(const char* connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol) |
AzureIoTClient | 16:deba40344375 | 172 | { |
AzureIoTClient | 16:deba40344375 | 173 | IOTHUB_CLIENT_INSTANCE* result = NULL; |
AzureIoTClient | 16:deba40344375 | 174 | |
AzureIoTClient | 16:deba40344375 | 175 | /* Codes_SRS_IOTHUBCLIENT_12_003: [IoTHubClient_CreateFromConnectionString shall verify the input parameter and if it is NULL then return NULL] */ |
AzureIoTClient | 16:deba40344375 | 176 | if (connectionString == NULL) |
AzureIoTClient | 16:deba40344375 | 177 | { |
AzureIoTClient | 39:2719651a5bee | 178 | LogError("Input parameter is NULL: connectionString"); |
AzureIoTClient | 16:deba40344375 | 179 | } |
AzureIoTClient | 16:deba40344375 | 180 | else if (protocol == NULL) |
AzureIoTClient | 16:deba40344375 | 181 | { |
AzureIoTClient | 39:2719651a5bee | 182 | LogError("Input parameter is NULL: protocol"); |
AzureIoTClient | 16:deba40344375 | 183 | } |
AzureIoTClient | 16:deba40344375 | 184 | else |
AzureIoTClient | 16:deba40344375 | 185 | { |
AzureIoTClient | 16:deba40344375 | 186 | /* Codes_SRS_IOTHUBCLIENT_12_004: [IoTHubClient_CreateFromConnectionString shall allocate a new IoTHubClient instance.] */ |
AzureIoTClient | 16:deba40344375 | 187 | result = malloc(sizeof(IOTHUB_CLIENT_INSTANCE)); |
AzureIoTClient | 16:deba40344375 | 188 | |
AzureIoTClient | 16:deba40344375 | 189 | /* Codes_SRS_IOTHUBCLIENT_12_011: [If the allocation failed, IoTHubClient_CreateFromConnectionString returns NULL] */ |
AzureIoTClient | 16:deba40344375 | 190 | if (result == NULL) |
AzureIoTClient | 16:deba40344375 | 191 | { |
AzureIoTClient | 39:2719651a5bee | 192 | LogError("Malloc failed"); |
AzureIoTClient | 16:deba40344375 | 193 | } |
AzureIoTClient | 16:deba40344375 | 194 | else |
AzureIoTClient | 16:deba40344375 | 195 | { |
AzureIoTClient | 42:448eecc3676e | 196 | /* Codes_SRS_IOTHUBCLIENT_12_005: [IoTHubClient_CreateFromConnectionString shall create a lock object to be used later for serializing IoTHubClient calls] */ |
AzureIoTClient | 42:448eecc3676e | 197 | result->LockHandle = Lock_Init(); |
AzureIoTClient | 42:448eecc3676e | 198 | if (result->LockHandle == NULL) |
AzureIoTClient | 42:448eecc3676e | 199 | { |
AzureIoTClient | 42:448eecc3676e | 200 | /* Codes_SRS_IOTHUBCLIENT_12_009: [If lock creation failed, IoTHubClient_CreateFromConnectionString shall do clean up and return NULL] */ |
AzureIoTClient | 42:448eecc3676e | 201 | free(result); |
AzureIoTClient | 42:448eecc3676e | 202 | result = NULL; |
AzureIoTClient | 42:448eecc3676e | 203 | LogError("Lock_Init failed"); |
AzureIoTClient | 42:448eecc3676e | 204 | } |
AzureIoTClient | 42:448eecc3676e | 205 | else |
AzureIoTClient | 42:448eecc3676e | 206 | { |
AzureIoTClient | 44:33dd78697616 | 207 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 42:448eecc3676e | 208 | /*Codes_SRS_IOTHUBCLIENT_02_059: [ IoTHubClient_CreateFromConnectionString shall create a LIST_HANDLE containing THREAD_HANDLE (created by future calls to IoTHubClient_UploadToBlobAsync). ]*/ |
AzureIoTClient | 42:448eecc3676e | 209 | if ((result->savedDataToBeCleaned = list_create()) == NULL) |
AzureIoTClient | 16:deba40344375 | 210 | { |
AzureIoTClient | 42:448eecc3676e | 211 | /*Codes_SRS_IOTHUBCLIENT_02_070: [ If creating the LIST_HANDLE fails then IoTHubClient_CreateFromConnectionString shall fail and return NULL]*/ |
AzureIoTClient | 42:448eecc3676e | 212 | LogError("unable to list_create"); |
AzureIoTClient | 42:448eecc3676e | 213 | Lock_Deinit(result->LockHandle); |
AzureIoTClient | 16:deba40344375 | 214 | free(result); |
AzureIoTClient | 16:deba40344375 | 215 | result = NULL; |
AzureIoTClient | 16:deba40344375 | 216 | } |
AzureIoTClient | 16:deba40344375 | 217 | else |
AzureIoTClient | 43:038d8511e817 | 218 | #endif |
Azure.IoT Build | 37:18310e4d888d | 219 | { |
Azure.IoT Build | 37:18310e4d888d | 220 | /* 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 | 37:18310e4d888d | 221 | result->IoTHubClientLLHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, protocol); |
Azure.IoT Build | 37:18310e4d888d | 222 | if (result->IoTHubClientLLHandle == NULL) |
Azure.IoT Build | 37:18310e4d888d | 223 | { |
Azure.IoT Build | 37:18310e4d888d | 224 | /* Codes_SRS_IOTHUBCLIENT_12_010: [If IoTHubClient_LL_CreateFromConnectionString fails then IoTHubClient_CreateFromConnectionString shall do clean - up and return NULL] */ |
AzureIoTClient | 44:33dd78697616 | 225 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 42:448eecc3676e | 226 | list_destroy(result->savedDataToBeCleaned); |
AzureIoTClient | 43:038d8511e817 | 227 | #endif |
Azure.IoT Build | 37:18310e4d888d | 228 | Lock_Deinit(result->LockHandle); |
Azure.IoT Build | 37:18310e4d888d | 229 | free(result); |
Azure.IoT Build | 37:18310e4d888d | 230 | result = NULL; |
AzureIoTClient | 39:2719651a5bee | 231 | LogError("IoTHubClient_LL_CreateFromConnectionString failed"); |
Azure.IoT Build | 37:18310e4d888d | 232 | } |
Azure.IoT Build | 37:18310e4d888d | 233 | else |
Azure.IoT Build | 37:18310e4d888d | 234 | { |
Azure.IoT Build | 37:18310e4d888d | 235 | result->ThreadHandle = NULL; |
AzureIoTClient | 42:448eecc3676e | 236 | result->TransportHandle = NULL; |
Azure.IoT Build | 37:18310e4d888d | 237 | } |
AzureIoTClient | 16:deba40344375 | 238 | } |
AzureIoTClient | 42:448eecc3676e | 239 | } |
AzureIoTClient | 42:448eecc3676e | 240 | |
AzureIoTClient | 16:deba40344375 | 241 | } |
AzureIoTClient | 16:deba40344375 | 242 | } |
AzureIoTClient | 16:deba40344375 | 243 | return result; |
AzureIoTClient | 16:deba40344375 | 244 | } |
AzureIoTClient | 16:deba40344375 | 245 | |
AzureIoTClient | 16:deba40344375 | 246 | IOTHUB_CLIENT_HANDLE IoTHubClient_Create(const IOTHUB_CLIENT_CONFIG* config) |
AzureIoTClient | 16:deba40344375 | 247 | { |
AzureIoTClient | 16:deba40344375 | 248 | /* Codes_SRS_IOTHUBCLIENT_01_001: [IoTHubClient_Create shall allocate a new IoTHubClient instance and return a non-NULL handle to it.] */ |
AzureIoTClient | 16:deba40344375 | 249 | IOTHUB_CLIENT_INSTANCE* result = (IOTHUB_CLIENT_INSTANCE*)malloc(sizeof(IOTHUB_CLIENT_INSTANCE)); |
AzureIoTClient | 16:deba40344375 | 250 | |
AzureIoTClient | 16:deba40344375 | 251 | /* Codes_SRS_IOTHUBCLIENT_01_004: [If allocating memory for the new IoTHubClient instance fails, then IoTHubClient_Create shall return NULL.] */ |
AzureIoTClient | 16:deba40344375 | 252 | if (result != NULL) |
AzureIoTClient | 16:deba40344375 | 253 | { |
AzureIoTClient | 16:deba40344375 | 254 | /* Codes_SRS_IOTHUBCLIENT_01_029: [IoTHubClient_Create shall create a lock object to be used later for serializing IoTHubClient calls.] */ |
AzureIoTClient | 16:deba40344375 | 255 | result->LockHandle = Lock_Init(); |
AzureIoTClient | 16:deba40344375 | 256 | if (result->LockHandle == NULL) |
AzureIoTClient | 16:deba40344375 | 257 | { |
AzureIoTClient | 16:deba40344375 | 258 | /* Codes_SRS_IOTHUBCLIENT_01_030: [If creating the lock fails, then IoTHubClient_Create shall return NULL.] */ |
AzureIoTClient | 16:deba40344375 | 259 | /* Codes_SRS_IOTHUBCLIENT_01_031: [If IoTHubClient_Create fails, all resources allocated by it shall be freed.] */ |
AzureIoTClient | 16:deba40344375 | 260 | free(result); |
AzureIoTClient | 16:deba40344375 | 261 | result = NULL; |
AzureIoTClient | 16:deba40344375 | 262 | } |
AzureIoTClient | 16:deba40344375 | 263 | else |
AzureIoTClient | 16:deba40344375 | 264 | { |
AzureIoTClient | 44:33dd78697616 | 265 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 42:448eecc3676e | 266 | /*Codes_SRS_IOTHUBCLIENT_02_060: [ IoTHubClient_Create shall create a LIST_HANDLE containing THREAD_HANDLE (created by future calls to IoTHubClient_UploadToBlobAsync). ]*/ |
AzureIoTClient | 42:448eecc3676e | 267 | if ((result->savedDataToBeCleaned = list_create()) == NULL) |
AzureIoTClient | 16:deba40344375 | 268 | { |
AzureIoTClient | 42:448eecc3676e | 269 | /*Codes_SRS_IOTHUBCLIENT_02_061: [ If creating the LIST_HANDLE fails then IoTHubClient_Create shall fail and return NULL. ]*/ |
AzureIoTClient | 42:448eecc3676e | 270 | LogError("unable to list_create"); |
AzureIoTClient | 16:deba40344375 | 271 | Lock_Deinit(result->LockHandle); |
AzureIoTClient | 16:deba40344375 | 272 | free(result); |
AzureIoTClient | 16:deba40344375 | 273 | result = NULL; |
AzureIoTClient | 16:deba40344375 | 274 | } |
AzureIoTClient | 42:448eecc3676e | 275 | else |
AzureIoTClient | 43:038d8511e817 | 276 | #endif |
AzureIoTClient | 42:448eecc3676e | 277 | { |
AzureIoTClient | 42:448eecc3676e | 278 | /* Codes_SRS_IOTHUBCLIENT_01_002: [IoTHubClient_Create shall instantiate a new IoTHubClient_LL instance by calling IoTHubClient_LL_Create and passing the config argument.] */ |
AzureIoTClient | 42:448eecc3676e | 279 | result->IoTHubClientLLHandle = IoTHubClient_LL_Create(config); |
AzureIoTClient | 42:448eecc3676e | 280 | if (result->IoTHubClientLLHandle == NULL) |
AzureIoTClient | 42:448eecc3676e | 281 | { |
AzureIoTClient | 42:448eecc3676e | 282 | /* Codes_SRS_IOTHUBCLIENT_01_003: [If IoTHubClient_LL_Create fails, then IoTHubClient_Create shall return NULL.] */ |
AzureIoTClient | 42:448eecc3676e | 283 | /* Codes_SRS_IOTHUBCLIENT_01_031: [If IoTHubClient_Create fails, all resources allocated by it shall be freed.] */ |
AzureIoTClient | 42:448eecc3676e | 284 | Lock_Deinit(result->LockHandle); |
AzureIoTClient | 44:33dd78697616 | 285 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 43:038d8511e817 | 286 | list_destroy(result->savedDataToBeCleaned); |
AzureIoTClient | 43:038d8511e817 | 287 | #endif |
AzureIoTClient | 42:448eecc3676e | 288 | free(result); |
AzureIoTClient | 42:448eecc3676e | 289 | result = NULL; |
AzureIoTClient | 42:448eecc3676e | 290 | } |
AzureIoTClient | 42:448eecc3676e | 291 | else |
AzureIoTClient | 42:448eecc3676e | 292 | { |
AzureIoTClient | 42:448eecc3676e | 293 | result->TransportHandle = NULL; |
AzureIoTClient | 42:448eecc3676e | 294 | result->ThreadHandle = NULL; |
AzureIoTClient | 42:448eecc3676e | 295 | } |
AzureIoTClient | 42:448eecc3676e | 296 | } |
AzureIoTClient | 16:deba40344375 | 297 | } |
AzureIoTClient | 16:deba40344375 | 298 | } |
AzureIoTClient | 16:deba40344375 | 299 | |
AzureIoTClient | 16:deba40344375 | 300 | return result; |
AzureIoTClient | 16:deba40344375 | 301 | } |
AzureIoTClient | 16:deba40344375 | 302 | |
Azure.IoT Build | 37:18310e4d888d | 303 | IOTHUB_CLIENT_HANDLE IoTHubClient_CreateWithTransport(TRANSPORT_HANDLE transportHandle, const IOTHUB_CLIENT_CONFIG* config) |
Azure.IoT Build | 37:18310e4d888d | 304 | { |
AzureIoTClient | 42:448eecc3676e | 305 | IOTHUB_CLIENT_INSTANCE* result; |
AzureIoTClient | 42:448eecc3676e | 306 | /*Codes_SRS_IOTHUBCLIENT_17_013: [ IoTHubClient_CreateWithTransport shall return NULL if transportHandle is NULL. ]*/ |
AzureIoTClient | 42:448eecc3676e | 307 | /*Codes_SRS_IOTHUBCLIENT_17_014: [ IoTHubClient_CreateWithTransport shall return NULL if config is NULL. ]*/ |
AzureIoTClient | 42:448eecc3676e | 308 | if (transportHandle == NULL || config == NULL) |
AzureIoTClient | 42:448eecc3676e | 309 | { |
AzureIoTClient | 43:038d8511e817 | 310 | LogError("invalid parameter TRANSPORT_HANDLE transportHandle=%p, const IOTHUB_CLIENT_CONFIG* config=%p", transportHandle, config); |
AzureIoTClient | 42:448eecc3676e | 311 | result = NULL; |
AzureIoTClient | 42:448eecc3676e | 312 | } |
AzureIoTClient | 42:448eecc3676e | 313 | else |
AzureIoTClient | 42:448eecc3676e | 314 | { |
AzureIoTClient | 42:448eecc3676e | 315 | /*Codes_SRS_IOTHUBCLIENT_17_001: [ IoTHubClient_CreateWithTransport shall allocate a new IoTHubClient instance and return a non-NULL handle to it. ]*/ |
AzureIoTClient | 42:448eecc3676e | 316 | result = (IOTHUB_CLIENT_INSTANCE*)malloc(sizeof(IOTHUB_CLIENT_INSTANCE)); |
AzureIoTClient | 42:448eecc3676e | 317 | /*Codes_SRS_IOTHUBCLIENT_17_002: [ If allocating memory for the new IoTHubClient instance fails, then IoTHubClient_CreateWithTransport shall return NULL. ]*/ |
AzureIoTClient | 43:038d8511e817 | 318 | if (result == NULL) |
AzureIoTClient | 43:038d8511e817 | 319 | { |
AzureIoTClient | 43:038d8511e817 | 320 | LogError("unable to malloc"); |
AzureIoTClient | 43:038d8511e817 | 321 | /*return as is*/ |
AzureIoTClient | 43:038d8511e817 | 322 | } |
AzureIoTClient | 43:038d8511e817 | 323 | else |
AzureIoTClient | 42:448eecc3676e | 324 | { |
AzureIoTClient | 44:33dd78697616 | 325 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 43:038d8511e817 | 326 | /*Codes_SRS_IOTHUBCLIENT_02_073: [ IoTHubClient_CreateWithTransport shall create a LIST_HANDLE that shall be used by IoTHubClient_UploadToBlobAsync. ]*/ |
AzureIoTClient | 43:038d8511e817 | 327 | if ((result->savedDataToBeCleaned = list_create()) == NULL) |
AzureIoTClient | 42:448eecc3676e | 328 | { |
AzureIoTClient | 43:038d8511e817 | 329 | /*Codes_SRS_IOTHUBCLIENT_02_074: [ If creating the LIST_HANDLE fails then IoTHubClient_CreateWithTransport shall fail and return NULL. ]*/ |
AzureIoTClient | 43:038d8511e817 | 330 | LogError("unable to list_create"); |
AzureIoTClient | 42:448eecc3676e | 331 | free(result); |
AzureIoTClient | 42:448eecc3676e | 332 | result = NULL; |
AzureIoTClient | 42:448eecc3676e | 333 | } |
AzureIoTClient | 42:448eecc3676e | 334 | else |
AzureIoTClient | 43:038d8511e817 | 335 | #endif |
AzureIoTClient | 42:448eecc3676e | 336 | { |
AzureIoTClient | 43:038d8511e817 | 337 | result->ThreadHandle = NULL; |
AzureIoTClient | 43:038d8511e817 | 338 | result->TransportHandle = transportHandle; |
AzureIoTClient | 43:038d8511e817 | 339 | /*Codes_SRS_IOTHUBCLIENT_17_005: [ IoTHubClient_CreateWithTransport shall call IoTHubTransport_GetLock to get the transport lock to be used later for serializing IoTHubClient calls. ]*/ |
AzureIoTClient | 43:038d8511e817 | 340 | LOCK_HANDLE transportLock = IoTHubTransport_GetLock(transportHandle); |
AzureIoTClient | 43:038d8511e817 | 341 | result->LockHandle = transportLock; |
AzureIoTClient | 43:038d8511e817 | 342 | if (result->LockHandle == NULL) |
AzureIoTClient | 42:448eecc3676e | 343 | { |
AzureIoTClient | 43:038d8511e817 | 344 | LogError("unable to IoTHubTransport_GetLock"); |
AzureIoTClient | 43:038d8511e817 | 345 | /*Codes_SRS_IOTHUBCLIENT_17_006: [ If IoTHubTransport_GetLock fails, then IoTHubClient_CreateWithTransport shall return NULL. ]*/ |
AzureIoTClient | 44:33dd78697616 | 346 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 43:038d8511e817 | 347 | list_destroy(result->savedDataToBeCleaned); |
AzureIoTClient | 43:038d8511e817 | 348 | #endif |
AzureIoTClient | 42:448eecc3676e | 349 | free(result); |
AzureIoTClient | 42:448eecc3676e | 350 | result = NULL; |
AzureIoTClient | 42:448eecc3676e | 351 | } |
AzureIoTClient | 42:448eecc3676e | 352 | else |
AzureIoTClient | 42:448eecc3676e | 353 | { |
AzureIoTClient | 43:038d8511e817 | 354 | IOTHUB_CLIENT_DEVICE_CONFIG deviceConfig; |
AzureIoTClient | 43:038d8511e817 | 355 | deviceConfig.deviceId = config->deviceId; |
AzureIoTClient | 43:038d8511e817 | 356 | deviceConfig.deviceKey = config->deviceKey; |
AzureIoTClient | 43:038d8511e817 | 357 | deviceConfig.protocol = config->protocol; |
AzureIoTClient | 43:038d8511e817 | 358 | deviceConfig.deviceSasToken = config->deviceSasToken; |
AzureIoTClient | 43:038d8511e817 | 359 | deviceConfig.protocol = config->protocol; |
AzureIoTClient | 43:038d8511e817 | 360 | |
AzureIoTClient | 43:038d8511e817 | 361 | /*Codes_SRS_IOTHUBCLIENT_17_003: [ IoTHubClient_CreateWithTransport shall call IoTHubTransport_GetLLTransport on transportHandle to get lower layer transport. ]*/ |
AzureIoTClient | 43:038d8511e817 | 362 | deviceConfig.transportHandle = IoTHubTransport_GetLLTransport(transportHandle); |
AzureIoTClient | 43:038d8511e817 | 363 | |
AzureIoTClient | 43:038d8511e817 | 364 | if (deviceConfig.transportHandle == NULL) |
AzureIoTClient | 42:448eecc3676e | 365 | { |
AzureIoTClient | 43:038d8511e817 | 366 | LogError("unable to IoTHubTransport_GetLLTransport"); |
AzureIoTClient | 43:038d8511e817 | 367 | /*Codes_SRS_IOTHUBCLIENT_17_004: [ If IoTHubTransport_GetLLTransport fails, then IoTHubClient_CreateWithTransport shall return NULL. ]*/ |
AzureIoTClient | 44:33dd78697616 | 368 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 43:038d8511e817 | 369 | list_destroy(result->savedDataToBeCleaned); |
AzureIoTClient | 43:038d8511e817 | 370 | #endif |
AzureIoTClient | 42:448eecc3676e | 371 | free(result); |
AzureIoTClient | 42:448eecc3676e | 372 | result = NULL; |
AzureIoTClient | 42:448eecc3676e | 373 | } |
AzureIoTClient | 42:448eecc3676e | 374 | else |
AzureIoTClient | 42:448eecc3676e | 375 | { |
AzureIoTClient | 43:038d8511e817 | 376 | if (Lock(transportLock) != LOCK_OK) |
AzureIoTClient | 42:448eecc3676e | 377 | { |
AzureIoTClient | 43:038d8511e817 | 378 | LogError("unable to Lock"); |
AzureIoTClient | 44:33dd78697616 | 379 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 43:038d8511e817 | 380 | list_destroy(result->savedDataToBeCleaned); |
AzureIoTClient | 43:038d8511e817 | 381 | #endif |
AzureIoTClient | 42:448eecc3676e | 382 | free(result); |
AzureIoTClient | 42:448eecc3676e | 383 | result = NULL; |
AzureIoTClient | 42:448eecc3676e | 384 | } |
AzureIoTClient | 43:038d8511e817 | 385 | else |
AzureIoTClient | 43:038d8511e817 | 386 | { |
AzureIoTClient | 43:038d8511e817 | 387 | /*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. ]*/ |
AzureIoTClient | 43:038d8511e817 | 388 | result->IoTHubClientLLHandle = IoTHubClient_LL_CreateWithTransport(&deviceConfig); |
AzureIoTClient | 43:038d8511e817 | 389 | if (result->IoTHubClientLLHandle == NULL) |
AzureIoTClient | 43:038d8511e817 | 390 | { |
AzureIoTClient | 43:038d8511e817 | 391 | LogError("unable to IoTHubClient_LL_CreateWithTransport"); |
AzureIoTClient | 43:038d8511e817 | 392 | /*Codes_SRS_IOTHUBCLIENT_17_008: [ If IoTHubClient_LL_CreateWithTransport fails, then IoTHubClient_Create shall return NULL. ]*/ |
AzureIoTClient | 43:038d8511e817 | 393 | /*Codes_SRS_IOTHUBCLIENT_17_009: [ If IoTHubClient_LL_CreateWithTransport fails, all resources allocated by it shall be freed. ]*/ |
AzureIoTClient | 44:33dd78697616 | 394 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 43:038d8511e817 | 395 | list_destroy(result->savedDataToBeCleaned); |
AzureIoTClient | 43:038d8511e817 | 396 | #endif |
AzureIoTClient | 43:038d8511e817 | 397 | free(result); |
AzureIoTClient | 43:038d8511e817 | 398 | result = NULL; |
AzureIoTClient | 43:038d8511e817 | 399 | } |
Azure.IoT Build | 37:18310e4d888d | 400 | |
AzureIoTClient | 43:038d8511e817 | 401 | if (Unlock(transportLock) != LOCK_OK) |
AzureIoTClient | 43:038d8511e817 | 402 | { |
AzureIoTClient | 43:038d8511e817 | 403 | LogError("unable to Unlock"); |
AzureIoTClient | 43:038d8511e817 | 404 | } |
AzureIoTClient | 42:448eecc3676e | 405 | } |
AzureIoTClient | 42:448eecc3676e | 406 | } |
AzureIoTClient | 42:448eecc3676e | 407 | } |
AzureIoTClient | 42:448eecc3676e | 408 | } |
AzureIoTClient | 42:448eecc3676e | 409 | } |
AzureIoTClient | 43:038d8511e817 | 410 | |
AzureIoTClient | 42:448eecc3676e | 411 | } |
Azure.IoT Build | 37:18310e4d888d | 412 | |
AzureIoTClient | 42:448eecc3676e | 413 | return result; |
Azure.IoT Build | 37:18310e4d888d | 414 | } |
Azure.IoT Build | 37:18310e4d888d | 415 | |
AzureIoTClient | 16:deba40344375 | 416 | /* Codes_SRS_IOTHUBCLIENT_01_005: [IoTHubClient_Destroy shall free all resources associated with the iotHubClientHandle instance.] */ |
AzureIoTClient | 18:1e9adb15c645 | 417 | void IoTHubClient_Destroy(IOTHUB_CLIENT_HANDLE iotHubClientHandle) |
AzureIoTClient | 16:deba40344375 | 418 | { |
AzureIoTClient | 16:deba40344375 | 419 | /* Codes_SRS_IOTHUBCLIENT_01_008: [IoTHubClient_Destroy shall do nothing if parameter iotHubClientHandle is NULL.] */ |
AzureIoTClient | 16:deba40344375 | 420 | if (iotHubClientHandle != NULL) |
AzureIoTClient | 16:deba40344375 | 421 | { |
AzureIoTClient | 42:448eecc3676e | 422 | bool okToJoin; |
Azure.IoT Build | 37:18310e4d888d | 423 | |
AzureIoTClient | 16:deba40344375 | 424 | IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle; |
AzureIoTClient | 16:deba40344375 | 425 | |
AzureIoTClient | 42:448eecc3676e | 426 | /*Codes_SRS_IOTHUBCLIENT_02_043: [ IoTHubClient_Destroy shall lock the serializing lock and signal the worker thread (if any) to end ]*/ |
AzureIoTClient | 42:448eecc3676e | 427 | if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK) |
AzureIoTClient | 42:448eecc3676e | 428 | { |
AzureIoTClient | 42:448eecc3676e | 429 | LogError("unable to Lock - - will still proceed to try to end the thread without locking"); |
AzureIoTClient | 42:448eecc3676e | 430 | } |
AzureIoTClient | 42:448eecc3676e | 431 | |
AzureIoTClient | 44:33dd78697616 | 432 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 42:448eecc3676e | 433 | /*Codes_SRS_IOTHUBCLIENT_02_069: [ IoTHubClient_Destroy shall free all data created by IoTHubClient_UploadToBlobAsync ]*/ |
AzureIoTClient | 42:448eecc3676e | 434 | /*wait for all uploading threads to finish*/ |
AzureIoTClient | 42:448eecc3676e | 435 | while (list_get_head_item(iotHubClientInstance->savedDataToBeCleaned) != NULL) |
AzureIoTClient | 42:448eecc3676e | 436 | { |
AzureIoTClient | 42:448eecc3676e | 437 | garbageCollectorImpl(iotHubClientInstance); |
AzureIoTClient | 42:448eecc3676e | 438 | } |
AzureIoTClient | 43:038d8511e817 | 439 | #endif |
AzureIoTClient | 16:deba40344375 | 440 | if (iotHubClientInstance->ThreadHandle != NULL) |
AzureIoTClient | 16:deba40344375 | 441 | { |
AzureIoTClient | 42:448eecc3676e | 442 | iotHubClientInstance->StopThread = 1; |
AzureIoTClient | 42:448eecc3676e | 443 | okToJoin = true; |
AzureIoTClient | 42:448eecc3676e | 444 | } |
AzureIoTClient | 42:448eecc3676e | 445 | else |
AzureIoTClient | 42:448eecc3676e | 446 | { |
AzureIoTClient | 42:448eecc3676e | 447 | okToJoin = false; |
AzureIoTClient | 16:deba40344375 | 448 | } |
Azure.IoT Build | 37:18310e4d888d | 449 | |
AzureIoTClient | 42:448eecc3676e | 450 | if (iotHubClientInstance->TransportHandle != NULL) |
AzureIoTClient | 42:448eecc3676e | 451 | { |
AzureIoTClient | 42:448eecc3676e | 452 | /*Codes_SRS_IOTHUBCLIENT_01_007: [ The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetNotificationMessageCallback shall be joined. ]*/ |
AzureIoTClient | 42:448eecc3676e | 453 | okToJoin = IoTHubTransport_SignalEndWorkerThread(iotHubClientInstance->TransportHandle, iotHubClientHandle); |
AzureIoTClient | 42:448eecc3676e | 454 | } |
AzureIoTClient | 16:deba40344375 | 455 | |
AzureIoTClient | 16:deba40344375 | 456 | /* Codes_SRS_IOTHUBCLIENT_01_006: [That includes destroying the IoTHubClient_LL instance by calling IoTHubClient_LL_Destroy.] */ |
AzureIoTClient | 16:deba40344375 | 457 | IoTHubClient_LL_Destroy(iotHubClientInstance->IoTHubClientLLHandle); |
AzureIoTClient | 16:deba40344375 | 458 | |
AzureIoTClient | 44:33dd78697616 | 459 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 43:038d8511e817 | 460 | if (iotHubClientInstance->savedDataToBeCleaned != NULL) |
AzureIoTClient | 43:038d8511e817 | 461 | { |
AzureIoTClient | 43:038d8511e817 | 462 | list_destroy(iotHubClientInstance->savedDataToBeCleaned); |
AzureIoTClient | 43:038d8511e817 | 463 | } |
AzureIoTClient | 43:038d8511e817 | 464 | #endif |
AzureIoTClient | 43:038d8511e817 | 465 | |
AzureIoTClient | 42:448eecc3676e | 466 | /*Codes_SRS_IOTHUBCLIENT_02_045: [ IoTHubClient_Destroy shall unlock the serializing lock. ]*/ |
AzureIoTClient | 42:448eecc3676e | 467 | if (Unlock(iotHubClientInstance->LockHandle) != LOCK_OK) |
AzureIoTClient | 42:448eecc3676e | 468 | { |
AzureIoTClient | 42:448eecc3676e | 469 | LogError("unable to Unlock"); |
AzureIoTClient | 42:448eecc3676e | 470 | } |
Azure.IoT Build | 37:18310e4d888d | 471 | |
AzureIoTClient | 42:448eecc3676e | 472 | if (okToJoin == true) |
AzureIoTClient | 42:448eecc3676e | 473 | { |
AzureIoTClient | 42:448eecc3676e | 474 | if (iotHubClientInstance->ThreadHandle != NULL) |
AzureIoTClient | 42:448eecc3676e | 475 | { |
AzureIoTClient | 42:448eecc3676e | 476 | int res; |
AzureIoTClient | 42:448eecc3676e | 477 | /*Codes_SRS_IOTHUBCLIENT_01_007: [ The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetNotificationMessageCallback shall be joined. ]*/ |
AzureIoTClient | 42:448eecc3676e | 478 | if (ThreadAPI_Join(iotHubClientInstance->ThreadHandle, &res) != THREADAPI_OK) |
AzureIoTClient | 42:448eecc3676e | 479 | { |
AzureIoTClient | 42:448eecc3676e | 480 | LogError("ThreadAPI_Join failed"); |
AzureIoTClient | 42:448eecc3676e | 481 | } |
AzureIoTClient | 42:448eecc3676e | 482 | } |
AzureIoTClient | 42:448eecc3676e | 483 | if (iotHubClientInstance->TransportHandle != NULL) |
AzureIoTClient | 42:448eecc3676e | 484 | { |
AzureIoTClient | 42:448eecc3676e | 485 | /*Codes_SRS_IOTHUBCLIENT_01_007: [ The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetNotificationMessageCallback shall be joined. ]*/ |
AzureIoTClient | 42:448eecc3676e | 486 | IoTHubTransport_JoinWorkerThread(iotHubClientInstance->TransportHandle, iotHubClientHandle); |
AzureIoTClient | 42:448eecc3676e | 487 | } |
AzureIoTClient | 42:448eecc3676e | 488 | } |
AzureIoTClient | 42:448eecc3676e | 489 | |
AzureIoTClient | 42:448eecc3676e | 490 | if (iotHubClientInstance->TransportHandle == NULL) |
AzureIoTClient | 42:448eecc3676e | 491 | { |
AzureIoTClient | 42:448eecc3676e | 492 | /* Codes_SRS_IOTHUBCLIENT_01_032: [If the lock was allocated in IoTHubClient_Create, it shall be also freed..] */ |
AzureIoTClient | 42:448eecc3676e | 493 | Lock_Deinit(iotHubClientInstance->LockHandle); |
AzureIoTClient | 42:448eecc3676e | 494 | } |
Azure.IoT Build | 37:18310e4d888d | 495 | |
AzureIoTClient | 16:deba40344375 | 496 | free(iotHubClientInstance); |
AzureIoTClient | 16:deba40344375 | 497 | } |
AzureIoTClient | 16:deba40344375 | 498 | } |
AzureIoTClient | 16:deba40344375 | 499 | |
AzureIoTClient | 16:deba40344375 | 500 | IOTHUB_CLIENT_RESULT IoTHubClient_SendEventAsync(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_MESSAGE_HANDLE eventMessageHandle, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK eventConfirmationCallback, void* userContextCallback) |
AzureIoTClient | 16:deba40344375 | 501 | { |
AzureIoTClient | 16:deba40344375 | 502 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 16:deba40344375 | 503 | |
AzureIoTClient | 16:deba40344375 | 504 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 16:deba40344375 | 505 | { |
AzureIoTClient | 16:deba40344375 | 506 | /* Codes_SRS_IOTHUBCLIENT_01_011: [If iotHubClientHandle is NULL, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 16:deba40344375 | 507 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 39:2719651a5bee | 508 | LogError("NULL iothubClientHandle"); |
AzureIoTClient | 16:deba40344375 | 509 | } |
AzureIoTClient | 16:deba40344375 | 510 | else |
AzureIoTClient | 16:deba40344375 | 511 | { |
AzureIoTClient | 16:deba40344375 | 512 | IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle; |
AzureIoTClient | 16:deba40344375 | 513 | |
AzureIoTClient | 16:deba40344375 | 514 | /* Codes_SRS_IOTHUBCLIENT_01_025: [IoTHubClient_SendEventAsync shall be made thread-safe by using the lock created in IoTHubClient_Create.] */ |
AzureIoTClient | 16:deba40344375 | 515 | if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK) |
AzureIoTClient | 16:deba40344375 | 516 | { |
AzureIoTClient | 16:deba40344375 | 517 | /* Codes_SRS_IOTHUBCLIENT_01_026: [If acquiring the lock fails, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 16:deba40344375 | 518 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 39:2719651a5bee | 519 | LogError("Could not acquire lock"); |
AzureIoTClient | 16:deba40344375 | 520 | } |
AzureIoTClient | 16:deba40344375 | 521 | else |
AzureIoTClient | 16:deba40344375 | 522 | { |
AzureIoTClient | 16:deba40344375 | 523 | /* Codes_SRS_IOTHUBCLIENT_01_009: [IoTHubClient_SendEventAsync shall start the worker thread if it was not previously started.] */ |
Azure.IoT Build | 37:18310e4d888d | 524 | if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 16:deba40344375 | 525 | { |
AzureIoTClient | 16:deba40344375 | 526 | /* Codes_SRS_IOTHUBCLIENT_01_010: [If starting the thread fails, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 16:deba40344375 | 527 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 39:2719651a5bee | 528 | LogError("Could not start worker thread"); |
AzureIoTClient | 16:deba40344375 | 529 | } |
AzureIoTClient | 16:deba40344375 | 530 | else |
AzureIoTClient | 16:deba40344375 | 531 | { |
AzureIoTClient | 16:deba40344375 | 532 | /* 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.] */ |
AzureIoTClient | 16:deba40344375 | 533 | /* Codes_SRS_IOTHUBCLIENT_01_013: [When IoTHubClient_LL_SendEventAsync is called, IoTHubClient_SendEventAsync shall return the result of IoTHubClient_LL_SendEventAsync.] */ |
AzureIoTClient | 16:deba40344375 | 534 | result = IoTHubClient_LL_SendEventAsync(iotHubClientInstance->IoTHubClientLLHandle, eventMessageHandle, eventConfirmationCallback, userContextCallback); |
AzureIoTClient | 16:deba40344375 | 535 | } |
AzureIoTClient | 16:deba40344375 | 536 | |
AzureIoTClient | 16:deba40344375 | 537 | /* Codes_SRS_IOTHUBCLIENT_01_025: [IoTHubClient_SendEventAsync shall be made thread-safe by using the lock created in IoTHubClient_Create.] */ |
AzureIoTClient | 16:deba40344375 | 538 | (void)Unlock(iotHubClientInstance->LockHandle); |
AzureIoTClient | 16:deba40344375 | 539 | } |
AzureIoTClient | 16:deba40344375 | 540 | } |
AzureIoTClient | 16:deba40344375 | 541 | |
AzureIoTClient | 16:deba40344375 | 542 | return result; |
AzureIoTClient | 16:deba40344375 | 543 | } |
AzureIoTClient | 16:deba40344375 | 544 | |
AzureIoTClient | 16:deba40344375 | 545 | IOTHUB_CLIENT_RESULT IoTHubClient_GetSendStatus(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_STATUS *iotHubClientStatus) |
AzureIoTClient | 16:deba40344375 | 546 | { |
AzureIoTClient | 16:deba40344375 | 547 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 16:deba40344375 | 548 | |
AzureIoTClient | 16:deba40344375 | 549 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 16:deba40344375 | 550 | { |
AzureIoTClient | 16:deba40344375 | 551 | /* Codes_SRS_IOTHUBCLIENT_01_023: [If iotHubClientHandle is NULL, IoTHubClient_ GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 16:deba40344375 | 552 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 39:2719651a5bee | 553 | LogError("NULL iothubClientHandle"); |
AzureIoTClient | 16:deba40344375 | 554 | } |
AzureIoTClient | 16:deba40344375 | 555 | else |
AzureIoTClient | 16:deba40344375 | 556 | { |
AzureIoTClient | 16:deba40344375 | 557 | IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle; |
AzureIoTClient | 16:deba40344375 | 558 | |
AzureIoTClient | 16:deba40344375 | 559 | /* Codes_SRS_IOTHUBCLIENT_01_033: [IoTHubClient_GetSendStatus shall be made thread-safe by using the lock created in IoTHubClient_Create.] */ |
AzureIoTClient | 16:deba40344375 | 560 | if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK) |
AzureIoTClient | 16:deba40344375 | 561 | { |
AzureIoTClient | 16:deba40344375 | 562 | /* Codes_SRS_IOTHUBCLIENT_01_034: [If acquiring the lock fails, IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 16:deba40344375 | 563 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 39:2719651a5bee | 564 | LogError("Could not acquire lock"); |
AzureIoTClient | 16:deba40344375 | 565 | } |
AzureIoTClient | 16:deba40344375 | 566 | else |
AzureIoTClient | 16:deba40344375 | 567 | { |
AzureIoTClient | 16:deba40344375 | 568 | /* 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 | 569 | /* Codes_SRS_IOTHUBCLIENT_01_024: [Otherwise, IoTHubClient_GetSendStatus shall return the result of IoTHubClient_LL_GetSendStatus.] */ |
AzureIoTClient | 16:deba40344375 | 570 | result = IoTHubClient_LL_GetSendStatus(iotHubClientInstance->IoTHubClientLLHandle, iotHubClientStatus); |
AzureIoTClient | 16:deba40344375 | 571 | |
AzureIoTClient | 16:deba40344375 | 572 | /* Codes_SRS_IOTHUBCLIENT_01_033: [IoTHubClient_GetSendStatus shall be made thread-safe by using the lock created in IoTHubClient_Create.] */ |
AzureIoTClient | 16:deba40344375 | 573 | (void)Unlock(iotHubClientInstance->LockHandle); |
AzureIoTClient | 16:deba40344375 | 574 | } |
AzureIoTClient | 16:deba40344375 | 575 | } |
AzureIoTClient | 16:deba40344375 | 576 | |
AzureIoTClient | 16:deba40344375 | 577 | return result; |
AzureIoTClient | 16:deba40344375 | 578 | } |
AzureIoTClient | 16:deba40344375 | 579 | |
AzureIoTClient | 16:deba40344375 | 580 | IOTHUB_CLIENT_RESULT IoTHubClient_SetMessageCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback, void* userContextCallback) |
AzureIoTClient | 16:deba40344375 | 581 | { |
AzureIoTClient | 16:deba40344375 | 582 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 16:deba40344375 | 583 | |
AzureIoTClient | 16:deba40344375 | 584 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 16:deba40344375 | 585 | { |
AzureIoTClient | 16:deba40344375 | 586 | /* Codes_SRS_IOTHUBCLIENT_01_016: [If iotHubClientHandle is NULL, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 16:deba40344375 | 587 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 39:2719651a5bee | 588 | LogError("NULL iothubClientHandle"); |
AzureIoTClient | 16:deba40344375 | 589 | } |
AzureIoTClient | 16:deba40344375 | 590 | else |
AzureIoTClient | 16:deba40344375 | 591 | { |
AzureIoTClient | 16:deba40344375 | 592 | IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle; |
AzureIoTClient | 16:deba40344375 | 593 | |
AzureIoTClient | 16:deba40344375 | 594 | /* Codes_SRS_IOTHUBCLIENT_01_027: [IoTHubClient_SetMessageCallback shall be made thread-safe by using the lock created in IoTHubClient_Create.] */ |
AzureIoTClient | 16:deba40344375 | 595 | if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK) |
AzureIoTClient | 16:deba40344375 | 596 | { |
AzureIoTClient | 16:deba40344375 | 597 | /* Codes_SRS_IOTHUBCLIENT_01_028: [If acquiring the lock fails, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 16:deba40344375 | 598 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 39:2719651a5bee | 599 | LogError("Could not acquire lock"); |
AzureIoTClient | 16:deba40344375 | 600 | } |
AzureIoTClient | 16:deba40344375 | 601 | else |
AzureIoTClient | 16:deba40344375 | 602 | { |
AzureIoTClient | 16:deba40344375 | 603 | /* Codes_SRS_IOTHUBCLIENT_01_014: [IoTHubClient_SetMessageCallback shall start the worker thread if it was not previously started.] */ |
Azure.IoT Build | 37:18310e4d888d | 604 | if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 16:deba40344375 | 605 | { |
AzureIoTClient | 16:deba40344375 | 606 | /* Codes_SRS_IOTHUBCLIENT_01_015: [If starting the thread fails, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 16:deba40344375 | 607 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 39:2719651a5bee | 608 | LogError("Could not start worker thread"); |
AzureIoTClient | 16:deba40344375 | 609 | } |
AzureIoTClient | 16:deba40344375 | 610 | else |
AzureIoTClient | 16:deba40344375 | 611 | { |
AzureIoTClient | 16:deba40344375 | 612 | /* Codes_SRS_IOTHUBCLIENT_01_017: [IoTHubClient_SetMessageCallback shall call IoTHubClient_LL_SetMessageCallback, while passing the IoTHubClient_LL handle created by IoTHubClient_Create and the parameters messageCallback and userContextCallback.] */ |
AzureIoTClient | 16:deba40344375 | 613 | result = IoTHubClient_LL_SetMessageCallback(iotHubClientInstance->IoTHubClientLLHandle, messageCallback, userContextCallback); |
AzureIoTClient | 16:deba40344375 | 614 | } |
AzureIoTClient | 16:deba40344375 | 615 | |
AzureIoTClient | 16:deba40344375 | 616 | /* Codes_SRS_IOTHUBCLIENT_01_027: [IoTHubClient_SetMessageCallback shall be made thread-safe by using the lock created in IoTHubClient_Create.] */ |
AzureIoTClient | 16:deba40344375 | 617 | Unlock(iotHubClientInstance->LockHandle); |
AzureIoTClient | 16:deba40344375 | 618 | } |
AzureIoTClient | 16:deba40344375 | 619 | } |
AzureIoTClient | 16:deba40344375 | 620 | |
AzureIoTClient | 16:deba40344375 | 621 | return result; |
AzureIoTClient | 16:deba40344375 | 622 | } |
AzureIoTClient | 16:deba40344375 | 623 | |
AzureIoTClient | 16:deba40344375 | 624 | IOTHUB_CLIENT_RESULT IoTHubClient_GetLastMessageReceiveTime(IOTHUB_CLIENT_HANDLE iotHubClientHandle, time_t* lastMessageReceiveTime) |
AzureIoTClient | 16:deba40344375 | 625 | { |
AzureIoTClient | 16:deba40344375 | 626 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 16:deba40344375 | 627 | |
AzureIoTClient | 16:deba40344375 | 628 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 16:deba40344375 | 629 | { |
AzureIoTClient | 16:deba40344375 | 630 | /* Codes_SRS_IOTHUBCLIENT_01_020: [If iotHubClientHandle is NULL, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 16:deba40344375 | 631 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 39:2719651a5bee | 632 | LogError("NULL iothubClientHandle"); |
AzureIoTClient | 16:deba40344375 | 633 | } |
AzureIoTClient | 16:deba40344375 | 634 | else |
AzureIoTClient | 16:deba40344375 | 635 | { |
AzureIoTClient | 16:deba40344375 | 636 | IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle; |
AzureIoTClient | 16:deba40344375 | 637 | |
AzureIoTClient | 16:deba40344375 | 638 | /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */ |
AzureIoTClient | 16:deba40344375 | 639 | if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK) |
AzureIoTClient | 16:deba40344375 | 640 | { |
AzureIoTClient | 16:deba40344375 | 641 | /* Codes_SRS_IOTHUBCLIENT_01_036: [If acquiring the lock fails, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 16:deba40344375 | 642 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 39:2719651a5bee | 643 | LogError("Could not acquire lock"); |
AzureIoTClient | 16:deba40344375 | 644 | } |
AzureIoTClient | 16:deba40344375 | 645 | else |
AzureIoTClient | 16:deba40344375 | 646 | { |
AzureIoTClient | 16:deba40344375 | 647 | /* 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 | 648 | /* Codes_SRS_IOTHUBCLIENT_01_021: [Otherwise, IoTHubClient_GetLastMessageReceiveTime shall return the result of IoTHubClient_LL_GetLastMessageReceiveTime.] */ |
AzureIoTClient | 16:deba40344375 | 649 | result = IoTHubClient_LL_GetLastMessageReceiveTime(iotHubClientInstance->IoTHubClientLLHandle, lastMessageReceiveTime); |
AzureIoTClient | 16:deba40344375 | 650 | |
AzureIoTClient | 16:deba40344375 | 651 | /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */ |
AzureIoTClient | 16:deba40344375 | 652 | Unlock(iotHubClientInstance->LockHandle); |
AzureIoTClient | 16:deba40344375 | 653 | } |
AzureIoTClient | 16:deba40344375 | 654 | } |
AzureIoTClient | 16:deba40344375 | 655 | |
AzureIoTClient | 16:deba40344375 | 656 | return result; |
AzureIoTClient | 16:deba40344375 | 657 | } |
AzureIoTClient | 16:deba40344375 | 658 | |
AzureIoTClient | 16:deba40344375 | 659 | IOTHUB_CLIENT_RESULT IoTHubClient_SetOption(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const char* optionName, const void* value) |
AzureIoTClient | 16:deba40344375 | 660 | { |
AzureIoTClient | 16:deba40344375 | 661 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 16:deba40344375 | 662 | /*Codes_SRS_IOTHUBCLIENT_02_034: [If parameter iotHubClientHandle is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 42:448eecc3676e | 663 | /*Codes_SRS_IOTHUBCLIENT_02_035: [ If parameter optionName is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/ |
AzureIoTClient | 42:448eecc3676e | 664 | /*Codes_SRS_IOTHUBCLIENT_02_036: [ If parameter value is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/ |
AzureIoTClient | 16:deba40344375 | 665 | if ( |
AzureIoTClient | 16:deba40344375 | 666 | (iotHubClientHandle == NULL) || |
AzureIoTClient | 16:deba40344375 | 667 | (optionName == NULL) || |
AzureIoTClient | 16:deba40344375 | 668 | (value == NULL) |
AzureIoTClient | 16:deba40344375 | 669 | ) |
AzureIoTClient | 16:deba40344375 | 670 | { |
AzureIoTClient | 16:deba40344375 | 671 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 16:deba40344375 | 672 | LogError("invalid arg (NULL)r\n"); |
AzureIoTClient | 16:deba40344375 | 673 | } |
AzureIoTClient | 16:deba40344375 | 674 | else |
AzureIoTClient | 16:deba40344375 | 675 | { |
AzureIoTClient | 16:deba40344375 | 676 | IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle; |
AzureIoTClient | 16:deba40344375 | 677 | |
AzureIoTClient | 40:1a94db9139ea | 678 | /* Codes_SRS_IOTHUBCLIENT_01_041: [ IoTHubClient_SetOption shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/ |
AzureIoTClient | 40:1a94db9139ea | 679 | if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK) |
AzureIoTClient | 40:1a94db9139ea | 680 | { |
AzureIoTClient | 40:1a94db9139ea | 681 | /* Codes_SRS_IOTHUBCLIENT_01_042: [ If acquiring the lock fails, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_ERROR. ]*/ |
AzureIoTClient | 40:1a94db9139ea | 682 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 40:1a94db9139ea | 683 | LogError("Could not acquire lock"); |
AzureIoTClient | 40:1a94db9139ea | 684 | } |
AzureIoTClient | 40:1a94db9139ea | 685 | else |
AzureIoTClient | 16:deba40344375 | 686 | { |
AzureIoTClient | 40:1a94db9139ea | 687 | /*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 | 688 | result = IoTHubClient_LL_SetOption(iotHubClientInstance->IoTHubClientLLHandle, optionName, value); |
AzureIoTClient | 40:1a94db9139ea | 689 | if (result != IOTHUB_CLIENT_OK) |
AzureIoTClient | 40:1a94db9139ea | 690 | { |
AzureIoTClient | 40:1a94db9139ea | 691 | LogError("IoTHubClient_LL_SetOption failed"); |
AzureIoTClient | 40:1a94db9139ea | 692 | } |
AzureIoTClient | 40:1a94db9139ea | 693 | |
AzureIoTClient | 40:1a94db9139ea | 694 | Unlock(iotHubClientInstance->LockHandle); |
AzureIoTClient | 16:deba40344375 | 695 | } |
AzureIoTClient | 16:deba40344375 | 696 | } |
AzureIoTClient | 16:deba40344375 | 697 | return result; |
AzureIoTClient | 16:deba40344375 | 698 | } |
AzureIoTClient | 42:448eecc3676e | 699 | |
AzureIoTClient | 44:33dd78697616 | 700 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 42:448eecc3676e | 701 | static int uploadingThread(void *data) |
AzureIoTClient | 42:448eecc3676e | 702 | { |
AzureIoTClient | 42:448eecc3676e | 703 | UPLOADTOBLOB_SAVED_DATA* savedData = (UPLOADTOBLOB_SAVED_DATA*)data; |
AzureIoTClient | 42:448eecc3676e | 704 | |
AzureIoTClient | 42:448eecc3676e | 705 | /*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 | 42:448eecc3676e | 706 | /*not having it protected means multiple simultaneous uploads can happen*/ |
AzureIoTClient | 42:448eecc3676e | 707 | /*Codes_SRS_IOTHUBCLIENT_02_054: [ The thread shall call IoTHubClient_LL_UploadToBlob passing the information packed in the structure. ]*/ |
AzureIoTClient | 42:448eecc3676e | 708 | if (IoTHubClient_LL_UploadToBlob(savedData->iotHubClientHandle->IoTHubClientLLHandle, savedData->destinationFileName, savedData->source, savedData->size) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 42:448eecc3676e | 709 | { |
AzureIoTClient | 42:448eecc3676e | 710 | LogError("unable to IoTHubClient_LL_UploadToBlob"); |
AzureIoTClient | 42:448eecc3676e | 711 | /*call the callback*/ |
AzureIoTClient | 42:448eecc3676e | 712 | if (savedData->iotHubClientFileUploadCallback != NULL) |
AzureIoTClient | 42:448eecc3676e | 713 | { |
AzureIoTClient | 42:448eecc3676e | 714 | /*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 | 42:448eecc3676e | 715 | savedData->iotHubClientFileUploadCallback(FILE_UPLOAD_ERROR, savedData->context); |
AzureIoTClient | 42:448eecc3676e | 716 | } |
AzureIoTClient | 42:448eecc3676e | 717 | } |
AzureIoTClient | 42:448eecc3676e | 718 | else |
AzureIoTClient | 42:448eecc3676e | 719 | { |
AzureIoTClient | 42:448eecc3676e | 720 | if (savedData->iotHubClientFileUploadCallback != NULL) |
AzureIoTClient | 42:448eecc3676e | 721 | { |
AzureIoTClient | 42:448eecc3676e | 722 | /*Codes_SRS_IOTHUBCLIENT_02_056: [ Otherwise the thread iotHubClientFileUploadCallbackInternal passing as result FILE_UPLOAD_OK and the structure from SRS IOTHUBCLIENT 02 051. ]*/ |
AzureIoTClient | 42:448eecc3676e | 723 | savedData->iotHubClientFileUploadCallback(FILE_UPLOAD_OK, savedData->context); |
AzureIoTClient | 42:448eecc3676e | 724 | } |
AzureIoTClient | 42:448eecc3676e | 725 | } |
AzureIoTClient | 42:448eecc3676e | 726 | |
AzureIoTClient | 42:448eecc3676e | 727 | /*Codes_SRS_IOTHUBCLIENT_02_071: [ The thread shall mark itself as disposable. ]*/ |
AzureIoTClient | 42:448eecc3676e | 728 | if (Lock(savedData->lockGarbage) != LOCK_OK) |
AzureIoTClient | 42:448eecc3676e | 729 | { |
AzureIoTClient | 42:448eecc3676e | 730 | LogError("unable to Lock - trying anyway"); |
AzureIoTClient | 42:448eecc3676e | 731 | savedData->canBeGarbageCollected = 1; |
AzureIoTClient | 42:448eecc3676e | 732 | } |
AzureIoTClient | 42:448eecc3676e | 733 | else |
AzureIoTClient | 42:448eecc3676e | 734 | { |
AzureIoTClient | 42:448eecc3676e | 735 | savedData->canBeGarbageCollected = 1; |
AzureIoTClient | 42:448eecc3676e | 736 | |
AzureIoTClient | 42:448eecc3676e | 737 | if (Unlock(savedData->lockGarbage) != LOCK_OK) |
AzureIoTClient | 42:448eecc3676e | 738 | { |
AzureIoTClient | 42:448eecc3676e | 739 | LogError("unable to Unlock after locking"); |
AzureIoTClient | 42:448eecc3676e | 740 | } |
AzureIoTClient | 42:448eecc3676e | 741 | } |
AzureIoTClient | 42:448eecc3676e | 742 | return 0; |
AzureIoTClient | 42:448eecc3676e | 743 | } |
AzureIoTClient | 43:038d8511e817 | 744 | #endif |
AzureIoTClient | 42:448eecc3676e | 745 | |
AzureIoTClient | 44:33dd78697616 | 746 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 42:448eecc3676e | 747 | 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 | 748 | { |
AzureIoTClient | 42:448eecc3676e | 749 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 42:448eecc3676e | 750 | /*Codes_SRS_IOTHUBCLIENT_02_047: [ If iotHubClientHandle is NULL then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/ |
AzureIoTClient | 42:448eecc3676e | 751 | /*Codes_SRS_IOTHUBCLIENT_02_048: [ If destinationFileName is NULL then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/ |
AzureIoTClient | 42:448eecc3676e | 752 | /*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 | 753 | if ( |
AzureIoTClient | 42:448eecc3676e | 754 | (iotHubClientHandle == NULL) || |
AzureIoTClient | 42:448eecc3676e | 755 | (destinationFileName == NULL) || |
AzureIoTClient | 42:448eecc3676e | 756 | ((source == NULL) && (size > 0)) |
AzureIoTClient | 42:448eecc3676e | 757 | ) |
AzureIoTClient | 42:448eecc3676e | 758 | { |
AzureIoTClient | 42:448eecc3676e | 759 | 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 | 760 | iotHubClientHandle, |
AzureIoTClient | 42:448eecc3676e | 761 | destinationFileName, |
AzureIoTClient | 42:448eecc3676e | 762 | source, |
AzureIoTClient | 42:448eecc3676e | 763 | size, |
AzureIoTClient | 42:448eecc3676e | 764 | iotHubClientFileUploadCallback, |
AzureIoTClient | 42:448eecc3676e | 765 | context |
AzureIoTClient | 42:448eecc3676e | 766 | ); |
AzureIoTClient | 42:448eecc3676e | 767 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 42:448eecc3676e | 768 | } |
AzureIoTClient | 42:448eecc3676e | 769 | else |
AzureIoTClient | 42:448eecc3676e | 770 | { |
AzureIoTClient | 42:448eecc3676e | 771 | /*Codes_SRS_IOTHUBCLIENT_02_051: [IoTHubClient_UploadToBlobAsync shall copy the souce, size, iotHubClientFileUploadCallback, context into a structure.]*/ |
AzureIoTClient | 42:448eecc3676e | 772 | UPLOADTOBLOB_SAVED_DATA *savedData = (UPLOADTOBLOB_SAVED_DATA *)malloc(sizeof(UPLOADTOBLOB_SAVED_DATA)); |
AzureIoTClient | 42:448eecc3676e | 773 | if (savedData == NULL) |
AzureIoTClient | 42:448eecc3676e | 774 | { |
AzureIoTClient | 42:448eecc3676e | 775 | /*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 | 776 | LogError("unable to malloc - oom"); |
AzureIoTClient | 42:448eecc3676e | 777 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 42:448eecc3676e | 778 | } |
AzureIoTClient | 42:448eecc3676e | 779 | else |
AzureIoTClient | 42:448eecc3676e | 780 | { |
AzureIoTClient | 42:448eecc3676e | 781 | if (mallocAndStrcpy_s((char**)&savedData->destinationFileName, destinationFileName) != 0) |
AzureIoTClient | 42:448eecc3676e | 782 | { |
AzureIoTClient | 42:448eecc3676e | 783 | /*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 | 784 | LogError("unable to mallocAndStrcpy_s"); |
AzureIoTClient | 42:448eecc3676e | 785 | free(savedData); |
AzureIoTClient | 42:448eecc3676e | 786 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 42:448eecc3676e | 787 | } |
AzureIoTClient | 42:448eecc3676e | 788 | else |
AzureIoTClient | 42:448eecc3676e | 789 | { |
AzureIoTClient | 42:448eecc3676e | 790 | savedData->size = size; |
AzureIoTClient | 42:448eecc3676e | 791 | int sourceCloned; |
AzureIoTClient | 42:448eecc3676e | 792 | if (size == 0) |
AzureIoTClient | 42:448eecc3676e | 793 | { |
AzureIoTClient | 42:448eecc3676e | 794 | savedData->source = NULL; |
AzureIoTClient | 42:448eecc3676e | 795 | sourceCloned = 1; |
AzureIoTClient | 42:448eecc3676e | 796 | } |
AzureIoTClient | 42:448eecc3676e | 797 | else |
AzureIoTClient | 42:448eecc3676e | 798 | { |
AzureIoTClient | 42:448eecc3676e | 799 | savedData->source = (unsigned char*)malloc(size); |
AzureIoTClient | 42:448eecc3676e | 800 | if (savedData->source == NULL) |
AzureIoTClient | 42:448eecc3676e | 801 | { |
AzureIoTClient | 42:448eecc3676e | 802 | /*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 | 803 | LogError("unable to malloc - oom"); |
AzureIoTClient | 42:448eecc3676e | 804 | free(savedData->destinationFileName); |
AzureIoTClient | 42:448eecc3676e | 805 | free(savedData); |
AzureIoTClient | 42:448eecc3676e | 806 | sourceCloned = 0; |
AzureIoTClient | 42:448eecc3676e | 807 | |
AzureIoTClient | 42:448eecc3676e | 808 | } |
AzureIoTClient | 42:448eecc3676e | 809 | else |
AzureIoTClient | 42:448eecc3676e | 810 | { |
AzureIoTClient | 42:448eecc3676e | 811 | sourceCloned = 1; |
AzureIoTClient | 42:448eecc3676e | 812 | } |
AzureIoTClient | 42:448eecc3676e | 813 | } |
AzureIoTClient | 42:448eecc3676e | 814 | |
AzureIoTClient | 42:448eecc3676e | 815 | if (sourceCloned == 0) |
AzureIoTClient | 42:448eecc3676e | 816 | { |
AzureIoTClient | 42:448eecc3676e | 817 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 42:448eecc3676e | 818 | } |
AzureIoTClient | 42:448eecc3676e | 819 | else |
AzureIoTClient | 42:448eecc3676e | 820 | { |
AzureIoTClient | 42:448eecc3676e | 821 | savedData->iotHubClientFileUploadCallback = iotHubClientFileUploadCallback; |
AzureIoTClient | 42:448eecc3676e | 822 | savedData->context = context; |
AzureIoTClient | 42:448eecc3676e | 823 | memcpy(savedData->source, source, size); |
AzureIoTClient | 42:448eecc3676e | 824 | IOTHUB_CLIENT_INSTANCE* iotHubClientHandleData = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle; |
AzureIoTClient | 42:448eecc3676e | 825 | if (Lock(iotHubClientHandleData->LockHandle) != LOCK_OK) /*locking because the next statement is changing blobThreadsToBeJoined*/ |
AzureIoTClient | 42:448eecc3676e | 826 | { |
AzureIoTClient | 42:448eecc3676e | 827 | LogError("unable to lock"); |
AzureIoTClient | 42:448eecc3676e | 828 | free(savedData->source); |
AzureIoTClient | 42:448eecc3676e | 829 | free(savedData->destinationFileName); |
AzureIoTClient | 42:448eecc3676e | 830 | free(savedData); |
AzureIoTClient | 42:448eecc3676e | 831 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 42:448eecc3676e | 832 | } |
AzureIoTClient | 42:448eecc3676e | 833 | else |
AzureIoTClient | 42:448eecc3676e | 834 | { |
AzureIoTClient | 42:448eecc3676e | 835 | if ((result = StartWorkerThreadIfNeeded(iotHubClientHandleData)) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 42:448eecc3676e | 836 | { |
AzureIoTClient | 42:448eecc3676e | 837 | free(savedData->source); |
AzureIoTClient | 42:448eecc3676e | 838 | free(savedData->destinationFileName); |
AzureIoTClient | 42:448eecc3676e | 839 | free(savedData); |
AzureIoTClient | 42:448eecc3676e | 840 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 42:448eecc3676e | 841 | LogError("Could not start worker thread"); |
AzureIoTClient | 42:448eecc3676e | 842 | } |
AzureIoTClient | 42:448eecc3676e | 843 | else |
AzureIoTClient | 42:448eecc3676e | 844 | { |
AzureIoTClient | 42:448eecc3676e | 845 | /*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 | 42:448eecc3676e | 846 | LIST_ITEM_HANDLE item = list_add(iotHubClientHandleData->savedDataToBeCleaned, savedData); |
AzureIoTClient | 42:448eecc3676e | 847 | if (item == NULL) |
AzureIoTClient | 42:448eecc3676e | 848 | { |
AzureIoTClient | 42:448eecc3676e | 849 | LogError("unable to list_add"); |
AzureIoTClient | 42:448eecc3676e | 850 | free(savedData->source); |
AzureIoTClient | 42:448eecc3676e | 851 | free(savedData->destinationFileName); |
AzureIoTClient | 42:448eecc3676e | 852 | free(savedData); |
AzureIoTClient | 42:448eecc3676e | 853 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 42:448eecc3676e | 854 | } |
AzureIoTClient | 42:448eecc3676e | 855 | else |
AzureIoTClient | 42:448eecc3676e | 856 | { |
AzureIoTClient | 42:448eecc3676e | 857 | savedData->iotHubClientHandle = iotHubClientHandle; |
AzureIoTClient | 42:448eecc3676e | 858 | savedData->canBeGarbageCollected = 0; |
AzureIoTClient | 42:448eecc3676e | 859 | if ((savedData->lockGarbage = Lock_Init()) == NULL) |
AzureIoTClient | 42:448eecc3676e | 860 | { |
AzureIoTClient | 42:448eecc3676e | 861 | (void)list_remove(iotHubClientHandleData->savedDataToBeCleaned, item); |
AzureIoTClient | 42:448eecc3676e | 862 | free(savedData->source); |
AzureIoTClient | 42:448eecc3676e | 863 | free(savedData->destinationFileName); |
AzureIoTClient | 42:448eecc3676e | 864 | free(savedData); |
AzureIoTClient | 42:448eecc3676e | 865 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 42:448eecc3676e | 866 | LogError("unable to Lock_Init"); |
AzureIoTClient | 42:448eecc3676e | 867 | } |
AzureIoTClient | 42:448eecc3676e | 868 | else |
AzureIoTClient | 42:448eecc3676e | 869 | { |
AzureIoTClient | 42:448eecc3676e | 870 | /*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 | 871 | if (ThreadAPI_Create(&savedData->uploadingThreadHandle, uploadingThread, savedData) != THREADAPI_OK) |
AzureIoTClient | 42:448eecc3676e | 872 | { |
AzureIoTClient | 42:448eecc3676e | 873 | /*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 | 874 | LogError("unablet to ThreadAPI_Create"); |
AzureIoTClient | 42:448eecc3676e | 875 | (void)Lock_Deinit(savedData->lockGarbage); |
AzureIoTClient | 42:448eecc3676e | 876 | (void)list_remove(iotHubClientHandleData->savedDataToBeCleaned, item); |
AzureIoTClient | 42:448eecc3676e | 877 | free(savedData->source); |
AzureIoTClient | 42:448eecc3676e | 878 | free(savedData->destinationFileName); |
AzureIoTClient | 42:448eecc3676e | 879 | free(savedData); |
AzureIoTClient | 42:448eecc3676e | 880 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 42:448eecc3676e | 881 | } |
AzureIoTClient | 42:448eecc3676e | 882 | else |
AzureIoTClient | 42:448eecc3676e | 883 | { |
AzureIoTClient | 42:448eecc3676e | 884 | |
AzureIoTClient | 42:448eecc3676e | 885 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 42:448eecc3676e | 886 | } |
AzureIoTClient | 42:448eecc3676e | 887 | } |
AzureIoTClient | 42:448eecc3676e | 888 | } |
AzureIoTClient | 42:448eecc3676e | 889 | } |
AzureIoTClient | 42:448eecc3676e | 890 | Unlock(iotHubClientHandleData->LockHandle); |
AzureIoTClient | 42:448eecc3676e | 891 | } |
AzureIoTClient | 42:448eecc3676e | 892 | } |
AzureIoTClient | 42:448eecc3676e | 893 | } |
AzureIoTClient | 42:448eecc3676e | 894 | } |
AzureIoTClient | 42:448eecc3676e | 895 | } |
AzureIoTClient | 42:448eecc3676e | 896 | return result; |
AzureIoTClient | 42:448eecc3676e | 897 | } |
AzureIoTClient | 44:33dd78697616 | 898 | #endif /*DONT_USE_UPLOADTOBLOB*/ |