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 18Note: as of Android 15, the `hidraw_device` SELinux label is provided by the 19system. 20 21```shell 22acme-co$ git -C device/acme/rocket-phone diff 23diff --git a/sensor_hal/hals.conf b/sensor_hal/hals.conf 24index a1f4b8b..d112546 100644 25--- a/sensor_hal/hals.conf 26+++ b/sensor_hal/hals.conf 27@@ -1 +1,2 @@ 28+sensors.dynamic_sensor_hal.so 29 sensors.rocket-phone.so 30diff --git a/rocket-phone.mk b/rocket-phone.mk 31index 3fc8538..b1bd8a1 100644 32--- a/rocket-phone.mk 33+++ b/rocket-phone.mk 34@@ -73,6 +73,9 @@ 35 PRODUCT_PACKAGES += sensors.rocket-phone 36 PRODUCT_PACKAGES += thruster_stats 37 38+# Add the dynamic sensor HAL. 39+PRODUCT_PACKAGES += sensors.dynamic_sensor_hal 40+ 41 # Only install test tools in debug build or eng build. 42 ifneq ($(filter userdebug eng,$(TARGET_BUILD_VARIANT)),) 43 PRODUCT_PACKAGES += thruster_test 44diff --git a/conf/ueventd.rc b/conf/ueventd.rc 45index 88ee00b..2f03009 100644 46--- a/conf/ueventd.rc 47+++ b/conf/ueventd.rc 48@@ -209,3 +209,7 @@ 49 50 # Thrusters 51 /dev/thruster* 0600 system system 52+ 53+# Raw HID devices 54+/dev/hidraw* 0660 system system 55+ 56diff --git a/sepolicy/sensor_hal.te b/sepolicy/sensor_hal.te 57index 0797253..22a4208 100644 58--- a/sepolicy/sensor_hal.te 59+++ b/sepolicy/sensor_hal.te 60@@ -52,6 +52,9 @@ 61 # Allow sensor HAL to read thruster state. 62 allow hal_sensors_default thruster_state:file r_file_perms; 63 64+# Allow access for dynamic sensor properties. 65+get_prop(hal_sensors_default, vendor_dynamic_sensor_prop) 66+ 67+# Allow access to raw HID devices for dynamic sensors. 68+allow hal_sensors_default device:dir r_dir_perms; 69+allow hal_sensors_default hidraw_device:chr_file rw_file_perms; 70+ 71 # 72 # Thruster sensor enforcements. 73 # 74diff --git a/sepolicy/property.te b/sepolicy/property.te 75index 4b671a4..bb0894f 100644 76--- a/sepolicy/property.te 77+++ b/sepolicy/property.te 78@@ -49,3 +49,7 @@ 79 80 # Thruster 81 vendor_internal_prop(vendor_thruster_debug_prop) 82+ 83+# Dynamic sensor 84+vendor_internal_prop(vendor_dynamic_sensor_prop) 85+ 86diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts 87index bc03a78..ff401dc 100644 88--- a/sepolicy/file_contexts 89+++ b/sepolicy/file_contexts 90@@ -441,3 +441,7 @@ 91 /dev/thruster-fuel u:object_r:thruster_device:s0 92 /dev/thruster-output u:object_r:thruster_device:s0 93 /dev/thruster-telemetry u:object_r:thruster_device:s0 94+ 95+# Raw HID device 96+/dev/hidraw[0-9]* u:object_r:hidraw_device:s0 97+ 98diff --git a/sepolicy/property_contexts b/sepolicy/property_contexts 99index 5d2f018..18a6059 100644 100--- a/sepolicy/property_contexts 101+++ b/sepolicy/property_contexts 102@@ -104,3 +104,7 @@ 103 104 # Thruster 105 vendor.thruster.debug u:object_r:vendor_thruster_debug_prop:s0 106+ 107+# Dynamic sensor 108+vendor.dynamic_sensor. u:object_r:vendor_dynamic_sensor_prop:s0 109+ 110acme-co$ 111``` 112 113Once the file modifications are made, rebuild and flash. The dynamic sensor HAL 114should be initialized and appear in the sensor service. 115 116```shell 117acme-co$ build_and_flash_android 118... 119acme-co$ adb logcat -d | grep DynamicSensorHal 12012-15 18:18:45.735 791 791 D DynamicSensorHal: DynamicSensorsSubHal::getSensorsList_2_1 invoked. 12112-15 18:18:47.474 791 791 D DynamicSensorHal: DynamicSensorsSubHal::initialize invoked. 122acme-co$ adb shell dumpsys sensorservice | grep Dynamic 1230000000000) Dynamic Sensor Manager | Google | ver: 1 | type: android.sensor.dynamic_sensor_meta(32) | perm: n/a | flags: 0x00000007 124Dynamic Sensor Manager (handle=0x00000000, connections=1) 125 Dynamic Sensor Manager 0x00000000 | status: active | pending flush events 0 126``` 127 128When a dynamic sensor is paired with the device (e.g., Bluetooth rocket buds), 129it will appear in the sensor service. 130 131```shell 132acme-co$ adb logcat -d | grep "DynamicSensorHal\|hidraw\|Rocket" 13312-15 18:19:55.268 157 157 I hid-generic 0003: 1234:5678.0001: hidraw0: BLUETOOTH HID v0.00 Device [RocketBuds] on 13412-15 18:19:55.235 791 809 E DynamicSensorHal: return 1 sensors 13512-15 18:19:56.239 1629 1787 I SensorService: Dynamic sensor handle 0x1 connected, type 37, name RocketBuds 136acme-co$ adb shell dumpsys sensorservice | grep head_tracker 1370x00000001) RocketBuds | BLUETOOTH 1234:5678 | ver: 1 | type: android.sensor.head_tracker(37) | perm: n/a | flags: 0x00000020 138acme-co$ 139``` 140 141