2013-08-17 15:39:29 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// stl includes
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
// Qt includes
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QTcpSocket>
|
2016-12-18 19:00:14 +01:00
|
|
|
#include <QMutex>
|
2013-08-17 15:39:29 +02:00
|
|
|
|
2013-08-18 12:21:07 +02:00
|
|
|
// Hyperion includes
|
|
|
|
#include <hyperion/Hyperion.h>
|
|
|
|
|
2013-08-17 19:20:19 +02:00
|
|
|
// util includes
|
2016-10-09 22:22:17 +02:00
|
|
|
#include <utils/jsonschema/QJsonSchemaChecker.h>
|
2016-07-11 17:08:22 +02:00
|
|
|
#include <utils/Logger.h>
|
2016-08-04 13:10:53 +02:00
|
|
|
#include <utils/Components.h>
|
2013-08-17 19:20:19 +02:00
|
|
|
|
2013-08-18 20:55:59 +02:00
|
|
|
class ImageProcessor;
|
|
|
|
|
2016-12-05 11:10:44 +01:00
|
|
|
|
|
|
|
/// Constants and utility functions related to WebSocket opcodes
|
|
|
|
/**
|
|
|
|
* WebSocket Opcodes are 4 bits. See RFC6455 section 5.2.
|
|
|
|
*/
|
|
|
|
namespace OPCODE {
|
|
|
|
enum value {
|
|
|
|
CONTINUATION = 0x0,
|
|
|
|
TEXT = 0x1,
|
|
|
|
BINARY = 0x2,
|
|
|
|
RSV3 = 0x3,
|
|
|
|
RSV4 = 0x4,
|
|
|
|
RSV5 = 0x5,
|
|
|
|
RSV6 = 0x6,
|
|
|
|
RSV7 = 0x7,
|
|
|
|
CLOSE = 0x8,
|
|
|
|
PING = 0x9,
|
|
|
|
PONG = 0xA,
|
|
|
|
CONTROL_RSVB = 0xB,
|
|
|
|
CONTROL_RSVC = 0xC,
|
|
|
|
CONTROL_RSVD = 0xD,
|
|
|
|
CONTROL_RSVE = 0xE,
|
|
|
|
CONTROL_RSVF = 0xF
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Check if an opcode is reserved
|
|
|
|
/**
|
|
|
|
* @param v The opcode to test.
|
|
|
|
* @return Whether or not the opcode is reserved.
|
|
|
|
*/
|
|
|
|
inline bool reserved(value v) {
|
|
|
|
return (v >= RSV3 && v <= RSV7) || (v >= CONTROL_RSVB && v <= CONTROL_RSVF);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if an opcode is invalid
|
|
|
|
/**
|
|
|
|
* Invalid opcodes are negative or require greater than 4 bits to store.
|
|
|
|
*
|
|
|
|
* @param v The opcode to test.
|
|
|
|
* @return Whether or not the opcode is invalid.
|
|
|
|
*/
|
|
|
|
inline bool invalid(value v) {
|
|
|
|
return (v > 0xF || v < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if an opcode is for a control frame
|
|
|
|
/**
|
|
|
|
* @param v The opcode to test.
|
|
|
|
* @return Whether or not the opcode is a control opcode.
|
|
|
|
*/
|
|
|
|
inline bool is_control(value v) {
|
|
|
|
return v >= 0x8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 19:48:15 +01:00
|
|
|
struct find_schema: std::unary_function<EffectSchema, bool>
|
|
|
|
{
|
|
|
|
QString pyFile;
|
|
|
|
find_schema(QString pyFile):pyFile(pyFile) { }
|
|
|
|
bool operator()(EffectSchema const& schema) const
|
|
|
|
{
|
|
|
|
return schema.pyFile == pyFile;
|
|
|
|
}
|
|
|
|
};
|
2016-12-05 11:10:44 +01:00
|
|
|
|
2016-12-16 19:48:15 +01:00
|
|
|
struct find_effect: std::unary_function<EffectDefinition, bool>
|
|
|
|
{
|
|
|
|
QString effectName;
|
|
|
|
find_effect(QString effectName) :effectName(effectName) { }
|
|
|
|
bool operator()(EffectDefinition const& effectDefinition) const
|
|
|
|
{
|
|
|
|
return effectDefinition.name == effectName;
|
|
|
|
}
|
|
|
|
};
|
2016-12-05 11:10:44 +01:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// The Connection object created by \a JsonServer when a new connection is establshed
|
2013-08-31 14:36:54 +02:00
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
class JsonClientConnection : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Constructor
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param socket The Socket object for this connection
|
|
|
|
/// @param hyperion The Hyperion server
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2016-07-31 22:21:35 +02:00
|
|
|
JsonClientConnection(QTcpSocket * socket);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Destructor
|
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
~JsonClientConnection();
|
|
|
|
|
2016-08-11 07:13:55 +02:00
|
|
|
public slots:
|
|
|
|
void componentStateChanged(const hyperion::Components component, bool enable);
|
2016-09-07 20:10:37 +02:00
|
|
|
void streamLedcolorsUpdate();
|
2016-11-26 22:34:46 +01:00
|
|
|
void incommingLogMessage(Logger::T_LOG_MESSAGE);
|
2016-12-18 19:00:14 +01:00
|
|
|
void setImage(int priority, const Image<ColorRgb> & image, int duration_ms);
|
2016-08-11 07:13:55 +02:00
|
|
|
|
2013-08-17 15:39:29 +02:00
|
|
|
signals:
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Signal which is emitted when the connection is being closed
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param connection This connection object
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
void connectionClosed(JsonClientConnection * connection);
|
|
|
|
|
|
|
|
private slots:
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Slot called when new data has arrived
|
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
void readData();
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Slot called when this connection is being closed
|
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
void socketClosed();
|
|
|
|
|
2016-09-07 20:10:37 +02:00
|
|
|
|
2013-08-17 15:39:29 +02:00
|
|
|
private:
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message as string
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleMessage(const QString & message);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Color message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleColorCommand(const QJsonObject & message, const QString &command, const int tan);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Image message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleImageCommand(const QJsonObject & message, const QString &command, const int tan);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-11-24 16:10:48 +01:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Effect message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleEffectCommand(const QJsonObject & message, const QString &command, const int tan);
|
2013-11-24 16:10:48 +01:00
|
|
|
|
2016-10-24 23:52:53 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Effect message (Write JSON Effect)
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
|
|
|
void handleCreateEffectCommand(const QJsonObject & message, const QString &command, const int tan);
|
|
|
|
|
2016-11-18 18:39:21 +01:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Effect message (Delete JSON Effect)
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
|
|
|
void handleDeleteEffectCommand(const QJsonObject & message, const QString &command, const int tan);
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Server info message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleServerInfoCommand(const QJsonObject & message, const QString &command, const int tan);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Clear message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleClearCommand(const QJsonObject & message, const QString &command, const int tan);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Clearall message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleClearallCommand(const QJsonObject & message, const QString &command, const int tan);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2016-04-02 00:04:11 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Adjustment message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleAdjustmentCommand(const QJsonObject & message, const QString &command, const int tan);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2016-07-31 22:21:35 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON SourceSelect message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleSourceSelectCommand(const QJsonObject & message, const QString &command, const int tan);
|
2016-08-03 22:03:19 +02:00
|
|
|
|
2016-08-15 22:32:01 +02:00
|
|
|
/// Handle an incoming JSON GetConfig message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleConfigCommand(const QJsonObject & message, const QString &command, const int tan);
|
2016-08-15 22:32:01 +02:00
|
|
|
|
|
|
|
/// Handle an incoming JSON GetConfig message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleSchemaGetCommand(const QJsonObject & message, const QString &command, const int tan);
|
2016-08-15 22:32:01 +02:00
|
|
|
|
2016-08-03 22:03:19 +02:00
|
|
|
/// Handle an incoming JSON GetConfig message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleConfigGetCommand(const QJsonObject & message, const QString &command, const int tan);
|
2016-08-14 20:17:12 +02:00
|
|
|
|
2016-08-04 13:10:53 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Component State message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleComponentStateCommand(const QJsonObject & message, const QString &command, const int tan);
|
2016-07-31 22:21:35 +02:00
|
|
|
|
2016-09-05 17:26:29 +02:00
|
|
|
/// Handle an incoming JSON Led Colors message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void handleLedColorsCommand(const QJsonObject & message, const QString &command, const int tan);
|
2016-09-05 17:26:29 +02:00
|
|
|
|
2016-11-26 22:34:46 +01:00
|
|
|
/// Handle an incoming JSON Logging message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
|
|
|
void handleLoggingCommand(const QJsonObject & message, const QString &command, const int tan);
|
|
|
|
|
2016-12-19 23:59:50 +01:00
|
|
|
/// Handle an incoming JSON Proccessing message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
|
|
|
void handleProcessingCommand(const QJsonObject & message, const QString &command, const int tan);
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON message of unknown type
|
|
|
|
///
|
2013-08-18 12:02:17 +02:00
|
|
|
void handleNotImplemented();
|
2013-08-17 15:39:29 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Send a message to the connected client
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message The JSON message to send
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void sendMessage(const QJsonObject & message);
|
|
|
|
void sendMessage(const QJsonObject & message, QTcpSocket * socket);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Send a standard reply indicating success
|
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void sendSuccessReply(const QString &command="", const int tan=0);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Send an error message back to the client
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param error String describing the error
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void sendErrorReply(const QString & error, const QString &command="", const int tan=0);
|
2014-11-08 21:01:46 +01:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Do handshake for a websocket connection
|
|
|
|
///
|
|
|
|
void doWebSocketHandshake();
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Handle incoming websocket data frame
|
|
|
|
///
|
|
|
|
void handleWebSocketFrame();
|
2013-08-17 15:39:29 +02:00
|
|
|
|
2016-02-15 18:25:18 +01:00
|
|
|
///
|
|
|
|
/// forward json message
|
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
void forwardJsonMessage(const QJsonObject & message);
|
2016-02-15 18:25:18 +01:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Check if a JSON messag is valid according to a given JSON schema
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message JSON message which need to be checked
|
2016-08-14 20:17:12 +02:00
|
|
|
/// @param schemaResource Qt Resource identifier with the JSON schema
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param errors Output error message
|
2016-08-14 20:17:12 +02:00
|
|
|
/// @param ignoreRequired ignore the required value in JSON schema
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @return true if message conforms the given JSON schema
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2016-10-09 22:22:17 +02:00
|
|
|
bool checkJson(const QJsonObject & message, const QString &schemaResource, QString & errors, bool ignoreRequired = false);
|
2013-08-17 15:39:29 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
/// The TCP-Socket that is connected tot the Json-client
|
2013-08-18 12:02:17 +02:00
|
|
|
QTcpSocket * _socket;
|
2013-08-17 19:20:19 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
/// The processor for translating images to led-values
|
2013-08-18 20:55:59 +02:00
|
|
|
ImageProcessor * _imageProcessor;
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
/// Link to Hyperion for writing led-values to a priority channel
|
2013-08-18 12:21:07 +02:00
|
|
|
Hyperion * _hyperion;
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
/// The buffer used for reading data from the socket
|
2013-08-17 15:39:29 +02:00
|
|
|
QByteArray _receiveBuffer;
|
2014-10-25 22:35:53 +02:00
|
|
|
|
|
|
|
/// used for WebSocket detection and connection handling
|
|
|
|
bool _webSocketHandshakeDone;
|
2016-07-31 22:21:35 +02:00
|
|
|
|
2016-08-11 07:13:55 +02:00
|
|
|
/// The logger instance
|
2016-07-11 17:08:22 +02:00
|
|
|
Logger * _log;
|
2016-08-11 07:13:55 +02:00
|
|
|
|
|
|
|
/// Flag if forwarder is enabled
|
|
|
|
bool _forwarder_enabled;
|
2016-09-05 17:26:29 +02:00
|
|
|
|
2016-11-26 22:34:46 +01:00
|
|
|
/// timer for ledcolors streaming
|
2016-09-05 17:26:29 +02:00
|
|
|
QTimer _timer_ledcolors;
|
2016-12-18 19:00:14 +01:00
|
|
|
|
2016-11-26 22:34:46 +01:00
|
|
|
// streaming buffers
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject _streaming_leds_reply;
|
2016-12-18 19:00:14 +01:00
|
|
|
QJsonObject _streaming_image_reply;
|
2016-11-26 22:34:46 +01:00
|
|
|
QJsonObject _streaming_logging_reply;
|
2016-12-18 19:00:14 +01:00
|
|
|
|
|
|
|
/// flag to determine state of log streaming
|
2016-11-26 22:34:46 +01:00
|
|
|
bool _streaming_logging_activated;
|
2016-09-05 17:26:29 +02:00
|
|
|
|
2016-12-18 19:00:14 +01:00
|
|
|
/// mutex to determine state of image streaming
|
|
|
|
QMutex _image_stream_mutex;
|
|
|
|
|
|
|
|
/// timeout for live video refresh
|
|
|
|
volatile qint64 _image_stream_timeout;
|
|
|
|
|
2016-12-05 11:10:44 +01:00
|
|
|
// masks for fields in the basic header
|
|
|
|
static uint8_t const BHB0_OPCODE = 0x0F;
|
|
|
|
static uint8_t const BHB0_RSV3 = 0x10;
|
|
|
|
static uint8_t const BHB0_RSV2 = 0x20;
|
|
|
|
static uint8_t const BHB0_RSV1 = 0x40;
|
|
|
|
static uint8_t const BHB0_FIN = 0x80;
|
|
|
|
|
|
|
|
static uint8_t const BHB1_PAYLOAD = 0x7F;
|
|
|
|
static uint8_t const BHB1_MASK = 0x80;
|
|
|
|
|
|
|
|
static uint8_t const payload_size_code_16bit = 0x7E; // 126
|
|
|
|
static uint8_t const payload_size_code_64bit = 0x7F; // 127
|
2013-08-17 15:39:29 +02:00
|
|
|
};
|