mkdir -p hardware/interfaces/hal/1.0cat > hardware/interfaces/hal/1.0/IHal.hal << EOF
package android.hardware.hal@1.0;import IHalClientCallback;
interface IHal {
SetHalClientCallback(IHalClientCallback clientCallback) generates (HalStatus status);
};
EOFcat > hardware/interfaces/hal/1.0/types.hal << EOF
package android.hardware.hal@1.0;enum HalStatus : uint32_t {
OK = 0,
NG = 1,
};
EOFcat > hardware/interfaces/hal/1.0/IHalClientCallback.hal << EOF
package android.hardware.hal@1.0;interface IHalClientCallback {
getCounter(uint32_t counter) generates (HalStatus status);
};
EOF./hardware/interfaces/update-makefiles.sh
PACKAGE=android.hardware.hal@1.0
LOC=./hardware/interfaces/hal/1.0/default
hidl-gen -o $LOC -L c++-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport $PACKAGE
hidl-gen -o $LOC -L androidbp-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport $PACKAGE
cat > hardware/interfaces/hal/1.0/default/Hal.cpp << EOF
#include "Hal.h"
#include <iostream>
#include <thread>namespace android {
namespace hardware {
namespace hal {
namespace V1_0 {
namespace implementation {using namespace std;
sp<IHalClientCallback> Hal::mHalClientCallback = nullptr;
// Methods from ::android::hardware::hal::V1_0::IHal follow.
Return<::android::hardware::hal::V1_0::HalStatus> Hal::setHalClientCallback(const sp<::android::hardware::hal::V1_0::IHalClientCallback>& clientCallback) {
// TODO implement
HalStatus status = HalStatus::NG;if(mHalClientCallback == nullptr) {
mHalClientCallback = clientCallback;
status = HalStatus::OK;
} else {
status = HalStatus::NG;
}
return ::android::hardware::hal::V1_0::HalStatus {};
}
Return<void> Hal::mLoop(void)
{
uint32_t counter = 0;while(1)
{
counter++;
if(mHalClientCallback == nullptr) {
//std::cout<<__func__<<":mHalClientCallback null"<<std::endl;
} else {
mHalClientCallback->getCounter(counter);
}
sleep(1);
}
}Hal::Hal(void)
{
thread *tid = new thread(mLoop);
(void)tid;
std::cout<<__func__<<"init done\n"<<std::endl;
}// Methods from ::android::hidl::base::V1_0::IBase follow.
//IHal* HIDL_FETCH_IHal(const char* /* name */) {
//return new Hal();
//}
//
} // namespace implementation
} // namespace V1_0
} // namespace hal
} // namespace hardware
} // namespace androidEOF
cat > hardware/interfaces/hal/1.0/default/Hal.h << EOF
#ifndef ANDROID_HARDWARE_HAL_V1_0_HAL_H
#define ANDROID_HARDWARE_HAL_V1_0_HAL_H#include <android/hardware/hal/1.0/IHal.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>namespace android {
namespace hardware {
namespace hal {
namespace V1_0 {
namespace implementation {
using ::android::hardware::hal::V1_0::IHal;
using ::android::hardware::hal::V1_0::IHalClientCallback;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;using ::android::hardware::hal::V1_0::IHalClientCallback;
struct Hal : public IHal {
Hal(void);
// Methods from ::android::hardware::hal::V1_0::IHal follow.
Return<::android::hardware::hal::V1_0::HalStatus> setHalClientCallback(const sp<::android::hardware::hal::V1_0::IHalClientCallback>& clientCallback) override;// Methods from ::android::hidl::base::V1_0::IBase follow.
private:
static sp<IHalClientCallback> mHalClientCallback;
static Return<void> mLoop(void);
};// FIXME: most likely delete, this is only for passthrough implementations
// extern "C" IHal* HIDL_FETCH_IHal(const char* name);} // namespace implementation
} // namespace V1_0
} // namespace hal
} // namespace hardware
} // namespace android#endif // ANDROID_HARDWARE_HAL_V1_0_HAL_H
EOF
cat > hardware/interfaces/hal/1.0/default/HalClientCallback.cpp << EOF
#include "HalClientCallback.h"
#include <iostream>namespace android {
namespace hardware {
namespace hal {
namespace V1_0 {
namespace implementation {
using namespace std;
// Methods from ::android::hardware::hal::V1_0::IHalClientCallback follow.
Return<::android::hardware::hal::V1_0::HalStatus> HalClientCallback::getCounter(uint32_t counter) {
// TODO implement
std::cout<<"counter = " << counter << std::endl;
return ::android::hardware::hal::V1_0::HalStatus::OK;
}
// Methods from ::android::hidl::base::V1_0::IBase follow.//IHalClientCallback* HIDL_FETCH_IHalClientCallback(const char* /* name */) {
//return new HalClientCallback();
//}
//
} // namespace implementation
} // namespace V1_0
} // namespace hal
} // namespace hardware
} // namespace androidEOF
cat > hardware/interfaces/hal/1.0/default/HalClientCallback.h << EOF
#ifndef ANDROID_HARDWARE_HAL_V1_0_HALCLIENTCALLBACK_H
#define ANDROID_HARDWARE_HAL_V1_0_HALCLIENTCALLBACK_H#include <android/hardware/hal/1.0/IHalClientCallback.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>namespace android {
namespace hardware {
namespace hal {
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 HalClientCallback : public IHalClientCallback {
// Methods from ::android::hardware::hal::V1_0::IHalClientCallback follow.
Return<::android::hardware::hal::V1_0::HalStatus> getCounter(uint32_t counter) override;// Methods from ::android::hidl::base::V1_0::IBase follow.
};
// FIXME: most likely delete, this is only for passthrough implementations
// extern "C" IHalClientCallback* HIDL_FETCH_IHalClientCallback(const char* name);} // namespace implementation
} // namespace V1_0
} // namespace hal
} // namespace hardware
} // namespace android#endif // ANDROID_HARDWARE_HAL_V1_0_HALCLIENTCALLBACK_H
EOF
cat > hardware/interfaces/hal/1.0/default/service.cpp << EOF
# define LOG_TAG "android.hardware.hal@1.0-service"
#include <hidl/HidlTransportSupport.h>
#include "Hal.h"
using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
using android::hardware::hal::V1_0::IHal;
using android::hardware::hal::V1_0::implementation::Hal;
using android::sp;
using android::OK;int main() {
configureRpcThreadpool(4, true);
sp<IHal> hal = new Hal();if(hal->registerAsService() != OK) {
printf("%s register ng\n", LOG_TAG);
} else {
printf("%s register ok\n", LOG_TAG);
joinRpcThreadpool();
}
return 0;
}EOF
cat > hardware/interfaces/hal/1.0/default/test.cpp << EOF
#define LOG_TAG "android.hardware.hello@1.0-test"
#include <hidl/LegacySupport.h>
#include <hidl/HidlSupport.h>
#include "Hal.h"
#include "HalClientCallback.h"using ::android::hardware::hal::V1_0::IHal;
using ::android::hardware::hal::V1_0::IHalClientCallback;
using ::android::hardware::hal::V1_0::implementation::HalClientCallback;
using ::android::sp;
using ::android::hardware::Return;
using ::android::hardware::Void;int main()
{
int ret = 0;sp<IHal> service = IHal::getService();
if(service == nullptr) {} else {
}
sp<IHalClientCallback> cb = new HalClientCallback();
service->setHalClientCallback(cb);while(1) {
sleep(1);
};
return ret;
}EOF
cat > hardware/interfaces/hal/1.0/default/Android.bp << EOF
cc_binary {
name: "android.hardware.hal@1.0-service",
init_rc: ["android.hardware.hal@1.0-service.rc"],
relative_install_path: "hw",
vendor: true,srcs: [
"Hal.cpp",
"service.cpp",
],cflags: [
"-Wall",
"-Werror",
],shared_libs: [
"libhidlbase",
"libhidltransport",
"libutils",
"liblog",
"android.hardware.hal@1.0",
],
}cc_binary {
name: "android.hardware.hal@1.0-test",
vendor: true,
srcs: [
"HalClientCallback.cpp",
"test.cpp",
],cflags: [
"-Wall",
"-Werror",
],shared_libs: [
"libhidlbase",
"libhidltransport",
"libutils",
"liblog",
"android.hardware.hal@1.0",
],
}EOF
cat > hardware/interfaces/hal/1.0/default/android.hardware.hal@1.0-service.rc << EOF
service hal_service /vendor/bin/hw/android.hardware.hal@1.0-service
class hal
user system
group system
EOF
#<9> let service can find android.hardware.hal@1.0.so
build/make/target/product/vndk/28.txt
build/make/target/product/vndk/current.txt
out/target/product/generic/obj/PACKAGING/vndk_intermediates/libs.txt
严格按字母顺序加入VNDK-core: android.hardware.hal@1.0.so
三个文件一样,若编译报错,按lib.txt重新添加#<10> let client can get services
device/generic/goldfish/manifest.xml(/vendor/manifest.xml)文件加入client才能拿到services
<hal format="hidl">
<name>android.hardware.hal</name>
<transport>hwbinder</transport>
<version>1.0</version>
<interface>
<name>IHal</name>
<instance>default</instance>
</interface>
</hal>#<11>Add hal HIDL *-impl and -service libraries to product packages
build/make/target/product/emulator.mk文件加入
PRODUCT_PACKAGES += android.hardware.hal@1.0-service
#<12>
mm hardware/interfaces/hal/1.0/
mmm hardware/interfaces/hal/1.0/default/
make -j 8
Android 9 HAL 回调函数
于 2022-05-19 20:33:53 首次发布
本文档展示了如何创建一个Android硬件抽象层(HAL)模块,包括定义接口、编写服务实现、客户端回调及测试用例。从创建头文件、更新Makefile到编译服务和测试程序,详细阐述了HAL组件的整个生命周期。
摘要由CSDN通过智能技术生成