小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

【轉(zhuǎn)】Android HAL hardware module分析 以GPS module為例

 techres 2012-02-07

準(zhǔn)備在QT210的android系統(tǒng)中實(shí)現(xiàn)GPS功能,程序已經(jīng)寫好了,也可以編譯出gps.s5pc110.so的庫(kù),但是不知道上層怎么調(diào)用這個(gè)庫(kù),在GPS相關(guān)的Android.mk中也沒有找到,很是納悶。通過分析hardware module才知道是怎么回事,之前并沒有詳細(xì)的了解hardware module,現(xiàn)在簡(jiǎn)單的分析一下。

HAL層的hardware module主要實(shí)現(xiàn)文件為:
hardware/libhardware/hareware.c
hardware/libhardware/include/hardware/hardware.h

重要的結(jié)構(gòu)體:
  1. typedef struct hw_device_t {
  2.     uint32_t tag; /** tag must be initialized to HARDWARE_DEVICE_TAG */ 標(biāo)識(shí)符
  3.     uint32_t version; /** version number for hw_device_t */             版本號(hào)
  4.     struct hw_module_t* module; /** reference to the module this device belongs to */   該硬件屬于哪個(gè)module
  5.     uint32_t reserved[12]; /** padding reserved for future use */
  6.     int (*close)(struct hw_device_t* device); /** Close this device */  關(guān)閉設(shè)備操作
  7. } hw_device_t;
表示一個(gè)硬件設(shè)備,存儲(chǔ)了各種硬件設(shè)備的公共屬性和方法

  1. typedef struct hw_module_t {
  2.     uint32_t tag; /** tag must be initialized to HARDWARE_MODULE_TAG */ 標(biāo)識(shí)符
  3.     uint16_t version_major; /** major version number for the module */ 主版本號(hào)
  4.     uint16_t version_minor; /** minor version number of the module */ 次版本號(hào)
  5.     const char *id; /** Identifier of module */                    id,決定了庫(kù)的文件名
  6.     const char *name; /** Name of this module */                    模塊的名字
  7.     const char *author; /** Author/owner/implementor of the module */   模塊作者
  8.     struct hw_module_methods_t* methods; /** Modules methods */         模塊的操作方法
  9.     void* dso; /** module's dso */
  10.     uint32_t reserved[32-7]; /** padding to 128 bytes, reserved for future use */
  11. } hw_module_t;
描述一個(gè)硬件模塊

  1. typedef struct hw_module_methods_t {
  2.     int (*open)(const struct hw_module_t* module, const char* id,
  3.             struct hw_device_t** device);
  4. } hw_module_methods_t;
定義了操作設(shè)備的方法,只有一個(gè)open函數(shù)

hareware.c分析:
  1. /** Base path of the hal modules */ 指定硬件模塊庫(kù)存放的路徑
  2. #define HAL_LIBRARY_PATH1 "/system/lib/hw"
  3. #define HAL_LIBRARY_PATH2 "/vendor/lib/hw"

下面這個(gè)用于搜索不同的庫(kù)文件名:
  1. /**
  2.  * There are a set of variant filename for modules. The form of the filename
  3.  * is "<MODULE_ID>.variant.so" so for the led module the Dream variants
  4.  * of base "ro.product.board", "ro.board.platform" and "ro.arch" would be:
  5.  *
  6.  * led.trout.so
  7.  * led.msm7k.so
  8.  * led.ARMV6.so
  9.  * led.default.so
  10.  */

  11. static const char *variant_keys[] = {
  12.     "ro.hardware",
  13.     "ro.product.board",
  14.     "ro.board.platform",
  15.     "ro.arch"
  16. };
庫(kù)的名稱要根據(jù)實(shí)際的product或board指定
在210平臺(tái)GPS模塊庫(kù)的文件名可以為:
gps.s5pc110.so
gps.ARMV7.so
gps.default.so

hw_get_module函數(shù)作用是在系統(tǒng)中查找模塊的庫(kù)文件
根據(jù)硬件模塊的id參數(shù)和指定的查找路徑,配合product,board等變量,最終指定到一個(gè)so庫(kù)文件
  1. int hw_get_module(const char *id, const struct hw_module_t **module)
