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:
Sun Apr 24 16:40:59 2016 -0700
Revision:
11:b1327861f5e0
Parent:
10:c2aee3965a83
Child:
13:16e88f0cfa5f
1.0.5

Who changed what in which revision?

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