Binder 程序示例之 aidl-cpp 篇

7/26/2023

Binder 程序示例之C++篇 (opens new window)中需要我们手写很多的模板代码,写起来还是有点繁琐的,google 提供了 aidl-cpp 工具来简化我们的开发工作,接下来我们就来看看如何使用 aidl-cpp 简化我们的开发工作:

本文源码可以在 https://github.com/yuandaimaahao/AIDLCppDemo 下载到。

# 定义协议文件

device/jelly/rice14/ 目录下创建如下的目录与文件:

AIDLCppDemo
├── Android.bp
├── com
│   └── yuandaima
│       └── IHello.aidl
├── HelloClient.cpp
└── HelloServer.cpp
1
2
3
4
5
6
7

其中 IHello.aidl 就是我们定义的协议文件:

package com.yuandaima;

interface IHello
{
    void hello();
    int sum(int x, int y);
}
1
2
3
4
5
6
7

定义了两个远程调用函数 hello 和 sum。

接着我们使用 aidl-cpp 命令生成必要的 cpp 代码:

aidl-cpp com/yuandaima/IHello.aidl ./ ./IHello.cpp
1

生成如下的代码:

AIDLCppDemo
├── Android.bp
├── com
│   └── yuandaima
│       ├── BnHello.h
│       ├── BpHello.h
│       ├── IHello.aidl
│       └── IHello.h
├── HelloClient.cpp
├── HelloServer.cpp
└── IHello.cpp
1
2
3
4
5
6
7
8
9
10
11

# 服务端实现

接着我们来实现我们的服务端 HelloServer.cpp:

#define LOG_TAG "aidl_cpp"


#include <binder/IInterface.h>#include <stdlib.h>
#include <utils/RefBase.h>
#include <utils/Log.h>
#include <binder/TextOutput.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>

#include "com/yuandaima/IHello.h"
#include "com/yuandaima/BnHello.h"

using namespace android;

class IHelloServer : public com::yuandaima::BnHello
{
public:
    binder::Status hello()
    {
        ALOGI("hello");
        return binder::Status();
    }

    binder::Status sum(int32_t v1, int32_t v2, int32_t *_aidl_return) override
    {
        ALOGI("server: sum: %d + %d", v1, v2);
        *_aidl_return = v1 + v2;
        return binder::Status();
    }
};

int main(int argc, char const *argv[])
{
    defaultServiceManager()->addService(String16("IHello"), new IHelloServer());
    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

这里实现了两个函数 hellosum,这两个函数等待着被远程调用。 main 函数中先添加服务,接着开启线程池。

# 客户端实现

接着我们来实现客户端代码 HelloClient.cpp:

#define LOG_TAG "aidl_cpp"

#include <stdlib.h>
#include <utils/RefBase.h>
#include <utils/Log.h>
#include <binder/TextOutput.h>
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>

#include "com/yuandaima/IHello.h"
#include "com/yuandaima/BnHello.h"

using namespace android;

int main(int argc, char const *argv[])
{
    sp<IServiceManager> sm = defaultServiceManager();
    sp<IBinder> binder = sm->getService(String16("IHello"));
    sp<com::yuandaima::IHello> hello = interface_cast<com::yuandaima::IHello>(binder);

    hello->hello();
    int ret = 0;
    hello->sum(1, 2, &ret);

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

客户端中,首先获得 BpServiceManager 代理对象,接着通过这个代理对象查找服务,获得 Hello 服务对应的 Binder 代理对象,接着通过这个代理对象发起远程调用

# 编译与运行

接着我们编写 Android.bp 文件:

cc_binary {
    name: "IHelloClient",
    srcs: ["HelloClient.cpp","IHello.cpp"],
    shared_libs: [
        "liblog",
        "libcutils",
        "libutils",
        "libbinder",
    ],
}


cc_binary {
    name: "IHelloServer",
    srcs: ["HelloServer.cpp","IHello.cpp"],
    shared_libs: [
        "liblog",
        "libcutils",
        "libutils",
        "libbinder",
    ],
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

最后编译运行:

# 启动模拟器
source build/envsetup.sh
lunch rice14-eng #选择合适的 product 版本
# clean 一下
make installclean
# 在 device/jelly/rice14/AIDLCppDemo 目录下
mm #模块编译
# push 可执行文件到设备上
adb push out/target/product/rice14/system/bin/IHellServer /data/local/tmp
adb push out/target/product/rice14/system/bin/IHellClient /data/local/tmp
1
2
3
4
5
6
7
8
9
10

接着执行可执行文件:

adb shell
cd /data/local/tmp
./IHelloServer &
./IHelloClient
1
2
3
4

接着查看 log:

logcat | grep "aidl_cpp"
1

至此我们的示例程序就完成了。

# 参考资料

# 关于

我叫阿豪,2015 年本科毕业于国防科学技术大学指挥信息系统专业,毕业后从事信息化装备的研发工作,主要研究方向是 Android Framework 与 Linux Kernel。

如果你对 Android Framework 感兴趣或者正在学习 Android Framework,可以关注我的微信公众号和抖音,我会持续分享我的学习经验,帮助正在学习的你少走一些弯路。学习过程中如果你有疑问或者你的经验想要分享给大家可以添加我的微信,我拉你进技术交流群。