Add bluetooth server debug categories and improve bluetooth server behaviour

This commit is contained in:
Simon Stürz 2018-07-06 08:39:55 +02:00 committed by Michael Zanetti
parent c2a253b3a2
commit ff66e366b4
4 changed files with 24 additions and 13 deletions

View File

@ -71,6 +71,7 @@ void BluetoothServer::sendData(const QUuid &clientId, const QByteArray &data)
if (!client) if (!client)
return; return;
qCDebug(dcBluetoothServerTraffic()) << "Send data:" << qUtf8Printable(data);
client->write(data + '\n'); client->write(data + '\n');
} }
@ -86,7 +87,7 @@ void BluetoothServer::onHostModeChanged(const QBluetoothLocalDevice::HostMode &m
if (!m_server || !m_localDevice) if (!m_server || !m_localDevice)
return; return;
if (mode != QBluetoothLocalDevice::HostDiscoverable || mode != QBluetoothLocalDevice::HostDiscoverableLimitedInquiry) { if (mode != QBluetoothLocalDevice::HostDiscoverable) {
m_localDevice->setHostMode(QBluetoothLocalDevice::HostDiscoverable); m_localDevice->setHostMode(QBluetoothLocalDevice::HostDiscoverable);
} }
} }
@ -98,14 +99,14 @@ void BluetoothServer::onClientConnected()
if (!client) if (!client)
return; 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(); QUuid clientId = QUuid::createUuid();
m_clientList.insert(clientId, client); m_clientList.insert(clientId, client);
connect(client, SIGNAL(readyRead()), this, SLOT(readData())); 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(disconnected()), this, SLOT(onClientDisconnected()));
connect(client, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(onError(QBluetoothSocket::SocketError)));
emit clientConnected(clientId); emit clientConnected(clientId);
} }
@ -116,14 +117,15 @@ void BluetoothServer::onClientDisconnected()
if (!client) if (!client)
return; 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); QUuid clientId = m_clientList.key(client);
m_clientList.take(clientId)->deleteLater(); m_clientList.take(clientId)->deleteLater();
emit clientDisconnected(clientId);
} }
void BluetoothServer::onError(QBluetoothSocket::SocketError error) void BluetoothServer::onError(QBluetoothSocket::SocketError error)
{ {
qCWarning(dcConnection) << "BluetoothServer: Error occured:" << error; qCWarning(dcBluetoothServer()) << "BluetoothServer: Error occured:" << error;
} }
void BluetoothServer::readData() void BluetoothServer::readData()
@ -133,14 +135,16 @@ void BluetoothServer::readData()
return; return;
m_receiveBuffer.append(client->readAll()); m_receiveBuffer.append(client->readAll());
qCDebug(dcBluetoothServerTraffic()) << "Current data buffer:" << qUtf8Printable(m_receiveBuffer);
int splitIndex = m_receiveBuffer.indexOf("}\n{"); int splitIndex = m_receiveBuffer.indexOf("}\n{");
while (splitIndex > -1) { while (splitIndex > -1) {
emit dataAvailable(m_clientList.key(client), m_receiveBuffer.left(splitIndex + 1)); emit dataAvailable(m_clientList.key(client), m_receiveBuffer.left(splitIndex + 1));
m_receiveBuffer = m_receiveBuffer.right(m_receiveBuffer.length() - splitIndex - 2); m_receiveBuffer = m_receiveBuffer.right(m_receiveBuffer.length() - splitIndex - 2);
splitIndex = m_receiveBuffer.indexOf("}\n{"); splitIndex = m_receiveBuffer.indexOf("}\n{");
} }
if (m_receiveBuffer.endsWith("}\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(); m_receiveBuffer.clear();
} }
} }
@ -152,14 +156,14 @@ bool BluetoothServer::startServer()
m_localDevice = new QBluetoothLocalDevice(this); m_localDevice = new QBluetoothLocalDevice(this);
if (!m_localDevice->isValid()) { if (!m_localDevice->isValid()) {
qCWarning(dcConnection()) << "BluetoothServer: could find any bluetooth hardware"; qCWarning(dcBluetoothServer()) << "BluetoothServer: could find any bluetooth hardware";
delete m_localDevice; delete m_localDevice;
m_localDevice = nullptr; m_localDevice = nullptr;
return false; return false;
} }
// Init adapter // 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->powerOn();
m_localDevice->setHostMode(QBluetoothLocalDevice::HostDiscoverable); m_localDevice->setHostMode(QBluetoothLocalDevice::HostDiscoverable);
connect(m_localDevice, &QBluetoothLocalDevice::hostModeStateChanged, this, &BluetoothServer::onHostModeChanged); connect(m_localDevice, &QBluetoothLocalDevice::hostModeStateChanged, this, &BluetoothServer::onHostModeChanged);
@ -168,7 +172,7 @@ bool BluetoothServer::startServer()
m_server = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); m_server = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
connect(m_server, SIGNAL(newConnection()), this, SLOT(onClientConnected())); connect(m_server, SIGNAL(newConnection()), this, SLOT(onClientConnected()));
if (!m_server->listen(m_localDevice->address())) { 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_localDevice;
delete m_server; delete m_server;
m_localDevice = nullptr; m_localDevice = nullptr;
@ -176,7 +180,7 @@ bool BluetoothServer::startServer()
return false; return false;
} }
qCDebug(dcConnection) << "BluetoothServer: Started bluetooth server" << m_server->serverAddress().toString(); qCDebug(dcBluetoothServer()) << "Started bluetooth server" << m_server->serverAddress().toString();
// Set service attributes // Set service attributes
QBluetoothServiceInfo::Sequence browseSequence; QBluetoothServiceInfo::Sequence browseSequence;
@ -208,15 +212,16 @@ bool BluetoothServer::startServer()
// Register the service in the local device // Register the service in the local device
if (!m_serviceInfo.registerService(m_localDevice->address())) { 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_localDevice;
delete m_server; delete m_server;
m_localDevice = nullptr; m_localDevice = nullptr;
m_server = nullptr; m_server = nullptr;
return false; 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; return true;
} }
@ -227,7 +232,7 @@ bool BluetoothServer::stopServer()
} }
if (m_server) { if (m_server) {
qCDebug(dcBluetooth()) << "Shutting down \"Bluetooth server\""; qCDebug(dcBluetoothServer()) << "Shutting down \"Bluetooth server\"";
m_serviceInfo.unregisterService(); m_serviceInfo.unregisterService();
m_server->close(); m_server->close();
m_server->deleteLater(); m_server->deleteLater();

View File

@ -49,3 +49,5 @@ Q_LOGGING_CATEGORY(dcAWS, "AWS")
Q_LOGGING_CATEGORY(dcAWSTraffic, "AWSTraffic") Q_LOGGING_CATEGORY(dcAWSTraffic, "AWSTraffic")
Q_LOGGING_CATEGORY(dcJanus, "Janus") Q_LOGGING_CATEGORY(dcJanus, "Janus")
Q_LOGGING_CATEGORY(dcJanusTraffic, "JanusTraffic") Q_LOGGING_CATEGORY(dcJanusTraffic, "JanusTraffic")
Q_LOGGING_CATEGORY(dcBluetoothServer, "BluetoothServer")
Q_LOGGING_CATEGORY(dcBluetoothServerTraffic, "BluetoothServerTraffic")

View File

@ -57,5 +57,7 @@ Q_DECLARE_LOGGING_CATEGORY(dcAWS)
Q_DECLARE_LOGGING_CATEGORY(dcAWSTraffic) Q_DECLARE_LOGGING_CATEGORY(dcAWSTraffic)
Q_DECLARE_LOGGING_CATEGORY(dcJanus) Q_DECLARE_LOGGING_CATEGORY(dcJanus)
Q_DECLARE_LOGGING_CATEGORY(dcJanusTraffic) Q_DECLARE_LOGGING_CATEGORY(dcJanusTraffic)
Q_DECLARE_LOGGING_CATEGORY(dcBluetoothServer)
Q_DECLARE_LOGGING_CATEGORY(dcBluetoothServerTraffic)
#endif // LOGGINGCATEGORYS_H #endif // LOGGINGCATEGORYS_H

View File

@ -139,6 +139,8 @@ int main(int argc, char *argv[])
s_loggingFilters.insert("AWSTraffic", false); s_loggingFilters.insert("AWSTraffic", false);
s_loggingFilters.insert("Janus", false); s_loggingFilters.insert("Janus", false);
s_loggingFilters.insert("JanusTraffic", false); s_loggingFilters.insert("JanusTraffic", false);
s_loggingFilters.insert("BluetoothServer", true);
s_loggingFilters.insert("BluetoothServerTraffic", false);
QHash<QString, bool> loggingFiltersPlugins; QHash<QString, bool> loggingFiltersPlugins;
foreach (const QJsonObject &pluginMetadata, DeviceManager::pluginsMetadata()) { foreach (const QJsonObject &pluginMetadata, DeviceManager::pluginsMetadata()) {