Android(安卓)振动器(Vibrator)系统
Android 振动器系统 是Android其他系统中相对简单的系统。振动器系统用来移动电话的震动功能。比如闹钟、振动模式都需要用到振动器系统。
平台 MTK6573
Android 振动器系统架构
1 驱动层
2 硬件抽象层
2 JNI框架层
3 Java应用层
一、驱动层
Android修改、新增Linux内核文件
/kernel/drivers/staging/android/timed_output.h
/kernel/drivers/staging/android/timed_output.c
timed_output.h 定义了timed_output_dev 结构体
struct timed_output_dev {const char*name;/* enable the output and set the timer */void(*enable)(struct timed_output_dev *sdev, int timeout);/* returns the current number of milliseconds remaining on the timer */int(*get_time)(struct timed_output_dev *sdev);/* private data */struct device*dev;intindex;intstate;};timed_output.c 实现了该结构体,使用函数timed_output_dev_register实现注册,使用timed_output_dev_unregister实现注销。
int timed_output_dev_register(struct timed_output_dev *tdev){int ret;if (!tdev || !tdev->name || !tdev->enable || !tdev->get_time)return -EINVAL;ret = create_timed_output_class();if (ret < 0)return ret;tdev->index = atomic_inc_return(&device_count);tdev->dev = device_create(timed_output_class, NULL,MKDEV(0, tdev->index), NULL, tdev->name);if (IS_ERR(tdev->dev))return PTR_ERR(tdev->dev);ret = device_create_file(tdev->dev, &dev_attr_enable);if (ret < 0)goto err_create_file;dev_set_drvdata(tdev->dev, tdev);tdev->state = 0;return 0;err_create_file:device_destroy(timed_output_class, MKDEV(0, tdev->index));printk(KERN_ERR "timed_output: Failed to register driver %s\n",tdev->name);return ret;}EXPORT_SYMBOL_GPL(timed_output_dev_register);void timed_output_dev_unregister(struct timed_output_dev *tdev){device_remove_file(tdev->dev, &dev_attr_enable);device_destroy(timed_output_class, MKDEV(0, tdev->index));dev_set_drvdata(tdev->dev, NULL);}EXPORT_SYMBOL_GPL(timed_output_dev_unregister);
驱动实现移植
以MTK 6573平台为例
./mediatek/platform/mt6573/kernel/drivers/vibrator/vibrator.c
操作设备
首先打开手机调试,连接USB,执行adb shell,进入/sys/devices/timed_output/vibrator/
执行echo "10000" enable发现手机在震动
# echo "10000" enableecho "10000" enable10000 enable
执行 cat enable 可以查看当前震动时间剩余数
# cat enablecat enable0
二、硬件抽象层
Android 封装了对底层驱动的调用,成为硬件抽象层。
/hardware/libhardware_legacy/vibrator/vibrator.c
int vibrator_on(int timeout_ms){ /* constant on, up to maximum allowed time */ return sendit(timeout_ms);}int vibrator_off(){ return sendit(0);}
提供了vibrato_on 和vibrator_off 两个函数用来操作振动器
static int sendit(int timeout_ms){ int nwr, ret, fd; char value[20]; fd = open(THE_DEVICE, O_RDWR); if(fd < 0) return errno; nwr = sprintf(value, "%d\n", timeout_ms); ret = write(fd, value, nwr); close(fd); return (ret == nwr) ? 0 : -1;}三、 JNI框架 层
Android JNI框架层是方便Java调用C/C++方法。
./frameworks/base/services/jni/com_android_server_VibratorService.cpp
namespace android { static void vibratorOn(JNIEnv *env, jobject clazz, jlong timeout_ms) { // LOGI("vibratorOn\n"); vibrator_on(timeout_ms); } static void vibratorOff(JNIEnv *env, jobject clazz) { // LOGI("vibratorOff\n"); vibrator_off(); } static JNINativeMethod method_table[] = { { "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)); }};四、Java应用层
这层包括Java 应用的调用,Android系统服务Java层
./frameworks/base/services/java/com/android/server/VibratorService.java
更多相关文章
- android设备adb usb驱动安装方法
- android电池(五):电池 充电IC(PM2301)驱动分析篇
- Android核心模块及相关技术
- Android(安卓)S5PV210 camera驱动测试程序
- android内核与驱动
- 谷歌Android被Linux内核除名(转)
- android内核字符驱动设备实战之----------内置C语言测试程序篇
- )Android核心模块及相关技术
- 2-8 Android(安卓)简单介绍 (一)