关于后端:Android-HAL-开发-2

8次阅读

共计 2710 个字符,预计需要花费 7 分钟才能阅读完成。

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

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

从下面能够看到当 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 调用。

正文完
 0