/* * 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 #include #include #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; }