一、什么是androidhidl
Android硬件抽象层(HAL)提供了一种用于将硬件外设(如传感器、GPS、摄像头等)的逻辑实现抽象出来的机制。但由于JNI的局限性,HAL的开发有一定的困难。Android-HIDL(Hardware Interface Definition Language),即Android硬件接口定义语言,是一种跨语言(C++、Java、Rust等)标准化方法,它可以简化HAL的开发,并提高了系统运行效率。
二、为什么要使用androidhidl
使用Android-HIDL可以使HAL开发更加便捷,从而提高生产力。使用Android-HIDL还可以改善系统运行效率,可以发现比使用JNI更快。此外,使用Android-HIDL还可以使不同语言之间的代码互通,促进跨语言编程。
三、Android-HIDL的使用场景
Android-HIDL的主要使用场景是设备外设的开发,如传感器、GPS、摄像头、蓝牙等等。除此之外,使用Android-HIDL也可以简化Android系统编写过程,使得代码的可读性和可维护性得到提高。
四、Android-HIDL代码示例
// Example.hidl
package example.v1_0;
interface IExample {
enum ValueType {
INT32,
INT64,
STRING,
};
struct ComplexType {
ValueType type;
union {
int32_t intValue;
int64_t longValue;
hidl_string stringValue;
}
};
int32_t getVersion();
bool setValue(in ComplexType type);
ComplexType getValue();
}
// Example.cpp
#include "Example.h"
using example::v1_0::IExample;
using example::v1_0::ComplexType;
using example::v1_0::ValueType;
struct Example : public IExample {
Example() {}
int32_t getVersion() override {
return 1;
}
bool setValue(in ComplexType type) override {
// Process the input ComplexType value.
return true;
}
ComplexType getValue() override {
ComplexType value;
value.type = ValueType::INT32;
value.intValue = 42;
return value;
}
};
int main() {
sp example = new Example();
ALOGI("The example version is %d", example->getVersion());
ComplexType value;
value.type = ValueType::INT32;
value.intValue = 10;
example->setValue(value);
ComplexType result = example->getValue();
ALOGI("The result value is %d", result.intValue);
return 0;
}
五、如何使用Android-HIDL
使用Android-HIDL可以分为三个步骤:定义HAL接口、实现服务端代码、实现客户端代码。
第一步:定义HAL接口
// Example.hidl
package example.v1_0;
interface IExample {
enum ValueType {
INT32,
INT64,
STRING,
};
struct ComplexType {
ValueType type;
union {
int32_t intValue;
int64_t longValue;
hidl_string stringValue;
}
};
int32_t getVersion();
bool setValue(in ComplexType type);
ComplexType getValue();
}
定义HAL接口使用HIDL语言开发,例如example为包名,v1_0表示版本号,IExample为接口名。在接口内定义复杂类型和相关方法。
第二步:实现服务端代码
// Example.cpp
#include "Example.h"
using example::v1_0::IExample;
using example::v1_0::ComplexType;
using example::v1_0::ValueType;
struct Example : public IExample {
Example() {}
int32_t getVersion() override {
return 1;
}
bool setValue(in ComplexType type) override {
// Process the input ComplexType value.
return true;
}
ComplexType getValue() override {
ComplexType value;
value.type = ValueType::INT32;
value.intValue = 42;
return value;
}
};
实现服务端代码需要在宿主机系统(linux)上编写。在实现代码中需要指定相应的HIDL版本号和接口名。实现代码中需要继承接口,并且实现相关方法。通过IPC机制,与客户端交互。
第三步:实现客户端代码
// ExampleClient.cpp
#include
#include
#include "Example.h"
using android::hardware::defaultServiceManager;
using android::hardware::hidl_string;
using android::hardware::Return;
using android::hardware::Void;
using example::v1_0::IExample;
using example::v1_0::ComplexType;
using example::v1_0::ValueType;
using android::hidl::manager::V1_0::IServiceManager;
using android::hidl::manager::V1_0::IServiceNotification;
int main() {
sp example = IExample::getService();
if (example == nullptr) {
ALOGE("Cannot find IExample service.");
return -1;
}
ALOGI("The example version is %d", example->getVersion());
ComplexType value;
value.type = ValueType::INT32;
value.intValue = 10;
example->setValue(value);
ComplexType result = example->getValue();
ALOGI("The result value is %d", result.intValue);
return 0;
}
实现客户端代码需要在Android设备上编写(C++、Java、Rust等都可以,只要遵循HIDL规范即可)。客户端代码中需要引入HIDL历程生成的头文件和IServiceManager.h、IServiceNotification.h文件。客户端代码中需要通过defaultServiceManager方法获取服务端IExample服务,之后就可以调用相关的方法。这里需要注意,服务端和客户端可能运行在不同的Android进程上,所以必须通过IPC通信机制,在HAL服务端和客户端之间传输数据。
原创文章,作者:CADXO,如若转载,请注明出处:https://www.506064.com/n/329221.html
微信扫一扫
支付宝扫一扫