---------------------------------------------------------------------------------
  1.     /* Loop through the configuration variants looking for a module */
  2.     for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {
  3.         if (i < HAL_VARIANT_KEYS_COUNT) {
  4.             if (property_get(variant_keys[i], prop, NULL) == 0) {
  5.                 continue;
  6.             }
  7.             snprintf(path, sizeof(path), "%s/%s.%s.so",       //system/lib/hw/*.*.so
  8.                     HAL_LIBRARY_PATH1, id, prop);
  9.             if (access(path, R_OK) == 0) break;

  10.             snprintf(path, sizeof(path), "%s/%s.%s.so",
  11.                      HAL_LIBRARY_PATH2, id, prop);
  12.             if (access(path, R_OK) == 0) break;
  13.         } else {
  14.             snprintf(path, sizeof(path), "%s/%s.default.so",    //如果沒查到硬件屬性,使用默認(rèn)的庫(kù)文件名system/lib/hw/*.default.so
  15.                      HAL_LIBRARY_PATH1, id);
  16.             if (access(path, R_OK) == 0) break;
  17.         }
  18.     }

創(chuàng)建GPS modules分析:

下面這段話很重要,說明了如何構(gòu)建一個(gè)模塊
  1. /**
  2.  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
  3.  * and the fields of this data structure must begin with hw_module_t
  4.  * followed by module specific information.
  5.  */

可以說這是andorid中標(biāo)準(zhǔn)的構(gòu)建模塊的寫法
  1. const struct hw_module_t HAL_MODULE_INFO_SYM = {
  2.     .tag = HARDWARE_MODULE_TAG,
  3.     .version_major = 1,
  4.     .version_minor = 0,
  5.     .id = GPS_HARDWARE_MODULE_ID,
  6.     .name = "XC-GPS Module",
  7.     .author = "WWW.XCEMBED.COM",
  8.     .methods = &hw_module_methods,
  9. };

通過分析hw_get_module函數(shù)我們知道,id是用來指定模塊庫(kù)文件名的,給GPS_HARDWARE_MODULE_ID賦值
#define GPS_HARDWARE_MODULE_ID  "gps"
實(shí)際上GPS_HARDWARE_MODULE_ID并不是在代碼中這樣定義的,可能是在某個(gè)編譯腳本里,我暫時(shí)還沒找到

指定methods:
  1. static struct hw_module_methods_t hw_module_methods = {
  2.     .open = open_gps
  3. };
只有一個(gè)open_gps函數(shù),當(dāng)模塊加載時(shí)就會(huì)執(zhí)行open_gps:
  1. static int open_gps(const struct hw_module_t* module, char const* name,
  2.                     struct hw_device_t** device)
  3. {
  4.     struct gps_device_t *gps_device = malloc(sizeof(struct gps_device_t));
  5.     if (gps_device)
  6.     {
  7.         memset(gps_device, 0, sizeof(*gps_device));
  8.         gps_device->common.tag = HARDWARE_DEVICE_TAG;
  9.         gps_device->common.version = 0;
  10.         gps_device->common.module = (struct hw_module_t*)module;
  11.         gps_device->get_gps_interface = gps_get_hardware_interface;

  12.         *device = (struct hw_device_t*)gps_device;

  13.         return 0;
  14.     }

  15.     return 1;
  16. }

每個(gè)硬件設(shè)備都應(yīng)該有一個(gè)類似xxxx_device_t的結(jié)構(gòu)體,里面包含了hw_device_t
gps的設(shè)備結(jié)構(gòu)體定義如下:
  1. struct gps_device_t {
  2.     struct hw_device_t common;
  3.     const GpsInterface* (*get_gps_interface)(struct gps_device_t* dev);
  4. };
在open函數(shù)中給這個(gè)模塊所包含的hw_device_t賦值

get_gps_interface函數(shù)用來得到GPS的操作接口
  1. const GpsInterface* gps_get_hardware_interface(struct gps_device_t* dev)
  2. {
  3.     return &_GpsInterface;
  4. }

接下來,程序的主體部分就是實(shí)現(xiàn)_GpsInterface里面所定義的所有操作

HAL層的模塊是由具體的上一層JNI來調(diào)用,關(guān)于怎么調(diào)用的,下一篇分析GPS module時(shí)再說,我也不是很清楚,再整理一下

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多