我正在使用一个使用Android5.1和Kotlin的DragonBoard 410 C来试验40针低功耗连接器上的GPIO引脚。我使用的库是使用sysfs接口与GPIO引脚进行交互,这需要在/sys/class/gpio/目录树中打开各种伪文件,并为这些文件读写值,请参阅在运行安卓的DragonBoard 410 C上访问GPIO低功耗连接器
我的理解是,我可以提供一个GPIO引脚作为输入和边缘触发,这将允许我连接一个简单的电路与一个瞬间接触开关,并能够检测什么时候按下开关。
但是,我发现的文档表明,我需要在文件描述符上使用民意测验(2)系统服务或选择(2)系统服务来检测何时检测到边缘,例如/sys/class/gpio/gpio910/value。
如何在Kotlin中使用带有文件描述符的poll(2)或select(2)系统服务?poll(2)与FileReader的ready()方法相同吗?
也许需要类似于Java WatchService功能的东西?.htm。
除非这是错误的方法,否则我计划的是有一个实用函数,类似于:
// pollPseudoFile() polls the specified GPIO pin using the sysfs interface in order
// to wait for a pin value due to an external event such as a momentary switch press
//
// Returns:
// - 0 -> an error of some kind
// - 1 -> time out expired with no value
// - 2 -> value change detected
public fun pollPseudoFile (pinPathFull : String, timeOut : Int) : Int {
println(" pollPseudoFile - String")
var iStatus : Int = 0 // status return indicating if poll() completed or not.
try {
val br = FileReader(pinPathFull)
br.poll(timeOut) // wait for data to be available or time out. how to do poll?
iStatus = 2 // indicate value change unless the poll() timed out
if (br.pollExpired) iStatus = 1 // poll timed out. how to check for poll status?
br.close()
} catch (e: Exception) {
println("Error: " + e.message)
}
return iStatus;
}
public fun pollGetValue (pinPathFull : String) : Int {
println(" pollGetValue - String")
var line = ""
try {
val br = BufferedReader(FileReader(pinPathFull))
line = br.readLine()
br.close()
} catch (e: Exception) {
println("Error: " + e.message)
}
return line.toInt()
}https://www.kernel.org/doc/Documentation/gpio/sysfs.txt
“价值”。读取为0(低)或1(高)。如果将GPIO配置为输出,则可以写入此值;任何非零值都被视为高值。 如果引脚可以配置为产生中断的中断,如果它被配置为生成中断(请参阅“edge”的描述),您可以对该文件进行轮询(2),每当中断被触发时,轮询(2)将返回。如果使用轮询(2),则设置事件POLLPRI和POLLERR。如果使用select(2),请将文件描述符设置为“除其他外”。在轮询(2)返回后,要么将After (2)返回sysfs文件的开头并读取新值,要么关闭该文件并重新打开它以读取该值。 “边缘”..。读为“无”、“上升”、“下降”或“两者兼而有之”。编写这些字符串以选择将对"value“文件返回进行轮询(2)的信号边缘。 只有当引脚可以配置为生成输入引脚的中断时,此文件才存在。
附加备注
备注1:使用实用程序,我能够将adb应用到DragonBoard 410 C中,并测试了配置物理引脚26 ( GPIO971 ),direction设置为in,edge设置为rising。使用一个简单的LED电路在面包板上,连接到物理引脚23,GPIO938,并添加一根线从物理引脚26到物理引脚23管理的发光二极管,我能够打开echo 1 > gpio938/value,然后cat gpio971/value,以看到物理引脚26的值已经很高,并被读取为1。然后,我将连接到物理引脚23的发光二极管与echo 0 > gpio938/value关闭,然后cat gpio971/value按预期返回0值。
然而,这个实验并没有告诉我,当poll(2)被打开和关闭时,是否会显示gpio971/value上的变化。
备注1a:我有一个原生C++ JNI函数的第一个版本来实现poll(2)服务调用,并且一直在用我的DragonBoard 410 C测试它。我看到的是,poll(2)函数立即返回,POLLIN和POLLERR都设置在struct pollfd数组的revents成员中。
测试是使用物理引脚26连接到一个面包板行与一个LED的一条腿连接到物理引脚23,我可以打开和关闭。当我试图打开10000 ms超时的轮询时,无论LED是否亮起(pin 26值为1),还是没有亮起(pin 26值为0),呼叫都会立即返回。
我的期望是,由于我的edge设置为rising,我应该看到poll(2)返回只有当LED是未亮,然后我打开它或10秒已经过去。
我正在继续我的调查,因为我觉得我如何使用我在应用程序的Kotlin端编写的本地C++函数可能有问题。
备注2:我试图在Kotlin应用程序中使用WatchService,但遇到了一个错误,即WatchService需要API级别26,而我在Android中的最小目标是API 22。看起来WatchService需要Android8.0(奥利奥),而DragonBoard在Android5.1 (Lollipop),所以我无法使用WatchService来监控文件状态。
发布于 2020-10-02 19:06:29
我采用的方法是创建一个本机C++ JNI函数,以提供实现轮询(2) Linux服务调用的方法。
我在开发和测试期间遇到的一个有趣的问题是,poll()立即返回,而不是等待超时或电压到GPIO输入引脚。在96Boards.org论坛上发布了DragonBoard 410 C、如何使用带sysfs接口的轮询()来输入GPIO引脚来处理按下开关事件之后,有人提出了一个可行的解决方案,在开始投票(2)之前读取伪文件。
为了使用这个函数,我需要有某种Kotlin协同线或侧线程,这样当主UI处理开始GPIO输入引脚轮询的按钮单击时,主UI线程不会被阻塞,直到函数返回一个GPIO事件或超时。
我尚未能够分辨出如何进行这种合作,因此,这仍是一项正在进行的工作。经过一些思考,似乎某种类型的事件侦听器体系结构将是最合适的方法。
然而,测试表明,当使用来自1.8v功率(引脚38)的电线施加到GPIO输入引脚时,当手动施加电压时,pollPseudoFile()函数通过执行超时或从/value返回值来正常工作。GPIO输入引脚在/edge伪文件中用rising或falling设置设置。
本机C++ JNI函数的源代码如下所示。我在下面的Kotlin源代码中使用它。
首先,在我的MainActivity.kt源文件中,我使用以下源代码提供了本机C++库:
// See the StackOverFlow question with answer at URL:
// https://stackoverflow.com/questions/36932662/android-how-to-call-ndk-function-from-kotlin
init {
System.loadLibrary("pollfileservice")
}
external fun pollFileWithTimeOut(pathPseudo : String, timeOutMs : Int): Int
external fun pollGetLastRevents() : Int接下来,我在Kotlin源文件Gpio.kt中使用这个函数来实际对伪文件执行poll()服务调用。
class Gpio(pin: Int) {
private val pin : Int
private val pinGpio : GpioFile = GpioFile()
/*
* The GPIO pins are represented by folders in the Linux file system
* within the folder /sys/class/gpio. Each pin is represented by a folder
* whose name is the prefix "gpio" followed by the pin number.
* Within the folder representing the pin are two files, "value" used to
* set or get the value of the pin and "direction" used to set or get
* the direction of the pin.
*
* This function creates the path to the Linux file which represents a particular
* GPIO pin function, "value" or "direction".
*/
private fun MakeFileName(pin: Int, op: String): String {
return "/sys/class/gpio/gpio$pin$op"
}
// ....... other source code in the Kotlin class Gpio
fun pinPoll (timeMs: Int) : Int {
val iStatus : Int = pinGpio.pollPseudoFile (MakeFileName(pin, "/value"), timeMs)
return iStatus
}上面的Gpio类用于实际UI按钮中单击监听器,如下所示:
val gpioProcessor = GpioProcessor()
// Get reference of GPIO23.
val gpioPin26 = gpioProcessor.pin26
// Set GPIO26 as input.
gpioPin26.pinIn()
gpioPin26.pinEdgeRising()
var xStatus: Int = gpioPin26.pinPoll(10000)
val xvalue = gpioPin26.valuePollFileService.h
//
// Created by rchamber on 9/24/2020.
//
#ifndef MY_APPLICATION_POLLFILESERVICE_H
#define MY_APPLICATION_POLLFILESERVICE_H
class PollFileService {
private:
int iValue;
int fd; /* file descriptor */
public:
// See poll(2) man page at https://linux.die.net/man/2/poll
static const int PollSuccess = 0;
static const int PollTimeOut = 1;
static const int PollErrorEFAULT = -1;
static const int PollErrorEINTR = -2;
static const int PollErrorEINVAL = -3;
static const int PollErrorENOMEM = -4;
static const int PollErrorPOLLERR = -5;
static const int PollErrorPOLLNVAL = -6;
static const int PollErrorPOLLERRNVAL = -7;
static const int PollErrorPOLLHUP = -8;
static const int PollErrorPOLLERRDEFLT = -9;
static const int PollErrorUNKNOWN = -100;
static int iPollStatus;
static int iPollRet;
static int iPollRevents;
PollFileService(const char *pathName = nullptr, int timeMilliSec = -1);
~PollFileService();
int PollFileCheck (const char *pathName, int timeMilliSec = -1);
int PollFileRead (const char *pathName = nullptr);
};
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_myapplication_MainActivity_pollFileWithTimeOut (JNIEnv* pEnv, jobject pThis, jstring pKey, jint timeMS);
#endif //MY_APPLICATION_POLLFILESERVICE_HPollFileService.cpp
//
// Created by rchamber on 9/24/2020.
//
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <errno.h>
#include <poll.h>
#include <jni.h>
#include "PollFileService.h"
int PollFileService::iPollStatus = 0;
int PollFileService::iPollRet = 0;
int PollFileService::iPollRevents = 0;
PollFileService::PollFileService(const char *pathName /* = nullptr */, int timeMilliSec /* = -1 */) : iValue(23), fd(-1)
{
iPollStatus = 0;
if (pathName) {
fd = open (pathName, O_RDONLY);
}
}
PollFileService::~PollFileService()
{
if (fd >= 0) {
close (fd);
fd = -1;
}
}
int PollFileService::PollFileCheck(const char *pathName, int timeMilliSec /* = -1 */)
{
struct pollfd fdList[] = {
{fd, POLLPRI | POLLERR, 0},
{0}
};
nfds_t nfds = 1;
unsigned char tempbuff[256] = {0};
if (fd < 0 && pathName) {
fd = open (pathName, O_RDONLY);
fdList[0].fd = fd;
}
// with a edge triggered GPIO that we are going to use the poll(2)
// function to wait on an event, we need to read from the
// pin before we do the poll(2). If the read is not done then
// the poll(2) returns with both POLLPRI and POLLERR set in the
// revents member. however if we read first then do the poll2()
// the poll(2) will wait for the event, input voltage change with
// either a rising edge or a falling edge, depending on the setting
// in the /edge pseudo file.
ssize_t iCount = read (fdList[0].fd, tempbuff, 255);
iPollStatus = PollErrorUNKNOWN;
int iRet = poll(fdList, nfds, timeMilliSec);
if (iRet == 0) {
iPollStatus = PollTimeOut;
} else if (iRet < 0) {
switch (errno) {
case EFAULT:
iPollStatus = PollErrorEFAULT;
break;
case EINTR:
iPollStatus = PollErrorEINTR;
break;
case EINVAL:
iPollStatus = PollErrorEINVAL;
break;
case ENOMEM:
iPollStatus = PollErrorENOMEM;
break;
default:
iPollStatus = PollErrorUNKNOWN;
break;
}
} else if (iRet > 0) {
// successful call now determine what we should return.
iPollRevents = fdList[0].revents; /* & (POLLIN | POLLPRI | POLLERR); */
switch (fdList[0].revents & (POLLIN | POLLPRI | POLLERR /* | POLLNVAL | POLLHUP*/)) {
case (POLLIN): // value of 1, There is data to read.
case (POLLPRI): // value of 2, There is urgent data to read
case (POLLOUT): // , Writing now will not block.
case (POLLIN | POLLPRI): // value of 3
iPollStatus = PollSuccess;
break;
// testing with a DragonBoard 410C indicates that we may
// see the POLLERR indicator set in revents along with
// the POLLIN and/or POLLPRI indicator set indicating there
// is data to be read.
// see as well poll(2) man page which states:
// POLLERR Error condition (output only).
case (POLLIN | POLLERR): // value of 9
case (POLLPRI | POLLERR): // value of 10
case (POLLIN | POLLPRI | POLLERR): // value of 11
iPollStatus = PollSuccess;
break;
case (POLLHUP): // , Hang up (output only).
iPollStatus = PollErrorPOLLHUP;
break;
case (POLLERR): // value of 8, Error condition (output only).
iPollStatus = PollErrorPOLLERR;
break;
case (POLLNVAL): // , Invalid request: fd not open (output only).
iPollStatus = PollErrorPOLLNVAL;
break;
case (POLLERR | POLLNVAL):
iPollStatus = PollErrorPOLLERRNVAL;
break;
default:
iPollStatus = PollErrorPOLLERRDEFLT;
break;
}
}
return iPollStatus;
}
int PollFileService::PollFileRead (const char *pathName /* = nullptr */)
{
char buffer[12] = {0};
int iRet = -1;
if (fd < 0 && pathName) {
fd = open (pathName, O_RDONLY);
}
int nCount = read (fd, buffer, 10);
if (nCount > 0) {
iRet = atoi (buffer);
}
return iRet;
}
// Check the specified file using the poll(2) service and
// return a status as follows:
// - 0 -> poll(2) success indicating something is available
// - 1 -> poll(2) failed with time out before anything available
// - -1 -> poll(2) error - EFAULT
// - -2 -> poll(2) error - EINTR
// - -3 -> poll(2) error - EINVAL
// - -4 -> poll(2) error - ENOMEM
// - -5 -> poll(2) error - POLLERR
// - -6 -> poll(2) error - POLLNVAL
// - -7 -> poll(2) error - POLLERR | POLLNVAL
// - -8 -> poll(2) error - POLLHUP
// - -9 -> poll(2) error - poll(2) revent indicator Unknown
// - -100 -> poll(2) error - Unknown error
//
static int lastRevents = 0;
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_myapplication_MainActivity_pollFileWithTimeOut (JNIEnv* pEnv, jobject pThis, jstring pKey, jint timeMS)
{
char *pathName;
int timeMilliSec;
PollFileService myPoll;
const char *str = pEnv->GetStringUTFChars(pKey, 0);
int timeMSint = 10000; // timeMS;
#if 1
int iStatus = myPoll.PollFileCheck(str, timeMSint);
#else
int iStatus = myPoll.PollFileRead(str);
#endif
pEnv->ReleaseStringUTFChars(pKey, str);
lastRevents = myPoll.iPollRevents;
return iStatus;
}
#if 0
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_myapplication_MainActivity_pollGetLastStatus (JNIEnv* pEnv, jobject pThis) {
return PollFileService::iPollStatus;
}
#endif
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_myapplication_MainActivity_pollGetLastRevents (JNIEnv* pEnv, jobject pThis)
{
return lastRevents;
}https://stackoverflow.com/questions/64028130
复制相似问题