1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

update bluetooth serial socket

This commit is contained in:
tianduanrui 2018-04-29 01:13:42 +08:00
parent 641cd05b08
commit 15104ce7c2
8 changed files with 291 additions and 98 deletions

View File

@ -46,7 +46,7 @@ void QQtBluetoothClient::installProtocol ( QQtProtocol* stack )
m_protocol = stack;
connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( write ( const QByteArray& ) ) );
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
}
void QQtBluetoothClient::uninstallProtocol ( QQtProtocol* stack )
@ -57,7 +57,7 @@ void QQtBluetoothClient::uninstallProtocol ( QQtProtocol* stack )
return;
disconnect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( write ( const QByteArray& ) ) );
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
m_protocol = NULL;
}
@ -212,5 +212,77 @@ void QQtBluetoothClient::readyReadData()
{
QByteArray bytes;
bytes = readAll();
m_protocol->translator ( bytes );
translator ( bytes );
}
void QQtBluetoothClient::slotWriteData ( const QByteArray& bytes )
{
write ( bytes );
}
void QQtBluetoothClient::translator ( const QByteArray& bytes )
{
// queued conn and queued package;
// direct conn and direct package;
/**
*
*
*
* protocol里是为了增添传输工具客户端类型方便
*
*
*
* staticbuffer
*
*/
static QByteArray sqbaBlockOnNet;
sqbaBlockOnNet += bytes;
//qint64 aaa = bytesAvailable();
//pline() << aaa;
do
{
quint16 nBlockLen = m_protocol->splitter ( sqbaBlockOnNet );
pmeta ( this ) << sqbaBlockOnNet.size() << "..." << nBlockLen;
if ( sqbaBlockOnNet.length() < nBlockLen || nBlockLen < m_protocol->minlength() )
{
/*
*
*/
return;
}
else if ( nBlockLen > m_protocol->maxlength() )
{
/*
*
*/
sqbaBlockOnNet.clear();
pmeta ( this ) << "forbidden package" << sqbaBlockOnNet.length() << nBlockLen;
return;
}
else if ( sqbaBlockOnNet.length() > nBlockLen )
{
/*
*
*
* :
*/
pmeta ( this ) << "stick package" << sqbaBlockOnNet.length() << nBlockLen;
QByteArray netData;
netData.resize ( nBlockLen );
sqbaBlockOnNet >> netData;
m_protocol->dispatcher ( netData );
continue;
}
/*
*
*/
m_protocol->dispatcher ( sqbaBlockOnNet );
break;
} while ( 1 );
sqbaBlockOnNet.clear();
}

View File

@ -10,7 +10,7 @@ class QQTSHARED_EXPORT QQtBluetoothClient : public QBluetoothSocket
Q_OBJECT
public:
explicit QQtBluetoothClient ( QBluetoothServiceInfo::Protocol socketType = QBluetoothServiceInfo::RfcommProtocol,
QObject* parent = nullptr );
QObject* parent = nullptr );
explicit QQtBluetoothClient ( QObject* parent = nullptr );
/**
@ -56,6 +56,15 @@ private slots:
protected slots:
void readyReadData();
void slotWriteData ( const QByteArray& bytes );
protected:
/**
* @brief translator
*
* @param bytes
*/
virtual void translator ( const QByteArray& bytes );
private:
void connectToSingelHost();

View File

