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 15 21:57:26 2015 -0700
Revision:
0:1f9b2707ec7d
Child:
5:c08e3ffc68e4
New release

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