Android (Java) controls GPIO methods and time consumption analysis, androidgpio
The previous two Articles respectively introduced how to control gpio by using scripts and C code to read/sys/class/GPIO. In actual project debugging, you often need to control GPIO in the Java code. Its implementation is similar to that in C code. The only difference is the permission. This article focuses on permission configuration and briefly analyzes the time consumption for controlling GPIO at the Java layer.
Taking the Qualcomm platform as an example, the permission configuration mainly modifies the file. te, file_contexts, and system_app.te files in the HLOS/device/qcom/sepolicy/common directory.
Modify file. te as follows,
# GPIO accessed by system apptype sysfs_gpio, fs_type, sysfs_type;
Modify file_contexts as follows,
/sys/devices/soc/1010000.pinctrl/gpio/gpio62/value u:object_r:sysfs_gpio:s0/sys/devices/soc/1010000.pinctrl/gpio/gpio63/value u:object_r:sysfs_gpio:s0
Modify system_app.te as follows,
allow system_app sysfs_gpio:file rw_file_perms;
After the modification is complete, run the make bootimage command to generate boot. img and use fastboot for burning.
The Android app modifies the AndroidManifest. xml file and adds android: sharedUserId = "android. uid. system" to the manifest node. Modify the Android. mk file and add LOCAL_CERTIFICATE: = platform to set the signature. The sample code for controlling GPIO in Java is as follows,
void IOCtrl(int pin, int level) { String path; path = "/sys/class/gpio/gpio" + pin + "/value"; try { FileOutputStream out = new FileOutputStream(path); out.write(level); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); return; } } if (trigger == 0x01) { trigger = 0x00; IOCtrl(62, '1'); IOCtrl(63, '1'); } else { trigger = 0x01; IOCtrl(62, '0'); IOCtrl(63, '0'); }
As shown in the preceding code execution result, we can see that the difference between the two IO increase is about 2.5 ms.
Note: If you only want to perform a test, you can use the setenforce 0 command to set SELinux to the permissive mode. Then, you can use Android Studio to compile and test the application to control GPIO, which is more convenient.