2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <json-glib/json-glib.h>
22 #include "json-schema-defs.h"
23 #include "config-deserializer.h"
25 #include "err-check.h"
28 static JsonNode *parse_string(JsonParser *parser, const char *config_json);
29 static void config_array_iterate_func(JsonArray *array, guint index, JsonNode *element, gpointer user_data);
30 static int calculate_task_counter(int frequency);
31 static config_top_subject_e config_parse_top_subject(const char *option);
32 static config_data_process_t config_parse_process_data(JsonArray *obj);
34 static struct _cfg_data
44 config_t *deserialize_configs(const char *config_json, int *size, int *task_counter)
46 JsonParser *parser = json_parser_new();
48 JsonNode *root = parse_string(parser, config_json);
49 JsonObject *obj = json_node_get_object(root);
51 cfg_data.total_duration = json_object_get_int_member(obj, SCHEMA_CONFIG_TOTAL_DURATION);
53 JsonArray *array = json_object_get_array_member(obj, SCHEMA_CONFIG_ENTRY);
56 ERR("Json config is invalid!");
57 g_object_unref(parser);
61 *size = json_array_get_length(array);
63 config_t *configs = g_malloc(*size * sizeof(config_t));
65 json_array_foreach_element(array, config_array_iterate_func, configs);
67 g_object_unref(parser);
68 *task_counter = cfg_data.task_counter;
72 static JsonNode *parse_string(JsonParser *parser, const char *config_json)
76 if (!json_parser_load_from_data(parser, config_json, -1, &err))
78 ERR("Function \"json_parser_load_from_data()\" failed with message ", err->message);
83 return json_parser_get_root(parser);
86 static void config_array_iterate_func(JsonArray *array, guint index, JsonNode *element, gpointer user_data)
88 config_t *configs = (config_t *)user_data;
90 JsonObject *entry = json_node_get_object(element);
91 const gchar *type = json_object_get_string_member(entry, SCHEMA_TYPE);
92 gint64 frequency = json_object_get_int_member(entry, SCHEMA_FREQUENCY);
94 configs[index].frequency = frequency;
95 int c = calculate_task_counter(frequency);
96 cfg_data.task_counter += c;
98 if (g_strcmp0(type, SCHEMA_TYPE_TOP) == 0)
100 configs[index].scope = TOP;
102 const gchar *subject = json_object_get_string_member(entry, SCHEMA_SUBJECT);
103 configs[index].data.top.subject = config_parse_top_subject(subject);
105 gint64 top = json_object_get_int_member(entry, SCHEMA_TOP);
106 configs[index].data.top.top = top;
108 else if (g_strcmp0(type, SCHEMA_TYPE_SYSTEM) == 0)
110 configs[index].scope = SYSTEM;
112 else if (g_strcmp0(type, SCHEMA_TYPE_PROCESS) == 0)
114 configs[index].scope = PROCESS;
116 configs[index].data.process = config_parse_process_data(json_object_get_array_member(entry, SCHEMA_REQUEST_DATA_FILTERS));
118 else if (g_strcmp0(type, SCHEMA_TYPE_LOAD_AVG) == 0)
120 configs[index].scope = LOAD_AVG;
124 static const char *json_object_safe_get_string_member(JsonObject *obj, const char *member)
126 if (json_object_has_member(obj, member))
127 return json_object_get_string_member(obj, member);
131 static void _process_foreach_filter(JsonArray *array, guint index,
132 JsonNode *node, gpointer user_data)
134 config_data_process_t *ret = user_data;
135 struct filter filter;
137 JsonObject *elem = json_node_get_object(node);
140 const char *appid = json_object_safe_get_string_member(elem, SCHEMA_RESULT_APP_ID);
141 const char *exe = json_object_safe_get_string_member(elem, SCHEMA_RESULT_EXE);
143 filter.app_id = appid ? strdup(appid) : NULL;
144 filter.exe = exe ? strdup(exe) : NULL;
146 ret->filters[ret->n_filters++] = filter;
149 static config_data_process_t config_parse_process_data(JsonArray *array)
151 config_data_process_t ret = {0,};
153 int len = json_array_get_length(array);
154 if (len == 0) return ret;
156 ret.filters = calloc(len, sizeof(struct filter));
159 json_array_foreach_element(array, _process_foreach_filter, &ret);
164 static int calculate_task_counter(int frequency)
166 return cfg_data.total_duration / frequency;
169 static config_top_subject_e config_parse_top_subject(const char *option)
171 if (g_strcmp0(option, SCHEMA_SUBJECT_APPS) == 0)
173 return TOP_SUBJECT_APPS;
175 else if (g_strcmp0(option, SCHEMA_SUBJECT_ALL) == 0)
177 return TOP_SUBJECT_ALL;
183 void free_configs(config_t *configs, int len)
185 for (int i=0; i < len; ++i) {
186 switch (configs[i].scope) {
188 free(configs[i].data.process.filters);