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>
26 #include "hardware_legacy/wifi.h"
27 #include "libwpa_client/wpa_ctrl.h"
29 #define LOG_TAG "WifiHW"
30 #include "cutils/log.h"
31 #include "cutils/memory.h"
32 #include "cutils/misc.h"
33 #include "cutils/properties.h"
34 #include "private/android_filesystem_config.h"
35 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
36 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
37 #include <sys/_system_properties.h>
40 /* PRIMARY refers to the connection on the primary interface
41 * SECONDARY refers to an optional connection on a p2p interface
43 * For concurrency, we only support one active p2p connection and
44 * one active STA connection at a time
50 static struct wpa_ctrl *ctrl_conn[MAX_CONNS];
51 static struct wpa_ctrl *monitor_conn[MAX_CONNS];
53 /* socket pair used to exit from a blocking read */
54 static int exit_sockets[MAX_CONNS][2];
57 extern int ifc_init();
58 extern void ifc_close();
59 extern char *dhcp_lasterror();
60 extern void get_dhcp_info();
61 extern int init_module(void *, unsigned long, const char *);
62 extern int delete_module(const char *, unsigned int);
63 void wifi_close_sockets(int index);
65 static char primary_iface[PROPERTY_VALUE_MAX];
66 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
69 #ifndef WIFI_DRIVER_MODULE_ARG
70 #define WIFI_DRIVER_MODULE_ARG ""
72 #ifndef WIFI_FIRMWARE_LOADER
73 #define WIFI_FIRMWARE_LOADER ""
75 #define WIFI_TEST_INTERFACE "sta"
77 #ifndef WIFI_DRIVER_FW_PATH_STA
78 #define WIFI_DRIVER_FW_PATH_STA NULL
80 #ifndef WIFI_DRIVER_FW_PATH_AP
81 #define WIFI_DRIVER_FW_PATH_AP NULL
83 #ifndef WIFI_DRIVER_FW_PATH_P2P
84 #define WIFI_DRIVER_FW_PATH_P2P NULL
87 #ifndef WIFI_DRIVER_FW_PATH_PARAM
88 #define WIFI_DRIVER_FW_PATH_PARAM "/sys/module/wlan/parameters/fwpath"
91 #define WIFI_DRIVER_LOADER_DELAY 1000000
93 static const char IFACE_DIR[] = "/data/system/wpa_supplicant";
94 #ifdef WIFI_DRIVER_MODULE_PATH
95 static const char DRIVER_MODULE_NAME[] = WIFI_DRIVER_MODULE_NAME;
96 static const char DRIVER_MODULE_TAG[] = WIFI_DRIVER_MODULE_NAME " ";
97 static const char DRIVER_MODULE_PATH[] = WIFI_DRIVER_MODULE_PATH;
98 static const char DRIVER_MODULE_ARG[] = WIFI_DRIVER_MODULE_ARG;
100 static const char FIRMWARE_LOADER[] = WIFI_FIRMWARE_LOADER;
101 static const char DRIVER_PROP_NAME[] = "wlan.driver.status";
102 static const char SUPPLICANT_NAME[] = "wpa_supplicant";
103 static const char SUPP_PROP_NAME[] = "init.svc.wpa_supplicant";
104 static const char P2P_SUPPLICANT_NAME[] = "p2p_supplicant";
105 static const char P2P_PROP_NAME[] = "init.svc.p2p_supplicant";
106 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
107 static const char SUPP_CONFIG_FILE[] = "/data/misc/wifi/wpa_supplicant.conf";
108 static const char P2P_CONFIG_FILE[] = "/data/misc/wifi/p2p_supplicant.conf";
109 static const char CONTROL_IFACE_PATH[] = "/data/misc/wifi/sockets";
110 static const char MODULE_FILE[] = "/proc/modules";
112 static const char SUPP_ENTROPY_FILE[] = WIFI_ENTROPY_FILE;
113 static unsigned char dummy_key[21] = { 0x02, 0x11, 0xbe, 0x33, 0x43, 0x35,
114 0x68, 0x47, 0x84, 0x99, 0xa9, 0x2b,
115 0x1c, 0xd3, 0xee, 0xff, 0xf1, 0xe2,
118 /* Is either SUPPLICANT_NAME or P2P_SUPPLICANT_NAME */
119 static char supplicant_name[PROPERTY_VALUE_MAX];
120 /* Is either SUPP_PROP_NAME or P2P_PROP_NAME */
121 static char supplicant_prop_name[PROPERTY_KEY_MAX];
123 static int is_primary_interface(const char *ifname)
125 //Treat NULL as primary interface to allow control
126 //on STA without an interface
127 if (ifname == NULL || !strncmp(ifname, primary_iface, strlen(primary_iface))) {
133 static int insmod(const char *filename, const char *args)
139 module = load_file(filename, &size);
143 ret = init_module(module, size, args);
150 static int rmmod(const char *modname)
155 while (maxtry-- > 0) {
156 ret = delete_module(modname, O_NONBLOCK | O_EXCL);
157 if (ret < 0 && errno == EAGAIN)
164 ALOGD("Unable to unload driver module \"%s\": %s\n",
165 modname, strerror(errno));
169 int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
170 int *dns1, int *dns2, int *server, int *lease) {
171 /* For test driver, always report success */
172 if (strcmp(primary_iface, WIFI_TEST_INTERFACE) == 0)
178 if (do_dhcp(primary_iface) < 0) {
183 get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
187 const char *get_dhcp_error_string() {
188 return dhcp_lasterror();
191 int is_wifi_driver_loaded() {
192 char driver_status[PROPERTY_VALUE_MAX];
193 #ifdef WIFI_DRIVER_MODULE_PATH
195 char line[sizeof(DRIVER_MODULE_TAG)+10];
198 if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
199 || strcmp(driver_status, "ok") != 0) {
200 return 0; /* driver not loaded */
202 #ifdef WIFI_DRIVER_MODULE_PATH
204 * If the property says the driver is loaded, check to
205 * make sure that the property setting isn't just left
206 * over from a previous manual shutdown or a runtime
209 if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
210 ALOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
211 property_set(DRIVER_PROP_NAME, "unloaded");
214 while ((fgets(line, sizeof(line), proc)) != NULL) {
215 if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
221 property_set(DRIVER_PROP_NAME, "unloaded");
228 int wifi_load_driver()
230 #ifdef WIFI_DRIVER_MODULE_PATH
231 char driver_status[PROPERTY_VALUE_MAX];
232 int count = 100; /* wait at most 20 seconds for completion */
234 if (is_wifi_driver_loaded()) {
238 if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0)
241 if (strcmp(FIRMWARE_LOADER,"") == 0) {
242 /* usleep(WIFI_DRIVER_LOADER_DELAY); */
243 property_set(DRIVER_PROP_NAME, "ok");
246 property_set("ctl.start", FIRMWARE_LOADER);
249 while (count-- > 0) {
250 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
251 if (strcmp(driver_status, "ok") == 0)
253 else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
254 wifi_unload_driver();
260 property_set(DRIVER_PROP_NAME, "timeout");
261 wifi_unload_driver();
264 property_set(DRIVER_PROP_NAME, "ok");
269 int wifi_unload_driver()
271 usleep(200000); /* allow to finish interface down */
272 #ifdef WIFI_DRIVER_MODULE_PATH
273 if (rmmod(DRIVER_MODULE_NAME) == 0) {
274 int count = 20; /* wait at most 10 seconds for completion */
275 while (count-- > 0) {
276 if (!is_wifi_driver_loaded())
280 usleep(500000); /* allow card removal */
288 property_set(DRIVER_PROP_NAME, "unloaded");
293 int ensure_entropy_file_exists()
298 ret = access(SUPP_ENTROPY_FILE, R_OK|W_OK);
299 if ((ret == 0) || (errno == EACCES)) {
301 (chmod(SUPP_ENTROPY_FILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
302 ALOGE("Cannot set RW to \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
307 destfd = TEMP_FAILURE_RETRY(open(SUPP_ENTROPY_FILE, O_CREAT|O_RDWR, 0660));
309 ALOGE("Cannot create \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
313 if (TEMP_FAILURE_RETRY(write(destfd, dummy_key, sizeof(dummy_key))) != sizeof(dummy_key)) {
314 ALOGE("Error writing \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
320 /* chmod is needed because open() didn't set permisions properly */
321 if (chmod(SUPP_ENTROPY_FILE, 0660) < 0) {
322 ALOGE("Error changing permissions of %s to 0660: %s",
323 SUPP_ENTROPY_FILE, strerror(errno));
324 unlink(SUPP_ENTROPY_FILE);
328 if (chown(SUPP_ENTROPY_FILE, AID_SYSTEM, AID_WIFI) < 0) {
329 ALOGE("Error changing group ownership of %s to %d: %s",
330 SUPP_ENTROPY_FILE, AID_WIFI, strerror(errno));
331 unlink(SUPP_ENTROPY_FILE);
337 int update_ctrl_interface(const char *config_file) {
341 char ifc[PROPERTY_VALUE_MAX];
347 if (stat(config_file, &sb) != 0)
350 pbuf = malloc(sb.st_size + PROPERTY_VALUE_MAX);
353 srcfd = TEMP_FAILURE_RETRY(open(config_file, O_RDONLY));
355 ALOGE("Cannot open \"%s\": %s", config_file, strerror(errno));
359 nread = TEMP_FAILURE_RETRY(read(srcfd, pbuf, sb.st_size));
362 ALOGE("Cannot read \"%s\": %s", config_file, strerror(errno));
367 if (!strcmp(config_file, SUPP_CONFIG_FILE)) {
368 property_get("wifi.interface", ifc, WIFI_TEST_INTERFACE);
370 strcpy(ifc, CONTROL_IFACE_PATH);
372 /* Assume file is invalid to begin with */
375 * if there is a "ctrl_interface=<value>" entry, re-write it ONLY if it is
376 * NOT a directory. The non-directory value option is an Android add-on
377 * that allows the control interface to be exchanged through an environment
378 * variable (initialized by the "init" program when it starts a service
379 * with a "socket" option).
381 * The <value> is deemed to be a directory if the "DIR=" form is used or
382 * the value begins with "/".
384 if (sptr = strstr(pbuf, "ctrl_interface=")) {
386 if ((!strstr(pbuf, "ctrl_interface=DIR=")) &&
387 (!strstr(pbuf, "ctrl_interface=/"))) {
388 char *iptr = sptr + strlen("ctrl_interface=");
390 int mlen = strlen(ifc);
392 if (strncmp(ifc, iptr, mlen) != 0) {
393 ALOGE("ctrl_interface != %s", ifc);
394 while (((ilen + (iptr - pbuf)) < nread) && (iptr[ilen] != '\n'))
396 mlen = ((ilen >= mlen) ? ilen : mlen) + 1;
397 memmove(iptr + mlen, iptr + ilen + 1, nread - (iptr + ilen + 1 - pbuf));
398 memset(iptr, '\n', mlen);
399 memcpy(iptr, ifc, strlen(ifc));
400 destfd = TEMP_FAILURE_RETRY(open(config_file, O_RDWR, 0660));
402 ALOGE("Cannot update \"%s\": %s", config_file, strerror(errno));
406 TEMP_FAILURE_RETRY(write(destfd, pbuf, nread + mlen - ilen -1));
415 int ensure_config_file_exists(const char *config_file)
423 ret = access(config_file, R_OK|W_OK);
424 if ((ret == 0) || (errno == EACCES)) {
426 (chmod(config_file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
427 ALOGE("Cannot set RW to \"%s\": %s", config_file, strerror(errno));
430 /* return if we were able to update control interface properly */
431 if (update_ctrl_interface(config_file) >=0) {
434 /* This handles the scenario where the file had bad data
435 * for some reason. We continue and recreate the file.
438 } else if (errno != ENOENT) {
439 ALOGE("Cannot access \"%s\": %s", config_file, strerror(errno));
443 srcfd = TEMP_FAILURE_RETRY(open(SUPP_CONFIG_TEMPLATE, O_RDONLY));
445 ALOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
449 destfd = TEMP_FAILURE_RETRY(open(config_file, O_CREAT|O_RDWR, 0660));
452 ALOGE("Cannot create \"%s\": %s", config_file, strerror(errno));
456 while ((nread = TEMP_FAILURE_RETRY(read(srcfd, buf, sizeof(buf)))) != 0) {
458 ALOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
464 TEMP_FAILURE_RETRY(write(destfd, buf, nread));
470 /* chmod is needed because open() didn't set permisions properly */
471 if (chmod(config_file, 0660) < 0) {
472 ALOGE("Error changing permissions of %s to 0660: %s",
473 config_file, strerror(errno));
478 if (chown(config_file, AID_SYSTEM, AID_WIFI) < 0) {
479 ALOGE("Error changing group ownership of %s to %d: %s",
480 config_file, AID_WIFI, strerror(errno));
484 return update_ctrl_interface(config_file);
488 * wifi_wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
489 * may be left over from clients that were previously connected to
490 * wpa_supplicant. This keeps these files from being orphaned in the
491 * event of crashes that prevented them from being removed as part
492 * of the normal orderly shutdown.
494 void wifi_wpa_ctrl_cleanup(void)
498 struct dirent *result;
501 char pathname[PATH_MAX];
503 char *local_socket_dir = CONFIG_CTRL_IFACE_CLIENT_DIR;
504 char *local_socket_prefix = CONFIG_CTRL_IFACE_CLIENT_PREFIX;
506 if ((dir = opendir(local_socket_dir)) == NULL)
509 dirnamelen = (size_t)snprintf(pathname, sizeof(pathname), "%s/", local_socket_dir);
510 if (dirnamelen >= sizeof(pathname)) {
514 namep = pathname + dirnamelen;
515 maxcopy = PATH_MAX - dirnamelen;
516 while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
517 if (strncmp(entry.d_name, local_socket_prefix, strlen(local_socket_prefix)) == 0) {
518 if (strlcpy(namep, entry.d_name, maxcopy) < maxcopy) {
526 int wifi_start_supplicant(int p2p_supported)
528 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
529 int count = 200; /* wait at most 20 seconds for completion */
530 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
532 unsigned serial = 0, i;
536 strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
537 strcpy(supplicant_prop_name, P2P_PROP_NAME);
539 /* Ensure p2p config file is created */
540 if (ensure_config_file_exists(P2P_CONFIG_FILE) < 0) {
541 ALOGE("Failed to create a p2p config file");
546 strcpy(supplicant_name, SUPPLICANT_NAME);
547 strcpy(supplicant_prop_name, SUPP_PROP_NAME);
550 /* Check whether already running */
551 if (property_get(supplicant_name, supp_status, NULL)
552 && strcmp(supp_status, "running") == 0) {
556 /* Before starting the daemon, make sure its config file exists */
557 if (ensure_config_file_exists(SUPP_CONFIG_FILE) < 0) {
558 ALOGE("Wi-Fi will not be enabled");
562 if (ensure_entropy_file_exists() < 0) {
563 ALOGE("Wi-Fi entropy file was not created");
566 /* Clear out any stale socket files that might be left over. */
567 wifi_wpa_ctrl_cleanup();
569 /* Reset sockets used for exiting from hung state */
570 for (i=0; i<MAX_CONNS; i++) {
571 exit_sockets[i][0] = exit_sockets[i][1] = -1;
574 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
576 * Get a reference to the status property, so we can distinguish
577 * the case where it goes stopped => running => stopped (i.e.,
578 * it start up, but fails right away) from the case in which
579 * it starts in the stopped state and never manages to start
582 pi = __system_property_find(supplicant_prop_name);
587 property_get("wifi.interface", primary_iface, WIFI_TEST_INTERFACE);
589 property_set("ctl.start", supplicant_name);
592 while (count-- > 0) {
593 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
595 pi = __system_property_find(supplicant_prop_name);
598 __system_property_read(pi, NULL, supp_status);
599 if (strcmp(supp_status, "running") == 0) {
601 } else if (pi->serial != serial &&
602 strcmp(supp_status, "stopped") == 0) {
607 if (property_get(supplicant_prop_name, supp_status, NULL)) {
608 if (strcmp(supp_status, "running") == 0)
617 int wifi_stop_supplicant(int p2p_supported)
619 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
620 int count = 50; /* wait at most 5 seconds for completion */
623 strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
624 strcpy(supplicant_prop_name, P2P_PROP_NAME);
626 strcpy(supplicant_name, SUPPLICANT_NAME);
627 strcpy(supplicant_prop_name, SUPP_PROP_NAME);
630 /* Check whether supplicant already stopped */
631 if (property_get(supplicant_prop_name, supp_status, NULL)
632 && strcmp(supp_status, "stopped") == 0) {
636 property_set("ctl.stop", supplicant_name);
639 while (count-- > 0) {
640 if (property_get(supplicant_prop_name, supp_status, NULL)) {
641 if (strcmp(supp_status, "stopped") == 0)
646 ALOGE("Failed to stop supplicant");
650 int wifi_connect_on_socket_path(int index, const char *path)
652 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
654 /* Make sure supplicant is running */
655 if (!property_get(supplicant_prop_name, supp_status, NULL)
656 || strcmp(supp_status, "running") != 0) {
657 ALOGE("Supplicant not running, cannot connect");
661 ctrl_conn[index] = wpa_ctrl_open(path);
662 if (ctrl_conn[index] == NULL) {
663 ALOGE("Unable to open connection to supplicant on \"%s\": %s",
664 path, strerror(errno));
667 monitor_conn[index] = wpa_ctrl_open(path);
668 if (monitor_conn[index] == NULL) {
669 wpa_ctrl_close(ctrl_conn[index]);
670 ctrl_conn[index] = NULL;
673 if (wpa_ctrl_attach(monitor_conn[index]) != 0) {
674 wpa_ctrl_close(monitor_conn[index]);
675 wpa_ctrl_close(ctrl_conn[index]);
676 ctrl_conn[index] = monitor_conn[index] = NULL;
680 if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets[index]) == -1) {
681 wpa_ctrl_close(monitor_conn[index]);
682 wpa_ctrl_close(ctrl_conn[index]);
683 ctrl_conn[index] = monitor_conn[index] = NULL;
690 /* Establishes the control and monitor socket connections on the interface */
691 int wifi_connect_to_supplicant(const char *ifname)
695 if (is_primary_interface(ifname)) {
696 if (access(IFACE_DIR, F_OK) == 0) {
697 snprintf(path, sizeof(path), "%s/%s", IFACE_DIR, primary_iface);
699 strlcpy(path, primary_iface, sizeof(path));
701 return wifi_connect_on_socket_path(PRIMARY, path);
703 sprintf(path, "%s/%s", CONTROL_IFACE_PATH, ifname);
704 return wifi_connect_on_socket_path(SECONDARY, path);
708 int wifi_send_command(int index, const char *cmd, char *reply, size_t *reply_len)
712 if (ctrl_conn[index] == NULL) {
713 ALOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
716 ret = wpa_ctrl_request(ctrl_conn[index], cmd, strlen(cmd), reply, reply_len, NULL);
718 ALOGD("'%s' command timed out.\n", cmd);
719 /* unblocks the monitor receive socket for termination */
720 TEMP_FAILURE_RETRY(write(exit_sockets[index][0], "T", 1));
722 } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
725 if (strncmp(cmd, "PING", 4) == 0) {
726 reply[*reply_len] = '\0';
731 int wifi_ctrl_recv(int index, char *reply, size_t *reply_len)
734 int ctrlfd = wpa_ctrl_get_fd(monitor_conn[index]);
735 struct pollfd rfds[2];
737 memset(rfds, 0, 2 * sizeof(struct pollfd));
739 rfds[0].events |= POLLIN;
740 rfds[1].fd = exit_sockets[index][1];
741 rfds[1].events |= POLLIN;
742 res = TEMP_FAILURE_RETRY(poll(rfds, 2, -1));
744 ALOGE("Error poll = %d", res);
747 if (rfds[0].revents & POLLIN) {
748 return wpa_ctrl_recv(monitor_conn[index], reply, reply_len);
749 } else if (rfds[1].revents & POLLIN) {
750 /* Close only the p2p sockets on receive side
751 * see wifi_close_supplicant_connection()
753 if (index == SECONDARY) {
754 ALOGD("close sockets %d", index);
755 wifi_close_sockets(index);
761 int wifi_wait_on_socket(int index, char *buf, size_t buflen)
763 size_t nread = buflen - 1;
766 if (monitor_conn[index] == NULL) {
767 ALOGD("Connection closed\n");
768 strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1);
769 buf[buflen-1] = '\0';
773 result = wifi_ctrl_recv(index, buf, &nread);
775 /* Terminate reception on exit socket */
777 strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1);
778 buf[buflen-1] = '\0';
783 ALOGD("wifi_ctrl_recv failed: %s\n", strerror(errno));
784 strncpy(buf, WPA_EVENT_TERMINATING " - recv error", buflen-1);
785 buf[buflen-1] = '\0';
789 /* Check for EOF on the socket */
790 if (result == 0 && nread == 0) {
791 /* Fabricate an event to pass up */
792 ALOGD("Received EOF on supplicant socket\n");
793 strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
794 buf[buflen-1] = '\0';
798 * Events strings are in the format
802 * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
803 * etc.) and XXX is the event name. The level information is not useful
804 * to us, so strip it off.
807 char *match = strchr(buf, '>');
809 nread -= (match+1-buf);
810 memmove(buf, match+1, nread+1);
817 int wifi_wait_for_event(const char *ifname, char *buf, size_t buflen)
819 if (is_primary_interface(ifname)) {
820 return wifi_wait_on_socket(PRIMARY, buf, buflen);
822 return wifi_wait_on_socket(SECONDARY, buf, buflen);
826 void wifi_close_sockets(int index)
828 if (ctrl_conn[index] != NULL) {
829 wpa_ctrl_close(ctrl_conn[index]);
830 ctrl_conn[index] = NULL;
833 if (monitor_conn[index] != NULL) {
834 wpa_ctrl_close(monitor_conn[index]);
835 monitor_conn[index] = NULL;
838 if (exit_sockets[index][0] >= 0) {
839 close(exit_sockets[index][0]);
840 exit_sockets[index][0] = -1;
843 if (exit_sockets[index][1] >= 0) {
844 close(exit_sockets[index][1]);
845 exit_sockets[index][1] = -1;
849 void wifi_close_supplicant_connection(const char *ifname)
851 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
852 int count = 50; /* wait at most 5 seconds to ensure init has stopped stupplicant */
854 if (is_primary_interface(ifname)) {
855 wifi_close_sockets(PRIMARY);
857 /* p2p socket termination needs unblocking the monitor socket
858 * STA connection does not need it since supplicant gets shutdown
860 TEMP_FAILURE_RETRY(write(exit_sockets[SECONDARY][0], "T", 1));
861 /* p2p sockets are closed after the monitor thread
862 * receives the terminate on the exit socket
867 while (count-- > 0) {
868 if (property_get(supplicant_prop_name, supp_status, NULL)) {
869 if (strcmp(supp_status, "stopped") == 0)
876 int wifi_command(const char *ifname, const char *command, char *reply, size_t *reply_len)
878 if (is_primary_interface(ifname)) {
879 return wifi_send_command(PRIMARY, command, reply, reply_len);
881 return wifi_send_command(SECONDARY, command, reply, reply_len);
885 const char *wifi_get_fw_path(int fw_type)
888 case WIFI_GET_FW_PATH_STA:
889 return WIFI_DRIVER_FW_PATH_STA;
890 case WIFI_GET_FW_PATH_AP:
891 return WIFI_DRIVER_FW_PATH_AP;
892 case WIFI_GET_FW_PATH_P2P:
893 return WIFI_DRIVER_FW_PATH_P2P;
898 int wifi_change_fw_path(const char *fwpath)
906 fd = TEMP_FAILURE_RETRY(open(WIFI_DRIVER_FW_PATH_PARAM, O_WRONLY));
908 ALOGE("Failed to open wlan fw path param (%s)", strerror(errno));
911 len = strlen(fwpath) + 1;
912 if (TEMP_FAILURE_RETRY(write(fd, fwpath, len)) != len) {
913 ALOGE("Failed to write wlan fw path param (%s)", strerror(errno));