一段WiFi代码

#include
#include
#include
#include
#include
#include
#include

#include "hardware_legacy/wifi.h"
#include "libwpa_client/wpa_ctrl.h"

#define LOG_TAG "WifiHW"
#include "cutils/log.h"
#include "cutils/memory.h"
#include "cutils/misc.h"
#include "cutils/properties.h"
#include "private/android_filesystem_config.h"
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include
#endif

static struct wpa_ctrl *ctrl_conn;
static struct wpa_ctrl *monitor_conn;
/* socket pair used to exit from a blocking read */
static int exit_sockets[2] = { -1, -1 };

extern int do_dhcp();
extern int ifc_init();
extern void ifc_close();
extern char *dhcp_lasterror();
extern void get_dhcp_info();
extern int init_module(void *, unsigned long, const char *);
extern int delete_module(const char *, unsigned int);

static char iface[PROPERTY_VALUE_MAX];
// TODO: use new ANDROID_SOCKET mechanism, once support for multiple
// sockets is in

#ifndef WIFI_DRIVER_MODULE_PATH_UTILRT5370
#define WIFI_DRIVER_MODULE_PATH_UTILRT5370        "/system/lib/modules/rtutil5370sta.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_UTILRT5370
#define WIFI_DRIVER_MODULE_NAME_UTILRT5370        "rtutil5370sta"
#endif

#ifndef WIFI_DRIVER_MODULE_PATH_RT5370
#define WIFI_DRIVER_MODULE_PATH_RT5370        "/system/lib/modules/rt5370sta.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_RT5370
#define WIFI_DRIVER_MODULE_NAME_RT5370        "rt5370sta"
#endif

#ifndef WIFI_DRIVER_MODULE_PATH_NETRT5370
#define WIFI_DRIVER_MODULE_PATH_NETRT5370        "/system/lib/modules/rtnet5370sta.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_NETRT5370
#define WIFI_DRIVER_MODULE_NAME_NETRT5370        "rtnet5370sta"
#endif

#ifndef WIFI_DRIVER_MODULE_PATH_RT3090
#define WIFI_DRIVER_MODULE_PATH_RT3090        "/system/lib/modules/rt3090sta.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_RT3090
#define WIFI_DRIVER_MODULE_NAME_RT3090        "rt3090sta"
#endif

#ifndef WIFI_DRIVER_MODULE_PATH_8712U
#define WIFI_DRIVER_MODULE_PATH_8712U         "/system/lib/modules/8712u.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_8712U
#define WIFI_DRIVER_MODULE_NAME_8712U         "8712u"
#endif

#ifndef WIFI_DRIVER_MODULE_PATH_CFG80211
#define WIFI_DRIVER_MODULE_PATH_CFG80211      "/system/lib/modules/cfg80211.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_CFG80211
#define WIFI_DRIVER_MODULE_NAME_CFG80211      "cfg80211"
#endif

#ifndef WIFI_DRIVER_MODULE_PATH_8192CU
#define WIFI_DRIVER_MODULE_PATH_8192CU         "/system/lib/modules/rtl8192cu.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_8192CU
#define WIFI_DRIVER_MODULE_NAME_8192CU         "rtl8192cu"
#endif

#ifndef WIFI_DRIVER_MODULE_PATH_8188EU
#define WIFI_DRIVER_MODULE_PATH_8188EU         "/system/lib/modules/rtl8188eu.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_8188EU
#define WIFI_DRIVER_MODULE_NAME_8188EU         "rtl8188eu"
#endif

#ifndef WIFI_AP_DRIVER_MODULE_PATH_RTUTIL5370
#define WIFI_AP_DRIVER_MODULE_PATH_RTUTIL5370 "/system/lib/modules/rtutil5370ap.ko"
#endif

#ifndef WIFI_AP_DRIVER_MODULE_NAME_RTUTIL5370
#define WIFI_AP_DRIVER_MODULE_NAME_RTUTIL5370 "rtutil5370ap"
#endif

#ifndef WIFI_AP_DRIVER_MODULE_PATH_RT5370
#define WIFI_AP_DRIVER_MODULE_PATH_RT5370 "/system/lib/modules/rt5370ap.ko"
#endif

#ifndef WIFI_AP_DRIVER_MODULE_NAME_RT5370
#define WIFI_AP_DRIVER_MODULE_NAME_RT5370 "rt5370ap"
#endif

#ifndef WIFI_AP_DRIVER_MODULE_PATH_RTNET5370
#define WIFI_AP_DRIVER_MODULE_PATH_RTNET5370 "/system/lib/modules/rtnet5370ap.ko"
#endif

#ifndef WIFI_AP_DRIVER_MODULE_NAME_RTNET5370
#define WIFI_AP_DRIVER_MODULE_NAME_RTNET5370 "rtnet5370ap"
#endif

#ifndef WIFI_DRIVER_MODULE_PATH_AR9271_COMPAT
#define WIFI_DRIVER_MODULE_PATH_AR9271_COMPAT       "/system/lib/modules/compat.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_AR9271_COMPAT
#define WIFI_DRIVER_MODULE_NAME_AR9271_COMPAT       "compat"
#endif
#ifndef WIFI_DRIVER_MODULE_PATH_AR9271_CFG80211
#define WIFI_DRIVER_MODULE_PATH_AR9271_CFG80211      "/system/lib/modules/cfg80211_ath9k.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_AR9271_CFG80211
#define WIFI_DRIVER_MODULE_NAME_AR9271_CFG80211      "cfg80211_ath9k"
#endif
#ifndef WIFI_DRIVER_MODULE_PATH_AR9271_MAC80211
#define WIFI_DRIVER_MODULE_PATH_AR9271_MAC80211      "/system/lib/modules/mac80211.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_AR9271_MAC80211
#define WIFI_DRIVER_MODULE_NAME_AR9271_MAC80211      "mac80211"
#endif
#ifndef WIFI_DRIVER_MODULE_PATH_AR9271_ATH
#define WIFI_DRIVER_MODULE_PATH_AR9271_ATH      "/system/lib/modules/ath.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_AR9271_ATH
#define WIFI_DRIVER_MODULE_NAME_AR9271_ATH      "ath"
#endif
#ifndef WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K_HW
#define WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K_HW      "/system/lib/modules/ath9k_hw.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_HW
#define WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_HW      "ath9k_hw"
#endif
#ifndef WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K_COMMON
#define WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K_COMMON      "/system/lib/modules/ath9k_common.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_COMMON
#define WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_COMMON      "ath9k_common"
#endif
#ifndef WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K_HTC
#define WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K_HTC      "/system/lib/modules/ath9k_htc.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_HTC
#define WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_HTC      "ath9k_htc"
#endif
#ifndef WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K
#define WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K      "/system/lib/modules/ath9k.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K
#define WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K      "ath9k"
#endif
#ifndef WIFI_AP_DRIVER_MODULE_ARG
#define WIFI_AP_DRIVER_MODULE_ARG ""
#endif

