[457] | 1 | // Copyright (c) Microsoft. All rights reserved.
|
---|
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
---|
| 3 |
|
---|
| 4 | #include <stdlib.h>
|
---|
| 5 |
|
---|
| 6 | #include <signal.h>
|
---|
| 7 | #include "azure_c_shared_utility/gballoc.h"
|
---|
| 8 | #include "azure_c_shared_utility/threadapi.h"
|
---|
| 9 | #include "azure_c_shared_utility/lock.h"
|
---|
| 10 | #include "azure_c_shared_utility/xlogging.h"
|
---|
| 11 |
|
---|
| 12 | #include "azure_prov_client/prov_device_ll_client.h"
|
---|
| 13 | #include "azure_prov_client/prov_device_client.h"
|
---|
| 14 | #include "azure_c_shared_utility/vector.h"
|
---|
| 15 |
|
---|
| 16 | #define DO_WORK_FREQ_DEFAULT 1
|
---|
| 17 |
|
---|
| 18 | typedef struct PROV_DEVICE_INSTANCE_TAG
|
---|
| 19 | {
|
---|
| 20 | PROV_DEVICE_LL_HANDLE ProvDeviceLLHandle;
|
---|
| 21 | THREAD_HANDLE ThreadHandle;
|
---|
| 22 | LOCK_HANDLE LockHandle;
|
---|
| 23 | sig_atomic_t StopThread;
|
---|
| 24 | uint16_t do_work_freq_ms;
|
---|
| 25 | } PROV_DEVICE_INSTANCE;
|
---|
| 26 |
|
---|
| 27 | static int ScheduleWork_Thread(void* threadArgument)
|
---|
| 28 | {
|
---|
| 29 | PROV_DEVICE_INSTANCE* prov_device_instance = (PROV_DEVICE_INSTANCE*)threadArgument;
|
---|
| 30 | uint16_t sleeptime_in_ms = DO_WORK_FREQ_DEFAULT;
|
---|
| 31 |
|
---|
| 32 | srand((unsigned int)get_time(NULL));
|
---|
| 33 |
|
---|
| 34 | while (1)
|
---|
| 35 | {
|
---|
| 36 | if (Lock(prov_device_instance->LockHandle) == LOCK_OK)
|
---|
| 37 | {
|
---|
| 38 | if (prov_device_instance->StopThread)
|
---|
| 39 | {
|
---|
| 40 | (void)Unlock(prov_device_instance->LockHandle);
|
---|
| 41 | break; /*gets out of the thread*/
|
---|
| 42 | }
|
---|
| 43 | else
|
---|
| 44 | {
|
---|
| 45 | Prov_Device_LL_DoWork(prov_device_instance->ProvDeviceLLHandle);
|
---|
| 46 | sleeptime_in_ms = prov_device_instance->do_work_freq_ms; // Update the sleepval within the locked thread.
|
---|
| 47 | (void)Unlock(prov_device_instance->LockHandle);
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | else
|
---|
| 51 | {
|
---|
| 52 | LogError("Lock failed, shall retry");
|
---|
| 53 | }
|
---|
| 54 | (void)ThreadAPI_Sleep(sleeptime_in_ms);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | ThreadAPI_Exit(0);
|
---|
| 58 | return 0;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | static PROV_DEVICE_RESULT StartWorkerThreadIfNeeded(PROV_DEVICE_INSTANCE* prov_device_instance)
|
---|
| 62 | {
|
---|
| 63 | PROV_DEVICE_RESULT result;
|
---|
| 64 | if (prov_device_instance->ThreadHandle == NULL)
|
---|
| 65 | {
|
---|
| 66 | prov_device_instance->StopThread = 0;
|
---|
| 67 | if (ThreadAPI_Create(&prov_device_instance->ThreadHandle, ScheduleWork_Thread, prov_device_instance) != THREADAPI_OK)
|
---|
| 68 | {
|
---|
| 69 | LogError("ThreadAPI_Create failed");
|
---|
| 70 | result = PROV_DEVICE_RESULT_ERROR;
|
---|
| 71 | }
|
---|
| 72 | else
|
---|
| 73 | {
|
---|
| 74 | result = PROV_DEVICE_RESULT_OK;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | else
|
---|
| 78 | {
|
---|
| 79 | result = PROV_DEVICE_RESULT_OK;
|
---|
| 80 | }
|
---|
| 81 | return result;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | PROV_DEVICE_HANDLE Prov_Device_Create(const char* uri, const char* id_scope, PROV_DEVICE_TRANSPORT_PROVIDER_FUNCTION protocol)
|
---|
| 85 | {
|
---|
| 86 | PROV_DEVICE_INSTANCE* result;
|
---|
| 87 |
|
---|
| 88 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_001: [ If any of the input parameter is NULL `Prov_Device_Create` shall return NULL.] */
|
---|
| 89 | if (uri == NULL || id_scope == NULL || protocol == NULL)
|
---|
| 90 | {
|
---|
| 91 | LogError("Invalid parameter specified uri: %p, id_scope: %p, protocol: %p", uri, id_scope, protocol);
|
---|
| 92 | result = NULL;
|
---|
| 93 | }
|
---|
| 94 | else
|
---|
| 95 | {
|
---|
| 96 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_002: [ The function shall allocate memory for PROV_DEVICE_INSTANCE data structure. ] */
|
---|
| 97 | result = (PROV_DEVICE_INSTANCE*)malloc(sizeof(PROV_DEVICE_INSTANCE));
|
---|
| 98 | if (result == NULL)
|
---|
| 99 | {
|
---|
| 100 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_003: [ If the memory allocation failed the function shall return NULL. ] */
|
---|
| 101 | LogError("Unable to allocate Instance Info");
|
---|
| 102 | }
|
---|
| 103 | else
|
---|
| 104 | {
|
---|
| 105 | memset(result, 0, sizeof(PROV_DEVICE_INSTANCE) );
|
---|
| 106 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_004: [ The function shall initialize the Lock. ] */
|
---|
| 107 | result->LockHandle = Lock_Init();
|
---|
| 108 | if (result->LockHandle == NULL)
|
---|
| 109 | {
|
---|
| 110 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_005: [ If the Lock initialization failed the function shall clean up the all resources and return NULL. ] */
|
---|
| 111 | LogError("Lock_Init failed");
|
---|
| 112 | free(result);
|
---|
| 113 | result = NULL;
|
---|
| 114 | }
|
---|
| 115 | else
|
---|
| 116 | {
|
---|
| 117 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_006: [ The function shall call the LL layer Prov_Device_LL_Create function and return with it's result. ] */
|
---|
| 118 | result->ProvDeviceLLHandle = Prov_Device_LL_Create(uri, id_scope, protocol);
|
---|
| 119 | if (result->ProvDeviceLLHandle == NULL)
|
---|
| 120 | {
|
---|
| 121 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_025: [ If the Client initialization failed the function shall clean up the all resources and return NULL. ] */
|
---|
| 122 | LogError("Prov_Device_LL_Create failed");
|
---|
| 123 | free(result);
|
---|
| 124 | result = NULL;
|
---|
| 125 | }
|
---|
| 126 | else
|
---|
| 127 | {
|
---|
| 128 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_007: [ The function shall initialize the result datastructure. ] */
|
---|
| 129 | result->ThreadHandle = NULL;
|
---|
| 130 | result->StopThread = 0;
|
---|
| 131 | result->do_work_freq_ms = DO_WORK_FREQ_DEFAULT;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | return result;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void Prov_Device_Destroy(PROV_DEVICE_HANDLE prov_device_handle)
|
---|
| 141 | {
|
---|
| 142 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_008: [ If the input parameter is NULL `Prov_Device_Destroy` shall return. ] */
|
---|
| 143 | if (prov_device_handle == NULL)
|
---|
| 144 | {
|
---|
| 145 | LogError("NULL prov_device_handle");
|
---|
| 146 | }
|
---|
| 147 | else
|
---|
| 148 | {
|
---|
| 149 | PROV_DEVICE_INSTANCE* prov_device_instance = (PROV_DEVICE_INSTANCE*)prov_device_handle;
|
---|
| 150 |
|
---|
| 151 | if (Lock(prov_device_handle->LockHandle) != LOCK_OK)
|
---|
| 152 | {
|
---|
| 153 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_009: [ The function shall check the Lock status and if it is not OK set the thread signal to stop. ] */
|
---|
| 154 | LogError("Could not acquire lock");
|
---|
| 155 | prov_device_handle->StopThread = 1; /*setting it even when Lock fails*/
|
---|
| 156 | }
|
---|
| 157 | else
|
---|
| 158 | {
|
---|
| 159 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_010: [ The function shall check the Lock status and if it is OK set the thread signal to stop and unlock the Lock. ] */
|
---|
| 160 | prov_device_handle->StopThread = 1;
|
---|
| 161 |
|
---|
| 162 | (void)Unlock(prov_device_handle->LockHandle);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_011: [ If there is a running worker thread the function shall call join to finish. ] */
|
---|
| 166 | if (prov_device_handle->ThreadHandle != NULL)
|
---|
| 167 | {
|
---|
| 168 | int res;
|
---|
| 169 | if (ThreadAPI_Join(prov_device_handle->ThreadHandle, &res) != THREADAPI_OK)
|
---|
| 170 | {
|
---|
| 171 | LogError("ThreadAPI_Join failed");
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_012: [ The function shall call the LL layer Prov_Device_LL_Destroy with the given handle. ] */
|
---|
| 176 | Prov_Device_LL_Destroy(prov_device_instance->ProvDeviceLLHandle);
|
---|
| 177 |
|
---|
| 178 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_013: [ The function shall free the Lock resource with de-init. ] */
|
---|
| 179 | Lock_Deinit(prov_device_instance->LockHandle);
|
---|
| 180 |
|
---|
| 181 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_014: [ The function shall free the device handle resource. ] */
|
---|
| 182 | free(prov_device_instance);
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | PROV_DEVICE_RESULT Prov_Device_Register_Device(PROV_DEVICE_HANDLE prov_device_handle, PROV_DEVICE_CLIENT_REGISTER_DEVICE_CALLBACK register_callback, void* user_context, PROV_DEVICE_CLIENT_REGISTER_STATUS_CALLBACK register_status_callback, void* status_user_context)
|
---|
| 187 | {
|
---|
| 188 | PROV_DEVICE_RESULT result;
|
---|
| 189 |
|
---|
| 190 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_015: [ If the prov_device_handle or register_callback input parameter is NULL `Prov_Device_Register_Device` shall return with invalid argument error. ] */
|
---|
| 191 | if (prov_device_handle == NULL || register_callback == NULL)
|
---|
| 192 | {
|
---|
| 193 | LogError("Invalid parameter specified prov_device_handle: %p, register_callback: %p", prov_device_handle, register_callback);
|
---|
| 194 | result = PROV_DEVICE_RESULT_INVALID_ARG;
|
---|
| 195 | }
|
---|
| 196 | else
|
---|
| 197 | {
|
---|
| 198 | PROV_DEVICE_INSTANCE* prov_device_instance = (PROV_DEVICE_INSTANCE*)prov_device_handle;
|
---|
| 199 |
|
---|
| 200 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_018: [ The function shall try to lock the Lock. ] */
|
---|
| 201 | if (Lock(prov_device_instance->LockHandle) != LOCK_OK)
|
---|
| 202 | {
|
---|
| 203 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_019: [ If the locking failed the function shall return with error. ] */
|
---|
| 204 | LogError("Could not acquire lock");
|
---|
| 205 | result = PROV_DEVICE_RESULT_ERROR;
|
---|
| 206 | }
|
---|
| 207 | else
|
---|
| 208 | {
|
---|
| 209 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_020: [ The function shall call the LL layer Prov_Device_LL_Register_Device with the given parameters and return with the result. ] */
|
---|
| 210 | result = Prov_Device_LL_Register_Device(prov_device_instance->ProvDeviceLLHandle, register_callback, user_context, register_status_callback, status_user_context);
|
---|
| 211 |
|
---|
| 212 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_021: [ The function shall unlock the Lock. ] */
|
---|
| 213 | (void)Unlock(prov_device_instance->LockHandle);
|
---|
| 214 |
|
---|
| 215 | if (result == PROV_DEVICE_RESULT_OK)
|
---|
| 216 | {
|
---|
| 217 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_016: [ The function shall start a worker thread with the device instance. ] */
|
---|
| 218 | if ((result = StartWorkerThreadIfNeeded(prov_device_instance)) != PROV_DEVICE_RESULT_OK)
|
---|
| 219 | {
|
---|
| 220 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_017: [ If the thread initialization failed the function shall return error. ] */
|
---|
| 221 | LogError("Could not start worker thread");
|
---|
| 222 | result = PROV_DEVICE_RESULT_ERROR;
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | return result;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | PROV_DEVICE_RESULT Prov_Device_SetOption(PROV_DEVICE_HANDLE prov_device_handle, const char* optionName, const void* value)
|
---|
| 232 | {
|
---|
| 233 | PROV_DEVICE_RESULT result;
|
---|
| 234 |
|
---|
| 235 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_022: [ If any of the input parameter is NULL `Prov_Device_SetOption` shall return with invalid argument error. ] */
|
---|
| 236 | if (
|
---|
| 237 | (prov_device_handle == NULL) ||
|
---|
| 238 | (optionName == NULL) ||
|
---|
| 239 | (value == NULL)
|
---|
| 240 | )
|
---|
| 241 | {
|
---|
| 242 | result = PROV_DEVICE_RESULT_INVALID_ARG;
|
---|
| 243 | LogError("Invalid parameter specified prov_device_handle: %p, optionName: %p, value: %p", prov_device_handle, optionName, value);
|
---|
| 244 | }
|
---|
| 245 | else
|
---|
| 246 | {
|
---|
| 247 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_023: [ The function shall call the LL layer Prov_Device_LL_SetOption with the given parameters and return with the result. ] */
|
---|
| 248 | PROV_DEVICE_INSTANCE* prov_device_instance = (PROV_DEVICE_INSTANCE*)prov_device_handle;
|
---|
| 249 |
|
---|
| 250 | /* Codes_SRS_PROV_DEVICE_CLIENT_41_002: [ `Prov_Device_SetOption` shall be made thread-safe by using the lock created in `Prov_Device_Create`. ] */
|
---|
| 251 | if (Lock(prov_device_instance->LockHandle) != LOCK_OK)
|
---|
| 252 | {
|
---|
| 253 | /* Codes_SRS_PROV_DEVICE_CLIENT_41_003: [ If acquiring the lock fails, `Prov_Device_SetOption` shall return `IOTHUB_CLIENT_ERROR`. ] */
|
---|
| 254 | result = PROV_DEVICE_RESULT_ERROR;
|
---|
| 255 | LogError("Could not acquire lock");
|
---|
| 256 | }
|
---|
| 257 | else
|
---|
| 258 | {
|
---|
| 259 | /* Codes_SRS_PROV_DEVICE_CLIENT_41_001: [ If parameter `optionName` is `OPTION_DO_WORK_FREQUENCY_IN_MS` then `IoTHubClientCore_SetOption` shall set `do_work_freq_ms` parameter of `prov_device_instance` ] */
|
---|
[464] | 260 | if (strcmp(PROV_OPTION_DO_WORK_FREQUENCY_IN_MS, optionName) == 0)
|
---|
[457] | 261 | {
|
---|
| 262 | prov_device_instance->do_work_freq_ms = *((uint16_t *)value);
|
---|
| 263 | result = PROV_DEVICE_RESULT_OK;
|
---|
| 264 | }
|
---|
| 265 | else
|
---|
| 266 | {
|
---|
| 267 | result = Prov_Device_LL_SetOption(prov_device_instance->ProvDeviceLLHandle, optionName, value);
|
---|
| 268 | if (result != PROV_DEVICE_RESULT_OK)
|
---|
| 269 | {
|
---|
| 270 | LogError("Prov_Device_LL_SetOption failed");
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | (void)Unlock(prov_device_instance->LockHandle);
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 | return result;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | const char* Prov_Device_GetVersionString(void)
|
---|
| 280 | {
|
---|
| 281 | /* Codes_SRS_PROV_DEVICE_CLIENT_12_024: [ The function shall call the LL layer Prov_Device_LL_GetVersionString and return with the result. ] */
|
---|
| 282 | return Prov_Device_LL_GetVersionString();
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | PROV_DEVICE_RESULT Prov_Device_Set_Provisioning_Payload(PROV_DEVICE_HANDLE handle, const char* json)
|
---|
| 286 | {
|
---|
| 287 | PROV_DEVICE_RESULT result;
|
---|
| 288 | if (handle == NULL)
|
---|
| 289 | {
|
---|
| 290 | LogError("Invalid parameter specified handle: %p", handle);
|
---|
| 291 | result = PROV_DEVICE_RESULT_INVALID_ARG;
|
---|
| 292 | }
|
---|
| 293 | else
|
---|
| 294 | {
|
---|
| 295 | result = Prov_Device_LL_Set_Provisioning_Payload(handle->ProvDeviceLLHandle, json);
|
---|
| 296 | }
|
---|
| 297 | return result;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | const char* Prov_Device_Get_Provisioning_Payload(PROV_DEVICE_HANDLE handle)
|
---|
| 301 | {
|
---|
| 302 | const char* result;
|
---|
| 303 | if (handle == NULL)
|
---|
| 304 | {
|
---|
| 305 | LogError("Invalid parameter specified handle: %p", handle);
|
---|
| 306 | result = NULL;
|
---|
| 307 | }
|
---|
| 308 | else
|
---|
| 309 | {
|
---|
| 310 | result = Prov_Device_LL_Get_Provisioning_Payload(handle->ProvDeviceLLHandle);
|
---|
| 311 | }
|
---|
| 312 | return result;
|
---|
| 313 | }
|
---|
| 314 |
|
---|