Developing Android Hardware Access services

Source: Internet
Author: User

In http://blog.csdn.net/getnextwindow/article/details/47731597, the HAL module was added to the Android system, and after the development of the good one hardware abstraction layer, We also typically need to implement a hardware access service in the application framework. The Hardware Access service provides read and write operations to the application through the hardware Abstraction Layer (HAL) module. Because the hardware abstraction layer is developed using C + +, and the hardware Access service in the application framework is developed in the Java language, the hardware Access service must invoke the interface of the Abstraction Layer module through JNI.

The hardware access services of the Android system are typically run in system process systems, and applications that use these hardware access services run in a different process, so the binder mechanism is required for process communication.

The next step is to develop a hardware access service Fregservice, which implements the Hardware Access service interface Ifregservice. This article first introduces the definition of Hardware Access Service Interface Ifregservice, then introduces the implementation of hardware Access service Fregservice, including its internal implementation and the JNI implementation of invoking the HAL module. Finally, it describes how to start the Hardware Access Service Fregservice in system process systems.

1. Hardware Access Service Interface definition
The Android system uses AIDL to define the service interface for cross-process access, the interface file suffix is aidl, and the compilation system converts the Aidl file into a Java file and compiles it. This section uses Aidl to define Ifregservice.
In the android4.0+ system, the Hardware Access service interface is defined in the following directory:

 frameworks/base/core/java/android/os/IFregService.aidl

IFREGSERVICE.AIDL is defined as follows:

1 package android.os;23interface IFregService {4void setVal(int val);5int getVal();6 }

Because the service interface Ifregservice is described in the Aidl language, we need to add it to the compilation script file so that the compilation system can convert it to a Java file and then compile it. The modified Android.mk file is located under the Frameworks/base directory, adding the following values to the Local_src_files variable:

services/instaboot/java/com/droidlogic/instaboot/IInstabootManager.aidl
LOCAL_SRC_FILES+=        ....

After the compilation script has been modified, we can compile the service interface ifregservice with the following command:

mmm ./frameworks/base/

The compilation results are as follows:

target dex:frameworkCopying:  out /target/common/obj/java_libraries/framework_intermediates/classes< Span class= "Hljs-preprocessor" >.dex  target jar:framework (out /target/ Common/obj/java_libraries/framework_intermediates/javalib.jar ) install:  out /target/product/magicbox_m13/system/ Framework/framework.jar   

We see the final compilation of the Framework.jar file, which contains the Ifregservice interface, which inherits the Android.os.IInterface interface. A binder local object stub is defined within Ifregservice, implements the Ifregservice interface, and inherits the Android.os.Binder class. In addition, within the Ifregservice.stub class, a Binder proxy object proxy is defined, which also implements the Ifregservice interface. How can I determine if the internal Framework.jar contains an Ifregservice interface? Very simple, Android Framework.jar through the Anti-compilation tool can clearly see the source code, the specific process of anti-compilation in the article http://blog.csdn.net/GetNextWindow/article/details/47748493 introduced.
The anti-compilation results are as follows:

2. Implementing Hardware Access Services
Fregservice inherits from the Ifregservice.stub class and implements the member functions Setval and Getval of the Ifregservice interface.
Hardware access services are typically implemented in the following directories:

 base/services/java/com/android/server/FregService.java

How to compile:
Mmm./frameworks/base/service/

Result: The generated Services.jar file contains the Fregservice class.

3. Implementing a JNI approach to hardware access services
In the Android system, the JNI method of the hardware Access service is implemented in the following directory:

frameworks/base/services/core/jni/com_android_server_FregService.cpp

Once the JNI code is written, we need to modify:

frameworks/base/services/core/jni/onload.cpp

Add Register_android_fregservice declarations and calls inside, onload.cpp files are implemented in the Libandroid_servers module, when the system loads the Libandroid_servers module, The Jni_onload function in the Onload.cpp file is called, which registers the Jni method with the virtual machine.

Finally, we need to modify:

frameworks/base/services/core/jni/Android.mk

Modifies the value of the variable local_src_files, increasing the com_android_server_fregservice.app.
How to compile:
Mmm./frameworks/base/services
Result: The Jni method is included in the generated libandroid_servers.so file

4: Start the hardware service
The following files need to be modified to run the hardware service in system process systems:

frameworks/base/services/java/com/android/server/SystemServe                   r.java

Add the following code to it:

            try {                        "Freg Service");                        ServiceManager.addService("freg"new FregService());                catch (Throwable e) {                        "Failure starting Freg Service", e);                }

How to compile:
Mmm./frameworks/base/services
Result: The Services.jar file is generated that contains the hardware access service Fregservice, and it is run in the system process systems when the system starts.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Developing Android Hardware Access services

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.