Showing posts with label ICS. Show all posts
Showing posts with label ICS. Show all posts

Thursday, December 01, 2011

Vibrator function support in Android

Android application connect to HAL by JNI interface, for vibrator, the JNI interface is defined very simple.


JNI: frameworks/base/services/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)
{
    // LOGI("vibratorOn\n");
    vibrator_on(timeout_ms);
}

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

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


vibratorOn, vibratorOff and vibratorExists just simple wrapper of vibrator_on, vibrator_off and vibrator_exists, which is provide by device maker, through HAL interface.

HAL: hardware/libhardware_legacy/vibrator/vibrator.c


#define THE_DEVICE "/sys/class/timed_output/vibrator/enable"


int vibrator_exists()
{
    int fd;


#ifdef QEMU_HARDWARE
    if (qemu_check()) {
        return 1;
    }
#endif


    fd = open(THE_DEVICE, O_RDWR);
    if(fd < 0)
        return 0;
    close(fd);
    return 1;
}



int vibrator_on(int timeout_ms)
{
    /* constant on, up to maximum allowed time */
    return sendit(timeout_ms);
}


int vibrator_off()
{
    return sendit(0);
}


HAL call into kernel space by sysfs, path at "/sys/class/timed_output/vibrator/enable"
It's a Android designed feature, a timed_output or timed_gpio device.

If you echo 10000 into this sysfs, then it will vibrate for ten seconds :


$ echo 10000 > /sys/class/timed_output/vibrator/enable 

If you cat it during the vibration, you will saw how many ms before it back to quiet :

$ cat /sys/class/timed_output/vibrator/enable
315


Inside kernel space, Android defined two new platform driver to be use for vibrator : timed_gpio and timed_output.

By grab HC kernel source that released by Acer/ASUS, found that they are using different of implementation.

Acer use a gpio pin, config with a timed_gpio driver, hook up with platform_device driver, and also to timed_output driver to provide vibrator interface from sysfs.

ASUS use an IC max1749, directly register a timed_output interface to provide vibrator interface from sysfs.

PS. Beside kernel source, above source code is trace from AOSP 4.0.1.