Thursday, January 24, 2013

android WIFI 架構和控制流程

本文轉自: http://neilchen0710.pixnet.net/blog/post/84945078-android-wifi-%E6%9E%B6%E6%A7%8B%E5%92%8C%E6%8E%A7%E5%88%B6%E6%B5%81%E7%A8%8B


一、WIFI的基本架構
    1wifi用戶空間的程式和庫:
         external/wpa_supplicant/
       
生成庫libwpaclient.so和守護進程wpa_supplicant
    2hardware/libhardware_legary/wifi/wifi管理庫。
    3JNI部分:
         frameworks/base/core/jni/android_net_wifi_Wifi.cpp
    4JAVA部分:
         frameworks/base/services/java/com/android/server/
         frameworks/base/wifi/java/android/net/wifi/
    5WIFI Settings應用程式位於:
       packages/apps/Settings/src/com/android/settings/wifi/
   6WIFI 驅動模組  wlan.ko
        wpa_supplicant通過wireless_ext 介面和驅動通信
   7WIFI 硬體模組 
二、WIFIAndroid中如何工作

   Android使用一個修改版wpa_supplicant作為daemon來控制WIFI,代碼位於
   external/wpa_supplicantwpa_supplicant是通過socket
   hardware/libhardware_legacy/wifi/wifi.c
通信。UI通過android.net.wifi package
   
frameworks/base/wifi/java/android/net/wifi/)發送命令給wifi.c
   
相應的JNI實現位於frameworks/base/core/jni/android_net_wifi_Wifi.cpp
   
更高一級的網路管理位於frameworks/base/core/java/android/net
  
三、配置Android支援WIFI

   BoardConfig.mk中添加:
      BOARD_HAVE_WIFI := true
      BOARD_WPA_SUPPLICANT_DRIVER := WEXT
 
   
這將在external/wpa_supplicant/Android.mk設置WPA_BUILD_SUPPLICANTtrue
   
默認使用驅動driver_wext.c
   
如果使用定制的wpa_supplicant驅動(例如 madwifi),可以設置:
       BOARD_WPA_SUPPLICANT_DRIVER := MADWIFI
  
四、使能wpa_supplicant調試信息

   默認wpa_supplicant設置為MSG_INFO,為了輸出更多資訊,可修改:
   1
、在common.c中設置wpa_debug_level = MSG_DEBUG;
   2
、在common.c中把#define wpa_printf宏中的
      if ((level) >= MSG_INFO)
      
改為
      if ((level) >= MSG_DEBUG)

五、配置wpa_supplicant.conf
 
   wpa_supplicant
是通過wpa_supplicant.conf中的ctrl_interface=來指定控制socket的,應該在
   AndroidBoard.mk中配置好複製到$(TARGET_OUT_ETC)/wifi(也就是
   /system/etc/wifi/wpa_supplicant.conf
   
這個位置會在init.rc中再次檢測的。
   
一般的wpa_supplicant.conf配置為:
      ctrl_interface=DIR=/data/system/wpa_supplicant GROUP=wifi
      update_config=1
      fast_reauth=1 
   
有時,驅動需要增加:
      ap_scan=1
   如果遇到AP連接問題,需要修改ap_scan=0來讓驅動連接,代替wpa_supplicant
 
   
如果要連接到non-WPA or open wireless networks,要增加:
      network={
              key_mgmt=NONE
      }

六、配置路徑和許可權

   Google修改的wpa_supplicant要運行在wifi用戶和組下的。代碼可見wpa_supplicant/os_unix.c
   中的os_program_init()函數。
 
   
如果配置不對,會出現下面錯誤:
      E/WifiHW  (  ): Unable to open connection to supplicant on
      "/data/system/wpa_supplicant/wlan0": No such file or directory will appear.

   
確認init.rc中有如下配置:
       mkdir /system/etc/wifi 0770 wifi wifi
       chmod 0770 /system/etc/wifi
       chmod 0660 /system/etc/wifi/wpa_supplicant.conf
       chown wifi wifi /system/etc/wifi/wpa_supplicant.conf
       # wpa_supplicant socket
       mkdir /data/system/wpa_supplicant 0771 wifi wifi
       chmod 0771 /data/system/wpa_supplicant
       #wpa_supplicant control socket for android wifi.c
       mkdir /data/misc/wifi 0770 wifi wifi
       mkdir /data/misc/wifi/sockets 0770 wifi wifi
       chmod 0770 /data/misc/wifi
       chmod 0660 /data/misc/wifi/wpa_supplicant.conf

   如果系統的/system目錄為唯讀,那應該使用路徑/data/misc/wifi/wpa_supplicant.conf
  
七、運行wpa_supplicantdhcpcd
 
   
init.rc中確保有如下語句:
      service wpa_supplicant /system/bin/logwrapper /system/bin/wpa_supplicant -dd
                   -Dwext -iwlan0 -c /data/misc/wifi/wpa_supplicant.conf
         user root
         group wifi inet
      socket wpa_wlan0 dgram 660 wifi wifi
         oneshot
      service dhcpcd /system/bin/logwrapper /system/bin/dhcpcd -d -B wlan0
         disabled
         oneshot

   根據所用的WIFI驅動名字,修改wlan0為自己驅動的名字。
   
七、編譯WIFI驅動為modulekernel built in

   1、編譯為module
      
BoardConfig.mk中添加:
         WIFI_DRIVER_MODULE_PATH := "/system/lib/modules/ar6000.ko"
         WIFI_DRIVER_MODULE_ARG := ""  #for example nohwcrypt
         WIFI_DRIVER_MODULE_NAME := "ar6000"  #for example wlan0
         WIFI_FIRMWARE_LOADER := ""        
 
   2
、編譯為kernel built in 
     1
)在hardware/libhardware_legacy/wifi/wifi.c要修改interface名字,
     2
)在init.rc中添加:
        setprop wifi.interface "wlan0"
     3
)在hardware/libhardware_legacy/wifi/wifi.c中當insmod/rmmod時,
        
