Java service interface provides access to the hardware in Ubuntu for Android hardware abstraction layer (HAL) modules written JNI method (4)

        In the two articles, we covered how to write drivers for the Android system hardware, including how to implement kernel drivers in Linux kernel space and the hardware abstraction layer interface in user space. To achieve the purpose of both is to provide hardware to access interface layer on more, that is the Android Application Frameworks layer provides hardware service. We know that the application from the Android system is written in Java, and hardware drivers are using C language to achieve, then, how Java interfaces to access the C interface it? As we all know, Java JNI provides a method call, Similarly, in the Android system, Java applications to invoke hardware abstraction layer interface through JNI. In this article, we'll show you how to write an interface JNI method for the Android hardware abstraction layer so that the upper layer of Java applications to use the services provided by the underlying hardware.

"Android system source code Scenario Analysis," a book under attack programmer network ( http://0xcc0xcd.com in) serial, click to enter!

      A reference to an increase in the Ubuntu for Android hardware abstraction layer (HAL) Linux kernel driver module to access a file, ready hardware abstraction layer module, to ensure that the Android system image file already contains hello.default system.img module.

      Second into the frameworks / base / services / jni directory, create 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 com_android_server_HelloService.cpp document, implement JNI method. CAUTION command file, com_android_server prefix indicates that the package name, a hardware service HelloService is placed under the com frameworks / base / services / java directory / android / server directory, that there is a command com.android.server. HelloService class. Here, we have omitted describe HelloService class, the next article, we will return to HelloService class. Simply put, HelloService is a Java interface provides hardware access service class.

      The first is to include the appropriate header files:


   
   
  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 <hardware/hardware.h>
  8. #include <hardware/hello.h>
  9. #include <stdio.h>

      It then defines hello_init, hello_getVal and hello_setVal three JNI method:


   
   
  1. namespace android
  2. {
  3. / * Hardware access structures defined in hardware abstraction layer, the reference <hardware / hello.h> * /
  4. struct hello_device_t* hello_device = NULL;
  5. / * Hardware access interface hardware registers provided by hardware abstraction layer val defined value * /
  6. static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {
  7. int val = value;
  8. LOG ( "Hello JNI: set value %d to device.", val);
  9. if(!hello_device) {
  10. LOG ( "Hello JNI: device is not open.");
  11. return;
  12. }
  13. hello_device->set_val(hello_device, val);
  14. }
  15. / * Read the hardware register access interface hardware val hardware abstraction layer defined by the value of * /
  16. static jint hello_getVal(JNIEnv* env, jobject clazz) {
  17. int val = 0;
  18. if(!hello_device) {
  19. LOG ( "Hello JNI: device is not open." );
  20. return val;
  21. }
  22. hello_device->get_val(hello_device, &val);
  23. LOG ( "Hello JNI: get value %d from device.", val);
  24. return val;
  25. }
  26. / * Hardware abstraction layer defined open interface to open a hardware module hardware * /
  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. / * Hardware module ID to load the specified hardware abstraction layer module and open hardware * /
  31. static jboolean hello_init(JNIEnv* env, jclass clazz) {
  32. hello_module_t* module;
  33. LOG ( "Hello JNI: initializing......");
  34. if(hw_get_module(HELLO_HARDWARE_MODULE_ID, ( const struct hw_module_t**)& module) == 0) {
  35. LOG ( "Hello JNI: hello Stub found.");
  36. if(hello_device_open(&( module->common), &hello_device) == 0) {
  37. LOG ( "Hello JNI: hello device is open.");
  38. return 0;
  39. }
  40. LODGE ( "Hello JNI: failed to open hello device.");
  41. return -1;
  42. }
  43. LODGE ( "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 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 hello_init function, hw_get_module Android method provided by the hardware abstraction layer for the load module ID HELLO_HARDWARE_MODULE_ID hardware abstraction layer module, wherein, HELLO_HARDWARE_MODULE_ID defined in <hardware / hello.h> of. Android based hardware abstraction layer to find the corresponding module in the system Android / system / lib / hw directory HELLO_HARDWARE_MODULE_ID value, and then load them, and the interface returns to the caller hw_module_t use. In jniRegisterNativeMethods function, the value of the second parameter is the path corresponding to the package must be located HelloService, i.e. com.android.server.HelloService.

      Three onload.cpp modify files in the same directory, first in the namespace android increase register_android_server_HelloService function declaration:

      namespace android {

      ..............................................................................................

      int register_android_server_HelloService(JNIEnv *env);

      };

      Increase register_android_server_HelloService function calls in JNI_onLoad:
      extern "C" jint JNI_onLoad(JavaVM* vm, void* reserved)
      {
       .................................................................................................
       register_android_server_HelloService(env);
       .................................................................................................
      }
      Thus, when the Android system initialization will automatically load the JNI method call table.
      Four Android.mk modify files in the same directory, add a line in LOCAL_SRC_FILES variable:
      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 \
      com_android_server_location_GpsLocationProvider.cpp \
       com_android_server_HelloService.cpp /
      onload.cpp
      V. Compile and re-find one hundred million system.img:
       USER-NAME@MACHINE-NAME:~/Android$ mmm frameworks/base/services/jni
      USER-NAME@MACHINE-NAME:~/Android$ make snod
       In this way, repackaged system.img image file contains JNI method we've just written, that is, we can provide hardware service HelloService by Application Frameworks layer system Android JNI to call these methods, which in turn calls the low-level hardware abstraction layer interface to access the hardware. Mentioned earlier in this article, we ignore the realization HelloService class, the next article, we will describe how to implement the hardware service HelloService, so stay tuned.

Guess you like

Origin blog.csdn.net/ll148305879/article/details/93031384