From 083040426fff90c8e61ce2bb3241b8dc7f27b25f Mon Sep 17 00:00:00 2001 From: "michal.szwaj" Date: Sat, 17 Mar 2018 23:30:04 +0100 Subject: [PATCH] Draft of bluetooth services --- .../btservice/AndroidBluetoothServer.hpp | 51 +++++++++++++++ .../btservice/AndroidBluetoothService.hpp | 45 +++++++++++++ .../btservice/IAndroidBluetoothServer.hpp | 40 ++++++++++++ .../btservice/IAndroidBluetoothService.hpp | 41 ++++++++++++ src/btservice/AndroidBluetoothServer.cpp | 56 +++++++++++++++++ src/btservice/AndroidBluetoothService.cpp | 63 +++++++++++++++++++ src/btservice/btservice.cpp | 57 ++++++++++++++++- 7 files changed, 352 insertions(+), 1 deletion(-) create mode 100644 include/f1x/openauto/btservice/AndroidBluetoothServer.hpp create mode 100644 include/f1x/openauto/btservice/AndroidBluetoothService.hpp create mode 100644 include/f1x/openauto/btservice/IAndroidBluetoothServer.hpp create mode 100644 include/f1x/openauto/btservice/IAndroidBluetoothService.hpp create mode 100644 src/btservice/AndroidBluetoothServer.cpp create mode 100644 src/btservice/AndroidBluetoothService.cpp diff --git a/include/f1x/openauto/btservice/AndroidBluetoothServer.hpp b/include/f1x/openauto/btservice/AndroidBluetoothServer.hpp new file mode 100644 index 0000000..4e159bc --- /dev/null +++ b/include/f1x/openauto/btservice/AndroidBluetoothServer.hpp @@ -0,0 +1,51 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#pragma once + +#include +#include +#include +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +class AndroidBluetoothServer: public QObject, public IAndroidBluetoothServer +{ + Q_OBJECT + +public: + AndroidBluetoothServer(); + + bool start(const QBluetoothAddress& address, uint16_t portNumber) override; + +private slots: + void onClientConnected(); + +private: + std::unique_ptr rfcommServer_; +}; + +} +} +} diff --git a/include/f1x/openauto/btservice/AndroidBluetoothService.hpp b/include/f1x/openauto/btservice/AndroidBluetoothService.hpp new file mode 100644 index 0000000..ebbb860 --- /dev/null +++ b/include/f1x/openauto/btservice/AndroidBluetoothService.hpp @@ -0,0 +1,45 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#pragma once + +#include +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +class AndroidBluetoothService: public IAndroidBluetoothService +{ +public: + AndroidBluetoothService(uint16_t portNumber); + + bool registerService(const QBluetoothAddress& bluetoothAddress) override; + bool unregisterService() override; + +private: + QBluetoothServiceInfo serviceInfo_; +}; + +} +} +} diff --git a/include/f1x/openauto/btservice/IAndroidBluetoothServer.hpp b/include/f1x/openauto/btservice/IAndroidBluetoothServer.hpp new file mode 100644 index 0000000..fb109ed --- /dev/null +++ b/include/f1x/openauto/btservice/IAndroidBluetoothServer.hpp @@ -0,0 +1,40 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#pragma once + +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +class IAndroidBluetoothServer +{ +public: + virtual ~IAndroidBluetoothServer() = default; + + virtual bool start(const QBluetoothAddress& address, uint16_t portNumber) = 0; +}; + +} +} +} diff --git a/include/f1x/openauto/btservice/IAndroidBluetoothService.hpp b/include/f1x/openauto/btservice/IAndroidBluetoothService.hpp new file mode 100644 index 0000000..71352c9 --- /dev/null +++ b/include/f1x/openauto/btservice/IAndroidBluetoothService.hpp @@ -0,0 +1,41 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#pragma once + +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +class IAndroidBluetoothService +{ +public: + virtual ~IAndroidBluetoothService() = default; + + virtual bool registerService(const QBluetoothAddress& bluetoothAddress) = 0; + virtual bool unregisterService() = 0; +}; + +} +} +} diff --git a/src/btservice/AndroidBluetoothServer.cpp b/src/btservice/AndroidBluetoothServer.cpp new file mode 100644 index 0000000..c827e2b --- /dev/null +++ b/src/btservice/AndroidBluetoothServer.cpp @@ -0,0 +1,56 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#include +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +AndroidBluetoothServer::AndroidBluetoothServer() + : rfcommServer_(std::make_unique(QBluetoothServiceInfo::RfcommProtocol, this)) +{ + connect(rfcommServer_.get(), &QBluetoothServer::newConnection, this, &AndroidBluetoothServer::onClientConnected); +} + +bool AndroidBluetoothServer::start(const QBluetoothAddress& address, uint16_t portNumber) +{ + return rfcommServer_->listen(address, portNumber); +} + +void AndroidBluetoothServer::onClientConnected() +{ + auto socket = rfcommServer_->nextPendingConnection(); + + if(socket != nullptr) + { + OPENAUTO_LOG(info) << "[AndroidBluetoothServer] rfcomm client connected, peer name: " << socket->peerName().toStdString(); + } + else + { + OPENAUTO_LOG(error) << "[AndroidBluetoothServer] received null socket during client connection."; + } +} + +} +} +} diff --git a/src/btservice/AndroidBluetoothService.cpp b/src/btservice/AndroidBluetoothService.cpp new file mode 100644 index 0000000..4da4867 --- /dev/null +++ b/src/btservice/AndroidBluetoothService.cpp @@ -0,0 +1,63 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +AndroidBluetoothService::AndroidBluetoothService(uint16_t portNumber) +{ + serviceInfo_.setAttribute(QBluetoothServiceInfo::ServiceName, "OpenAuto Bluetooth Service"); + serviceInfo_.setAttribute(QBluetoothServiceInfo::ServiceDescription, "AndroidAuto WiFi projection automatic setup"); + serviceInfo_.setAttribute(QBluetoothServiceInfo::ServiceProvider, "f1xstudio.com"); + + //"4de17a00-52cb-11e6-bdf4-0800200c9a66"; + //"669a0c20-0008-f4bd-e611-cb52007ae14d"; + serviceInfo_.setServiceUuid(QBluetoothUuid(QString("4de17a00-52cb-11e6-bdf4-0800200c9a66"))); + + QBluetoothServiceInfo::Sequence publicBrowse; + publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); + serviceInfo_.setAttribute(QBluetoothServiceInfo::BrowseGroupList, publicBrowse); + + QBluetoothServiceInfo::Sequence protocolDescriptorList; + QBluetoothServiceInfo::Sequence protocol; + + protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) + << QVariant::fromValue(portNumber); + protocolDescriptorList.append(QVariant::fromValue(protocol)); + serviceInfo_.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); +} + +bool AndroidBluetoothService::registerService(const QBluetoothAddress& bluetoothAddress) +{ + return serviceInfo_.registerService(bluetoothAddress); +} + +bool AndroidBluetoothService::unregisterService() +{ + return serviceInfo_.unregisterService(); +} + +} +} +} diff --git a/src/btservice/btservice.cpp b/src/btservice/btservice.cpp index 905869d..bf015c6 100644 --- a/src/btservice/btservice.cpp +++ b/src/btservice/btservice.cpp @@ -1,4 +1,59 @@ -int main() +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#include +#include +#include +#include + +namespace btservice = f1x::openauto::btservice; + +int main(int argc, char* argv[]) { + QCoreApplication qApplication(argc, argv); + + const QBluetoothAddress address; + const uint16_t portNumber = 5000; + btservice::AndroidBluetoothService androidBluetoothService(portNumber); + + if(!androidBluetoothService.registerService(address)) + { + OPENAUTO_LOG(error) << "[btservice] Service registration failed."; + return 1; + } + else + { + OPENAUTO_LOG(info) << "[btservice] Service registered, port: " << portNumber; + } + + btservice::AndroidBluetoothServer androidBluetoothServer; + + if(!androidBluetoothServer.start(address, portNumber)) + { + OPENAUTO_LOG(error) << "[btservice] Server start failed."; + return 2; + } + + OPENAUTO_LOG(info) << "[btservice] Listening for connections, address: " << address.toString().toStdString() + << ", port: " << portNumber; + + qApplication.exec(); + androidBluetoothService.unregisterService(); + return 0; }