-
Notifications
You must be signed in to change notification settings - Fork 738
/
provisioning_sc_device_capabilities.c
130 lines (111 loc) · 3.31 KB
/
provisioning_sc_device_capabilities.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <stdlib.h>
#include <stdbool.h>
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/gballoc.h"
#include "prov_service_client/provisioning_sc_device_capabilities.h"
#include "prov_service_client/provisioning_sc_json_const.h"
#include "parson.h"
typedef struct DEVICE_CAPABILITIES_TAG
{
bool iotEdge;
} DEVICE_CAPABILITIES;
DEVICE_CAPABILITIES_HANDLE deviceCapabilities_create(void)
{
DEVICE_CAPABILITIES_HANDLE new_capabilities = NULL;
if ((new_capabilities = malloc(sizeof(DEVICE_CAPABILITIES))) == NULL)
{
LogError("Failed to allocate device capabilities");
}
else
{
new_capabilities->iotEdge = false;
}
return new_capabilities;
}
void deviceCapabilities_destroy(DEVICE_CAPABILITIES_HANDLE capabilities)
{
if (capabilities != NULL)
{
free(capabilities);
}
}
DEVICE_CAPABILITIES_HANDLE deviceCapabilities_fromJson(JSON_Object* root_object)
{
DEVICE_CAPABILITIES_HANDLE new_capabilities = NULL;
if (root_object == NULL)
{
LogError("No device capabilites in JSON");
}
else if ((new_capabilities = malloc(sizeof(DEVICE_CAPABILITIES))) == NULL)
{
LogError("Allocation of Device Capabilities failed");
}
else
{
int iotEdge;
memset(new_capabilities, 0, sizeof(DEVICE_CAPABILITIES));
if ((iotEdge = json_object_get_boolean(root_object, DEVICE_CAPABILITIES_JSON_KEY_IOT_EDGE)) == -1)
{
LogError("Failure to retrieve key %s", DEVICE_CAPABILITIES_JSON_KEY_IOT_EDGE);
deviceCapabilities_destroy(new_capabilities);
new_capabilities = NULL;
}
else
{
new_capabilities->iotEdge = (bool)iotEdge;
}
}
return new_capabilities;
}
JSON_Value* deviceCapabilities_toJson(DEVICE_CAPABILITIES_HANDLE capabilities)
{
JSON_Value* root_value = NULL;
JSON_Object* root_object = NULL;
//Setup
if (capabilities == NULL)
{
LogError("capabilities is NULL");
}
else if ((root_value = json_value_init_object()) == NULL)
{
LogError("json_value_init_object failed");
}
else if ((root_object = json_value_get_object(root_value)) == NULL)
{
LogError("json_value_get_object failed");
json_value_free(root_value);
root_value = NULL;
}
//Set data
else if (json_object_set_boolean(root_object, DEVICE_CAPABILITIES_JSON_KEY_IOT_EDGE, capabilities->iotEdge) != JSONSuccess)
{
LogError("Failed to set '%s' in JSON string", DEVICE_CAPABILITIES_JSON_KEY_IOT_EDGE);
json_value_free(root_value);
root_value = NULL;
}
return root_value;
}
/* Acessor Functions */
bool deviceCapabilities_isIotEdgeCapable(DEVICE_CAPABILITIES_HANDLE capabilities)
{
bool result;
if (capabilities == NULL)
{
LogError("device capabilities is NULL");
result = false;
}
else
{
result = capabilities->iotEdge;
}
return result;
}
void deviceCapabilities_setIotEdgeCapable(DEVICE_CAPABILITIES_HANDLE capabilities, bool iotEdgeCapable)
{
if (capabilities != NULL)
{
capabilities->iotEdge = iotEdgeCapable;
}
}