顯示具有 Android 標籤的文章。 顯示所有文章
顯示具有 Android 標籤的文章。 顯示所有文章

2010年11月30日 星期二

Porting lsusb & libusb on Android.

目的
在Android User Mode 下,透過 libusb 操作 USB Device,如:送出 Control MSG 至 Device。


參考文件
Using Android in Industrial Automation
Technical Report
A student semester project at the University of Applied Sciences Northwestern Switzerland for the Institute of Automation
Authors: Manuel Di Cerbo, and Andreas Rudolf
Project principal: Matthias Meier ⃝
FHNW/IA, 29.01.2010


 源碼下載
libusb-1.0.8.tar.bz2


檔案放置
libusb:  /external/libusb-1.0.8/
lsusb:  /external/lsusb/lsusb.c


編譯及安裝
  1. 新增 libusb-1.0.8/Android.mk
    ifeq ($(TARGET_ARCH),arm)

    LOCAL_PATH:= $(call my-dir)

    subdirs := $(addprefix $(LOCAL_PATH)/,$(addsuffix /Android.mk, \
    libusb \
    ))

    include $(subdirs)

    endif


  1. 新增 lsusb/Android.mk
    LOCAL_PATH := $(call my-dir)

    include $(CLEAR_VARS)
    LOCAL_SRC_FILES := lsusb.c
    LOCAL_MODULE := lsusb
    LOCAL_C_INCLUDES += external/libusb-1.0.8/
    LOCAL_SHARED_LIBRARIES := libc libusb
    include $(BUILD_EXECUTABLE)


  1. 修改libusb-1.0.8/libusb/io.c
    #ifndef TIMESPEC_TO_TIMEVAL
    #define TIMESPEC_TO_TIMEVAL(tv, ts)                                            \
           do {                                                                \
                   (tv)->tv_sec = (ts)->tv_sec;                                    \
                   (tv)->tv_usec = (ts)->tv_nsec / 1000;                        \
           } while(0)
    #endif /* end of TIMESPEC_TO_TIMEVAL */

    為了解決
    E: undefined reference to ‘TIMESPEC_TO_TIMEVAL’
    S: defined TIMESPEC_TO_TIMEVAL macro in libusb/io.c

  1. 在 memory map(/build/core/prelink-linux-arm.map 中加入 libusb.so(會依 platform 不同而有所不同)
    # extended system libraries
    libusb.so                0xAE600000
    libril.so                0xAE400000

    如果沒有定義 memory address,在編譯時會產生底下的錯誤訊息:
    E: build/tools/apriori/prelinkmap.c(137): library ’libusb.so’ not in prelink map
    S: build/core/prelink-linux-arm.map



  1. 在 init.rc 的 on boot 節(session) 中加入 mount usbfs 的命令:
    # usbfs
           mkdir /proc/bus/usb 0771
           mount usbfs none /proc/bus/usb -o devmode=0666

    重新make & download image 試試看在 shell 底下 lsusb 能不能動囉!

      2010年6月8日 星期二

      Android Reference-RIL -- 初始化/訊息處理 簡略文字流程

      RIL 初始化流程:

      1. RILD 起動 RIL OBJ(ril.cpp) 中 RIL_StartLoop,其 New Thread 執行 RIL Event (ril_event.cpp) ::ril_event_loop() 為 for(;;) loop 等待處理 event list 中的訊息(event)
      2. RIL daemon(RILD) 載入 Libreferece-ril.so 
      3. RILD 透過 Dynaminc Loader(DL) dlsym() 取得 Librerfernce-ril.so 中的 RIL_Init function address(*rilInit).
      4. RILD 呼叫 (*rilInit) 取得 Reference-RIL callback function table[].
      callback function table[] --
      .RIL_VERSION,
      .onRequest(),
      .currentState(),
      .onSupport(),
      .onCancel(),
      .getVersion(),
      1. RILD 透過 RIL OBJ :: RIL_Register() 將 callback function table[] 儲存在 RIL OBJ. 並且取得 RIL socket-s_fdListen,等待連線。

      <<缺圖說明>>
      RIL 事件處理流程:
      1. RIL OBJ :: listenCallback 檢查是否有從 Phone process 的連線。
      2. 一旦確認為 Phone Process 連線,開取讀取 command,將其 Command Data 及 handle function (RIL :: processCommandsCallback) 設定至 RIL Event,並交至 Event Loop 處理。
      3. RIL Event :: ril_event_loop() 檢查是否有訊息待處理。會觸發 RIL Event :: firePending() ,其中會呼叫 Event Element 中的 handle function。[該步驟會重覆執行至所有訊息被處理結束。]
      4. 通 常 handle function 都被設定為 RIL :: processCommandsCallback(),結束 Stream data 後,即呼叫 RIL OBJ :: processCommandBuffer() 。
      5. RIL OBJ :: processCommandBuffer() 將依上層的 Request Number 利用 s_command[] table 轉換成相對應之 Request data 。
      6. 並且呼叫 s_command[] command 已定義的 dispatchFunction()。
      dispatchFunction() 大致分類為:void, string, strings, ints, smsWrite, Dail, SIM_IO, CallForward, Raw, CdmaSMS, CdmaSMSAck, GSMBrSmsCnf, CdmaBrSmsCnf, RilCdmaSMSWrite 這些類別。
      1. dispatchFunction 最終都會呼叫 Ref-RIL(Reference-RIL) callback function table[].onRequest() 並且代入 Request Number。
      2. Ref-RIL :: on Request() 會依 Requset Number 執行相對應之工作。

      2010年6月6日 星期日

      Android (or Android-x86) 開發環境的設置及工具安裝

      <<設定Build code 及環境的文章多不可數,但還是仍然放置在這邊,供自己查詢使用。 >>

      開發環境的設置及工具安裝:

      • 系 統設定 -- Using Ubuntu 10.04 or 9.10
      -- 安裝 JDK5
      # 開啟 APT 設定檔
      $sudo gedit /etc/apt/sources.list

      # 加入 server For sun-java5-jdk
      deb http://ir.archive.ubuntu.com/ubuntu jaunty-updates main multiverse

      # 更新及安裝 JDK5
      $sudo aptitude update
      $sudo aptitude install sun-java5-jdk

      -- 安裝 相關工具
      $sudo apt-get install gcc g++
      $sudo apt-get install flex bison gperf libsdl1.2-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl libncurses5-dev zlib1g-dev valgrind libreadline5-dev

      -- 安裝 GIT
      $sudo apt-get install git-core

      -- 額外的部份: X11/Xatom.h & X11/Xlib.h (非必要):
      # provides Xatom.h
      $sudo apt-get install x11proto-core-dev 
      # provides Xlib.h
      $sudo apt-get install libx11-dev        
      • 建立 bin 目錄
      $mkdir ~/bin

      • 創 建 repo
      $curl http://android.git.kernel.org/repo > ~/bin/repo

              # 變更 repo 為可執行檔
      $chmod a+w ~/bin/repo

      • 把 repo 的路徑加至 $PATH
      # 編輯 ~/ 底下的 .bashrc 檔案,加上底下這行
      export PATH=$HOME/bin:$PATH

      • 建 立workspace
      # 新建目錄放置 Android Source Code. 底下使用 來代表自訂的名稱。
      $mkdir ~/
      $cd ~/
      • 從 官方 server 的 GIT source tree 下載 Source Code
      # 初始化要下載的文件清單
      $repo init -u git://android.git.kernel.org/platform/manifest.git

      # 同步資料庫(下載 Source Code)
      $repo sync
      • 在 有 HTTP Proxy 設定的情況下,我們需要透過 SSH , 把 git port 轉至 http port,所以需要透過 connect.c 程式,相關的設定如下:

      下載 connect.c, http://www.meadowy.org/~gotoh/ssh/connect.c


      請個別使用相關的設定值。
      請個別使用相關的設定值。 可使用 cd ~; pwd 查詢。


      編 譯 connect 執行檔,並且放置在 ~/bin/ 目錄下。
      $gcc connect.c -o connect
      $cp connect ~/bin/.
      $chmod a+x ~/bin/connect

      新增 proxy-cmd.sh, socks-gw.sh, sock-gw.sh 於 ~/bin/ 。
      $cd ~/bin
      $touch proxy-cmd.sh
      $chmod a+x proxy-cmd.sh
      $touch socks-gw.sh
      $chmod a+x socks-gw.sh
      $touch socks-ssh.sh
      $chmod a+x socks-gw.sh

      編輯 ~/bin/proxy-cmd.sh 內容:
      (echo "CONNECT $1:$2 HTTP/1.0"; echo; cat ) | socket | (read a; read a; cat )

      編輯 ~/bin/socks-gw.sh 內容:#!/bin/sh
      # ~/bin/socks-gw.sh
      # This script connects to a HTTP proxy using connect.c
      connect -H : $@

      編輯 ~/bin/socks-ssh.sh 內容:#!/bin/sh
      # ~/bin/socks-ssh.sh
      ssh -o ProxyCommand="/bin/socks-gw.sh %h %p" $@

      新增 proxy.sh 於 ~/ 。
      $cd ~
      $touch proxy.sh
      $chmod a+x proxy.sh

      編輯 proxy.sh 內容:
      #!/bin/sh
      # ~/proxy.sh

      # http proxy setting
      export HTTP_PROXY=http://:
      export http_proxy=$HTTP_PROXY

      # set git to use ssh over http proxy
      export GIT_SSH="/bin/socks-ssh.sh"
      export GIT_PROXY_COMMAND="/bin/socks-gw.sh"

      在 執行 repo sync 前,請先在workspace folder 中執行:
      $source ~/proxy.sh

      參考網頁:http://mmmyddd.freeshell.net/wiki/android/build.html#sec4