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 "schemalib.h"
AzureIoTClient 0:1f9b2707ec7d 7 #include "codefirst.h"
AzureIoTClient 0:1f9b2707ec7d 8 #include "schema.h"
AzureIoTClient 0:1f9b2707ec7d 9 #include "datamarshaller.h"
AzureIoTClient 0:1f9b2707ec7d 10 #include "datapublisher.h"
AzureIoTClient 0:1f9b2707ec7d 11 #include <stddef.h>
Azure.IoT Build 13:16e88f0cfa5f 12 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 0:1f9b2707ec7d 13 #include "iotdevice.h"
AzureIoTClient 0:1f9b2707ec7d 14
AzureIoTClient 0:1f9b2707ec7d 15 #define DEFAULT_CONTAINER_NAME "Container"
AzureIoTClient 0:1f9b2707ec7d 16
AzureIoTClient 0:1f9b2707ec7d 17 DEFINE_ENUM_STRINGS(SERIALIZER_RESULT, SERIALIZER_RESULT_VALUES);
AzureIoTClient 0:1f9b2707ec7d 18
AzureIoTClient 0:1f9b2707ec7d 19 typedef enum AGENT_STATE_TAG
AzureIoTClient 0:1f9b2707ec7d 20 {
AzureIoTClient 0:1f9b2707ec7d 21 AGENT_NOT_INITIALIZED,
AzureIoTClient 0:1f9b2707ec7d 22 AGENT_INITIALIZED
AzureIoTClient 0:1f9b2707ec7d 23 } AGENT_STATE;
AzureIoTClient 0:1f9b2707ec7d 24
AzureIoTClient 0:1f9b2707ec7d 25 static AGENT_STATE g_AgentState = AGENT_NOT_INITIALIZED;
AzureIoTClient 0:1f9b2707ec7d 26
AzureIoTClient 0:1f9b2707ec7d 27 SERIALIZER_RESULT serializer_init(const char* overrideSchemaNamespace)
AzureIoTClient 0:1f9b2707ec7d 28 {
AzureIoTClient 0:1f9b2707ec7d 29 SERIALIZER_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 30
AzureIoTClient 0:1f9b2707ec7d 31 /* Codes_SRS_SCHEMALIB_99_074:[serializer_init when already initialized shall return SERIALIZER_ALREADY_INIT.] */
AzureIoTClient 0:1f9b2707ec7d 32 if (g_AgentState != AGENT_NOT_INITIALIZED)
AzureIoTClient 0:1f9b2707ec7d 33 {
AzureIoTClient 0:1f9b2707ec7d 34 result = SERIALIZER_ALREADY_INIT;
AzureIoTClient 11:b1327861f5e0 35 LogError("(result = %s)", ENUM_TO_STRING(SERIALIZER_RESULT, result));
AzureIoTClient 0:1f9b2707ec7d 36 }
AzureIoTClient 0:1f9b2707ec7d 37 else
AzureIoTClient 0:1f9b2707ec7d 38 {
AzureIoTClient 0:1f9b2707ec7d 39 /* Codes_SRS_SCHEMALIB_99_006:[ Initialize CodeFirst by a call to CodeFirst_Init.] */
AzureIoTClient 0:1f9b2707ec7d 40 /* Codes_SRS_SCHEMALIB_99_076:[serializer_init shall pass the value of overrideSchemaNamespace argument to CodeFirst_Init.] */
AzureIoTClient 0:1f9b2707ec7d 41 if (CodeFirst_Init(overrideSchemaNamespace) != CODEFIRST_OK)
AzureIoTClient 0:1f9b2707ec7d 42 {
AzureIoTClient 0:1f9b2707ec7d 43 /* Codes_SRS_SCHEMALIB_99_007:[ On error SERIALIZER_CODEFIRST_INIT_FAILED shall be returned.] */
AzureIoTClient 0:1f9b2707ec7d 44 result = SERIALIZER_CODEFIRST_INIT_FAILED;
AzureIoTClient 11:b1327861f5e0 45 LogError("(result = %s)", ENUM_TO_STRING(SERIALIZER_RESULT, result));
AzureIoTClient 0:1f9b2707ec7d 46 }
AzureIoTClient 0:1f9b2707ec7d 47 else
AzureIoTClient 0:1f9b2707ec7d 48 {
AzureIoTClient 0:1f9b2707ec7d 49 /* Codes_SRS_SCHEMALIB_99_075:[When an serializer_init call fails for any reason the previous initialization state shall be preserved. The initialized state shall only be changed on a succesfull Init.] */
AzureIoTClient 0:1f9b2707ec7d 50 g_AgentState = AGENT_INITIALIZED;
AzureIoTClient 0:1f9b2707ec7d 51
AzureIoTClient 0:1f9b2707ec7d 52 /* Codes_SRS_SCHEMALIB_99_073:[On success serializer_init shall return SERIALIZER_OK.] */
AzureIoTClient 0:1f9b2707ec7d 53 result = SERIALIZER_OK;
AzureIoTClient 0:1f9b2707ec7d 54 }
AzureIoTClient 0:1f9b2707ec7d 55 }
AzureIoTClient 0:1f9b2707ec7d 56
AzureIoTClient 0:1f9b2707ec7d 57 return result;
AzureIoTClient 0:1f9b2707ec7d 58 }
AzureIoTClient 0:1f9b2707ec7d 59
AzureIoTClient 0:1f9b2707ec7d 60 void serializer_deinit(void)
AzureIoTClient 0:1f9b2707ec7d 61 {
AzureIoTClient 0:1f9b2707ec7d 62 /* Codes_SRS_SCHEMALIB_99_025:[ serializer_deinit shall de-initialize all modules initialized by serializer_init.] */
AzureIoTClient 0:1f9b2707ec7d 63 if (g_AgentState == AGENT_INITIALIZED)
AzureIoTClient 0:1f9b2707ec7d 64 {
AzureIoTClient 0:1f9b2707ec7d 65 CodeFirst_Deinit();
AzureIoTClient 0:1f9b2707ec7d 66 }
AzureIoTClient 0:1f9b2707ec7d 67
AzureIoTClient 0:1f9b2707ec7d 68 /* Codes_SRS_SCHEMALIB_99_026:[ A subsequent call to serializer_start shall succeed.] */
AzureIoTClient 0:1f9b2707ec7d 69 g_AgentState = AGENT_NOT_INITIALIZED;
AzureIoTClient 0:1f9b2707ec7d 70 }
AzureIoTClient 0:1f9b2707ec7d 71
AzureIoTClient 0:1f9b2707ec7d 72 SERIALIZER_RESULT serializer_setconfig(SERIALIZER_CONFIG which, void* value)
AzureIoTClient 0:1f9b2707ec7d 73 {
AzureIoTClient 0:1f9b2707ec7d 74 SERIALIZER_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 75
AzureIoTClient 0:1f9b2707ec7d 76 /* Codes_SRS_SCHEMALIB_99_137:[ If the value argument is NULL, serializer_setconfig shall return SERIALIZER_INVALID_ARG.] */
AzureIoTClient 0:1f9b2707ec7d 77 if (value == NULL)
AzureIoTClient 0:1f9b2707ec7d 78 {
AzureIoTClient 0:1f9b2707ec7d 79 result = SERIALIZER_INVALID_ARG;
AzureIoTClient 0:1f9b2707ec7d 80 }
AzureIoTClient 0:1f9b2707ec7d 81 /* Codes_SRS_SCHEMALIB_99_142:[ When the which argument is SerializeDelayedBufferMaxSize, serializer_setconfig shall invoke DataPublisher_SetMaxBufferSize with the dereferenced value argument, and shall return SERIALIZER_OK.] */
AzureIoTClient 0:1f9b2707ec7d 82 else if (which == SerializeDelayedBufferMaxSize)
AzureIoTClient 0:1f9b2707ec7d 83 {
AzureIoTClient 0:1f9b2707ec7d 84 DataPublisher_SetMaxBufferSize(*(size_t*)value);
AzureIoTClient 0:1f9b2707ec7d 85 result = SERIALIZER_OK;
AzureIoTClient 0:1f9b2707ec7d 86 }
AzureIoTClient 0:1f9b2707ec7d 87 /* Codes_SRS_SCHEMALIB_99_138:[ If the which argument is not one of the declared members of the SERIALIZER_CONFIG enum, serializer_setconfig shall return SERIALIZER_INVALID_ARG.] */
AzureIoTClient 0:1f9b2707ec7d 88 else
AzureIoTClient 0:1f9b2707ec7d 89 {
AzureIoTClient 0:1f9b2707ec7d 90 result = SERIALIZER_INVALID_ARG;
AzureIoTClient 0:1f9b2707ec7d 91 }
AzureIoTClient 0:1f9b2707ec7d 92
AzureIoTClient 0:1f9b2707ec7d 93 return result;
AzureIoTClient 0:1f9b2707ec7d 94 }