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
;
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
);
35 static enum qmi_cmd_result
36 cmd_version_prepare(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
, char *arg
)
38 qmi_set_ctl_get_version_info_request(msg
);
39 return QMI_CMD_REQUEST
;
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
)
46 QmiService svc
= qmi_service_get_by_name(arg
);
49 fprintf(stderr
, "Invalid service name '%s'\n", arg
);
53 if (qmi_service_connect(qmi
, svc
, -1)) {
54 fprintf(stderr
, "Failed to connect to service\n");
58 printf("%d\n", qmi_service_get_client_id(qmi
, svc
));
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
)
72 fprintf(stderr
, "Invalid argument\n");
78 id
= strtoul(s
, &s
, 0);
80 fprintf(stderr
, "Invalid argument\n");
84 svc
= qmi_service_get_by_name(arg
);
86 fprintf(stderr
, "Invalid service name '%s'\n", arg
);
90 if (qmi_service_connect(qmi
, svc
, id
)) {
91 fprintf(stderr
, "Failed to connect to service\n");
98 #include "commands-wds.c"
99 #include "commands-dms.c"
100 #include "commands-nas.c"
101 #include "commands-wms.c"
103 #define __uqmi_command(_name, _optname, _arg, _type) \
104 [__UQMI_COMMAND_##_name] = { \
107 .prepare = cmd_##_name##_prepare, \
108 .cb = cmd_##_name##_cb, \
111 const struct uqmi_cmd_handler uqmi_cmd_handler
[__UQMI_COMMAND_LAST
] = {
114 #undef __uqmi_command
116 static struct uqmi_cmd
*cmds
;
119 void uqmi_add_command(char *arg
, int cmd
)
123 cmds
= realloc(cmds
, n_cmds
* sizeof(*cmds
));
124 cmds
[idx
].handler
= &uqmi_cmd_handler
[cmd
];
125 cmds
[idx
].arg
= optarg
;
128 static void uqmi_print_result(struct blob_attr
*data
)
135 str
= blobmsg_format_json_indent(blob_data(data
), false, single_line
? -1 : 0);
143 static bool __uqmi_run_commands(struct qmi_dev
*qmi
, bool option
)
145 static char buf
[2048];
146 static struct qmi_request req
;
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;
154 if (cmd_option
!= option
)
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");
163 res
= cmds
[i
].handler
->prepare(qmi
, &req
, (void *) buf
, cmds
[i
].arg
);
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
));
173 } else if (res
== QMI_CMD_EXIT
) {
177 uqmi_print_result(status
.head
);
184 int uqmi_add_error(const char *msg
)
186 blobmsg_add_string(&status
, NULL
, msg
);
190 bool uqmi_run_commands(struct qmi_dev
*qmi
)
194 ret
= __uqmi_run_commands(qmi
, true) &&
195 __uqmi_run_commands(qmi
, false);