如何绕过 Android 事物进行 GPIO Java

How to do GPIO on Android Things bypassing Java

我一直在尝试使用 NDK 通过 Android 访问 Raspberry Pi 上的 GPIO,但在尝试了几种方法后,我总是发现某种安全墙阻止我访问它。

动机是我使用 Java API 从 GPIO 获得的缓慢速度。作为基本参考,我能够以大约 2KHz 的频率打开和关闭它,这恰好是 this list. See also 中最差的,到目前为止还没有答案,我在其中添加了关于 java 速度的评论。事实上,这项工作的动机是我在检查该问题时得到的结果。

迄今为止最好的方法是尝试类似于 Debian 下的 sysfs 所采取的方法。从这个意义上说,代码似乎是公平的,但是在 运行 作为 root 之后,将应用程序安装为系统应用程序(通过将其移动到 /system/app)和 chmod a+rw 在 /sys/class/gpio/ 下的几个不同的东西/sys/class/gpio/gpio24/ 还有 /dev/gpiomem 这就是我得到的:

01-27 12:54:47.069  8412  8412 I NativeHelper: Call native = hello from helper java class
01-27 12:54:47.079  8412  8412 I NativeHelper: Open pin true
01-27 12:54:47.080  8412  8412 F libc    : Fatal signal 4 (SIGILL), code 1, fault addr 0xaf2e039c in tid 8412 (le.thingssample)
01-27 12:54:47.081   128   128 W         : debuggerd: handling request: pid=8412 uid=10028 gid=10028 tid=8412
01-27 12:54:47.066  8412  8412 I le.thingssample: type=1400 audit(0.0:211): avc: denied { write } for name="export" dev="sysfs" ino=854 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:sysfs:s0 tclass=file permissive=1
01-27 12:54:47.076  8412  8412 I le.thingssample: type=1400 audit(0.0:212): avc: denied { open } for path="/sys/class/gpio/export" dev="sysfs" ino=854 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:sysfs:s0 tclass=file permissive=1
01-27 12:54:47.166  8427  8427 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
01-27 12:54:47.166  8427  8427 F DEBUG   : Build fingerprint: 'generic/iot_rpi3/rpi3:7.0/NIF73/3565696:userdebug/test-keys'
01-27 12:54:47.166  8427  8427 F DEBUG   : Revision: '0'
01-27 12:54:47.166  8427  8427 F DEBUG   : ABI: 'arm'
01-27 12:54:47.167  8427  8427 F DEBUG   : pid: 8412, tid: 8412, name: le.thingssample  >>> com.amazingapps.sample.thingssample <<<
01-27 12:54:47.167  8427  8427 F DEBUG   : signal 4 (SIGILL), code 1 (ILL_ILLOPC), fault addr 0xaf2e039c
01-27 12:54:47.167  8427  8427 F DEBUG   :     r0 00000000  r1 b1f5e59f  r2 b1f5e59f  r3 af2e1c55
01-27 12:54:47.167  8427  8427 F DEBUG   :     r4 00000000  r5 be8e35fc  r6 acca3230  r7 be8e35d8
01-27 12:54:47.167  8427  8427 F DEBUG   :     r8 be8e36f8  r9 acc85400  sl 00000000  fp be8e3684
01-27 12:54:47.168  8427  8427 F DEBUG   :     ip be8e3590  sp be8e3598  lr ae7fbd2b  pc af2e039c  cpsr 60000030
01-27 12:54:47.170  8427  8427 F DEBUG   :
01-27 12:54:47.170  8427  8427 F DEBUG   : backtrace:
01-27 12:54:47.170  8427  8427 F DEBUG   :     #00 pc 0000139c  /data/app/com.amazingapps.sample.thingssample-1/lib/arm/libsample.so (Java_com_amazingapps_sample_thingssample_ndk_NativeHelper_doAll+59)
01-27 12:54:47.170  8427  8427 F DEBUG   :     #01 pc 002c8cb3  /data/app/com.amazingapps.sample.thingssample-1/oat/arm/base.odex (offset 0x2af000)
01-27 12:54:48.133   408  8432 W ActivityManager:   Force finishing activity com.amazingapps.sample.thingssample/.view.MainActivity
01-27 12:54:48.137   128   128 W         : debuggerd: resuming target 8412
01-27 12:54:48.138   408   427 I BootReceiver: Copying /data/tombstones/tombstone_02 to DropBox (SYSTEM_TOMBSTONE)
01-27 12:54:48.228  8433  8433 W crash_reporter: Received crash notification for app_process32[8412] sig 4, user 10028 (handling)
01-27 12:54:48.233  8433  8433 I crash_reporter: State of crashed process [8412]: S (sleeping)
01-27 12:54:48.226  8433  8433 I crash_reporter: type=1400 audit(0.0:213): avc: denied { search } for name="8412" dev="proc" ino=214991 scontext=u:r:crash_reporter:s0 tcontext=u:r:untrusted_app:s0:c512,c768 tclass=dir permissive=1
01-27 12:54:48.226  8433  8433 I crash_reporter: type=1400 audit(0.0:214): avc: denied { read } for name="exe" dev="proc" ino=217417 scontext=u:r:crash_reporter:s0 tcontext=u:r:untrusted_app:s0:c512,c768 tclass=lnk_file permissive=1
01-27 12:54:48.226  8433  8433 I crash_reporter: type=1400 audit(0.0:215): avc: denied { read } for scontext=u:r:crash_reporter:s0 tcontext=u:r:untrusted_app:s0:c512,c768 tclass=file permissive=1
01-27 12:54:48.226  8433  8433 I crash_reporter: type=1400 audit(0.0:216): avc: denied { open } for path="/proc/8412/status" dev="proc" ino=216902 scontext=u:r:crash_reporter:s0 tcontext=u:r:untrusted_app:s0:c512,c768 tclass=file permissive=1
01-27 12:54:48.226  8433  8433 I crash_reporter: type=1400 audit(0.0:217): avc: denied { getattr } for path="/proc/8412/status" dev="proc" ino=216902 scontext=u:r:crash_reporter:s0 tcontext=u:r:untrusted_app:s0:c512,c768 tclass=file permissive=1
01-27 12:54:48.672   408   422 W ActivityManager: Activity pause timeout for ActivityRecord{f2233fb u0 com.amazingapps.sample.thingssample/.view.MainActivity t86 f}

