Summary: How to Use the android Hal Layer

Source: Internet
Author: User

1. Using hal

The following uses the sensor as an example to describe how to use Hal. The specific process is as follows.

 

Step1. native code calls Hal stub through hw_get_module.

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

Step2. enable the device by inheriting the callback of hw_module_methods_t.

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

Step control the device by inheriting the callback (callback function) of hw_device_t.

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

 

2. Compile Hal stub

The basic process for writing Hal stub is as follows.

 

Step1. customize the Hal struct and write the header files led. h and hardware/hardware. h. The main code is as follows.

Struct led_module_t {struct hw_module_t common;}; struct led_control_device_t {struct hw_device_t commom; int FD; // LED Device File tag code // The API int (* set_on) that supports Control) (struct led_control_device_t * Dev, int32_t led); int (* set_off) (struct led_control_device_t * Dev, int32_t led );};

 

Step2. write the LED. c file to implement the Hal stub registration function.

 

Step3. set led_module_methods to inherit from hw_module_methods_t and implement callback for the open () method.

struct hw_module_methods_t led_module_methods = {open: led_device_open};

Step 4. Use hal_module_info_sym to instantiate led_module_t. Note that the names cannot be modified.

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

Note:

Tag: Specifies hardware_module_tag.

ID: the module ID specified as Hal stub.

Methods: The method defined by Hal.

Step 5. open () is a required callback API. It is used to apply for struct controls and fill in information. It can also register specific API interfaces and open the Linux driver.
However, because there are multiple inheritance relationships, you only need to apply for controls for the Child structure hw_device_t object.

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

 

Step 6. Fill in the specific API operations. The specific code is as follows.

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

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.