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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* Licensed under the Flora License, Version 1.1 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <glib.h>
#include <json-glib/json-glib.h>
#include "json-schema-defs.h"
#include "config-deserializer.h"
#include "config.h"
#include "err-check.h"
#include "log.h"
static JsonNode *parse_string(JsonParser *parser, const char *config_json);
static void config_array_iterate_func(JsonArray *array, guint index, JsonNode *element, gpointer user_data);
static config_options_e config_parse_options(const char *option);
static int calculate_task_counter(int frequency);
static struct _cfg_data
{
int total_duration;
int task_counter;
} cfg_data =
{
.total_duration = 0,
.task_counter = 0
};
config_t *deserialize_configs(const char *config_json, int *size, int *task_counter)
{
JsonParser *parser = json_parser_new();
JsonNode *root = parse_string(parser, config_json);
JsonObject *obj = json_node_get_object(root);
cfg_data.total_duration = json_object_get_int_member(obj, SCHEMA_CONFIG_TOTAL_DURATION);
JsonArray *array = json_object_get_array_member(obj, SCHEMA_CONFIG_ENTRY);
if (!array)
{
ERR("Json config is invalid!");
g_object_unref(parser);
return NULL;
}
*size = json_array_get_length(array);
config_t *configs = g_malloc(*size * sizeof(config_t));
json_array_foreach_element(array, config_array_iterate_func, configs);
g_object_unref(parser);
*task_counter = cfg_data.task_counter;
return configs;
}
static JsonNode *parse_string(JsonParser *parser, const char *config_json)
{
GError *err = NULL;
if (!json_parser_load_from_data(parser, config_json, -1, &err))
{
ERR("Function \"json_parser_load_from_data()\" failed with message ", err->message);
g_error_free(err);
return NULL;
}
return json_parser_get_root(parser);
}
static void config_array_iterate_func(JsonArray *array, guint index, JsonNode *element, gpointer user_data)
{
config_t *configs = (config_t *)user_data;
JsonObject *entry = json_node_get_object(element);
const gchar *type = json_object_get_string_member(entry, SCHEMA_TYPE);
gint64 frequency = json_object_get_int_member(entry, SCHEMA_FREQUENCY);
configs[index].frequency = frequency;
int c = calculate_task_counter(frequency);
cfg_data.task_counter += c;
if (g_strcmp0(type, SCHEMA_TYPE_TOP) == 0)
{
configs[index].scope = TOP;
const gchar *target = json_object_get_string_member(entry, SCHEMA_TARGET);
configs[index].data.top.options = config_parse_options(target);
gint64 top = json_object_get_int_member(entry, SCHEMA_TOP);
configs[index].data.top.top = top;
}
else if (g_strcmp0(type, SCHEMA_TYPE_SYSTEM) == 0)
{
configs[index].scope = SYSTEM;
const gchar *target = json_object_get_string_member(entry, SCHEMA_TARGET);
configs[index].data.system.options = config_parse_options(target);
}
else if (g_strcmp0(type, SCHEMA_TYPE_PROCESS) == 0)
{
configs[index].scope = APPS;
const gchar *target = json_object_get_string_member(entry, SCHEMA_TARGET);
configs[index].data.apps.options = config_parse_options(target);
const gchar *app_id = json_object_get_string_member(entry, SCHEMA_ID);
snprintf(configs[index].data.apps.app_id, APP_ID_REGEX_MAX_LEN + 1, "%s", app_id);
}
else if (g_strcmp0(type, SCHEMA_TYPE_LOAD_AVG) == 0)
{
configs[index].scope = LOAD_AVG;
}
}
static config_options_e config_parse_options(const char *option)
{
if (g_strcmp0(option, SCHEMA_TARGET_CPU) == 0)
{
return OBSERVE_CPU;
}
else if (g_strcmp0(option, SCHEMA_TARGET_MEMORY) == 0)
{
return OBSERVE_MEMORY;
}
return -1;
}
static int calculate_task_counter(int frequency)
{
return cfg_data.total_duration / frequency;
}
|