OSDN Git Service

am c191f678: am 3c2559ea: Merge commit \'c0ff7762509131a5a95ee258fa6da881a23100a6...
[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 #include <dirent.h>
22 #include <sys/socket.h>
23 #include <unistd.h>
24 #include <poll.h>
25
26 #include "hardware_legacy/wifi.h"
27 #include "libwpa_client/wpa_ctrl.h"
28
29 #define LOG_TAG "WifiHW"
30 #include "cutils/log.h"
31 #include "cutils/memory.h"
32 #include "cutils/misc.h"
33 #include "cutils/properties.h"
34 #include "private/android_filesystem_config.h"
35 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
36 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
37 #include <sys/_system_properties.h>
38 #endif
39
40 static struct wpa_ctrl *ctrl_conn;
41 static struct wpa_ctrl *monitor_conn;
42
43 /* socket pair used to exit from a blocking read */
44 static int exit_sockets[2];
45
46 extern int do_dhcp();
47 extern int ifc_init();
48 extern void ifc_close();
49 extern char *dhcp_lasterror();
50 extern void get_dhcp_info();
51 extern int init_module(void *, unsigned long, const char *);
52 extern int delete_module(const char *, unsigned int);
53 void wifi_close_sockets();
54
55 static char primary_iface[PROPERTY_VALUE_MAX];
56 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
57 // sockets is in
58
59 #ifndef WIFI_DRIVER_MODULE_ARG
60 #define WIFI_DRIVER_MODULE_ARG          ""
61 #endif
62 #ifndef WIFI_FIRMWARE_LOADER
63 #define WIFI_FIRMWARE_LOADER            ""
64 #endif
65 #define WIFI_TEST_INTERFACE             "sta"
66
67 #ifndef WIFI_DRIVER_FW_PATH_STA
68 #define WIFI_DRIVER_FW_PATH_STA         NULL
69 #endif
70 #ifndef WIFI_DRIVER_FW_PATH_AP
71 #define WIFI_DRIVER_FW_PATH_AP          NULL
72 #endif
73 #ifndef WIFI_DRIVER_FW_PATH_P2P
74 #define WIFI_DRIVER_FW_PATH_P2P         NULL
75 #endif
76
77 #ifndef WIFI_DRIVER_FW_PATH_PARAM
78 #define WIFI_DRIVER_FW_PATH_PARAM       "/sys/module/wlan/parameters/fwpath"
79 #endif
80
81 #define WIFI_DRIVER_LOADER_DELAY        1000000
82
83 static const char IFACE_DIR[]           = "/data/system/wpa_supplicant";
84 #ifdef WIFI_DRIVER_MODULE_PATH
85 static const char DRIVER_MODULE_NAME[]  = WIFI_DRIVER_MODULE_NAME;
86 static const char DRIVER_MODULE_TAG[]   = WIFI_DRIVER_MODULE_NAME " ";
87 static const char DRIVER_MODULE_PATH[]  = WIFI_DRIVER_MODULE_PATH;
88 static const char DRIVER_MODULE_ARG[]   = WIFI_DRIVER_MODULE_ARG;
89 #endif
90 static const char FIRMWARE_LOADER[]     = WIFI_FIRMWARE_LOADER;
91 static const char DRIVER_PROP_NAME[]    = "wlan.driver.status";
92 static const char SUPPLICANT_NAME[]     = "wpa_supplicant";
93 static const char SUPP_PROP_NAME[]      = "init.svc.wpa_supplicant";
94 static const char P2P_SUPPLICANT_NAME[] = "p2p_supplicant";
95 static const char P2P_PROP_NAME[]       = "init.svc.p2p_supplicant";
96 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
97 static const char SUPP_CONFIG_FILE[]    = "/data/misc/wifi/wpa_supplicant.conf";
98 static const char P2P_CONFIG_FILE[]     = "/data/misc/wifi/p2p_supplicant.conf";
99 static const char CONTROL_IFACE_PATH[]  = "/data/misc/wifi/sockets";
100 static const char MODULE_FILE[]         = "/proc/modules";
101
102 static const char IFNAME[]              = "IFNAME=";
103 #define IFNAMELEN                       (sizeof(IFNAME) - 1)
104 static const char WPA_EVENT_IGNORE[]    = "CTRL-EVENT-IGNORE ";
105
106 static const char SUPP_ENTROPY_FILE[]   = WIFI_ENTROPY_FILE;
107 static unsigned char dummy_key[21] = { 0x02, 0x11, 0xbe, 0x33, 0x43, 0x35,
108                                        0x68, 0x47, 0x84, 0x99, 0xa9, 0x2b,
109                                        0x1c, 0xd3, 0xee, 0xff, 0xf1, 0xe2,
110                                        0xf3, 0xf4, 0xf5 };
111
112 /* Is either SUPPLICANT_NAME or P2P_SUPPLICANT_NAME */
113 static char supplicant_name[PROPERTY_VALUE_MAX];
114 /* Is either SUPP_PROP_NAME or P2P_PROP_NAME */
115 static char supplicant_prop_name[PROPERTY_KEY_MAX];
116
117 static int insmod(const char *filename, const char *args)
118 {
119     void *module;
120     unsigned int size;
121     int ret;
122
123     module = load_file(filename, &size);
124     if (!module)
125         return -1;
126
127     ret = init_module(module, size, args);
128
129     free(module);
130
131     return ret;
132 }
133
134 static int rmmod(const char *modname)
135 {
136     int ret = -1;
137     int maxtry = 10;
138
139     while (maxtry-- > 0) {
140         ret = delete_module(modname, O_NONBLOCK | O_EXCL);
141         if (ret < 0 && errno == EAGAIN)
142             usleep(500000);
143         else
144             break;
145     }
146
147     if (ret != 0)
148         ALOGD("Unable to unload driver module \"%s\": %s\n",
149              modname, strerror(errno));
150     return ret;
151 }
152
153 int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
154                     int *dns1, int *dns2, int *server, int *lease) {
155     /* For test driver, always report success */
156     if (strcmp(primary_iface, WIFI_TEST_INTERFACE) == 0)
157         return 0;
158
159     if (ifc_init() < 0)
160         return -1;
161
162     if (do_dhcp(primary_iface) < 0) {
163         ifc_close();
164         return -1;
165     }
166     ifc_close();
167     get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
168     return 0;
169 }
170
171 const char *get_dhcp_error_string() {
172     return dhcp_lasterror();
173 }
174
175 int is_wifi_driver_loaded() {
176     char driver_status[PROPERTY_VALUE_MAX];
177 #ifdef WIFI_DRIVER_MODULE_PATH
178     FILE *proc;
179     char line[sizeof(DRIVER_MODULE_TAG)+10];
180 #endif
181
182     if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
183             || strcmp(driver_status, "ok") != 0) {
184         return 0;  /* driver not loaded */
185     }
186 #ifdef WIFI_DRIVER_MODULE_PATH
187     /*
188      * If the property says the driver is loaded, check to
189      * make sure that the property setting isn't just left
190      * over from a previous manual shutdown or a runtime
191      * crash.
192      */
193     if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
194         ALOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
195         property_set(DRIVER_PROP_NAME, "unloaded");
196         return 0;
197     }
198     while ((fgets(line, sizeof(line), proc)) != NULL) {
199         if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
200             fclose(proc);
201             return 1;
202         }
203     }
204     fclose(proc);
205     property_set(DRIVER_PROP_NAME, "unloaded");
206     return 0;
207 #else
208     return 1;
209 #endif
210 }
211
212 int wifi_load_driver()
213 {
214 #ifdef WIFI_DRIVER_MODULE_PATH
215     char driver_status[PROPERTY_VALUE_MAX];
216     int count = 100; /* wait at most 20 seconds for completion */
217
218     if (is_wifi_driver_loaded()) {
219         return 0;
220     }
221
222     if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0)
223         return -1;
224
225     if (strcmp(FIRMWARE_LOADER,"") == 0) {
226         /* usleep(WIFI_DRIVER_LOADER_DELAY); */
227         property_set(DRIVER_PROP_NAME, "ok");
228     }
229     else {
230         property_set("ctl.start", FIRMWARE_LOADER);
231     }
232     sched_yield();
233     while (count-- > 0) {
234         if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
235             if (strcmp(driver_status, "ok") == 0)
236                 return 0;
237             else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
238                 wifi_unload_driver();
239                 return -1;
240             }
241         }
242         usleep(200000);
243     }
244     property_set(DRIVER_PROP_NAME, "timeout");
245     wifi_unload_driver();
246     return -1;
247 #else
248     property_set(DRIVER_PROP_NAME, "ok");
249     return 0;
250 #endif
251 }
252
253 int wifi_unload_driver()
254 {
255     usleep(200000); /* allow to finish interface down */
256 #ifdef WIFI_DRIVER_MODULE_PATH
257     if (rmmod(DRIVER_MODULE_NAME) == 0) {
258         int count = 20; /* wait at most 10 seconds for completion */
259         while (count-- > 0) {
260             if (!is_wifi_driver_loaded())
261                 break;
262             usleep(500000);
263         }
264         usleep(500000); /* allow card removal */
265         if (count) {
266             return 0;
267         }
268         return -1;
269     } else
270         return -1;
271 #else
272     property_set(DRIVER_PROP_NAME, "unloaded");
273     return 0;
274 #endif
275 }
276
277 int ensure_entropy_file_exists()
278 {
279     int ret;
280     int destfd;
281
282     ret = access(SUPP_ENTROPY_FILE, R_OK|W_OK);
283     if ((ret == 0) || (errno == EACCES)) {
284         if ((ret != 0) &&
285             (chmod(SUPP_ENTROPY_FILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
286             ALOGE("Cannot set RW to \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
287             return -1;
288         }
289         return 0;
290     }
291     destfd = TEMP_FAILURE_RETRY(open(SUPP_ENTROPY_FILE, O_CREAT|O_RDWR, 0660));
292     if (destfd < 0) {
293         ALOGE("Cannot create \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
294         return -1;
295     }
296
297     if (TEMP_FAILURE_RETRY(write(destfd, dummy_key, sizeof(dummy_key))) != sizeof(dummy_key)) {
298         ALOGE("Error writing \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
299         close(destfd);
300         return -1;
301     }
302     close(destfd);
303
304     /* chmod is needed because open() didn't set permisions properly */
305     if (chmod(SUPP_ENTROPY_FILE, 0660) < 0) {
306         ALOGE("Error changing permissions of %s to 0660: %s",
307              SUPP_ENTROPY_FILE, strerror(errno));
308         unlink(SUPP_ENTROPY_FILE);
309         return -1;
310     }
311
312     if (chown(SUPP_ENTROPY_FILE, AID_SYSTEM, AID_WIFI) < 0) {
313         ALOGE("Error changing group ownership of %s to %d: %s",
314              SUPP_ENTROPY_FILE, AID_WIFI, strerror(errno));
315         unlink(SUPP_ENTROPY_FILE);
316         return -1;
317     }
318     return 0;
319 }
320
321 int ensure_config_file_exists(const char *config_file)
322 {
323     char buf[2048];
324     int srcfd, destfd;
325     struct stat sb;
326     int nread;
327     int ret;
328
329     ret = access(config_file, R_OK|W_OK);
330     if ((ret == 0) || (errno == EACCES)) {
331         if ((ret != 0) &&
332             (chmod(config_file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
333             ALOGE("Cannot set RW to \"%s\": %s", config_file, strerror(errno));
334             return -1;
335         }
336         return 0;
337     } else if (errno != ENOENT) {
338         ALOGE("Cannot access \"%s\": %s", config_file, strerror(errno));
339         return -1;
340     }
341
342     srcfd = TEMP_FAILURE_RETRY(open(SUPP_CONFIG_TEMPLATE, O_RDONLY));
343     if (srcfd < 0) {
344         ALOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
345         return -1;
346     }
347
348     destfd = TEMP_FAILURE_RETRY(open(config_file, O_CREAT|O_RDWR, 0660));
349     if (destfd < 0) {
350         close(srcfd);
351         ALOGE("Cannot create \"%s\": %s", config_file, strerror(errno));
352         return -1;
353     }
354
355     while ((nread = TEMP_FAILURE_RETRY(read(srcfd, buf, sizeof(buf)))) != 0) {
356         if (nread < 0) {
357             ALOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
358             close(srcfd);
359             close(destfd);
360             unlink(config_file);
361             return -1;
362         }
363         TEMP_FAILURE_RETRY(write(destfd, buf, nread));
364     }
365
366     close(destfd);
367     close(srcfd);
368
369     /* chmod is needed because open() didn't set permisions properly */
370     if (chmod(config_file, 0660) < 0) {
371         ALOGE("Error changing permissions of %s to 0660: %s",
372              config_file, strerror(errno));
373         unlink(config_file);
374         return -1;
375     }
376
377     if (chown(config_file, AID_SYSTEM, AID_WIFI) < 0) {
378         ALOGE("Error changing group ownership of %s to %d: %s",
379              config_file, AID_WIFI, strerror(errno));
380         unlink(config_file);
381         return -1;
382     }
383     return 0;
384 }
385
386 int wifi_start_supplicant(int p2p_supported)
387 {
388     char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
389     int count = 200; /* wait at most 20 seconds for completion */
390 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
391     const prop_info *pi;
392     unsigned serial = 0, i;
393 #endif
394
395     if (p2p_supported) {
396         strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
397         strcpy(supplicant_prop_name, P2P_PROP_NAME);
398
399         /* Ensure p2p config file is created */
400         if (ensure_config_file_exists(P2P_CONFIG_FILE) < 0) {
401             ALOGE("Failed to create a p2p config file");
402             return -1;
403         }
404
405     } else {
406         strcpy(supplicant_name, SUPPLICANT_NAME);
407         strcpy(supplicant_prop_name, SUPP_PROP_NAME);
408     }
409
410     /* Check whether already running */
411     if (property_get(supplicant_name, supp_status, NULL)
412             && strcmp(supp_status, "running") == 0) {
413         return 0;
414     }
415
416     /* Before starting the daemon, make sure its config file exists */
417     if (ensure_config_file_exists(SUPP_CONFIG_FILE) < 0) {
418         ALOGE("Wi-Fi will not be enabled");
419         return -1;
420     }
421
422     if (ensure_entropy_file_exists() < 0) {
423         ALOGE("Wi-Fi entropy file was not created");
424     }
425
426     /* Clear out any stale socket files that might be left over. */
427     wpa_ctrl_cleanup();
428
429     /* Reset sockets used for exiting from hung state */
430     exit_sockets[0] = exit_sockets[1] = -1;
431
432 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
433     /*
434      * Get a reference to the status property, so we can distinguish
435      * the case where it goes stopped => running => stopped (i.e.,
436      * it start up, but fails right away) from the case in which
437      * it starts in the stopped state and never manages to start
438      * running at all.
439      */
440     pi = __system_property_find(supplicant_prop_name);
441     if (pi != NULL) {
442         serial = __system_property_serial(pi);
443     }
444 #endif
445     property_get("wifi.interface", primary_iface, WIFI_TEST_INTERFACE);
446
447     property_set("ctl.start", supplicant_name);
448     sched_yield();
449
450     while (count-- > 0) {
451 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
452         if (pi == NULL) {
453             pi = __system_property_find(supplicant_prop_name);
454         }
455         if (pi != NULL) {
456             __system_property_read(pi, NULL, supp_status);
457             if (strcmp(supp_status, "running") == 0) {
458                 return 0;
459             } else if (__system_property_serial(pi) != serial &&
460                     strcmp(supp_status, "stopped") == 0) {
461                 return -1;
462             }
463         }
464 #else
465         if (property_get(supplicant_prop_name, supp_status, NULL)) {
466             if (strcmp(supp_status, "running") == 0)
467                 return 0;
468         }
469 #endif
470         usleep(100000);
471     }
472     return -1;
473 }
474
475 int wifi_stop_supplicant(int p2p_supported)
476 {
477     char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
478     int count = 50; /* wait at most 5 seconds for completion */
479
480     if (p2p_supported) {
481         strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
482         strcpy(supplicant_prop_name, P2P_PROP_NAME);
483     } else {
484         strcpy(supplicant_name, SUPPLICANT_NAME);
485         strcpy(supplicant_prop_name, SUPP_PROP_NAME);
486     }
487
488     /* Check whether supplicant already stopped */
489     if (property_get(supplicant_prop_name, supp_status, NULL)
490         && strcmp(supp_status, "stopped") == 0) {
491         return 0;
492     }
493
494     property_set("ctl.stop", supplicant_name);
495     sched_yield();
496
497     while (count-- > 0) {
498         if (property_get(supplicant_prop_name, supp_status, NULL)) {
499             if (strcmp(supp_status, "stopped") == 0)
500                 return 0;
501         }
502         usleep(100000);
503     }
504     ALOGE("Failed to stop supplicant");
505     return -1;
506 }
507
508 int wifi_connect_on_socket_path(const char *path)
509 {
510     char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
511
512     /* Make sure supplicant is running */
513     if (!property_get(supplicant_prop_name, supp_status, NULL)
514             || strcmp(supp_status, "running") != 0) {
515         ALOGE("Supplicant not running, cannot connect");
516         return -1;
517     }
518
519     ctrl_conn = wpa_ctrl_open(path);
520     if (ctrl_conn == NULL) {
521         ALOGE("Unable to open connection to supplicant on \"%s\": %s",
522              path, strerror(errno));
523         return -1;
524     }
525     monitor_conn = wpa_ctrl_open(path);
526     if (monitor_conn == NULL) {
527         wpa_ctrl_close(ctrl_conn);
528         ctrl_conn = NULL;
529         return -1;
530     }
531     if (wpa_ctrl_attach(monitor_conn) != 0) {
532         wpa_ctrl_close(monitor_conn);
533         wpa_ctrl_close(ctrl_conn);
534         ctrl_conn = monitor_conn = NULL;
535         return -1;
536     }
537
538     if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets) == -1) {
539         wpa_ctrl_close(monitor_conn);
540         wpa_ctrl_close(ctrl_conn);
541         ctrl_conn = monitor_conn = NULL;
542         return -1;
543     }
544
545     return 0;
546 }
547
548 /* Establishes the control and monitor socket connections on the interface */
549 int wifi_connect_to_supplicant()
550 {
551     static char path[PATH_MAX];
552
553     if (access(IFACE_DIR, F_OK) == 0) {
554         snprintf(path, sizeof(path), "%s/%s", IFACE_DIR, primary_iface);
555     } else {
556         snprintf(path, sizeof(path), "@android:wpa_%s", primary_iface);
557     }
558     return wifi_connect_on_socket_path(path);
559 }
560
561 int wifi_send_command(const char *cmd, char *reply, size_t *reply_len)
562 {
563     int ret;
564     if (ctrl_conn == NULL) {
565         ALOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
566         return -1;
567     }
568     ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), reply, reply_len, NULL);
569     if (ret == -2) {
570         ALOGD("'%s' command timed out.\n", cmd);
571         /* unblocks the monitor receive socket for termination */
572         TEMP_FAILURE_RETRY(write(exit_sockets[0], "T", 1));
573         return -2;
574     } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
575         return -1;
576     }
577     if (strncmp(cmd, "PING", 4) == 0) {
578         reply[*reply_len] = '\0';
579     }
580     return 0;
581 }
582
583 int wifi_ctrl_recv(char *reply, size_t *reply_len)
584 {
585     int res;
586     int ctrlfd = wpa_ctrl_get_fd(monitor_conn);
587     struct pollfd rfds[2];
588
589     memset(rfds, 0, 2 * sizeof(struct pollfd));
590     rfds[0].fd = ctrlfd;
591     rfds[0].events |= POLLIN;
592     rfds[1].fd = exit_sockets[1];
593     rfds[1].events |= POLLIN;
594     res = TEMP_FAILURE_RETRY(poll(rfds, 2, -1));
595     if (res < 0) {
596         ALOGE("Error poll = %d", res);
597         return res;
598     }
599     if (rfds[0].revents & POLLIN) {
600         return wpa_ctrl_recv(monitor_conn, reply, reply_len);
601     }
602
603     /* it is not rfds[0], then it must be rfts[1] (i.e. the exit socket)
604      * or we timed out. In either case, this call has failed ..
605      */
606     return -2;
607 }
608
609 int wifi_wait_on_socket(char *buf, size_t buflen)
610 {
611     size_t nread = buflen - 1;
612     int result;
613     char *match, *match2;
614
615     if (monitor_conn == NULL) {
616         return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - connection closed");
617     }
618
619     result = wifi_ctrl_recv(buf, &nread);
620
621     /* Terminate reception on exit socket */
622     if (result == -2) {
623         return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - connection closed");
624     }
625
626     if (result < 0) {
627         ALOGD("wifi_ctrl_recv failed: %s\n", strerror(errno));
628         return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - recv error");
629     }
630     buf[nread] = '\0';
631     /* Check for EOF on the socket */
632     if (result == 0 && nread == 0) {
633         /* Fabricate an event to pass up */
634         ALOGD("Received EOF on supplicant socket\n");
635         return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - signal 0 received");
636     }
637     /*
638      * Events strings are in the format
639      *
640      *     IFNAME=iface <N>CTRL-EVENT-XXX 
641      *        or
642      *     <N>CTRL-EVENT-XXX 
643      *
644      * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
645      * etc.) and XXX is the event name. The level information is not useful
646      * to us, so strip it off.
647      */
648
649     if (strncmp(buf, IFNAME, IFNAMELEN) == 0) {
650         match = strchr(buf, ' ');
651         if (match != NULL) {
652             if (match[1] == '<') {
653                 match2 = strchr(match + 2, '>');
654                 if (match2 != NULL) {
655                     nread -= (match2 - match);
656                     memmove(match + 1, match2 + 1, nread - (match - buf) + 1);
657                 }
658             }
659         } else {
660             return snprintf(buf, buflen, "%s", WPA_EVENT_IGNORE);
661         }
662     } else if (buf[0] == '<') {
663         match = strchr(buf, '>');
664         if (match != NULL) {
665             nread -= (match + 1 - buf);
666             memmove(buf, match + 1, nread + 1);
667             ALOGV("supplicant generated event without interface - %s\n", buf);
668         }
669     } else {
670         /* let the event go as is! */
671         ALOGW("supplicant generated event without interface and without message level - %s\n", buf);
672     }
673
674     return nread;
675 }
676
677 int wifi_wait_for_event(char *buf, size_t buflen)
678 {
679     return wifi_wait_on_socket(buf, buflen);
680 }
681
682 void wifi_close_sockets()
683 {
684     if (ctrl_conn != NULL) {
685         wpa_ctrl_close(ctrl_conn);
686         ctrl_conn = NULL;
687     }
688
689     if (monitor_conn != NULL) {
690         wpa_ctrl_close(monitor_conn);
691         monitor_conn = NULL;
692     }
693
694     if (exit_sockets[0] >= 0) {
695         close(exit_sockets[0]);
696         exit_sockets[0] = -1;
697     }
698
699     if (exit_sockets[1] >= 0) {
700         close(exit_sockets[1]);
701         exit_sockets[1] = -1;
702     }
703 }
704
705 void wifi_close_supplicant_connection()
706 {
707     char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
708     int count = 50; /* wait at most 5 seconds to ensure init has stopped stupplicant */
709
710     wifi_close_sockets();
711
712     while (count-- > 0) {
713         if (property_get(supplicant_prop_name, supp_status, NULL)) {
714             if (strcmp(supp_status, "stopped") == 0)
715                 return;
716         }
717         usleep(100000);
718     }
719 }
720
721 int wifi_command(const char *command, char *reply, size_t *reply_len)
722 {
723     return wifi_send_command(command, reply, reply_len);
724 }
725
726 const char *wifi_get_fw_path(int fw_type)
727 {
728     switch (fw_type) {
729     case WIFI_GET_FW_PATH_STA:
730         return WIFI_DRIVER_FW_PATH_STA;
731     case WIFI_GET_FW_PATH_AP:
732         return WIFI_DRIVER_FW_PATH_AP;
733     case WIFI_GET_FW_PATH_P2P:
734         return WIFI_DRIVER_FW_PATH_P2P;
735     }
736     return NULL;
737 }
738
739 int wifi_change_fw_path(const char *fwpath)
740 {
741     int len;
742     int fd;
743     int ret = 0;
744
745     if (!fwpath)
746         return ret;
747     fd = TEMP_FAILURE_RETRY(open(WIFI_DRIVER_FW_PATH_PARAM, O_WRONLY));
748     if (fd < 0) {
749         ALOGE("Failed to open wlan fw path param (%s)", strerror(errno));
750         return -1;
751     }
752     len = strlen(fwpath) + 1;
753     if (TEMP_FAILURE_RETRY(write(fd, fwpath, len)) != len) {
754         ALOGE("Failed to write wlan fw path param (%s)", strerror(errno));
755         ret = -1;
756     }
757     close(fd);
758     return ret;
759 }