2 Copyright (C) 2012 Intel Corporation
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "bluetoothplugin.h"
20 #include "timestamp.h"
21 #include "listplusplus.h"
24 #include <boost/assert.hpp>
25 #include <boost/lexical_cast.hpp>
28 #include <QDBusConnection>
29 #include <QDBusInterface>
31 #include <QSocketNotifier>
37 bool readCallback(GIOChannel *source, GIOCondition condition, gpointer data)
39 // DebugOut(5) << "Polling..." << condition << endl;
41 if(condition & G_IO_ERR)
43 DebugOut(DebugOut::Error)<<"GpsNmeaSource polling error."<<endl;
46 if (condition & G_IO_HUP)
48 //Hang up. Returning false closes out the GIOChannel.
49 //printf("Callback on G_IO_HUP\n");
50 DebugOut(DebugOut::Warning)<<"socket hangup event..."<<endl;
54 BluetoothSinkPlugin* p = static_cast<BluetoothSinkPlugin*>(data);
61 BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map<string, string> config)
62 :AbstractSink(re, config)
67 extern "C" void create(AbstractRoutingEngine* routingengine, map<string, string> config)
69 new BluetoothSinkPlugin(routingengine, config);
72 const string BluetoothSinkPlugin::uuid()
74 return "47ec4ed0-dc45-11e3-9c1a-0800200c9a66";
77 void BluetoothSinkPlugin::propertyChanged(AbstractPropertyType *value)
82 void BluetoothSinkPlugin::dataReceived(QByteArray data)
87 AbstractBluetoothSerialProfile::AbstractBluetoothSerialProfile(QString r)
90 new BtProfileAdaptor(this);
92 if(!QDBusConnection::systemBus().registerService("org.automotive.message.broker.bluetooth"))
93 DebugOut(DebugOut::Error)<<"Failed to register DBus service name: "<<QDBusConnection::systemBus().lastError().message().toStdString()<<endl;
95 if(!QDBusConnection::systemBus().registerObject("/org/bluez/spp", this))
96 DebugOut(DebugOut::Error)<<"Failed to register DBus object"<<endl;
98 QDBusInterface profileManagerIface("org.bluez", "/org/bluez", "org.bluez.ProfileManager1", QDBusConnection::systemBus());
101 options["Name"] = "AMB spp";
102 options["Role"] = role;
103 options["Channel"] = qVariantFromValue(uint16_t(23));
104 options["AutoConnect"] = true;
106 QDBusReply<void> reply = profileManagerIface.call("RegisterProfile", qVariantFromValue(QDBusObjectPath("/org/bluez/spp")), "00001101-0000-1000-8000-00805F9B34FB", options);
110 DebugOut(DebugOut::Error)<<"RegisterProfile call failed: "<<reply.error().message().toStdString()<<endl;
114 void AbstractBluetoothSerialProfile::release()
116 DebugOut()<<"release called."<<endl;
119 void AbstractBluetoothSerialProfile::newConnection(string path, QDBusUnixFileDescriptor fd, QVariantMap props)
121 DebugOut()<<"new Connection! Path: "<<path<<" fd: "<<fd.fileDescriptor()<<endl;
123 socket.setDescriptor(fd.fileDescriptor());
124 socket.write("ping");
126 GIOChannel *chan = g_io_channel_unix_new(socket.fileDescriptor());
127 g_io_add_watch(chan, GIOCondition(G_IO_IN | G_IO_HUP | G_IO_ERR),(GIOFunc)readCallback, this);
128 g_io_channel_set_close_on_unref(chan, true);
129 g_io_channel_unref(chan);
132 void AbstractBluetoothSerialProfile::requestDisconnection(string path)
134 DebugOut()<<"requestDisconnection called. Path: "<<path<<endl;
138 void AbstractBluetoothSerialProfile::canHasData()
140 QByteArray data = socket.read().c_str();
142 DebugOut()<<"data read: "<<data.constData()<<endl;
147 BtProfileAdaptor::BtProfileAdaptor(AbstractBluetoothSerialProfile *parent)
148 :QDBusAbstractAdaptor(parent), mParent(parent)
153 void BtProfileAdaptor::Release()
158 void BtProfileAdaptor::NewConnection(QDBusObjectPath device, QDBusUnixFileDescriptor fd, QVariantMap fd_properties)
160 mParent->newConnection(device.path().toStdString(), fd, fd_properties);
163 void BtProfileAdaptor::RequestDisconnection(QDBusObjectPath device)
165 mParent->requestDisconnection(device.path().toStdString());