Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2150496
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2015-01-29 11:14:28

一.server层的代码
1.1 server_main.cpp
  1. #define LOG_TAG "MYSERVER"

  2. #include <fcntl.h>
  3. #include <sys/prctl.h>
  4. #include <sys/wait.h>
  5. #include <binder/IPCThreadState.h>
  6. #include <binder/ProcessState.h>
  7. #include <binder/IServiceManager.h>
  8. #include <cutils/properties.h>
  9. #include <utils/Log.h>

  10. #include "DataService.h"

  11. using namespace android;

  12. int main(int argc, char** argv)
  13. {
  14.     sp<ProcessState> proc(ProcessState::self());
  15.     sp<IServiceManager> sm = defaultServiceManager();
  16.     SLOGD("ServiceManager: %p", sm.get());
  17.     DataService::instantiate();
  18.     ProcessState::self()->startThreadPool();
  19.     IPCThreadState::self()->joinThreadPool();
  20.     return 0;
  21. }

1.2

1.2.1
  1. cong@msi:/work/frameworks/myserver/server$ cat IDataService.h
  2. #ifndef ANDROID_IMOUSE_INJECT_H_FOR_APP
  3. #define ANDROID_IMOUSE_INJECT_H_FOR_APP

  4. #include <utils/Errors.h>
  5. #include <utils/KeyedVector.h>
  6. #include <utils/RefBase.h>
  7. #include <utils/String8.h>
  8. #include <binder/IInterface.h>
  9. #include <binder/Parcel.h>

  10. namespace android {
  11. class IDataService: public IInterface
  12. {
  13. public:
  14.     enum{
  15.         SET_MOUSE_POS = IBinder::FIRST_CALL_TRANSACTION,
  16.         READ__DATA,
  17.         WRITE__DATA,
  18.         AT_CREAT_NETWORK,
  19.         AT_SEND,
  20.         AT_RECV,
  21.         ADD_LISTENER,
  22.         REMOVE_LISTENER,
  23.     };
  24.     DECLARE_META_INTERFACE(DataService);
  25. };

  26. class BnDataService: public BnInterface<IDataService>
  27. {
  28. public:
  29.     virtual status_t onTransact( uint32_t code,
  30.                                     const Parcel& data,
  31.                                     Parcel* reply,
  32.                                     uint32_t flags = 0);
  33.     virtual int32_t setSpritePosition(int x, int y) = 0;
  34.     virtual int32_t readData(char* buf)=0;
  35.     virtual int32_t writeData(char* buf, int c)=0;
  36. };

  37. };

  38. #endif

