Java Access Hardware Service interface is provided in Ubuntu authoring Jni method for Android hardware abstraction Layer (HAL) module

Source: Internet
Author: User

Article reprinted to CSDN community Luo Shenyang's Android tour, original address: http://blog.csdn.net/luoshengyang/article/details/6575988

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 the Java 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 the 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:

[CPP]View Plaincopy
    1. #define LOG_TAG "HelloService"
    2. #include "jni.h"
    3. #include "JNIHelp.h"
    4. #include "android_runtime/androidruntime.h"
    5. #include <utils/misc.h>
    6. #include <utils/Log.h>
    7. #include
    8. #include
    9. #include <stdio.h>

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

[CPP]View Plaincopy
  1. Namespace Android
  2. {
  3. / * Hardware access structure defined in the hardware abstraction layer, refer to
  4. struct hello_device_t* hello_device = NULL;
  5. / * Sets the value of the hardware register Val by hardware interface defined by the hardware abstraction Layer * /
  6. static void Hello_setval (jnienv* env, Jobject clazz, Jint value) {
  7. int val = value;
  8. Logi ("Hello Jni:set value%d to device.", Val);
  9. if (!hello_device) {
  10. Logi ("Hello Jni:device is not open.");
  11. return;
  12. }
  13. Hello_device->set_val (Hello_device, Val);
  14. }
  15. / * Reads the value of the hardware register Val from the hardware interface defined by the hardware abstraction Layer * /
  16. static Jint Hello_getval (jnienv* env, Jobject clazz) {
  17. int val = 0;
  18. if (!hello_device) {
  19. Logi ("Hello Jni:device is not open.");
  20. return Val;
  21. }
  22. Hello_device->get_val (Hello_device, &val);
  23. Logi ("Hello jni:get value%d from device.", Val);
  24. return Val;
  25. }
  26. / * Open the interface with hardware modules defined by the hardware abstraction layer to open the hardware device * /
  27. static inline int hello_device_open (const hw_module_t* module, struct hello_device_t** device) {
  28. return Module->methods->open (module, hello_hardware_module_id, (struct hw_device_t**) device);
  29. }
  30. / * Load the specified hardware abstraction Layer module with the hardware module ID and turn on the hardware * /
  31. static Jboolean Hello_init (jnienv* env, Jclass clazz) {
  32. hello_module_t* module;
  33. Logi ("Hello jni:initializing ...");
  34. if (Hw_get_module (hello_hardware_module_id, (const struct hw_module_t**) &module) = = 0) {
  35. Logi ("Hello Jni:hello Stub found.");
  36. if (Hello_device_open (& (Module->common), &hello_device) = = 0) {
  37. Logi ("Hello Jni:hello device is open.");
  38. return 0;
  39. }
  40. LOGE ("Hello jni:failed to open Hello device.");
  41. return-1;
  42. }
  43. LOGE ("Hello jni:failed to get hello stub module.");
  44. return-1;
  45. }
  46. /*jni Method Table * /
  47. static const Jninativemethod method_table[] = {
  48. {"init_native", "() Z", (void*) Hello_init},
  49. {"setval_native", "(I) V", (void*) Hello_setval},
  50. {"getval_native", "() I", (void*) Hello_getval},
  51. };
  52. / * Register the JNI method * /
  53. int Register_android_server_helloservice (jnienv *env) {
  54. return Jniregisternativemethods (env, "Com/android/server/helloservice", Method_table, Nelem (method_table))  ;
  55. }
  56. };

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. Lao Luo's Sina Weibo: Http://weibo.com/shengyangluo, welcome attention!

Java Access Hardware Service interface is provided in Ubuntu authoring Jni method for Android hardware abstraction Layer (HAL) module

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.