AIDL Hal 开发笔记7----AIDL HAL 的升级

目录


|------------------|
| AIDL HAL 的升级 |

一、修改 aidl

当前整体目录结构如下

c 复制代码
hardware/interfaces/hello_aidl_hal/aidl$ tree
.
├── aidl_api
│   └── android.hardware.hello
│       ├── 1
│       │   └── android
│       │       └── hardware
│       │           └── hello
│       │               └── IHello.aidl
│       └── current
│           └── android
│               └── hardware
│                   └── hello
│                       └── IHello.aidl
├── android
│   └── hardware
│       └── hello
│           └── IHello.aidl
├── Android.bp
└── default
    ├── Android.bp
    ├── android.hardware.hello.rc
    ├── hellohal-default.xml
    ├── HelloHalImpl.cpp
    ├── HelloHalImpl.h
    └── main.cpp

先修改aidl接口 hardware/interfaces/hello_aidl_hal/aidl/android/hardware/hello/IHelloHal.aidl

c 复制代码
package android.hardware.hello;

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

    //增加一个接口
    void hello_init();
}

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

修改完成后,接着执行:

c 复制代码
m android.hardware.hello-freeze-api
c 复制代码
out/target/product/ceres-b7/obj/CONFIG/kati_packaging/dist.mk was modified, regenerating...
[100% 263/263] //hardware/interfaces/hello_aidl_hal/aidl:android.hardware.hello-api Making AIDL API of android.hardware.hello as version 2

#### build completed successfully (57 seconds) ####

执行完成后,再次执行 tree 应该能看到 2 目录:

c 复制代码
├── aidl_api
│   └── android.hardware.hello
│       ├── 1
│       │   └── android
│       │       └── hardware
│       │           └── hello
│       │               └── IHello.aidl
│       ├── 2
│       │   └── android
│       │       └── hardware
│       │           └── hello
│       │               └── IHello.aidl
│       └── current
│           └── android
│               └── hardware
│                   └── hello
│                       └── IHello.aidl
├── android
│   └── hardware
│       └── hello
│           └── IHello.aidl
├── Android.bp
└── default
    ├── Android.bp
    ├── android.hardware.hello.rc
    ├── hellohal-default.xml
    ├── HelloHalImpl.cpp
    ├── HelloHalImpl.h
    └── main.cpp

18 directories, 11 files

编译模块

c 复制代码
mmm hardware/interfaces/hello_aidl_hal/

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

c 复制代码
out/soong/.intermediates/hardware/interfaces/hello_aidl_hal/aidl$ ll
total 104
drwxrwxr-x 26 xuejie xuejie 4096 Jan 26 13:33 ./
drwxrwxr-x  4 xuejie xuejie 4096 Jan 20 15:47 ../
drwxrwxr-x  3 xuejie xuejie 4096 Jan 26 13:33 android.hardware.hello-api/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 22 11:13 android.hardware.hello-java/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 22 11:13 android.hardware.hello-java-source/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 22 11:13 android.hardware.hello-ndk_platform-source/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 22 11:13 android.hardware.hello-ndk-source/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 20 13:53 android.hardware.hello-V1-java/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 26 13:33 android.hardware.hello-V1-java-source/
drwxrwxr-x  6 xuejie xuejie 4096 Jan 20 14:05 android.hardware.hello-V1-ndk/
drwxrwxr-x  7 xuejie xuejie 4096 Jan 20 14:05 android.hardware.hello-V1-ndk_platform/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 26 13:33 android.hardware.hello-V1-ndk_platform-source/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 26 13:33 android.hardware.hello-V1-ndk-source/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 26 13:33 android.hardware.hello-V2-java/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 26 13:33 android.hardware.hello-V2-java-source/
drwxrwxr-x  6 xuejie xuejie 4096 Jan 26 13:33 android.hardware.hello-V2-ndk/
drwxrwxr-x  7 xuejie xuejie 4096 Jan 26 13:33 android.hardware.hello-V2-ndk_platform/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 26 13:33 android.hardware.hello-V2-ndk_platform-source/
drwxrwxr-x  3 xuejie xuejie 4096 Jan 26 13:33 android.hardware.hello-V2-ndk-source/

二、服务端代码修改

执行完成后,就在修改服务端实现:
hardware/interfaces/hello_aidl_hal/aidl/default/HelloHalImpl.h

c 复制代码
#ifndef ANDROID_HARDWARE_HELLO_H
#define ANDROID_HARDWARE_HELLO_H

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

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

class HelloHalImpl : public BnHello {
  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

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

c 复制代码
#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();
}

}  

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


三、客户端代码修改

hardware/interfaces/hello_aidl_hal/test_aidl_hal/main.cpp

c 复制代码
int main() {
std::shared_ptr<IHelloHal> service = IHelloHal::fromBinder(ndk::SpAIBinder(AServiceManager_getService("android.hardware.hello.IHello/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
}

四、编译文件修改

接着,修改 Android.bp:
hardware/interfaces/hello_aidl_hal/aidl/default/Android.bp

c 复制代码
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: [
        "android.hardware.hello-V2-ndk_platform",
        "liblog",
        "libbase",
        "libcutils",
        "libutils",
        "libbinder_ndk",
    ],
    srcs: [
        "main.cpp",
        "HelloHalImpl.cpp",
    ],
}

hardware/interfaces/hello_aidl_hal/test_aidl_hal/Android.bp

c 复制代码
cc_binary {
    name: "test_aidl_hal",
    vendor: true,
    shared_libs: [
        "android.hardware.hello-V2-ndk_platform",
        "liblog",
        "libbase",
        "libcutils",
        "libutils",
        "libbinder_ndk",
    ],
    srcs: [
        "main.cpp",
    ],
}

五、编译运行

执行客户端测试程序后,log 信息如下:

打印信息可以看出新接口hello_init的日志成功打印


相关推荐
传奇开心果编程42 分钟前
【Jetpack Compose基础语法学与练】第8课 rememberSaveable,页面旋转/系统重建保留状态
android·学习·ui·kotlin·android jetpack
>Andre<1 小时前
UFS5.0标准中文全译·卷一:范围、术语与架构
android·linux·嵌入式硬件
事圆则缓2 小时前
Java 8 Lambda、Stream、Optional 与 Android 边界:从回调语法到运行时兼容
android·java
mmsx2 小时前
Android 地图十万要素不卡顿:空间网格 + 渐进式加载的移动端实践
android·大数据·opengl
Godikov4 小时前
Android 工业终端保活实战:前台服务 + 开机自启 + 更新自启的三重保障
android
字节暗面4 小时前
SO加固强度怎么量化?腾讯ACE与FairGuard静态分析实测
android·逆向
终端安全笔记5 小时前
描述文件、企业证书、MDM 不是三个名字,是三层
android·ios·智能手机
字节暗面6 小时前
一次游戏安全SO静态分析记录:腾讯ACE与FairGuard加固强度对比
android·逆向工程·游戏加固
传奇开心果编程6 小时前
【Jetpack Compose基础语法学与练】第9课 SideEffect副作用 + LaunchedEffect,处理异步任务和生命周期
android·学习·ui·kotlin·android jetpack
2401_833269308 小时前
RecyclerView多布局详解
android