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;}