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:
Thu Oct 22 18:32:36 2015 -0700
Revision:
16:deba40344375
Child:
18:1e9adb15c645
v1.0.0-preview.4

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
AzureIoTClient 16:deba40344375 8 #include "gballoc.h"
AzureIoTClient 16:deba40344375 9
AzureIoTClient 16:deba40344375 10 #include <stdlib.h>
AzureIoTClient 16:deba40344375 11 #include <signal.h>
AzureIoTClient 16:deba40344375 12 #include "crt_abstractions.h"
AzureIoTClient 16:deba40344375 13 #include "iothub_client.h"
AzureIoTClient 16:deba40344375 14 #include "iothub_client_ll.h"
AzureIoTClient 16:deba40344375 15 #include "threadapi.h"
AzureIoTClient 16:deba40344375 16 #include "lock.h"
AzureIoTClient 16:deba40344375 17 #include "iot_logging.h"
AzureIoTClient 16:deba40344375 18
AzureIoTClient 16:deba40344375 19 typedef struct IOTHUB_CLIENT_INSTANCE_TAG
AzureIoTClient 16:deba40344375 20 {
AzureIoTClient 16:deba40344375 21 IOTHUB_CLIENT_LL_HANDLE IoTHubClientLLHandle;
AzureIoTClient 16:deba40344375 22 THREAD_HANDLE ThreadHandle;
AzureIoTClient 16:deba40344375 23 LOCK_HANDLE LockHandle;
AzureIoTClient 16:deba40344375 24 sig_atomic_t StopThread;
AzureIoTClient 16:deba40344375 25 } IOTHUB_CLIENT_INSTANCE;
AzureIoTClient 16:deba40344375 26
AzureIoTClient 16:deba40344375 27 static int ScheduleWork_Thread(void* threadArgument)
AzureIoTClient 16:deba40344375 28 {
AzureIoTClient 16:deba40344375 29 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)threadArgument;
AzureIoTClient 16:deba40344375 30
AzureIoTClient 16:deba40344375 31 /* Codes_SRS_IOTHUBCLIENT_01_038: [The thread shall exit when IoTHubClient_Destroy is called.] */
AzureIoTClient 16:deba40344375 32 while (!iotHubClientInstance->StopThread)
AzureIoTClient 16:deba40344375 33 {
AzureIoTClient 16:deba40344375 34 /* Codes_SRS_IOTHUBCLIENT_01_039: [All calls to IoTHubClient_LL_DoWork shall be protected by the lock created in IotHubClient_Create.] */
AzureIoTClient 16:deba40344375 35 /* Codes_SRS_IOTHUBCLIENT_01_040: [If acquiring the lock fails, IoTHubClient_LL_DoWork shall not be called.] */
AzureIoTClient 16:deba40344375 36 if (Lock(iotHubClientInstance->LockHandle) == LOCK_OK)
AzureIoTClient 16:deba40344375 37 {
AzureIoTClient 16:deba40344375 38 /* Codes_SRS_IOTHUBCLIENT_01_037: [The thread created by IoTHubClient_SendEvent or IoTHubClient_SetMessageCallback shall call IoTHubClient_LL_DoWork every 1 ms.] */
AzureIoTClient 16:deba40344375 39 IoTHubClient_LL_DoWork(iotHubClientInstance->IoTHubClientLLHandle);
AzureIoTClient 16:deba40344375 40 /* Codes_SRS_IOTHUBCLIENT_01_039: [All calls to IoTHubClient_LL_DoWork shall be protected by the lock created in IotHubClient_Create.] */
AzureIoTClient 16:deba40344375 41 Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 42 }
AzureIoTClient 16:deba40344375 43
AzureIoTClient 16:deba40344375 44 ThreadAPI_Sleep(1);
AzureIoTClient 16:deba40344375 45 }
AzureIoTClient 16:deba40344375 46
AzureIoTClient 16:deba40344375 47 return 0;
AzureIoTClient 16:deba40344375 48 }
AzureIoTClient 16:deba40344375 49
AzureIoTClient 16:deba40344375 50 static void StartWorkerThreadIfNeeded(IOTHUB_CLIENT_INSTANCE* iotHubClientInstance)
AzureIoTClient 16:deba40344375 51 {
AzureIoTClient 16:deba40344375 52 if (iotHubClientInstance->ThreadHandle == NULL)
AzureIoTClient 16:deba40344375 53 {
AzureIoTClient 16:deba40344375 54 iotHubClientInstance->StopThread = 0;
AzureIoTClient 16:deba40344375 55 if (ThreadAPI_Create(&iotHubClientInstance->ThreadHandle, ScheduleWork_Thread, iotHubClientInstance) != THREADAPI_OK)
AzureIoTClient 16:deba40344375 56 {
AzureIoTClient 16:deba40344375 57 iotHubClientInstance->ThreadHandle = NULL;
AzureIoTClient 16:deba40344375 58 }
AzureIoTClient 16:deba40344375 59 }
AzureIoTClient 16:deba40344375 60 }
AzureIoTClient 16:deba40344375 61
AzureIoTClient 16:deba40344375 62 IOTHUB_CLIENT_HANDLE IoTHubClient_CreateFromConnectionString(const char* connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol)
AzureIoTClient 16:deba40344375 63 {
AzureIoTClient 16:deba40344375 64 IOTHUB_CLIENT_INSTANCE* result = NULL;
AzureIoTClient 16:deba40344375 65
AzureIoTClient 16:deba40344375 66 /* Codes_SRS_IOTHUBCLIENT_12_003: [IoTHubClient_CreateFromConnectionString shall verify the input parameter and if it is NULL then return NULL] */
AzureIoTClient 16:deba40344375 67 if (connectionString == NULL)
AzureIoTClient 16:deba40344375 68 {
AzureIoTClient 16:deba40344375 69 LogError("Input parameter is NULL: connectionString\r\n");
AzureIoTClient 16:deba40344375 70 }
AzureIoTClient 16:deba40344375 71 else if (protocol == NULL)
AzureIoTClient 16:deba40344375 72 {
AzureIoTClient 16:deba40344375 73 LogError("Input parameter is NULL: protocol\r\n");
AzureIoTClient 16:deba40344375 74 }
AzureIoTClient 16:deba40344375 75 else
AzureIoTClient 16:deba40344375 76 {
AzureIoTClient 16:deba40344375 77 /* Codes_SRS_IOTHUBCLIENT_12_004: [IoTHubClient_CreateFromConnectionString shall allocate a new IoTHubClient instance.] */
AzureIoTClient 16:deba40344375 78 result = malloc(sizeof(IOTHUB_CLIENT_INSTANCE));
AzureIoTClient 16:deba40344375 79
AzureIoTClient 16:deba40344375 80 /* Codes_SRS_IOTHUBCLIENT_12_011: [If the allocation failed, IoTHubClient_CreateFromConnectionString returns NULL] */
AzureIoTClient 16:deba40344375 81 if (result == NULL)
AzureIoTClient 16:deba40344375 82 {
AzureIoTClient 16:deba40344375 83 LogError("Malloc failed\r\n");
AzureIoTClient 16:deba40344375 84 }
AzureIoTClient 16:deba40344375 85 else
AzureIoTClient 16:deba40344375 86 {
AzureIoTClient 16:deba40344375 87 /* Codes_SRS_IOTHUBCLIENT_12_005: [IoTHubClient_CreateFromConnectionString shall create a lock object to be used later for serializing IoTHubClient calls] */
AzureIoTClient 16:deba40344375 88 result->LockHandle = Lock_Init();
AzureIoTClient 16:deba40344375 89 if (result->LockHandle == NULL)
AzureIoTClient 16:deba40344375 90 {
AzureIoTClient 16:deba40344375 91 /* Codes_SRS_IOTHUBCLIENT_12_009: [If lock creation failed, IoTHubClient_CreateFromConnectionString shall do clean up and return NULL] */
AzureIoTClient 16:deba40344375 92 free(result);
AzureIoTClient 16:deba40344375 93 result = NULL;
AzureIoTClient 16:deba40344375 94 LogError("Lock_Init failed\r\n");
AzureIoTClient 16:deba40344375 95 }
AzureIoTClient 16:deba40344375 96 else
AzureIoTClient 16:deba40344375 97 {
AzureIoTClient 16:deba40344375 98 /* Codes_SRS_IOTHUBCLIENT_12_006: [IoTHubClient_CreateFromConnectionString shall instantiate a new IoTHubClient_LL instance by calling IoTHubClient_LL_CreateFromConnectionString and passing the connectionString] */
AzureIoTClient 16:deba40344375 99 result->IoTHubClientLLHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, protocol);
AzureIoTClient 16:deba40344375 100 if (result->IoTHubClientLLHandle == NULL)
AzureIoTClient 16:deba40344375 101 {
AzureIoTClient 16:deba40344375 102 /* Codes_SRS_IOTHUBCLIENT_12_010: [If IoTHubClient_LL_CreateFromConnectionString fails then IoTHubClient_CreateFromConnectionString shall do clean - up and return NULL] */
AzureIoTClient 16:deba40344375 103 Lock_Deinit(result->LockHandle);
AzureIoTClient 16:deba40344375 104 free(result);
AzureIoTClient 16:deba40344375 105 result = NULL;
AzureIoTClient 16:deba40344375 106 LogError("IoTHubClient_LL_CreateFromConnectionString failed\r\n");
AzureIoTClient 16:deba40344375 107 }
AzureIoTClient 16:deba40344375 108 else
AzureIoTClient 16:deba40344375 109 {
AzureIoTClient 16:deba40344375 110 result->ThreadHandle = NULL;
AzureIoTClient 16:deba40344375 111 }
AzureIoTClient 16:deba40344375 112 }
AzureIoTClient 16:deba40344375 113 }
AzureIoTClient 16:deba40344375 114 }
AzureIoTClient 16:deba40344375 115 return result;
AzureIoTClient 16:deba40344375 116 }
AzureIoTClient 16:deba40344375 117
AzureIoTClient 16:deba40344375 118
AzureIoTClient 16:deba40344375 119 IOTHUB_CLIENT_HANDLE IoTHubClient_Create(const IOTHUB_CLIENT_CONFIG* config)
AzureIoTClient 16:deba40344375 120 {
AzureIoTClient 16:deba40344375 121 /* Codes_SRS_IOTHUBCLIENT_01_001: [IoTHubClient_Create shall allocate a new IoTHubClient instance and return a non-NULL handle to it.] */
AzureIoTClient 16:deba40344375 122 IOTHUB_CLIENT_INSTANCE* result = (IOTHUB_CLIENT_INSTANCE*)malloc(sizeof(IOTHUB_CLIENT_INSTANCE));
AzureIoTClient 16:deba40344375 123
AzureIoTClient 16:deba40344375 124 /* Codes_SRS_IOTHUBCLIENT_01_004: [If allocating memory for the new IoTHubClient instance fails, then IoTHubClient_Create shall return NULL.] */
AzureIoTClient 16:deba40344375 125 if (result != NULL)
AzureIoTClient 16:deba40344375 126 {
AzureIoTClient 16:deba40344375 127 result->ThreadHandle = NULL;
AzureIoTClient 16:deba40344375 128
AzureIoTClient 16:deba40344375 129 /* Codes_SRS_IOTHUBCLIENT_01_029: [IoTHubClient_Create shall create a lock object to be used later for serializing IoTHubClient calls.] */
AzureIoTClient 16:deba40344375 130 result->LockHandle = Lock_Init();
AzureIoTClient 16:deba40344375 131 if (result->LockHandle == NULL)
AzureIoTClient 16:deba40344375 132 {
AzureIoTClient 16:deba40344375 133 /* Codes_SRS_IOTHUBCLIENT_01_030: [If creating the lock fails, then IoTHubClient_Create shall return NULL.] */
AzureIoTClient 16:deba40344375 134 /* Codes_SRS_IOTHUBCLIENT_01_031: [If IoTHubClient_Create fails, all resources allocated by it shall be freed.] */
AzureIoTClient 16:deba40344375 135 free(result);
AzureIoTClient 16:deba40344375 136 result = NULL;
AzureIoTClient 16:deba40344375 137 }
AzureIoTClient 16:deba40344375 138 else
AzureIoTClient 16:deba40344375 139 {
AzureIoTClient 16:deba40344375 140 /* 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 16:deba40344375 141 result->IoTHubClientLLHandle = IoTHubClient_LL_Create(config);
AzureIoTClient 16:deba40344375 142 if (result->IoTHubClientLLHandle == NULL)
AzureIoTClient 16:deba40344375 143 {
AzureIoTClient 16:deba40344375 144 /* Codes_SRS_IOTHUBCLIENT_01_003: [If IoTHubClient_LL_Create fails, then IoTHubClient_Create shall return NULL.] */
AzureIoTClient 16:deba40344375 145 /* Codes_SRS_IOTHUBCLIENT_01_031: [If IoTHubClient_Create fails, all resources allocated by it shall be freed.] */
AzureIoTClient 16:deba40344375 146 Lock_Deinit(result->LockHandle);
AzureIoTClient 16:deba40344375 147 free(result);
AzureIoTClient 16:deba40344375 148 result = NULL;
AzureIoTClient 16:deba40344375 149 }
AzureIoTClient 16:deba40344375 150 }
AzureIoTClient 16:deba40344375 151 }
AzureIoTClient 16:deba40344375 152
AzureIoTClient 16:deba40344375 153 return result;
AzureIoTClient 16:deba40344375 154 }
AzureIoTClient 16:deba40344375 155
AzureIoTClient 16:deba40344375 156 /* Codes_SRS_IOTHUBCLIENT_01_005: [IoTHubClient_Destroy shall free all resources associated with the iotHubClientHandle instance.] */
AzureIoTClient 16:deba40344375 157 void IoTHubClient_Destroy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 16:deba40344375 158 {
AzureIoTClient 16:deba40344375 159 /* Codes_SRS_IOTHUBCLIENT_01_008: [IoTHubClient_Destroy shall do nothing if parameter iotHubClientHandle is NULL.] */
AzureIoTClient 16:deba40344375 160 if (iotHubClientHandle != NULL)
AzureIoTClient 16:deba40344375 161 {
AzureIoTClient 16:deba40344375 162 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 163
AzureIoTClient 16:deba40344375 164 /* Codes_SRS_IOTHUBCLIENT_01_007: [The thread created as part of executing IoTHubClient_SendEventAsync or IoTHubClient_SetMessageCallback shall be joined.] */
AzureIoTClient 16:deba40344375 165 if (iotHubClientInstance->ThreadHandle != NULL)
AzureIoTClient 16:deba40344375 166 {
AzureIoTClient 16:deba40344375 167 int res;
AzureIoTClient 16:deba40344375 168 iotHubClientInstance->StopThread = 1;
AzureIoTClient 16:deba40344375 169 if (ThreadAPI_Join(iotHubClientInstance->ThreadHandle, &res) != THREADAPI_OK)
AzureIoTClient 16:deba40344375 170 {
AzureIoTClient 16:deba40344375 171 LogError("ThreadAPI_Join failed\r\n");
AzureIoTClient 16:deba40344375 172 }
AzureIoTClient 16:deba40344375 173 }
AzureIoTClient 16:deba40344375 174
AzureIoTClient 16:deba40344375 175 /* Codes_SRS_IOTHUBCLIENT_01_006: [That includes destroying the IoTHubClient_LL instance by calling IoTHubClient_LL_Destroy.] */
AzureIoTClient 16:deba40344375 176 IoTHubClient_LL_Destroy(iotHubClientInstance->IoTHubClientLLHandle);
AzureIoTClient 16:deba40344375 177
AzureIoTClient 16:deba40344375 178 /* Codes_SRS_IOTHUBCLIENT_01_032: [The lock allocated in IoTHubClient_Create shall be also freed.] */
AzureIoTClient 16:deba40344375 179 Lock_Deinit(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 180 free(iotHubClientInstance);
AzureIoTClient 16:deba40344375 181 }
AzureIoTClient 16:deba40344375 182 }
AzureIoTClient 16:deba40344375 183
AzureIoTClient 16:deba40344375 184 IOTHUB_CLIENT_RESULT IoTHubClient_SendEventAsync(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_MESSAGE_HANDLE eventMessageHandle, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK eventConfirmationCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 185 {
AzureIoTClient 16:deba40344375 186 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 187
AzureIoTClient 16:deba40344375 188 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 189 {
AzureIoTClient 16:deba40344375 190 /* Codes_SRS_IOTHUBCLIENT_01_011: [If iotHubClientHandle is NULL, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 191 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 16:deba40344375 192 LogError("NULL iothubClientHandle\r\n");
AzureIoTClient 16:deba40344375 193 }
AzureIoTClient 16:deba40344375 194 else
AzureIoTClient 16:deba40344375 195 {
AzureIoTClient 16:deba40344375 196 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 197
AzureIoTClient 16:deba40344375 198 /* Codes_SRS_IOTHUBCLIENT_01_025: [IoTHubClient_SendEventAsync shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 199 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 200 {
AzureIoTClient 16:deba40344375 201 /* Codes_SRS_IOTHUBCLIENT_01_026: [If acquiring the lock fails, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 202 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 16:deba40344375 203 LogError("Could not acquire lock\r\n");
AzureIoTClient 16:deba40344375 204 }
AzureIoTClient 16:deba40344375 205 else
AzureIoTClient 16:deba40344375 206 {
AzureIoTClient 16:deba40344375 207 /* Codes_SRS_IOTHUBCLIENT_01_009: [IoTHubClient_SendEventAsync shall start the worker thread if it was not previously started.] */
AzureIoTClient 16:deba40344375 208 StartWorkerThreadIfNeeded(iotHubClientInstance);
AzureIoTClient 16:deba40344375 209
AzureIoTClient 16:deba40344375 210 if (iotHubClientInstance->ThreadHandle == NULL)
AzureIoTClient 16:deba40344375 211 {
AzureIoTClient 16:deba40344375 212 /* Codes_SRS_IOTHUBCLIENT_01_010: [If starting the thread fails, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 213 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 16:deba40344375 214 LogError("Could not start worker thread\r\n");
AzureIoTClient 16:deba40344375 215 }
AzureIoTClient 16:deba40344375 216 else
AzureIoTClient 16:deba40344375 217 {
AzureIoTClient 16:deba40344375 218 /* 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 219 /* Codes_SRS_IOTHUBCLIENT_01_013: [When IoTHubClient_LL_SendEventAsync is called, IoTHubClient_SendEventAsync shall return the result of IoTHubClient_LL_SendEventAsync.] */
AzureIoTClient 16:deba40344375 220 result = IoTHubClient_LL_SendEventAsync(iotHubClientInstance->IoTHubClientLLHandle, eventMessageHandle, eventConfirmationCallback, userContextCallback);
AzureIoTClient 16:deba40344375 221 }
AzureIoTClient 16:deba40344375 222
AzureIoTClient 16:deba40344375 223 /* Codes_SRS_IOTHUBCLIENT_01_025: [IoTHubClient_SendEventAsync shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 224 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 225 }
AzureIoTClient 16:deba40344375 226 }
AzureIoTClient 16:deba40344375 227
AzureIoTClient 16:deba40344375 228 return result;
AzureIoTClient 16:deba40344375 229 }
AzureIoTClient 16:deba40344375 230
AzureIoTClient 16:deba40344375 231 IOTHUB_CLIENT_RESULT IoTHubClient_GetSendStatus(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 16:deba40344375 232 {
AzureIoTClient 16:deba40344375 233 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 234
AzureIoTClient 16:deba40344375 235 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 236 {
AzureIoTClient 16:deba40344375 237 /* Codes_SRS_IOTHUBCLIENT_01_023: [If iotHubClientHandle is NULL, IoTHubClient_ GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 238 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 16:deba40344375 239 LogError("NULL iothubClientHandle\r\n");
AzureIoTClient 16:deba40344375 240 }
AzureIoTClient 16:deba40344375 241 else
AzureIoTClient 16:deba40344375 242 {
AzureIoTClient 16:deba40344375 243 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 244
AzureIoTClient 16:deba40344375 245 /* Codes_SRS_IOTHUBCLIENT_01_033: [IoTHubClient_GetSendStatus shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 246 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 247 {
AzureIoTClient 16:deba40344375 248 /* Codes_SRS_IOTHUBCLIENT_01_034: [If acquiring the lock fails, IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 249 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 16:deba40344375 250 LogError("Could not acquire lock\r\n");
AzureIoTClient 16:deba40344375 251 }
AzureIoTClient 16:deba40344375 252 else
AzureIoTClient 16:deba40344375 253 {
AzureIoTClient 16:deba40344375 254 /* 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 255 /* Codes_SRS_IOTHUBCLIENT_01_024: [Otherwise, IoTHubClient_GetSendStatus shall return the result of IoTHubClient_LL_GetSendStatus.] */
AzureIoTClient 16:deba40344375 256 result = IoTHubClient_LL_GetSendStatus(iotHubClientInstance->IoTHubClientLLHandle, iotHubClientStatus);
AzureIoTClient 16:deba40344375 257
AzureIoTClient 16:deba40344375 258 /* Codes_SRS_IOTHUBCLIENT_01_033: [IoTHubClient_GetSendStatus shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 259 (void)Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 260 }
AzureIoTClient 16:deba40344375 261 }
AzureIoTClient 16:deba40344375 262
AzureIoTClient 16:deba40344375 263 return result;
AzureIoTClient 16:deba40344375 264 }
AzureIoTClient 16:deba40344375 265
AzureIoTClient 16:deba40344375 266 IOTHUB_CLIENT_RESULT IoTHubClient_SetMessageCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 267 {
AzureIoTClient 16:deba40344375 268 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 269
AzureIoTClient 16:deba40344375 270 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 271 {
AzureIoTClient 16:deba40344375 272 /* Codes_SRS_IOTHUBCLIENT_01_016: [If iotHubClientHandle is NULL, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 273 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 16:deba40344375 274 LogError("NULL iothubClientHandle\r\n");
AzureIoTClient 16:deba40344375 275 }
AzureIoTClient 16:deba40344375 276 else
AzureIoTClient 16:deba40344375 277 {
AzureIoTClient 16:deba40344375 278 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 279
AzureIoTClient 16:deba40344375 280 /* Codes_SRS_IOTHUBCLIENT_01_027: [IoTHubClient_SetMessageCallback shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 281 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 282 {
AzureIoTClient 16:deba40344375 283 /* Codes_SRS_IOTHUBCLIENT_01_028: [If acquiring the lock fails, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 284 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 16:deba40344375 285 LogError("Could not acquire lock\r\n");
AzureIoTClient 16:deba40344375 286 }
AzureIoTClient 16:deba40344375 287 else
AzureIoTClient 16:deba40344375 288 {
AzureIoTClient 16:deba40344375 289 /* Codes_SRS_IOTHUBCLIENT_01_014: [IoTHubClient_SetMessageCallback shall start the worker thread if it was not previously started.] */
AzureIoTClient 16:deba40344375 290 StartWorkerThreadIfNeeded(iotHubClientInstance);
AzureIoTClient 16:deba40344375 291
AzureIoTClient 16:deba40344375 292 if (iotHubClientInstance->ThreadHandle == NULL)
AzureIoTClient 16:deba40344375 293 {
AzureIoTClient 16:deba40344375 294 /* Codes_SRS_IOTHUBCLIENT_01_015: [If starting the thread fails, IoTHubClient_SetMessageCallback shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 295 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 16:deba40344375 296 LogError("Could not start worker thread\r\n");
AzureIoTClient 16:deba40344375 297 }
AzureIoTClient 16:deba40344375 298 else
AzureIoTClient 16:deba40344375 299 {
AzureIoTClient 16:deba40344375 300 /* 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 301 result = IoTHubClient_LL_SetMessageCallback(iotHubClientInstance->IoTHubClientLLHandle, messageCallback, userContextCallback);
AzureIoTClient 16:deba40344375 302 }
AzureIoTClient 16:deba40344375 303
AzureIoTClient 16:deba40344375 304 /* Codes_SRS_IOTHUBCLIENT_01_027: [IoTHubClient_SetMessageCallback shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 305 Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 306 }
AzureIoTClient 16:deba40344375 307 }
AzureIoTClient 16:deba40344375 308
AzureIoTClient 16:deba40344375 309 return result;
AzureIoTClient 16:deba40344375 310 }
AzureIoTClient 16:deba40344375 311
AzureIoTClient 16:deba40344375 312 IOTHUB_CLIENT_RESULT IoTHubClient_GetLastMessageReceiveTime(IOTHUB_CLIENT_HANDLE iotHubClientHandle, time_t* lastMessageReceiveTime)
AzureIoTClient 16:deba40344375 313 {
AzureIoTClient 16:deba40344375 314 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 315
AzureIoTClient 16:deba40344375 316 if (iotHubClientHandle == NULL)
AzureIoTClient 16:deba40344375 317 {
AzureIoTClient 16:deba40344375 318 /* Codes_SRS_IOTHUBCLIENT_01_020: [If iotHubClientHandle is NULL, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 319 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 16:deba40344375 320 LogError("NULL iothubClientHandle\r\n");
AzureIoTClient 16:deba40344375 321 }
AzureIoTClient 16:deba40344375 322 else
AzureIoTClient 16:deba40344375 323 {
AzureIoTClient 16:deba40344375 324 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 325
AzureIoTClient 16:deba40344375 326 /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 327 if (Lock(iotHubClientInstance->LockHandle) != LOCK_OK)
AzureIoTClient 16:deba40344375 328 {
AzureIoTClient 16:deba40344375 329 /* Codes_SRS_IOTHUBCLIENT_01_036: [If acquiring the lock fails, IoTHubClient_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 16:deba40344375 330 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 16:deba40344375 331 LogError("Could not acquire lock\r\n");
AzureIoTClient 16:deba40344375 332 }
AzureIoTClient 16:deba40344375 333 else
AzureIoTClient 16:deba40344375 334 {
AzureIoTClient 16:deba40344375 335 /* 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 336 /* Codes_SRS_IOTHUBCLIENT_01_021: [Otherwise, IoTHubClient_GetLastMessageReceiveTime shall return the result of IoTHubClient_LL_GetLastMessageReceiveTime.] */
AzureIoTClient 16:deba40344375 337 result = IoTHubClient_LL_GetLastMessageReceiveTime(iotHubClientInstance->IoTHubClientLLHandle, lastMessageReceiveTime);
AzureIoTClient 16:deba40344375 338
AzureIoTClient 16:deba40344375 339 /* Codes_SRS_IOTHUBCLIENT_01_035: [IoTHubClient_GetLastMessageReceiveTime shall be made thread-safe by using the lock created in IoTHubClient_Create.] */
AzureIoTClient 16:deba40344375 340 Unlock(iotHubClientInstance->LockHandle);
AzureIoTClient 16:deba40344375 341 }
AzureIoTClient 16:deba40344375 342 }
AzureIoTClient 16:deba40344375 343
AzureIoTClient 16:deba40344375 344 return result;
AzureIoTClient 16:deba40344375 345 }
AzureIoTClient 16:deba40344375 346
AzureIoTClient 16:deba40344375 347 IOTHUB_CLIENT_RESULT IoTHubClient_SetOption(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const char* optionName, const void* value)
AzureIoTClient 16:deba40344375 348 {
AzureIoTClient 16:deba40344375 349 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 350 /*Codes_SRS_IOTHUBCLIENT_02_034: [If parameter iotHubClientHandle is NULL then IoTHubClient_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 16:deba40344375 351 if (
AzureIoTClient 16:deba40344375 352 (iotHubClientHandle == NULL) ||
AzureIoTClient 16:deba40344375 353 (optionName == NULL) ||
AzureIoTClient 16:deba40344375 354 (value == NULL)
AzureIoTClient 16:deba40344375 355 )
AzureIoTClient 16:deba40344375 356 {
AzureIoTClient 16:deba40344375 357 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 16:deba40344375 358 LogError("invalid arg (NULL)r\n");
AzureIoTClient 16:deba40344375 359 }
AzureIoTClient 16:deba40344375 360 else
AzureIoTClient 16:deba40344375 361 {
AzureIoTClient 16:deba40344375 362 IOTHUB_CLIENT_INSTANCE* iotHubClientInstance = (IOTHUB_CLIENT_INSTANCE*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 363 /*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 16:deba40344375 364 result = IoTHubClient_LL_SetOption(iotHubClientInstance->IoTHubClientLLHandle, optionName, value);
AzureIoTClient 16:deba40344375 365
AzureIoTClient 16:deba40344375 366 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 16:deba40344375 367 {
AzureIoTClient 16:deba40344375 368 LogError("IoTHubClient_LL_SetOption failed\r\n");
AzureIoTClient 16:deba40344375 369 }
AzureIoTClient 16:deba40344375 370 }
AzureIoTClient 16:deba40344375 371 return result;
AzureIoTClient 16:deba40344375 372 }