I am trying to replace the main loop in my program ( while(1)...select()
) with boost::asio::io_service.run()
.
The program has a couple sockets open, which were monitored by select().
The tricky part is that the FD_SET in the select statement has socket file descriptors as well as char device descriptors (for hardware input). In the previous code, calling int fd = open("/dev/button1", O_RDONLY);
was enough, and that fd was added to the FD_SET.
The select()
statement is able to monitor all of them.
So in order to be able to monitor the character device from boost::asio::io_service
, I've been reading a lot about boost::asio::stream_descriptor
. But I haven't been able to get it to work.
I've tried opening the device normally and then creating a stream_descriptor, and adding it to the ioservice.
void callback(const boost::system::error_code &ec, std::size_t bytes){
std::cout << "callback called" << std::endl;
}
int main() {
static boost::asio::streambuf buffer;
int fd = open("/dev/button1", O_RDONLY);
boost::asio::posix::stream_descriptor btn(io_service, fd);
boost::asio::async_read(btn, buffer, &button_callback);
io_service.run();
}
However, that does not work.