Ahao's Technical Blog Ahao's Technical Blog
首页
  • 001.基础篇
  • 002.玩转AOSP篇
  • 003.学穿Binder篇
  • 004.基础组件篇
  • 005.系统启动过程分析
  • 006.Hal开发入门与实践
  • 007.显示系统
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

阿豪讲Framework

不积跬步无以至千里
首页
  • 001.基础篇
  • 002.玩转AOSP篇
  • 003.学穿Binder篇
  • 004.基础组件篇
  • 005.系统启动过程分析
  • 006.Hal开发入门与实践
  • 007.显示系统
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 基础篇

  • 玩转AOSP篇

  • 学穿Binder篇

  • 基础组件篇

  • 系统启动过程分析

  • Hal开发入门与实践

    • 001.Android HAL 层概览
    • 002.传统 Hal 开发指南1 —— 开发环境准备
    • 003.传统 Hal 开发指南2 —— 传统 HAL 整体架构
    • 004.传统 Hal 开发指南3 —— 驱动开发
    • 005.传统 Hal 开发指南4 —— 实现一个简单的 Hal 模块
    • 006.传统 Hal 开发指南5 —— 添加硬件访问服务
    • 007.传统 Hal 开发指南6 —— 开发一个 App 访问硬件服务
    • 008.HIDL HAL 开发指南1 —— 开发环境准备
    • 009.HIDL Hal 开发指南2 —— Android 8 HAL 变迁
    • 010.HIDL Hal 开发指南3 —— HIDL HAL 实例程序
    • 011.HIDL Hal 开发指南4 —— Binderized HALs 实例分析
    • 012.HIDL Hal 开发指南5 —— Passthrough HALs 实例分析
    • 013.HIDL Hal 开发指南6 —— Same-Process HALs 实例分析
    • 014.HIDL Hal 开发指南7 —— 驱动开发
    • 015.HIDL Hal 开发指南8 —— 简单 HIDL HAL 实现
    • 016.HIDL Hal 开发指南9 —— 添加硬件访问服务
    • 017.HIDL Hal 开发指南10 —— 开发一个 App 访问硬件服务
    • 018.AIDL Hal 开发指南1—— 开发环境准备
    • 019.AIDL Hal 开发指南2 —— AIDL HAL 整体架构
    • 020.AIDL Hal 开发指南 3 ———— AIDL HAL 实例分析1
    • 021.AIDL Hal 开发指南 4 ———— AIDL HAL 实例分析2
    • 022.AIDL Hal 开发指南5 —— stable-c HAL 实例分析
    • 023.AIDL Hal 开发指南6 —— 驱动开发
    • 024.AIDL Hal 开发指南7 —— 实现一个简单的 AIDL HAL
    • 025.AIDL Hal 开发指南8 —— 添加硬件访问服务
    • 026.AIDL Hal 开发指南10 —— AIDL HAL 的升级
      • 修改 aidl
      • 服务端类代码修改
      • 客户端代码修改
      • 编译文件修改
      • 编译运行
      • 参考资料
  • 显示系统

  • Framework
  • Hal开发入门与实践
阿豪
2024-03-29
目录

026.AIDL Hal 开发指南10 —— AIDL HAL 的升级

HAL 最后一篇教程,我们来看看 aidl hal 的升级。

我们之前写的 aidl hal 没有指定版本,默认版本为 V1。如果我们想要修改 aidl hal 就需要对 aidl hal 进行升级操作。

# 修改 aidl

第一步修改 aidl,在修改 aidl 之前需要执行以下命令冻结 aidl:

m android.hardware.hello-freeze-api
1

执行命令后,目录结构中会多一个名为 1 的文件夹:

20240418090025

接下来就可以修改 hardware/interfaces/hello_aidl_hal/aidl/android/hardware/hello/IHelloHal.aidl 了:

package android.hardware.hello;

@VintfStability 
interface IHelloHal {
    void hello_write(String str);
    String hello_read();

    //增加一个接口
    void hello_init();
}
1
2
3
4
5
6
7
8
9
10

修改的时候,通常是增加接口,不动之前的接口,保持老版本的兼容性。

修改完成后,接着执行:

m android.hardware.hello-update-api
1

执行完成后,会在 out/soong/.intermediates/hardware/interfaces/hello_aidl_hal 目录下生成 V2 相关的库:

20240418092102

# 服务端类代码修改

执行完成后,就在修改服务端实现:

