• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android9.0 HAL 层开发

武飞扬头像
水无声风无痕
帮助1

一、生成 HAL 代码

1. 生成 hidl-gen 工具

  1.  
    source source ./build/envsetup.sh
  2.  
    lunch your_project
  3.  
    make hidl-gen -j4

2. 编写接口文件

编写 types.hal, IHello.hal, IHelloCallback.hal,Android.bp

  1.  
    1. IHello.hal 代码
  2.  
    package vendor.tests.hardware.hello@1.0;
  3.  
     
  4.  
    import types;
  5.  
    import IHelloCallback;
  6.  
     
  7.  
    interface IHello {
  8.  
    get_data_char() generates(Status st, vec<int8_t> version);
  9.  
     
  10.  
    write_data_char(vec<int8_t>data, uint32_t size) generates(int32_t result);
  11.  
     
  12.  
    write_data_string(string data) generates(int32_t result);
  13.  
    };
  1.  
    2. types.hal 代码
  2.  
    package vendor.tests.hardware.hello@1.0;
  3.  
     
  4.  
    enum Status : int32_t{
  5.  
    SUCCESS,
  6.  
    FAILURE
  7.  
    };
  1.  
    3. IHelloCallback.hal 代码
  2.  
    package vendor.tests.hardware.hello@1.0;
  3.  
     
  4.  
    interface IHelloCallback {
  5.  
    oneway onNotifyHelloEvent(string event);
  6.  
    };
  1.  
    4. Android.bp 代码
  2.  
    如果 HAL 代码是在hardware/interfaces 目录下面,则可以在 hardware/interfaces/ 目录执行下面自动生成下面的 Android.bp
  3.  
     
  4.  
    ./update-makefiles.sh
  1.  
    hidl_interface {
  2.  
    name: "vendor.tests.hardware.hello@1.0",
  3.  
    root: "vendor.tests.hardware",
  4.  
    srcs: [
  5.  
    "types.hal",
  6.  
    "IHello.hal",
  7.  
    "IHelloCallback.hal",
  8.  
    ],
  9.  
    interfaces: [
  10.  
    "android.hidl.base@1.0",
  11.  
    ],
  12.  
    types: [
  13.  
    "Status",
  14.  
    ],
  15.  
    gen_java: true,
  16.  
    }
学新通

注意,由于 "vendor.test.hardware.hello@1.0" root 是 "vendor.test.hardware"
所以这个 root 一定是之前定义过的,如果自己新建的root,可以参考下面新建root,比如:

  1.  
    root 包 bp参考写法:
  2.  
    vendor/tests/hardware/Android.bp
  3.  
    hidl_package_root {
  4.  
        name: "vendor.tests.hardware", // root 包名
  5.  
        path: "vendor/tests/interfaces" // root 路径
  6.  
    }

那么所有依赖 "vendor.tests.hardware" 的都在 "vendor/tests/interfaces" 目录下面,比如 "vendor.tests.hardware.hello@1.0" 就在 "vendor/tests/interfaces" 下面新建 "hello/1.0" 目录,然后在该目录下面新建 "Android.bp"、 "types.hal"、"IHello.hal"、"IHelloCallback.hal" 等。

在"vendor/tests/interfaces/" 目录下面新建具体实现的目录,比如 "impl",在impl下面新建 ”hello“对应的目录,比如 "hello/1.0/default"目录


3. 配置临时变量,包名,生成代码路径

  1.  
    // 包名
  2.  
    PACKAGE=vendor.tests.hardware.hello@1.0
  3.  
     
  4.  
    // 生成代码路径
  5.  
    LOC=vendor/tests/interfaces/impl/hello/1.0/default/