1.2.2
  1. cong@msi:/work/frameworks/myserver/server$ cat IDataService.cpp
  2. #define LOG_TAG "MYSERVER"
  3. #include <stdint.h>
  4. #include <sys/types.h>

  5. #include <binder/Parcel.h>
  6. #include <binder/IMemory.h>

  7. #include <utils/Errors.h>
  8. #include <utils/String8.h>

  9. #include "IDataService.h"
  10. #include "DataService.h"
  11. namespace android {

  12. class BpDataService: public BpInterface<IDataService>
  13. {

  14. public:
  15.     BpDataService(const sp<IBinder>& impl)
  16.         : BpInterface<IDataService>(impl)
  17.     {}
  18.     virtual int32_t setSpritePosition(int x, int y)
  19.     {
  20.         Parcel data, reply;
  21.         SLOGD("IDataService.cpp L26: SET_MOUSE_POS");
  22.         data.writeInterfaceToken(IDataService::getInterfaceDescriptor());
  23.         status_t status = remote()->transact(SET_MOUSE_POS,data,&reply);
  24.         if(status == NO_ERROR){
  25.             return reply.readInt32();
  26.         }
  27.         return status;
  28.     }
  29.     virtual int32_t readData(char* buf)
  30.     {
  31.         Parcel data, reply;
  32.         data.writeInterfaceToken(IDataService::getInterfaceDescriptor());
  33.         status_t status = remote()->transact(READ__DATA,data,&reply);
  34.         if(status == NO_ERROR){
  35.             return reply.readInt32();
  36.         }
  37.         return status;
  38.     }
  39.     virtual int32_t writeData(char* buf, int cnt)
  40.     {
  41.         Parcel data, reply;
  42.         data.writeInterfaceToken(IDataService::getInterfaceDescriptor());
  43.         status_t status = remote()->transact(WRITE__DATA,data,&reply);
  44.         if(status == NO_ERROR){
  45.             return reply.readInt32();
  46.         }
  47.         return status;
  48.     }

  49. };

  50. IMPLEMENT_META_INTERFACE(DataService, "test.myserver.IDataService");

  51. status_t BnDataService::onTransact(
  52.     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  53. {
  54.     int32_t i;
  55.     char buf[1024];
  56.     memset(buf, 0, sizeof(buf));
  57.     switch (code) {
  58.         case SET_MOUSE_POS: { //
  59.             SLOGD("IDataService.cpp L124: SET_MOUSE_POS");
  60.             CHECK_INTERFACE(IDataService, data, reply);
  61.             int x = data.readInt32();
  62.             int y = data.readInt32();
  63.             reply->writeNoException();
  64.             reply->writeInt32(setSpritePosition(x, y));
  65.             return NO_ERROR;
  66.         } break;
  67.         case READ__DATA: { //
  68.             CHECK_INTERFACE(IDataService, data, reply);
  69.             int cnt = readData(buf);
  70. #if 0
  71.             for(int i=0; i<cnt; i++)
  72.                 SLOGD("%d=0x%x", i, buf[i]);
  73. #endif
  74.             reply->writeNoException();
  75.             //writeByteArray
  76.             reply->writeInt32(cnt);
  77.             reply->write(buf, cnt);
  78.             return NO_ERROR;
  79.         } break;
  80.         case WRITE__DATA: { //
  81.             CHECK_INTERFACE(IDataService, data, reply);
  82.             int bufferSize = data.readInt32();
  83.             if (0 < bufferSize) {
  84.                 data.read(buf, bufferSize);
  85.             }
  86.             reply->writeNoException();
  87.             reply->writeInt32(writeData(buf, bufferSize));
  88.             return NO_ERROR;
  89.         } break;
  90.         default:
  91.             return BBinder::onTransact(code, data, reply, flags);
  92.     }
  93. }

  94. };



1.3
1.3.1
  1. cong@msi:/work/frameworks/myserver/server$ cat DataService.h
  2. #ifndef ANDROID_MOUSE_INJECT_SERVICE_H_FOR_APP
  3. #define ANDROID_MOUSE_INJECT_SERVICE_H_FOR_APP

  4. #include <arpa/inet.h>

  5. #include <utils/threads.h>
  6. #include <utils/List.h>
  7. #include <utils/Errors.h>
  8. #include <utils/KeyedVector.h>
  9. #include <utils/String8.h>
  10. #include <utils/Vector.h>

  11. #include <binder/Parcel.h>
  12. #include <binder/IPCThreadState.h>
  13. #include <binder/IServiceManager.h>
  14. //#include <binder/AppOpsManager.h>
  15. #include <binder/BinderService.h>
  16. //#include <binder/IAppOpsCallback.h>
  17. #include "IDataService.h"

  18. namespace android {

  19. class DataService :
  20.     public BinderService<DataService>,
  21.     public BnDataService
  22. {
  23.     friend class BinderService<DataService>;
  24. public:
  25.   DataService();
  26.   virtual ~DataService();

  27.   static void instantiate();

  28.   virtual int32_t setSpritePosition(int x, int y);
  29.   virtual int32_t readData(char* buft);
  30.   virtual int32_t writeData(char* buf, int cnt);

  31.   virtual status_t onTransact(uint32_t code,
  32.             const Parcel &data,
  33.             Parcel *reply,
  34.             uint32_t flags);
  35. };

  36. static sp<DataService> mDataService;

  37. };

  38. #endif


1.3.2
  1. cong@msi:/work/frameworks/myserver/server$ cat DataService.cpp
  2. #define LOG_TAG "MYSERVER"

  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/time.h>
  8. #include <dirent.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <cutils/atomic.h>
  12. #include <cutils/properties.h>
  13. #include <utils/misc.h>
  14. #include <binder/IPCThreadState.h>
  15. #include <binder/IServiceManager.h>
  16. #include <binder/MemoryHeapBase.h>
  17. #include <binder/MemoryBase.h>
  18. #include <binder/Parcel.h>
  19. #include <utils/Errors.h>
  20. #include <utils/String8.h>
  21. #include <utils/SystemClock.h>
  22. #include <utils/Vector.h>
  23. #include <sys/ioctl.h>
  24. #include <linux/ioctl.h>
  25. #include "DataService.h"

  26. namespace android {
  27. status_t DataService::onTransact(uint32_t code,
  28.     const Parcel &data,
  29.     Parcel *reply,
  30.     uint32_t flags)
  31. {
  32.     return BnDataService::onTransact(code, data, reply, flags);
  33. }

  34. void DataService::instantiate() {
  35.     SLOGD("DataService instantiate...");
  36.     sp<DataService> iReadygokeyService = new DataService();
  37.     sp<IServiceManager> sm = defaultServiceManager();
  38.     status_t status = sm->addService(String16("iReadygo.RWService"), iReadygokeyService);
  39.     if(status != 0){
  40.        SLOGD("can't add InputMapper Service, status=%d, %s",status,strerror(errno));
  41.     }
  42.     mDataService = iReadygokeyService;
  43. }

  44. static void recv_callback(const char *s, const char *sms_pdu);

  45. DataService::DataService()
  46. {
  47.     SLOGD("DataService construct...");
  48. }

  49. DataService::~DataService()
  50. {
  51. }

  52. int DataService::setSpritePosition(int x, int y)
  53. {
  54.     SLOGD("server:DataService.cpp L111: final setSpritePosition\n");
  55.     return 0;
  56. }

  57. int DataService::readData(char* buf)
  58. {
  59.     int i;
  60.     int len = 10;
  61.     SLOGD("server: read");
  62.     for(i=0; i<10; i++)
  63.     {
  64.         buf[i] = i;
  65.     }
  66.     return len;
  67. }

  68. int DataService::writeData(char* buf, int cnt)
  69. {
  70.     int i;
  71.     SLOGD("server: write");
  72.     for(i=0; i<cnt; i++)
  73.     {
  74.         SLOGD("server: %d=0x%x ", i, buf[i]);
  75.     }
  76.     return 0;
  77. }

  78. }

1.4 Android.mk
  1. LOCAL_PATH:= $(call my-dir)

  2. include $(CLEAR_VARS)

  3. LOCAL_SRC_FILES:= \
  4.     server_main.cpp \
  5.     IDataService.cpp \
  6.     DataService.cpp

  7. LOCAL_SHARED_LIBRARIES := \
  8.     libutils \
  9.     libcutils \
  10.     liblog \
  11.     libhardware_legacy \
  12.     libbinder

  13. LOCAL_MODULE_TAGS := optional

  14. LOCAL_MODULE:= my_server

  15. include $(BUILD_EXECUTABLE)



二. core层的代码
2.1
  1. cong@msi:/work/frameworks/myserver/core$ cat java/com/test/myserver/DataService.java
  2. package test.myserver.dataservice;

  3. import android.content.Context;
  4. import test.myserver.IDataService;
  5. import android.os.IBinder;
  6. import android.os.Handler;
  7. import android.os.Looper;
  8. import android.os.RemoteException;
  9. import android.os.ServiceManager;
  10. import android.util.Log;

  11. public class DataService {
  12.     public static final String TAG = "MYSERVER";
  13.     //private static final String IREADYGO_KEY_SERVICE_BINDER_NAME = "iReadygo.DataService";
  14.     private static final String MYSERVICE_BINDER_NAME = "iReadygo.RWService";
  15.     private final IDataService mDataService;
  16.     private final Context mContext;

  17.     public DataService(Context context)
  18.     {
  19.         mContext = context;
  20.         IBinder DataServiceBinder = ServiceManager.getService(MYSERVICE_BINDER_NAME);
  21.         mDataService = IDataService.Stub.asInterface(DataServiceBinder);
  22.     }

  23.     public int setSpritePosition(int x, int y)
  24.     {
  25.         int ret = -1;
  26.         Log.d(TAG, "in func setSpritePosition()");
  27.         if( null ){
  28.             try{
  29.                 ret = mDataService.setSpritePosition(x, y);
  30.             } catch (RemoteException e){
  31.                 Log.e(TAG, Log.getStackTraceString(new Throwable()));
  32.             }
  33.         }
  34.         return ret;
  35.     }

  36.     public byte[] readData( )
  37.     {
  38.         byte[] buf = null;
  39.         int ret = -1;
  40.         int i=0;
  41.         Log.d(TAG, "core: in func readData()");
  42.         if( null ){
  43.             try{
  44.                 buf = mDataService.readData();
  45.                 for(i=0; i<buf.length; i++)
  46.                     Log.d(TAG, "core: readData "+i+" = "+ buf[i]);
  47.             } catch (RemoteException e){
  48.                 Log.e(TAG, Log.getStackTraceString(new Throwable()));
  49.                 return null;
  50.             }
  51.         }
  52.         return buf;
  53.     }
  54.     public int writeData(byte[] buf)
  55.     {
  56.         int ret = -1;
  57.         Log.d(TAG, "core: in func writeData()");
  58.         if( null ){
  59.             try{
  60.                 ret = mDataService.writeData(buf);
  61.             } catch (RemoteException e){
  62.                 Log.e(TAG, Log.getStackTraceString(new Throwable()));
  63.             }
  64.         }
  65.         return ret;
  66.     }


  67. }

2.2
  1. cong@msi:/work/frameworks/myserver/core$ cat java/com/test/myserver/IDataService.aidl
  2. package test.myserver;


  3. /** @hide */
  4. interface IDataService
  5. {
  6.     int setSpritePosition(int x, int y);
  7.     byte[] readData();
  8.     int writeData(in byte[] data);
  9. }

2.3
  1. cong@msi:/work/frameworks/myserver/core$ cat com.test.myserver.xml
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <permissions>
  4.     <library name="com.test.myserver.xml"
  5.             file="/system/framework/com.test.myserver.jar"/>
  6. </permissions>

2.4
  1. cong@msi:/work/frameworks/myserver/core$ cat Android.mk
  2. LOCAL_PATH := $(call my-dir)

  3. include $(CLEAR_VARS)
  4.     
  5. LOCAL_SRC_FILES := \
  6.     $(call all-subdir-java-files) \
  7.     java/com/test/myserver/IDataService.aidl

  8. LOCAL_AIDL_INCLUDES += frameworks/myserver/core/java/com

  9. LOCAL_MODULE_TAGS := optional

  10. # This is the target being built.
  11. LOCAL_MODULE:= com.test.myserver

  12. include $(BUILD_JAVA_LIBRARY)

  13. include $(CLEAR_VARS)
  14. LOCAL_MODULE := com.test.myserver.xml

  15. LOCAL_MODULE_TAGS := optional

  16. LOCAL_MODULE_CLASS := ETC

  17. LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/permissions

  18. LOCAL_SRC_FILES := $(LOCAL_MODULE)

  19. include $(BUILD_PREBUILT)




三.apk层的代码
3.1 TestData.java
  1. package com.test;
  2. import com.test.testData.R;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.util.Log;
  9. import android.widget.Button;

  10. import test.myserver.dataservice.DataService;

  11. public class TestData extends Activity {
  12.     public static final String TAG = "MYSERVER";

  13.     class DataServiceWrapper extends DataService {
  14.         public DataServiceWrapper(Context context) {
  15.             super(context);
  16.         }

  17.     }

  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.         final DataService im = new DataService(this);
  23.         Button btnTest= (Button) findViewById(R.id.btn_test);

  24.         btnTest.setOnClickListener(new OnClickListener() {
  25.             @Override
  26.             public void onClick(View v) {
  27.                 byte [] buf = new byte[16];
  28.                 int i;

  29.                 //Log.d(TAG, "next setSpritePosition()");
  30.                 //im.setSpritePosition(5, 6);
  31.                 //Log.d(TAG, "next im.readData()");
  32.                 
  33.                 Log.d(TAG, "apk: next readData");
  34.                 buf = im.readData();
  35.                 Log.d(TAG, "apk: next readData over");
  36.                 for(i=0; i<buf.length; i++)
  37.                 {
  38.                     Log.d(TAG, "apk: readData " + i + "= " + buf[i]);
  39.                 }

  40.                 Log.d(TAG, "apk: next writeData");
  41.                 im.writeData(buf);
  42.                 Log.d(TAG, "apk: next writeData over");
  43.                 try{
  44.                 }catch(Exception e){
  45.                     Log.d(TAG, "cong: error");
  46.                 }
  47.             }
  48.         });
  49.     }
  50. }


