Android添加USB add-on硬件访问服务

这里要在android N添加一个Usb add-on的状态查询、切换控制的服务。应该L/M版本中也可以使用。android O以及之后的话,框架有变化,应该都放到vendor下。
1. AIDL
1.1 创建 frameworks/base/core/java/android/os/IUsbAddon.aidl

package android.os;

/** {@hide} */
interface IUsbAddon
{
    boolean isAddon();
    boolean switch2Addon(boolean on);
}

1.2. 修改 frameworks/base/Android.mk
    添加    core/java/android/os/IUsbAddon.aidl \    如下:

    core/java/android/os/IUserManager.aidl \
    core/java/android/os/IVibratorService.aidl \
    ......
    core/java/android/os/IUsbAddon.aidl \

执行 mmm frameworks/base,此时会自动根据aidl文件生成对应的stub接口,输出在 out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os/IUsbAddon.java

2. 添加远端实现UsbAddonService
frameworks/base/services/core/java/com/android/server/UsbAddonService.java

package com.android.server;
import android.util.Log;
import android.os.IUsbAddon;
/**
     * @hide
     */
public class UsbAddonService extends IUsbAddon.Stub {
    /**
     * @hide
     */
    public boolean isAddon() {
        int result = isusbexternel();
        Slog.e("UsbAddon", "isusbexternel=" + result);
        return result==1;
    }
    /**
     * @hide
     */
    public boolean switch2Addon(boolean on) {
        Log.e("UsbAddon", "switch2Addon:" + on);
        return usb_switch(on?1:0) == 0;
    }

    private static int usb_switch( int which );
    /*
     *    retrun val: 0: usb internel/pc; 1: usb externel/addon/fingerprint; other: error.
     */
    private static native int isusbexternel();
}


3. 创建对应的USB add-on管理程序
frameworks/base/core/java/android/app/UsbAddon.java

package android.app;
import android.util.Log;
import android.os.IUsbAddon;
import android.content.Context;
import android.os.RemoteException;

public class UsbAddon {
    private static String TAG = "UsbAddon";

    IUsbAddon mUsbAddon;

    public UsbAddon(Context ctx,IUsbAddon UsbAddon) {
        mUsbAddon = UsbAddon;
    }

    public boolean isAddon() throws RemoteException {
        return mUsbAddon.isAddon();
    }

    public boolean switch2Addon(boolean on) throws RemoteException {
        return mUsbAddon.switch2Addon(on);
    }
}

4. 管理和注册 USB add-on service
在系统Service中注册服务,需要分别在SystemServer.java和SystemServiceRegistry.java中修改。

在SystemServer.java中将UsbAddonService添加到系统服务中
frameworks/base/services/java/com/android/server/SystemServer.java

@@ -678,6 +678,11 @@ public final class SystemServer {
             Slog.i(TAG, "Reading configuration...");
             SystemConfig.getInstance();
 
+            //sukha, usbaddon
+            traceBeginAndSlog("Start UsbAddonService");
+            ServiceManager.addService("usbaddonservice", new UsbAddonService());
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+
             traceBeginAndSlog("StartSchedulingPolicyService");
             ServiceManager.addService("scheduling_policy", new SchedulingPolicyService());
             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); 

在SystemServiceRegistry.java中注册UsbAddonService服务
frameworks/base/core/java/android/app/SystemServiceRegistry.java

@@ -156,6 +156,8 @@ import com.mediatek.usp.IUspService;
 import com.mediatek.usp.UspManager;
 /// @}
 
+import android.os.IUsbAddon;
+
 import java.util.HashMap;
 
 /**
@@ -177,6 +179,16 @@ final class SystemServiceRegistry {
     private SystemServiceRegistry() { }
 
     static {
+            //sukha, usbaddon
+            registerService("usbaddonservice", UsbAddon.class,
+                new CachedServiceFetcher() {
+                    @Override
+                    public UsbAddon createService(ContextImpl ctx) {
+                        IBinder b = ServiceManager.getService("usbaddonservice");
+                        IUsbAddon service = IUsbAddon.Stub.asInterface(b);
+                        return new UsbAddon(ctx,service);
+                }});
+        
         registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
                 new CachedServiceFetcher() {
             @Override 


 

5. native jni
5.1 功能实现
创建 frameworks/base/services/core/jni/com_android_server_UsbAddonService.cpp 文件如下:

#define LOG_TAG "UsbAddonService"

#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"
#include      /*标准输入输出定义*/  
#include     /*标准函数库定义*/  
#include     /*Unix 标准函数定义*/  
#include   
#include     
#include      /*文件控制定义*/  
#include    /*PPSIX 终端控制定义*/  
#include      /*错误号定义*/  
#include  

