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