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