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