添加硬件访问服务主要涉及三部分,JNI、AIDL、和Service。
JNI接口
- 在
frameworks/base/services/core/jni/
添加jni接口实现文件com_android_server_LedService.cpp。
com_android_server_LedService.cpp源文件:
...
...
static const JNINativeMethod methods[] = {
{"native_ledOpen", "()I", (void *)ledOpen},
{"native_ledClose", "()V", (void *)ledClose},
{"native_ledCtrl", "(II)I", (void *)ledCtrl},
};
int register_android_server_LedService(JNIEnv *env)
{
return jniRegisterNativeMethods(env, "com/android/server/LedService",
methods, NELEM(methods));
}
2 修改frameworks/base/services/core/jni/onload.cpp
...
namespace android {
...
int register_android_server_Watchdog(JNIEnv* env);
+int register_android_server_LedService(JNIEnv *env);
};
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
ALOGE("GetEnv failed!");
return result;
}
ALOG_ASSERT(env, "Could not retrieve the env!");
register_android_server_PowerManagerService(env);
+register_android_server_LedService(env);
return JNI_VERSION_1_4;
}
3 修改frameworks/base/services/core/jni/Android.mk
$(LOCAL_REL_DIR)/com_android_server_VibratorService.cpp \
+ $(LOCAL_REL_DIR)/com_android_server_LedService.cpp \
AIDL
1.把 ILedService.aidl 放入 frameworks/base/core/java/android/os
package android.os;
/** {@hide} */
interface ILedService
{
int ledCtrl(int which, int status);
}
- 修改
frameworks/base/Android.mk
添加一行
core/java/android/os/IVibratorService.aidl \
+core/java/android/os/ILedService.aidl \
Service
1.添加新文件frameworks/base/services/java/com/android/server/SystemServer.java
package com.android.server;
import android.os.ILedService;
public class LedService extends ILedService.Stub {
private static final String TAG = "LedService";
/* call native c function to access hardware */
public int ledCtrl(int which, int status) throws android.os.RemoteException
{
return native_ledCtrl(which, status);
}
public LedService() {
native_ledOpen();
}
public static native int native_ledOpen();
public static native void native_ledClose();
public static native int native_ledCtrl(int which, int status);
}
2.修改文件frameworks/base/services/java/com/android/server/SystemServer.java
+Slog.i(TAG, "Led Service");
+ServiceManager.addService("led", new LedService());
编译打包
mmm frameworks/base
mmm frameworks/base/services
make snod