Apply new dbus lib
[apps/native/ttsd-worker-system.git] / src / controller.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Junkyu Han <junkyu.han@samsung.com>
5  *
6  * Licensed under the Flora License, Version 1.1 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://floralicense.org/license/
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <system_info.h>
22 #include <service_app.h>
23 #include <glib/gprintf.h>
24 #include <ttd-worker-lib.h>
25
26 #include "log.h"
27 #include "controller.h"
28 #include "package_worker_report.h"
29
30 #define SUBMIT_SYNC 0
31 #define BUF_MAX 1024
32
33 typedef struct app_data_s {
34 } app_data;
35
36 static int __get_requested_system_info(const char *key);
37 static int __get_all_system_info(void);
38 static int __get_system_info_int(const char *key);
39 static int __get_system_info_bool(const char *key);
40 static int __get_system_info_string(const char *key);
41 static const char *__check_error_reason(int err_code);
42 static void __submit_report_completed_cb(int result, void *user_data);
43
44 static bool service_app_create(void *data)
45 {
46         return true;
47 }
48
49 static void service_app_terminate(void *data)
50 {
51         app_data *ad = data;
52
53         free(ad);
54 }
55
56 static void service_app_control(app_control_h app_control, void *data)
57 {
58     /* APP_CONTROL */
59         int ret = -1;
60         char *command = NULL;
61         char *cmd_id = NULL;
62         char *report = NULL;
63
64         app_control_get_extra_data(app_control, "id", &cmd_id);
65         if (!cmd_id)
66                 service_app_exit();
67
68         ret = package_worker_report_init(cmd_id);
69         if (ret < 0)
70                 service_app_exit();
71
72         g_free(cmd_id);
73
74         package_worker_report_begin_json_object();
75
76         app_control_get_extra_data(app_control, "system-information", &command);
77
78         if (command) {
79                 char *ptr;
80
81                 ptr = strtok(command, ",");
82
83                 while (ptr != NULL) {
84                         __get_requested_system_info(ptr);
85
86                         ptr = strtok(NULL, ",");
87                 }
88         } else
89                 __get_all_system_info();
90
91         g_free(command);
92
93         ret = package_worker_report_end_json_object();
94         goto_if(ret < 0, ERROR);
95
96         report = package_worker_report_get_json_string();
97         goto_if(!report, ERROR);
98
99         _D("System information: %s", report);
100
101         ret = package_worker_report_submit_report(report, __submit_report_completed_cb, NULL);
102         free(report);
103         if (ret < 0) {
104                 _E("Failed to submit report");
105                 goto ERROR;
106         }
107
108         return;
109
110 ERROR:
111         package_worker_report_fini();
112         service_app_exit();
113 }
114
115 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
116 {
117         /*APP_EVENT_LANGUAGE_CHANGED*/
118 }
119
120 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
121 {
122         /*APP_EVENT_REGION_FORMAT_CHANGED*/
123 }
124
125 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
126 {
127         /*APP_EVENT_LOW_BATTERY*/
128 }
129
130 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
131 {
132         /*APP_EVENT_LOW_MEMORY*/
133 }
134
135 int main(int argc, char* argv[])
136 {
137         app_data *ad = NULL;
138         int ret = 0;
139         service_app_lifecycle_callback_s event_callback;
140         app_event_handler_h handlers[5] = {NULL, };
141
142         ad = calloc(1, sizeof(app_data));
143         retv_if(!ad, -1);
144
145         event_callback.create = service_app_create;
146         event_callback.terminate = service_app_terminate;
147         event_callback.app_control = service_app_control;
148
149         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
150         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
151         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
152         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
153
154         ret = service_app_main(argc, argv, &event_callback, ad);
155
156         return ret;
157 }
158
159 static int __get_system_info_int(const char *key)
160 {
161         int val = -1;
162         char *buf = NULL;
163         int ret = SYSTEM_INFO_ERROR_NONE;
164
165         retv_if(!key, -1);
166
167         ret = system_info_get_platform_int(key, &val);
168         if (ret != SYSTEM_INFO_ERROR_NONE) {
169                 _E("Failed to get System Info [%s] -- [%s]", key, __check_error_reason(ret));
170                 buf = g_strdup(__check_error_reason(ret));
171         } else
172                 buf = g_strdup_printf("%d", val);
173
174         ret = package_worker_report_add_string(key, buf);
175
176         g_free(buf);
177
178         return ret;
179 }
180
181 static int __get_system_info_bool(const char *key)
182 {
183         bool val = false;
184         int ret = SYSTEM_INFO_ERROR_NONE;
185         const char *converted_str = NULL;
186
187         retv_if(!key, -1);
188
189         ret = system_info_get_platform_bool(key, &val);
190         if (ret != SYSTEM_INFO_ERROR_NONE) {
191                 _E("Failed to get System Info [%s] -- [%s]", key, __check_error_reason(ret));
192                 converted_str = __check_error_reason(ret);
193         } else {
194                 _D("%s : %s", key, val ? "TRUE" : "FALSE");
195                 converted_str = val ? "TRUE" : "FALSE";
196         }
197
198         ret = package_worker_report_add_string(key, converted_str);
199
200         return ret;
201 }
202
203 static int __get_system_info_string(const char *key)
204 {
205         char *val = NULL;
206         int ret = SYSTEM_INFO_ERROR_NONE;
207
208         ret = system_info_get_platform_string(key, &val);
209         if (ret != SYSTEM_INFO_ERROR_NONE) {
210                 _E("Failed to get System Info [%s] -- [%s]", key, __check_error_reason(ret));
211                 ret = package_worker_report_add_string(key, __check_error_reason(ret));
212         } else {
213                 _D("%s : %s", key, val);
214                 ret = package_worker_report_add_string(key, val);
215         }
216
217         g_free(val);
218
219         return ret;
220 }
221
222 static const char *__check_error_reason(int err_code)
223 {
224         const char *reason = NULL;
225
226         switch (err_code)
227         {
228         case SYSTEM_INFO_ERROR_INVALID_PARAMETER:
229                 reason = "Invalid Parameter";
230                 break;
231         case SYSTEM_INFO_ERROR_IO_ERROR:
232                 reason = "IO Error";
233                 break;
234         case SYSTEM_INFO_ERROR_PERMISSION_DENIED:
235                 reason = "Permission Denied";
236                 break;
237         case SYSTEM_INFO_ERROR_NOT_SUPPORTED:
238                 reason = "Not Supported";
239                 break;
240         default:
241                 reason = "Failure caused some other reaseon";
242                 break;
243         }
244
245         return reason;
246 }
247
248 static int __get_requested_system_info(const char *key)
249 {
250         int ret = -1;
251
252         retv_if(!key, -1);
253
254         for (int i = 0; i < INFO_KEY_MAX; i++)
255         {
256                 if (!strncmp(key, sys_info[i].key, strlen(key))) {
257                         switch (sys_info[i].type)
258                         {
259                         case KEY_TYPE_INT:
260                                 ret = __get_system_info_int(sys_info[i].key);
261                                 break;
262                         case KEY_TYPE_BOOL:
263                                 ret = __get_system_info_bool(sys_info[i].key);
264                                 break;
265                         case KEY_TYPE_STRING:
266                                 ret = __get_system_info_string(sys_info[i].key);
267                                 break;
268                         default:
269                                 _E("Strange key type");
270                                 break;
271                         }
272                         return ret;
273                 }
274         }
275         _E("There's no matched key[%s]", key);
276
277         ret = package_worker_report_add_string(key, "There's no matched key");
278         if (ret < 0)
279            _E("Failed to add string");
280
281         return -1;
282 }
283
284 static int __get_all_system_info(void)
285 {
286         int ret = -1;
287
288         for (int i = 0; i < INFO_KEY_MAX; i++)
289         {
290                 if (sys_info[i].is_key_supported) {
291                         switch (sys_info[i].type)
292                         {
293                         case KEY_TYPE_INT:
294                                 ret = __get_system_info_int(sys_info[i].key);
295                                 break;
296                         case KEY_TYPE_BOOL:
297                                 ret = __get_system_info_bool(sys_info[i].key);
298                                 break;
299                         case KEY_TYPE_STRING:
300                                 ret = __get_system_info_string(sys_info[i].key);
301                                 break;
302                         default:
303                                 _E("Strange type");
304                                 break;
305                         }
306                 }
307         }
308
309         return ret;
310 }
311
312 static void __submit_report_completed_cb(int result, void *user_data)
313 {
314         if (result < 0)
315                 _D("Submit is failed");
316
317         package_worker_report_fini();
318         service_app_exit();
319 }