android10 在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口

学习总纲:

Android10 硬件抽象层(HAL)概要介绍和学习计划

编译环境:

   - android 版本 - android-10.0.0_r41
   - 架构 - aosp_x86_64-eng
   - 内核分支:android-goldfish-4.14-gchips

参考教材:

 罗升阳:https://blog.csdn.net/luoshengyang/article/details/6575988

修改如下:

在frameworks/base/services/ core /jni目录下创建 com_android_server_HelloService.cpp :

#define LOG_TAG "HelloService"
#include <nativehelper/JNIHelp.h>
#include "jni.h"
#include <utils/Log.h>
#include <utils/misc.h>
#include <utils/String8.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <linux/ioctl.h>
#include <linux/rtc.h>
#include <hardware/hello.h>

namespace android
{
	/*在硬件抽象层中定义的硬件访问结构体,参考<hardware/hello.h>*/
        struct hello_device_t* hello_device = NULL;
	/*通过硬件抽象层定义的硬件访问接口设置硬件寄存器val的值*/
        static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {
		int val = value;
		ALOGI("Hello JNI: set value %d to device.", val);
		if(!hello_device) {
			ALOGI("Hello JNI: device is not open.");
			return;
		}
		
		hello_device->set_val(hello_device, val);
	}
        /*通过硬件抽象层定义的硬件访问接口读取硬件寄存器val的值*/
	static jint hello_getVal(JNIEnv* env, jobject clazz) {
		int val = 0;
		if(!hello_device) {
			ALOGI("Hello JNI: device is not open.");
			return val;
		}
		hello_device->get_val(hello_device, &val);
		
		ALOGI("Hello JNI: get value %d from device.", val);
	
		return val;
	}
        /*通过硬件抽象层定义的硬件模块打开接口打开硬件设备*/
	static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) {
		return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device);
	}
        /*通过硬件模块ID来加载指定的硬件抽象层模块并打开硬件*/
	static jboolean hello_init(JNIEnv* env, jclass clazz) {
		hello_module_t* module;
		
		ALOGI("Hello JNI: initializing......");
		if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) {
			ALOGI("Hello JNI: hello Stub found.");
			if(hello_device_open(&(module->common), &hello_device) == 0) {
				ALOGI("Hello JNI: hello device is open.");
				return 0;
			}
			ALOGE("Hello JNI: failed to open hello device.");
			return -1;
		}
		ALOGE("Hello JNI: failed to get hello stub module.");
		return -1;		
	}
        /*JNI方法表*/
	static const JNINativeMethod method_table[] = {
		{"init_native", "()Z", (void*)hello_init},
		{"setVal_native", "(I)V", (void*)hello_setVal},
		{"getVal_native", "()I", (void*)hello_getVal},
	};
        /*注册JNI方法*/
	int register_android_server_HelloService(JNIEnv *env) {
    		return jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table));
	}
};

在当前目录找到Android.bp,添加如下:

"com_android_server_HelloService.cpp",

请添加图片描述
最后执行:

mmm ./frameworks/base/services/core/jni
make snod
make -j8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值