Compile the JNI method for the hardware abstraction layer (HAL) module of Android in Ubuntu to provide the Java access hardware service interface (Lao Luo study note 4), androidjni

Source: Internet
Author: User

Compile the JNI method for the hardware abstraction layer (HAL) module of Android in Ubuntu to provide the Java access hardware service interface (Lao Luo study note 4), androidjni

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 does the Java interface access the C interface?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. Simply put,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" // some function definition declarations used by the jni layer # include "JNIHelp. h "//?? The role of each header file. Please advise ???????????? Thank you.# Include "android_runtime/AndroidRuntime. h "//? # Include <utils/misc. h> //? # Include <utils/Log. h> //? # Include 
#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    /* Write */Static void hello_setVal (JNIEnv * env, jobject clazz, jint value) {// JNIEnv *, jobject, jint type 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 );//Execution bottom layer: set_val}/* Read the val value of the hardware register through the hardware access interface defined in the hardware abstraction layer */
    /* -- Read --*/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 );//Execute the underlying layer: get_val (that is, simply run it)LOGI ("Hello JNI: get value % d from device.", val); return val;}/* hardware module defined through the Hardware Abstraction LayerOpenOpen InterfaceHardware*/Static inline int hello_device_open (const hw_module_t * module, struct hello_device_t ** device ){// Inline: built-in FunctionReturn 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 */
/* -- Bool is the variable type in C,Jboolean is the variable type in JNI., Boolean is the variable type in Java;
In C, jboolean is defined:
Typedef unsigned char jboolean; -- */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) {// call the hello_device_open () function 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 [] = {// The top layer only uses {" 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) {// http://blog.csdn.net/horkychen/article/details/10952249 return jniRegisterNativeMethods (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 isCom. 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.

1.

Make: Enter the directory '/opt/FriendlyARM/tiny4412/android-4.1.2'
Make: * ** you can create the target "out/target/product/generic/obj/SHARED_LIBRARIES/libandroid_servers_intermediates/import_includes" required by "out/target/product/generic/obj/SHARED_LIBRARIES/ libc_intermediates/export_includes ". Stop.
Make: Leave the directory "/opt/FriendlyARM/tiny4412/android-4.1.2"
Root @ phone-desktop:/opt/FriendlyARM/tiny4412/android-4.1.2 #

 

Solution:

. Setenv

Output:

Out/target/product/tiny4412/system/lib/libandroid_servers.so

3

Make: *** [out/target/common/obj/APPS/CMParts_intermediates/classes-full-debug.jar] Error 41 this error, solution, if you know it is very simple,
Run the following command: make update-api; // $ make clobber $ make
Local program modification: http://blog.csdn.net/oldmtn/article/details/9214143 article: http://blog.csdn.net/luoshengyang/article/details/6575988

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.