// hardware/interfaces/hello_aidl_hal/aidl/default/HelloHalImpl.h

#ifndef ANDROID_HARDWARE_HELLO_H
#define ANDROID_HARDWARE_HELLO_H

#include <aidl/android/hardware/hello/BnHelloHal.h>

namespace aidl::android::hardware::hello {

class HelloHalImpl : public BnHelloHal {
  public:
        ::ndk::ScopedAStatus hello_write(const std::string& in_str) override;

        ::ndk::ScopedAStatus hello_read(std::string* _aidl_return) override;

        ::ndk::ScopedAStatus hello_init() override;
};

}  

#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// hardware/interfaces/hello_aidl_hal/aidl/default/HelloHalImpl.cpp
#define LOG_TAG "HelloHalImpl"
#define LOG_NDEBUG 0

#include "HelloHalImpl.h"

#include <log/log.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

namespace aidl::android::hardware::hello {


::ndk::ScopedAStatus HelloHalImpl::hello_write(const std::string& in_str) {
    ALOGD("write %s", in_str.c_str());
    int fd;
    int len = in_str.size();
	fd = open("/dev/hello", O_RDWR);
    write(fd, in_str.c_str(), len); 
    close(fd);   
    return ::ndk::ScopedAStatus::ok();
}
  
::ndk::ScopedAStatus HelloHalImpl::hello_read(std::string* _aidl_return)  {
    
    int fd;
    char buf[1024];
	fd = open("/dev/hello", O_RDWR);
    read(fd, buf, 1024);
    *_aidl_return = buf;
    return ::ndk::ScopedAStatus::ok();
}

::ndk::ScopedAStatus HelloHalImpl::hello_init() {
     ALOGD("Hello hal init");
    return ::ndk::ScopedAStatus::ok();
}

}  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

hello_init 中就简单打印一个日志。

