fixed up bluez5 support code
authorKevron Rees <kevron.m.rees@intel.com>
Fri, 5 Sep 2014 16:05:25 +0000 (09:05 -0700)
committerKevron Rees <kevron.m.rees@intel.com>
Fri, 5 Sep 2014 16:05:25 +0000 (09:05 -0700)
lib/superptr.hpp
plugins/bluetooth/CMakeLists.txt
plugins/bluetooth/bluetoothplugin.cpp
plugins/bluetooth/bluetoothplugin.h
plugins/bluetooth/testAmbBt.cpp
plugins/common/bluetooth5.cpp
plugins/common/serialport.hpp
plugins/gpsnmea/gpsnmea.cpp
plugins/gpsnmea/gpsnmea.h

index 53de9ba..26e9087 100644 (file)
@@ -67,8 +67,8 @@ template<typename T> super_ptr<T> make_super(T* t) {
   return super_ptr<T>(t);
 }
 
-template<typename T> unique_ptr<T> make_unique(T* t) {
-  return unique_ptr<T>(t);
+template<typename T> ::std::unique_ptr<T> make_unique(T* t) {
+  return ::std::unique_ptr<T>(t);
 }
 
 }
index 27c13a1..360b0d2 100644 (file)
@@ -27,8 +27,7 @@ target_link_libraries(bluetoothplugin amb -L${CMAKE_CURRENT_BINARY_DIR}/lib  amb
 install(TARGETS bluetoothplugin LIBRARY DESTINATION lib/automotive-message-broker)
 install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/ambbt.conf DESTINATION /etc/dbus-1/system.d )
 
-
-add_executable(testAmbBt "testAmbBt.cpp")
+add_executable(testAmbBt testAmbBt.cpp bluetoothplugin.cpp)
 target_link_libraries(testAmbBt ${link_libraries} amb -L${CMAKE_CURRENT_BINARY_DIR}/lib amb-plugins-common -L${CMAKE_CURRENT_BINARY_DIR}/plugins/common ${QT_LIBRARIES})
 
 endif(bluetooth_plugin)
index b605726..e5ffa32 100644 (file)
@@ -61,6 +61,33 @@ bool readCallback(GIOChannel *source, GIOCondition condition, gpointer data)
 BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map<string, string> config)
 :AbstractSink(re, config)
 {
+
+}
+
+extern "C" AbstractSink * create(AbstractRoutingEngine* routingengine, map<string, string> config)
+{
+       return new BluetoothSinkPlugin(routingengine, config);
+
+}
+
+const string BluetoothSinkPlugin::uuid()
+{
+       return "47ec4ed0-dc45-11e3-9c1a-0800200c9a66";
+}
+
+void BluetoothSinkPlugin::propertyChanged(AbstractPropertyType *value)
+{
+
+}
+
+void BluetoothSinkPlugin::dataReceived(QByteArray data)
+{
+
+}
+
+AbstractBluetoothSerialProfile::AbstractBluetoothSerialProfile(QString r)
+       :role(r)
+{
        new BtProfileAdaptor(this);
 
        if(!QDBusConnection::systemBus().registerService("org.automotive.message.broker.bluetooth"))
@@ -72,9 +99,10 @@ BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map<string,
        QDBusInterface profileManagerIface("org.bluez", "/org/bluez", "org.bluez.ProfileManager1", QDBusConnection::systemBus());
 
        QVariantMap options;
-       options["Name"] = "AMB spp server";
-       options["Role"] = "server";
+       options["Name"] = "AMB spp";
+       options["Role"] = role;
        options["Channel"] = qVariantFromValue(uint16_t(23));
+       options["AutoConnect"] = true;
 
        QDBusReply<void> reply = profileManagerIface.call("RegisterProfile", qVariantFromValue(QDBusObjectPath("/org/bluez/spp")), "00001101-0000-1000-8000-00805F9B34FB", options);
 
@@ -84,30 +112,12 @@ BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map<string,
        }
 }
 
-
-
-extern "C" AbstractSink * create(AbstractRoutingEngine* routingengine, map<string, string> config)
-{
-       return new BluetoothSinkPlugin(routingengine, config);
-       
-}
-
-const string BluetoothSinkPlugin::uuid()
-{
-       return "47ec4ed0-dc45-11e3-9c1a-0800200c9a66";
-}
-
-void BluetoothSinkPlugin::propertyChanged(AbstractPropertyType *value)
-{
-
-}
-
-void BluetoothSinkPlugin::release()
+void AbstractBluetoothSerialProfile::release()
 {
        DebugOut()<<"release called."<<endl;
 }
 