@ -1,37 +1,39 @@
#include "qqtserialport.h"
#include "qqtcore.h"
QQtSerialPort::QQtSerialPort(QObject* parent) : QSerialPort(parent)
QQtSerialPort::QQtSerialPort ( QObject* parent ) : QSerialPort ( parent )
{
//connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(updateProgress(qint64)) );
connect(this, SIGNAL(readyRead()), this, SLOT(readyReadData()));
connect ( this, SIGNAL ( readyRead() ), this, SLOT ( readyReadData() ) );
//connect(this, SIGNAL(aboutToClose()), this, SLOT(aboutToClose()));
//connect(this, SIGNAL(readChannelFinished()), this, SLOT(readChannelFinished()));
}
QQtSerialPort::~QQtSerialPort()
{
if (isOpen())
if ( isOpen() )
close();
}
void QQtSerialPort::installProtocol(QQtProtocol* stack)
void QQtSerialPort::installProtocol ( QQtProtocol* stack )
{
if (m_protocol)
if ( m_protocol )
return;
m_protocol = stack;
connect(m_protocol, SIGNAL(write(const QByteArray&)), this, SLOT(write(const QByteArray&)));
connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
}
void QQtSerialPort::uninstallProtocol(QQtProtocol* stack)
void QQtSerialPort::uninstallProtocol ( QQtProtocol* stack )
{
Q_UNUSED(stack)
Q_UNUSED ( stack )
if (!m_protocol)
if ( !m_protocol )
return;
disconnect(m_protocol, SIGNAL(write(const QByteArray&)), this, SLOT(write(const QByteArray&)));
disconnect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
m_protocol = NULL;
}
@ -44,5 +46,77 @@ void QQtSerialPort::readyReadData()
{
QByteArray bytes;
bytes = readAll();
m_protocol->translator(bytes);
translator ( bytes );
}
void QQtSerialPort::slotWriteData ( const QByteArray& bytes )
{
write ( bytes );
}
void QQtSerialPort::translator ( const QByteArray& bytes )
{
// queued conn and queued package;
// direct conn and direct package;
/**
*
*
*
* protocol里是为了增添传输工具客户端类型方便
*
*
*
* staticbuffer
*
*/
static QByteArray sqbaBlockOnNet;
sqbaBlockOnNet += bytes;
//qint64 aaa = bytesAvailable();
//pline() << aaa;
do
{
quint16 nBlockLen = m_protocol->splitter ( sqbaBlockOnNet );
pmeta ( this ) << sqbaBlockOnNet.size() << "..." << nBlockLen;
if ( sqbaBlockOnNet.length() < nBlockLen || nBlockLen < m_protocol->minlength() )
{
/*
*
*/
return;
}
else if ( nBlockLen > m_protocol->maxlength() )
{
/*
*
*/
sqbaBlockOnNet.clear();
pmeta ( this ) << "forbidden package" << sqbaBlockOnNet.length() << nBlockLen;
return;
}
else if ( sqbaBlockOnNet.length() > nBlockLen )
{
/*
*
*
* :
*/
pmeta ( this ) << "stick package" << sqbaBlockOnNet.length() << nBlockLen;
QByteArray netData;
netData.resize ( nBlockLen );
sqbaBlockOnNet >> netData;
m_protocol->dispatcher ( netData );
continue;
}
/*
*
*/
m_protocol->dispatcher ( sqbaBlockOnNet );
break;
} while ( 1 );
sqbaBlockOnNet.clear();
}

View File

@ -10,7 +10,7 @@ class QSerialPort : public QextSerialPort
{
Q_OBJECT
public:
explicit QSerialPort(QObject* parent = 0) {}
explicit QSerialPort ( QObject* parent = 0 ) {}
virtual ~QSerialPort() {}
enum BaudRate
{
@ -24,7 +24,7 @@ public:
Baud115200 = BAUD115200,
UnknownBaud = -1
};
Q_ENUMS(BaudRate)
Q_ENUMS ( BaudRate )
enum DataBits
{
Data5 = DATA_5,
@ -33,7 +33,7 @@ public:
Data8 = DATA_8,
UnknownDataBits = -1
};
Q_ENUMS(DataBits)
Q_ENUMS ( DataBits )
enum Parity
{
NoParity = PAR_NONE,
@ -43,7 +43,7 @@ public:
MarkParity = PAR_MARK,
UnknownParity = -1
};
Q_ENUMS(Parity)
Q_ENUMS ( Parity )
enum StopBits
{
OneStop = STOP_1,
@ -51,7 +51,7 @@ public:
TwoStop = STOP_2,
UnknownStopBits = -1
};
Q_ENUMS(StopBits)
Q_ENUMS ( StopBits )
enum FlowControl
{
NoFlowControl = FLOW_OFF,
@ -59,14 +59,14 @@ public:
SoftwareControl = FLOW_XONXOFF,
UnknownFlowControl = -1
};
Q_ENUMS(FlowControl)
Q_ENUMS ( FlowControl )
// QextSerialBase interface
public:
virtual void setBaudRate(BaudRate v) { QextSerialPort::setBaudRate((BaudRateType)v); }
virtual void setDataBits(DataBits v) { QextSerialPort::setDataBits((DataBitsType)v); }
virtual void setParity(Parity v) { QextSerialPort::setParity((ParityType)v); }
virtual void setStopBits(StopBits v) { QextSerialPort::setStopBits((StopBitsType)v); }
virtual void setFlowControl(FlowControl v) { QextSerialPort::setFlowControl((FlowType)v); }
virtual void setBaudRate ( BaudRate v ) { QextSerialPort::setBaudRate ( ( BaudRateType ) v ); }
virtual void setDataBits ( DataBits v ) { QextSerialPort::setDataBits ( ( DataBitsType ) v ); }
virtual void setParity ( Parity v ) { QextSerialPort::setParity ( ( ParityType ) v ); }
virtual void setStopBits ( StopBits v ) { QextSerialPort::setStopBits ( ( StopBitsType ) v ); }
virtual void setFlowControl ( FlowControl v ) { QextSerialPort::setFlowControl ( ( FlowType ) v ); }
};
#else
#include <QSerialPort>
@ -76,16 +76,24 @@ class QQTSHARED_EXPORT QQtSerialPort : public QSerialPort
{
Q_OBJECT
public:
explicit QQtSerialPort(QObject* parent = 0);
explicit QQtSerialPort ( QObject* parent = 0 );
~QQtSerialPort();
void installProtocol(QQtProtocol* stack);
void uninstallProtocol(QQtProtocol* stack);
void installProtocol ( QQtProtocol* stack );
void uninstallProtocol ( QQtProtocol* stack );
QQtProtocol* installedProtocol();
private slots:
void readyReadData();
void slotWriteData ( const QByteArray& bytes );
protected:
/**
* @brief translator
*
* @param bytes
*/
virtual void translator ( const QByteArray& bytes );
private:
QQtProtocol* m_protocol;
};