# 客户端代码修改

    // hardware/interfaces/hello_aidl_hal/test_aidl_hal/main.cpp
    int main() {
        std::shared_ptr<IHelloHal> service = IHelloHal::fromBinder(ndk::SpAIBinder(AServiceManager_getService("android.hardware.hello.IHelloHal/default")));
        ALOGD("get service = %p\n",service.get());

        if (service == nullptr) {
            return -1;
        }
        // 添加 init
        service->hello_init();
        service->hello_write("hello");
        fflush(stdout);
        return EXIT_FAILURE;  // should not reach
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 编译文件修改

接着,修改 Android.bp:

// hardware/interfaces/hello_aidl_hal/aidl/default/Android.bp

cc_binary {
    name: "android.hardware.hello.example",
    relative_install_path: "hw",
    vendor: true,
    init_rc: ["android.hardware.hello.rc"],
    vintf_fragments: ["hellohal-default.xml"],
    shared_libs: [
        // 改成 v2
        "android.hardware.hello-V2-ndk", 
        "liblog",
        "libbase",
        "libcutils",
        "libutils",
        "libbinder_ndk",
    ],
    srcs: [
        "main.cpp",
        "HelloHalImpl.cpp",
    ],
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// hardware/interfaces/hello_aidl_hal/test_aidl_hal/Android.bp
cc_binary {
    name: "test_aidl_hal",
    vendor: true,
    shared_libs: [
        "android.hardware.hello-V2-ndk", 
        "liblog",
        "libbase",
        "libcutils",
        "libutils",
        "libbinder_ndk",
    ],
    srcs: [
        "main.cpp",
    ],
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 编译运行

最后编译运行:

source build/envsetup.sh
lunch aosp_cf_x86_64_phone-eng
m
cvd start  -kernel_path=/home/zzh0838/Project/aosp/kernel/out/virtual_device_x86_64/dist/bzImage  -initramfs_path=/home/zzh0838/Project/aosp/kernel/out/virtual_device_x86_64/dist/initramfs.img

adb shell 
test_aidl_hal
logcat | grep hello
1
2
3
4
5
6
7
8

log 信息如下:

04-18 17:31:06.700     0     0 W         : drivers/char/hello_driver.c hello_init line 69
04-18 17:31:07.913     0     0 I init    : Parsing file /vendor/etc/init/android.hardware.hello.rc...
04-18 17:31:12.555     0     0 I init    : starting service 'vendor.hellohal-default'...
04-18 17:31:12.565     0     0 I init    : ... started service 'vendor.hellohal-default' has pid 339
04-18 17:31:12.644     0     0 I servicemanager: Found android.hardware.hello.IHelloHal/default in device VINTF manifest.
04-18 17:31:19.401   560   560 I SystemServiceManager: Starting com.android.server.hello.HelloService
04-18 17:31:19.441     0     0 I servicemanager: Found android.hardware.hello.IHelloHal/default in device VINTF manifest.
04-18 17:31:19.422   560   560 D SystemServerTiming: OnBootPhase_100_com.android.server.hello.HelloService
04-18 17:31:19.422   560   560 V SystemServerTiming: OnBootPhase_100_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:31:21.769   560   560 D SystemServerTiming: OnBootPhase_200_com.android.server.hello.HelloService
04-18 17:31:21.769   560   560 V SystemServerTiming: OnBootPhase_200_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:31:36.456   560   560 D SystemServerTiming: OnBootPhase_480_com.android.server.hello.HelloService
04-18 17:31:36.456   560   560 V SystemServerTiming: OnBootPhase_480_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:31:36.517   560   560 D SystemServerTiming: OnBootPhase_500_com.android.server.hello.HelloService
04-18 17:31:36.517   560   560 V SystemServerTiming: OnBootPhase_500_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:31:36.876   560   560 D SystemServerTiming: OnBootPhase_520_com.android.server.hello.HelloService
04-18 17:31:36.876   560   560 V SystemServerTiming: OnBootPhase_520_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:31:36.957   560   560 D SystemServerTiming: OnBootPhase_550_com.android.server.hello.HelloService
04-18 17:31:36.957   560   560 V SystemServerTiming: OnBootPhase_550_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:31:38.655   560   560 D SystemServerTiming: OnBootPhase_600_com.android.server.hello.HelloService
04-18 17:31:38.655   560   560 V SystemServerTiming: OnBootPhase_600_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:31:38.769   560   560 D SystemServerTimingAsync: ssm.onStartUser-0_com.android.server.hello.HelloService
04-18 17:31:38.769   560   560 V SystemServerTimingAsync: ssm.onStartUser-0_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:31:43.730   560   587 D ActivityManagerTiming: OnBootPhase_1000_com.android.server.hello.HelloService
04-18 17:31:43.730   560   587 V ActivityManagerTiming: OnBootPhase_1000_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:31:44.105   560   594 D SystemServerTimingAsync: ssm.onUnlockingUser-0_com.android.server.hello.HelloService
04-18 17:31:44.105   560   594 V SystemServerTimingAsync: ssm.onUnlockingUser-0_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:31:45.150     0     0 W         : drivers/char/hello_driver.c hello_drv_open line 44
04-18 17:31:45.151     0     0 W         : drivers/char/hello_driver.c hello_drv_write line 37
04-18 17:31:45.151     0     0 W         : drivers/char/hello_driver.c hello_drv_close line 50
04-18 17:31:45.982   560   594 D SystemServerTimingAsync: ssm.onUnlockedUser-0_com.android.server.hello.HelloService
04-18 17:31:45.982   560   594 V SystemServerTimingAsync: ssm.onUnlockedUser-0_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:31:51.034   560  2617 D SystemServerTimingAsync: ssm.onCompletedEventUser-0_{|Unlocked|}_com.android.server.hello.HelloService
04-18 17:31:51.034   560  2617 V SystemServerTimingAsync: ssm.onCompletedEventUser-0_{|Unlocked|}_com.android.server.hello.HelloService took to complete: 0ms
04-18 17:34:48.428   339   339 D HelloHalImpl: write hello
04-18 17:34:48.467     0     0 W         : drivers/char/hello_driver.c hello_drv_open line 44
04-18 17:34:48.468     0     0 W         : drivers/char/hello_driver.c hello_drv_write line 37
04-18 17:34:48.468     0     0 W         : drivers/char/hello_driver.c hello_drv_close line 50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

打印信息可以看出访问到了驱动代码。

# 参考资料

  • android hal aidl升级部分-android framework车载手机系统开发 (opens new window)
025.AIDL Hal 开发指南8 —— 添加硬件访问服务
如何调试 SurfaceFlinger

← 025.AIDL Hal 开发指南8 —— 添加硬件访问服务 如何调试 SurfaceFlinger→

最近更新
01
如何调试 SurfaceFlinger
10-05
02
SurfaceFlinger 概述
10-05
03
HWC 接口分析
10-05
更多文章>
Theme by Vdoing | Copyright © 2020-2025 AHao Framework | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式