-void BluetoothSinkPlugin::newConnection(string path, QDBusUnixFileDescriptor fd, QVariantMap props)
+void AbstractBluetoothSerialProfile::newConnection(string path, QDBusUnixFileDescriptor fd, QVariantMap props)
 {
        DebugOut()<<"new Connection! Path: "<<path<<" fd: "<<fd.fileDescriptor()<<endl;
 
@@ -120,23 +130,22 @@ void BluetoothSinkPlugin::newConnection(string path, QDBusUnixFileDescriptor fd,
        g_io_channel_unref(chan);
 }
 
-void BluetoothSinkPlugin::requestDisconnection(string path)
+void AbstractBluetoothSerialProfile::requestDisconnection(string path)
 {
        DebugOut()<<"requestDisconnection called.  Path: "<<path<<endl;
        socket.close();
 }
 
-void BluetoothSinkPlugin::canHasData()
+void AbstractBluetoothSerialProfile::canHasData()
 {
        QByteArray data = socket.read().c_str();
 
        DebugOut()<<"data read: "<<data.constData()<<endl;
 
-       if(data == "ping")
-               socket.write("pong");
+       dataReceived(data);
 }
 
-BtProfileAdaptor::BtProfileAdaptor(BluetoothSinkPlugin *parent)
+BtProfileAdaptor::BtProfileAdaptor(AbstractBluetoothSerialProfile *parent)
        :QDBusAbstractAdaptor(parent), mParent(parent)
 {
 
index 98ff09d..8c0bc98 100644 (file)
@@ -29,7 +29,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 #include <QTcpSocket>
 #include <QDBusUnixFileDescriptor>
 
-class BluetoothSinkPlugin;
+class AbstractBluetoothSerialProfile;
 
 class BtProfileAdaptor : public QDBusAbstractAdaptor
 {
@@ -38,7 +38,7 @@ class BtProfileAdaptor : public QDBusAbstractAdaptor
 
 public:
 
-       BtProfileAdaptor(BluetoothSinkPlugin* parent);
+       BtProfileAdaptor(AbstractBluetoothSerialProfile* parent);
 
 public Q_SLOTS:
 
@@ -50,34 +50,49 @@ public Q_SLOTS:
 
 private:
 
-       BluetoothSinkPlugin* mParent;
+       AbstractBluetoothSerialProfile* mParent;
 
 };
 
-class BluetoothSinkPlugin: public QObject, public AbstractSink
+class AbstractBluetoothSerialProfile: public QObject
 {
-Q_OBJECT
+       Q_OBJECT
 
 public:
-       BluetoothSinkPlugin(AbstractRoutingEngine* re, map<string, string> config);
-       
-       const string uuid();
+       AbstractBluetoothSerialProfile(QString role = "server");
 
-       void supportedChanged(const PropertyList &) {}
+       virtual void release();
 
-       void propertyChanged(AbstractPropertyType* value);
+       virtual void newConnection(std::string path, QDBusUnixFileDescriptor fd, QVariantMap props);
 
-       void release();
+       virtual void requestDisconnection(std::string path);
 
-       void newConnection(std::string path, QDBusUnixFileDescriptor fd, QVariantMap props);
+       virtual void canHasData();
 
-       void requestDisconnection(std::string path);
+protected:
+       virtual void dataReceived(QByteArray data) = 0;
+       SerialPort socket;
 
-       void canHasData();
+private:
+       QString role;
+};
 
+class BluetoothSinkPlugin: public AbstractBluetoothSerialProfile, public AbstractSink
+{
+       Q_OBJECT
+
+public:
+       BluetoothSinkPlugin(AbstractRoutingEngine* re, map<string, string> config);
+
+       const string uuid();
+
+       void supportedChanged(const PropertyList &) {}
+
+       void propertyChanged(AbstractPropertyType* value);
+
+protected:
+       virtual void dataReceived(QByteArray data);
 
-private:
-        SerialPort socket;
 };
 
 
index 53b470d..7b078a2 100644 (file)
@@ -5,32 +5,17 @@
 #include <QStringList>
 #include <QDebug>
 
-#include "bluetooth5.h"
-#include <serialport.hpp>
+#include "bluetoothplugin.h"
 
-SerialPort *s = new SerialPort();
-
-bool readCallback(GIOChannel *source, GIOCondition condition, gpointer data)
+class Bluetooth5: public AbstractBluetoothSerialProfile
 {
-//     DebugOut(5) << "Polling..." << condition << endl;
-
-       if(condition & G_IO_ERR)
-       {
-               DebugOut(DebugOut::Error)<<"GpsNmeaSource polling error."<<endl;
-       }
-
-       if (condition & G_IO_HUP)
+public:
+       Bluetooth5(): AbstractBluetoothSerialProfile("client") {}
+       void dataReceived(QByteArray d)
        {
-               //Hang up. Returning false closes out the GIOChannel.
-               //printf("Callback on G_IO_HUP\n");
-               DebugOut(DebugOut::Warning)<<"socket hangup event..."<<endl;
-               return false;
+               qDebug()<<"data: "<< d;
        }
-
-       DebugOut(0)<<"Data: "<<s->read();
-
-       return true;
-}
+};
 
 int main(int argc, char** argv)
 {
@@ -42,23 +27,9 @@ int main(int argc, char** argv)
 
        qDebug()<<"connecting to: "<<addy;
 
-       Bluetooth5 btdev;
-       btdev.getDeviceForAddress(addy.toStdString(),[](int fd)
-       {
-               qDebug()<<"I am connected "<<fd;
-               s->setDescriptor(fd);
-
-               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\r\n");
-       });
+       Bluetooth5 client;
 
        app.exec();
 
-       s->close();
-
        return 1;
 }
index b1934db..5244e70 100644 (file)
@@ -1,6 +1,9 @@
 #include "bluetooth5.h"
+#include "superptr.hpp"
+
 #include "debugout.h"
 #include <gio/gio.h>
+#include <gio/gunixfdlist.h>
 #include <string>
 
 static const gchar introspection_xml[] =
@@ -45,8 +48,40 @@ static void handleMethodCall(GDBusConnection       *connection,
                gint32 fd;
                GVariantIter* iter;
 
+               DebugOut() << "parameters signature: " << g_variant_get_type_string(parameters) << endl;
+
                g_variant_get(parameters,"(oha{sv})", &device, &fd, &iter);
 
+               DebugOut() << "device: " << device << endl;
+
+               auto message = g_dbus_method_invocation_get_message(invocation);
+
+               auto fdList = g_dbus_message_get_unix_fd_list(message);
+
+               GError* error = nullptr;
+
+               fd = g_unix_fd_list_get(fdList, 0, &error);
+
+               auto errorPtr = amb::make_super(error);
+
+               if(errorPtr)
+               {
+                       DebugOut(DebugOut::Error) << "Error trying to get fd: " << errorPtr->message << endl;
+                       return;
+               }
+
+               char* propertyName;
+               GVariant* value;
+
+               DebugOut() << "trying to see what properties we got with this call" << endl;
+
+               while(g_variant_iter_next(iter,"{sv}", &propertyName, &value))
+               {
+                       auto keyPtr = amb::make_super(propertyName);
+                       auto valuePtr = amb::make_super(value);
+                       DebugOut() << "key " << keyPtr.get() << "value signature: " << g_variant_get_type_string(valuePtr.get()) << endl;
+               }
+
                manager->connected(fd);
        }
        else if(method == "RequestDisconnection")
@@ -81,106 +116,33 @@ static const GDBusInterfaceVTable interfaceVTable =
        setProperty
 };
 
