00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "bluez-bluetoothinterface.h"
00023
00024 #include <QtDBus>
00025
00026 #include <solid/control/bluetoothinterface.h>
00027
00028 #include "bluez-bluetoothremotedevice.h"
00029 #include <KDebug>
00030
00031
00032 class BluezBluetoothInterfacePrivate
00033 {
00034 public:
00035 BluezBluetoothInterfacePrivate(const QString & objPath)
00036 : iface("org.bluez",
00037 objPath,
00038 "org.bluez.Adapter",
00039 QDBusConnection::systemBus()),
00040 objectPath(objPath)
00041 { }
00042 QDBusInterface iface;
00043 QString objectPath;
00044
00045 QMap<QString, BluezBluetoothRemoteDevice *> devices;
00046 };
00047
00048 BluezBluetoothInterface::BluezBluetoothInterface(const QString & objectPath)
00049 : BluetoothInterface(0), d(new BluezBluetoothInterfacePrivate(objectPath))
00050 {
00051
00052 #define connectInterfaceToThis(signal, slot) \
00053 d->iface.connection().connect("org.bluez", \
00054 objectPath, \
00055 "org.bluez.Adapter", \
00056 signal, this, SLOT(slot));
00057
00058 connectInterfaceToThis("ModeChanged", slotModeChanged(const QString &));
00059 connectInterfaceToThis("DiscoverableTimeoutChanged", slotDiscoverableTimeoutChanged(int));
00060 connectInterfaceToThis("MinorClassChanged", slotMinorClassChanged(const QString &));
00061 connectInterfaceToThis("NameChanged", slotNameChanged(const QString &));
00062 connectInterfaceToThis("DiscoveryStarted", slotDiscoveryStarted());
00063 connectInterfaceToThis("DiscoveryCompleted", slotDiscoveryCompleted());
00064 connectInterfaceToThis("RemoteDeviceDisappeared", slotRemoteDeviceDisappeared(const QString &));
00065 connectInterfaceToThis("RemoteDeviceFound", slotRemoteDeviceFound(const QString &, uint, short));
00066 connectInterfaceToThis("RemoteNameUpdated", slotRemoteNameUpdated(const QString &, const QString &));
00067 connectInterfaceToThis("RemoteDeviceConnected", slotRemoteDeviceConnected(const QString &));
00068 connectInterfaceToThis("RemoteDeviceDisconnected", slotRemoteDeviceDisconnected(const QString &));
00069 connectInterfaceToThis("TrustAdded", slotTrustAdded(const QString &));
00070 connectInterfaceToThis("TrustRemoved", slotTrustRemoved(const QString &));
00071 connectInterfaceToThis("BondingCreated", slotBondingCreated(const QString &));
00072 connectInterfaceToThis("BondingRemoved", slotBondingRemoved(const QString &));
00073 }
00074
00075 BluezBluetoothInterface::~BluezBluetoothInterface()
00076 {
00077 delete d;
00078 }
00079
00080 QString BluezBluetoothInterface::ubi() const
00081 {
00082 return d->objectPath;
00083 }
00084
00085 QString BluezBluetoothInterface::address() const
00086 {
00087 return stringReply("GetAddress");
00088 }
00089
00090 QString BluezBluetoothInterface::version() const
00091 {
00092 return stringReply("GetVersion");
00093 }
00094
00095 QString BluezBluetoothInterface::revision() const
00096 {
00097 return stringReply("GetRevision");
00098 }
00099
00100 QString BluezBluetoothInterface::manufacturer() const
00101 {
00102 return stringReply("GetManufacturer");
00103 }
00104
00105 QString BluezBluetoothInterface::company() const
00106 {
00107 return stringReply("GetCompany");
00108 }
00109
00110 Solid::Control::BluetoothInterface::Mode BluezBluetoothInterface::mode() const
00111 {
00112 QString theMode = stringReply("GetMode");
00113 Solid::Control::BluetoothInterface::Mode modeEnum;
00114 if (theMode == "connectable")
00115 {
00116 modeEnum = Solid::Control::BluetoothInterface::Connectable;
00117 }
00118 else if (theMode == "discoverable")
00119 {
00120 modeEnum = Solid::Control::BluetoothInterface::Discoverable;
00121 } else {
00122 Q_ASSERT(theMode == "off");
00123 modeEnum = Solid::Control::BluetoothInterface::Off;
00124 }
00125 return modeEnum;
00126 }
00127
00128 int BluezBluetoothInterface::discoverableTimeout() const
00129 {
00130 QDBusReply< uint > timeout = d->iface.call("GetDiscoverableTimeout");
00131 if (timeout.isValid()) {
00132 return timeout.value();
00133 }
00134
00135 return -1;
00136 }
00137
00138 bool BluezBluetoothInterface::isDiscoverable() const
00139 {
00140 return boolReply("IsDiscoverable");
00141 }
00142
00143 QStringList BluezBluetoothInterface::listConnections() const
00144 {
00145 QStringList list = listReply("ListConnections");
00146 for (int i = 0; i < list.size(); i++) {
00147 list[i] = ubi() + '/' + list[i];
00148 }
00149 return list;
00150 }
00151
00152 QString BluezBluetoothInterface::majorClass() const
00153 {
00154 return stringReply("GetMajorClass");
00155 }
00156
00157 QStringList BluezBluetoothInterface::listAvailableMinorClasses() const
00158 {
00159 return listReply("ListAvailableMinorClasses");
00160 }
00161
00162 QString BluezBluetoothInterface::minorClass() const
00163 {
00164 return stringReply("GetMinorClass");
00165 }
00166
00167 QStringList BluezBluetoothInterface::serviceClasses() const
00168 {
00169 return listReply("GetServiceClasses");
00170 }
00171
00172 QString BluezBluetoothInterface::name() const
00173 {
00174 return stringReply("GetName");
00175 }
00176
00177 QString BluezBluetoothInterface::getRemoteName(const QString &mac)
00178 {
00179 return stringReply("GetRemoteName",mac);
00180 }
00181
00182 bool BluezBluetoothInterface::isTrusted(const QString &mac)
00183 {
00184 return boolReply("IsTrusted",mac);
00185 }
00186
00187 QStringList BluezBluetoothInterface::listBondings() const
00188 {
00189 return listReply("ListBondings");
00190 }
00191
00192 bool BluezBluetoothInterface::isPeriodicDiscoveryActive() const
00193 {
00194 return boolReply("IsPeriodicDiscovery");
00195 }
00196
00197 bool BluezBluetoothInterface::isPeriodicDiscoveryNameResolvingActive() const
00198 {
00199 return boolReply("IsPeriodicDiscoveryNameResolving");
00200 }
00201
00202 QStringList BluezBluetoothInterface::listRemoteDevices() const
00203 {
00204 QStringList list = listReply("ListRemoteDevices");
00205 for (int i = 0; i < list.size(); i++) {
00206 list[i] = ubi() + '/' + list[i];
00207 }
00208 return list;
00209 }
00210
00211 QStringList BluezBluetoothInterface::listRecentRemoteDevices(const QDateTime &) const
00212 {
00213 return listReply("ListRecentRemoteDevices");
00214 }
00215
00216 void BluezBluetoothInterface::setMode(const Solid::Control::BluetoothInterface::Mode mode)
00217 {
00218 QString modeString;
00219 switch(mode)
00220 {
00221 case Solid::Control::BluetoothInterface::Off:
00222 modeString = "off";
00223 break;
00224 case Solid::Control::BluetoothInterface::Discoverable:
00225 modeString = "discoverable";
00226 break;
00227 case Solid::Control::BluetoothInterface::Connectable:
00228 modeString = "connectable";
00229 break;
00230 }
00231 d->iface.call("SetMode", modeString);
00232 }
00233
00234 void BluezBluetoothInterface::setDiscoverableTimeout(int timeout)
00235 {
00236 d->iface.call("SetDiscoverableTimeout", (uint)timeout);
00237 }
00238
00239 void BluezBluetoothInterface::setMinorClass(const QString &minorClass)
00240 {
00241 d->iface.call("SetMinorClass", minorClass);
00242 }
00243
00244 void BluezBluetoothInterface::setName(const QString &name)
00245 {
00246 d->iface.call("SetName", name);
00247 }
00248
00249 void BluezBluetoothInterface::discoverDevices()
00250 {
00251 d->iface.call("DiscoverDevices");
00252 }
00253
00254 void BluezBluetoothInterface::discoverDevicesWithoutNameResolving()
00255 {
00256 d->iface.call("DiscoverDevicesWithoutNameResolving");
00257 }
00258
00259 void BluezBluetoothInterface::cancelDiscovery()
00260 {
00261 d->iface.call("CancelDiscovery");
00262 }
00263
00264 void BluezBluetoothInterface::startPeriodicDiscovery()
00265 {
00266 d->iface.call("StartPeriodicDiscovery");
00267 }
00268
00269 void BluezBluetoothInterface::stopPeriodicDiscovery()
00270 {
00271 d->iface.call("StopPeriodicDiscovery");
00272 }
00273
00274 void BluezBluetoothInterface::setPeriodicDiscoveryNameResolving(bool nameResolving)
00275 {
00276 d->iface.call("SetPeriodicDiscoveryNameResolving", nameResolving);
00277 }
00278
00279 void BluezBluetoothInterface::setTrusted(const QString& mac)
00280 {
00281 d->iface.call("SetTrusted", mac);
00282 }
00283
00284 void BluezBluetoothInterface::removeTrust(const QString& mac)
00285 {
00286 d->iface.call("RemoveTrust", mac);
00287 }
00288
00289 void BluezBluetoothInterface::slotModeChanged(const Solid::Control::BluetoothInterface::Mode mode)
00290 {
00291 emit modeChanged(mode);
00292 }
00293
00294 void BluezBluetoothInterface::slotDiscoverableTimeoutChanged(int timeout)
00295 {
00296 emit discoverableTimeoutChanged(timeout);
00297 }
00298
00299 void BluezBluetoothInterface::slotMinorClassChanged(const QString &minorClass)
00300 {
00301 emit minorClassChanged(minorClass);
00302 }
00303
00304 void BluezBluetoothInterface::slotNameChanged(const QString &name)
00305 {
00306 emit nameChanged(name);
00307 }
00308
00309 void BluezBluetoothInterface::slotDiscoveryStarted()
00310 {
00311 emit discoveryStarted();
00312 }
00313
00314 void BluezBluetoothInterface::slotDiscoveryCompleted()
00315 {
00316 emit discoveryCompleted();
00317 }
00318
00319 void BluezBluetoothInterface::slotRemoteDeviceFound(const QString &address, uint deviceClass, short rssi)
00320 {
00321 QString remoteubi = QString("%1/%2").arg(ubi()).arg(address);
00322 emit remoteDeviceFound(remoteubi, deviceClass, rssi);
00323 }
00324
00325 void BluezBluetoothInterface::slotRemoteDeviceDisappeared(const QString &address)
00326 {
00327 QString remoteubi = QString("%1/%2").arg(ubi()).arg(address);
00328 emit remoteDeviceDisappeared(remoteubi);
00329 }
00330
00331 void BluezBluetoothInterface::slotRemoteNameUpdated(const QString &address, const QString& name)
00332 {
00333 emit remoteNameUpdated(address,name);
00334 }
00335
00336 void BluezBluetoothInterface::slotRemoteDeviceConnected(const QString &address)
00337 {
00338 emit remoteDeviceConnected(address);
00339 }
00340
00341 void BluezBluetoothInterface::slotRemoteDeviceDisconnected(const QString &address)
00342 {
00343 emit remoteDeviceDisconnected(address);
00344 }
00345
00346 void BluezBluetoothInterface::slotTrustAdded(const QString &address)
00347 {
00348 emit trustAdded(address);
00349 }
00350
00351 void BluezBluetoothInterface::slotTrustRemoved(const QString &address)
00352 {
00353 emit trustRemoved(address);
00354 }
00355
00356 void BluezBluetoothInterface::slotBondingCreated(const QString &address)
00357 {
00358 emit bondingCreated(address);
00359 }
00360
00361 void BluezBluetoothInterface::slotBondingRemoved(const QString &address)
00362 {
00363 emit bondingRemoved(address);
00364 }
00365
00366 QObject *BluezBluetoothInterface::createBluetoothRemoteDevice(const QString &ubi)
00367 {
00368 BluezBluetoothRemoteDevice *bluetoothInterface;
00369 if (d->devices.contains(ubi)) {
00370 bluetoothInterface = d->devices[ubi];
00371 } else {
00372 bluetoothInterface = new BluezBluetoothRemoteDevice(ubi);
00373 d->devices.insert(ubi, bluetoothInterface);
00374 }
00375 return bluetoothInterface;
00376 }
00377
00378
00379
00380 QStringList BluezBluetoothInterface::listReply(const QString &method) const
00381 {
00382 QDBusReply< QStringList > list = d->iface.call(method);
00383 if (!list.isValid()) {
00384 return QStringList();
00385 }
00386
00387 return list.value();
00388 }
00389
00390 QString BluezBluetoothInterface::stringReply(const QString &method, const QString ¶m) const
00391 {
00392 QDBusReply< QString > reply;
00393
00394 if (param.isEmpty())
00395 reply = d->iface.call(method);
00396 else
00397 reply = d->iface.call(method, param);
00398
00399 if (reply.isValid()) {
00400 return reply.value();
00401 }
00402
00403 return QString();
00404 }
00405
00406 bool BluezBluetoothInterface::boolReply(const QString &method, const QString ¶m) const
00407 {
00408 QDBusReply< bool > reply;
00409
00410 if (param.isEmpty())
00411 reply = d->iface.call(method);
00412 else
00413 reply = d->iface.call(method, param);
00414
00415 if (reply.isValid()) {
00416 return reply.value();
00417 }
00418
00419 return false;
00420 }
00421
00422 #include "bluez-bluetoothinterface.moc"