Chinaunix首页 | 论坛 | 博客
  • 博客访问: 752276
  • 博文数量: 98
  • 博客积分: 4934
  • 博客等级: 上校
  • 技术积分: 1151
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-12 19:55
文章分类

全部博文(98)

文章存档

2014年(1)

2013年(2)

2012年(4)

2011年(25)

2010年(33)

2009年(33)

分类: 嵌入式

2012-09-27 15:03:57

三,框架层(framework)

   这里主要实现两部分

(1: 为Android HAL编写JNI方法,以便使得上层的APP能够使用下层提供的硬件服务

(2: 在Android系统的框架层提供Java接口的硬件服务

1,在frameworks/base/services/jni中新建com_android_server_swtled.cpp

点击(此处)折叠或打开

  1. #define LOG_TAG "SwtledService"
  2. #include "jni.h"
  3. #include "JNIHelp.h"
  4. #include "android_runtime/AndroidRuntime.h"
  5. #include <utils/misc.h>
  6. #include <utils/Log.h>
  7. #include <hardware/hardware.h>
  8. #include <hardware/swtled.h>
  9. #include <stdio.h>



  10. namespace android
  11. {
  12.     struct swtled_device_t* swtled_device = NULL;


  13.     static void swtled_set_on(JNIEnv* env, jobject clazz,jint number)
  14.     {
  15.         int num = number;
  16.         LOGI("Swtled JNI: select lighton is %d",num);

  17.         if (!swtled_device)
  18.         {
  19.             LOGI("Swtled JNI:device is not open.");
  20.             return;
  21.         }
  22.         swtled_device->set_on(swtled_device,num);
  23.     }

  24.     static void swtled_set_off(JNIEnv* env, jobject clazz,jint number)
  25.     {
  26.          
  27.         int num = number;
  28.         LOGI("Swtled JNI: select lightoff is %d",num);

  29.         if (!swtled_device)
  30.         {
  31.             LOGI("Swtled JNI:device is not open.");
  32.             return;
  33.         }
  34.         swtled_device->set_off(swtled_device,num);
  35.     }

  36.     static inline int swtled_device_open(hw_module_t* module,swtled_device_t** device)
  37.     {
  38.         return module->methods->open(module,SWTLED_HARDWARE_MODULE_ID,(hw_device_t**)device);
  39.     }
  40.     

  41.     static jboolean swtled_init(JNIEnv* env,jclass clazz)
  42.     {
  43.         swtled_module_t* swtledmodule;
  44.         
  45.         LOGI("Swtled JNI: initializing...");

  46.         if (hw_get_module(SWTLED_HARDWARE_MODULE_ID,(const struct hw_module_t**)&swtledmodule)==0)
  47.         {
  48.             LOGI("Swtled JNI: swtled stub be found.");
  49.             if (swtled_device_open(&(swtledmodule->common),&swtled_device) == 0)
  50.             {
  51.                 LOGI("Swtled JNI: swtled device open successful.");
  52.                 return 0;
  53.             }

  54.             LOGI("Swtled JNI: failed to open swtled device.");
  55.             return -1;
  56.         }
  57.         
  58.         LOGI("Swtled JNI: failed to get swtled stub module.");
  59.         return -1;
  60.      }
  61.     
  62.      static const JNINativeMethod method_table[] =
  63.      {
  64.         {"init_native", "()Z", (void*)swtled_init},
  65.         {"setOn_native","(I)V",(void*)swtled_set_on},
  66.         {"setOff_native","(I)V",(void*)swtled_set_off},
  67.      };

  68.     int register_android_server_SwtledService(JNIEnv* env)
  69.     {
  70.         return jniRegisterNativeMethods(env,"com/android/server/SwtledService",method_table,NELEM(method_table));
  71.     }
  72. }

修改同级目录下的Android.mk和Onload.cpp文件

在Android.mk的LOCAL_SRC_FILES:=下添加

点击(此处)折叠或打开

  1. LOCAL_SRC_FILES:= \
  2. ....
  3. com_android_server_swtled.cpp \

在Onload.cpp中的namespace android {  下加入

点击(此处)折叠或打开

  1. int register_android_server_SwtledService(JNIEnv* env);

同时在JNI_OnLoad函数下加入

点击(此处)折叠或打开

  1. register_android_server_SwtledService(env);


2,编译

   (1,执行mmm framework/base/services/jni

   (2,make snod

3,在frameworks/base/core/java/android/os 新建ISwtledService.aidl,代码如下

点击(此处)折叠或打开

  1. package android.os;



  2. interface ISwtledService {

  3.     void setOn(int number);

  4.     void setOff(int number);

  5. }
返回到frameworks/base目录,打开Android.mk文件,修改LOCAL_SRC_FILES变量的值,增加 ISwtledService.aidl源文件

点击(此处)折叠或打开

  1. LOCAL_SRC_FILES += /
  2. ....................................................................
  3. core/java/android/os/IVibratorService.aidl /
  4. core/java/android/os/ISwtledService.aidl /
  5. core/java/android/service/urlrenderer/IUrlRendererService.aidl /
4,编译ISwtledService接口
  mmm framework/base,如果正确,那么会根据ISwtledService.aidl生成对应的ISwtledService.Stub接口
 
5,进入到frameworks/base/services/java/com/android/server目录,新增ISwtledService.java文件:


点击(此处)折叠或打开

  1. package com.android.server;
  2. import android.content.Context;
  3. import android.os.ISwtledService;
  4. import android.util.Slog;


  5. public class SwtledService extends ISwtledService.Stub {

  6.     private static final String TAG = "SwtledService";

  7.     SwtledService()
  8.     {
  9.         init_native();
  10.     }

  11.     public void setOn(int number)
  12.     {
  13.         setOn_native(number);
  14.     }

  15.     public void setOff(int number)
  16.     {
  17.         setOff_native(number);
  18.     }

  19.     private static native boolean init_native();
  20.     private static native void setOn_native(int number);
  21.     private static native void setOff_native(int number);
  22. };
6,修改同目录下的SystemServer.java文件,在ServerThread::run方法里加入

点击(此处)折叠或打开

  1. try{
  2.                  
  3.                 Slog.i(TAG, "Swtled Service");
  4.                 ServiceManager.addService("swtled", new SwtledService());
  5.             } catch (Throwable e) {
  6.                 Slog.e(TAG, "Failure starting Swtled Service", e);
  7.             }
7,编译SwtledService.java并打包进system.img
   (1)mmm framework/base/services/java
   (2)make snod


 

 

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