- fixed base agent ignoring pass in object path
[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
21 BluetoothDevice::BluetoothDevice(QDBusObjectPath path, QObject *parent) :
22                 QObject(parent),m_device(new OrgBluezDeviceInterface("org.bluez",path.path(),QDBusConnection::systemBus(),this)),audio(NULL)
23 {
24         if(!m_device->isValid())
25                 return;
26         QObject::connect(m_device,SIGNAL(PropertyChanged(QString,QDBusVariant)),
27                                          this,SLOT(propertyChanged(QString,QDBusVariant)));
28 }
29
30 void BluetoothDevice::unpair()
31 {
32         OrgBluezManagerInterface manager(
33                         "org.bluez",
34                         "/", QDBusConnection::systemBus());
35
36         QDBusObjectPath adapterpath = manager.DefaultAdapter();
37
38         if(adapterpath.path().isEmpty()) return;
39
40         OrgBluezAdapterInterface adapter(
41                         "org.bluez",
42                         adapterpath.path(),
43                         QDBusConnection::systemBus());
44
45         adapter.RemoveDevice(QDBusObjectPath(m_device->path()));
46 }
47
48 void BluetoothDevice::connectAudio()
49 {
50         if(isProfileSupported(BluetoothProfiles::a2sink) || isProfileSupported(BluetoothProfiles::hs))
51         {
52                 if(!audio) audio = new OrgBluezAudioInterface("org.bluez", m_device->path(), QDBusConnection::systemBus(), this);
53
54                 audio->Connect();
55
56                 connect(audio,SIGNAL(PropertyChanged(QString,QDBusVariant)),this,SLOT(audioPropertiesChanged(QString,QDBusVariant)));
57         }
58 }
59 void BluetoothDevice::connectAudioSrc()
60 {
61
62         if(isProfileSupported(BluetoothProfiles::a2src))
63         {
64                 OrgBluezAudioSourceInterface source("org.bluez",m_device->path(),
65                                                                                 QDBusConnection::systemBus());
66                 source.Connect();
67         }
68 }
69
70 QString BluetoothDevice::connectSerial()
71 {
72         if(isProfileSupported(BluetoothProfiles::spp))
73         {
74                 QDBusInterface interface("org.bluez",m_device->path(),"org.bluez.Serial",QDBusConnection::systemBus());
75                 QDBusReply<QString> reply = interface.call(QDBus::AutoDetect, "Connect","spp");
76
77                 if(reply.isValid()) return reply;
78                 else qDebug()<<"Error connecting spp profile: "<<reply.error().message();
79         }
80
81         return "";
82 }
83
84 void BluetoothDevice::disconnect()
85 {
86         m_device->Disconnect();
87 }
88
89 void BluetoothDevice::disconnectAudio()
90 {
91         if(!audio) audio = new OrgBluezAudioInterface("org.bluez", m_device->path(), QDBusConnection::systemBus(), this);
92
93         audio->Disconnect();
94 }
95
96 QStringList BluetoothDevice::profiles()
97 {
98         QVariantMap props = m_device->GetProperties();
99
100         QStringList uuidlist = props["UUIDs"].toStringList();
101
102         return uuidlist;
103 }
104
105 bool BluetoothDevice::isProfileSupported(QString profile)
106 {
107         QVariantMap props = m_device->GetProperties();
108
109         QStringList uuidlist = props["UUIDs"].toStringList();
110
111         return uuidlist.contains(profile.toLower());
112 }
113
114 bool BluetoothDevice::connected()
115 {
116         QVariantMap props = m_device->GetProperties();
117         return props["Connected"].toBool();
118 }
119
120 bool BluetoothDevice::audioConnected()
121 {
122         if(!audio)
123         {
124                 audio = new OrgBluezAudioInterface("org.bluez",m_device->path(), QDBusConnection::systemBus(),this);
125                 connect(audio,SIGNAL(PropertyChanged(QString,QDBusVariant)),this,SLOT(audioPropertiesChanged(QString,QDBusVariant)));
126         }
127
128         QVariantMap props = audio->GetProperties();
129         return props["State"].toString() == "connected";
130 }
131
132 QString BluetoothDevice::alias()
133 {
134         QVariantMap props = m_device->GetProperties();
135         return props["Alias"].toString();
136 }
137
138 QString BluetoothDevice::name()
139 {
140         QVariantMap props = m_device->GetProperties();
141         return props["Name"].toString();
142 }
143
144 QString BluetoothDevice::address()
145 {
146         QVariantMap props = m_device->GetProperties();
147         return props["Address"].toString();
148 }
149
150 QString BluetoothDevice::icon()
151 {
152         QVariantMap props = m_device->GetProperties();
153         return props["Icon"].toString();
154 }
155
156 QString BluetoothDevice::path()
157 {
158         return m_device->path();
159 }
160
161 void BluetoothDevice::propertyChanged(QString name,QDBusVariant value)
162 {
163         emit propertyChanged(name,value.variant());
164
165         if(name == "Connected")
166         {
167                 emit connectedChanged(value.variant().toBool());
168         }
169         ///TODO: create individual signals for each property
170 }
171
172 void BluetoothDevice::audioPropertiesChanged(QString name,QDBusVariant value)
173 {
174         emit propertyChanged(name, value.variant());
175
176         if(name == "State")
177         {
178                 emit audioConnectedChanged(value.variant().toString() == "connected");
179         }
180 }