#include 
#include 

#include 
#include 
#include 


/*
  retrun val: 0: usb internel; 1: usb externel; other: error.
*/
int usb2addon_drv_isusbexternel()
{

........
    return 0;
}


int usb2addon_drv_switch( int turnon )
{

......
    return 0;
}


namespace android
{
    JNIEXPORT jint JNICALL com_android_server_UsbAddonService_usb_switch (JNIEnv *e, jobject o, jint i)
    {
        ALOGE ("%s: enter; mode=%d", __FUNCTION__,i );
        return usb2addon_drv_switch(i);
    }

    JNIEXPORT jint JNICALL com_android_server_UsbAddonService_isusbexternel(JNIEnv *e, jobject o)
    {
        ALOGE ("%s: enter", __FUNCTION__);
        return usb2addon_drv_isusbexternel();
    }

    /*Java本地接口方法表*/
    static JNINativeMethod method_table[] = {
        { "isusbexternel", "()I", (void*)com_android_server_UsbAddonService_isusbexternel },
        { "usb_switch", "(I)I", (void*)com_android_server_UsbAddonService_usb_switch }
    };

    /*注册Java本地接口方法*/
    int register_android_server_UsbAddonService(JNIEnv *env)
    {
            return jniRegisterNativeMethods(env, "com/android/server/UsbAddonService",
            method_table, NELEM(method_table));
    }

};

5.2. 注册native接口
在frameworks/base/services/core/jni/onload.cpp中添加如下:

@@ -51,6 +51,8 @@ int register_android_server_tv_TvInputHal(JNIEnv* env);
 int register_android_server_PersistentDataBlockService(JNIEnv* env);
 int register_android_server_Watchdog(JNIEnv* env);
 int register_android_server_HardwarePropertiesManagerService(JNIEnv* env);
+//sukha, usbaddon
+int register_android_server_UsbAddonService(JNIEnv *env);
 };

 /// M: Mediatek defined @{
@@ -118,6 +120,7 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
     register_com_android_internal_app_ShutdownManager(env);
 #endif
 
-
+    //sukha, usbaddon
+    register_android_server_UsbAddonService(env);
     return JNI_VERSION_1_4;
 } 

 

5.3 修改Android.mk
在frameworks/base/services/core/jni/Android.mk中加入如下:

@@ -39,6 +39,7 @@ LOCAL_SRC_FILES += \
     $(LOCAL_REL_DIR)/com_mediatek_perfservice_PerfServiceManager.cpp \
     $(LOCAL_REL_DIR)/com_mediatek_hdmi_MtkHdmiManagerService.cpp \
     $(LOCAL_REL_DIR)/com_android_server_display_DisplayPowerController.cpp \
+    $(LOCAL_REL_DIR)/com_android_server_UsbAddonService.cpp \
     $(LOCAL_REL_DIR)/onload.cpp
 
 ifneq (yes,$(MTK_BSP_PACKAGE)) 

6. sepolicy 修改
6.1 添加服务类型
system/sepolicy/service.te

#sukha, usbaddon
type usbaddonservice, system_api_service, system_server_service, service_manager_type;


6.2 服务权限
system/sepolicy/service_contexts

#sukha, usbaddon
usbaddonservice               u:object_r:usbaddonservice:s0


6.3 添加system_server操作设备文件的权限
device/mediatek/common/sepolicy/basic/system_server.te

#sukha, usbaddon
allow system_server g909_usb_sys_file:file rw_file_perms;


6.4 让应用可以访问改服务,这里只在untrusted_app添加,如果需要的话,system app之类的都要加上。
device/mediatek/common/sepolicy/basic/untrusted_app.te

allow untrusted_app usbaddonservice:service_manager find;

7. 编译
mmm frameworks/base/services 重编服务
make update-api 更新api8. 在app中调用

UsbAddon usbAddon = (UsbAddon) getSystemService("usbaddonservice");
try {
    useAddonUsb = useAddonUsb();
    if (!useAddonUsb) {
        switch2AddonUsb(true);
    }
} catch (RemoteException e) {
    e.printStackTrace();
}

//Add-on USB switch
public boolean useAddonUsb() throws RemoteException {
    useAddonUsb = usbAddon.isAddon();
    Log.d(TAG, "Is using addon USB: " + useAddonUsb);
    return useAddonUsb;
}
void switch2AddonUsb(boolean on) throws RemoteException {
    usbAddon.switch2Addon(on);
}

这里需要导入的jar文件可以在以上编译中生成获得。
 

你可能感兴趣的:(eclipse,android,android,studio,framework)