namespace Android2.{
/*在硬件抽象層中定義的硬件訪問(wèn)結(jié)構(gòu)體,參考*/
struct hello_device_t* hello_device = NULL;
/*通過(guò)硬件抽象層定義的硬件訪問(wèn)接口設(shè)置硬件寄存器val 的值*/
static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {
??? int val = value;
??? LOGI("Hello JNI: set value %d to device.", val);
??? if(!hello_device) {
??????????? LOGI("Hello JNI: device is not open.");
?? ? ? ? ?? return;
??? }
? ? hello_device->set_val(hello_device, val);
}
/*通過(guò)硬件抽象層定義的硬件訪問(wèn)接口讀取硬件寄存器val 的值*/
static jint hello_getVal(JNIEnv* env, jobject clazz) {
??? int val = 0;
??? if(!hello_device) {
??????? LOGI("Hello JNI: device is not open.");
??????? return val;
? ?? }
??? hello_device->get_val(hello_device, &val);
??? LOGI("Hello JNI: get value %d from device.", val);
? ? return val;
?}
/*通過(guò)硬件抽象層定義的硬件模塊打開(kāi)接口打開(kāi)硬件設(shè)備*/
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);
}
/*通過(guò)硬件模塊ID 來(lái)加載指定的硬件抽象層模塊并打開(kāi)硬件*/
static jboolean hello_init(JNIEnv* env, jclass clazz) {
??? hello_module_t* module;
??? LOGI("Hello JNI: initializing......");
??? if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) {
??? LOGI("Hello JNI: hello Stub found.");
??? if(hello_device_open(&(module->common), &hello_device) == 0) {
??????? LOGI("Hello JNI: hello device is open.");
? ? ? ? return 0;
??? }
??? LOGE("Hello JNI: failed to open hello device.");
??? return -1;
?? }
??? LOGE("Hello JNI: failed to get hello stub module.");
??? return -1;
}
/*JNI 方法表*/
static const JNINativeMethod method_table[] = {
??? {"init_native", "()Z", (void*)hello_init},
??? {"setVal_native", "(I)V", (void*)hello_setVal},
??? {"getVal_native", "()I", (void*)hello_getVal},
};
/*注冊(cè)JNI 方法*/
int register_Android_server_HelloService(JNIEnv *env) {
??? return jniRegisterNativeMethods(env, "com/Android/server/HelloService", method_table,NELEM(method_table));
}
};
修改onload.cpp
修改Android.mk