Added hardware access to the Android application frameworks layer on Ubuntu

Source: Internet
Author: User
Tags throwable

Article reprinted to CSDN community Luo Shenyang's Android tour, original address: http://blog.csdn.net/luoshengyang/article/details/6578352

With the rapid development of digital technology today, the perfect combination of software and hardware has created the popularity of smart mobile devices. The popularity of iOS and Android today is partly due to the variety of applications on both systems. Therefore, the relationship between software and hardware, to a certain extent, can be said that the hardware is for software services. Hardware engineers developed a hardware device that is less natural for software engineers to write drivers for, and the ultimate purpose of the driver is to enable top-level applications to use the services provided by these hardware to provide software functionality to users. For the application software on the Android system, it is to provide the hardware service on the application frameworks layer of the system. In the previous articles, we highlighted the custom hardware service interfaces provided by the Linux kernel layer, the hardware abstraction layer, and the runtime Library layer, all implemented through the C or C + + language. In this article, we will show you how to provide hardware services for the Java interface on the application frameworks layer of the Android system.

I. Refer to the article on the Java Access Hardware service interface in Ubuntu for authoring the Jni method for the Android hardware Abstraction Layer (HAL) module, and prepare the JNI method call layer for the hardware Abstraction Layer module.

Two. In Android, hardware services are typically run in a separate process that serves a variety of applications. Therefore, the communication between applications that invoke these hardware services and those hardware services needs to be done through a proxy. To do this, we need to define the communication interface first. Go to the Frameworks/base/core/java/android/os directory and add the Ihelloservice.aidl interface definition file:

[email protected]:~/android$ cd Frameworks/base/core/java/android/os

[Email protected]:~/android/frameworks/base/core/java/android/os$ VI ihelloservice.aidl

Ihelloservice.aidl defines the Ihelloservice interface:

[Java]View Plaincopy
    1. Package Android.os;
    2. Interface Ihelloservice {
    3. void Setval (int val);
    4. int getval ();
    5. }

The Ihelloservice interface mainly provides the function of the device and the value of obtaining the Val of the hardware register, respectively, by the Setval and getval two functions.

Three. Return to the Frameworks/base directory, open the Android.mk file, modify the value of the Local_src_files variable, and add the Ihelloservice.aidl source file:

# READ ME: ########################################################

##

# # When updating this list of aidl files, consider if-aidl is

# # Part of the SDK API. If It is, also add it to the list below that

# are preprocessed and distributed with the SDK. This list should

# # contain any aidl files for Parcelables, but the one below should

# # If you intend for 3rd parties to be able to send those objects

# # Across process boundaries.

##

# READ ME: ########################################################

Local_src_files + =/

....................................................................

Core/java/android/os/ivibratorservice.aidl/

Core/java/android/os/ihelloservice.aidl/

Core/java/android/service/urlrenderer/iurlrendererservice.aidl/

.....................................................................

Four. Compile the Ihelloservice.aidl interface: [email protected]:~/android$ mmm frameworks/base

In this way, the corresponding Ihelloservice.stub interface is generated based on the ihelloservice.aidl.

Five. Enter the Frameworks/base/services/java/com/android/server directory and add the Helloservice.java file:

[Java]View Plaincopy
  1. Package com.android.server;
  2. Import Android.content.Context;
  3. Import Android.os.IHelloService;
  4. Import Android.util.Slog;
  5. Public class HelloService extends Ihelloservice.stub {
  6. private static final String TAG = "HelloService";
  7. HelloService () {
  8. Init_native ();
  9. }
  10. public void Setval (int val) {
  11. Setval_native (Val);
  12. }
  13. public int Getval () {
  14. return getval_native ();
  15. }
  16. private static native Boolean init_native ();
  17. private static native void setval_native (int val);
  18. private static native int getval_native ();
  19. };

HelloService is primarily done by invoking the Jni method Init_native, Setval_native, and Getval_native ( See the article "Java Access Hardware service Interface" provided by Ubuntu for the Android hardware Abstraction Layer (HAL) module, which provides hardware services.

Six. Modify the Systemserver.java file of the same directory and add the code to load the HelloService in the Serverthread::run function:

@Override

public void Run () {

....................................................................................

try {

SLOG.I (TAG, "Diskstats Service");

Servicemanager.addservice ("Diskstats", new Diskstatsservice (context));

} catch (Throwable e) {

SLOG.E (TAG, "Failure starting Diskstats Service", e);

}

try {

SLOG.I (TAG, "Hello Service");

Servicemanager.addservice ("Hello", New HelloService ());

} catch (Throwable e) {

SLOG.E (TAG, "Failure starting Hello Service", e);

}

......................................................................................

}

Seven. Compile HelloService and repack system.img:

[email protected]:~/android$ mmm Frameworks/base/services/java

[Email protected]:~/android$ make Snod

This way, the repackaged system.img system image file contains our custom hardware service HelloService in the application frameworks layer, and when the system starts, Load HelloService automatically. At this point, the application can access the Hello hardware service through the Java interface. We will describe in the next article how to write a Java application to call this HelloService interface to access the hardware, so please look forward to it.

Lao Luo's Sina Weibo: Http://weibo.com/shengyangluo, welcome attention!

Added hardware access to the Android application frameworks layer on Ubuntu

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.