Initialize Tizen 2.3
[framework/api/system-info.git] / src / system_info_hardware.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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdint.h>
21 #include <unistd.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_value_from_cpuinfo(char *field, char **value)
35 {
36         int tmpStrlen = 0;
37         FILE *cpuinfo = NULL;
38         char *name = NULL;
39         char str[MAXBUFSIZE] = "";
40         char tmpStr[MAXBUFSIZE] = "";
41
42         cpuinfo = fopen(CPU_INFO_FILE_PATH, "r");
43         if (NULL == cpuinfo) {
44                 LOGE("cannot file open %s file!!!", CPU_INFO_FILE_PATH);
45                 return SYSTEM_INFO_ERROR_IO_ERROR;
46         }
47
48         while (fgets(str, MAXBUFSIZE, cpuinfo)) {
49                 if (!strncmp(field, str, strlen(field))) {
50                         name = strchr(str, ':');
51                         tmpStrlen = strlen(name+2);
52                         strncpy(tmpStr, name+2, tmpStrlen);
53                         tmpStr[tmpStrlen-1] = '\0';
54                         *value = strdup(tmpStr);
55                         if (*value == NULL) {
56                                 LOGE("OUT_OF_MEMORY(0x%08x)", SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
57                                 fclose(cpuinfo);
58                                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
59                         }
60                 }
61         }
62
63         if (system_info_get_system_info_model_type() == SYSTEM_INFO_MODEL_TYPE_EMULATOR)
64                 *value = strdup("default");
65
66         if (*value == NULL) {
67                 LOGE("cannot get %s in cpuinfo", field);
68                 fclose(cpuinfo);
69                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
70         }
71
72         fclose(cpuinfo);
73
74         return SYSTEM_INFO_ERROR_NONE;
75 }
76
77 int system_info_get_bsp_info(const char *key, char **value)
78 {
79         char *id_field;
80         char *string = NULL;
81         char *model = NULL;
82         char *revision = NULL;
83         char Rrevision[MAXBUFSIZE] = "";
84         char file_path[MAXBUFSIZE] = "";
85
86         if (system_info_get_system_info_model_type() == SYSTEM_INFO_MODEL_TYPE_EMULATOR) {
87                 LOGE("cannot get Hardware info in emulator!!!");
88                 return SYSTEM_INFO_ERROR_IO_ERROR;
89         }
90
91         if (system_info_get_value_from_cpuinfo("Hardware", &model)) {
92                 LOGE("cannot get Hardware info from cpuinfo file!!!");
93                 return SYSTEM_INFO_ERROR_IO_ERROR;
94         }
95
96         if (system_info_get_value_from_cpuinfo("Revision", &revision)) {
97                 LOGE("cannot get Hardware info from cpuinfo file!!!");
98                 return SYSTEM_INFO_ERROR_IO_ERROR;
99         }
100
101         if (!revision || !strcmp(revision, "default")) {
102                 LOGE("cannot get revision info from cpuinfo file!!!");
103                 return SYSTEM_INFO_ERROR_IO_ERROR;
104         }
105
106         snprintf(Rrevision, MAXBUFSIZE, "R%s", revision);
107         snprintf(file_path, MAXBUFSIZE, "/etc/config/board-config-%s.xml", model);
108
109         if (access(file_path, R_OK)) {
110                 LOGE("cannot find file %s!!!", file_path);
111                 return SYSTEM_INFO_ERROR_IO_ERROR;
112         }
113
114         id_field = strdup(key);
115
116         if (system_info_get_value_from_xml(file_path, Rrevision, id_field, &string)) {
117                 free(id_field);
118                 free(model);
119                 free(revision);
120                 LOGE("cannot get aud_amrwb info from %s!!!", file_path);
121                 return SYSTEM_INFO_ERROR_IO_ERROR;
122         }
123
124         free(id_field);
125         free(model);
126         free(revision);
127
128         *value = string;
129
130         return SYSTEM_INFO_ERROR_NONE;
131 }