android P hal开发demo(改)

注:在没有特殊说明下,所有操作都在aosp目录下进行。

1.编译(基于ubuntu16.04)

1.1先安装repo工具(参考https://mirrors.tuna.tsinghua.edu.cn/help/git-repo/)

1.2代码下载

到内网10.19.118.212的/home/neusoft/android_source/zhangshuang文件夹下(用户名:neusoft,密码:root)用smb把aosp_9_back.tar.gz拷贝到任意目录,然后解压

或者

$ repo init -u https://android.googlesource.com/platform/manifest -b android-9.0.0_r39 --depth=1

$ repo sync

1.3编译

$ source build/envsetup.sh

// 因为我们要实验car的版本

image

$ lunch 10

$ make -j8

image

1.4启动模拟器

编译成功之后,输入命令emulator可以启动模拟器(模拟器更多指令参数参照emulator -help)

image

2.HAL服务端开发

2.1.编写hal文件

在AOSP代码的目录 hardware/interfaces/中新建test/1.0/,在此目录中新建 ITest.hal,内容:

package android.hardware.test@1.0;
interface ITest{
    helloWorld(string name) generates (string result);
};

2.2.使用hidl-gen生成对应的c++文件

2.2.1编译hidl-gen工具

make hidl-gen

2.2.2设置临时变量

PACKAGE=android.hardware.test@1.0
LOC=hardware/interfaces/test/1.0/default

2.2.3生成c++文件

(在aosp根目录下执行,source完之后执行croot可以回到aosp根目录)

hidl-gen -o $LOC -Lc++-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
image

2.2.4生成Android.bp文件

hidl-gen -o $LOC -Landroidbp-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
image

2.2.5生成1.0目录下的Android.bp

执行./hardware/interfaces/update-makefiles.sh

image

2.3实现c++文件

2.3.1Test.h文件实现passthrough模式

#ifndef ANDROID_HARDWARE_TEST_V1_0_TEST_H
#define ANDROID_HARDWARE_TEST_V1_0_TEST_H
#include <android/hardware/test/1.0/ITest.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>
namespace android {
namespace hardware {
namespace test {
namespace V1_0 {
namespace implementation {
using ::android::hardware::hidl_array;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;
struct Test : public ITest {
    // Methods from ::android::hardware::test::V1_0::ITest follow.
    Return<void> helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) override;
    // Methods from ::android::hidl::base::V1_0::IBase follow.
};
// FIXME: most likely delete, this is only for passthrough implementations
extern "C" ITest* HIDL_FETCH_ITest(const char* name);
}  // namespace implementation
}  // namespace V1_0
}  // namespace test
}  // namespace hardware
}  // namespace android
#endif  // ANDROID_HARDWARE_TEST_V1_0_TEST_H

2.3.2Test.cpp实现

#include "Test.h"
namespace android {
namespace hardware {
namespace test {
namespace V1_0 {
namespace implementation {
// Methods from ::android::hardware::test::V1_0::ITest follow.
Return<void> Test::helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) {
    // TODO implement
    // 做了简单实现 start @{
    printf("Test helloWorld");
    char buf[100];
    memset(buf, 0, 100);
    snprintf(buf, 100, "Hello World, %s", name.c_str());
    hidl_string result(buf);
    _hidl_cb(result);
    // 做了简单实现 end @}
    return Void();
}
// Methods from ::android::hidl::base::V1_0::IBase follow.
ITest* HIDL_FETCH_ITest(const char* /* name */) {
    return new Test();
}
}  // namespace implementation
}  // namespace V1_0
}  // namespace test
}  // namespace hardware
}  // namespace android

2.4添加启动service

在hardware/interfaces/test/1.0/default目录下新建android.hardware.test@1.0-service.rc 启动脚本

service test_hal_service /vendor/bin/hw/android.hardware.test@1.0-service
    class hal
    user system
    group system
image

2.5新建service.cpp,采用直通式

#define LOG_TAG "android.hardware.test@1.0-service"
#include <android/hardware/test/1.0/ITest.h>
#include <hidl/LegacySupport.h>
using android::hardware::test::V1_0::ITest;
using android::hardware::defaultPassthroughServiceImplementation;;
//passthrough mode
int main() {
  return defaultPassthroughServiceImplementation<ITest>();
//passthrough mode
}
image

2.6修改hardware/interfaces/test/1.0/default目录下的Android.bp

cc_library_shared {
    name: "android.hardware.test@1.0-impl",
    relative_install_path: "hw",
    proprietary: true,
    srcs: [
        "Test.cpp",
    ],
    shared_libs: [
        "libhidlbase",
        "libhidltransport",
        "libutils",
        "android.hardware.test@1.0",
    ],
}
cc_binary {
    name: "android.hardware.test@1.0-service",
    defaults: ["hidl_defaults"],
    proprietary: true,
    relative_install_path: "hw",
    srcs: ["service.cpp"],
    init_rc: ["android.hardware.test@1.0-service.rc"],
    shared_libs: [
        "libhidlbase",
        "libhidltransport",
        "libutils",
        "liblog",
        "android.hardware.test@1.0",
    ],
}