4. 生成代码

  1.  
    生成 c 代码:其中 LOC 指定生成代码的路径:
  2.  
    hidl-gen -o $LOC -Lc -impl -rvendor.tests.hardware:vendor/tests/interfaces/ -randroid.hidl:system/libhidl/transport $PACKAGE
  3.  
     
  4.  
    其中 "-r" 参数作用 "-r <package:path root>: E.g., android.hardware:hardware/interfaces." 生成的 c 代码路径由 "-o" 参数指定,即上面的 LOC 变量定义的路径
  5.  
     
  6.  
    生成 c 代码的 Android.bp:
  7.  
    hidl-gen -o $LOC -Landroidbp-impl -rvendor.tests.hardware:vendor/tests/interfaces/ -randroid.hidl:system/libhidl/transport $PACKAGE
  8.  
     
  9.  
    更新代码 bp 文件
  10.  
    vendor/tests/interfaces/update-makefiles.sh
  11.  
    // 该文件一般的 interfaces 的root 目录都会有,参考 hardware/interfaces/update-makefiles.sh

二、修改 c 实现代码

1. 通过类方式实现功能

修改 Helllo.cpp HelloCallback.cpp "struct" 为 "class",并实现对应构造方法,析构方法。

  1.  
    // Hello.h 代码
  2.  
    #ifndef VENDOR_TESTS_HARDWARE_HELLO_V1_0_HELLO_H
  3.  
    #define VENDOR_TESTS_HARDWARE_HELLO_V1_0_HELLO_H
  4.  
     
  5.  
    #include <vendor/tests/hardware/hello/1.0/IHello.h>
  6.  
    #include <hidl/MQDescriptor.h>
  7.  
    #include <hidl/Status.h>
  8.  
     
  9.  
    namespace vendor {
  10.  
    namespace tests {
  11.  
    namespace hardware {
  12.  
    namespace hello {
  13.  
    namespace V1_0 {
  14.  
    namespace implementation {
  15.  
     
  16.  
    using ::android::hardware::hidl_array;
  17.  
    using ::android::hardware::hidl_memory;
  18.  
    using ::android::hardware::hidl_string;
  19.  
    using ::android::hardware::hidl_vec;
  20.  
    using ::android::hardware::Return;
  21.  
    using ::android::hardware::Void;
  22.  
    using ::android::sp;
  23.  
     
  24.  
    class Hello : public IHello {
  25.  
    public:
  26.  
    // Methods from ::vendor::tests::hardware::hello::V1_0::IHello follow.
  27.  
    Return<void> get_data_char(get_data_char_cb _hidl_cb) override;
  28.  
    Return<int32_t> write_data_char(const hidl_vec<int8_t>& data, uint32_t size) override;
  29.  
    Return<int32_t> write_data_string(const hidl_string& data) override;
  30.  
     
  31.  
    // Methods from ::android::hidl::base::V1_0::IBase follow.
  32.  
    Hello();
  33.  
    ~Hello();
  34.  
    void onServiceDied();
  35.  
    };
  36.  
     
  37.  
    // FIXME: most likely delete, this is only for passthrough implementations
  38.  
    // extern "C" IHello* HIDL_FETCH_IHello(const char* name);
  39.  
     
  40.  
    } // namespace implementation
  41.  
    } // namespace V1_0
  42.  
    } // namespace hello
  43.  
    } // namespace hardware
  44.  
    } // namespace tests
  45.  
    } // namespace vendor
  46.  
     
  47.  
    #endif // VENDOR_TESTS_HARDWARE_HELLO_V1_0_HELLO_H
