您的位置:首页 > 移动开发 > Android开发

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;  

}  

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