本文是参考以下大神文档后根据自己调试的方案整理一下,想要详细了解sensor工作流程的可以点击以下链接:
https://blog.csdn.net/goodnight1994/article/details/97503586
从大神的文章可以了解到,sensor流程是从APP --> framework --> hal
1、从framework层的Java调用Jni接口时,执行如下代码,代码路径:frameworks/base/services/java/com/android/server/SystemServer.java
private void startBootstrapServices() {
Slog.i(TAG, "Reading configuration...");
...skip...
// The sensor service needs access to package manager service, app ops
// service, and permissions service, therefore we start it after them.
// Start sensor service in a separate thread. Completion should be checked
// before using it.
mSensorServiceStart = SystemServerInitThreadPool.get().submit(() -> {
TimingsTraceLog traceLog = new TimingsTraceLog(
SYSTEM_SERVER_TIMING_ASYNC_TAG, Trace.TRACE_TAG_SYSTEM_SERVER);
traceLog.traceBegin(START_SENSOR_SERVICE);
startSensorService();
traceLog.traceEnd();
}, START_SENSOR_SERVICE);
}
2、startSensorService();为本地接口,代码路径:frameworks/base/services/core/jni/com_android_server_SystemServer.cpp
static void android_server_SystemServer_startSensorService(JNIEnv* /* env */, jobject /* clazz */) {
char propBuf[PROPERTY_VALUE_MAX];
property_get("system_init.startsensorservice", propBuf, "1");
if (strcmp(propBuf, "1") == 0) {
SensorService::instantiate();
}
}
由上面可知,system_init.startsensorservice这个属性为1时才会调用SensorService::instantiate()函数创建的sensorservice实例,搜索了整份代码也没有设置system_init.startsensorservice这个属性的值,我直接把它写到buildinfo.sh,编译代码时直接写入build.prop,如下:
diff --git a/build/make/tools/buildinfo.sh b/build/make/tools/buildinfo.sh
index 6c5dba979c..c645132993 100755
--- a/build/make/tools/buildinfo.sh
+++ b/build/make/tools/buildinfo.sh
@@ -27,6 +27,7 @@ fi
if [ -n "$AB_OTA_UPDATER" ] ; then
echo "ro.build.ab_update=$AB_OTA_UPDATER"
fi
+echo "system_init.startsensorservice=1"
3、拷贝sensors.default.so到板卡的vendor/lib/hw/路径,权限直接赋644
console:/ # chmod 644 vendor/lib/hw/sensors.default.so
console:/ # ls -l vendor/lib/hw/sensors.default.so
-rw-r--r-- 1 root root 100580 2020-11-07 10:50 vendor/lib/hw/sensors.default.so
console:/ #
可以正常创建sensorservice实例,不过找不到Sensors接口:[email protected]::ISensors/default,这个接口是在manifest.xml中定义的,但是在板卡的vendor/manifest.xml中找不到对应的接口,需要在代码中定义,如下:
diff --git a/device/mediatek/m5621/manifest.xml b/device/mediatek/m5621/manifest.xml
index d986412116..e3c482deed 100755
--- a/device/mediatek/m5621/manifest.xml
+++ b/device/mediatek/m5621/manifest.xml
@@ -439,6 +439,15 @@
default
+
+ android.hardware.sensors
+ hwbinder
+ 1.0
+
+ ISensors
+ default
+
+
26.0
经过一番检查后,发现MTK方案的sensor流程有很多缺陷,system_init.startsensorservice属性没有设置、ISensors接口没有定义、sensors依赖库及服务也没有预置,te权限也不够完善(蓝牙空鼠是通过/dev/uhid传输数据的)
diff --git a/device/mediatek/m5621/device_fusion.mk b/device/mediatek/m5621/device_fusion.mk
index c82cc61e48..fc5b502c6b 100755
--- a/device/mediatek/m5621/device_fusion.mk
+++ b/device/mediatek/m5621/device_fusion.mk
@@ -568,3 +568,15 @@ PRODUCT_SOONG_NAMESPACES += vendor/mediatek/proprietary_tv/open/custom/$(BRAND)/
# Add for Smoke Test
PRODUCT_PROPERTY_OVERRIDES += \
ro.com.google.clientidbase=android-m5621-tv
+
+# Sensors HAL
+PRODUCT_PACKAGES += \
+ [email protected] \
+ [email protected] \
+ [email protected]
+
+PRODUCT_COPY_FILES += \
+ device/mediatek/m5621/sensor/sensors.default.so:vendor/lib/hw/sensors.default.so
diff --git a/device/mediatek/m5621/sensor/sensors.default.so b/device/mediatek/m5621/sensor/sensors.default.so
new file mode 100755
index 0000000000..f852793575
Binary files /dev/null and b/device/mediatek/m5621/sensor/sensors.default.so differ
diff --git a/system/core/rootdir/ueventd.rc b/system/core/rootdir/ueventd.rc
index 38f3d6b9b8..76d4e8f053 100755
--- a/system/core/rootdir/ueventd.rc
+++ b/system/core/rootdir/ueventd.rc
@@ -69,7 +69,7 @@ subsystem sound
/dev/diag 0660 radio radio
/dev/diag_arm9 0660 radio radio
/dev/ttyMSM0 0600 bluetooth bluetooth
-/dev/uhid 0660 uhid uhid
+/dev/uhid 0777 system uhid
/dev/uinput 0660 system bluetooth
/dev/alarm 0664 system radio
/dev/rtc0 0640 system system
diff --git a/system/sepolicy/vendor/hal_sensors_default.te b/system/sepolicy/vendor/hal_sensors_default.te
old mode 100644
new mode 100755
index 1bde858cd0..2a81acd8ec
--- a/system/sepolicy/vendor/hal_sensors_default.te
+++ b/system/sepolicy/vendor/hal_sensors_default.te
@@ -14,3 +14,6 @@ allow hal_sensors_default ion_device:chr_file r_file_perms;
# allow sensor hal to use lock for keeping system awake for wake up
# events delivery.
wakelock_use(hal_sensors_default);
+
+allow hal_sensors_default sysfs:dir { read open };
+allow hal_sensors_default uhid_device:chr_file { read write open };
+allow hal_sensors_default bluetooth_device:chr_file { read write open ioctl };
diff --git a/vendor/toptech/sepolicy/device.te b/vendor/toptech/sepolicy/device.te
index 36e2c8e2ab..d4242a8505 100755
--- a/vendor/toptech/sepolicy/device.te
+++ b/vendor/toptech/sepolicy/device.te
@@ -1,3 +1,5 @@
type topupgrade_block_device, dev_type;
type rtk_bt_device, dev_type;
+
+type hidraw_audio_device, dev_type;
diff --git a/vendor/toptech/sepolicy/file_contexts b/vendor/toptech/sepolicy/file_contexts
index 4759ddd543..f70074c9b6 100755
--- a/vendor/toptech/sepolicy/file_contexts
+++ b/vendor/toptech/sepolicy/file_contexts
@@ -11,4 +11,5 @@
/dev/rtkbt_dev u:object_r:rtk_bt_device:s0
# /data
/data/vendor/bluetooth(/.*)? u:object_r:bluetooth_vendor_data_file:s0
+/dev/hidraw[0-9]* u:object_r:hidraw_audio_device:s0
注意:修改system/core/rootdir/ueventd.rc文件时需要注意一下,MTKrelease code时这个文件有点问题,里面的/*直接把后面的代码屏蔽了,导致后面的权限没有作用
diff --git a/system/core/rootdir/ueventd.rc b/system/core/rootdir/ueventd.rc
old mode 100644
new mode 100755
index b03d83bf1b..38f3d6b9b8
--- a/system/core/rootdir/ueventd.rc
+++ b/system/core/rootdir/ueventd.rc
@@ -63,7 +63,7 @@ subsystem sound
/dev/kgsl 0666 root root
# kms driver for drm based gpu
-/dev/dri/* 0666 root graphics
+/dev/dri* 0666 root graphics
# these should not be world writable
/dev/diag 0660 radio radio
@@ -74,20 +74,20 @@ subsystem sound
/dev/alarm 0664 system radio
/dev/rtc0 0640 system system
/dev/tty0 0660 root system
-/dev/graphics/* 0660 root graphics
+/dev/graphics* 0660 root graphics
/dev/msm_hw3dm 0660 system graphics
-/dev/input/* 0660 root input
+/dev/input* 0660 root input
编译完软件,蓝牙空鼠功能运行结果如下:
console:/ # logcat -G 20M;logcat | grep -E "Sensor|sensor"
11-11 07:33:23.302 2633 2651 I sensor_apps: 11-11 07:33:23.301 Nano_DevsEvent(329): list { 'add', 'hidraw0' }
11-11 07:33:23.303 2633 2648 I sensor_apps: 11-11 07:33:23.303 Nano_HidrawOpen(1082): Raw Name: TV RC
11-11 07:33:23.303 2633 2648 I sensor_apps: 11-11 07:33:23.303 Nano_HidrawOpen(1083): Raw Phys:
11-11 07:33:23.303 2633 2648 I sensor_apps: 11-11 07:33:23.303 Nano_HidrawOpen(1084): Report Descriptor Size: 145
11-11 07:33:23.303 2633 2648 I sensor_apps: 11-11 07:33:23.303 Nano_HidrawOpen(1085): Report Descriptor:
11-11 07:33:23.307 2633 2648 I sensor_apps: 11-11 07:33:23.307 Nano_HidrawOpen(1091): 05 01 09 06 a1 01 85 01 05 07 19 e0 29 e7 15 00 25 01 75 01 95 08 81 02 75 08 95 01 81 01 05 08 19 01 29 05 75 01 95 05 91 02 75 03 95 01 91 03 05 07 19 00 29 ff 15 00 25 ff 75 08 95 06 81 00 c0 06 00 ff 09 01 a1 01 a1 02 85 5d 09 00 15 00 26 ff 00 75 08 95 14 81 22 c0 a1 02 85 5b 09 02 15 00 26 ff
11-11 07:33:23.307 2633 2648 I sensor_apps: 11-11 07:33:23.307 Nano_HidrawOpen(1092): Raw Info:
11-11 07:33:23.307 2633 2648 I sensor_apps: 11-11 07:33:23.307 Nano_HidrawOpen(1093): bustype: 5
11-11 07:33:23.307 2633 2648 I sensor_apps: 11-11 07:33:23.307 Nano_HidrawOpen(1094): vendor: 0x0417
11-11 07:33:23.307 2633 2648 I sensor_apps: 11-11 07:33:23.307 Nano_HidrawOpen(1095): product: 0x0001
11-11 07:33:23.311 2633 4788 I sensor_apps: 11-11 07:33:23.311 uhid_create(124): Open uhid-cdev
11-11 07:33:23.311 2633 4788 I sensor_apps: 11-11 07:33:23.311 uhid_create(133): Create uhid device success
11-11 07:33:23.311 2633 2648 I sensor_apps: 11-11 07:33:23.311 Nano_CursorOpen(290): Create cursor thread
11-11 07:33:23.311 2633 2648 I sensor_apps: 11-11 07:33:23.311 Nano_HidrawActive(1177): [RC0] Active 12
11-11 07:33:23.312 2633 4788 I sensor_apps: 11-11 07:33:23.312 Nano_CursorProcess(85): Cursor: WnAirmiceInit...
11-11 07:33:23.353 2633 2651 I sensor_apps: 11-11 07:33:23.353 Nano_DevsEvent(329): list { 'add', 'hidraw1' }
11-11 07:33:23.354 2633 2648 I sensor_apps: 11-11 07:33:23.354 Nano_EventHandle(240): [1]Device Unkown
11-11 07:33:28.954 2633 4785 I sensor_apps: 11-11 07:33:28.954 Nano_Hidraw_BleDataProc(799): call WnAirmiceRecover,periord:20
11-11 07:33:28.954 2633 4785 I sensor_apps: 11-11 07:33:28.954 Nano_Hidraw_BleDataProc(805): [RC0] -> {START}FrameSend=0 FrameRecv=0 FrameLost=0
11-11 07:33:29.141 2633 4785 I sensor_apps: 11-11 07:33:29.141 Nano_CursorRead(216): typeFlag:[0] -> [aa],call WnAirmiceRecover,periord:20
11-11 07:33:32.079 2633 4785 I sensor_apps: 11-11 07:33:32.079 Nano_CursorRead(233): Btn state : (0)->(1)
11-11 07:33:32.291 2633 4785 I sensor_apps: 11-11 07:33:32.291 Nano_CursorRead(233): Btn state : (1)->(0)
11-11 07:33:39.654 2633 4785 I sensor_apps: 11-11 07:33:39.654 Nano_Hidraw_BleDataProc(813): [RC0] -> {STOP}FrameSend=0 FrameRecv=0 FrameLost=0