added dbus system config
[profile/ivi/automotive-message-broker.git] / plugins / bluetooth / bluetoothplugin.cpp
1 /*
2 Copyright (C) 2012 Intel Corporation
3
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.
8
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.
13
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
17 */
18
19 #include "bluetoothplugin.h"
20 #include "timestamp.h"
21 #include "listplusplus.h"
22
23 #include <iostream>
24 #include <boost/assert.hpp>
25 #include <boost/lexical_cast.hpp>
26 #include <glib.h>
27
28 #include <QDBusConnection>
29 #include <QDBusInterface>
30 #include <QDBusReply>
31
32 using namespace std;
33
34 #include "debugout.h"
35
36 BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map<string, string> config)
37 :AbstractSink(re, config)
38 {
39         new BtProfileAdaptor(this);
40
41         if(!QDBusConnection::systemBus().registerService("org.automotive.message.broker.bluetooth"))
42                 DebugOut(DebugOut::Error)<<"Failed to register DBus service name: "<<QDBusConnection::systemBus().lastError().message().toStdString()<<endl;
43
44         if(!QDBusConnection::systemBus().registerObject("/org/bluez/spp", this))
45                 DebugOut(DebugOut::Error)<<"Failed to register DBus object"<<endl;
46
47         QDBusInterface profileManagerIface("org.bluez", "/org/bluez", "org.bluez.ProfileManager1", QDBusConnection::systemBus());
48
49         QVariantMap options;
50         options["Name"] = "spp";
51         options["Role"] = "server";
52
53         QDBusReply<void> reply = profileManagerIface.call("RegisterProfile", qVariantFromValue(QDBusObjectPath("/org/bluez/spp")), "00001101-0000-1000-8000-00805F9B34FB", options);
54
55         if(!reply.isValid())
56         {
57                 DebugOut(DebugOut::Error)<<"RegisterProfile call failed: "<<reply.error().message().toStdString()<<endl;
58         }
59 }
60
61
62
63 extern "C" AbstractSink * create(AbstractRoutingEngine* routingengine, map<string, string> config)
64 {
65         return new BluetoothSinkPlugin(routingengine, config);
66         
67 }
68
69 const string BluetoothSinkPlugin::uuid()
70 {
71         return "47ec4ed0-dc45-11e3-9c1a-0800200c9a66";
72 }
73
74 void BluetoothSinkPlugin::propertyChanged(AbstractPropertyType *value)
75 {
76
77 }
78
79 void BluetoothSinkPlugin::release()
80 {
81         DebugOut()<<"release called."<<endl;
82 }
83
84 void BluetoothSinkPlugin::newConnection(string path, int fd, QVariantMap props)
85 {
86         DebugOut()<<"new Connection! Path: "<<path<<endl;
87
88         socket.setSocketDescriptor(fd);
89
90         connect(&socket, &QTcpSocket::readyRead, this, &BluetoothSinkPlugin::canHasData);
91 }
92
93 void BluetoothSinkPlugin::requestDisconnection(string path)
94 {
95         DebugOut()<<"requestDisconnection called.  Path: "<<path<<endl;
96         socket.close();
97 }
98
99 void BluetoothSinkPlugin::canHasData()
100 {
101         QByteArray data = socket.readAll();
102
103         DebugOut()<<"data read: "<<data.constData()<<endl;
104 }
105
106 BtProfileAdaptor::BtProfileAdaptor(BluetoothSinkPlugin *parent)
107         :QDBusAbstractAdaptor(parent), mParent(parent)
108 {
109
110 }
111
112 void BtProfileAdaptor::Release()
113 {
114         mParent->release();
115 }
116
117 void BtProfileAdaptor::NewConnection(QDBusObjectPath device, int fd, QVariantMap fd_properties)
118 {
119         mParent->newConnection(device.path().toStdString(), fd, fd_properties);
120 }
121
122 void BtProfileAdaptor::RequestDisconnection(QDBusObjectPath device)
123 {
124         mParent->requestDisconnection(device.path().toStdString());
125 }