一、什麼是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/zh-tw/n/329221.html