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 .


  first contains the corresponding header file:



#define LOG_TAG "HelloService" //
#include "jni.h" // Some function definitions used by the jni layer
#include "JNIHelp.h" //? ? The role of each header file, and ask for advice? ? ? ? ? ? ? ? ? ? ? ? Thank you
#include "android_runtime/AndroidRuntime.h" //?
#include <utils/misc.h> //?
#include <utils/Log.h> //?
#include <hardware/hardware.h> //The underlying interface needs to be called eg hello_device
#include <hardware/hello.h> #include <stdio.h>



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

Namespace android
{
    /* Hardware access structure defined in the hardware abstraction layer, refer to <hardware/hello.h>*/
        Struct hello_device_t* hello_device = NULL;
    /* Set the value of the hardware register val by the hardware access interface defined by the hardware abstraction layer*/
/* 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); //execute the underlying: set_val
    }
        /* Read the value of the hardware register val by the hardware access interface defined by 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: get_val (just run it)
          
        LOGI("Hello JNI: get value %d from device.", val);
      
        Return val;
    }
        /* Open the hardware device by opening the interface with the hardware module defined by the hardware abstraction layer*/
    Static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) { //inline: built-in function
        Return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device);
    }
        /* Loads the specified hardware abstraction layer module by hardware module ID and opens the hardware*/
/*--bool is the variable type in C, jboolean is the variable type in JNI, and boolean is the variable type in Java;
The definition of jboolean in C is:
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[] = { // top level only
        {"init_native", "()Z", (void*)hello_init},
        {"setVal_native", "(I)V", (void*)hello_setVal},
        {"getVal_native", "()I", (void*)hello_getVal},
    };
        /*Register 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 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 jniregisternativemethods function, the value of the second parameter must correspond to the path of the package where the HelloService resides , that is, com.android.server.HelloService.



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.


1.



Make: Enter directory '/opt/friendlyarm/tiny4412/android/android-4.1.2 '
Make: * * * No rules can be created "Out/target/product/generic/obj/shared_libraries/libandroid_servers_intermediates/import_ Includes "the desired target" out/target/product/generic/obj/shared_libraries/libc_intermediates/export_includes ". Stop it.
Make: Leave the directory "/opt/friendlyarm/tiny4412/android/android-4.1.2"
[Email protected]:/opt/friendlyarm/tiny4412/android/android-4.1.2#






Workaround:



. Setenv



Output:



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



3


<span class="pln">make: *** 
[out/target/common/obj/APPS/CMParts_intermediates/classes-full-debug.jar]
 Error 41 This error, the solution, if you know it, it is very simple, < /span>


<span class= "PLN" > Just need to hit the following command: Make Update-api;          
//$ made clobber $ made </span>
Local Program Modification: http://blog.csdn.net/oldmtn/article/details/9214143 Original: http://blog.csdn.net/luoshengyang/article/details/6575988


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.