-
-std::string findAdapterPath(std::string adapterAddy)
+std::string findDevice(std::string address, std::string adapterPath="")
 {
-       std::string adapterObjectPath;
+       std::string objectPath;
 
-       GError * error = nullptr;
-       GDBusProxy * managerProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
+       GError * proxyError = nullptr;
+       auto managerProxy = amb::make_super(g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
                                                                                                                          "org.bluez",
                                                                                                                          "/",
                                                                                                                          "org.freedesktop.DBus.ObjectManager",
-                                                                                                                         nullptr,&error);
+                                                                                                                         nullptr, &proxyError));
 
-       if(error)
+       auto proxyErrorPtr = amb::make_super(proxyError);
+       if(proxyErrorPtr)
        {
-               DebugOut(DebugOut::Error)<<"Could not create ObjectManager proxy for Bluez: "<<error->message<<endl;
-               g_error_free(error);
+               DebugOut(DebugOut::Error)<<"Could not create ObjectManager proxy for Bluez: "<<proxyErrorPtr->message<<endl;
                return "";
        }
 
-       GVariant * objectMap = g_dbus_proxy_call_sync(managerProxy, "GetManagedObjects",nullptr, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+       GError * getManagerObjectError = nullptr;
 
-       if(error)
-       {
-               DebugOut(DebugOut::Error)<<"Failed call to GetManagedObjects: "<<error->message<<endl;
-               g_object_unref(managerProxy);
-               g_error_free(error);
-               return "";
-       }
-
-       GVariantIter* iter;
-       char* objPath;
-       GVariantIter* level2Dict;
+       auto objectMap = amb::make_super(g_dbus_proxy_call_sync(managerProxy.get(), "GetManagedObjects",nullptr, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &getManagerObjectError));
 
-       g_variant_get(objectMap, "(a{oa{sa{sv}}})",&iter);
+       auto getManagerObjectErrorPtr = amb::make_super(getManagerObjectError);
 
-       while(g_variant_iter_next(iter, "(oa{sa{sv}})",&objPath, &level2Dict))
+       if(getManagerObjectErrorPtr)
        {
-               char * interfaceName;
-               GVariantIter* innerDict;
-               while(g_variant_iter_next(level2Dict, "sa{sv}", &interfaceName, &innerDict))
-               {
-                       if(std::string(interfaceName) == "org.bluez.Adapter1")
-                       {
-                               char* propertyName;
-                               GVariant* value;
-
-                               while(adapterObjectPath == "" && g_variant_iter_next(innerDict,"sv", &propertyName, &value))
-                               {
-                                       if(std::string(propertyName) == "Address")
-                                       {
-                                               char* addy;
-                                               g_variant_get(value,"s",&addy);
-
-                                               if(adapterAddy == "" || addy && std::string(addy) == adapterAddy)
-                                               {
-                                                       adapterObjectPath = objPath;
-                                               }
-
-                                               g_free(addy);
-                                       }
-                                       g_free(propertyName);
-                                       g_variant_unref(value);
-                               }
-                       }
-                       g_free(interfaceName);
-                       g_variant_iter_free(innerDict);
-               }
-               g_free(objPath);
-               g_variant_iter_free(level2Dict);
-       }
-       g_variant_iter_free(iter);
-
-       return adapterObjectPath;
-}
-
-std::string findDevice(std::string addy, std::string adapterPath="")
-{
-       std::string objectPath;
-
-       GError * error = nullptr;
-       GDBusProxy * managerProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
-                                                                                                                         "org.bluez",
-                                                                                                                         "/",
-                                                                                                                         "org.freedesktop.DBus.ObjectManager",
-                                                                                                                         nullptr,&error);
-
-       if(error)
-       {
-               DebugOut(DebugOut::Error)<<"Could not create ObjectManager proxy for Bluez: "<<error->message<<endl;
-               g_error_free(error);
-               return "";
-       }
-
-       GVariant * objectMap = g_dbus_proxy_call_sync(managerProxy, "GetManagedObjects",nullptr, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
-
-       if(error)
-       {
-               DebugOut(DebugOut::Error)<<"Failed call to GetManagedObjects: "<<error->message<<endl;
-               g_object_unref(managerProxy);
-               g_error_free(error);
+               DebugOut(DebugOut::Error)<<"Failed call to GetManagedObjects: "<<getManagerObjectErrorPtr->message<<endl;
                return "";
        }
 
@@ -188,45 +150,49 @@ std::string findDevice(std::string addy, std::string adapterPath="")
        char* objPath;
        GVariantIter* level2Dict;
 
-       g_variant_get(objectMap, "(a{oa{sa{sv}}})",&iter);
+       g_variant_get(objectMap.get(), "(a{oa{sa{sv}}})",&iter);
+
+       auto iterPtr = amb::make_super(iter);
+
 
        while(g_variant_iter_next(iter, "{oa{sa{sv}}}",&objPath, &level2Dict))
        {
+               auto level2DictPtr = amb::make_super(level2Dict);
+               auto objPathPtr = amb::make_super(objPath);
+
                char * interfaceName;
                GVariantIter* innerDict;
-               while(g_variant_iter_next(level2Dict, "{sa{sv}}", &interfaceName, &innerDict))
+               while(g_variant_iter_next(level2DictPtr.get(), "{sa{sv}}", &interfaceName, &innerDict))
                {
-                       if(std::string(interfaceName) == "org.bluez.Device1")
+                       auto interfaceNamePtr = amb::make_super(interfaceName);
+                       auto innerDictPtr = amb::make_super(innerDict);
+                       if(std::string(interfaceNamePtr.get()) == "org.bluez.Device1")
                        {
                                char* propertyName;
                                GVariant* value;
 
-                               while(objectPath == "" && g_variant_iter_next(innerDict,"{sv}", &propertyName, &value))
+                               while(objectPath == "" && g_variant_iter_next(innerDictPtr.get(),"{sv}", &propertyName, &value))
                                {
-                                       if(std::string(propertyName) == "Address")
+                                       auto propertyNamePtr = amb::make_super(propertyName);
+                                       auto valuePtr = amb::make_super(value);
+
+                                       if(std::string(propertyNamePtr.get()) == "Address")
                                        {
                                                char* addy;
-                                               g_variant_get(value,"s",&addy);
+                                               g_variant_get(valuePtr.get(),"s",&addy);
 
-                                               if(adapterPath == "" || addy && std::string(addy) == adapterPath)
+                                               auto addyPtr = amb::make_super(addy);
+
+                                               if(addyPtr && std::string(addyPtr.get()) == address)
                                                {
-                                                       objectPath = objPath;
+                                                       objectPath = objPathPtr.get();
                                                }
-
-                                               g_free(addy);
                                        }
                                        ///TODO: filter only devices that have the specified adapter
-                                       g_free(propertyName);
-                                       g_variant_unref(value);
                                }
                        }
-                       g_free(interfaceName);
-                       g_variant_iter_free(innerDict);
                }
-               g_free(objPath);
-               g_variant_iter_free(level2Dict);
        }
-       g_variant_iter_free(iter);
 
        return objectPath;
 }
