2.0_alpha release commit
[profile/ivi/system-info.git] / src / system_info_platform.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 <unistd.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 #include <dlog.h>
27 #include <vconf.h>
28 #include <iniparser.h>
29
30 #include <system_info.h>
31 #include <system_info_private.h>
32
33 #include <GLES/gl.h>
34 #include <GLES/glext.h>
35
36 #ifdef LOG_TAG
37 #undef LOG_TAG
38 #endif
39
40 #define LOG_TAG "TIZEN_N_SYSTEM_INFO"
41
42 #define LIB_GLES_V1 "/usr/lib/libGLESv1_CM.so"
43 #define LIB_GLES_V2 "/usr/lib/libGLESv2.so"
44
45 #define SIZE_OF_MODEL_NAME 8
46
47 int system_info_get_model(system_info_key_e key, system_info_data_type_e data_type, void **value)
48 {
49         FILE *info = NULL;
50         char *name = NULL;
51         char *MODEL = NULL;
52         char *token = NULL;
53         char tmpStr[MAXBUFSIZE];
54         char str[MAXBUFSIZE];
55         extern char *strcasestr(const char *s, const char *find);
56
57         if (system_info_get_system_info_model_type() != SYSTEM_INFO_MODEL_TYPE_EMULATOR) {
58                 info = fopen(INFO_FILE_PATH, "r");
59                 if (NULL == info) {
60                         LOGE("[%s] cannot file open %s file!!!", __func__, INFO_FILE_PATH);
61                         return SYSTEM_INFO_ERROR_IO_ERROR;
62                 } else {
63                         while (fgets(str, MAXBUFSIZE, info)) {
64                                 if (strncmp("Build", str, strlen("Build")))
65                                         continue;
66                                 else {
67                                         /* Open SDK Target*/
68                                         if (strcasestr(str, "Tizen")) {
69                                                 strcpy(tmpStr, str);
70                                                 token = strtok(tmpStr, "_");
71                                                 token = strtok(NULL, "_");
72                                                 MODEL = strdup(token);
73                                                 if (MODEL == NULL) {
74                                                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __func__, SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
75                                                         fclose(info);
76                                                         return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
77                                                 }
78                                         } else {
79                                                 /* DEV, SEL, REL Target */
80                                                 name = strchr(str, '=');
81                                                 strncpy(tmpStr, name+1, SIZE_OF_MODEL_NAME);
82                                                 tmpStr[SIZE_OF_MODEL_NAME] = '\0';
83                                                 MODEL = strdup(tmpStr);
84                                                 if (MODEL == NULL) {
85                                                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __func__, SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
86                                                         fclose(info);
87                                                         return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
88                                                 }
89                                         }
90                                         break;
91                                 }
92                         }
93                         fclose(info);
94                 }
95         } else {
96                 /* Emulator */
97                 MODEL = strdup("Emulator");
98                 if (MODEL == NULL) {
99                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __func__, SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
100                         return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
101                 }
102         }
103
104         *value = MODEL;
105
106         return SYSTEM_INFO_ERROR_NONE;
107 }
108
109 int system_info_get_tizen_version(system_info_key_e key, system_info_data_type_e data_type, void **value)
110 {
111         char *TIZEN_VERSION = NULL;
112         char *name = NULL;
113         char *major = NULL;
114         char *minor = NULL;
115         char *order = NULL;
116         char str[MAXBUFSIZE];
117         char tmpStr[MAXBUFSIZE];
118         FILE *info;
119
120
121         info = fopen(INFO_FILE_PATH, "r");
122         if (NULL == info) {
123                 LOGE("[%s] cannot file open %s file!!!", __func__, INFO_FILE_PATH);
124                 return SYSTEM_INFO_ERROR_IO_ERROR;
125         } else {
126                 while (fgets(str, MAXBUFSIZE, info)) {
127                         if (!strncmp("Major", str, strlen("Major"))) {
128                                 name = strchr(str, '=');
129                                 name++;
130                                 strncpy(tmpStr, name, strlen(name)-2);
131                                 tmpStr[strlen(name)-2] = '\0';
132                                 major = strdup(tmpStr);
133                                 continue;
134                         } else if (!strncmp("Minor", str, strlen("Minor"))) {
135                                 name = strchr(str, '=');
136                                 name++;
137                                 strncpy(tmpStr, name, strlen(name)-2);
138                                 tmpStr[strlen(name)-2] = '\0';
139                                 minor = strdup(tmpStr);
140                                 continue;
141                         } else if (!strncmp("Order", str, strlen("Order"))) {
142                                 name = strchr(str, '=');
143                                 name++;
144                                 strncpy(tmpStr, name, strlen(name)-2);
145                                 tmpStr[strlen(name)-2] = '\0';
146                                 if (!strcmp(tmpStr, ""))
147                                         order = strdup("0");
148                                 else
149                                         order = strdup(tmpStr);
150                                 continue;
151                         } else
152                                 continue;
153                 }
154         }
155         fclose(info);
156
157         TIZEN_VERSION = (char *)malloc(strlen(major)+strlen(minor)+strlen(order)+3);
158
159         if (TIZEN_VERSION == NULL) {
160                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __func__, SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
161                 free(major);
162                 free(minor);
163                 free(order);
164                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
165         }
166
167         memset(TIZEN_VERSION, 0, strlen(major)+strlen(minor)+strlen(order)+3);
168
169         sprintf(TIZEN_VERSION, "%s.%s.%s", major, minor, order);
170         TIZEN_VERSION[strlen(TIZEN_VERSION)] = '\0';
171
172         *value = TIZEN_VERSION;
173
174         free(major);
175         free(minor);
176         free(order);
177         return SYSTEM_INFO_ERROR_NONE;
178 }
179
180 int system_info_get_platform_name(system_info_key_e key, system_info_data_type_e data_type, void **value)
181 {
182         char *PLATFORM_NAME = NULL;
183
184         PLATFORM_NAME = strdup("TIZEN");
185         if (PLATFORM_NAME == NULL) {
186                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __func__, SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
187                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
188         }
189
190         *value = PLATFORM_NAME;
191
192         return SYSTEM_INFO_ERROR_NONE;
193 }
194
195 int system_info_get_tizen_version_name(system_info_key_e key, system_info_data_type_e data_type, void **value)
196 {
197         char *TIZEN_VERSION_NAME = NULL;
198         char *name = NULL;
199         char str[MAXBUFSIZE];
200         FILE *info = NULL;
201
202         info = fopen(INFO_FILE_PATH, "r");
203         if (NULL == info) {
204                 LOGE("[%s] cannot file open %s file!!!", __func__, INFO_FILE_PATH);
205                 return SYSTEM_INFO_ERROR_IO_ERROR;
206         } else {
207                 while (fgets(str, MAXBUFSIZE, info)) {
208                         if (!strncmp("Major", str, strlen("Major"))) {
209                                 name = strchr(str, '=');
210                                 name++;
211                                 if (!strncmp("1", name, 1))
212                                         TIZEN_VERSION_NAME = strdup("Larkspur");
213                                 else if (!strncmp("2", name, 1))
214                                         TIZEN_VERSION_NAME = strdup("Magnolia");
215                                 else
216                                         TIZEN_VERSION_NAME = strdup("Unknown Version Name");
217
218                                 if (TIZEN_VERSION_NAME == NULL) {
219                                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __func__, SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
220                                         fclose(info);
221                                         return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
222                                 }
223
224                                 break;
225                         } else
226                                 continue;
227                 }
228         }
229         *value = TIZEN_VERSION_NAME;
230         fclose(info);
231         return SYSTEM_INFO_ERROR_NONE;
232 }
233
234 int system_info_get_opengles_version(system_info_key_e key, system_info_data_type_e data_type, void **value)
235 {
236         char *version;
237         char *OPENGLES_VERSION = NULL;
238         int version_supported = 0;
239
240         if (!access(LIB_GLES_V1, R_OK))
241                 version_supported += 1;
242
243         if (!access(LIB_GLES_V2, R_OK))
244                 version_supported += 2;
245
246         switch (version_supported) {
247         case 1:
248                 version = "1.1";
249                 break;
250
251         case 2:
252                 version = "2.0";
253                 break;
254
255         case 3:
256                 version = "1.0/2.0";
257                 break;
258         default:
259                 version = NULL;
260                 break;
261         }
262
263         if (version != NULL) {
264                 OPENGLES_VERSION = strdup(version);
265
266                 if (OPENGLES_VERSION == NULL) {
267                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __func__, SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
268                         return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
269                 }
270         }
271
272         *value = OPENGLES_VERSION;
273
274         return SYSTEM_INFO_ERROR_NONE;
275 }
276
277 int system_info_get_core_cpu_arch(system_info_key_e key, system_info_data_type_e data_type, void **value)
278 {
279         FILE *cpuinfo;
280         char *CORE_CPU_ARCH = NULL;
281         char *name;
282         char str[MAXBUFSIZE];
283
284         if (system_info_get_system_info_model_type() != SYSTEM_INFO_MODEL_TYPE_EMULATOR) {
285                 cpuinfo = fopen(CPU_INFO_FILE_PATH, "r");
286                 if (NULL == cpuinfo) {
287                         LOGE("[%s] cannot file open %s file!!!", __func__, CPU_INFO_FILE_PATH);
288                         return SYSTEM_INFO_ERROR_IO_ERROR;
289                 } else {
290                         while (fgets(str, MAXBUFSIZE, cpuinfo)) {
291                                 if (!strncmp("Processor", str, strlen("Processor"))) {
292                                         name = strchr(str, ':');
293                                         if (!(strncmp("ARMv7", name+2, strlen("ARMv7"))))
294                                                 CORE_CPU_ARCH = strdup("ARMv7");
295                                         else if (!(strncmp("ARMv6", name+2, strlen("ARMv6"))))
296                                                 CORE_CPU_ARCH = strdup("ARMv6");
297                                         else if (!(strncmp("x86", name+2, strlen("x86"))))
298                                                 CORE_CPU_ARCH = strdup("x86");
299                                 } else
300                                         continue;
301                         }
302                         fclose(cpuinfo);
303                 }
304         } else {
305                 /* Emulator */
306                 CORE_CPU_ARCH = NULL;
307         }
308
309         *value = CORE_CPU_ARCH;
310
311         return SYSTEM_INFO_ERROR_NONE;
312 }
313
314 int system_info_get_core_fpu_arch(system_info_key_e key, system_info_data_type_e data_type, void **value)
315 {
316         FILE *cpuinfo;
317         char *CORE_FPU_ARCH = NULL;
318         char *name;
319         char str[MAXBUFSIZE];
320
321         if (system_info_get_system_info_model_type() != SYSTEM_INFO_MODEL_TYPE_EMULATOR) {
322                 cpuinfo = fopen(CPU_INFO_FILE_PATH, "r");
323                 if (NULL == cpuinfo) {
324                         LOGE("[%s] cannot file open %s file!!!", __func__, CPU_INFO_FILE_PATH);
325                         return SYSTEM_INFO_ERROR_IO_ERROR;
326                 } else {
327                         while (fgets(str, MAXBUFSIZE, cpuinfo)) {
328                                 if (!strncmp("Features", str, strlen("Features"))) {
329                                         name = strchr(str, ':');
330                                         if (strstr(name+2, "vfpv3"))
331                                                 CORE_FPU_ARCH = strdup("vfpv3");
332                                         else if (strstr(name+2, "vfpv2"))
333                                                 CORE_FPU_ARCH = strdup("vfpv2");
334                                         else if (strstr(name+2, "sse2"))
335                                                 CORE_FPU_ARCH = strdup("sse2");
336                                         else if (strstr(name+2, "sse3"))
337                                                 CORE_FPU_ARCH = strdup("sse3");
338                                         else if (strstr(name+2, "ssse3"))
339                                                 CORE_FPU_ARCH = strdup("ssse3");
340                                 } else
341                                         continue;
342                         }
343                         fclose(cpuinfo);
344                 }
345         } else {
346                 /* Emulator */
347                 CORE_FPU_ARCH = NULL;
348         }
349
350         *value = CORE_FPU_ARCH;
351
352         return SYSTEM_INFO_ERROR_NONE;
353 }
354
355 int system_info_get_core_cpu_freq(system_info_key_e key, system_info_data_type_e data_type, void **value)
356 {
357         double *count;
358         FILE *cpuinfo, *cpuinfo_max_freq;
359         double max_freq;
360         char *name;
361         char str[MAXBUFSIZE];
362
363         if (system_info_get_system_info_model_type() != SYSTEM_INFO_MODEL_TYPE_EMULATOR) {
364                 cpuinfo_max_freq = fopen(CPU_INFO_MAX_FREQ_PATH, "r");
365                 if (NULL == cpuinfo_max_freq) {
366                         LOGE("[%s] cannot file open %s file!!!", __func__, CPU_INFO_MAX_FREQ_PATH);
367                         return SYSTEM_INFO_ERROR_IO_ERROR;
368                 } else {
369                         if (fscanf(cpuinfo_max_freq, "%lf", &max_freq) < 1) {
370                                 fclose(cpuinfo_max_freq);
371                                 return SYSTEM_INFO_ERROR_IO_ERROR;
372                         }
373                         max_freq = max_freq / 1024;
374                 }
375         } else {
376                 /* Emulator */
377                 cpuinfo = fopen(CPU_INFO_FILE_PATH, "r");
378                 if (NULL == cpuinfo) {
379                         LOGE("[%s] cannot file open %s file!!!", __func__, CPU_INFO_FILE_PATH);
380                         return SYSTEM_INFO_ERROR_IO_ERROR;
381                 } else {
382                         while (fgets(str, MAXBUFSIZE, cpuinfo)) {
383                                 if (!strncmp("cpu MHz", str, strlen("cpu MHz"))) {
384                                         name = strchr(str, ':');
385                                         max_freq = atof(name+2);
386                                         break;
387                                 }
388                         }
389                 }
390         }
391
392         count = (double *)value;
393
394         *count = max_freq;
395
396         return SYSTEM_INFO_ERROR_NONE;
397 }
398
399
400 int system_info_get_opengles_texture_format(system_info_key_e key, system_info_data_type_e data_type, void **value)
401 {
402         int cnt = 0;
403         char *OPENGLES_TEXTURE_FORMAT;
404         char textureformat[MAXBUFSIZE];
405
406         memset(textureformat, 0, MAXBUFSIZE);
407
408         if (!access(LIB_GLES_V1, R_OK)) {
409                 strncpy(textureformat+cnt, "utc ", strlen("utc "));
410                 cnt += strlen("utc ");
411         }
412         if (GL_OES_compressed_paletted_texture) {
413                 strncpy(textureformat+cnt, "| ptc ", strlen("| ptc "));
414                 cnt += strlen("| ptc ");
415         }
416         if (GL_OES_compressed_ETC1_RGB8_texture) {
417                 strncpy(textureformat+cnt, "| etc ", strlen("| etc "));
418                 cnt += strlen("| etc ");
419         }
420         if (GL_AMD_compressed_3DC_texture) {
421                 strncpy(textureformat+cnt, "| 3dc ", strlen("| 3dc "));
422                 cnt += strlen("| 3dc ");
423         }
424         if (GL_AMD_compressed_ATC_texture) {
425                 strncpy(textureformat+cnt, "| atc ", strlen("| atc "));
426                 cnt += strlen("| atc ");
427         }
428         if (GL_IMG_texture_compression_pvrtc) {
429                 strncpy(textureformat+cnt, "| pvrtc", strlen("| pvrtc"));
430                 cnt += strlen("| pvrtc");
431         }
432
433         OPENGLES_TEXTURE_FORMAT = strdup(textureformat);
434
435         if (OPENGLES_TEXTURE_FORMAT == NULL) {
436                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __func__, SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
437                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
438         }
439
440         *value = OPENGLES_TEXTURE_FORMAT;
441
442         return SYSTEM_INFO_ERROR_NONE;
443 }
444
445 int system_info_get_build_string(system_info_key_e key, system_info_data_type_e data_type, void **value)
446 {
447         char *BUILD_STRING = NULL;
448         char *name = NULL;
449         char str[MAXBUFSIZE];
450         char tmpStr[MAXBUFSIZE];
451         FILE *info = NULL;
452
453         info = fopen(INFO_FILE_PATH, "r");
454         if (NULL == info) {
455                 LOGE("[%s] cannot file open %s file!!!", __func__, INFO_FILE_PATH);
456                 return SYSTEM_INFO_ERROR_IO_ERROR;
457         } else {
458                 while (fgets(str, MAXBUFSIZE, info)) {
459                         if (!strncmp("Build", str, strlen("Build"))) {
460                                 name = strchr(str, '=');
461                                 name++;
462                                 strncpy(tmpStr, name, strlen(name)-2);
463                                 tmpStr[strlen(name)-2] = '\0';
464
465                                 BUILD_STRING = strdup(tmpStr);
466                                 if (BUILD_STRING == NULL) {
467                                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __func__, SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
468                                         fclose(info);
469                                         return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
470                                 }
471                                 break;
472                         } else
473                                 continue;
474                 }
475         }
476         *value = BUILD_STRING;
477         fclose(info);
478         return SYSTEM_INFO_ERROR_NONE;
479 }
480
481 int system_info_get_build_date(system_info_key_e key, system_info_data_type_e data_type, void **value)
482 {
483         char *BUILD_DATE = NULL;
484         char *name = NULL;
485         char str[MAXBUFSIZE];
486         char tmpStr[MAXBUFSIZE];
487         FILE *info = NULL;
488
489         info = fopen(INFO_FILE_PATH, "r");
490         if (NULL == info) {
491                 LOGE("[%s] cannot file open %s file!!!", __func__, INFO_FILE_PATH);
492                 return SYSTEM_INFO_ERROR_IO_ERROR;
493         } else {
494                 while (fgets(str, MAXBUFSIZE, info)) {
495                         if (!strncmp("Date", str, strlen("Date"))) {
496                                 name = strchr(str, '=');
497                                 name++;
498                                 strncpy(tmpStr, name, strlen(name)-2);
499                                 tmpStr[strlen(name)-2] = '\0';
500
501                                 BUILD_DATE = strdup(tmpStr);
502                                 if (BUILD_DATE == NULL) {
503                                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __func__, SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
504                                         fclose(info);
505                                         return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
506                                 }
507                                 break;
508                         } else
509                                 continue;
510                 }
511         }
512         *value = BUILD_DATE;
513         fclose(info);
514         return SYSTEM_INFO_ERROR_NONE;
515 }
516
517 int system_info_get_build_time(system_info_key_e key, system_info_data_type_e data_type, void **value)
518 {
519         char *BUILD_TIME = NULL;
520         char *name = NULL;
521         char str[MAXBUFSIZE];
522         char tmpStr[MAXBUFSIZE];
523         FILE *info = NULL;
524
525         info = fopen(INFO_FILE_PATH, "r");
526         if (NULL == info) {
527                 LOGE("[%s] cannot file open %s file!!!", __func__, INFO_FILE_PATH);
528                 return SYSTEM_INFO_ERROR_IO_ERROR;
529         } else {
530                 while (fgets(str, MAXBUFSIZE, info)) {
531                         if (!strncmp("Time", str, strlen("Time"))) {
532                                 name = strchr(str, '=');
533                                 name++;
534                                 strncpy(tmpStr, name, strlen(name)-2);
535                                 tmpStr[strlen(name)-2] = '\0';
536
537                                 BUILD_TIME = strdup(tmpStr);
538                                 if (BUILD_TIME == NULL) {
539                                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __func__, SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
540                                         fclose(info);
541                                         return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
542                                 }
543                                 break;
544                         } else
545                                 continue;
546                 }
547         }
548         *value = BUILD_TIME;
549         fclose(info);
550         return SYSTEM_INFO_ERROR_NONE;
551 }