学新通
  1.  
    // HelloCallback.h 代码
  2.  
    #ifndef VENDOR_TESTS_HARDWARE_HELLO_V1_0_HELLOCALLBACK_H
  3.  
    #define VENDOR_TESTS_HARDWARE_HELLO_V1_0_HELLOCALLBACK_H
  4.  
     
  5.  
    #include <vendor/tests/hardware/hello/1.0/IHelloCallback.h>
  6.  
    #include <hidl/MQDescriptor.h>
  7.  
    #include <hidl/Status.h>
  8.  
     
  9.  
    namespace vendor {
  10.  
    namespace tests {
  11.  
    namespace hardware {
  12.  
    namespace hello {
  13.  
    namespace V1_0 {
  14.  
    namespace implementation {
  15.  
     
  16.  
    using ::android::hardware::hidl_array;
  17.  
    using ::android::hardware::hidl_memory;
  18.  
    using ::android::hardware::hidl_string;
  19.  
    using ::android::hardware::hidl_vec;
  20.  
    using ::android::hardware::Return;
  21.  
    using ::android::hardware::Void;
  22.  
    using ::android::sp;
  23.  
     
  24.  
    class HelloCallback : public IHelloCallback {
  25.  
    // Methods from ::vendor::tests::hardware::hello::V1_0::IHelloCallback follow.
  26.  
    Return<void> onNotifyHelloEvent(const hidl_string& event) override;
  27.  
     
  28.  
    // Methods from ::android::hidl::base::V1_0::IBase follow.
  29.  
     
  30.  
    };
  31.  
     
  32.  
    // FIXME: most likely delete, this is only for passthrough implementations
  33.  
    // extern "C" IHelloCallback* HIDL_FETCH_IHelloCallback(const char* name);
  34.  
     
  35.  
    } // namespace implementation
  36.  
    } // namespace V1_0
  37.  
    } // namespace hello
  38.  
    } // namespace hardware
  39.  
    } // namespace tests
  40.  
    } // namespace vendor
  41.  
     
  42.  
    #endif // VENDOR_TESTS_HARDWARE_HELLO_V1_0_HELLOCALLBACK_H
  43.  
     
  44.  
     
  45.  
    // HelloCallback.cpp 代码
  46.  
    #include "HelloCallback.h"
  47.  
     
  48.  
    namespace vendor {
  49.  
    namespace tests {
  50.  
    namespace hardware {
  51.  
    namespace hello {
  52.  
    namespace V1_0 {
  53.  
    namespace implementation {
  54.  
     
  55.  
    // Methods from ::vendor::tests::hardware::hello::V1_0::IHelloCallback follow.
  56.  
    Return<void> HelloCallback::onNotifyHelloEvent(const hidl_string& event) {
  57.  
    // TODO implement
  58.  
    return Void();
  59.  
    }
  60.  
     
  61.  
     
  62.  
    // Methods from ::android::hidl::base::V1_0::IBase follow.
  63.  
     
  64.  
    //IHelloCallback* HIDL_FETCH_IHelloCallback(const char* /* name */) {
  65.  
    //return new HelloCallback();
  66.  
    //}
  67.  
    //
  68.  
    } // namespace implementation
  69.  
    } // namespace V1_0
  70.  
    } // namespace hello
  71.  
    } // namespace hardware
  72.  
    } // namespace tests
  73.  
    } // namespace vendor
学新通

2. 实现 HelloDeathListener 类