@@ -271,8 +237,13 @@ Bluetooth5::Bluetooth5()
        g_variant_builder_add(&builder, "{sv}", "Role", g_variant_new("s","client"));
        g_variant_builder_add(&builder, "{sv}", "AutoConnect", g_variant_new("b",true));
 
-       g_dbus_connection_call_sync(mConnection, "org.bluez", "/org/bluez", "org.bluez.ProfileManager1", "RegisterProfile", g_variant_new("(osa{sv})", "/org/bluez/spp", "00001101-0000-1000-8000-00805F9B34FB",
-                                                                                                                                                                                                                                                                &builder), nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error);
+       g_dbus_connection_call_sync(mConnection,
+                                                               "org.bluez",
+                                                               "/org/bluez",
+                                                               "org.bluez.ProfileManager1",
+                                                               "RegisterProfile",
+                                                               g_variant_new("(osa{sv})", "/org/bluez/spp", "00001101-0000-1000-8000-00805F9B34FB", &builder),
+                                                               nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error);
 
        if(error)
        {
@@ -287,44 +258,52 @@ void Bluetooth5::getDeviceForAddress(std::string address, ConnectedCallback conn
 
        std::string devicePath = findDevice(address);
 
-       GDBusProxy * deviceProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,G_DBUS_PROXY_FLAGS_NONE,NULL,
-                                                                                                                         "org.bluez", devicePath.c_str(), "org.bluez.Device1", nullptr, nullptr);
-
-       GError* error = nullptr;
+       DebugOut() << "Bluetooth device path: " << devicePath << endl;
 
-       g_dbus_proxy_call(deviceProxy, "Connect", nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr,[](GObject *source_object,
-                                                                                                                                                                                          GAsyncResult *res,
-                                                                                                                                                                                          gpointer user_data)
+       if(devicePath == "")
        {
+               DebugOut(DebugOut::Error) << "device path not found.  Not paired? " << endl;
+       }
 
-               GError* error = nullptr;
-               GDBusProxy *deviceProxy = (GDBusProxy*)user_data;
+       GError* error = nullptr;
 
-               g_dbus_proxy_call_finish(G_DBUS_PROXY (source_object),res, &error);
+       GDBusProxy * deviceProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,G_DBUS_PROXY_FLAGS_NONE,NULL,
+                                                                                                                         "org.bluez", devicePath.c_str(), "org.bluez.Device1", nullptr, &error);
 
-               if(error)
-               {
-                       DebugOut(DebugOut::Error)<<"error trying to connect profile: "<<error->message<<endl;
-                       g_error_free(error);
-               }
+       auto errorPtr = amb::make_super(error);
 
-               g_object_unref(deviceProxy);
-       }, deviceProxy);
+       if(errorPtr)
+       {
+               DebugOut(DebugOut::Error) << "Error getting bluetooth device proxy " << errorPtr->message <<endl;
+               return;
+       }
 
