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:
Tue Jan 24 15:23:38 2017 -0800
Revision:
57:4524910c6445
Parent:
53:1e5a1ca1f274
Child:
66:a419827cb051
1.1.5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 37:18310e4d888d 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 37:18310e4d888d 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 37:18310e4d888d 3
Azure.IoT Build 37:18310e4d888d 4 #include <stdlib.h>
Azure.IoT Build 38:a05929a75111 5 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 37:18310e4d888d 6 #include <signal.h>
Azure.IoT Build 37:18310e4d888d 7 #include <stddef.h>
Azure.IoT Build 38:a05929a75111 8 #include "azure_c_shared_utility/crt_abstractions.h"
Azure.IoT Build 37:18310e4d888d 9 #include "iothubtransport.h"
Azure.IoT Build 37:18310e4d888d 10 #include "iothub_client.h"
Azure.IoT Build 37:18310e4d888d 11 #include "iothub_client_private.h"
Azure.IoT Build 38:a05929a75111 12 #include "azure_c_shared_utility/threadapi.h"
Azure.IoT Build 38:a05929a75111 13 #include "azure_c_shared_utility/lock.h"
Azure.IoT Build 45:54c11b1b1407 14 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 38:a05929a75111 15 #include "azure_c_shared_utility/vector.h"
Azure.IoT Build 37:18310e4d888d 16
Azure.IoT Build 37:18310e4d888d 17 typedef struct TRANSPORT_HANDLE_DATA_TAG
Azure.IoT Build 37:18310e4d888d 18 {
Azure.IoT Build 37:18310e4d888d 19 TRANSPORT_LL_HANDLE transportLLHandle;
Azure.IoT Build 37:18310e4d888d 20 THREAD_HANDLE workerThreadHandle;
Azure.IoT Build 37:18310e4d888d 21 LOCK_HANDLE lockHandle;
Azure.IoT Build 37:18310e4d888d 22 sig_atomic_t stopThread;
Azure.IoT Build 37:18310e4d888d 23 TRANSPORT_PROVIDER_FIELDS;
Azure.IoT Build 37:18310e4d888d 24 VECTOR_HANDLE clients;
Azure.IoT Build 37:18310e4d888d 25 } TRANSPORT_HANDLE_DATA;
Azure.IoT Build 37:18310e4d888d 26
Azure.IoT Build 37:18310e4d888d 27 /* Used for Unit test */
Azure.IoT Build 37:18310e4d888d 28 const size_t IoTHubTransport_ThreadTerminationOffset = offsetof(TRANSPORT_HANDLE_DATA, stopThread);
Azure.IoT Build 37:18310e4d888d 29
Azure.IoT Build 37:18310e4d888d 30 TRANSPORT_HANDLE IoTHubTransport_Create(IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol, const char* iotHubName, const char* iotHubSuffix)
Azure.IoT Build 37:18310e4d888d 31 {
AzureIoTClient 53:1e5a1ca1f274 32 TRANSPORT_HANDLE_DATA *result;
Azure.IoT Build 37:18310e4d888d 33
Azure.IoT Build 37:18310e4d888d 34 if (protocol == NULL || iotHubName == NULL || iotHubSuffix == NULL)
Azure.IoT Build 37:18310e4d888d 35 {
Azure.IoT Build 37:18310e4d888d 36 /*Codes_SRS_IOTHUBTRANSPORT_17_002: [ If protocol is NULL, this function shall return NULL. ]*/
Azure.IoT Build 37:18310e4d888d 37 /*Codes_SRS_IOTHUBTRANSPORT_17_003: [ If iotHubName is NULL, this function shall return NULL. ]*/
Azure.IoT Build 37:18310e4d888d 38 /*Codes_SRS_IOTHUBTRANSPORT_17_004: [ If iotHubSuffix is NULL, this function shall return NULL. ]*/
Azure.IoT Build 37:18310e4d888d 39 LogError("Invalid NULL argument, protocol [%p], name [%p], suffix [%p].", protocol, iotHubName, iotHubSuffix);
Azure.IoT Build 37:18310e4d888d 40 result = NULL;
Azure.IoT Build 37:18310e4d888d 41 }
Azure.IoT Build 37:18310e4d888d 42 else
Azure.IoT Build 37:18310e4d888d 43 {
Azure.IoT Build 37:18310e4d888d 44 /*Codes_SRS_IOTHUBTRANSPORT_17_032: [ IoTHubTransport_Create shall allocate memory for the transport data. ]*/
Azure.IoT Build 37:18310e4d888d 45 result = (TRANSPORT_HANDLE_DATA*)malloc(sizeof(TRANSPORT_HANDLE_DATA));
Azure.IoT Build 37:18310e4d888d 46 if (result == NULL)
Azure.IoT Build 37:18310e4d888d 47 {
Azure.IoT Build 37:18310e4d888d 48 /*Codes_SRS_IOTHUBTRANSPORT_17_040: [ If memory allocation fails, IoTHubTransport_Create shall return NULL. ]*/
Azure.IoT Build 37:18310e4d888d 49 LogError("Transport handle was not allocated.");
Azure.IoT Build 37:18310e4d888d 50 }
Azure.IoT Build 37:18310e4d888d 51 else
Azure.IoT Build 37:18310e4d888d 52 {
Azure.IoT Build 37:18310e4d888d 53 TRANSPORT_PROVIDER * transportProtocol = (TRANSPORT_PROVIDER*)(protocol());
Azure.IoT Build 37:18310e4d888d 54 IOTHUB_CLIENT_CONFIG upperConfig;
Azure.IoT Build 37:18310e4d888d 55 upperConfig.deviceId = NULL;
Azure.IoT Build 37:18310e4d888d 56 upperConfig.deviceKey = NULL;
Azure.IoT Build 37:18310e4d888d 57 upperConfig.iotHubName = iotHubName;
Azure.IoT Build 37:18310e4d888d 58 upperConfig.iotHubSuffix = iotHubSuffix;
Azure.IoT Build 37:18310e4d888d 59 upperConfig.protocol = protocol;
Azure.IoT Build 37:18310e4d888d 60 upperConfig.protocolGatewayHostName = NULL;
Azure.IoT Build 37:18310e4d888d 61
Azure.IoT Build 37:18310e4d888d 62 IOTHUBTRANSPORT_CONFIG transportLLConfig;
Azure.IoT Build 37:18310e4d888d 63 transportLLConfig.upperConfig = &upperConfig;
Azure.IoT Build 37:18310e4d888d 64 transportLLConfig.waitingToSend = NULL;
Azure.IoT Build 37:18310e4d888d 65
Azure.IoT Build 37:18310e4d888d 66 /*Codes_SRS_IOTHUBTRANSPORT_17_005: [ IoTHubTransport_Create shall create the lower layer transport by calling the protocol's IoTHubTransport_Create function. ]*/
Azure.IoT Build 37:18310e4d888d 67 result->transportLLHandle = transportProtocol->IoTHubTransport_Create(&transportLLConfig);
Azure.IoT Build 37:18310e4d888d 68 if (result->transportLLHandle == NULL)
Azure.IoT Build 37:18310e4d888d 69 {
Azure.IoT Build 37:18310e4d888d 70 /*Codes_SRS_IOTHUBTRANSPORT_17_006: [ If the creation of the transport fails, IoTHubTransport_Create shall return NULL. ]*/
Azure.IoT Build 37:18310e4d888d 71 LogError("Lower Layer transport not created.");
Azure.IoT Build 37:18310e4d888d 72 free(result);
Azure.IoT Build 37:18310e4d888d 73 result = NULL;
Azure.IoT Build 37:18310e4d888d 74 }
Azure.IoT Build 37:18310e4d888d 75 else
Azure.IoT Build 37:18310e4d888d 76 {
Azure.IoT Build 37:18310e4d888d 77 /*Codes_SRS_IOTHUBTRANSPORT_17_007: [ IoTHubTransport_Create shall create the transport lock by Calling Lock_Init. ]*/
Azure.IoT Build 37:18310e4d888d 78 result->lockHandle = Lock_Init();
Azure.IoT Build 37:18310e4d888d 79 if (result->lockHandle == NULL)
Azure.IoT Build 37:18310e4d888d 80 {
Azure.IoT Build 37:18310e4d888d 81 /*Codes_SRS_IOTHUBTRANSPORT_17_008: [ If the lock creation fails, IoTHubTransport_Create shall return NULL. ]*/
Azure.IoT Build 37:18310e4d888d 82 LogError("transport Lock not created.");
Azure.IoT Build 37:18310e4d888d 83 transportProtocol->IoTHubTransport_Destroy(result->transportLLHandle);
Azure.IoT Build 37:18310e4d888d 84 free(result);
Azure.IoT Build 37:18310e4d888d 85 result = NULL;
Azure.IoT Build 37:18310e4d888d 86 }
Azure.IoT Build 37:18310e4d888d 87 else
Azure.IoT Build 37:18310e4d888d 88 {
Azure.IoT Build 37:18310e4d888d 89 /*Codes_SRS_IOTHUBTRANSPORT_17_038: [ IoTHubTransport_Create shall call VECTOR_Create to make a list of IOTHUB_CLIENT_HANDLE using this transport. ]*/
Azure.IoT Build 37:18310e4d888d 90 result->clients = VECTOR_create(sizeof(IOTHUB_CLIENT_HANDLE));
Azure.IoT Build 37:18310e4d888d 91 if (result->clients == NULL)
Azure.IoT Build 37:18310e4d888d 92 {
Azure.IoT Build 37:18310e4d888d 93 /*Codes_SRS_IOTHUBTRANSPORT_17_039: [ If the Vector creation fails, IoTHubTransport_Create shall return NULL. ]*/
Azure.IoT Build 37:18310e4d888d 94 /*Codes_SRS_IOTHUBTRANSPORT_17_009: [ IoTHubTransport_Create shall clean up any resources it creates if the function does not succeed. ]*/
Azure.IoT Build 37:18310e4d888d 95 LogError("clients list not created.");
Azure.IoT Build 37:18310e4d888d 96 Lock_Deinit(result->lockHandle);
Azure.IoT Build 37:18310e4d888d 97 transportProtocol->IoTHubTransport_Destroy(result->transportLLHandle);
Azure.IoT Build 37:18310e4d888d 98 free(result);
Azure.IoT Build 37:18310e4d888d 99 result = NULL;
Azure.IoT Build 37:18310e4d888d 100 }
Azure.IoT Build 37:18310e4d888d 101 else
Azure.IoT Build 37:18310e4d888d 102 {
Azure.IoT Build 37:18310e4d888d 103 /*Codes_SRS_IOTHUBTRANSPORT_17_001: [ IoTHubTransport_Create shall return a non-NULL handle on success.]*/
Azure.IoT Build 37:18310e4d888d 104 result->stopThread = 1;
Azure.IoT Build 37:18310e4d888d 105 result->workerThreadHandle = NULL; /* create thread when work needs to be done */
AzureIoTClient 43:038d8511e817 106 result->IoTHubTransport_GetHostname = transportProtocol->IoTHubTransport_GetHostname;
Azure.IoT Build 37:18310e4d888d 107 result->IoTHubTransport_SetOption = transportProtocol->IoTHubTransport_SetOption;
Azure.IoT Build 37:18310e4d888d 108 result->IoTHubTransport_Create = transportProtocol->IoTHubTransport_Create;
Azure.IoT Build 37:18310e4d888d 109 result->IoTHubTransport_Destroy = transportProtocol->IoTHubTransport_Destroy;
Azure.IoT Build 37:18310e4d888d 110 result->IoTHubTransport_Register = transportProtocol->IoTHubTransport_Register;
Azure.IoT Build 37:18310e4d888d 111 result->IoTHubTransport_Unregister = transportProtocol->IoTHubTransport_Unregister;
Azure.IoT Build 37:18310e4d888d 112 result->IoTHubTransport_Subscribe = transportProtocol->IoTHubTransport_Subscribe;
Azure.IoT Build 37:18310e4d888d 113 result->IoTHubTransport_Unsubscribe = transportProtocol->IoTHubTransport_Unsubscribe;
Azure.IoT Build 37:18310e4d888d 114 result->IoTHubTransport_DoWork = transportProtocol->IoTHubTransport_DoWork;
AzureIoTClient 53:1e5a1ca1f274 115 result->IoTHubTransport_SetRetryPolicy = transportProtocol->IoTHubTransport_SetRetryPolicy;
Azure.IoT Build 37:18310e4d888d 116 result->IoTHubTransport_GetSendStatus = transportProtocol->IoTHubTransport_GetSendStatus;
Azure.IoT Build 37:18310e4d888d 117 }
Azure.IoT Build 37:18310e4d888d 118 }
Azure.IoT Build 37:18310e4d888d 119 }
Azure.IoT Build 37:18310e4d888d 120 }
Azure.IoT Build 37:18310e4d888d 121 }
Azure.IoT Build 37:18310e4d888d 122
Azure.IoT Build 37:18310e4d888d 123 return result;
Azure.IoT Build 37:18310e4d888d 124 }
Azure.IoT Build 37:18310e4d888d 125
Azure.IoT Build 37:18310e4d888d 126 static int transport_worker_thread(void* threadArgument)
Azure.IoT Build 37:18310e4d888d 127 {
Azure.IoT Build 37:18310e4d888d 128 TRANSPORT_HANDLE_DATA* transportData = (TRANSPORT_HANDLE_DATA*)threadArgument;
Azure.IoT Build 37:18310e4d888d 129
Azure.IoT Build 37:18310e4d888d 130 while (1)
Azure.IoT Build 37:18310e4d888d 131 {
Azure.IoT Build 37:18310e4d888d 132 /*Codes_SRS_IOTHUBTRANSPORT_17_030: [ All calls to lower layer transport DoWork shall be protected by the lock created in IoTHubTransport_Create. ]*/
Azure.IoT Build 37:18310e4d888d 133 if (Lock(transportData->lockHandle) == LOCK_OK)
Azure.IoT Build 37:18310e4d888d 134 {
Azure.IoT Build 37:18310e4d888d 135 /*Codes_SRS_IOTHUBTRANSPORT_17_031: [ If acquiring the lock fails, lower layer transport DoWork shall not be called. ]*/
Azure.IoT Build 37:18310e4d888d 136 if (transportData->stopThread)
Azure.IoT Build 37:18310e4d888d 137 {
Azure.IoT Build 37:18310e4d888d 138 /*Codes_SRS_IOTHUBTRANSPORT_17_028: [ The thread shall exit when IoTHubTransport_EndWorkerThread has been called for each clientHandle which invoked IoTHubTransport_StartWorkerThread. ]*/
Azure.IoT Build 37:18310e4d888d 139 (void)Unlock(transportData->lockHandle);
Azure.IoT Build 37:18310e4d888d 140 break;
Azure.IoT Build 37:18310e4d888d 141 }
Azure.IoT Build 37:18310e4d888d 142 else
Azure.IoT Build 37:18310e4d888d 143 {
Azure.IoT Build 37:18310e4d888d 144 (transportData->IoTHubTransport_DoWork)(transportData->transportLLHandle, NULL);
Azure.IoT Build 37:18310e4d888d 145 (void)Unlock(transportData->lockHandle);
Azure.IoT Build 37:18310e4d888d 146 }
Azure.IoT Build 37:18310e4d888d 147 }
Azure.IoT Build 37:18310e4d888d 148 /*Codes_SRS_IOTHUBTRANSPORT_17_029: [ The thread shall call lower layer transport DoWork every 1 ms. ]*/
Azure.IoT Build 37:18310e4d888d 149 ThreadAPI_Sleep(1);
Azure.IoT Build 37:18310e4d888d 150 }
Azure.IoT Build 37:18310e4d888d 151
Azure.IoT Build 37:18310e4d888d 152 return 0;
Azure.IoT Build 37:18310e4d888d 153 }
Azure.IoT Build 37:18310e4d888d 154
Azure.IoT Build 37:18310e4d888d 155 static bool find_by_handle(const void* element, const void* value)
Azure.IoT Build 37:18310e4d888d 156 {
Azure.IoT Build 37:18310e4d888d 157 /* data stored at element is device handle */
Azure.IoT Build 37:18310e4d888d 158 const IOTHUB_CLIENT_HANDLE * guess = (const IOTHUB_CLIENT_HANDLE *)element;
Azure.IoT Build 37:18310e4d888d 159 const IOTHUB_CLIENT_HANDLE match = (const IOTHUB_CLIENT_HANDLE)value;
Azure.IoT Build 37:18310e4d888d 160 return (*guess == match);
Azure.IoT Build 37:18310e4d888d 161 }
Azure.IoT Build 37:18310e4d888d 162
Azure.IoT Build 37:18310e4d888d 163 static IOTHUB_CLIENT_RESULT start_worker_if_needed(TRANSPORT_HANDLE_DATA * transportData, IOTHUB_CLIENT_HANDLE clientHandle)
Azure.IoT Build 37:18310e4d888d 164 {
Azure.IoT Build 37:18310e4d888d 165 IOTHUB_CLIENT_RESULT result;
Azure.IoT Build 37:18310e4d888d 166 if (transportData->workerThreadHandle == NULL)
Azure.IoT Build 37:18310e4d888d 167 {
Azure.IoT Build 37:18310e4d888d 168 /*Codes_SRS_IOTHUBTRANSPORT_17_018: [ If the worker thread does not exist, IoTHubTransport_StartWorkerThread shall start the thread using ThreadAPI_Create. ]*/
Azure.IoT Build 37:18310e4d888d 169 transportData->stopThread = 0;
Azure.IoT Build 37:18310e4d888d 170 if (ThreadAPI_Create(&transportData->workerThreadHandle, transport_worker_thread, transportData) != THREADAPI_OK)
Azure.IoT Build 37:18310e4d888d 171 {
Azure.IoT Build 37:18310e4d888d 172 transportData->workerThreadHandle = NULL;
Azure.IoT Build 37:18310e4d888d 173 }
Azure.IoT Build 37:18310e4d888d 174 }
Azure.IoT Build 37:18310e4d888d 175 if (transportData->workerThreadHandle != NULL)
Azure.IoT Build 37:18310e4d888d 176 {
Azure.IoT Build 37:18310e4d888d 177 /*Codes_SRS_IOTHUBTRANSPORT_17_020: [ IoTHubTransport_StartWorkerThread shall search for IoTHubClient clientHandle in the list of IoTHubClient handles. ]*/
Azure.IoT Build 37:18310e4d888d 178 bool addToList = ((VECTOR_size(transportData->clients) == 0) || (VECTOR_find_if(transportData->clients, find_by_handle, clientHandle) == NULL));
Azure.IoT Build 37:18310e4d888d 179 if (addToList)
Azure.IoT Build 37:18310e4d888d 180 {
Azure.IoT Build 37:18310e4d888d 181 /*Codes_SRS_IOTHUBTRANSPORT_17_021: [ If handle is not found, then clientHandle shall be added to the list. ]*/
Azure.IoT Build 37:18310e4d888d 182 if (VECTOR_push_back(transportData->clients, &clientHandle, 1) != 0)
Azure.IoT Build 37:18310e4d888d 183 {
Azure.IoT Build 37:18310e4d888d 184 /*Codes_SRS_IOTHUBTRANSPORT_17_042: [ If Adding to the client list fails, IoTHubTransport_StartWorkerThread shall return IOTHUB_CLIENT_ERROR. ]*/
Azure.IoT Build 37:18310e4d888d 185 result = IOTHUB_CLIENT_ERROR;
Azure.IoT Build 37:18310e4d888d 186 }
Azure.IoT Build 37:18310e4d888d 187 else
Azure.IoT Build 37:18310e4d888d 188 {
Azure.IoT Build 37:18310e4d888d 189 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 37:18310e4d888d 190 }
Azure.IoT Build 37:18310e4d888d 191 }
Azure.IoT Build 37:18310e4d888d 192 else
Azure.IoT Build 37:18310e4d888d 193 {
Azure.IoT Build 37:18310e4d888d 194 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 37:18310e4d888d 195 }
Azure.IoT Build 37:18310e4d888d 196 }
Azure.IoT Build 37:18310e4d888d 197 else
Azure.IoT Build 37:18310e4d888d 198 {
Azure.IoT Build 37:18310e4d888d 199 result = IOTHUB_CLIENT_ERROR;
Azure.IoT Build 37:18310e4d888d 200 }
Azure.IoT Build 37:18310e4d888d 201 return result;
Azure.IoT Build 37:18310e4d888d 202 }
Azure.IoT Build 37:18310e4d888d 203
Azure.IoT Build 37:18310e4d888d 204 static void stop_worker_thread(TRANSPORT_HANDLE_DATA * transportData)
Azure.IoT Build 37:18310e4d888d 205 {
Azure.IoT Build 37:18310e4d888d 206 /*Codes_SRS_IOTHUBTRANSPORT_17_043: [** IoTHubTransport_SignalEndWorkerThread shall signal the worker thread to end.*/
Azure.IoT Build 37:18310e4d888d 207 transportData->stopThread = 1;
Azure.IoT Build 37:18310e4d888d 208 }
Azure.IoT Build 37:18310e4d888d 209
Azure.IoT Build 37:18310e4d888d 210 static void wait_worker_thread(TRANSPORT_HANDLE_DATA * transportData)
Azure.IoT Build 37:18310e4d888d 211 {
Azure.IoT Build 37:18310e4d888d 212 if (transportData->workerThreadHandle != NULL)
Azure.IoT Build 37:18310e4d888d 213 {
Azure.IoT Build 37:18310e4d888d 214 int res;
Azure.IoT Build 37:18310e4d888d 215 /*Codes_SRS_IOTHUBTRANSPORT_17_027: [ If handle list is empty, IoTHubTransport_EndWorkerThread shall be joined. ]*/
Azure.IoT Build 37:18310e4d888d 216 if (ThreadAPI_Join(transportData->workerThreadHandle, &res) != THREADAPI_OK)
Azure.IoT Build 37:18310e4d888d 217 {
Azure.IoT Build 37:18310e4d888d 218 LogError("ThreadAPI_Join failed");
Azure.IoT Build 37:18310e4d888d 219 }
Azure.IoT Build 37:18310e4d888d 220 else
Azure.IoT Build 37:18310e4d888d 221 {
Azure.IoT Build 37:18310e4d888d 222 transportData->workerThreadHandle = NULL;
Azure.IoT Build 37:18310e4d888d 223 }
Azure.IoT Build 37:18310e4d888d 224 }
Azure.IoT Build 37:18310e4d888d 225 }
Azure.IoT Build 37:18310e4d888d 226
Azure.IoT Build 37:18310e4d888d 227 static bool signal_end_worker_thread(TRANSPORT_HANDLE_DATA * transportData, IOTHUB_CLIENT_HANDLE clientHandle)
Azure.IoT Build 37:18310e4d888d 228 {
Azure.IoT Build 37:18310e4d888d 229 bool okToJoin;
Azure.IoT Build 37:18310e4d888d 230 void* element = VECTOR_find_if(transportData->clients, find_by_handle, clientHandle);
Azure.IoT Build 37:18310e4d888d 231 if (element != NULL)
Azure.IoT Build 37:18310e4d888d 232 {
Azure.IoT Build 37:18310e4d888d 233 /*Codes_SRS_IOTHUBTRANSPORT_17_026: [ IoTHubTransport_EndWorkerThread shall remove clientHandlehandle from handle list. ]*/
Azure.IoT Build 37:18310e4d888d 234 VECTOR_erase(transportData->clients, element, 1);
Azure.IoT Build 37:18310e4d888d 235 }
Azure.IoT Build 37:18310e4d888d 236 /*Codes_SRS_IOTHUBTRANSPORT_17_025: [ If the worker thread does not exist, then IoTHubTransport_EndWorkerThread shall return. ]*/
Azure.IoT Build 37:18310e4d888d 237 if (transportData->workerThreadHandle != NULL)
Azure.IoT Build 37:18310e4d888d 238 {
Azure.IoT Build 37:18310e4d888d 239 if (VECTOR_size(transportData->clients) == 0)
Azure.IoT Build 37:18310e4d888d 240 {
Azure.IoT Build 37:18310e4d888d 241 stop_worker_thread(transportData);
Azure.IoT Build 37:18310e4d888d 242 okToJoin = true;
Azure.IoT Build 37:18310e4d888d 243 }
Azure.IoT Build 37:18310e4d888d 244 else
Azure.IoT Build 37:18310e4d888d 245 {
Azure.IoT Build 37:18310e4d888d 246 okToJoin = false;
Azure.IoT Build 37:18310e4d888d 247 }
Azure.IoT Build 37:18310e4d888d 248 }
Azure.IoT Build 37:18310e4d888d 249 else
Azure.IoT Build 37:18310e4d888d 250 {
Azure.IoT Build 37:18310e4d888d 251 okToJoin = false;
Azure.IoT Build 37:18310e4d888d 252 }
Azure.IoT Build 37:18310e4d888d 253 return okToJoin;
Azure.IoT Build 37:18310e4d888d 254 }
Azure.IoT Build 37:18310e4d888d 255
Azure.IoT Build 38:a05929a75111 256 void IoTHubTransport_Destroy(TRANSPORT_HANDLE transportHandle)
Azure.IoT Build 37:18310e4d888d 257 {
Azure.IoT Build 38:a05929a75111 258 /*Codes_SRS_IOTHUBTRANSPORT_17_011: [ IoTHubTransport_Destroy shall do nothing if transportHandle is NULL. ]*/
Azure.IoT Build 38:a05929a75111 259 if (transportHandle != NULL)
Azure.IoT Build 37:18310e4d888d 260 {
Azure.IoT Build 38:a05929a75111 261 TRANSPORT_HANDLE_DATA * transportData = (TRANSPORT_HANDLE_DATA*)transportHandle;
Azure.IoT Build 37:18310e4d888d 262 /*Codes_SRS_IOTHUBTRANSPORT_17_033: [ IoTHubTransport_Destroy shall lock the transport lock. ]*/
Azure.IoT Build 37:18310e4d888d 263 if (Lock(transportData->lockHandle) != LOCK_OK)
Azure.IoT Build 37:18310e4d888d 264 {
Azure.IoT Build 37:18310e4d888d 265 LogError("Unable to lock - will still attempt to end thread without thread safety");
Azure.IoT Build 37:18310e4d888d 266 stop_worker_thread(transportData);
Azure.IoT Build 37:18310e4d888d 267 }
Azure.IoT Build 37:18310e4d888d 268 else
Azure.IoT Build 37:18310e4d888d 269 {
Azure.IoT Build 37:18310e4d888d 270 stop_worker_thread(transportData);
Azure.IoT Build 37:18310e4d888d 271 (void)Unlock(transportData->lockHandle);
Azure.IoT Build 37:18310e4d888d 272 }
Azure.IoT Build 37:18310e4d888d 273 wait_worker_thread(transportData);
Azure.IoT Build 37:18310e4d888d 274 /*Codes_SRS_IOTHUBTRANSPORT_17_010: [ IoTHubTransport_Destroy shall free all resources. ]*/
Azure.IoT Build 37:18310e4d888d 275 Lock_Deinit(transportData->lockHandle);
Azure.IoT Build 37:18310e4d888d 276 (transportData->IoTHubTransport_Destroy)(transportData->transportLLHandle);
Azure.IoT Build 37:18310e4d888d 277 VECTOR_destroy(transportData->clients);
Azure.IoT Build 38:a05929a75111 278 free(transportHandle);
Azure.IoT Build 37:18310e4d888d 279 }
Azure.IoT Build 37:18310e4d888d 280 }
Azure.IoT Build 37:18310e4d888d 281
Azure.IoT Build 38:a05929a75111 282 LOCK_HANDLE IoTHubTransport_GetLock(TRANSPORT_HANDLE transportHandle)
Azure.IoT Build 37:18310e4d888d 283 {
Azure.IoT Build 37:18310e4d888d 284 LOCK_HANDLE lock;
Azure.IoT Build 38:a05929a75111 285 if (transportHandle == NULL)
Azure.IoT Build 37:18310e4d888d 286 {
Azure.IoT Build 38:a05929a75111 287 /*Codes_SRS_IOTHUBTRANSPORT_17_013: [ If transportHandle is NULL, IoTHubTransport_GetLock shall return NULL. ]*/
Azure.IoT Build 37:18310e4d888d 288 lock = NULL;
Azure.IoT Build 37:18310e4d888d 289 }
Azure.IoT Build 37:18310e4d888d 290 else
Azure.IoT Build 37:18310e4d888d 291 {
Azure.IoT Build 37:18310e4d888d 292 /*Codes_SRS_IOTHUBTRANSPORT_17_012: [ IoTHubTransport_GetLock shall return a handle to the transport lock. ]*/
Azure.IoT Build 38:a05929a75111 293 TRANSPORT_HANDLE_DATA * transportData = (TRANSPORT_HANDLE_DATA*)transportHandle;
Azure.IoT Build 37:18310e4d888d 294 lock = transportData->lockHandle;
Azure.IoT Build 37:18310e4d888d 295 }
Azure.IoT Build 37:18310e4d888d 296 return lock;
Azure.IoT Build 37:18310e4d888d 297 }
Azure.IoT Build 37:18310e4d888d 298
Azure.IoT Build 38:a05929a75111 299 TRANSPORT_LL_HANDLE IoTHubTransport_GetLLTransport(TRANSPORT_HANDLE transportHandle)
Azure.IoT Build 37:18310e4d888d 300 {
Azure.IoT Build 37:18310e4d888d 301 TRANSPORT_LL_HANDLE llTransport;
Azure.IoT Build 38:a05929a75111 302 if (transportHandle == NULL)
Azure.IoT Build 37:18310e4d888d 303 {
Azure.IoT Build 38:a05929a75111 304 /*Codes_SRS_IOTHUBTRANSPORT_17_015: [ If transportHandle is NULL, IoTHubTransport_GetLLTransport shall return NULL. ]*/
Azure.IoT Build 37:18310e4d888d 305 llTransport = NULL;
Azure.IoT Build 37:18310e4d888d 306 }
Azure.IoT Build 37:18310e4d888d 307 else
Azure.IoT Build 37:18310e4d888d 308 {
Azure.IoT Build 37:18310e4d888d 309 /*Codes_SRS_IOTHUBTRANSPORT_17_014: [ IoTHubTransport_GetLLTransport shall return a handle to the lower layer transport. ]*/
Azure.IoT Build 38:a05929a75111 310 TRANSPORT_HANDLE_DATA * transportData = (TRANSPORT_HANDLE_DATA*)transportHandle;
Azure.IoT Build 37:18310e4d888d 311 llTransport = transportData->transportLLHandle;
Azure.IoT Build 37:18310e4d888d 312 }
Azure.IoT Build 37:18310e4d888d 313 return llTransport;
Azure.IoT Build 37:18310e4d888d 314 }
Azure.IoT Build 37:18310e4d888d 315
Azure.IoT Build 38:a05929a75111 316 IOTHUB_CLIENT_RESULT IoTHubTransport_StartWorkerThread(TRANSPORT_HANDLE transportHandle, IOTHUB_CLIENT_HANDLE clientHandle)
Azure.IoT Build 37:18310e4d888d 317 {
Azure.IoT Build 37:18310e4d888d 318 IOTHUB_CLIENT_RESULT result;
Azure.IoT Build 38:a05929a75111 319 if (transportHandle == NULL || clientHandle == NULL)
Azure.IoT Build 37:18310e4d888d 320 {
Azure.IoT Build 38:a05929a75111 321 /*Codes_SRS_IOTHUBTRANSPORT_17_016: [ If transportHandle is NULL, IoTHubTransport_StartWorkerThread shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
Azure.IoT Build 37:18310e4d888d 322 /*Codes_SRS_IOTHUBTRANSPORT_17_017: [ If clientHandle is NULL, IoTHubTransport_StartWorkerThread shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
Azure.IoT Build 37:18310e4d888d 323 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 37:18310e4d888d 324 }
Azure.IoT Build 37:18310e4d888d 325 else
Azure.IoT Build 37:18310e4d888d 326 {
Azure.IoT Build 38:a05929a75111 327 TRANSPORT_HANDLE_DATA * transportData = (TRANSPORT_HANDLE_DATA*)transportHandle;
Azure.IoT Build 37:18310e4d888d 328
Azure.IoT Build 37:18310e4d888d 329 if ((result = start_worker_if_needed(transportData, clientHandle)) != IOTHUB_CLIENT_OK)
Azure.IoT Build 37:18310e4d888d 330 {
Azure.IoT Build 37:18310e4d888d 331 /*Codes_SRS_IOTHUBTRANSPORT_17_019: [ If thread creation fails, IoTHubTransport_StartWorkerThread shall return IOTHUB_CLIENT_ERROR. */
Azure.IoT Build 37:18310e4d888d 332 LogError("Unable to start thread safely");
Azure.IoT Build 37:18310e4d888d 333 }
Azure.IoT Build 37:18310e4d888d 334 else
Azure.IoT Build 37:18310e4d888d 335 {
Azure.IoT Build 37:18310e4d888d 336 /*Codes_SRS_IOTHUBTRANSPORT_17_022: [ Upon success, IoTHubTransport_StartWorkerThread shall return IOTHUB_CLIENT_OK. ]*/
Azure.IoT Build 37:18310e4d888d 337 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 37:18310e4d888d 338 }
Azure.IoT Build 37:18310e4d888d 339 }
Azure.IoT Build 37:18310e4d888d 340 return result;
Azure.IoT Build 37:18310e4d888d 341 }
Azure.IoT Build 37:18310e4d888d 342
Azure.IoT Build 38:a05929a75111 343 bool IoTHubTransport_SignalEndWorkerThread(TRANSPORT_HANDLE transportHandle, IOTHUB_CLIENT_HANDLE clientHandle)
Azure.IoT Build 37:18310e4d888d 344 {
Azure.IoT Build 37:18310e4d888d 345 bool okToJoin;
Azure.IoT Build 38:a05929a75111 346 /*Codes_SRS_IOTHUBTRANSPORT_17_023: [ If transportHandle is NULL, IoTHubTransport_EndWorkerThread shall return. ]*/
Azure.IoT Build 37:18310e4d888d 347 /*Codes_SRS_IOTHUBTRANSPORT_17_024: [ If clientHandle is NULL, IoTHubTransport_EndWorkerThread shall return. ]*/
Azure.IoT Build 38:a05929a75111 348 if (!(transportHandle == NULL || clientHandle == NULL))
Azure.IoT Build 37:18310e4d888d 349 {
Azure.IoT Build 38:a05929a75111 350 TRANSPORT_HANDLE_DATA * transportData = (TRANSPORT_HANDLE_DATA*)transportHandle;
Azure.IoT Build 37:18310e4d888d 351 okToJoin = signal_end_worker_thread(transportData, clientHandle);
Azure.IoT Build 37:18310e4d888d 352 }
Azure.IoT Build 37:18310e4d888d 353 else
Azure.IoT Build 37:18310e4d888d 354 {
Azure.IoT Build 37:18310e4d888d 355 okToJoin = false;
Azure.IoT Build 37:18310e4d888d 356 }
Azure.IoT Build 37:18310e4d888d 357 return okToJoin;
Azure.IoT Build 37:18310e4d888d 358 }
Azure.IoT Build 37:18310e4d888d 359
Azure.IoT Build 38:a05929a75111 360 void IoTHubTransport_JoinWorkerThread(TRANSPORT_HANDLE transportHandle, IOTHUB_CLIENT_HANDLE clientHandle)
Azure.IoT Build 37:18310e4d888d 361 {
Azure.IoT Build 38:a05929a75111 362 /*Codes_SRS_IOTHUBTRANSPORT_17_044: [ If transportHandle is NULL, IoTHubTransport_JoinWorkerThread shall do nothing. ]*/
Azure.IoT Build 37:18310e4d888d 363 /*Codes_SRS_IOTHUBTRANSPORT_17_045: [ If clientHandle is NULL, IoTHubTransport_JoinWorkerThread shall do nothing. ]*/
Azure.IoT Build 38:a05929a75111 364 if (!(transportHandle == NULL || clientHandle == NULL))
Azure.IoT Build 37:18310e4d888d 365 {
Azure.IoT Build 38:a05929a75111 366 TRANSPORT_HANDLE_DATA * transportData = (TRANSPORT_HANDLE_DATA*)transportHandle;
Azure.IoT Build 37:18310e4d888d 367 /*Codes_SRS_IOTHUBTRANSPORT_17_027: [ The worker thread shall be joined. ]*/
Azure.IoT Build 37:18310e4d888d 368 wait_worker_thread(transportData);
Azure.IoT Build 37:18310e4d888d 369 }
Azure.IoT Build 37:18310e4d888d 370 }