added support for input devices
[profile/ivi/bluetooth-qt.git] / bluetoothdevice.cpp
1 /*  -*- Mode: C++ -*-
2  *
3  * meego handset bluetooth
4  * Copyright © 2010, Intel Corporation.
5  *
6  * This program is licensed under the terms and conditions of the
7  * Apache License, version 2.0.  The full text of the Apache License is at
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  */
11
12 #include "bluetoothdevice.h"
13 #include "blueadapter.h"
14 #include "bluemanager.h"
15 #include "btprofiles.h"
16 #include "audiosink.h"
17 #include "audiosource.h"
18 #include "headset.h"
19 #include "audio.h"
20 #include "input.h"
21
22 BluetoothDevice::BluetoothDevice(QDBusObjectPath path, QObject *parent) :
23         QObject(parent),m_device(new OrgBluezDeviceInterface("org.bluez",path.path(),QDBusConnection::systemBus(),this)),audio(NULL),input(NULL)
24 {
25     if(!m_device->isValid())
26         return;
27     QObject::connect(m_device,SIGNAL(PropertyChanged(QString,QDBusVariant)),
28                      this,SLOT(propertyChanged(QString,QDBusVariant)));
29 }
30
31 void BluetoothDevice::unpair()
32 {
33     OrgBluezManagerInterface manager(
34             "org.bluez",
35             "/", QDBusConnection::systemBus());
36
37     QDBusObjectPath adapterpath = manager.DefaultAdapter();
38
39     if(adapterpath.path().isEmpty()) return;
40
41     OrgBluezAdapterInterface adapter(
42             "org.bluez",
43             adapterpath.path(),
44             QDBusConnection::systemBus());
45
46     adapter.RemoveDevice(QDBusObjectPath(m_device->path()));
47 }
48
49 void BluetoothDevice::connectAudio()
50 {
51     if(isProfileSupported(BluetoothProfiles::a2sink) || isProfileSupported(BluetoothProfiles::hs))
52     {
53         if(!audio) audio = new OrgBluezAudioInterface("org.bluez", m_device->path(), QDBusConnection::systemBus(), this);
54
55         audio->Connect();
56
57         connect(audio,SIGNAL(PropertyChanged(QString,QDBusVariant)),this,SLOT(audioPropertiesChanged(QString,QDBusVariant)));
58     }
59 }
60 void BluetoothDevice::connectAudioSrc()
61 {
62
63     if(isProfileSupported(BluetoothProfiles::a2src))
64     {
65         OrgBluezAudioSourceInterface source("org.bluez",m_device->path(),
66                                             QDBusConnection::systemBus());
67         source.Connect();
68     }
69 }
70
71 QString BluetoothDevice::connectSerial()
72 {
73     if(isProfileSupported(BluetoothProfiles::spp))
74     {
75         QDBusInterface interface("org.bluez",m_device->path(),"org.bluez.Serial",QDBusConnection::systemBus());
76         QDBusReply<QString> reply = interface.call(QDBus::AutoDetect, "Connect","spp");
77
78         if(reply.isValid()) return reply;
79         else qDebug()<<"Error connecting spp profile: "<<reply.error().message();
80     }
81
82     return "";
83 }
84
85 void BluetoothDevice::connectInput()
86 {
87     if(isProfileSupported(BluetoothProfiles::hid))
88     {
89         if(!input)
90         {
91             input = new OrgBluezInputInterface("org.bluez", m_device->path(),
92                                                QDBusConnection::systemBus(), this);
93             connect(input,SIGNAL(PropertyChanged(QString,QDBusVariant)),this,
94                     SLOT(inputPropertiesChanged(QString,QDBusVariant)));
95         }
96         input->Connect();
97     }
98 }
99
100 void BluetoothDevice::disconnect()
101 {
102     m_device->Disconnect();
103 }
104
105 void BluetoothDevice::disconnectAudio()
106 {
107     if(!audio) audio = new OrgBluezAudioInterface("org.bluez", m_device->path(), QDBusConnection::systemBus(), this);
108
109     audio->Disconnect();
110 }
111
112 QStringList BluetoothDevice::profiles()
113 {
114     QVariantMap props = m_device->GetProperties();
115
116     QStringList uuidlist = props["UUIDs"].toStringList();
117
118     return uuidlist;
119 }
120
121 bool BluetoothDevice::isProfileSupported(QString profile)
122 {
123     QVariantMap props = m_device->GetProperties();
124
125     QStringList uuidlist = props["UUIDs"].toStringList();
126
127     return uuidlist.contains(profile.toLower());
128 }
129
130 bool BluetoothDevice::connected()
131 {
132     QVariantMap props = m_device->GetProperties();
133     return props["Connected"].toBool();
134 }
135
136 bool BluetoothDevice::audioConnected()
137 {
138     if(!audio)
139     {
140         audio = new OrgBluezAudioInterface("org.bluez",m_device->path(), QDBusConnection::systemBus(),this);
141         connect(audio,SIGNAL(PropertyChanged(QString,QDBusVariant)),this,SLOT(audioPropertiesChanged(QString,QDBusVariant)));
142     }
143
144     QVariantMap props = audio->GetProperties();
145     return props["State"].toString() == "connected";
146 }
147
148 bool BluetoothDevice::inputConnected()
149 {
150     if(!input)
151     {
152         input = new OrgBluezInputInterface("org.bluez",m_device->path(), QDBusConnection::systemBus(),this);
153         connect(input,SIGNAL(PropertyChanged(QString,QDBusVariant)),this, SLOT(inputPropertiesChanged(QString,QDBusVariant)));
154     }
155
156     QVariantMap props = input->GetProperties();
157     return props["Connected"].toBool();
158 }
159
160 QString BluetoothDevice::alias()
161 {
162     QVariantMap props = m_device->GetProperties();
163     return props["Alias"].toString();
164 }
165
166 QString BluetoothDevice::name()
167 {
168     QVariantMap props = m_device->GetProperties();
169     return props["Name"].toString();
170 }
171
172 QString BluetoothDevice::address()
173 {
174     QVariantMap props = m_device->GetProperties();
175     return props["Address"].toString();
176 }
177
178 QString BluetoothDevice::icon()
179 {
180     QVariantMap props = m_device->GetProperties();
181     return props["Icon"].toString();
182 }
183
184 QString BluetoothDevice::path()
185 {
186     return m_device->path();
187 }
188
189 void BluetoothDevice::propertyChanged(QString name,QDBusVariant value)
190 {
191     emit propertyChanged(name,value.variant());
192
193     if(name == "Connected")
194     {
195         emit connectedChanged(value.variant().toBool());
196     }
197     ///TODO: create individual signals for each property
198 }
199
200 void BluetoothDevice::audioPropertiesChanged(QString name,QDBusVariant value)
201 {
202     ///don't think i need to be doing this:
203     //emit propertyChanged(name, value.variant());
204
205     if(name == "State")
206     {
207         emit audioConnectedChanged(value.variant().toString() == "connected");
208     }
209 }
210
211 void BluetoothDevice::inputPropertiesChanged(QString name, QDBusVariant value)
212 {
213     qDebug()<<"Input properties changed: "<<name<<" "<<value.variant();
214     if(name == "Connected")
215     {
216         emit inputConnectedChanged(value.variant().toBool());
217     }
218 }