Merge pull request #5 from RS7-TizenReferenceSampleApplication/jjoggoba
[apps/native/tizen-things-daemon.git] / workers / system-info / 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 <tizen.h>
20 #include <service_app.h>
21 #include <system_info.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <time.h>
27
28 #include "log.h"
29 #include "controller.h"
30
31 typedef struct app_data_s {
32         FILE *file;
33 } app_data;
34
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);
42
43 static bool service_app_create(void *data)
44 {
45         app_data *ad = data;
46
47         return true;
48 }
49
50 static void service_app_terminate(void *data)
51 {
52         app_data *ad = (app_data *)data;
53
54         free(ad);
55 }
56
57 static void service_app_control(app_control_h app_control, void *data)
58 {
59     /* APP_CONTROL */
60         app_data *ad = data;
61
62         if (__get_system_info(ad) < 0) {
63                 _E("Failed to get system information");
64         }
65
66         service_app_exit();
67 }
68
69 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
70 {
71         /*APP_EVENT_LANGUAGE_CHANGED*/
72 }
73
74 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
75 {
76         /*APP_EVENT_REGION_FORMAT_CHANGED*/
77 }
78
79 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
80 {
81         /*APP_EVENT_LOW_BATTERY*/
82 }
83
84 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
85 {
86         /*APP_EVENT_LOW_MEMORY*/
87 }
88
89 int main(int argc, char* argv[])
90 {
91         app_data *ad = NULL;
92         int ret = 0;
93         service_app_lifecycle_callback_s event_callback;
94         app_event_handler_h handlers[5] = {NULL, };
95
96         ad = calloc(1, sizeof(app_data));
97         retv_if(!ad, -1);
98
99         event_callback.create = service_app_create;
100         event_callback.terminate = service_app_terminate;
101         event_callback.app_control = service_app_control;
102
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);
107
108         ret = service_app_main(argc, argv, &event_callback, ad);
109
110         return ret;
111 }
112
113 static int __get_system_info_int(char *key, FILE *file)
114 {
115         int ret = SYSTEM_INFO_ERROR_NONE;
116         char *reason = NULL;
117         int val = -1;
118
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));
122                 return -1;
123         } else {
124                 _D("[%s] : [%d]", key, val);
125                 fprintf(file, "[%s] : [%d]\n", key, val);
126         }
127
128         return 0;
129 }
130
131 static int __get_system_info_bool(char *key, FILE *file)
132 {
133         int ret = SYSTEM_INFO_ERROR_NONE;
134         char *reason = NULL;
135         bool val = false;
136
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));
140                 return -1;
141         } else {
142                 _D("[%s] : [%s]", key, val ? "TRUE" : "FALSE");
143                 fprintf(file, "[%s] : [%s]\n", key, val ? "TRUE" : "FALSE");
144         }
145
146         return 0;
147 }
148
149 static int __get_system_info_string(char *key, FILE *file)
150 {
151         int ret = SYSTEM_INFO_ERROR_NONE;
152         char *reason = NULL;
153         char *val = NULL;
154
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));
158         } else {
159                 _D("[%s] : [%s]", key, val);
160                 fprintf(file, "[%s] : [%s]\n", key, val);
161         }
162
163         free(val);
164
165         return 0;
166 }
167
168 static int __initialize_file_logging(FILE **file)
169 {
170         if((*(file) = fopen("/tmp/info.txt", "w+")) == NULL) {
171                 _E("Failed to oepn log file");
172                 return -1;
173         }
174         
175         return 0;
176 }
177
178 static void __finalize_file_logging(FILE **file)
179 {
180         fclose(*(file));
181 }
182
183 static int __get_system_info(void *data)
184 {
185         _D("Get SYSTEM INFORMATION");
186         int i;
187         int ret = -1;
188         app_data *ad = data;
189
190         if (__initialize_file_logging(&(ad->file)) < 0)
191                 return -1;
192
193         for (i = 0; i < INFO_KEY_MAX && sys_info[i].key != NULL; i++) {
194                 switch (sys_info[i].type)
195                 {
196                 case KEY_TYPE_INT:
197                         ret = __get_system_info_int(sys_info[i].key, ad->file);
198                         break;
199                 case KEY_TYPE_BOOL:
200                         ret = __get_system_info_bool(sys_info[i].key, ad->file);
201                         break;
202                 case KEY_TYPE_STRING:
203                         ret = __get_system_info_string(sys_info[i].key, ad->file);
204                         break;
205                 default:
206                         _E("Strange key type");
207                         break;
208                 }
209
210                 if (ret < 0)
211                         break;
212         }
213
214         __finalize_file_logging(&(ad->file));
215
216         return 0;
217 }
218
219 static char *__check_error_reason(int ret, char *reason)
220 {
221         switch (ret)
222         {
223         case SYSTEM_INFO_ERROR_INVALID_PARAMETER:
224                 reason = "Invalid Parameter";
225                 break;
226         case SYSTEM_INFO_ERROR_IO_ERROR:
227                 reason = "IO Error";
228                 break;
229         case SYSTEM_INFO_ERROR_PERMISSION_DENIED:
230                 reason = "Permission Denied";
231                 break;
232         case SYSTEM_INFO_ERROR_NOT_SUPPORTED:
233                 reason = "Not Supported";
234                 break;
235         default:
236                 reason = "Failure caused some other reaseon";
237                 break;
238         }
239
240         return reason;
241 }
242