9.7 Binder System _c++ Implementation _ programming

Source: Internet
Author: User

Reference files:
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: (Reference: IMediaPlayerService.h)

Ifndef Android_ihelloervice_h

#define Android_ihelloervice_h

................ The header file ........ .....

Namespace android{

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 (reference: IMediaPlayerService.cpp)

#include Log_tag "HelloService"

#include "IHelloService.h"

#define HELLO_SVR_CMD_SAYHELLO     1 #define HELLO_SVR_CMD_SAYHELLO_TO  2

Namespace android{

status_t bnhelloservice::ontransact (uint32_t code,const parcel& data,parcel* reply,uint32_t flags)

{

Parse data, call sayhello/sayhello_to

Switch (code) {

Case hello_svr_cmd_sayhello:{

SayHello ();

return no_error;

}

Case hello_svr_cmd_sayhello_to:{

Extracting parameters from data

int32_t policy = Data->readint32 ();//read out four bytes of all 0 data

String16 Name16 =data->readstring16 ();

String8 Name8 (NAME16);

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

Write the return value to reply and pass it back.

Reply->writeint32 (CNT);

return no_error;

}breakl

Default

Return Bbinder::ontransact (code,data,reply,flags);

}

}

void Bnhelloservice::sayhello (void);

{

      staticintcnt = 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 (reference: IMediaPlayerService.cpp)

#include "IHelloService.h"

#define HELLO_SVR_CMD_SAYHELLO     1 #define HELLO_SVR_CMD_SAYHELLO_TO  2

Class Bphelloservice:public Bpinterface<ihelloservice>

{

Public

Bphelloservice (const sp<ibinder>& impl):bpinterface<ihelloservice> (Impl)

{

}

void SayHello (void)

{

Structuring/Sending data

Parcel data,reply;

Data.writeint32 (0);//data data fields can be defined by themselves, and this is to unify

Remote ()->transact (hello_svr_cmd_sayhello,data,&reply);

}

void sayhello_to (const char *name)

{

Structuring/Sending data

Parcel data,reply;

Data.writeint32 (0);//data data fields can be defined by themselves, and this is to unify

Data.writestring16 (STRING16 (name));

Remote ()->transact (hello_svr_cmd_sayhello_to,data,&reply);

Return Reply.readint32 (HelloService, "Android.media.IMediaPlayerService");

}

}

Implement_meta_interface ();

Test_server.cpp (reference: Main_mediaserver.cpp)

#define LOG_TAG "HelloService"

#include "IHelloService.h"

................. The header file ......... .......

using namespace Android;

int main (void)

{

Add service//while (1) {read data, parsing data, invoking service function}

Open Drive, Mmap

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

Get Bpservicemanager

sp<iservicemanager> sm = Defaultservicemanager ();

Sm->addservice (String16 ("Hello"), new Bnhelloservice ());

Loop body

Processstate::self ()->startthreadpool ();

Ipcthreadstate::self ()->jointhreadpool ();

return 0;

}

Test_client.cpp

#define LOG_TAG "Helloclient"

#include "IHelloService.h"

................. The header file ......... .......

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

Open Drive, Mmap

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

Get Bpservicemanager

sp<iservicemanager> sm = Defaultservicemanager ();

sp<ibinder> Binder = Sm->getservice (String16 ("Hello"));

if (binder = = 0)

{

Alogi ("Can ' t get Hello service\n");

return-1;

}

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

Calling the service function

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;

}

Test compilation:

Reference frameworks\av\media\mediaserver\android.mk

Compile:
A. File into Frameworks/testing/app_0004_binder_cpp_app
B. cd/work/android-5.0.2/
. Setenv
Lunch//Select 23. Full_tiny4412-eng
C. CD Frameworks/testing/app_0004_binder_cpp_app
Mmm.

Test:
A. Recompile the kernel and let IT support 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 and use the new zimage to start the veneer

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. Implementation of Test_server, test_client

./test_server &
Logcat helloservice:* *:s &
./test_client Hello
./test_client Hello Weidongshan

9.7 Binder System _c++ Implementation _ programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.