report-json-serializer: app and top serializers
[apps/native/ttsd-worker-task.git] / src / config-deserializer.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <glib.h>
19 #include <json-glib/json-glib.h>
20 #include "json-schema-defs.h"
21 #include "config-deserializer.h"
22 #include "config.h"
23 #include "err-check.h"
24
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);
28
29 config_t *deserialize_configs(const char *config_json, int *size)
30 {
31     JsonParser *parser = json_parser_new();
32
33     JsonNode *root = parse_string(parser, config_json);
34
35     JsonArray *array = json_node_get_array(root);
36     if (!array)
37     {
38         ERR("Json config is invalid!");
39         g_object_unref(parser);
40         return NULL;
41     }
42
43     *size = json_array_get_length(array);
44
45     config_t *configs = g_malloc(*size * sizeof(config_t));
46
47     json_array_foreach_element(array, config_array_iterate_func, configs);
48
49     g_object_unref(parser);
50
51     return configs;
52 }
53
54 static JsonNode *parse_string(JsonParser *parser, const char *config_json)
55 {
56     GError *err = NULL;
57
58     if (!json_parser_load_from_data(parser, config_json, -1, &err))
59     {
60         ERR("Function \"json_parser_load_from_data()\" failed with message ", err->message);
61         g_error_free(err);
62         return NULL;
63     }
64
65     return json_parser_get_root(parser);
66 }
67
68 static void config_array_iterate_func(JsonArray *array, guint index, JsonNode *element, gpointer user_data)
69 {
70     config_t *configs = (config_t *)user_data;
71
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);
75
76     configs[index].frequency = frequency;
77
78     if (g_strcmp0(type, SCHEMA_TYPE_TOP) == 0)
79     {
80         configs[index].scope = TOP;
81
82         const gchar *target = json_object_get_string_member(entry, SCHEMA_TARGET);
83         configs[index].data.top.options = config_parse_options(target);
84
85         gint64 top = json_object_get_int_member(entry, SCHEMA_TOP);
86         configs[index].data.top.top = top;
87     }
88     else if (g_strcmp0(type, SCHEMA_TYPE_SYSTEM) == 0)
89     {
90         configs[index].scope = SYSTEM;
91
92         const gchar *target = json_object_get_string_member(entry, SCHEMA_TARGET);
93         configs[index].data.system.options = config_parse_options(target);
94     }
95     else if (g_strcmp0(type, SCHEMA_TYPE_PROCESS) == 0)
96     {
97         configs[index].scope = APPS;
98
99         const gchar *target = json_object_get_string_member(entry, SCHEMA_TARGET);
100         configs[index].data.apps.options = config_parse_options(target);
101
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);
104     }
105     else if (g_strcmp0(type, SCHEMA_TYPE_LOAD_AVG) == 0)
106     {
107         configs[index].scope = LOAD_AVG;
108     }
109 }
110
111 static config_options_e config_parse_options(const char *option)
112 {
113     if (g_strcmp0(option, SCHEMA_TARGET_CPU) == 0)
114     {
115         return OBSERVE_CPU;
116     }
117     else if (g_strcmp0(option, SCHEMA_TARGET_MEMORY) == 0)
118     {
119         return OBSERVE_MEMORY;
120     }
121
122     return -1;
123 }