#ifndef WIFI_DRIVER_MODULE_ARG
#define WIFI_DRIVER_MODULE_ARG          ""
#endif
#ifndef WIFI_FIRMWARE_LOADER
#define WIFI_FIRMWARE_LOADER  ""
#endif
#define WIFI_TEST_INTERFACE  "wlan0"

#ifndef WIFI_DRIVER_FW_PATH_STA
#define WIFI_DRIVER_FW_PATH_STA  NULL
#endif
#ifndef WIFI_DRIVER_FW_PATH_AP
#define WIFI_DRIVER_FW_PATH_AP  NULL
#endif
#ifndef WIFI_DRIVER_FW_PATH_P2P
#define WIFI_DRIVER_FW_PATH_P2P  NULL
#endif

#ifndef WIFI_DRIVER_FW_PATH_PARAM
#define WIFI_DRIVER_FW_PATH_PARAM "/sys/module/wlan/parameters/fwpath"
#endif

#define WIFI_DRIVER_LOADER_DELAY 1000000

static const char IFACE_DIR[]           = "/data/system/wpa_supplicant";
static const char DRIVER_MODULE_ARG[]   = WIFI_DRIVER_MODULE_ARG;

static const char DRIVER_MODULE_NAME_UTILRT5370[] = WIFI_DRIVER_MODULE_NAME_UTILRT5370;
static const char DRIVER_MODULE_TAG_UTILRT5370[]  = WIFI_DRIVER_MODULE_NAME_UTILRT5370 " ";
static const char DRIVER_MODULE_PATH_UTILRT5370[] = WIFI_DRIVER_MODULE_PATH_UTILRT5370;
static const char DRIVER_MODULE_NAME_RT5370[] = WIFI_DRIVER_MODULE_NAME_RT5370;
static const char DRIVER_MODULE_TAG_RT5370[]  = WIFI_DRIVER_MODULE_NAME_RT5370 " ";
static const char DRIVER_MODULE_PATH_RT5370[] = WIFI_DRIVER_MODULE_PATH_RT5370;
static const char DRIVER_MODULE_NAME_NETRT5370[] = WIFI_DRIVER_MODULE_NAME_NETRT5370;
static const char DRIVER_MODULE_TAG_NETRT5370[]  = WIFI_DRIVER_MODULE_NAME_NETRT5370 " ";
static const char DRIVER_MODULE_PATH_NETRT5370[] = WIFI_DRIVER_MODULE_PATH_NETRT5370;

static const char DRIVER_MODULE_NAME_RT3090[] = WIFI_DRIVER_MODULE_NAME_RT3090;
static const char DRIVER_MODULE_TAG_RT3090[]  = WIFI_DRIVER_MODULE_NAME_RT3090 " ";
static const char DRIVER_MODULE_PATH_RT3090[] = WIFI_DRIVER_MODULE_PATH_RT3090;

static const char DRIVER_MODULE_NAME_8712U[]  = WIFI_DRIVER_MODULE_NAME_8712U;
static const char DRIVER_MODULE_TAG_8712U[]   = WIFI_DRIVER_MODULE_NAME_8712U " ";
static const char DRIVER_MODULE_PATH_8712U[]  = WIFI_DRIVER_MODULE_PATH_8712U;
static const char DRIVER_MODULE_NAME_CFG80211[] = WIFI_DRIVER_MODULE_NAME_CFG80211;
static const char DRIVER_MODULE_TAG_CFG80211[]  = WIFI_DRIVER_MODULE_NAME_CFG80211 " ";
static const char DRIVER_MODULE_PATH_CFG80211[] = WIFI_DRIVER_MODULE_PATH_CFG80211;
static const char DRIVER_MODULE_NAME_8192CU[]  = WIFI_DRIVER_MODULE_NAME_8192CU;
static const char DRIVER_MODULE_TAG_8192CU[]   = WIFI_DRIVER_MODULE_NAME_8192CU " ";
static const char DRIVER_MODULE_PATH_8192CU[]  = WIFI_DRIVER_MODULE_PATH_8192CU;

static const char DRIVER_MODULE_NAME_8188EU[]  = WIFI_DRIVER_MODULE_NAME_8188EU;
static const char DRIVER_MODULE_TAG_8188EU[]   = WIFI_DRIVER_MODULE_NAME_8188EU " ";
static const char DRIVER_MODULE_PATH_8188EU[]  = WIFI_DRIVER_MODULE_PATH_8188EU;

static const char DRIVER_MODULE_NAME_RTUTIL5370AP[] = WIFI_AP_DRIVER_MODULE_NAME_RTUTIL5370;
static const char DRIVER_MODULE_TAG_RTUTIL5370AP[]  = WIFI_AP_DRIVER_MODULE_NAME_RTUTIL5370 " ";
static const char DRIVER_MODULE_PATH_RTUTIL5370AP[] = WIFI_AP_DRIVER_MODULE_PATH_RTUTIL5370;
static const char DRIVER_MODULE_NAME_RT5370AP[] = WIFI_AP_DRIVER_MODULE_NAME_RT5370;
static const char DRIVER_MODULE_TAG_RT5370AP[]  = WIFI_AP_DRIVER_MODULE_NAME_RT5370 " ";
static const char DRIVER_MODULE_PATH_RT5370AP[] = WIFI_AP_DRIVER_MODULE_PATH_RT5370;
static const char DRIVER_MODULE_NAME_RTNET5370AP[] = WIFI_AP_DRIVER_MODULE_NAME_RTNET5370;
static const char DRIVER_MODULE_TAG_RTNET5370AP[]  = WIFI_AP_DRIVER_MODULE_NAME_RTNET5370 " ";
static const char DRIVER_MODULE_PATH_RTNET5370AP[] = WIFI_AP_DRIVER_MODULE_PATH_RTNET5370;

