1. vibrator system structure and porting content
The vibrator is responsible for controlling the vibration function of the reference phone. The Vibrator System in Android is a small system dedicated to this function and provides the vibration function based on time.
The vibrator system includes the driver, hardware abstraction layer, JNI, and java framework. It also provides a simple API to the java application layer as a platform interface.
2. Structure of vibrator part
The Android vibrator system consists of the driver, the hardware abstraction layer of the vibrator system, the java framework of the vibrator system, and the use of the vibrator system in the java framework.
From the bottom up, the Android vibrator system is divided into the following parts.
1. DRIVER: the driver of the vibrator on a specific hardware platform, which is usually implemented based on the Android Timed Output driver framework.
2. Hardware Abstraction Layer: The Hardware Abstraction Layer of the vibrator system has a default implementation in Android. Code path:
Hardware/libhardwar_legacy/vibrator. c
The Hardware Abstraction Layer of the vibrator is usually not re-implemented and is part of libhardware_legacy.so.
3. JNI Section
This class is the JNI part of the vibrator and provides interfaces up by calling the hardware abstraction layer.
4. VibratorService. Java calls VibratorService JNI to implement the VibrattorService class in the com. android. server package. This class is not a platform API. It is called by a small part of the Android Java framework. Vibrator. java file to implement the Vibrator class in the android. OS package. This is the API provided to the java layer.
Iii. Porting Methods
There are two ways to port the vibrator system to a specific hardware platform.
Method 1: Because the hardware abstraction layer is already in place, only the driver program is required for porting the vibrator system. This driver must be provided to the Timed Output driver framework in the Android kernel.
Method 2: Re-implement the hardware abstraction layer interface of the Vibrator Based on the self-implemented driver. Because the interface of the hardware abstraction layer of the vibrator is very simple, this implementation method will not be very complicated.
Iv. Key Points of transplantation and debugging
1. Driver
Vibrator drivers only need to implement the vibration interface, which is an output device and must accept the vibration time as a parameter. Because it is relatively simple, the Vibrator driver can be implemented in multiple ways.
In Android, we recommend that you define the TimedOutput driver framework based on the Android kernel to implement the Vibrator driver.
Timed Output refers to scheduled Output, which is used to periodically send an Output. In fact, the driver is still based on the sys File System.
The timed_output_dev struct is defined in the Driver/staging/android/directory timed_output.h, which contains the enable and get_time function pointers. After the struct is implemented, timed_output_dev_register () and merge () are used () function registration and logout.
The Timed Output driver framework creates a subdirectory for each device in the/sys/class/timed_output/directory. The enable file in the subdirectory is the control file of the device. Reading the enable file indicates obtaining the remaining time. Writing this file indicates time-based vibration.
Debug the Timed Output driver through the sys File System.
For a Vibrator device, the name of the implemented TimedOutput driver should be "vibrator ". Therefore, the method of Vibrator in sys file system is as follows:
# Echo "10000">/sys/class/timed_output/vibrator/enable
# Cat/sys/class/timed_output/vibrator/enable
3290
# Echo "0">/sys/class/timed_output/vibrator/enable
For an enable file, "write" indicates the time specified for enabling, and "read" indicates obtaining the remaining time.
2. Hardware Abstraction Layer Interface
The Vibrator hardware abstraction layer interface is defined in the vibrator. h file of the hardware/libhardware_legacy/include/hardware_legacy/directory:
Int vibrator_on (int timeout_ms );
Int vibrator_off ();
The vibrator. h file defines two interfaces, indicating vibration and shutdown respectively. The vibration starts in milliseconds as the time unit.
3. Implementation of the standard hardware abstraction layer
The Vibrator hardware abstraction layer has standard implementations. In the vibrator. c directory of the hardware/libhardware_legacy/vibrator/directory.
The core content of the implementation is the sendit () function. The content of this function is as follows:
[Java] <span style = "font-size: 16px;"> # define THE_DEVICE "/sys/class/timed_output/vibrator/enable"
Static int sendit (int timeout_ms)
{
Int nwr, ret, fd;
Char value [20];
Ifdef QEMU_HARDWARE
If (qemu_check ()){
Return qemu_control_command ("vibrator: % d, timeout_ms );
}
# Endif
Fd = open (THE_DEVICE, O_RDWR );
If (fd <0) return errno;
Nwr = sprint (value, "% d \ n", timeout_ms );
Ret = write (fd, value, nwr );
Close (fd );
Return (rnt = nwr )? 0:-1;
}
</Span>
<Span style = "font-size: 16px;"> # define THE_DEVICE "/sys/class/timed_output/vibrator/enable"
Static int sendit (int timeout_ms)
{
Int nwr, ret, fd;
Char value [20];
Ifdef QEMU_HARDWARE
If (qemu_check ()){
Return qemu_control_command ("vibrator: % d, timeout_ms );
}
# Endif
Fd = open (THE_DEVICE, O_RDWR );
If (fd <0) return errno;
Nwr = sprint (value, "% d \ n", timeout_ms );
Ret = write (fd, value, nwr );
Close (fd );
Return (rnt = nwr )? 0:-1;
}
</Span>
The Sendit () function is responsible for time-based "vibration". In real hardware, it controls the file through the sys File System. In the simulator environment, it sends commands through QEMU.
Vibrator_on () calls sendit () with time as the parameter, and vibrator_on () calls sendit () with 0 as the parameter.
From Peking University-Google Android lab