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.
23 #include <arpa/inet.h>
25 #include "qmi-message.h"
27 static struct qmi_wds_start_network_request wds_sn_req = {
28 QMI_INIT(authentication_preference,
29 QMI_WDS_AUTHENTICATION_PAP | QMI_WDS_AUTHENTICATION_CHAP),
31 static struct qmi_wds_stop_network_request wds_stn_req;
33 #define cmd_wds_set_auth_cb no_cb
34 static enum qmi_cmd_result
35 cmd_wds_set_auth_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
39 QmiWdsAuthentication auth;
41 { "pap", QMI_WDS_AUTHENTICATION_PAP },
42 { "chap", QMI_WDS_AUTHENTICATION_CHAP },
43 { "both", QMI_WDS_AUTHENTICATION_PAP | QMI_WDS_AUTHENTICATION_CHAP },
44 { "none", QMI_WDS_AUTHENTICATION_NONE },
48 for (i = 0; i < ARRAY_SIZE(modes); i++) {
49 if (strcasecmp(modes[i].name, arg) != 0)
52 qmi_set(&wds_sn_req, authentication_preference, modes[i].auth);
56 uqmi_add_error("Invalid auth mode (valid: pap, chap, both, none)");
60 #define cmd_wds_set_username_cb no_cb
61 static enum qmi_cmd_result
62 cmd_wds_set_username_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
64 qmi_set_ptr(&wds_sn_req, username, arg);
68 #define cmd_wds_set_password_cb no_cb
69 static enum qmi_cmd_result
70 cmd_wds_set_password_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
72 qmi_set_ptr(&wds_sn_req, password, arg);
76 #define cmd_wds_set_autoconnect_cb no_cb
77 static enum qmi_cmd_result
78 cmd_wds_set_autoconnect_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
80 qmi_set(&wds_sn_req, enable_autoconnect, true);
81 qmi_set(&wds_stn_req, disable_autoconnect, true);
86 cmd_wds_start_network_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
88 struct qmi_wds_start_network_response res;
90 qmi_parse_wds_start_network_response(msg, &res);
91 if (res.set.packet_data_handle)
92 blobmsg_add_u32(&status, NULL, res.data.packet_data_handle);
95 static enum qmi_cmd_result
96 cmd_wds_start_network_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
98 qmi_set_ptr(&wds_sn_req, apn, arg);
99 qmi_set_wds_start_network_request(msg, &wds_sn_req);
100 return QMI_CMD_REQUEST;
103 #define cmd_wds_stop_network_cb no_cb
104 static enum qmi_cmd_result
105 cmd_wds_stop_network_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
107 uint32_t pdh = strtoul(arg, NULL, 0);
109 qmi_set(&wds_stn_req, packet_data_handle, pdh);
110 qmi_set_wds_stop_network_request(msg, &wds_stn_req);
111 return QMI_CMD_REQUEST;
115 cmd_wds_get_packet_service_status_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
117 struct qmi_wds_get_packet_service_status_response res;
118 const char *data_status[] = {
119 [QMI_WDS_CONNECTION_STATUS_UNKNOWN] = "unknown",
120 [QMI_WDS_CONNECTION_STATUS_DISCONNECTED] = "disconnected",
121 [QMI_WDS_CONNECTION_STATUS_CONNECTED] = "connected",
122 [QMI_WDS_CONNECTION_STATUS_SUSPENDED] = "suspended",
123 [QMI_WDS_CONNECTION_STATUS_AUTHENTICATING] = "authenticating",
127 qmi_parse_wds_get_packet_service_status_response(msg, &res);
128 if (res.set.connection_status &&
129 res.data.connection_status < ARRAY_SIZE(data_status))
130 s = res.data.connection_status;
132 blobmsg_add_string(&status, NULL, data_status[s]);
135 static enum qmi_cmd_result
136 cmd_wds_get_packet_service_status_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
138 qmi_set_wds_get_packet_service_status_request(msg);
139 return QMI_CMD_REQUEST;
142 #define cmd_wds_set_autoconnect_setting_cb no_cb
143 static enum qmi_cmd_result
144 cmd_wds_set_autoconnect_setting_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
146 struct qmi_wds_set_autoconnect_setting_request ac_req;
147 const char *modes[] = {
148 [QMI_WDS_AUTOCONNECT_DISABLED] = "disabled",
149 [QMI_WDS_AUTOCONNECT_ENABLED] = "enabled",
150 [QMI_WDS_AUTOCONNECT_PAUSED] = "paused",
154 for (i = 0; i < ARRAY_SIZE(modes); i++) {
155 if (strcasecmp(modes[i], arg) != 0)
158 qmi_set(&ac_req, setting, i);
159 qmi_set_wds_set_autoconnect_setting_request(msg, &ac_req);
163 uqmi_add_error("Invalid value (valid: disabled, enabled, paused)");
167 #define cmd_wds_reset_cb no_cb
168 static enum qmi_cmd_result
169 cmd_wds_reset_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
171 qmi_set_wds_reset_request(msg);
172 return QMI_CMD_REQUEST;
175 #define cmd_wds_set_ip_family_cb no_cb
176 static enum qmi_cmd_result
177 cmd_wds_set_ip_family_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
179 struct qmi_wds_set_ip_family_request ipf_req;
180 const struct ip_modes {
182 const QmiWdsIpFamily mode;
184 { "ipv4", QMI_WDS_IP_FAMILY_IPV4 },
185 { "ipv6", QMI_WDS_IP_FAMILY_IPV6 },
186 { "unspecified", QMI_WDS_IP_FAMILY_UNSPECIFIED },
190 for (i = 0; i < ARRAY_SIZE(modes); i++) {
191 if (strcasecmp(modes[i].name, arg) != 0)
194 qmi_set(&ipf_req, preference, modes[i].mode);
195 qmi_set_wds_set_ip_family_request(msg, &ipf_req);
196 return QMI_CMD_REQUEST;
199 uqmi_add_error("Invalid value (valid: ipv4, ipv6, unspecified)");
203 static void wds_to_ipv4(const char *name, const uint32_t addr)
205 struct in_addr ip_addr;
206 char buf[INET_ADDRSTRLEN];
208 ip_addr.s_addr = htonl(addr);
209 blobmsg_add_string(&status, name, inet_ntop(AF_INET, &ip_addr, buf, sizeof(buf)));
212 static void wds_to_ipv6(const char *name, const uint16_t *addr)
214 char buf[INET6_ADDRSTRLEN];
218 for (i = 0; i < ARRAY_SIZE(ip_addr); i++)
219 ip_addr[i] = htons(addr[i]);
221 blobmsg_add_string(&status, name, inet_ntop(AF_INET6, &ip_addr, buf, sizeof(buf)));
225 cmd_wds_get_current_settings_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
227 void *v4, *v6, *d, *t;
228 struct qmi_wds_get_current_settings_response res;
229 const char *pdptypes[] = {
230 [QMI_WDS_PDP_TYPE_IPV4] = "ipv4",
231 [QMI_WDS_PDP_TYPE_PPP] = "ppp",
232 [QMI_WDS_PDP_TYPE_IPV6] = "ipv6",
233 [QMI_WDS_PDP_TYPE_IPV4_OR_IPV6] = "ipv4-or-ipv6",
235 const struct ip_modes {
237 const QmiWdsIpFamily mode;
239 { "ipv4", QMI_WDS_IP_FAMILY_IPV4 },
240 { "ipv6", QMI_WDS_IP_FAMILY_IPV6 },
241 { "unspecified", QMI_WDS_IP_FAMILY_UNSPECIFIED },
245 qmi_parse_wds_get_current_settings_response(msg, &res);
247 t = blobmsg_open_table(&status, NULL);
249 if (res.set.pdp_type && (int) res.data.pdp_type < ARRAY_SIZE(pdptypes))
250 blobmsg_add_string(&status, "pdp-type", pdptypes[res.data.pdp_type]);
252 if (res.set.ip_family) {
253 for (i = 0; i < ARRAY_SIZE(modes); i++) {
254 if (modes[i].mode != res.data.ip_family)
256 blobmsg_add_string(&status, "ip-family", modes[i].name);
262 blobmsg_add_u32(&status, "mtu", res.data.mtu);
265 v4 = blobmsg_open_table(&status, "ipv4");
267 if (res.set.ipv4_address)
268 wds_to_ipv4("ip", res.data.ipv4_address);
269 if (res.set.primary_ipv4_dns_address)
270 wds_to_ipv4("dns1", res.data.primary_ipv4_dns_address);
271 if (res.set.secondary_ipv4_dns_address)
272 wds_to_ipv4("dns2", res.data.secondary_ipv4_dns_address);
273 if (res.set.ipv4_gateway_address)
274 wds_to_ipv4("gateway", res.data.ipv4_gateway_address);
275 if (res.set.ipv4_gateway_subnet_mask)
276 wds_to_ipv4("subnet", res.data.ipv4_gateway_subnet_mask);
277 blobmsg_close_table(&status, v4);
280 v6 = blobmsg_open_table(&status, "ipv6");
282 if (res.set.ipv6_address) {
283 wds_to_ipv6("ip", res.data.ipv6_address.address);
284 blobmsg_add_u32(&status, "ip-prefix-length", res.data.ipv6_address.prefix_length);
286 if (res.set.ipv6_gateway_address) {
287 wds_to_ipv6("gateway", res.data.ipv6_gateway_address.address);
288 blobmsg_add_u32(&status, "gw-prefix-length", res.data.ipv6_gateway_address.prefix_length);
290 if (res.set.ipv6_primary_dns_address)
291 wds_to_ipv6("dns1", res.data.ipv6_primary_dns_address);
292 if (res.set.ipv6_secondary_dns_address)
293 wds_to_ipv6("dns2", res.data.ipv6_secondary_dns_address);
295 blobmsg_close_table(&status, v6);
297 d = blobmsg_open_table(&status, "domain-names");
298 for (i = 0; i < res.data.domain_name_list_n; i++) {
299 blobmsg_add_string(&status, NULL, res.data.domain_name_list[i]);
301 blobmsg_close_table(&status, d);
303 blobmsg_close_table(&status, t);
306 static enum qmi_cmd_result
307 cmd_wds_get_current_settings_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
309 struct qmi_wds_get_current_settings_request gcs_req;
310 memset(&gcs_req, '\0', sizeof(struct qmi_wds_get_current_settings_request));
311 qmi_set(&gcs_req, requested_settings,
312 QMI_WDS_GET_CURRENT_SETTINGS_REQUESTED_SETTINGS_PDP_TYPE |
313 QMI_WDS_GET_CURRENT_SETTINGS_REQUESTED_SETTINGS_DNS_ADDRESS |
314 QMI_WDS_GET_CURRENT_SETTINGS_REQUESTED_SETTINGS_GRANTED_QOS |
315 QMI_WDS_GET_CURRENT_SETTINGS_REQUESTED_SETTINGS_IP_ADDRESS |
316 QMI_WDS_GET_CURRENT_SETTINGS_REQUESTED_SETTINGS_GATEWAY_INFO |
317 QMI_WDS_GET_CURRENT_SETTINGS_REQUESTED_SETTINGS_MTU |
318 QMI_WDS_GET_CURRENT_SETTINGS_REQUESTED_SETTINGS_DOMAIN_NAME_LIST |
319 QMI_WDS_GET_CURRENT_SETTINGS_REQUESTED_SETTINGS_IP_FAMILY);
320 qmi_set_wds_get_current_settings_request(msg, &gcs_req);
321 return QMI_CMD_REQUEST;