1. 程式人生 > >Android P HIDL demo程式碼編寫 (原創)

Android P HIDL demo程式碼編寫 (原創)

之前的文章已經分析了HIDL服務的註冊和呼叫,這篇文章來總結下一個HIDL的服務如何編寫。

縮寫HAL檔案

 首先要確認放置資料夾和介面的包名,因為這跟後面使用指令碼生成一部分程式碼有關,一般預設的放在hardware/interfaces目錄下,我們寫一個簡單的IDemo.hal (hardware/interface/demo/1.0/IDemo.hal)

package [email protected];

interface IDemo {
    init(int32_t level) generates (int32_t initRet);

    getProperty(string key) generates (int32_t getResult, string value);
};

 生成介面的執行程式碼

使用如下命令生成執行檔案:

hidl-gen -o hardware/interfaces/demo/1.0/default -Lc++-impl -randroid.hardware:hardware/intefaces -randroid.hidl:system/libhidl/transport [email protected]

這個命令會在hardware/interfaces/demo/1.0/default 下生成Demo.cpp 和 Demo.h兩個檔案,上面那個介面檔案裡,getProperty裡有兩個返回值,一般函式都只有一個返回值,那這個兩個返回值是回事呢?那我們看看生成的執行程式碼是怎麼樣的?

#include "Demo.h"

namespace android {
namespace hardware {
namespace demo {
namespace V1_0 {
namespace implementation {

Return<int32_t> Demo::init(int32_t level) {
//這種返回一個引數就按普通函式的做法,返回值就OK return int32_t{}; } Return<void> Demo::getProperty(const hidl_string& key, getProperty_cb _hidl_cb ) { //這裡將兩個返回值封裝到一個回撥函式裡,然後傳給呼叫者,使用如下 int result = 0; //使用map 裡值來返回
  std::Map<hidl_string, hidl_string> demoMap = {
    {"one", "first"},
    {"two", "second"},
    {"three", "third"}
  };

  int result = -1;
  hidl_string value = "";
  auto findRet = demoMap.find(key);
  if(findRet != demoMap.end()){
    result = 0;
    value = findRet->second;
  }
  //這裡就將result 和 value 作為引數直接回調給呼叫者
  _hidl_cb(result, value); return Void(); } } } } }

生成Android.bp

方法實現完了之後,就需要用命令再生成一個Android.bp

hidl-gen -o hardware/interfaces/demo/1.0/default -Landroidbp-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport [email protected]

執行命令之後,就會在hardeare/interfaces/demo/1.0/default/Android.bp

進入hardware/interface目錄下,執行./update-makefiles.sh之後,會生成demo/1.0/Android.bp 檔案

現在有了兩個Android.bp之後,需要採用繫結模式來建立一個Demo服務,在demo/1.0/default下建立一個service.cpp

新增繫結模式的服務程式碼

#include <android-base/logging.h>
#include <hidl/HidlTransportSupport.h>
#include <android/hardware/demo/1.0/IDemo.h>

#undef LOG_TAG
#define LOG_TAG "[email protected]"

#include <hidl/LegacySupport.h>
#include "Demo.h"
using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
using android::hardware::demo::V1.0::implementation::Demo;

int main(){
    configureRpcThreadpool(4, true);
    Demo demo = new Demo();
    auto status = demo.registerAsService();
    if(status != android::OK){
        ALOGE("register demo As Service failed");
        return -1;
    }
    joinRpcThreadpool();
}

 然後再在demo/1.0/default下建立[email protected]

service demo_hal_service /vendor/bin/hw/[email protected]
    class hal
    user  system
    group  system

在demo/1.0/default/Android.bp中增加 init_rc和 service.cpp的編譯項,並將cc_library_shared 改為 cc_binary ,具體如下:

cc_binary {
    name: "[email protected]",
    relative_install_path: "hw",
    proprietary: true,
    init_rc: ["[email protected]"],
    srcs: [
        "Demo.cpp",
        "service.cpp"
    ],  
    shared_libs: [
        "libhidlbase",
        "libhidltransport",
        "libutils",
     "liblog", "[email protected]", ], }

 然後通過mmm 或 mm 的方式 就可以編譯出[email protected] 和 [email protected]的服務了。

如果要自執行,新增上Selinux 的相關許可權,可以參考其它HIDL 服務的selinux。

關於呼叫HIDL的介面,之前文章分析過,下面我們寫核心的幾句程式碼:

#include <android/hardware/demo/1.0/IDemo.h>
sp<IDemo> mDemo = IDemo::getService();
mDemo->init(1);
string mValue;
mDemo->getProperty("two", [&](int result, string value) {
       if(result == 0)
             mVaule = value;
});

到這裡,HIDL的Demo服務程式碼可以結束了,後面可以增加複雜的Icallback HIDL介面相關的程式碼。

&n