6 #include "system-info-tool.h"
7 #include "system-info-tool-get.h"
9 void system_info_tool_get_help(void)
11 printf(" -l|--list-all List all system-info database entries\n");
12 printf(" -g|--get KEY [-v|--verbose] Get value of feature KEY\n");
13 printf(" If -v option is given, shows result in detail\n");
14 printf(" ex) system-info-tool -g tizen.org/feature/display\n");
15 printf(" system-info-tool -g tizen.org/system/manufacturer -v\n");
18 static void print_dbentry(struct dbentry *dbentry, bool found)
22 if (!dbentry || !dbentry->db)
28 printf("%s Key (%c) found: ", db->name, db->ticker);
30 printf("%s Key (%c) not found\n", db->name, db->ticker);
34 print_value(dbentry->value);
37 int system_info_tool_get_raw(const char *searchkey, system_info_type_e searchtype,
38 enum db_e searchdb, struct dbentry *dbentry)
40 char dbpath[BUFFER_MAX - 1];
41 char dbentry_format[BUFFER_MAX];
47 if (searchdb < DB_START || searchdb >= DB_END)
50 dbentry->db = &db[searchdb];
52 /* scanf format: "%127[^:]:%127[^:]:%127s %127s" */
53 snprintf(dbentry_format, sizeof(dbentry_format), "%%%d[^:]:%%%d[^:]:%%%ds %%%ds",
54 BUFFER_MAX - 1, BUFFER_MAX - 1, BUFFER_MAX - 1, BUFFER_MAX - 1);
56 for (int i = 0; i < NUM_HASH_FILE; ++i) {
57 __auto_fclose__ FILE *fp = NULL;
58 char line[BUFFER_MAX * 8] = {0, };
59 char tag[BUFFER_MAX - 1] = {0, };
60 char key[BUFFER_MAX - 1] = {0, };
61 char type[BUFFER_MAX - 1] = {0, };
62 char value[BUFFER_MAX - 1] = {0, };
63 const char *typestring;
65 snprintf(dbpath, sizeof(dbpath), "%s/%d", db[searchdb].path, i);
66 fp = fopen(dbpath, "r");
70 while ((fgets(line, sizeof(line), fp))) {
71 retval = sscanf(line, dbentry_format, tag, key, type, value);
75 if (strncmp(key, searchkey, strlen(key) + 1))
78 typestring = type_to_string(searchtype);
79 if (strncmp(type, typestring, strlen(type) + 1))
82 strncpy(dbentry->key, key, sizeof(dbentry->key) - 1);
83 strncpy(dbentry->tag, tag, sizeof(dbentry->tag) - 1);
84 dbentry->value.type = searchtype;
85 if (strncmp(type, "bool", sizeof("bool")) == 0)
86 dbentry->value.b = (value[runtime_env] == 'T');
87 else if (strncmp(type, "int", sizeof("int")) == 0)
88 dbentry->value.i = atoi(value);
89 else if (strncmp(type, "double", sizeof("double")) == 0)
90 dbentry->value.d = atof(value);
91 else if (strncmp(type, "string", sizeof("string")) == 0)
92 dbentry->value.s = strndup(value, sizeof(value));
101 static int system_info_tool_get_api(const char *key, struct value *value)
112 } cache[SYSTEM_INFO_TYPE_MAX] = {
113 { -1, { SYSTEM_INFO_BOOL, } },
114 { -1, { SYSTEM_INFO_INT, } },
115 { -1, { SYSTEM_INFO_DOUBLE, } },
116 { -1, { SYSTEM_INFO_STRING, } },
119 retval = system_info_get_platform_bool(key, &cache[SYSTEM_INFO_BOOL].value.b);
121 cache[SYSTEM_INFO_BOOL].match = ++match;
123 retval = system_info_get_platform_int(key, &cache[SYSTEM_INFO_INT].value.i);
125 cache[SYSTEM_INFO_INT].match = ++match;
127 retval = system_info_get_platform_double(key, &cache[SYSTEM_INFO_DOUBLE].value.d);
129 cache[SYSTEM_INFO_DOUBLE].match = ++match;
131 retval = system_info_get_platform_string(key, &cache[SYSTEM_INFO_STRING].value.s);
133 cache[SYSTEM_INFO_STRING].match = ++match;
136 for (i = 0; i < SYSTEM_INFO_TYPE_MAX; ++i)
137 if (cache[i].match == match)
138 memcpy(value, &cache[i].value, sizeof(struct value));
140 } else if (match > 1) {
141 printf("There is multiple same keys with different type. Which type to search?\n");
142 for (i = 0; i < SYSTEM_INFO_TYPE_MAX; ++i)
143 if (cache[i].match > 0) {
144 printf(" [%d] ", cache[i].match);
145 print_value(cache[i].value);
149 retval = scanf("%d", &select);
151 printf("Invalid selection\n");
156 for (i = 0; i < SYSTEM_INFO_TYPE_MAX; ++i) {
157 if (cache[i].match == select) {
158 memcpy(value, &cache[i].value, sizeof(struct value));
163 if (i == SYSTEM_INFO_TYPE_MAX) {
164 printf("Invalid selection\n");
170 printf("key=%s is not found\n", key);
174 free(cache[SYSTEM_INFO_STRING].value.s);
179 void system_info_tool_get(const char *rawkey, bool verbose)
182 char key[BUFFER_MAX];
185 retval = convert_raw_key(rawkey, key, sizeof(key));
189 retval = system_info_tool_get_api(key, &api);
193 /* show each database if -v option is given */
199 /* set db to be searched */
200 __auto_free_dbentry__ struct dbentry plt = { 0, }; /* scan platform ro db */
201 __auto_free_dbentry__ struct dbentry hal = { 0, }; /* scan platform hal ro db */
202 __auto_free_dbentry__ struct dbentry usr = { 0, }; /* scan platform rw db */
204 retplt = system_info_tool_get_raw(key, api.type, DB_DEFAULT_RO, &plt);
205 print_dbentry(&plt, retplt == 0);
207 rethal = system_info_tool_get_raw(key, api.type, DB_HAL_RO, &hal);
208 print_dbentry(&hal, rethal == 0);
210 retusr = system_info_tool_get_raw(key, api.type, DB_DEFAULT_RW, &usr);
211 print_dbentry(&usr, retusr == 0);
213 /* print description how API have derived the result */
215 printf("Result (=%c overrides %c,%c): ", usr.db->ticker, plt.db->ticker, hal.db->ticker);
216 } else if (retplt == 0 && rethal == 0) {
217 if (plt.value.type == SYSTEM_INFO_BOOL && hal.value.type == SYSTEM_INFO_BOOL)
218 printf("Result (=%c && %c): ", plt.db->ticker, hal.db->ticker);
220 printf("Result (=%c overrides %c): ", hal.db->ticker, plt.db->ticker);
221 } else if (retplt == 0) {
222 printf("Result (=%c): ", plt.db->ticker);
223 } else if (rethal == 0) {
224 printf("Result (=%c): ", hal.db->ticker);
226 printf("Result (error occured): ");
235 void system_info_tool_list_all(enum db_e dbtype)
237 char dbpath[BUFFER_MAX - 1];
238 char dbentry_format[BUFFER_MAX];
241 if (access(db[dbtype].path, F_OK) != 0)
244 /* scanf format: "%127[^:]:%127[^:]:%127s %127s" */
245 snprintf(dbentry_format, sizeof(dbentry_format), "%%%d[^:]:%%%d[^:]:%%%ds %%%ds",
246 BUFFER_MAX - 1, BUFFER_MAX - 1, BUFFER_MAX - 1, BUFFER_MAX - 1);
248 for (int i = 0; i < NUM_HASH_FILE; ++i) {
249 __auto_fclose__ FILE *fp = NULL;
250 char line[BUFFER_MAX * 8] = {0, };
251 char tag[BUFFER_MAX - 1] = {0, };
252 char key[BUFFER_MAX - 1] = {0, };
253 char type[BUFFER_MAX - 1] = {0, };
254 char value[BUFFER_MAX - 1] = {0, };
256 snprintf(dbpath, sizeof(dbpath), "%s/%d", db[dbtype].path, i);
257 fp = fopen(dbpath, "r");
261 while ((fgets(line, sizeof(line), fp))) {
262 retval = sscanf(line, dbentry_format, tag, key, type, value);
266 printf("%s: ", db[dbtype].name);
267 printf("key=%s, ", key);
268 printf("type=%s, ", type);
269 if (strncmp(type, "bool", sizeof("bool")) == 0)
270 printf("value=%d, ", (value[runtime_env] == 'T'));
272 printf("value=%s, ", value);
273 printf("tag=%s\n", tag);