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:
Tue Sep 11 11:14:37 2018 -0700
Revision:
36:7d12a5386197
Parent:
28:79078ba286e5
1.2.9

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