Android添加硬件訪問服務

添加硬件訪問服務主要涉及三部分,JNI、AIDL、和Service。

JNI接口

  1. 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);
}
  1. 修改 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章