android的HAL第一种调用hal方法中的JNI代码的实现:
2011-11-15 15:56
579 查看
(2)JNI层
文件:mokoid-read-only/frameworks/base/service/jni/com_mokoid_server_LedService.cpp
struct led_control_device_t *sLedDevice = NULL;
static jboolean mokoid_setOn(JNIEnv* env, jobject thiz, jint led)
{
LOGI("LedService JNI: mokoid_setOn() is invoked.");
if (sLedDevice == NULL) {
LOGI("LedService JNI: sLedDevice was not fetched correctly.");
return -1;
} else {
return sLedDevice->set_on(sLedDevice, led);//调用hal层的注册的方法
}
}
static jboolean mokoid_setOff(JNIEnv* env, jobject thiz, jint led)
{
LOGI("LedService JNI: mokoid_setOff() is invoked.");
if (sLedDevice == NULL) {
LOGI("LedService JNI: sLedDevice was not fetched correctly.");
return -1;
} else {
return sLedDevice->set_off(sLedDevice, led); //调用hal层的注册的方法
}
}
/** helper APIs */
static inline int led_control_open(const struct hw_module_t* module,
struct led_control_device_t** device) {
return module->methods->open(module,LED_HARDWARE_MODULE_ID, (struct hw_device_t**)device);
//这个过程非常重要,jni通过LED_HARDWARE_MODULE_ID找到对应的stub
}
static jboolean mokoid_init(JNIEnv *env, jclass clazz)
{
led_module_t* module;
LOGI("jni init-----------------------.");
if (hw_get_module(LED_HARDWARE_MODULE_ID, (const hw_module_t**)&module) == 0) {
//根据LED_HARDWARE_MODULE_ID找到hw_module_t,参考hal层的实现
LOGI("LedService JNI: LED Stub found.");
if (led_control_open(&module->common, &sLedDevice) == 0) {
//通过hw_module_t找到led_control_device_t
LOGI("LedService JNI: Got Stub operations.");
return 0;
}
}
LOGE("LedService JNI: Get Stub operations failed.");
return -1;
}
/*
* Array of methods.
* Each entry has three fields: the name of the method, the method
* signature, and a pointer to the native implementation.
*/
static const JNINativeMethod gMethods[] = {
{ "_init", "()Z", (void *)mokoid_init },//Framework层调用_init时促发
{ "_set_on", "(I)Z", (void *)mokoid_setOn },
{ "_set_off", "(I)Z", (void *)mokoid_setOff },
};
/*
*JNINativeMethod是jni层注册的方法,Framework层可以使用这些方法
*_init 、_set_on、_set_off是在Framework中调用的方法名称,函数的类型及返回值如下:
*()Z 无参数 返回值为bool型
* (I)Z 整型参数 返回值为bool型
*/
static int registerMethods(JNIEnv* env) {
static const char* const kClassName =
"com/mokoid/server/LedService";//注意:必须和你Framework层的service类名相同
jclass clazz;
/* look up the class */
clazz = env->FindClass(kClassName);
if (clazz == NULL) {
LOGE("Can't find class %s\n", kClassName);
return -1;
}
/* register all the methods */
if (env->RegisterNatives(clazz, gMethods,
sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
{
LOGE("Failed registering methods for %s\n", kClassName);
return -1;
}
/* fill out the rest of the ID cache */
return 0;
}
jint JNI_OnLoad(JavaVM* vm, void* reserved) {//Framework层加载jni库时调用
JNIEnv* env = NULL;
jint result = -1;
LOGI("JNI_OnLoad LED");
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGE("ERROR: GetEnv failed\n");
goto fail;
}
assert(env != NULL);
if (registerMethods(env) != 0) { //注册你的JNINativeMethod
LOGE("ERROR: PlatformLibrary native registration failed\n");
goto fail;
}
/* success -- return valid version number */
result = JNI_VERSION_1_4;
fail:
return result;
}
文件:mokoid-read-only/frameworks/base/service/jni/com_mokoid_server_LedService.cpp
struct led_control_device_t *sLedDevice = NULL;
static jboolean mokoid_setOn(JNIEnv* env, jobject thiz, jint led)
{
LOGI("LedService JNI: mokoid_setOn() is invoked.");
if (sLedDevice == NULL) {
LOGI("LedService JNI: sLedDevice was not fetched correctly.");
return -1;
} else {
return sLedDevice->set_on(sLedDevice, led);//调用hal层的注册的方法
}
}
static jboolean mokoid_setOff(JNIEnv* env, jobject thiz, jint led)
{
LOGI("LedService JNI: mokoid_setOff() is invoked.");
if (sLedDevice == NULL) {
LOGI("LedService JNI: sLedDevice was not fetched correctly.");
return -1;
} else {
return sLedDevice->set_off(sLedDevice, led); //调用hal层的注册的方法
}
}
/** helper APIs */
static inline int led_control_open(const struct hw_module_t* module,
struct led_control_device_t** device) {
return module->methods->open(module,LED_HARDWARE_MODULE_ID, (struct hw_device_t**)device);
//这个过程非常重要,jni通过LED_HARDWARE_MODULE_ID找到对应的stub
}
static jboolean mokoid_init(JNIEnv *env, jclass clazz)
{
led_module_t* module;
LOGI("jni init-----------------------.");
if (hw_get_module(LED_HARDWARE_MODULE_ID, (const hw_module_t**)&module) == 0) {
//根据LED_HARDWARE_MODULE_ID找到hw_module_t,参考hal层的实现
LOGI("LedService JNI: LED Stub found.");
if (led_control_open(&module->common, &sLedDevice) == 0) {
//通过hw_module_t找到led_control_device_t
LOGI("LedService JNI: Got Stub operations.");
return 0;
}
}
LOGE("LedService JNI: Get Stub operations failed.");
return -1;
}
/*
* Array of methods.
* Each entry has three fields: the name of the method, the method
* signature, and a pointer to the native implementation.
*/
static const JNINativeMethod gMethods[] = {
{ "_init", "()Z", (void *)mokoid_init },//Framework层调用_init时促发
{ "_set_on", "(I)Z", (void *)mokoid_setOn },
{ "_set_off", "(I)Z", (void *)mokoid_setOff },
};
/*
*JNINativeMethod是jni层注册的方法,Framework层可以使用这些方法
*_init 、_set_on、_set_off是在Framework中调用的方法名称,函数的类型及返回值如下:
*()Z 无参数 返回值为bool型
* (I)Z 整型参数 返回值为bool型
*/
static int registerMethods(JNIEnv* env) {
static const char* const kClassName =
"com/mokoid/server/LedService";//注意:必须和你Framework层的service类名相同
jclass clazz;
/* look up the class */
clazz = env->FindClass(kClassName);
if (clazz == NULL) {
LOGE("Can't find class %s\n", kClassName);
return -1;
}
/* register all the methods */
if (env->RegisterNatives(clazz, gMethods,
sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
{
LOGE("Failed registering methods for %s\n", kClassName);
return -1;
}
/* fill out the rest of the ID cache */
return 0;
}
jint JNI_OnLoad(JavaVM* vm, void* reserved) {//Framework层加载jni库时调用
JNIEnv* env = NULL;
jint result = -1;
LOGI("JNI_OnLoad LED");
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGE("ERROR: GetEnv failed\n");
goto fail;
}
assert(env != NULL);
if (registerMethods(env) != 0) { //注册你的JNINativeMethod
LOGE("ERROR: PlatformLibrary native registration failed\n");
goto fail;
}
/* success -- return valid version number */
result = JNI_VERSION_1_4;
fail:
return result;
}
相关文章推荐
- android的HAL第一种调用hal方法中的HAL代码的实现:
- android的HAL第一种调用hal方法中的Framework层代码的实现:
- android的HAL第一种调用hal方法中的APP层代码的实现:
- android的HAL第二种调用hal方法中的Framework层代码的实现:
- android的HAL第二种调用hal方法中的SystemServer (属于APP层)代码的实现:
- android的HAL第二种调用hal方法中的APP 测试程序(属于APP层)代码的实现:
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法实现边缘检测代码(2)
- JNI NDK (AndroidStudio+CMake )实现C C++调用Java代码流程
- android JNI 实现C代码调用
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法 边缘检测 范例代码
- android调用本地C/C++代码,jni的实现
- [转]Android通过NDK调用JNI,使用opencv做本地c++代码开发配置方法
- 模仿android_debug_JNITest实现apk 调用framework java JNI中方法
- android 指纹识别调用实现方法及示例代码
- Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
- 编写android程序调用jni本地方法的实现(详细例子)
- Android - Android调用JNI方法 及 代码
- Android-NDK开发之基础--Android JNI实例代码(一)-- 在JNI中执行Java方法--C/C++调用Java
- Android-NDK开发之基础--Android JNI实例代码(一)-- 在JNI中执行Java方法--C/C++调用Java
- cocosdx交叉编译到android使用jni实现java端调用C++方法