• 9.7 Binder系统_c++实现_编写程序


    参考文件:
    frameworksavincludemediaIMediaPlayerService.h (IMediaPlayerService,BnMediaPlayerService)
    frameworksavmedialibmediaIMediaPlayerService.cpp (BpMediaPlayerService)
    frameworksavmedialibmediaplayerserviceMediaPlayerService.h
    frameworksavmedialibmediaplayerserviceMediaPlayerService.cpp
    frameworksavmediamediaserverMain_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 ", ++cnt);

      }

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

      {

          static int cnt = 0;
          ALOGI("say hello to %s : %d ", 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: ");
            ALOGI(stderr, "%s hello ", argv[0]);
            ALOGI(stderr, "%s hello <name> ", 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 ");

        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;

    }

    测试编译:

    参考frameworksavmediamediaserverAndroid.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

  • 相关阅读:
    反射、枚举
    WebService、Http请求、Socket请求
    RPC和REST的区别
    命名分组
    golang isPowerOfTwo判断是否是2的幂
    golang 判断平台是32位还是64位
    vue的permission.js详解
    windows 下完全卸载oracle 11的详细过程
    freemarker导出word
    freemarker详细教程从入门到精通(三)模板入门与指令
  • 原文地址:https://www.cnblogs.com/liusiluandzhangkun/p/9152745.html
Copyright © 2020-2023  润新知