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:
Mon Jun 11 15:40:43 2018 -0700
Revision:
33:30999a4089f1
Parent:
22:422d94bd3c18
Child:
36:7d12a5386197
1.2.5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 4:233dd7616d73 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 4:233dd7616d73 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 4:233dd7616d73 3
AzureIoTClient 4:233dd7616d73 4 #include <stdlib.h>
Azure.IoT Build 10:c2aee3965a83 5 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 4:233dd7616d73 6
AzureIoTClient 4:233dd7616d73 7 #include <stdbool.h>
AzureIoTClient 4:233dd7616d73 8 #include "datapublisher.h"
AzureIoTClient 4:233dd7616d73 9 #include "jsonencoder.h"
AzureIoTClient 4:233dd7616d73 10 #include "datamarshaller.h"
AzureIoTClient 4:233dd7616d73 11 #include "agenttypesystem.h"
AzureIoTClient 4:233dd7616d73 12 #include "schema.h"
Azure.IoT Build 10:c2aee3965a83 13 #include "azure_c_shared_utility/crt_abstractions.h"
Azure.IoT Build 13:16e88f0cfa5f 14 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 17:fa1bba4c6053 15 #include "azure_c_shared_utility/vector.h"
AzureIoTClient 4:233dd7616d73 16
AzureIoTClient 4:233dd7616d73 17 DEFINE_ENUM_STRINGS(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_RESULT_VALUES)
AzureIoTClient 4:233dd7616d73 18
AzureIoTClient 4:233dd7616d73 19 #define LOG_DATA_PUBLISHER_ERROR \
AzureIoTClient 11:b1327861f5e0 20 LogError("(result = %s)", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, result))
AzureIoTClient 4:233dd7616d73 21
AzureIoTClient 4:233dd7616d73 22 #define DEFAULT_MAX_BUFFER_SIZE 10240
AzureIoTClient 4:233dd7616d73 23 /* Codes_SRS_DATA_PUBLISHER_99_066:[ A single value shall be used by all instances of DataPublisher.] */
AzureIoTClient 4:233dd7616d73 24 /* Codes_SRS_DATA_PUBLISHER_99_067:[ Before any call to DataPublisher_SetMaxBufferSize, the default max buffer size shall be equal to 10KB.] */
AzureIoTClient 4:233dd7616d73 25 static size_t maxBufferSize_ = DEFAULT_MAX_BUFFER_SIZE;
AzureIoTClient 4:233dd7616d73 26
AzureIoTClient 17:fa1bba4c6053 27 typedef struct DATA_PUBLISHER_HANDLE_DATA_TAG
AzureIoTClient 4:233dd7616d73 28 {
AzureIoTClient 4:233dd7616d73 29 DATA_MARSHALLER_HANDLE DataMarshallerHandle;
AzureIoTClient 4:233dd7616d73 30 SCHEMA_MODEL_TYPE_HANDLE ModelHandle;
AzureIoTClient 17:fa1bba4c6053 31 } DATA_PUBLISHER_HANDLE_DATA;
AzureIoTClient 4:233dd7616d73 32
AzureIoTClient 17:fa1bba4c6053 33 typedef struct TRANSACTION_HANDLE_DATA_TAG
AzureIoTClient 4:233dd7616d73 34 {
AzureIoTClient 17:fa1bba4c6053 35 DATA_PUBLISHER_HANDLE_DATA* DataPublisherInstance;
AzureIoTClient 4:233dd7616d73 36 size_t ValueCount;
AzureIoTClient 4:233dd7616d73 37 DATA_MARSHALLER_VALUE* Values;
AzureIoTClient 17:fa1bba4c6053 38 } TRANSACTION_HANDLE_DATA;
AzureIoTClient 17:fa1bba4c6053 39
AzureIoTClient 17:fa1bba4c6053 40 typedef struct REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA_TAG
AzureIoTClient 17:fa1bba4c6053 41 {
AzureIoTClient 17:fa1bba4c6053 42 DATA_PUBLISHER_HANDLE_DATA* DataPublisherInstance;
AzureIoTClient 17:fa1bba4c6053 43 VECTOR_HANDLE value; /*holds (DATA_MARSHALLER_VALUE*) */
AzureIoTClient 17:fa1bba4c6053 44 }REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA;
AzureIoTClient 4:233dd7616d73 45
AzureIoTClient 4:233dd7616d73 46 DATA_PUBLISHER_HANDLE DataPublisher_Create(SCHEMA_MODEL_TYPE_HANDLE modelHandle, bool includePropertyPath)
AzureIoTClient 4:233dd7616d73 47 {
AzureIoTClient 17:fa1bba4c6053 48 DATA_PUBLISHER_HANDLE_DATA* result;
AzureIoTClient 4:233dd7616d73 49
AzureIoTClient 4:233dd7616d73 50 /* Codes_SRS_DATA_PUBLISHER_99_042:[ If a NULL argument is passed to it, DataPublisher_Create shall return NULL.] */
AzureIoTClient 22:422d94bd3c18 51 if (modelHandle == NULL)
AzureIoTClient 4:233dd7616d73 52 {
AzureIoTClient 4:233dd7616d73 53 result = NULL;
AzureIoTClient 11:b1327861f5e0 54 LogError("(result = %s)", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_INVALID_ARG));
AzureIoTClient 4:233dd7616d73 55 }
AzureIoTClient 17:fa1bba4c6053 56 else if ((result = (DATA_PUBLISHER_HANDLE_DATA*)malloc(sizeof(DATA_PUBLISHER_HANDLE_DATA))) == NULL)
AzureIoTClient 4:233dd7616d73 57 {
AzureIoTClient 4:233dd7616d73 58 /* Codes_SRS_DATA_PUBLISHER_99_047:[ For any other error not specified here, DataPublisher_Create shall return NULL.] */
AzureIoTClient 4:233dd7616d73 59 result = NULL;
AzureIoTClient 11:b1327861f5e0 60 LogError("(result = %s)", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_ERROR));
AzureIoTClient 4:233dd7616d73 61 }
AzureIoTClient 4:233dd7616d73 62 else
AzureIoTClient 4:233dd7616d73 63 {
AzureIoTClient 4:233dd7616d73 64 /* Codes_SRS_DATA_PUBLISHER_99_043:[ DataPublisher_Create shall initialize and hold a handle to a DataMarshaller instance.] */
AzureIoTClient 4:233dd7616d73 65 /* Codes_SRS_DATA_PUBLISHER_01_001: [DataPublisher_Create shall pass the includePropertyPath argument to DataMarshaller_Create.] */
AzureIoTClient 17:fa1bba4c6053 66 if ((result->DataMarshallerHandle = DataMarshaller_Create(modelHandle, includePropertyPath)) == NULL)
AzureIoTClient 4:233dd7616d73 67 {
AzureIoTClient 17:fa1bba4c6053 68 free(result);
AzureIoTClient 4:233dd7616d73 69
AzureIoTClient 4:233dd7616d73 70 /* Codes_SRS_DATA_PUBLISHER_99_044:[ If the creation of the DataMarshaller instance fails, DataPublisher_Create shall return NULL.] */
AzureIoTClient 4:233dd7616d73 71 result = NULL;
AzureIoTClient 11:b1327861f5e0 72 LogError("(result = %s)", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_MARSHALLER_ERROR));
AzureIoTClient 4:233dd7616d73 73 }
AzureIoTClient 4:233dd7616d73 74 else
AzureIoTClient 4:233dd7616d73 75 {
AzureIoTClient 4:233dd7616d73 76 /* Codes_SRS_DATA_PUBLISHER_99_041:[ DataPublisher_Create shall create a new DataPublisher instance and return a non-NULL handle in case of success.] */
AzureIoTClient 17:fa1bba4c6053 77 result->ModelHandle = modelHandle;
AzureIoTClient 4:233dd7616d73 78 }
AzureIoTClient 4:233dd7616d73 79 }
AzureIoTClient 4:233dd7616d73 80
AzureIoTClient 4:233dd7616d73 81 return result;
AzureIoTClient 4:233dd7616d73 82 }
AzureIoTClient 4:233dd7616d73 83
AzureIoTClient 4:233dd7616d73 84 void DataPublisher_Destroy(DATA_PUBLISHER_HANDLE dataPublisherHandle)
AzureIoTClient 4:233dd7616d73 85 {
AzureIoTClient 4:233dd7616d73 86 if (dataPublisherHandle != NULL)
AzureIoTClient 4:233dd7616d73 87 {
AzureIoTClient 17:fa1bba4c6053 88 DATA_PUBLISHER_HANDLE_DATA* dataPublisherInstance = (DATA_PUBLISHER_HANDLE_DATA*)dataPublisherHandle;
AzureIoTClient 4:233dd7616d73 89 DataMarshaller_Destroy(dataPublisherInstance->DataMarshallerHandle);
AzureIoTClient 4:233dd7616d73 90
AzureIoTClient 4:233dd7616d73 91 free(dataPublisherHandle);
AzureIoTClient 4:233dd7616d73 92 }
AzureIoTClient 4:233dd7616d73 93 }
AzureIoTClient 4:233dd7616d73 94
AzureIoTClient 4:233dd7616d73 95 TRANSACTION_HANDLE DataPublisher_StartTransaction(DATA_PUBLISHER_HANDLE dataPublisherHandle)
AzureIoTClient 4:233dd7616d73 96 {
AzureIoTClient 17:fa1bba4c6053 97 TRANSACTION_HANDLE_DATA* transaction;
AzureIoTClient 4:233dd7616d73 98
AzureIoTClient 4:233dd7616d73 99 /* Codes_SRS_DATA_PUBLISHER_99_038:[ If DataPublisher_StartTransaction is called with a NULL argument it shall return NULL.] */
AzureIoTClient 4:233dd7616d73 100 if (dataPublisherHandle == NULL)
AzureIoTClient 4:233dd7616d73 101 {
AzureIoTClient 4:233dd7616d73 102 transaction = NULL;
AzureIoTClient 11:b1327861f5e0 103 LogError("(Error code: %s)", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_INVALID_ARG));
AzureIoTClient 4:233dd7616d73 104 }
AzureIoTClient 4:233dd7616d73 105 else
AzureIoTClient 4:233dd7616d73 106 {
AzureIoTClient 4:233dd7616d73 107 /* Codes_SRS_DATA_PUBLISHER_99_007:[ A call to DataPublisher_StartTransaction shall start a new transaction.] */
AzureIoTClient 17:fa1bba4c6053 108 transaction = (TRANSACTION_HANDLE_DATA*)malloc(sizeof(TRANSACTION_HANDLE_DATA));
AzureIoTClient 4:233dd7616d73 109 if (transaction == NULL)
AzureIoTClient 4:233dd7616d73 110 {
AzureIoTClient 11:b1327861f5e0 111 LogError("Allocating transaction failed (Error code: %s)", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_ERROR));
AzureIoTClient 4:233dd7616d73 112 }
AzureIoTClient 4:233dd7616d73 113 else
AzureIoTClient 4:233dd7616d73 114 {
AzureIoTClient 4:233dd7616d73 115 transaction->ValueCount = 0;
AzureIoTClient 4:233dd7616d73 116 transaction->Values = NULL;
AzureIoTClient 17:fa1bba4c6053 117 transaction->DataPublisherInstance = (DATA_PUBLISHER_HANDLE_DATA*)dataPublisherHandle;
AzureIoTClient 4:233dd7616d73 118 }
AzureIoTClient 4:233dd7616d73 119 }
AzureIoTClient 4:233dd7616d73 120
AzureIoTClient 4:233dd7616d73 121 /* Codes_SRS_DATA_PUBLISHER_99_008:[ DataPublisher_StartTransaction shall return a non-NULL handle upon success.] */
AzureIoTClient 4:233dd7616d73 122 /* Codes_SRS_DATA_PUBLISHER_99_009:[ DataPublisher_StartTransaction shall return NULL upon failure.] */
AzureIoTClient 4:233dd7616d73 123 return transaction;
AzureIoTClient 4:233dd7616d73 124 }
AzureIoTClient 4:233dd7616d73 125
AzureIoTClient 4:233dd7616d73 126 DATA_PUBLISHER_RESULT DataPublisher_PublishTransacted(TRANSACTION_HANDLE transactionHandle, const char* propertyPath, const AGENT_DATA_TYPE* data)
AzureIoTClient 4:233dd7616d73 127 {
AzureIoTClient 4:233dd7616d73 128 DATA_PUBLISHER_RESULT result;
AzureIoTClient 4:233dd7616d73 129 char* propertyPathCopy;
AzureIoTClient 4:233dd7616d73 130
AzureIoTClient 4:233dd7616d73 131 /* Codes_SRS_DATA_PUBLISHER_99_017:[ When one or more NULL parameter(s) are specified, DataPublisher_PublishTransacted is called with a NULL transactionHandle, it shall return DATA_PUBLISHER_INVALID_ARG.] */
AzureIoTClient 4:233dd7616d73 132 if ((transactionHandle == NULL) ||
AzureIoTClient 4:233dd7616d73 133 (propertyPath == NULL) ||
AzureIoTClient 4:233dd7616d73 134 (data == NULL))
AzureIoTClient 4:233dd7616d73 135 {
AzureIoTClient 4:233dd7616d73 136 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 4:233dd7616d73 137 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 138 }
AzureIoTClient 4:233dd7616d73 139 else if (mallocAndStrcpy_s(&propertyPathCopy, propertyPath) != 0)
AzureIoTClient 4:233dd7616d73 140 {
AzureIoTClient 4:233dd7616d73 141 /* Codes_SRS_DATA_PUBLISHER_99_020:[ For any errors not explicitly mentioned here the DataPublisher APIs shall return DATA_PUBLISHER_ERROR.] */
AzureIoTClient 4:233dd7616d73 142 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 143 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 144 }
AzureIoTClient 4:233dd7616d73 145 else
AzureIoTClient 4:233dd7616d73 146 {
AzureIoTClient 17:fa1bba4c6053 147 TRANSACTION_HANDLE_DATA* transaction = (TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 4:233dd7616d73 148 AGENT_DATA_TYPE* propertyValue;
AzureIoTClient 4:233dd7616d73 149
AzureIoTClient 4:233dd7616d73 150 if (!Schema_ModelPropertyByPathExists(transaction->DataPublisherInstance->ModelHandle, propertyPath))
AzureIoTClient 4:233dd7616d73 151 {
AzureIoTClient 4:233dd7616d73 152 free(propertyPathCopy);
AzureIoTClient 4:233dd7616d73 153
AzureIoTClient 4:233dd7616d73 154 /* Codes_SRS_DATA_PUBLISHER_99_040:[ When propertyPath does not exist in the supplied model, DataPublisher_Publish shall return DATA_PUBLISHER_SCHEMA_FAILED without dispatching data.] */
AzureIoTClient 4:233dd7616d73 155 result = DATA_PUBLISHER_SCHEMA_FAILED;
AzureIoTClient 4:233dd7616d73 156 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 157 }
AzureIoTClient 4:233dd7616d73 158 else if ((propertyValue = (AGENT_DATA_TYPE*)malloc(sizeof(AGENT_DATA_TYPE))) == NULL)
AzureIoTClient 4:233dd7616d73 159 {
AzureIoTClient 4:233dd7616d73 160 free(propertyPathCopy);
AzureIoTClient 4:233dd7616d73 161
AzureIoTClient 4:233dd7616d73 162 /* Codes_SRS_DATA_PUBLISHER_99_020:[ For any errors not explicitly mentioned here the DataPublisher APIs shall return DATA_PUBLISHER_ERROR.] */
AzureIoTClient 4:233dd7616d73 163 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 164 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 165 }
AzureIoTClient 4:233dd7616d73 166 else if (Create_AGENT_DATA_TYPE_from_AGENT_DATA_TYPE(propertyValue, data) != AGENT_DATA_TYPES_OK)
AzureIoTClient 4:233dd7616d73 167 {
AzureIoTClient 4:233dd7616d73 168 free(propertyPathCopy);
AzureIoTClient 4:233dd7616d73 169 free(propertyValue);
AzureIoTClient 4:233dd7616d73 170
AzureIoTClient 4:233dd7616d73 171 /* Codes_SRS_DATA_PUBLISHER_99_028:[ If creating the copy fails then DATA_PUBLISHER_AGENT_DATA_TYPES_ERROR shall be returned.] */
AzureIoTClient 4:233dd7616d73 172 result = DATA_PUBLISHER_AGENT_DATA_TYPES_ERROR;
AzureIoTClient 4:233dd7616d73 173 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 174 }
AzureIoTClient 4:233dd7616d73 175 else
AzureIoTClient 4:233dd7616d73 176 {
AzureIoTClient 4:233dd7616d73 177 size_t i;
AzureIoTClient 4:233dd7616d73 178 DATA_MARSHALLER_VALUE* propertySlot = NULL;
AzureIoTClient 4:233dd7616d73 179
AzureIoTClient 4:233dd7616d73 180 /* Codes_SRS_DATA_PUBLISHER_99_019:[ If the same property is associated twice with a transaction, then the last value shall be kept associated with the transaction.] */
AzureIoTClient 4:233dd7616d73 181 for (i = 0; i < transaction->ValueCount; i++)
AzureIoTClient 4:233dd7616d73 182 {
AzureIoTClient 4:233dd7616d73 183 if (strcmp(transaction->Values[i].PropertyPath, propertyPath) == 0)
AzureIoTClient 4:233dd7616d73 184 {
AzureIoTClient 4:233dd7616d73 185 propertySlot = &transaction->Values[i];
AzureIoTClient 4:233dd7616d73 186 break;
AzureIoTClient 4:233dd7616d73 187 }
AzureIoTClient 4:233dd7616d73 188 }
AzureIoTClient 4:233dd7616d73 189
AzureIoTClient 4:233dd7616d73 190 if (propertySlot == NULL)
AzureIoTClient 4:233dd7616d73 191 {
AzureIoTClient 4:233dd7616d73 192 DATA_MARSHALLER_VALUE* newValues = (DATA_MARSHALLER_VALUE*)realloc(transaction->Values, sizeof(DATA_MARSHALLER_VALUE)* (transaction->ValueCount + 1));
AzureIoTClient 4:233dd7616d73 193 if (newValues != NULL)
AzureIoTClient 4:233dd7616d73 194 {
AzureIoTClient 4:233dd7616d73 195 transaction->Values = newValues;
AzureIoTClient 4:233dd7616d73 196 propertySlot = &transaction->Values[transaction->ValueCount];
AzureIoTClient 4:233dd7616d73 197 propertySlot->Value = NULL;
AzureIoTClient 4:233dd7616d73 198 propertySlot->PropertyPath = NULL;
AzureIoTClient 4:233dd7616d73 199 transaction->ValueCount++;
AzureIoTClient 4:233dd7616d73 200 }
AzureIoTClient 4:233dd7616d73 201 }
AzureIoTClient 4:233dd7616d73 202
AzureIoTClient 4:233dd7616d73 203 if (propertySlot == NULL)
AzureIoTClient 4:233dd7616d73 204 {
AzureIoTClient 4:233dd7616d73 205 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)propertyValue);
AzureIoTClient 4:233dd7616d73 206 free(propertyValue);
AzureIoTClient 4:233dd7616d73 207 free(propertyPathCopy);
AzureIoTClient 4:233dd7616d73 208
AzureIoTClient 4:233dd7616d73 209 /* Codes_SRS_DATA_PUBLISHER_99_020:[ For any errors not explicitly mentioned here the DataPublisher APIs shall return DATA_PUBLISHER_ERROR.] */
AzureIoTClient 4:233dd7616d73 210 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 211 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 212 }
AzureIoTClient 4:233dd7616d73 213 else
AzureIoTClient 4:233dd7616d73 214 {
AzureIoTClient 4:233dd7616d73 215 if (propertySlot->Value != NULL)
AzureIoTClient 4:233dd7616d73 216 {
AzureIoTClient 4:233dd7616d73 217 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)propertySlot->Value);
AzureIoTClient 4:233dd7616d73 218 free((AGENT_DATA_TYPE*)propertySlot->Value);
AzureIoTClient 4:233dd7616d73 219 }
AzureIoTClient 4:233dd7616d73 220 if (propertySlot->PropertyPath != NULL)
AzureIoTClient 4:233dd7616d73 221 {
AzureIoTClient 4:233dd7616d73 222 char* existingValue = (char*)propertySlot->PropertyPath;
AzureIoTClient 4:233dd7616d73 223 free(existingValue);
AzureIoTClient 4:233dd7616d73 224 }
AzureIoTClient 4:233dd7616d73 225
AzureIoTClient 4:233dd7616d73 226 /* Codes_SRS_DATA_PUBLISHER_99_016:[ When DataPublisher_PublishTransacted is invoked, DataPublisher shall associate the data with the transaction identified by the transactionHandle argument and return DATA_PUBLISHER_OK. No data shall be dispatched at the time of the call.] */
AzureIoTClient 4:233dd7616d73 227 propertySlot->PropertyPath = propertyPathCopy;
AzureIoTClient 4:233dd7616d73 228 propertySlot->Value = propertyValue;
AzureIoTClient 4:233dd7616d73 229
AzureIoTClient 4:233dd7616d73 230 result = DATA_PUBLISHER_OK;
AzureIoTClient 4:233dd7616d73 231 }
AzureIoTClient 4:233dd7616d73 232 }
AzureIoTClient 4:233dd7616d73 233 }
AzureIoTClient 4:233dd7616d73 234
AzureIoTClient 4:233dd7616d73 235 return result;
AzureIoTClient 4:233dd7616d73 236 }
AzureIoTClient 4:233dd7616d73 237
AzureIoTClient 4:233dd7616d73 238 DATA_PUBLISHER_RESULT DataPublisher_EndTransaction(TRANSACTION_HANDLE transactionHandle, unsigned char** destination, size_t* destinationSize)
AzureIoTClient 4:233dd7616d73 239 {
AzureIoTClient 4:233dd7616d73 240 DATA_PUBLISHER_RESULT result;
AzureIoTClient 4:233dd7616d73 241
AzureIoTClient 4:233dd7616d73 242 /*Codes_SRS_DATA_PUBLISHER_02_006: [If the destination argument is NULL, DataPublisher_EndTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */
AzureIoTClient 4:233dd7616d73 243 /*Codes_SRS_DATA_PUBLISHER_02_007: [If the destinationSize argument is NULL, DataPublisher_EndTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */
AzureIoTClient 4:233dd7616d73 244 if (
AzureIoTClient 17:fa1bba4c6053 245 (transactionHandle == NULL) ||
AzureIoTClient 4:233dd7616d73 246 (destination == NULL) ||
AzureIoTClient 4:233dd7616d73 247 (destinationSize == NULL)
AzureIoTClient 4:233dd7616d73 248 )
AzureIoTClient 4:233dd7616d73 249 {
AzureIoTClient 4:233dd7616d73 250 /* Codes_SRS_DATA_PUBLISHER_99_011:[ If the transactionHandle argument is NULL, DataPublisher_EndTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */
AzureIoTClient 4:233dd7616d73 251 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 4:233dd7616d73 252 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 253 }
AzureIoTClient 4:233dd7616d73 254 else
AzureIoTClient 4:233dd7616d73 255 {
AzureIoTClient 17:fa1bba4c6053 256 TRANSACTION_HANDLE_DATA* transaction = (TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 4:233dd7616d73 257
AzureIoTClient 4:233dd7616d73 258 if (transaction->ValueCount == 0)
AzureIoTClient 4:233dd7616d73 259 {
AzureIoTClient 4:233dd7616d73 260 /* Codes_SRS_DATA_PUBLISHER_99_024:[ If no values have been associated with the transaction, no data shall be dispatched
AzureIoTClient 4:233dd7616d73 261 to DataMarshaller, the transaction shall be discarded and DataPublisher_EndTransaction shall return DATA_PUBLISHER_EMPTY_TRANSACTION.] */
AzureIoTClient 4:233dd7616d73 262 result = DATA_PUBLISHER_EMPTY_TRANSACTION;
AzureIoTClient 4:233dd7616d73 263 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 264 }
AzureIoTClient 4:233dd7616d73 265 /* Codes_SRS_DATA_PUBLISHER_99_010:[ A call to DataPublisher_EndTransaction shall mark the end of a transaction and, trigger a dispatch of all the data grouped by that transaction.] */
AzureIoTClient 4:233dd7616d73 266 else if (DataMarshaller_SendData(transaction->DataPublisherInstance->DataMarshallerHandle, transaction->ValueCount, transaction->Values, destination, destinationSize) != DATA_MARSHALLER_OK)
AzureIoTClient 4:233dd7616d73 267 {
AzureIoTClient 4:233dd7616d73 268 /* Codes_SRS_DATA_PUBLISHER_99_025:[ When the DataMarshaller_SendData call fails, DataPublisher_EndTransaction shall return DATA_PUBLISHER_MARSHALLER_ERROR.] */
AzureIoTClient 4:233dd7616d73 269 result = DATA_PUBLISHER_MARSHALLER_ERROR;
AzureIoTClient 4:233dd7616d73 270 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 271 }
AzureIoTClient 4:233dd7616d73 272 else
AzureIoTClient 4:233dd7616d73 273 {
AzureIoTClient 4:233dd7616d73 274 /* Codes_SRS_DATA_PUBLISHER_99_026:[ On success, DataPublisher_EndTransaction shall return DATA_PUBLISHER_OK.] */
AzureIoTClient 4:233dd7616d73 275 result = DATA_PUBLISHER_OK;
AzureIoTClient 4:233dd7616d73 276 }
AzureIoTClient 4:233dd7616d73 277
AzureIoTClient 4:233dd7616d73 278 /* Codes_SRS_DATA_PUBLISHER_99_012:[ DataPublisher_EndTransaction shall dispose of any resources associated with the transaction.] */
AzureIoTClient 4:233dd7616d73 279 (void)DataPublisher_CancelTransaction(transactionHandle);
AzureIoTClient 4:233dd7616d73 280 }
AzureIoTClient 4:233dd7616d73 281
AzureIoTClient 4:233dd7616d73 282 return result;
AzureIoTClient 4:233dd7616d73 283 }
AzureIoTClient 4:233dd7616d73 284
AzureIoTClient 4:233dd7616d73 285 DATA_PUBLISHER_RESULT DataPublisher_CancelTransaction(TRANSACTION_HANDLE transactionHandle)
AzureIoTClient 4:233dd7616d73 286 {
AzureIoTClient 4:233dd7616d73 287 DATA_PUBLISHER_RESULT result;
AzureIoTClient 4:233dd7616d73 288
AzureIoTClient 4:233dd7616d73 289 if (transactionHandle == NULL)
AzureIoTClient 4:233dd7616d73 290 {
AzureIoTClient 4:233dd7616d73 291 /* Codes_SRS_DATA_PUBLISHER_99_014:[ If the transactionHandle argument is NULL DataPublisher_CancelTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */
AzureIoTClient 4:233dd7616d73 292 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 4:233dd7616d73 293 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 294 }
AzureIoTClient 4:233dd7616d73 295 else
AzureIoTClient 4:233dd7616d73 296 {
AzureIoTClient 17:fa1bba4c6053 297 TRANSACTION_HANDLE_DATA* transaction = (TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 4:233dd7616d73 298 size_t i;
AzureIoTClient 4:233dd7616d73 299
AzureIoTClient 4:233dd7616d73 300 /* Codes_SRS_DATA_PUBLISHER_99_015:[ DataPublisher_CancelTransaction shall dispose of any resources associated with the transaction.] */
AzureIoTClient 4:233dd7616d73 301 for (i = 0; i < transaction->ValueCount; i++)
AzureIoTClient 4:233dd7616d73 302 {
AzureIoTClient 4:233dd7616d73 303 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)transaction->Values[i].Value);
AzureIoTClient 4:233dd7616d73 304 free((char*)transaction->Values[i].PropertyPath);
AzureIoTClient 4:233dd7616d73 305 free((AGENT_DATA_TYPE*)transaction->Values[i].Value);
AzureIoTClient 4:233dd7616d73 306 }
AzureIoTClient 4:233dd7616d73 307
AzureIoTClient 4:233dd7616d73 308 /* Codes_SRS_DATA_PUBLISHER_99_015:[ DataPublisher_CancelTransaction shall dispose of any resources associated with the transaction.] */
AzureIoTClient 4:233dd7616d73 309 free(transaction->Values);
AzureIoTClient 4:233dd7616d73 310 free(transaction);
AzureIoTClient 4:233dd7616d73 311
AzureIoTClient 17:fa1bba4c6053 312 /* Codes_SRS_DATA_PUBLISHER_99_013:[ A call to DataPublisher_CancelTransaction shall dispose of the transaction without dispatching
AzureIoTClient 4:233dd7616d73 313 the data to the DataMarshaller module and it shall return DATA_PUBLISHER_OK.] */
AzureIoTClient 4:233dd7616d73 314 result = DATA_PUBLISHER_OK;
AzureIoTClient 4:233dd7616d73 315 }
AzureIoTClient 4:233dd7616d73 316
AzureIoTClient 4:233dd7616d73 317 return result;
AzureIoTClient 4:233dd7616d73 318 }
AzureIoTClient 4:233dd7616d73 319
AzureIoTClient 4:233dd7616d73 320 /* Codes_SRS_DATA_PUBLISHER_99_065:[ DataPublisher_SetMaxBufferSize shall directly update the value used to limit how much data (in bytes) can be buffered in the BufferStorage instance.] */
AzureIoTClient 4:233dd7616d73 321 void DataPublisher_SetMaxBufferSize(size_t value)
AzureIoTClient 4:233dd7616d73 322 {
AzureIoTClient 4:233dd7616d73 323 maxBufferSize_ = value;
AzureIoTClient 4:233dd7616d73 324 }
AzureIoTClient 4:233dd7616d73 325
AzureIoTClient 4:233dd7616d73 326 /* Codes_SRS_DATA_PUBLISHER_99_069:[ DataMarshaller_GetMaxBufferSize shall return the current max buffer size value used by any new instance of DataMarshaller.] */
AzureIoTClient 4:233dd7616d73 327 size_t DataPublisher_GetMaxBufferSize(void)
AzureIoTClient 4:233dd7616d73 328 {
AzureIoTClient 4:233dd7616d73 329 return maxBufferSize_;
AzureIoTClient 4:233dd7616d73 330 }
AzureIoTClient 17:fa1bba4c6053 331
AzureIoTClient 17:fa1bba4c6053 332 REPORTED_PROPERTIES_TRANSACTION_HANDLE DataPublisher_CreateTransaction_ReportedProperties(DATA_PUBLISHER_HANDLE dataPublisherHandle)
AzureIoTClient 17:fa1bba4c6053 333 {
AzureIoTClient 17:fa1bba4c6053 334 REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA* result;
AzureIoTClient 17:fa1bba4c6053 335 /*Codes_SRS_DATA_PUBLISHER_02_027: [ If argument dataPublisherHandle is NULL then DataPublisher_CreateTransaction_ReportedProperties shall fail and return NULL. ]*/
AzureIoTClient 17:fa1bba4c6053 336 if (dataPublisherHandle == NULL)
AzureIoTClient 17:fa1bba4c6053 337 {
AzureIoTClient 17:fa1bba4c6053 338 LogError("invalid argument DATA_PUBLISHER_HANDLE dataPublisherHandle=%p", dataPublisherHandle);
AzureIoTClient 17:fa1bba4c6053 339 result = NULL;
AzureIoTClient 17:fa1bba4c6053 340 }
AzureIoTClient 17:fa1bba4c6053 341 else
AzureIoTClient 17:fa1bba4c6053 342 {
AzureIoTClient 17:fa1bba4c6053 343 /*Codes_SRS_DATA_PUBLISHER_02_028: [ DataPublisher_CreateTransaction_ReportedProperties shall create a VECTOR_HANDLE holding the individual elements of the transaction (DATA_MARSHALLER_VALUE). ]*/
AzureIoTClient 17:fa1bba4c6053 344 result = (REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA*)malloc(sizeof(REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA));
AzureIoTClient 17:fa1bba4c6053 345 if (result == NULL)
AzureIoTClient 17:fa1bba4c6053 346 {
AzureIoTClient 17:fa1bba4c6053 347 /*Codes_SRS_DATA_PUBLISHER_02_029: [ If any error occurs then DataPublisher_CreateTransaction_ReportedProperties shall fail and return NULL. ]*/
AzureIoTClient 17:fa1bba4c6053 348 LogError("unable to malloc");
AzureIoTClient 17:fa1bba4c6053 349 /*return as is */
AzureIoTClient 17:fa1bba4c6053 350 }
AzureIoTClient 17:fa1bba4c6053 351 else
AzureIoTClient 17:fa1bba4c6053 352 {
AzureIoTClient 17:fa1bba4c6053 353 result->value = VECTOR_create(sizeof(DATA_MARSHALLER_VALUE*));
AzureIoTClient 17:fa1bba4c6053 354 if (result->value == NULL)
AzureIoTClient 17:fa1bba4c6053 355 {
AzureIoTClient 17:fa1bba4c6053 356 /*Codes_SRS_DATA_PUBLISHER_02_029: [ If any error occurs then DataPublisher_CreateTransaction_ReportedProperties shall fail and return NULL. ]*/
AzureIoTClient 17:fa1bba4c6053 357 LogError("unable to VECTOR_create");
AzureIoTClient 17:fa1bba4c6053 358 free(result);
AzureIoTClient 17:fa1bba4c6053 359 result = NULL;
AzureIoTClient 17:fa1bba4c6053 360 }
AzureIoTClient 17:fa1bba4c6053 361 else
AzureIoTClient 17:fa1bba4c6053 362 {
AzureIoTClient 17:fa1bba4c6053 363 /*Codes_SRS_DATA_PUBLISHER_02_030: [ Otherwise DataPublisher_CreateTransaction_ReportedProperties shall succeed and return a non-NULL handle. ]*/
AzureIoTClient 17:fa1bba4c6053 364 result->DataPublisherInstance = dataPublisherHandle;
AzureIoTClient 17:fa1bba4c6053 365 }
AzureIoTClient 17:fa1bba4c6053 366 }
AzureIoTClient 17:fa1bba4c6053 367 }
AzureIoTClient 17:fa1bba4c6053 368
AzureIoTClient 17:fa1bba4c6053 369 return result;
AzureIoTClient 17:fa1bba4c6053 370 }
AzureIoTClient 17:fa1bba4c6053 371
AzureIoTClient 17:fa1bba4c6053 372 static bool reportedPropertyExistsByPath(const void* element, const void* value)
AzureIoTClient 17:fa1bba4c6053 373 {
AzureIoTClient 17:fa1bba4c6053 374 DATA_MARSHALLER_VALUE* dataMarshallerValue = *(DATA_MARSHALLER_VALUE**)element;
AzureIoTClient 17:fa1bba4c6053 375 return (strcmp(dataMarshallerValue->PropertyPath, (const char*)value) == 0);
AzureIoTClient 17:fa1bba4c6053 376 }
AzureIoTClient 17:fa1bba4c6053 377
AzureIoTClient 17:fa1bba4c6053 378 DATA_PUBLISHER_RESULT DataPublisher_PublishTransacted_ReportedProperty(REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle, const char* reportedPropertyPath, const AGENT_DATA_TYPE* data)
AzureIoTClient 17:fa1bba4c6053 379 {
AzureIoTClient 17:fa1bba4c6053 380 DATA_PUBLISHER_RESULT result;
AzureIoTClient 17:fa1bba4c6053 381 /*Codes_SRS_DATA_PUBLISHER_02_009: [ If argument transactionHandle is NULL then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 382 /*Codes_SRS_DATA_PUBLISHER_02_010: [ If argument reportedPropertyPath is NULL then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 383 /*Codes_SRS_DATA_PUBLISHER_02_011: [ If argument data is NULL then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 384 if (
AzureIoTClient 17:fa1bba4c6053 385 (transactionHandle == NULL) ||
AzureIoTClient 17:fa1bba4c6053 386 (reportedPropertyPath == NULL) ||
AzureIoTClient 17:fa1bba4c6053 387 (data == NULL)
AzureIoTClient 17:fa1bba4c6053 388 )
AzureIoTClient 17:fa1bba4c6053 389 {
AzureIoTClient 17:fa1bba4c6053 390 LogError("invalid argument REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle=%p, const char* reportedPropertyPath=%p, const AGENT_DATA_TYPE* data=%p", transactionHandle, reportedPropertyPath, data);
AzureIoTClient 17:fa1bba4c6053 391 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 392 }
AzureIoTClient 17:fa1bba4c6053 393 else
AzureIoTClient 17:fa1bba4c6053 394 {
AzureIoTClient 17:fa1bba4c6053 395 REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA* handleData = (REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 17:fa1bba4c6053 396 /*Codes_SRS_DATA_PUBLISHER_02_012: [ DataPublisher_PublishTransacted_ReportedProperty shall verify that a reported property having the path reportedPropertyPath exists in the model by calling Schema_ModelReportedPropertyByPathExists ]*/
AzureIoTClient 17:fa1bba4c6053 397 if (!Schema_ModelReportedPropertyByPathExists(handleData->DataPublisherInstance->ModelHandle, reportedPropertyPath))
AzureIoTClient 17:fa1bba4c6053 398 {
AzureIoTClient 17:fa1bba4c6053 399 /*Codes_SRS_DATA_PUBLISHER_02_013: [ If a reported property with path reportedPropertyPath does not exist in the model then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 400 LogError("unable to find a reported property by path \"%s\"", reportedPropertyPath);
AzureIoTClient 17:fa1bba4c6053 401 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 402 }
AzureIoTClient 17:fa1bba4c6053 403 else
AzureIoTClient 17:fa1bba4c6053 404 {
AzureIoTClient 17:fa1bba4c6053 405 DATA_MARSHALLER_VALUE** existingValue = VECTOR_find_if(handleData->value, reportedPropertyExistsByPath, reportedPropertyPath);
AzureIoTClient 17:fa1bba4c6053 406 if(existingValue != NULL)
AzureIoTClient 17:fa1bba4c6053 407 {
AzureIoTClient 17:fa1bba4c6053 408 /*Codes_SRS_DATA_PUBLISHER_02_014: [ If the same (by reportedPropertypath) reported property has already been added to the transaction, then DataPublisher_PublishTransacted_ReportedProperty shall overwrite the previous reported property. ]*/
AzureIoTClient 17:fa1bba4c6053 409 AGENT_DATA_TYPE *clone = (AGENT_DATA_TYPE *)malloc(sizeof(AGENT_DATA_TYPE));
AzureIoTClient 17:fa1bba4c6053 410 if(clone == NULL)
AzureIoTClient 17:fa1bba4c6053 411 {
AzureIoTClient 17:fa1bba4c6053 412 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 413 LogError("unable to malloc");
AzureIoTClient 17:fa1bba4c6053 414 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 415 }
AzureIoTClient 17:fa1bba4c6053 416 else
AzureIoTClient 17:fa1bba4c6053 417 {
AzureIoTClient 17:fa1bba4c6053 418 if (Create_AGENT_DATA_TYPE_from_AGENT_DATA_TYPE(clone, data) != AGENT_DATA_TYPES_OK)
AzureIoTClient 17:fa1bba4c6053 419 {
AzureIoTClient 17:fa1bba4c6053 420 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 421 LogError("unable to Create_AGENT_DATA_TYPE_from_AGENT_DATA_TYPE");
AzureIoTClient 17:fa1bba4c6053 422 free(clone);
AzureIoTClient 17:fa1bba4c6053 423 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 424 }
AzureIoTClient 17:fa1bba4c6053 425 else
AzureIoTClient 17:fa1bba4c6053 426 {
AzureIoTClient 17:fa1bba4c6053 427 /*Codes_SRS_DATA_PUBLISHER_02_017: [ Otherwise DataPublisher_PublishTransacted_ReportedProperty shall succeed and return DATA_PUBLISHER_OK. ]*/
AzureIoTClient 17:fa1bba4c6053 428 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)((*existingValue)->Value));
AzureIoTClient 17:fa1bba4c6053 429 free((void*)((*existingValue)->Value));
AzureIoTClient 17:fa1bba4c6053 430 (*existingValue)->Value = clone;
AzureIoTClient 17:fa1bba4c6053 431 result = DATA_PUBLISHER_OK;
AzureIoTClient 17:fa1bba4c6053 432 }
AzureIoTClient 17:fa1bba4c6053 433 }
AzureIoTClient 17:fa1bba4c6053 434 }
AzureIoTClient 17:fa1bba4c6053 435 else
AzureIoTClient 17:fa1bba4c6053 436 {
AzureIoTClient 17:fa1bba4c6053 437 /*totally new reported property*/
AzureIoTClient 17:fa1bba4c6053 438 DATA_MARSHALLER_VALUE* newValue = (DATA_MARSHALLER_VALUE*)malloc(sizeof(DATA_MARSHALLER_VALUE));
AzureIoTClient 17:fa1bba4c6053 439 if (newValue == NULL)
AzureIoTClient 17:fa1bba4c6053 440 {
AzureIoTClient 17:fa1bba4c6053 441 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 442 LogError("unable to malloc");
AzureIoTClient 17:fa1bba4c6053 443 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 444 }
AzureIoTClient 17:fa1bba4c6053 445 else
AzureIoTClient 17:fa1bba4c6053 446 {
AzureIoTClient 17:fa1bba4c6053 447 if (mallocAndStrcpy_s((char**)&(newValue->PropertyPath), reportedPropertyPath) != 0)
AzureIoTClient 17:fa1bba4c6053 448 {
AzureIoTClient 17:fa1bba4c6053 449 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 450 LogError("unable to mallocAndStrcpy_s");
AzureIoTClient 17:fa1bba4c6053 451 free(newValue);
AzureIoTClient 17:fa1bba4c6053 452 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 453 }
AzureIoTClient 17:fa1bba4c6053 454 else
AzureIoTClient 17:fa1bba4c6053 455 {
AzureIoTClient 17:fa1bba4c6053 456 if ((newValue->Value = (AGENT_DATA_TYPE*)malloc(sizeof(AGENT_DATA_TYPE))) == NULL)
AzureIoTClient 17:fa1bba4c6053 457 {
AzureIoTClient 17:fa1bba4c6053 458 LogError("unable to malloc");
AzureIoTClient 17:fa1bba4c6053 459 free((void*)newValue->PropertyPath);
AzureIoTClient 17:fa1bba4c6053 460 free(newValue);
AzureIoTClient 17:fa1bba4c6053 461 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 462 }
AzureIoTClient 17:fa1bba4c6053 463 else
AzureIoTClient 17:fa1bba4c6053 464 {
AzureIoTClient 17:fa1bba4c6053 465 if (Create_AGENT_DATA_TYPE_from_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)newValue->Value, data) != AGENT_DATA_TYPES_OK)
AzureIoTClient 17:fa1bba4c6053 466 {
AzureIoTClient 17:fa1bba4c6053 467 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 468 LogError("unable to Create_AGENT_DATA_TYPE_from_AGENT_DATA_TYPE");
AzureIoTClient 17:fa1bba4c6053 469 free((void*)newValue->Value);
AzureIoTClient 17:fa1bba4c6053 470 free((void*)newValue->PropertyPath);
AzureIoTClient 17:fa1bba4c6053 471 free(newValue);
AzureIoTClient 17:fa1bba4c6053 472 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 473 }
AzureIoTClient 17:fa1bba4c6053 474 else
AzureIoTClient 17:fa1bba4c6053 475 {
AzureIoTClient 17:fa1bba4c6053 476 /*Codes_SRS_DATA_PUBLISHER_02_015: [ DataPublisher_PublishTransacted_ReportedProperty shall add a new DATA_MARSHALLER_VALUE to the VECTOR_HANDLE. ]*/
AzureIoTClient 17:fa1bba4c6053 477 if (VECTOR_push_back(handleData->value, &newValue, 1) != 0)
AzureIoTClient 17:fa1bba4c6053 478 {
AzureIoTClient 17:fa1bba4c6053 479 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. */
AzureIoTClient 17:fa1bba4c6053 480 LogError("unable to VECTOR_push_back");
AzureIoTClient 17:fa1bba4c6053 481 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)newValue->Value);
AzureIoTClient 17:fa1bba4c6053 482 free((void*)newValue->Value);
AzureIoTClient 17:fa1bba4c6053 483 free((void*)newValue->PropertyPath);
AzureIoTClient 17:fa1bba4c6053 484 free(newValue);
AzureIoTClient 17:fa1bba4c6053 485 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 486 }
AzureIoTClient 17:fa1bba4c6053 487 else
AzureIoTClient 17:fa1bba4c6053 488 {
AzureIoTClient 17:fa1bba4c6053 489 /*Codes_SRS_DATA_PUBLISHER_02_017: [ Otherwise DataPublisher_PublishTransacted_ReportedProperty shall succeed and return DATA_PUBLISHER_OK. ]*/
AzureIoTClient 17:fa1bba4c6053 490 result = DATA_PUBLISHER_OK;
AzureIoTClient 17:fa1bba4c6053 491 }
AzureIoTClient 17:fa1bba4c6053 492 }
AzureIoTClient 17:fa1bba4c6053 493 }
AzureIoTClient 17:fa1bba4c6053 494 }
AzureIoTClient 17:fa1bba4c6053 495 }
AzureIoTClient 17:fa1bba4c6053 496 }
AzureIoTClient 17:fa1bba4c6053 497 }
AzureIoTClient 17:fa1bba4c6053 498 }
AzureIoTClient 17:fa1bba4c6053 499 return result;
AzureIoTClient 17:fa1bba4c6053 500 }
AzureIoTClient 17:fa1bba4c6053 501
AzureIoTClient 17:fa1bba4c6053 502 DATA_PUBLISHER_RESULT DataPublisher_CommitTransaction_ReportedProperties(REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle, unsigned char** destination, size_t* destinationSize)
AzureIoTClient 17:fa1bba4c6053 503 {
AzureIoTClient 17:fa1bba4c6053 504 DATA_PUBLISHER_RESULT result;
AzureIoTClient 17:fa1bba4c6053 505 /*Codes_SRS_DATA_PUBLISHER_02_019: [ If argument transactionHandle is NULL then DataPublisher_CommitTransaction_ReportedProperties shall fail and return DATA_PUBLISHER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 506 /*Codes_SRS_DATA_PUBLISHER_02_020: [ If argument destination is NULL then DataPublisher_CommitTransaction_ReportedProperties shall fail and return DATA_PUBLISHER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 507 /*Codes_SRS_DATA_PUBLISHER_02_021: [ If argument destinationSize NULL then DataPublisher_CommitTransaction_ReportedProperties shall fail and return DATA_PUBLISHER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 508 if (
AzureIoTClient 17:fa1bba4c6053 509 (transactionHandle == NULL) ||
AzureIoTClient 17:fa1bba4c6053 510 (destination == NULL) ||
AzureIoTClient 17:fa1bba4c6053 511 (destinationSize == NULL)
AzureIoTClient 17:fa1bba4c6053 512 )
AzureIoTClient 17:fa1bba4c6053 513 {
AzureIoTClient 17:fa1bba4c6053 514 LogError("invalid argument REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle=%p, unsigned char** destination=%p, size_t* destinationSize=%p", transactionHandle, destination, destinationSize);
AzureIoTClient 17:fa1bba4c6053 515 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 516 }
AzureIoTClient 17:fa1bba4c6053 517 else
AzureIoTClient 17:fa1bba4c6053 518 {
AzureIoTClient 17:fa1bba4c6053 519 /*Codes_SRS_DATA_PUBLISHER_02_031: [ If the transaction contains zero elements then DataPublisher_CommitTransaction_ReportedProperties shall fail and return DATA_PUBLISHER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 520 REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA* handle = (REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 17:fa1bba4c6053 521 if (VECTOR_size(handle->value) == 0)
AzureIoTClient 17:fa1bba4c6053 522 {
AzureIoTClient 17:fa1bba4c6053 523 LogError("cannot commit empty transaction");
AzureIoTClient 17:fa1bba4c6053 524 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 525 }
AzureIoTClient 17:fa1bba4c6053 526 else
AzureIoTClient 17:fa1bba4c6053 527 {
AzureIoTClient 17:fa1bba4c6053 528 /*Codes_SRS_DATA_PUBLISHER_02_022: [ DataPublisher_CommitTransaction_ReportedProperties shall call DataMarshaller_SendData_ReportedProperties providing the VECTOR_HANDLE holding the transacted reported properties, destination and destinationSize. ]*/
AzureIoTClient 17:fa1bba4c6053 529 if (DataMarshaller_SendData_ReportedProperties(handle->DataPublisherInstance->DataMarshallerHandle, handle->value, destination, destinationSize) != DATA_MARSHALLER_OK)
AzureIoTClient 17:fa1bba4c6053 530 {
AzureIoTClient 17:fa1bba4c6053 531 /*Codes_SRS_DATA_PUBLISHER_02_023: [ If any error occurs then DataPublisher_CommitTransaction_ReportedProperties shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 532 LogError("unable to DataMarshaller_SendData_ReportedProperties");
AzureIoTClient 17:fa1bba4c6053 533 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 534 }
AzureIoTClient 17:fa1bba4c6053 535 else
AzureIoTClient 17:fa1bba4c6053 536 {
AzureIoTClient 17:fa1bba4c6053 537 /*Codes_SRS_DATA_PUBLISHER_02_024: [ Otherwise DataPublisher_CommitTransaction_ReportedProperties shall succeed and return DATA_PUBLISHER_OK. ]*/
AzureIoTClient 17:fa1bba4c6053 538 result = DATA_PUBLISHER_OK;
AzureIoTClient 17:fa1bba4c6053 539 }
AzureIoTClient 17:fa1bba4c6053 540 }
AzureIoTClient 17:fa1bba4c6053 541 }
AzureIoTClient 17:fa1bba4c6053 542
AzureIoTClient 17:fa1bba4c6053 543 return result;
AzureIoTClient 17:fa1bba4c6053 544 }
AzureIoTClient 17:fa1bba4c6053 545
AzureIoTClient 17:fa1bba4c6053 546 void DataPublisher_DestroyTransaction_ReportedProperties(REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle)
AzureIoTClient 17:fa1bba4c6053 547 {
AzureIoTClient 17:fa1bba4c6053 548 /*Codes_SRS_DATA_PUBLISHER_02_025: [ If argument transactionHandle is NULL then DataPublisher_DestroyTransaction_ReportedProperties shall return. ]*/
AzureIoTClient 17:fa1bba4c6053 549 if (transactionHandle == NULL)
AzureIoTClient 17:fa1bba4c6053 550 {
AzureIoTClient 17:fa1bba4c6053 551 LogError("invalig argument REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle=%p", transactionHandle);
AzureIoTClient 17:fa1bba4c6053 552 }
AzureIoTClient 17:fa1bba4c6053 553 else
AzureIoTClient 17:fa1bba4c6053 554 {
AzureIoTClient 17:fa1bba4c6053 555 /*Codes_SRS_DATA_PUBLISHER_02_026: [ Otherwise DataPublisher_DestroyTransaction_ReportedProperties shall free all resources associated with the reported properties transactionHandle. ]*/
AzureIoTClient 17:fa1bba4c6053 556 REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA* handleData = (REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 17:fa1bba4c6053 557 size_t i, nReportedProperties;
AzureIoTClient 17:fa1bba4c6053 558 nReportedProperties = VECTOR_size(handleData->value);
AzureIoTClient 17:fa1bba4c6053 559 for (i = 0;i < nReportedProperties;i++)
AzureIoTClient 17:fa1bba4c6053 560 {
AzureIoTClient 17:fa1bba4c6053 561 DATA_MARSHALLER_VALUE *value = *(DATA_MARSHALLER_VALUE**)VECTOR_element(handleData->value, i);
AzureIoTClient 17:fa1bba4c6053 562 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)value->Value);
AzureIoTClient 17:fa1bba4c6053 563 free((void*)value->Value);
AzureIoTClient 17:fa1bba4c6053 564 free((void*)value->PropertyPath);
AzureIoTClient 17:fa1bba4c6053 565 free((void*)value);
AzureIoTClient 17:fa1bba4c6053 566 }
AzureIoTClient 17:fa1bba4c6053 567 VECTOR_destroy(handleData->value);
AzureIoTClient 17:fa1bba4c6053 568 free(handleData);
AzureIoTClient 17:fa1bba4c6053 569 }
AzureIoTClient 17:fa1bba4c6053 570 return;
AzureIoTClient 33:30999a4089f1 571 }