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


相关推荐
黄大包2 小时前
android MQTT封装
android·mqtt·mt
2501_916007475 小时前
跨平台 App 安全,Flutter、RN、Unity、H5 混合应用加固
android·ios·小程序·https·uni-app·iphone·webview
hinewcc6 小时前
Linux电源管理 - wakelocks
android·linux
你怎么知道我是队长6 小时前
win11系统查看设备配置
android·java·javascript
DevangLic6 小时前
【确认是否安装了 C++ 工具】
android·java·c++
2501_916007476 小时前
不越狱如何查看iOS 应用的详细信息及其文件目录结构
android·macos·ios·小程序·uni-app·cocoa·iphone
龚礼鹏6 小时前
图像显示框架十——BufferQueue的工作流程(基于Android 15源码分析)
android
TheNextByte17 小时前
如何将音乐从Android手机传输到电脑 [4 种方法]
android·智能手机·电脑
一起养小猫7 小时前
Flutter for OpenHarmony 实战:贪吃蛇蛇的移动逻辑详解
android·flutter