做单位网站的公司吗,网络营销怎样,广东手机网站建设公司,外贸获客简述
当你在Android系统中使用不同的硬件设备#xff08;例如摄像头、传感器、音频设备等#xff09;时#xff0c;你需要与硬件抽象层#xff08;HAL#xff09;进行通信。 HAL是一个中间层#xff0c;它充当了硬件和应用程序之间的桥梁。但是#xff0c;由于硬件设备…简述
当你在Android系统中使用不同的硬件设备例如摄像头、传感器、音频设备等时你需要与硬件抽象层HAL进行通信。 HAL是一个中间层它充当了硬件和应用程序之间的桥梁。但是由于硬件设备的不同HAL接口在不同的硬件之间也会有所不同。这就是Android HAL接口定义语言HIDL的用武之地。
HIDL允许开发人员为每个硬件设备定义自己的接口。这些接口定义了硬件设备的功能和特性包括输入参数、返回值和异常。使用这些接口应用程序可以直接与硬件设备通信而无需了解特定的硬件细节。在Android中HIDL被广泛用于与HAL进行通信。
在Android Project Treble被提出在android O中被全面的推送设计 HIDL 这个机制的目的主要目的是把框架framework与 HAL 进行隔离使得框架部分可以直接被覆盖、更新而不需要重新对 HAL 进行编译。
HIDL 实际上是用于进行进程间通信Inter-process CommunicationIPC的。进程间的通信可以称为 Binder 化Binderized。对于必须连接到进程的库也可以使用 passthough 模式但在Java中不支持。
官方介绍
编写代码
在Aosp代码目录中创建目录 hardware/interfaces/stksensor/1.0(有关1.0版本相关的可以查看官网的版本管理概念)
1、新建文件IStksensor.hal
package android,hardware.stksensor1.0;improt IStksensorCallBack;interface IStksensor {setCallBack(IStksensorCallBack callback);write(string data) generates (bool res);init() generates (MyResult result);
};2、创建UDT(用户自定义类型)types.hal
package android.hardware.stksensor1.0;enum ResultCode : int32_t {UNKNOWN -1,ERROR 0,OK 1,};struct MyResult{ResultCode resultCode;string msg;
};3、新建文件回调文件IStksensorCallBack.hal
package android.hardware.stksensor1.0;interface IStksensorCallBack {passData(uint32_t data) generates (MyResult result);
};
使用hidl-gen生成相关文件
在终端执行以下命令设置临时变量
PACKAGEandroid.hardware.stksensor1.0
LOChardware/interfaces/stksensor/1.0/default在AOSP源码根目录执行hidl-gen生成default目录里的c文件
hidl-gen -o $LOC -Lc-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE在AOSP源码根目录执行hidl-gen生成default目录里的Android.bp文件
hidl-gen -o $LOC -Landroidbp-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE在AOSP源码根目录执行./hardware/interfaces/update-makefiles.sh会生成hardware/interfaces/stksensor/1.0/目录里的Android.bp文件
实现接口代码
修改hardware/interfaces/stksensor/1.0/default/Stksensor.cpp这边实现单纯是打印日志
// FIXME: your file license if you have one#include Stksensor.h
#include log/log.hnamespace android::hardware::stksensor::implementation {// Methods from ::android::hardware::stksensor::V1_0::IStksensor follow.
Returnvoid Stksensor::setCallBack(const sp::android::hardware::stksensor::V1_0::IStksensorCallBack callback) {// TODO implementALOGE(stksensor service have called the funtion of setCallBack);return Void();
}Return::android::hardware::stksensor::V1_0::ResultCode Stksensor::write(const hidl_string data) {// TODO implementALOGE(stksensor service have called the funtion of write);return ::android::hardware::stksensor::V1_0::ResultCode {};
}Returnvoid Stksensor::init(init_cb _hidl_cb) {// TODO implementALOGE(stksensor service have called the funtion of init);return Void();
}// Methods from ::android::hidl::base::V1_0::IBase follow.//IStksensor* HIDL_FETCH_IStksensor(const char* /* name */) {//return new Stksensor();
//}
//
} // namespace android::hardware::stksensor::implementation
添加启动服务
1、在hardware/interfaces/stksensor/1.0/default/创建service.cpp
#define LOG_TAG android.hardware.stksensor1.0-service
#include hidl/HidlTransportSupport.h
#include log/log.husing android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
using android::hardwara::stksensor::V1_0::IStksensor;
using android::hardware::stksensor::V1_0::implementation::Stksensor;
using namespace android;int main() {configureRpcThreadpool(1, true);spIStksensor service new Stksensor();status_t status service-registerAsService(stksensor); if (status ! OK) {ALOGE(Error registering stksensor as service: %d, status);return status;}ALOGE(Ok to registering stksensor as service);joinRpcThreadpool();return 1;
}
2、在hardware/interfaces/stksensor/1.0/default/创建android.hardware.stksensor1.0-service.rc启动脚本
service stksensor-hal-1-0 /vendor/bin/hw/android.hardware.stksensor1.0-serviceclass haluser systemgroup system3、在hardware/interfaces/stksensor/1.0/default/创建android.hardware.stksensor1.0-service.xml的vintf
manifest version1.0 typedevicehal formathidlnameandroid.hardware.stksensor/nametransporthwbinder/transportversion1.0/versioninterfacenameIStksensor/nameinstancestksensor/instance/interface/hal
/manifest修改编译脚本
1、再修改hardware/interfaces/stksensor/1.0/default/Android.bp
// FIXME: your file license if you have onecc_library_shared {// FIXME: this should only be -impl for a passthrough hal.// In most cases, to convert this to a binderized implementation, you should:// - change -impl to -service here and make it a cc_binary instead of a// cc_library_shared.// - add a *.rc file for this module.// - delete HIDL_FETCH_I* functions.// - call configureRpcThreadpool and registerAsService on the instance.// You may also want to append -impl/-service with a specific identifier like// -vendor or -hardware identifier etc to distinguish it.name: android.hardware.stksensor1.0-impl,relative_install_path: hw,// FIXME: this should be vendor: true for modules that will eventually be// on AOSP.proprietary: true,srcs: [Stksensor.cpp,StksensorCallBack.cpp,],shared_libs: [libhidlbase,libutils,android.hardware.stksensor1.0,],
}cc_binary {name: android.hardware.stksensor1.0-service,relative_install_path: hw,defaults: [hidl_defaults],proprietary: true,init_rc: [android.hardware.stksensor1.0-service.rc],vintf_fragments: [android.hardware.stksensor1.0-service.xml],srcs: [SerialPort.cpp,service.cpp,],shared_libs: [libbase,liblog,libdl,libutils,libhardware,libhidlbase,libhidltransport,android.hardware.stksensor1.0,],
}
2、在device/$your_company/$SOC/device.mk添加
PRODUCT_PACKAGES \android.hardware.stksensor1.0-service \android.hardware.stksensor1.0-impl现在stksensor目录结构
stksensor/
└── 1.0├── Android.bp├── default│ ├── Android.bp│ ├── android.hardware.stksensor1.0-service.rc│ ├── android.hardware.stksensor1.0-service.xml│ ├── service.cpp│ ├── StksensorCallBack.cpp│ ├── StksensorCallBack.h│ ├── Stksensor.cpp│ └── Stksensor.h├── IStksensorCallBack.hal├── IStksensor.hal└── types.hal注意会报一些avc权限 需要自己添加