#include <fcntl.h>#include <iostream>#include <boost/bind.hpp>#include <boost/thread.hpp>#include <boost/asio.hpp>#include <boost/function.hpp>class gpio_interrupt{public:gpio_interrupt(boost::asio::io_service &ios, unsigned int number) :_socket(ios){open(number);}~gpio_interrupt(){close();}void open(unsigned int number){boost::system::error_code ec;open(number, ec);boost::asio::detail::throw_error(ec, "open");}void open(unsigned int number, boost::system::error_code& ec){std::ostringstream filename;filename << "/sys/class/gpio/gpio" << number << "/value";int fd = ::open(filename.str().c_str(), O_RDONLY | O_NONBLOCK);if (fd < 0){ec = boost::system::error_code(errno, boost::asio::error::get_system_category());return;}_socket.assign(boost::asio::ip::udp::v4(), fd, ec);}void close(){boost::system::error_code ec;cancel(ec);boost::asio::detail::throw_error(ec, "close");}void close(boost::system::error_code& ec){cancel(ec);}void cancel(){boost::system::error_code ec;_socket.cancel(ec);boost::asio::detail::throw_error(ec, "close");}void cancel(boost::system::error_code& ec){_socket.cancel(ec);}void async_wait(boost::function<void(const boost::system::error_code& ec)> cb){_socket.async_receive(boost::asio::null_buffers(), [cb](const boost::system::error_code& ec, std::size_t bytes_transferred){cb(ec);});}private:boost::asio::ip::udp::socket _socket;};int main(int argc, char **argv){boost::asio::io_service ios;boost::asio::io_service::work work(ios);boost::thread thread(boost::bind(&boost::asio::io_service::run, &ios));gpio_interrupt interrupt(ios, 21);interrupt.async_wait([](const boost::system::error_code& ec){if (ec){std::cout << ec.message() << std::endl;return;}std::cout << "Pin changed state" << std::endl;});while (true){boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); // just dummy - do something else here...}ios.stop();thread.join();return 0;}