回 帖 发 新 帖 刷新版面

主题:[讨论]一个JNI程序

一个JNI程序,求注释。新手。。
static jint get_count(void)
{
    if(sLedDevice)
        sLedDevice->getcount_led(sLedDevice);
    else
        LOGI("sLedDevice is null");
    return 0;
}

static jint led_setOn(JNIEnv* env, jobject thiz) {
    if (sLedDevice) {
        LOGI("led_set_on");
        sLedDevice->set_on(sLedDevice);
    }else{
        LOGI("sLedDevice is null");
    }
    return 0;
}

static jint led_setOff(JNIEnv* env, jobject thiz) {
    if (sLedDevice) {
        LOGI("led_set_off");
        sLedDevice->set_off(sLedDevice);
    }else{
        LOGI("sLedDevice is null");
    }

    return 0;
}

/** helper APIs */
static inline int led_control_open(const struct hw_module_t* module,
    struct led_control_device_t** device) {
    LOGI("led_control_ope");
    return module->methods->open(module,
        LED_HARDWARE_MODULE_ID, (struct hw_device_t**)device);
}

static jint led_init(JNIEnv *env, jclass clazz)
{
    led_module_t const * module;
    LOGI("led_init");   

    if (hw_get_module(LED_HARDWARE_MODULE_ID, (const hw_module_t**)&module) == 0) {
        LOGI("get Module OK");     
        sLedModule = (led_module_t *) module;
        if (led_control_open(&module->common, &sLedDevice) != 0) {
            LOGI("led_init error");
            return -1;
        }
    }

    LOGI("led_init success");
    return 0;
}

/*
 ** 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*)led_init},
    { "_set_on",          "()I",
        (void*)led_setOn },
    { "_set_off",          "()I",
        (void*)led_setOff },
    { "_get_count",          "()I",
        (void*)get_count },
};

static int registerMethods(JNIEnv* env) {
    static const char* const kClassName =
        "com/hello/LedService";
    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;


/*
  *   * This is called by the VM when the shared library is first loaded.
 */ 
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    JNIEnv* env = NULL;
    jint result = -1;
    LOGI("JNI_OnLoad");
    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) {
        LOGE("ERROR: PlatformLibrary native registration failed\n");
        goto fail;
    }
    /* success -- return valid version number */    
    result = JNI_VERSION_1_4;
fail:
    return result;
}

回复列表 (共2个回复)

沙发

注释找文档,然后自己加。这是你自己的工作啊,这东西还让人代劳那以后怎么办?

板凳

自己多琢磨一下~

我来回复

您尚未登录,请登录后再回复。点此登录或注册