Draft of bluetooth services

pull/53/head
michal.szwaj 2018-03-17 23:30:04 +01:00
parent 5c31630155
commit 083040426f
7 changed files with 352 additions and 1 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <memory>
#include <QBluetoothServer>
#include <f1x/openauto/btservice/IAndroidBluetoothServer.hpp>
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<QBluetoothServer> rfcommServer_;
};
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QBluetoothServiceInfo>
#include <f1x/openauto/btservice/IAndroidBluetoothService.hpp>
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_;
};
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QBluetoothAddress>
namespace f1x
{
namespace openauto
{
namespace btservice
{
class IAndroidBluetoothServer
{
public:
virtual ~IAndroidBluetoothServer() = default;
virtual bool start(const QBluetoothAddress& address, uint16_t portNumber) = 0;
};
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QBluetoothAddress>
namespace f1x
{
namespace openauto
{
namespace btservice
{
class IAndroidBluetoothService
{
public:
virtual ~IAndroidBluetoothService() = default;
virtual bool registerService(const QBluetoothAddress& bluetoothAddress) = 0;
virtual bool unregisterService() = 0;
};
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <f1x/openauto/Common/Log.hpp>
#include <f1x/openauto/btservice/AndroidBluetoothServer.hpp>
namespace f1x
{
namespace openauto
{
namespace btservice
{
AndroidBluetoothServer::AndroidBluetoothServer()
: rfcommServer_(std::make_unique<QBluetoothServer>(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.";
}
}
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <f1x/openauto/btservice/AndroidBluetoothService.hpp>
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();
}
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <QCoreApplication>
#include <f1x/openauto/Common/Log.hpp>
#include <f1x/openauto/btservice/AndroidBluetoothService.hpp>
#include <f1x/openauto/btservice/AndroidBluetoothServer.hpp>
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;
}