add command for setting network preference
[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 #include "uqmi.h"
7 #include "commands.h"
8
9 static void no_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
10 {
11 }
12
13 static void cmd_version_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
14 {
15 struct qmi_ctl_get_version_info_response res;
16 int i;
17
18 if (!msg) {
19 printf("Request version failed: %d\n", req->ret);
20 return;
21 }
22
23 qmi_parse_ctl_get_version_info_response(msg, &res);
24
25 printf("Found %d: services:\n", res.data.service_list_n);
26 for (i = 0; i < res.data.service_list_n; i++) {
27 printf("Service %d, version: %d.%d\n",
28 res.data.service_list[i].service,
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
101 #define __uqmi_command(_name, _optname, _arg, _type) \
102 [__UQMI_COMMAND_##_name] = { \
103 .name = #_optname, \
104 .type = _type, \
105 .prepare = cmd_##_name##_prepare, \
106 .cb = cmd_##_name##_cb, \
107 }
108
109 const struct uqmi_cmd_handler uqmi_cmd_handler[__UQMI_COMMAND_LAST] = {
110 __uqmi_commands
111 };
112 #undef __uqmi_command
113
114 static struct uqmi_cmd *cmds;
115 static int n_cmds;
116
117 void uqmi_add_command(char *arg, int cmd)
118 {
119 int idx = n_cmds++;
120
121 cmds = realloc(cmds, n_cmds * sizeof(*cmds));
122 cmds[idx].handler = &uqmi_cmd_handler[cmd];
123 cmds[idx].arg = optarg;
124 }
125
126 static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
127 {
128 static char buf[2048];
129 static struct qmi_request req;
130 int i;
131
132 for (i = 0; i < n_cmds; i++) {
133 enum qmi_cmd_result res;
134 bool cmd_option = cmds[i].handler->type == CMD_TYPE_OPTION;
135
136 if (cmd_option != option)
137 continue;
138
139 if (cmds[i].handler->type > QMI_SERVICE_CTL &&
140 qmi_service_connect(qmi, cmds[i].handler->type, -1)) {
141 fprintf(stderr, "Error in command '%s': failed to connect to service\n",
142 cmds[i].handler->name);
143 return false;
144 }
145 res = cmds[i].handler->prepare(qmi, &req, (void *) buf, cmds[i].arg);
146 switch(res) {
147 case QMI_CMD_REQUEST:
148 qmi_request_start(qmi, &req, (void *) buf, cmds[i].handler->cb);
149 qmi_request_wait(qmi, &req);
150 break;
151 case QMI_CMD_EXIT:
152 return false;
153 case QMI_CMD_DONE:
154 default:
155 continue;
156 }
157 }
158 return true;
159 }
160
161 void uqmi_run_commands(struct qmi_dev *qmi)
162 {
163 if (__uqmi_run_commands(qmi, true))
164 __uqmi_run_commands(qmi, false);
165 free(cmds);
166 cmds = NULL;
167 n_cmds = 0;
168 }