Search code examples
androidlinux-kernelaccelerometer

Access and use the values of the accelerometer in the Android kernel level


How can I access the accelerometer in the android kernel space and use it's data? I'm working on a project where I need to control some of the phone's functionality while the phone is moving (in a car) and I want to use the accelerometer to help determine if the phone had accelerated. It has to all be done in the kernel space. I found on the android developer pages how to use the accelerometer and the SensorEvent to detect changes in a user level application. But how to do something like that on the kernel level? Should I make a system call?

From looking around a bit and searching, I think the files are in /goldfish/drivers/hwmon/ but I'm not quite sure.


Solution

  • The accelerometer input, at the antive level, is taken from /dev/input/event devices. You can verify that using the toolbox's getevent(), which will show you the input events in real time as you tilt the phone (if memory serves, you need /dev/input/event3 but you can check this for yourself easily).

    The drivers for the accelerometer export this character device, which in turn the Android runtime (with the assistance of libhardware) later converts to the familiar SensorEvent. There is no kernel API for this. Your simplest solution would be to interface with the /dev/event directly. This could be done in two ways:

    1. User mode daemon listens on /dev/event, and also communicates with your kernel thread (e.g. by blocking on another character device which your kernel module exports, reverse system call, etc)

    2. not recommended but would work - opening the /dev/input/event from kernel mode (using a direct call to sys_open and then calls to sys_read). This would require "working around" kernel protections (most notably KERNEL_DS being set).