e5ffa32802404fdeaba35d9077629dda6ee88e53
[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 #include <QSocketNotifier>
32
33 using namespace std;
34
35 #include "debugout.h"
36
37 bool readCallback(GIOChannel *source, GIOCondition condition, gpointer data)
38 {
39 //      DebugOut(5) << "Polling..." << condition << endl;
40
41         if(condition & G_IO_ERR)
42         {
43                 DebugOut(DebugOut::Error)<<"GpsNmeaSource polling error."<<endl;
44         }
45
46         if (condition & G_IO_HUP)
47         {
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;
51                 return false;
52         }
53
54         BluetoothSinkPlugin* p = static_cast<BluetoothSinkPlugin*>(data);
55
56         p->canHasData();
57
58         return true;
59 }
60
61 BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map<string, string> config)
62 :AbstractSink(re, config)
63 {
64
65 }
66
67 extern "C" AbstractSink * create(AbstractRoutingEngine* routingengine, map<string, string> config)
68 {
69         return new BluetoothSinkPlugin(routingengine, config);
70
71 }
72
73 const string BluetoothSinkPlugin::uuid()
74 {
75         return "47ec4ed0-dc45-11e3-9c1a-0800200c9a66";
76 }
77
78 void BluetoothSinkPlugin::propertyChanged(AbstractPropertyType *value)
79 {
80
81 }
82
83 void BluetoothSinkPlugin::dataReceived(QByteArray data)
84 {
85
86 }
87
88 AbstractBluetoothSerialProfile::AbstractBluetoothSerialProfile(QString r)
89         :role(r)
90 {
91         new BtProfileAdaptor(this);
92
93         if(!QDBusConnection::systemBus().registerService("org.automotive.message.broker.bluetooth"))
94                 DebugOut(DebugOut::Error)<<"Failed to register DBus service name: "<<QDBusConnection::systemBus().lastError().message().toStdString()<<endl;
95
96         if(!QDBusConnection::systemBus().registerObject("/org/bluez/spp", this))
97                 DebugOut(DebugOut::Error)<<"Failed to register DBus object"<<endl;
98
99         QDBusInterface profileManagerIface("org.bluez", "/org/bluez", "org.bluez.ProfileManager1", QDBusConnection::systemBus());
100
101         QVariantMap options;
102         options["Name"] = "AMB spp";
103         options["Role"] = role;
104         options["Channel"] = qVariantFromValue(uint16_t(23));
105         options["AutoConnect"] = true;
106
107         QDBusReply<void> reply = profileManagerIface.call("RegisterProfile", qVariantFromValue(QDBusObjectPath("/org/bluez/spp")), "00001101-0000-1000-8000-00805F9B34FB", options);
108
109         if(!reply.isValid())
110         {
111                 DebugOut(DebugOut::Error)<<"RegisterProfile call failed: "<<reply.error().message().toStdString()<<endl;
112         }
113 }
114
115 void AbstractBluetoothSerialProfile::release()
116 {
117         DebugOut()<<"release called."<<endl;
118 }
119
120 void AbstractBluetoothSerialProfile::newConnection(string path, QDBusUnixFileDescriptor fd, QVariantMap props)
121 {
122         DebugOut()<<"new Connection! Path: "<<path<<" fd: "<<fd.fileDescriptor()<<endl;
123
124         socket.setDescriptor(fd.fileDescriptor());
125         socket.write("ping");
126
127         GIOChannel *chan = g_io_channel_unix_new(socket.fileDescriptor());
128         g_io_add_watch(chan, GIOCondition(G_IO_IN | G_IO_HUP | G_IO_ERR),(GIOFunc)readCallback, this);
129         g_io_channel_set_close_on_unref(chan, true);
130         g_io_channel_unref(chan);
131 }
132
133 void AbstractBluetoothSerialProfile::requestDisconnection(string path)
134 {
135         DebugOut()<<"requestDisconnection called.  Path: "<<path<<endl;
136         socket.close();
137 }
138
139 void AbstractBluetoothSerialProfile::canHasData()
140 {
141         QByteArray data = socket.read().c_str();
142
143         DebugOut()<<"data read: "<<data.constData()<<endl;
144
145         dataReceived(data);
146 }
147
148 BtProfileAdaptor::BtProfileAdaptor(AbstractBluetoothSerialProfile *parent)
149         :QDBusAbstractAdaptor(parent), mParent(parent)
150 {
151
152 }
153
154 void BtProfileAdaptor::Release()
155 {
156         mParent->release();
157 }
158
159 void BtProfileAdaptor::NewConnection(QDBusObjectPath device, QDBusUnixFileDescriptor fd, QVariantMap fd_properties)
160 {
161         mParent->newConnection(device.path().toStdString(), fd, fd_properties);
162 }
163
164 void BtProfileAdaptor::RequestDisconnection(QDBusObjectPath device)
165 {
166         mParent->requestDisconnection(device.path().toStdString());
167 }