Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use asio with device files

I'm using boost asio throughout my project. I now want to read a device file (/dev/input/eventX). In the boost asio documentation it states that normal file IO is not possible but device files or pipes are supported by using asio::posix::stream_descriptor.

I opened the file descriptor via open and assigned it to the stream_descriptor. I now issue a async_read() call which never returns.

Is it possible to use boost asio with input events? Do I need to configure the file handle before using it with asio via ioctl?

Edit: Add some example code -> added some example code.

The following code opens /dev/input/event12 and the run method on the io_service object is called.

#include <boost/asio.hpp>
#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <linux/input.h>

namespace asio = boost::asio;
#ifdef BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
typedef asio::posix::stream_descriptor stream_descriptor;
#else // BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
typedef asio::windows::stream_handle stream_descriptor;
#endif // BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR

class FileReader
{
    typedef boost::shared_ptr<asio::streambuf> StreambufPtr;
    typedef boost::shared_ptr<FileReader> FileReaderPtr;
    typedef boost::weak_ptr<FileReader> FileReaderWeakPtr;
    public:
    static FileReaderWeakPtr Create(asio::io_service& io_service, const std::string& path);
    virtual ~FileReader();

    void HandleRead(FileReaderPtr me, StreambufPtr sb,
                    const boost::system::error_code &error);
private:
    FileReader(asio::io_service& io_service, const std::string& path);
    stream_descriptor m_InputStream;
};

FileReader::FileReaderWeakPtr FileReader::Create(asio::io_service& io_service,
                                                 const std::string& path){
    FileReaderPtr ptr(new FileReader(io_service, path));
    StreambufPtr sb(new boost::asio::streambuf());

    asio::async_read(ptr->m_InputStream, *sb,
            boost::bind(&FileReader::HandleRead, ptr.get(),
            ptr, sb, asio::placeholders::error));
    return ptr;
}

FileReader::FileReader(asio::io_service& io_service, const std::string& path)
    :m_InputStream(io_service)
{
    int dev = open(path.c_str(), O_RDONLY);
    if (dev == -1) {
        throw std::runtime_error("failed to open device " + path);
    }

    m_InputStream.assign(dev);
}

void FileReader::HandleRead(FileReaderPtr me, StreambufPtr sb,
                    const boost::system::error_code &error) {
    if(!error) {
        //Inform all of a sucessfull read
        std::istream is(sb.get());
        size_t data_length = sb->size();
        asio::async_read(m_InputStream, *sb,
            boost::bind(&FileReader::HandleRead, this, me, sb, asio::placeholders::error));
    }
}
like image 627
David Feurle Avatar asked Dec 22 '12 08:12

David Feurle


1 Answers

The problem was that I was using async_read without any complete condition. Therefore the callback was never invoked. After changing the call to async_read_some everything works as expected.

like image 111
David Feurle Avatar answered Oct 12 '22 12:10

David Feurle