当前位置: 首页 > news >正文

Android 12 S Binder原理之BpBinder,BnBinder以及IInterface介绍

系列文章

Android 12 S ServiceManager原理

Android 12 S Native Service的创建流程

Android 12 S Binder原理之BpBinder,BnBinder以及IInterface介绍

Android 12 S HIDL Service创建流程

Android 12 S 自定义Hal服务selinux权限添加

Android 12 S 自定义Native服务selinux权限添加

Android 12 S java服务调用native服务

Android 12 S 自定义native服务访问java服务


一:Binder机制介绍

Binder机制是Android系统提供的跨进程通讯机制。Client进程希望可以与Server进程沟通,但由于是跨进程,所以需要借助Binder驱动进程通信。

Binder由以下模块组成:

* Binder驱动

* Binder Server端 - Server进程

* Binder Client端 - Client进程

* ServiceManager - 管理者

Binder的通信模型如下所示:

二:创建Service模型

如果要创建一个Service,需要Server端继承BnInterface, Client端继承BpInterface。具体可用下图表示:

可在如下文件中找到关于Bn, Bp相关的声明

frameworks/native/libs/binder/include/binder/IInterface.h

frameworks/native/libs/binder/include/binder/IBinder.h

具体代码如下

frameworks/native/libs/binder/include/binder/IInterface.h 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; }; ----------------------------------------------------------- class BnInterface : public INTERFACE, public BBinder { public: virtual sp<IInterface> queryLocalInterface(const String16& _descriptor); virtual const String16& getInterfaceDescriptor() const; protected: typedef INTERFACE BaseInterface; virtual IBinder* onAsBinder(); }; frameworks/native/libs/binder/include/binder/Binder.h class BBinder : public IBinder { public: BBinder(); virtual const String16& getInterfaceDescriptor() const; virtual bool isBinderAlive() const; virtual status_t pingBinder(); ... }; frameworks/native/libs/binder/include/binder/IBinder.h class [[clang::lto_visibility_public]] IBinder : public virtual RefBase { public: enum { FIRST_CALL_TRANSACTION = 0x00000001, LAST_CALL_TRANSACTION = 0x00ffffff, ... }; ------------------------------------------------------------------- class BpInterface : public INTERFACE, public BpRefBase { public: explicit BpInterface(const sp<IBinder>& remote); protected: typedef INTERFACE BaseInterface; virtual IBinder* onAsBinder(); }; frameworks/native/libs/binder/include/binder/Binder.h class BpRefBase : public virtual RefBase { protected: explicit BpRefBase(const sp<IBinder>& o); virtual ~BpRefBase(); virtual void onFirstRef(); virtual void onLastStrongRef(const void* id); virtual bool onIncStrongAttempted(uint32_t flags, const void* id); inline IBinder* remote() const { return mRemote; }//返回的是BpBinder inline sp<IBinder> remoteStrong() const { return sp<IBinder>::fromExisting(mRemote); ...

三:进程间传递数据的载体 - Parcel

Parcel代码位于如下目录,Parcel用于传递进程间的数据。不管是Client发送数据给Server,还是Server发送数据给Client,都是通过Parcel进行传递。

frameworks/native/include/binder/Parcel.h

frameworks/native/include/binder/Parcel.cpp

write相关接口:

status_t write(const void* data, size_t len); status_t writeInt32(int32_t val); status_t writeUint32(uint32_t val); status_t writeInt64(int64_t val); status_t writeUint64(uint64_t val); status_t writeFloat(float val); status_t writeDouble(double val); status_t writeStrongBinder(const sp<IBinder>& val); status_t writeInt32Array(size_t len, const int32_t *val); status_t writeByteArray(size_t len, const uint8_t *val); status_t writeBool(bool val); status_t writeChar(char16_t val); status_t writeByte(int8_t val); ......

read相关接口:

status_t read(void* outData, size_t len) const; const void* readInplace(size_t len) const; int32_t readInt32() const; status_t readInt32(int32_t *pArg) const; uint32_t readUint32() const; status_t readUint32(uint32_t *pArg) const; int64_t readInt64() const; status_t readInt64(int64_t *pArg) const; uint64_t readUint64() const; status_t readUint64(uint64_t *pArg) const; float readFloat() const; status_t readFloat(float *pArg) const; double readDouble() const; status_t readDouble(double *pArg) const; bool readBool() const; status_t readBool(bool *pArg) const; char16_t readChar() const; status_t readChar(char16_t *pArg) const; int8_t readByte() const; status_t readByte(int8_t *pArg) const; ......

举个例子,在native service中是如何通过Parcel传递数据的,如下所示,都是通过Parcel进行传递。

class BpCustomizeManagerService : public BpInterface<ICustomizeManagerService> { public: explicit BpCustomizeManagerService (const sp<IBinder>& impl) : BpInterface <ICustomizeManagerService>(impl) { ALOGD("create Service \n"); } int customeize(int size){ Parcel data, reply; data.writeInterfaceToken(ICustomizeManagerService::getInterfaceDescriptor()); data.writeInt32(size); remote()->transact(CUSTOMIZE, data, &reply); return reply.readInt32(); } }; status_t BnCustomizeManagerService ::onTransact(uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags){ switch (code) { case CUSTOMIZE: { CHECK_INTERFACE(ICustomizeManagerService, data, reply); int size = data.readInt32(); int ret = customeize(size); //函数返回值从Server传递给Client reply->writeInt32(); return NO_ERROR; } ... };
http://www.jsqmd.com/news/1404949/

相关文章:

  • 防火墙技术全解析:从基础到应用场景
  • 告别Postman!IDEA内一站式API调试神器 Cool Request 完全指南
  • 抖音批量下载老失败?从限流翻车到稳定采集,这份完整指南一次讲透
  • 2026AI论文工具客观排行榜|无恰饭实测!适配毕业双检新规
  • AI搜索优化帮印刷厂获客,比传统推广便宜太多 - 红枫叶GEO优化公司
  • 如何零基础接入直播弹幕抓取?BarrageGrab 直连方案上手完整指南
  • 从零开始手把手配置 Raw Accel:把 Windows 鼠标加速从玄学变成科学
  • VSCode集成DeepSeek AI:本地代理服务部署与智能编程实践
  • IDEA插件实现Spring Boot接口自动同步YApi:原理、配置与避坑指南
  • 被苹果放弃的旧Mac,如何靠OpenCore Legacy Patcher重跑新版macOS
  • 宁安AI超级员工系统:一站式全链路企业智能运营平台
  • 基于SpringBoot的警务数据交互平台设计与实现源码+文档+讲解视频
  • 大语言模型推理优化:从KV缓存到vLLM部署的工程实践
  • 嵌入式开发中I2C总线初始化与调用:从硬件电路到软件驱动的完整实践指南
  • 开放不入耳还能听环境音?热门十款骨传导耳机实测漏音续航怎么样
  • AI搜索优化让餐饮老板的店被食客在AI里推荐 - 红枫叶GEO优化公司
  • OpenCore Legacy Patcher完整使用指南:让旧款Mac彻底重生的终极方案
  • 从焦虑乱查资料到坦然接纳 一名口腔扁平苔藓患者的五年心路历程
  • OpenCore Legacy Patcher完全指南:让十年前的旧Mac跑起新版macOS
  • GPT-5.6 三模之争:Sol、Terra、Luna 加六档推理,到底该怎么选
  • 2026年口碑好的学历提升机构实力参考 - 最新政策解读
  • 开发一个 Agent,还需要哪些东西?
  • Visual Assist X版本选择与开发环境管理:从工具依赖到工程实践
  • 被命令行劝退过?TegraRcmGUI 图形化一键注入让你 3 分钟上手 Switch 注入
  • AI搜索优化让建材批发商不再被同行抢走客户 - 红枫叶GEO优化公司
  • 天津鑫源广告加工厂的门店卡布灯箱报价怎么看更划算
  • RAG技术实战:从零搭建基于LangChain与向量数据库的智能问答系统
  • Git核心操作与高效工作流实战指南
  • 大语言模型安全漏洞:本地测试与防御实践指南
  • 2026年新型数控大圆机制造商:智能针织设备与高效织造方案 - 卓企推荐