Android Deep Explore HAL Reading notes 9

Source: Internet
Author: User

After reading the Nineth chapter of this book, I Learned:

    1. The HAL (abstract hardware layer) is a set of libraries built on the Linux driver, which is the application layer above the kernel layer-the System runtime layer.
    2. Linux driver code type: Access to hardware register code and business logic code.
    3. The Linux kernel uses the GPL, which requires the source code must be open source, that is, the Linux driver must open source.
    4. Android adds Hal's purpose: ① avoids application direct access to Linux driver ② to protect private property, satisfies the requirements of Linux drivers who do not want open source, Linux driver with HAL is equivalent to upload data from HAL to register, that is, from register to Hal's "Data Setter".

Summary: 1) The calling interface of the unified hardware. 2) solve the GPL copyright issue: Since Linux and Android are based on different protocols (GPL, Apache Licence2.0), adding HAL allows sensitive code to get rid of the GPL protocol. 3) Use the HAL code in user space to assist the Linux driver to complete some special requirements.

    1. Hal Initial & New version architecture:

① Initial Architecture: Use the library (. so file) on the Android system to call the Linux driver for kernel space, and then the Android application (. APK) can access the library in the HAL through the NDK program, or access the HAL's library directly in the Android application.

② new version of the schema: added the HAL Schema required entry code in the old schema to be recognized by the Android system. Locate the corresponding stub (. so) by calling the ID. The service library (. So) is located between stub and JNI, which accesses the service library in the HAL using the mechanism of the call HAL provided by the Android system, which locates the HAL Library by ID, then the Android apk and then calls the service library.

    1. The source storage location of the Android HAL is generally stored in the <android source directory >/libhardware directory, or <android source directory >/hardware directory.

Final compiled. So files are primarily placed in the/SYSTEM/LIB/HW directory of the Android system

    1. Adding HAL to the LED driver: the application no longer cares about how Linux drives and device files interact, just like accessing the API to interact with Linux drivers.
    2. Write Linux drivers that support HAL:

① writing Linux drivers: drive code as concise as possible to put business logic into the HAL Library

② writing the HAL Library:hal Library is the Linux library (*.so) file. This type of library file has an interface that is implemented by the HAL_MODULE_INFO_SYM variable. The Service library locates the library through the ID defined in this interface.

③ writing a Service Library:service library can be a Linux library or a JNI library. This chapter LED driver Example Service Library is the JNI library. This step includes a service management class (Sevicemanager) written in Java, in addition to the *.so library file, which is called by the Services LIBRARY,APK program, which invokes the class to access the service library.

    1. The LED driver using the HAL layer retains only the code that creates the device file and register interaction, and reads and writes the specified register in the read and write functions of the device file. Rationale: Reading or writing 5 bytes of data from a specified register can read and write to the specified register, the first byte specifies the read-write action and the Register type, and the second 4 registers are the actual data to read and write.
    2. LED driver code with HAL layer in addition to the read, write functions, the other code is essentially the same as without the HAL layer. The read and write functions remove all LED-related logic and use only the IOWRITE32 and IOREAD32 functions to read and write the specified registers.

The first byte (Mem[0]) of the MEM array is obtained before the read function reads the data in order to determine the register type.

    1. Enter the HAL layer directory to perform #sh build.sh compiling and installing LED drivers. The script file references a common.sh script file that implements the Find_devices function to display a list of Android devices and allows the user to select an Android device and finally return the device ID.
    2. Test read-Write registers: via command line Arguments "Rwdev <r|w> <dev_file> <byte_count> [byte1] [Byte2] ... [Byten] "passes information such as the device file name, Bytes, bytes to be passed, and so on. Execute the build.sh script file in the Read_write_dev directory, call the cross-compiler (ARM-NONE-LINUX-GNUEABI-GCC) compile rw_dev.c directly, and use the ADB push command to upload the Rwdev command to the Development Board/ Data/local directory.
    3. Write the HAL module that calls the LED driver: The HAL module consists of LEDS_HAL.C and leds_hal.h. Leds_hal.h is used to define structs, macros, and header files that refer to some systems. LEDS_HAL.C references the leds_hal.h and leds_hal_define.h header files.

① defining structs and Macros: hw_module_t,hw_device_t,hw_module_methods_t

② writes the HAL module's Open function: Is the HAL's entry point, which works: Initializes the hw_device_t, opens the device file, initializes the register

③ defining hw_module_methods_t struct-body variables

④ defines the HAL_MODULE_INFO_SYM variable: The variable type is generally hw_module_t or its child structure, which typically initializes some of the member variables of the hw_module_t or its substructure, where the ID represents the identity of the Android system in the HAL module, A program using the HAL module finds and loads the HAL module through this ID; the methods variable points to the address of the hw_module_methods_t struct.

⑤ writing the close function of the HAL module

⑥ writing functions for controlling LEDs: led_on, Led_off

    1. Create a Leds_hal symbolic link in the hardware subdirectory of the Android source directory, create a android.mk file in the Leds_hal directory, compile with the "MM" command
    2. Execute the build.sh script file "#sh build.sh" to compile and install the HAL module
    3. The ADB shell command enters the development Board terminal, and the HAL module is usually placed under/SYSEM/LIB/HW with the file name "led_hal.default.so"
    4. Writing a service that calls the HAL module
    5. Writing a Java library that invokes a service
    6. Test LED Driver

Android Deep Explore HAL Reading notes 9

Related Article

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.