From ff66e366b4b15b2cdff0c2591c576ad643e54cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Fri, 6 Jul 2018 08:39:55 +0200 Subject: [PATCH] Add bluetooth server debug categories and improve bluetooth server behaviour --- libnymea-core/bluetoothserver.cpp | 31 ++++++++++++++++++------------- libnymea/loggingcategories.cpp | 2 ++ libnymea/loggingcategories.h | 2 ++ server/main.cpp | 2 ++ 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/libnymea-core/bluetoothserver.cpp b/libnymea-core/bluetoothserver.cpp index 8c634fc9..7857126e 100644 --- a/libnymea-core/bluetoothserver.cpp +++ b/libnymea-core/bluetoothserver.cpp @@ -71,6 +71,7 @@ void BluetoothServer::sendData(const QUuid &clientId, const QByteArray &data) if (!client) return; + qCDebug(dcBluetoothServerTraffic()) << "Send data:" << qUtf8Printable(data); client->write(data + '\n'); } @@ -86,7 +87,7 @@ void BluetoothServer::onHostModeChanged(const QBluetoothLocalDevice::HostMode &m if (!m_server || !m_localDevice) return; - if (mode != QBluetoothLocalDevice::HostDiscoverable || mode != QBluetoothLocalDevice::HostDiscoverableLimitedInquiry) { + if (mode != QBluetoothLocalDevice::HostDiscoverable) { m_localDevice->setHostMode(QBluetoothLocalDevice::HostDiscoverable); } } @@ -98,14 +99,14 @@ void BluetoothServer::onClientConnected() if (!client) return; - qCDebug(dcConnection) << "BluetoothServer: New client connected:" << client->localName() << client->localAddress().toString(); + qCDebug(dcConnection()) << "BluetoothServer: New client connected:" << client->peerName() << client->peerAddress().toString(); QUuid clientId = QUuid::createUuid(); m_clientList.insert(clientId, client); connect(client, SIGNAL(readyRead()), this, SLOT(readData())); - connect(client, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(onError(QBluetoothSocket::SocketError))); connect(client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected())); + connect(client, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(onError(QBluetoothSocket::SocketError))); emit clientConnected(clientId); } @@ -116,14 +117,15 @@ void BluetoothServer::onClientDisconnected() if (!client) return; - qCDebug(dcConnection) << "BluetoothServer: Client disconnected:" << client->localName() << client->localAddress().toString(); + qCDebug(dcConnection()) << "BluetoothServer: Client disconnected:" << client->peerName() << client->peerAddress().toString(); QUuid clientId = m_clientList.key(client); m_clientList.take(clientId)->deleteLater(); + emit clientDisconnected(clientId); } void BluetoothServer::onError(QBluetoothSocket::SocketError error) { - qCWarning(dcConnection) << "BluetoothServer: Error occured:" << error; + qCWarning(dcBluetoothServer()) << "BluetoothServer: Error occured:" << error; } void BluetoothServer::readData() @@ -133,14 +135,16 @@ void BluetoothServer::readData() return; m_receiveBuffer.append(client->readAll()); + qCDebug(dcBluetoothServerTraffic()) << "Current data buffer:" << qUtf8Printable(m_receiveBuffer); int splitIndex = m_receiveBuffer.indexOf("}\n{"); while (splitIndex > -1) { emit dataAvailable(m_clientList.key(client), m_receiveBuffer.left(splitIndex + 1)); m_receiveBuffer = m_receiveBuffer.right(m_receiveBuffer.length() - splitIndex - 2); splitIndex = m_receiveBuffer.indexOf("}\n{"); } + if (m_receiveBuffer.endsWith("}\n")) { - emit dataAvailable(m_clientList.key(client), m_receiveBuffer); + emit dataAvailable(m_clientList.key(client), m_receiveBuffer.trimmed()); m_receiveBuffer.clear(); } } @@ -152,14 +156,14 @@ bool BluetoothServer::startServer() m_localDevice = new QBluetoothLocalDevice(this); if (!m_localDevice->isValid()) { - qCWarning(dcConnection()) << "BluetoothServer: could find any bluetooth hardware"; + qCWarning(dcBluetoothServer()) << "BluetoothServer: could find any bluetooth hardware"; delete m_localDevice; m_localDevice = nullptr; return false; } // Init adapter - qCDebug(dcConnection()) << "BluetoothServer: Using adapter" << m_localDevice->name() << m_localDevice->address().toString(); + qCDebug(dcBluetoothServer()) << "Using adapter" << m_localDevice->name() << m_localDevice->address().toString(); m_localDevice->powerOn(); m_localDevice->setHostMode(QBluetoothLocalDevice::HostDiscoverable); connect(m_localDevice, &QBluetoothLocalDevice::hostModeStateChanged, this, &BluetoothServer::onHostModeChanged); @@ -168,7 +172,7 @@ bool BluetoothServer::startServer() m_server = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); connect(m_server, SIGNAL(newConnection()), this, SLOT(onClientConnected())); if (!m_server->listen(m_localDevice->address())) { - qCWarning(dcConnection()) << "BluetoothServer: Could not listen on local device." << m_localDevice->name(); + qCWarning(dcBluetoothServer()) << "Could not listen on local device." << m_localDevice->name(); delete m_localDevice; delete m_server; m_localDevice = nullptr; @@ -176,7 +180,7 @@ bool BluetoothServer::startServer() return false; } - qCDebug(dcConnection) << "BluetoothServer: Started bluetooth server" << m_server->serverAddress().toString(); + qCDebug(dcBluetoothServer()) << "Started bluetooth server" << m_server->serverAddress().toString(); // Set service attributes QBluetoothServiceInfo::Sequence browseSequence; @@ -208,15 +212,16 @@ bool BluetoothServer::startServer() // Register the service in the local device if (!m_serviceInfo.registerService(m_localDevice->address())) { - qCWarning(dcConnection()) << "BluetoothServer: Could not register service" << m_serviceInfo.serviceName() << nymeaServiceUuid.toString(); + qCWarning(dcBluetoothServer()) << "Could not register service" << m_serviceInfo.serviceName() << nymeaServiceUuid.toString(); delete m_localDevice; delete m_server; m_localDevice = nullptr; m_server = nullptr; return false; } + qCDebug(dcBluetoothServer()) << "Registered successfully service" << m_serviceInfo.serviceName() << nymeaServiceUuid.toString(); + qCDebug(dcConnection()) << "Started bluetooth server" << m_localDevice->name() << m_localDevice->address().toString() << "Serivce:" << m_serviceInfo.serviceName() << nymeaServiceUuid.toString(); - qCDebug(dcConnection()) << "BluetoothServer: Registered successfully service" << m_serviceInfo.serviceName() << nymeaServiceUuid.toString(); return true; } @@ -227,7 +232,7 @@ bool BluetoothServer::stopServer() } if (m_server) { - qCDebug(dcBluetooth()) << "Shutting down \"Bluetooth server\""; + qCDebug(dcBluetoothServer()) << "Shutting down \"Bluetooth server\""; m_serviceInfo.unregisterService(); m_server->close(); m_server->deleteLater(); diff --git a/libnymea/loggingcategories.cpp b/libnymea/loggingcategories.cpp index 3a25f54c..1a6cd237 100644 --- a/libnymea/loggingcategories.cpp +++ b/libnymea/loggingcategories.cpp @@ -49,3 +49,5 @@ Q_LOGGING_CATEGORY(dcAWS, "AWS") Q_LOGGING_CATEGORY(dcAWSTraffic, "AWSTraffic") Q_LOGGING_CATEGORY(dcJanus, "Janus") Q_LOGGING_CATEGORY(dcJanusTraffic, "JanusTraffic") +Q_LOGGING_CATEGORY(dcBluetoothServer, "BluetoothServer") +Q_LOGGING_CATEGORY(dcBluetoothServerTraffic, "BluetoothServerTraffic") diff --git a/libnymea/loggingcategories.h b/libnymea/loggingcategories.h index 81e33053..84a06329 100644 --- a/libnymea/loggingcategories.h +++ b/libnymea/loggingcategories.h @@ -57,5 +57,7 @@ Q_DECLARE_LOGGING_CATEGORY(dcAWS) Q_DECLARE_LOGGING_CATEGORY(dcAWSTraffic) Q_DECLARE_LOGGING_CATEGORY(dcJanus) Q_DECLARE_LOGGING_CATEGORY(dcJanusTraffic) +Q_DECLARE_LOGGING_CATEGORY(dcBluetoothServer) +Q_DECLARE_LOGGING_CATEGORY(dcBluetoothServerTraffic) #endif // LOGGINGCATEGORYS_H diff --git a/server/main.cpp b/server/main.cpp index 3a24fa44..ac84526f 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -139,6 +139,8 @@ int main(int argc, char *argv[]) s_loggingFilters.insert("AWSTraffic", false); s_loggingFilters.insert("Janus", false); s_loggingFilters.insert("JanusTraffic", false); + s_loggingFilters.insert("BluetoothServer", true); + s_loggingFilters.insert("BluetoothServerTraffic", false); QHash loggingFiltersPlugins; foreach (const QJsonObject &pluginMetadata, DeviceManager::pluginsMetadata()) {