在上一篇文章中,我們看到了如何撰寫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調用。