tizen id: change tizen id path and smack label
[platform/core/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] = { 0, };
40         char tmpStr[MAXBUFSIZE] = { 0, };
41
42         if (system_info_get_system_info_model_type() == SYSTEM_INFO_MODEL_TYPE_EMULATOR) {
43                 snprintf(tmpStr, sizeof(tmpStr), "default");
44                 goto out;
45         }
46
47         cpuinfo = fopen(CPU_INFO_FILE_PATH, "r");
48         if (NULL == cpuinfo) {
49                 LOGE("cannot file open %s file!!!", CPU_INFO_FILE_PATH);
50                 if (errno == EPERM || errno == EACCES)
51                         return SYSTEM_INFO_ERROR_PERMISSION_DENIED;
52                 return SYSTEM_INFO_ERROR_IO_ERROR;
53         }
54
55         while (fgets(str, sizeof(str), cpuinfo)) {
56                 if (strncmp(field, str, strlen(field)))
57                         continue;
58
59                 name = strchr(str, ':');
60                 tmpStrlen = strlen(name+2);
61                 strncpy(tmpStr, name+2, tmpStrlen);
62                 tmpStr[tmpStrlen-1] = '\0';
63                 break;
64         }
65
66         fclose(cpuinfo);
67
68 out:
69         if (strlen(tmpStr) == 0) {
70                 LOGE("Failed to get (%s) info", field);
71                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
72         }
73
74         *value = strdup(tmpStr);
75         if (*value == NULL) {
76                 LOGE("malloc() failed to get (%s) info", field);
77                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
78         }
79
80         return SYSTEM_INFO_ERROR_NONE;
81 }