Write the Jni method for Android hardware abstraction Layer (HAL) module in Ubuntu to provide Java access to the Hardware service interface (Lao Luo study Note 4)

Source: Internet
Author: User

In the previous two articles, we described how to write drivers for the hardware of an Android system, including how to implement kernel drivers in Linux kernel space and implement hardware abstraction layer interfaces in user space. The purpose of both is to provide hardware access to the next level, which is to provide hardware services for the Android application frameworks layer. We know that the Android application is written in the Java language, and the hardware driver is implemented in C language, then how to access theJava interface C interface? as is well known, Java provides JNI method calls, and similarly, in Android systems,Java applications invoke the hardware abstraction layer interface through JNI . In this article, we'll show you how to write JNI methods for the Android hardware Abstraction layer interface so that the upper-level Java applications can use the hardware services provided below.

I. Refer to the Add Hardware Abstraction Layer (HAL) module on Ubuntu to access the Linux kernel driver article and prepare the Hardware Abstraction Layer Module to ensure that the Android system image file System.img already contains Hello.default module.

Two. Go to the Frameworks/base/services/jni directory and create a new Com_android_server_helloservice.cpp file:

[email protected]:~/android$ cd Frameworks/base/services/jni

[Email protected]:~/android/frameworks/base/services/jni$ VI com_android_server_helloservice.cpp

in the Com_android_server_helloservice.cpp file, implement the Jni method. Note the command method of the file, the Com_android_server prefix represents the package name, which indicates that the hardware service HelloService is placed in the Frameworks/base/services/java directory Com/android/ In the server directory, there is a class that has a command of Com.android.server.HelloService. Here, we temporarily omit the description of the HelloService class, and in the next article we will go back to the HelloService class. To put it simply,HelloService is a hardware Access service class that provides Java interfaces .

The first is to include the corresponding header file:

#defineLog_tag "HelloService"//#include"jni.h"Some function definition declarations used by the//JNI layer # include"JNIHelp.h"//?? Each head of the role of the file, to seek the expert guidance?????????????????? Thank you #include"android_runtime/androidruntime.h"//? #include<utils/misc.h>//? #include<utils/Log.h>//? #include#include#include<stdio.h>

Then define the three JNI methods of Hello_init, Hello_getval, and Hello_setval:

namespaceAndroid {/*hardware access structure defined in the hardware abstraction layer, reference */          structhello_device_t* Hello_device =NULL; /*sets the value of the hardware register Val by the hardware interface defined by the hardware abstraction layer*/          Static voidHello_setval (jnienv*env, Jobject clazz, Jint value) {          intval =value; Logi ("Hello Jni:set value%d to device.", Val); if(!Hello_device) {Logi ("Hello Jni:device is not open."); return; } Hello_device-Set_val (Hello_device, Val); }          /*reads the value of the hardware register Val from the hardware interface defined by the hardware abstraction layer*/      StaticJint Hello_getval (jnienv*env, Jobject clazz) {          intval =0; if(!Hello_device) {Logi ("Hello Jni:device is not open."); returnVal; } Hello_device->get_val (Hello_device, &val); Logi ("Hello Jni:get value%d from device.", Val); returnVal; }          /*Hardware modules defined through the hardware abstraction layer open the interface to open the hardware device*/      StaticInlineintHello_device_open (Consthw_module_t* module,structhello_device_t**device) {          returnModule->methods->open (module, hello_hardware_module_id, (structhw_device_t**) device); }          /*load the specified hardware abstraction Layer module with hardware module ID and turn on the hardware*/      StaticJboolean Hello_init (jnienv*env, Jclass clazz) {hello_module_t*module; Logi ("Hello jni:initializing ..."); if(Hw_get_module (hello_hardware_module_id, (Const structhw_module_t**) &module) = =0) {Logi ("Hello Jni:hello Stub found."); if(Hello_device_open (& (Module->common), &hello_device) = =0) {Logi ("Hello Jni:hello device is open."); return 0; } LOGE ("Hello jni:failed to open Hello device."); return-1; } LOGE ("Hello jni:failed to get hello stub module."); return-1; }          /*Jni Method Table*/      Static ConstJninativemethod method_table[] = {          {"init_native","() Z", (void*) Hello_init}, {"setval_native","(I) V", (void*) Hello_setval}, {"getval_native","() I", (void*) Hello_getval},}; /*registering the Jni method*/      intRegister_android_server_helloservice (JNIENV *env) {              returnJniregisternativemethods (ENV,"Com/android/server/helloservice", Method_table, Nelem (method_table));  }  }; 

Note that in the Hello_init function, the hardware Abstraction Layer module with module ID hello_hardware_module_id is loaded through the Hw_get_module method provided by the Android hardware abstraction layer, where hello_ HARDWARE_MODULE_ID is defined in

Three. Modify the Onload.cpp file in the same directory, first add the Register_android_server_helloservice function declaration on 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);        .................................................................................................      }       This will automatically load the Jni method call table when the Android system is initialized.        Four .  modify the Android.mk file in the same directory, adding a line to the Local_src_files variable:     &NBSP;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 \     &NBSP;COM_ Android_server_location_gpslocationprovider.cpp \       Com_android_server_helloservice.cpp/Onload.cpp Five. Compiling and re-searching for billion system.img: [email protected]:~/android$ mmm frameworks/base/services/jni [email protected]:~/android$ make Snod       In this way, the repackaged system.img image file contains the Jni method we just wrote, which means that we can invoke these JNI methods through the hardware service HelloService provided by the application frameworks layer of the Android system. In turn, the hardware abstraction layer interface of the lower level is called to access the hardware. As mentioned earlier, in this article, we temporarily ignore the implementation of the HelloService class, and in the next article we will describe how to implement hardware service HelloService, so stay tuned.

Write the Jni method for Android hardware abstraction Layer (HAL) module in Ubuntu to provide Java access to the Hardware service interface (Lao Luo study Note 4)

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.