2.7调用update-makefiles.sh更新一下

执行./hardware/interfaces/update-makefiles.sh

image

2.8.编译

mmm ./hardware/interfaces/test/1.0

image

3.VNDK相关(https://chendongqi.me/2019/09/01/hidl-binderizing-passthrough/)

build/make/target/product/vndk/28.txt增加一行


2020-04-13 09-59-56屏幕截图.png

build/make/target/product/vndk/current.txt增加一行


2020-04-13 09-59-56屏幕截图.png

4.device部分

为了保证新增的hidl是可以被客户端访问到的,由于此次要使用emulator验证,并且lunch的是aosp_car_x86_64-eng所以在device找到下面目录device/generic/car/common/manifest.xml添加如下内容:

    <hal format="hidl">
        <name>android.hardware.test</name>
        <transport>hwbinder</transport>
        <version>1.0</version>
        <interface>
            <name>ITest</name>
            <instance>default</instance>
        </interface>
    </hal>

5.完整编译

在aosp根目录执行make编译出相应的镜像

5.1错误处理

Added VNDK-core: android.hardware.power@1.3.so
 error: VNDK library list has been changed.
        Changing the VNDK library list is not allowed in API locked branches.
[  2% 13/554] build out/target/product/g...pp_intermediates/oat/x86_64/package.odex
ninja: build stopped: subcommand failed.
09:47:07 ninja failed with: exit status 1

#### failed to build some targets (14 seconds) ####

将build/make/target/product/vndk/28.txt
build/make/target/product/vndk/current.txt
与out/target/product/generic_x86_64/obj/PACKAGING/vndk_intermediates/libs.txt
文件比较,将libs.txt中的内容同步到28.txt和current.txt文件中。


2020-04-13 10-49-40屏幕截图.png

6.启动服务

6.1启动模拟器

// -writable-system让模拟器可写,-selinux disabled跳过selinux检查
emulator -writable-system -selinux disabled

执行如下命令

// 获取root权限

adb root

// 获取模拟器写权限

adb remount

// 进入模拟器控制台

adb shell

// 启动hal服务

vendor/bin/hw/android.hardware.test@1.0-service &

image

6.2查看服务是否启动成功

ps -A |grep "test"

image

7.客户端测试程序

7.1.c++客户端

7.1.1内容编写

在packages/apps目录下新建testApp/native文件夹,创建Android.mk,内容如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_PROPRIETARY_MODULE := true
LOCAL_MODULE := test
LOCAL_SRC_FILES := \
client.cpp \

LOCAL_SHARED_LIBRARIES := \
liblog \
libhidlbase \
libutils \
android.hardware.test@1.0 \

include $(BUILD_EXECUTABLE)

创建client.cpp内容如下:

# include <android/hardware/test/1.0/ITest.h>
# include <hidl/Status.h>
# include <hidl/LegacySupport.h>
# include <utils/misc.h>
# include <hidl/HidlSupport.h>
# include <stdio.h>
using android::hardware::test::V1_0::ITest;
using android::sp;
using android::hardware::hidl_string;
int main()
{
    fprintf(stderr,"%s\n" , "start");
    android::sp<ITest> service = ITest::getService();
    printf("%s\n", "service");
    if(service == nullptr) {
        printf("Failed to get service\n");
        return -1;
    }
    service->helloWorld("test", [&](hidl_string result) {
                printf("%s\n", result.c_str());
        });
    printf("%s\n", "end");
    return 0;
}
image

7.1.2.编译客户端

mmm packages/apps/testApp/native/,编译成功之后

image

7.1.3.把可执行文件push到模拟器中

adb push out/target/product/generic_x86/vendor/bin/test /vendor/bin/

image

7.1.4.在模拟器控制台中验证是否成功

image

8.在App中调用hal服务

在编译hal服务端的时候,自动编译出来的java端支持的客户端,编译的结果在(可以通过反编译来查看里面的具体内容)

image

打包之后再模拟器的路径

image

所以我们的app只需要引入包即可调用hal的客户端

8.1编写HAL客户端

在packages/apps/testApp中新建如下目录(完整程序在git@github.com:nicholaszhou/HalClientTest.git)

image

编译客户端程序

mmm packages/apps/testApp

image

编译成功之后,在会生成客户端的apk程序

image

把apk安装到模拟器中

adb install -r out/target/product/generic_x86/system/app/TestApp/TestApp.apk

image

点击运行程序

image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,667评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,361评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,700评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,027评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,988评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,230评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,705评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,366评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,496评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,405评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,453评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,725评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,803评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,015评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,514评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,111评论 2 341

推荐阅读更多精彩内容