1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtSerialBus module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 
37 #ifndef QMODBUSERVER_P_H
38 #define QMODBUSERVER_P_H
39 
40 #include <QtSerialBus/qmodbusdataunit.h>
41 #include <QtSerialBus/qmodbusserver.h>
42 
43 #include <private/qmodbuscommevent_p.h>
44 #include <private/qmodbusdevice_p.h>
45 #include <private/qmodbus_symbols_p.h>
46 
47 #include <array>
48 #include <deque>
49 
50 //
51 //  W A R N I N G
52 //  -------------
53 //
54 // This file is not part of the Qt API. It exists purely as an
55 // implementation detail. This header file may change from version to
56 // version without notice, or even be removed.
57 //
58 // We mean it.
59 //
60 
61 QT_BEGIN_NAMESPACE
62 
63 class QModbusServerPrivate : public QModbusDevicePrivate
64 {
65     Q_DECLARE_PUBLIC(QModbusServer)
66 
67 public:
68     enum Counter {
69         CommEvent = 0x0001,
70         BusMessage = Diagnostics::ReturnBusMessageCount,
71         BusCommunicationError = Diagnostics::ReturnBusCommunicationErrorCount,
72         BusExceptionError = Diagnostics::ReturnBusExceptionErrorCount,
73         ServerMessage = Diagnostics::ReturnServerMessageCount,
74         ServerNoResponse = Diagnostics::ReturnServerNoResponseCount,
75         ServerNAK = Diagnostics::ReturnServerNAKCount,
76         ServerBusy = Diagnostics::ReturnServerBusyCount,
77         BusCharacterOverrun = Diagnostics::ReturnBusCharacterOverrunCount
78     };
79 
QModbusServerPrivate()80     QModbusServerPrivate()
81         : m_counters()
82     {
83     }
84 
85     bool setMap(const QModbusDataUnitMap &map);
86 
resetCommunicationCounters()87     void resetCommunicationCounters() { m_counters.fill(0u); }
incrementCounter(QModbusServerPrivate::Counter counter)88     void incrementCounter(QModbusServerPrivate::Counter counter) { m_counters[counter]++; }
89 
90     QModbusResponse processRequest(const QModbusPdu &request);
91 
92     QModbusResponse processReadCoilsRequest(const QModbusRequest &request);
93     QModbusResponse processReadDiscreteInputsRequest(const QModbusRequest &request);
94     QModbusResponse readBits(const QModbusPdu &request, QModbusDataUnit::RegisterType unitType);
95 
96     QModbusResponse processReadHoldingRegistersRequest(const QModbusRequest &request);
97     QModbusResponse processReadInputRegistersRequest(const QModbusRequest &request);
98     QModbusResponse readBytes(const QModbusPdu &request, QModbusDataUnit::RegisterType unitType);
99 
100     QModbusResponse processWriteSingleCoilRequest(const QModbusRequest &request);
101     QModbusResponse processWriteSingleRegisterRequest(const QModbusRequest &request);
102     QModbusResponse writeSingle(const QModbusPdu &request, QModbusDataUnit::RegisterType unitType);
103 
104     QModbusResponse processReadExceptionStatusRequest(const QModbusRequest &request);
105     QModbusResponse processDiagnosticsRequest(const QModbusRequest &request);
106     QModbusResponse processGetCommEventCounterRequest(const QModbusRequest &request);
107     QModbusResponse processGetCommEventLogRequest(const QModbusRequest &request);
108     QModbusResponse processWriteMultipleCoilsRequest(const QModbusRequest &request);
109     QModbusResponse processWriteMultipleRegistersRequest(const QModbusRequest &request);
110     QModbusResponse processReportServerIdRequest(const QModbusRequest &request);
111     QModbusResponse processMaskWriteRegisterRequest(const QModbusRequest &request);
112     QModbusResponse processReadWriteMultipleRegistersRequest(const QModbusRequest &request);
113     QModbusResponse processReadFifoQueueRequest(const QModbusRequest &request);
114     QModbusResponse processEncapsulatedInterfaceTransportRequest(const QModbusRequest &request);
115 
116     void storeModbusCommEvent(const QModbusCommEvent &eventByte);
117 
118     int m_serverAddress = 1;
119     std::array<quint16, 20> m_counters;
120     QHash<int, QVariant> m_serverOptions;
121     QModbusDataUnitMap m_modbusDataUnitMap;
122     std::deque<quint8> m_commEventLog;
123 };
124 
125 QT_END_NAMESPACE
126 
127 #endif // QMODBUSERVER_P_H
128