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
datapublisher.c@0:1f9b2707ec7d, 2015-09-15 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Tue Sep 15 21:57:26 2015 -0700
- Revision:
- 0:1f9b2707ec7d
New release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 0:1f9b2707ec7d | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 0:1f9b2707ec7d | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 0:1f9b2707ec7d | 3 | |
AzureIoTClient | 0:1f9b2707ec7d | 4 | #include <stdlib.h> |
AzureIoTClient | 0:1f9b2707ec7d | 5 | #ifdef _CRTDBG_MAP_ALLOC |
AzureIoTClient | 0:1f9b2707ec7d | 6 | #include <crtdbg.h> |
AzureIoTClient | 0:1f9b2707ec7d | 7 | #endif |
AzureIoTClient | 0:1f9b2707ec7d | 8 | #include "gballoc.h" |
AzureIoTClient | 0:1f9b2707ec7d | 9 | |
AzureIoTClient | 0:1f9b2707ec7d | 10 | #include <stdbool.h> |
AzureIoTClient | 0:1f9b2707ec7d | 11 | #include "datapublisher.h" |
AzureIoTClient | 0:1f9b2707ec7d | 12 | #include "jsonencoder.h" |
AzureIoTClient | 0:1f9b2707ec7d | 13 | #include "datamarshaller.h" |
AzureIoTClient | 0:1f9b2707ec7d | 14 | #include "agenttypesystem.h" |
AzureIoTClient | 0:1f9b2707ec7d | 15 | #include "schema.h" |
AzureIoTClient | 0:1f9b2707ec7d | 16 | #include "crt_abstractions.h" |
AzureIoTClient | 0:1f9b2707ec7d | 17 | #include "iot_logging.h" |
AzureIoTClient | 0:1f9b2707ec7d | 18 | |
AzureIoTClient | 0:1f9b2707ec7d | 19 | DEFINE_ENUM_STRINGS(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_RESULT_VALUES) |
AzureIoTClient | 0:1f9b2707ec7d | 20 | |
AzureIoTClient | 0:1f9b2707ec7d | 21 | #define LOG_DATA_PUBLISHER_ERROR \ |
AzureIoTClient | 0:1f9b2707ec7d | 22 | LogError("(result = %s)\r\n", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, result)) |
AzureIoTClient | 0:1f9b2707ec7d | 23 | |
AzureIoTClient | 0:1f9b2707ec7d | 24 | #define DEFAULT_MAX_BUFFER_SIZE 10240 |
AzureIoTClient | 0:1f9b2707ec7d | 25 | /* Codes_SRS_DATA_PUBLISHER_99_066:[ A single value shall be used by all instances of DataPublisher.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 26 | /* Codes_SRS_DATA_PUBLISHER_99_067:[ Before any call to DataPublisher_SetMaxBufferSize, the default max buffer size shall be equal to 10KB.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 27 | static size_t maxBufferSize_ = DEFAULT_MAX_BUFFER_SIZE; |
AzureIoTClient | 0:1f9b2707ec7d | 28 | |
AzureIoTClient | 0:1f9b2707ec7d | 29 | typedef struct DATA_PUBLISHER_INSTANCE_TAG |
AzureIoTClient | 0:1f9b2707ec7d | 30 | { |
AzureIoTClient | 0:1f9b2707ec7d | 31 | DATA_MARSHALLER_HANDLE DataMarshallerHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 32 | SCHEMA_MODEL_TYPE_HANDLE ModelHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 33 | } DATA_PUBLISHER_INSTANCE; |
AzureIoTClient | 0:1f9b2707ec7d | 34 | |
AzureIoTClient | 0:1f9b2707ec7d | 35 | typedef struct TRANSACTION_TAG |
AzureIoTClient | 0:1f9b2707ec7d | 36 | { |
AzureIoTClient | 0:1f9b2707ec7d | 37 | DATA_PUBLISHER_INSTANCE* DataPublisherInstance; |
AzureIoTClient | 0:1f9b2707ec7d | 38 | size_t ValueCount; |
AzureIoTClient | 0:1f9b2707ec7d | 39 | DATA_MARSHALLER_VALUE* Values; |
AzureIoTClient | 0:1f9b2707ec7d | 40 | } TRANSACTION; |
AzureIoTClient | 0:1f9b2707ec7d | 41 | |
AzureIoTClient | 0:1f9b2707ec7d | 42 | DATA_PUBLISHER_HANDLE DataPublisher_Create(SCHEMA_MODEL_TYPE_HANDLE modelHandle, bool includePropertyPath) |
AzureIoTClient | 0:1f9b2707ec7d | 43 | { |
AzureIoTClient | 0:1f9b2707ec7d | 44 | DATA_PUBLISHER_HANDLE result; |
AzureIoTClient | 0:1f9b2707ec7d | 45 | DATA_PUBLISHER_INSTANCE* dataPublisherInstance; |
AzureIoTClient | 0:1f9b2707ec7d | 46 | |
AzureIoTClient | 0:1f9b2707ec7d | 47 | /* Codes_SRS_DATA_PUBLISHER_99_042:[ If a NULL argument is passed to it, DataPublisher_Create shall return NULL.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 48 | if ( |
AzureIoTClient | 0:1f9b2707ec7d | 49 | (modelHandle == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 50 | ) |
AzureIoTClient | 0:1f9b2707ec7d | 51 | { |
AzureIoTClient | 0:1f9b2707ec7d | 52 | result = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 53 | LogError("(result = %s)\r\n", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_INVALID_ARG)); |
AzureIoTClient | 0:1f9b2707ec7d | 54 | } |
AzureIoTClient | 0:1f9b2707ec7d | 55 | else if ((dataPublisherInstance = (DATA_PUBLISHER_INSTANCE*)malloc(sizeof(DATA_PUBLISHER_INSTANCE))) == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 56 | { |
AzureIoTClient | 0:1f9b2707ec7d | 57 | /* Codes_SRS_DATA_PUBLISHER_99_047:[ For any other error not specified here, DataPublisher_Create shall return NULL.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 58 | result = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 59 | LogError("(result = %s)\r\n", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_ERROR)); |
AzureIoTClient | 0:1f9b2707ec7d | 60 | } |
AzureIoTClient | 0:1f9b2707ec7d | 61 | else |
AzureIoTClient | 0:1f9b2707ec7d | 62 | { |
AzureIoTClient | 0:1f9b2707ec7d | 63 | /* Codes_SRS_DATA_PUBLISHER_99_043:[ DataPublisher_Create shall initialize and hold a handle to a DataMarshaller instance.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 64 | /* Codes_SRS_DATA_PUBLISHER_01_001: [DataPublisher_Create shall pass the includePropertyPath argument to DataMarshaller_Create.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 65 | if ((dataPublisherInstance->DataMarshallerHandle = DataMarshaller_Create(modelHandle, includePropertyPath)) == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 66 | { |
AzureIoTClient | 0:1f9b2707ec7d | 67 | free(dataPublisherInstance); |
AzureIoTClient | 0:1f9b2707ec7d | 68 | |
AzureIoTClient | 0:1f9b2707ec7d | 69 | /* Codes_SRS_DATA_PUBLISHER_99_044:[ If the creation of the DataMarshaller instance fails, DataPublisher_Create shall return NULL.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 70 | result = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 71 | LogError("(result = %s)\r\n", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_MARSHALLER_ERROR)); |
AzureIoTClient | 0:1f9b2707ec7d | 72 | } |
AzureIoTClient | 0:1f9b2707ec7d | 73 | else |
AzureIoTClient | 0:1f9b2707ec7d | 74 | { |
AzureIoTClient | 0:1f9b2707ec7d | 75 | dataPublisherInstance->ModelHandle = modelHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 76 | |
AzureIoTClient | 0:1f9b2707ec7d | 77 | /* 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 | 0:1f9b2707ec7d | 78 | result = dataPublisherInstance; |
AzureIoTClient | 0:1f9b2707ec7d | 79 | } |
AzureIoTClient | 0:1f9b2707ec7d | 80 | } |
AzureIoTClient | 0:1f9b2707ec7d | 81 | |
AzureIoTClient | 0:1f9b2707ec7d | 82 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 83 | } |
AzureIoTClient | 0:1f9b2707ec7d | 84 | |
AzureIoTClient | 0:1f9b2707ec7d | 85 | void DataPublisher_Destroy(DATA_PUBLISHER_HANDLE dataPublisherHandle) |
AzureIoTClient | 0:1f9b2707ec7d | 86 | { |
AzureIoTClient | 0:1f9b2707ec7d | 87 | if (dataPublisherHandle != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 88 | { |
AzureIoTClient | 0:1f9b2707ec7d | 89 | DATA_PUBLISHER_INSTANCE* dataPublisherInstance = (DATA_PUBLISHER_INSTANCE*)dataPublisherHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 90 | DataMarshaller_Destroy(dataPublisherInstance->DataMarshallerHandle); |
AzureIoTClient | 0:1f9b2707ec7d | 91 | |
AzureIoTClient | 0:1f9b2707ec7d | 92 | free(dataPublisherHandle); |
AzureIoTClient | 0:1f9b2707ec7d | 93 | } |
AzureIoTClient | 0:1f9b2707ec7d | 94 | } |
AzureIoTClient | 0:1f9b2707ec7d | 95 | |
AzureIoTClient | 0:1f9b2707ec7d | 96 | TRANSACTION_HANDLE DataPublisher_StartTransaction(DATA_PUBLISHER_HANDLE dataPublisherHandle) |
AzureIoTClient | 0:1f9b2707ec7d | 97 | { |
AzureIoTClient | 0:1f9b2707ec7d | 98 | TRANSACTION* transaction; |
AzureIoTClient | 0:1f9b2707ec7d | 99 | |
AzureIoTClient | 0:1f9b2707ec7d | 100 | /* Codes_SRS_DATA_PUBLISHER_99_038:[ If DataPublisher_StartTransaction is called with a NULL argument it shall return NULL.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 101 | if (dataPublisherHandle == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 102 | { |
AzureIoTClient | 0:1f9b2707ec7d | 103 | transaction = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 104 | LogError("(Error code: %s)\r\n", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_INVALID_ARG)); |
AzureIoTClient | 0:1f9b2707ec7d | 105 | } |
AzureIoTClient | 0:1f9b2707ec7d | 106 | else |
AzureIoTClient | 0:1f9b2707ec7d | 107 | { |
AzureIoTClient | 0:1f9b2707ec7d | 108 | /* Codes_SRS_DATA_PUBLISHER_99_007:[ A call to DataPublisher_StartTransaction shall start a new transaction.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 109 | transaction = (TRANSACTION*)malloc(sizeof(TRANSACTION)); |
AzureIoTClient | 0:1f9b2707ec7d | 110 | if (transaction == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 111 | { |
AzureIoTClient | 0:1f9b2707ec7d | 112 | LogError("Allocating transaction failed (Error code: %s)\r\n", ENUM_TO_STRING(DATA_PUBLISHER_RESULT, DATA_PUBLISHER_ERROR)); |
AzureIoTClient | 0:1f9b2707ec7d | 113 | } |
AzureIoTClient | 0:1f9b2707ec7d | 114 | else |
AzureIoTClient | 0:1f9b2707ec7d | 115 | { |
AzureIoTClient | 0:1f9b2707ec7d | 116 | transaction->ValueCount = 0; |
AzureIoTClient | 0:1f9b2707ec7d | 117 | transaction->Values = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 118 | transaction->DataPublisherInstance = (DATA_PUBLISHER_INSTANCE*)dataPublisherHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 119 | } |
AzureIoTClient | 0:1f9b2707ec7d | 120 | } |
AzureIoTClient | 0:1f9b2707ec7d | 121 | |
AzureIoTClient | 0:1f9b2707ec7d | 122 | /* Codes_SRS_DATA_PUBLISHER_99_008:[ DataPublisher_StartTransaction shall return a non-NULL handle upon success.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 123 | /* Codes_SRS_DATA_PUBLISHER_99_009:[ DataPublisher_StartTransaction shall return NULL upon failure.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 124 | return transaction; |
AzureIoTClient | 0:1f9b2707ec7d | 125 | } |
AzureIoTClient | 0:1f9b2707ec7d | 126 | |
AzureIoTClient | 0:1f9b2707ec7d | 127 | DATA_PUBLISHER_RESULT DataPublisher_PublishTransacted(TRANSACTION_HANDLE transactionHandle, const char* propertyPath, const AGENT_DATA_TYPE* data) |
AzureIoTClient | 0:1f9b2707ec7d | 128 | { |
AzureIoTClient | 0:1f9b2707ec7d | 129 | DATA_PUBLISHER_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 130 | char* propertyPathCopy; |
AzureIoTClient | 0:1f9b2707ec7d | 131 | |
AzureIoTClient | 0:1f9b2707ec7d | 132 | /* 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 | 0:1f9b2707ec7d | 133 | if ((transactionHandle == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 134 | (propertyPath == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 135 | (data == NULL)) |
AzureIoTClient | 0:1f9b2707ec7d | 136 | { |
AzureIoTClient | 0:1f9b2707ec7d | 137 | result = DATA_PUBLISHER_INVALID_ARG; |
AzureIoTClient | 0:1f9b2707ec7d | 138 | LOG_DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 139 | } |
AzureIoTClient | 0:1f9b2707ec7d | 140 | else if (mallocAndStrcpy_s(&propertyPathCopy, propertyPath) != 0) |
AzureIoTClient | 0:1f9b2707ec7d | 141 | { |
AzureIoTClient | 0:1f9b2707ec7d | 142 | /* Codes_SRS_DATA_PUBLISHER_99_020:[ For any errors not explicitly mentioned here the DataPublisher APIs shall return DATA_PUBLISHER_ERROR.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 143 | result = DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 144 | LOG_DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 145 | } |
AzureIoTClient | 0:1f9b2707ec7d | 146 | else |
AzureIoTClient | 0:1f9b2707ec7d | 147 | { |
AzureIoTClient | 0:1f9b2707ec7d | 148 | TRANSACTION* transaction = (TRANSACTION*)transactionHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 149 | AGENT_DATA_TYPE* propertyValue; |
AzureIoTClient | 0:1f9b2707ec7d | 150 | |
AzureIoTClient | 0:1f9b2707ec7d | 151 | if (!Schema_ModelPropertyByPathExists(transaction->DataPublisherInstance->ModelHandle, propertyPath)) |
AzureIoTClient | 0:1f9b2707ec7d | 152 | { |
AzureIoTClient | 0:1f9b2707ec7d | 153 | free(propertyPathCopy); |
AzureIoTClient | 0:1f9b2707ec7d | 154 | |
AzureIoTClient | 0:1f9b2707ec7d | 155 | /* 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 | 0:1f9b2707ec7d | 156 | result = DATA_PUBLISHER_SCHEMA_FAILED; |
AzureIoTClient | 0:1f9b2707ec7d | 157 | LOG_DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 158 | } |
AzureIoTClient | 0:1f9b2707ec7d | 159 | else if ((propertyValue = (AGENT_DATA_TYPE*)malloc(sizeof(AGENT_DATA_TYPE))) == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 160 | { |
AzureIoTClient | 0:1f9b2707ec7d | 161 | free(propertyPathCopy); |
AzureIoTClient | 0:1f9b2707ec7d | 162 | |
AzureIoTClient | 0:1f9b2707ec7d | 163 | /* Codes_SRS_DATA_PUBLISHER_99_020:[ For any errors not explicitly mentioned here the DataPublisher APIs shall return DATA_PUBLISHER_ERROR.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 164 | result = DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 165 | LOG_DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 166 | } |
AzureIoTClient | 0:1f9b2707ec7d | 167 | else if (Create_AGENT_DATA_TYPE_from_AGENT_DATA_TYPE(propertyValue, data) != AGENT_DATA_TYPES_OK) |
AzureIoTClient | 0:1f9b2707ec7d | 168 | { |
AzureIoTClient | 0:1f9b2707ec7d | 169 | free(propertyPathCopy); |
AzureIoTClient | 0:1f9b2707ec7d | 170 | free(propertyValue); |
AzureIoTClient | 0:1f9b2707ec7d | 171 | |
AzureIoTClient | 0:1f9b2707ec7d | 172 | /* Codes_SRS_DATA_PUBLISHER_99_028:[ If creating the copy fails then DATA_PUBLISHER_AGENT_DATA_TYPES_ERROR shall be returned.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 173 | result = DATA_PUBLISHER_AGENT_DATA_TYPES_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 174 | LOG_DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 175 | } |
AzureIoTClient | 0:1f9b2707ec7d | 176 | else |
AzureIoTClient | 0:1f9b2707ec7d | 177 | { |
AzureIoTClient | 0:1f9b2707ec7d | 178 | size_t i; |
AzureIoTClient | 0:1f9b2707ec7d | 179 | DATA_MARSHALLER_VALUE* propertySlot = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 180 | |
AzureIoTClient | 0:1f9b2707ec7d | 181 | /* 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 | 0:1f9b2707ec7d | 182 | for (i = 0; i < transaction->ValueCount; i++) |
AzureIoTClient | 0:1f9b2707ec7d | 183 | { |
AzureIoTClient | 0:1f9b2707ec7d | 184 | if (strcmp(transaction->Values[i].PropertyPath, propertyPath) == 0) |
AzureIoTClient | 0:1f9b2707ec7d | 185 | { |
AzureIoTClient | 0:1f9b2707ec7d | 186 | propertySlot = &transaction->Values[i]; |
AzureIoTClient | 0:1f9b2707ec7d | 187 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 188 | } |
AzureIoTClient | 0:1f9b2707ec7d | 189 | } |
AzureIoTClient | 0:1f9b2707ec7d | 190 | |
AzureIoTClient | 0:1f9b2707ec7d | 191 | if (propertySlot == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 192 | { |
AzureIoTClient | 0:1f9b2707ec7d | 193 | DATA_MARSHALLER_VALUE* newValues = (DATA_MARSHALLER_VALUE*)realloc(transaction->Values, sizeof(DATA_MARSHALLER_VALUE)* (transaction->ValueCount + 1)); |
AzureIoTClient | 0:1f9b2707ec7d | 194 | if (newValues != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 195 | { |
AzureIoTClient | 0:1f9b2707ec7d | 196 | transaction->Values = newValues; |
AzureIoTClient | 0:1f9b2707ec7d | 197 | propertySlot = &transaction->Values[transaction->ValueCount]; |
AzureIoTClient | 0:1f9b2707ec7d | 198 | propertySlot->Value = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 199 | propertySlot->PropertyPath = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 200 | transaction->ValueCount++; |
AzureIoTClient | 0:1f9b2707ec7d | 201 | } |
AzureIoTClient | 0:1f9b2707ec7d | 202 | } |
AzureIoTClient | 0:1f9b2707ec7d | 203 | |
AzureIoTClient | 0:1f9b2707ec7d | 204 | if (propertySlot == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 205 | { |
AzureIoTClient | 0:1f9b2707ec7d | 206 | Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)propertyValue); |
AzureIoTClient | 0:1f9b2707ec7d | 207 | free(propertyValue); |
AzureIoTClient | 0:1f9b2707ec7d | 208 | free(propertyPathCopy); |
AzureIoTClient | 0:1f9b2707ec7d | 209 | |
AzureIoTClient | 0:1f9b2707ec7d | 210 | /* Codes_SRS_DATA_PUBLISHER_99_020:[ For any errors not explicitly mentioned here the DataPublisher APIs shall return DATA_PUBLISHER_ERROR.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 211 | result = DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 212 | LOG_DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 213 | } |
AzureIoTClient | 0:1f9b2707ec7d | 214 | else |
AzureIoTClient | 0:1f9b2707ec7d | 215 | { |
AzureIoTClient | 0:1f9b2707ec7d | 216 | if (propertySlot->Value != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 217 | { |
AzureIoTClient | 0:1f9b2707ec7d | 218 | Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)propertySlot->Value); |
AzureIoTClient | 0:1f9b2707ec7d | 219 | free((AGENT_DATA_TYPE*)propertySlot->Value); |
AzureIoTClient | 0:1f9b2707ec7d | 220 | } |
AzureIoTClient | 0:1f9b2707ec7d | 221 | if (propertySlot->PropertyPath != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 222 | { |
AzureIoTClient | 0:1f9b2707ec7d | 223 | char* existingValue = (char*)propertySlot->PropertyPath; |
AzureIoTClient | 0:1f9b2707ec7d | 224 | free(existingValue); |
AzureIoTClient | 0:1f9b2707ec7d | 225 | } |
AzureIoTClient | 0:1f9b2707ec7d | 226 | |
AzureIoTClient | 0:1f9b2707ec7d | 227 | /* 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 | 0:1f9b2707ec7d | 228 | propertySlot->PropertyPath = propertyPathCopy; |
AzureIoTClient | 0:1f9b2707ec7d | 229 | propertySlot->Value = propertyValue; |
AzureIoTClient | 0:1f9b2707ec7d | 230 | |
AzureIoTClient | 0:1f9b2707ec7d | 231 | result = DATA_PUBLISHER_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 232 | } |
AzureIoTClient | 0:1f9b2707ec7d | 233 | } |
AzureIoTClient | 0:1f9b2707ec7d | 234 | } |
AzureIoTClient | 0:1f9b2707ec7d | 235 | |
AzureIoTClient | 0:1f9b2707ec7d | 236 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 237 | } |
AzureIoTClient | 0:1f9b2707ec7d | 238 | |
AzureIoTClient | 0:1f9b2707ec7d | 239 | DATA_PUBLISHER_RESULT DataPublisher_EndTransaction(TRANSACTION_HANDLE transactionHandle, unsigned char** destination, size_t* destinationSize) |
AzureIoTClient | 0:1f9b2707ec7d | 240 | { |
AzureIoTClient | 0:1f9b2707ec7d | 241 | DATA_PUBLISHER_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 242 | |
AzureIoTClient | 0:1f9b2707ec7d | 243 | /*Codes_SRS_DATA_PUBLISHER_02_006: [If the destination argument is NULL, DataPublisher_EndTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 244 | /*Codes_SRS_DATA_PUBLISHER_02_007: [If the destinationSize argument is NULL, DataPublisher_EndTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 245 | if ( |
AzureIoTClient | 0:1f9b2707ec7d | 246 | (transactionHandle == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 247 | (destination == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 248 | (destinationSize == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 249 | ) |
AzureIoTClient | 0:1f9b2707ec7d | 250 | { |
AzureIoTClient | 0:1f9b2707ec7d | 251 | /* Codes_SRS_DATA_PUBLISHER_99_011:[ If the transactionHandle argument is NULL, DataPublisher_EndTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 252 | result = DATA_PUBLISHER_INVALID_ARG; |
AzureIoTClient | 0:1f9b2707ec7d | 253 | LOG_DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 254 | } |
AzureIoTClient | 0:1f9b2707ec7d | 255 | else |
AzureIoTClient | 0:1f9b2707ec7d | 256 | { |
AzureIoTClient | 0:1f9b2707ec7d | 257 | TRANSACTION* transaction = (TRANSACTION*)transactionHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 258 | |
AzureIoTClient | 0:1f9b2707ec7d | 259 | if (transaction->ValueCount == 0) |
AzureIoTClient | 0:1f9b2707ec7d | 260 | { |
AzureIoTClient | 0:1f9b2707ec7d | 261 | /* Codes_SRS_DATA_PUBLISHER_99_024:[ If no values have been associated with the transaction, no data shall be dispatched |
AzureIoTClient | 0:1f9b2707ec7d | 262 | to DataMarshaller, the transaction shall be discarded and DataPublisher_EndTransaction shall return DATA_PUBLISHER_EMPTY_TRANSACTION.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 263 | result = DATA_PUBLISHER_EMPTY_TRANSACTION; |
AzureIoTClient | 0:1f9b2707ec7d | 264 | LOG_DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 265 | } |
AzureIoTClient | 0:1f9b2707ec7d | 266 | /* 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 | 0:1f9b2707ec7d | 267 | else if (DataMarshaller_SendData(transaction->DataPublisherInstance->DataMarshallerHandle, transaction->ValueCount, transaction->Values, destination, destinationSize) != DATA_MARSHALLER_OK) |
AzureIoTClient | 0:1f9b2707ec7d | 268 | { |
AzureIoTClient | 0:1f9b2707ec7d | 269 | /* Codes_SRS_DATA_PUBLISHER_99_025:[ When the DataMarshaller_SendData call fails, DataPublisher_EndTransaction shall return DATA_PUBLISHER_MARSHALLER_ERROR.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 270 | result = DATA_PUBLISHER_MARSHALLER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 271 | LOG_DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 272 | } |
AzureIoTClient | 0:1f9b2707ec7d | 273 | else |
AzureIoTClient | 0:1f9b2707ec7d | 274 | { |
AzureIoTClient | 0:1f9b2707ec7d | 275 | /* Codes_SRS_DATA_PUBLISHER_99_026:[ On success, DataPublisher_EndTransaction shall return DATA_PUBLISHER_OK.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 276 | result = DATA_PUBLISHER_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 277 | } |
AzureIoTClient | 0:1f9b2707ec7d | 278 | |
AzureIoTClient | 0:1f9b2707ec7d | 279 | /* Codes_SRS_DATA_PUBLISHER_99_012:[ DataPublisher_EndTransaction shall dispose of any resources associated with the transaction.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 280 | (void)DataPublisher_CancelTransaction(transactionHandle); |
AzureIoTClient | 0:1f9b2707ec7d | 281 | } |
AzureIoTClient | 0:1f9b2707ec7d | 282 | |
AzureIoTClient | 0:1f9b2707ec7d | 283 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 284 | } |
AzureIoTClient | 0:1f9b2707ec7d | 285 | |
AzureIoTClient | 0:1f9b2707ec7d | 286 | DATA_PUBLISHER_RESULT DataPublisher_CancelTransaction(TRANSACTION_HANDLE transactionHandle) |
AzureIoTClient | 0:1f9b2707ec7d | 287 | { |
AzureIoTClient | 0:1f9b2707ec7d | 288 | DATA_PUBLISHER_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 289 | |
AzureIoTClient | 0:1f9b2707ec7d | 290 | if (transactionHandle == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 291 | { |
AzureIoTClient | 0:1f9b2707ec7d | 292 | /* Codes_SRS_DATA_PUBLISHER_99_014:[ If the transactionHandle argument is NULL DataPublisher_CancelTransaction shall return DATA_PUBLISHER_INVALID_ARG.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 293 | result = DATA_PUBLISHER_INVALID_ARG; |
AzureIoTClient | 0:1f9b2707ec7d | 294 | LOG_DATA_PUBLISHER_ERROR; |
AzureIoTClient | 0:1f9b2707ec7d | 295 | } |
AzureIoTClient | 0:1f9b2707ec7d | 296 | else |
AzureIoTClient | 0:1f9b2707ec7d | 297 | { |
AzureIoTClient | 0:1f9b2707ec7d | 298 | TRANSACTION* transaction = (TRANSACTION*)transactionHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 299 | size_t i; |
AzureIoTClient | 0:1f9b2707ec7d | 300 | |
AzureIoTClient | 0:1f9b2707ec7d | 301 | /* Codes_SRS_DATA_PUBLISHER_99_015:[ DataPublisher_CancelTransaction shall dispose of any resources associated with the transaction.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 302 | for (i = 0; i < transaction->ValueCount; i++) |
AzureIoTClient | 0:1f9b2707ec7d | 303 | { |
AzureIoTClient | 0:1f9b2707ec7d | 304 | Destroy_AGENT_DATA_TYPE((AGENT_DATA_TYPE*)transaction->Values[i].Value); |
AzureIoTClient | 0:1f9b2707ec7d | 305 | free((char*)transaction->Values[i].PropertyPath); |
AzureIoTClient | 0:1f9b2707ec7d | 306 | free((AGENT_DATA_TYPE*)transaction->Values[i].Value); |
AzureIoTClient | 0:1f9b2707ec7d | 307 | } |
AzureIoTClient | 0:1f9b2707ec7d | 308 | |
AzureIoTClient | 0:1f9b2707ec7d | 309 | /* Codes_SRS_DATA_PUBLISHER_99_015:[ DataPublisher_CancelTransaction shall dispose of any resources associated with the transaction.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 310 | free(transaction->Values); |
AzureIoTClient | 0:1f9b2707ec7d | 311 | free(transaction); |
AzureIoTClient | 0:1f9b2707ec7d | 312 | |
AzureIoTClient | 0:1f9b2707ec7d | 313 | /* Codes_SRS_DATA_PUBLISHER_99_013:[ A call to DataPublisher_CancelTransaction shall dispose of the transaction without dispatching |
AzureIoTClient | 0:1f9b2707ec7d | 314 | the data to the DataMarshaller module and it shall return DATA_PUBLISHER_OK.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 315 | result = DATA_PUBLISHER_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 316 | } |
AzureIoTClient | 0:1f9b2707ec7d | 317 | |
AzureIoTClient | 0:1f9b2707ec7d | 318 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 319 | } |
AzureIoTClient | 0:1f9b2707ec7d | 320 | |
AzureIoTClient | 0:1f9b2707ec7d | 321 | /* 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 | 0:1f9b2707ec7d | 322 | void DataPublisher_SetMaxBufferSize(size_t value) |
AzureIoTClient | 0:1f9b2707ec7d | 323 | { |
AzureIoTClient | 0:1f9b2707ec7d | 324 | maxBufferSize_ = value; |
AzureIoTClient | 0:1f9b2707ec7d | 325 | } |
AzureIoTClient | 0:1f9b2707ec7d | 326 | |
AzureIoTClient | 0:1f9b2707ec7d | 327 | /* Codes_SRS_DATA_PUBLISHER_99_069:[ DataMarshaller_GetMaxBufferSize shall return the current max buffer size value used by any new instance of DataMarshaller.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 328 | size_t DataPublisher_GetMaxBufferSize(void) |
AzureIoTClient | 0:1f9b2707ec7d | 329 | { |
AzureIoTClient | 0:1f9b2707ec7d | 330 | return maxBufferSize_; |
AzureIoTClient | 0:1f9b2707ec7d | 331 | } |