test : update CMakeFile.txt
[platform/core/api/system-info.git] / src / system_info_file.c
1 /*
2  * Copyright (c) 2014 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 <unistd.h>
21 #include <fcntl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24
25 #include <system_info.h>
26 #include <system_info_private.h>
27
28 #define KEY_PREFIX "http://"
29
30 #define BUF_MAX 256
31 #define KEY_MAX 128
32
33 #define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0]))
34
35 static int get_tizenid(char *val, size_t len)
36 {
37         char id[BUF_MAX];
38         FILE *fp;
39
40         fp = fopen(TIZEN_ID_PATH, "r");
41         if (!fp) {
42                 _E("Failed to open file (%s)", TIZEN_ID_PATH); //LCOV_EXCL_LINE
43                 return SYSTEM_INFO_ERROR_IO_ERROR; //LCOV_EXCL_LINE
44         }
45
46         if (fgets(id, sizeof(id), fp) == NULL) {
47                 _E("Failed to get string (errno:%d)", errno); //LCOV_EXCL_LINE
48                 fclose(fp); //LCOV_EXCL_LINE
49                 return SYSTEM_INFO_ERROR_IO_ERROR; //LCOV_EXCL_LINE
50         }
51
52         fclose(fp);
53
54         if (strlen(id) == 0) {
55                 _E("String length of id is 0"); //LCOV_EXCL_LINE
56                 return SYSTEM_INFO_ERROR_IO_ERROR; //LCOV_EXCL_LINE
57         }
58
59         snprintf(val, len, "%s", id);
60         return SYSTEM_INFO_ERROR_NONE;
61 }
62
63 struct system_info_file_key {
64         const char *key;
65         int (*get_value)(char *val, size_t len);
66         system_info_type_e type;
67 } info_file_key[] = {
68         { "http://tizen.org/system/tizenid", get_tizenid, SYSTEM_INFO_STRING },
69 };
70
71 int system_info_get_file(const char *key, char *value, size_t len)
72 {
73         char p_key[KEY_MAX];
74         int i;
75         size_t p_len;
76
77         if (!key || !value)
78                 return SYSTEM_INFO_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
79
80         if (strstr(key, KEY_PREFIX) == key)
81                 snprintf(p_key, sizeof(p_key), "%s", key);
82         else
83                 snprintf(p_key, sizeof(p_key), "%s%s", KEY_PREFIX, key);
84
85         p_len = strlen(p_key) + 1;
86         for (i = 0 ; i < ARRAY_SIZE(info_file_key); i++)
87                 if (!strncmp(p_key, info_file_key[i].key, p_len))
88                         return info_file_key[i].get_value(value, len);
89
90         return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
91 }
92
93 int system_info_get_type_file(const char *key, system_info_type_e *type)
94 {
95         char p_key[KEY_MAX];
96         int i, len;
97
98         if (!key)
99                 return SYSTEM_INFO_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
100
101         if (strstr(key, KEY_PREFIX) == key)
102                 snprintf(p_key, sizeof(p_key), "%s", key);
103         else
104                 snprintf(p_key, sizeof(p_key), "%s%s", KEY_PREFIX, key);
105
106         len = strlen(p_key) + 1;
107         for (i = 0 ; i < ARRAY_SIZE(info_file_key); i++) {
108                 if (strncmp(p_key, info_file_key[i].key, len))
109                         continue;
110                 *type = info_file_key[i].type;
111                 return SYSTEM_INFO_ERROR_NONE;
112         }
113
114         return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
115 }