Explanation of Android Vibrator System

Source: Internet
Author: User

Platform MTK6573

Android Vibrator System Architecture

I. Driver Layer

Modify and add Linux kernel files for Android

/kernel/drivers/staging/android/timed_output.h/kernel/drivers/staging/android/timed_output.c

Timed_output.h defines the timed_output_dev struct.

 
 
  1. struct timed_output_dev { 
  2.     const char  *name; 
  3.  
  4.     /* enable the output and set the timer */ 
  5.     void    (*enable)(struct timed_output_dev *sdev, int timeout); 
  6.  
  7.     /* returns the current number of milliseconds remaining on the timer */ 
  8.     int     (*get_time)(struct timed_output_dev *sdev); 
  9.  
  10.     /* private data */ 
  11.     struct device   *dev; 
  12.     int     index; 
  13.     int     state; 
  14. }; 

Timed_output.c implements this struct. The timed_output_dev_register function is used for registration and timed_output_dev_unregister is used for cancellation.

 
 
  1. int timed_output_dev_register(struct timed_output_dev *tdev) 
  2.     int ret; 
  3.  
  4.     if (!tdev || !tdev->name || !tdev->enable || !tdev->get_time) 
  5.         return -EINVAL; 
  6.  
  7.     ret = create_timed_output_class(); 
  8.     if (ret < 0) 
  9.         return ret; 
  10.  
  11.     tdev->index = atomic_inc_return(&device_count); 
  12.     tdev->dev = device_create(timed_output_class, NULL, 
  13.         MKDEV(0, tdev->index), NULL, tdev->name); 
  14.     if (IS_ERR(tdev->dev)) 
  15.         return PTR_ERR(tdev->dev); 
  16.  
  17.     ret = device_create_file(tdev->dev, &dev_attr_enable); 
  18.     if (ret < 0) 
  19.         goto err_create_file; 
  20.  
  21.     dev_set_drvdata(tdev->dev, tdev); 
  22.     tdev->state = 0; 
  23.     return 0; 
  24.  
  25. err_create_file: 
  26.     device_destroy(timed_output_class, MKDEV(0, tdev->index)); 
  27.     printk(KERN_ERR "timed_output: Failed to register driver %s\n", 
  28.             tdev->name); 
  29.  
  30.     return ret; 
  31. EXPORT_SYMBOL_GPL(timed_output_dev_register); 
  32.  
  33. void timed_output_dev_unregister(struct timed_output_dev *tdev) 
  34.     device_remove_file(tdev->dev, &dev_attr_enable); 
  35.     device_destroy(timed_output_class, MKDEV(0, tdev->index)); 
  36.     dev_set_drvdata(tdev->dev, NULL); 
  37. EXPORT_SYMBOL_GPL(timed_output_dev_unregister); 

Driver implementation porting

Take MTK 6573 as an Example

./Mediatek/platform/mt6573/kernel/drivers/vibrator. c

Operation Device

First open the mobile phone debugging, connect to the USB, execute the adb shell, and enter/sys/devices/timed_output/vibrator/

Execute echo "10000" enable to find that the mobile phone is shaking

 
 
  1. # echo "10000" enable 
  2. echo "10000" enable 
  3. 10000 enable 

Run cat enable to view the remaining number of current vibration time:

 
 
  1. # cat enable 
  2. cat enable 

Ii. Hardware Abstraction Layer

Android encapsulates calls to the underlying driver and becomes the hardware abstraction layer.

/Hardware/libhardware_legacy/vibrator. c

 
 
  1. int vibrator_on(int timeout_ms) 
  2.     /* constant on, up to maximum allowed time */ 
  3.     return sendit(timeout_ms); 
  4.  
  5. int vibrator_off() 
  6.     return sendit(0); 

Iii. JNI framework layer

The Android JNI framework layer facilitates Java to call the C/C ++ method.

./Frameworks/base/services/jni/com_android_server_VibratorService.cpp

 
 
  1. namespace android 
  2.  { 
  3.   
  4.  static void vibratorOn(JNIEnv *env, jobject clazz, jlong timeout_ms) 
  5.  { 
  6.      // LOGI("vibratorOn\n"); 
  7.      vibrator_on(timeout_ms); 
  8.  } 
  9.  static void vibratorOff(JNIEnv *env, jobject clazz) 
  10.  { 
  11.      // LOGI("vibratorOff\n"); 
  12.      vibrator_off(); 
  13.  } 
  14.  static JNINativeMethod method_table[] = { 
  15.      { "vibratorOn", "(J)V", (void*)vibratorOn }, 
  16.      { "vibratorOff", "()V", (void*)vibratorOff } 
  17.  }; 
  18.  int register_android_server_VibratorService(JNIEnv *env) 
  19.  { 
  20.      return jniRegisterNativeMethods(env, "com/android/server/VibratorService", 
  21.              method_table, NELEM(method_table)); 
  22.  } 
  23. }; 

Iv. Java Application Layer

This layer includes Java application calls and Android system service Java Layer

./Frameworks/base/services/java/com/android/server/VibratorService. java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.