美文网首页
android 自定义驱动(第四篇:Framework JNI调

android 自定义驱动(第四篇:Framework JNI调

作者: momxmo | 来源:发表于2020-05-25 23:08 被阅读0次

    简介

    上一篇已经实现了HDIL服务端打开驱动文件读取功能,并提供了.hal接口给客户端调用。
    这一篇文章主要是实现Framework层JNI调用HDIL服务操作;编写实现JNI方法,方便上层Java层能够使用底层提供的硬件服务;

    注:其中JNI这一层是可以不用的,HDIL支持Java方式,这里为了巩固学习,所以加了这一层方式

    一、实现JNI

    frameworks/base/services/core/jni/目录新建com_android_server_HelloService.cpp文件:
    实现JNI方法。这里文件的命名方式以om_android_server为前缀表示包名,表示硬件服务HelloService是放在frameworks/base/services/java目录下的com/android/server目录的,对应一个名为com.android.server.HelloService的类。下一篇文章将会介绍HelloService.java类的实现,它是一个提供Java接口的硬件访问服务类,对应用层提供AIDL接口进程远程服务;

    #define LOG_TAG "HelloService"
    #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/hello.h>
    #include <stdio.h>
    
    #include <android/hardware/hello/1.0/IHello.h>
    # include <hidl/Status.h>
    # include <hidl/LegacySupport.h>
    # include <utils/misc.h>
    # include <hidl/HidlSupport.h>
    
    // 定义hello_init、 hello_getVal 和 hello_setVal 三个JNI方法:
    namespace android 
    {
    using IHello     = ::android::hardware::hello::V1_0::IHello;
    using android::sp;
    
    
    class HelloHal{
    public:
        static sp<IHello> service;
        static bool hasInit;
        HelloHal(){}
    
    public:
        static sp<IHello> open(){
            if((service == nullptr || !hasInit) ){
                service = IHello::getService();
                hasInit = true;
                if(service == nullptr){
                    ALOGE("Unable to get IHello interface.");
                }
            } 
            service->open();
            ALOGE("JNI hello open");
            return service;
        }
    
        static void close(){
            service = nullptr;
            hasInit = false;
        }
    
    };
    sp<IHello> HelloHal::service = nullptr;
    bool HelloHal::hasInit = false;
    
    
    static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {
            if(HelloHal::service==nullptr){
                ALOGI("Hello JNI:service is null");
                return;
            }
            HelloHal::service->write(value);
        }
    
        
        static jint hello_getVal(JNIEnv* env, jobject clazz) {
            int val = 0;
    
            if(HelloHal::service==nullptr){
                ALOGI("Hello JNI:service is null");
                return val;
            }
            return HelloHal::service->read();
        }
    
    
        static jboolean hello_init(JNIEnv* env, jclass clazz) {
            sp<IHello> hal = HelloHal::open();
            if (hal == nullptr) {
                ALOGI("Hello JNI: service is init failed");
                return -1;
            }
            ALOGI("Hello JNI: service is init succes");
            return 1;
        }
    
        /*JNI 方法表*/
        // java和native的方法对应表,第一个是java中的方法,
        //第二个是参数(括号中为参数)和返回值,含义为:[B -- byte数组,Z -- bool, I -- int, V -- void,
        // Ljava/lang/String; -- String对象, [Ljava/lang/String; -- String对象数组
        //第三个对应的是native中的函数
        static const JNINativeMethod method_table[] = {
            {"init_native", "()Z", (void*)hello_init},
            {"setVal_native","(I)V", (void*)hello_setVal},
            {"getVal_native","()I",(void*)hello_getVal},        
        };
    
        /*注册JNI方法*/
        // 第二个参数的值必须对应HelloService所在的包的路径,即com.android.server.HelloService
        int register_android_server_HelloService(JNIEnv* env) {
            return jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table));
        }
    
    }
    
    
    

    这里主要提供了三个函数,分别是开机启动初始化、设置值、获取值;

    二、修改onload.cpp文件

    将自定义的JNI添加到其中,如下:

    namespace android {
    .........添加一
    int register_android_server_HelloService(JNIEnv *env);
    };
    extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
    {
    ............添加二
        register_android_server_HelloService(env); // 这样,在Android系统初始化时,就会自动加载该JNI方法调用表
        return JNI_VERSION_1_4;
    }
    

    三、修改编译脚本Android.mk

    同目录下修改Android.mk文件,在LOCAL_SRC_FILES变量添加一行:

    LOCAL_SRC_FILES += \
    .......
    $(LOCAL_REL_DIR)/com_android_server_HelloService.cpp \
        $(LOCAL_REL_DIR)/onload.cpp
    
    ........
    LOCAL_SHARED_LIBRARIES += \
    ......
    #导入hello硬件库
        android.hardware.hello@1.0 \
    ......
    .......
    

    四、编译

    单模块编译如下:

    root@ubuntu:frameworks/base/services/core/jni$ mm -j16
    

    这里就完成了Framework JNI层C++是实现,下一章介绍Framework服务Java的实现以及对外提供服务!

    相关文章

      网友评论

          本文标题:android 自定义驱动(第四篇:Framework JNI调

          本文链接:https://www.haomeiwen.com/subject/hepaghtx.html