当前位置:操作系统 > 安卓/Android >>

Android 驱动开发系列四

时隔多日,终于都抽出时间来写blog了。废话不多说,接着上一篇,这里将介绍如何编写HAL层(硬件抽象层)对应的JNI方法。

这里提到的都是在ICS源码里编译的。

 

1、定义JNI层接口
进入到android-4.0.4_r1.2/hardware/libhardware/include/hardware目录,并创建 ttt.h 文件,内容如下:

[cpp]
#ifndef ANDROID_TTT_INTERFACE_H  
#define ANDROID_TTT_INTERFACE_H  
#include <hardware/hardware.h>  
 
__BEGIN_DECLS 
 
// 定义模块ID  
#define HELLO_HARDWARE_MODULE_ID    "ttt"  
 
// 硬件模块结构体  
struct ttt_module_t{ 
    struct hw_module_t common; 
}; 
 
// hardware inte易做图ce struct  
struct ttt_device_t{ 
    struct hw_device_t common; 
    int fd; 
    int(*set_val)(struct ttt_device_t* dev, int val); 
    int(*get_val)(struct ttt_device_t* dev, int* val); 
}; 
 
__END_DECLS 
 
 
#endif 

#ifndef ANDROID_TTT_INTERFACE_H
#define ANDROID_TTT_INTERFACE_H
#include <hardware/hardware.h>

__BEGIN_DECLS

// 定义模块ID
#define HELLO_HARDWARE_MODULE_ID "ttt"

// 硬件模块结构体
struct ttt_module_t{
    struct hw_module_t common;
};

// hardware inte易做图ce struct
struct ttt_device_t{
 struct hw_device_t common;
 int fd;
 int(*set_val)(struct ttt_device_t* dev, int val);
 int(*get_val)(struct ttt_device_t* dev, int* val);
};

__END_DECLS


#endif

2、实现JNI层接口功能
进入到android-4.0.4_r1.2/frameworks/base/services/jni目录,并创建com_android_server_TTTService.cpp文件,其内容如下:

[cpp]
#define LOG_TAG     "TTTService"  
 
#include "jni.h"  
#include "JNIHelp.h"  
#include "android_runtime/AndroidRuntime.h"  
#include <utils/misc.h>  
#include <utils/Log.h>  
#include <hardware/hardware.h>  
#include <hardware/ttt.h>  
#include <stdio.h>  
 
namespace android 

    struct ttt_device_t* ttt_device = NULL; 
 
    // through the HAL inte易做图ce to set the register value  
    static void ttt_setVal(JNIEnv* env, jobject clazz, jint value){ 
        int val = value; 
        LOGI("TTT JNI: set value %d to device.", val); 
        if(!ttt_device){ 
            LOGI("TTT JNI: device is not open."); 
            return; 
        } 
 
        ttt_device->set_val(ttt_device, val); 
    } 
 
    // through the HAL inte易做图ce to read the register value  
    static jint ttt_getVal(JNIEnv* env, jobject clazz){ 
        int val = 0; 
        if(!ttt_device){ 
            LOGI("TTT JNI: device is not open."); 
            return val; 
        } 
        ttt_device->get_val(ttt_device, &val); 
 
        LOGI("TTT JNI: get value %d from device.", val); 
 
        return val; 
    } 
 
    // through the HAL inte易做图ce to open the hardware device  
    static inline int ttt_device_open(const hw_module_t* module, struct ttt_device_t** device){ 
        return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device); 
    } 
 
    // throught the hardware module ID to load the HAL module and open the device  
    static jboolean ttt_init(JNIEnv* env, jclass clazz){ 
        ttt_module_t* module; 
 
        LOGI("TTT JNI: initializing..."); 
        if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0){ 
            LOGI("TTT JNI: ttt stub found."); 
            if(ttt_device_open(&(module->common), &ttt_device) == 0){ 
                LOGI("TTT JNI: ttt device is open."); 
                return 0; 
            } 
            LOGE("TTT JNI: failed to open ttt device."); 
            return -1; 
        } 
 
        LOGE("TTT JNI: failed to get ttt stub module."); 
        return -1; 
    } 
 
    // JNI methods table  
    static const JNINativeMethod method_table[] = { 
        {"init_native", "()Z", (void*)ttt_init}, 
        {"setVal_native", "(I)V", (void*)ttt_setVal}, 
        {"getVal_native", "()I", (void*)ttt_getVal}, 
    }; 
 
    // regist JNI method  
    int register_android_server_TTTService(JNIEnv* env){ 
        return jniRegisterNativeMethods(env, "com/android/server/TTTService", method_table, NELEM(method_table)); 
    } 
}; 

#define LOG_TAG     "TTTService"

#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"
#include <utils/misc.h>
#include <utils/Log.h>
#include <hardware/hardware.h>
#include <hardware/ttt.h>
#include <stdio.h>

namespace android
{
    struct ttt_device_t* ttt_device = NULL;

 &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,