I wrote a char device driver and am now writing a QT "wrapper" which part of it is to get a signal to fire when the device becomes readable via the poll mechanism. I had tried to do:
QFile file("/dev/testDriver");
if(file.open(QFile::ReadOnly)) {
QSocketNotifier sn(file.handle(), , QSocketNotifier::Read);
sn.setEnabled(true);
connect(&sn, SIGNAL(activated(int)), &this, SLOT(readyRead()));
}
But readyRead was never called and my driver never reported having its poll method called.
I was able to get the following code to work so I know my driver is working
QFile file("/dev/testDriver");
if(file.open(QFile::ReadOnly)) {
struct pollfd fd;
fd.fd = file.handle();
fd.events = POLLIN;
struct pollfd fds[] = {fd};
int ready;
qDebug() << "Started poll";
ready = poll(fds, 1, -1);
qDebug() << "Poll returned: " << ready;
QTextStream in(&file);
QTextStream out(stdout);
out << in.readAll();
}
This properly waits for my driver to call wake_up and I can see two poll calls from my driver. One for the initial poll registration and one for when the wake_up happens.
Doing it this way I would probably have to spawn a separate thread which all it did was poll on this device and throw a signal and loop.
Is it possible to use QSocketNotifier in this way? The documentation of QFile::handle() seems to indicate it should be.
Your
QSocketNotifer
gets destroyed as soon as thatif
block ends. It doesn't stand a chance of reporting anything.You must keep that socket notifier alive as long as you want that file to be monitored. The simplest way of doing that is probably keeping a
QSocketNotifer*
member in one of your classes.I'll also mention that QSocketNotifier can be used to watch stdin using the following
---Header