该类继承于 "android::hardware::hidl_death_recipient",并实现 “serviceDied” 方法,在该方法中调用 Helllo 类的 "onServiceDied"方法

  1.  
    // HelloDeathListener.h 代码
  2.  
    #ifndef VENDOR_TESTS_HARDWARE_HELLO_V1_0_HELLO_DEATH_LISTENE_H
  3.  
    #define VENDOR_TESTS_HARDWARE_HELLO_V1_0_HELLO_DEATH_LISTENE_H
  4.  
     
  5.  
    #include <hidl/MQDescriptor.h>
  6.  
    #include <hidl/Status.h>
  7.  
    #include <android-base/file.h>
  8.  
    #include <android-base/logging.h>
  9.  
    #include <android-base/properties.h>
  10.  
    #include <android-base/stringprintf.h>
  11.  
    #include <android-base/strings.h>
  12.  
    #include "Hello.h"
  13.  
    #include "HelloCallback.h"
  14.  
     
  15.  
    namespace vendor {
  16.  
    namespace tests {
  17.  
    namespace hardware {
  18.  
    namespace hello {
  19.  
    namespace V1_0 {
  20.  
    namespace implementation {
  21.  
     
  22.  
    using ::android::hardware::hidl_array;
  23.  
    using ::android::hardware::hidl_memory;
  24.  
    using ::android::hardware::hidl_string;
  25.  
    using ::android::hardware::hidl_vec;
  26.  
    using ::android::hardware::Return;
  27.  
    using ::android::hardware::Void;
  28.  
    using ::android::hidl::base::V1_0::IBase;
  29.  
    using ::android::sp;
  30.  
    using ::android::wp;
  31.  
     
  32.  
    class Hello;
  33.  
     
  34.  
    class HelloDeathListener : public android::hardware::hidl_death_recipient {
  35.  
    public:
  36.  
    HelloDeathListener(Hello *hello);
  37.  
    virtual ~HelloDeathListener();
  38.  
    virtual void serviceDied(uint64_t cookie, const wp<IBase>& who) override;
  39.  
    private:
  40.  
    Hello *mHello;
  41.  
    };
  42.  
     
  43.  
    } // namespace implementation
  44.  
    } // namespace V1_0
  45.  
    } // namespace hello
  46.  
    } // namespace hardware
  47.  
    } // namespace tests
  48.  
    } // namespace vendor
  49.  
     
  50.  
    #endif // VENDOR_TESTS_HARDWARE_HELLO_V1_0_HELLO_DEATH_LISTENE_H
  51.  
     
  52.  
     
  53.  
    // HelloDeathListener.cpp 代码
  54.  
    #include "HelloDeathListener.h"
  55.  
    #include <log/log.h>
  56.  
     
  57.  
    #undef LOG_TAG
  58.  
    #define LOG_TAG "Hello@DeathListener"
  59.  
     
  60.  
    namespace vendor {
  61.  
    namespace tests {
  62.  
    namespace hardware {
  63.  
    namespace hello {
  64.  
    namespace V1_0 {
  65.  
    namespace implementation {
  66.  
     
  67.  
    using namespace android::base;
  68.  
    using namespace std;
  69.  
     
  70.  
    HelloDeathListener::HelloDeathListener(Hello *hello) {
  71.  
    ALOGE("%s", __FUNCTION__);
  72.  
    if (hello != nullptr) {
  73.  
    mHello = hello;
  74.  
    }
  75.  
    }
  76.  
     
  77.  
    HelloDeathListener:: ~HelloDeathListener() {}
  78.  
     
  79.  
    void HelloDeathListener::serviceDied(uint64_t cookie, const wp<IBase>& who) {
  80.  
    ALOGE("%s", __FUNCTION__);
  81.  
    if (mHello != nullptr) {
  82.  
    mHello->onServiceDied();
  83.  
    }
  84.  
    }
  85.  
     
  86.  
    } // namespace implementation
  87.  
    } // namespace V1_0
  88.  
    } // namespace hello
  89.  
    } // namespace hardware
  90.  
    } // namespace tests
  91.  
    } // namespace vendor
学新通

