• Android HIDL 介绍学习之客户端调用


    应上一篇文章Android HIDL 介绍学习_Super Jang的博客-CSDN博客_安卓hidl读者的要求,本文更新客户端调用方法。

    hidl的客户端调用相比服务端的实现要简单很多,本次我们通过一个bin程序直接来调用客户端。

    本地的代码路经为:

    SA800U_Android10.0_R03_r200\frameworks\native\services\chaoservice\Android.bp

    首先是Android.bp文件的书写,内容如下:

    cc_defaults {
        name: "chaoservice_defaults",
        cflags: [
            "-Wall",
            "-Werror",
            "-Wformat",
            "-Wthread-safety",
            "-Wunused",
            "-Wunreachable-code",
        ],
        shared_libs: [
            "liblog",
            "libutils",
            "libcutils",
            "android.hardware.light@2.0",
            "android.hardware.chao@1.0",
            "libhardware",
            "libhidlbase",
            "libhidltransport",
            "libhwbinder",
        ],
    }

    cc_binary {
        name: "chaoservice",
        vendor: true,
        defaults: ["chaoservice_defaults"],
        srcs: [
            "main_chaoservice.cpp",
        ],
    }

    我们此次生成的bin文件就是chaoservice。

    SA800U_Android10.0_R03_r200\frameworks\native\services\chaoservice\main_chaoservice.cpp

    我们的"main_chaoservice.cpp"文件内容如下

    #include
    #include
    #include
    #include
    #include
    using namespace android;

    using IChao    = ::android::hardware::chao::V1_0::IChao;
    using Status     = ::android::hardware::chao::V1_0::Status;
    using Type       = ::android::hardware::chao::V1_0::Type;
    using ChaoState       = ::android::hardware::chao::V1_0::ChaoState;

    template
    using Return     = ::android::hardware::Return;
        
        
    static void processReturn(
            const Return &ret,
            Type type,
            const ChaoState &state) {
        if (!ret.isOk()) {
            ALOGE("Failed to issue set light command.");
            return;
        }

        switch (static_cast(ret)) {
            case Status::SUCCESS:
                ALOGE("Light requested success on this device. %d", type);
                break;
            case Status::CHAO_NOT_SUPPORTED:
                ALOGE("Chao parameter not supported on this device: %d",
                    state.chaoRen);
                break;
            case Status::UNKNOWN:
            default:
                ALOGE("Unknown error setting light.");
        }
    }    
        
    int main(){
        
        Type type = static_cast(0);
        ChaoState chaoState{0,1};
        
        ALOGE("liangchao we will start getting hidl service");
        sp hal = IChao::getService();
        if(hal == NULL){
            ALOGE("liangchao we cant hidl service");
        }else{
            ALOGE("liangchao get Chao service success");
        }
        Return ret = hal->setChao(type, chaoState);
        processReturn(ret,type,chaoState);
        
        return 0;
    }

    通过编译以后会在out的vendor/bin目录生成本次的bin文件,如果想要开机正常使用需要添加bin文件的selinux权限,本次我们测试通过adb root & adb shell setenforce 0的方式强制关闭seliunx权限检查。

    我们生成chaoservice以后可以手动push到/vendor/bin目录

    当我们程序中传入的Type type = static_cast(0);
        ChaoState chaoState{0,1};

    这两个参数时,根据我们上一篇的逻辑判断此时会返回UNKNOW的错误

    // Methods from ::android::hardware::chao::V1_0::IChao follow.
    Return<::android::hardware::chao::V1_0::Status> Chao::setChao(::android::hardware::chao::V1_0::Type type, const ::android::hardware::chao::V1_0::ChaoState& state) {
        // TODO implement
            
        if(type != Type::CHAO && state.chaoRen != 1){
            return ::android::hardware::chao::V1_0::Status::UNKNOWN;
        }
        // TODO implement
        return ::android::hardware::chao::V1_0::Status::SUCCESS;
    }
     

    我们传入Type type = static_cast(2);
        ChaoState chaoState{1,2};

    的时候就会返回成功,通过logcat可以看到两次的返回结果如下:

     

    至此我们客户端的调用就算是完成了。

    创作不易,各位看官想要支持请扫如下二维码:

  • 相关阅读:
    JAVA 多态
    Imu_PreIntegrate_07 Vecility bias update 零偏更新后速度预积分量对零偏的偏导
    maven安装及配置(详细版)
    steam deck科普、上手教程及模拟器配置指南
    【大数据】Kafka 实战教程(一)
    2022年的最后100天渡德赋能网伴天下,修炼本领,不断学习|网安伴
    常用文件名后缀一览
    计算浮点数相除的余数(c++基础)
    什么是https(详细),http和https
    计算机毕设(附源码)JAVA-SSM基于技术的高校学生勤工俭学管理系统的设计与开发
  • 原文地址:https://blog.csdn.net/superjaingchao/article/details/127753657