android系统级别硬件访问服务程序

硬件访问服务程序

实现一个硬件访问服务程序,需要
- 接口文件aidl
- service实现这个aidl
- SystemServer中注册这个服务
- service需要的jni文件
- OnLoad.cpp中添加jni文件
- hal 文件
- 相应的Android.mk

接口文件aidl

  • aidl的路径
  • aidl的写法
  • aidl的结果

aidl的路径

android系统的aidl文件在

/frameworks/base/core/java/android/os/IVibratorService.aidl
/frameworks/base/core/java/android/hardware/

这两个目录下都存在一些aidl文件。其他的目录下也存在一些aidl文件。
android.os下的aidl文件,通过其实现类,在SystemService中被调用,并添加到了ServiceManager中。同时通常能被应用程序通过getSystemService来访问使用。
在android.hardware下模块。通常允许应用程序调用andrid.hardware.xxx来实现相应的功能。例如开源的二维码扫描软件zxing,就利用android.hardware.camera来调用camera的相关接口。

aidl的写法

aidl内定义的是一些接口

package android.os;

/** {@hide} */
interface IVibratorService
{
    boolean hasVibrator();
    void vibrate(int uid, String opPkg, long milliseconds, int usageHint, IBinder token);
    void vibratePattern(int uid, String opPkg, in long[] pattern, int repeat, int usageHint, IBinder token);
    void cancelVibrate(IBinder token);
}

首先需要指定包名。Vibrator位于android.os这个包下。
需要注意的是这些接口文件,需要使用/* {@hide} / 进行修饰一下
这些接口是隐藏的。

aidl的结果

将aidl文件放入
frameworks/base/core/java/android/os/目录下后,
修改framework/base/Android.mk 添加定义的aidl文件
使用 . build/envsetu.sh
lunch xxx
mmm framework/base

 将会在 /out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os/

下生成 IVibratorService.java文件
生成的文件依然是一个interface类型的文件
内部还有一个静态的类。继承android.os.binder 并实现了 IVibratorService 这个接口

public interface IVibratorService extends android.os.IInterface{
public static abstract class Stub extends android.os.Binder implements android.os.IVibratorService
{

}
}

android的编译系统自动帮助我们实现了binder机制,实现了这个interface。
必然存在一个文件实现这个interface

service来实现这个aidl

  • 代码的路径
/frameworks/base/services/core/java/com/android/server/VibratorService.java
  • 代码实现
public class VibratorService extends IVibratorService.Stub
        implements InputManager.InputDeviceListener {
        //本地实现的方法 
      native static boolean vibratorExists();
      native static void vibratorOn(long milliseconds);
      native static void vibratorOff();

        //构造方法
         VibratorService(Context context) {
        // Reset the hardware to a default state, in case this is a runtime
        // restart instead of a fresh boot.
        vibratorOff();

        mContext = context;
        PowerManager pm = (PowerManager)context.getSystemService(
                Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
        mWakeLock.setReferenceCounted(true);

        mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
        mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
                BatteryStats.SERVICE_NAME));

        mVibrations = new LinkedList<Vibration>();

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        context.registerReceiver(mIntentReceiver, filter);
    }

SystemServer中注册这个服务

调用构造函数,同时将virbator添加到ServiceManager中

 VibratorService vibrator = null;
vibrator = new VibratorService(context);
ServiceManager.addService("vibrator", vibrator);

 try {
            vibrator.systemReady();
        } catch (Throwable e) {
            reportWtf("making Vibrator Service ready", e);
      }

service需要的jni文件

在构造函数中调用了
vibratorOff(); 这个本地方法

这个本地的方法是在
/frameworks/base/services/core/jni/com_android_server_VibratorService.cpp
中实现的

     static jboolean vibratorExists(JNIEnv *env, jobject clazz)
     {
        return vibrator_exists() > 0 ? JNI_TRUE : JNI_FALSE;
     }

     static void vibratorOn(JNIEnv *env, jobject clazz, jlong timeout_ms)
     {
        // ALOGI("vibratorOn/n");
        vibrator_on(timeout_ms);
     }

      static void vibratorOff(JNIEnv *env, jobject clazz)
      {
        // ALOGI("vibratorOff/n");
        vibrator_off();
      }

      static JNINativeMethod method_table[] = {
        { "vibratorExists", "()Z", (void*)vibratorExists },
        { "vibratorOn", "(J)V", (void*)vibratorOn },
        { "vibratorOff", "()V", (void*)vibratorOff }
     };

     int register_android_server_VibratorService(JNIEnv *env)
     {
        return jniRegisterNativeMethods(env, "com/android/server/VibratorService",
                method_table, NELEM(method_table));
      }

在jni中,可以知道最终调用的是 vibrator_off
这个vibrator_off 是在jni中实现的

OnLoad.cpp中添加jni文件

register_android_server_VibratorService(env);

hal

/hardware/libhardware/include/hardware/vibrator.h
/hardware/libhardware_legacy/vibrator/vibrator.c

在libhardware下的hal文件,形成的是libhardware.so这个动态库。

Context进行注册

    registerService(VIBRATOR_SERVICE, new ServiceFetcher() {
            public Object createService(ContextImpl ctx) {
                return new SystemVibrator(ctx);
            }});

应用程序的使用

vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);

其他

vibrator的java binder层面。在service部分,还有 binder的 死亡通知。
(还需要深入的理解)

总结

以上是从系统的角度来进行的硬件访问服务程序。

参考文献

Android震动vibrator系统开发全过程

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值