android 9.0从driver到APP(3)--HIDL

本文详细介绍了在Android 9.0中使用HIDL从创建接口描述文件到更新Makefile,再到service.cpp和服务的实现,客户端库的修改,以及测试代码的编写和运行过程中遇到的问题及其解决方案。通过实例展示了HIDL在硬件抽象层的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


注:本文为个人学习记录,可能存在个别或多处错误,欢迎指正和讨论。

参考:
https://www.jianshu.com/p/ca6823b897b5
https://source.android.com/devices/architecture
进行学习。
android9.0 从driver到APP(2)–hardware

一、 接口描述文件创建

1.1 创建HIDL目录

mkdir -p hardware/interfaces/sample/1.0

1.2 创建接口描述文件sample.hal,

/*
 * Copyright (C) 2019 Alex
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hardware.sample@1.0;

interface ISample {
   
   

    init();

    close();

    Read(vec<int32_t> val);

    Write(int32_t val);

    getVal(vec<int32_t> val);

    setVal(int32_t val);

};

1.3 生成HAL 相关文件

在Android根目录,使用hidl-gen工具生成HAL 相关文件

hidl-gen -o hardware/interfaces/sample/1.0/default -Lc++-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport android.hardware.sample@1.0

二 、更新Makefile

然后使用脚本来更新Makefile,自动生成 Android.bp

 ./hardware/interfaces/update-makefiles.sh

update-makefiles.sh脚本是SDK自带的。

目录结构:

./
├── Android.bp
├── default
│   ├── Sample.cpp
│   └── Sample.h
└── ISample.hal

在1.0的目录的Android.bp内容如下:

// This file is autogenerated by hidl-gen -Landroidbp.

hidl_interface {
   
   
    name: "android.hardware.sample@1.0",
    root: "android.hardware",
    vndk: {
   
   
        enabled: true,
    },
    srcs: [
        "ISample.hal",
    ],
    interfaces: [
        "android.hidl.base@1.0",
    ],
    gen_java: true,
}

三 、 service.cpp相关

3.1 service.cpp创建

在default目录创建 service.cpp
内容可以从其他文件夹中复制过来再修改:
改完之后的内容如下:

/*
 * Copyright (C) 2019 Alex
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "android.hardware.sample@1.0-service"

#include <android/hardware/sample/1.0/ISample.h>
#include <hidl/LegacySupport.h>

using android::hardware::sample::V1_0::ISample;
using android::hardware::defaultPassthroughServiceImplementation;

int main() {
   
   
    return defaultPassthroughServiceImplementation<ISample>();
}

在default 目录创建一个

android.hardware.sample@1.0-service.rc

这个文件是自动启动的时候需要使用。

3.2 Android.bp

default目录没有自动生成Android.bp。
从其他地方拷贝个修改吧

cc_library_shared {
   
   
    name: "android.hardware.sample@1.0-impl",
    defaults: ["hidl_defaults"],
    proprietary: true,
    relative_install_path: "hw",
    srcs: ["Sample.cpp"],

    cflags: [
        "-Wall",
        "-Werror",
    ],

    shared_libs
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值