Android HAL 开发 (2)
在上一篇文章中,我们看到了如何撰写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++代码,
【Android HAL 开发 (2)】而_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调用。
推荐阅读
- android第三方框架(五)ButterKnife
- 深入理解Go之generate
- Android中的AES加密-下
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- 带有Hilt的Android上的依赖注入
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库
- 深入理解|深入理解 Android 9.0 Crash 机制(二)