Android HAL 开发 (2)

来源:互联网 发布:布丁淘宝客软件好用吗 编辑:程序博客网 时间:2024/05/20 13:38
标签:开发 Android 移动开发 HAL 休闲
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://buaadallas.blog.51cto.com/399160/371554

 在上一篇文章中,我们看到了如何撰写HAL层的用户硬件驱动程序,我们每也知道,最终该代码会被编译成动态链接库提供给service(jni)使用,那么我们下面来看看service(jni)是如何与HAL通信的。

一般service的jni代码位于framework/base/service/jni/中,我们看看mokoid的ledservice是如何实现的:

 

framework/base/service/jni/com_mokoid_server_LedService.cpp

  1. static const JNINativeMethod gMethods[] = { 
  2.     { "_init",      "()Z",  (void *)mokoid_init }, 
  3.     { "_set_on",        "(I)Z", (void *)mokoid_setOn }, 
  4.     { "_set_off",       "(I)Z", (void *)mokoid_setOff }, 
  5. }; 
  6.  
  7. int register_mokoid_server_LedService(JNIEnv* env) { 
  8.     static const charconst kClassName = 
  9.         "com/mokoid/server/LedService"
  10.     jclass clazz; 
  11.  
  12.     /* look up the class */ 
  13.     clazz = env->FindClass(kClassName); 
  14.     if (clazz == NULL) { 
  15.         LOGE("Can't find class %s\n", kClassName); 
  16.         return -1; 
  17.     } 
  18.  
  19.     /* register all the methods */ 
  20.     if (env->RegisterNatives(clazz, gMethods, 
  21.             sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK) 
  22.     { 
  23.         LOGE("Failed registering methods for %s\n", kClassName); 
  24.         return -1; 
  25.     } 
  26.  
  27.     /* fill out the rest of the ID cache */ 
  28.     return 0; 

上面的函数register_mokoid_server_LedService会把以C/C++实现的接口注册为java可调用的接口,比如mokoid_init为C/C++代码,

而_init则位java可以使用的接口。这个函数会在JNI_OnLoad里面被调用。

再看看下面C/C++接口的具体实现:

  1. /** helper APIs */ 
  2. static inline int led_control_open(const struct hw_module_t* module, 
  3.         struct led_control_device_t** device) { 
  4.     return module->methods->open(module, 
  5.             LED_HARDWARE_MODULE_ID, (struct hw_device_t**)device); 
  6.  
  7. static jboolean mokoid_init(JNIEnv *env, jclass clazz) 
  8.     led_module_t* module; 
  9.  
  10.     if (hw_get_module(LED_HARDWARE_MODULE_ID, (const hw_module_t**)&module) == 0) { 
  11.         LOGI("LedService JNI: LED Stub found."); 
  12.         if (led_control_open(&module->common, &sLedDevice) == 0) { 
  13.             LOGI("LedService JNI: Got Stub operations."); 
  14.             return 0; 
  15.         } 
  16.     } 
  17.  
  18.     LOGE("LedService JNI: Get Stub operations failed."); 
  19.     return -1; 
  20.  
  21. static jboolean mokoid_setOn(JNIEnv* env, jobject thiz, jint led)  
  22.     LOGI("LedService JNI: mokoid_setOn() is invoked."); 
  23.  
  24.     if (sLedDevice == NULL) { 
  25.         LOGI("LedService JNI: sLedDevice was not fetched correctly."); 
  26.         return -1; 
  27.     } else { 
  28.         return sLedDevice->set_on(sLedDevice, led); 
  29.     } 
  30.  
  31. static jboolean mokoid_setOff(JNIEnv* env, jobject thiz, jint led)  
  32.     LOGI("LedService JNI: mokoid_setOff() is invoked."); 
  33.  
  34.  
  35.     if (sLedDevice == NULL) { 
  36.         LOGI("LedService JNI: sLedDevice was not fetched correctly."); 
  37.         return -1; 
  38.     } else { 
  39.         return sLedDevice->set_off(sLedDevice, led); 
  40.     } 

 

从上面可以看到当init的时候,该jni service通过hw_get_module得到led的module,然后通过module->methods->open()打开该硬件,并且得到led硬件操作的

结构体(led_control_device_t)对象。

接着在mokoid_setOn和mokoid_setOff中分别利用led_control_device_t中的set_on和set_off进行相应的操作。

 

该jni service会被编译成libmokoid_runtime的动态链接库,提供给java service调用。

本文出自 “Mobile and Linux Deve..” 博客,请务必保留此出处http://buaadallas.blog.51cto.com/399160/371554

原创粉丝点击