Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Fri Mar 10 11:48:28 2017 -0800
Revision:
22:10640b226104
Parent:
19:2e0811512ceb
1.1.9

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:fa2de1b79154 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:fa2de1b79154 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:fa2de1b79154 3
Azure.IoT Build 0:fa2de1b79154 4 #include "azure_c_shared_utility/lock.h"
Azure.IoT Build 6:c55b013dfc2a 5 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 6 #include "rtos.h"
Azure.IoT Build 0:fa2de1b79154 7
Azure.IoT Build 0:fa2de1b79154 8
Azure.IoT Build 0:fa2de1b79154 9 LOCK_HANDLE Lock_Init(void)
Azure.IoT Build 0:fa2de1b79154 10 {
AzureIoTClient 22:10640b226104 11 /* Codes_SRS_LOCK_10_002: [Lock_Init on success shall return a valid lock handle which should be a non NULL value] */
AzureIoTClient 22:10640b226104 12 /* Codes_SRS_LOCK_10_003: [Lock_Init on error shall return NULL] */
AzureIoTClient 22:10640b226104 13 Mutex* result = new Mutex();
AzureIoTClient 22:10640b226104 14 if (result == NULL)
AzureIoTClient 22:10640b226104 15 {
AzureIoTClient 22:10640b226104 16 LogError("Failed to instantiate a new Mutex object.");
AzureIoTClient 22:10640b226104 17 }
AzureIoTClient 22:10640b226104 18
AzureIoTClient 22:10640b226104 19 return (LOCK_HANDLE)result;
Azure.IoT Build 0:fa2de1b79154 20 }
Azure.IoT Build 0:fa2de1b79154 21
Azure.IoT Build 0:fa2de1b79154 22 LOCK_RESULT Lock(LOCK_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 23 {
Azure.IoT Build 0:fa2de1b79154 24 LOCK_RESULT result;
Azure.IoT Build 0:fa2de1b79154 25 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 26 {
AzureIoTClient 22:10640b226104 27 /* Codes_SRS_LOCK_10_007: [Lock on NULL handle passed returns LOCK_ERROR] */
AzureIoTClient 22:10640b226104 28 LogError("Invalid argument; handle is NULL.");
Azure.IoT Build 0:fa2de1b79154 29 result = LOCK_ERROR;
Azure.IoT Build 0:fa2de1b79154 30 }
Azure.IoT Build 0:fa2de1b79154 31 else
Azure.IoT Build 0:fa2de1b79154 32 {
Azure.IoT Build 0:fa2de1b79154 33 Mutex* lock_mtx = (Mutex*)handle;
Azure.IoT Build 0:fa2de1b79154 34 if (lock_mtx->lock() == osOK)
Azure.IoT Build 0:fa2de1b79154 35 {
AzureIoTClient 22:10640b226104 36 /* Codes_SRS_LOCK_10_005: [Lock on success shall return LOCK_OK] */
Azure.IoT Build 0:fa2de1b79154 37 result = LOCK_OK;
Azure.IoT Build 0:fa2de1b79154 38 }
Azure.IoT Build 0:fa2de1b79154 39 else
Azure.IoT Build 0:fa2de1b79154 40 {
AzureIoTClient 22:10640b226104 41 /* Codes_SRS_LOCK_10_006: [Lock on error shall return LOCK_ERROR] */
AzureIoTClient 22:10640b226104 42 LogError("Mutex(%p)->lock() failed.", handle);
Azure.IoT Build 0:fa2de1b79154 43 result = LOCK_ERROR;
Azure.IoT Build 0:fa2de1b79154 44 }
Azure.IoT Build 0:fa2de1b79154 45 }
AzureIoTClient 22:10640b226104 46
Azure.IoT Build 0:fa2de1b79154 47 return result;
Azure.IoT Build 0:fa2de1b79154 48 }
AzureIoTClient 22:10640b226104 49
Azure.IoT Build 0:fa2de1b79154 50 LOCK_RESULT Unlock(LOCK_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 51 {
Azure.IoT Build 0:fa2de1b79154 52 LOCK_RESULT result;
Azure.IoT Build 0:fa2de1b79154 53 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 54 {
AzureIoTClient 22:10640b226104 55 /* Codes_SRS_LOCK_10_007: [Unlock on NULL handle passed returns LOCK_ERROR] */
AzureIoTClient 22:10640b226104 56 LogError("Invalid argument; handle is NULL.");
Azure.IoT Build 0:fa2de1b79154 57 result = LOCK_ERROR;
Azure.IoT Build 0:fa2de1b79154 58 }
Azure.IoT Build 0:fa2de1b79154 59 else
Azure.IoT Build 0:fa2de1b79154 60 {
Azure.IoT Build 0:fa2de1b79154 61 Mutex* lock_mtx = (Mutex*)handle;
Azure.IoT Build 0:fa2de1b79154 62 if (lock_mtx->unlock() == osOK)
Azure.IoT Build 0:fa2de1b79154 63 {
AzureIoTClient 22:10640b226104 64 /* Codes_SRS_LOCK_10_009: [Unlock on success shall return LOCK_OK] */
Azure.IoT Build 0:fa2de1b79154 65 result = LOCK_OK;
Azure.IoT Build 0:fa2de1b79154 66 }
Azure.IoT Build 0:fa2de1b79154 67 else
Azure.IoT Build 0:fa2de1b79154 68 {
AzureIoTClient 22:10640b226104 69 /* Codes_SRS_LOCK_10_010: [Unlock on error shall return LOCK_ERROR] */
AzureIoTClient 22:10640b226104 70 LogError("Mutex(%p)->unlock() failed.", handle);
Azure.IoT Build 0:fa2de1b79154 71 result = LOCK_ERROR;
Azure.IoT Build 0:fa2de1b79154 72 }
Azure.IoT Build 0:fa2de1b79154 73 }
AzureIoTClient 22:10640b226104 74
Azure.IoT Build 0:fa2de1b79154 75 return result;
Azure.IoT Build 0:fa2de1b79154 76 }
Azure.IoT Build 0:fa2de1b79154 77
Azure.IoT Build 0:fa2de1b79154 78 LOCK_RESULT Lock_Deinit(LOCK_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 79 {
AzureIoTClient 22:10640b226104 80 LOCK_RESULT result;
Azure.IoT Build 0:fa2de1b79154 81 if (NULL == handle)
Azure.IoT Build 0:fa2de1b79154 82 {
AzureIoTClient 22:10640b226104 83 /* Codes_SRS_LOCK_10_007: [Lock_Deinit on NULL handle passed returns LOCK_ERROR] */
AzureIoTClient 22:10640b226104 84 LogError("Invalid argument; handle is NULL.");
Azure.IoT Build 0:fa2de1b79154 85 result = LOCK_ERROR;
Azure.IoT Build 0:fa2de1b79154 86 }
Azure.IoT Build 0:fa2de1b79154 87 else
Azure.IoT Build 0:fa2de1b79154 88 {
AzureIoTClient 22:10640b226104 89 /* Codes_SRS_LOCK_10_012: [Lock_Deinit frees the memory pointed by handle] */
Azure.IoT Build 0:fa2de1b79154 90 Mutex* lock_mtx = (Mutex*)handle;
Azure.IoT Build 0:fa2de1b79154 91 delete lock_mtx;
AzureIoTClient 22:10640b226104 92 result = LOCK_OK;
Azure.IoT Build 0:fa2de1b79154 93 }
Azure.IoT Build 0:fa2de1b79154 94
Azure.IoT Build 0:fa2de1b79154 95 return result;
Azure.IoT Build 0:fa2de1b79154 96 }