通过 chmod a+rw /sys/class/gpio/export 至少我得到了一个差异并且可以打开和关闭引脚,通过方法 return 值验证并检查 /sys/class/gpio/gpio24.[=21 的创建=]

我正在使用以下代码尝试 Raspberry Pi 3:https://github.com/fmatosqg/androidthings_ndk/tree/SO_question

在另一种方法中,我尝试直接在 java 中加载 libperipheralman.so,希望调用它的函数(显然这是 GPIO setValue() 所在的位置),但这次我 运行 变成另一种问题,其中库的子集被某些安全问题拒绝加载。请参阅关于他们不允许开发人员访问所有 .so 的 Nougat 文档,因为 back/future 兼容性目的。

我想任何跳过使用 Java VM/Dalvik 的实现也有可能让我更接近我的目标,即在 .apk 上获得不错的 GPIO 速度,但我不知道是否可以在此处使用适用于 rpi3 + debian 的任何方法。

已编辑:

运行 每次您要 运行 应用程序时都这样(适用于 pin 24,替换为您喜欢的 pin):

cd /sys/class/gpio
su root chmod a+w export
echo 24 > export
su root chmod a+w gpio24/direction
su root chmod a+w gpio24/value

ls gpio24/ -l

不幸调用execl returns -1 和系统returns 32256,所以没有办法替换这个手动步骤。即使尝试 system("/system/bin/date > /storage/self/primary/now "); 也会得到 32256.

然后将监听命名管道的脚本放在一起,并基于此 运行 编写上述代码。在使用 suchmod 使 运行 变得容易之后,我将一个 C 代码放在一起,该代码将写入同一个管道。添加 300ms 等待管道被读取,我将开始从 apk 中的 C 代码写入 gpio23/direction 和 gpio/value。

Led 点亮,一旦我优化 C 代码以获得更紧密的循环,我就会 post 进行一些速度基准测试。

这里有完整的解决方案 https://github.com/fmatosqg/androidthings_ndk/tree/SO_answer。查看 README.md 了解说明。

为了让它变得更好,我需要调试命名管道读数,因为有时我会读取错误的信息。并且还使脚本在启动时启动。