Compile the JNI method for the hardware abstraction layer (HAL) module of Android in Ubuntu to provide Java interfaces for accessing hardware services.

Source: Internet
Author: User

In the last two articles, we introduced how to write drivers for Android hardware, including how to implement kernel drivers in Linux kernel space and Hardware Abstraction Layer interfaces in user space. The purpose of both is to provide a hardware access interface to the previous layer, that is, to provide hardware services for the Android Application Frameworks layer. We know that android applications are written in Java and hardware drivers are implemented in C. How can we access the C interface through Java interfaces? As we all know, Java provides JNI method calls. Similarly, in Android systems, Java applications call the hardware abstraction layer interface through JNI. In this article, we will introduce how to compile the JNI method for the Android hardware abstraction layer interface so that upper-layer Java applications can use the underlying hardware services.

I. refer to add the hardware abstraction layer (HAL) module to Android on Ubuntu to access the Linux kernel driver. Prepare the hardware abstraction layer module to ensure the Android system image file system. IMG already contains hello. default module.

2. Go to the frameworks/base/services/JNI directory and create the 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, implement the JNI method. Note the command method of the file. The com_android_server prefix indicates the package name, indicating that the hardware service helloservice is placed in the COM/Android/server directory under the frameworks/base/services/Java directory, that is, a command named COM. android. server. helloservice class. Here, we will skip the description of the helloservice class for the moment. In the next article, we will return to the helloservice class. To put it simply, helloservice is a hardware access service class that provides Java interfaces.

First, it contains 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 we define three JNI Methods: hello_init, hello_getval, and hello_setval:

Namespace Android {/* hardware access struct defined in the hardware abstraction layer. For more information, see <Hardware/hello. h> */struct hello_device_t * hello_device = NULL;/* set the value of the hardware register Val through the hardware access interface defined in the hardware abstraction layer */static void hello_setval (jnienv * ENV, jobject clazz, jint value) {int val = 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 );} /* read the Val value of the hardware register through the hardware access interface defined in the hardware abstraction layer */static jint hello_getval (jnienv * ENV, jobject clazz) {int val = 0; If (! Hello_device) {Logi ("Hello JNI: device is not open. "); Return val;} hello_device-> get_val (hello_device, & Val); Logi (" Hello JNI: Get value % d from device. ", Val); Return val;}/* Open the hardware device through the hardware module defined in the hardware abstraction layer. */static inline int hello_device_open (const hw_module_t * module, struct hello_device_t ** device) {return module-> methods-> open (module, hello_hardware_module_id, (struct hw_device_t **) device );} /* use the hardware module ID to load the specified Hardware Abstraction Layer module and open the hardware */static jboolean hello_init (jnienv * ENV, jclass clazz) {hello_module_t * module; logi ("Hello JNI: initializing ...... "); If (hw_get_module (hello_hardware_module_id, (const struct hw_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 const jninativemethod method_table [] = {" init_native "," () Z ", (void *) hello_init}, {"setval_native", "(I) V", (void *) hello_setval}, {"getval_native", "() I", (void *) hello_getval},};/* register the JNI Method */INT register_android_server_helloservice (jnienv * env) {return partition (ENV, "com/Android/Server/helloservice", method_table, nelem (method_table ));}};

Note: 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 whose module ID is hello_hardware_module_id. In this module, hello_hardware_module_id is in. h>. The Android hardware abstraction layer finds the corresponding module in the/system/lib/HW directory of the Android system based on the value of hello_hardware_module_id, loads it, and returns the hw_module_t interface for the caller. In the jniregisternativemethods function, the value of the second parameter must correspond to the path of the helloservice package, that is, Com. Android. server. helloservice.

3. Modify the onload. cpp file in 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 call table is automatically loaded during Android initialization. 4. modify the android. MK file, add a line in the local_src_files variable: local_src_files :=\ Alibaba \ com_android_server_systemserver.cpp \ com_android_server_usbservice.cpp \ Users \ Com_android_server_helloservice.cpp/Onload. cpp 5. Compile and re-find million system. IMG: User-name @ machine-Name :~ /Android $ Mmm frameworks/base/services/JNI User-name @ machine-Name :~ /Android $ make Snod In this way, repackage the system. the IMG image file contains the JNI method we just compiled. That is, we can call these JNI methods through the hardware service helloservice provided by the Application Frameworks layer of the Android system, then, the hardware abstraction layer interface is called to access the hardware. As mentioned above, we temporarily ignore the implementation of the helloservice class in this article. In the next article, we will describe how to implement the hardware service helloservice. Lao Luo's Sina Weibo: http://weibo.com/shengyangluo. welcome to the attention!
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.