Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

This library implements the Microsoft Azure IoTHub client library. The code is replicated from https://github.com/Azure/azure-iot-sdks

Committer:
AzureIoTClient
Date:
Wed Nov 16 21:37:53 2016 -0800
Revision:
53:1e5a1ca1f274
Parent:
52:1cc3c6d07cad
Child:
54:6dcad9019a64
1.0.10

Who changed what in which revision?

UserRevisionLine numberNew 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 52:1cc3c6d07cad 20 #include "azure_c_shared_utility/singlylinkedlist.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 52:1cc3c6d07cad 30 SINGLYLINKEDLIST_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 52:1cc3c6d07cad 58 LIST_ITEM_HANDLE item = singlylinkedlist_get_head_item(iotHubClientInstance->savedDataToBeCleaned);
AzureIoTClient 42:448eecc3676e 59 while (item != NULL)
AzureIoTClient 42:448eecc3676e 60 {
AzureIoTClient 52:1cc3c6d07cad 61 const UPLOADTOBLOB_SAVED_DATA* savedData = (const UPLOADTOBLOB_SAVED_DATA*)singlylinkedlist_item_get_value(item);
AzureIoTClient 42:448eecc3676e 62 LIST_ITEM_HANDLE old_item = item;
AzureIoTClient 52:1cc3c6d07cad 63 item = singlylinkedlist_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, &notUsed) != THREADAPI_OK)
AzureIoTClient 42:448eecc3676e 75 {
AzureIoTClient 42:448eecc3676e 76 LogError("unable to ThreadAPI_Join");
AzureIoTClient 42:448eecc3676e 77 }
AzureIoTClient 52:1cc3c6d07cad 78 (void)singlylinkedlist_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 53:1e5a1ca1f274 187 result = (IOTHUB_CLIENT_INSTANCE *) 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 52:1cc3c6d07cad 208 /*Codes_SRS_IOTHUBCLIENT_02_059: [ IoTHubClient_CreateFromConnectionString shall create a SINGLYLINKEDLIST_HANDLE containing THREAD_HANDLE (created by future calls to IoTHubClient_UploadToBlobAsync). ]*/
AzureIoTClient 52:1cc3c6d07cad 209 if ((result->savedDataToBeCleaned = singlylinkedlist_create()) == NULL)
AzureIoTClient 16:deba40344375 210 {
AzureIoTClient 52:1cc3c6d07cad 211 /*Codes_SRS_IOTHUBCLIENT_02_070: [ If creating the SINGLYLINKEDLIST_HANDLE fails then IoTHubClient_CreateFromConnectionString shall fail and return NULL]*/
AzureIoTClient 52:1cc3c6d07cad 212 LogError("unable to singlylinkedlist_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 52:1cc3c6d07cad 226 singlylinkedlist_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 52:1cc3c6d07cad 266 /*Codes_SRS_IOTHUBCLIENT_02_060: [ IoTHubClient_Create shall create a SINGLYLINKEDLIST_HANDLE containing THREAD_HANDLE (created by future calls to IoTHubClient_UploadToBlobAsync). ]*/
AzureIoTClient 52:1cc3c6d07cad 267 if ((result->savedDataToBeCleaned = singlylinkedlist_create()) == NULL)
AzureIoTClient 16:deba40344375 268 {
AzureIoTClient 52:1cc3c6d07cad 269 /*Codes_SRS_IOTHUBCLIENT_02_061: [ If creating the SINGLYLINKEDLIST_HANDLE fails then IoTHubClient_Create shall fail and return NULL. ]*/
AzureIoTClient 52:1cc3c6d07cad 270 LogError("unable to singlylinkedlist_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 52:1cc3c6d07cad 286 singlylinkedlist_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 52:1cc3c6d07cad 326 /*Codes_SRS_IOTHUBCLIENT_02_073: [ IoTHubClient_CreateWithTransport shall create a SINGLYLINKEDLIST_HANDLE that shall be used by IoTHubClient_UploadToBlobAsync. ]*/
AzureIoTClient 52:1cc3c6d07cad 327 if ((result->savedDataToBeCleaned = singlylinkedlist_create()) == NULL)
AzureIoTClient 42:448eecc3676e 328 {
AzureIoTClient 52:1cc3c6d07cad 329 /*Codes_SRS_IOTHUBCLIENT_02_074: [ If creating the SINGLYLINKEDLIST_HANDLE fails then IoTHubClient_CreateWithTransport shall fail and return NULL. ]*/
AzureIoTClient 52:1cc3c6d07cad 330 LogError("unable to singlylinkedlist_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 52:1cc3c6d07cad 347 singlylinkedlist_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 52:1cc3c6d07cad 369 singlylinkedlist_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 52:1cc3c6d07cad 380 singlylinkedlist_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 52:1cc3c6d07cad 395 singlylinkedlist_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 52:1cc3c6d07cad 435 while (singlylinkedlist_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 52:1cc3c6d07cad 462 singlylinkedlist_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 53:1e5a1ca1f274 617 (void)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 53:1e5a1ca1f274 624 IOTHUB_CLIENT_RESULT IoTHubClient_SetConnectionStatusCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK connectionStatusCallback, void * userContextCallback)
AzureIoTClient 52:1cc3c6d07cad 625 {
AzureIoTClient 53:1e5a1ca1f274 626 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 627
AzureIoTClient 53:1e5a1ca1f274 628 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 629 {
AzureIoTClient 53:1e5a1ca1f274 630 /* Codes_SRS_IOTHUBCLIENT_25_076: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ] */
AzureIoTClient 53:1e5a1ca1f274 631 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 632 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 633 }
AzureIoTClient 53:1e5a1ca1f274 634 else
AzureIoTClient 53:1e5a1ca1f274 635 {
AzureIoTClient 53:1e5a1ca1f274 636 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 52:1cc3c6d07cad 637
AzureIoTClient 53:1e5a1ca1f274 638 /* Codes_SRS_IOTHUBCLIENT_25_087: [ `IoTHubClient_SetConnectionStatusCallback` shall be made thread-safe by using the lock created in `IoTHubClient_Create`. ] */
AzureIoTClient 53:1e5a1ca1f274 639 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 640 {
AzureIoTClient 53:1e5a1ca1f274 641 /* Codes_SRS_IOTHUBCLIENT_25_088: [ If acquiring the lock fails, `IoTHubClient_SetConnectionStatusCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 642 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 643 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 644 }
AzureIoTClient 53:1e5a1ca1f274 645 else
AzureIoTClient 53:1e5a1ca1f274 646 {
AzureIoTClient 53:1e5a1ca1f274 647 /* Codes_SRS_IOTHUBCLIENT_25_081: [ `IoTHubClient_SetConnectionStatusCallback` shall start the worker thread if it was not previously started. ]*/
AzureIoTClient 53:1e5a1ca1f274 648 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 649 {
AzureIoTClient 53:1e5a1ca1f274 650 /* Codes_SRS_IOTHUBCLIENT_25_083: [ If starting the thread fails, `IoTHubClient_SetConnectionStatusCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 651 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 652 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 653 }
AzureIoTClient 53:1e5a1ca1f274 654 else
AzureIoTClient 53:1e5a1ca1f274 655 {
AzureIoTClient 53:1e5a1ca1f274 656 /* Codes_SRS_IOTHUBCLIENT_25_085: [ `IoTHubClient_SetConnectionStatusCallback` shall call `IoTHubClient_LL_SetConnectionStatusCallback`, while passing the `IoTHubClient_LL` handle created by `IoTHubClient_Create` and the parameters `connectionStatusCallback` and `userContextCallback`. ]*/
AzureIoTClient 53:1e5a1ca1f274 657 result = IoTHubClient_LL_SetConnectionStatusCallback(iotHubClientInstance->IoTHubClientLLHandle, connectionStatusCallback, userContextCallback);
AzureIoTClient 53:1e5a1ca1f274 658 }
AzureIoTClient 53:1e5a1ca1f274 659
AzureIoTClient 53:1e5a1ca1f274 660 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 661 }
AzureIoTClient 53:1e5a1ca1f274 662 }
AzureIoTClient 52:1cc3c6d07cad 663
AzureIoTClient 52:1cc3c6d07cad 664 return result;
AzureIoTClient 53:1e5a1ca1f274 665
AzureIoTClient 52:1cc3c6d07cad 666 }
AzureIoTClient 52:1cc3c6d07cad 667
AzureIoTClient 53:1e5a1ca1f274 668 IOTHUB_CLIENT_RESULT IoTHubClient_SetRetryPolicy(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 669 {
AzureIoTClient 53:1e5a1ca1f274 670 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 671
AzureIoTClient 53:1e5a1ca1f274 672 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 673 {
AzureIoTClient 53:1e5a1ca1f274 674 /* Codes_SRS_IOTHUBCLIENT_25_076: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ] */
AzureIoTClient 53:1e5a1ca1f274 675 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 676 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 677 }
AzureIoTClient 53:1e5a1ca1f274 678 else
AzureIoTClient 53:1e5a1ca1f274 679 {
AzureIoTClient 53:1e5a1ca1f274 680 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 681
AzureIoTClient 53:1e5a1ca1f274 682 /* Codes_SRS_IOTHUBCLIENT_25_079: [ `IoTHubClient_SetRetryPolicy` shall be made thread-safe by using the lock created in `IoTHubClient_Create`.] */
AzureIoTClient 53:1e5a1ca1f274 683 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 684 {
AzureIoTClient 53:1e5a1ca1f274 685 /* Codes_SRS_IOTHUBCLIENT_25_080: [ If acquiring the lock fails, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 686 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 687 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 688 }
AzureIoTClient 53:1e5a1ca1f274 689 else
AzureIoTClient 53:1e5a1ca1f274 690 {
AzureIoTClient 53:1e5a1ca1f274 691 /* Codes_SRS_IOTHUBCLIENT_25_073: [ `IoTHubClient_SetRetryPolicy` shall start the worker thread if it was not previously started. ] */
AzureIoTClient 53:1e5a1ca1f274 692 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 693 {
AzureIoTClient 53:1e5a1ca1f274 694 /* Codes_SRS_IOTHUBCLIENT_25_075: [ If starting the thread fails, `IoTHubClient_SetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 695 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 696 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 697 }
AzureIoTClient 53:1e5a1ca1f274 698 else
AzureIoTClient 53:1e5a1ca1f274 699 {
AzureIoTClient 53:1e5a1ca1f274 700 /* Codes_SRS_IOTHUBCLIENT_25_077: [ `IoTHubClient_SetRetryPolicy` shall call `IoTHubClient_LL_SetRetryPolicy`, while passing the `IoTHubClient_LL` handle created by `IoTHubClient_Create` and the parameters `retryPolicy` and `retryTimeoutLimitinSeconds`.]*/
AzureIoTClient 53:1e5a1ca1f274 701 result = IoTHubClient_LL_SetRetryPolicy(iotHubClientInstance->IoTHubClientLLHandle, retryPolicy, retryTimeoutLimitInSeconds);
AzureIoTClient 53:1e5a1ca1f274 702 }
AzureIoTClient 53:1e5a1ca1f274 703
AzureIoTClient 53:1e5a1ca1f274 704 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 705 }
AzureIoTClient 53:1e5a1ca1f274 706 }
AzureIoTClient 52:1cc3c6d07cad 707
AzureIoTClient 52:1cc3c6d07cad 708 return result;
AzureIoTClient 52:1cc3c6d07cad 709 }
AzureIoTClient 52:1cc3c6d07cad 710
AzureIoTClient 53:1e5a1ca1f274 711 IOTHUB_CLIENT_RESULT IoTHubClient_GetRetryPolicy(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY * retryPolicy, size_t * retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 712 {
AzureIoTClient 53:1e5a1ca1f274 713 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 714
AzureIoTClient 53:1e5a1ca1f274 715 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 716 {
AzureIoTClient 53:1e5a1ca1f274 717 /* Codes_SRS_IOTHUBCLIENT_25_092: [ If `iotHubClientHandle` is `NULL`, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 53:1e5a1ca1f274 718 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 719 LogError("NULL iothubClientHandle");
AzureIoTClient 53:1e5a1ca1f274 720 }
AzureIoTClient 53:1e5a1ca1f274 721 else
AzureIoTClient 53:1e5a1ca1f274 722 {
AzureIoTClient 53:1e5a1ca1f274 723 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 724
AzureIoTClient 53:1e5a1ca1f274 725 /* Codes_SRS_IOTHUBCLIENT_25_095: [ `IoTHubClient_GetRetryPolicy` shall be made thread-safe by using the lock created in `IoTHubClient_Create`. ]*/
AzureIoTClient 53:1e5a1ca1f274 726 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 727 {
AzureIoTClient 53:1e5a1ca1f274 728 /* Codes_SRS_IOTHUBCLIENT_25_096: [ If acquiring the lock fails, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 729 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 730 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 731 }
AzureIoTClient 53:1e5a1ca1f274 732 else
AzureIoTClient 53:1e5a1ca1f274 733 {
AzureIoTClient 53:1e5a1ca1f274 734 /* Codes_SRS_IOTHUBCLIENT_25_089: [ `IoTHubClient_GetRetryPolicy` shall start the worker thread if it was not previously started.]*/
AzureIoTClient 53:1e5a1ca1f274 735 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 736 {
AzureIoTClient 53:1e5a1ca1f274 737 /* Codes_SRS_IOTHUBCLIENT_25_091: [ If starting the thread fails, `IoTHubClient_GetRetryPolicy` shall return `IOTHUB_CLIENT_ERROR`.]*/
AzureIoTClient 53:1e5a1ca1f274 738 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 739 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 740 }
AzureIoTClient 53:1e5a1ca1f274 741 else
AzureIoTClient 53:1e5a1ca1f274 742 {
AzureIoTClient 53:1e5a1ca1f274 743 /* Codes_SRS_IOTHUBCLIENT_25_093: [ `IoTHubClient_GetRetryPolicy` shall call `IoTHubClient_LL_GetRetryPolicy`, while passing the `IoTHubClient_LL` handle created by `IoTHubClient_Create` and the parameters `connectionStatusCallback` and `userContextCallback`.]*/
AzureIoTClient 53:1e5a1ca1f274 744 result = IoTHubClient_LL_GetRetryPolicy(iotHubClientInstance->IoTHubClientLLHandle, retryPolicy, retryTimeoutLimitInSeconds);
AzureIoTClient 53:1e5a1ca1f274 745 }
AzureIoTClient 53:1e5a1ca1f274 746
AzureIoTClient 53:1e5a1ca1f274 747 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 748 }
AzureIoTClient 53:1e5a1ca1f274 749 }
AzureIoTClient 52:1cc3c6d07cad 750
AzureIoTClient 52:1cc3c6d07cad 751 return result;
AzureIoTClient 52:1cc3c6d07cad 752 }
AzureIoTClient 52:1cc3c6d07cad 753
AzureIoTClient 16:deba40344375 754 IOTHUB_CLIENT_RESULT IoTHubClient_GetLastMessageReceiveTime(IOTHUB_CLIENT_HANDLE iotHubClientHandle, time_t* lastMessageReceiveTime)
AzureIoTClient 16:deba40344375 755 {
AzureIoTClient 16:deba40344375 756 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 757
AzureIoTClient 16:deba40344375 758 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 759 {
AzureIoTClient 16:deba40344375 760 /* Codes_SRS_IOTHUBCLIENT_01_020: [If iotHubClientHandle is NULL, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 761 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 762 LogError("NULL iothubClientHandle");
AzureIoTClient 16:deba40344375 763 }
AzureIoTClient 16:deba40344375 764 else
AzureIoTClient 16:deba40344375 765 {
AzureIoTClient 16:deba40344375 766 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 767
AzureIoTClient 16:deba40344375 768 /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 769 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 770 {
AzureIoTClient 16:deba40344375 771 /* Codes_SRS_IOTHUBCLIENT_01_036: [If acquiring the lock fails, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 772 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 39:2719651a5bee 773 LogError("Could not acquire lock");
AzureIoTClient 16:deba40344375 774 }
AzureIoTClient 16:deba40344375 775 else
AzureIoTClient 16:deba40344375 776 {
AzureIoTClient 16:deba40344375 777 /* 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 778 /* Codes_SRS_IOTHUBCLIENT_01_021: [Otherwise, IoTHubClient_GetLastMessageReceiveTime shall return the result of IoTHubClient_LL_GetLastMessageReceiveTime.] */
AzureIoTClient 16:deba40344375 779 result = IoTHubClient_LL_GetLastMessageReceiveTime(iotHubClientInstance->IoTHubClientLLHandle, lastMessageReceiveTime);
AzureIoTClient 16:deba40344375 780
AzureIoTClient 16:deba40344375 781 /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 53:1e5a1ca1f274 782 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 783 }
AzureIoTClient 16:deba40344375 784 }
AzureIoTClient 16:deba40344375 785
AzureIoTClient 16:deba40344375 786 return result;
AzureIoTClient 16:deba40344375 787 }
AzureIoTClient 16:deba40344375 788
AzureIoTClient 16:deba40344375 789 IOTHUB_CLIENT_RESULT IoTHubClient_SetOption(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const char* optionName, const void* value)
AzureIoTClient 16:deba40344375 790 {
AzureIoTClient 16:deba40344375 791 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 792 /*Codes_SRS_IOTHUBCLIENT_02_034: [If parameter iotHubClientHandle is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 42:448eecc3676e 793 /*Codes_SRS_IOTHUBCLIENT_02_035: [ If parameter optionName is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 794 /*Codes_SRS_IOTHUBCLIENT_02_036: [ If parameter value is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 16:deba40344375 795 if (
AzureIoTClient 16:deba40344375 796 (iotHubClientHandle == NULL) ||
AzureIoTClient 16:deba40344375 797 (optionName == NULL) ||
AzureIoTClient 16:deba40344375 798 (value == NULL)
AzureIoTClient 16:deba40344375 799 )
AzureIoTClient 16:deba40344375 800 {
AzureIoTClient 16:deba40344375 801 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 802 LogError("invalid arg (NULL)");
AzureIoTClient 16:deba40344375 803 }
AzureIoTClient 16:deba40344375 804 else
AzureIoTClient 16:deba40344375 805 {
AzureIoTClient 16:deba40344375 806 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 807
AzureIoTClient 40:1a94db9139ea 808 /* Codes_SRS_IOTHUBCLIENT_01_041: [ IoTHubClient_SetOption shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 40:1a94db9139ea 809 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 40:1a94db9139ea 810 {
AzureIoTClient 40:1a94db9139ea 811 /* Codes_SRS_IOTHUBCLIENT_01_042: [ If acquiring the lock fails, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 40:1a94db9139ea 812 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 40:1a94db9139ea 813 LogError("Could not acquire lock");
AzureIoTClient 40:1a94db9139ea 814 }
AzureIoTClient 40:1a94db9139ea 815 else
AzureIoTClient 16:deba40344375 816 {
AzureIoTClient 40:1a94db9139ea 817 /*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 818 result = IoTHubClient_LL_SetOption(iotHubClientInstance->IoTHubClientLLHandle, optionName, value);
AzureIoTClient 40:1a94db9139ea 819 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 40:1a94db9139ea 820 {
AzureIoTClient 40:1a94db9139ea 821 LogError("IoTHubClient_LL_SetOption failed");
AzureIoTClient 40:1a94db9139ea 822 }
AzureIoTClient 40:1a94db9139ea 823
AzureIoTClient 53:1e5a1ca1f274 824 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 825 }
AzureIoTClient 53:1e5a1ca1f274 826 }
AzureIoTClient 53:1e5a1ca1f274 827 return result;
AzureIoTClient 53:1e5a1ca1f274 828 }
AzureIoTClient 53:1e5a1ca1f274 829
AzureIoTClient 53:1e5a1ca1f274 830 IOTHUB_CLIENT_RESULT IoTHubClient_SetDeviceTwinCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 831 {
AzureIoTClient 53:1e5a1ca1f274 832 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 833
AzureIoTClient 53:1e5a1ca1f274 834 /*Codes_SRS_IOTHUBCLIENT_10_001: [** `IoTHubClient_SetDeviceTwinCallback` shall fail and return `IOTHUB_CLIENT_INVALID_ARG` if parameter `iotHubClientHandle` is `NULL`. ]*/
AzureIoTClient 53:1e5a1ca1f274 835 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 836 {
AzureIoTClient 53:1e5a1ca1f274 837 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 838 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 839 }
AzureIoTClient 53:1e5a1ca1f274 840 else
AzureIoTClient 53:1e5a1ca1f274 841 {
AzureIoTClient 53:1e5a1ca1f274 842 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 843
AzureIoTClient 53:1e5a1ca1f274 844 /*Codes_SRS_IOTHUBCLIENT_10_020: [** `IoTHubClient_SetDeviceTwinCallback` shall be made thread - safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 53:1e5a1ca1f274 845 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 846 {
AzureIoTClient 53:1e5a1ca1f274 847 /*Codes_SRS_IOTHUBCLIENT_10_002: [** If acquiring the lock fails, `IoTHubClient_SetDeviceTwinCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 848 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 849 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 850 }
AzureIoTClient 53:1e5a1ca1f274 851 else
AzureIoTClient 53:1e5a1ca1f274 852 {
AzureIoTClient 53:1e5a1ca1f274 853 /*Codes_SRS_IOTHUBCLIENT_10_003: [** If the transport connection is shared, the thread shall be started by calling `IoTHubTransport_StartWorkerThread`. ]*/
AzureIoTClient 53:1e5a1ca1f274 854 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 855 {
AzureIoTClient 53:1e5a1ca1f274 856 /*Codes_SRS_IOTHUBCLIENT_10_004: [** If starting the thread fails, `IoTHubClient_SetDeviceTwinCallback` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 857 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 858 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 859 }
AzureIoTClient 53:1e5a1ca1f274 860 else
AzureIoTClient 53:1e5a1ca1f274 861 {
AzureIoTClient 53:1e5a1ca1f274 862 /*Codes_SRS_IOTHUBCLIENT_10_005: [** `IoTHubClient_LL_SetDeviceTwinCallback` shall call `IoTHubClient_LL_SetDeviceTwinCallback`, while passing the `IoTHubClient_LL handle` created by `IoTHubClient_LL_Create` along with the parameters `reportedStateCallback` and `userContextCallback`. ]*/
AzureIoTClient 53:1e5a1ca1f274 863 result = IoTHubClient_LL_SetDeviceTwinCallback(iotHubClientInstance->IoTHubClientLLHandle, deviceTwinCallback, userContextCallback);
AzureIoTClient 53:1e5a1ca1f274 864 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 865 {
AzureIoTClient 53:1e5a1ca1f274 866 LogError("IoTHubClient_LL_SetDeviceTwinCallback failed");
AzureIoTClient 53:1e5a1ca1f274 867 }
AzureIoTClient 53:1e5a1ca1f274 868 }
AzureIoTClient 53:1e5a1ca1f274 869
AzureIoTClient 53:1e5a1ca1f274 870 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 871 }
AzureIoTClient 16:deba40344375 872 }
AzureIoTClient 16:deba40344375 873 return result;
AzureIoTClient 16:deba40344375 874 }
AzureIoTClient 42:448eecc3676e 875
AzureIoTClient 53:1e5a1ca1f274 876 IOTHUB_CLIENT_RESULT IoTHubClient_SendReportedState(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const unsigned char* reportedState, size_t size, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK reportedStateCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 877 {
AzureIoTClient 53:1e5a1ca1f274 878 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 879
AzureIoTClient 53:1e5a1ca1f274 880 /*Codes_SRS_IOTHUBCLIENT_10_013: [** If `iotHubClientHandle` is `NULL`, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 53:1e5a1ca1f274 881 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 882 {
AzureIoTClient 53:1e5a1ca1f274 883 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 884 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 885 }
AzureIoTClient 53:1e5a1ca1f274 886 else
AzureIoTClient 53:1e5a1ca1f274 887 {
AzureIoTClient 53:1e5a1ca1f274 888 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 889
AzureIoTClient 53:1e5a1ca1f274 890 /*Codes_SRS_IOTHUBCLIENT_10_021: [** `IoTHubClient_SendReportedState` shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 53:1e5a1ca1f274 891 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 892 {
AzureIoTClient 53:1e5a1ca1f274 893 /*Codes_SRS_IOTHUBCLIENT_10_014: [** If acquiring the lock fails, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 894 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 895 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 896 }
AzureIoTClient 53:1e5a1ca1f274 897 else
AzureIoTClient 53:1e5a1ca1f274 898 {
AzureIoTClient 53:1e5a1ca1f274 899 /*Codes_SRS_IOTHUBCLIENT_10_015: [** If the transport connection is shared, the thread shall be started by calling `IoTHubTransport_StartWorkerThread`. ]*/
AzureIoTClient 53:1e5a1ca1f274 900 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 901 {
AzureIoTClient 53:1e5a1ca1f274 902 /*Codes_SRS_IOTHUBCLIENT_10_016: [** If starting the thread fails, `IoTHubClient_SendReportedState` shall return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 53:1e5a1ca1f274 903 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 904 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 905 }
AzureIoTClient 53:1e5a1ca1f274 906 else
AzureIoTClient 53:1e5a1ca1f274 907 {
AzureIoTClient 53:1e5a1ca1f274 908 /*Codes_SRS_IOTHUBCLIENT_10_017: [** `IoTHubClient_SendReportedState` shall call `IoTHubClient_LL_SendReportedState`, while passing the `IoTHubClient_LL handle` created by `IoTHubClient_LL_Create` along with the parameters `reportedState`, `size`, `reportedStateCallback`, and `userContextCallback`. ]*/
AzureIoTClient 53:1e5a1ca1f274 909 /*Codes_SRS_IOTHUBCLIENT_10_018: [** When `IoTHubClient_LL_SendReportedState` is called, `IoTHubClient_SendReportedState` shall return the result of `IoTHubClient_LL_SendReportedState`. **]*/
AzureIoTClient 53:1e5a1ca1f274 910 result = IoTHubClient_LL_SendReportedState(iotHubClientInstance->IoTHubClientLLHandle, reportedState, size, reportedStateCallback, userContextCallback);
AzureIoTClient 53:1e5a1ca1f274 911 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 912 {
AzureIoTClient 53:1e5a1ca1f274 913 LogError("IoTHubClient_LL_SendReportedState failed");
AzureIoTClient 53:1e5a1ca1f274 914 }
AzureIoTClient 53:1e5a1ca1f274 915 }
AzureIoTClient 53:1e5a1ca1f274 916
AzureIoTClient 53:1e5a1ca1f274 917 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 918 }
AzureIoTClient 53:1e5a1ca1f274 919 }
AzureIoTClient 53:1e5a1ca1f274 920 return result;
AzureIoTClient 53:1e5a1ca1f274 921 }
AzureIoTClient 53:1e5a1ca1f274 922
AzureIoTClient 53:1e5a1ca1f274 923 IOTHUB_CLIENT_RESULT IoTHubClient_SetDeviceMethodCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC deviceMethodCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 924 {
AzureIoTClient 53:1e5a1ca1f274 925 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 926
AzureIoTClient 53:1e5a1ca1f274 927 /*Codes_SRS_IOTHUBCLIENT_12_012: [ If iotHubClientHandle is NULL, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 53:1e5a1ca1f274 928 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 929 {
AzureIoTClient 53:1e5a1ca1f274 930 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 931 LogError("invalid arg (NULL)");
AzureIoTClient 53:1e5a1ca1f274 932 }
AzureIoTClient 53:1e5a1ca1f274 933 else
AzureIoTClient 53:1e5a1ca1f274 934 {
AzureIoTClient 53:1e5a1ca1f274 935 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 936
AzureIoTClient 53:1e5a1ca1f274 937 /*Codes_SRS_IOTHUBCLIENT_12_018: [ IoTHubClient_SetDeviceMethodCallback shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/
AzureIoTClient 53:1e5a1ca1f274 938 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 53:1e5a1ca1f274 939 {
AzureIoTClient 53:1e5a1ca1f274 940 /*Codes_SRS_IOTHUBCLIENT_12_013: [ If acquiring the lock fails, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 53:1e5a1ca1f274 941 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 942 LogError("Could not acquire lock");
AzureIoTClient 53:1e5a1ca1f274 943 }
AzureIoTClient 53:1e5a1ca1f274 944 else
AzureIoTClient 53:1e5a1ca1f274 945 {
AzureIoTClient 53:1e5a1ca1f274 946 /*Codes_SRS_IOTHUBCLIENT_12_014: [ If the transport handle is null and the worker thread is not initialized, the thread shall be started by calling IoTHubTransport_StartWorkerThread. ]*/
AzureIoTClient 53:1e5a1ca1f274 947 if ((result = StartWorkerThreadIfNeeded(iotHubClientInstance)) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 948 {
AzureIoTClient 53:1e5a1ca1f274 949 /*Codes_SRS_IOTHUBCLIENT_12_015: [ If starting the thread fails, IoTHubClient_SetDeviceMethodCallback shall return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 53:1e5a1ca1f274 950 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 951 LogError("Could not start worker thread");
AzureIoTClient 53:1e5a1ca1f274 952 }
AzureIoTClient 53:1e5a1ca1f274 953 else
AzureIoTClient 53:1e5a1ca1f274 954 {
AzureIoTClient 53:1e5a1ca1f274 955 /*Codes_SRS_IOTHUBCLIENT_12_016: [ IoTHubClient_SetDeviceMethodCallback shall call IoTHubClient_LL_SetDeviceMethodCallback, while passing the IoTHubClient_LL_handle created by IoTHubClient_LL_Create along with the parameters deviceMethodCallback and userContextCallback. ]*/
AzureIoTClient 53:1e5a1ca1f274 956 /*Codes_SRS_IOTHUBCLIENT_12_017: [ When IoTHubClient_LL_SetDeviceMethodCallback is called, IoTHubClient_SetDeviceMethodCallback shall return the result of IoTHubClient_LL_SetDeviceMethodCallback. ]*/
AzureIoTClient 53:1e5a1ca1f274 957 result = IoTHubClient_LL_SetDeviceMethodCallback(iotHubClientInstance->IoTHubClientLLHandle, deviceMethodCallback, userContextCallback);
AzureIoTClient 53:1e5a1ca1f274 958 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 959 {
AzureIoTClient 53:1e5a1ca1f274 960 LogError("IoTHubClient_LL_SetDeviceMethodCallback failed");
AzureIoTClient 53:1e5a1ca1f274 961 }
AzureIoTClient 53:1e5a1ca1f274 962 }
AzureIoTClient 53:1e5a1ca1f274 963
AzureIoTClient 53:1e5a1ca1f274 964 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 53:1e5a1ca1f274 965 }
AzureIoTClient 53:1e5a1ca1f274 966 }
AzureIoTClient 53:1e5a1ca1f274 967 return result;
AzureIoTClient 53:1e5a1ca1f274 968 }
AzureIoTClient 53:1e5a1ca1f274 969
AzureIoTClient 53:1e5a1ca1f274 970
AzureIoTClient 44:33dd78697616 971 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 972 static int uploadingThread(void *data)
AzureIoTClient 42:448eecc3676e 973 {
AzureIoTClient 42:448eecc3676e 974 UPLOADTOBLOB_SAVED_DATA* savedData = (UPLOADTOBLOB_SAVED_DATA*)data;
AzureIoTClient 42:448eecc3676e 975
AzureIoTClient 42:448eecc3676e 976 /*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 977 /*not having it protected means multiple simultaneous uploads can happen*/
AzureIoTClient 42:448eecc3676e 978 /*Codes_SRS_IOTHUBCLIENT_02_054: [ The thread shall call IoTHubClient_LL_UploadToBlob passing the information packed in the structure. ]*/
AzureIoTClient 42:448eecc3676e 979 if (IoTHubClient_LL_UploadToBlob(savedData->iotHubClientHandle->IoTHubClientLLHandle, savedData->destinationFileName, savedData->source, savedData->size) != IOTHUB_CLIENT_OK)
AzureIoTClient 42:448eecc3676e 980 {
AzureIoTClient 42:448eecc3676e 981 LogError("unable to IoTHubClient_LL_UploadToBlob");
AzureIoTClient 42:448eecc3676e 982 /*call the callback*/
AzureIoTClient 42:448eecc3676e 983 if (savedData->iotHubClientFileUploadCallback != NULL)
AzureIoTClient 42:448eecc3676e 984 {
AzureIoTClient 42:448eecc3676e 985 /*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 986 savedData->iotHubClientFileUploadCallback(FILE_UPLOAD_ERROR, savedData->context);
AzureIoTClient 42:448eecc3676e 987 }
AzureIoTClient 42:448eecc3676e 988 }
AzureIoTClient 42:448eecc3676e 989 else
AzureIoTClient 42:448eecc3676e 990 {
AzureIoTClient 42:448eecc3676e 991 if (savedData->iotHubClientFileUploadCallback != NULL)
AzureIoTClient 42:448eecc3676e 992 {
AzureIoTClient 42:448eecc3676e 993 /*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 994 savedData->iotHubClientFileUploadCallback(FILE_UPLOAD_OK, savedData->context);
AzureIoTClient 42:448eecc3676e 995 }
AzureIoTClient 42:448eecc3676e 996 }
AzureIoTClient 42:448eecc3676e 997
AzureIoTClient 42:448eecc3676e 998 /*Codes_SRS_IOTHUBCLIENT_02_071: [ The thread shall mark itself as disposable. ]*/
AzureIoTClient 42:448eecc3676e 999 if (Lock(savedData->lockGarbage) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 1000 {
AzureIoTClient 42:448eecc3676e 1001 LogError("unable to Lock - trying anyway");
AzureIoTClient 42:448eecc3676e 1002 savedData->canBeGarbageCollected = 1;
AzureIoTClient 42:448eecc3676e 1003 }
AzureIoTClient 42:448eecc3676e 1004 else
AzureIoTClient 42:448eecc3676e 1005 {
AzureIoTClient 42:448eecc3676e 1006 savedData->canBeGarbageCollected = 1;
AzureIoTClient 42:448eecc3676e 1007
AzureIoTClient 42:448eecc3676e 1008 if (Unlock(savedData->lockGarbage) != LOCK_OK)
AzureIoTClient 42:448eecc3676e 1009 {
AzureIoTClient 42:448eecc3676e 1010 LogError("unable to Unlock after locking");
AzureIoTClient 42:448eecc3676e 1011 }
AzureIoTClient 42:448eecc3676e 1012 }
AzureIoTClient 42:448eecc3676e 1013 return 0;
AzureIoTClient 42:448eecc3676e 1014 }
AzureIoTClient 43:038d8511e817 1015 #endif
AzureIoTClient 42:448eecc3676e 1016
AzureIoTClient 44:33dd78697616 1017 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 1018 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 1019 {
AzureIoTClient 42:448eecc3676e 1020 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1021 /*Codes_SRS_IOTHUBCLIENT_02_047: [ If iotHubClientHandle is NULL then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1022 /*Codes_SRS_IOTHUBCLIENT_02_048: [ If destinationFileName is NULL then IoTHubClient_UploadToBlobAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1023 /*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 1024 if (
AzureIoTClient 42:448eecc3676e 1025 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 1026 (destinationFileName == NULL) ||
AzureIoTClient 42:448eecc3676e 1027 ((source == NULL) && (size > 0))
AzureIoTClient 42:448eecc3676e 1028 )
AzureIoTClient 42:448eecc3676e 1029 {
AzureIoTClient 42:448eecc3676e 1030 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 1031 iotHubClientHandle,
AzureIoTClient 42:448eecc3676e 1032 destinationFileName,
AzureIoTClient 42:448eecc3676e 1033 source,
AzureIoTClient 42:448eecc3676e 1034 size,
AzureIoTClient 42:448eecc3676e 1035 iotHubClientFileUploadCallback,
AzureIoTClient 42:448eecc3676e 1036 context
AzureIoTClient 42:448eecc3676e 1037 );
AzureIoTClient 42:448eecc3676e 1038 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 1039 }
AzureIoTClient 42:448eecc3676e 1040 else
AzureIoTClient 42:448eecc3676e 1041 {
AzureIoTClient 42:448eecc3676e 1042 /*Codes_SRS_IOTHUBCLIENT_02_051: [IoTHubClient_UploadToBlobAsync shall copy the souce, size, iotHubClientFileUploadCallback, context into a structure.]*/
AzureIoTClient 42:448eecc3676e 1043 UPLOADTOBLOB_SAVED_DATA *savedData = (UPLOADTOBLOB_SAVED_DATA *)malloc(sizeof(UPLOADTOBLOB_SAVED_DATA));
AzureIoTClient 42:448eecc3676e 1044 if (savedData == NULL)
AzureIoTClient 42:448eecc3676e 1045 {
AzureIoTClient 42:448eecc3676e 1046 /*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 1047 LogError("unable to malloc - oom");
AzureIoTClient 42:448eecc3676e 1048 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1049 }
AzureIoTClient 42:448eecc3676e 1050 else
AzureIoTClient 42:448eecc3676e 1051 {
AzureIoTClient 42:448eecc3676e 1052 if (mallocAndStrcpy_s((char**)&savedData->destinationFileName, destinationFileName) != 0)
AzureIoTClient 42:448eecc3676e 1053 {
AzureIoTClient 42:448eecc3676e 1054 /*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 1055 LogError("unable to mallocAndStrcpy_s");
AzureIoTClient 42:448eecc3676e 1056 free(savedData);
AzureIoTClient 42:448eecc3676e 1057 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1058 }
AzureIoTClient 42:448eecc3676e 1059 else
AzureIoTClient 42:448eecc3676e 1060 {
AzureIoTClient 42:448eecc3676e 1061 savedData->size = size;
AzureIoTClient 42:448eecc3676e 1062 int sourceCloned;
AzureIoTClient 42:448eecc3676e 1063 if (size == 0)
AzureIoTClient 42:448eecc3676e 1064 {
AzureIoTClient 42:448eecc3676e 1065 savedData->source = NULL;
AzureIoTClient 42:448eecc3676e 1066 sourceCloned = 1;
AzureIoTClient 42:448eecc3676e 1067 }
AzureIoTClient 42:448eecc3676e 1068 else
AzureIoTClient 42:448eecc3676e 1069 {
AzureIoTClient 42:448eecc3676e 1070 savedData->source = (unsigned char*)malloc(size);
AzureIoTClient 42:448eecc3676e 1071 if (savedData->source == NULL)
AzureIoTClient 42:448eecc3676e 1072 {
AzureIoTClient 42:448eecc3676e 1073 /*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 1074 LogError("unable to malloc - oom");
AzureIoTClient 42:448eecc3676e 1075 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1076 free(savedData);
AzureIoTClient 42:448eecc3676e 1077 sourceCloned = 0;
AzureIoTClient 42:448eecc3676e 1078
AzureIoTClient 42:448eecc3676e 1079 }
AzureIoTClient 42:448eecc3676e 1080 else
AzureIoTClient 42:448eecc3676e 1081 {
AzureIoTClient 42:448eecc3676e 1082 sourceCloned = 1;
AzureIoTClient 42:448eecc3676e 1083 }
AzureIoTClient 42:448eecc3676e 1084 }
AzureIoTClient 42:448eecc3676e 1085
AzureIoTClient 42:448eecc3676e 1086 if (sourceCloned == 0)
AzureIoTClient 42:448eecc3676e 1087 {
AzureIoTClient 42:448eecc3676e 1088 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1089 }
AzureIoTClient 42:448eecc3676e 1090 else
AzureIoTClient 42:448eecc3676e 1091 {
AzureIoTClient 42:448eecc3676e 1092 savedData->iotHubClientFileUploadCallback = iotHubClientFileUploadCallback;
AzureIoTClient 42:448eecc3676e 1093 savedData->context = context;
AzureIoTClient 42:448eecc3676e 1094 memcpy(savedData->source, source, size);
AzureIoTClient 42:448eecc3676e 1095 IOTHUB_CLIENT_INSTANCE* iotHubClientHandleData = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 1096 if (Lock(iotHubClientHandleData->LockHandle) != LOCK_OK) /*locking because the next statement is changing blobThreadsToBeJoined*/
AzureIoTClient 42:448eecc3676e 1097 {
AzureIoTClient 42:448eecc3676e 1098 LogError("unable to lock");
AzureIoTClient 42:448eecc3676e 1099 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1100 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1101 free(savedData);
AzureIoTClient 42:448eecc3676e 1102 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1103 }
AzureIoTClient 42:448eecc3676e 1104 else
AzureIoTClient 42:448eecc3676e 1105 {
AzureIoTClient 42:448eecc3676e 1106 if ((result = StartWorkerThreadIfNeeded(iotHubClientHandleData)) != IOTHUB_CLIENT_OK)
AzureIoTClient 42:448eecc3676e 1107 {
AzureIoTClient 42:448eecc3676e 1108 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1109 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1110 free(savedData);
AzureIoTClient 42:448eecc3676e 1111 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1112 LogError("Could not start worker thread");
AzureIoTClient 42:448eecc3676e 1113 }
AzureIoTClient 42:448eecc3676e 1114 else
AzureIoTClient 42:448eecc3676e 1115 {
AzureIoTClient 42:448eecc3676e 1116 /*Codes_SRS_IOTHUBCLIENT_02_058: [ IoTHubClient_UploadToBlobAsync shall add the structure to the list of structures that need to be cleaned once file upload finishes. ]*/
AzureIoTClient 52:1cc3c6d07cad 1117 LIST_ITEM_HANDLE item = singlylinkedlist_add(iotHubClientHandleData->savedDataToBeCleaned, savedData);
AzureIoTClient 42:448eecc3676e 1118 if (item == NULL)
AzureIoTClient 42:448eecc3676e 1119 {
AzureIoTClient 52:1cc3c6d07cad 1120 LogError("unable to singlylinkedlist_add");
AzureIoTClient 42:448eecc3676e 1121 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1122 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1123 free(savedData);
AzureIoTClient 42:448eecc3676e 1124 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1125 }
AzureIoTClient 42:448eecc3676e 1126 else
AzureIoTClient 42:448eecc3676e 1127 {
AzureIoTClient 42:448eecc3676e 1128 savedData->iotHubClientHandle = iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 1129 savedData->canBeGarbageCollected = 0;
AzureIoTClient 42:448eecc3676e 1130 if ((savedData->lockGarbage = Lock_Init()) == NULL)
AzureIoTClient 42:448eecc3676e 1131 {
AzureIoTClient 52:1cc3c6d07cad 1132 (void)singlylinkedlist_remove(iotHubClientHandleData->savedDataToBeCleaned, item);
AzureIoTClient 42:448eecc3676e 1133 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1134 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1135 free(savedData);
AzureIoTClient 42:448eecc3676e 1136 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1137 LogError("unable to Lock_Init");
AzureIoTClient 42:448eecc3676e 1138 }
AzureIoTClient 42:448eecc3676e 1139 else
AzureIoTClient 42:448eecc3676e 1140 {
AzureIoTClient 42:448eecc3676e 1141 /*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 1142 if (ThreadAPI_Create(&savedData->uploadingThreadHandle, uploadingThread, savedData) != THREADAPI_OK)
AzureIoTClient 42:448eecc3676e 1143 {
AzureIoTClient 42:448eecc3676e 1144 /*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 1145 LogError("unablet to ThreadAPI_Create");
AzureIoTClient 42:448eecc3676e 1146 (void)Lock_Deinit(savedData->lockGarbage);
AzureIoTClient 52:1cc3c6d07cad 1147 (void)singlylinkedlist_remove(iotHubClientHandleData->savedDataToBeCleaned, item);
AzureIoTClient 42:448eecc3676e 1148 free(savedData->source);
AzureIoTClient 42:448eecc3676e 1149 free(savedData->destinationFileName);
AzureIoTClient 42:448eecc3676e 1150 free(savedData);
AzureIoTClient 42:448eecc3676e 1151 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 1152 }
AzureIoTClient 42:448eecc3676e 1153 else
AzureIoTClient 42:448eecc3676e 1154 {
AzureIoTClient 42:448eecc3676e 1155
AzureIoTClient 42:448eecc3676e 1156 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 1157 }
AzureIoTClient 42:448eecc3676e 1158 }
AzureIoTClient 42:448eecc3676e 1159 }
AzureIoTClient 42:448eecc3676e 1160 }
AzureIoTClient 53:1e5a1ca1f274 1161 (void)Unlock(iotHubClientHandleData->LockHandle);
AzureIoTClient 42:448eecc3676e 1162 }
AzureIoTClient 42:448eecc3676e 1163 }
AzureIoTClient 42:448eecc3676e 1164 }
AzureIoTClient 42:448eecc3676e 1165 }
AzureIoTClient 42:448eecc3676e 1166 }
AzureIoTClient 42:448eecc3676e 1167 return result;
AzureIoTClient 42:448eecc3676e 1168 }
AzureIoTClient 44:33dd78697616 1169 #endif /*DONT_USE_UPLOADTOBLOB*/