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


相关推荐
plainGeekDev3 小时前
文件读写(Java IO)→ Kotlin 扩展函数
android·java·kotlin
s_nshine3 小时前
释放C盘,迁移studio相关数据到其他盘
android·windows·android studio·内存·c盘
韩曙亮4 小时前
【Flutter】Flutter 中的 Android / iOS 特殊配置 ① ( 网络权限配置 | HTTP 明文传输配置 | 应用名称配置 )
android·网络·flutter·http·ios·网络权限
_李小白4 小时前
【android opencv学习笔记】Day 31:提取轮廓之Canny算法
android·opencv·学习
hashiqimiya4 小时前
每日android布局xml文件
android·xml·gitee
m0_738120725 小时前
渗透测试基础——PHP 序列化数据结构与反序列化机制详解
android·服务器·网络·数据结构·安全·php
故渊at5 小时前
第二板块:Android 四大组件标准化学理 | 第十一篇:组件间通信(IPC)与 Binder 深度解析
android·binder·组件化·组件间通信
ZC跨境爬虫5 小时前
跟着 MDN 学JavaScript day_10:数组——数据的有序集合
android·java·开发语言·前端·javascript
消失的旧时光-19436 小时前
Kotlin 协程设计思想(九):Flow 到底是什么?为什么 suspend 函数还需要 Flow?
android·kotlin·协程·协程异常