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