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.
23 #include "hardware_legacy/wifi.h"
24 #include "libwpa_client/wpa_ctrl.h"
26 #define LOG_TAG "WifiHW"
27 #include "cutils/log.h"
28 #include "cutils/memory.h"
29 #include "cutils/misc.h"
30 #include "cutils/properties.h"
31 #include "private/android_filesystem_config.h"
32 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
33 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
34 #include <sys/_system_properties.h>
37 static struct wpa_ctrl *ctrl_conn;
38 static struct wpa_ctrl *monitor_conn;
41 extern int ifc_init();
42 extern void ifc_close();
43 extern char *dhcp_lasterror();
44 extern void get_dhcp_info();
45 extern int init_module(void *, unsigned long, const char *);
46 extern int delete_module(const char *, unsigned int);
48 static char iface[PROPERTY_VALUE_MAX];
49 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
52 #ifndef WIFI_DRIVER_MODULE_ARG
53 #define WIFI_DRIVER_MODULE_ARG ""
55 #ifndef WIFI_FIRMWARE_LOADER
56 #define WIFI_FIRMWARE_LOADER ""
58 #define WIFI_TEST_INTERFACE "sta"
60 #ifndef WIFI_DRIVER_FW_PATH_STA
61 #define WIFI_DRIVER_FW_PATH_STA NULL
63 #ifndef WIFI_DRIVER_FW_PATH_AP
64 #define WIFI_DRIVER_FW_PATH_AP NULL
66 #ifndef WIFI_DRIVER_FW_PATH_P2P
67 #define WIFI_DRIVER_FW_PATH_P2P NULL
70 #ifndef WIFI_DRIVER_FW_PATH_PARAM
71 #define WIFI_DRIVER_FW_PATH_PARAM "/sys/module/wlan/parameters/fwpath"
74 #define WIFI_DRIVER_LOADER_DELAY 1000000
76 static const char IFACE_DIR[] = "/data/system/wpa_supplicant";
77 #ifdef WIFI_DRIVER_MODULE_PATH
78 static const char DRIVER_MODULE_NAME[] = WIFI_DRIVER_MODULE_NAME;
79 static const char DRIVER_MODULE_TAG[] = WIFI_DRIVER_MODULE_NAME " ";
80 static const char DRIVER_MODULE_PATH[] = WIFI_DRIVER_MODULE_PATH;
81 static const char DRIVER_MODULE_ARG[] = WIFI_DRIVER_MODULE_ARG;
83 static const char FIRMWARE_LOADER[] = WIFI_FIRMWARE_LOADER;
84 static const char DRIVER_PROP_NAME[] = "wlan.driver.status";
85 static const char SUPPLICANT_NAME[] = "wpa_supplicant";
86 static const char SUPP_PROP_NAME[] = "init.svc.wpa_supplicant";
87 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
88 static const char SUPP_CONFIG_FILE[] = "/data/misc/wifi/wpa_supplicant.conf";
89 static const char P2P_CONFIG_FILE[] = "/data/misc/wifi/p2p_supplicant.conf";
90 static const char CONTROL_IFACE_PATH[] = "/data/misc/wifi";
91 static const char MODULE_FILE[] = "/proc/modules";
93 static const char SUPP_ENTROPY_FILE[] = WIFI_ENTROPY_FILE;
94 static unsigned char dummy_key[20] = { 0x11, 0xbe, 0x33, 0x43, 0x35,
95 0x68, 0x47, 0x84, 0x99, 0xa9,
96 0x2b, 0x1c, 0xd3, 0xee, 0xff,
97 0xf1, 0xe2, 0xf3, 0xf4, 0xf5 };
99 static int insmod(const char *filename, const char *args)
105 module = load_file(filename, &size);
109 ret = init_module(module, size, args);
116 static int rmmod(const char *modname)
121 while (maxtry-- > 0) {
122 ret = delete_module(modname, O_NONBLOCK | O_EXCL);
123 if (ret < 0 && errno == EAGAIN)
130 LOGD("Unable to unload driver module \"%s\": %s\n",
131 modname, strerror(errno));
135 int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
136 int *dns1, int *dns2, int *server, int *lease) {
137 /* For test driver, always report success */
138 if (strcmp(iface, WIFI_TEST_INTERFACE) == 0)
144 if (do_dhcp(iface) < 0) {
149 get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
153 const char *get_dhcp_error_string() {
154 return dhcp_lasterror();
157 int is_wifi_driver_loaded() {
158 char driver_status[PROPERTY_VALUE_MAX];
159 #ifdef WIFI_DRIVER_MODULE_PATH
161 char line[sizeof(DRIVER_MODULE_TAG)+10];
164 if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
165 || strcmp(driver_status, "ok") != 0) {
166 return 0; /* driver not loaded */
168 #ifdef WIFI_DRIVER_MODULE_PATH
170 * If the property says the driver is loaded, check to
171 * make sure that the property setting isn't just left
172 * over from a previous manual shutdown or a runtime
175 if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
176 LOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
177 property_set(DRIVER_PROP_NAME, "unloaded");
180 while ((fgets(line, sizeof(line), proc)) != NULL) {
181 if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
187 property_set(DRIVER_PROP_NAME, "unloaded");
194 int wifi_load_driver()
196 #ifdef WIFI_DRIVER_MODULE_PATH
197 char driver_status[PROPERTY_VALUE_MAX];
198 int count = 100; /* wait at most 20 seconds for completion */
200 if (is_wifi_driver_loaded()) {
204 if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0)
207 if (strcmp(FIRMWARE_LOADER,"") == 0) {
208 /* usleep(WIFI_DRIVER_LOADER_DELAY); */
209 property_set(DRIVER_PROP_NAME, "ok");
212 property_set("ctl.start", FIRMWARE_LOADER);
215 while (count-- > 0) {
216 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
217 if (strcmp(driver_status, "ok") == 0)
219 else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
220 wifi_unload_driver();
226 property_set(DRIVER_PROP_NAME, "timeout");
227 wifi_unload_driver();
230 property_set(DRIVER_PROP_NAME, "ok");
235 int wifi_unload_driver()
237 usleep(200000); /* allow to finish interface down */
238 #ifdef WIFI_DRIVER_MODULE_PATH
239 if (rmmod(DRIVER_MODULE_NAME) == 0) {
240 int count = 20; /* wait at most 10 seconds for completion */
241 while (count-- > 0) {
242 if (!is_wifi_driver_loaded())
246 usleep(500000); /* allow card removal */
254 property_set(DRIVER_PROP_NAME, "unloaded");
259 int ensure_entropy_file_exists()
264 ret = access(SUPP_ENTROPY_FILE, R_OK|W_OK);
265 if ((ret == 0) || (errno == EACCES)) {
267 (chmod(SUPP_ENTROPY_FILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
268 LOGE("Cannot set RW to \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
273 destfd = open(SUPP_ENTROPY_FILE, O_CREAT|O_RDWR, 0660);
275 LOGE("Cannot create \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
279 if (write(destfd, dummy_key, sizeof(dummy_key)) != sizeof(dummy_key)) {
280 LOGE("Error writing \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
286 /* chmod is needed because open() didn't set permisions properly */
287 if (chmod(SUPP_ENTROPY_FILE, 0660) < 0) {
288 LOGE("Error changing permissions of %s to 0660: %s",
289 SUPP_ENTROPY_FILE, strerror(errno));
290 unlink(SUPP_ENTROPY_FILE);
294 if (chown(SUPP_ENTROPY_FILE, AID_SYSTEM, AID_WIFI) < 0) {
295 LOGE("Error changing group ownership of %s to %d: %s",
296 SUPP_ENTROPY_FILE, AID_WIFI, strerror(errno));
297 unlink(SUPP_ENTROPY_FILE);
303 int update_ctrl_interface(const char *config_file) {
307 char ifc[PROPERTY_VALUE_MAX];
312 if (stat(config_file, &sb) != 0)
315 pbuf = malloc(sb.st_size + PROPERTY_VALUE_MAX);
318 srcfd = open(config_file, O_RDONLY);
320 LOGE("Cannot open \"%s\": %s", config_file, strerror(errno));
324 nread = read(srcfd, pbuf, sb.st_size);
327 LOGE("Cannot read \"%s\": %s", config_file, strerror(errno));
332 if (!strcmp(config_file, SUPP_CONFIG_FILE)) {
333 property_get("wifi.interface", ifc, WIFI_TEST_INTERFACE);
335 strcpy(ifc, CONTROL_IFACE_PATH);
337 if ((sptr = strstr(pbuf, "ctrl_interface="))) {
338 char *iptr = sptr + strlen("ctrl_interface=");
340 int mlen = strlen(ifc);
342 if (strncmp(ifc, iptr, mlen) != 0) {
343 LOGE("ctrl_interface != %s", ifc);
344 while (((ilen + (iptr - pbuf)) < nread) && (iptr[ilen] != '\n'))
346 mlen = ((ilen >= mlen) ? ilen : mlen) + 1;
347 memmove(iptr + mlen, iptr + ilen + 1, nread - (iptr + ilen + 1 - pbuf));
348 memset(iptr, '\n', mlen);
349 memcpy(iptr, ifc, strlen(ifc));
350 destfd = open(config_file, O_RDWR, 0660);
352 LOGE("Cannot update \"%s\": %s", config_file, strerror(errno));
356 write(destfd, pbuf, nread + mlen - ilen -1);
364 int ensure_config_file_exists(const char *config_file)
372 ret = access(config_file, R_OK|W_OK);
373 if ((ret == 0) || (errno == EACCES)) {
375 (chmod(config_file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
376 LOGE("Cannot set RW to \"%s\": %s", config_file, strerror(errno));
379 /* return if filesize is at least 10 bytes */
380 if (stat(config_file, &sb) == 0 && sb.st_size > 10) {
381 return update_ctrl_interface(config_file);
383 } else if (errno != ENOENT) {
384 LOGE("Cannot access \"%s\": %s", config_file, strerror(errno));
388 srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
390 LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
394 destfd = open(config_file, O_CREAT|O_RDWR, 0660);
397 LOGE("Cannot create \"%s\": %s", config_file, strerror(errno));
401 while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
403 LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
409 write(destfd, buf, nread);
415 /* chmod is needed because open() didn't set permisions properly */
416 if (chmod(config_file, 0660) < 0) {
417 LOGE("Error changing permissions of %s to 0660: %s",
418 config_file, strerror(errno));
423 if (chown(config_file, AID_SYSTEM, AID_WIFI) < 0) {
424 LOGE("Error changing group ownership of %s to %d: %s",
425 config_file, AID_WIFI, strerror(errno));
429 return update_ctrl_interface(config_file);
433 * wifi_wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
434 * may be left over from clients that were previously connected to
435 * wpa_supplicant. This keeps these files from being orphaned in the
436 * event of crashes that prevented them from being removed as part
437 * of the normal orderly shutdown.
439 void wifi_wpa_ctrl_cleanup(void)
443 struct dirent *result;
446 char pathname[PATH_MAX];
448 char *local_socket_dir = CONFIG_CTRL_IFACE_CLIENT_DIR;
449 char *local_socket_prefix = CONFIG_CTRL_IFACE_CLIENT_PREFIX;
451 if ((dir = opendir(local_socket_dir)) == NULL)
454 dirnamelen = (size_t)snprintf(pathname, sizeof(pathname), "%s/", local_socket_dir);
455 if (dirnamelen >= sizeof(pathname)) {
459 namep = pathname + dirnamelen;
460 maxcopy = PATH_MAX - dirnamelen;
461 while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
462 if (strncmp(entry.d_name, local_socket_prefix, strlen(local_socket_prefix)) == 0) {
463 if (strlcpy(namep, entry.d_name, maxcopy) < maxcopy) {
471 int wifi_start_supplicant_common(const char *config_file)
473 char daemon_cmd[PROPERTY_VALUE_MAX];
474 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
475 int count = 200; /* wait at most 20 seconds for completion */
476 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
481 /* Check whether already running */
482 if (property_get(SUPP_PROP_NAME, supp_status, NULL)
483 && strcmp(supp_status, "running") == 0) {
487 /* Before starting the daemon, make sure its config file exists */
488 if (ensure_config_file_exists(config_file) < 0) {
489 LOGE("Wi-Fi will not be enabled");
493 if (ensure_entropy_file_exists() < 0) {
494 LOGE("Wi-Fi entropy file was not created");
497 /* Clear out any stale socket files that might be left over. */
498 wifi_wpa_ctrl_cleanup();
500 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
502 * Get a reference to the status property, so we can distinguish
503 * the case where it goes stopped => running => stopped (i.e.,
504 * it start up, but fails right away) from the case in which
505 * it starts in the stopped state and never manages to start
508 pi = __system_property_find(SUPP_PROP_NAME);
513 property_get("wifi.interface", iface, WIFI_TEST_INTERFACE);
514 snprintf(daemon_cmd, PROPERTY_VALUE_MAX, "%s:-i%s -c%s", SUPPLICANT_NAME, iface, config_file);
515 property_set("ctl.start", daemon_cmd);
518 while (count-- > 0) {
519 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
521 pi = __system_property_find(SUPP_PROP_NAME);
524 __system_property_read(pi, NULL, supp_status);
525 if (strcmp(supp_status, "running") == 0) {
527 } else if (pi->serial != serial &&
528 strcmp(supp_status, "stopped") == 0) {
533 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
534 if (strcmp(supp_status, "running") == 0)
543 int wifi_start_supplicant()
545 return wifi_start_supplicant_common(SUPP_CONFIG_FILE);
548 int wifi_start_p2p_supplicant()
550 return wifi_start_supplicant_common(P2P_CONFIG_FILE);
553 int wifi_stop_supplicant()
555 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
556 int count = 50; /* wait at most 5 seconds for completion */
558 /* Check whether supplicant already stopped */
559 if (property_get(SUPP_PROP_NAME, supp_status, NULL)
560 && strcmp(supp_status, "stopped") == 0) {
564 property_set("ctl.stop", SUPPLICANT_NAME);
567 while (count-- > 0) {
568 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
569 if (strcmp(supp_status, "stopped") == 0)
577 int wifi_connect_to_supplicant()
580 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
582 /* Make sure supplicant is running */
583 if (!property_get(SUPP_PROP_NAME, supp_status, NULL)
584 || strcmp(supp_status, "running") != 0) {
585 LOGE("Supplicant not running, cannot connect");
589 if (access(IFACE_DIR, F_OK) == 0) {
590 snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface);
592 strlcpy(ifname, iface, sizeof(ifname));
595 ctrl_conn = wpa_ctrl_open(ifname);
596 if (ctrl_conn == NULL) {
597 LOGE("Unable to open connection to supplicant on \"%s\": %s",
598 ifname, strerror(errno));
601 monitor_conn = wpa_ctrl_open(ifname);
602 if (monitor_conn == NULL) {
603 wpa_ctrl_close(ctrl_conn);
607 if (wpa_ctrl_attach(monitor_conn) != 0) {
608 wpa_ctrl_close(monitor_conn);
609 wpa_ctrl_close(ctrl_conn);
610 ctrl_conn = monitor_conn = NULL;
616 int wifi_send_command(struct wpa_ctrl *ctrl, const char *cmd, char *reply, size_t *reply_len)
620 if (ctrl_conn == NULL) {
621 LOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
624 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), reply, reply_len, NULL);
626 LOGD("'%s' command timed out.\n", cmd);
628 } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
631 if (strncmp(cmd, "PING", 4) == 0) {
632 reply[*reply_len] = '\0';
637 int wifi_wait_for_event(char *buf, size_t buflen)
639 size_t nread = buflen - 1;
644 struct timeval *tptr;
646 if (monitor_conn == NULL) {
647 LOGD("Connection closed\n");
648 strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1);
649 buf[buflen-1] = '\0';
653 result = wpa_ctrl_recv(monitor_conn, buf, &nread);
655 LOGD("wpa_ctrl_recv failed: %s\n", strerror(errno));
656 strncpy(buf, WPA_EVENT_TERMINATING " - recv error", buflen-1);
657 buf[buflen-1] = '\0';
661 /* LOGD("wait_for_event: result=%d nread=%d string=\"%s\"\n", result, nread, buf); */
662 /* Check for EOF on the socket */
663 if (result == 0 && nread == 0) {
664 /* Fabricate an event to pass up */
665 LOGD("Received EOF on supplicant socket\n");
666 strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
667 buf[buflen-1] = '\0';
671 * Events strings are in the format
675 * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
676 * etc.) and XXX is the event name. The level information is not useful
677 * to us, so strip it off.
680 char *match = strchr(buf, '>');
682 nread -= (match+1-buf);
683 memmove(buf, match+1, nread+1);
689 void wifi_close_supplicant_connection()
691 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
692 int count = 50; /* wait at most 5 seconds to ensure init has stopped stupplicant */
694 if (ctrl_conn != NULL) {
695 wpa_ctrl_close(ctrl_conn);
698 if (monitor_conn != NULL) {
699 wpa_ctrl_close(monitor_conn);
703 while (count-- > 0) {
704 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
705 if (strcmp(supp_status, "stopped") == 0)
712 int wifi_command(const char *command, char *reply, size_t *reply_len)
714 return wifi_send_command(ctrl_conn, command, reply, reply_len);
717 const char *wifi_get_fw_path(int fw_type)
720 case WIFI_GET_FW_PATH_STA:
721 return WIFI_DRIVER_FW_PATH_STA;
722 case WIFI_GET_FW_PATH_AP:
723 return WIFI_DRIVER_FW_PATH_AP;
724 case WIFI_GET_FW_PATH_P2P:
725 return WIFI_DRIVER_FW_PATH_P2P;
730 int wifi_change_fw_path(const char *fwpath)
738 fd = open(WIFI_DRIVER_FW_PATH_PARAM, O_WRONLY);
740 LOGE("Failed to open wlan fw path param (%s)", strerror(errno));
743 len = strlen(fwpath) + 1;
744 if (write(fd, fwpath, len) != len) {
745 LOGE("Failed to write wlan fw path param (%s)", strerror(errno));