static const char DRIVER_MODULE_NAME_AR9271_COMPAT[] = WIFI_DRIVER_MODULE_NAME_AR9271_COMPAT;
static const char DRIVER_MODULE_TAG_AR9271_COMPAT[]  = WIFI_DRIVER_MODULE_NAME_AR9271_COMPAT " ";
static const char DRIVER_MODULE_PATH_AR9271_COMPAT[] = WIFI_DRIVER_MODULE_PATH_AR9271_COMPAT;
static const char DRIVER_MODULE_NAME_AR9271_CFG80211[] = WIFI_DRIVER_MODULE_NAME_AR9271_CFG80211;
static const char DRIVER_MODULE_TAG_AR9271_CFG80211[]  = WIFI_DRIVER_MODULE_NAME_AR9271_CFG80211 " ";
static const char DRIVER_MODULE_PATH_AR9271_CFG80211[] = WIFI_DRIVER_MODULE_PATH_AR9271_CFG80211;
static const char DRIVER_MODULE_NAME_AR9271_MAC80211[] = WIFI_DRIVER_MODULE_NAME_AR9271_MAC80211;
static const char DRIVER_MODULE_TAG_AR9271_MAC80211[]  = WIFI_DRIVER_MODULE_NAME_AR9271_MAC80211 " ";
static const char DRIVER_MODULE_PATH_AR9271_MAC80211[] = WIFI_DRIVER_MODULE_PATH_AR9271_MAC80211;
static const char DRIVER_MODULE_NAME_AR9271_ATH[] = WIFI_DRIVER_MODULE_NAME_AR9271_ATH;
static const char DRIVER_MODULE_TAG_AR9271_ATH[]  = WIFI_DRIVER_MODULE_NAME_AR9271_ATH " ";
static const char DRIVER_MODULE_PATH_AR9271_ATH[] = WIFI_DRIVER_MODULE_PATH_AR9271_ATH;
static const char DRIVER_MODULE_NAME_AR9271_ATH9K_HW[] = WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_HW;
static const char DRIVER_MODULE_TAG_AR9271_ATH9K_HW[]  = WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_HW " ";
static const char DRIVER_MODULE_PATH_AR9271_ATH9K_HW[] = WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K_HW;
static const char DRIVER_MODULE_NAME_AR9271_ATH9K_COMMON[] = WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_COMMON;
static const char DRIVER_MODULE_TAG_AR9271_ATH9K_COMMON[]  = WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_COMMON " ";
static const char DRIVER_MODULE_PATH_AR9271_ATH9K_COMMON[] = WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K_COMMON;
static const char DRIVER_MODULE_NAME_AR9271_ATH9K_HTC[] = WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_HTC;
static const char DRIVER_MODULE_TAG_AR9271_ATH9K_HTC[]  = WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K_HTC " ";
static const char DRIVER_MODULE_PATH_AR9271_ATH9K_HTC[] = WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K_HTC;
static const char DRIVER_MODULE_NAME_AR9271_ATH9K[] = WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K;
static const char DRIVER_MODULE_TAG_AR9271_ATH9K[]  = WIFI_DRIVER_MODULE_NAME_AR9271_ATH9K " ";
static const char DRIVER_MODULE_PATH_AR9271_ATH9K[] = WIFI_DRIVER_MODULE_PATH_AR9271_ATH9K;

static const char AP_DRIVER_MODULE_ARG[]      = WIFI_AP_DRIVER_MODULE_ARG;

static const char FIRMWARE_LOADER[]     = WIFI_FIRMWARE_LOADER;
static const char DRIVER_PROP_NAME[]    = "wlan.driver.status";
//static const char SUPPLICANT_NAME[]     = "wpa_supplicant";
//static const char SUPP_PROP_NAME[]      = "init.svc.wpa_supplicant";
static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
static const char P2P_SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/p2p_supplicant.conf";
static const char SUPP_CONFIG_FILE[]    = "/data/misc/wifi/wpa_supplicant.conf";
static const char P2P_CONFIG_FILE[]     = "/data/misc/wifi/p2p_supplicant.conf";
static const char CONTROL_IFACE_PATH[]  = "/data/misc/wifi";
static const char MODULE_FILE[]         = "/proc/modules";

static const char SUPP_ENTROPY_FILE[]   = WIFI_ENTROPY_FILE;
static unsigned char dummy_key[21] = { 0x02, 0x11, 0xbe, 0x33, 0x43, 0x35,
           0x68, 0x47, 0x84, 0x99, 0xa9, 0x2b,
           0x1c, 0xd3, 0xee, 0xff, 0xf1, 0xe2,
           0xf3, 0xf4, 0xf5 };

static int WIFI_ID = INVALID_DEIVCE;
static int wpa_mode = 0;   // mode of wpa_supplicant: 0 - STA, 1 - P2P
static char SUPPLICANT_NAME[PROPERTY_KEY_MAX];
static char SUPP_PROP_NAME[PROPERTY_KEY_MAX];
#define TRUE  0x01
#define FALSE 0x00

static int insmod(const char *filename, const char *args)
{
    void *module;
    unsigned int size;
    int ret;

    module = load_file(filename, &size);
    if (!module)
 return -1;

    ret = init_module(module, size, args);

    free(module);

    return ret;
}

static int rmmod(const char *modname)
{
    int ret = -1;
    int maxtry = 10;

    while (maxtry-- > 0) {
 ret = delete_module(modname, O_NONBLOCK | O_EXCL);
 if (ret < 0 && errno == EAGAIN)
     usleep(500000);
 else
     break;
    }

    if (ret != 0)
 LOGD("Unable to unload driver module \"%s\": %s\n",
      modname, strerror(errno));
    return ret;
}

int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
      int *dns1, int *dns2, int *server, int *lease) {
    /* For test driver, always report success */
    if (strcmp(iface, WIFI_TEST_INTERFACE) == 0)
 return 0;

    if (ifc_init() < 0)
 return -1;

    if (do_dhcp(iface) < 0) {
 ifc_close();
 return -1;
    }
    ifc_close();
    get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
    return 0;
}

const char *get_dhcp_error_string() {
    return dhcp_lasterror();
}

