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 Jan 24 15:24:19 2017 -0800
Revision:
21:6d3dea1abd9c
Parent:
17:fa1bba4c6053
Child:
22:422d94bd3c18
1.1.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 4:233dd7616d73 51 if (
AzureIoTClient 4:233dd7616d73 52 (modelHandle == NULL)
AzureIoTClient 4:233dd7616d73 53 )
AzureIoTClient 4:233dd7616d73 54 {
AzureIoTClient 4:233dd7616d73 55 result = NULL;
AzureIoTClient 11:b1327861f5e0 56 LogError("(result = %s)", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_INVALID_ARG));
AzureIoTClient 4:233dd7616d73 57 }
AzureIoTClient 17:fa1bba4c6053 58 else if ((result = (DATA_PUBLISHER_HANDLE_DATA*)malloc(sizeof(DATA_PUBLISHER_HANDLE_DATA))) == NULL)
AzureIoTClient 4:233dd7616d73 59 {
AzureIoTClient 4:233dd7616d73 60 /* Codes_SRS_DATA_PUBLISHER_99_047:[ For any other error not specified here, DataPublisher_Create shall return NULL.] */
AzureIoTClient 4:233dd7616d73 61 result = NULL;
AzureIoTClient 11:b1327861f5e0 62 LogError("(result = %s)", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_ERROR));
AzureIoTClient 4:233dd7616d73 63 }
AzureIoTClient 4:233dd7616d73 64 else
AzureIoTClient 4:233dd7616d73 65 {
AzureIoTClient 4:233dd7616d73 66 /* Codes_SRS_DATA_PUBLISHER_99_043:[ DataPublisher_Create shall initialize and hold a handle to a DataMarshaller instance.] */
AzureIoTClient 4:233dd7616d73 67 /* Codes_SRS_DATA_PUBLISHER_01_001: [DataPublisher_Create shall pass the includePropertyPath argument to DataMarshaller_Create.] */
AzureIoTClient 17:fa1bba4c6053 68 if ((result->DataMarshallerHandle = DataMarshaller_Create(modelHandle, includePropertyPath)) == NULL)
AzureIoTClient 4:233dd7616d73 69 {
AzureIoTClient 17:fa1bba4c6053 70 free(result);
AzureIoTClient 4:233dd7616d73 71
AzureIoTClient 4:233dd7616d73 72 /* Codes_SRS_DATA_PUBLISHER_99_044:[ If the creation of the DataMarshaller instance fails, DataPublisher_Create shall return NULL.] */
AzureIoTClient 4:233dd7616d73 73 result = NULL;
AzureIoTClient 11:b1327861f5e0 74 LogError("(result = %s)", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_MARSHALLER_ERROR));
AzureIoTClient 4:233dd7616d73 75 }
AzureIoTClient 4:233dd7616d73 76 else
AzureIoTClient 4:233dd7616d73 77 {
AzureIoTClient 4:233dd7616d73 78 /* 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 79 result->ModelHandle = modelHandle;
AzureIoTClient 4:233dd7616d73 80 }
AzureIoTClient 4:233dd7616d73 81 }
AzureIoTClient 4:233dd7616d73 82
AzureIoTClient 4:233dd7616d73 83 return result;
AzureIoTClient 4:233dd7616d73 84 }
AzureIoTClient 4:233dd7616d73 85
AzureIoTClient 4:233dd7616d73 86 void DataPublisher_Destroy(DATA_PUBLISHER_HANDLE dataPublisherHandle)
AzureIoTClient 4:233dd7616d73 87 {
AzureIoTClient 4:233dd7616d73 88 if (dataPublisherHandle != NULL)
AzureIoTClient 4:233dd7616d73 89 {
AzureIoTClient 17:fa1bba4c6053 90 DATA_PUBLISHER_HANDLE_DATA* dataPublisherInstance = (DATA_PUBLISHER_HANDLE_DATA*)dataPublisherHandle;
AzureIoTClient 4:233dd7616d73 91 DataMarshaller_Destroy(dataPublisherInstance->DataMarshallerHandle);
AzureIoTClient 4:233dd7616d73 92
AzureIoTClient 4:233dd7616d73 93 free(dataPublisherHandle);
AzureIoTClient 4:233dd7616d73 94 }
AzureIoTClient 4:233dd7616d73 95 }
AzureIoTClient 4:233dd7616d73 96
AzureIoTClient 4:233dd7616d73 97 TRANSACTION_HANDLE DataPublisher_StartTransaction(DATA_PUBLISHER_HANDLE dataPublisherHandle)
AzureIoTClient 4:233dd7616d73 98 {
AzureIoTClient 17:fa1bba4c6053 99 TRANSACTION_HANDLE_DATA* transaction;
AzureIoTClient 4:233dd7616d73 100
AzureIoTClient 4:233dd7616d73 101 /* Codes_SRS_DATA_PUBLISHER_99_038:[ If DataPublisher_StartTransaction is called with a NULL argument it shall return NULL.] */
AzureIoTClient 4:233dd7616d73 102 if (dataPublisherHandle == NULL)
AzureIoTClient 4:233dd7616d73 103 {
AzureIoTClient 4:233dd7616d73 104 transaction = NULL;
AzureIoTClient 11:b1327861f5e0 105 LogError("(Error code: %s)", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_INVALID_ARG));
AzureIoTClient 4:233dd7616d73 106 }
AzureIoTClient 4:233dd7616d73 107 else
AzureIoTClient 4:233dd7616d73 108 {
AzureIoTClient 4:233dd7616d73 109 /* Codes_SRS_DATA_PUBLISHER_99_007:[ A call to DataPublisher_StartTransaction shall start a new transaction.] */
AzureIoTClient 17:fa1bba4c6053 110 transaction = (TRANSACTION_HANDLE_DATA*)malloc(sizeof(TRANSACTION_HANDLE_DATA));
AzureIoTClient 4:233dd7616d73 111 if (transaction == NULL)
AzureIoTClient 4:233dd7616d73 112 {
AzureIoTClient 11:b1327861f5e0 113 LogError("Allocating transaction failed (Error code: %s)", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_ERROR));
AzureIoTClient 4:233dd7616d73 114 }
AzureIoTClient 4:233dd7616d73 115 else
AzureIoTClient 4:233dd7616d73 116 {
AzureIoTClient 4:233dd7616d73 117 transaction->ValueCount = 0;
AzureIoTClient 4:233dd7616d73 118 transaction->Values = NULL;
AzureIoTClient 17:fa1bba4c6053 119 transaction->DataPublisherInstance = (DATA_PUBLISHER_HANDLE_DATA*)dataPublisherHandle;
AzureIoTClient 4:233dd7616d73 120 }
AzureIoTClient 4:233dd7616d73 121 }
AzureIoTClient 4:233dd7616d73 122
AzureIoTClient 4:233dd7616d73 123 /* Codes_SRS_DATA_PUBLISHER_99_008:[ DataPublisher_StartTransaction shall return a non-NULL handle upon success.] */
AzureIoTClient 4:233dd7616d73 124 /* Codes_SRS_DATA_PUBLISHER_99_009:[ DataPublisher_StartTransaction shall return NULL upon failure.] */
AzureIoTClient 4:233dd7616d73 125 return transaction;
AzureIoTClient 4:233dd7616d73 126 }
AzureIoTClient 4:233dd7616d73 127
AzureIoTClient 4:233dd7616d73 128 DATA_PUBLISHER_RESULT DataPublisher_PublishTransacted(TRANSACTION_HANDLE transactionHandle, const char* propertyPath, const AGENT_DATA_TYPE* data)
AzureIoTClient 4:233dd7616d73 129 {
AzureIoTClient 4:233dd7616d73 130 DATA_PUBLISHER_RESULT result;
AzureIoTClient 4:233dd7616d73 131 char* propertyPathCopy;
AzureIoTClient 4:233dd7616d73 132
AzureIoTClient 4:233dd7616d73 133 /* 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 134 if ((transactionHandle == NULL) ||
AzureIoTClient 4:233dd7616d73 135 (propertyPath == NULL) ||
AzureIoTClient 4:233dd7616d73 136 (data == NULL))
AzureIoTClient 4:233dd7616d73 137 {
AzureIoTClient 4:233dd7616d73 138 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 4:233dd7616d73 139 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 140 }
AzureIoTClient 4:233dd7616d73 141 else if (mallocAndStrcpy_s(&propertyPathCopy, propertyPath) != 0)
AzureIoTClient 4:233dd7616d73 142 {
AzureIoTClient 4:233dd7616d73 143 /* Codes_SRS_DATA_PUBLISHER_99_020:[ For any errors not explicitly mentioned here the DataPublisher APIs shall return DATA_PUBLISHER_ERROR.] */
AzureIoTClient 4:233dd7616d73 144 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 145 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 146 }
AzureIoTClient 4:233dd7616d73 147 else
AzureIoTClient 4:233dd7616d73 148 {
AzureIoTClient 17:fa1bba4c6053 149 TRANSACTION_HANDLE_DATA* transaction = (TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 4:233dd7616d73 150 AGENT_DATA_TYPE* propertyValue;
AzureIoTClient 4:233dd7616d73 151
AzureIoTClient 4:233dd7616d73 152 if (!Schema_ModelPropertyByPathExists(transaction->DataPublisherInstance->ModelHandle, propertyPath))
AzureIoTClient 4:233dd7616d73 153 {
AzureIoTClient 4:233dd7616d73 154 free(propertyPathCopy);
AzureIoTClient 4:233dd7616d73 155
AzureIoTClient 4:233dd7616d73 156 /* 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 157 result = DATA_PUBLISHER_SCHEMA_FAILED;
AzureIoTClient 4:233dd7616d73 158 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 159 }
AzureIoTClient 4:233dd7616d73 160 else if ((propertyValue = (AGENT_DATA_TYPE*)malloc(sizeof(AGENT_DATA_TYPE))) == NULL)
AzureIoTClient 4:233dd7616d73 161 {
AzureIoTClient 4:233dd7616d73 162 free(propertyPathCopy);
AzureIoTClient 4:233dd7616d73 163
AzureIoTClient 4:233dd7616d73 164 /* Codes_SRS_DATA_PUBLISHER_99_020:[ For any errors not explicitly mentioned here the DataPublisher APIs shall return DATA_PUBLISHER_ERROR.] */
AzureIoTClient 4:233dd7616d73 165 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 166 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 167 }
AzureIoTClient 4:233dd7616d73 168 else if (Create_AGENT_DATA_TYPE_from_AGENT_DATA_TYPE(propertyValue, data) != AGENT_DATA_TYPES_OK)
AzureIoTClient 4:233dd7616d73 169 {
AzureIoTClient 4:233dd7616d73 170 free(propertyPathCopy);
AzureIoTClient 4:233dd7616d73 171 free(propertyValue);
AzureIoTClient 4:233dd7616d73 172
AzureIoTClient 4:233dd7616d73 173 /* Codes_SRS_DATA_PUBLISHER_99_028:[ If creating the copy fails then DATA_PUBLISHER_AGENT_DATA_TYPES_ERROR shall be returned.] */
AzureIoTClient 4:233dd7616d73 174 result = DATA_PUBLISHER_AGENT_DATA_TYPES_ERROR;
AzureIoTClient 4:233dd7616d73 175 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 176 }
AzureIoTClient 4:233dd7616d73 177 else
AzureIoTClient 4:233dd7616d73 178 {
AzureIoTClient 4:233dd7616d73 179 size_t i;
AzureIoTClient 4:233dd7616d73 180 DATA_MARSHALLER_VALUE* propertySlot = NULL;
AzureIoTClient 4:233dd7616d73 181
AzureIoTClient 4:233dd7616d73 182 /* 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 183 for (i = 0; i < transaction->ValueCount; i++)
AzureIoTClient 4:233dd7616d73 184 {
AzureIoTClient 4:233dd7616d73 185 if (strcmp(transaction->Values[i].PropertyPath, propertyPath) == 0)
AzureIoTClient 4:233dd7616d73 186 {
AzureIoTClient 4:233dd7616d73 187 propertySlot = &transaction->Values[i];
AzureIoTClient 4:233dd7616d73 188 break;
AzureIoTClient 4:233dd7616d73 189 }
AzureIoTClient 4:233dd7616d73 190 }
AzureIoTClient 4:233dd7616d73 191
AzureIoTClient 4:233dd7616d73 192 if (propertySlot == NULL)
AzureIoTClient 4:233dd7616d73 193 {
AzureIoTClient 4:233dd7616d73 194 DATA_MARSHALLER_VALUE* newValues = (DATA_MARSHALLER_VALUE*)realloc(transaction->Values, sizeof(DATA_MARSHALLER_VALUE)* (transaction->ValueCount + 1));
AzureIoTClient 4:233dd7616d73 195 if (newValues != NULL)
AzureIoTClient 4:233dd7616d73 196 {
AzureIoTClient 4:233dd7616d73 197 transaction->Values = newValues;
AzureIoTClient 4:233dd7616d73 198 propertySlot = &transaction->Values[transaction->ValueCount];
AzureIoTClient 4:233dd7616d73 199 propertySlot->Value = NULL;
AzureIoTClient 4:233dd7616d73 200 propertySlot->PropertyPath = NULL;
AzureIoTClient 4:233dd7616d73 201 transaction->ValueCount++;
AzureIoTClient 4:233dd7616d73 202 }
AzureIoTClient 4:233dd7616d73 203 }
AzureIoTClient 4:233dd7616d73 204
AzureIoTClient 4:233dd7616d73 205 if (propertySlot == NULL)
AzureIoTClient 4:233dd7616d73 206 {
AzureIoTClient 4:233dd7616d73 207 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)propertyValue);
AzureIoTClient 4:233dd7616d73 208 free(propertyValue);
AzureIoTClient 4:233dd7616d73 209 free(propertyPathCopy);
AzureIoTClient 4:233dd7616d73 210
AzureIoTClient 4:233dd7616d73 211 /* Codes_SRS_DATA_PUBLISHER_99_020:[ For any errors not explicitly mentioned here the DataPublisher APIs shall return DATA_PUBLISHER_ERROR.] */
AzureIoTClient 4:233dd7616d73 212 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 213 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 214 }
AzureIoTClient 4:233dd7616d73 215 else
AzureIoTClient 4:233dd7616d73 216 {
AzureIoTClient 4:233dd7616d73 217 if (propertySlot->Value != NULL)
AzureIoTClient 4:233dd7616d73 218 {
AzureIoTClient 4:233dd7616d73 219 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)propertySlot->Value);
AzureIoTClient 4:233dd7616d73 220 free((AGENT_DATA_TYPE*)propertySlot->Value);
AzureIoTClient 4:233dd7616d73 221 }
AzureIoTClient 4:233dd7616d73 222 if (propertySlot->PropertyPath != NULL)
AzureIoTClient 4:233dd7616d73 223 {
AzureIoTClient 4:233dd7616d73 224 char* existingValue = (char*)propertySlot->PropertyPath;
AzureIoTClient 4:233dd7616d73 225 free(existingValue);
AzureIoTClient 4:233dd7616d73 226 }
AzureIoTClient 4:233dd7616d73 227
AzureIoTClient 4:233dd7616d73 228 /* 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 229 propertySlot->PropertyPath = propertyPathCopy;
AzureIoTClient 4:233dd7616d73 230 propertySlot->Value = propertyValue;
AzureIoTClient 4:233dd7616d73 231
AzureIoTClient 4:233dd7616d73 232 result = DATA_PUBLISHER_OK;
AzureIoTClient 4:233dd7616d73 233 }
AzureIoTClient 4:233dd7616d73 234 }
AzureIoTClient 4:233dd7616d73 235 }
AzureIoTClient 4:233dd7616d73 236
AzureIoTClient 4:233dd7616d73 237 return result;
AzureIoTClient 4:233dd7616d73 238 }
AzureIoTClient 4:233dd7616d73 239
AzureIoTClient 4:233dd7616d73 240 DATA_PUBLISHER_RESULT DataPublisher_EndTransaction(TRANSACTION_HANDLE transactionHandle, unsigned char** destination, size_t* destinationSize)
AzureIoTClient 4:233dd7616d73 241 {
AzureIoTClient 4:233dd7616d73 242 DATA_PUBLISHER_RESULT result;
AzureIoTClient 4:233dd7616d73 243
AzureIoTClient 4:233dd7616d73 244 /*Codes_SRS_DATA_PUBLISHER_02_006: [If the destination argument is NULL, DataPublisher_EndTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */
AzureIoTClient 4:233dd7616d73 245 /*Codes_SRS_DATA_PUBLISHER_02_007: [If the destinationSize argument is NULL, DataPublisher_EndTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */
AzureIoTClient 4:233dd7616d73 246 if (
AzureIoTClient 17:fa1bba4c6053 247 (transactionHandle == NULL) ||
AzureIoTClient 4:233dd7616d73 248 (destination == NULL) ||
AzureIoTClient 4:233dd7616d73 249 (destinationSize == NULL)
AzureIoTClient 4:233dd7616d73 250 )
AzureIoTClient 4:233dd7616d73 251 {
AzureIoTClient 4:233dd7616d73 252 /* Codes_SRS_DATA_PUBLISHER_99_011:[ If the transactionHandle argument is NULL, DataPublisher_EndTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */
AzureIoTClient 4:233dd7616d73 253 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 4:233dd7616d73 254 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 255 }
AzureIoTClient 4:233dd7616d73 256 else
AzureIoTClient 4:233dd7616d73 257 {
AzureIoTClient 17:fa1bba4c6053 258 TRANSACTION_HANDLE_DATA* transaction = (TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 4:233dd7616d73 259
AzureIoTClient 4:233dd7616d73 260 if (transaction->ValueCount == 0)
AzureIoTClient 4:233dd7616d73 261 {
AzureIoTClient 4:233dd7616d73 262 /* Codes_SRS_DATA_PUBLISHER_99_024:[ If no values have been associated with the transaction, no data shall be dispatched
AzureIoTClient 4:233dd7616d73 263 to DataMarshaller, the transaction shall be discarded and DataPublisher_EndTransaction shall return DATA_PUBLISHER_EMPTY_TRANSACTION.] */
AzureIoTClient 4:233dd7616d73 264 result = DATA_PUBLISHER_EMPTY_TRANSACTION;
AzureIoTClient 4:233dd7616d73 265 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 266 }
AzureIoTClient 4:233dd7616d73 267 /* 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 268 else if (DataMarshaller_SendData(transaction->DataPublisherInstance->DataMarshallerHandle, transaction->ValueCount, transaction->Values, destination, destinationSize) != DATA_MARSHALLER_OK)
AzureIoTClient 4:233dd7616d73 269 {
AzureIoTClient 4:233dd7616d73 270 /* Codes_SRS_DATA_PUBLISHER_99_025:[ When the DataMarshaller_SendData call fails, DataPublisher_EndTransaction shall return DATA_PUBLISHER_MARSHALLER_ERROR.] */
AzureIoTClient 4:233dd7616d73 271 result = DATA_PUBLISHER_MARSHALLER_ERROR;
AzureIoTClient 4:233dd7616d73 272 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 273 }
AzureIoTClient 4:233dd7616d73 274 else
AzureIoTClient 4:233dd7616d73 275 {
AzureIoTClient 4:233dd7616d73 276 /* Codes_SRS_DATA_PUBLISHER_99_026:[ On success, DataPublisher_EndTransaction shall return DATA_PUBLISHER_OK.] */
AzureIoTClient 4:233dd7616d73 277 result = DATA_PUBLISHER_OK;
AzureIoTClient 4:233dd7616d73 278 }
AzureIoTClient 4:233dd7616d73 279
AzureIoTClient 4:233dd7616d73 280 /* Codes_SRS_DATA_PUBLISHER_99_012:[ DataPublisher_EndTransaction shall dispose of any resources associated with the transaction.] */
AzureIoTClient 4:233dd7616d73 281 (void)DataPublisher_CancelTransaction(transactionHandle);
AzureIoTClient 4:233dd7616d73 282 }
AzureIoTClient 4:233dd7616d73 283
AzureIoTClient 4:233dd7616d73 284 return result;
AzureIoTClient 4:233dd7616d73 285 }
AzureIoTClient 4:233dd7616d73 286
AzureIoTClient 4:233dd7616d73 287 DATA_PUBLISHER_RESULT DataPublisher_CancelTransaction(TRANSACTION_HANDLE transactionHandle)
AzureIoTClient 4:233dd7616d73 288 {
AzureIoTClient 4:233dd7616d73 289 DATA_PUBLISHER_RESULT result;
AzureIoTClient 4:233dd7616d73 290
AzureIoTClient 4:233dd7616d73 291 if (transactionHandle == NULL)
AzureIoTClient 4:233dd7616d73 292 {
AzureIoTClient 4:233dd7616d73 293 /* Codes_SRS_DATA_PUBLISHER_99_014:[ If the transactionHandle argument is NULL DataPublisher_CancelTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */
AzureIoTClient 4:233dd7616d73 294 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 4:233dd7616d73 295 LOG_DATA_PUBLISHER_ERROR;
AzureIoTClient 4:233dd7616d73 296 }
AzureIoTClient 4:233dd7616d73 297 else
AzureIoTClient 4:233dd7616d73 298 {
AzureIoTClient 17:fa1bba4c6053 299 TRANSACTION_HANDLE_DATA* transaction = (TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 4:233dd7616d73 300 size_t i;
AzureIoTClient 4:233dd7616d73 301
AzureIoTClient 4:233dd7616d73 302 /* Codes_SRS_DATA_PUBLISHER_99_015:[ DataPublisher_CancelTransaction shall dispose of any resources associated with the transaction.] */
AzureIoTClient 4:233dd7616d73 303 for (i = 0; i < transaction->ValueCount; i++)
AzureIoTClient 4:233dd7616d73 304 {
AzureIoTClient 4:233dd7616d73 305 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)transaction->Values[i].Value);
AzureIoTClient 4:233dd7616d73 306 free((char*)transaction->Values[i].PropertyPath);
AzureIoTClient 4:233dd7616d73 307 free((AGENT_DATA_TYPE*)transaction->Values[i].Value);
AzureIoTClient 4:233dd7616d73 308 }
AzureIoTClient 4:233dd7616d73 309
AzureIoTClient 4:233dd7616d73 310 /* Codes_SRS_DATA_PUBLISHER_99_015:[ DataPublisher_CancelTransaction shall dispose of any resources associated with the transaction.] */
AzureIoTClient 4:233dd7616d73 311 free(transaction->Values);
AzureIoTClient 4:233dd7616d73 312 free(transaction);
AzureIoTClient 4:233dd7616d73 313
AzureIoTClient 17:fa1bba4c6053 314 /* Codes_SRS_DATA_PUBLISHER_99_013:[ A call to DataPublisher_CancelTransaction shall dispose of the transaction without dispatching
AzureIoTClient 4:233dd7616d73 315 the data to the DataMarshaller module and it shall return DATA_PUBLISHER_OK.] */
AzureIoTClient 4:233dd7616d73 316 result = DATA_PUBLISHER_OK;
AzureIoTClient 4:233dd7616d73 317 }
AzureIoTClient 4:233dd7616d73 318
AzureIoTClient 4:233dd7616d73 319 return result;
AzureIoTClient 4:233dd7616d73 320 }
AzureIoTClient 4:233dd7616d73 321
AzureIoTClient 4:233dd7616d73 322 /* 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 323 void DataPublisher_SetMaxBufferSize(size_t value)
AzureIoTClient 4:233dd7616d73 324 {
AzureIoTClient 4:233dd7616d73 325 maxBufferSize_ = value;
AzureIoTClient 4:233dd7616d73 326 }
AzureIoTClient 4:233dd7616d73 327
AzureIoTClient 4:233dd7616d73 328 /* 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 329 size_t DataPublisher_GetMaxBufferSize(void)
AzureIoTClient 4:233dd7616d73 330 {
AzureIoTClient 4:233dd7616d73 331 return maxBufferSize_;
AzureIoTClient 4:233dd7616d73 332 }
AzureIoTClient 17:fa1bba4c6053 333
AzureIoTClient 17:fa1bba4c6053 334 REPORTED_PROPERTIES_TRANSACTION_HANDLE DataPublisher_CreateTransaction_ReportedProperties(DATA_PUBLISHER_HANDLE dataPublisherHandle)
AzureIoTClient 17:fa1bba4c6053 335 {
AzureIoTClient 17:fa1bba4c6053 336 REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA* result;
AzureIoTClient 17:fa1bba4c6053 337 /*Codes_SRS_DATA_PUBLISHER_02_027: [ If argument dataPublisherHandle is NULL then DataPublisher_CreateTransaction_ReportedProperties shall fail and return NULL. ]*/
AzureIoTClient 17:fa1bba4c6053 338 if (dataPublisherHandle == NULL)
AzureIoTClient 17:fa1bba4c6053 339 {
AzureIoTClient 17:fa1bba4c6053 340 LogError("invalid argument DATA_PUBLISHER_HANDLE dataPublisherHandle=%p", dataPublisherHandle);
AzureIoTClient 17:fa1bba4c6053 341 result = NULL;
AzureIoTClient 17:fa1bba4c6053 342 }
AzureIoTClient 17:fa1bba4c6053 343 else
AzureIoTClient 17:fa1bba4c6053 344 {
AzureIoTClient 17:fa1bba4c6053 345 /*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 346 result = (REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA*)malloc(sizeof(REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA));
AzureIoTClient 17:fa1bba4c6053 347 if (result == NULL)
AzureIoTClient 17:fa1bba4c6053 348 {
AzureIoTClient 17:fa1bba4c6053 349 /*Codes_SRS_DATA_PUBLISHER_02_029: [ If any error occurs then DataPublisher_CreateTransaction_ReportedProperties shall fail and return NULL. ]*/
AzureIoTClient 17:fa1bba4c6053 350 LogError("unable to malloc");
AzureIoTClient 17:fa1bba4c6053 351 /*return as is */
AzureIoTClient 17:fa1bba4c6053 352 }
AzureIoTClient 17:fa1bba4c6053 353 else
AzureIoTClient 17:fa1bba4c6053 354 {
AzureIoTClient 17:fa1bba4c6053 355 result->value = VECTOR_create(sizeof(DATA_MARSHALLER_VALUE*));
AzureIoTClient 17:fa1bba4c6053 356 if (result->value == NULL)
AzureIoTClient 17:fa1bba4c6053 357 {
AzureIoTClient 17:fa1bba4c6053 358 /*Codes_SRS_DATA_PUBLISHER_02_029: [ If any error occurs then DataPublisher_CreateTransaction_ReportedProperties shall fail and return NULL. ]*/
AzureIoTClient 17:fa1bba4c6053 359 LogError("unable to VECTOR_create");
AzureIoTClient 17:fa1bba4c6053 360 free(result);
AzureIoTClient 17:fa1bba4c6053 361 result = NULL;
AzureIoTClient 17:fa1bba4c6053 362 }
AzureIoTClient 17:fa1bba4c6053 363 else
AzureIoTClient 17:fa1bba4c6053 364 {
AzureIoTClient 17:fa1bba4c6053 365 /*Codes_SRS_DATA_PUBLISHER_02_030: [ Otherwise DataPublisher_CreateTransaction_ReportedProperties shall succeed and return a non-NULL handle. ]*/
AzureIoTClient 17:fa1bba4c6053 366 result->DataPublisherInstance = dataPublisherHandle;
AzureIoTClient 17:fa1bba4c6053 367 }
AzureIoTClient 17:fa1bba4c6053 368 }
AzureIoTClient 17:fa1bba4c6053 369 }
AzureIoTClient 17:fa1bba4c6053 370
AzureIoTClient 17:fa1bba4c6053 371 return result;
AzureIoTClient 17:fa1bba4c6053 372 }
AzureIoTClient 17:fa1bba4c6053 373
AzureIoTClient 17:fa1bba4c6053 374 static bool reportedPropertyExistsByPath(const void* element, const void* value)
AzureIoTClient 17:fa1bba4c6053 375 {
AzureIoTClient 17:fa1bba4c6053 376 DATA_MARSHALLER_VALUE* dataMarshallerValue = *(DATA_MARSHALLER_VALUE**)element;
AzureIoTClient 17:fa1bba4c6053 377 return (strcmp(dataMarshallerValue->PropertyPath, (const char*)value) == 0);
AzureIoTClient 17:fa1bba4c6053 378 }
AzureIoTClient 17:fa1bba4c6053 379
AzureIoTClient 17:fa1bba4c6053 380 DATA_PUBLISHER_RESULT DataPublisher_PublishTransacted_ReportedProperty(REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle, const char* reportedPropertyPath, const AGENT_DATA_TYPE* data)
AzureIoTClient 17:fa1bba4c6053 381 {
AzureIoTClient 17:fa1bba4c6053 382 DATA_PUBLISHER_RESULT result;
AzureIoTClient 17:fa1bba4c6053 383 /*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 384 /*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 385 /*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 386 if (
AzureIoTClient 17:fa1bba4c6053 387 (transactionHandle == NULL) ||
AzureIoTClient 17:fa1bba4c6053 388 (reportedPropertyPath == NULL) ||
AzureIoTClient 17:fa1bba4c6053 389 (data == NULL)
AzureIoTClient 17:fa1bba4c6053 390 )
AzureIoTClient 17:fa1bba4c6053 391 {
AzureIoTClient 17:fa1bba4c6053 392 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 393 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 394 }
AzureIoTClient 17:fa1bba4c6053 395 else
AzureIoTClient 17:fa1bba4c6053 396 {
AzureIoTClient 17:fa1bba4c6053 397 REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA* handleData = (REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 17:fa1bba4c6053 398 /*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 399 if (!Schema_ModelReportedPropertyByPathExists(handleData->DataPublisherInstance->ModelHandle, reportedPropertyPath))
AzureIoTClient 17:fa1bba4c6053 400 {
AzureIoTClient 17:fa1bba4c6053 401 /*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 402 LogError("unable to find a reported property by path \"%s\"", reportedPropertyPath);
AzureIoTClient 17:fa1bba4c6053 403 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 404 }
AzureIoTClient 17:fa1bba4c6053 405 else
AzureIoTClient 17:fa1bba4c6053 406 {
AzureIoTClient 17:fa1bba4c6053 407 DATA_MARSHALLER_VALUE** existingValue = VECTOR_find_if(handleData->value, reportedPropertyExistsByPath, reportedPropertyPath);
AzureIoTClient 17:fa1bba4c6053 408 if(existingValue != NULL)
AzureIoTClient 17:fa1bba4c6053 409 {
AzureIoTClient 17:fa1bba4c6053 410 /*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 411 AGENT_DATA_TYPE *clone = (AGENT_DATA_TYPE *)malloc(sizeof(AGENT_DATA_TYPE));
AzureIoTClient 17:fa1bba4c6053 412 if(clone == NULL)
AzureIoTClient 17:fa1bba4c6053 413 {
AzureIoTClient 17:fa1bba4c6053 414 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 415 LogError("unable to malloc");
AzureIoTClient 17:fa1bba4c6053 416 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 417 }
AzureIoTClient 17:fa1bba4c6053 418 else
AzureIoTClient 17:fa1bba4c6053 419 {
AzureIoTClient 17:fa1bba4c6053 420 if (Create_AGENT_DATA_TYPE_from_AGENT_DATA_TYPE(clone, data) != AGENT_DATA_TYPES_OK)
AzureIoTClient 17:fa1bba4c6053 421 {
AzureIoTClient 17:fa1bba4c6053 422 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 423 LogError("unable to Create_AGENT_DATA_TYPE_from_AGENT_DATA_TYPE");
AzureIoTClient 17:fa1bba4c6053 424 free(clone);
AzureIoTClient 17:fa1bba4c6053 425 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 426 }
AzureIoTClient 17:fa1bba4c6053 427 else
AzureIoTClient 17:fa1bba4c6053 428 {
AzureIoTClient 17:fa1bba4c6053 429 /*Codes_SRS_DATA_PUBLISHER_02_017: [ Otherwise DataPublisher_PublishTransacted_ReportedProperty shall succeed and return DATA_PUBLISHER_OK. ]*/
AzureIoTClient 17:fa1bba4c6053 430 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)((*existingValue)->Value));
AzureIoTClient 17:fa1bba4c6053 431 free((void*)((*existingValue)->Value));
AzureIoTClient 17:fa1bba4c6053 432 (*existingValue)->Value = clone;
AzureIoTClient 17:fa1bba4c6053 433 result = DATA_PUBLISHER_OK;
AzureIoTClient 17:fa1bba4c6053 434 }
AzureIoTClient 17:fa1bba4c6053 435 }
AzureIoTClient 17:fa1bba4c6053 436 }
AzureIoTClient 17:fa1bba4c6053 437 else
AzureIoTClient 17:fa1bba4c6053 438 {
AzureIoTClient 17:fa1bba4c6053 439 /*totally new reported property*/
AzureIoTClient 17:fa1bba4c6053 440 DATA_MARSHALLER_VALUE* newValue = (DATA_MARSHALLER_VALUE*)malloc(sizeof(DATA_MARSHALLER_VALUE));
AzureIoTClient 17:fa1bba4c6053 441 if (newValue == NULL)
AzureIoTClient 17:fa1bba4c6053 442 {
AzureIoTClient 17:fa1bba4c6053 443 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 444 LogError("unable to malloc");
AzureIoTClient 17:fa1bba4c6053 445 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 446 }
AzureIoTClient 17:fa1bba4c6053 447 else
AzureIoTClient 17:fa1bba4c6053 448 {
AzureIoTClient 17:fa1bba4c6053 449 if (mallocAndStrcpy_s((char**)&(newValue->PropertyPath), reportedPropertyPath) != 0)
AzureIoTClient 17:fa1bba4c6053 450 {
AzureIoTClient 17:fa1bba4c6053 451 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 452 LogError("unable to mallocAndStrcpy_s");
AzureIoTClient 17:fa1bba4c6053 453 free(newValue);
AzureIoTClient 17:fa1bba4c6053 454 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 455 }
AzureIoTClient 17:fa1bba4c6053 456 else
AzureIoTClient 17:fa1bba4c6053 457 {
AzureIoTClient 17:fa1bba4c6053 458 if ((newValue->Value = (AGENT_DATA_TYPE*)malloc(sizeof(AGENT_DATA_TYPE))) == NULL)
AzureIoTClient 17:fa1bba4c6053 459 {
AzureIoTClient 17:fa1bba4c6053 460 LogError("unable to malloc");
AzureIoTClient 17:fa1bba4c6053 461 free((void*)newValue->PropertyPath);
AzureIoTClient 17:fa1bba4c6053 462 free(newValue);
AzureIoTClient 17:fa1bba4c6053 463 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 464 }
AzureIoTClient 17:fa1bba4c6053 465 else
AzureIoTClient 17:fa1bba4c6053 466 {
AzureIoTClient 17:fa1bba4c6053 467 if (Create_AGENT_DATA_TYPE_from_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)newValue->Value, data) != AGENT_DATA_TYPES_OK)
AzureIoTClient 17:fa1bba4c6053 468 {
AzureIoTClient 17:fa1bba4c6053 469 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 470 LogError("unable to Create_AGENT_DATA_TYPE_from_AGENT_DATA_TYPE");
AzureIoTClient 17:fa1bba4c6053 471 free((void*)newValue->Value);
AzureIoTClient 17:fa1bba4c6053 472 free((void*)newValue->PropertyPath);
AzureIoTClient 17:fa1bba4c6053 473 free(newValue);
AzureIoTClient 17:fa1bba4c6053 474 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 475 }
AzureIoTClient 17:fa1bba4c6053 476 else
AzureIoTClient 17:fa1bba4c6053 477 {
AzureIoTClient 17:fa1bba4c6053 478 /*Codes_SRS_DATA_PUBLISHER_02_015: [ DataPublisher_PublishTransacted_ReportedProperty shall add a new DATA_MARSHALLER_VALUE to the VECTOR_HANDLE. ]*/
AzureIoTClient 17:fa1bba4c6053 479 if (VECTOR_push_back(handleData->value, &newValue, 1) != 0)
AzureIoTClient 17:fa1bba4c6053 480 {
AzureIoTClient 17:fa1bba4c6053 481 /*Codes_SRS_DATA_PUBLISHER_02_016: [ If any error occurs then DataPublisher_PublishTransacted_ReportedProperty shall fail and return DATA_PUBLISHER_ERROR. */
AzureIoTClient 17:fa1bba4c6053 482 LogError("unable to VECTOR_push_back");
AzureIoTClient 17:fa1bba4c6053 483 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)newValue->Value);
AzureIoTClient 17:fa1bba4c6053 484 free((void*)newValue->Value);
AzureIoTClient 17:fa1bba4c6053 485 free((void*)newValue->PropertyPath);
AzureIoTClient 17:fa1bba4c6053 486 free(newValue);
AzureIoTClient 17:fa1bba4c6053 487 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 488 }
AzureIoTClient 17:fa1bba4c6053 489 else
AzureIoTClient 17:fa1bba4c6053 490 {
AzureIoTClient 17:fa1bba4c6053 491 /*Codes_SRS_DATA_PUBLISHER_02_017: [ Otherwise DataPublisher_PublishTransacted_ReportedProperty shall succeed and return DATA_PUBLISHER_OK. ]*/
AzureIoTClient 17:fa1bba4c6053 492 result = DATA_PUBLISHER_OK;
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 }
AzureIoTClient 17:fa1bba4c6053 500 }
AzureIoTClient 17:fa1bba4c6053 501 return result;
AzureIoTClient 17:fa1bba4c6053 502 }
AzureIoTClient 17:fa1bba4c6053 503
AzureIoTClient 17:fa1bba4c6053 504 DATA_PUBLISHER_RESULT DataPublisher_CommitTransaction_ReportedProperties(REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle, unsigned char** destination, size_t* destinationSize)
AzureIoTClient 17:fa1bba4c6053 505 {
AzureIoTClient 17:fa1bba4c6053 506 DATA_PUBLISHER_RESULT result;
AzureIoTClient 17:fa1bba4c6053 507 /*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 508 /*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 509 /*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 510 if (
AzureIoTClient 17:fa1bba4c6053 511 (transactionHandle == NULL) ||
AzureIoTClient 17:fa1bba4c6053 512 (destination == NULL) ||
AzureIoTClient 17:fa1bba4c6053 513 (destinationSize == NULL)
AzureIoTClient 17:fa1bba4c6053 514 )
AzureIoTClient 17:fa1bba4c6053 515 {
AzureIoTClient 17:fa1bba4c6053 516 LogError("invalid argument REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle=%p, unsigned char** destination=%p, size_t* destinationSize=%p", transactionHandle, destination, destinationSize);
AzureIoTClient 17:fa1bba4c6053 517 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 518 }
AzureIoTClient 17:fa1bba4c6053 519 else
AzureIoTClient 17:fa1bba4c6053 520 {
AzureIoTClient 17:fa1bba4c6053 521 /*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 522 REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA* handle = (REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 17:fa1bba4c6053 523 if (VECTOR_size(handle->value) == 0)
AzureIoTClient 17:fa1bba4c6053 524 {
AzureIoTClient 17:fa1bba4c6053 525 LogError("cannot commit empty transaction");
AzureIoTClient 17:fa1bba4c6053 526 result = DATA_PUBLISHER_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 527 }
AzureIoTClient 17:fa1bba4c6053 528 else
AzureIoTClient 17:fa1bba4c6053 529 {
AzureIoTClient 17:fa1bba4c6053 530 /*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 531 if (DataMarshaller_SendData_ReportedProperties(handle->DataPublisherInstance->DataMarshallerHandle, handle->value, destination, destinationSize) != DATA_MARSHALLER_OK)
AzureIoTClient 17:fa1bba4c6053 532 {
AzureIoTClient 17:fa1bba4c6053 533 /*Codes_SRS_DATA_PUBLISHER_02_023: [ If any error occurs then DataPublisher_CommitTransaction_ReportedProperties shall fail and return DATA_PUBLISHER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 534 LogError("unable to DataMarshaller_SendData_ReportedProperties");
AzureIoTClient 17:fa1bba4c6053 535 result = DATA_PUBLISHER_ERROR;
AzureIoTClient 17:fa1bba4c6053 536 }
AzureIoTClient 17:fa1bba4c6053 537 else
AzureIoTClient 17:fa1bba4c6053 538 {
AzureIoTClient 17:fa1bba4c6053 539 /*Codes_SRS_DATA_PUBLISHER_02_024: [ Otherwise DataPublisher_CommitTransaction_ReportedProperties shall succeed and return DATA_PUBLISHER_OK. ]*/
AzureIoTClient 17:fa1bba4c6053 540 result = DATA_PUBLISHER_OK;
AzureIoTClient 17:fa1bba4c6053 541 }
AzureIoTClient 17:fa1bba4c6053 542 }
AzureIoTClient 17:fa1bba4c6053 543 }
AzureIoTClient 17:fa1bba4c6053 544
AzureIoTClient 17:fa1bba4c6053 545 return result;
AzureIoTClient 17:fa1bba4c6053 546 }
AzureIoTClient 17:fa1bba4c6053 547
AzureIoTClient 17:fa1bba4c6053 548 void DataPublisher_DestroyTransaction_ReportedProperties(REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle)
AzureIoTClient 17:fa1bba4c6053 549 {
AzureIoTClient 17:fa1bba4c6053 550 /*Codes_SRS_DATA_PUBLISHER_02_025: [ If argument transactionHandle is NULL then DataPublisher_DestroyTransaction_ReportedProperties shall return. ]*/
AzureIoTClient 17:fa1bba4c6053 551 if (transactionHandle == NULL)
AzureIoTClient 17:fa1bba4c6053 552 {
AzureIoTClient 17:fa1bba4c6053 553 LogError("invalig argument REPORTED_PROPERTIES_TRANSACTION_HANDLE transactionHandle=%p", transactionHandle);
AzureIoTClient 17:fa1bba4c6053 554 }
AzureIoTClient 17:fa1bba4c6053 555 else
AzureIoTClient 17:fa1bba4c6053 556 {
AzureIoTClient 17:fa1bba4c6053 557 /*Codes_SRS_DATA_PUBLISHER_02_026: [ Otherwise DataPublisher_DestroyTransaction_ReportedProperties shall free all resources associated with the reported properties transactionHandle. ]*/
AzureIoTClient 17:fa1bba4c6053 558 REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA* handleData = (REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA*)transactionHandle;
AzureIoTClient 17:fa1bba4c6053 559 size_t i, nReportedProperties;
AzureIoTClient 17:fa1bba4c6053 560 nReportedProperties = VECTOR_size(handleData->value);
AzureIoTClient 17:fa1bba4c6053 561 for (i = 0;i < nReportedProperties;i++)
AzureIoTClient 17:fa1bba4c6053 562 {
AzureIoTClient 17:fa1bba4c6053 563 DATA_MARSHALLER_VALUE *value = *(DATA_MARSHALLER_VALUE**)VECTOR_element(handleData->value, i);
AzureIoTClient 17:fa1bba4c6053 564 Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)value->Value);
AzureIoTClient 17:fa1bba4c6053 565 free((void*)value->Value);
AzureIoTClient 17:fa1bba4c6053 566 free((void*)value->PropertyPath);
AzureIoTClient 17:fa1bba4c6053 567 free((void*)value);
AzureIoTClient 17:fa1bba4c6053 568 }
AzureIoTClient 17:fa1bba4c6053 569 VECTOR_destroy(handleData->value);
AzureIoTClient 17:fa1bba4c6053 570 free(handleData);
AzureIoTClient 17:fa1bba4c6053 571 }
AzureIoTClient 17:fa1bba4c6053 572 return;
AzureIoTClient 17:fa1bba4c6053 573 }