View File

@ -3,5 +3,48 @@
QQtWebSocketClient::QQtWebSocketClient ( QObject* parent, const QString& origin, QWebSocketProtocol::Version version ) :
QWebSocket ( origin, version, parent )
{
connect ( this, SIGNAL ( binaryMessageReceived ( QByteArray ) ),
this, SLOT ( slotReadData ( QByteArray ) ) );
}
void QQtWebSocketClient::installProtocol ( QQtProtocol* stack )
{
if ( m_protocol )
return;
m_protocol = stack;
connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
}
void QQtWebSocketClient::uninstallProtocol ( QQtProtocol* stack )
{
Q_UNUSED ( stack )
if ( !m_protocol )
return;
disconnect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
m_protocol = NULL;
}
QQtProtocol* QQtWebSocketClient::installedProtocol()
{
return m_protocol;
}
void QQtWebSocketClient::slotReadData ( const QByteArray& bytes )
{
translator ( bytes );
}
void QQtWebSocketClient::slotWriteData ( const QByteArray& bytes )
{
sendBinaryMessage ( bytes );
}
void QQtWebSocketClient::translator ( const QByteArray& bytes )
{
m_protocol->dispatcher ( bytes );
}

View File

@ -1,14 +1,14 @@
#ifndef QQTWEBCLIENT_H
#define QQTWEBCLIENT_H
#ifndef QQTWEBSOCKETCLIENT_H
#define QQTWEBSOCKETCLIENT_H
#include <QtWebSockets/QWebSocket>
#include <qqtprotocol.h>
#include <qqtcore.h>
#include <qqt-local.h>
/**
* @brief The QQtWebClient class
* QQtWebSocketClient通过安装QQtWebProtocol来实现和RawSocket相似的通信方式
* QQtWebProtocol是否和QQtProtocol格式相同使QQtWebProtocol继承QQtProtocol
* @brief The QQtWebSocketClient class
* QQtWebSocketClient通过安装QQtProtocol来实现和RawSocket相似的通信方式
*/
class QQTSHARED_EXPORT QQtWebSocketClient : public QWebSocket
{
@ -18,11 +18,26 @@ public:
const QString& origin = QString(),
QWebSocketProtocol::Version version = QWebSocketProtocol::VersionLatest );
void installProtocol ( QQtProtocol* stack );
void uninstallProtocol ( QQtProtocol* stack );
QQtProtocol* installedProtocol();
signals:
public slots:
private slots:
void slotReadData ( const QByteArray& bytes );
void slotWriteData ( const QByteArray& bytes );
protected:
/**
* @brief translator
*
* @param bytes
*/
virtual void translator ( const QByteArray& bytes );
private:
QQtProtocol* m_protocol;
};
#endif // QQTWEBCLIENT_H
#endif // QQTWEBSOCKETCLIENT_H

View File

@ -1,10 +1,10 @@
#ifndef QQTWEBSERVER_H
#define QQTWEBSERVER_H
#ifndef QQTWEBSOCKETSERVER_H
#define QQTWEBSOCKETSERVER_H
#include <QObject>
#include <QWebSocketServer>
#include <qqtcore.h>
class QQtWebSocketServer : public QObject
class QQtWebSocketServer : public QWebSocketServer
{
Q_OBJECT
public:
@ -15,4 +15,4 @@ signals:
public slots:
};
#endif // QQTWEBSERVER_H
#endif // QQTWEBSOCKETSERVER_H

View File

