OSDN Git Service

Add additional logging
[android-x86/hardware-libhardware_legacy.git] / wifi / wifi.c
1 /*
2  * Copyright 2008, The Android Open Source Project
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdlib.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <string.h>
21
22 #include "hardware_legacy/wifi.h"
23 #include "libwpa_client/wpa_ctrl.h"
24
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>
34 #endif
35
36 static struct wpa_ctrl *ctrl_conn;
37 static struct wpa_ctrl *monitor_conn;
38
39 extern int do_dhcp();
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);
46
47 static char iface[PROPERTY_VALUE_MAX];
48 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
49 // sockets is in
50
51 #ifndef WIFI_DRIVER_MODULE_PATH
52 #define WIFI_DRIVER_MODULE_PATH         "/system/lib/modules/wlan.ko"
53 #endif
54 #ifndef WIFI_DRIVER_MODULE_NAME
55 #define WIFI_DRIVER_MODULE_NAME         "wlan"
56 #endif
57 #ifndef WIFI_DRIVER_MODULE_ARG
58 #define WIFI_DRIVER_MODULE_ARG          ""
59 #endif
60 #ifndef WIFI_FIRMWARE_LOADER
61 #define WIFI_FIRMWARE_LOADER            ""
62 #endif
63 #define WIFI_TEST_INTERFACE             "sta"
64
65 static const char IFACE_DIR[]           = "/data/system/wpa_supplicant";
66 static const char DRIVER_MODULE_NAME[]  = WIFI_DRIVER_MODULE_NAME;
67 static const char DRIVER_MODULE_TAG[]   = WIFI_DRIVER_MODULE_NAME " ";
68 static const char DRIVER_MODULE_PATH[]  = WIFI_DRIVER_MODULE_PATH;
69 static const char DRIVER_MODULE_ARG[]   = WIFI_DRIVER_MODULE_ARG;
70 static const char FIRMWARE_LOADER[]     = WIFI_FIRMWARE_LOADER;
71 static const char DRIVER_PROP_NAME[]    = "wlan.driver.status";
72 static const char SUPPLICANT_NAME[]     = "wpa_supplicant";
73 static const char SUPP_PROP_NAME[]      = "init.svc.wpa_supplicant";
74 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
75 static const char SUPP_CONFIG_FILE[]    = "/data/misc/wifi/wpa_supplicant.conf";
76 static const char MODULE_FILE[]         = "/proc/modules";
77
78 static int insmod(const char *filename, const char *args)
79 {
80     void *module;
81     unsigned int size;
82     int ret;
83
84     module = load_file(filename, &size);
85     if (!module)
86         return -1;
87
88     ret = init_module(module, size, args);
89
90     free(module);
91
92     return ret;
93 }
94
95 static int rmmod(const char *modname)
96 {
97     int ret = -1;
98     int maxtry = 10;
99
100     while (maxtry-- > 0) {
101         ret = delete_module(modname, O_NONBLOCK | O_EXCL);
102         if (ret < 0 && errno == EAGAIN)
103             usleep(500000);
104         else
105             break;
106     }
107
108     if (ret != 0)
109         LOGD("Unable to unload driver module \"%s\": %s\n",
110              modname, strerror(errno));
111     return ret;
112 }
113
114 int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
115                     int *dns1, int *dns2, int *server, int *lease) {
116     /* For test driver, always report success */
117     if (strcmp(iface, WIFI_TEST_INTERFACE) == 0)
118         return 0;
119
120     if (ifc_init() < 0)
121         return -1;
122
123     if (do_dhcp(iface) < 0) {
124         ifc_close();
125         return -1;
126     }
127     ifc_close();
128     get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
129     return 0;
130 }
131
132 const char *get_dhcp_error_string() {
133     return dhcp_lasterror();
134 }
135
136 static int check_driver_loaded() {
137     char driver_status[PROPERTY_VALUE_MAX];
138     FILE *proc;
139     char line[sizeof(DRIVER_MODULE_TAG)+10];
140
141     if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
142             || strcmp(driver_status, "ok") != 0) {
143         return 0;  /* driver not loaded */
144     }
145     /*
146      * If the property says the driver is loaded, check to
147      * make sure that the property setting isn't just left
148      * over from a previous manual shutdown or a runtime
149      * crash.
150      */
151     if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
152         LOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
153         property_set(DRIVER_PROP_NAME, "unloaded");
154         return 0;
155     }
156     while ((fgets(line, sizeof(line), proc)) != NULL) {
157         if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
158             fclose(proc);
159             return 1;
160         }
161     }
162     fclose(proc);
163     property_set(DRIVER_PROP_NAME, "unloaded");
164     return 0;
165 }
166
167 int wifi_load_driver()
168 {
169     char driver_status[PROPERTY_VALUE_MAX];
170     int count = 100; /* wait at most 20 seconds for completion */
171
172     if (check_driver_loaded()) {
173         return 0;
174     }
175
176     if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0)
177         return -1;
178
179     if (strcmp(FIRMWARE_LOADER,"") == 0) {
180         usleep(500000);
181         property_set(DRIVER_PROP_NAME, "ok");
182     }
183     else {
184         property_set("ctl.start", FIRMWARE_LOADER);
185     }
186     sched_yield();
187     while (count-- > 0) {
188         if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
189             if (strcmp(driver_status, "ok") == 0)
190                 return 0;
191             else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
192                 wifi_unload_driver();
193                 return -1;
194             }
195         }
196         usleep(200000);
197     }
198     property_set(DRIVER_PROP_NAME, "timeout");
199     wifi_unload_driver();
200     return -1;
201 }
202
203 int wifi_unload_driver()
204 {
205     int count = 20; /* wait at most 10 seconds for completion */
206
207     if (rmmod(DRIVER_MODULE_NAME) == 0) {
208         while (count-- > 0) {
209             if (!check_driver_loaded())
210                 break;
211             usleep(500000);
212         }
213         if (count) {
214             return 0;
215         }
216         return -1;
217     } else
218         return -1;
219 }
220
221 int ensure_config_file_exists()
222 {
223     char buf[2048];
224     int srcfd, destfd;
225     int nread;
226
227     if (access(SUPP_CONFIG_FILE, R_OK|W_OK) == 0) {
228         return 0;
229     } else if (errno != ENOENT) {
230         LOGE("Cannot access \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
231         return -1;
232     }
233
234     srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
235     if (srcfd < 0) {
236         LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
237         return -1;
238     }
239
240     destfd = open(SUPP_CONFIG_FILE, O_CREAT|O_WRONLY, 0660);
241     if (destfd < 0) {
242         close(srcfd);
243         LOGE("Cannot create \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
244         return -1;
245     }
246
247     while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
248         if (nread < 0) {
249             LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
250             close(srcfd);
251             close(destfd);
252             unlink(SUPP_CONFIG_FILE);
253             return -1;
254         }
255         write(destfd, buf, nread);
256     }
257
258     close(destfd);
259     close(srcfd);
260
261     if (chown(SUPP_CONFIG_FILE, AID_SYSTEM, AID_WIFI) < 0) {
262         LOGE("Error changing group ownership of %s to %d: %s",
263              SUPP_CONFIG_FILE, AID_WIFI, strerror(errno));
264         unlink(SUPP_CONFIG_FILE);
265         return -1;
266     }
267     return 0;
268 }
269
270 int wifi_start_supplicant()
271 {
272     char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
273     int count = 200; /* wait at most 20 seconds for completion */
274 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
275     const prop_info *pi;
276     unsigned serial = 0;
277 #endif
278
279     /* Check whether already running */
280     if (property_get(SUPP_PROP_NAME, supp_status, NULL)
281             && strcmp(supp_status, "running") == 0) {
282         return 0;
283     }
284
285     /* Before starting the daemon, make sure its config file exists */
286     if (ensure_config_file_exists() < 0) {
287         LOGE("Wi-Fi will not be enabled");
288         return -1;
289     }
290
291     /* Clear out any stale socket files that might be left over. */
292     wpa_ctrl_cleanup();
293
294 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
295     /*
296      * Get a reference to the status property, so we can distinguish
297      * the case where it goes stopped => running => stopped (i.e.,
298      * it start up, but fails right away) from the case in which
299      * it starts in the stopped state and never manages to start
300      * running at all.
301      */
302     pi = __system_property_find(SUPP_PROP_NAME);
303     if (pi != NULL) {
304         serial = pi->serial;
305     }
306 #endif
307     property_set("ctl.start", SUPPLICANT_NAME);
308     sched_yield();
309
310     while (count-- > 0) {
311 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
312         if (pi == NULL) {
313             pi = __system_property_find(SUPP_PROP_NAME);
314         }
315         if (pi != NULL) {
316             __system_property_read(pi, NULL, supp_status);
317             if (strcmp(supp_status, "running") == 0) {
318                 return 0;
319             } else if (pi->serial != serial &&
320                     strcmp(supp_status, "stopped") == 0) {
321                 return -1;
322             }
323         }
324 #else
325         if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
326             if (strcmp(supp_status, "running") == 0)
327                 return 0;
328         }
329 #endif
330         usleep(100000);
331     }
332     return -1;
333 }
334
335 int wifi_stop_supplicant()
336 {
337     char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
338     int count = 50; /* wait at most 5 seconds for completion */
339
340     /* Check whether supplicant already stopped */
341     if (property_get(SUPP_PROP_NAME, supp_status, NULL)
342         && strcmp(supp_status, "stopped") == 0) {
343         return 0;
344     }
345
346     property_set("ctl.stop", SUPPLICANT_NAME);
347     sched_yield();
348
349     while (count-- > 0) {
350         if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
351             if (strcmp(supp_status, "stopped") == 0)
352                 return 0;
353         }
354         usleep(100000);
355     }
356     return -1;
357 }
358
359 int wifi_connect_to_supplicant()
360 {
361     char ifname[256];
362     char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
363
364     /* Make sure supplicant is running */
365     if (!property_get(SUPP_PROP_NAME, supp_status, NULL)
366             || strcmp(supp_status, "running") != 0) {
367         LOGE("Supplicant not running, cannot connect");
368         return -1;
369     }
370
371     property_get("wifi.interface", iface, WIFI_TEST_INTERFACE);
372
373     if (access(IFACE_DIR, F_OK) == 0) {
374         snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface);
375     } else {
376         strlcpy(ifname, iface, sizeof(ifname));
377     }
378
379     ctrl_conn = wpa_ctrl_open(ifname);
380     if (ctrl_conn == NULL) {
381         LOGE("Unable to open connection to supplicant on \"%s\": %s",
382              ifname, strerror(errno));
383         return -1;
384     }
385     monitor_conn = wpa_ctrl_open(ifname);
386     if (monitor_conn == NULL) {
387         wpa_ctrl_close(ctrl_conn);
388         ctrl_conn = NULL;
389         return -1;
390     }
391     if (wpa_ctrl_attach(monitor_conn) != 0) {
392         wpa_ctrl_close(monitor_conn);
393         wpa_ctrl_close(ctrl_conn);
394         ctrl_conn = monitor_conn = NULL;
395         return -1;
396     }
397     return 0;
398 }
399
400 int wifi_send_command(struct wpa_ctrl *ctrl, const char *cmd, char *reply, size_t *reply_len)
401 {
402     int ret;
403
404     if (ctrl_conn == NULL) {
405         LOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
406         return -1;
407     }
408     ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), reply, reply_len, NULL);
409     if (ret == -2) {
410         LOGD("'%s' command timed out.\n", cmd);
411         return -2;
412     } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
413         return -1;
414     }
415     if (strncmp(cmd, "PING", 4) == 0) {
416         reply[*reply_len] = '\0';
417     }
418     return 0;
419 }
420
421 int wifi_wait_for_event(char *buf, size_t buflen)
422 {
423     size_t nread = buflen - 1;
424     int fd;
425     fd_set rfds;
426     int result;
427     struct timeval tval;
428     struct timeval *tptr;
429     
430     if (monitor_conn == NULL) {
431         LOGD("Connection closed\n");
432         strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1);
433         buf[buflen-1] = '\0';
434         return strlen(buf);
435     }
436
437     result = wpa_ctrl_recv(monitor_conn, buf, &nread);
438     if (result < 0) {
439         LOGD("wpa_ctrl_recv failed: %s\n", strerror(errno));
440         strncpy(buf, WPA_EVENT_TERMINATING " - recv error", buflen-1);
441         buf[buflen-1] = '\0';
442         return strlen(buf);
443     }
444     buf[nread] = '\0';
445     /* LOGD("wait_for_event: result=%d nread=%d string=\"%s\"\n", result, nread, buf); */
446     /* Check for EOF on the socket */
447     if (result == 0 && nread == 0) {
448         /* Fabricate an event to pass up */
449         LOGD("Received EOF on supplicant socket\n");
450         strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
451         buf[buflen-1] = '\0';
452         return strlen(buf);
453     }
454     /*
455      * Events strings are in the format
456      *
457      *     <N>CTRL-EVENT-XXX 
458      *
459      * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
460      * etc.) and XXX is the event name. The level information is not useful
461      * to us, so strip it off.
462      */
463     if (buf[0] == '<') {
464         char *match = strchr(buf, '>');
465         if (match != NULL) {
466             nread -= (match+1-buf);
467             memmove(buf, match+1, nread+1);
468         }
469     }
470     return nread;
471 }
472
473 void wifi_close_supplicant_connection()
474 {
475     if (ctrl_conn != NULL) {
476         wpa_ctrl_close(ctrl_conn);
477         ctrl_conn = NULL;
478     }
479     if (monitor_conn != NULL) {
480         wpa_ctrl_close(monitor_conn);
481         monitor_conn = NULL;
482     }
483 }
484
485 int wifi_command(const char *command, char *reply, size_t *reply_len)
486 {
487     return wifi_send_command(ctrl_conn, command, reply, reply_len);
488 }