Android Binder 框架层简介
本文主在简单介绍框架层的结构及使用,从Kernel 源码看从V3.19之后已经支持binder驱动,默认不编译,如需使用需要配置编译选项:make menuconfig
驱动源码:kernel/msm-4.9/drivers/android(高通源码目录)
框架源码:frameworks/native/libs/binder
Kernel源码在线:https://elixir.bootlin.com
Device Drivers->Android->Android Binder IPC Driver
#Android Binder IPC Driver 都勾选上
框架代码介绍如下(只做简单介绍几个重要点,细节较多,不详细介绍):
一、RefBase android非常重要的基础库
#源码路径:
system\core\libutils\include\utils\RefBase.h
system\core\libutils\RefBase.cpp
RefBase 是Android内部的智能指针,采用引用计数实现对象的生命周期管理。CPP继承RefBase即可使用智能指针。
RefBase两种使用方式:
强引用计数:sp
弱引用计数:wp
RefBase生命周期控制有两种:STRONG模式(OBJECT_LIFETIME_STRONG)、WEAK 模式(OBJECT_LIFETIME_WEAK),默认使用强引用模式,可通过void extendObjectLifetime(int32_t mode);接口切换模式
STRONG模式:强引用归零时立即销毁对象
WEAK 模式:需强、弱引用计数均归零才销毁对象
二、IInterface.h/IInterface.cpp
class IInterface : public virtual RefBase
{
public:
IInterface();
static sp<IBinder> asBinder(const IInterface*);
static sp<IBinder> asBinder(const sp<IInterface>&);
protected:
virtual ~IInterface();
virtual IBinder* onAsBinder() = 0;
}
源码中看到IInterface只是定义简单的几个接口,两个模板类BpInterface及BnInterface,其中BnInterface在服务端实现,BpInterface在客户端实现。定义了DECLARE_META_INTERFACE、IMPLEMENT_META_INTERFACE两个预处理,DECLARE_META_INTERFACE是接口的定义,在继承IInterface类的声明中替换,DECLARE_META_INTERFACE是方法的实现。如IShellCallback示例:
//IShellCallback.h
class IShellCallback : public IInterface{
public:
DECLARE_META_INTERFACE(ShellCallback);//替换为定义的接口
virtual int openFile(const String16& path, const String16& seLinuxContext,
const String16& mode) = 0;
enum {
OP_OPEN_OUTPUT_FILE = IBinder::FIRST_CALL_TRANSACTION
};
};
class BnShellCallback : public BnInterface<IShellCallback>{
public:
virtual status_t onTransact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0);
};
//IShellCallback.cpp
class BpShellCallback : public BpInterface<IShellCallback>{
public:
explicit BpShellCallback(const sp<IBinder>& impl): BpInterface<IShellCallback>(impl){}
virtual int openFile(const String16& path, const String16& seLinuxContext,
const String16& mode) {
Parcel data, reply;
data.writeInterfaceToken(IShellCallback::getInterfaceDescriptor());
data.writeString16(path);
data.writeString16(seLinuxContext);
data.writeString16(mode);
remote()->transact(OP_OPEN_OUTPUT_FILE, data, &reply, 0);
reply.readExceptionCode();
int fd = reply.readParcelFileDescriptor();
return fd >= 0 ? fcntl(fd, F_DUPFD_CLOEXEC, 0) : fd;
}
}
IMPLEMENT_META_INTERFACE(ShellCallback, "com.android.internal.os.IShellCallback");//替换为接口的实现
status_t BnShellCallback::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags){
....
}
三、IBinder.h
//frameworks\native\libs\binder\include\binder\IBinder.h
class [[clang::lto_visibility_public]] IBinder : public virtual RefBase{
enum {
FIRST_CALL_TRANSACTION = 0x00000001,//第一个方法的序列号,比较重要
LAST_CALL_TRANSACTION = 0x00ffffff,
...
}
//接口定义
IBinder();
virtual sp<IInterface> queryLocalInterface(const String16& descriptor);
virtual const String16& getInterfaceDescriptor() const = 0;
virtual bool isBinderAlive() const = 0;
....
}
从源码看只是一些接口定义
四、Binder.h/Binder.cpp
定义BBinder、BpRefBase相关接口。BBinder是服务端(Server)实现的核心基类,负责处理客户端发起的跨进程请求,作为服务端 Binder 对象的基类,通过重写BBinder的 **onTransact()** 方法解析并执行客户端请求。该方法接收三个参数:
-
code:标识客户端调用的方法编号 -
data:包含调用参数的Parcel对象 -
reply:用于返回结果的Parcel对象继承关系:BnxxxService->BnInterface->BBbinder->IBinder->RefBase
BpInterface继承BpRefBase作为客户端在interface_cast后将IBinder转为远程对象(IxxxService)
继承关系:BpxxxService->BpInterface->BpRefBase->RefBase
五、BpBinder.h/BpBinder.cpp
BpBinder 是Binder通讯中非常重要环节,主要负责与驱动层通讯(ioctl),IBinder持有此对象,通过ProcessState中的getStrongProxyForHandle、getWeakProxyForHandle初始化,在通讯中通过IPCThreadState的transact方法将消息发送到kernel层。同一服务的多个代理可能共享相同 BpBinder 实例,具体逻辑在框架内部实现,在使用Binder通讯过程中不涉及这块。
六、ProcessState.h/ProcessState.cpp
ProcessState负责分配内存,大小为BINDER_VM_SIZE(需要为内存页的整数倍,内存的page概念:PAGE_SIZE=4K=4096Byte,可通过getconf PAGESIZE查看),也可通过修改此参数调整Binder内存,内存起始地址为mVMStart,句柄mDriverFD。每个进程仅有一个实例,维护 Binder 线程池(IPCThreadState),负责全局资源管理。
另外在ProcessState中可将binder 设置为ServiceManager,负责其他xxxManagerService的管理工作,ServiceManager只能有一个。
七、IPCThreadState.h/IPCThreadState.cpp
IPCThreadState为Binder通讯线程,默认有15+1个线程(一个主线程+15个子线程BINDER_SET_MAX_THREADS)。每个IPCThreadState都持有ProcessState的对象mProcess,通过mDriverFD与kernel 通讯。通讯命令有下发(kCommandStrings)及回复(kReturnStrings),具体的逻辑可参考:https://blog.youkuaiyun.com/zhendeC1/article/details/137254671介绍
1708

被折叠的 条评论
为什么被折叠?