四.编译脚本
4.1 myserver.sh
  1. cong@msi:/work/$ cat myserver.sh
  2. #!/bin/sh
  3. make_server()
  4. {
  5.     adb remount
  6.     mmm ./frameworks/myserver/server
  7.     adb push out/debug/target/product/ardbeg/system/bin/my_server /system/bin/
  8.     adb shell sync
  9. }

  10. make_core()
  11. {
  12.     adb remount
  13.     mmm ./frameworks/myserver/core
  14.     adb push out/debug/target/product/ardbeg/system/framework/com.test.myserver.jar /system/framework/com.test.myserver.jar
  15.     adb push out/debug/target/product/ardbeg/system/etc/permissions/com.test.myserver.xml /system/etc/permissions/com.test.myserver.xml
  16.     adb shell sync
  17.     #cp out/target/common/obj/JAVA_LIBRARIES/com.ireadygo.cloudsim_intermediates/classes-full-debug.jar /tmp/
  18. }


  19. make_apk()
  20. {
  21.     rm -rf ./frameworks/myserver/testapk/gen/
  22.     rm -rf ./frameworks/myserver/testakp/bin/
  23.     rm -rf ./frameworks/myserver/testapk/assets/
  24.     adb remount
  25.     mmm ./frameworks/myserver/testapk/
  26.     adb push out/debug/target/product/ardbeg/system/app/myserv_app.apk /system/app/
  27.     adb shell sync
  28. }

  29. case "$1" in
  30.     serv)
  31.         make_server
  32.         ;;
  33.     core)
  34.         make_core
  35.         ;;
  36.     apk)
  37.         make_apk
  38.         ;;
  39.     *)
  40.         make_server
  41.         make_core
  42.         make_apk
  43.         ;;
  44. esac


