Fix logical expression which is always true
[project/uqmi.git] / commands.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <strings.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include <libubox/blobmsg.h>
8 #include <libubox/blobmsg_json.h>
9
10 #include "uqmi.h"
11 #include "commands.h"
12
13 static struct blob_buf status;
14 bool single_line = false;
15
16 static void no_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
17 {
18 }
19
20 static void cmd_version_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
21 {
22         struct qmi_ctl_get_version_info_response res;
23         void *c;
24         char name_buf[16];
25         int i;
26
27         qmi_parse_ctl_get_version_info_response(msg, &res);
28
29         c = blobmsg_open_table(&status, NULL);
30         for (i = 0; i < res.data.service_list_n; i++) {
31                 sprintf(name_buf, "service_%d", res.data.service_list[i].service);
32                 blobmsg_printf(&status, name_buf, "%d,%d",
33                         res.data.service_list[i].major_version,
34                         res.data.service_list[i].minor_version);
35         }
36         blobmsg_close_table(&status, c);
37 }
38
39 static enum qmi_cmd_result
40 cmd_version_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
41 {
42         qmi_set_ctl_get_version_info_request(msg);
43         return QMI_CMD_REQUEST;
44 }
45
46 #define cmd_get_client_id_cb no_cb
47 static enum qmi_cmd_result
48 cmd_get_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
49 {
50         QmiService svc = qmi_service_get_by_name(arg);
51
52         if (svc < 0) {
53                 fprintf(stderr, "Invalid service name '%s'\n", arg);
54                 return QMI_CMD_EXIT;
55         }
56
57         if (qmi_service_connect(qmi, svc, -1)) {
58                 fprintf(stderr, "Failed to connect to service\n");
59                 return QMI_CMD_EXIT;
60         }
61
62         printf("%d\n", qmi_service_get_client_id(qmi, svc));
63         return QMI_CMD_DONE;
64 }
65
66 #define cmd_set_client_id_cb no_cb
67 static enum qmi_cmd_result
68 cmd_set_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
69 {
70         QmiService svc;
71         int id;
72         char *s;
73
74         s = strchr(arg, ',');
75         if (!s) {
76                 fprintf(stderr, "Invalid argument\n");
77                 return QMI_CMD_EXIT;
78         }
79         *s = 0;
80         s++;
81
82         id = strtoul(s, &s, 0);
83         if (s && *s) {
84                 fprintf(stderr, "Invalid argument\n");
85                 return QMI_CMD_EXIT;
86         }
87
88         svc = qmi_service_get_by_name(arg);
89         if (svc < 0) {
90                 fprintf(stderr, "Invalid service name '%s'\n", arg);
91                 return QMI_CMD_EXIT;
92         }
93
94         if (qmi_service_connect(qmi, svc, id)) {
95                 fprintf(stderr, "Failed to connect to service\n");
96                 return QMI_CMD_EXIT;
97         }
98
99         return QMI_CMD_DONE;
100 }
101
102 static int
103 qmi_get_array_idx(const char **array, int size, const char *str)
104 {
105         int i;
106
107         for (i = 0; i < size; i++) {
108                 if (!array[i])
109                         continue;
110
111                 if (!strcmp(array[i], str))
112                         return i;
113         }
114
115         return -1;
116 }
117
118 #define cmd_ctl_set_data_format_cb no_cb
119 static enum qmi_cmd_result
120 cmd_ctl_set_data_format_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
121 {
122         struct qmi_ctl_set_data_format_request sreq = {};
123         const char *modes[] = {
124                 [QMI_CTL_DATA_LINK_PROTOCOL_802_3] = "802.3",
125                 [QMI_CTL_DATA_LINK_PROTOCOL_RAW_IP] = "raw-ip",
126         };
127         int mode = qmi_get_array_idx(modes, ARRAY_SIZE(modes), arg);
128
129         if (mode < 0) {
130                 uqmi_add_error("Invalid mode (modes: 802.3, raw-ip)");
131                 return QMI_CMD_EXIT;
132         }
133
134         qmi_set_ctl_set_data_format_request(msg, &sreq);
135         return QMI_CMD_DONE;
136 }
137
138 #include "commands-wds.c"
139 #include "commands-dms.c"
140 #include "commands-nas.c"
141 #include "commands-wms.c"
142 #include "commands-wda.c"
143
144 #define __uqmi_command(_name, _optname, _arg, _type) \
145         [__UQMI_COMMAND_##_name] = { \
146                 .name = #_optname, \
147                 .type = _type, \
148                 .prepare = cmd_##_name##_prepare, \
149                 .cb = cmd_##_name##_cb, \
150         }
151
152 const struct uqmi_cmd_handler uqmi_cmd_handler[__UQMI_COMMAND_LAST] = {
153         __uqmi_commands
154 };
155 #undef __uqmi_command
156
157 static struct uqmi_cmd *cmds;
158 static int n_cmds;
159
160 void uqmi_add_command(char *arg, int cmd)
161 {
162         int idx = n_cmds++;
163
164         cmds = realloc(cmds, n_cmds * sizeof(*cmds));
165         cmds[idx].handler = &uqmi_cmd_handler[cmd];
166         cmds[idx].arg = optarg;
167 }
168
169 static void uqmi_print_result(struct blob_attr *data)
170 {
171         char *str;
172
173         if (!blob_len(data))
174                 return;
175
176         str = blobmsg_format_json_indent(blob_data(data), false, single_line ? -1 : 0);
177         if (!str)
178                 return;
179
180         printf("%s\n", str);
181         free(str);
182 }
183
184 static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
185 {
186         static char buf[2048];
187         static struct qmi_request req;
188         int i;
189
190         for (i = 0; i < n_cmds; i++) {
191                 enum qmi_cmd_result res;
192                 bool cmd_option = cmds[i].handler->type == CMD_TYPE_OPTION;
193                 bool do_break = false;
194
195                 if (cmd_option != option)
196                         continue;
197
198                 blob_buf_init(&status, 0);
199                 if (cmds[i].handler->type > QMI_SERVICE_CTL &&
200                     qmi_service_connect(qmi, cmds[i].handler->type, -1)) {
201                         uqmi_add_error("Failed to connect to service");
202                         res = QMI_CMD_EXIT;
203                 } else {
204                         res = cmds[i].handler->prepare(qmi, &req, (void *) buf, cmds[i].arg);
205                 }
206
207                 if (res == QMI_CMD_REQUEST) {
208                         qmi_request_start(qmi, &req, (void *) buf, cmds[i].handler->cb);
209                         req.no_error_cb = true;
210                         if (qmi_request_wait(qmi, &req)) {
211                                 uqmi_add_error(qmi_get_error_str(req.ret));
212                                 do_break = true;
213                         }
214                 } else if (res == QMI_CMD_EXIT) {
215                         do_break = true;
216                 }
217
218                 uqmi_print_result(status.head);
219                 if (do_break)
220                         return false;
221         }
222         return true;
223 }
224
225 int uqmi_add_error(const char *msg)
226 {
227         blobmsg_add_string(&status, NULL, msg);
228         return QMI_CMD_EXIT;
229 }
230
231 bool uqmi_run_commands(struct qmi_dev *qmi)
232 {
233         bool ret;
234
235         ret = __uqmi_run_commands(qmi, true) &&
236               __uqmi_run_commands(qmi, false);
237
238         free(cmds);
239         cmds = NULL;
240         n_cmds = 0;
241
242         return ret;
243 }