驱动程序
Vibrator的驱动程序只需要实现振动的接口即可,这是一个输出设备,需要接受振动时间作为参数。由于比较简单,因此Vibrator的驱动程序可以使用多种方式来实现。
在Android中,推荐基于Android内核定义Timed Output驱动程序框架来实现Vibrator的驱动程序。
Timed Output的含义为定时输出,用于定时发出某个输出。实际上,这种驱动程序依然是基于sys文件系统来完成的。
drivers/staging/android/目录timed_output.h中定义timed_output_dev结构体,其中包含enable和get_time这两个函数指针,实现结构体后,使用timed_output_dev_register()和timed_output_dev_unregister()函数注册和注销即可。
Timed Output驱动程序框架将为每个设备在/sys/class/timed_output/目录中建立一个子目录,设备子目录中的enable文件就是设备的控制文件。读enable文件表示获得剩余时间,写这个文件表示根据时间振动。
Timed Output驱动的设备调试,通过sys文件系统即可。
对于Vibrator设备,其实现的Timed Output驱动程序的名称应该为“vibrator”。因此Vibrator设备在sys文件系统中的方法如下所示:
# cat /sys/class/timed_output/vibrator/enable
3290
# echo "0" > /sys/class/timed_output/vibrator/enable
对于enable文件,“写”表示使能指定的时间,“读”表示获取剩余时间。
硬件抽象层的内容
1.硬件抽象层的接口
Vibrator硬件抽象层的接口在hardware/libhardware_legacy/include/hardware_legacy/目录的vibrator.h文件中定义:
int vibrator_on(int timeout_ms); // 开始振动
int vibrator_off(); // 关闭振动
vibrator.h文件中定义两个接口,分别表示振动和关闭,振动开始以毫秒(ms)作为时间单位。
提示:Timed Output类型驱动本身有获得剩余时间的能力(读enable文件),但是在Android Vibrator硬件抽象层以上的各层接口都没有使用这个功能。
2.标准硬件抽象层的实现
Vibrator硬件抽象层具有标准的实现,在hardware/libhardware_legacy/vibrator/目录的vibrator.c中。
其中实现的核心内容为sendit()函数,这个函数的内容如下所示:
static int sendit(int timeout_ms)
{
int nwr, ret, fd;
char value[20];
#ifdef QEMU_HARDWARE // 使用QEMU的情况
if (qemu_check()) {
return qemu_control_command( "vibrator:%d", timeout_ms );
}
#endif
fd = open(THE_DEVICE, O_RDWR); // 读取sys文件系统中的内容
if(fd < 0) return errno;
nwr = sprintf(value, "%d\n", timeout_ms);
ret = write(fd, value, nwr);
close(fd);
return (ret == nwr) ? 0 : -1;
}
sendit()函数负责根据时间“振动”:在真实的硬件中,通过sys文件系统的文件进行控制;如果是模拟器环境则通过QEMU发送命令。
vibrator_on()调用sendit()以时间作为参数,vibrator_on()调用sendit()以0作为参数。
上层的情况和注意事项
frameworks/base/services/jni/目录中的com_android_server_VibratorService.cpp文件是Vibrator硬件抽象层的调用者,它同时也向Java提供JNI支持。
其中,为JNI定义的方法列表如下所示:
{ "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));
}
vibratorOn()和vibratorOff()这两个函数的实现分别如下所示:
vibrator_on(timeout_ms);
}
static void vibratorOff(JNIEnv *env, jobject clazz){
vibrator_off();
}
frameworks/base/services/java/com/android/server/目录中的VibratorService.java通过调用VibratorService JNI来实现com.android.server包中的VibratorService类。
frameworks/base/core/java/android/os/目录中的Vibrator.java文件实现了android.os包中的Vibrator类。它通过调用vibrator的Java服务来实现(获得名称为vibrator的服务),配合同目录中的IVibratorService.aidl文件向应用程序层提供Vibrator的相关API。