五.执执结果
5.1
  1. D/MYSERVER( 1502): apk: next readData            --> 1. apk层调用read
  2. D/MYSERVER( 1502): core: in func readData()      --> 1. core层调用read
  3. D/MYSERVER( 1459): server: read                  --> 1. server层调用read
  4. D/MYSERVER( 1502): core: readData 0 = 0          --> 1. core层调用read返回并打印结果
  5. D/MYSERVER( 1502): core: readData 1 = 1
  6. D/MYSERVER( 1502): core: readData 2 = 2
  7. D/MYSERVER( 1502): core: readData 3 = 3
  8. D/MYSERVER( 1502): core: readData 4 = 4
  9. D/MYSERVER( 1502): core: readData 5 = 5
  10. D/MYSERVER( 1502): core: readData 6 = 6
  11. D/MYSERVER( 1502): core: readData 7 = 7
  12. D/MYSERVER( 1502): core: readData 8 = 8
  13. D/MYSERVER( 1502): core: readData 9 = 9
  14. D/MYSERVER( 1502): apk: next readData over      --> 1. apk层调用read结束
  15. D/MYSERVER( 1502): apk: readData 0= 0           --> 1. apk层调用read返回并打印结果
  16. D/MYSERVER( 1502): apk: readData 1= 1
  17. D/MYSERVER( 1502): apk: readData 2= 2
  18. D/MYSERVER( 1502): apk: readData 3= 3
  19. D/MYSERVER( 1502): apk: readData 4= 4
  20. D/MYSERVER( 1502): apk: readData 5= 5
  21. D/MYSERVER( 1502): apk: readData 6= 6
  22. D/MYSERVER( 1502): apk: readData 7= 7
  23. D/MYSERVER( 1502): apk: readData 8= 8
  24. D/MYSERVER( 1502): apk: readData 9= 9
  25. D/MYSERVER( 1502): apk: next writeData           --> 2. apk层调用write
  26. D/MYSERVER( 1502): core: in func writeData()     --> 2. core层调用write
  27. D/MYSERVER( 1459): server: write                 --> 2. server层调用write
  28. D/MYSERVER( 1459): server: 0=0x0                 --> 2. server层打印write的buf
  29. D/MYSERVER( 1459): server: 1=0x1
  30. D/MYSERVER( 1459): server: 2=0x2
  31. D/MYSERVER( 1459): server: 3=0x3
  32. D/MYSERVER( 1459): server: 4=0x4
  33. D/MYSERVER( 1459): server: 5=0x5
  34. D/MYSERVER( 1459): server: 6=0x6
  35. D/MYSERVER( 1459): server: 7=0x7
  36. D/MYSERVER( 1459): server: 8=0x8
  37. D/MYSERVER( 1459): server: 9=0x9
  38. 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


阅读(2929) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~