Initialize Tizen 2.3
[framework/api/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
23 #include <dlog.h>
24
25 #include <system_info.h>
26 #include <system_info_private.h>
27
28 #ifdef LOG_TAG
29 #undef LOG_TAG
30 #endif
31
32 #define LOG_TAG "CAPI_SYSTEM_INFO"
33
34 int system_info_get_model(system_info_key_e key, system_info_data_type_e data_type, void **value)
35 {
36         return system_info_ini_get_string(INFO_FILE_PATH, "version:model", (char **)value);
37 }
38
39 int system_info_get_build_string(system_info_key_e key, system_info_data_type_e data_type, void **value)
40 {
41         return system_info_ini_get_string(INFO_FILE_PATH, "version:build", (char **)value);
42 }
43
44 int system_info_get_build_date(system_info_key_e key, system_info_data_type_e data_type, void **value)
45 {
46         return system_info_ini_get_string(INFO_FILE_PATH, "build:date", (char **)value);
47 }
48
49 int system_info_get_build_time(system_info_key_e key, system_info_data_type_e data_type, void **value)
50 {
51         return system_info_ini_get_string(INFO_FILE_PATH, "build:time", (char **)value);
52 }
53
54 int system_info_get_tizen_version(system_info_key_e key, system_info_data_type_e data_type, void **value)
55 {
56         return system_info_get_platform_string("tizen.org/feature/platform.version", (char**)value);
57 }
58
59 int system_info_get_core_cpu_arch(system_info_key_e key, system_info_data_type_e data_type, void **value)
60 {
61         bool cpu_arch;
62         char *CORE_CPU_ARCH = NULL;
63
64         if (system_info_get_platform_bool("tizen.org/feature/platform.core.cpu.arch.armv6", &cpu_arch) == SYSTEM_INFO_ERROR_NONE
65                 && cpu_arch == true)
66                 CORE_CPU_ARCH = strdup("armv6");
67         else if (system_info_get_platform_bool("tizen.org/feature/platform.core.cpu.arch.armv7", &cpu_arch) == SYSTEM_INFO_ERROR_NONE
68                 && cpu_arch == true)
69                 CORE_CPU_ARCH = strdup("armv7");
70         else if (system_info_get_platform_bool("tizen.org/feature/platform.core.cpu.arch.x86", &cpu_arch) == SYSTEM_INFO_ERROR_NONE
71                 && cpu_arch == true)
72                 CORE_CPU_ARCH = strdup("x86");
73
74         if (CORE_CPU_ARCH == NULL) {
75                 LOGE("Unknown cpu");
76                 return SYSTEM_INFO_ERROR_IO_ERROR;
77         }
78
79         *value = CORE_CPU_ARCH;
80
81         return SYSTEM_INFO_ERROR_NONE;
82 }
83
84 int system_info_get_core_cpu_freq(system_info_key_e key, system_info_data_type_e data_type, void **value)
85 {
86         double *count;
87         FILE *cpuinfo, *cpuinfo_max_freq;
88         double max_freq = 0.0;
89         char *name;
90         char str[MAXBUFSIZE];
91
92         if (system_info_get_system_info_model_type() != SYSTEM_INFO_MODEL_TYPE_EMULATOR) {
93                 cpuinfo_max_freq = fopen(CPU_INFO_MAX_FREQ_PATH, "r");
94                 if (NULL == cpuinfo_max_freq) {
95                         LOGE("cannot file open %s file!!!", CPU_INFO_MAX_FREQ_PATH);
96                         return SYSTEM_INFO_ERROR_IO_ERROR;
97                 } else {
98                         if (fscanf(cpuinfo_max_freq, "%lf", &max_freq) < 1) {
99                                 fclose(cpuinfo_max_freq);
100                                 return SYSTEM_INFO_ERROR_IO_ERROR;
101                         }
102                         max_freq = max_freq / 1024;
103                 }
104                 fclose(cpuinfo_max_freq);
105         } else {
106                 /* Emulator */
107                 cpuinfo = fopen(CPU_INFO_FILE_PATH, "r");
108                 if (NULL == cpuinfo) {
109                         LOGE("cannot file open %s file!!!", CPU_INFO_FILE_PATH);
110                         return SYSTEM_INFO_ERROR_IO_ERROR;
111                 } else {
112                         while (fgets(str, MAXBUFSIZE, cpuinfo)) {
113                                 if (!strncmp("cpu MHz", str, strlen("cpu MHz"))) {
114                                         name = strchr(str, ':');
115                                         max_freq = atof(name+2);
116                                         break;
117                                 }
118                         }
119                 }
120                 fclose(cpuinfo);
121         }
122
123         count = (double *)value;
124
125         *count = max_freq;
126
127         return SYSTEM_INFO_ERROR_NONE;
128 }
129
130 int system_info_get_platform_name(system_info_key_e key, system_info_data_type_e data_type, void **value)
131 {
132         return system_info_get_platform_string("tizen.org/system/platform.name", (char**)value);
133 }
134
135 int system_info_get_tizen_version_name(system_info_key_e key, system_info_data_type_e data_type, void **value)
136 {
137         char *TIZEN_VERSION_NAME = NULL;
138         char *name = NULL;
139         char str[MAXBUFSIZE];
140         char tmpStr[MAXBUFSIZE];
141         int tmpStrlen = 0;
142         FILE *info;
143         extern char *strcasestr(const char *s, const char *find);
144
145         info = fopen(OS_RELEASE_FILE_PATH, "r");
146         if (NULL == info) {
147                 LOGE("cannot file open %s file!!!", OS_RELEASE_FILE_PATH);
148                 return SYSTEM_INFO_ERROR_IO_ERROR;
149         }
150
151         while (fgets(str, MAXBUFSIZE, info)) {
152                 if (strcasestr(str, "VERSION")) {
153                         name = strchr(str, ',');
154                         name += 2;
155                         tmpStrlen = strlen(name);
156
157                         strncpy(tmpStr, name, tmpStrlen-2);
158                         tmpStr[tmpStrlen-2] = '\0';
159
160                         TIZEN_VERSION_NAME = strdup(tmpStr);
161                         if (TIZEN_VERSION_NAME == NULL) {
162                                 LOGE("OUT_OF_MEMORY(0x%08x)", SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
163                                 fclose(info);
164                                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
165                         }
166                         break;
167                 }
168         }
169
170         *value = TIZEN_VERSION_NAME;
171         fclose(info);
172         return SYSTEM_INFO_ERROR_NONE;
173 }