三,框架层(framework)
这里主要实现两部分
(1: 为Android HAL编写JNI方法,以便使得上层的APP能够使用下层提供的硬件服务
(2: 在Android系统的框架层提供Java接口的硬件服务
1,在frameworks/base/services/jni中新建com_android_server_swtled.cpp
- #define LOG_TAG "SwtledService"
- #include "jni.h"
- #include "JNIHelp.h"
- #include "android_runtime/AndroidRuntime.h"
- #include <utils/misc.h>
- #include <utils/Log.h>
- #include <hardware/hardware.h>
- #include <hardware/swtled.h>
- #include <stdio.h>
- namespace android
- {
- struct swtled_device_t* swtled_device = NULL;
- static void swtled_set_on(JNIEnv* env, jobject clazz,jint number)
- {
- int num = number;
- LOGI("Swtled JNI: select lighton is %d",num);
- if (!swtled_device)
- {
- LOGI("Swtled JNI:device is not open.");
- return;
- }
- swtled_device->set_on(swtled_device,num);
- }
- static void swtled_set_off(JNIEnv* env, jobject clazz,jint number)
- {
-
- int num = number;
- LOGI("Swtled JNI: select lightoff is %d",num);
- if (!swtled_device)
- {
- LOGI("Swtled JNI:device is not open.");
- return;
- }
- swtled_device->set_off(swtled_device,num);
- }
- static inline int swtled_device_open(hw_module_t* module,swtled_device_t** device)
- {
- return module->methods->open(module,SWTLED_HARDWARE_MODULE_ID,(hw_device_t**)device);
- }
-
- static jboolean swtled_init(JNIEnv* env,jclass clazz)
- {
- swtled_module_t* swtledmodule;
-
- LOGI("Swtled JNI: initializing...");
- if (hw_get_module(SWTLED_HARDWARE_MODULE_ID,(const struct hw_module_t**)&swtledmodule)==0)
- {
- LOGI("Swtled JNI: swtled stub be found.");
- if (swtled_device_open(&(swtledmodule->common),&swtled_device) == 0)
- {
- LOGI("Swtled JNI: swtled device open successful.");
- return 0;
- }
- LOGI("Swtled JNI: failed to open swtled device.");
- return -1;
- }
-
- LOGI("Swtled JNI: failed to get swtled stub module.");
- return -1;
- }
-
- static const JNINativeMethod method_table[] =
- {
- {"init_native", "()Z", (void*)swtled_init},
- {"setOn_native","(I)V",(void*)swtled_set_on},
- {"setOff_native","(I)V",(void*)swtled_set_off},
- };
- int register_android_server_SwtledService(JNIEnv* env)
- {
- return jniRegisterNativeMethods(env,"com/android/server/SwtledService",method_table,NELEM(method_table));
- }
- }
修改同级目录下的Android.mk和Onload.cpp文件
在Android.mk的LOCAL_SRC_FILES:=下添加
- LOCAL_SRC_FILES:= \
- ....
- com_android_server_swtled.cpp \
在Onload.cpp中的namespace android { 下加入
- int register_android_server_SwtledService(JNIEnv* env);
同时在JNI_OnLoad函数下加入
- register_android_server_SwtledService(env);
2,编译
(1,执行mmm framework/base/services/jni
(2,make snod
3,在frameworks/base/core/java/android/os 新建ISwtledService.aidl,代码如下
- package android.os;
- interface ISwtledService {
- void setOn(int number);
- void setOff(int number);
- }
返回到frameworks/base目录,打开Android.mk文件,修改LOCAL_SRC_FILES变量的值,增加 ISwtledService.aidl源文件
- LOCAL_SRC_FILES += /
- ....................................................................
- core/java/android/os/IVibratorService.aidl /
- core/java/android/os/ISwtledService.aidl /
- 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文件:
- package com.android.server;
- import android.content.Context;
- import android.os.ISwtledService;
- import android.util.Slog;
- public class SwtledService extends ISwtledService.Stub {
- private static final String TAG = "SwtledService";
- SwtledService()
- {
- init_native();
- }
- public void setOn(int number)
- {
- setOn_native(number);
- }
- public void setOff(int number)
- {
- setOff_native(number);
- }
- private static native boolean init_native();
- private static native void setOn_native(int number);
- private static native void setOff_native(int number);
- };
6,修改同目录下的SystemServer.java文件,在ServerThread::run方法里加入
- try{
-
- Slog.i(TAG, "Swtled Service");
- ServiceManager.addService("swtled", new SwtledService());
- } catch (Throwable e) {
- Slog.e(TAG, "Failure starting Swtled Service", e);
- }
7,编译SwtledService.java并打包进system.img
(1)mmm framework/base/services/java
(2)make snod
阅读(5445) | 评论(0) | 转发(0) |