OSDN Git Service

91ff4cd8bbc99f9c5285b04001af368932798b9e
[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
32 static struct wpa_ctrl *ctrl_conn;
33 static struct wpa_ctrl *monitor_conn;
34
35 extern int do_dhcp();
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);
42
43 static char iface[PROPERTY_VALUE_MAX];
44 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
45 // sockets is in
46
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";
58
59 static int insmod(const char *filename)
60 {
61     void *module;
62     unsigned int size;
63     int ret;
64
65     module = load_file(filename, &size);
66     if (!module)
67         return -1;
68
69     ret = init_module(module, size, "");
70
71     free(module);
72
73     return ret;
74 }
75
76 static int rmmod(const char *modname)
77 {
78     int ret = -1;
79     int maxtry = 10;
80
81     while (maxtry-- > 0) {
82         ret = delete_module(modname, O_NONBLOCK | O_EXCL);
83         if (ret < 0 && errno == EAGAIN)
84             usleep(500000);
85         else
86             break;
87     }
88
89     if (ret != 0)
90         LOGD("Unable to unload driver module \"%s\": %s\n",
91              modname, strerror(errno));
92     return ret;
93 }
94
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)
99         return 0;
100
101     if (ifc_init() < 0)
102         return -1;
103
104     if (do_dhcp(iface) < 0) {
105         ifc_close();
106         return -1;
107     }
108     ifc_close();
109     get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
110     return 0;
111 }
112
113 const char *get_dhcp_error_string() {
114     return dhcp_lasterror();
115 }
116
117 static int check_driver_loaded() {
118     char driver_status[PROPERTY_VALUE_MAX];
119     FILE *proc;
120     char line[sizeof(DRIVER_MODULE_TAG)+10];
121
122     if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
123             || strcmp(driver_status, "ok") != 0) {
124         return 0;  /* driver not loaded */
125     }
126     /*
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
130      * crash.
131      */
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");     
135         return 0;
136     }
137     while ((fgets(line, sizeof(line), proc)) != NULL) {
138         if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
139             fclose(proc);
140             return 1;
141         }
142     }
143     fclose(proc);
144     property_set(DRIVER_PROP_NAME, "unloaded");
145     return 0;
146 }
147
148 int wifi_load_driver()
149 {
150     char driver_status[PROPERTY_VALUE_MAX];
151     int count = 100; /* wait at most 20 seconds for completion */
152
153     if (check_driver_loaded()) {
154         return 0;
155     }
156
157     if (insmod(DRIVER_MODULE_PATH) < 0)
158         return -1;
159       
160     property_set("ctl.start", FIRMWARE_LOADER);
161     sched_yield();
162     while (count-- > 0) {
163         if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
164             if (strcmp(driver_status, "ok") == 0)
165                 return 0;
166             else if (strcmp(DRIVER_PROP_NAME, "failed") == 0)
167                 return -1;
168         }
169         usleep(200000);
170     }
171     property_set(DRIVER_PROP_NAME, "timeout");
172     return -1;
173 }
174
175 int wifi_unload_driver()
176 {
177     int count = 20; /* wait at most 10 seconds for completion */
178     
179     if (rmmod(DRIVER_MODULE_NAME) == 0) {
180         while (count-- > 0) {
181             if (!check_driver_loaded())
182                 break;
183             usleep(500000);
184         }
185         if (count) {
186             return 0;
187         }
188         return -1;
189     } else
190         return -1;
191 }
192
193 static int control_supplicant(int startIt)
194 {
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 */
199
200     if (property_get(SUPP_PROP_NAME, supp_status, NULL)
201         && strcmp(supp_status, desired_status) == 0) {
202         return 0;  /* supplicant already running */
203     }
204     property_set(ctrl_prop, SUPPLICANT_NAME);
205     sched_yield();
206
207     while (count-- > 0) {
208         if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
209             if (strcmp(supp_status, desired_status) == 0)
210                 return 0;
211         }
212         usleep(100000);
213     }
214     return -1;
215 }
216
217 int ensure_config_file_exists()
218 {
219     char buf[2048];
220     int srcfd, destfd;
221     int nread;
222
223     if (access(SUPP_CONFIG_FILE, R_OK|W_OK) == 0) {
224         return 0;
225     } else if (errno != ENOENT) {
226         LOGE("Cannot access \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
227         return -1;
228     }
229
230     srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
231     if (srcfd < 0) {
232         LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
233         return -1;
234     }
235
236     destfd = open(SUPP_CONFIG_FILE, O_CREAT|O_WRONLY, 0660);
237     if (destfd < 0) {
238         close(srcfd);
239         LOGE("Cannot create \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
240         return -1;
241     }
242
243     while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
244         if (nread < 0) {
245             LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
246             close(srcfd);
247             close(destfd);
248             unlink(SUPP_CONFIG_FILE);
249             return -1;
250         }
251         write(destfd, buf, nread);
252     }
253
254     close(destfd);
255     close(srcfd);
256
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);
261         return -1;
262     }
263     return 0;
264 }
265
266 int wifi_start_supplicant()
267 {
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");
271         return -1;
272     }
273     return control_supplicant(1);
274 }
275
276 int wifi_stop_supplicant()
277 {
278     return control_supplicant(0);
279 }
280
281 int wifi_connect_to_supplicant()
282 {
283     char ifname[256];
284     static int cleaned_up = 0;
285
286     property_get("wifi.interface", iface, "sta");
287
288     if (access(IFACE_DIR, F_OK) == 0) {
289         snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface);
290     } else {
291         strlcpy(ifname, iface, sizeof(ifname));
292     }
293
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));
298         /*
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.
309          */
310         if (!cleaned_up && (errno == ENOENT || errno == EADDRINUSE)) {
311             cleaned_up = 1; /* do this just once */
312             wpa_ctrl_cleanup();
313         }
314         return -1;
315     }
316     monitor_conn = wpa_ctrl_open(ifname);
317     if (monitor_conn == NULL) {
318         wpa_ctrl_close(ctrl_conn);
319         ctrl_conn = NULL;
320         return -1;
321     }
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;
326         return -1;
327     }
328     return 0;
329 }
330
331 int wifi_send_command(struct wpa_ctrl *ctrl, const char *cmd, char *reply, size_t *reply_len)
332 {
333     int ret;
334
335     if (ctrl_conn == NULL) {
336         LOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
337         return -1;
338     }
339     ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), reply, reply_len, NULL);
340     if (ret == -2) {
341         LOGD("'%s' command timed out.\n", cmd);
342         return -2;
343     } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
344         return -1;
345     }
346     if (strncmp(cmd, "PING", 4) == 0) {
347         reply[*reply_len] = '\0';
348     }
349     return 0;
350 }
351
352 int wifi_wait_for_event(char *buf, size_t buflen)
353 {
354     size_t nread = buflen - 1;
355     int fd;
356     fd_set rfds;
357     int result;
358     struct timeval tval;
359     struct timeval *tptr;
360     
361     if (monitor_conn == NULL)
362         return 0;
363
364     result = wpa_ctrl_recv(monitor_conn, buf, &nread);
365     if (result < 0) {
366         LOGD("wpa_ctrl_recv failed: %s\n", strerror(errno));
367         return -1;
368     }
369     buf[nread] = '\0';
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';
377         return strlen(buf);
378     }
379     /*
380      * Events strings are in the format
381      *
382      *     <N>CTRL-EVENT-XXX 
383      *
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.
387      */
388     if (buf[0] == '<') {
389         char *match = strchr(buf, '>');
390         if (match != NULL) {
391             nread -= (match+1-buf);
392             memmove(buf, match+1, nread+1);
393         }
394     }
395     return nread;
396 }
397
398 void wifi_close_supplicant_connection()
399 {
400     if (ctrl_conn != NULL) {
401         wpa_ctrl_close(ctrl_conn);
402         ctrl_conn = NULL;
403     }
404     if (monitor_conn != NULL) {
405         wpa_ctrl_close(monitor_conn);
406         monitor_conn = NULL;
407     }
408 }
409
410 int wifi_command(const char *command, char *reply, size_t *reply_len)
411 {
412     return wifi_send_command(ctrl_conn, command, reply, reply_len);
413 }