+       g_dbus_proxy_call(deviceProxy, "Connect", nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr,
+                                         [](GObject *source_object, GAsyncResult *res, gpointer user_data)
+       {
 
+               GError* error = nullptr;
 
+               g_dbus_proxy_call_finish(G_DBUS_PROXY (source_object),res, &error);
 
+               auto errorPtr = amb::make_super(error);
 
+               if(errorPtr)
+               {
+                       DebugOut(DebugOut::Error) << "error trying to connect profile: " << errorPtr->message << endl;
+               }
+       },
+       this);
 }
 
 void Bluetooth5::connected(int fd)
 {
-       try
+       //try
        {
                mConnected(fd);
        }
-       catch(...)
+       //catch(...)
        {
-
+               //DebugOut(DebugOut::Error) << "Error calling connected callback" << endl;
        }
 }
index 0908687..238a4a4 100644 (file)
@@ -50,19 +50,19 @@ public:
                int ret = 0;
                switch(newspeed)
                {
-                       case 2400: 
+                       case 2400:
                                speed = B2400;
                                break;
-                       case 4800: 
+                       case 4800:
                                speed = B4800;
                                break;
-                       case 9600: 
+                       case 9600:
                                speed = B9600;
                                break;
-                       case 19200: 
+                       case 19200:
                                speed = B19200;
                                break;
-                       case 38400: 
+                       case 38400:
                                speed = B38400;
                                break;
                        default:
@@ -75,8 +75,6 @@ public:
        {
                fd = ::open(tty.c_str(), O_RDWR, O_NOCTTY);
 
-
-
                return setupDevice();
        }
 
@@ -97,12 +95,11 @@ public:
                char buff;
                std::string result;
                int bytesread = 0;
-               while( bytesread = ::read(fd,&buff,1) > 0 )
+               while( bytesread = ::read(fd, &buff, 1) > 0 )
                {
                        result += buff;
                }
 
-
                if(bytesread == -1)
                        perror("Error while reading: ");
 
index 1925945..c250d2b 100644 (file)
@@ -390,11 +390,11 @@ bool readCallback(GIOChannel *source, GIOCondition condition, gpointer data)
 extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
 {
        return new GpsNmeaSource(routingengine, config);
-       
+
 }
 
 GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map<string, string> config)
