2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Contact: Junkyu Han <junkyu.han@samsung.com>
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
10 * http://floralicense.org/license/
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.
21 #include <system_info.h>
22 #include <service_app.h>
23 #include <glib/gprintf.h>
24 #include <ttd-worker-lib.h>
27 #include "controller.h"
28 #include "package_worker_report.h"
33 typedef struct app_data_s {
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);
44 static bool service_app_create(void *data)
49 static void service_app_terminate(void *data)
56 static void service_app_control(app_control_h app_control, void *data)
64 app_control_get_extra_data(app_control, "id", &cmd_id);
68 ret = package_worker_report_init(cmd_id);
74 package_worker_report_begin_json_object();
76 app_control_get_extra_data(app_control, "system-information", &command);
81 ptr = strtok(command, ",");
84 __get_requested_system_info(ptr);
86 ptr = strtok(NULL, ",");
89 __get_all_system_info();
93 ret = package_worker_report_end_json_object();
94 goto_if(ret < 0, ERROR);
96 report = package_worker_report_get_json_string();
97 goto_if(!report, ERROR);
99 _D("System information: %s", report);
101 ret = package_worker_report_submit_report(report, __submit_report_completed_cb, NULL);
104 _E("Failed to submit report");
111 package_worker_report_fini();
115 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
117 /*APP_EVENT_LANGUAGE_CHANGED*/
120 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
122 /*APP_EVENT_REGION_FORMAT_CHANGED*/
125 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
127 /*APP_EVENT_LOW_BATTERY*/
130 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
132 /*APP_EVENT_LOW_MEMORY*/
135 int main(int argc, char* argv[])
139 service_app_lifecycle_callback_s event_callback;
140 app_event_handler_h handlers[5] = {NULL, };
142 ad = calloc(1, sizeof(app_data));
145 event_callback.create = service_app_create;
146 event_callback.terminate = service_app_terminate;
147 event_callback.app_control = service_app_control;
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);
154 ret = service_app_main(argc, argv, &event_callback, ad);
159 static int __get_system_info_int(const char *key)
163 int ret = SYSTEM_INFO_ERROR_NONE;
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));
172 buf = g_strdup_printf("%d", val);
174 ret = package_worker_report_add_string(key, buf);
181 static int __get_system_info_bool(const char *key)
184 int ret = SYSTEM_INFO_ERROR_NONE;
185 const char *converted_str = NULL;
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);
194 _D("%s : %s", key, val ? "TRUE" : "FALSE");
195 converted_str = val ? "TRUE" : "FALSE";
198 ret = package_worker_report_add_string(key, converted_str);
203 static int __get_system_info_string(const char *key)
206 int ret = SYSTEM_INFO_ERROR_NONE;
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));
213 _D("%s : %s", key, val);
214 ret = package_worker_report_add_string(key, val);
222 static const char *__check_error_reason(int err_code)
224 const char *reason = NULL;
228 case SYSTEM_INFO_ERROR_INVALID_PARAMETER:
229 reason = "Invalid Parameter";
231 case SYSTEM_INFO_ERROR_IO_ERROR:
234 case SYSTEM_INFO_ERROR_PERMISSION_DENIED:
235 reason = "Permission Denied";
237 case SYSTEM_INFO_ERROR_NOT_SUPPORTED:
238 reason = "Not Supported";
241 reason = "Failure caused some other reaseon";
248 static int __get_requested_system_info(const char *key)
254 for (int i = 0; i < INFO_KEY_MAX; i++)
256 if (!strncmp(key, sys_info[i].key, strlen(key))) {
257 switch (sys_info[i].type)
260 ret = __get_system_info_int(sys_info[i].key);
263 ret = __get_system_info_bool(sys_info[i].key);
265 case KEY_TYPE_STRING:
266 ret = __get_system_info_string(sys_info[i].key);
269 _E("Strange key type");
275 _E("There's no matched key[%s]", key);
277 ret = package_worker_report_add_string(key, "There's no matched key");
279 _E("Failed to add string");
284 static int __get_all_system_info(void)
288 for (int i = 0; i < INFO_KEY_MAX; i++)
290 if (sys_info[i].is_key_supported) {
291 switch (sys_info[i].type)
294 ret = __get_system_info_int(sys_info[i].key);
297 ret = __get_system_info_bool(sys_info[i].key);
299 case KEY_TYPE_STRING:
300 ret = __get_system_info_string(sys_info[i].key);
312 static void __submit_report_completed_cb(int result, void *user_data)
315 _D("Submit is failed");
317 package_worker_report_fini();