In the last two articles, we described how to write drivers for the hardware of the Android system, including how to implement kernel drivers in the Linux kernel space and implement hardware abstraction layer interfaces in user space. Both are implemented to provide hardware access to the upper tier, providing hardware services for the application frameworks layer of Android. We know that Android applications are written in the Java language, and hardware drivers are implemented in C, so how does the Java interface access the C interface? As we all know, Java provides a JNI method call, and also, in Android, Java applications invoke the hardware abstraction layer interface through JNI. In this article, we'll describe how to write JNI methods for the Android hardware Abstraction layer interface so that the upper-tier Java applications can use the hardware services provided by the lower layers.
A. Refer to the article on Adding the Hardware Abstraction Layer (HAL) module in Ubuntu Android to access the Linux kernel driver , prepare the Hardware Abstraction Layer module, Make sure that the Android system image file System.img already contains the Hello.default module.
two. Go to the Frameworks/base/services/jni directory and create a new Com_android_server_helloservice.cpp file:
user-name@machine-name:~/android$ CD Frameworks/base/services/jni
user-name@machine-name:~/android/frameworks/base/services/jni$ VI com_android_server_helloservice.cpp
In the Com_android_server_helloservice.cpp file, the Jni method is implemented. Note the command method of the file, the Com_android_server prefix denotes the package name, which means that the hardware service HelloService is placed under the Frameworks/base/services/java directory Com/android/ Server directory, there is a class with a command of Com.android.server.HelloService. Here, we temporarily omit the description of the HelloService class, and in the next article we'll go back to the HelloService class. Simply put, HelloService is a hardware Access service class that provides a Java interface.
The first is to include the corresponding header file:
#define LOG_TAG "HelloService"
#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/ AndroidRuntime.h "
#include <utils/misc.h>
#include <utils/Log.h>
#include
Then define the Hello_init, Hello_getval, and Hello_setval three JNI methods:
Namespace Android {/* Hardware access structure defined in the hardware abstraction layer, reference
Note that in the Hello_init function, the Hw_get_module method provided by the Android hardware abstraction layer is used to load the hardware Abstraction Layer module with the module ID hello_hardware_module_id, where the Hello_ The hardware_module_id is defined in
three. Modify the Onload.cpp file under the same directory, first add the Register_android_server_helloservice function declaration in namespace Android:
Namespace Android {
..............................................................................................
int Register_android_server_helloservice (jnienv *env);
};
Add Register_android_server_helloservice function call in Jni_onload:
extern "C" Jint jni_onload (javavm* vm, void* reserved)
{
.................................................................................................
Register_android_server_helloservice (env);
.................................................................................................
}
In this way, the JNI method invocation table is loaded automatically when the Android system initializes.
Four. Modify the Android.mk file in the same directory and add one line to the Local_src_files variable:
local_src_files:= \
Com_android_server_alarmmanagerservice.cpp \
Com_android_server_batteryservice.cpp \
Com_android_server_inputmanager.cpp \
Com_android_server_lightsservice.cpp \
Com_android_server_powermanagerservice.cpp \
Com_android_server_systemserver.cpp \
Com_android_server_usbservice.cpp \
Com_android_server_vibratorservice.cpp \
Com_android_server_location_gpslocationprovider.cpp \
Com_android_server_helloservice.cpp/
Onload.cpp
Five. Compile and re-find billion system.img:
user-name@machine-name:~/android$ mmm Frameworks/base/services/jni
user-name@machine-name:~/android$ make snod
In this way, the repackaged system.img mirror file contains the Jni method we have just written, which means that we can invoke these JNI methods through the hardware service HelloService provided by the application frameworks layer of the Android system. Then call the low-level hardware abstraction layer interface to access the hardware. Previously mentioned, in this article, we temporarily ignore the implementation of the HelloService class, in the next article, we will describe how to implement hardware services HelloService, please pay attention.
The above is the process of writing a JNI method to access the Android hardware abstraction layer, continuing to supplement the knowledge, hoping to help students who are studying the Android code.