corrected version (with typedef struct IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA* IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE;) included in the sources

Dependents:   STM32F746_iothub_client_sample_mqtt

Fork of iothub_client by Azure IoT

Committer:
AzureIoTClient
Date:
Sat Sep 26 00:18:01 2015 -0700
Revision:
10:38383e246675
Parent:
9:3ec7e2695f98
v1.0.0-preview.3

Who changed what in which revision?

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