android添加hidl,android hidl

1、定义.hal接口文件,如:

在vendor/sprd/interface中新建目录hello,其中定义好hidl接口,如:

1 package [email protected]1.0;2

3 interfaceIHello {4

5 helloWorld(string name) generates (stringresult);6

7 };

2、利用hidl-gen工具生成通信框架及业务类模板

# [email protected]

# LOC=vendor/sprd/interfaces/hello/1.0/default/

# hidl-gen -o $LOC -Lc++-impl -r vendor.sprd.hardware:vendor/sprd/interfaces -r android.hidl:system/libhidl/transport $PACKAGE

# hidl-gen -o $LOC -Landroidbp-impl -r vendor.sprd.hardware:vendor/sprd/interfaces -r android.hidl:system/libhidl/transport $PACKAGE

这样在default下,可见新生成Hello.cpp  Hello.h文件,此为我们将要实现的业务逻辑类

1 struct Hello : publicIHello {2 //Methods from ::vendor::sprd::hardware::hello::V1_0::IHello follow.

3 Return helloWorld(const hidl_string& name,helloWorld_cb _hidl_cb) override;4

5 //Methods from ::android::hidl::base::V1_0::IBase follow.

6

7 };8

9 //FIXME: most likely delete,this is only for passthrough implementations10 //extern "C" IHello* HIDL_FETCH_IHello(const char* name);

在Hello.cpp中实现之

3、运行业务逻辑,上面的Hello.cpp代码需要在进程中运行,所以需要新建一个

创建文件service.cpp、[email protected],

上面的业务类可以编译成so加载到此进程中,也可以把代码直接编译成bin文件运行。这里采取前者

bp文件如下:

1 cc_binary {

2 name: "[email protected]",3 init_rc: ["[email protected]"],4 relative_install_path: "hw",5 vendor: true,6 7 srcs: [ 8 "Hello.cpp",9 "service.cpp" 10 ],11 12 shared_libs: [ 13 "libcutils",14 "liblog",15 "libhidlbase",16 "libhidltransport",17 "libhardware",18 "libutils",19 "[email protected]",20 ],21 }

sevice.cpp:

1 intmain() {2 ALOGD("Hello hidl main()...");3

4 android::sp hello = newHello();5

6 configureRpcThreadpool(1,true /*callerWillJoin*/);7

8 if (hello !=nullptr) {9 if (::android::OK != hello->registerAsService()) {10 ALOGE("Error while registering IHello service");11 return 1;12 }13 } else{14 ALOGE("Can‘t create instance of Hello,nullptr");15 }16

17 joinRpcThreadpool();18

19 return 0; //should never get here

20 }

4、至此,命令行下启动[email protected]可执行文件,即可运行该hidl服务

5、测试文件:

1 intmain() {2 intret;3

4 android::sp service =IHello::getService();5 if (service ==nullptr) {6 printf("Failed to get service\n");7 return -1;8 }9

10 service->helloWorld("Trump",[&](hidl_string result) {11 printf("%s\n",result.c_str());12 });13

14 return 0;15 }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值