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.
20 #include <service_app.h>
21 #include <system_info.h>
29 #include "controller.h"
31 typedef struct app_data_s {
35 static int __get_system_info(void *data);
36 static int __get_system_info_int(char *key, FILE *file);
37 static int __get_system_info_bool(char *key, FILE *file);
38 static int __get_system_info_string(char *key, FILE *file);
39 static int __initialize_file_logging(FILE **file);
40 static void __finalize_file_logging(FILE **file);
41 static char *__check_error_reason(int ret, char *reason);
43 static bool service_app_create(void *data)
50 static void service_app_terminate(void *data)
52 app_data *ad = (app_data *)data;
57 static void service_app_control(app_control_h app_control, void *data)
62 if (__get_system_info(ad) < 0) {
63 _E("Failed to get system information");
69 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
71 /*APP_EVENT_LANGUAGE_CHANGED*/
74 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
76 /*APP_EVENT_REGION_FORMAT_CHANGED*/
79 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
81 /*APP_EVENT_LOW_BATTERY*/
84 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
86 /*APP_EVENT_LOW_MEMORY*/
89 int main(int argc, char* argv[])
93 service_app_lifecycle_callback_s event_callback;
94 app_event_handler_h handlers[5] = {NULL, };
96 ad = calloc(1, sizeof(app_data));
99 event_callback.create = service_app_create;
100 event_callback.terminate = service_app_terminate;
101 event_callback.app_control = service_app_control;
103 service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
104 service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
105 service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
106 service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
108 ret = service_app_main(argc, argv, &event_callback, ad);
113 static int __get_system_info_int(char *key, FILE *file)
115 int ret = SYSTEM_INFO_ERROR_NONE;
119 ret = system_info_get_platform_int(key, &val);
120 if (ret != SYSTEM_INFO_ERROR_NONE) {
121 _E("Failed to get System Info [%s] -- [%s]", key, __check_error_reason(ret, reason));
124 _D("[%s] : [%d]", key, val);
125 fprintf(file, "[%s] : [%d]\n", key, val);
131 static int __get_system_info_bool(char *key, FILE *file)
133 int ret = SYSTEM_INFO_ERROR_NONE;
137 ret = system_info_get_platform_bool(key, &val);
138 if (ret != SYSTEM_INFO_ERROR_NONE) {
139 _E("Failed to get System Info [%s] -- [%s]", key, __check_error_reason(ret, reason));
142 _D("[%s] : [%s]", key, val ? "TRUE" : "FALSE");
143 fprintf(file, "[%s] : [%s]\n", key, val ? "TRUE" : "FALSE");
149 static int __get_system_info_string(char *key, FILE *file)
151 int ret = SYSTEM_INFO_ERROR_NONE;
155 ret = system_info_get_platform_string(key, &val);
156 if (ret != SYSTEM_INFO_ERROR_NONE) {
157 _E("Failed to get System Info [%s] -- [%s]", key, __check_error_reason(ret, reason));
159 _D("[%s] : [%s]", key, val);
160 fprintf(file, "[%s] : [%s]\n", key, val);
168 static int __initialize_file_logging(FILE **file)
170 if((*(file) = fopen("/tmp/info.txt", "w+")) == NULL) {
171 _E("Failed to oepn log file");
178 static void __finalize_file_logging(FILE **file)
183 static int __get_system_info(void *data)
185 _D("Get SYSTEM INFORMATION");
190 if (__initialize_file_logging(&(ad->file)) < 0)
193 for (i = 0; i < INFO_KEY_MAX && sys_info[i].key != NULL; i++) {
194 switch (sys_info[i].type)
197 ret = __get_system_info_int(sys_info[i].key, ad->file);
200 ret = __get_system_info_bool(sys_info[i].key, ad->file);
202 case KEY_TYPE_STRING:
203 ret = __get_system_info_string(sys_info[i].key, ad->file);
206 _E("Strange key type");
214 __finalize_file_logging(&(ad->file));
219 static char *__check_error_reason(int ret, char *reason)
223 case SYSTEM_INFO_ERROR_INVALID_PARAMETER:
224 reason = "Invalid Parameter";
226 case SYSTEM_INFO_ERROR_IO_ERROR:
229 case SYSTEM_INFO_ERROR_PERMISSION_DENIED:
230 reason = "Permission Denied";
232 case SYSTEM_INFO_ERROR_NOT_SUPPORTED:
233 reason = "Not Supported";
236 reason = "Failure caused some other reaseon";