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