int is_wifi_driver_loaded() {
    char driver_status[PROPERTY_VALUE_MAX];
    FILE *proc;
    char line[128];
    unsigned char driver_loaded = FALSE;

    if(WIFI_ID == INVALID_DEIVCE)
     WIFI_ID = get_wifi_device();

    /*
       for add new wifi device
       load new wifi driver
       run "setprop wlan.driver.status test"
       turn on wifi
       run "setprop wlan.driver.status ok"
       turn off wifi
    */
    if(property_get(DRIVER_PROP_NAME, driver_status, NULL)
      && strcmp(driver_status, "test") == 0){
     LOGW("Wifi test mode");
     return 1;
    }

    if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
     || strcmp(driver_status, "ok") != 0) {
 return 0;  /* driver not loaded */
    }
    /*
     * If the property says the driver is loaded, check to
     * make sure that the property setting isn't just left
     * over from a previous manual shutdown or a runtime
     * crash.
     */
    if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
 LOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
 property_set(DRIVER_PROP_NAME, "unloaded");
 return 0;
    }
    while ((fgets(line, sizeof(line), proc)) != NULL) {
 switch(WIFI_ID){
        case WIFI_RALINK_RT3070:
        case WIFI_RALINK_RT5370:
        case WIFI_RALINK_RT5372:
            if((strncmp(line, DRIVER_MODULE_TAG_UTILRT5370, strlen(DRIVER_MODULE_TAG_UTILRT5370)) == 0)
                || (strncmp(line, DRIVER_MODULE_TAG_RT5370, strlen(DRIVER_MODULE_TAG_RT5370)) == 0)
                || (strncmp(line, DRIVER_MODULE_TAG_NETRT5370, strlen(DRIVER_MODULE_TAG_NETRT5370)) == 0))
                driver_loaded = TRUE;
            break;
        case WIFI_ATHEROS_AR9271:
  if(
      (strncmp(line, DRIVER_MODULE_TAG_AR9271_COMPAT, strlen(DRIVER_MODULE_TAG_AR9271_COMPAT)) == 0)
   || (strncmp(line, DRIVER_MODULE_TAG_AR9271_CFG80211, strlen(DRIVER_MODULE_TAG_AR9271_CFG80211)) == 0)
   || (strncmp(line, DRIVER_MODULE_TAG_AR9271_MAC80211, strlen(DRIVER_MODULE_TAG_AR9271_MAC80211)) == 0)
   || (strncmp(line, DRIVER_MODULE_TAG_AR9271_ATH, strlen(DRIVER_MODULE_TAG_AR9271_ATH)) == 0)
   || (strncmp(line, DRIVER_MODULE_TAG_AR9271_ATH9K_HW, strlen(DRIVER_MODULE_TAG_AR9271_ATH9K_HW)) == 0)
   || (strncmp(line, DRIVER_MODULE_TAG_AR9271_ATH9K_COMMON, strlen(DRIVER_MODULE_TAG_AR9271_ATH9K_COMMON)) == 0)
   || (strncmp(line, DRIVER_MODULE_TAG_AR9271_ATH9K_HTC, strlen(DRIVER_MODULE_TAG_AR9271_ATH9K_HTC)) == 0)
   || (strncmp(line, DRIVER_MODULE_TAG_AR9271_ATH9K, strlen(DRIVER_MODULE_TAG_AR9271_ATH9K)) == 0)
  )
      driver_loaded = TRUE;
  break;
     case WIFI_REALTEK_RTL8188SU:
            case WIFI_REALTEK_RTL8192SU:
  if(strncmp(line, DRIVER_MODULE_TAG_8712U, strlen(DRIVER_MODULE_TAG_8712U)) == 0)
      driver_loaded = TRUE;
  break;
     case WIFI_REALTEK_RTL8192CU:
     case WIFI_REALTEK_RTL8188CU:
  if((strncmp(line, DRIVER_MODULE_TAG_CFG80211, strlen(DRIVER_MODULE_TAG_CFG80211)) == 0)
   || (strncmp(line, DRIVER_MODULE_TAG_8192CU, strlen(DRIVER_MODULE_TAG_8192CU)) == 0)
   )
      driver_loaded = TRUE;
  break;
  case WIFI_REALTEK_RTL8188EU:
  if((strncmp(line, DRIVER_MODULE_TAG_CFG80211, strlen(DRIVER_MODULE_TAG_CFG80211)) == 0)
   || (strncmp(line, DRIVER_MODULE_TAG_8188EU, strlen(DRIVER_MODULE_TAG_8188EU)) == 0)
   )
      driver_loaded = TRUE;
  break;
  case WIFI_REALTEK_RTL8188ETV:
  if((strncmp(line, DRIVER_MODULE_TAG_CFG80211, strlen(DRIVER_MODULE_TAG_CFG80211)) == 0)
   || (strncmp(line, DRIVER_MODULE_TAG_8188EU, strlen(DRIVER_MODULE_TAG_8188EU)) == 0)
   )
      driver_loaded = TRUE;
     default:
  break;
 }
 if(driver_loaded == TRUE || WIFI_ID == INVALID_DEIVCE)  break;
    }

    fclose(proc);
    if(driver_loaded == TRUE)
     return 1;
    property_set(DRIVER_PROP_NAME, "unloaded");
    return 0;
}

