Add hardware access to the application frameworks layer for Android in Ubuntu _android

Source: Internet
Author: User
Tags stub throwable

With the rapid advances in digital technology today, the perfect combination of software and hardware has created the popularity of smart mobile devices. The scramble for iOS and Android today is partly due to the variety of apps 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 develop a hardware device that naturally has less software engineers writing drivers for it, and the driver's ultimate goal is to enable top-level applications to use the services provided by these hardware to provide software functionality to users. For applications on Android, it's about providing hardware services to the application frameworks layer of the system. In the previous articles, we focused on the Linux kernel layer, the hardware abstraction layer, and the custom hardware service interfaces provided by the Run-time library, which are implemented through the C or C + + language. In this article, we will describe how to provide hardware services for the Java interface in the application frameworks layer of the Android system.

A. Refer to the JNI method call layer for the hardware Abstraction Layer module, as shown in the article, providing Java Access Hardware service interface in the Ubuntu Android Hal programming Jni method .

two. In the Android system, hardware services are typically run in a separate process to serve a variety of applications. Therefore, the communication between the application that calls these hardware services and those hardware services needs to be done through the broker. To do this, we first define the communication interface. Enter the Frameworks/base/core/java/android/os directory and add the Ihelloservice.aidl interface definition file:

user-name@machine-name:~/android$ CD Frameworks/base/core/java/android/os

user-name@machine-name:~/android/frameworks/base/core/java/android/os$ VI ihelloservice.aidl

Ihelloservice.aidl defines the Ihelloservice interface:

Package Android.os; 
 
Interface Ihelloservice { 
 void setval (int val); 
 int Getval (); 

The Ihelloservice interface mainly provides the device and the ability to obtain the value of the hardware register Val, which is implemented by Setval and getval two functions respectively.

Three. Return to Frameworks/base directory, open android.mk file, modify Local_src_files variable value, add ihelloservice.aidl source file:

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

 updating this

 list of aidl files , consider if that Aidl is

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

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

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

 # If you are intend for 3rd Parties to is 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 Ihelloservice.aidl interface:

user-name@machine-name:~/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, add the Helloservice.java file:

Package com.android.server; 
Import Android.content.Context; 
Import Android.os.IHelloService; 
Import Android.util.Slog; 
public class HelloService extends Ihelloservice.stub { 
 private static final String TAG = "HelloService"; 
 HelloService () { 
 init_native (); 
 } 
 public void Setval (int val) { 
 setval_native (val); 
 } 
 public int Getval () {return 
 getval_native (); 
 } 
 
 private static native Boolean init_native (); 
 private static native void setval_native (int val); 
 private static native int getval_native (); 

HelloService is primarily by invoking JNI methods Init_native, Setval_native, and getval_native (see Java Access Hardware service interface in Ubuntu Android Hal writing Jni method ) to provide hardware services.

Six. Modify the Systemserver.java file in 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);

 } 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 repackage system.img:

user-name@machine-name:~/android$ mmm Frameworks/base/services/java

user-name@machine-name:~/android$ make Snod

In this way, the repackaged system.img system mirroring file contains our customized hardware service HelloService in the application frameworks layer, and automatically loads HelloService when the system is started. At this point, the application can access the Hello hardware service through the Java interface. We'll look forward to the next article describing how to write a Java application to call this HelloService interface to access the hardware.

The above is the Android application frameworks layer to increase hardware access service implementation method, follow-up continue to supplement the relevant knowledge, thank you for your support!

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.