On Ubuntu additional hardware Access Service (5) for the Android Application Frameworks layer system

      In today's rapidly changing digital technology, the perfect combination of software and hardware, creating a popular smart mobile devices. Today, they rush to iOS and Android system, partly due to the variety of applications with a variety of software on both systems. Therefore, the relationship between software and hardware, to a certain extent, can be said that the hardware for software and services. Hardware engineers developed a hardware device, a software engineer for its natural and less to write drivers; the ultimate aim driver is to make the top applications to use hardware to provide these services to provide software functionality to users. Application software on the Android system, is to provide hardware services in Application Frameworks layer system. In previous articles, we highlight the Linux kernel layer, the hardware abstraction layer and run custom hardware layer provides a service interface library, these interfaces are via C or C ++ language to achieve. In this article, we will describe how to provide hardware services in the Java interface layer Application Frameworks Android system.

"Android system source code Scenario Analysis," a book under attack programmer network ( http://0xcc0xcd.com in) serial, click to enter!

      A reference (HAL) module in Ubuntu for Android hardware abstraction layer written in Java JNI method to access the hardware service interfaces a shown below, ready to JNI method call level hardware abstraction layer module.

      II. In the Android system, hardware service is normally run in a separate process to provide services for a variety of applications. Therefore, the communication between the application calls these hardware services and hardware services need to be done by proxy. To this end, we must first define good communication interface. Into frameworks / base / core / java / android / os directory, add IHelloService.aidl interface definition files:

      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 defined IHelloService interfaces:


   
   
  1. package android.os;
  2. interface IHelloService {
  3. void setVal(int val);
  4. int getVal () ;
  5. }

IHelloService provides the main interface hardware register and retrieve the value of the val function, and are achieved by setVal getVal two functions.

Three return to frameworks / base directory, Android.mk open the file, change the value of the variable LOCAL_SRC_FILES increase 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

   ## not 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 IHelloService.aidl Interface:
     USER-NAME@MACHINE-NAME:~/Android$ mmm frameworks/base

   Thus, the interface will generate the corresponding IHelloService.Stub according IHelloService.aidl.

   Five into the frameworks / base / services / java / com / android / server directory, add HelloService.java file:


   
   
  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 mainly by calling the JNI method init_native, setVal_native and getVal_native (see in Ubuntu for Android hardware abstraction layer (HAL) modules written in Java JNI approach provides access to the hardware service interface article) to provide hardware services.

     Six modify the same file directory SystemServer.java increased load HelloService code 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 compiled HelloService and repackaged system.img:

     USER-NAME@MACHINE-NAME:~/Android$ mmm frameworks/base/services/java

     USER-NAME@MACHINE-NAME:~/Android$ make snod

     In this way, system.img system image file contains the re-packaged our custom hardware service HelloService in Application Frameworks layer, and when the system will boot automatically load HelloService. At this time, the application can be accessed via Java Hello hardware interface to the service. We will describe in the next article how to write a Java application to call this HelloService interface to access the hardware, so stay tuned.


Guess you like

Origin blog.csdn.net/ll148305879/article/details/93032013