reverted varianttype
[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" void create(AbstractRoutingEngine* routingengine, map<string, string> config)
68 {
69         new BluetoothSinkPlugin(routingengine, config);
70 }
71
72 const string BluetoothSinkPlugin::uuid()
73 {
74         return "47ec4ed0-dc45-11e3-9c1a-0800200c9a66";
75 }
76
77 void BluetoothSinkPlugin::propertyChanged(AbstractPropertyType *value)
78 {
79
80 }
81
82 void BluetoothSinkPlugin::dataReceived(QByteArray data)
83 {
84
85 }
86
87 AbstractBluetoothSerialProfile::AbstractBluetoothSerialProfile(QString r)
88         :role(r)
89 {
90         new BtProfileAdaptor(this);
91
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;
94
95         if(!QDBusConnection::systemBus().registerObject("/org/bluez/spp", this))
96                 DebugOut(DebugOut::Error)<<"Failed to register DBus object"<<endl;
97
98         QDBusInterface profileManagerIface("org.bluez", "/org/bluez", "org.bluez.ProfileManager1", QDBusConnection::systemBus());
99
100         QVariantMap options;
101         options["Name"] = "AMB spp";
102         options["Role"] = role;
103         options["Channel"] = qVariantFromValue(uint16_t(23));
104         options["AutoConnect"] = true;
105
106         QDBusReply<void> reply = profileManagerIface.call("RegisterProfile", qVariantFromValue(QDBusObjectPath("/org/bluez/spp")), "00001101-0000-1000-8000-00805F9B34FB", options);
107
108         if(!reply.isValid())
109         {
110                 DebugOut(DebugOut::Error)<<"RegisterProfile call failed: "<<reply.error().message().toStdString()<<endl;
111         }
112 }
113
114 void AbstractBluetoothSerialProfile::release()
115 {
116         DebugOut()<<"release called."<<endl;
117 }
118
119 void AbstractBluetoothSerialProfile::newConnection(string path, QDBusUnixFileDescriptor fd, QVariantMap props)
120 {
121         DebugOut()<<"new Connection! Path: "<<path<<" fd: "<<fd.fileDescriptor()<<endl;
122
123         socket.setDescriptor(fd.fileDescriptor());
124         socket.write("ping");
125
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);
130 }
131
132 void AbstractBluetoothSerialProfile::requestDisconnection(string path)
133 {
134         DebugOut()<<"requestDisconnection called.  Path: "<<path<<endl;
135         socket.close();
136 }
137
138 void AbstractBluetoothSerialProfile::canHasData()
139 {
140         QByteArray data = socket.read().c_str();
141
142         DebugOut()<<"data read: "<<data.constData()<<endl;
143
144         dataReceived(data);
145 }
146
147 BtProfileAdaptor::BtProfileAdaptor(AbstractBluetoothSerialProfile *parent)
148         :QDBusAbstractAdaptor(parent), mParent(parent)
149 {
150
151 }
152
153 void BtProfileAdaptor::Release()
154 {
155         mParent->release();
156 }
157
158 void BtProfileAdaptor::NewConnection(QDBusObjectPath device, QDBusUnixFileDescriptor fd, QVariantMap fd_properties)
159 {
160         mParent->newConnection(device.path().toStdString(), fd, fd_properties);
161 }
162
163 void BtProfileAdaptor::RequestDisconnection(QDBusObjectPath device)
164 {
165         mParent->requestDisconnection(device.path().toStdString());
166 }