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"
32 static struct wpa_ctrl *ctrl_conn;
33 static struct wpa_ctrl *monitor_conn;
36 extern int ifc_init();
37 extern void ifc_close();
38 extern char *dhcp_lasterror();
39 extern void get_dhcp_info();
40 extern int init_module(void *, unsigned long, const char *);
41 extern int delete_module(const char *, unsigned int);
43 static char iface[PROPERTY_VALUE_MAX];
44 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
47 static const char IFACE_DIR[] = "/data/system/wpa_supplicant";
48 static const char DRIVER_MODULE_NAME[] = "wlan";
49 static const char DRIVER_MODULE_TAG[] = "wlan ";
50 static const char DRIVER_MODULE_PATH[] = "/system/lib/modules/wlan.ko";
51 static const char FIRMWARE_LOADER[] = "wlan_loader";
52 static const char DRIVER_PROP_NAME[] = "wlan.driver.status";
53 static const char SUPPLICANT_NAME[] = "wpa_supplicant";
54 static const char SUPP_PROP_NAME[] = "init.svc.wpa_supplicant";
55 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
56 static const char SUPP_CONFIG_FILE[] = "/data/misc/wifi/wpa_supplicant.conf";
57 static const char MODULE_FILE[] = "/proc/modules";
59 static int insmod(const char *filename)
65 module = load_file(filename, &size);
69 ret = init_module(module, size, "");
76 static int rmmod(const char *modname)
81 while (maxtry-- > 0) {
82 ret = delete_module(modname, O_NONBLOCK | O_EXCL);
83 if (ret < 0 && errno == EAGAIN)
90 LOGD("Unable to unload driver module \"%s\": %s\n",
91 modname, strerror(errno));
95 int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
96 int *dns1, int *dns2, int *server, int *lease) {
97 /* For test driver, always report success */
98 if (strcmp(iface, "sta") == 0)
104 if (do_dhcp(iface) < 0) {
109 get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
113 const char *get_dhcp_error_string() {
114 return dhcp_lasterror();
117 static int check_driver_loaded() {
118 char driver_status[PROPERTY_VALUE_MAX];
120 char line[sizeof(DRIVER_MODULE_TAG)+10];
122 if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
123 || strcmp(driver_status, "ok") != 0) {
124 return 0; /* driver not loaded */
127 * If the property says the driver is loaded, check to
128 * make sure that the property setting isn't just left
129 * over from a previous manual shutdown or a runtime
132 if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
133 LOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
134 property_set(DRIVER_PROP_NAME, "unloaded");
137 while ((fgets(line, sizeof(line), proc)) != NULL) {
138 if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
144 property_set(DRIVER_PROP_NAME, "unloaded");
148 int wifi_load_driver()
150 char driver_status[PROPERTY_VALUE_MAX];
151 int count = 100; /* wait at most 20 seconds for completion */
153 if (check_driver_loaded()) {
157 if (insmod(DRIVER_MODULE_PATH) < 0)
160 property_set("ctl.start", FIRMWARE_LOADER);
162 while (count-- > 0) {
163 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
164 if (strcmp(driver_status, "ok") == 0)
166 else if (strcmp(DRIVER_PROP_NAME, "failed") == 0)
171 property_set(DRIVER_PROP_NAME, "timeout");
175 int wifi_unload_driver()
177 int count = 20; /* wait at most 10 seconds for completion */
179 if (rmmod(DRIVER_MODULE_NAME) == 0) {
180 while (count-- > 0) {
181 if (!check_driver_loaded())
193 static int control_supplicant(int startIt)
195 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
196 const char *ctrl_prop = (startIt ? "ctl.start" : "ctl.stop");
197 const char *desired_status = (startIt ? "running" : "stopped");
198 int count = 200; /* wait at most 20 seconds for completion */
200 if (property_get(SUPP_PROP_NAME, supp_status, NULL)
201 && strcmp(supp_status, desired_status) == 0) {
202 return 0; /* supplicant already running */
204 property_set(ctrl_prop, SUPPLICANT_NAME);
207 while (count-- > 0) {
208 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
209 if (strcmp(supp_status, desired_status) == 0)
217 int ensure_config_file_exists()
223 if (access(SUPP_CONFIG_FILE, R_OK|W_OK) == 0) {
225 } else if (errno != ENOENT) {
226 LOGE("Cannot access \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
230 srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
232 LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
236 destfd = open(SUPP_CONFIG_FILE, O_CREAT|O_WRONLY, 0660);
239 LOGE("Cannot create \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
243 while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
245 LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
248 unlink(SUPP_CONFIG_FILE);
251 write(destfd, buf, nread);
257 if (chown(SUPP_CONFIG_FILE, AID_SYSTEM, AID_WIFI) < 0) {
258 LOGE("Error changing group ownership of %s to %d: %s",
259 SUPP_CONFIG_FILE, AID_WIFI, strerror(errno));
260 unlink(SUPP_CONFIG_FILE);
266 int wifi_start_supplicant()
268 /* Before starting the daemon, make sure its config file exists */
269 if (ensure_config_file_exists() < 0) {
270 LOGE("Wi-Fi will not be enabled");
273 return control_supplicant(1);
276 int wifi_stop_supplicant()
278 return control_supplicant(0);
281 int wifi_connect_to_supplicant()
284 static int cleaned_up = 0;
286 property_get("wifi.interface", iface, "sta");
288 if (access(IFACE_DIR, F_OK) == 0) {
289 snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface);
291 strlcpy(ifname, iface, sizeof(ifname));
294 ctrl_conn = wpa_ctrl_open(ifname);
295 if (ctrl_conn == NULL) {
296 LOGD("Unable to open connection to supplicant on \"%s\": %s",
297 ifname, strerror(errno));
299 * errno == ENOENT means the supplicant daemon isn't
300 * running. Take this opportunity to clear out any
301 * stale socket files that might be left over. Note
302 * there's a possible race with the command line client
303 * trying to connect to the daemon, but it would require
304 * that the supplicant be started and the command line
305 * client connect to it during the window between the
306 * error check and the removal of the files. And in
307 * any event, the remedy is that the user would simply
308 * have to run the command line program again.
310 if (!cleaned_up && (errno == ENOENT || errno == EADDRINUSE)) {
311 cleaned_up = 1; /* do this just once */
316 monitor_conn = wpa_ctrl_open(ifname);
317 if (monitor_conn == NULL) {
318 wpa_ctrl_close(ctrl_conn);
322 if (wpa_ctrl_attach(monitor_conn) != 0) {
323 wpa_ctrl_close(monitor_conn);
324 wpa_ctrl_close(ctrl_conn);
325 ctrl_conn = monitor_conn = NULL;
331 int wifi_send_command(struct wpa_ctrl *ctrl, const char *cmd, char *reply, size_t *reply_len)
335 if (ctrl_conn == NULL) {
336 LOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
339 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), reply, reply_len, NULL);
341 LOGD("'%s' command timed out.\n", cmd);
343 } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
346 if (strncmp(cmd, "PING", 4) == 0) {
347 reply[*reply_len] = '\0';
352 int wifi_wait_for_event(char *buf, size_t buflen)
354 size_t nread = buflen - 1;
359 struct timeval *tptr;
361 if (monitor_conn == NULL)
364 result = wpa_ctrl_recv(monitor_conn, buf, &nread);
366 LOGD("wpa_ctrl_recv failed: %s\n", strerror(errno));
370 /* LOGD("wait_for_event: result=%d nread=%d string=\"%s\"\n", result, nread, buf); */
371 /* Check for EOF on the socket */
372 if (result == 0 && nread == 0) {
373 /* Fabricate an event to pass up */
374 LOGD("Received EOF on supplicant socket\n");
375 strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
376 buf[buflen-1] = '\0';
380 * Events strings are in the format
384 * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
385 * etc.) and XXX is the event name. The level information is not useful
386 * to us, so strip it off.
389 char *match = strchr(buf, '>');
391 nread -= (match+1-buf);
392 memmove(buf, match+1, nread+1);
398 void wifi_close_supplicant_connection()
400 if (ctrl_conn != NULL) {
401 wpa_ctrl_close(ctrl_conn);
404 if (monitor_conn != NULL) {
405 wpa_ctrl_close(monitor_conn);
410 int wifi_command(const char *command, char *reply, size_t *reply_len)
412 return wifi_send_command(ctrl_conn, command, reply, reply_len);