apk打开串口失败
报错信息:
07-03 17:05:27.030 4582 4582 W jw.ComAssistant: type=1400 audit(0.0:56): avc: denied { read } for name="ttyS2" dev="tmpfs" ino=7704 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:ttyS2_device:s0 tclass=chr_file permissive=0
07-03 17:05:27.035 4582 4582 W System.err: java.io.IOException: Cannot run program "/system/bin/su": error=2, No such file or directory
07-03 17:05:27.036 4582 4582 W System.err: at java.lang.ProcessBuilder.start(ProcessBuilder.java:983)
07-03 17:05:27.036 4582 4582 W System.err: at java.lang.Runtime.exec(Runtime.java:691)
07-03 17:05:27.036 4582 4582 W System.err: at java.lang.Runtime.exec(Runtime.java:524)
07-03 17:05:27.036 4582 4582 W System.err: at java.lang.Runtime.exec(Runtime.java:421)
07-03 17:05:27.036 4582 4582 W System.err: at android_serialport_api.SerialPort.<init>(SerialPort.java:47)
07-03 17:05:27.036 4582 4582 W System.err: at com.bjw.ComAssistant.SerialHelper.open(SerialHelper.java:43)
07-03 17:05:27.036 4582 4582 W System.err: at com.bjw.ComAssistant.ComAssistantActivity.OpenComPort(ComAssistantActivity.java:885)
07-03 17:05:27.036 4582 4582 W System.err: at com.bjw.ComAssistant.ComAssistantActivity.access$8(ComAssistantActivity.java:882)
07-03 17:05:27.036 4582 4582 W System.err: at com.bjw.ComAssistant.ComAssistantActivity$ToggleButtonCheckedChangeEvent.onCheckedChanged(ComAssistantActivity.java:562)
07-03 17:05:27.036 4582 4582 W System.err: at android.widget.CompoundButton.setChecked(CompoundButton.java:156)
07-03 17:05:27.036 4582 4582 W System.err: at android.widget.ToggleButton.setChecked(ToggleButton.java:71)
07-03 17:05:27.036 4582 4582 W System.err: at android.widget.CompoundButton.toggle(CompoundButton.java:115)
07-03 17:05:27.036 4582 4582 W System.err: at android.widget.CompoundButton.performClick(CompoundButton.java:120)
07-03 17:05:27.036 4582 4582 W System.err: at android.view.View$PerformClick.run(View.java:22445)
07-03 17:05:27.036 4582 4582 W System.err: at android.os.Handler.handleCallback(Handler.java:755)
07-03 17:05:27.036 4582 4582 W System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-03 17:05:27.036 4582 4582 W System.err: at android.os.Looper.loop(Looper.java:154)
07-03 17:05:27.036 4582 4582 W System.err: at android.app.ActivityThread.main(ActivityThread.java:6141)
07-03 17:05:27.036 4582 4582 W System.err: at java.lang.reflect.Method.invoke(Native Method)
07-03 17:05:27.036 4582 4582 W System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
07-03 17:05:27.036 4582 4582 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
07-03 17:05:27.037 4582 4582 W System.err: Caused by: java.io.IOException: error=2, No such file or directory
07-03 17:05:27.037 4582 4582 W System.err: at java.lang.UNIXProcess.forkAndExec(Native Method)
07-03 17:05:27.037 4582 4582 W System.err: at java.lang.UNIXProcess.<init>(UNIXProcess.java:133)
07-03 17:05:27.037 4582 4582 W System.err: at java.lang.ProcessImpl.start(ProcessImpl.java:128)
07-03 17:05:27.037 4582 4582 W System.err: at java.lang.ProcessBuilder.start(ProcessBuilder.java:964)
07-03 17:05:27.037 4582 4582 W System.err: ... 20 more
思路:根据报错信息很容易知道是/system/bin/su这个文件不在,这个问题比较好解决,烧一下其他固件,adb poll /system/bin/su 然后再烧录这个有问题固件把su烧录进去。
然后再次打开串口:
07-03 17:15:16.023 4582 4582 W jw.ComAssistant: type=1400 audit(0.0:70): avc: denied { read } for name="ttyS2" dev="tmpfs" ino=7704 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:ttyS2_device:s0 tclass=chr_file permissive=0
07-03 17:15:16.029 4582 4582 W System.err: java.lang.SecurityException
07-03 17:15:16.029 4582 4582 W System.err: at android_serialport_api.SerialPort.<init>(SerialPort.java:53)
07-03 17:15:16.029 4582 4582 W System.err: at com.bjw.ComAssistant.SerialHelper.open(SerialHelper.java:43)
07-03 17:15:16.029 4582 4582 W System.err: at com.bjw.ComAssistant.ComAssistantActivity.OpenComPort(ComAssistantActivity.java:885)
07-03 17:15:16.029 4582 4582 W System.err: at com.bjw.ComAssistant.ComAssistantActivity.access$8(ComAssistantActivity.java:882)
07-03 17:15:16.029 4582 4582 W System.err: at com.bjw.ComAssistant.ComAssistantActivity$ToggleButtonCheckedChangeEvent.onCheckedChanged(ComAssistantActivity.java:562)
07-03 17:15:16.029 4582 4582 W System.err: at android.widget.CompoundButton.setChecked(CompoundButton.java:156)
07-03 17:15:16.029 4582 4582 W System.err: at android.widget.ToggleButton.setChecked(ToggleButton.java:71)
07-03 17:15:16.029 4582 4582 W System.err: at android.widget.CompoundButton.toggle(CompoundButton.java:115)
07-03 17:15:16.029 4582 4582 W System.err: at android.widget.CompoundButton.performClick(CompoundButton.java:120)
07-03 17:15:16.029 4582 4582 W System.err: at android.view.View$PerformClick.run(View.java:22445)
07-03 17:15:16.029 4582 4582 W System.err: at android.os.Handler.handleCallback(Handler.java:755)
07-03 17:15:16.029 4582 4582 W System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-03 17:15:16.030 4582 4582 W System.err: at android.os.Looper.loop(Looper.java:154)
07-03 17:15:16.030 4582 4582 W System.err: at android.app.ActivityThread.main(ActivityThread.java:6141)
07-03 17:15:16.030 4582 4582 W System.err: at java.lang.reflect.Method.invoke(Native Method)
07-03 17:15:16.030 4582 4582 W System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
07-03 17:15:16.030 4582 4582 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
思路:
在device/rockchip/common/sepolicy/untrusted_app.te
allow untrusted_app serial_device:chr_file { rw_file_perms open read write ioctl getattr setattr };
给串口赋予权限。然后还是不行,一样的错误。然后从网上找的串口api的源代码了,定位错误
肯定是这里出问题了,设备不可读并且不可写。我们去dev下面看一下权限,发现权限没有问题,那肯定是!device.canRead() 这里面有问题。
搜一下源代码:
这边应该是getSecurityManager()出问题了。这边实在是想不出什么办法了,在网上搜了一顿发现最终还是解决了。发一下方案:
--- a/device/rockchip/common/sepolicy/device.te
+++ b/device/rockchip/common/sepolicy/device.te
@@ -1,3 +1,8 @@
+type ttyS1_device, dev_type, mlstrustedobject;
+type ttyS2_device, dev_type, mlstrustedobject;
+type ttyS3_device, dev_type, mlstrustedobject;
+type ttyS4_device, dev_type, mlstrustedobject;
//定义ttyS1_device为dev_type, mlstrustedobject类型
--- a/device/rockchip/common/sepolicy/file_contexts
+++ b/device/rockchip/common/sepolicy/file_contexts
@@ -1,3 +1,10 @@
+#增加我们需要访问的设备
+/dev/ttyS1 u:object_r:ttyS1_device:s0
+/dev/ttyS2 u:object_r:ttyS2_device:s0
+/dev/ttyS3 u:object_r:ttyS3_device:s0
+/dev/ttyS4 u:object_r:ttyS4_device:s0
//将dev/ttyS1与我们签名定义的ttyS1_device进行绑定关系
--- a/device/rockchip/common/sepolicy/untrusted_app.te
+++ b/device/rockchip/common/sepolicy/untrusted_app.te
@@ -19,10 +19,14 @@ allow untrusted_app system_data_file:file r_file_perms;
allow untrusted_app sysfs_lut:file { getattr setattr open write };
allow untrusted_app ttys_device:chr_file { open read write ioctl getattr setattr };
allow untrusted_app mc_gpio_device:chr_file { open read write ioctl };
-allow untrusted_app device:dir { open read write };
+allow untrusted_app device:dir { open read write ioctl getattr setattr };
allow untrusted_app selinuxfs:dir { open read write ioctl getattr setattr };
allow untrusted_app serial_device:chr_file { rw_file_perms open read write ioctl getattr setattr };
allow untrusted_app tty_device:chr_file { rw_file_perms open read write ioctl getattr setattr };
allow untrusted_app sysfs:file { rw_file_perms open read write ioctl getattr setattr };
allow untrusted_app system_file:file { getattr setattr open read write execmod };
+allow untrusted_app ttyS1_device:chr_file rw_file_perms;
+allow untrusted_app ttyS2_device:chr_file rw_file_perms;
+allow untrusted_app ttyS3_device:chr_file rw_file_perms;
+allow untrusted_app ttyS4_device:chr_file rw_file_perms;
//赋予读写权限
其实就是重新定义一下我们要使用到的串口,然后赋予权限。
至此问题解决
参考资料:
https://blog.csdn.net/Sunxiaolin2016/article/details/91039775?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0-91039775-blog-127211886.235v38pc_relevant_sort_base1&spm=1001.2101.3001.4242.1&utm_relevant_index=3