一、硬件抽象层(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;
}
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();
五、native接口定义
JNIEnv定义了一个虚拟机的接口,通过这个接口可以访问虚拟机的所有功能:
1)分配对象(AllocObject/NewObject),并且控制对象的引用计数(NewGlobalRef/DeleteGlobalRef/DeleteLocalRef/IsSameObject/NewLocalRef)。
2)获取类的定义(FindClass),并通过类的定义来获取获取类得方法和成员的ID(GetMethodID/GetFieldID)
3)通过方法ID调用类的普通方法(CallObjectMethod)和静态方法(CallStaticObjectMethod)
4)通过成员ID获取和设置类的普通成员(GetObjectField/SetObjectField)和静态成员(GetStaticObjectField/SetStaticObjectField)
常用方法:
下面是比较常用的方法:
1)查找该类:
jclass xxx = (*env)->FindClass(env, "Lclass_name;");
2)取得方法的id:
jmethodID xxx = (*env)->GetMethodID(env, jclass, methodName, "(M)N");
3)查找需要调用的该类的方法:
jmethodID xxx = (*env)->GetMethodID(env, jclass, "(M)N" );
4)取得静态方法的id
jmethodID xxx = (*env)->GetStaticMethodID(env,jclass, methodName,"(M)N")
5)初始化该类的实例:
jobject xxx = (*env)->NewObject(env, jclass, jmethodID );
6)调用实例的某方法:
(*env)->CallObjectMethod(env, jobject, jmethodID, [parameter1, parameter2,...] );
7)释放实例:
(*env)->DeleteLocalRef(env, xxx);
8)取得成员变量的id
jfieldID xxx = (*env)->GetFieldID(env,jclass ,jfieldID,jfieldType)
9)取得静态成员变量的id
jfieldID xxx = GetStaticFieldID(env,jclass ,jfieldID,jfieldType)
注:JNIENV - java的运行环境;jobject - 代表java的instance;jclass - 代表java的类
在GetMethodID和GetFieldID这两个函数中,最后一个参数都是签名字符串,用来标示java函数和成员的唯一性。因为java中存在重载函数,所以一个函数名不足以唯一指定一个函数,这时候就需要签名字符串来指定函数的参数列表和返回值类型了。函数签名是一个字符串:"(M)N",括号中的内容是函数的参数类型,括号后面表示函数的返回值。
参考博客:http://blog.chinaunix.net/uid-25885064-id-3364407.html
作者:frank_zyp
您的支持是对博主最大的鼓励,感谢您的认真阅读。
本文无所谓版权,欢迎转载。