int wifi_load_driver()
{
    char driver_status[PROPERTY_VALUE_MAX] = {0};
    int count = 100; /* wait at most 20 seconds for completion */

    if (is_wifi_driver_loaded()) {
 return 0;
    }

    if(WIFI_ID == INVALID_DEIVCE)
 return -1;

    property_get("wlan.ap.state", driver_status, NULL);
    if (!strcmp(driver_status, "enable")) {
 switch (WIFI_ID) {
     case WIFI_RALINK_RT3070:
     case WIFI_RALINK_RT5370:
  if (insmod(DRIVER_MODULE_PATH_RTUTIL5370AP, AP_DRIVER_MODULE_ARG) < 0)
      return -1;
  if (insmod(DRIVER_MODULE_PATH_RT5370AP, AP_DRIVER_MODULE_ARG) < 0)
   return -1;
  if (insmod(DRIVER_MODULE_PATH_RTNET5370AP, AP_DRIVER_MODULE_ARG) < 0)
   return -1;
  break;
        case WIFI_REALTEK_RTL8192CU:
        case WIFI_REALTEK_RTL8188CU:
            insmod(DRIVER_MODULE_PATH_CFG80211, DRIVER_MODULE_ARG);
            insmod(DRIVER_MODULE_PATH_8192CU, DRIVER_MODULE_ARG);
        break;
        case WIFI_REALTEK_RTL8188EU:
            insmod(DRIVER_MODULE_PATH_CFG80211, DRIVER_MODULE_ARG);
            insmod(DRIVER_MODULE_PATH_8188EU, DRIVER_MODULE_ARG);
 break;
        case WIFI_REALTEK_RTL8188ETV:
            insmod(DRIVER_MODULE_PATH_CFG80211, DRIVER_MODULE_ARG);
            insmod(DRIVER_MODULE_PATH_8188EU, DRIVER_MODULE_ARG);
        break;

     default:
  break;
 }
    } else {
 switch (WIFI_ID) {
     case WIFI_RALINK_RT3070:
     case WIFI_RALINK_RT5370:
     case WIFI_RALINK_RT5372:
            insmod(DRIVER_MODULE_PATH_UTILRT5370, DRIVER_MODULE_ARG);
            insmod(DRIVER_MODULE_PATH_RT5370, DRIVER_MODULE_ARG);
            insmod(DRIVER_MODULE_PATH_NETRT5370, DRIVER_MODULE_ARG);
      break;
     case WIFI_ATHEROS_AR9271:
      insmod(DRIVER_MODULE_PATH_AR9271_COMPAT, DRIVER_MODULE_ARG);
      insmod(DRIVER_MODULE_PATH_AR9271_CFG80211, DRIVER_MODULE_ARG);
      insmod(DRIVER_MODULE_PATH_AR9271_MAC80211, DRIVER_MODULE_ARG);
      insmod(DRIVER_MODULE_PATH_AR9271_ATH, DRIVER_MODULE_ARG);
      insmod(DRIVER_MODULE_PATH_AR9271_ATH9K_HW, DRIVER_MODULE_ARG);
      insmod(DRIVER_MODULE_PATH_AR9271_ATH9K_COMMON, DRIVER_MODULE_ARG);
      insmod(DRIVER_MODULE_PATH_AR9271_ATH9K_HTC, DRIVER_MODULE_ARG);
      insmod(DRIVER_MODULE_PATH_AR9271_ATH9K, DRIVER_MODULE_ARG);
      break;
     case WIFI_REALTEK_RTL8188SU:
     case WIFI_REALTEK_RTL8192SU:
      insmod(DRIVER_MODULE_PATH_8712U, DRIVER_MODULE_ARG);
      break;
     case WIFI_REALTEK_RTL8192CU:
     case WIFI_REALTEK_RTL8188CU:
   insmod(DRIVER_MODULE_PATH_CFG80211, DRIVER_MODULE_ARG);
      insmod(DRIVER_MODULE_PATH_8192CU, DRIVER_MODULE_ARG);
      break;
    case WIFI_REALTEK_RTL8188EU:
   insmod(DRIVER_MODULE_PATH_CFG80211, DRIVER_MODULE_ARG);
      insmod(DRIVER_MODULE_PATH_8188EU, DRIVER_MODULE_ARG);
   break;
         case WIFI_REALTEK_RTL8188ETV:
             insmod(DRIVER_MODULE_PATH_CFG80211, DRIVER_MODULE_ARG);
             insmod(DRIVER_MODULE_PATH_8188EU, DRIVER_MODULE_ARG);
      break;
     default:
      return -1;
      break;
 }
    }

    if (strcmp(FIRMWARE_LOADER,"") == 0) {
 /* usleep(WIFI_DRIVER_LOADER_DELAY); */
 property_set(DRIVER_PROP_NAME, "ok");
    }
    else {
 property_set("ctl.start", FIRMWARE_LOADER);
    }
    sched_yield();
    while (count-- > 0) {
 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
     if (strcmp(driver_status, "ok") == 0)
  return 0;
     else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
  wifi_unload_driver();
  return -1;
     }
 }
 usleep(200000);
    }
    property_set(DRIVER_PROP_NAME, "timeout");
    wifi_unload_driver();
    return -1;
}

