README.md
1# Dynamic Sensors
2
3[TOC]
4
5## Links
6
7* [Sensor HAL dynamic sensor support](https://source.android.com/devices/sensors/sensors-hal2#dynamic-sensors)
8* [Sensors Multi-HAL](https://source.android.com/devices/sensors/sensors-multihal)
9
10## Adding dynamic sensor support to a device
11
12A few files need to be modified to add dynamic sensor support to a device. The
13dynamic sensor HAL must be enabled in the device product makefile and in the
14sensor sub-HAL configuration file, support for raw HID devices must be configured
15in the Linux kernel (`CONFIG_HIDRAW=y`), and SELinux policy files must be updated
16to provide the necessary permissions. Example changes are provided below.
17
18```shell
19acme-co$ git -C device/acme/rocket-phone diff
20diff --git a/sensor_hal/hals.conf b/sensor_hal/hals.conf
21index a1f4b8b..d112546 100644
22--- a/sensor_hal/hals.conf
23+++ b/sensor_hal/hals.conf
24@@ -1 +1,2 @@
25+sensors.dynamic_sensor_hal.so
26 sensors.rocket-phone.so
27diff --git a/rocket-phone.mk b/rocket-phone.mk
28index 3fc8538..b1bd8a1 100644
29--- a/rocket-phone.mk
30+++ b/rocket-phone.mk
31@@ -73,6 +73,9 @@
32 PRODUCT_PACKAGES += sensors.rocket-phone
33 PRODUCT_PACKAGES += thruster_stats
34
35+# Add the dynamic sensor HAL.
36+PRODUCT_PACKAGES += sensors.dynamic_sensor_hal
37+
38 # Only install test tools in debug build or eng build.
39 ifneq ($(filter userdebug eng,$(TARGET_BUILD_VARIANT)),)
40 PRODUCT_PACKAGES += thruster_test
41diff --git a/conf/ueventd.rc b/conf/ueventd.rc
42index 88ee00b..2f03009 100644
43--- a/conf/ueventd.rc
44+++ b/conf/ueventd.rc
45@@ -209,3 +209,7 @@
46
47 # Thrusters
48 /dev/thruster* 0600 system system
49+
50+# Raw HID devices
51+/dev/hidraw* 0660 system system
52+
53diff --git a/sepolicy/sensor_hal.te b/sepolicy/sensor_hal.te
54index 0797253..22a4208 100644
55--- a/sepolicy/sensor_hal.te
56+++ b/sepolicy/sensor_hal.te
57@@ -52,6 +52,9 @@
58 # Allow sensor HAL to read thruster state.
59 allow hal_sensors_default thruster_state:file r_file_perms;
60
61+# Allow access for dynamic sensor properties.
62+get_prop(hal_sensors_default, vendor_dynamic_sensor_prop)
63+
64+# Allow access to raw HID devices for dynamic sensors.
65+allow hal_sensors_default device:dir r_dir_perms;
66+allow hal_sensors_default hidraw_device:chr_file rw_file_perms;
67+
68 #
69 # Thruster sensor enforcements.
70 #
71diff --git a/sepolicy/device.te b/sepolicy/device.te
72index bc3c947..bad0be0 100644
73--- a/sepolicy/device.te
74+++ b/sepolicy/device.te
75@@ -55,3 +55,7 @@
76
77 # Thruster
78 type thruster_device, dev_type;
79+
80+# Raw HID device
81+type hidraw_device, dev_type;
82+
83diff --git a/sepolicy/property.te b/sepolicy/property.te
84index 4b671a4..bb0894f 100644
85--- a/sepolicy/property.te
86+++ b/sepolicy/property.te
87@@ -49,3 +49,7 @@
88
89 # Thruster
90 vendor_internal_prop(vendor_thruster_debug_prop)
91+
92+# Dynamic sensor
93+vendor_internal_prop(vendor_dynamic_sensor_prop)
94+
95diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
96index bc03a78..ff401dc 100644
97--- a/sepolicy/file_contexts
98+++ b/sepolicy/file_contexts
99@@ -441,3 +441,7 @@
100 /dev/thruster-fuel u:object_r:thruster_device:s0
101 /dev/thruster-output u:object_r:thruster_device:s0
102 /dev/thruster-telemetry u:object_r:thruster_device:s0
103+
104+# Raw HID device
105+/dev/hidraw[0-9]* u:object_r:hidraw_device:s0
106+
107diff --git a/sepolicy/property_contexts b/sepolicy/property_contexts
108index 5d2f018..18a6059 100644
109--- a/sepolicy/property_contexts
110+++ b/sepolicy/property_contexts
111@@ -104,3 +104,7 @@
112
113 # Thruster
114 vendor.thruster.debug u:object_r:vendor_thruster_debug_prop:s0
115+
116+# Dynamic sensor
117+vendor.dynamic_sensor. u:object_r:vendor_dynamic_sensor_prop:s0
118+
119acme-co$
120```
121
122Once the file modifications are made, rebuild and flash. The dynamic sensor HAL
123should be initialized and appear in the sensor service.
124
125```shell
126acme-co$ build_and_flash_android
127...
128acme-co$ adb logcat -d | grep DynamicSensorHal
12912-15 18:18:45.735 791 791 D DynamicSensorHal: DynamicSensorsSubHal::getSensorsList_2_1 invoked.
13012-15 18:18:47.474 791 791 D DynamicSensorHal: DynamicSensorsSubHal::initialize invoked.
131acme-co$ adb shell dumpsys sensorservice | grep Dynamic
1320000000000) Dynamic Sensor Manager | Google | ver: 1 | type: android.sensor.dynamic_sensor_meta(32) | perm: n/a | flags: 0x00000007
133Dynamic Sensor Manager (handle=0x00000000, connections=1)
134 Dynamic Sensor Manager 0x00000000 | status: active | pending flush events 0
135```
136
137When a dynamic sensor is paired with the device (e.g., Bluetooth rocket buds),
138it will appear in the sensor service.
139
140```shell
141acme-co$ adb logcat -d | grep "DynamicSensorHal\|hidraw\|Rocket"
14212-15 18:19:55.268 157 157 I hid-generic 0003: 1234:5678.0001: hidraw0: BLUETOOTH HID v0.00 Device [RocketBuds] on
14312-15 18:19:55.235 791 809 E DynamicSensorHal: return 1 sensors
14412-15 18:19:56.239 1629 1787 I SensorService: Dynamic sensor handle 0x1 connected, type 37, name RocketBuds
145acme-co$ adb shell dumpsys sensorservice | grep head_tracker
1460x00000001) RocketBuds | BLUETOOTH 1234:5678 | ver: 1 | type: android.sensor.head_tracker(37) | perm: n/a | flags: 0x00000020
147acme-co$
148```
149
150