3. 根据 IHello.hal 实现对应接口

  1.  
    IHello.hal 中接口:
  2.  
     
  3.  
    // 该方法参数都为 输出参数,即返回值为 Status,返回数据为 vector<int8_t> 实际为 vector<char>,可进行数据转换。
  4.  
    get_data_char() generates(Status st, vec<int8_t> version);
  5.  
     
  6.  
    // 该方法参数为 两个输入参赛,一个返回值。
  7.  
    write_data_char(vec<int8_t>data, uint32_t size) generates(int32_t result);
  8.  
     
  9.  
    / 该方法参数为 一个输入参赛,一个返回值。
  10.  
    write_data_string(string data) generates(int32_t result);
  11.  
     
  12.  
     
  13.  
    // Hello.cpp 实现:
  14.  
    #include "Hello.h"
  15.  
    #include <string.h>
  16.  
    #include <log/log.h>
  17.  
    #include <android/log.h>
  18.  
     
  19.  
    namespace vendor {
  20.  
    namespace tests {
  21.  
    namespace hardware {
  22.  
    namespace hello {
  23.  
    namespace V1_0 {
  24.  
    namespace implementation {
  25.  
     
  26.  
    Hello::Hello()
  27.  
    {}
  28.  
     
  29.  
    Hello::~Hello()
  30.  
    {
  31.  
     
  32.  
    }
  33.  
     
  34.  
    void Hello::onServiceDied()
  35.  
    {
  36.  
    ALOGE("invoked ");
  37.  
    }
  38.  
     
  39.  
     
  40.  
    // Methods from ::vendor::tests::hardware::hello::V1_0::IHello follow.
  41.  
    Return<void> Hello::get_data_char(get_data_char_cb _hidl_cb) {
  42.  
    static char buffer[] = "hello return data!";
  43.  
    std::vector<int8_t> data(buffer, buffer strlen(buffer));
  44.  
     
  45.  
    _hidl_cb(Status::SUCCESS, (const hidl_vec<int8_t>)data);
  46.  
    return Void();
  47.  
    }
  48.  
     
  49.  
    Return<int32_t> Hello::write_data_char(const hidl_vec<int8_t>& data, uint32_t size) {
  50.  
    ALOGE("data: %s, len: %d", data.data(), size);
  51.  
     
  52.  
    return int32_t {0};
  53.  
    }
  54.  
     
  55.  
    Return<int32_t> Hello::write_data_string(const hidl_string& data) {
  56.  
    ALOGE("data: %s", data.c_str());
  57.  
    return int32_t {0};
  58.  
    }
  59.  
     
  60.  
    // Methods from ::android::hidl::base::V1_0::IBase follow.
  61.  
     
  62.  
    //IHello* HIDL_FETCH_IHello(const char* /* name */) {
  63.  
    //return new Hello();
  64.  
    //}
  65.  
    //
  66.  
    } // namespace implementation
  67.  
    } // namespace V1_0
  68.  
    } // namespace hello
  69.  
    } // namespace hardware
  70.  
    } // namespace tests
  71.  
    } // namespace vendor
学新通

4. 实现HelloService 

  1.  
    // HelloService.cpp 代码
  2.  
    #define LOG_TAG "vendor.tests.hardware.hello@1.0-service"
  3.  
     
  4.  
    #include <stdio.h>
  5.  
    #include <android/log.h>
  6.  
    #include <utils/SystemClock.h>
  7.  
    #include <hidl/HidlTransportSupport.h>
  8.  
    #include "Hello.h"
  9.  
     
  10.  
    using namespace android;
  11.  
    using namespace android::hardware;
  12.  
    using namespace vendor::tests::hardware::hello::V1_0::implementation;
  13.  
    using namespace std::placeholders;
  14.  
     
  15.  
    int main(int /* argc */, char * /* argv */[])
  16.  
    {
  17.  
    auto service = std::make_unique<Hello>();
  18.  
    configureRpcThreadpool(4, true /* callerWillJoin */);
  19.  
    ALOGD("Hello HAL service starting");
  20.  
    status_t status = service->registerAsService();
  21.  
    if (status != OK)
  22.  
    {
  23.  
    ALOGE("Unable to register Hello HAL service (%d)", status);
  24.  
    return 1;
  25.  
    }
  26.  
    ALOGI("Register Hello Service successfully");
  27.  
    joinRpcThreadpool();
  28.  
    return 1;
  29.  
    }
学新通

