OSDN Git Service

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