044760e7f7dd7530b00814ef4f9925d1ee01cc59
[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
9 #include "uqmi.h"
10 #include "commands.h"
11
12 static struct blob_buf status;
13
14 static void no_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
15 {
16 }
17
18 static void cmd_version_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
19 {
20 struct qmi_ctl_get_version_info_response res;
21 char name_buf[16];
22 int i;
23
24 qmi_parse_ctl_get_version_info_response(msg, &res);
25 for (i = 0; i < res.data.service_list_n; i++) {
26 sprintf(name_buf, "service_%d", res.data.service_list[i].service);
27 blobmsg_printf(&status, name_buf, "%d,%d",
28 res.data.service_list[i].major_version,
29 res.data.service_list[i].minor_version);
30 }
31 }
32
33 static enum qmi_cmd_result
34 cmd_version_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
35 {
36 qmi_set_ctl_get_version_info_request(msg);
37 return QMI_CMD_REQUEST;
38 }
39
40 #define cmd_get_client_id_cb no_cb
41 static enum qmi_cmd_result
42 cmd_get_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
43 {
44 QmiService svc = qmi_service_get_by_name(arg);
45
46 if (svc < 0) {
47 fprintf(stderr, "Invalid service name '%s'\n", arg);
48 return QMI_CMD_EXIT;
49 }
50
51 if (qmi_service_connect(qmi, svc, -1)) {
52 fprintf(stderr, "Failed to connect to service\n");
53 return QMI_CMD_EXIT;
54 }
55
56 printf("%d\n", qmi_service_get_client_id(qmi, svc));
57 return QMI_CMD_DONE;
58 }
59
60 #define cmd_set_client_id_cb no_cb
61 static enum qmi_cmd_result
62 cmd_set_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
63 {
64 QmiService svc;
65 int id;
66 char *s;
67
68 s = strchr(arg, ',');
69 if (!s) {
70 fprintf(stderr, "Invalid argument\n");
71 return QMI_CMD_EXIT;
72 }
73 *s = 0;
74 s++;
75
76 id = strtoul(s, &s, 0);
77 if (s && *s) {
78 fprintf(stderr, "Invalid argument\n");
79 return QMI_CMD_EXIT;
80 }
81
82 svc = qmi_service_get_by_name(arg);
83 if (svc < 0) {
84 fprintf(stderr, "Invalid service name '%s'\n", arg);
85 return QMI_CMD_EXIT;
86 }
87
88 if (qmi_service_connect(qmi, svc, id)) {
89 fprintf(stderr, "Failed to connect to service\n");
90 return QMI_CMD_EXIT;
91 }
92
93 return QMI_CMD_DONE;
94 }
95
96 #include "commands-wds.c"
97 #include "commands-dms.c"
98 #include "commands-nas.c"
99
100 #define __uqmi_command(_name, _optname, _arg, _type) \
101 [__UQMI_COMMAND_##_name] = { \
102 .name = #_optname, \
103 .type = _type, \
104 .prepare = cmd_##_name##_prepare, \
105 .cb = cmd_##_name##_cb, \
106 }
107
108 const struct uqmi_cmd_handler uqmi_cmd_handler[__UQMI_COMMAND_LAST] = {
109 __uqmi_commands
110 };
111 #undef __uqmi_command
112
113 static struct uqmi_cmd *cmds;
114 static int n_cmds;
115
116 void uqmi_add_command(char *arg, int cmd)
117 {
118 int idx = n_cmds++;
119
120 cmds = realloc(cmds, n_cmds * sizeof(*cmds));
121 cmds[idx].handler = &uqmi_cmd_handler[cmd];
122 cmds[idx].arg = optarg;
123 }
124
125 static void uqmi_print_result(struct blob_attr *data)
126 {
127 struct blob_attr *cur;
128 int rem;
129
130 blob_for_each_attr(cur, data, rem) {
131 switch (blobmsg_type(cur)) {
132 case BLOBMSG_TYPE_STRING:
133 printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
134 break;
135 case BLOBMSG_TYPE_INT32:
136 printf("%s=%d\n", blobmsg_name(cur), (int32_t) blobmsg_get_u32(cur));
137 break;
138 case BLOBMSG_TYPE_INT8:
139 printf("%s=%s\n", blobmsg_name(cur), blobmsg_get_u8(cur) ? "true" : "false");
140 break;
141 }
142 }
143 }
144
145 static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
146 {
147 static char buf[2048];
148 static struct qmi_request req;
149 int i;
150
151 for (i = 0; i < n_cmds; i++) {
152 enum qmi_cmd_result res;
153 bool cmd_option = cmds[i].handler->type == CMD_TYPE_OPTION;
154 bool do_break = false;
155
156 if (cmd_option != option)
157 continue;
158
159 blob_buf_init(&status, 0);
160 if (cmds[i].handler->type > QMI_SERVICE_CTL &&
161 qmi_service_connect(qmi, cmds[i].handler->type, -1)) {
162 blobmsg_printf(&status, "error", "failed to connect to service");
163 res = QMI_CMD_EXIT;
164 } else {
165 res = cmds[i].handler->prepare(qmi, &req, (void *) buf, cmds[i].arg);
166 }
167
168 if (res == QMI_CMD_REQUEST) {
169 qmi_request_start(qmi, &req, (void *) buf, cmds[i].handler->cb);
170 req.no_error_cb = true;
171 if (qmi_request_wait(qmi, &req))
172 blobmsg_add_string(&status, "error", qmi_get_error_str(req.ret));
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_run_commands(struct qmi_dev *qmi)
185 {
186 if (__uqmi_run_commands(qmi, true))
187 __uqmi_run_commands(qmi, false);
188 free(cmds);
189 cmds = NULL;
190 n_cmds = 0;
191 }