一、硬件抽象层(hal)函数:
static int hello_device_open(const struct hw_module_t* module , const char* name , struct hw_device_t **device)
{
struct hello_device_t* dev;
dev = (struct hello_device_t *)malloc(sizeof(struct hello_device_t));//分配一块内存
if(!dev){
ALOGE("Hello Stub: failed to alloc space");
return -EFAULT;
}
memset(dev,0,sizeof(struct hello_device_t));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t*)module;
dev->common.close = hello_device_close;
dev->set_val = hello_set_val;
dev->get_val = hello_get_val;
if(dev->fd = open(DEVICE_NAME,O_RDWR) == -1){
ALOGE("Hello Stub: failed to open /dev/chrdev -- %s",strerror(errno));
free(dev);
return -EFAULT;
}
*device = &(dev->common);
ALOGI("Hello Stub : open /dev/chrdev/ successfully.");
return 0;
}
static int hello_device_close(struct hw_device_t* device)
{
struct hello_device_t* hello_device = (struct hello_device_t *)device;
if(hello_device){
close(hello_device->fd);
free(hello_device);
}
return 0;
}
static int hello_set_val(struct hello_device_t* dev,int val)
{
ALOGI("Hello Stub: set value %d to device.\n",val);
write(dev->fd , &val , sizeof(val));
return 0;
}
二、cpp文件构件JNI桥梁
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);//以此实现cpp到c的调用
}
/*通过硬件抽象层定义的硬件访问接口读取硬件寄存器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[] = {//此处实现java对cpp的调用转化 注1
{"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){//此处注册jni
return jniRegisterNativeMethods(env,"com/android/server/HelloService" , method_table , NELEM(method_table));
}
}
注1:
typedef struct {
const char* name; //Java中函数的名字
const char* signature; //用字符串是描述了函数的参数和返回值
void* fnPtr; //函数指针,指向C函数
} JNINativeMethod;
其中比较难以理解的是第二个参数,例如
"()V"
"(II)V"
"(Ljava/lang/String;Ljava/lang/String;)V"
实际上这些字符是与函数的参数类型一一对应的。
"()" 中的字符表示参数,后面的则代表返回值。例如"()V" 就表示void Func();
"(II)V" 表示 void Func(int, int);
具体的每一个字符的对应关系如下
字符 Java类型 C类型
V void void
Z jboolean boolean
I jint int
J jlong long
D jdouble double
F jfloat float
B jbyte byte
C jchar char
S jshort short
数组则以"["开始,用两个字符表示
[I jintArray int[]
[F jfloatArray float[]
[B jbyteArray byte[]
[C jcharArray char[]
[S jshortArray short[]
[D jdoubleArray double[]
[J jlongArray long[]
[Z jbooleanArray boolean[]
上面的都是基本类型。如果Java函数的参数是class,则以"L"开头,以";"结尾中间是用"/" 隔开的包及类名。而其对应的C函数名的参数则为jobject. 一个例外是String类,其对应的类为jstring
Ljava/lang/String; String jstring
Ljava/net/Socket; Socket jobject
如果JAVA函数位于一个嵌入类,则用$作为类名间的分隔符。
例如 "(Ljava/lang/String;Landroid/os/FileUtils$FileStatus;)Z"
package android.os;
interface IHelloService{
void setVal(int val);
int getVal();
}
package com.android.server;
import android.content.Context;
import android.os.IHelloService;
import android.util.Slog;
public class HelloService extends IHelloService.Stub {
private static final String TAG = "HelloService";
HelloService() {
init_native();
}
public void setVal(int val) {
setVal_native(val);
}
public int getVal() {
return getVal_native();
}
//被native修饰的表示调用了非java语言的本地方法
private static native boolean init_native();
private static native void setVal_native(int val);
private static native int getVal_native();
};
四、java的调用
helloService = IHelloService.Stub.asInterface(ServiceManager.getService("hello"));
int val = helloService.getVal();
作者:frank_zyp
您的支持是对博主最大的鼓励,感谢您的认真阅读。
本文无所谓版权,欢迎转载。