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的日志成功打印


相关推荐
成都大菠萝17 分钟前
Android Car CarProperty 车辆信号链路
android
敲代码的鱼32 分钟前
PDF 预览与签名批注写回 支持安卓 iOS 鸿蒙 UTS插件
android·前端·ios
时光足迹2 小时前
uni-app 视频通话实战:康复师与患者视频问诊的 6 个致命 Bug 与解决方案
android·ios·uni-app
Coffeeee6 小时前
闲聊几句,Android老哥们,你们多久没做技改需求了
android·程序员·代码规范
萝卜er7 小时前
Fragment 生命周期与状态恢复-《Android深水区(四)》
android
萝卜er7 小时前
Intent 显式、隐式与 PendingIntent-《Android深水区(五)》
android
Kapaseker9 小时前
一文吃透 Kotlin 集合操作符
android·kotlin
三少爷的鞋11 小时前
Main-safe:现代Android 架构真正的分水岭
android
沐怡旸19 小时前
深入解析 Android Performance Analyzer (APA) 底层架构与技术原理
android
李斯维1 天前
从历史的角度看 Android 软件架构
android·架构·android jetpack