参考文件:
frameworks\av\include\media\IMediaPlayerService.h (IMediaPlayerService,BnMediaPlayerService)
frameworks\av\media\libmedia\IMediaPlayerService.cpp (BpMediaPlayerService)
frameworks\av\media\libmediaplayerservice\MediaPlayerService.h
frameworks\av\media\libmediaplayerservice\MediaPlayerService.cpp
frameworks\av\media\mediaserver\Main_mediaserver.cpp (server, addService)

IHelloService.h:(参考:IMediaPlayerService.h)

ifndef ANDROID_IHELLOERVICE_H

#define ANDROID_IHELLOERVICE_H

................头文件..................

namespace android{

#define HELLO_SVR_CMD_SAYHELLO 1

#define HELLO_SVR_CMD_SAYHELLO_TO 2

class IHelloService:public IInterface

{

public:

  DECLARE_META_INTERFACE(HelloService);

  virtual void sayhello(void) = 0;

  virtual int sayhello_to(const char *name) = 0;

};

class BnHelloService:public BnInterface<IHelloService>

{

public:

  virtual status_t onTransact(uint32_t code,const Parcel& data,Parcel* reply,uint32_t flags = 0);

  virtual void sayhello(void);

  virtual int sayhello_to(const char *name);

}

}

#endfi

BnHelloService.cpp(参考:IMediaPlayerService.cpp)

#include LOG_TAG "HelloService"

#include "IHelloService.h"

namespace android{

  status_t BnHelloService::onTransact(uint32_t code,const Parcel& data,Parcel* reply,uint32_t flags)

  {

    //解析数据,调用sayhello/sayhello_to

    switch(code){

      case HELLO_SVR_CMD_SAYHELLO:{

        sayhello();

        return NO_ERROR;

      }

      case HELLO_SVR_CMD_SAYHELLO_TO:{

        //从data中取出参数

        int32_t policy = data->readInt32();//把四字节的全零数据读出来

        String16 name16 =data->readString16();

        String8 name8(name16);

        int cnt = sayhello_to(name8.string());

        //把返回值写入reply传回去

        reply->writeInt32(cnt);

        return NO_ERROR;

      }breakl

      default:

        return BBinder::onTransact(code,data,reply,flags);

    }

  }

  void BnHelloService::sayhello(void);

  {

      static int cnt = 0;

      ALOGI("say hello : %d\n", ++cnt);

  }

  int BnHelloService::sayhello_to(const char *name);

  {

      static int cnt = 0;
      ALOGI("say hello to %s : %d\n", name, ++cnt);
      return cnt;

  }

  

}

BpHelloService.cpp(参考:IMediaPlayerService.cpp)

#include "IHelloService.h"

namespace android{

class BpHelloService:public BpInterface<IHelloService>

{

public:

  BpHelloService(const sp<IBinder>& impl):BpInterface<IHelloService>(impl)

  {

  }

  void sayhello(void)

  {

  //构造/发送数据

  Parcel data,reply;

  data.writeInt32(0);//data数据域可以自己定义,这里是为了统一

  remote()->transact(HELLO_SVR_CMD_SAYHELLO,data,&reply);

  }

  void sayhello_to(const char *name)

  {

  //构造/发送数据

  Parcel data,reply;

  data.writeInt32(0);//data数据域可以自己定义,这里是为了统一

  data.writeString16(String16(name));

  remote()->transact(HELLO_SVR_CMD_SAYHELLO_TO,data,&reply);

  return reply.readInt32(HelloService,"android.media.IMediaPlayerService");

  }

}

IMPLEMENT_META_INTERFACE(HelloService,"android.meida.In");

}

test_server.cpp(参考:Main_mediaserver.cpp)

#define LOG_TAG “HelloService”

#include "IHelloService.h"

.................头文件.......................

using namespace android;

int main(void)

{

  //add service   //while(1){read data,解析数据,调用服务函数}

  //打开驱动,mmap

  sp<ProcessState> proc(ProcessState::self());

  //获得BpServiceManager

  sp<IServiceManager> sm = defaultServiceManager();

  sm->addService(String16("hello"),new BnHelloService());

  //循环体

  ProcessState::self()->startThreadPool();

  IPCThreadState::self()->joinThreadPool();

  return 0;

}

test_client.cpp

#define LOG_TAG “HelloService”

#include "IHelloService.h"

.................头文件.......................

using namespace android;

void main(int argc,char **argv)

{

    int cnt;
  if (argc < 2){
        ALOGI(stderr, "Usage:\n");
        ALOGI(stderr, "%s hello\n", argv[0]);
        ALOGI(stderr, "%s hello <name>\n", argv[0]);
        return -1;
    }
  //getService

  //打开驱动,mmap

  sp<ProcessState> proc(ProcessState::self());

  //获得BpServiceManager

  sp<IServiceManager> sm = defaultServiceManager();

  sp<IBinder> binder = sm->getService(String16("hello"));

  if(binder  == 0)

  {

    ALOGI("can't get hello service\n");

    return -1;

  }

  //service肯定是BpHelloService指针

  sp<IHelloService> service = interface_cast<IHelloService>(binder);

  //调用Service函数

  if(argc < 3){

    service->sayhello();

    ALOGI("client call sayhello");

  }

  else{

    cnt = service->sayhello_to(argv[2]);

    ALOGI("client call sayhello_to,cnt = %d ",cnt);

  }

  return 0;

}

测试编译:

参考frameworks\av\media\mediaserver\Android.mk