5. 更新 Android.bp

  1.  
    // Android.bp 代码
  2.  
    // new add
  3.  
    cc_defaults {
  4.  
    name: "hello_defaults",
  5.  
    shared_libs: [
  6.  
    "liblog",
  7.  
    "libutils",
  8.  
    "libhidlbase",
  9.  
    "libhardware",
  10.  
    "libhidltransport",
  11.  
    ],
  12.  
    vendor: true,
  13.  
    compile_multilib: "64",
  14.  
    cflags: [
  15.  
    "-Wall",
  16.  
    "-Wextra",
  17.  
    "-Werror",
  18.  
    "-Wno-unused-parameter"
  19.  
    ],
  20.  
    }
  21.  
     
  22.  
    // modify
  23.  
    cc_library_shared {
  24.  
    name: "vendor.tests.hardware.hello@1.0-impl",
  25.  
    relative_install_path: "hw",
  26.  
    defaults: ["hello_defaults"],
  27.  
    proprietary: true,
  28.  
    srcs: [
  29.  
    "Hello.cpp",
  30.  
    "HelloCallback.cpp",
  31.  
    "HelloDeathListener.cpp",
  32.  
    ],
  33.  
    shared_libs: [
  34.  
    "libhidlbase",
  35.  
    "libhidltransport",
  36.  
    "libutils",
  37.  
    "vendor.tests.hardware.hello@1.0",
  38.  
    ],
  39.  
    }
  40.  
     
  41.  
    // new add
  42.  
    cc_binary {
  43.  
    name: "vendor.tests.hardware.hello@1.0-service",
  44.  
    relative_install_path: "hw",
  45.  
    defaults: ["hello_defaults"],
  46.  
    init_rc: ["vendor.tests.hardware.hello@1.0-service.rc"],
  47.  
    rtti: false,
  48.  
    srcs: [
  49.  
    "HelloService.cpp"
  50.  
    ],
  51.  
    header_libs: [],
  52.  
    shared_libs: [
  53.  
    "libbase",
  54.  
    "libprotobuf-cpp-lite",
  55.  
    "libcommon-base",
  56.  
    "vendor.tests.hardware.hello@1.0",
  57.  
    "vendor.tests.hardware.hello@1.0-impl",
  58.  
    ],
  59.  
    cppflags: [
  60.  
    "-fexceptions"
  61.  
    ]
  62.  
    }
学新通

6.  HAL service 启动 rc文件

  1.  
    //vendor.tests.hardware.hello@1.0-service.rc
  2.  
    service vendor.tests.hardware.hello-1.0 /vendor/bin/hw/vendor.tests.hardware.hello@1.0-service
  3.  
    class hal
  4.  
    user system
  5.  
    group system inet

7. selinux 标签修改

  1.  
    在 xxx.te 中对 service 标签进行修改
  2.  
     
  3.  
    // file_contexs
  4.  
    /(vendor|system/vendor)/bin/hw/vendor.tests.hardware.hello@1.0-service u:object_r:hal_hello_default_exec:s0
  5.  
     
  6.  
    // hal_hello_default_exec.te
  7.  
    # add for hello
  8.  
    type hal_hello_default, domain, mlstrustedsubject;
  9.  
    typeattribute hal_hello_default nobohaldomain, mlstrustedsubject;
  10.  
    hal_server_domain(hal_hello_default, hal_hello)
  11.  
    type hal_hello_default_exec, exec_type, vendor_file_type, file_type;
  12.  
     
  13.  
    init_daemon_domain(hal_hello_default)
  14.  
     
  15.  
    # Allow hwbinder call from hal client to server
  16.  
    binder_call(hal_hello_client, hal_hello_server)
  17.  
     
  18.  
    # Add hwservice related rules
  19.  
    add_hwservice(hal_hello_server, hal_hello_hwservice)
  20.  
    allow hal_hello_client hal_hello_hwservice:hwservice_manager find;
  21.  
     
  22.  
    #hwbinder_use(hal_hello)
  23.  
    get_prop(hal_hello, hwservicemanager_prop)
  24.  
    allow hal_hello_default mnt_vendor_file:dir rw_dir_perms;
  25.  
     
  26.  
    allow hal_hello_default tee_device:chr_file { ioctl open read write };
  27.  
    allow hal_hello_default ion_device:chr_file { ioctl open read write };
  28.  
     
  29.  
     
  30.  
    typeattribute hal_hello_default system_writes_vendor_properties_violators;
  31.  
     
  32.  
    allow hal_hello_default mnt_vendor_crypto_file:dir create_dir_perms;
  33.  
    allow hal_hello_default mnt_vendor_crypto_file:file create_file_perms;
  34.  
    allow hal_hello_default mnt_vendor_crypto_file:filesystem getattr;
  35.  
    allow hal_hello_default vendor_data_nfs_file:dir search;
  36.  
     
  37.  
    allow hal_hello_default pki_sdk_file:file { read open execute getattr map };
  38.  
    allow hal_hello_default default_prop:file { read open execute getattr map };
  39.  
    allow hal_hello_default vendor_bean_prop:file { read open execute getattr map };
  40.  
    allow hal_hello_default property_socket:sock_file { write };
  41.  
    allow hal_hello_default init:unix_stream_socket { connectto };
  42.  
     
  43.  
    allow hal_hello_default vfat:dir create_dir_perms;
  44.  
    allow hal_hello_default vfat:file create_file_perms;
  45.  
    allow hal_hello_default vfat:filesystem getattr;
  46.  
    allow hal_hello_default mnt_user_log_file:lnk_file read;
  47.  
    allow hal_hello_default mnt_user_file:dir search;
  48.  
    allow hal_hello_default mnt_user_file:lnk_file read;
  49.  
    allow hal_hello_default unlabeled:dir { search write read add_name open create };
  50.  
    allow hal_hello_default unlabeled:file { read append write open create getattr };
