Ahao's Technical Blog Ahao's Technical Blog
首页
  • 001.基础篇
  • 002.玩转AOSP篇
  • 003.学穿Binder篇
  • 004.基础组件篇
  • 005.系统启动过程分析
  • 006.Hal开发入门与实践
  • 007.显示系统
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

阿豪讲Framework

不积跬步无以至千里
首页
  • 001.基础篇
  • 002.玩转AOSP篇
  • 003.学穿Binder篇
  • 004.基础组件篇
  • 005.系统启动过程分析
  • 006.Hal开发入门与实践
  • 007.显示系统
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 基础篇

  • 玩转AOSP篇

  • 学穿Binder篇

    • 000.Binder 专题导学 —— 如何深入掌握 Binder
    • 001.学习 Binder 的预备知识
    • 002.Binder 基本原理
    • 003.Binder 程序示例之 C 语言篇
    • 004.Binder 服务注册过程情景分析之 C 语言篇
    • 005.Binder 服务获取与使用过程情景分析之C语言篇
    • 006.Android Binder 驱动框架设计与分析
    • 007.Binder 驱动情景分析之 ServiceManager 启动过程
    • 008.Binder 驱动情景分析之服务注册过程
    • 009.Binder 驱动情景分析之服务获取与使用过程
    • 010.Binder 程序示例之 C++ 篇
    • 011.Binder C++ 程序分析之主要类解析
    • 012.Binder 服务注册过程情景分析之 C++ 篇
    • 013.Binder 服务获取与使用过程情景分析之C++篇
    • 014.Binder 程序示例之 aidl-cpp 篇
      • 定义协议文件
      • 服务端实现
      • 客户端实现
      • 编译与运行
      • 参考资料
    • 015.添加 Android Native 系统服务
    • 016.添加 Native 系统服务回调
    • 017.Binder 程序示例之 Java 篇
    • 018.Binder Java 层初始化
    • 019.Binder Java 层服务注册过程分析
    • 020.Binder Java 层服务获取与使用过程分析
    • 021.添加 Java 系统服务
    • 022.Android Java 系统服务框架与第三方 App 使用自定义 Java 系统服务
    • 023.添加 Java 系统服务回调
    • 024.AIDL 数据类型详解之 Java 篇
    • 025.AIDL 数据类型详解之 C++ 篇
    • 026.Java 调用 Native 服务
    • 027.Native 调用 Java Binder 服务
    • 028.AIDL 关键字 in out inout oneway 解析
    • 029.Binder 驱动 Debug 入门指南
    • 030.Binder 匿名服务源码分析
    • 031.Binder 中的 Parcel 数据结构分析(C++)
    • 032.Binder 中的 Parcel 数据结构分析(Java)
    • 033.Binder 多线程情景分析
    • 034.Binder 线程池溢出问题
    • 035.Binder 代理对象泄露问题分析
    • 036.Binder 死亡通知情景分析
    • 037.Binder 异常处理机制
    • 038.Binder 系统源码演进
    • 039.Binder 面试题汇总
    • 补充——LocalService
  • 基础组件篇

  • 系统启动过程分析

  • Hal开发入门与实践

  • 显示系统

  • Framework
  • 学穿Binder篇
阿豪
2023-07-26
目录

014.Binder 程序示例之 aidl-cpp 篇

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

这里实现了两个函数 hello 和 sum,这两个函数等待着被远程调用。 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

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

# 参考资料

  • Generating C++ Binder Interfaces with aidl-cpp (opens new window)
013.Binder 服务获取与使用过程情景分析之C++篇
015.添加 Android Native 系统服务

← 013.Binder 服务获取与使用过程情景分析之C++篇 015.添加 Android Native 系统服务→

最近更新
01
如何调试 SurfaceFlinger
10-05
02
SurfaceFlinger 概述
10-05
03
HWC 接口分析
10-05
更多文章>
Theme by Vdoing | Copyright © 2020-2025 AHao Framework | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式