add an option for printing output as a single line
[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 char name_buf[16];
24 int i;
25
26 qmi_parse_ctl_get_version_info_response(msg, &res);
27 for (i = 0; i < res.data.service_list_n; i++) {
28 sprintf(name_buf, "service_%d", res.data.service_list[i].service);
29 blobmsg_printf(&status, name_buf, "%d,%d",
30 res.data.service_list[i].major_version,
31 res.data.service_list[i].minor_version);
32 }
33 }
34
35 static enum qmi_cmd_result
36 cmd_version_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
37 {
38 qmi_set_ctl_get_version_info_request(msg);
39 return QMI_CMD_REQUEST;
40 }
41
42 #define cmd_get_client_id_cb no_cb
43 static enum qmi_cmd_result
44 cmd_get_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
45 {
46 QmiService svc = qmi_service_get_by_name(arg);
47
48 if (svc < 0) {
49 fprintf(stderr, "Invalid service name '%s'\n", arg);
50 return QMI_CMD_EXIT;
51 }
52
53 if (qmi_service_connect(qmi, svc, -1)) {
54 fprintf(stderr, "Failed to connect to service\n");
55 return QMI_CMD_EXIT;
56 }
57
58 printf("%d\n", qmi_service_get_client_id(qmi, svc));
59 return QMI_CMD_DONE;
60 }
61
62 #define cmd_set_client_id_cb no_cb
63 static enum qmi_cmd_result
64 cmd_set_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
65 {
66 QmiService svc;
67 int id;
68 char *s;
69
70 s = strchr(arg, ',');
71 if (!s) {
72 fprintf(stderr, "Invalid argument\n");
73 return QMI_CMD_EXIT;
74 }
75 *s = 0;
76 s++;
77
78 id = strtoul(s, &s, 0);
79 if (s && *s) {
80 fprintf(stderr, "Invalid argument\n");
81 return QMI_CMD_EXIT;
82 }
83
84 svc = qmi_service_get_by_name(arg);
85 if (svc < 0) {
86 fprintf(stderr, "Invalid service name '%s'\n", arg);
87 return QMI_CMD_EXIT;
88 }
89
90 if (qmi_service_connect(qmi, svc, id)) {
91 fprintf(stderr, "Failed to connect to service\n");
92 return QMI_CMD_EXIT;
93 }
94
95 return QMI_CMD_DONE;
96 }
97
98 #include "commands-wds.c"
99 #include "commands-dms.c"
100 #include "commands-nas.c"
101 #include "commands-wms.c"
102
103 #define __uqmi_command(_name, _optname, _arg, _type) \
104 [__UQMI_COMMAND_##_name] = { \
105 .name = #_optname, \
106 .type = _type, \
107 .prepare = cmd_##_name##_prepare, \
108 .cb = cmd_##_name##_cb, \
109 }
110
111 const struct uqmi_cmd_handler uqmi_cmd_handler[__UQMI_COMMAND_LAST] = {
112 __uqmi_commands
113 };
114 #undef __uqmi_command
115
116 static struct uqmi_cmd *cmds;
117 static int n_cmds;
118
119 void uqmi_add_command(char *arg, int cmd)
120 {
121 int idx = n_cmds++;
122
123 cmds = realloc(cmds, n_cmds * sizeof(*cmds));
124 cmds[idx].handler = &uqmi_cmd_handler[cmd];
125 cmds[idx].arg = optarg;
126 }
127
128 static void uqmi_print_result(struct blob_attr *data)
129 {
130 char *str;
131
132 if (!blob_len(data))
133 return;
134
135 str = blobmsg_format_json_indent(blob_data(data), false, single_line ? -1 : 0);
136 if (!str)
137 return;
138
139 printf("%s\n", str);
140 free(str);
141 }
142
143 static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
144 {
145 static char buf[2048];
146 static struct qmi_request req;
147 int i;
148
149 for (i = 0; i < n_cmds; i++) {
150 enum qmi_cmd_result res;
151 bool cmd_option = cmds[i].handler->type == CMD_TYPE_OPTION;
152 bool do_break = false;
153
154 if (cmd_option != option)
155 continue;
156
157 blob_buf_init(&status, 0);
158 if (cmds[i].handler->type > QMI_SERVICE_CTL &&
159 qmi_service_connect(qmi, cmds[i].handler->type, -1)) {
160 uqmi_add_error("Failed to connect to service");
161 res = QMI_CMD_EXIT;
162 } else {
163 res = cmds[i].handler->prepare(qmi, &req, (void *) buf, cmds[i].arg);
164 }
165
166 if (res == QMI_CMD_REQUEST) {
167 qmi_request_start(qmi, &req, (void *) buf, cmds[i].handler->cb);
168 req.no_error_cb = true;
169 if (qmi_request_wait(qmi, &req)) {
170 uqmi_add_error(qmi_get_error_str(req.ret));
171 do_break = true;
172 }
173 } else if (res == QMI_CMD_EXIT) {
174 do_break = true;
175 }
176
177 uqmi_print_result(status.head);
178 if (do_break)
179 return false;
180 }
181 return true;
182 }
183
184 void uqmi_add_error(const char *msg)
185 {
186 blobmsg_add_string(&status, NULL, msg);
187 }
188
189 bool uqmi_run_commands(struct qmi_dev *qmi)
190 {
191 bool ret;
192
193 ret = __uqmi_run_commands(qmi, true) &&
194 __uqmi_run_commands(qmi, false);
195
196 free(cmds);
197 cmds = NULL;
198 n_cmds = 0;
199
200 return ret;
201 }