2 * Copyright 2008, The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/socket.h>
27 #include "hardware_legacy/wifi.h"
28 #ifdef LIBWPA_CLIENT_EXISTS
29 #include "libwpa_client/wpa_ctrl.h"
32 #define LOG_TAG "WifiHW"
33 #include "cutils/log.h"
34 #include "cutils/memory.h"
35 #include "cutils/misc.h"
36 #include "cutils/properties.h"
37 #include "private/android_filesystem_config.h"
39 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
40 #include <sys/_system_properties.h>
43 extern int ifc_init();
44 extern void ifc_close();
45 extern char *dhcp_lasterror();
46 extern void get_dhcp_info();
47 extern int init_module(void *, unsigned long, const char *);
48 extern int delete_module(const char *, unsigned int);
49 void wifi_close_sockets();
51 #ifndef LIBWPA_CLIENT_EXISTS
52 #define WPA_EVENT_TERMINATING "CTRL-EVENT-TERMINATING "
54 void wpa_ctrl_cleanup(void) {}
55 struct wpa_ctrl *wpa_ctrl_open(const char *ctrl_path) { return NULL; }
56 void wpa_ctrl_close(struct wpa_ctrl *ctrl) {}
57 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
58 char *reply, size_t *reply_len, void (*msg_cb)(char *msg, size_t len))
60 int wpa_ctrl_attach(struct wpa_ctrl *ctrl) { return 0; }
61 int wpa_ctrl_detach(struct wpa_ctrl *ctrl) { return 0; }
62 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
64 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl) { return 0; }
67 static struct wpa_ctrl *ctrl_conn;
68 static struct wpa_ctrl *monitor_conn;
70 /* socket pair used to exit from a blocking read */
71 static int exit_sockets[2];
73 static char primary_iface[PROPERTY_VALUE_MAX];
74 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
77 #ifndef WIFI_DRIVER_MODULE_ARG
78 #define WIFI_DRIVER_MODULE_ARG ""
80 #ifndef WIFI_FIRMWARE_LOADER
81 #define WIFI_FIRMWARE_LOADER ""
83 #define WIFI_TEST_INTERFACE "sta"
85 #ifndef WIFI_DRIVER_FW_PATH_STA
86 #define WIFI_DRIVER_FW_PATH_STA NULL
88 #ifndef WIFI_DRIVER_FW_PATH_AP
89 #define WIFI_DRIVER_FW_PATH_AP NULL
91 #ifndef WIFI_DRIVER_FW_PATH_P2P
92 #define WIFI_DRIVER_FW_PATH_P2P NULL
95 #ifndef WIFI_DRIVER_FW_PATH_PARAM
96 #define WIFI_DRIVER_FW_PATH_PARAM "/sys/module/wlan/parameters/fwpath"
99 #define WIFI_DRIVER_LOADER_DELAY 1000000
100 #define SYSFS_PATH_MAX 256
102 static const char IFACE_DIR[] = "/data/system/wpa_supplicant";
103 #ifdef WIFI_DRIVER_MODULE_PATH
104 static const char DRIVER_NAME_PROP[] = "wlan.modname";
105 static const char DRIVER_PATH_PROP[] = "wlan.modpath";
106 static const char DRIVER_MODULE_ARG[] = WIFI_DRIVER_MODULE_ARG;
108 static const char FIRMWARE_LOADER[] = WIFI_FIRMWARE_LOADER;
109 static const char DRIVER_PROP_NAME[] = "wlan.driver.status";
110 static const char SUPPLICANT_NAME[] = "wpa_supplicant";
111 static const char SUPP_PROP_NAME[] = "init.svc.wpa_supplicant";
112 static const char P2P_SUPPLICANT_NAME[] = "p2p_supplicant";
113 static const char P2P_PROP_NAME[] = "init.svc.p2p_supplicant";
114 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
115 static const char SUPP_CONFIG_FILE[] = "/data/misc/wifi/wpa_supplicant.conf";
116 static const char P2P_CONFIG_FILE[] = "/data/misc/wifi/p2p_supplicant.conf";
117 static const char CONTROL_IFACE_PATH[] = "/data/misc/wifi/sockets";
118 static const char MODULE_FILE[] = "/proc/modules";
119 static const char SYSFS_CLASS_NET[] = "/sys/class/net";
120 static const char MODULE_DEFAULT_DIR[] = "/system/lib/modules";
121 static const char SYS_MOD_NAME_DIR[] = "device/driver/module";
123 static const char IFNAME[] = "IFNAME=";
124 #define IFNAMELEN (sizeof(IFNAME) - 1)
125 static const char WPA_EVENT_IGNORE[] = "CTRL-EVENT-IGNORE ";
127 static const char SUPP_ENTROPY_FILE[] = WIFI_ENTROPY_FILE;
128 static unsigned char dummy_key[21] = { 0x02, 0x11, 0xbe, 0x33, 0x43, 0x35,
129 0x68, 0x47, 0x84, 0x99, 0xa9, 0x2b,
130 0x1c, 0xd3, 0xee, 0xff, 0xf1, 0xe2,
133 /* Is either SUPPLICANT_NAME or P2P_SUPPLICANT_NAME */
134 static char supplicant_name[PROPERTY_VALUE_MAX];
135 /* Is either SUPP_PROP_NAME or P2P_PROP_NAME */
136 static char supplicant_prop_name[PROPERTY_KEY_MAX];
138 static int insmod(const char *filename, const char *args)
144 module = load_file(filename, &size);
148 ret = init_module(module, size, args);
155 static int rmmod(const char *modname)
160 while (maxtry-- > 0) {
161 ret = delete_module(modname, O_NONBLOCK | O_EXCL);
162 if (ret < 0 && errno == EAGAIN)
169 ALOGD("Unable to unload driver module \"%s\": %s\n",
170 modname, strerror(errno));
174 int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
175 int *dns1, int *dns2, int *server, int *lease) {
176 /* For test driver, always report success */
177 if (strcmp(primary_iface, WIFI_TEST_INTERFACE) == 0)
183 if (do_dhcp(primary_iface) < 0) {
188 get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
192 const char *get_dhcp_error_string() {
193 return dhcp_lasterror();
196 #ifdef WIFI_DRIVER_STATE_CTRL_PARAM
197 int wifi_change_driver_state(const char *state)
205 fd = TEMP_FAILURE_RETRY(open(WIFI_DRIVER_STATE_CTRL_PARAM, O_WRONLY));
207 ALOGE("Failed to open driver state control param (%s)", strerror(errno));
210 len = strlen(state) + 1;
211 if (TEMP_FAILURE_RETRY(write(fd, state, len)) != len) {
212 ALOGE("Failed to write driver state control param (%s)", strerror(errno));
220 #ifdef WIFI_DRIVER_MODULE_PATH
221 static int get_driver_path(const char *mod, const char *path, char *buf) {
224 char modpath[SYSFS_PATH_MAX];
227 if ((dir = opendir(path))) {
228 while ((de = readdir(dir))) {
230 if (de->d_name[0] == '.')
232 snprintf(modpath, SYSFS_PATH_MAX, "%s/%s", path, de->d_name);
233 if (!strcmp(de->d_name, mod)) {
234 strncpy(buf, modpath, SYSFS_PATH_MAX - 1);
235 buf[SYSFS_PATH_MAX - 1] = '\0';
239 if (!stat(modpath, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR)
240 if ((ret = get_driver_path(mod, modpath, buf)))
248 static int get_driver_info(char *buf) {
251 char path[SYSFS_PATH_MAX];
252 char link[SYSFS_PATH_MAX];
255 if ((netdir = opendir(SYSFS_CLASS_NET))) {
256 while ((de = readdir(netdir))) {
259 if (de->d_name[0] == '.')
261 snprintf(path, SYSFS_PATH_MAX, "%s/%s/wireless", SYSFS_CLASS_NET, de->d_name);
262 if (access(path, F_OK)) {
263 snprintf(path, SYSFS_PATH_MAX, "%s/%s/phy80211", SYSFS_CLASS_NET, de->d_name);
264 if (access(path, F_OK))
267 /* found the wifi interface */
268 property_set("wlan.interface", de->d_name);
269 snprintf(path, SYSFS_PATH_MAX, "%s/%s/%s", SYSFS_CLASS_NET, de->d_name, SYS_MOD_NAME_DIR);
270 if ((cnt = readlink(path, link, SYSFS_PATH_MAX - 1)) < 0) {
271 ALOGW("can not find link of %s", path);
275 if ((pos = strrchr(link, '/'))) {
276 property_set(DRIVER_NAME_PROP, ++pos);
277 strncpy(buf, pos, PROPERTY_VALUE_MAX - 1);
278 buf[PROPERTY_VALUE_MAX - 1] = '\0';
290 int is_wifi_driver_loaded() {
291 #ifdef WIFI_DRIVER_MODULE_PATH
292 char modname[PROPERTY_VALUE_MAX];
293 char line[PROPERTY_VALUE_MAX];
295 int cnt = property_get(DRIVER_NAME_PROP, modname, NULL);
298 if (get_driver_info(modname))
299 cnt = strlen(modname);
300 else if (property_get("wlan.interface", line, NULL))
301 return 1; // found an interface without modname, assume built-in
305 modname[cnt++] = ' ';
308 * If the property says the driver is loaded, check to
309 * make sure that the property setting isn't just left
310 * over from a previous manual shutdown or a runtime
313 if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
314 ALOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
318 while ((fgets(line, sizeof(line), proc)) != NULL) {
319 if (strncmp(line, modname, cnt) == 0) {
327 property_set(DRIVER_PROP_NAME, "unloaded");
334 int wifi_load_driver()
336 #ifdef WIFI_DRIVER_MODULE_PATH
337 char driver_status[PROPERTY_VALUE_MAX];
338 char modname[PROPERTY_VALUE_MAX];
339 char modpath[SYSFS_PATH_MAX];
340 int count = 100; /* wait at most 20 seconds for completion */
342 if (is_wifi_driver_loaded()) {
346 if (!property_get(DRIVER_PATH_PROP, modpath, NULL)) {
347 property_get(DRIVER_NAME_PROP, modname, NULL);
348 strcat(modname, ".ko");
349 if (!get_driver_path(modname, MODULE_DEFAULT_DIR, modpath))
350 strcpy(modpath, WIFI_DRIVER_MODULE_PATH);
353 ALOGI("got module path %s", modpath);
354 if (insmod(modpath, DRIVER_MODULE_ARG) < 0)
357 if (strcmp(FIRMWARE_LOADER,"") == 0) {
358 /* usleep(WIFI_DRIVER_LOADER_DELAY); */
359 property_set(DRIVER_PROP_NAME, "ok");
362 property_set("ctl.start", FIRMWARE_LOADER);
365 while (count-- > 0) {
366 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
367 if (strcmp(driver_status, "ok") == 0) {
368 get_driver_info(modname);
370 } else if (strcmp(driver_status, "failed") == 0) {
371 wifi_unload_driver();
377 property_set(DRIVER_PROP_NAME, "timeout");
378 wifi_unload_driver();
381 #ifdef WIFI_DRIVER_STATE_CTRL_PARAM
382 if (is_wifi_driver_loaded()) {
386 if (wifi_change_driver_state(WIFI_DRIVER_STATE_ON) < 0)
389 property_set(DRIVER_PROP_NAME, "ok");
394 int wifi_unload_driver()
396 usleep(200000); /* allow to finish interface down */
397 #ifdef WIFI_DRIVER_MODULE_PATH
398 char modname[PROPERTY_VALUE_MAX];
399 if (!property_get(DRIVER_NAME_PROP, modname, NULL))
402 if (rmmod(modname) == 0) {
403 int count = 20; /* wait at most 10 seconds for completion */
404 while (count-- > 0) {
405 if (!is_wifi_driver_loaded())
409 usleep(500000); /* allow card removal */
416 #ifdef WIFI_DRIVER_STATE_CTRL_PARAM
417 if (is_wifi_driver_loaded()) {
418 if (wifi_change_driver_state(WIFI_DRIVER_STATE_OFF) < 0)
422 property_set(DRIVER_PROP_NAME, "unloaded");
427 int ensure_entropy_file_exists()
432 ret = access(SUPP_ENTROPY_FILE, R_OK|W_OK);
433 if ((ret == 0) || (errno == EACCES)) {
435 (chmod(SUPP_ENTROPY_FILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
436 ALOGE("Cannot set RW to \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
441 destfd = TEMP_FAILURE_RETRY(open(SUPP_ENTROPY_FILE, O_CREAT|O_RDWR, 0660));
443 ALOGE("Cannot create \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
447 if (TEMP_FAILURE_RETRY(write(destfd, dummy_key, sizeof(dummy_key))) != sizeof(dummy_key)) {
448 ALOGE("Error writing \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
454 /* chmod is needed because open() didn't set permisions properly */
455 if (chmod(SUPP_ENTROPY_FILE, 0660) < 0) {
456 ALOGE("Error changing permissions of %s to 0660: %s",
457 SUPP_ENTROPY_FILE, strerror(errno));
458 unlink(SUPP_ENTROPY_FILE);
462 if (chown(SUPP_ENTROPY_FILE, AID_SYSTEM, AID_WIFI) < 0) {
463 ALOGE("Error changing group ownership of %s to %d: %s",
464 SUPP_ENTROPY_FILE, AID_WIFI, strerror(errno));
465 unlink(SUPP_ENTROPY_FILE);
471 int ensure_config_file_exists(const char *config_file)
479 ret = access(config_file, R_OK|W_OK);
480 if ((ret == 0) || (errno == EACCES)) {
482 (chmod(config_file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
483 ALOGE("Cannot set RW to \"%s\": %s", config_file, strerror(errno));
487 } else if (errno != ENOENT) {
488 ALOGE("Cannot access \"%s\": %s", config_file, strerror(errno));
492 srcfd = TEMP_FAILURE_RETRY(open(SUPP_CONFIG_TEMPLATE, O_RDONLY));
494 ALOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
498 destfd = TEMP_FAILURE_RETRY(open(config_file, O_CREAT|O_RDWR, 0660));
501 ALOGE("Cannot create \"%s\": %s", config_file, strerror(errno));
505 while ((nread = TEMP_FAILURE_RETRY(read(srcfd, buf, sizeof(buf)))) != 0) {
507 ALOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
513 TEMP_FAILURE_RETRY(write(destfd, buf, nread));
519 /* chmod is needed because open() didn't set permisions properly */
520 if (chmod(config_file, 0660) < 0) {
521 ALOGE("Error changing permissions of %s to 0660: %s",
522 config_file, strerror(errno));
527 if (chown(config_file, AID_SYSTEM, AID_WIFI) < 0) {
528 ALOGE("Error changing group ownership of %s to %d: %s",
529 config_file, AID_WIFI, strerror(errno));
536 int wifi_start_supplicant(int p2p_supported)
538 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
539 int count = 200; /* wait at most 20 seconds for completion */
541 unsigned serial = 0, i;
544 strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
545 strcpy(supplicant_prop_name, P2P_PROP_NAME);
547 /* Ensure p2p config file is created */
548 if (ensure_config_file_exists(P2P_CONFIG_FILE) < 0) {
549 ALOGE("Failed to create a p2p config file");
554 strcpy(supplicant_name, SUPPLICANT_NAME);
555 strcpy(supplicant_prop_name, SUPP_PROP_NAME);
558 /* Check whether already running */
559 if (property_get(supplicant_prop_name, supp_status, NULL)
560 && strcmp(supp_status, "running") == 0) {
564 /* Before starting the daemon, make sure its config file exists */
565 if (ensure_config_file_exists(SUPP_CONFIG_FILE) < 0) {
566 ALOGE("Wi-Fi will not be enabled");
570 if (ensure_entropy_file_exists() < 0) {
571 ALOGE("Wi-Fi entropy file was not created");
574 /* Clear out any stale socket files that might be left over. */
577 /* Reset sockets used for exiting from hung state */
578 exit_sockets[0] = exit_sockets[1] = -1;
581 * Get a reference to the status property, so we can distinguish
582 * the case where it goes stopped => running => stopped (i.e.,
583 * it start up, but fails right away) from the case in which
584 * it starts in the stopped state and never manages to start
587 pi = __system_property_find(supplicant_prop_name);
589 serial = __system_property_serial(pi);
591 property_get("wlan.interface", primary_iface, WIFI_TEST_INTERFACE);
593 property_set("ctl.start", supplicant_name);
596 while (count-- > 0) {
598 pi = __system_property_find(supplicant_prop_name);
602 * property serial updated means that init process is scheduled
603 * after we sched_yield, further property status checking is based on this */
604 if (__system_property_serial(pi) != serial) {
605 __system_property_read(pi, NULL, supp_status);
606 if (strcmp(supp_status, "running") == 0) {
608 } else if (strcmp(supp_status, "stopped") == 0) {
618 int wifi_stop_supplicant(int p2p_supported)
620 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
621 int count = 50; /* wait at most 5 seconds for completion */
624 strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
625 strcpy(supplicant_prop_name, P2P_PROP_NAME);
627 strcpy(supplicant_name, SUPPLICANT_NAME);
628 strcpy(supplicant_prop_name, SUPP_PROP_NAME);
631 /* Check whether supplicant already stopped */
632 if (property_get(supplicant_prop_name, supp_status, NULL)
633 && strcmp(supp_status, "stopped") == 0) {
637 property_set("ctl.stop", supplicant_name);
640 while (count-- > 0) {
641 if (property_get(supplicant_prop_name, supp_status, NULL)) {
642 if (strcmp(supp_status, "stopped") == 0)
647 ALOGE("Failed to stop supplicant");
651 int wifi_connect_on_socket_path(const char *path)
653 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
655 /* Make sure supplicant is running */
656 if (!property_get(supplicant_prop_name, supp_status, NULL)
657 || strcmp(supp_status, "running") != 0) {
658 ALOGE("Supplicant not running, cannot connect");
662 ctrl_conn = wpa_ctrl_open(path);
663 if (ctrl_conn == NULL) {
664 ALOGE("Unable to open connection to supplicant on \"%s\": %s",
665 path, strerror(errno));
668 monitor_conn = wpa_ctrl_open(path);
669 if (monitor_conn == NULL) {
670 wpa_ctrl_close(ctrl_conn);
674 if (wpa_ctrl_attach(monitor_conn) != 0) {
675 wpa_ctrl_close(monitor_conn);
676 wpa_ctrl_close(ctrl_conn);
677 ctrl_conn = monitor_conn = NULL;
681 if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets) == -1) {
682 wpa_ctrl_close(monitor_conn);
683 wpa_ctrl_close(ctrl_conn);
684 ctrl_conn = monitor_conn = NULL;
691 /* Establishes the control and monitor socket connections on the interface */
692 int wifi_connect_to_supplicant()
694 static char path[PATH_MAX];
696 if (access(IFACE_DIR, F_OK) == 0) {
697 snprintf(path, sizeof(path), "%s/%s", IFACE_DIR, primary_iface);
699 snprintf(path, sizeof(path), "@android:wpa_%s", primary_iface);
701 return wifi_connect_on_socket_path(path);
704 int wifi_send_command(const char *cmd, char *reply, size_t *reply_len)
707 if (ctrl_conn == NULL) {
708 ALOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
711 ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), reply, reply_len, NULL);
713 ALOGD("'%s' command timed out.\n", cmd);
714 /* unblocks the monitor receive socket for termination */
715 TEMP_FAILURE_RETRY(write(exit_sockets[0], "T", 1));
717 } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
720 if (strncmp(cmd, "PING", 4) == 0) {
721 reply[*reply_len] = '\0';
726 int wifi_supplicant_connection_active()
728 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
730 if (property_get(supplicant_prop_name, supp_status, NULL)) {
731 if (strcmp(supp_status, "stopped") == 0)
738 int wifi_ctrl_recv(char *reply, size_t *reply_len)
741 int ctrlfd = wpa_ctrl_get_fd(monitor_conn);
742 struct pollfd rfds[2];
744 memset(rfds, 0, 2 * sizeof(struct pollfd));
746 rfds[0].events |= POLLIN;
747 rfds[1].fd = exit_sockets[1];
748 rfds[1].events |= POLLIN;
750 res = TEMP_FAILURE_RETRY(poll(rfds, 2, 30000));
752 ALOGE("Error poll = %d", res);
754 } else if (res == 0) {
755 /* timed out, check if supplicant is active
758 res = wifi_supplicant_connection_active();
764 if (rfds[0].revents & POLLIN) {
765 return wpa_ctrl_recv(monitor_conn, reply, reply_len);
768 /* it is not rfds[0], then it must be rfts[1] (i.e. the exit socket)
769 * or we timed out. In either case, this call has failed ..
774 int wifi_wait_on_socket(char *buf, size_t buflen)
776 size_t nread = buflen - 1;
778 char *match, *match2;
780 if (monitor_conn == NULL) {
781 return snprintf(buf, buflen, "IFNAME=%s %s - connection closed",
782 primary_iface, WPA_EVENT_TERMINATING);
785 result = wifi_ctrl_recv(buf, &nread);
787 /* Terminate reception on exit socket */
789 return snprintf(buf, buflen, "IFNAME=%s %s - connection closed",
790 primary_iface, WPA_EVENT_TERMINATING);
794 ALOGD("wifi_ctrl_recv failed: %s\n", strerror(errno));
795 return snprintf(buf, buflen, "IFNAME=%s %s - recv error",
796 primary_iface, WPA_EVENT_TERMINATING);
799 /* Check for EOF on the socket */
800 if (result == 0 && nread == 0) {
801 /* Fabricate an event to pass up */
802 ALOGD("Received EOF on supplicant socket\n");
803 return snprintf(buf, buflen, "IFNAME=%s %s - signal 0 received",
804 primary_iface, WPA_EVENT_TERMINATING);
807 * Events strings are in the format
809 * IFNAME=iface <N>CTRL-EVENT-XXX
813 * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
814 * etc.) and XXX is the event name. The level information is not useful
815 * to us, so strip it off.
818 if (strncmp(buf, IFNAME, IFNAMELEN) == 0) {
819 match = strchr(buf, ' ');
821 if (match[1] == '<') {
822 match2 = strchr(match + 2, '>');
823 if (match2 != NULL) {
824 nread -= (match2 - match);
825 memmove(match + 1, match2 + 1, nread - (match - buf) + 1);
829 return snprintf(buf, buflen, "%s", WPA_EVENT_IGNORE);
831 } else if (buf[0] == '<') {
832 match = strchr(buf, '>');
834 nread -= (match + 1 - buf);
835 memmove(buf, match + 1, nread + 1);
836 ALOGV("supplicant generated event without interface - %s\n", buf);
839 /* let the event go as is! */
840 ALOGW("supplicant generated event without interface and without message level - %s\n", buf);
846 int wifi_wait_for_event(char *buf, size_t buflen)
848 return wifi_wait_on_socket(buf, buflen);
851 void wifi_close_sockets()
853 if (ctrl_conn != NULL) {
854 wpa_ctrl_close(ctrl_conn);
858 if (monitor_conn != NULL) {
859 wpa_ctrl_close(monitor_conn);
863 if (exit_sockets[0] >= 0) {
864 close(exit_sockets[0]);
865 exit_sockets[0] = -1;
868 if (exit_sockets[1] >= 0) {
869 close(exit_sockets[1]);
870 exit_sockets[1] = -1;
874 void wifi_close_supplicant_connection()
876 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
877 int count = 50; /* wait at most 5 seconds to ensure init has stopped stupplicant */
879 wifi_close_sockets();
881 while (count-- > 0) {
882 if (property_get(supplicant_prop_name, supp_status, NULL)) {
883 if (strcmp(supp_status, "stopped") == 0)
890 int wifi_command(const char *command, char *reply, size_t *reply_len)
892 return wifi_send_command(command, reply, reply_len);
895 const char *wifi_get_fw_path(int fw_type)
898 case WIFI_GET_FW_PATH_STA:
899 return WIFI_DRIVER_FW_PATH_STA;
900 case WIFI_GET_FW_PATH_AP:
901 return WIFI_DRIVER_FW_PATH_AP;
902 case WIFI_GET_FW_PATH_P2P:
903 return WIFI_DRIVER_FW_PATH_P2P;
908 int wifi_change_fw_path(const char *fwpath)
916 fd = TEMP_FAILURE_RETRY(open(WIFI_DRIVER_FW_PATH_PARAM, O_WRONLY));
918 ALOGE("Failed to open wlan fw path param (%s)", strerror(errno));
921 len = strlen(fwpath) + 1;
922 if (TEMP_FAILURE_RETRY(write(fd, fwpath, len)) != len) {
923 ALOGE("Failed to write wlan fw path param (%s)", strerror(errno));