added service record for spp
[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 QString SPP_RECORD = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
37                                                                                                                    "<record>"
38                                                                                                                          "<attribute id=\"0x0001\">"
39                                                                                                                            "<sequence>"
40                                                                                                                                  "<uuid value=\"0x1101\"/>"
41                                                                                                                            "</sequence>"
42                                                                                                                          "</attribute>"
43                                                                                                                          "<attribute id=\"0x0004\">"
44                                                                                                                            "<sequence>"
45                                                                                                                                  "<sequence>"
46                                                                                                                                    "<uuid value=\"0x0100\"/>"
47                                                                                                                                  "</sequence>"
48                                                                                                                                  "<sequence>"
49                                                                                                                                    "<uuid value=\"0x0003\"/>"
50                                                                                                                                    "<uint8 value=\"23\" name=\"channel\"/>"
51                                                                                                                                  "</sequence>"
52                                                                                                                            "</sequence>"
53                                                                                                                          "</attribute>"
54                                                                                                                          "<attribute id=\"0x0100\">"
55                                                                                                                            "<text value=\"COM5\" name=\"name\"/>"
56                                                                                                                          "</attribute>"
57                                                                                                                    "</record>";
58
59 BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map<string, string> config)
60 :AbstractSink(re, config)
61 {
62         new BtProfileAdaptor(this);
63
64         if(!QDBusConnection::systemBus().registerService("org.automotive.message.broker.bluetooth"))
65                 DebugOut(DebugOut::Error)<<"Failed to register DBus service name: "<<QDBusConnection::systemBus().lastError().message().toStdString()<<endl;
66
67         if(!QDBusConnection::systemBus().registerObject("/org/bluez/spp", this))
68                 DebugOut(DebugOut::Error)<<"Failed to register DBus object"<<endl;
69
70         QDBusInterface profileManagerIface("org.bluez", "/org/bluez", "org.bluez.ProfileManager1", QDBusConnection::systemBus());
71
72         QVariantMap options;
73         options["Name"] = "AMB spp server";
74         options["Role"] = "server";
75         options["ServiceRecord"] = SPP_RECORD;
76         options["Channel"] = 23;
77
78         QDBusReply<void> reply = profileManagerIface.call("RegisterProfile", qVariantFromValue(QDBusObjectPath("/org/bluez/spp")), "00001101-0000-1000-8000-00805F9B34FB", options);
79
80         if(!reply.isValid())
81         {
82                 DebugOut(DebugOut::Error)<<"RegisterProfile call failed: "<<reply.error().message().toStdString()<<endl;
83         }
84 }
85
86
87
88 extern "C" AbstractSink * create(AbstractRoutingEngine* routingengine, map<string, string> config)
89 {
90         return new BluetoothSinkPlugin(routingengine, config);
91         
92 }
93
94 const string BluetoothSinkPlugin::uuid()
95 {
96         return "47ec4ed0-dc45-11e3-9c1a-0800200c9a66";
97 }
98
99 void BluetoothSinkPlugin::propertyChanged(AbstractPropertyType *value)
100 {
101
102 }
103
104 void BluetoothSinkPlugin::release()
105 {
106         DebugOut()<<"release called."<<endl;
107 }
108
109 void BluetoothSinkPlugin::newConnection(string path, int fd, QVariantMap props)
110 {
111         DebugOut()<<"new Connection! Path: "<<path<<endl;
112
113         socket.setSocketDescriptor(fd);
114
115         connect(&socket, &QTcpSocket::readyRead, this, &BluetoothSinkPlugin::canHasData);
116 }
117
118 void BluetoothSinkPlugin::requestDisconnection(string path)
119 {
120         DebugOut()<<"requestDisconnection called.  Path: "<<path<<endl;
121         socket.close();
122 }
123
124 void BluetoothSinkPlugin::canHasData()
125 {
126         QByteArray data = socket.readAll();
127
128         DebugOut()<<"data read: "<<data.constData()<<endl;
129 }
130
131 BtProfileAdaptor::BtProfileAdaptor(BluetoothSinkPlugin *parent)
132         :QDBusAbstractAdaptor(parent), mParent(parent)
133 {
134
135 }
136
137 void BtProfileAdaptor::Release()
138 {
139         mParent->release();
140 }
141
142 void BtProfileAdaptor::NewConnection(QDBusObjectPath device, int fd, QVariantMap fd_properties)
143 {
144         mParent->newConnection(device.path().toStdString(), fd, fd_properties);
145 }
146
147 void BtProfileAdaptor::RequestDisconnection(QDBusObjectPath device)
148 {
149         mParent->requestDisconnection(device.path().toStdString());
150 }