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)
21 char prefix[BUFFER_MAX];
23 if (!dbentry || !dbentry->db)
28 snprintf(prefix, sizeof(prefix), "%s Key (%c)", db->name, db->ticker);
29 printf("%21s: ", prefix);
32 print_value(dbentry->value);
34 printf("not found\n");
38 int system_info_tool_get_raw(const char *searchkey, system_info_type_e searchtype,
39 enum db_e searchdb, struct dbentry *dbentry)
41 char dbpath[BUFFER_MAX - 1];
42 char dbentry_format[BUFFER_MAX];
48 if (searchdb < DB_START || searchdb >= DB_END)
51 dbentry->db = &db[searchdb];
53 /* scanf format: "%127[^:]:%127[^:]:%127s %127s" */
54 snprintf(dbentry_format, sizeof(dbentry_format), "%%%d[^:]:%%%d[^:]:%%%ds %%%ds",
55 BUFFER_MAX - 1, BUFFER_MAX - 1, BUFFER_MAX - 1, BUFFER_MAX - 1);
57 for (int i = 0; i < NUM_HASH_FILE; ++i) {
58 __auto_fclose__ FILE *fp = NULL;
59 char line[BUFFER_MAX * 8] = {0, };
60 char tag[BUFFER_MAX - 1] = {0, };
61 char key[BUFFER_MAX - 1] = {0, };
62 char type[BUFFER_MAX - 1] = {0, };
63 char value[BUFFER_MAX - 1] = {0, };
64 const char *typestring;
66 snprintf(dbpath, sizeof(dbpath), "%s/%d", db[searchdb].path, i);
67 fp = fopen(dbpath, "r");
71 while ((fgets(line, sizeof(line), fp))) {
72 retval = sscanf(line, dbentry_format, tag, key, type, value);
76 if (strncmp(key, searchkey, strlen(key) + 1))
79 typestring = type_to_string(searchtype);
80 if (strncmp(type, typestring, strlen(type) + 1))
83 strncpy(dbentry->key, key, sizeof(dbentry->key) - 1);
84 strncpy(dbentry->tag, tag, sizeof(dbentry->tag) - 1);
85 dbentry->value.type = searchtype;
86 if (strncmp(type, "bool", sizeof("bool")) == 0)
87 dbentry->value.b = (value[runtime_env] == 'T');
88 else if (strncmp(type, "int", sizeof("int")) == 0)
89 dbentry->value.i = atoi(value);
90 else if (strncmp(type, "double", sizeof("double")) == 0)
91 dbentry->value.d = atof(value);
92 else if (strncmp(type, "string", sizeof("string")) == 0)
93 dbentry->value.s = strndup(value, sizeof(value));
102 static int system_info_tool_get_api(const char *key, struct value *value)
113 } cache[SYSTEM_INFO_TYPE_MAX] = {
114 { -1, { SYSTEM_INFO_BOOL, } },
115 { -1, { SYSTEM_INFO_INT, } },
116 { -1, { SYSTEM_INFO_DOUBLE, } },
117 { -1, { SYSTEM_INFO_STRING, } },
120 retval = system_info_get_platform_bool(key, &cache[SYSTEM_INFO_BOOL].value.b);
122 cache[SYSTEM_INFO_BOOL].match = ++match;
124 retval = system_info_get_platform_int(key, &cache[SYSTEM_INFO_INT].value.i);
126 cache[SYSTEM_INFO_INT].match = ++match;
128 retval = system_info_get_platform_double(key, &cache[SYSTEM_INFO_DOUBLE].value.d);
130 cache[SYSTEM_INFO_DOUBLE].match = ++match;
132 retval = system_info_get_platform_string(key, &cache[SYSTEM_INFO_STRING].value.s);
134 cache[SYSTEM_INFO_STRING].match = ++match;
137 for (i = 0; i < SYSTEM_INFO_TYPE_MAX; ++i)
138 if (cache[i].match == match)
139 memcpy(value, &cache[i].value, sizeof(struct value));
141 } else if (match > 1) {
142 printf("There is multiple same keys with different type. Which type to search?\n");
143 for (i = 0; i < SYSTEM_INFO_TYPE_MAX; ++i)
144 if (cache[i].match > 0) {
145 printf(" [%d] ", cache[i].match);
146 print_value(cache[i].value);
150 retval = scanf("%d", &select);
152 printf("Invalid selection\n");
157 for (i = 0; i < SYSTEM_INFO_TYPE_MAX; ++i) {
158 if (cache[i].match == select) {
159 memcpy(value, &cache[i].value, sizeof(struct value));
164 if (i == SYSTEM_INFO_TYPE_MAX) {
165 printf("Invalid selection\n");
171 printf("key=%s is not found\n", key);
175 free(cache[SYSTEM_INFO_STRING].value.s);
180 void system_info_tool_get(const char *rawkey, bool verbose)
183 char key[BUFFER_MAX];
186 retval = convert_raw_key(rawkey, key, sizeof(key));
190 retval = system_info_tool_get_api(key, &api);
194 /* show each database if -v option is given */
200 /* set db to be searched */
201 __auto_free_dbentry__ struct dbentry plt = { 0, }; /* scan platform ro db */
202 __auto_free_dbentry__ struct dbentry hal = { 0, }; /* scan platform hal ro db */
203 __auto_free_dbentry__ struct dbentry usr = { 0, }; /* scan platform rw db */
205 retplt = system_info_tool_get_raw(key, api.type, DB_DEFAULT_RO, &plt);
206 print_dbentry(&plt, retplt == 0);
208 rethal = system_info_tool_get_raw(key, api.type, DB_HAL_RO, &hal);
209 print_dbentry(&hal, rethal == 0);
211 retusr = system_info_tool_get_raw(key, api.type, DB_DEFAULT_RW, &usr);
212 print_dbentry(&usr, retusr == 0);
214 /* print description how API have derived the result */
216 printf("Result (=%c overrides %c,%c): ", usr.db->ticker, plt.db->ticker, hal.db->ticker);
217 } else if (retplt == 0 && rethal == 0) {
218 if (plt.value.type == SYSTEM_INFO_BOOL && hal.value.type == SYSTEM_INFO_BOOL)
219 printf("Result (=%c && %c): ", plt.db->ticker, hal.db->ticker);
221 printf("Result (=%c overrides %c): ", hal.db->ticker, plt.db->ticker);
222 } else if (retplt == 0) {
223 printf("Result (=%c): ", plt.db->ticker);
224 } else if (rethal == 0) {
225 printf("Result (=%c): ", hal.db->ticker);
227 printf("Result (error occured): ");
236 void system_info_tool_list_all(enum db_e dbtype)
238 char dbpath[BUFFER_MAX - 1];
239 char dbentry_format[BUFFER_MAX];
242 if (access(db[dbtype].path, F_OK) != 0)
245 /* scanf format: "%127[^:]:%127[^:]:%127s %127s" */
246 snprintf(dbentry_format, sizeof(dbentry_format), "%%%d[^:]:%%%d[^:]:%%%ds %%%ds",
247 BUFFER_MAX - 1, BUFFER_MAX - 1, BUFFER_MAX - 1, BUFFER_MAX - 1);
249 for (int i = 0; i < NUM_HASH_FILE; ++i) {
250 __auto_fclose__ FILE *fp = NULL;
251 char line[BUFFER_MAX * 8] = {0, };
252 char tag[BUFFER_MAX - 1] = {0, };
253 char key[BUFFER_MAX - 1] = {0, };
254 char type[BUFFER_MAX - 1] = {0, };
255 char value[BUFFER_MAX - 1] = {0, };
257 snprintf(dbpath, sizeof(dbpath), "%s/%d", db[dbtype].path, i);
258 fp = fopen(dbpath, "r");
262 while ((fgets(line, sizeof(line), fp))) {
263 retval = sscanf(line, dbentry_format, tag, key, type, value);
267 printf("%s: ", db[dbtype].name);
268 printf("key=%s, ", key);
269 printf("type=%s, ", type);
270 if (strncmp(type, "bool", sizeof("bool")) == 0)
271 printf("value=%d, ", (value[runtime_env] == 'T'));
273 printf("value=%s, ", value);
274 printf("tag=%s\n", tag);