Android Binder(C++版本例子)

Posted we1less

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Binder(C++版本例子)相关的知识,希望对你有一定的参考价值。

packages/apps/Godv/IGodvService.h

//
// Created by godv on 21-6-29.
//  这个是接口
// 参考  frameworks/av/include/media/IMediaPlayerService.h

#ifndef android_IGODVSERVICE_H
#define ANDROID_IGODVSERVICE_H

#include <utils/Errors.h>  // for status_t
#include <utils/KeyedVector.h>
#include <utils/RefBase.h>
#include <utils/String8.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>

#define HELLO_SVR_CMD_SAYHELLO 0
#define HELLO_SVR_CMD_SAYHELLO_TO 1

namespace android {
    class IGodvService: public IInterface
    {
    public:
        //声明一些必须的函数
        DECLARE_META_INTERFACE(GodvService);
        //纯虚函数
        virtual void sayhello(void) = 0;
        virtual int sayhello_to(const char *name) = 0;

    };

    //声明Bn
    //BnInterface 模板类
    //相当于 BnGodvService : IGodvService, BBinder
    class BnGodvService: public BnInterface<IGodvService>
    {
    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);
    };
};

#endif //ANDROID_IGODVSERVICE_H

packages/apps/Godv/BnGodvService.cpp

//
// Created by godv on 21-6-29.
// Bn Binder native 是服务端使用的 接收,解析,调用,回复类
// 参考  frameworks/av/media/libmedia/IMediaPlayerService.cpp
#define LOG_TAG "GodvService"

#include "IGodvService.h"

namespace android {

    //onTransact  用来解析数据 调用函数
    status_t BnGodvService::onTransact(
        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    {
        switch (code) {
            case HELLO_SVR_CMD_SAYHELLO: {
                sayhello();
                return NO_ERROR;
            } break;
            case HELLO_SVR_CMD_SAYHELLO_TO: {
                //从 data取出参数入参
                uint32_t params = data.readInt32();
                const String16 name16 = data.readString16();
                //String16 -> char *
                String8 name8(name16);
                int i = sayhello_to(name8.string());
                //并且把返回值写入reply回复回去
                reply->writeInt32(i);
                return NO_ERROR;
            } break;
            default:
                return BBinder::onTransact(code, data, reply, flags);
        }
    }

    void BnGodvService::sayhello(void)
    {
        static int cnt = 0;
        ALOGI("sayhello : %d\\n", cnt++);
    }

    int BnGodvService::sayhello_to(const char *name)
    {
        static int cnt = 9002;
        ALOGI("sayhello_to %s : %d\\n", name, cnt++);
        return cnt;
    }

};


packages/apps/Godv/BpGodvService.cpp

//
// Created by godv on 21-6-29.
// Bp Binder proxy 客户端要使用的 发送类
//参考  frameworks/av/media/libmedia/IMediaPlayerService.cpp

#include "IGodvService.h"

namespace android{

//相当于 BpGodvService : IGodvService, BpRefBase
class BpGodvService: public BpInterface<IGodvService>
{
public:
    explicit BpGodvService(const sp<IBinder>& impl)
        : BpInterface<IGodvService>(impl)
    {

    }

    void sayhello(void)
    {
        //构造 发送数据
        Parcel data, reply;
        data.writeInt32(0);
        remote()->transact(HELLO_SVR_CMD_SAYHELLO, data, &reply);
    }

    int sayhello_to(const char *name)
    {
        //构造 发送数据
        Parcel data, reply;
        data.writeInt32(0);
        //char * -> String16
        data.writeString16(String16(name));
        remote()->transact(HELLO_SVR_CMD_SAYHELLO_TO, data, &reply);
        return reply.readInt32();
    }
};

//实现必须的接口
IMPLEMENT_META_INTERFACE(GodvService, "android.media.IGodvService");

};

packages/apps/Godv/GodvService.cpp

//
// Created by godv on 21-6-29.
//参考  frameworks/av/media/mediaserver/main_mediaserver.cpp
#define LOG_TAG "GodvService"

#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include "IGodvService.h"

using namespace android;

int main(void)
{
    //add service
    //循环 {读数据  解析数据  调用服务函数}

    //打开驱动 mmap
    sp<ProcessState> proc(ProcessState::self());

    //获得BpServiceManager
    sp<IServiceManager> sm(defaultServiceManager());

    sm->addService(String16("godv"),new BnGodvService());
    //循环
    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();

    return 0;
}


packages/apps/Godv/GodvClient.cpp

//
// Created by godv on 21-6-29.
//
#define LOG_TAG "GodvService"

#include <binder/IBinder.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include "IGodvService.h"

using namespace android;

int main(int argc,char **argv)
{
    ALOGI("Usage:\\n");
    if(argc < 2){
        ALOGI("Usage:\\n");
        ALOGI("%s hello\\n",argv[0]);
        ALOGI("%s hello <name>\\n",argv[0]);
    }

    //getService
    //打开驱动 mmap
    sp<ProcessState> proc(ProcessState::self());

    //获得BpServiceManager
    sp<IServiceManager> sm(defaultServiceManager());
    sp<IBinder> binder =
        sm->getService(String16("godv"));

    if(binder == 0)
    {
        ALOGI("can not get godv service\\n");
        return -1;
    }

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

    int cnt;
    if(argc == 2){
        service->sayhello();
        ALOGI("client call sayhello\\n");
    } else if (argc == 3){
        cnt = service->sayhello_to(argv[2]);
        ALOGI("client call sayhello_to cnt = %d\\n", cnt);
    }
    return 0;
}

packages/apps/Godv/Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \\
        GodvService.cpp \\
        BnGodvService.cpp \\
        BpGodvService.cpp

LOCAL_SHARED_LIBRARIES := \\
        liblog \\
        libutils \\
        libbinder

LOCAL_MODULE:= godvserverc

include $(BUILD_EXECUTABLE)



include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \\
        GodvClient.cpp \\
        BpGodvService.cpp

LOCAL_SHARED_LIBRARIES := \\
        liblog \\
        libutils \\
        libbinder

LOCAL_MODULE:= godvclientc

include $(BUILD_EXECUTABLE)

generic_x86_64:/data/local # logcat GodvService:* *:S & 

以上是关于Android Binder(C++版本例子)的主要内容,如果未能解决你的问题,请参考以下文章

Android Binder(C语言版本例子)

Android C++语言 通过Binder通信调用activity: [android.app.IActivityManager] 服务发广播

Android Binder ServiceManager启动源码分析

android binder使用

使用 C++ 在 android 中理解和实现本机 Binder

一篇文章了解相见恨晚的 Android Binder 进程间通讯机制