-       :AbstractSource(re,config), mUuid("33d86462-1708-4f78-a001-99ea8d55422b"), device(nullptr)
+       :AbstractSource(re,config), mUuid("33d86462-1708-4f78-a001-99ea8d55422b"), device(nullptr), bt(nullptr)
 {
        int baudrate = 0;
        location =new Location(re, mUuid);
@@ -425,11 +425,14 @@ GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map<string, string> conf
        if(config.find("device")!= config.end())
        {
                std::string dev = config["device"];
+
+#ifdef USE_BLUEZ5
                if(dev.find(":") != string::npos)
                {
-#ifdef USE_BLUEZ5
-                       Bluetooth5 bt;
-                       bt.getDeviceForAddress(dev,[this](int fd) {
+
+                       bt = new Bluetooth5();
+                       bt->getDeviceForAddress(dev, [this](int fd) {
+                               DebugOut() << "fd: " << fd << endl;
                                device = new SerialPort(fd);
                                int baudrate=0;
 
@@ -439,59 +442,73 @@ GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map<string, string> conf
                                                DebugOut(DebugOut::Error)<<"Unsupported baudrate " << configuration["baudrate"] << endl;
                                }
 
-                               if(!device->open())
-                               {
-                                       DebugOut(DebugOut::Error)<<"Failed to open gps tty: "<<configuration["device"]<<endl;
-                                       perror("Error");
-                                       return;
-                               }
-
                                DebugOut()<<"read from device: "<<device->read()<<endl;
 
                                GIOChannel *chan = g_io_channel_unix_new(device->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); //Pass ownership of the GIOChannel to the watch.
+                               g_io_channel_unref(chan);
                        });
+#else
+                       bt = new BluetoothDevice();
+                       dev = bt->getDeviceForAddress(dev, btaddapter);
 
-               }
-       }
+                       device = new SerialPort(dev);
 
-#else
-                       BluetoothDevice bt;
-                       dev = bt.getDeviceForAddress(dev, btaddapter);
+                       if(baudrate!=0)
+                       {
+                               if((static_cast<SerialPort*>(device))->setSpeed(baudrate))
+                                       DebugOut(DebugOut::Error)<<"Unsupported baudrate " << config["baudrate"] << endl;
+                       }
 
-               }
+                       if(!device->open())
+                       {
+                               DebugOut(DebugOut::Error)<<"Failed to open gps tty: "<<config["device"]<<endl;
+                               perror("Error");
+                               return;
+                       }
 
-               device = new SerialPort(dev);
+                       DebugOut()<<"read from device: "<<device->read()<<endl;
 
-               if(baudrate!=0)
-               {
-                       if((static_cast<SerialPort*>(device))->setSpeed(baudrate))
-                               DebugOut(DebugOut::Error)<<"Unsupported baudrate " << config["baudrate"] << endl;
+                       GIOChannel *chan = g_io_channel_unix_new(device->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);
+#endif
                }
-
-               if(!device->open())
+               else
                {
-                       DebugOut(DebugOut::Error)<<"Failed to open gps tty: "<<config["device"]<<endl;
-                       perror("Error");
-                       return;
-               }
+                       device = new SerialPort(dev);
 
-               DebugOut()<<"read from device: "<<device->read()<<endl;
+                       if(baudrate!=0)
+                       {
+                               if((static_cast<SerialPort*>(device))->setSpeed(baudrate))
+                                       DebugOut(DebugOut::Error)<<"Unsupported baudrate " << config["baudrate"] << endl;
+                       }
+
+                       if(!device->open())
+                       {
+                               DebugOut(DebugOut::Error)<<"Failed to open gps tty: "<<config["device"]<<endl;
+                               perror("Error");
+                               return;
+                       }
 
-               GIOChannel *chan = g_io_channel_unix_new(device->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); //Pass ownership of the GIOChannel to the watch.
+                       DebugOut()<<"read from device: "<<device->read()<<endl;
+
+                       GIOChannel *chan = g_io_channel_unix_new(device->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);
+               }
        }
