2 * wpa_supplicant ctrl_iface helpers
3 * Copyright (c) 2010-2011, Atheros Communications, Inc.
4 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
15 #include "wpa_helpers.h"
18 char *wpas_ctrl_path = "/var/run/wpa_supplicant/";
19 static int default_timeout = 60;
22 static struct wpa_ctrl * wpa_open_ctrl(const char *ifname)
25 struct wpa_ctrl *ctrl;
27 os_snprintf(buf, sizeof(buf), "%s%s", wpas_ctrl_path, ifname);
28 ctrl = wpa_ctrl_open(buf);
30 printf("wpa_command: wpa_ctrl_open(%s) failed\n", buf);
35 int wpa_command(const char *ifname, const char *cmd)
37 struct wpa_ctrl *ctrl;
41 printf("wpa_command(ifname='%s', cmd='%s')\n", ifname, cmd);
42 ctrl = wpa_open_ctrl(ifname);
46 if (wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, NULL) < 0) {
47 printf("wpa_command: wpa_ctrl_request failed\n");
53 if (strncmp(buf, "FAIL", 4) == 0) {
54 printf("wpa_command: Command failed (FAIL received)\n");
61 int wpa_command_resp(const char *ifname, const char *cmd,
62 char *resp, size_t resp_size)
64 struct wpa_ctrl *ctrl;
67 printf("wpa_command(ifname='%s', cmd='%s')\n", ifname, cmd);
68 ctrl = wpa_open_ctrl(ifname);
72 if (wpa_ctrl_request(ctrl, cmd, strlen(cmd), resp, &len, NULL) < 0) {
73 printf("wpa_command: wpa_ctrl_request failed\n");
83 struct wpa_ctrl * open_wpa_mon(const char *ifname)
85 struct wpa_ctrl *ctrl;
87 ctrl = wpa_open_ctrl(ifname);
90 if (wpa_ctrl_attach(ctrl) < 0) {
99 int get_wpa_cli_event2(struct wpa_ctrl *mon,
100 const char *event, const char *event2,
101 char *buf, size_t buf_size)
109 printf("Waiting for wpa_cli event %s\n", event);
110 fd = wpa_ctrl_get_fd(mon);
120 tv.tv_sec = default_timeout;
122 ret = select(fd + 1, &rfd, NULL, NULL, &tv);
124 printf("Timeout on waiting for event %s\n", event);
128 printf("select: %s\n", strerror(errno));
132 if (wpa_ctrl_recv(mon, buf, &len) < 0) {
133 printf("Failure while waiting for event %s\n", event);
140 pos = strchr(buf, '>');
142 (strncmp(pos + 1, event, strlen(event)) == 0 ||
144 strncmp(pos + 1, event2, strlen(event2)) == 0)))
145 return 0; /* Event found */
148 if ((int) (now - start) > default_timeout) {
149 printf("Timeout on waiting for event %s\n", event);
156 int get_wpa_cli_event(struct wpa_ctrl *mon,
157 const char *event, char *buf, size_t buf_size)
159 return get_wpa_cli_event2(mon, event, NULL, buf, buf_size);
163 int get_wpa_status(const char *ifname, const char *field, char *obuf,
166 struct wpa_ctrl *ctrl;
171 ctrl = wpa_open_ctrl(ifname);
175 if (wpa_ctrl_request(ctrl, "STATUS", 6, buf, &len, NULL) < 0) {
176 wpa_ctrl_close(ctrl);
179 wpa_ctrl_close(ctrl);
182 flen = strlen(field);
184 while (pos + flen < buf + len) {
192 if (strncmp(pos, field, flen) != 0 || pos[flen] != '=') {
197 end = strchr(pos, '\n');
201 if (end - pos > (int) obuf_size)
203 memcpy(obuf, pos, end - pos);
211 int wait_ip_addr(const char *ifname, int timeout)
215 struct wpa_ctrl *ctrl;
218 printf("%s: ifname='%s' - %d seconds remaining\n",
219 __func__, ifname, count);
221 if (get_wpa_status(ifname, "ip_address", ip, sizeof(ip)) == 0
223 printf("IP address found: '%s'\n", ip);
226 ctrl = wpa_open_ctrl(ifname);
229 wpa_ctrl_close(ctrl);
232 printf("%s: Could not get IP address for ifname='%s'", __func__,
238 int add_network(const char *ifname)
242 if (wpa_command_resp(ifname, "ADD_NETWORK", res, sizeof(res)) < 0)
248 int set_network(const char *ifname, int id, const char *field,
252 snprintf(buf, sizeof(buf), "SET_NETWORK %d %s %s", id, field, value);
253 return wpa_command(ifname, buf);
257 int set_network_quoted(const char *ifname, int id, const char *field,
261 snprintf(buf, sizeof(buf), "SET_NETWORK %d %s \"%s\"",
263 return wpa_command(ifname, buf);
267 int add_cred(const char *ifname)
271 if (wpa_command_resp(ifname, "ADD_CRED", res, sizeof(res)) < 0)
277 int set_cred(const char *ifname, int id, const char *field, const char *value)
280 snprintf(buf, sizeof(buf), "SET_CRED %d %s %s", id, field, value);
281 return wpa_command(ifname, buf);
285 int set_cred_quoted(const char *ifname, int id, const char *field,
289 snprintf(buf, sizeof(buf), "SET_CRED %d %s \"%s\"",
291 return wpa_command(ifname, buf);