一.server层的代码
1.1 server_main.cpp
-
#define LOG_TAG "MYSERVER"
-
-
#include <fcntl.h>
-
#include <sys/prctl.h>
-
#include <sys/wait.h>
-
#include <binder/IPCThreadState.h>
-
#include <binder/ProcessState.h>
-
#include <binder/IServiceManager.h>
-
#include <cutils/properties.h>
-
#include <utils/Log.h>
-
-
#include "DataService.h"
-
-
using namespace android;
-
-
int main(int argc, char** argv)
-
{
-
sp<ProcessState> proc(ProcessState::self());
-
sp<IServiceManager> sm = defaultServiceManager();
-
SLOGD("ServiceManager: %p", sm.get());
-
DataService::instantiate();
-
ProcessState::self()->startThreadPool();
-
IPCThreadState::self()->joinThreadPool();
-
return 0;
-
}
1.2
1.2.1
-
cong@msi:/work/frameworks/myserver/server$ cat IDataService.h
-
#ifndef ANDROID_IMOUSE_INJECT_H_FOR_APP
-
#define ANDROID_IMOUSE_INJECT_H_FOR_APP
-
-
#include <utils/Errors.h>
-
#include <utils/KeyedVector.h>
-
#include <utils/RefBase.h>
-
#include <utils/String8.h>
-
#include <binder/IInterface.h>
-
#include <binder/Parcel.h>
-
-
namespace android {
-
class IDataService: public IInterface
-
{
-
public:
-
enum{
-
SET_MOUSE_POS = IBinder::FIRST_CALL_TRANSACTION,
-
READ__DATA,
-
WRITE__DATA,
-
AT_CREAT_NETWORK,
-
AT_SEND,
-
AT_RECV,
-
ADD_LISTENER,
-
REMOVE_LISTENER,
-
};
-
DECLARE_META_INTERFACE(DataService);
-
};
-
-
class BnDataService: public BnInterface<IDataService>
-
{
-
public:
-
virtual status_t onTransact( uint32_t code,
-
const Parcel& data,
-
Parcel* reply,
-
uint32_t flags = 0);
-
virtual int32_t setSpritePosition(int x, int y) = 0;
-
virtual int32_t readData(char* buf)=0;
-
virtual int32_t writeData(char* buf, int c)=0;
-
};
-
-
};
-
-
#endif
1.2.2
-
cong@msi:/work/frameworks/myserver/server$ cat IDataService.cpp
-
#define LOG_TAG "MYSERVER"
-
#include <stdint.h>
-
#include <sys/types.h>
-
-
#include <binder/Parcel.h>
-
#include <binder/IMemory.h>
-
-
#include <utils/Errors.h>
-
#include <utils/String8.h>
-
-
#include "IDataService.h"
-
#include "DataService.h"
-
namespace android {
-
-
class BpDataService: public BpInterface<IDataService>
-
{
-
-
public:
-
BpDataService(const sp<IBinder>& impl)
-
: BpInterface<IDataService>(impl)
-
{}
-
virtual int32_t setSpritePosition(int x, int y)
-
{
-
Parcel data, reply;
-
SLOGD("IDataService.cpp L26: SET_MOUSE_POS");
-
data.writeInterfaceToken(IDataService::getInterfaceDescriptor());
-
status_t status = remote()->transact(SET_MOUSE_POS,data,&reply);
-
if(status == NO_ERROR){
-
return reply.readInt32();
-
}
-
return status;
-
}
-
virtual int32_t readData(char* buf)
-
{
-
Parcel data, reply;
-
data.writeInterfaceToken(IDataService::getInterfaceDescriptor());
-
status_t status = remote()->transact(READ__DATA,data,&reply);
-
if(status == NO_ERROR){
-
return reply.readInt32();
-
}
-
return status;
-
}
-
virtual int32_t writeData(char* buf, int cnt)
-
{
-
Parcel data, reply;
-
data.writeInterfaceToken(IDataService::getInterfaceDescriptor());
-
status_t status = remote()->transact(WRITE__DATA,data,&reply);
-
if(status == NO_ERROR){
-
return reply.readInt32();
-
}
-
return status;
-
}
-
-
};
-
-
IMPLEMENT_META_INTERFACE(DataService, "test.myserver.IDataService");
-
-
status_t BnDataService::onTransact(
-
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
-
{
-
int32_t i;
-
char buf[1024];
-
memset(buf, 0, sizeof(buf));
-
switch (code) {
-
case SET_MOUSE_POS: { //
-
SLOGD("IDataService.cpp L124: SET_MOUSE_POS");
-
CHECK_INTERFACE(IDataService, data, reply);
-
int x = data.readInt32();
-
int y = data.readInt32();
-
reply->writeNoException();
-
reply->writeInt32(setSpritePosition(x, y));
-
return NO_ERROR;
-
} break;
-
case READ__DATA: { //
-
CHECK_INTERFACE(IDataService, data, reply);
-
int cnt = readData(buf);
-
#if 0
-
for(int i=0; i<cnt; i++)
-
SLOGD("%d=0x%x", i, buf[i]);
-
#endif
-
reply->writeNoException();
-
//writeByteArray
-
reply->writeInt32(cnt);
-
reply->write(buf, cnt);
-
return NO_ERROR;
-
} break;
-
case WRITE__DATA: { //
-
CHECK_INTERFACE(IDataService, data, reply);
-
int bufferSize = data.readInt32();
-
if (0 < bufferSize) {
-
data.read(buf, bufferSize);
-
}
-
reply->writeNoException();
-
reply->writeInt32(writeData(buf, bufferSize));
-
return NO_ERROR;
-
} break;
-
default:
-
return BBinder::onTransact(code, data, reply, flags);
-
}
-
}
-
-
};
1.3
1.3.1
-
cong@msi:/work/frameworks/myserver/server$ cat DataService.h
-
#ifndef ANDROID_MOUSE_INJECT_SERVICE_H_FOR_APP
-
#define ANDROID_MOUSE_INJECT_SERVICE_H_FOR_APP
-
-
#include <arpa/inet.h>
-
-
#include <utils/threads.h>
-
#include <utils/List.h>
-
#include <utils/Errors.h>
-
#include <utils/KeyedVector.h>
-
#include <utils/String8.h>
-
#include <utils/Vector.h>
-
-
#include <binder/Parcel.h>
-
#include <binder/IPCThreadState.h>
-
#include <binder/IServiceManager.h>
-
//#include <binder/AppOpsManager.h>
-
#include <binder/BinderService.h>
-
//#include <binder/IAppOpsCallback.h>
-
#include "IDataService.h"
-
-
namespace android {
-
-
class DataService :
-
public BinderService<DataService>,
-
public BnDataService
-
{
-
friend class BinderService<DataService>;
-
public:
-
DataService();
-
virtual ~DataService();
-
-
static void instantiate();
-
-
virtual int32_t setSpritePosition(int x, int y);
-
virtual int32_t readData(char* buft);
-
virtual int32_t writeData(char* buf, int cnt);
-
-
virtual status_t onTransact(uint32_t code,
-
const Parcel &data,
-
Parcel *reply,
-
uint32_t flags);
-
};
-
-
static sp<DataService> mDataService;
-
-
};
-
-
#endif
1.3.2
-
cong@msi:/work/frameworks/myserver/server$ cat DataService.cpp
-
#define LOG_TAG "MYSERVER"
-
-
#include <stdio.h>
-
#include <fcntl.h>
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <sys/time.h>
-
#include <dirent.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <cutils/atomic.h>
-
#include <cutils/properties.h>
-
#include <utils/misc.h>
-
#include <binder/IPCThreadState.h>
-
#include <binder/IServiceManager.h>
-
#include <binder/MemoryHeapBase.h>
-
#include <binder/MemoryBase.h>
-
#include <binder/Parcel.h>
-
#include <utils/Errors.h>
-
#include <utils/String8.h>
-
#include <utils/SystemClock.h>
-
#include <utils/Vector.h>
-
#include <sys/ioctl.h>
-
#include <linux/ioctl.h>
-
#include "DataService.h"
-
-
namespace android {
-
status_t DataService::onTransact(uint32_t code,
-
const Parcel &data,
-
Parcel *reply,
-
uint32_t flags)
-
{
-
return BnDataService::onTransact(code, data, reply, flags);
-
}
-
-
void DataService::instantiate() {
-
SLOGD("DataService instantiate...");
-
sp<DataService> iReadygokeyService = new DataService();
-
sp<IServiceManager> sm = defaultServiceManager();
-
status_t status = sm->addService(String16("iReadygo.RWService"), iReadygokeyService);
-
if(status != 0){
-
SLOGD("can't add InputMapper Service, status=%d, %s",status,strerror(errno));
-
}
-
mDataService = iReadygokeyService;
-
}
-
-
static void recv_callback(const char *s, const char *sms_pdu);
-
-
DataService::DataService()
-
{
-
SLOGD("DataService construct...");
-
}
-
-
DataService::~DataService()
-
{
-
}
-
-
int DataService::setSpritePosition(int x, int y)
-
{
-
SLOGD("server:DataService.cpp L111: final setSpritePosition\n");
-
return 0;
-
}
-
-
int DataService::readData(char* buf)
-
{
-
int i;
-
int len = 10;
-
SLOGD("server: read");
-
for(i=0; i<10; i++)
-
{
-
buf[i] = i;
-
}
-
return len;
-
}
-
-
int DataService::writeData(char* buf, int cnt)
-
{
-
int i;
-
SLOGD("server: write");
-
for(i=0; i<cnt; i++)
-
{
-
SLOGD("server: %d=0x%x ", i, buf[i]);
-
}
-
return 0;
-
}
-
-
}
1.4 Android.mk
-
LOCAL_PATH:= $(call my-dir)
-
-
include $(CLEAR_VARS)
-
-
LOCAL_SRC_FILES:= \
-
server_main.cpp \
-
IDataService.cpp \
-
DataService.cpp
-
-
LOCAL_SHARED_LIBRARIES := \
-
libutils \
-
libcutils \
-
liblog \
-
libhardware_legacy \
-
libbinder
-
-
LOCAL_MODULE_TAGS := optional
-
-
LOCAL_MODULE:= my_server
-
-
include $(BUILD_EXECUTABLE)
二. core层的代码
2.1
-
cong@msi:/work/frameworks/myserver/core$ cat java/com/test/myserver/DataService.java
-
package test.myserver.dataservice;
-
-
import android.content.Context;
-
import test.myserver.IDataService;
-
import android.os.IBinder;
-
import android.os.Handler;
-
import android.os.Looper;
-
import android.os.RemoteException;
-
import android.os.ServiceManager;
-
import android.util.Log;
-
-
public class DataService {
-
public static final String TAG = "MYSERVER";
-
//private static final String IREADYGO_KEY_SERVICE_BINDER_NAME = "iReadygo.DataService";
-
private static final String MYSERVICE_BINDER_NAME = "iReadygo.RWService";
-
private final IDataService mDataService;
-
private final Context mContext;
-
-
public DataService(Context context)
-
{
-
mContext = context;
-
IBinder DataServiceBinder = ServiceManager.getService(MYSERVICE_BINDER_NAME);
-
mDataService = IDataService.Stub.asInterface(DataServiceBinder);
-
}
-
-
public int setSpritePosition(int x, int y)
-
{
-
int ret = -1;
-
Log.d(TAG, "in func setSpritePosition()");
-
if( null ){
-
try{
-
ret = mDataService.setSpritePosition(x, y);
-
} catch (RemoteException e){
-
Log.e(TAG, Log.getStackTraceString(new Throwable()));
-
}
-
}
-
return ret;
-
}
-
-
public byte[] readData( )
-
{
-
byte[] buf = null;
-
int ret = -1;
-
int i=0;
-
Log.d(TAG, "core: in func readData()");
-
if( null ){
-
try{
-
buf = mDataService.readData();
-
for(i=0; i<buf.length; i++)
-
Log.d(TAG, "core: readData "+i+" = "+ buf[i]);
-
} catch (RemoteException e){
-
Log.e(TAG, Log.getStackTraceString(new Throwable()));
-
return null;
-
}
-
}
-
return buf;
-
}
-
public int writeData(byte[] buf)
-
{
-
int ret = -1;
-
Log.d(TAG, "core: in func writeData()");
-
if( null ){
-
try{
-
ret = mDataService.writeData(buf);
-
} catch (RemoteException e){
-
Log.e(TAG, Log.getStackTraceString(new Throwable()));
-
}
-
}
-
return ret;
-
}
-
-
-
}
2.2
-
cong@msi:/work/frameworks/myserver/core$ cat java/com/test/myserver/IDataService.aidl
-
package test.myserver;
-
-
-
/** @hide */
-
interface IDataService
-
{
-
int setSpritePosition(int x, int y);
-
byte[] readData();
-
int writeData(in byte[] data);
-
}
2.3
-
cong@msi:/work/frameworks/myserver/core$ cat com.test.myserver.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<permissions>
-
<library name="com.test.myserver.xml"
-
file="/system/framework/com.test.myserver.jar"/>
-
</permissions>
2.4
-
cong@msi:/work/frameworks/myserver/core$ cat Android.mk
-
LOCAL_PATH := $(call my-dir)
-
-
include $(CLEAR_VARS)
-
-
LOCAL_SRC_FILES := \
-
$(call all-subdir-java-files) \
-
java/com/test/myserver/IDataService.aidl
-
-
LOCAL_AIDL_INCLUDES += frameworks/myserver/core/java/com
-
-
LOCAL_MODULE_TAGS := optional
-
-
# This is the target being built.
-
LOCAL_MODULE:= com.test.myserver
-
-
include $(BUILD_JAVA_LIBRARY)
-
-
include $(CLEAR_VARS)
-
LOCAL_MODULE := com.test.myserver.xml
-
-
LOCAL_MODULE_TAGS := optional
-
-
LOCAL_MODULE_CLASS := ETC
-
-
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/permissions
-
-
LOCAL_SRC_FILES := $(LOCAL_MODULE)
-
-
include $(BUILD_PREBUILT)
三.apk层的代码
3.1 TestData.java
-
package com.test;
-
import com.test.testData.R;
-
import android.app.Activity;
-
import android.content.Context;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.util.Log;
-
import android.widget.Button;
-
-
import test.myserver.dataservice.DataService;
-
-
public class TestData extends Activity {
-
public static final String TAG = "MYSERVER";
-
-
class DataServiceWrapper extends DataService {
-
public DataServiceWrapper(Context context) {
-
super(context);
-
}
-
-
}
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
final DataService im = new DataService(this);
-
Button btnTest= (Button) findViewById(R.id.btn_test);
-
-
btnTest.setOnClickListener(new OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
byte [] buf = new byte[16];
-
int i;
-
-
//Log.d(TAG, "next setSpritePosition()");
-
//im.setSpritePosition(5, 6);
-
//Log.d(TAG, "next im.readData()");
-
-
Log.d(TAG, "apk: next readData");
-
buf = im.readData();
-
Log.d(TAG, "apk: next readData over");
-
for(i=0; i<buf.length; i++)
-
{
-
Log.d(TAG, "apk: readData " + i + "= " + buf[i]);
-
}
-
-
Log.d(TAG, "apk: next writeData");
-
im.writeData(buf);
-
Log.d(TAG, "apk: next writeData over");
-
try{
-
}catch(Exception e){
-
Log.d(TAG, "cong: error");
-
}
-
}
-
});
-
}
-
}
四.编译脚本
4.1 myserver.sh
-
cong@msi:/work/$ cat myserver.sh
-
#!/bin/sh
-
make_server()
-
{
-
adb remount
-
mmm ./frameworks/myserver/server
-
adb push out/debug/target/product/ardbeg/system/bin/my_server /system/bin/
-
adb shell sync
-
}
-
-
make_core()
-
{
-
adb remount
-
mmm ./frameworks/myserver/core
-
adb push out/debug/target/product/ardbeg/system/framework/com.test.myserver.jar /system/framework/com.test.myserver.jar
-
adb push out/debug/target/product/ardbeg/system/etc/permissions/com.test.myserver.xml /system/etc/permissions/com.test.myserver.xml
-
adb shell sync
-
#cp out/target/common/obj/JAVA_LIBRARIES/com.ireadygo.cloudsim_intermediates/classes-full-debug.jar /tmp/
-
}
-
-
-
make_apk()
-
{
-
rm -rf ./frameworks/myserver/testapk/gen/
-
rm -rf ./frameworks/myserver/testakp/bin/
-
rm -rf ./frameworks/myserver/testapk/assets/
-
adb remount
-
mmm ./frameworks/myserver/testapk/
-
adb push out/debug/target/product/ardbeg/system/app/myserv_app.apk /system/app/
-
adb shell sync
-
}
-
-
case "$1" in
-
serv)
-
make_server
-
;;
-
core)
-
make_core
-
;;
-
apk)
-
make_apk
-
;;
-
*)
-
make_server
-
make_core
-
make_apk
-
;;
-
esac
五.执执结果
5.1
-
D/MYSERVER( 1502): apk: next readData --> 1. apk层调用read
-
D/MYSERVER( 1502): core: in func readData() --> 1. core层调用read
-
D/MYSERVER( 1459): server: read --> 1. server层调用read
-
D/MYSERVER( 1502): core: readData 0 = 0 --> 1. core层调用read返回并打印结果
-
D/MYSERVER( 1502): core: readData 1 = 1
-
D/MYSERVER( 1502): core: readData 2 = 2
-
D/MYSERVER( 1502): core: readData 3 = 3
-
D/MYSERVER( 1502): core: readData 4 = 4
-
D/MYSERVER( 1502): core: readData 5 = 5
-
D/MYSERVER( 1502): core: readData 6 = 6
-
D/MYSERVER( 1502): core: readData 7 = 7
-
D/MYSERVER( 1502): core: readData 8 = 8
-
D/MYSERVER( 1502): core: readData 9 = 9
-
D/MYSERVER( 1502): apk: next readData over --> 1. apk层调用read结束
-
D/MYSERVER( 1502): apk: readData 0= 0 --> 1. apk层调用read返回并打印结果
-
D/MYSERVER( 1502): apk: readData 1= 1
-
D/MYSERVER( 1502): apk: readData 2= 2
-
D/MYSERVER( 1502): apk: readData 3= 3
-
D/MYSERVER( 1502): apk: readData 4= 4
-
D/MYSERVER( 1502): apk: readData 5= 5
-
D/MYSERVER( 1502): apk: readData 6= 6
-
D/MYSERVER( 1502): apk: readData 7= 7
-
D/MYSERVER( 1502): apk: readData 8= 8
-
D/MYSERVER( 1502): apk: readData 9= 9
-
D/MYSERVER( 1502): apk: next writeData --> 2. apk层调用write
-
D/MYSERVER( 1502): core: in func writeData() --> 2. core层调用write
-
D/MYSERVER( 1459): server: write --> 2. server层调用write
-
D/MYSERVER( 1459): server: 0=0x0 --> 2. server层打印write的buf
-
D/MYSERVER( 1459): server: 1=0x1
-
D/MYSERVER( 1459): server: 2=0x2
-
D/MYSERVER( 1459): server: 3=0x3
-
D/MYSERVER( 1459): server: 4=0x4
-
D/MYSERVER( 1459): server: 5=0x5
-
D/MYSERVER( 1459): server: 6=0x6
-
D/MYSERVER( 1459): server: 7=0x7
-
D/MYSERVER( 1459): server: 8=0x8
-
D/MYSERVER( 1459): server: 9=0x9
-
D/MYSERVER( 1502): apk: next writeData over --> 2. apk层调用write返回
六.代码打包
6.1
server.rar(下载后改名为server.tar.gz)
6.2
core.rar(下载后改名为core.tar.gz)
6.3
testapk.rar(下载后改名为testapk.tar.gz)
七.注意
7.1 不在源码下编译apk
需要把在源码下编译出来的jar包发给apk的开发人员,路径是:
out/debug/target/common/obj/JAVA_LIBRARIES/com.test.myserver_intermediates/classes-full-debug.jar
一定是这个classes-full-debug.jar而不是
install out/debug/target/product/ardbeg/system/framework/com.test.myserver.jar
阅读(2963) | 评论(0) | 转发(0) |