赞
踩
第一篇:HIDL学习笔记之HIDL C++(第一天)
第二篇:HIDL学习笔记之HIDL C++(第二天)
第三篇:HIDL实战笔记
进入hardware/interfaces/
目录下建立新的接口文件.
首先建立对应的文件夹:
mkdir -p hardware/interfaces/xiaoqing/1.0/defaul
接着创建接口描述文件IXiaoqing.hal:
vim hardware/interfaces/xiaoqing/1.0/IXiaoqing.hal
IXiaoqing.hal内容:
- package android.hardware.xiaoqing@1.0;
-
- interface IXiaoqing {
- getHelloString(string name) generates (string result);
- };
Google提供了一些工具来帮助制作HIDL的框架:
make hidl-gen
源码中编译生成hidl-gen.注意:编译前需要执行全编译的环境变量加载
- $ PACKAGE=android.hardware.xiaoqing@1.0
- $ LOC=hardware/interfaces/xiaoqing/1.0/default/
- $ hidl-gen -o $LOC -Lc++-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
- $ hidl-gen -o $LOC -Landroidbp-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
执行完后,会生成三个文件:
接着使用脚本来更新Makefile:
./hardware/interfaces/update-makefiles.sh
执行完后,会生成Android.bp和Android.mk:
ok,大功告成.O(∩_∩)O哈哈~
xiaoqing.cpp和xiaoqing.h是实现接口的关键文件.下面来详细介绍下.
- #ifndef ANDROID_HARDWARE_XIAOQING_V1_0_XIAOQING_H
- #define ANDROID_HARDWARE_XIAOQING_V1_0_XIAOQING_H
-
- #include <android/hardware/xiaoqing/1.0/IXiaoqing.h>
- #include <hidl/MQDescriptor.h>
- #include <hidl/Status.h>
-
- namespace android {
- namespace hardware {
- namespace xiaoqing {
- namespace V1_0 {
- namespace implementation {
-
- using ::android::hardware::hidl_array;
- using ::android::hardware::hidl_memory;
- using ::android::hardware::hidl_string;
- using ::android::hardware::hidl_vec;
- using ::android::hardware::Return;
- using ::android::hardware::Void;
- using ::android::sp;
-
- struct Xiaoqing : public IXiaoqing {
- // Methods from IXiaoqing follow.
- Return<void> getHelloString(const hidl_string& name, getHelloString_cb _hidl_cb) override;
-
- // Methods from ::android::hidl::base::V1_0::IBase follow.
-
- };
-
- // FIXME: most likely delete, this is only for passthrough implementations
- extern "C" IXiaoqing* HIDL_FETCH_IXiaoqing(const char* name);
-
- } // namespace implementation
- } // namespace V1_0
- } // namespace xiaoqing
- } // namespace hardware
- } // namespace android
-
- #endif // ANDROID_HARDWARE_XIAOQING_V1_0_XIAOQING_H
去掉注释.采用直通模式.
// extern "C" IXiaoqing* HIDL_FETCH_IXiaoqing(const char* name);
- #include "Xiaoqing.h"
-
- namespace android {
- namespace hardware {
- namespace xiaoqing {
- namespace V1_0 {
- namespace implementation {
-
- // Methods from IXiaoqing follow.
- Return<void> Xiaoqing::getHelloString(const hidl_string& name, getHelloString_cb _hidl_cb) {
- // TODO implement
- return Void();
- }
-
-
- // Methods from ::android::hidl::base::V1_0::IBase follow.
-
- IXiaoqing* HIDL_FETCH_IXiaoqing(const char* /* name */) {
- return new Xiaoqing();
- }
-
- } // namespace implementation
- } // namespace V1_0
- } // namespace xiaoqing
- } // namespace hardware
- } // namespace android
-
去掉注释.
- //IXiaoqing* HIDL_FETCH_IXiaoqing(const char* /* name */) {
- // return new Xiaoqing();
- //}
这些都是工具生成的代码,接下来我们来完善下接口.
主要修改的就是xiaoqing.cpp代码getHelloString
函数.
- // Methods from IXiaoqing follow.
- Return<void> Xiaoqing::getHelloString(const hidl_string& name, helloWorld_cb _hidl_cb) {
- // TODO implement
- char buf[128];
- ::memset(buf, 0, 128);
- ::snprintf(buf, 128, "Hello World, %s", name.c_str());
- hidl_string result(buf);
-
- _hidl_cb(result);
- return Void();
- }
到这里基本上代码就写完了,大部分都是生成的,还是挺方便的.O(∩_∩)O哈哈~
下面开始编译.
按照源码编译的方法:
$ mmm hardware/interfaces/xiaoqing/1.0/default/
编译成功:
虽然有了库,但是我们还需要构建binder通信服务.
vim hardware/interfaces/xiaoqing/1.0/default/android.hardware.naruto@1.0-service.rc
创建rc文件!(rc文件是被init进程访问的)
android.hardware.naruto@1.0-service.rc
- service xiaoqing_service /vendor/bin/hw/android.hardware.xiaoqing@1.0-service
- class hal
- user system
- group system
设定xiaoqing_service服务.
接下来完善服务代码:
service.cpp
vim hardware/interfaces/xiaoqing/1.0/default/service.cpp
服务代码:
- #define LOG_TAG "android.hardware.xiaoqing@1.0-service"
-
-
- #include <android/hardware/xiaoqing/1.0/IXiaoqing.h>
-
- #include <hidl/LegacySupport.h>
-
- using android::hardware::xiaoqing::V1_0::IXiaoqing;
- using android::hardware::defaultPassthroughServiceImplementation;
-
- int main() {
- return defaultPassthroughServiceImplementation<IXiaoqing>();
- }
在Android.bp中添加对服务器的编译:
vim hardware/interfaces/xiaoqing/1.0/default/Android.bp
Android.bp中添加:
- cc_binary {
- name: "android.hardware.xiaoqing@1.0-service",
- defaults: ["hidl_defaults"],
- proprietary: true,
- relative_install_path: "hw",
- srcs: ["service.cpp"],
- init_rc: ["android.hardware.xiaoqing@1.0-service.rc"],
- shared_libs: [
- "libhidlbase",
- "libhidltransport",
- "libutils",
- "liblog",
- "android.hardware.xiaoqing@1.0",
- "android.hardware.xiaoqing@1.0-impl",
- ],
- }
最后的目录结构:
为了让服务器被客户端访问到,还需要在device/fsl/mek_8q/manifest.xml
(不同厂商路径不同)添加如下:
然后在编译下:
$ mmm hardware/interfaces/xiaoqing/1.0/default/
ok, 只剩下测试了.O(∩_∩)O哈哈~
编译后生成的文件
最后会生成出一下文件:
- /vendor/lib64/hw/android.hardware.xiaoqing@1.0-impl.so
- /vendor/etc/init/android.hardware.xiaoqing@1.0-service.rc
- /vendor/bin/hw/android.hardware.xiaoqing@1.0-service
- /system/lib64/android.hardware.xiaoqing@1.0.so
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。