OSDN Git Service

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