demo project

Dependencies:   AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL

Committer:
henryrawas
Date:
Tue Jan 26 20:19:43 2016 +0000
Revision:
21:051751f9ca9e
Use private modified iothub client and eliminate extra thread.

Who changed what in which revision?

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