Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
krogank9 committed Aug 25, 2017
1 parent e1c54b2 commit 0253e7e
Show file tree
Hide file tree
Showing 16 changed files with 1,045 additions and 105 deletions.
9 changes: 8 additions & 1 deletion WifiMouseServer.pro
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

QT += svg
QT += network
QT += bluetooth

win32 {
SOURCES += fakeinput-windows.cpp
Expand All @@ -34,14 +35,20 @@ SOURCES += main.cpp\
networkthread.cpp \
runguard.cpp \
setpassworddialog.cpp \
abstractedserver.cpp \
encryptutils.cpp \
aes.c

HEADERS += mainwindow.h \
fixedsvgwidget.h \
rotatingsquare.h \
networkthread.h \
runguard.h \
setpassworddialog.h \
fakeinput.h
fakeinput.h \
abstractedserver.h \
encryptutils.h \
aes.h

FORMS += mainwindow.ui \
setpassworddialog.ui
Expand Down
4 changes: 2 additions & 2 deletions WifiMouseServer.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.5.1, 2017-08-12T16:17:37. -->
<!-- Written by QtCreator 3.5.1, 2017-08-22T20:29:46. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -61,7 +61,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{c118769e-40e7-4fa7-9464-62db37a5eb58}</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
Expand Down
134 changes: 134 additions & 0 deletions abstractedserver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include "abstractedserver.h"
#include <QEventLoop>
#include <QTime>
#include <QBluetoothSocket>
#include <QTcpSocket>

AbstractedServer::AbstractedServer()
: bluetoothServer(QBluetoothServiceInfo::RfcommProtocol),
pendingSocket(0)
{
tcpServer.setMaxPendingConnections(1);
bluetoothServer.setMaxPendingConnections(1);

QObject::connect(&tcpServer, SIGNAL(newConnection()), this, SLOT(newTcpConnection()));
QObject::connect(&bluetoothServer, SIGNAL(newConnection()), this, SLOT(newBluetoothConnection()));

trySetupServers();
}

AbstractedServer::~AbstractedServer()
{
if(pendingSocket != 0)
delete pendingSocket;
}

void AbstractedServer::newTcpConnection()
{
QTcpSocket *tcpSocket = tcpServer.nextPendingConnection();
tcpSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);

if(pendingSocket == 0) {
pendingSocket = tcpSocket;
pendingIsBluetooth = false;

QString clientIp = tcpSocket->peerAddress().toString();
int index = clientIp.lastIndexOf(':'); index = index==-1?0:index;
clientIp = clientIp.right(clientIp.length() - index - 1);
pendingSocketInfo = clientIp;

emit newConnection();
}
else
delete tcpSocket;
}

void AbstractedServer::newBluetoothConnection()
{
qInfo() << "new bluetooth connection";
QBluetoothSocket *bluetoothSocket = bluetoothServer.nextPendingConnection();

if(pendingSocket == 0) {
pendingSocket = bluetoothSocket;
pendingIsBluetooth = true;
pendingSocketInfo = "Connected via Bluetooth";
emit newConnection();
}
else
delete bluetoothSocket;
}

QIODevice *AbstractedServer::nextPendingConnection()
{
QIODevice *ret = pendingSocket;
pendingSocket = 0;
return ret;
}

bool AbstractedServer::registerBluetoothService()
{
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("WifiMouseServer"));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription, tr("WifiMouseServer"));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("wifi-mouse.xyz"));

static const QLatin1String serviceUuid("c05efa2b-5e9f-4a39-9705-72ccf47d2eb8");
serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid));

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::L2cap));
protocolDescriptorList.append(QVariant::fromValue(protocol));
protocol.clear();
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
<< QVariant::fromValue(quint8(bluetoothServer.serverPort()));
protocolDescriptorList.append(QVariant::fromValue(protocol));
serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList);

return serviceInfo.registerService();
}

bool AbstractedServer::bluetoothServerListen()
{
if(!bluetoothServer.isListening() && !bluetoothServer.listen()) {
qInfo() << "Unable to start bluetooth server.\n";
return false;
}
else if(serviceInfo.isRegistered() == false) {
if( !registerBluetoothService() ) {
qInfo() << "Couldn't register service";
return false;
}
}

return true;
}

bool AbstractedServer::tcpServerListen()
{
if(!tcpServer.isListening() && !tcpServer.listen(QHostAddress::Any, 9798)) {
qInfo() << "Unable to start TCP server on port 9798.\n";
return false;
}
return true;
}

void AbstractedServer::listenWithTimeout(qint16 timeoutMs)
{
trySetupServers();
QTime stopWatch;
stopWatch.start();
while(stopWatch.elapsed() < timeoutMs && pendingSocket == 0) {
eventLoop.processEvents();
}
}

void AbstractedServer::trySetupServers()
{
// these can be recalled if initial setup failed
bluetoothServerListen();
tcpServerListen();
}
44 changes: 44 additions & 0 deletions abstractedserver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef ABSTRACTEDSERVER_H
#define ABSTRACTEDSERVER_H

#include <QTcpServer>
#include <QBluetoothServer>
#include <QBluetoothServiceInfo>
#include <QEventLoop>

class AbstractedServer : public QObject
{
Q_OBJECT

public:
AbstractedServer();
~AbstractedServer();

QIODevice *nextPendingConnection();
void listenWithTimeout(qint16 timeoutMs);
void trySetupServers();

QString pendingSocketInfo;
bool pendingIsBluetooth = false;

private:
QEventLoop eventLoop;

bool registerBluetoothService();
bool bluetoothServerListen();
bool tcpServerListen();
QTcpServer tcpServer;
QBluetoothServer bluetoothServer;
QBluetoothServiceInfo serviceInfo;

QIODevice *pendingSocket;

public slots:
void newTcpConnection();
void newBluetoothConnection();

signals:
void newConnection();
};

#endif // ABSTRACTEDSERVER_H
Loading

0 comments on commit 0253e7e

Please sign in to comment.