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.
19 #include <json-glib/json-glib.h>
20 #include "json-schema-defs.h"
21 #include "config-deserializer.h"
23 #include "err-check.h"
25 static JsonNode *parse_string(JsonParser *parser, const char *config_json);
26 static void config_array_iterate_func(JsonArray *array, guint index, JsonNode *element, gpointer user_data);
27 static config_options_e config_parse_options(const char *option);
29 config_t *deserialize_configs(const char *config_json, int *size)
31 JsonParser *parser = json_parser_new();
33 JsonNode *root = parse_string(parser, config_json);
35 JsonArray *array = json_node_get_array(root);
38 ERR("Json config is invalid!");
39 g_object_unref(parser);
43 *size = json_array_get_length(array);
45 config_t *configs = g_malloc(*size * sizeof(config_t));
47 json_array_foreach_element(array, config_array_iterate_func, configs);
49 g_object_unref(parser);
54 static JsonNode *parse_string(JsonParser *parser, const char *config_json)
58 if (!json_parser_load_from_data(parser, config_json, -1, &err))
60 ERR("Function \"json_parser_load_from_data()\" failed with message ", err->message);
65 return json_parser_get_root(parser);
68 static void config_array_iterate_func(JsonArray *array, guint index, JsonNode *element, gpointer user_data)
70 config_t *configs = (config_t *)user_data;
72 JsonObject *entry = json_node_get_object(element);
73 const gchar *type = json_object_get_string_member(entry, SCHEMA_TYPE);
74 gint64 frequency = json_object_get_int_member(entry, SCHEMA_FREQUENCY);
76 configs[index].frequency = frequency;
78 if (g_strcmp0(type, SCHEMA_TYPE_TOP) == 0)
80 configs[index].scope = TOP;
82 const gchar *target = json_object_get_string_member(entry, SCHEMA_TARGET);
83 configs[index].data.top.options = config_parse_options(target);
85 gint64 top = json_object_get_int_member(entry, SCHEMA_TOP);
86 configs[index].data.top.top = top;
88 else if (g_strcmp0(type, SCHEMA_TYPE_SYSTEM) == 0)
90 configs[index].scope = SYSTEM;
92 const gchar *target = json_object_get_string_member(entry, SCHEMA_TARGET);
93 configs[index].data.system.options = config_parse_options(target);
95 else if (g_strcmp0(type, SCHEMA_TYPE_PROCESS) == 0)
97 configs[index].scope = APPS;
99 const gchar *target = json_object_get_string_member(entry, SCHEMA_TARGET);
100 configs[index].data.apps.options = config_parse_options(target);
102 const gchar *app_id = json_object_get_string_member(entry, SCHEMA_ID);
103 snprintf(configs[index].data.apps.app_id, APP_ID_REGEX_MAX_LEN + 1, "%s", app_id);
105 else if (g_strcmp0(type, SCHEMA_TYPE_LOAD_AVG) == 0)
107 configs[index].scope = LOAD_AVG;
111 static config_options_e config_parse_options(const char *option)
113 if (g_strcmp0(option, SCHEMA_TARGET_CPU) == 0)
117 else if (g_strcmp0(option, SCHEMA_TARGET_MEMORY) == 0)
119 return OBSERVE_MEMORY;