A modelling and serializer library for Microsoft Azure IoTHub client applications

Dependents:   sht15_remote_monitoring f767zi_mqtt remote_monitoring simplesample_amqp ... more

This library implements a serializer library to be used in projects involving Microsoft Azure IoT Hub connectivity. The code is replicated from https://github.com/Azure/azure-iot-sdks

Committer:
AzureIoTClient
Date:
Wed Nov 16 21:38:26 2016 -0800
Revision:
17:fa1bba4c6053
Parent:
13:16e88f0cfa5f
Child:
18:58b667752399
1.0.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 0:1f9b2707ec7d 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 0:1f9b2707ec7d 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 0:1f9b2707ec7d 3
AzureIoTClient 0:1f9b2707ec7d 4 #include <stdlib.h>
AzureIoTClient 0:1f9b2707ec7d 5 #ifdef _CRTDBG_MAP_ALLOC
AzureIoTClient 0:1f9b2707ec7d 6 #include <crtdbg.h>
AzureIoTClient 0:1f9b2707ec7d 7 #endif
Azure.IoT Build 10:c2aee3965a83 8 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 0:1f9b2707ec7d 9
AzureIoTClient 0:1f9b2707ec7d 10 #include <stdbool.h>
AzureIoTClient 0:1f9b2707ec7d 11 #include "iotdevice.h"
AzureIoTClient 0:1f9b2707ec7d 12 #include "datapublisher.h"
AzureIoTClient 0:1f9b2707ec7d 13 #include "commanddecoder.h"
Azure.IoT Build 10:c2aee3965a83 14 #include "azure_c_shared_utility/crt_abstractions.h"
Azure.IoT Build 13:16e88f0cfa5f 15 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 0:1f9b2707ec7d 16
AzureIoTClient 0:1f9b2707ec7d 17 #define LOG_DEVICE_ERROR \
AzureIoTClient 11:b1327861f5e0 18 LogError("(result = %s)", ENUM_TO_STRING(DEVICE_RESULT, result))
AzureIoTClient 0:1f9b2707ec7d 19
AzureIoTClient 17:fa1bba4c6053 20
AzureIoTClient 17:fa1bba4c6053 21
AzureIoTClient 17:fa1bba4c6053 22 typedef struct DEVICE_HANDLE_DATA_TAG
AzureIoTClient 0:1f9b2707ec7d 23 {
AzureIoTClient 0:1f9b2707ec7d 24 SCHEMA_MODEL_TYPE_HANDLE model;
AzureIoTClient 0:1f9b2707ec7d 25 DATA_PUBLISHER_HANDLE dataPublisherHandle;
AzureIoTClient 17:fa1bba4c6053 26 pfDeviceActionCallback deviceActionCallback;
AzureIoTClient 0:1f9b2707ec7d 27 void* callbackUserContext;
AzureIoTClient 0:1f9b2707ec7d 28 COMMAND_DECODER_HANDLE commandDecoderHandle;
AzureIoTClient 17:fa1bba4c6053 29 } DEVICE_HANDLE_DATA;
AzureIoTClient 0:1f9b2707ec7d 30
AzureIoTClient 0:1f9b2707ec7d 31 DEFINE_ENUM_STRINGS(DEVICE_RESULT, DEVICE_RESULT_VALUES);
AzureIoTClient 0:1f9b2707ec7d 32
AzureIoTClient 0:1f9b2707ec7d 33 static EXECUTE_COMMAND_RESULT DeviceInvokeAction(void* actionCallbackContext, const char* relativeActionPath, const char* actionName, size_t argCount, const AGENT_DATA_TYPE* args)
AzureIoTClient 0:1f9b2707ec7d 34 {
AzureIoTClient 0:1f9b2707ec7d 35 EXECUTE_COMMAND_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 36
AzureIoTClient 0:1f9b2707ec7d 37 /*Codes_SRS_DEVICE_02_011: [If the parameter actionCallbackContent passed the callback is NULL then the callback shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 0:1f9b2707ec7d 38 if (actionCallbackContext == NULL)
AzureIoTClient 0:1f9b2707ec7d 39 {
AzureIoTClient 0:1f9b2707ec7d 40 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 11:b1327861f5e0 41 LogError("(Error code = %s)", ENUM_TO_STRING(DEVICE_RESULT, DEVICE_INVALID_ARG));
AzureIoTClient 0:1f9b2707ec7d 42 }
AzureIoTClient 0:1f9b2707ec7d 43 else
AzureIoTClient 0:1f9b2707ec7d 44 {
AzureIoTClient 17:fa1bba4c6053 45 DEVICE_HANDLE_DATA* device = (DEVICE_HANDLE_DATA*)actionCallbackContext;
AzureIoTClient 0:1f9b2707ec7d 46
AzureIoTClient 0:1f9b2707ec7d 47 /* Codes_SRS_DEVICE_01_052: [When the action callback passed to CommandDecoder is called, Device shall call the appropriate user callback associated with the device handle.] */
AzureIoTClient 0:1f9b2707ec7d 48 /* Codes_SRS_DEVICE_01_055: [The value passed in callbackUserContext when creating the device shall be passed to the callback as the value for the callbackUserContext argument.] */
AzureIoTClient 0:1f9b2707ec7d 49 result = device->deviceActionCallback((DEVICE_HANDLE)device, device->callbackUserContext, relativeActionPath, actionName, argCount, args);
AzureIoTClient 0:1f9b2707ec7d 50 }
AzureIoTClient 0:1f9b2707ec7d 51
AzureIoTClient 0:1f9b2707ec7d 52 return result;
AzureIoTClient 0:1f9b2707ec7d 53 }
AzureIoTClient 0:1f9b2707ec7d 54
AzureIoTClient 17:fa1bba4c6053 55 DEVICE_RESULT Device_Create(SCHEMA_MODEL_TYPE_HANDLE modelHandle, pfDeviceActionCallback deviceActionCallback, void* callbackUserContext, bool includePropertyPath, DEVICE_HANDLE* deviceHandle)
AzureIoTClient 0:1f9b2707ec7d 56 {
AzureIoTClient 0:1f9b2707ec7d 57 DEVICE_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 58
AzureIoTClient 0:1f9b2707ec7d 59 /* Codes_SRS_DEVICE_05_014: [If any of the modelHandle, deviceHandle or deviceActionCallback arguments are NULL, Device_Create shall return DEVICE_INVALID_ARG.]*/
AzureIoTClient 0:1f9b2707ec7d 60 if (modelHandle == NULL || deviceHandle == NULL || deviceActionCallback == NULL )
AzureIoTClient 0:1f9b2707ec7d 61 {
AzureIoTClient 0:1f9b2707ec7d 62 result = DEVICE_INVALID_ARG;
AzureIoTClient 0:1f9b2707ec7d 63 LOG_DEVICE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 64 }
AzureIoTClient 0:1f9b2707ec7d 65 else
AzureIoTClient 0:1f9b2707ec7d 66 {
AzureIoTClient 17:fa1bba4c6053 67 DEVICE_HANDLE_DATA* device = (DEVICE_HANDLE_DATA*)malloc(sizeof(DEVICE_HANDLE_DATA));
AzureIoTClient 0:1f9b2707ec7d 68 if (device == NULL)
AzureIoTClient 0:1f9b2707ec7d 69 {
AzureIoTClient 0:1f9b2707ec7d 70 /* Codes_SRS_DEVICE_05_015: [If an error occurs while trying to create the device, Device_Create shall return DEVICE_ERROR.] */
AzureIoTClient 0:1f9b2707ec7d 71 result = DEVICE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 72 LOG_DEVICE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 73 }
AzureIoTClient 0:1f9b2707ec7d 74 else
AzureIoTClient 0:1f9b2707ec7d 75 {
AzureIoTClient 0:1f9b2707ec7d 76 /* Codes_SRS_DEVICE_01_018: [Device_Create shall create a DataPublisher instance by calling DataPublisher_Create.] */
AzureIoTClient 0:1f9b2707ec7d 77 /* Codes_SRS_DEVICE_01_020: [Device_Create shall pass to DataPublisher_Create the FrontDoor instance obtained earlier.] */
AzureIoTClient 0:1f9b2707ec7d 78 /* Codes_SRS_DEVICE_01_004: [DeviceCreate shall pass to DataPublisher_create the includePropertyPath argument.] */
AzureIoTClient 0:1f9b2707ec7d 79 if ((device->dataPublisherHandle = DataPublisher_Create(modelHandle, includePropertyPath)) == NULL)
AzureIoTClient 0:1f9b2707ec7d 80 {
AzureIoTClient 0:1f9b2707ec7d 81 free(device);
AzureIoTClient 0:1f9b2707ec7d 82
AzureIoTClient 0:1f9b2707ec7d 83 /* Codes_SRS_DEVICE_01_019: [If creating the DataPublisher instance fails, Device_Create shall return DEVICE_DATA_PUBLISHER_FAILED.] */
AzureIoTClient 0:1f9b2707ec7d 84 result = DEVICE_DATA_PUBLISHER_FAILED;
AzureIoTClient 0:1f9b2707ec7d 85 LOG_DEVICE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 86 }
AzureIoTClient 0:1f9b2707ec7d 87 else
AzureIoTClient 0:1f9b2707ec7d 88 {
AzureIoTClient 0:1f9b2707ec7d 89 device->model = modelHandle;
AzureIoTClient 0:1f9b2707ec7d 90 device->deviceActionCallback = deviceActionCallback;
AzureIoTClient 0:1f9b2707ec7d 91 device->callbackUserContext = callbackUserContext;
AzureIoTClient 0:1f9b2707ec7d 92
AzureIoTClient 0:1f9b2707ec7d 93 /* Codes_SRS_DEVICE_01_001: [Device_Create shall create a CommandDecoder instance by calling CommandDecoder_Create and passing to it the model handle.] */
AzureIoTClient 0:1f9b2707ec7d 94 /* Codes_SRS_DEVICE_01_002: [Device_Create shall also pass to CommandDecoder_Create a callback to be invoked when a command is received and a context that shall be the device handle.] */
AzureIoTClient 0:1f9b2707ec7d 95 if ((device->commandDecoderHandle = CommandDecoder_Create(modelHandle, DeviceInvokeAction, device)) == NULL)
AzureIoTClient 0:1f9b2707ec7d 96 {
AzureIoTClient 17:fa1bba4c6053 97 DataPublisher_Destroy(device->dataPublisherHandle);
AzureIoTClient 0:1f9b2707ec7d 98 free(device);
AzureIoTClient 0:1f9b2707ec7d 99
AzureIoTClient 0:1f9b2707ec7d 100 /* Codes_SRS_DEVICE_01_003: [If CommandDecoder_Create fails, Device_Create shall return DEVICE_COMMAND_DECODER_FAILED.] */
AzureIoTClient 0:1f9b2707ec7d 101 result = DEVICE_COMMAND_DECODER_FAILED;
AzureIoTClient 0:1f9b2707ec7d 102 LOG_DEVICE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 103 }
AzureIoTClient 0:1f9b2707ec7d 104 else
AzureIoTClient 0:1f9b2707ec7d 105 {
AzureIoTClient 0:1f9b2707ec7d 106 /* Codes_SRS_DEVICE_03_003: [The DEVICE_HANDLE shall be provided via the deviceHandle out argument.] */
AzureIoTClient 0:1f9b2707ec7d 107 *deviceHandle = (DEVICE_HANDLE)device;
AzureIoTClient 0:1f9b2707ec7d 108 /* Codes_SRS_DEVICE_03_004: [Device_Create shall return DEVICE_OK upon success.] */
AzureIoTClient 0:1f9b2707ec7d 109 result = DEVICE_OK;
AzureIoTClient 0:1f9b2707ec7d 110 }
AzureIoTClient 0:1f9b2707ec7d 111 }
AzureIoTClient 0:1f9b2707ec7d 112 }
AzureIoTClient 0:1f9b2707ec7d 113 }
AzureIoTClient 0:1f9b2707ec7d 114
AzureIoTClient 0:1f9b2707ec7d 115 return result;
AzureIoTClient 0:1f9b2707ec7d 116 }
AzureIoTClient 0:1f9b2707ec7d 117
AzureIoTClient 0:1f9b2707ec7d 118 /* Codes_SRS_DEVICE_03_006: [Device_Destroy shall free all resources associated with a device.] */
AzureIoTClient 0:1f9b2707ec7d 119 void Device_Destroy(DEVICE_HANDLE deviceHandle)
AzureIoTClient 0:1f9b2707ec7d 120 {
AzureIoTClient 0:1f9b2707ec7d 121 /* Codes_SRS_DEVICE_03_007: [Device_Destroy will not do anything if deviceHandle is NULL.] */
AzureIoTClient 0:1f9b2707ec7d 122 if (deviceHandle != NULL)
AzureIoTClient 0:1f9b2707ec7d 123 {
AzureIoTClient 17:fa1bba4c6053 124 DEVICE_HANDLE_DATA* device = (DEVICE_HANDLE_DATA*)deviceHandle;
AzureIoTClient 0:1f9b2707ec7d 125
AzureIoTClient 0:1f9b2707ec7d 126 DataPublisher_Destroy(device->dataPublisherHandle);
AzureIoTClient 0:1f9b2707ec7d 127 CommandDecoder_Destroy(device->commandDecoderHandle);
AzureIoTClient 0:1f9b2707ec7d 128
AzureIoTClient 0:1f9b2707ec7d 129 free(device);
AzureIoTClient 0:1f9b2707ec7d 130 }
AzureIoTClient 0:1f9b2707ec7d 131 }
AzureIoTClient 0:1f9b2707ec7d 132
AzureIoTClient 0:1f9b2707ec7d 133 TRANSACTION_HANDLE Device_StartTransaction(DEVICE_HANDLE deviceHandle)
AzureIoTClient 0:1f9b2707ec7d 134 {
AzureIoTClient 0:1f9b2707ec7d 135 TRANSACTION_HANDLE result;
AzureIoTClient 0:1f9b2707ec7d 136
AzureIoTClient 0:1f9b2707ec7d 137 /* Codes_SRS_DEVICE_01_035: [If any argument is NULL, Device_StartTransaction shall return NULL.] */
AzureIoTClient 0:1f9b2707ec7d 138 if (deviceHandle == NULL)
AzureIoTClient 0:1f9b2707ec7d 139 {
AzureIoTClient 0:1f9b2707ec7d 140 result = NULL;
AzureIoTClient 11:b1327861f5e0 141 LogError("(Error code = %s)", ENUM_TO_STRING(DEVICE_RESULT, DEVICE_INVALID_ARG));
AzureIoTClient 0:1f9b2707ec7d 142 }
AzureIoTClient 0:1f9b2707ec7d 143 else
AzureIoTClient 0:1f9b2707ec7d 144 {
AzureIoTClient 17:fa1bba4c6053 145 DEVICE_HANDLE_DATA* deviceInstance = (DEVICE_HANDLE_DATA*)deviceHandle;
AzureIoTClient 0:1f9b2707ec7d 146
AzureIoTClient 0:1f9b2707ec7d 147 /* Codes_SRS_DEVICE_01_034: [Device_StartTransaction shall invoke DataPublisher_StartTransaction for the DataPublisher handle associated with the deviceHandle argument.] */
AzureIoTClient 0:1f9b2707ec7d 148 /* Codes_SRS_DEVICE_01_043: [On success, Device_StartTransaction shall return a non NULL handle.] */
AzureIoTClient 0:1f9b2707ec7d 149 /* Codes_SRS_DEVICE_01_048: [When DataPublisher_StartTransaction fails, Device_StartTransaction shall return NULL.] */
AzureIoTClient 0:1f9b2707ec7d 150 result = DataPublisher_StartTransaction(deviceInstance->dataPublisherHandle);
AzureIoTClient 0:1f9b2707ec7d 151 }
AzureIoTClient 0:1f9b2707ec7d 152
AzureIoTClient 0:1f9b2707ec7d 153 return result;
AzureIoTClient 0:1f9b2707ec7d 154 }
AzureIoTClient 0:1f9b2707ec7d 155
AzureIoTClient 0:1f9b2707ec7d 156 DEVICE_RESULT Device_PublishTransacted(TRANSACTION_HANDLE transactionHandle, const char* propertyPath, const AGENT_DATA_TYPE* data)
AzureIoTClient 0:1f9b2707ec7d 157 {
AzureIoTClient 0:1f9b2707ec7d 158 DEVICE_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 159
AzureIoTClient 0:1f9b2707ec7d 160 /* Codes_SRS_DEVICE_01_037: [If any argument is NULL, Device_PublishTransacted shall return DEVICE_INVALID_ARG.] */
AzureIoTClient 17:fa1bba4c6053 161 if (
AzureIoTClient 17:fa1bba4c6053 162 (transactionHandle == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 163 (propertyPath == NULL) ||
AzureIoTClient 17:fa1bba4c6053 164 (data == NULL)
AzureIoTClient 17:fa1bba4c6053 165 )
AzureIoTClient 0:1f9b2707ec7d 166 {
AzureIoTClient 0:1f9b2707ec7d 167 result = DEVICE_INVALID_ARG;
AzureIoTClient 0:1f9b2707ec7d 168 LOG_DEVICE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 169 }
AzureIoTClient 0:1f9b2707ec7d 170 /* Codes_SRS_DEVICE_01_036: [Device_PublishTransacted shall invoke DataPublisher_PublishTransacted.] */
AzureIoTClient 0:1f9b2707ec7d 171 else if (DataPublisher_PublishTransacted(transactionHandle, propertyPath, data) != DATA_PUBLISHER_OK)
AzureIoTClient 0:1f9b2707ec7d 172 {
AzureIoTClient 0:1f9b2707ec7d 173 /* Codes_SRS_DEVICE_01_049: [When DataPublisher_PublishTransacted fails, Device_PublishTransacted shall return DEVICE_DATA_PUBLISHER_FAILED.] */
AzureIoTClient 0:1f9b2707ec7d 174 result = DEVICE_DATA_PUBLISHER_FAILED;
AzureIoTClient 0:1f9b2707ec7d 175 LOG_DEVICE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 176 }
AzureIoTClient 0:1f9b2707ec7d 177 else
AzureIoTClient 0:1f9b2707ec7d 178 {
AzureIoTClient 0:1f9b2707ec7d 179 /* Codes_SRS_DEVICE_01_044: [On success, Device_PublishTransacted shall return DEVICE_OK.] */
AzureIoTClient 0:1f9b2707ec7d 180 result = DEVICE_OK;
AzureIoTClient 0:1f9b2707ec7d 181 }
AzureIoTClient 0:1f9b2707ec7d 182
AzureIoTClient 0:1f9b2707ec7d 183 return result;
AzureIoTClient 0:1f9b2707ec7d 184 }
AzureIoTClient 0:1f9b2707ec7d 185
AzureIoTClient 0:1f9b2707ec7d 186 DEVICE_RESULT Device_EndTransaction(TRANSACTION_HANDLE transactionHandle, unsigned char** destination, size_t* destinationSize)
AzureIoTClient 0:1f9b2707ec7d 187 {
AzureIoTClient 0:1f9b2707ec7d 188 DEVICE_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 189
AzureIoTClient 0:1f9b2707ec7d 190 /* Codes_SRS_DEVICE_01_039: [If any parameter is NULL, Device_EndTransaction shall return DEVICE_INVALID_ARG.]*/
AzureIoTClient 0:1f9b2707ec7d 191 if (
AzureIoTClient 0:1f9b2707ec7d 192 (transactionHandle == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 193 (destination == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 194 (destinationSize == NULL)
AzureIoTClient 0:1f9b2707ec7d 195 )
AzureIoTClient 0:1f9b2707ec7d 196 {
AzureIoTClient 0:1f9b2707ec7d 197 result = DEVICE_INVALID_ARG;
AzureIoTClient 0:1f9b2707ec7d 198 LOG_DEVICE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 199 }
AzureIoTClient 0:1f9b2707ec7d 200 /* Codes_SRS_DEVICE_01_038: [Device_EndTransaction shall invoke DataPublisher_EndTransaction.] */
AzureIoTClient 0:1f9b2707ec7d 201 else if (DataPublisher_EndTransaction(transactionHandle, destination, destinationSize) != DATA_PUBLISHER_OK)
AzureIoTClient 0:1f9b2707ec7d 202 {
AzureIoTClient 0:1f9b2707ec7d 203 /* Codes_SRS_DEVICE_01_050: [When DataPublisher_EndTransaction fails, Device_EndTransaction shall return DEVICE_DATA_PUBLISHER_FAILED.] */
AzureIoTClient 0:1f9b2707ec7d 204 result = DEVICE_DATA_PUBLISHER_FAILED;
AzureIoTClient 0:1f9b2707ec7d 205 LOG_DEVICE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 206 }
AzureIoTClient 0:1f9b2707ec7d 207 else
AzureIoTClient 0:1f9b2707ec7d 208 {
AzureIoTClient 0:1f9b2707ec7d 209 /* Codes_SRS_DEVICE_01_045: [On success, Device_EndTransaction shall return DEVICE_OK.] */
AzureIoTClient 0:1f9b2707ec7d 210 result = DEVICE_OK;
AzureIoTClient 0:1f9b2707ec7d 211 }
AzureIoTClient 0:1f9b2707ec7d 212
AzureIoTClient 0:1f9b2707ec7d 213 return result;
AzureIoTClient 0:1f9b2707ec7d 214 }
AzureIoTClient 0:1f9b2707ec7d 215
AzureIoTClient 0:1f9b2707ec7d 216 DEVICE_RESULT Device_CancelTransaction(TRANSACTION_HANDLE transactionHandle)
AzureIoTClient 0:1f9b2707ec7d 217 {
AzureIoTClient 0:1f9b2707ec7d 218 DEVICE_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 219
AzureIoTClient 0:1f9b2707ec7d 220 /* Codes_SRS_DEVICE_01_041: [If any argument is NULL, Device_CancelTransaction shall return DEVICE_INVALID_ARG.] */
AzureIoTClient 0:1f9b2707ec7d 221 if (transactionHandle == NULL)
AzureIoTClient 0:1f9b2707ec7d 222 {
AzureIoTClient 0:1f9b2707ec7d 223 result = DEVICE_INVALID_ARG;
AzureIoTClient 0:1f9b2707ec7d 224 LOG_DEVICE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 225 }
AzureIoTClient 0:1f9b2707ec7d 226 /* Codes_SRS_DEVICE_01_040: [Device_CancelTransaction shall invoke DataPublisher_CancelTransaction.] */
AzureIoTClient 0:1f9b2707ec7d 227 else if (DataPublisher_CancelTransaction(transactionHandle) != DATA_PUBLISHER_OK)
AzureIoTClient 0:1f9b2707ec7d 228 {
AzureIoTClient 0:1f9b2707ec7d 229 /* Codes_SRS_DEVICE_01_051: [When DataPublisher_CancelTransaction fails, Device_CancelTransaction shall return DEVICE_DATA_PUBLISHER_FAILED.] */
AzureIoTClient 0:1f9b2707ec7d 230 result = DEVICE_DATA_PUBLISHER_FAILED;
AzureIoTClient 0:1f9b2707ec7d 231 LOG_DEVICE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 232 }
AzureIoTClient 0:1f9b2707ec7d 233 else
AzureIoTClient 0:1f9b2707ec7d 234 {
AzureIoTClient 0:1f9b2707ec7d 235 /* Codes_SRS_DEVICE_01_046: [On success, Device_PublishTransacted shall return DEVICE_OK.] */
AzureIoTClient 0:1f9b2707ec7d 236 result = DEVICE_OK;
AzureIoTClient 0:1f9b2707ec7d 237 }
AzureIoTClient 0:1f9b2707ec7d 238
AzureIoTClient 0:1f9b2707ec7d 239 return result;
AzureIoTClient 0:1f9b2707ec7d 240 }
AzureIoTClient 0:1f9b2707ec7d 241
AzureIoTClient 0:1f9b2707ec7d 242 EXECUTE_COMMAND_RESULT Device_ExecuteCommand(DEVICE_HANDLE deviceHandle, const char* command)
AzureIoTClient 0:1f9b2707ec7d 243 {
AzureIoTClient 0:1f9b2707ec7d 244 EXECUTE_COMMAND_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 245 /*Codes_SRS_DEVICE_02_012: [If any parameters are NULL, then Device_ExecuteCommand shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 0:1f9b2707ec7d 246 if (
AzureIoTClient 0:1f9b2707ec7d 247 (deviceHandle == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 248 (command == NULL)
AzureIoTClient 0:1f9b2707ec7d 249 )
AzureIoTClient 0:1f9b2707ec7d 250 {
AzureIoTClient 0:1f9b2707ec7d 251 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 252 LogError("invalid parameter (NULL passed to Device_ExecuteCommand DEVICE_HANDLE deviceHandle=%p, const char* command=%p", deviceHandle, command);
AzureIoTClient 0:1f9b2707ec7d 253 }
AzureIoTClient 0:1f9b2707ec7d 254 else
AzureIoTClient 0:1f9b2707ec7d 255 {
AzureIoTClient 0:1f9b2707ec7d 256 /*Codes_SRS_DEVICE_02_013: [Otherwise, Device_ExecuteCommand shall call CommandDecoder_ExecuteCommand and return what CommandDecoder_ExecuteCommand is returning.] */
AzureIoTClient 17:fa1bba4c6053 257 DEVICE_HANDLE_DATA* device = (DEVICE_HANDLE_DATA*)deviceHandle;
AzureIoTClient 0:1f9b2707ec7d 258 result = CommandDecoder_ExecuteCommand(device->commandDecoderHandle, command);
AzureIoTClient 0:1f9b2707ec7d 259 }
AzureIoTClient 0:1f9b2707ec7d 260 return result;
AzureIoTClient 0:1f9b2707ec7d 261 }
AzureIoTClient 17:fa1bba4c6053 262
AzureIoTClient 17:fa1bba4c6053 263 REPORTED_PROPERTIES_TRANSACTION_HANDLE Device_CreateTransaction_ReportedProperties(DEVICE_HANDLE deviceHandle)
AzureIoTClient 17:fa1bba4c6053 264 {
AzureIoTClient 17:fa1bba4c6053 265 REPORTED_PROPERTIES_TRANSACTION_HANDLE result;
AzureIoTClient 17:fa1bba4c6053 266
AzureIoTClient 17:fa1bba4c6053 267 /*Codes_SRS_DEVICE_02_014: [ If argument deviceHandle is NULL then Device_CreateTransaction_ReportedProperties shall fail and return NULL. ]*/
AzureIoTClient 17:fa1bba4c6053 268 if (deviceHandle == NULL)
AzureIoTClient 17:fa1bba4c6053 269 {
AzureIoTClient 17:fa1bba4c6053 270 LogError("invalid argument DEVICE_HANDLE deviceHandle=%p", deviceHandle);
AzureIoTClient 17:fa1bba4c6053 271 result = NULL;
AzureIoTClient 17:fa1bba4c6053 272 }
AzureIoTClient 17:fa1bba4c6053 273 else
AzureIoTClient 17:fa1bba4c6053 274 {
AzureIoTClient 17:fa1bba4c6053 275 DEVICE_HANDLE_DATA* deviceInstance = (DEVICE_HANDLE_DATA*)deviceHandle;
AzureIoTClient 17:fa1bba4c6053 276 /*Codes_SRS_DEVICE_02_015: [ Otherwise, Device_CreateTransaction_ReportedProperties shall call DataPublisher_CreateTransaction_ReportedProperties. ]*/
AzureIoTClient 17:fa1bba4c6053 277 /*Codes_SRS_DEVICE_02_016: [ If DataPublisher_CreateTransaction_ReportedProperties fails then Device_CreateTransaction_ReportedProperties shall fail and return NULL. ]*/
AzureIoTClient 17:fa1bba4c6053 278 /*Codes_SRS_DEVICE_02_017: [ Otherwise Device_CreateTransaction_ReportedProperties shall succeed and return a non-NULL value. ]*/
AzureIoTClient 17:fa1bba4c6053 279 result = DataPublisher_CreateTransaction_ReportedProperties(deviceInstance->dataPublisherHandle);
AzureIoTClient 17:fa1bba4c6053 280 if (result == NULL)
AzureIoTClient 17:fa1bba4c6053 281 {
AzureIoTClient 17:fa1bba4c6053 282 LogError("unable to DataPublisher_CreateTransaction_ReportedProperties");
AzureIoTClient 17:fa1bba4c6053 283 /*return as is*/
AzureIoTClient 17:fa1bba4c6053 284 }
AzureIoTClient 17:fa1bba4c6053 285 else
AzureIoTClient 17:fa1bba4c6053 286 {
AzureIoTClient 17:fa1bba4c6053 287 /*return as is*/
AzureIoTClient 17:fa1bba4c6053 288 }
AzureIoTClient 17:fa1bba4c6053 289 }
AzureIoTClient 17:fa1bba4c6053 290
AzureIoTClient 17:fa1bba4c6053 291 return result;
AzureIoTClient 17:fa1bba4c6053 292 }
AzureIoTClient 17:fa1bba4c6053 293
AzureIoTClient 17:fa1bba4c6053 294 DEVICE_RESULT Device_PublishTransacted_ReportedProperty(REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle, const char* reportedPropertyPath, const AGENT_DATA_TYPE* data)
AzureIoTClient 17:fa1bba4c6053 295 {
AzureIoTClient 17:fa1bba4c6053 296 DEVICE_RESULT result;
AzureIoTClient 17:fa1bba4c6053 297 /*Codes_SRS_DEVICE_02_018: [ If argument transactionHandle is NULL then Device_PublishTransacted_ReportedProperty shall fail and return DEVICE_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 298 /*Codes_SRS_DEVICE_02_019: [ If argument reportedPropertyPath is NULL then Device_PublishTransacted_ReportedProperty shall fail and return DEVICE_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 299 /*Codes_SRS_DEVICE_02_020: [ If argument data is NULL then Device_PublishTransacted_ReportedProperty shall fail and return DEVICE_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 300 if (
AzureIoTClient 17:fa1bba4c6053 301 (transactionHandle == NULL) ||
AzureIoTClient 17:fa1bba4c6053 302 (reportedPropertyPath == NULL) ||
AzureIoTClient 17:fa1bba4c6053 303 (data == NULL)
AzureIoTClient 17:fa1bba4c6053 304 )
AzureIoTClient 17:fa1bba4c6053 305 {
AzureIoTClient 17:fa1bba4c6053 306 LogError("invalid argument REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle=%p, const char* reportedPropertyPath=%s, const AGENT_DATA_TYPE* data=%p", transactionHandle, reportedPropertyPath, data);
AzureIoTClient 17:fa1bba4c6053 307 result = DEVICE_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 308 }
AzureIoTClient 17:fa1bba4c6053 309 else
AzureIoTClient 17:fa1bba4c6053 310 {
AzureIoTClient 17:fa1bba4c6053 311 /*Codes_SRS_DEVICE_02_021: [ Device_PublishTransacted_ReportedProperty shall call DataPublisher_PublishTransacted_ReportedProperty. ]*/
AzureIoTClient 17:fa1bba4c6053 312 DATA_PUBLISHER_RESULT r = DataPublisher_PublishTransacted_ReportedProperty(transactionHandle, reportedPropertyPath, data);
AzureIoTClient 17:fa1bba4c6053 313 if (r != DATA_PUBLISHER_OK)
AzureIoTClient 17:fa1bba4c6053 314 {
AzureIoTClient 17:fa1bba4c6053 315 LogError("unable to DataPublisher_PublishTransacted_ReportedProperty");
AzureIoTClient 17:fa1bba4c6053 316 /*Codes_SRS_DEVICE_02_022: [ If DataPublisher_PublishTransacted_ReportedProperty fails then Device_PublishTransacted_ReportedProperty shall fail and return DEVICE_DATA_PUBLISHER_FAILED. ]*/
AzureIoTClient 17:fa1bba4c6053 317 result = DEVICE_DATA_PUBLISHER_FAILED;
AzureIoTClient 17:fa1bba4c6053 318 }
AzureIoTClient 17:fa1bba4c6053 319 else
AzureIoTClient 17:fa1bba4c6053 320 {
AzureIoTClient 17:fa1bba4c6053 321 /*Codes_SRS_DEVICE_02_023: [ Otherwise, Device_PublishTransacted_ReportedProperty shall succeed and return DEVICE_OK. ]*/
AzureIoTClient 17:fa1bba4c6053 322 result = DEVICE_OK;
AzureIoTClient 17:fa1bba4c6053 323 }
AzureIoTClient 17:fa1bba4c6053 324 }
AzureIoTClient 17:fa1bba4c6053 325 return result;
AzureIoTClient 17:fa1bba4c6053 326 }
AzureIoTClient 17:fa1bba4c6053 327
AzureIoTClient 17:fa1bba4c6053 328 DEVICE_RESULT Device_CommitTransaction_ReportedProperties(REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle, unsigned char** destination, size_t* destinationSize)
AzureIoTClient 17:fa1bba4c6053 329 {
AzureIoTClient 17:fa1bba4c6053 330 DEVICE_RESULT result;
AzureIoTClient 17:fa1bba4c6053 331
AzureIoTClient 17:fa1bba4c6053 332 /*Codes_SRS_DEVICE_02_024: [ If argument transactionHandle is NULL then Device_CommitTransaction_ReportedProperties shall fail and return DEVICE_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 333 /*Codes_SRS_DEVICE_02_025: [ If argument destination is NULL then Device_CommitTransaction_ReportedProperties shall fail and return DEVICE_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 334 /*Codes_SRS_DEVICE_02_026: [ If argument destinationSize is NULL then Device_CommitTransaction_ReportedProperties shall fail and return DEVICE_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 335 if (
AzureIoTClient 17:fa1bba4c6053 336 (transactionHandle == NULL) ||
AzureIoTClient 17:fa1bba4c6053 337 (destination == NULL) ||
AzureIoTClient 17:fa1bba4c6053 338 (destinationSize == NULL)
AzureIoTClient 17:fa1bba4c6053 339 )
AzureIoTClient 17:fa1bba4c6053 340 {
AzureIoTClient 17:fa1bba4c6053 341 LogError("invalid argument REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle=%p, unsigned char** destination=%p, size_t* destinationSize=%p", transactionHandle, destination, destinationSize);
AzureIoTClient 17:fa1bba4c6053 342 result = DEVICE_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 343 }
AzureIoTClient 17:fa1bba4c6053 344 else
AzureIoTClient 17:fa1bba4c6053 345 {
AzureIoTClient 17:fa1bba4c6053 346 /*Codes_SRS_DEVICE_02_027: [ Device_CommitTransaction_ReportedProperties shall call DataPublisher_CommitTransaction_ReportedProperties. ]*/
AzureIoTClient 17:fa1bba4c6053 347 DATA_PUBLISHER_RESULT r = DataPublisher_CommitTransaction_ReportedProperties(transactionHandle, destination, destinationSize);
AzureIoTClient 17:fa1bba4c6053 348
AzureIoTClient 17:fa1bba4c6053 349 /*Codes_SRS_DEVICE_02_028: [ If DataPublisher_CommitTransaction_ReportedProperties fails then Device_CommitTransaction_ReportedProperties shall fail and return DEVICE_DATA_PUBLISHER_FAILED. ]*/
AzureIoTClient 17:fa1bba4c6053 350 if (r != DATA_PUBLISHER_OK)
AzureIoTClient 17:fa1bba4c6053 351 {
AzureIoTClient 17:fa1bba4c6053 352 LogError("unable to DataPublisher_CommitTransaction_ReportedProperties");
AzureIoTClient 17:fa1bba4c6053 353 result = DEVICE_DATA_PUBLISHER_FAILED;
AzureIoTClient 17:fa1bba4c6053 354 }
AzureIoTClient 17:fa1bba4c6053 355 else
AzureIoTClient 17:fa1bba4c6053 356 {
AzureIoTClient 17:fa1bba4c6053 357 /*Codes_SRS_DEVICE_02_029: [ Otherwise Device_CommitTransaction_ReportedProperties shall succeed and return DEVICE_OK. ]*/
AzureIoTClient 17:fa1bba4c6053 358 result = DEVICE_OK;
AzureIoTClient 17:fa1bba4c6053 359 }
AzureIoTClient 17:fa1bba4c6053 360 }
AzureIoTClient 17:fa1bba4c6053 361 return result;
AzureIoTClient 17:fa1bba4c6053 362 }
AzureIoTClient 17:fa1bba4c6053 363
AzureIoTClient 17:fa1bba4c6053 364 void Device_DestroyTransaction_ReportedProperties(REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle)
AzureIoTClient 17:fa1bba4c6053 365 {
AzureIoTClient 17:fa1bba4c6053 366 /*Codes_SRS_DEVICE_02_030: [ If argument transactionHandle is NULL then Device_DestroyTransaction_ReportedProperties shall return. ]*/
AzureIoTClient 17:fa1bba4c6053 367 if (transactionHandle == NULL)
AzureIoTClient 17:fa1bba4c6053 368 {
AzureIoTClient 17:fa1bba4c6053 369 LogError("invalid argument REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle=%p", transactionHandle);
AzureIoTClient 17:fa1bba4c6053 370 }
AzureIoTClient 17:fa1bba4c6053 371 else
AzureIoTClient 17:fa1bba4c6053 372 {
AzureIoTClient 17:fa1bba4c6053 373 /*Codes_SRS_DEVICE_02_031: [ Otherwise Device_DestroyTransaction_ReportedProperties shall free all used resources. ]*/
AzureIoTClient 17:fa1bba4c6053 374 DataPublisher_DestroyTransaction_ReportedProperties(transactionHandle);
AzureIoTClient 17:fa1bba4c6053 375 }
AzureIoTClient 17:fa1bba4c6053 376 }
AzureIoTClient 17:fa1bba4c6053 377
AzureIoTClient 17:fa1bba4c6053 378 DEVICE_RESULT Device_IngestDesiredProperties(void* startAddress, DEVICE_HANDLE deviceHandle, const char* desiredProperties)
AzureIoTClient 17:fa1bba4c6053 379 {
AzureIoTClient 17:fa1bba4c6053 380 DEVICE_RESULT result;
AzureIoTClient 17:fa1bba4c6053 381 /*Codes_SRS_DEVICE_02_032: [ If deviceHandle is NULL then Device_IngestDesiredProperties shall fail and return DEVICE_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 382 /*Codes_SRS_DEVICE_02_033: [ If desiredProperties is NULL then Device_IngestDesiredProperties shall fail and return DEVICE_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 383 /*Codes_SRS_DEVICE_02_037: [ If startAddress is NULL then Device_IngestDesiredProperties shall fail and return DEVICE_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 384 if (
AzureIoTClient 17:fa1bba4c6053 385 (deviceHandle == NULL) ||
AzureIoTClient 17:fa1bba4c6053 386 (desiredProperties == NULL) ||
AzureIoTClient 17:fa1bba4c6053 387 (startAddress == NULL)
AzureIoTClient 17:fa1bba4c6053 388 )
AzureIoTClient 17:fa1bba4c6053 389 {
AzureIoTClient 17:fa1bba4c6053 390 LogError("invalid argument void* startAddress=%p, DEVICE_HANDLE deviceHandle=%p, const char* desiredProperties=%p\n", startAddress, deviceHandle, desiredProperties);
AzureIoTClient 17:fa1bba4c6053 391 result = DEVICE_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 392 }
AzureIoTClient 17:fa1bba4c6053 393 else
AzureIoTClient 17:fa1bba4c6053 394 {
AzureIoTClient 17:fa1bba4c6053 395 /*Codes_SRS_DEVICE_02_034: [ Device_IngestDesiredProperties shall call CommandDecoder_IngestDesiredProperties. ]*/
AzureIoTClient 17:fa1bba4c6053 396 DEVICE_HANDLE_DATA* device = (DEVICE_HANDLE_DATA*)deviceHandle;
AzureIoTClient 17:fa1bba4c6053 397 if (CommandDecoder_IngestDesiredProperties(startAddress, device->commandDecoderHandle, desiredProperties) != COMMANDDECODER_OK)
AzureIoTClient 17:fa1bba4c6053 398 {
AzureIoTClient 17:fa1bba4c6053 399 /*Codes_SRS_DEVICE_02_035: [ If any failure happens then Device_IngestDesiredProperties shall fail and return DEVICE_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 400 LogError("failure in CommandDecoder_IngestDesiredProperties");
AzureIoTClient 17:fa1bba4c6053 401 result = DEVICE_ERROR;
AzureIoTClient 17:fa1bba4c6053 402 }
AzureIoTClient 17:fa1bba4c6053 403 else
AzureIoTClient 17:fa1bba4c6053 404 {
AzureIoTClient 17:fa1bba4c6053 405 /*Codes_SRS_DEVICE_02_036: [ Otherwise, Device_IngestDesiredProperties shall succeed and return DEVICE_OK. ]*/
AzureIoTClient 17:fa1bba4c6053 406 result = DEVICE_OK;
AzureIoTClient 17:fa1bba4c6053 407 }
AzureIoTClient 17:fa1bba4c6053 408 }
AzureIoTClient 17:fa1bba4c6053 409 return result;
AzureIoTClient 17:fa1bba4c6053 410 }