use gio for watching fd
authorKevron Rees <kevron.m.rees@intel.com>
Tue, 27 May 2014 18:40:39 +0000 (11:40 -0700)
committerKevron Rees <kevron.m.rees@intel.com>
Tue, 27 May 2014 18:40:39 +0000 (11:40 -0700)
plugins/bluetooth/bluetoothplugin.cpp
plugins/bluetooth/bluetoothplugin.h
plugins/bluetooth/testAmbBt.cpp

index ca98229..76c3076 100644 (file)
@@ -34,6 +34,30 @@ using namespace std;
 
 #include "debugout.h"
 
+bool readCallback(GIOChannel *source, GIOCondition condition, gpointer data)
+{
+//     DebugOut(5) << "Polling..." << condition << endl;
+
+       if(condition & G_IO_ERR)
+       {
+               DebugOut(DebugOut::Error)<<"GpsNmeaSource polling error."<<endl;
+       }
+
+       if (condition & G_IO_HUP)
+       {
+               //Hang up. Returning false closes out the GIOChannel.
+               //printf("Callback on G_IO_HUP\n");
+               DebugOut(DebugOut::Warning)<<"socket hangup event..."<<endl;
+               return false;
+       }
+
+       BluetoothSinkPlugin* p = static_cast<BluetoothSinkPlugin*>(data);
+
+       p->canHasData();
+
+       return true;
+}
+
 BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map<string, string> config)
 :AbstractSink(re, config)
 {
@@ -89,9 +113,10 @@ void BluetoothSinkPlugin::newConnection(string path, QDBusUnixFileDescriptor fd,
 
        socket.setDescriptor(fd.fileDescriptor());
 
-       QSocketNotifier *notifier = new QSocketNotifier(fd.fileDescriptor(), QSocketNotifier::Read, this);
-
-       connect(notifier,&QSocketNotifier::activated,this, &BluetoothSinkPlugin::canHasData);
+       GIOChannel *chan = g_io_channel_unix_new(socket.fileDescriptor());
+       g_io_add_watch(chan, GIOCondition(G_IO_IN | G_IO_HUP | G_IO_ERR),(GIOFunc)readCallback, this);
+       g_io_channel_set_close_on_unref(chan, true);
+       g_io_channel_unref(chan);
 }
 
 void BluetoothSinkPlugin::requestDisconnection(string path)
@@ -105,6 +130,9 @@ void BluetoothSinkPlugin::canHasData()
        QByteArray data = socket.read().c_str();
 
        DebugOut()<<"data read: "<<data.constData()<<endl;
+
+       if(data == "ping")
+               socket.write("pong");
 }
 
 BtProfileAdaptor::BtProfileAdaptor(BluetoothSinkPlugin *parent)
index 46d3455..98ff09d 100644 (file)
@@ -73,9 +73,9 @@ public:
 
        void requestDisconnection(std::string path);
 
-private Q_SLOTS:
        void canHasData();
 
+
 private:
         SerialPort socket;
 };
index f7fa827..f00c046 100644 (file)
@@ -7,7 +7,30 @@
 
 #include "bluetooth5.h"
 #include <serialport.hpp>
-#include <QSocketNotifier>
+
+SerialPort *s = new SerialPort();
+
+bool readCallback(GIOChannel *source, GIOCondition condition, gpointer data)
+{
+//     DebugOut(5) << "Polling..." << condition << endl;
+
+       if(condition & G_IO_ERR)
+       {
+               DebugOut(DebugOut::Error)<<"GpsNmeaSource polling error."<<endl;
+       }
+
+       if (condition & G_IO_HUP)
+       {
+               //Hang up. Returning false closes out the GIOChannel.
+               //printf("Callback on G_IO_HUP\n");
+               DebugOut(DebugOut::Warning)<<"socket hangup event..."<<endl;
+               return false;
+       }
+
+       DebugOut(0)<<"Data: "<<s->read();
+
+       return true;
+}
 
 int main(int argc, char** argv)
 {
@@ -18,18 +41,14 @@ int main(int argc, char** argv)
        Bluetooth5 btdev;
        btdev.getDeviceForAddress(addy.toStdString(),[](int fd){
                DebugOut(0)<<"I am connected"<<endl;
-
-               SerialPort *s = new SerialPort();
                s->setDescriptor(fd);
 
-               QSocketNotifier* sock = new QSocketNotifier(fd, QSocketNotifier::Read);
-
-               QObject::connect(sock, &QSocketNotifier::activated, [&s](){
-                       DebugOut(0)<<"data: "<<s->read()<<endl;
-               });
-
-
+               GIOChannel *chan = g_io_channel_unix_new(s->fileDescriptor());
+               g_io_add_watch(chan, GIOCondition(G_IO_IN | G_IO_HUP | G_IO_ERR),(GIOFunc)readCallback, nullptr);
+               g_io_channel_set_close_on_unref(chan, true);
+               g_io_channel_unref(chan);
 
+               s->write("ping");
        });
 
        return app.exec();