aboutsummaryrefslogtreecommitdiffstats
path: root/mock-server/MockServer.h
blob: b14dda5762a8ffcca58a920b9db9bdecfcb6f6a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
 * @file   MockServer.h
 * @brief  Mock XPL2 server — listens on all three protocol ports.
 */
#pragma once

#include <QList>
#include <QObject>
#include <QTcpServer>
#include <QTimer>

class QTcpSocket;

class MockServer : public QObject
{
  Q_OBJECT

public:
  explicit MockServer (QObject *parent = nullptr);
  static void enableWireDebug ();

private slots:
  void onNewConnection ();
  void onClientMessageReady ();
  void onClientDisconnected ();
  /* Send KA_PING keepalive to all connected clients. */
  void sendKaPing ();

private:
  struct Port
  {
    QTcpServer server;
    const char *name = nullptr;
    quint16 number = 0;
  };

  void setupPort (Port &port, const char *name, quint16 number);
  /* Return a fixed-width "[Name:port]" tag for log lines. */
  QString logTag (quint16 localPort) const;
  /* Parse incoming line, log RX, and dispatch to the matching handler. */
  void handleCommand (QTcpSocket *client, const QByteArray &line);
  void sendReply (QTcpSocket *client, const QByteArray &data);
  void handleKaPing (QTcpSocket *client, const QByteArray &params);
  void handleGsJcVersion (QTcpSocket *client);
  void handleGsPhVersion (QTcpSocket *client, const QByteArray &params);
  /* CN_ control command handlers */
  void handleCnJcSuccess (QTcpSocket *client, const QByteArray &cmd);
  void handleCnPhSuccess (QTcpSocket *client, const QByteArray &cmd,
                          const QByteArray &params);
  void handleCnPhCalibrationData (QTcpSocket *client,
                                  const QByteArray &params);
  void handleCnPhCalibrationRawData (QTcpSocket *client,
                                     const QByteArray &params);
  void handleCnPhCalibratedBaseFrequency (QTcpSocket *client,
                                          const QByteArray &params);
  void handleCnStatusMessagingStart (QTcpSocket *client, const QByteArray &cmd,
                                     const QByteArray &params);
  void handleCnStatusMessagingStop (QTcpSocket *client, const QByteArray &cmd);
  /* CF_ configuration command handlers */
  void handleCfJcSetPurgeSettings (QTcpSocket *client,
                                   const QByteArray &params);
  void handleCfJcSetJettingParams (QTcpSocket *client,
                                   const QByteArray &params);
  void handleCfPhJettingParams (QTcpSocket *client, const QByteArray &cmd,
                                const QByteArray &params);
  void handleCfJcSetter (QTcpSocket *client, const QByteArray &params);
  void handleCfPhSetter (QTcpSocket *client, const QByteArray &params);
  void handleCfJcGetter (QTcpSocket *client, const QByteArray &params);
  void handleCfPhGetter (QTcpSocket *client, const QByteArray &params);

  Port m_command;
  Port m_imaging;
  Port m_status;
  QList<QTcpSocket *> m_clients;
  QTimer m_pingTimer;
  static bool s_wireDebug;
};