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 "hardware_legacy/wifi.h"
23 #include "libwpa_client/wpa_ctrl.h"
25 #define LOG_TAG "WifiHW"
26 #include "cutils/log.h"
27 #include "cutils/memory.h"
28 #include "cutils/misc.h"
29 #include "cutils/properties.h"
30 #include "private/android_filesystem_config.h"
31 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
32 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
33 #include <sys/_system_properties.h>
36 static struct wpa_ctrl *ctrl_conn;
37 static struct wpa_ctrl *monitor_conn;
40 extern int ifc_init();
41 extern void ifc_close();
42 extern char *dhcp_lasterror();
43 extern void get_dhcp_info();
44 extern int init_module(void *, unsigned long, const char *);
45 extern int delete_module(const char *, unsigned int);
47 static char iface[PROPERTY_VALUE_MAX];
48 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
51 #ifndef WIFI_DRIVER_MODULE_PATH
52 #define WIFI_DRIVER_MODULE_PATH "/system/lib/modules/wlan.ko"
54 #ifndef WIFI_DRIVER_MODULE_NAME
55 #define WIFI_DRIVER_MODULE_NAME "wlan"
57 #ifndef WIFI_DRIVER_MODULE_ARG
58 #define WIFI_DRIVER_MODULE_ARG ""
60 #ifndef WIFI_FIRMWARE_LOADER
61 #define WIFI_FIRMWARE_LOADER ""
63 #define WIFI_TEST_INTERFACE "sta"
65 #define WIFI_DRIVER_LOADER_DELAY 1000000
67 static const char IFACE_DIR[] = "/data/system/wpa_supplicant";
68 static const char DRIVER_MODULE_NAME[] = WIFI_DRIVER_MODULE_NAME;
69 static const char DRIVER_MODULE_TAG[] = WIFI_DRIVER_MODULE_NAME " ";
70 static const char DRIVER_MODULE_PATH[] = WIFI_DRIVER_MODULE_PATH;
71 static const char DRIVER_MODULE_ARG[] = WIFI_DRIVER_MODULE_ARG;
72 static const char FIRMWARE_LOADER[] = WIFI_FIRMWARE_LOADER;
73 static const char DRIVER_PROP_NAME[] = "wlan.driver.status";
74 static const char SUPPLICANT_NAME[] = "wpa_supplicant";
75 static const char SUPP_PROP_NAME[] = "init.svc.wpa_supplicant";
76 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
77 static const char SUPP_CONFIG_FILE[] = "/data/misc/wifi/wpa_supplicant.conf";
78 static const char MODULE_FILE[] = "/proc/modules";
80 static int insmod(const char *filename, const char *args)
86 module = load_file(filename, &size);
90 ret = init_module(module, size, args);
97 static int rmmod(const char *modname)
102 while (maxtry-- > 0) {
103 ret = delete_module(modname, O_NONBLOCK | O_EXCL);
104 if (ret < 0 && errno == EAGAIN)
111 LOGD("Unable to unload driver module \"%s\": %s\n",
112 modname, strerror(errno));
116 int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
117 int *dns1, int *dns2, int *server, int *lease) {
118 /* For test driver, always report success */
119 if (strcmp(iface, WIFI_TEST_INTERFACE) == 0)
125 if (do_dhcp(iface) < 0) {
130 get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
134 const char *get_dhcp_error_string() {
135 return dhcp_lasterror();
138 static int check_driver_loaded() {
139 char driver_status[PROPERTY_VALUE_MAX];
141 char line[sizeof(DRIVER_MODULE_TAG)+10];
143 if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
144 || strcmp(driver_status, "ok") != 0) {
145 return 0; /* driver not loaded */
148 * If the property says the driver is loaded, check to
149 * make sure that the property setting isn't just left
150 * over from a previous manual shutdown or a runtime
153 if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
154 LOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
155 property_set(DRIVER_PROP_NAME, "unloaded");
158 while ((fgets(line, sizeof(line), proc)) != NULL) {
159 if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
165 property_set(DRIVER_PROP_NAME, "unloaded");
169 int wifi_load_driver()
171 char driver_status[PROPERTY_VALUE_MAX];
172 int count = 100; /* wait at most 20 seconds for completion */
174 if (check_driver_loaded()) {
178 if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0)
181 if (strcmp(FIRMWARE_LOADER,"") == 0) {
182 usleep(WIFI_DRIVER_LOADER_DELAY);
183 property_set(DRIVER_PROP_NAME, "ok");
186 property_set("ctl.start", FIRMWARE_LOADER);
189 while (count-- > 0) {
190 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
191 if (strcmp(driver_status, "ok") == 0)
193 else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
194 wifi_unload_driver();
200 property_set(DRIVER_PROP_NAME, "timeout");
201 wifi_unload_driver();
205 int wifi_unload_driver()
207 int count = 20; /* wait at most 10 seconds for completion */
209 if (rmmod(DRIVER_MODULE_NAME) == 0) {
210 while (count-- > 0) {
211 if (!check_driver_loaded())
223 int ensure_config_file_exists()
229 if (access(SUPP_CONFIG_FILE, R_OK|W_OK) == 0) {
231 } else if (errno != ENOENT) {
232 LOGE("Cannot access \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
236 srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
238 LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
242 destfd = open(SUPP_CONFIG_FILE, O_CREAT|O_WRONLY, 0660);
245 LOGE("Cannot create \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
249 while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
251 LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
254 unlink(SUPP_CONFIG_FILE);
257 write(destfd, buf, nread);
263 if (chown(SUPP_CONFIG_FILE, AID_SYSTEM, AID_WIFI) < 0) {
264 LOGE("Error changing group ownership of %s to %d: %s",
265 SUPP_CONFIG_FILE, AID_WIFI, strerror(errno));
266 unlink(SUPP_CONFIG_FILE);
272 int wifi_start_supplicant()
274 char daemon_cmd[PROPERTY_VALUE_MAX];
275 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
276 int count = 200; /* wait at most 20 seconds for completion */
277 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
282 /* Check whether already running */
283 if (property_get(SUPP_PROP_NAME, supp_status, NULL)
284 && strcmp(supp_status, "running") == 0) {
288 /* Before starting the daemon, make sure its config file exists */
289 if (ensure_config_file_exists() < 0) {
290 LOGE("Wi-Fi will not be enabled");
294 /* Clear out any stale socket files that might be left over. */
297 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
299 * Get a reference to the status property, so we can distinguish
300 * the case where it goes stopped => running => stopped (i.e.,
301 * it start up, but fails right away) from the case in which
302 * it starts in the stopped state and never manages to start
305 pi = __system_property_find(SUPP_PROP_NAME);
310 property_get("wifi.interface", iface, WIFI_TEST_INTERFACE);
311 snprintf(daemon_cmd, PROPERTY_VALUE_MAX, "%s:-i%s", SUPPLICANT_NAME, iface);
312 property_set("ctl.start", daemon_cmd);
315 while (count-- > 0) {
316 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
318 pi = __system_property_find(SUPP_PROP_NAME);
321 __system_property_read(pi, NULL, supp_status);
322 if (strcmp(supp_status, "running") == 0) {
324 } else if (pi->serial != serial &&
325 strcmp(supp_status, "stopped") == 0) {
330 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
331 if (strcmp(supp_status, "running") == 0)
340 int wifi_stop_supplicant()
342 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
343 int count = 50; /* wait at most 5 seconds for completion */
345 /* Check whether supplicant already stopped */
346 if (property_get(SUPP_PROP_NAME, supp_status, NULL)
347 && strcmp(supp_status, "stopped") == 0) {
351 property_set("ctl.stop", SUPPLICANT_NAME);
354 while (count-- > 0) {
355 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
356 if (strcmp(supp_status, "stopped") == 0)
364 int wifi_connect_to_supplicant()
367 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
369 /* Make sure supplicant is running */
370 if (!property_get(SUPP_PROP_NAME, supp_status, NULL)
371 || strcmp(supp_status, "running") != 0) {
372 LOGE("Supplicant not running, cannot connect");
376 if (access(IFACE_DIR, F_OK) == 0) {
377 snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface);
379 strlcpy(ifname, iface, sizeof(ifname));
382 ctrl_conn = wpa_ctrl_open(ifname);
383 if (ctrl_conn == NULL) {
384 LOGE("Unable to open connection to supplicant on \"%s\": %s",
385 ifname, strerror(errno));
388 monitor_conn = wpa_ctrl_open(ifname);
389 if (monitor_conn == NULL) {
390 wpa_ctrl_close(ctrl_conn);
394 if (wpa_ctrl_attach(monitor_conn) != 0) {
395 wpa_ctrl_close(monitor_conn);
396 wpa_ctrl_close(ctrl_conn);
397 ctrl_conn = monitor_conn = NULL;
403 int wifi_send_command(struct wpa_ctrl *ctrl, const char *cmd, char *reply, size_t *reply_len)
407 if (ctrl_conn == NULL) {
408 LOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
411 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), reply, reply_len, NULL);
413 LOGD("'%s' command timed out.\n", cmd);
415 } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
418 if (strncmp(cmd, "PING", 4) == 0) {
419 reply[*reply_len] = '\0';
424 int wifi_wait_for_event(char *buf, size_t buflen)
426 size_t nread = buflen - 1;
431 struct timeval *tptr;
433 if (monitor_conn == NULL) {
434 LOGD("Connection closed\n");
435 strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1);
436 buf[buflen-1] = '\0';
440 result = wpa_ctrl_recv(monitor_conn, buf, &nread);
442 LOGD("wpa_ctrl_recv failed: %s\n", strerror(errno));
443 strncpy(buf, WPA_EVENT_TERMINATING " - recv error", buflen-1);
444 buf[buflen-1] = '\0';
448 /* LOGD("wait_for_event: result=%d nread=%d string=\"%s\"\n", result, nread, buf); */
449 /* Check for EOF on the socket */
450 if (result == 0 && nread == 0) {
451 /* Fabricate an event to pass up */
452 LOGD("Received EOF on supplicant socket\n");
453 strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
454 buf[buflen-1] = '\0';
458 * Events strings are in the format
462 * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
463 * etc.) and XXX is the event name. The level information is not useful
464 * to us, so strip it off.
467 char *match = strchr(buf, '>');
469 nread -= (match+1-buf);
470 memmove(buf, match+1, nread+1);
476 void wifi_close_supplicant_connection()
478 if (ctrl_conn != NULL) {
479 wpa_ctrl_close(ctrl_conn);
482 if (monitor_conn != NULL) {
483 wpa_ctrl_close(monitor_conn);
488 int wifi_command(const char *command, char *reply, size_t *reply_len)
490 return wifi_send_command(ctrl_conn, command, reply, reply_len);