Framework Binder 程序示例之 aidl-cpp 篇

154 阅读2分钟

Framework Binder 程序示例之 aidl-cpp 篇

google 提供了 aidl-cpp 工具来简化我们的开发工作,接下来我们就来看看如何使用 aidl-cpp 简化我们的开发工作:

1.在 evice/google/crosshatch 目录下创建如下的目录与文件

AIDLCppDemo
├── Android.bp
├── com
│   └── yuandaima
│       └── IHello.aidl
├── HelloClient.cpp
└── HelloServer.cpp

2.编写IHello.aidl文件

package com.yuandaima;

interface IHello
{
    void hello();
    int sum(int x, int y);
}

3.使用aidl-cpp命令生成必要的cpp代码(需要在android源码环境下执行)

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

4.生成如下代码

AIDLCppDemo
├── Android.bp
├── com
│   └── yuandaima
│       ├── BnHello.h
│       ├── BpHello.h
│       ├── IHello.aidl
│       ├── IHello.cpp
│       └── IHello.h
├── HelloClient.cpp
└── HelloServer.cpp

5.服务端代码实现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;
}

6.客户端实现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;
}

7.编写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",
    ],
}

8.编译运行

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

9.执行可执行文件

adb shell
cd /data/local/tmp
./IHelloServer &
./IHelloClient

10.监听log,显示结果

lueline:/ # logcat | grep "aidl_cpp"
05-01 08:16:05.033  8688  8689 I aidl_cpp: hello
05-01 08:16:05.033  8688  8689 I aidl_cpp: server: sum: 1 + 2

11.[源代码见](study_8_aidl_c_plus/AIDLCppDemo · 王俊/framework - 码云 - 开源中国)

[原文章见](Binder 程序示例之 aidl-cpp 篇 | Android Framework)