直接return 0
 
八、WIFI需要的firmware

   Android不使用標準的hotplug binaryWIFI需要的firmware要複製到/etc/firmware
 
   
或者複製到WIFI驅動指定的位置,然後WIFI驅動會自動載入。

九、修改WIFI驅動適合Android

   Google修改的wpa_supplicant要求SIOCSIWPRIV ioctl發送命令到驅動,及接收資訊,例如signal
   strength, mac address of the AP, link speed等。所以要正確實現WIFI驅動,需要從
   SIOCSIWPRIV ioctl返回RSSI (signal strength)MACADDR資訊。

   如果沒實現這個ioctl,會出現如下錯誤:
     E/wpa_supplicant(  ): wpa_driver_priv_driver_cmd failed
                               wpa_driver_priv_driver_cmd RSSI len = 4096
     E/wpa_supplicant(  ): wpa_driver_priv_driver_cmd failed
     D/wpa_supplicant(  ): wpa_driver_priv_driver_cmd LINKSPEED len = 4096
     E/wpa_supplicant(  ): wpa_driver_priv_driver_cmd failed
     I/wpa_supplicant(  ): CTRL-EVENT-DRIVER-STATE HANGED
   
十、設置dhcpcd.conf
 
   
一般/system/etc/dhcpcd/dhcpcd.conf的配置為:
      interface wlan0
      option subnet_mask, routers, domain_name_server

Monday, January 14, 2013

EnhanceIO: New Solid State Drive Caching For Linux

http://www.phoronix.com/scan.php?page=news_item&px=MTI3Mzc

Posted by Michael Larabel on January 13, 2013

A commercial company has opened up their Linux driver that is based upon their SSD (Solid-State Drive) caching software product. This code is designed to use SSDs as cache devices for traditional rotating hard drives. This new SSD caching driver is based upon Facebook's Flashcache

Flashcache is the project Facebook open-sourced in 2011 that is a kernel-level solution for providing a block cache for Linux that supports multiple caching models. Flashcache can be used to accelerate reads and writes from slower rotational media by caching the data on a solid-state drive. 

Based upon Flashcache, STEC Inc has opened up their EnhanceIO SSD caching software for Linux. EnhanceIO uses SSDs as cache devices for traditional HDDs and works with any block device whether it be an actual physical device, a disk partition, a RAID-ed DAS device, SAN volumes, and device mapper volumes. 

EnhanceIO supports caching modes of read-only, write-through, and write-back. The cache replacement policies are random, FIFO, and LRU. 

STEC Inc didn't simply make a couple of changes to Flashcache and call it something new, but rather they have made some significant and original improvements. Over Flashcache, EnhanceIO adds a new write-back enhance that's designed from scratch, transparent cache support not using the Linux device mapper, large I/O support, a smaller memory footprint, supports loadable replacement policies, optimal alignment of data blocks on SSDs, improved device failure handling, and code optimizations. 

The EnhanceIO code was originally opened in late December while on Saturday the work was just announced on the Linux kernel mailing list. "STEC is happy to announce hosting of our EnhanceIO SSD caching software on github. We would like to invite kernel hackers to try it. We'll appreciate your valuable feedback to help us improve it to the standards of Linux kernel source code. We hope to eventually submit it for a possible inclusion in Linux kernel." 

The GPL-licensed code to EnhanceIO can be found on GitHub.