HAL development for Android operating system

Source: Internet
Author: User

1. The HAL of Android is designed to protect the proprietary Driver proposed by some hardware providers. In short, it is designed to avoid the limitations of GPL license of Linuxkernal. Android puts all the hardware control actions in the user space, while in the kernel driver, there is only the simplest operation to read and write registers, all the operations that reflect the hardware features are placed on the HAL layer of Android, while Android is based on the license of Aparch, therefore, hardware vendors can only provide binary code. Therefore, an open platform for Android knowledge is not an open source platform.

2. The implementation of HAL in Android requires JNIJava Native Intereface. In short, JNI allows java programs to call Dynamic Link Libraries written in C/C ++. In this case, HAL can be written in C/C ++ with higher efficiency. Android apps can call. so directly or through app-> app_manager-> service (java)-> service (jni)-> HAL. The second method looks complicated, but is more in line with the android architecture.

3. 1) Kernel Driver

The kernel driver is the same as the real driver in linux. It also provides open, read, write, ioctl, mmap, and other interfaces to write data to the Register, as for how to write and why to write, these tasks will be performed at the HAL layer, but the code is not visible to users. This is why linux mainstream kicked android kernel out, because these drivers cannot be used on other linux platforms.

2) This layer is located on the user space above the kernel. Generally, two structs are involved: hw_module_t and hw_device_t, the first struct is the initialization operation provided by hw_get_module () when the hardware stub is loaded, for example, the stub openmodule-> methods-> open () operation, the second struct provides the interface for operating the hardware of the hardware stub. In the mokoid project of jollen, it mainly provides the operation for opening and closing the led. The file is led. h and file led. c. These two files will eventually be compiled into a dynamic link library, which is worse than libled. so is put in/system/libs/hw/. When the service calls hw_get_module (hardware/libhardware/hardware. c) the corresponding dynamic link library will be searched in/system/libs/hw/and then provided to the corresponding service Operation interface.


Process and Lifecycle have five levels of priority. The following list shows the priority levels:

Foreground Process

Visualized Process

Service Process

Background Process

Empty Process

4. Learn how to collaborate in different languages, especially how to implement garbage collection and multithreading. Integrate a virtual machine into a program written in C/C ++. The system environment refers to the local operating system, which has its own local database and CPU instruction set. Local program Native Applications) is written in a local language such as C/C ++, compiled into binary code that can only be run in the local system environment, and linked together with the local database. Local programs and local libraries generally depend on a specific local system environment. JNI is used only when local code is called in the same process.

5. Use a JAVA program to call the C function to print ", helloworld! ", This process includes the following steps:

1. Create a class Hello World. java) to declare the local method. Declare a local method in Java code that must have an "native" identifier and a native modifier. in java code, the local method only exists as a declaration. Before calling a local method, you must first load the local library containing the method. As shown in HelloWorld. java, place it in the static block. When Java VM initializes a class, it first executes this part of code, which ensures that the local library is loaded before the local method is used.

2. Use javac to compile the source file HelloWorld. java to generate HelloWorld. class. Use javah-jni to generate the C header file HelloWorld. h). This header file contains the function prototype of the local method.

3. Use C code to write the implementation of the function prototype.

4. Compile the C function implementation into a local library to create a Hello-World.dll or libHello-World.so.

5. Run the HelloWorld program using java commands, and the class file Hello-World.class and local library are loaded at runtime.

5. When using JNI, developers are most often asked how data is transmitted between JAVA and C/C ++, and how data types are mapped to each other.

Java_HelloWorld_print (JNIEnv *, jobject); this function declaration accepts two parameters, and the corresponding Java code has no parameters for the function declaration. The first parameter is a pointer to the JNIEnv structure; the second function is the HelloWorld object itself, that is, the this pointer. JNIEnv is one of the core databases of JNI and has a very high position. All calls to JNI must use this struct. Note: The jni. h file must be included. This file defines all the functions and data types of JNI. Generate the name of the local database, which must be the same as System. loadLibrary ("HelloWorld"); Name of the database to be loaded. -MD: ensure that Windows is connected to the Win32 multi-threaded C library, including static, dynamic, and dynamic multithreading... Library C)-LD: generate dynamic link library

6. Comparison between one-to-one ing and shared stubs

The main advantage of shared stubs is that programmers do not have to write a lot of stub functions in local code. The one-to-one ing feature is efficient because it does not require too many additional data type conversions.



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.