7 #include <libubox/blobmsg.h>
8 #include <libubox/blobmsg_json.h>
13 static struct blob_buf status;
14 bool single_line = false;
16 static void no_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
20 static void cmd_version_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
22 struct qmi_ctl_get_version_info_response res;
27 qmi_parse_ctl_get_version_info_response(msg, &res);
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);
36 blobmsg_close_table(&status, c);
39 static enum qmi_cmd_result
40 cmd_version_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
42 qmi_set_ctl_get_version_info_request(msg);
43 return QMI_CMD_REQUEST;
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)
50 QmiService svc = qmi_service_get_by_name(arg);
53 fprintf(stderr, "Invalid service name '%s'\n", arg);
57 if (qmi_service_connect(qmi, svc, -1)) {
58 fprintf(stderr, "Failed to connect to service\n");
62 printf("%d\n", qmi_service_get_client_id(qmi, svc));
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)
76 fprintf(stderr, "Invalid argument\n");
82 id = strtoul(s, &s, 0);
84 fprintf(stderr, "Invalid argument\n");
88 svc = qmi_service_get_by_name(arg);
90 fprintf(stderr, "Invalid service name '%s'\n", arg);
94 if (qmi_service_connect(qmi, svc, id)) {
95 fprintf(stderr, "Failed to connect to service\n");
103 qmi_get_array_idx(const char **array, int size, const char *str)
107 for (i = 0; i < size; i++) {
111 if (!strcmp(array[i], str))
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)
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",
127 int mode = qmi_get_array_idx(modes, ARRAY_SIZE(modes), arg);
130 uqmi_add_error("Invalid mode (modes: 802.3, raw-ip)");
134 qmi_set_ctl_set_data_format_request(msg, &sreq);
138 #include "commands-wds.c"
139 #include "commands-dms.c"
140 #include "commands-nas.c"
141 #include "commands-wms.c"
143 #define __uqmi_command(_name, _optname, _arg, _type) \
144 [__UQMI_COMMAND_##_name] = { \
147 .prepare = cmd_##_name##_prepare, \
148 .cb = cmd_##_name##_cb, \
151 const struct uqmi_cmd_handler uqmi_cmd_handler[__UQMI_COMMAND_LAST] = {
154 #undef __uqmi_command
156 static struct uqmi_cmd *cmds;
159 void uqmi_add_command(char *arg, int cmd)
163 cmds = realloc(cmds, n_cmds * sizeof(*cmds));
164 cmds[idx].handler = &uqmi_cmd_handler[cmd];
165 cmds[idx].arg = optarg;
168 static void uqmi_print_result(struct blob_attr *data)
175 str = blobmsg_format_json_indent(blob_data(data), false, single_line ? -1 : 0);
183 static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
185 static char buf[2048];
186 static struct qmi_request req;
189 for (i = 0; i < n_cmds; i++) {
190 enum qmi_cmd_result res;
191 bool cmd_option = cmds[i].handler->type == CMD_TYPE_OPTION;
192 bool do_break = false;
194 if (cmd_option != option)
197 blob_buf_init(&status, 0);
198 if (cmds[i].handler->type > QMI_SERVICE_CTL &&
199 qmi_service_connect(qmi, cmds[i].handler->type, -1)) {
200 uqmi_add_error("Failed to connect to service");
203 res = cmds[i].handler->prepare(qmi, &req, (void *) buf, cmds[i].arg);
206 if (res == QMI_CMD_REQUEST) {
207 qmi_request_start(qmi, &req, (void *) buf, cmds[i].handler->cb);
208 req.no_error_cb = true;
209 if (qmi_request_wait(qmi, &req)) {
210 uqmi_add_error(qmi_get_error_str(req.ret));
213 } else if (res == QMI_CMD_EXIT) {
217 uqmi_print_result(status.head);
224 int uqmi_add_error(const char *msg)
226 blobmsg_add_string(&status, NULL, msg);
230 bool uqmi_run_commands(struct qmi_dev *qmi)
234 ret = __uqmi_run_commands(qmi, true) &&
235 __uqmi_run_commands(qmi, false);