C++通过aidl与Android系统服务通信(一)

C++通过aidl与Android系统服务通信(一)前言:既然你能找到这篇文章,说明你对aidl是什么已经有所了解,这里不再赘述,需要声明的一个概念是 Android Binder。1,什么是Bi

大家好,欢迎来到IT知识分享网。

前言:

既然你能找到这篇文章,说明你对aidl是什么已经有所了解,这里不再赘述,需要声明的一个概念是 Android Binder

1,什么是Binder?

字面意思:“粘合剂”,作为Android中跨进程调用的一种RPC机制,是Android提供的跨进程通信的核心框架。 例如:系统服务ActivityManagerService,LocationManagerService,等都是在单独进程中的,使用binder和应用进行通信。

2,为什么使用Binder?

一个简单例子:client需要请求service服务,但client和service在两个不同的进程中,一个很直观的请求过程如下。

C++通过aidl与Android系统服务通信(一)

但是注意,一个进程是不能直接操作另一个进程的,比如说读取另一个进程的数据,或者往另一个进程的内存空间写数据,进程之间的通信要通过内核进程才可以,因此这里就要使用到进程通信工具Binder,如下图:

C++通过aidl与Android系统服务通信(一)

Binder driver通过/dev/binder提供了 open, release, poll, mmap, flush等操作的接口api。这样进程A和进程B就可以通过内核进程进行通信了。进程中大部分的通信都是通过系统调用接口ioctl(binderFd, BINDER_WRITE_READ, &bwd)来进行的。

看起来流程已经通了,但是通常情况下,client和service并不会直接实现binder相关协议,所以在client与binder之间添加了proxy,在service与binder之间添加stub分别与binder交互:

C++通过aidl与Android系统服务通信(一)

3,android中的binder

上图看起来已经很完善了,但在Android系统中,更进一步的进行了封装,在client与proxy之间添加了manager来管理client,如下图:

C++通过aidl与Android系统服务通信(一)

关于android中的manager,其实已经很熟悉了,例如activity是由activityManager管理的,clipboard是由clipboardManager管理的。clipbpardManager是通过binder获取clipboardService来处理clipboard相关的操作的。

更进一步需要知道的,同一个Binder怎么对应多个Service呢,在Android系统中,Binder和Service之间同样有一个SeriviceManager管理着众多的Service,因此我们的流程进一步更新如下:

C++通过aidl与Android系统服务通信(一)

以上就是Binder以及Binder在Android系统的中的作用的相关知识。

AIDL概述:

android aidl最终是通过C++实现的,所以我们可以使用C++通过aidl直接调用java层的系统服务。这里需要首先了解的一个工具“aidl-cpp”。

首先看下google官方对aidl-cpp的介绍:https://android.googlesource.com/platform/system/tools/aidl/+/brillo-m10-dev/docs/aidl-cpp.md (Generating C++ Binder Interfaces with aidl-cpp)。

是的,我们需要一个叫做 aidl-cpp 的程序,用来生成C++ Binder Interface。Linux系统安装aidl-cpp:

Java sudo apt-get install aidl

cpp 接口定义:

1)首先创建文件夹,需要和aidl文件名字匹配,此处以foo文件夹为例

mkdir foo cd foo vim IFoo.aidl

2)实现一个aidl文件,文件名定义为(IFoo.aidl)内容如下:

package foo; interface IFoo { }

3)使用aidl-cpp 工具生成代码

aidl-cpp IFoo.aidl . foo.cpp

命令共三个参数,第一个为aidl文件,第二个为头文件生成目录,最后一个是cpp文件。

运行后,如果不出意外,生成的目录结构如下:

├── foo │ ├── foo │ │ ├── BnFoo.h │ │ ├── BpFoo.h │ │ └── IFoo.h │ ├── foo.cpp │ └── IFoo.aidl

4) 依次打开文件查看内容

  • foo/IFoo.h
#ifndef AIDL_GENERATED_FOO_I_FOO_H_ #define AIDL_GENERATED_FOO_I_FOO_H_ #include <binder/IBinder.h> #include <binder/IInterface.h> #include <binder/Status.h> #include <utils/StrongPointer.h> namespace foo { class IFoo : public ::android::IInterface { public: DECLARE_META_INTERFACE(Foo) }; // class IFoo } // namespace foo #endif // AIDL_GENERATED_FOO_I_FOO_H_

生成了一个继承自android::IInterface的类文件

  • foo/BnFoo.h
#ifndef AIDL_GENERATED_FOO_BN_FOO_H_ #define AIDL_GENERATED_FOO_BN_FOO_H_ #include <binder/IInterface.h> #include <foo/IFoo.h> namespace foo { class BnFoo : public ::android::BnInterface<IFoo> { public: ::android::status_t onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags = 0) override; }; // class BnFoo } // namespace foo #endif // AIDL_GENERATED_FOO_BN_FOO_H_
  • foo/BpFoo.h
#ifndef AIDL_GENERATED_FOO_BP_FOO_H_ #define AIDL_GENERATED_FOO_BP_FOO_H_ #include <binder/IBinder.h> #include <binder/IInterface.h> #include <utils/Errors.h> #include <foo/IFoo.h> namespace foo { class BpFoo : public ::android::BpInterface<IFoo> { public: explicit BpFoo(const ::android::sp<::android::IBinder>& _aidl_impl); virtual ~BpFoo() = default; }; // class BpFoo } // namespace foo #endif // AIDL_GENERATED_FOO_BP_FOO_H_
  • foo.cpp
#include <foo/IFoo.h> #include <foo/BpFoo.h> namespace foo { IMPLEMENT_META_INTERFACE(Foo, "foo.IFoo") } // namespace foo #include <foo/BpFoo.h> #include <binder/Parcel.h> namespace foo { BpFoo::BpFoo(const ::android::sp<::android::IBinder>& _aidl_impl) : BpInterface<IFoo>(_aidl_impl){ } } // namespace foo #include <foo/BnFoo.h> #include <binder/Parcel.h> namespace foo { ::android::status_t BnFoo::onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags) { ::android::status_t _aidl_ret_status = ::android::OK; switch (_aidl_code) { default: { _aidl_ret_status = ::android::BBinder::onTransact(_aidl_code, _aidl_data, _aidl_reply, _aidl_flags); } break; } if (_aidl_ret_status == ::android::UNEXPECTED_NULL) { _aidl_ret_status = ::android::binder::Status::fromExceptionCode(::android::binder::Status::EX_NULL_POINTER).writeToParcel(_aidl_reply); } return _aidl_ret_status; } } // namespace foo

至此使用aidl-cpp 生成接口文件的流程已经完成,接下来需要丰富相关接口,并通过生成的cpp 函数,调用android服务中的相关接口。将在下半部分中详细介绍。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/47570.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信