總結Android HAL層的使用方法

來源:互聯網
上載者:User

1、使用HAL的方法

下面以Sensor感應器為例介紹使用HAL的方法,具體流程如下所示。

 

step1. Native code通過 hw_get_module 調用 HAL stub。

hw_get_module( LED_HARDWARE_MODULE_ID, (const hw_module_t**)&module)

step2. 通過繼承 hw_module_methods_t 的callback來開啟裝置。

module->methods->open(module, LED_HARDWARE_MODULE_ID, (struct hw_device_t**)device);

step3. 通過繼承 hw_device_t 的 callback(回呼函數)來控制裝置。

sLedDevice->set_on( sLedDevice, led);sLedDevice->set_off( sLedDevice, led);

 

2、編寫 HAL stub 的方法

編寫 HAL stub 的基本流程如下所示。

 

step1. 自訂 HAL 結構體,編寫標頭檔 led.h 和 hardware/hardware.h,主要代碼如下所示。

struct led_module_t {struct hw_module_t common;};struct led_control_device_t {struct hw_device_t commom;int fd;// LED裝置檔案標碼// 支援控制的 API介面int (*set_on)(struct led_control_device_t *dev, int32_t led);int (*set_off)(struct led_control_device_t *dev, int32_t led);};

 

step2. 編寫檔案 led.c 實現 HAL stub 註冊功能。

 

step3. 設定 led_module_methods 繼承於hw_module_methods_t,並實現對 open() 方法的回調。

struct hw_module_methods_t led_module_methods = {open: led_device_open};

step4. 使用HAL_MODULE_INFO_SYM 執行個體化 led_module_t,注意各個名稱不可以修改。

const struct led_module_t HAL_MODULE_INFO_SYM = {common: {tag: HARDWARE_MODULE_TAG,version_major: 1,version_minor: 0,id: LED_HARDWARE_MODULE_ID,name: "Sample LED Stub",author: &led_module_methods,}};

補充說明:

tag :表示需要指定為 HARDWARE_MODULE_TAG.

id :表示指定為 HAL Stub 的 module ID。

methods :為 HAL 所定義的方法。

step5. open()是一個必須實現的回調 API,用於負責申請結構體控制項並填充資訊,並且可以註冊具體操作API介面,並開啟linux驅動。
但是因為存在多重繼承關係,所以只需對子結構體 hw_device_t 對象申請控制項即可。

int led_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device){struct led_control_device_t* dev;dev = (struct led_control_device_t *)malloc( sizeof(*dev) );memset(dev, 0, sizeof(*dev) );dev->common.tag = HARDWARE_DEVICE_TAG;dev->common.version = 0;dev->common.module = module;dev->common.close = led_device_close;dev->set_on = led_on;dev->set_off = led_off;*device = &dev->common;// initialize Led hardware heredev->fd = open(LED_DEVICE, O_RDONLY);if( dev->fd < 0 )return -1;led_off(dev, LED_C608);led_off(dev, LED_C609);success:return 0;}

 

step6. 填充具體API操作,具體代碼如下所示。

int led_on(struct led_control_device_t* dev, int32_t led){int fd;LOGI("LED Stub: set %d on.", led);fd = dev->fd;switch(fd){case LED_C608:ioctl(fd, 1, &led);break;case LED_C609:ioctl(fd, 1, &led);break;default:return -1;}return 0;}int led_off(struct led_control_device_t* dev, int32_t led){int fd;LOGI("LED Stub: set %d off.", led);fd = dev->fd;switch(fd){case LED_C608:ioctl(fd, 2, &led);break;case LED_C609:ioctl(fd, 2, &led);break;default:return -1;}return 0;}

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.