Implementation and call of the Android HAL Layer

Source: Internet
Author: User

Implementation and call of the AndroidHAL Layer

1. HAL implementation

In this article, we will discuss the simple composition of HAL-Layer Code and how jni calls HAL-layer code. The article tries its best to avoid useless information and directly write practical things.

Here, I will use a simple HAL-Layer Code (electronic anti-glare hal Code) to explain it.

In a hal code, the main task is to implement a module instance named HAL_MODULE_INFO_SYM. Its structure is defined


[Cpp]
<Span xmlns = "http://www.w3.org/1999/xhtml" style = "">
Struct LCD _reflect_module_t {
 
Struct hw_module_t common;
 
}; </Span>

We can see that the name of this structure is random, which is usually expressed by (Module name _ module_t). We can see that it only has one member variable hw_module_t, that is to say, the main task is to fill in the hw_module_t structure.


[Cpp]
<Span xmlns = "http://www.w3.org/1999/xhtml" style = ""> <span xmlns = "http://www.w3.org/1999/xhtml" style = "">
Const struct LCD _reflect_module_t HAL_MODULE_INFO_SYM = {
 
. Common = {
 
. Tag = HARDWARE_MODULE_TAG,
 
. Version_major = 1,
 
. Version_minor = 0,
 
. Id = LCD _REFLECT_HARDWARE_MODULE_ID,
 
. Name = "LCD Reflect ",
 
. Author = "Archermind Tech .",
 
. Methods = & LCD _reflect_module_methods,
 
}
 
};
</Span>

Tag: it must be specified as HARDWARE_MODULE_TAG and cannot be modified.

Id: moduleID of HALStub. Here # defineLCD_REFLECT_HARDWARE_MODULE_ID "lcdreflect"

Next we need to implement another important structure --- structhw_module_methods_t methods.

Let's take a look at the definition of this struct:

[Cpp]
<Span xmlns = "http://www.w3.org/1999/xhtml" style = ""> <span xmlns = "http://www.w3.org/1999/xhtml" style = "">
Typedef struct hw_module_methods_t {
 
/** Open a specific device */
 
Int (* open) (const struct hw_module_t * module, const char * id,
 
Struct hw_device_t ** device );
 
} Hw_module_methods_t;
</Span>

We can see that there is only one open function pointer, which serves as the module callback.

We have already touched on two important structs, hw_module_t and hw_module_methods_t. Next we need to take a look at the third important struct hw_device_t in the hal layer.

Let's take a look at the open function LCD _reflect_module_methods in hw_module_methods_t.

[Cpp]
<Span xmlns = "http://www.w3.org/1999/xhtml" style = ""> <span xmlns = "http://www.w3.org/1999/xhtml" style = "">
Static int
 
LCD _reflect_open (const struct hw_module_t * module,
 
Const char * name, struct hw_device_t ** device)
 
{
 
Int status =-EINVAL;
 
 
 
LOGV ("LCD _reflect_open \ n ");
 
If (! Strcmp (name, LCD _REFLECT_HARDWARE )){
 
Struct LCD _reflect_device_t * dev;
 
 
 
Dev = malloc (sizeof (* dev ));
 
Memset (dev, 0, sizeof (* dev ));
 
 
 
Dev-> common. tag = HARDWARE_DEVICE_TAG;
 
Dev-> common. version = 0;
 
Dev-> common. module = (struct hw_module_t *) module;
 
Dev-> common. close = LCD _reflect_close;
 
 
 
* Device = & dev-> common;
 
 
 
Dev-> get_level = & LCD _reflect_get_level;
 
Dev-> set_level = & LCD _reflect_set_level;
 
Dev-> get_state = & LCD _reflect_get_state;
 
Dev-> set_state = & LCD _reflect_set_state;
 
 
 
Status = 0;
 
}
 
Return status;
 
} </Span>

We can see that hw_device_t is passed into the open function as the open parameter, and we also find another struct structlcd_reflect_device_t * dev. This structure is defined by our module, it is used to store some operations required by our modules. In the jni layer, this struct is used to call the interfaces provided in the hal layer. Let's take a look at its definition.

