应上一篇文章Android HIDL 介绍学习_Super Jang的博客-优快云博客_安卓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 <android/hardware/light/2.0/ILight.h>
#include <android/hardware/chao/1.0/IChao.h>
#include <android/hardware/chao/1.0/types.h>
#include <stdio.h>
#include <utils/Log.h>
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<typename T>
using Return = ::android::hardware::Return<T>;
static void processReturn(
const Return<Status> &ret,
Type type,
const ChaoState &state) {
if (!ret.isOk()) {
ALOGE("Failed to issue set light command.");
return;
}
switch (static_cast<Status>(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<Type>(0);
ChaoState chaoState{0,1};
ALOGE("liangchao we will start getting hidl service");
sp<IChao> hal = IChao::getService();
if(hal == NULL){
ALOGE("liangchao we cant hidl service");
}else{
ALOGE("liangchao get Chao service success");
}
Return<Status> 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<Type>(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<Type>(2);
ChaoState chaoState{1,2};
的时候就会返回成功,通过logcat可以看到两次的返回结果如下:
至此我们客户端的调用就算是完成了。
创作不易,各位看官想要支持请扫如下二维码: