system-info: Merge Tizen 2.3 source code into tizen branch
[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_platform_bool(const char *key, bool *value)
243 {
244         int ret;
245         bool *supported;
246         char *string = NULL;
247
248         supported = (bool *)value;
249
250         if (access(CONFIG_FILE_PATH, R_OK)) {
251                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
252                 if (errno == EPERM || errno == EACCES)
253                         return SYSTEM_INFO_ERROR_PERMISSION_DENIED;
254                 return SYSTEM_INFO_ERROR_IO_ERROR;
255         }
256
257         ret = system_info_get_value_from_config_xml(PLATFORM_TAG, key, BOOL_TYPE, &string);
258         if (ret) {
259                 LOGE("cannot get %s", key);
260                 return ret;
261         }
262
263         if (!strcmp(string, "true") || !strcmp(string, "TRUE"))
264                 *supported = true;
265         else
266                 *supported = false;
267
268         free(string);
269
270         return SYSTEM_INFO_ERROR_NONE;
271 }
272
273 API int system_info_get_platform_int(const char *key, int *value)
274 {
275         int ret;
276         int *ret_val;
277         char *string = NULL;
278
279         ret_val = (int *)value;
280
281         if (access(CONFIG_FILE_PATH, R_OK)) {
282                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
283                 if (errno == EPERM || errno == EACCES)
284                         return SYSTEM_INFO_ERROR_PERMISSION_DENIED;
285                 return SYSTEM_INFO_ERROR_IO_ERROR;
286         }
287
288         ret = system_info_get_value_from_config_xml(PLATFORM_TAG, key, INT_TYPE, &string);
289         if (ret) {
290                 LOGE("cannot get %s", key);
291                 return ret;
292         }
293
294         *ret_val = atoi(string);
295
296         free(string);
297
298         return SYSTEM_INFO_ERROR_NONE;
299 }
300
301 API int system_info_get_platform_double(const char *key, double *value)
302 {
303         int ret;
304         double *ret_val;
305         char *string = NULL;
306
307         ret_val = (double *)value;
308
309         if (access(CONFIG_FILE_PATH, R_OK)) {
310                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
311                 if (errno == EPERM || errno == EACCES)
312                         return SYSTEM_INFO_ERROR_PERMISSION_DENIED;
313                 return SYSTEM_INFO_ERROR_IO_ERROR;
314         }
315
316         ret = system_info_get_value_from_config_xml(PLATFORM_TAG, key, DBL_TYPE, &string);
317         if (ret) {
318                 LOGE("cannot get %s", key);
319                 return ret;
320         }
321
322         *ret_val = atof(string);
323
324         free(string);
325
326         return SYSTEM_INFO_ERROR_NONE;
327 }
328
329 API int system_info_get_platform_string(const char *key, char **value)
330 {
331         int ret;
332         char *string = NULL;
333
334         if (access(CONFIG_FILE_PATH, R_OK)) {
335                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
336                 if (errno == EPERM || errno == EACCES)
337                         return SYSTEM_INFO_ERROR_PERMISSION_DENIED;
338                 return SYSTEM_INFO_ERROR_IO_ERROR;
339         }
340
341         ret = system_info_get_no_file(key, (void**)&string);
342         if (ret == 0) {
343                 *value = string;
344                 return SYSTEM_INFO_ERROR_NONE;
345         }
346
347         ret = system_info_get_value_from_config_xml(PLATFORM_TAG, key, STR_TYPE, &string);
348         if (ret) {
349                 LOGE("cannot get %s", key);
350                 return ret;
351         }
352
353         *value = string;
354
355         return SYSTEM_INFO_ERROR_NONE;
356 }
357
358 API int system_info_get_custom_bool(const char *key, bool *value)
359 {
360         int ret;
361         bool *supported;
362         char *string = NULL;
363
364         supported = (bool *)value;
365
366         if (access(CONFIG_FILE_PATH, R_OK)) {
367                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
368                 if (errno == EPERM || errno == EACCES)
369                         return SYSTEM_INFO_ERROR_PERMISSION_DENIED;
370                 return SYSTEM_INFO_ERROR_IO_ERROR;
371         }
372
373         ret = system_info_get_value_from_config_xml(CUSTOM_TAG, key, BOOL_TYPE, &string);
374         if (ret) {
375                 LOGE("cannot get %s info from %s!!!", key, CONFIG_FILE_PATH);
376                 return ret;
377         }
378
379         if (!strcmp(string, "true") || !strcmp(string, "TRUE"))
380                 *supported = true;
381         else
382                 *supported = false;
383
384         free(string);
385
386         return SYSTEM_INFO_ERROR_NONE;
387 }
388
389 API int system_info_get_custom_int(const char *key, int *value)
390 {
391         int ret;
392         int *ret_val;
393         char *string = NULL;
394
395         ret_val = (int *)value;
396
397         if (access(CONFIG_FILE_PATH, R_OK)) {
398                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
399                 if (errno == EPERM || errno == EACCES)
400                         return SYSTEM_INFO_ERROR_PERMISSION_DENIED;
401                 return SYSTEM_INFO_ERROR_IO_ERROR;
402         }
403
404         ret = system_info_get_value_from_config_xml(CUSTOM_TAG, key, INT_TYPE, &string);
405         if (ret) {
406                 LOGE("cannot get %s info from %s!!!", key, CONFIG_FILE_PATH);
407                 return ret;
408         }
409
410         *ret_val = atoi(string);
411
412         free(string);
413
414         return SYSTEM_INFO_ERROR_NONE;
415 }
416
417 API int system_info_get_custom_double(const char *key, double *value)
418 {
419         int ret;
420         double *ret_val;
421         char *string = NULL;
422
423         ret_val = (double *)value;
424
425         if (access(CONFIG_FILE_PATH, R_OK)) {
426                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
427                 if (errno == EPERM || errno == EACCES)
428                         return SYSTEM_INFO_ERROR_PERMISSION_DENIED;
429                 return SYSTEM_INFO_ERROR_IO_ERROR;
430         }
431
432         ret = system_info_get_value_from_config_xml(CUSTOM_TAG, key, DBL_TYPE, &string);
433         if (ret) {
434                 LOGE("cannot get %s info from %s!!!", key, CONFIG_FILE_PATH);
435                 return ret;
436         }
437
438         *ret_val = atof(string);
439
440         free(string);
441
442         return SYSTEM_INFO_ERROR_NONE;
443 }
444
445 API int system_info_get_custom_string(const char *key, char **value)
446 {
447         int ret;
448         char *string = NULL;
449
450         if (access(CONFIG_FILE_PATH, R_OK)) {
451                 LOGE("cannot find file %s!!!", CONFIG_FILE_PATH);
452                 if (errno == EPERM || errno == EACCES)
453                         return SYSTEM_INFO_ERROR_PERMISSION_DENIED;
454                 return SYSTEM_INFO_ERROR_IO_ERROR;
455         }
456
457         ret = system_info_get_value_from_config_xml(CUSTOM_TAG, key, STR_TYPE, &string);
458         if (ret) {
459                 LOGE("cannot get %s info from %s!!!", key, CONFIG_FILE_PATH);
460                 return ret;
461         }
462
463         *value = string;
464
465         return SYSTEM_INFO_ERROR_NONE;
466 }