A modelling and serializer library for Microsoft Azure IoTHub client applications

Dependents:   sht15_remote_monitoring f767zi_mqtt remote_monitoring simplesample_amqp ... more

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

Committer:
AzureIoTClient
Date:
Tue Sep 11 11:14:37 2018 -0700
Revision:
36:7d12a5386197
Parent:
21:6d3dea1abd9c
1.2.9

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 0:1f9b2707ec7d 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 0:1f9b2707ec7d 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 0:1f9b2707ec7d 3
Azure.IoT Build 10:c2aee3965a83 4 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 0:1f9b2707ec7d 5
AzureIoTClient 0:1f9b2707ec7d 6 #include "dataserializer.h"
Azure.IoT Build 13:16e88f0cfa5f 7 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 0:1f9b2707ec7d 8
AzureIoTClient 0:1f9b2707ec7d 9 DEFINE_ENUM_STRINGS(DATA_SERIALIZER_RESULT, DATA_SERIALIZER_RESULT_VALUES);
AzureIoTClient 0:1f9b2707ec7d 10
AzureIoTClient 0:1f9b2707ec7d 11 BUFFER_HANDLE DataSerializer_Encode(MULTITREE_HANDLE multiTreeHandle, DATA_SERIALIZER_MULTITREE_TYPE dataType, DATA_SERIALIZER_ENCODE_FUNC encodeFunc)
AzureIoTClient 0:1f9b2707ec7d 12 {
AzureIoTClient 0:1f9b2707ec7d 13 BUFFER_HANDLE pBuffer;
AzureIoTClient 0:1f9b2707ec7d 14
AzureIoTClient 0:1f9b2707ec7d 15 /* Codes_SRS_DATA_SERIALIZER_07_003: [NULL shall be returned when an invalid parameter is supplied.] */
AzureIoTClient 0:1f9b2707ec7d 16 if (multiTreeHandle == NULL || encodeFunc == NULL)
AzureIoTClient 0:1f9b2707ec7d 17 {
AzureIoTClient 0:1f9b2707ec7d 18 pBuffer = NULL;
AzureIoTClient 11:b1327861f5e0 19 LogError("(Error code: %s)", ENUM_TO_STRING(DATA_SERIALIZER_RESULT, DATA_SERIALIZER_INVALID_ARG) );
AzureIoTClient 0:1f9b2707ec7d 20 }
AzureIoTClient 0:1f9b2707ec7d 21 else
AzureIoTClient 0:1f9b2707ec7d 22 {
AzureIoTClient 0:1f9b2707ec7d 23 /* Codes_SRS_DATA_SERIALIZER_07_009: [DataSerializer_Encode function shall call into the given DATA_SERIALIZER_ENCODE_FUNC callback with a valid BUFFER object and valid MULTITREE_HANDLE object.] */
AzureIoTClient 0:1f9b2707ec7d 24 pBuffer = encodeFunc(multiTreeHandle, dataType);
AzureIoTClient 0:1f9b2707ec7d 25 if (pBuffer == NULL)
AzureIoTClient 0:1f9b2707ec7d 26 {
AzureIoTClient 0:1f9b2707ec7d 27 /* Codes_SRS_DATA_SERIALIZER_07_010: [Upon a DATA_SERIALIZER_ENCODE_FUNC failure the DataSerializer_Encode function shall return NULL.] */
AzureIoTClient 11:b1327861f5e0 28 LogError("(Error code: %s)", ENUM_TO_STRING(DATA_SERIALIZER_RESULT, DATA_SERIALIZER_ERROR) );
AzureIoTClient 0:1f9b2707ec7d 29 }
AzureIoTClient 0:1f9b2707ec7d 30 }
AzureIoTClient 0:1f9b2707ec7d 31 /* Codes_SRS_DATA_SERIALIZER_07_002: [DataSerializer_Encode shall return a valid BUFFER_HANDLE when the function executes successfully.] */
AzureIoTClient 0:1f9b2707ec7d 32 return pBuffer;
AzureIoTClient 0:1f9b2707ec7d 33 }
AzureIoTClient 0:1f9b2707ec7d 34
AzureIoTClient 0:1f9b2707ec7d 35 MULTITREE_HANDLE DataSerializer_Decode(BUFFER_HANDLE data, DATA_SERIALIZER_DECODE_FUNC decodeFunc)
AzureIoTClient 0:1f9b2707ec7d 36 {
AzureIoTClient 0:1f9b2707ec7d 37 MULTITREE_HANDLE multiTreeHandle;
AzureIoTClient 0:1f9b2707ec7d 38
AzureIoTClient 0:1f9b2707ec7d 39 /* Codes_SRS_DATA_SERIALIZER_07_007: [NULL shall be returned when an invalid parameter is supplied.] */
AzureIoTClient 0:1f9b2707ec7d 40 if (data == NULL || decodeFunc == NULL)
AzureIoTClient 0:1f9b2707ec7d 41 {
AzureIoTClient 0:1f9b2707ec7d 42 multiTreeHandle = NULL;
AzureIoTClient 11:b1327861f5e0 43 LogError("(Error code: %s)", ENUM_TO_STRING(DATA_SERIALIZER_RESULT, DATA_SERIALIZER_INVALID_ARG) );
AzureIoTClient 0:1f9b2707ec7d 44 }
AzureIoTClient 0:1f9b2707ec7d 45 else
AzureIoTClient 0:1f9b2707ec7d 46 {
AzureIoTClient 0:1f9b2707ec7d 47 /* Codes_SRS_DATA_SERIALIZER_07_012: [DataSerializer_Decode function shall call into the given DATA_SERIALIZER_DECODE_FUNC callback with a valid BUFFER object and valid MULTITREE_HANDLE object.] */
AzureIoTClient 0:1f9b2707ec7d 48 multiTreeHandle = decodeFunc(data);
AzureIoTClient 0:1f9b2707ec7d 49 if (multiTreeHandle == NULL)
AzureIoTClient 0:1f9b2707ec7d 50 {
AzureIoTClient 0:1f9b2707ec7d 51 /* Codes_SRS_DATA_SERIALIZER_07_013: [Upon a DATA_SERIALIZER_DECODE_FUNC callback failure the DataSerializer_Encode function Shall return NULL.] */
AzureIoTClient 11:b1327861f5e0 52 LogError("(Error code: %s)", ENUM_TO_STRING(DATA_SERIALIZER_RESULT, DATA_SERIALIZER_ERROR) );
AzureIoTClient 0:1f9b2707ec7d 53 }
AzureIoTClient 0:1f9b2707ec7d 54 }
AzureIoTClient 0:1f9b2707ec7d 55
AzureIoTClient 0:1f9b2707ec7d 56 /* Codes_SRS_DATA_SERIALIZER_07_006: [DataSerializer_Decode shall return a valid MULTITREE_HANDLE when the function executes successfully.] */
AzureIoTClient 0:1f9b2707ec7d 57 return multiTreeHandle;
AzureIoTClient 0:1f9b2707ec7d 58 }