int wifi_unload_driver()
{
    char driver_status[PROPERTY_VALUE_MAX] = {0};
    int count = 20; /* wait at most 10 seconds for completion */
    unsigned char unload_driver_status = TRUE;

    usleep(200000); /* allow to finish interface down */
    property_get("wlan.ap.state", driver_status, NULL);
    if (!strcmp(driver_status, "enable")) {
 switch (WIFI_ID) {
     case WIFI_RALINK_RT3070:
     case WIFI_RALINK_RT5370:
  if ((rmmod(DRIVER_MODULE_NAME_RTNET5370AP) != 0)
   || (rmmod(DRIVER_MODULE_NAME_RT5370AP) != 0)
   || (rmmod(DRIVER_MODULE_NAME_RTUTIL5370AP) != 0))
      unload_driver_status = FALSE;
  break;
     case WIFI_REALTEK_RTL8192CU:
     case WIFI_REALTEK_RTL8188CU:
  if((rmmod(DRIVER_MODULE_NAME_8192CU) != 0)
   || (rmmod(DRIVER_MODULE_NAME_CFG80211)!=0))
      unload_driver_status = FALSE;
  break;
     case WIFI_REALTEK_RTL8188EU:
  if((rmmod(DRIVER_MODULE_NAME_8188EU) != 0)
   || (rmmod(DRIVER_MODULE_NAME_CFG80211)!=0))
      unload_driver_status = FALSE;
  break;
     case WIFI_REALTEK_RTL8188ETV:
  if((rmmod(DRIVER_MODULE_NAME_8188EU) != 0)
   || (rmmod(DRIVER_MODULE_NAME_CFG80211)!=0))
      unload_driver_status = FALSE;
  break;
     default:
  break;
 }
    } else {
 switch (WIFI_ID) {
     case WIFI_RALINK_RT3070:
            case WIFI_RALINK_RT5370:
            case WIFI_RALINK_RT5372:
  if ((rmmod(DRIVER_MODULE_NAME_NETRT5370) != 0)
   || (rmmod(DRIVER_MODULE_NAME_RT5370) != 0)
   || (rmmod(DRIVER_MODULE_NAME_UTILRT5370) != 0))
      unload_driver_status = FALSE;
  break;
     case WIFI_ATHEROS_AR9271:
  if ((rmmod(DRIVER_MODULE_NAME_AR9271_ATH9K)!=0)
  || (rmmod(DRIVER_MODULE_NAME_AR9271_ATH9K_HTC)!=0)
                || (rmmod(DRIVER_MODULE_NAME_AR9271_ATH9K_COMMON)!=0)
                || (rmmod(DRIVER_MODULE_NAME_AR9271_ATH9K_HW)!=0)
                || (rmmod(DRIVER_MODULE_NAME_AR9271_ATH)!=0)
                || (rmmod(DRIVER_MODULE_NAME_AR9271_MAC80211)!=0)
                || (rmmod(DRIVER_MODULE_NAME_AR9271_CFG80211)!=0)
                || (rmmod(DRIVER_MODULE_NAME_AR9271_COMPAT)!=0))
      unload_driver_status = FALSE;
  break;
     case WIFI_REALTEK_RTL8188SU:
     case WIFI_REALTEK_RTL8192SU:
  if(rmmod(DRIVER_MODULE_NAME_8712U) != 0)
      unload_driver_status = FALSE;
  break;
     case WIFI_REALTEK_RTL8192CU:
     case WIFI_REALTEK_RTL8188CU:
  if((rmmod(DRIVER_MODULE_NAME_8192CU) != 0)
   || (rmmod(DRIVER_MODULE_NAME_CFG80211)!=0))
      unload_driver_status = FALSE;
  break;
  case WIFI_REALTEK_RTL8188EU:
  if((rmmod(DRIVER_MODULE_NAME_8188EU) != 0)
   || (rmmod(DRIVER_MODULE_NAME_CFG80211)!=0))
      unload_driver_status = FALSE;
  break;
  case WIFI_REALTEK_RTL8188ETV:
  if((rmmod(DRIVER_MODULE_NAME_8188EU) != 0)
   || (rmmod(DRIVER_MODULE_NAME_CFG80211)!=0))
      unload_driver_status = FALSE;
  break;
     default:
  break;
        }
    }

    if(unload_driver_status == FALSE){
 WIFI_ID = INVALID_DEIVCE;
 return -1;
    }

    while (count-- > 0) {
 if (!is_wifi_driver_loaded())
     break;
 usleep(500000);
    }
    usleep(500000); /* allow card removal */

    WIFI_ID = INVALID_DEIVCE;

    if (count)
     return 0;
    return -1;
}

