int wifi_connect_to_supplicant() { char ifname[256]; static int cleaned_up = 0; property_get("wifi.interface", iface, "sta"); if (access(IFACE_DIR, F_OK) == 0) { snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface); } else { strlcpy(ifname, iface, sizeof(ifname)); } ctrl_conn = wpa_ctrl_open(ifname); if (ctrl_conn == NULL) { LOGD("Unable to open connection to supplicant on \"%s\": %s", ifname, strerror(errno)); /* * errno == ENOENT means the supplicant daemon isn't * running. Take this opportunity to clear out any * stale socket files that might be left over. Note * there's a possible race with the command line client * trying to connect to the daemon, but it would require * that the supplicant be started and the command line * client connect to it during the window between the * error check and the removal of the files. And in * any event, the remedy is that the user would simply * have to run the command line program again. */ if (!cleaned_up && (errno == ENOENT || errno == EADDRINUSE)) { cleaned_up = 1; /* do this just once */ wpa_ctrl_cleanup(); } return -1; } monitor_conn = wpa_ctrl_open(ifname); if (monitor_conn == NULL) { wpa_ctrl_close(ctrl_conn); ctrl_conn = NULL; return -1; } if (wpa_ctrl_attach(monitor_conn) != 0) { wpa_ctrl_close(monitor_conn); wpa_ctrl_close(ctrl_conn); ctrl_conn = monitor_conn = NULL; return -1; } return 0; }
static int wifi_start_hostapd() { static int hostapdPid=0; LOGD("SoftapController::wifi_start_hostapd"); /* Check whether already running */ if (hostapdPid != 0) { LOGE("%s already started", HOSTAPD_NAME); return 0; } /* Clear out any stale socket files that might be left over. */ wpa_ctrl_cleanup(); LOGD("Starting %s services", HOSTAPD_NAME); if ((pid = fork()) < 0) { LOGE("fork failed (%s)", strerror(errno)); return -1; } if (!pid) { char *args[] = { (char *)"/system/bin/hostapd" (char *)HOSTAPD_CONFIG_FILE ,(char *) 0 }; if (execv(args[0], args)) { LOGE("execl failed (%s)", strerror(errno)); } LOGE("Should never get here!"); return 0; } else { hostapdPid = pid; LOGD("%s services running", HOSTAPD_NAME); } return 0; }
int Supplicant::start() { if (setupConfig()) { LOGW("Unable to setup supplicant.conf"); } if (mServiceManager->start(SUPPLICANT_SERVICE_NAME)) { LOGE("Error starting supplicant (%s)", strerror(errno)); return -1; } wpa_ctrl_cleanup(); if (connectToSupplicant()) { LOGE("Error connecting to supplicant (%s)\n", strerror(errno)); return -1; } if (retrieveInterfaceName()) { LOGE("Error retrieving interface name (%s)\n", strerror(errno)); return -1; } return 0; }
int wifi_start_supplicant(int p2p_supported) { char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; int count = 200; /* wait at most 20 seconds for completion */ #ifdef HAVE_LIBC_SYSTEM_PROPERTIES const prop_info *pi; unsigned serial = 0, i; #endif if (p2p_supported) { strcpy(supplicant_name, P2P_SUPPLICANT_NAME); strcpy(supplicant_prop_name, P2P_PROP_NAME); /* Ensure p2p config file is created */ if (ensure_config_file_exists(P2P_CONFIG_FILE) < 0) { ALOGE("Failed to create a p2p config file"); return -1; } } else { strcpy(supplicant_name, SUPPLICANT_NAME); strcpy(supplicant_prop_name, SUPP_PROP_NAME); } /* Check whether already running */ if (property_get(supplicant_prop_name, supp_status, NULL) && strcmp(supp_status, "running") == 0) { return 0; } /* Before starting the daemon, make sure its config file exists */ if (ensure_config_file_exists(SUPP_CONFIG_FILE) < 0) { ALOGE("Wi-Fi will not be enabled"); return -1; } if (ensure_entropy_file_exists() < 0) { ALOGE("Wi-Fi entropy file was not created"); } /* Clear out any stale socket files that might be left over. */ wpa_ctrl_cleanup(); /* Reset sockets used for exiting from hung state */ exit_sockets[0] = exit_sockets[1] = -1; #ifdef HAVE_LIBC_SYSTEM_PROPERTIES /* * Get a reference to the status property, so we can distinguish * the case where it goes stopped => running => stopped (i.e., * it start up, but fails right away) from the case in which * it starts in the stopped state and never manages to start * running at all. */ pi = __system_property_find(supplicant_prop_name); if (pi != NULL) { serial = __system_property_serial(pi); } #endif property_get("wifi.interface", primary_iface, WIFI_TEST_INTERFACE); property_set("ctl.start", supplicant_name); sched_yield(); while (count-- > 0) { #ifdef HAVE_LIBC_SYSTEM_PROPERTIES if (pi == NULL) { pi = __system_property_find(supplicant_prop_name); } if (pi != NULL) { /* * property serial updated means that init process is scheduled * after we sched_yield, further property status checking is based on this */ if (__system_property_serial(pi) != serial) { __system_property_read(pi, NULL, supp_status); if (strcmp(supp_status, "running") == 0) { return 0; } else if (strcmp(supp_status, "stopped") == 0) { return -1; } } } #else if (property_get(supplicant_prop_name, supp_status, NULL)) { if (strcmp(supp_status, "running") == 0) return 0; } #endif usleep(100000); } return -1; }
int wifi_start_supplicant() { char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; int count = 200; /* wait at most 20 seconds for completion */ #ifdef HAVE_LIBC_SYSTEM_PROPERTIES const prop_info *pi; unsigned serial = 0; #endif /* Check whether already running */ if (property_get(SUPP_PROP_NAME, supp_status, NULL) && strcmp(supp_status, "running") == 0) { LOGD("[wifiHW] wpa is running already"); return 0; } /* Before starting the daemon, make sure its config file exists */ if (ensure_config_file_exists() < 0) { LOGE("Wi-Fi will not be enabled"); return -1; } /* Clear out any stale socket files that might be left over. */ wpa_ctrl_cleanup(); #ifdef HAVE_LIBC_SYSTEM_PROPERTIES /* * Get a reference to the status property, so we can distinguish * the case where it goes stopped => running => stopped (i.e., * it start up, but fails right away) from the case in which * it starts in the stopped state and never manages to start * running at all. */ pi = __system_property_find(SUPP_PROP_NAME); if (pi != NULL) { serial = pi->serial; } #endif property_set("ctl.start", SUPPLICANT_NAME); sched_yield(); while (count-- > 0) { #ifdef HAVE_LIBC_SYSTEM_PROPERTIES if (pi == NULL) { pi = __system_property_find(SUPP_PROP_NAME); } if (pi != NULL) { __system_property_read(pi, NULL, supp_status); if (strcmp(supp_status, "running") == 0) { LOGD("[wifiHW] wpa running"); return 0; } else if (pi->serial != serial && strcmp(supp_status, "stopped") == 0) { return -1; } } #else if (property_get(SUPP_PROP_NAME, supp_status, NULL)) { if (strcmp(supp_status, "running") == 0){ LOGD("[wifiHW] wpa running"); return 0; } } #endif usleep(100000); } return -1; }