本文中Helloworld 位于hardware/interfaces中,所以目录路径为hardware/interfaces/helloworld/1.0。
cd ./hardware/interfaces/helloworld/1.0
创建文件IHelloWorld.hal
package android.hardware.helloworld@1.0;
?
interface IHelloWorld {
? ? justTest(string name) generates (string result, HelloTest value);
?
? ? justTest1(HelloTest name);
};
创建文件:types.hal
package android.hardware.helloworld@1.0;
?
enum HelloTest : uint8_t {
? ? V_TEST1 = 0,
? ? V_TEST2 = 1,
};
自动生成.bp文件的命令:
source ./build/envsetup.sh?
lunch
./hardware/interfaces/update-makefiles.sh?
用hidl-gen生成空.cpp文件,
#! /bin/bash
?
PACKAGE=android.hardware.helloworld@1.0
LOC=hardware/interfaces/helloworld/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
保存文件gen_cpp.sh,运行gen_cpp.sh后产生default目录和.cpp .h .bp文件,其中cpp文件就是我们需要实现的。
?? 编辑.cpp文件,修改如下:
#define LOG_TAG "HelloWorldImpl"
#include <log/log.h>
#include "HelloWorld.h"
namespace android {
namespace hardware {
namespace helloworld {
namespace V1_0 {
namespace implementation {
// Methods from ::android::hardware::helloworld::V1_0::IHelloWorld follow.
Return<void> HelloWorld::justTest(const hidl_string& name, justTest_cb _hidl_cb) {
ALOGD("justTest, name = %s", name.c_str());
_hidl_cb(name, HelloTest::V_TEST2);
//aa+bb=cc//zyf dbg
ALOGD("justTest end.");
return Void();
}
Return<void> HelloWorld::justTest1(::android::hardware::helloworld::V1_0::HelloTest name) {
ALOGD("justTest1, name = %hhu", name);
return Void();
}
// Methods from ::android::hidl::base::V1_0::IBase follow.
IHelloWorld* HIDL_FETCH_IHelloWorld(const char* name) {
ALOGD("FETCH IHelloWorld: %s", name);
if (name != nullptr)
return new HelloWorld();
else
return nullptr;
}
//
} // namespace implementation
} // namespace V1_0
} // namespace helloworld
} // namespace hardware
} // namespace android
创建service.cpp
#define LOG_TAG "android.hardware.helloworld@1.0-service"
?
#include <android/hardware/helloworld/1.0/IHelloWorld.h>
?
#include <hidl/LegacySupport.h>
#include "HelloWorld.h"
?
// Generated HIDL files
using android::hardware::helloworld::V1_0::IHelloWorld;
using android::hardware::helloworld::V1_0::implementation::HelloWorld;
?
using android::hardware::defaultPassthroughServiceImplementation;
using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
?
int main() {
#if 1
? ? return defaultPassthroughServiceImplementation<IHelloWorld>();
#else
? ? sp<IHelloWorld> service = new HelloWorld();
? ? configureRpcThreadpool(1, true /*callerWillJoin*/);
? ? if(android::OK != ?service->registerAsService())
? ? ? ? return 1;
? ? joinRpcThreadpool();
#endif
}?
修改Android.bp
cc_library_shared {
? ? name: "android.hardware.helloworld@1.0-impl",
? ? relative_install_path: "hw",
? ? proprietary: true,
? ? srcs: [
? ? ? ? "HelloWorld.cpp",
? ? ],
? ? shared_libs: [
?? ? ? ?"liblog",
? ? ? ? "libhidlbase",
? ? ? ? "libhidltransport",
? ? ? ? "libutils",
? ? ? ? "android.hardware.helloworld@1.0",
? ? ],
}
cc_binary {
? ? name: "android.hardware.helloworld@1.0-service",
? ? defaults: ["hidl_defaults"],
? ? relative_install_path: "hw",
? ? vendor: true,
? ? init_rc: ["android.hardware.helloworld@1.0-service.rc"],
? ? srcs: [
? ? ? ? "HelloWorld.cpp",
? ? ? ? "service.cpp"
? ? ],
? ? shared_libs: [
? ? ? ? "liblog",
? ? ? ? "libhidlbase",
? ? ? ? "libhidltransport",
? ? ? ? "libutils",
? ? ? ? "libhardware",
? ? ? ? "android.hardware.helloworld@1.0",
? ? ],
}
创建android.hardware.helloworld@1.0-service.rc
service helloworld-hal-1-0 /vendor/bin/hw/android.hardware.helloworld@1.0-service
? ? class hal
? ? user system
? ? group system
编译可执行二进制: mmm hardware/interfaces/helloworld/1.0/default/
生成.so文件: out/target/product/lime/system/lib64/vndk-29/android.hardware.helloworld@1.0.so out/target/product/lime/vendor/lib64/hw/android.hardware.helloworld@1.0-impl.so
生成可执行文件: out/target/product/lime/vendor/bin/hw/android.hardware.helloworld@1.0-service 启动脚本: out/target/product/lime/vendor/etc/init/android.hardware.helloworld@1.0-service.rc
--------------------------------------------------------------------------------------------------
到此,服务、.so库文件、都已经编译完成,下面开始客户端的调用的实现
--------------------------------------------------------------------------------------------------- 创建直通模式客户端 client.cpp:(default目录)
#include <android/hardware/helloworld/1.0/IHelloWorld.h>
#include <hidl/Status.h>
#include <hidl/LegacySupport.h>
#include <utils/misc.h>
#include <hidl/HidlSupport.h>
#include <stdio.h>
using android::hardware::helloworld::V1_0::IHelloWorld;
using android::sp;
using android::hardware::hidl_string;
int main()
{
? ? int ret = 0;
? ? android::sp<IHelloWorld> service = IHelloWorld::getService();
? ? if(service == nullptr) {
? ? ? ? printf("Failed to get service\n");
? ? ? ? ret = -1;
? ? ? ? return ret;
? ? }
? ? ::android::hardware::helloworld::V1_0::HelloTest name = {};
? ? service->justTest1(name);
? ? return ret;
}
创建Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PROPRIETARY_MODULE := true
LOCAL_MODULE := helloworld_client
LOCAL_SRC_FILES := \
? ? client.cpp \
?
LOCAL_SHARED_LIBRARIES := \
? ?liblog \
? ?libhidlbase \
? ?libutils \
? ?android.hardware.helloworld@1.0 \
?
include $(BUILD_EXECUTABLE)
编译可执行二进制: mmm hardware/interfaces/helloworld/1.0/default/ 生成文件: out/target/product/lime/vendor/bin/helloworld_client
将vendor烧到手机上后,可直接运行helloworld_client。
参考文章:
https://blog.csdn.net/shift_wwx/article/details/86530600
|