blob: 68b1a1e57e5832f865f7357198adfc5ec72b948b [file] [log] [blame]
The Android Open Source Projectd6054a32008-10-21 07:00:00 -07001/*
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/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
32static struct wpa_ctrl *ctrl_conn;
33static struct wpa_ctrl *monitor_conn;
34
35extern int do_dhcp();
36extern int ifc_init();
37extern void ifc_close();
38extern char *dhcp_lasterror();
39extern void get_dhcp_info();
40extern int init_module(void *, unsigned long, const char *);
41extern int delete_module(const char *, unsigned int);
42
43static char iface[PROPERTY_VALUE_MAX];
44// TODO: use new ANDROID_SOCKET mechanism, once support for multiple
45// sockets is in
46
47static const char IFACE_DIR[] = "/data/system/wpa_supplicant";
48static const char DRIVER_MODULE_NAME[] = "wlan";
49static const char DRIVER_MODULE_TAG[] = "wlan ";
50static const char DRIVER_MODULE_PATH[] = "/system/lib/modules/wlan.ko";
51static const char FIRMWARE_LOADER[] = "wlan_loader";
52static const char DRIVER_PROP_NAME[] = "wlan.driver.status";
53static const char SUPPLICANT_NAME[] = "wpa_supplicant";
54static const char SUPP_PROP_NAME[] = "init.svc.wpa_supplicant";
55static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
56static const char SUPP_CONFIG_FILE[] = "/data/misc/wifi/wpa_supplicant.conf";
57static const char MODULE_FILE[] = "/proc/modules";
58
59static 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
76static 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
95int 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
113const char *get_dhcp_error_string() {
114 return dhcp_lasterror();
115}
116
117static 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 return 0;
135 }
136 while ((fgets(line, sizeof(line), proc)) != NULL) {
137 if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
138 fclose(proc);
139 return 1;
140 }
141 }
142 fclose(proc);
143 property_set(DRIVER_PROP_NAME, "unloaded");
144 return 0;
145}
146
147int wifi_load_driver()
148{
149 char driver_status[PROPERTY_VALUE_MAX];
150 int count = 40; /* wait at most 20 seconds for completion */
151
152 if (check_driver_loaded()) {
153 return 0;
154 }
155 insmod(DRIVER_MODULE_PATH);
156 property_set("ctl.start", FIRMWARE_LOADER);
157 while (count-- > 0) {
158 usleep(500000);
159 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
160 if (strcmp(driver_status, "ok") == 0)
161 return 0;
162 else if (strcmp(DRIVER_PROP_NAME, "failed") == 0)
163 return -1;
164 }
165 }
166 property_set(DRIVER_PROP_NAME, "timeout");
167 return -1;
168}
169
170int wifi_unload_driver()
171{
172 if (rmmod(DRIVER_MODULE_NAME) == 0) {
173 usleep(1000000);
174 property_set(DRIVER_PROP_NAME, "unloaded");
175 return 0;
176 } else
177 return -1;
178}
179
180static int control_supplicant(int startIt)
181{
182 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
183 const char *ctrl_prop = (startIt ? "ctl.start" : "ctl.stop");
184 const char *desired_status = (startIt ? "running" : "stopped");
185 int count = 20; /* wait at most 20 seconds for completion */
186
187 if (property_get(SUPP_PROP_NAME, supp_status, NULL)
188 && strcmp(supp_status, desired_status) == 0) {
189 return 0; /* supplicant already running */
190 }
191 property_set(ctrl_prop, SUPPLICANT_NAME);
192
193 while (count-- > 0) {
194 usleep(1000000);
195 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
196 if (strcmp(supp_status, desired_status) == 0)
197 return 0;
198 }
199 }
200 return -1;
201}
202
203int ensure_config_file_exists()
204{
205 char buf[2048];
206 int srcfd, destfd;
207 int nread;
208
209 if (access(SUPP_CONFIG_FILE, R_OK|W_OK) == 0) {
210 return 0;
211 } else if (errno != ENOENT) {
212 LOGE("Cannot access \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
213 return -1;
214 }
215
216 srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
217 if (srcfd < 0) {
218 LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
219 return -1;
220 }
221
222 destfd = open(SUPP_CONFIG_FILE, O_CREAT|O_WRONLY, 0660);
223 if (destfd < 0) {
224 close(srcfd);
225 LOGE("Cannot create \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
226 return -1;
227 }
228
229 while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
230 if (nread < 0) {
231 LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
232 close(srcfd);
233 close(destfd);
234 unlink(SUPP_CONFIG_FILE);
235 return -1;
236 }
237 write(destfd, buf, nread);
238 }
239
240 close(destfd);
241 close(srcfd);
242
243 if (chown(SUPP_CONFIG_FILE, AID_SYSTEM, AID_WIFI) < 0) {
244 LOGE("Error changing group ownership of %s to %d: %s",
245 SUPP_CONFIG_FILE, AID_WIFI, strerror(errno));
246 unlink(SUPP_CONFIG_FILE);
247 return -1;
248 }
249 return 0;
250}
251
252int wifi_start_supplicant()
253{
254 /* Before starting the daemon, make sure its config file exists */
255 if (ensure_config_file_exists() < 0) {
256 LOGE("Wi-Fi will not be enabled");
257 return -1;
258 }
259 return control_supplicant(1);
260}
261
262int wifi_stop_supplicant()
263{
264 return control_supplicant(0);
265}
266
267int wifi_connect_to_supplicant()
268{
269 char ifname[256];
270 static int cleaned_up = 0;
271
272 property_get("wifi.interface", iface, "sta");
273
274 if (access(IFACE_DIR, F_OK) == 0) {
275 snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface);
276 } else {
277 strlcpy(ifname, iface, sizeof(ifname));
278 }
279
280 ctrl_conn = wpa_ctrl_open(ifname);
281 if (ctrl_conn == NULL) {
282 LOGD("Unable to open connection to supplicant on \"%s\": %s",
283 ifname, strerror(errno));
284 /*
285 * errno == ENOENT means the supplicant daemon isn't
286 * running. Take this opportunity to clear out any
287 * stale socket files that might be left over. Note
288 * there's a possible race with the command line client
289 * trying to connect to the daemon, but it would require
290 * that the supplicant be started and the command line
291 * client connect to it during the window between the
292 * error check and the removal of the files. And in
293 * any event, the remedy is that the user would simply
294 * have to run the command line program again.
295 */
296 if (!cleaned_up && (errno == ENOENT || errno == EADDRINUSE)) {
297 cleaned_up = 1; /* do this just once */
298 wpa_ctrl_cleanup();
299 }
300 return -1;
301 }
302 monitor_conn = wpa_ctrl_open(ifname);
303 if (monitor_conn == NULL) {
304 wpa_ctrl_close(ctrl_conn);
305 ctrl_conn = NULL;
306 return -1;
307 }
308 if (wpa_ctrl_attach(monitor_conn) != 0) {
309 wpa_ctrl_close(monitor_conn);
310 wpa_ctrl_close(ctrl_conn);
311 ctrl_conn = monitor_conn = NULL;
312 return -1;
313 }
314 return 0;
315}
316
317int wifi_send_command(struct wpa_ctrl *ctrl, const char *cmd, char *reply, size_t *reply_len)
318{
319 int ret;
320
321 if (ctrl_conn == NULL) {
322 LOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
323 return -1;
324 }
325 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), reply, reply_len, NULL);
326 if (ret == -2) {
327 LOGD("'%s' command timed out.\n", cmd);
328 return -2;
329 } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
330 return -1;
331 }
332 if (strncmp(cmd, "PING", 4) == 0) {
333 reply[*reply_len] = '\0';
334 }
335 return 0;
336}
337
338int wifi_wait_for_event(char *buf, size_t buflen)
339{
340 size_t nread = buflen - 1;
341 int fd;
342 fd_set rfds;
343 int result;
344 struct timeval tval;
345 struct timeval *tptr;
346
347 if (monitor_conn == NULL)
348 return 0;
349
350 result = wpa_ctrl_recv(monitor_conn, buf, &nread);
351 if (result < 0) {
352 LOGD("wpa_ctrl_recv failed: %s\n", strerror(errno));
353 return -1;
354 }
355 buf[nread] = '\0';
356 /* LOGD("wait_for_event: result=%d nread=%d string=\"%s\"\n", result, nread, buf); */
357 /* Check for EOF on the socket */
358 if (result == 0 && nread == 0) {
359 /* Fabricate an event to pass up */
360 LOGD("Received EOF on supplicant socket\n");
361 strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
362 buf[buflen-1] = '\0';
363 return strlen(buf);
364 }
365 /*
366 * Events strings are in the format
367 *
368 * <N>CTRL-EVENT-XXX
369 *
370 * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
371 * etc.) and XXX is the event name. The level information is not useful
372 * to us, so strip it off.
373 */
374 if (buf[0] == '<') {
375 char *match = strchr(buf, '>');
376 if (match != NULL) {
377 nread -= (match+1-buf);
378 memmove(buf, match+1, nread+1);
379 }
380 }
381 return nread;
382}
383
384void wifi_close_supplicant_connection()
385{
386 if (ctrl_conn != NULL) {
387 wpa_ctrl_close(ctrl_conn);
388 ctrl_conn = NULL;
389 }
390 if (monitor_conn != NULL) {
391 wpa_ctrl_close(monitor_conn);
392 monitor_conn = NULL;
393 }
394}
395
396int wifi_command(const char *command, char *reply, size_t *reply_len)
397{
398 return wifi_send_command(ctrl_conn, command, reply, reply_len);
399}