Initialize Tizen 2.3
[framework/api/system-info.git] / src / system_info.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <dlog.h>
24
25 #include <system_info.h>
26 #include <system_info_private.h>
27 #include <sys/utsname.h>
28
29 #ifdef LOG_TAG
30 #undef LOG_TAG
31 #endif
32
33 #define LOG_TAG "CAPI_SYSTEM_INFO"
34
35 #define SYSTEM_INFO_MAX -1
36
37 typedef struct {
38         system_info_key_e key;
39         system_info_data_type_e data_type;
40         system_info_get_value_cb get_value_cb;
41 } system_info_s;
42
43 typedef system_info_s * system_info_h;
44
45 system_info_s system_info_table[] = {
46
47 {
48          /**< The model of the device */
49         SYSTEM_INFO_KEY_MODEL,
50         SYSTEM_INFO_DATA_TYPE_STRING,
51         system_info_get_model
52 },
53
54 {
55          /**< The version of the Tizen supported by the platform */
56         SYSTEM_INFO_KEY_TIZEN_VERSION,
57         SYSTEM_INFO_DATA_TYPE_STRING,
58         system_info_get_tizen_version
59 },
60
61 {
62         /**< The height of the screen in pixels */
63         SYSTEM_INFO_KEY_SCREEN_HEIGHT,
64         SYSTEM_INFO_DATA_TYPE_INT,
65         system_info_get_screen_height
66 },
67
68 {
69         /**< The width of the screen in pixels */
70         SYSTEM_INFO_KEY_SCREEN_WIDTH,
71         SYSTEM_INFO_DATA_TYPE_INT,
72         system_info_get_screen_width
73 },
74
75 {
76         /**< The Name of the Platform */
77         SYSTEM_INFO_KEY_PLATFORM_NAME,
78         SYSTEM_INFO_DATA_TYPE_STRING,
79         system_info_get_platform_name
80 },
81
82 {
83         /**< The Name of the Tizen version */
84         SYSTEM_INFO_KEY_TIZEN_VERSION_NAME,
85         SYSTEM_INFO_DATA_TYPE_STRING,
86         system_info_get_tizen_version_name
87 },
88
89 {
90         /**< The CORE CPU architecture of model */
91         SYSTEM_INFO_KEY_CORE_CPU_ARCH,
92         SYSTEM_INFO_DATA_TYPE_STRING,
93         system_info_get_core_cpu_arch
94 },
95
96 {
97         /**< The CORE CPU frequency of model */
98         SYSTEM_INFO_KEY_CORE_CPU_FREQ,
99         SYSTEM_INFO_DATA_TYPE_DOUBLE,
100         system_info_get_core_cpu_freq
101 },
102
103 {
104         /**< The height of the physical screen size in millimeters */
105         SYSTEM_INFO_KEY_PHYSICAL_SCREEN_HEIGHT,
106         SYSTEM_INFO_DATA_TYPE_INT,
107         system_info_get_physical_screen_height
108 },
109
110 {
111         /**< The width of the physical screen size in millimeters */
112         SYSTEM_INFO_KEY_PHYSICAL_SCREEN_WIDTH,
113         SYSTEM_INFO_DATA_TYPE_INT,
114         system_info_get_physical_screen_width
115 },
116
117 {
118         /**< The build string of the platform binary */
119         SYSTEM_INFO_KEY_BUILD_STRING,
120         SYSTEM_INFO_DATA_TYPE_STRING,
121         system_info_get_build_string
122 },
123
124 {
125         /**< The build date of the platform binary */
126         SYSTEM_INFO_KEY_BUILD_DATE,
127         SYSTEM_INFO_DATA_TYPE_STRING,
128         system_info_get_build_date
129 },
130
131 {
132         /**< The build time of the platform binary */
133         SYSTEM_INFO_KEY_BUILD_TIME,
134         SYSTEM_INFO_DATA_TYPE_STRING,
135         system_info_get_build_time
136 },
137
138 {
139         /**< The manufacturer of the device */
140         SYSTEM_INFO_KEY_MANUFACTURER,
141         SYSTEM_INFO_DATA_TYPE_STRING,
142         system_info_get_manufacturer
143 },
144
145 {
146         /**< Indicates whether the device supports tethering */
147         SYSTEM_INFO_KEY_TETHERING_SUPPORTED,
148         SYSTEM_INFO_DATA_TYPE_BOOL,
149         system_info_get_tethering_supported
150 },
151
152 {
153         SYSTEM_INFO_MAX, -1, NULL
154 }
155
156 };
157
158 static system_info_mode_type_e system_info_system_info_model_type;
159
160 system_info_mode_type_e system_info_get_system_info_model_type(void)
161 {
162         return system_info_system_info_model_type;
163 }
164
165 void __attribute__((constructor)) system_info_init(void)
166 {
167         int ret;
168         char *str;
169
170         ret = system_info_get_platform_string("tizen.org/system/model_name", &str);
171
172         if (ret != SYSTEM_INFO_ERROR_NONE) {
173                 LOGE("initialize error");
174                 return;
175         }
176
177         if (!strcmp(str, "Emulator"))
178                 system_info_system_info_model_type = SYSTEM_INFO_MODEL_TYPE_EMULATOR;
179         else
180                 system_info_system_info_model_type = SYSTEM_INFO_MODEL_TYPE_TARGET;
181
182         free(str);
183 }
184
185 static int system_info_get(system_info_key_e key, system_info_h *system_info)
186 {
187         int index = 0;
188
189         while (system_info_table[index].key != SYSTEM_INFO_MAX) {
190                 if (system_info_table[index].key == key) {
191                         *system_info = &system_info_table[index];
192                         return 0;
193                 }
194
195                 index++;
196         }
197
198         return -1;
199 }
200
201 int system_info_get_value(system_info_key_e key, system_info_data_type_e data_type, void **value)
202 {
203         system_info_h system_info;
204         system_info_get_value_cb system_info_getter;
205
206         if (value == NULL) {
207                 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", SYSTEM_INFO_ERROR_INVALID_PARAMETER);
208                 return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
209         }
210
211         if (system_info_get(key, &system_info)) {
212                 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", SYSTEM_INFO_ERROR_INVALID_PARAMETER);
213                 return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
214         }
215
216         if (system_info->data_type != data_type) {
217                 LOGE("INVALID_PARAMETER(0x%08x) : invalid data type", SYSTEM_INFO_ERROR_INVALID_PARAMETER);
218                 return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
219         }
220
221         system_info_getter = system_info->get_value_cb;
222
223         if (system_info_getter == NULL) {
224                 LOGE("IO_ERROR(0x%08x) : failed to call getter for the system information", SYSTEM_INFO_ERROR_IO_ERROR);
225                 return SYSTEM_INFO_ERROR_IO_ERROR;
226         }
227
228         return system_info_getter(key, system_info->data_type, value);
229 }
230
231 API int system_info_get_value_int(system_info_key_e key, int *value)
232 {
233         return system_info_get_value(key, SYSTEM_INFO_DATA_TYPE_INT, (void **)value);
234 }
235
236 API int system_info_get_value_bool(system_info_key_e key, bool *value)
237 {
238         return system_info_get_value(key, SYSTEM_INFO_DATA_TYPE_BOOL, (void **)value);
239 }
240
241 API int system_info_get_value_double(system_info_key_e key, double *value)
242 {
243         return system_info_get_value(key, SYSTEM_INFO_DATA_TYPE_DOUBLE, (void **)value);
244 }
245
246 API int system_info_get_value_string(system_info_key_e key, char **value)
247 {
248         return system_info_get_value(key, SYSTEM_INFO_DATA_TYPE_STRING, (void **)value);
249 }
250
251 API int system_info_get_platform_bool(const char *key, bool *value)
252 {
253         int ret;
254         bool *supported;
255         char *string = NULL;
256
257         supported = (bool *)value;
258
259         if (access(CONFIG_FILE_PATH, R_OK)) {
260                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
261                 return SYSTEM_INFO_ERROR_IO_ERROR;
262         }
263
264         ret = system_info_get_value_from_config_xml(PLATFORM_TAG, key, BOOL_TYPE, &string);
265         if (ret) {
266                 LOGE("cannot get %s", key);
267                 return ret;
268         }
269
270         if (!strcmp(string, "true") || !strcmp(string, "TRUE"))
271                 *supported = true;
272         else
273                 *supported = false;
274
275         free(string);
276
277         return SYSTEM_INFO_ERROR_NONE;
278 }
279
280 API int system_info_get_platform_int(const char *key, int *value)
281 {
282         int ret;
283         int *ret_val;
284         char *string = NULL;
285
286         ret_val = (int *)value;
287
288         if (access(CONFIG_FILE_PATH, R_OK)) {
289                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
290                 return SYSTEM_INFO_ERROR_IO_ERROR;
291         }
292
293         ret = system_info_get_value_from_config_xml(PLATFORM_TAG, key, INT_TYPE, &string);
294         if (ret) {
295                 LOGE("cannot get %s", key);
296                 return ret;
297         }
298
299         *ret_val = atoi(string);
300
301         free(string);
302
303         return SYSTEM_INFO_ERROR_NONE;
304 }
305
306 API int system_info_get_platform_double(const char *key, double *value)
307 {
308         int ret;
309         double *ret_val;
310         char *string = NULL;
311
312         ret_val = (double *)value;
313
314         if (access(CONFIG_FILE_PATH, R_OK)) {
315                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
316                 return SYSTEM_INFO_ERROR_IO_ERROR;
317         }
318
319         ret = system_info_get_value_from_config_xml(PLATFORM_TAG, key, DBL_TYPE, &string);
320         if (ret) {
321                 LOGE("cannot get %s", key);
322                 return ret;
323         }
324
325         *ret_val = atof(string);
326
327         free(string);
328
329         return SYSTEM_INFO_ERROR_NONE;
330 }
331
332 API int system_info_get_platform_string(const char *key, char **value)
333 {
334         int ret;
335         char *string = NULL;
336
337         if (access(CONFIG_FILE_PATH, R_OK)) {
338                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
339                 return SYSTEM_INFO_ERROR_IO_ERROR;
340         }
341
342         ret = system_info_get_value_from_config_xml(PLATFORM_TAG, key, STR_TYPE, &string);
343         if (ret) {
344                 LOGE("cannot get %s", key);
345                 return ret;
346         }
347
348         *value = string;
349
350         return SYSTEM_INFO_ERROR_NONE;
351 }
352
353 API int system_info_get_custom_bool(const char *key, bool *value)
354 {
355         int ret;
356         bool *supported;
357         char *string = NULL;
358
359         supported = (bool *)value;
360
361         if (access(CONFIG_FILE_PATH, R_OK)) {
362                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
363                 return SYSTEM_INFO_ERROR_IO_ERROR;
364         }
365
366         ret = system_info_get_value_from_config_xml(CUSTOM_TAG, key, BOOL_TYPE, &string);
367         if (ret) {
368                 LOGI("cannot get %s", key);
369                 *supported = false;
370                 return SYSTEM_INFO_ERROR_NONE;
371         }
372
373         if (!strcmp(string, "true") || !strcmp(string, "TRUE"))
374                 *supported = true;
375         else
376                 *supported = false;
377
378         free(string);
379
380         return SYSTEM_INFO_ERROR_NONE;
381 }
382
383 API int system_info_get_custom_int(const char *key, int *value)
384 {
385         int ret;
386         int *ret_val;
387         char *string = NULL;
388
389         ret_val = (int *)value;
390
391         if (access(CONFIG_FILE_PATH, R_OK)) {
392                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
393                 return SYSTEM_INFO_ERROR_IO_ERROR;
394         }
395
396         ret = system_info_get_value_from_config_xml(CUSTOM_TAG, key, INT_TYPE, &string);
397         if (ret) {
398                 LOGI("cannot get %s", key);
399                 *ret_val = 0;
400                 return SYSTEM_INFO_ERROR_NONE;
401         }
402
403         *ret_val = atoi(string);
404
405         free(string);
406
407         return SYSTEM_INFO_ERROR_NONE;
408 }
409
410 API int system_info_get_custom_double(const char *key, double *value)
411 {
412         int ret;
413         double *ret_val;
414         char *string = NULL;
415
416         ret_val = (double *)value;
417
418         if (access(CONFIG_FILE_PATH, R_OK)) {
419                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
420                 return SYSTEM_INFO_ERROR_IO_ERROR;
421         }
422
423         ret = system_info_get_value_from_config_xml(CUSTOM_TAG, key, DBL_TYPE, &string);
424         if (ret) {
425                 LOGI("cannot get %s", key);
426                 *ret_val = 0;
427                 return SYSTEM_INFO_ERROR_NONE;
428         }
429
430         *ret_val = atof(string);
431
432         free(string);
433
434         return SYSTEM_INFO_ERROR_NONE;
435 }
436
437 API int system_info_get_custom_string(const char *key, char **value)
438 {
439         int ret;
440         char *string = NULL;
441
442         if (access(CONFIG_FILE_PATH, R_OK)) {
443                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
444                 return SYSTEM_INFO_ERROR_IO_ERROR;
445         }
446
447         ret = system_info_get_value_from_config_xml(CUSTOM_TAG, key, STR_TYPE, &string);
448         if (ret) {
449                 LOGE("cannot get %s info from %s!!!", key, CONFIG_FILE_PATH);
450                 return ret;
451         }
452
453         *value = string;
454
455         return SYSTEM_INFO_ERROR_NONE;
456 }
457
458 API int system_info_get_internal_value(const char *key, char **value)
459 {
460         int ret, len;
461         char *string = NULL;
462
463         len = strlen(BOARD_CONFIG);
464
465         if (!strncmp(key, BOARD_CONFIG, len)) {
466                 if (system_info_get_bsp_info(key, value) == SYSTEM_INFO_ERROR_NONE)
467                         return SYSTEM_INFO_ERROR_NONE;
468         }
469
470         if (access(CONFIG_FILE_PATH, R_OK)) {
471                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
472                 return SYSTEM_INFO_ERROR_IO_ERROR;
473         }
474
475         ret = system_info_get_value_from_config_xml(INTERNAL_TAG, key, STR_TYPE, &string);
476         if (ret) {
477                 LOGE("cannot get %s info from %s!!!", key, CONFIG_FILE_PATH);
478                 return ret;
479         }
480
481         *value = string;
482
483         return SYSTEM_INFO_ERROR_NONE;
484 }