btserialportclient.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**********************************************************************
  2. ** Copyright (C) 2009 Nokia Corporation.
  3. ** All rights reserved.
  4. **
  5. ** $LICENSE_TEXT$
  6. **********************************************************************/
  7. #include "btserialportclient.h"
  8. #include "serialCommHandler/qserialport.h"
  9. #include <QDBusInterface>
  10. #include <QDBusObjectPath>
  11. #include <QDBusPendingCallWatcher>
  12. #include <QDBusPendingReply>
  13. #include <QVariant>
  14. #include "qdatetime.h"
  15. #include <QDebug>
  16. BtSerialPortClient::BtSerialPortClient(QObject *parent) :
  17. QObject(parent)
  18. {
  19. m_isConnected = false;
  20. }
  21. BtSerialPortClient::~BtSerialPortClient()
  22. {
  23. }
  24. void BtSerialPortClient::setupInterface(QString address)
  25. {
  26. QDBusConnection systemBus = QDBusConnection::systemBus();
  27. QDBusInterface managerInterface("org.bluez", "/", "org.bluez.Manager", systemBus, this);
  28. QDBusMessage callReturn = managerInterface.call("DefaultAdapter");
  29. QString objectPath = firstArgumentToObjectPath(callReturn);
  30. if (isError(objectPath.isNull()))
  31. return;
  32. QDBusInterface adapterInterface("org.bluez", objectPath, "org.bluez.Adapter", systemBus, this);
  33. qDebug() << "Adapter Interface" << adapterInterface.isValid();
  34. if (isError(!adapterInterface.isValid()))
  35. return;
  36. QDBusMessage devicePathMessage = adapterInterface.call("FindDevice", address);
  37. qDebug() << devicePathMessage;
  38. if (devicePathMessage.type() == QDBusMessage::ErrorMessage) {
  39. devicePathMessage = adapterInterface.call("CreateDevice", address);
  40. qDebug() << devicePathMessage;
  41. }
  42. if (isErrorMessage(devicePathMessage))
  43. return;
  44. m_devicePath = firstArgumentToObjectPath(devicePathMessage);
  45. if (isError(m_devicePath.isNull()))
  46. return;
  47. QDBusInterface serialInterface("org.bluez", m_devicePath, "org.bluez.Serial", systemBus, this);
  48. if (isError(!serialInterface.isValid()))
  49. return;
  50. QDBusPendingCall async = serialInterface.asyncCall("Connect", "spp");
  51. QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this);
  52. QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
  53. this, SLOT(connectionCallFinishedSlot(QDBusPendingCallWatcher*)));
  54. }
  55. void BtSerialPortClient::updateConnectionStatus(const QDBusMessage& message)
  56. {
  57. QList<QVariant> result = message.arguments();
  58. QString property = result.at(0).toString();
  59. bool isConnected = result.at(1).value<bool> ();
  60. qDebug() << "property: " << property;
  61. qDebug() << "isConnected: " << isConnected;
  62. if (!property.compare("Connected") && (isConnected == false)) {
  63. disconnect();
  64. }
  65. }
  66. void BtSerialPortClient::connectionCallFinishedSlot(QDBusPendingCallWatcher* call)
  67. {
  68. QDBusMessage serialMessage = call->reply();
  69. if (!isErrorMessage(serialMessage)) {
  70. QDBusConnection systemBus = QDBusConnection::systemBus();
  71. QVariant serial = serialMessage.arguments().takeFirst();
  72. QString serialString = serial.toString();
  73. QSerialPort *serialPort = NULL;
  74. if (!serialString.isEmpty()) {
  75. serialPort = QSerialPort::create(serialString, 115200, true);
  76. // Being an observer for connection status from bluez
  77. QDBusInterface deviceInterface("org.bluez", m_devicePath, "org.bluez.Device",
  78. systemBus, this);
  79. if (!deviceInterface.isValid()) {
  80. return;
  81. }
  82. systemBus.connect("org.bluez", m_devicePath, "org.bluez.Device", "PropertyChanged",
  83. this, SLOT(updateConnectionStatus(const QDBusMessage&)));
  84. m_isConnected = true;
  85. emit interfaceBounded(serialPort);
  86. } else {
  87. emit error();
  88. }
  89. }
  90. }
  91. QString BtSerialPortClient::firstArgumentToObjectPath(QDBusMessage message)
  92. {
  93. QVariant messageVariant = message.arguments().takeFirst();
  94. QString objectPath;
  95. if (messageVariant.canConvert<QDBusObjectPath> ()) {
  96. QDBusObjectPath argument = messageVariant.value<QDBusObjectPath> ();
  97. objectPath = argument.path();
  98. }
  99. return objectPath;
  100. }
  101. bool BtSerialPortClient::isError(bool errorTest)
  102. {
  103. if (errorTest) {
  104. emit error();
  105. }
  106. return errorTest;
  107. }
  108. bool BtSerialPortClient::isErrorMessage(QDBusMessage message)
  109. {
  110. bool errorTest = message.type() == QDBusMessage::ErrorMessage;
  111. if (isError(errorTest))
  112. qDebug() << "Error message: " << message.errorMessage();
  113. return errorTest;
  114. }
  115. void BtSerialPortClient::disconnect()
  116. {
  117. qDebug() << "disconnect called";
  118. if(m_isConnected) {
  119. QDBusInterface serialInterface("org.bluez", m_devicePath, "org.bluez.Serial",
  120. QDBusConnection::systemBus(), this);
  121. if (isError(!serialInterface.isValid()))
  122. return;
  123. QDBusMessage serialMessage = serialInterface.call("Disconnect", "spp");
  124. if (isErrorMessage(serialMessage)) {
  125. qDebug() << "error disconnecting serial interface";
  126. } else {
  127. qDebug() << "successfull disconnection";
  128. m_isConnected = false;
  129. emit disconnected();
  130. }
  131. }
  132. }