如何在Linux GPIO中使用Boost::ASIO [英] How to use boost::asio with Linux GPIOs

查看:42
本文介绍了如何在Linux GPIO中使用Boost::ASIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Boost::ASIO进行异步输入/输出的单线程Linux应用程序。现在,我需要扩展此应用程序以读取/sys/class/gpio/gpioXX/value上的GPIO输入。

在边沿触发的GPIO输入上使用Boost::ASIO::POSIX::STREAM_DESCRIPTOR可以做到这一点吗?

我配置的GPIO输入如下:

echo XX >/sys/class/gpio/export
echo in >/sys/class/gpio/gpioXX/direction
echo both >/sys/class/gpio/gpioXX/edge
我设法编写了一个基于epoll的测试应用程序,它在GPIO文件描述符上阻塞,直到GPIO信号改变,但boost::asio似乎无法正确阻塞。对boost::asio::async_read的调用总是立即使用EOF调用处理程序(当然仅在io_service.run()内),或者-如果文件指针被设置回-2字节数据。

我不是boost::asio内部结构方面的专家,但可能的原因是boost::asioEPOLL反应器是水平触发的,而不是在posix::stream_descriptor的情况下边缘触发的?

以下是我的代码:

#include <fcntl.h>

#include <algorithm>
#include <iterator>
#include <stdexcept>

#include <boost/asio.hpp>

boost::asio::io_service io_service;
boost::asio::posix::stream_descriptor sd(io_service);
boost::asio::streambuf streambuf;

void read_handler(const boost::system::error_code& error, std::size_t bytes_transferred)
{
    if (error.value() == boost::asio::error::eof) {
        // If we don't reset the file pointer we only get EOFs
        lseek(sd.native_handle(), 0, SEEK_SET);
    } else if (error)
        throw std::runtime_error(std::string("Error ") + std::to_string(error.value()) + " occurred (" + error.message() + ")");

    std::copy_n(std::istreambuf_iterator<char>(&streambuf), bytes_transferred, std::ostreambuf_iterator<char>(std::cout));
    streambuf.consume(bytes_transferred);
    boost::asio::async_read(sd, streambuf, &read_handler);
}

int main(int argc, char *argv[])
{
    if (argc != 2)
        return 1;

    int fd = open(argv[1], O_RDONLY);
    if (fd < 1)
        return 1;

    try {
        sd.assign(fd);
        boost::asio::async_read(sd, streambuf, &read_handler);
        io_service.run();
    } catch (...) {
        close(fd);
        return 1;
    }

    close(fd);
    return 0;
}

推荐答案

据我所知,不可能通过Boost.ASIO获得此特定行为。虽然内核将procf和sysf上的一些文件标记为可轮询,但它们并不提供boost::asio::posix::stream_descriptor及其操作所期望的类似流的行为。

Boost.ASIO的EPOLL反应堆是边缘触发的(参见Boost.ASIO 1.43revision history notes)。在某些情况下1,Boost.Asio将尝试在启动函数(例如async_read())的上下文中执行I/O操作。如果I/O操作完成(成功或失败),则io_service.post()将完成处理程序发送到io_serviceAS-IF。否则,文件描述符将被添加到事件多路分解器中进行监控。文档中提到了此行为:

无论异步操作是否立即完成,都不会从此函数中调用处理程序。处理程序的调用将以与使用boost::asio::io_service::post()相同的方式执行。

用于组合操作,如async_read()EOF is treated as an error,因为它指示操作的契约中存在违规(即,完成条件将永远不会得到满足,因为没有更多的数据可用)。在这种情况下,I/O系统调用将发生在async_read()启动函数中,从文件的开始(偏移量0)读取到文件的结尾,导致操作失败,并显示boost::asio::error::eof。由于操作已完成,因此永远不会将其添加到用于边缘触发监控的事件多路分解器:

boost::asio::io_service io_service;
boost::asio::posix::stream_descriptor stream_descriptor(io_service);

void read_handler(const boost::system::error_code& error, ...)
{
  if (error.value() == boost::asio::error::eof)
  {
    // Reset to start of file.
    lseek(sd.native_handle(), 0, SEEK_SET);
  }

  // Same as below.  ::readv() will occur within this context, reading
  // from the start of file to end-of-file, causing the operation to
  // complete with failure.
  boost::asio::async_read(stream_descriptor, ..., &read_handler);
}

int main()
{
  int fd = open( /* sysfs file */, O_RDONLY);

  // This would throw an exception for normal files, as they are not
  // poll-able.  However, the kernel flags some files on procfs and
  // sysfs as pollable.
  stream_descriptor.assign(fd);

  // The underlying ::readv() system call will occur within the
  // following function (not deferred until edge-triggered notification
  // by the reactor).  The operation will read from start of file to
  // end-of-file, causing the operation to complete with failure.
  boost::asio::async_read(stream_descriptor, ..., &read_handler);

  // Run will invoke the ready-to-run completion handler from the above
  // operation.
  io_service.run();
}

1.在内部,Boost.Asio将此行为称为推测性操作。这是实现细节,但如果操作可能不需要事件通知(例如,它可以立即尝试非阻塞I/O调用),并且在I/O对象上既没有相同类型的挂起操作,也没有挂起的带外操作,则将在启动函数内尝试I/O操作。没有阻止此行为的自定义挂钩。

这篇关于如何在Linux GPIO中使用Boost::ASIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