• 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可以看到两次的返回结果如下:

     

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

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

  • 相关阅读:
    循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(7) -- 图标列表展示和选择处理
    利用pytorch自定义CNN网络(二):数据集的准备
    网站页面禁止复制内容 JS代码
    【JAVA】浅解线程池ThreadPoolExecutor的各个参数
    IT66021FN: Single-port HDMI 1.4 Receiver with 3D Support
    竞赛选题 基于大数据的社交平台数据爬虫舆情分析可视化系统
    c语言方阵循环右移
    web前端网页设计期末课程大作业:中华传统文化题材网页源码——基于HTML实现中国水墨风书画艺术网站(12个页面)
    2024最新-ubuntu22.04安装最新版QT6.6~6.8教程
    Servlet生命周期
  • 原文地址:https://blog.csdn.net/superjaingchao/article/details/127753657