OSDN Git Service

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