学新通
  1.  
    // 其中 hal_hello_default_exec 需要自己定义,具体参考系统中的其他修改
  2.  
    // 同时 attributes、hwservice.te、hwservice_contexts 需要对应修改
  3.  
     
  4.  
    // attributes 修改
  5.  
    attribute hal_hello;
  6.  
    expandattribute hal_hello true;
  7.  
    attribute hal_hello_client;
  8.  
    expandattribute hal_hello_client true;
  9.  
    attribute hal_hello_server;
  10.  
    expandattribute hal_hello_server false;
  11.  
     
  12.  
    // hwservice.te
  13.  
    type hal_hello_hwservice, hwservice_manager_type;
  14.  
     
  15.  
    // hwservice_contexts
  16.  
    vendor.tests.hardware.hello::IHello u:object_r:hal_hello_hwservice:s0
学新通

8. 对应模块加入系统编译

  1.  
    xxx.mk
  2.  
     
  3.  
    PRODUCT_PACKAGES = \
  4.  
    vendor.tests.hardware.hello@1.0 \
  5.  
    vendor.tests.hardware.hello@1.0-impl \
  6.  
    vendor.tests.hardware.hello@1.0-service \

三、client 实现

  1.  
    #define LOG_TAG "HelloClient"
  2.  
     
  3.  
    #include <log/log.h>
  4.  
    #include <android/log.h>
  5.  
     
  6.  
    #include <hidl/HidlSupport.h>
  7.  
    #include <hidl/Status.h>
  8.  
    #include <vendor/tests/hardware/hello/1.0/IHello.h>
  9.  
     
  10.  
    using namespace vendor::tests::hardware::hello::V1_0;
  11.  
     
  12.  
    using ::android::hardware::hidl_array;
  13.  
    using ::android::hardware::hidl_memory;
  14.  
    using ::android::hardware::hidl_string;
  15.  
    using ::android::hardware::hidl_vec;
  16.  
    using ::android::hardware::Return;
  17.  
    using ::android::hardware::Void;
  18.  
    using vendor::tests::hardware::hello::V1_0::IHello;
  19.  
    using vendor::tests::hardware::hello::V1_0::Status;
  20.  
    using android::sp;
  21.  
    using android::status_t;
  22.  
     
  23.  
    int main(){
  24.  
    sp<IHello> helloService = IHello::getService();
  25.  
    if (helloService == nullptr) {
  26.  
    LOGE("can't not get IHello service!");
  27.  
    return -1;
  28.  
    }
  29.  
     
  30.  
    Status status = FAILURE;
  31.  
    std::vector<int8_t> readBuffer;
  32.  
    helloService->get_data_char([&](Status st, std::vector<int8_t> data){
  33.  
    status = st; readBuffer = data;
  34.  
    });
  35.  
     
  36.  
     
  37.  
    char str[] = "write data string .....";
  38.  
    std::vector<int8_t> writeBuffer(str, str strlen(str));
  39.  
    int len = helloService->write_data_char(writeBuffer, strlen(str));
  40.  
     
  41.  
    }
学新通

四. 完整测试代码

https://download.csdn.net/download/SHK242673/72544934

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfaggf
系列文章
更多 icon
同类精品
更多 icon
继续加载