-#endif
 }
 
 GpsNmeaSource::~GpsNmeaSource()
 {
        if(device && device->isOpen())
                device->close();
+       if(bt)
+               delete bt;
 }
 
 const string GpsNmeaSource::uuid()
index 2179ed5..5336a97 100644 (file)
@@ -27,6 +27,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 using namespace std;
 
 class Location;
+class Bluetooth5;
+class BluetoothDevice;
 
 class GpsNmeaSource: public AbstractSource
 {
@@ -34,7 +36,7 @@ class GpsNmeaSource: public AbstractSource
 public:
        GpsNmeaSource(AbstractRoutingEngine* re, map<string, string> config);
        ~GpsNmeaSource();
-       
+
        const string uuid();
        void getPropertyAsync(AsyncPropertyReply *reply);
        void getRangePropertyAsync(AsyncRangePropertyReply *reply);
@@ -44,9 +46,9 @@ public:
        PropertyList supported();
 
        int supportedOperations();
-       
+
        void supportedChanged(const PropertyList &) {}
-       
+
        PropertyInfo getPropertyInfo(VehicleProperty::Property property)
        {
                if(propertyInfoMap.find(property) != propertyInfoMap.end())
@@ -60,9 +62,6 @@ public:
        void test();
 
 private:
-
-
-
        bool tryParse(std::string data);
 
        void addPropertySupport(VehicleProperty::Property property, Zone::Type zone);
@@ -80,6 +79,11 @@ private:
        std::string mUuid;
 
        std::string buffer;
+#ifdef USE_BLUEZ5
+       Bluetooth5 * bt;
+#else
+       BluetoothDevice *bt
+#endif
 };
 
 #endif // EXAMPLEPLUGIN_H