2 * uqmi -- tiny QMI support implementation
4 * Copyright (C) 2014-2015 Felix Fietkau <nbd@openwrt.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA.
28 #include <libubox/blobmsg.h>
29 #include <libubox/blobmsg_json.h>
34 static struct blob_buf status;
35 bool single_line = false;
37 static void no_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
41 static void cmd_version_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
43 struct qmi_ctl_get_version_info_response res;
48 qmi_parse_ctl_get_version_info_response(msg, &res);
50 c = blobmsg_open_table(&status, NULL);
51 for (i = 0; i < res.data.service_list_n; i++) {
52 sprintf(name_buf, "service_%d", res.data.service_list[i].service);
53 blobmsg_printf(&status, name_buf, "%d,%d",
54 res.data.service_list[i].major_version,
55 res.data.service_list[i].minor_version);
57 blobmsg_close_table(&status, c);
60 static enum qmi_cmd_result
61 cmd_version_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
63 qmi_set_ctl_get_version_info_request(msg);
64 return QMI_CMD_REQUEST;
67 #define cmd_get_client_id_cb no_cb
68 static enum qmi_cmd_result
69 cmd_get_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
71 QmiService svc = qmi_service_get_by_name(arg);
74 fprintf(stderr, "Invalid service name '%s'\n", arg);
78 if (qmi_service_connect(qmi, svc, -1)) {
79 fprintf(stderr, "Failed to connect to service\n");
83 printf("%d\n", qmi_service_get_client_id(qmi, svc));
87 #define cmd_set_client_id_cb no_cb
88 static enum qmi_cmd_result
89 cmd_set_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
97 fprintf(stderr, "Invalid argument\n");
103 id = strtoul(s, &s, 0);
105 fprintf(stderr, "Invalid argument\n");
109 svc = qmi_service_get_by_name(arg);
111 fprintf(stderr, "Invalid service name '%s'\n", arg);
115 if (qmi_service_connect(qmi, svc, id)) {
116 fprintf(stderr, "Failed to connect to service\n");
124 qmi_get_array_idx(const char **array, int size, const char *str)
128 for (i = 0; i < size; i++) {
132 if (!strcmp(array[i], str))
139 #define cmd_ctl_set_data_format_cb no_cb
140 static enum qmi_cmd_result
141 cmd_ctl_set_data_format_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
143 struct qmi_ctl_set_data_format_request sreq = {};
144 const char *modes[] = {
145 [QMI_CTL_DATA_LINK_PROTOCOL_802_3] = "802.3",
146 [QMI_CTL_DATA_LINK_PROTOCOL_RAW_IP] = "raw-ip",
148 int mode = qmi_get_array_idx(modes, ARRAY_SIZE(modes), arg);
151 uqmi_add_error("Invalid mode (modes: 802.3, raw-ip)");
155 qmi_set_ctl_set_data_format_request(msg, &sreq);
159 #include "commands-wds.c"
160 #include "commands-dms.c"
161 #include "commands-nas.c"
162 #include "commands-wms.c"
163 #include "commands-wda.c"
165 #define __uqmi_command(_name, _optname, _arg, _type) \
166 [__UQMI_COMMAND_##_name] = { \
169 .prepare = cmd_##_name##_prepare, \
170 .cb = cmd_##_name##_cb, \
173 const struct uqmi_cmd_handler uqmi_cmd_handler[__UQMI_COMMAND_LAST] = {
176 #undef __uqmi_command
178 static struct uqmi_cmd *cmds;
181 void uqmi_add_command(char *arg, int cmd)
185 cmds = realloc(cmds, n_cmds * sizeof(*cmds));
186 cmds[idx].handler = &uqmi_cmd_handler[cmd];
187 cmds[idx].arg = optarg;
190 static void uqmi_print_result(struct blob_attr *data)
197 str = blobmsg_format_json_indent(blob_data(data), false, single_line ? -1 : 0);
205 static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
207 static char buf[2048];
208 static struct qmi_request req;
211 for (i = 0; i < n_cmds; i++) {
212 enum qmi_cmd_result res;
213 bool cmd_option = cmds[i].handler->type == CMD_TYPE_OPTION;
214 bool do_break = false;
216 if (cmd_option != option)
219 blob_buf_init(&status, 0);
220 if (cmds[i].handler->type > QMI_SERVICE_CTL &&
221 qmi_service_connect(qmi, cmds[i].handler->type, -1)) {
222 uqmi_add_error("Failed to connect to service");
225 res = cmds[i].handler->prepare(qmi, &req, (void *) buf, cmds[i].arg);
228 if (res == QMI_CMD_REQUEST) {
229 qmi_request_start(qmi, &req, (void *) buf, cmds[i].handler->cb);
230 req.no_error_cb = true;
231 if (qmi_request_wait(qmi, &req)) {
232 uqmi_add_error(qmi_get_error_str(req.ret));
235 } else if (res == QMI_CMD_EXIT) {
239 uqmi_print_result(status.head);
246 int uqmi_add_error(const char *msg)
248 blobmsg_add_string(&status, NULL, msg);
252 bool uqmi_run_commands(struct qmi_dev *qmi)
256 ret = __uqmi_run_commands(qmi, true) &&
257 __uqmi_run_commands(qmi, false);