int ensure_entropy_file_exists()
{
    int ret;
    int destfd;

    ret = access(SUPP_ENTROPY_FILE, R_OK|W_OK);
    if ((ret == 0) || (errno == EACCES)) {
 if ((ret != 0) &&
     (chmod(SUPP_ENTROPY_FILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
     LOGE("Cannot set RW to \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
     return -1;
 }
 return 0;
    }
    destfd = open(SUPP_ENTROPY_FILE, O_CREAT|O_RDWR, 0660);
    if (destfd < 0) {
 LOGE("Cannot create \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
 return -1;
    }

    if (write(destfd, dummy_key, sizeof(dummy_key)) != sizeof(dummy_key)) {
 LOGE("Error writing \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
 close(destfd);
 return -1;
    }
    close(destfd);

    /* chmod is needed because open() didn't set permisions properly */
    if (chmod(SUPP_ENTROPY_FILE, 0660) < 0) {
 LOGE("Error changing permissions of %s to 0660: %s",
      SUPP_ENTROPY_FILE, strerror(errno));
 unlink(SUPP_ENTROPY_FILE);
 return -1;
    }

    if (chown(SUPP_ENTROPY_FILE, AID_SYSTEM, AID_WIFI) < 0) {
 LOGE("Error changing group ownership of %s to %d: %s",
      SUPP_ENTROPY_FILE, AID_WIFI, strerror(errno));
 unlink(SUPP_ENTROPY_FILE);
 return -1;
    }
    return 0;
}

int update_ctrl_interface(const char *config_file) {

    int srcfd, destfd;
    int nread;
    char ifc[PROPERTY_VALUE_MAX];
    char *pbuf;
    char *sptr;
    struct stat sb;

    if (stat(config_file, &sb) != 0)
 return -1;

    pbuf = malloc(sb.st_size + PROPERTY_VALUE_MAX);
    if (!pbuf)
 return 0;
    srcfd = open(config_file, O_RDONLY);
    if (srcfd < 0) {
 LOGE("Cannot open \"%s\": %s", config_file, strerror(errno));
 free(pbuf);
 return 0;
    }
    nread = read(srcfd, pbuf, sb.st_size);
    close(srcfd);
    if (nread < 0) {
 LOGE("Cannot read \"%s\": %s", config_file, strerror(errno));
 free(pbuf);
 return 0;
    }

    if (!strcmp(config_file, SUPP_CONFIG_FILE)) {
 property_get("wifi.interface", ifc, WIFI_TEST_INTERFACE);
    } else {
 strcpy(ifc, "wlan0");
    }
    if ((sptr = strstr(pbuf, "ctrl_interface="))) {
 char *iptr = sptr + strlen("ctrl_interface=");
 int ilen = 0;
 int mlen = strlen(ifc);
 int nwrite;
 if (strncmp(ifc, iptr, mlen) != 0) {
     LOGE("ctrl_interface != %s", ifc);
     while (((ilen + (iptr - pbuf)) < nread) && (iptr[ilen] != '\n'))
  ilen++;
     mlen = ((ilen >= mlen) ? ilen : mlen) + 1;
     memmove(iptr + mlen, iptr + ilen + 1, nread - (iptr + ilen + 1 - pbuf));
     memset(iptr, '\n', mlen);
     memcpy(iptr, ifc, strlen(ifc));
     destfd = open(config_file, O_RDWR, 0660);
     if (destfd < 0) {
  LOGE("Cannot update \"%s\": %s", config_file, strerror(errno));
  free(pbuf);
  return -1;
     }
     write(destfd, pbuf, nread + mlen - ilen -1);
     close(destfd);
 }
    }
    free(pbuf);
    return 0;
}

int ensure_config_file_exists(const char *config_file)
{
    char buf[2048];
    int srcfd, destfd;
    struct stat sb;
    int nread;
    int ret;

    ret = access(config_file, R_OK|W_OK);
    if ((ret == 0) || (errno == EACCES)) {
 if ((ret != 0) &&
     (chmod(config_file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
     LOGE("Cannot set RW to \"%s\": %s", config_file, strerror(errno));
     return -1;
 }
 /* return if filesize is at least 10 bytes */
 if (stat(config_file, &sb) == 0 && sb.st_size > 10) {
     return update_ctrl_interface(config_file);
 }
    } else if (errno != ENOENT) {
 LOGE("Cannot access \"%s\": %s", config_file, strerror(errno));
 return -1;
    }
 if (wpa_mode == 0)
        srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
 else
        srcfd = open(P2P_SUPP_CONFIG_TEMPLATE, O_RDONLY);
    if (srcfd < 0) {
 LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
 return -1;
    }

    destfd = open(config_file, O_CREAT|O_RDWR, 0660);
    if (destfd < 0) {
 close(srcfd);
 LOGE("Cannot create \"%s\": %s", config_file, strerror(errno));
 return -1;
    }

    while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
 if (nread < 0) {
     LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
     close(srcfd);
     close(destfd);
     unlink(config_file);
     return -1;
 }
 write(destfd, buf, nread);
    }

    close(destfd);
    close(srcfd);

    /* chmod is needed because open() didn't set permisions properly */
    if (chmod(config_file, 0660) < 0) {
 LOGE("Error changing permissions of %s to 0660: %s",
      config_file, strerror(errno));
 unlink(config_file);
 return -1;
    }

    if (chown(config_file, AID_SYSTEM, AID_WIFI) < 0) {
 LOGE("Error changing group ownership of %s to %d: %s",
      config_file, AID_WIFI, strerror(errno));
 unlink(config_file);
 return -1;
    }
    return update_ctrl_interface(config_file);
}

/**
 * wifi_wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
 * may be left over from clients that were previously connected to
 * wpa_supplicant. This keeps these files from being orphaned in the
 * event of crashes that prevented them from being removed as part
 * of the normal orderly shutdown.
 */
void wifi_wpa_ctrl_cleanup(void)
{
    DIR *dir;
    struct dirent entry;
    struct dirent *result;
    size_t dirnamelen;
    size_t maxcopy;
    char pathname[PATH_MAX];
    char *namep;
    char *local_socket_dir = CONFIG_CTRL_IFACE_CLIENT_DIR;
    char *local_socket_prefix = CONFIG_CTRL_IFACE_CLIENT_PREFIX;

    if ((dir = opendir(local_socket_dir)) == NULL)
 return;

    dirnamelen = (size_t)snprintf(pathname, sizeof(pathname), "%s/", local_socket_dir);
    if (dirnamelen >= sizeof(pathname)) {
 closedir(dir);
 return;
    }
    namep = pathname + dirnamelen;
    maxcopy = PATH_MAX - dirnamelen;
    while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
 if (strncmp(entry.d_name, local_socket_prefix, strlen(local_socket_prefix)) == 0) {
     if (strlcpy(namep, entry.d_name, maxcopy) < maxcopy) {
  unlink(pathname);
     }
 }
    }
    closedir(dir);
}

int wifi_start_supplicant_common(const char *config_file)
{
    char daemon_cmd[PROPERTY_VALUE_MAX];
    char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
    char driver[PROPERTY_VALUE_MAX] = {'\0'};
 char wpa_postfix[PROPERTY_KEY_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

 memset(SUPPLICANT_NAME, 0, sizeof(SUPPLICANT_NAME));
 memset(SUPP_PROP_NAME, 0, sizeof(SUPP_PROP_NAME));
 if (WIFI_ID == WIFI_ATHEROS_AR9271) {
  sprintf(wpa_postfix, "%s", "ar9271");
 } else {
  sprintf(wpa_postfix, "%s", "supplicant");
 }

 sprintf(SUPPLICANT_NAME, "wpa_%s", wpa_postfix);
 sprintf(SUPP_PROP_NAME, "init.svc.wpa_%s", wpa_postfix);

    /* Check whether already running */
    if (property_get(SUPP_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(config_file) < 0) {
 LOGE("Wi-Fi will not be enabled");
 return -1;
    }

    if (ensure_entropy_file_exists() < 0) {
 LOGE("Wi-Fi entropy file was not created");
    }

    /* Clear out any stale socket files that might be left over. */
    wifi_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

 if (WIFI_ID == WIFI_RALINK_RT3070 || WIFI_ID == WIFI_RALINK_RT5370
    || WIFI_ID == WIFI_RALINK_RT5372) {
  strcpy(driver, "wext");
 } else {
#ifdef CONFIG_DRIVER_NL80211
    strcpy(driver, "nl80211");
#else
    strcpy(driver, "wext");
#endif
 }
    property_get("wifi.interface", iface, WIFI_TEST_INTERFACE);
    snprintf(daemon_cmd, PROPERTY_VALUE_MAX, "%s:-i%s -D%s -c%s",
                 SUPPLICANT_NAME, iface, driver, config_file);
    property_set("ctl.start", daemon_cmd);
    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) {
  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)
  return 0;
 }
#endif
 usleep(100000);
    }
    return -1;
}

int wifi_start_supplicant()
{
 wpa_mode = 0;
    return wifi_start_supplicant_common(SUPP_CONFIG_FILE);
}

int wifi_start_p2p_supplicant()
{
 wpa_mode = 1;
    return wifi_start_supplicant_common(P2P_CONFIG_FILE);
}

int wifi_stop_supplicant()
{
    char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
    int count = 50; /* wait at most 5 seconds for completion */

    /* Check whether supplicant already stopped */
    if (property_get(SUPP_PROP_NAME, supp_status, NULL)
 && strcmp(supp_status, "stopped") == 0) {
 return 0;
    }

    property_set("ctl.stop", SUPPLICANT_NAME);
    sched_yield();

    while (count-- > 0) {
 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
     if (strcmp(supp_status, "stopped") == 0)
  return 0;
 }
 usleep(100000);
    }
    return -1;
}

int wifi_connect_to_supplicant()
{
    char ifname[256];
    char supp_status[PROPERTY_VALUE_MAX] = {'\0'};

    /* Make sure supplicant is running */
    if (!property_get(SUPP_PROP_NAME, supp_status, NULL)
     || strcmp(supp_status, "running") != 0) {
 LOGE("Supplicant not running, cannot connect");
 return -1;
    }

    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) {
 LOGE("Unable to open connection to supplicant on \"%s\": %s",
      ifname, strerror(errno));
 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;
    }

    if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets) == -1) {
 wpa_ctrl_close(monitor_conn);
 wpa_ctrl_close(ctrl_conn);
 ctrl_conn = monitor_conn = NULL;
 return -1;
    }

    return 0;
}

