Android HAL Notes

vmlinz posted @ Tue, 26 Apr 2011 11:18:40 +0800 in Android with tags linux Android hal , 3737 readers

Android HAL

Android Hal 是用来隔离linux内核和Android用户空间的一个中间库,它主要是为了让一些硬件厂商不用根据linux内核的gpl协议放出全部的linux驱动代码。有了hal硬件厂商就可以只在内核驱动中实现基本的操作而把硬件操作的逻辑移到hal层来实现,hal层是apache协议的,所以厂商可以只提供二进制库。

0.HAL Arch


app -> framework -> jni -> runtime lib -> libhardware -> hal stub -> hal imp -> kernel driver

1.HAL implementations


  • framework -> jni -> device driver,直接通过jni操作硬件驱动
  • framework -> jni -> libhardware -> hal stub -> device driver,通过hal stub操作硬件驱动


第二种方式则通过hal stub来给上层提供一个统一的调用接口,只要在hal中实现这些接口,用户层的代码就可以不用做太大的改变。

2.HAL api

新的stubbed hal的实现和它提供的api(hardware/libhardware)




  • struct hw_module_t; 从libhardware加载的动态库中获取的模块结构体
  • struct hw_module_methods_t; 模块操作方法,打开模块,获取设备结构体
  • struct hw_device_t; 设备以及操作方法


int hw_get_module(const char *id, const struct hw_module_t **module);

  • id: HAL module id
  • *module: HAL module operations


这个接口的主要工作是通过id和系统的设置获取到硬件共享库的路径,然后通过dlopen打开共享库并通过一个固定的符号找到struct hw_module_t在内存中的地址,然后再通过该结构体提供的方法来打开设备并操作设备。

3.Lights service分析

light service是使用hal stub的一个典型例子,它的java service通过jni调用hal stub来操作驱动给用户空间提供的接口来实现硬件操作的。

3.0.lights service的实现


  • static light_device_t* get_device(hw_module_t* module, char const* name); 打开设备模块
  • static jint init_native(JNIEnv *env, jobject clazz); 获取模块
  • static void setLight_native(JNIEnv *env, jobject clazz, int ptr, int light, int colorARGB, int flashMode, int onMS, int offMS, int brightnessMode) 设置light的颜色和亮度等参数


  • struct light_device_t; light设备结构体,其中有设置light的函数指针
  • int (*set_light)(struct light_device_t* dev, struct light_state_t const* state); 设置light的函数指针


  • struct led_prop; led属性, filename指led属性在sysfs中的路径,fd指向打开的文件描述符 struct led_prop { const char *filename; int fd;};

  • static int open_lights(const struct hw_module_t* module, char const* name, struct hw_device_t** device) lights模块的打开函数实现,使用名字来设置不同类型的light


AAA said:
Sun, 24 Apr 2022 22:14:27 +0800

Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks. Selling Credit Card Machines

Jimmy said:
Thu, 20 Oct 2022 08:07:00 +0800

I was just looking for the same post to translate some of the part for my  article and its research part. Good to reach your post. Thank you.


Invisalign in Dubai said:
Mon, 22 Jan 2024 22:14:37 +0800

I’m so glad to see your article.Really a fantastic work.Stay informed and stay in the loop with the latest insights on food, fashion, technology, beauty, health, and wellness. If you're considering orthodontic treatment in Dubai, explore the comprehensive services listed by the leading orthodontists in the city. Discover top-notch services, Retainers, Dental Braces,Aligners, Invisalign including Damon braces, dental braces, metal braces, and invisible braces. Find the best orthodontic braces services in Dubai, tailored to meet your specific needs. Keep yourself updated on the latest trends and make informed decisions about your orthodontic care with the help of the finest orthodontists in Dubai.

Hyperpigmentation tr said:
Thu, 25 Jan 2024 16:30:17 +0800

Excellent work! I thoroughly enjoyed reading the article on this platform. Engaging with content that revolves around beauty, fashion, technology, medical advancements, and food is truly my forte. Allow me to recommend a highly trending aesthetic clinic, Al Biraa Clinic for Skin Pigmentation treatment in Dubai, UAE. I'll be sharing this insightful article with my network and friends. Thank you for the valuable information!

Login *

loading captcha image...
(type the code from the image)
or Ctrl+Enter