[Cpp]
<Span xmlns = "http://www.w3.org/1999/xhtml" style = ""> <span xmlns = "http://www.w3.org/1999/xhtml" style = "">
Struct LCD _reflect_device_t {
 
Struct hw_device_t common;
 
 
 
/**
 
* Get the mxc_reflect state
 
*
 
* Returns: 0 on success, error code on failure
 
*/
 
Int (* get_state) (struct LCD _reflect_device_t * dev,
 
Int * stat );
 
 
 
/**
 
* Set the mxc_reflect state
 
*
 
* Returns: 0 on success, error code on failure
 
*/
 
Int (* set_state) (struct LCD _reflect_device_t * dev,
 
Int stat );
 
 
 
/**
 
* Get the mxc_reflect level
 
*
 
* Returns: 0 on success, error code on failure
 
*/
 
Int (* get_level) (struct LCD _reflect_device_t * dev,
 
Int * level );
 
 
 
/**
 
* Set the mxc_reflect state
 
*
 
* Returns: 0 on success, error code on failure
 
*/
 
Int (* set_level) (struct LCD _reflect_device_t * dev,
 
Int level );
 
};</Span> </span>

It contains the hw_device_t member and is a member at the beginning. This is very important. We will explain why it needs to be placed at the beginning below, in addition to hw_device_t, some hal layers must provide function pointers for jni calls.
Return to the open function, and now we can understand the work done by this open function. It first registers a structlcd_reflect_device_t * dev; LCD _reflect_device_t variable, and then fills in the common, that is, the structure of hw_device_t. Note that there are several members:

Tag: it must be specified as HARDWARE_DEVICE_TAG.

You also need to implement a close function, and then * device = & dev-> common;, that is, associate the hw_device_t of jni with the LCD _reflect_device_t in hal, because the common member is defined at the beginning of LCD _reflect_device_t, the address of LCD _reflect_device_t is the same as that of common. As long as you know the common address, you can know the address of LCD _reflect_device_t. In this way, the LCD _reflect_device_t structure can be transmitted to the jni layer. You only need to forcibly convert the common address. Finally, we need to provide the hal layer to the jni API implementation.

This is a simple hal-Layer Code framework.

2. Call the HAL Layer

In the jni layer, we use the hw_get_module function to obtain the hw_module_t structure, as shown below:

Hw_get_module (LCD _REFLECT_HARDWARE_MODULE_ID, (hw_module_t const **) & module );

Specify LCD _REFLECT_HARDWARE_MODULE_ID to differentiate the module. Then, we need to get the LCD _reflect_device_t * device. This structure can be implemented through the following function:

[Cpp]
<Span xmlns = "http://www.w3.org/1999/xhtml" style = ""> <span xmlns = "http://www.w3.org/1999/xhtml" style = ""> <span xmlns = "http://www.w3.org/1999/xhtml" style = ""> <span xmlns = "http://www.w3.org/1999/xhtml" style = ""> <span xmlns = "http://www.w3.org/1999/xhtml" style = "">
Static LCD _reflect_device_t *
 
Get_device (hw_module_t * module, char const * name)
 
{
 
Int err;
 
Hw_device_t * device;
 
 
 
Err = module-> methods-> open (module, name, & device );
 
If (err = 0 ){
 
Return (LCD _reflect_device_t *) device;
 
} Else {
 
Return NULL;
 
}
 
} </Span>
The function returns the address of the LCD _reflect_device_t structure. In the function, define hw_device_t * device. Then, use module-> methods-> open (module, name, & device ); obtain the address of the hw_device_t structure, and then return the address. before returning the address, you need to forcibly convert the address type (LCD _reflect_device_t *) device to obtain the address of the LCD _reflect_device_t structure. The reason we mentioned above is that the first address of the hw_device_t and LCD _reflect_device_t structures is the same.
With LCD _reflect_device_t, we can call the APIS implemented in the hal layer and use these APIs to operate hardware.

 


From android_huber's column

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.