编译:
a. 文件放入frameworks/testing/APP_0004_Binder_CPP_App
b. cd /work/android-5.0.2/
. setenv
lunch //选择23. full_tiny4412-eng
c. cd frameworks/testing/APP_0004_Binder_CPP_App
mmm .

测试:
a. 重新编译内核让它支持NFS
make menuconfig
<*> NFS client support | |
[*] NFS client support for NFS version 3 | |
[*] NFS client support for the NFSv3 ACL protocol extension | |
[*] NFS client support for NFS version 4 | |
[*] NFS client support for NFSv4.1 (EXPERIMENTAL)

make zImage, 并使用新的zImage启动单板

b. mount nfs
su
ifconfig eth0 192.168.1.100
busybox mount -t nfs -o nolock,vers=2 192.168.1.123:/work/nfs_root /mnt

c. 执行 test_server, test_client

./test_server &
logcat HelloService:* *:S &
./test_client hello
./test_client hello weidongshan

9.7 Binder系统_c++实现_编写程序的更多相关文章

  1. 9.8 Binder系统_c++实现_内部机制1

    1. 内部机制_回顾binder框架关键点 binder进程通讯过程情景举例: test_server通过addservice向service_manager注册服务 test_client通过get ...

  2. 9.1 Binder系统_C程序示例_框架分析和编写程序

    IPC : Inter-Process Communication, 进程间通信 A进程把数据原原本本的发给B,这就是IPC RPC : Remote Procedure Call, 远程过程调用 A ...

  3. 9.13 Binder系统_Java实现_内部机制_Server端

    logcat TestServer:* TestClient:* HelloService:* *:S &CLASSPATH=/mnt/android_fs/TestServer.jar ap ...

  4. 9.12 Binder系统_Java实现_内部机制_Client端

    Java实现中client端的RPC层(java实现)如何通过JNI来调用IPC层(C++实现)发送数据 TestServer通过addService向Service_manager注册的时候Test ...

  5. 9.2 Binder系统_驱动情景分析_服务注册过程

    1. 几个重要结构体的引入给test_server添加一个goodbye服务, 由此引入以下概念: 进程间通信其实质也是需要三要素:源.目的.数据,源是自己,目的用handle表示:通讯的过程是源向实 ...

  6. 9.11 Binder系统_分层

    1.Binder系统过程分析,情景分析 server提供服务 (1)addService(服务名称,xxx)执行后会导致binder驱动在server的内核空间为服务创建一个binder_node结构 ...

  7. Android系统--Binder系统具体框架分析(一)

    Binder系统具体框架分析(一) 一.Binder系统核心框架 1. IPC:Inter-Process Communication, 进程间通信 A进程将数据原原本本发送B进程,主要负责进程间数据 ...

  8. C#读写文件的方法汇总_C#教程_脚本之家

    C#读写文件的方法汇总_C#教程_脚本之家 http://www.jb51.net/article/34936.htm

  9. Android系统--Binder系统具体框架分析(二)Binder驱动情景分析

    Android系统--Binder系统具体框架分析(二)Binder驱动情景分析 1. Binder驱动情景分析 1.1 进程间通信三要素 源 目的:handle表示"服务",即向 ...

随机推荐

  1. 使用PHP来压缩CSS文件

    这里将介绍使用PHP以一种简便的方式来压缩你的CSS文件.这种方法不需要命名你的.css文件和.php文件. 当前有许多方法需要将.css文件重命名成.php文件,然后在所有PHP文件中放置压缩代码. ...

  2. c++位运算符介绍

    下面是C/C++位操作运算符列表,其中运算符优先级为从上到下递减,但<<,>>优先级相同. C/C++位操作运算符 操作符 功能 用法 ~ 位求反 ~expr << ...

  3. Python 数据结构与算法 —— 哈弗曼树

    1. 从扩充二叉树到哈弗曼树 扩充二叉树:对二叉树 T,加入足够多的新叶节点(而不是任意),使 T 的原有结点都变成度数为 2 的分支节点,得到的二叉树称为 T 的扩充二叉树. 对于扩充二叉树而言, ...

  4. Linux系统存储交换机日志

    Linux系统存储交换机日志     日志记录是为系统设备在运行过程中报告其运行情况而设的, 为了保证系统正常运行, 解决每一天可能遇到的各种各样的问题, 网络管理员必须认真地读取日志记录.目前公司系 ...

  5. 【CS Round #39 (Div. 2 only) B】Circle Elimination

    [Link]:https://csacademy.com/contest/round-39/task/circle-elimination/ [Description] [Solution] 把n个点 ...

  6. [Anuglar & NgRx] StoreRouterConnectingModule

    Always treat Router as the source of truth When we use Ngrx, we can see that we will use a "Sto ...

  7. 细说 iOS 消息推送

    APNS的推送机制 与Android上我们自己实现的推送服务不一样,Apple对设备的控制很严格.消息推送的流程必需要经过APNs: 这里 Provider 是指某个应用的Developer,当然假设 ...

  8. 旧知识打造新技术--AJAX学习总结

    AJAX是将旧知识在新思想的容器内进行碰撞产生的新技术:推翻传统网页的设计技术,改善用户体验的技术. 学习AJAX之初写过一篇<与Ajax的初次谋面>.当中都仅仅是一些自己浅显的理解.这次 ...

  9. IAR FOR STM8 学习笔记 IAR工程的建立

    STM8是ST意法半导体针对工业应用和消费电子开发而推出的8位单片机. 每种MCU都有自身的优点与缺点,与其它8-bit MCU相比,STM8 8-bit MCU最大的特点是: · 内核: o 最高f ...

  10. POJ3622 Gourmet Grazers(FHQ Treap)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2363   Accepted: 881 Description Like s ...