system-info-generated: Fix inclusion guard to __TIZEN_SYSTEM_SYSTEM_INFO_GENERATED_H__
[platform/core/api/system-info.git] / tool / system-info-tool.c
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include <getopt.h>
4
5 #include <glib.h>
6 #include "system-info-tool.h"
7 #include "system-info-tool-get.h"
8 #include "system-info-tool-set.h"
9
10 #define KEY_PREFIX    "http://"
11
12 int runtime_env = C;
13 const struct db db[DB_END] = {
14 /*                        { dbpath,                     dbname,         ticker } */
15         [DB_DEFAULT_RO] = { SYSTEM_INFO_DB_RO_PATH,     "Platform",     'A'    },
16         [DB_HAL_RO]     = { SYSTEM_INFO_HAL_DB_RO_PATH, "HAL",          'B'    },
17         [DB_DEFAULT_RW] = { SYSTEM_INFO_DB_RW_PATH,     "User-defined", 'C'    },
18 };
19
20 static const char *__type_to_string[SYSTEM_INFO_TYPE_MAX] = {
21         [SYSTEM_INFO_BOOL]   = "bool",
22         [SYSTEM_INFO_INT]    = "int",
23         [SYSTEM_INFO_DOUBLE] = "double",
24         [SYSTEM_INFO_STRING] = "string",
25 };
26
27 const char *type_to_string(system_info_type_e type)
28 {
29         if (type < SYSTEM_INFO_TYPE_MIN || type >= SYSTEM_INFO_TYPE_MAX)
30                 return NULL;
31
32         return __type_to_string[type];
33 }
34
35 system_info_type_e string_to_type(const char *type)
36 {
37         if (strncasecmp(type, "bool", sizeof("bool")) == 0)
38                 return SYSTEM_INFO_BOOL;
39         else if (strncasecmp(type, "int", sizeof("int")) == 0)
40                 return SYSTEM_INFO_INT;
41         else if (strncasecmp(type, "double", sizeof("double")) == 0)
42                 return SYSTEM_INFO_DOUBLE;
43         else if (strncasecmp(type, "string", sizeof("string")) == 0)
44                 return SYSTEM_INFO_STRING;
45         else
46                 return SYSTEM_INFO_TYPE_MAX;
47 }
48
49 int convert_raw_key(const char *rawkey, char *buffer, int len)
50 {
51         int retval;
52
53         if (!rawkey || !buffer)
54                 return -EINVAL;
55
56         if (strstr(rawkey, KEY_PREFIX) == rawkey)
57                 retval = snprintf(buffer, len, "%s", rawkey + strlen(KEY_PREFIX));
58         else
59                 retval = snprintf(buffer, len, "%s", rawkey);
60
61         if (retval >= len)
62                 return -ENAMETOOLONG;
63
64         return 0;
65 }
66
67 void print_value(struct value value)
68 {
69         if (value.type == SYSTEM_INFO_BOOL)
70                 printf("type=bool, value=%d\n", value.b);
71         else if (value.type == SYSTEM_INFO_INT)
72                 printf("type=int, value=%d\n", value.i);
73         else if (value.type == SYSTEM_INFO_DOUBLE)
74                 printf("type=double, value=%f\n", value.d);
75         else if (value.type == SYSTEM_INFO_STRING)
76                 printf("type=string, value=%s\n", value.s);
77         else
78                 printf("type=inavlid, error\n");
79 }
80
81 static void system_info_tool_show_help(void)
82 {
83         printf("USAGE\n");
84         printf("  system-info-tool [OPTION]\n\n");
85         printf("OPTION\n");
86         system_info_tool_get_help();
87         system_info_tool_set_help();
88         printf("  -h|--help                      Show this help\n");
89 }
90
91 int main(int argc, char *argv[])
92 {
93         int opt;
94         int retval;
95         char *runtime_str;
96         char *key;
97         bool verbose = false;
98         GList *keys = NULL;
99         GList *elem;
100
101         if (argc < 2) {
102                 system_info_tool_show_help();
103                 return 1;
104         }
105
106         runtime_str = getenv("RUNTIME_TYPE");
107         if (runtime_str) {
108                 for (int i = 0; runtime[i].lang != LANG_MAX; ++i) {
109                         if (strncmp(runtime[i].runtime_type, runtime_str, strlen(runtime_str) + 1) == 0) {
110                                 runtime_env = runtime[i].lang;
111                                 break;
112                         }
113                 }
114         }
115
116         const struct option long_option[] = {
117                 { "get",       required_argument, NULL, 'g'  },
118                 { "verbose",   no_argument,       NULL, 'v'  },
119                 { "list-all",  no_argument,       NULL, 'l'  },
120                 { "set" ,      no_argument,       NULL, 's'  }, /* requires 3 arguments */
121                 { "clear",     required_argument, NULL, 'c'  },
122                 { "clear-all", no_argument,       NULL, 'C'  },
123                 { "help",      no_argument,       NULL, 'h'  },
124                 { 0,           0,                 0,     0   },
125         };
126
127         for (;;) {
128                 opt = getopt_long(argc, argv, "g:vlsc:Ch", long_option, NULL);
129                 if (opt < 0)
130                         break;
131                 switch (opt) {
132                 case 'g':
133                         SYSINFO_G_LIST_APPEND(keys, strdup(optarg));
134                         break;
135                 case 'v':
136                         verbose = true;
137                         break;
138                 case 'l':
139                         for (int i = DB_START; i < DB_END; ++i)
140                                 system_info_tool_list_all(i);
141                         break;
142                 case 's':
143                         retval = system_info_tool_set(argc, argv);
144                         if (retval < 0) {
145                                 printf("Failed to set key\n");
146                                 system_info_tool_set_help();
147                                 return 1;
148                         }
149                         optind += 3; /* manually consume 3 arguments */
150                         break;
151                 case 'c':
152                         system_info_tool_clear_key(optarg);
153                         break;
154                 case 'C':
155                         system_info_tool_clear_all();
156                         break;
157                 case 'h':
158                         system_info_tool_show_help();
159                         break;
160                 default:
161                         system_info_tool_show_help();
162                         break;
163                 }
164         }
165
166         SYSINFO_G_LIST_FOREACH(keys, elem, key) {
167                 system_info_tool_get(key, verbose);
168                 free(key);
169         }
170
171         return 0;
172 }