@ -224,91 +224,65 @@ contains(DEFINES, __QQTCHARTS__) {
#network
contains (DEFINES, __NETWORKSUPPORT__) {
SOURCES += \
$$PWD/network/qqtnetwork.cpp
HEADERS += \
$$PWD/network/qqtnetwork.h
SOURCES += $$PWD/network/qqtnetwork.cpp
HEADERS += $$PWD/network/qqtnetwork.h
#报文 用户重要
HEADERS += \
$$PWD/network/qqtmessage.h
HEADERS += $$PWD/network/qqtmessage.h
#协议 用户重要
HEADERS += \
$$PWD/network/qqtprotocol.h
HEADERS += $$PWD/network/qqtprotocol.h
#protocol manager for server iodevice
SOURCES += \
$$PWD/network/qqtprotocolmanager.cpp
HEADERS += \
$$PWD/network/qqtprotocolmanager.h
SOURCES += $$PWD/network/qqtprotocolmanager.cpp
HEADERS += $$PWD/network/qqtprotocolmanager.h
#tcp and udp iodevice
contains(DEFINES, __TCPUDPSOCKET__){
#tcp client iodevice
SOURCES += \
$$PWD/network/qqttcpclient.cpp
HEADERS += \
$$PWD/network/qqttcpclient.h
SOURCES += $$PWD/network/qqttcpclient.cpp
HEADERS += $$PWD/network/qqttcpclient.h
#tcp server iodevice
SOURCES += \
$$PWD/network/qqttcpserver.cpp
HEADERS += \
$$PWD/network/qqttcpserver.h
SOURCES += $$PWD/network/qqttcpserver.cpp
HEADERS += $$PWD/network/qqttcpserver.h
#udp client iodevice
SOURCES += \
$$PWD/network/qqtudpclient.cpp
HEADERS += \
$$PWD/network/qqtudpclient.h
SOURCES += $$PWD/network/qqtudpclient.cpp
HEADERS += $$PWD/network/qqtudpclient.h
#udp server iodevice
SOURCES += \
$$PWD/network/qqtudpserver.cpp
HEADERS += \
$$PWD/network/qqtudpserver.h
SOURCES += $$PWD/network/qqtudpserver.cpp
HEADERS += $$PWD/network/qqtudpserver.h
}
#serialport iodevice
SOURCES += \
$$PWD/network/qqtserialport.cpp
HEADERS += \
$$PWD/network/qqtserialport.h
SOURCES += $$PWD/network/qqtserialport.cpp
HEADERS += $$PWD/network/qqtserialport.h
#bluetooth iodevice
#注释在qqt_header.pri打开 DEFINES += __BLUETOOTH__
contains (DEFINES, __BLUETOOTH__) {
#bluetooth client iodevice
SOURCES += \
$$PWD/network/qqtbluetoothclient.cpp
HEADERS += \
$$PWD/network/qqtbluetoothclient.h
SOURCES += $$PWD/network/qqtbluetoothclient.cpp
HEADERS += $$PWD/network/qqtbluetoothclient.h
#bluetooth server iodevice
SOURCES += \
$$PWD/network/qqtbluetoothserver.cpp
HEADERS += \
$$PWD/network/qqtbluetoothserver.h
SOURCES += $$PWD/network/qqtbluetoothserver.cpp
HEADERS += $$PWD/network/qqtbluetoothserver.h
#bluetooth manager
SOURCES += \
$$PWD/network/qqtbluetoothmanager.cpp
HEADERS += \
$$PWD/network/qqtbluetoothmanager.h
SOURCES += $$PWD/network/qqtbluetoothmanager.cpp
HEADERS += $$PWD/network/qqtbluetoothmanager.h
}
contains (DEFINES, __WEBSOCKETSUPPORT__) {
#websocket client iodevice
SOURCES += \
$$PWD/network/qqtwebsocketclient.cpp
HEADERS += \
$$PWD/network/qqtwebsocketclient.h
SOURCES += $$PWD/network/qqtwebsocketclient.cpp
HEADERS += $$PWD/network/qqtwebsocketclient.h
#websocket server iodevice
SOURCES += \
$$PWD/network/qqtwebsocketserver.cpp
HEADERS += \
$$PWD/network/qqtwebsocketserver.h
SOURCES += $$PWD/network/qqtwebsocketserver.cpp
HEADERS += $$PWD/network/qqtwebsocketserver.h
}
#ethnet(+wifi) manager
@ -324,10 +298,8 @@ contains (DEFINES, __NETWORKSUPPORT__) {
#webaccess manager
contains (DEFINES, __WEBACCESSSUPPORT__) {
SOURCES += \
$$PWD/network/qqtwebaccessmanager.cpp
HEADERS += \
$$PWD/network/qqtwebaccessmanager.h
SOURCES += $$PWD/network/qqtwebaccessmanager.cpp
HEADERS += $$PWD/network/qqtwebaccessmanager.h
}
}