Android HAL 开发 (2)
  ygkfWHMRKw4x 2023年11月02日 32 0



在上一篇文章中,我们看到了如何撰写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.  }; 

7.  int register_mokoid_server_LedService(JNIEnv* env) { 
8.  static const char* const kClassName = 
9.  "com/mokoid/server/LedService"; 
10.  jclass clazz; 

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.  } 

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.  } 

27.  /* fill out the rest of the ID cache */ 
28.  return 0; 
29.  }

Android HAL 开发 (2)_驱动程序

上面的函数
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.  } 

8.  static jboolean mokoid_init(JNIEnv *env, jclass clazz) 
9.  { 
10.  led_module_t* module; 

12.  if (hw_get_module(LED_HARDWARE_MODULE_ID, (const hw_module_t**)&module) == 0) { 
13.  LOGI("LedService JNI: LED Stub found."); 
14.  if (led_control_open(&module->common, &sLedDevice) == 0) { 
15.  LOGI("LedService JNI: Got Stub operations."); 
16.  return 0; 
17.  } 
18.  } 

20.  LOGE("LedService JNI: Get Stub operations failed."); 
21.  return -1; 
22.  } 

24.  static jboolean mokoid_setOn(JNIEnv* env, jobject thiz, jint led)  
25.  { 
26.  LOGI("LedService JNI: mokoid_setOn() is invoked."); 

28.  if (sLedDevice == NULL) { 
29.  LOGI("LedService JNI: sLedDevice was not fetched correctly."); 
30.  return -1; 
31.  } else { 
32.  return sLedDevice->set_on(sLedDevice, led); 
33.  } 
34.  } 

36.  static jboolean mokoid_setOff(JNIEnv* env, jobject thiz, jint led)  
37.  { 
38.  LOGI("LedService JNI: mokoid_setOff() is invoked."); 

41.  if (sLedDevice == NULL) { 
42.  LOGI("LedService JNI: sLedDevice was not fetched correctly."); 
43.  return -1; 
44.  } else { 
45.  return sLedDevice->set_off(sLedDevice, led); 
46.  } 
47.  }

Android HAL 开发 (2)_java_02

从上面可以看到当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调用。

想要了解更多Anrloid相关知识可以点击下方课堂链接              https://edu.51cto.com/course/32703.html Android课

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

ygkfWHMRKw4x