int wifi_send_command(struct wpa_ctrl *ctrl, const char *cmd, char *reply, size_t *reply_len)
{
    int ret;

    if (ctrl_conn == NULL) {
 LOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
 return -1;
    }
    ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), reply, reply_len, NULL);
    if (ret == -2) {
 LOGD("'%s' command timed out.\n", cmd);
 /* unblocks the monitor receive socket for termination */
 write(exit_sockets[0], "T", 1);
 return -2;
    } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
 return -1;
    }
    if (strncmp(cmd, "PING", 4) == 0) {
 reply[*reply_len] = '\0';
    }
    return 0;
}

int wifi_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
{
    int res;
    int ctrlfd = wpa_ctrl_get_fd(ctrl);
    struct pollfd rfds[2];

    memset(rfds, 0, 2 * sizeof(struct pollfd));
    rfds[0].fd = ctrlfd;
    rfds[0].events |= POLLIN;
    rfds[1].fd = exit_sockets[1];
    rfds[1].events |= POLLIN;
    res = poll(rfds, 2, -1);
    if (res < 0) {
 LOGE("Error poll = %d", res);
 return res;
    }
    if (rfds[0].revents & POLLIN) {
 return wpa_ctrl_recv(ctrl, reply, reply_len);
    } else {
 LOGD("Received on exit socket, terminate");
 return -1;
    }
    return 0;
}

int wifi_wait_for_event(char *buf, size_t buflen)
{
    size_t nread = buflen - 1;
    int fd;
    fd_set rfds;
    int result;
    struct timeval tval;
    struct timeval *tptr;

    if (monitor_conn == NULL) {
 LOGD("Connection closed\n");
 strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1);
 buf[buflen-1] = '\0';
 return strlen(buf);
    }

    result = wifi_ctrl_recv(monitor_conn, buf, &nread);
    if (result < 0) {
 LOGD("wifi_ctrl_recv failed: %s\n", strerror(errno));
 strncpy(buf, WPA_EVENT_TERMINATING " - recv error", buflen-1);
 buf[buflen-1] = '\0';
 return strlen(buf);
    }
    buf[nread] = '\0';
    /* LOGD("wait_for_event: result=%d nread=%d string=\"%s\"\n", result, nread, buf); */
    /* Check for EOF on the socket */
    if (result == 0 && nread == 0) {
 /* Fabricate an event to pass up */
 LOGD("Received EOF on supplicant socket\n");
 strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
 buf[buflen-1] = '\0';
 return strlen(buf);
    }
    /*
     * Events strings are in the format
     *
     *     CTRL-EVENT-XXX
     *
     * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
     * etc.) and XXX is the event name. The level information is not useful
     * to us, so strip it off.
     */
    if (buf[0] == '<') {
 char *match = strchr(buf, '>');
 if (match != NULL) {
     nread -= (match+1-buf);
     memmove(buf, match+1, nread+1);
 }
    }
    return nread;
}

void wifi_close_supplicant_connection()
{
    char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
    int count = 50; /* wait at most 5 seconds to ensure init has stopped stupplicant */

    if (ctrl_conn != NULL) {
 wpa_ctrl_close(ctrl_conn);
 ctrl_conn = NULL;
    }
    if (monitor_conn != NULL) {
 wpa_ctrl_close(monitor_conn);
 monitor_conn = NULL;
    }

    if (exit_sockets[0] >= 0) {
 close(exit_sockets[0]);
 exit_sockets[0] = -1;
    }

    if (exit_sockets[1] >= 0) {
 close(exit_sockets[1]);
 exit_sockets[1] = -1;
    }

    while (count-- > 0) {
 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
     if (strcmp(supp_status, "stopped") == 0)
  return;
 }
 usleep(100000);
    }
}

int wifi_command(const char *command, char *reply, size_t *reply_len)
{
    return wifi_send_command(ctrl_conn, command, reply, reply_len);
}

const char *wifi_get_fw_path(int fw_type)
{
    switch (fw_type) {
    case WIFI_GET_FW_PATH_STA:
 return WIFI_DRIVER_FW_PATH_STA;
    case WIFI_GET_FW_PATH_AP:
 return WIFI_DRIVER_FW_PATH_AP;
    case WIFI_GET_FW_PATH_P2P:
 return WIFI_DRIVER_FW_PATH_P2P;
    }
    return NULL;
}

int wifi_change_fw_path(const char *fwpath)
{
    int len;
    int fd;
    int ret = 0;

    if (!fwpath)
 return ret;
    fd = open(WIFI_DRIVER_FW_PATH_PARAM, O_WRONLY);
    if (fd < 0) {
 LOGE("Failed to open wlan fw path param (%s)", strerror(errno));
 return -1;
    }
    len = strlen(fwpath) + 1;
    if (write(fd, fwpath, len) != len) {
 LOGE("Failed to write wlan fw path param (%s)", strerror(errno));
 ret = -1;
    }
    close(fd);
    return ret;
}

int find_wifi_device(int id, const char **id_table)
{
 int i, ret = INVALID_DEIVCE;
 FILE *stream = NULL;
 char *p = NULL; char cmdStr[64] = {0};
 char resStr[1024] = {0};

 sprintf(cmdStr, "%s", "/system/busybox/sbin/lsusb");
 stream = popen(cmdStr, "r");
 if (stream == NULL)
  return INVALID_DEIVCE;

 fread(resStr, sizeof(char), sizeof(resStr), stream);
 for (i = 0; i < id; i++) {
  p = strstr(resStr, id_table[i]);
  if (p != NULL)
   ret = i;
 }
 pclose(stream);

 return ret;
}

int get_wifi_device(void)
{
 int i;
 i = sizeof(wifi_id_table) / sizeof(wifi_id_table[0]);

 return find_wifi_device(i, wifi_id_table);
}

你可能感兴趣的:(编程语言,通讯)