| 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 QMODBUSADU_P_H | 
| 38 | #define QMODBUSADU_P_H | 
| 39 |  | 
| 40 | #include <QtSerialBus/qmodbuspdu.h> | 
| 41 |  | 
| 42 | // | 
| 43 | //  W A R N I N G | 
| 44 | //  ------------- | 
| 45 | // | 
| 46 | // This file is not part of the Qt API. It exists purely as an | 
| 47 | // implementation detail. This header file may change from version to | 
| 48 | // version without notice, or even be removed. | 
| 49 | // | 
| 50 | // We mean it. | 
| 51 | // | 
| 52 |  | 
| 53 | QT_BEGIN_NAMESPACE | 
| 54 |  | 
| 55 | class QModbusSerialAdu | 
| 56 | { | 
| 57 | public: | 
| 58 |     enum Type { | 
| 59 |         Ascii, | 
| 60 |         Rtu | 
| 61 |     }; | 
| 62 |  | 
| 63 |     inline QModbusSerialAdu(Type type, const QByteArray &data) | 
| 64 |         : m_type(type), m_data(data), m_rawData(data) | 
| 65 |     { | 
| 66 |         if (m_type == Ascii) | 
| 67 |             m_data = QByteArray::fromHex(hexEncoded: m_data.mid(index: 1, len: m_data.size() - 3)); | 
| 68 |     } | 
| 69 |  | 
| 70 |     inline int size() const { | 
| 71 |         if (m_type == Ascii) | 
| 72 |             return m_data.size() - 1; // one byte, LRC | 
| 73 |         return m_data.size() - 2; // two bytes, CRC | 
| 74 |     } | 
| 75 |     inline QByteArray data() const { return m_data.left(len: size()); } | 
| 76 |  | 
| 77 |     inline int rawSize() const { return m_rawData.size(); } | 
| 78 |     inline QByteArray rawData() const { return m_rawData; } | 
| 79 |  | 
| 80 |     inline int serverAddress() const { | 
| 81 |         Q_ASSERT_X(!m_data.isEmpty(), "QModbusAdu::serverAddress()" , "Empty ADU." ); | 
| 82 |         return quint8(m_data.at(i: 0)); | 
| 83 |     } | 
| 84 |  | 
| 85 |     inline QModbusPdu pdu() const { | 
| 86 |         Q_ASSERT_X(!m_data.isEmpty(), "QModbusAdu::pdu()" , "Empty ADU." ); | 
| 87 |         return QModbusPdu(QModbusPdu::FunctionCode(m_data.at(i: 1)), m_data.mid(index: 2, len: size() - 2)); | 
| 88 |     } | 
| 89 |  | 
| 90 |     template <typename T> | 
| 91 |     auto checksum() const -> decltype(T()) { | 
| 92 |         Q_ASSERT_X(!m_data.isEmpty(), "QModbusAdu::checksum()" , "Empty ADU." ); | 
| 93 |         if (m_type == Ascii) | 
| 94 |             return quint8(m_data[m_data.size() - 1]); | 
| 95 |         return quint16(quint8(m_data[m_data.size() - 2]) << 8 | quint8(m_data[m_data.size() - 1])); | 
| 96 |     } | 
| 97 |  | 
| 98 |     inline bool matchingChecksum() const { | 
| 99 |         Q_ASSERT_X(!m_data.isEmpty(), "QModbusAdu::matchingChecksum()" , "Empty ADU." ); | 
| 100 |         if (m_type == Ascii) | 
| 101 |             return QModbusSerialAdu::calculateLRC(data: data(), len: size()) == checksum<quint8>(); | 
| 102 |         return QModbusSerialAdu::calculateCRC(data: data(), len: size()) == checksum<quint16>(); | 
| 103 |     } | 
| 104 |  | 
| 105 |     /*! | 
| 106 |         \internal | 
| 107 |         \fn quint8 QModbusSerialAdu::calculateLRC(const char *data, qint32 len) | 
| 108 |  | 
| 109 |         Returns the LRC checksum of the first \a len bytes of \a data. The checksum is independent | 
| 110 |         of the byte order (endianness). | 
| 111 |     */ | 
| 112 |     inline static quint8 calculateLRC(const char *data, qint32 len) | 
| 113 |     { | 
| 114 |         quint32 lrc = 0; | 
| 115 |         while (len--) | 
| 116 |             lrc += *data++; | 
| 117 |         return -(quint8(lrc)); | 
| 118 |     } | 
| 119 |  | 
| 120 |     /*! | 
| 121 |         \internal | 
| 122 |         \fn quint16 QModbusSerialAdu::calculateCRC(const char *data, qint32 len) const | 
| 123 |  | 
| 124 |         Returns the CRC checksum of the first \a len bytes of \a data. | 
| 125 |  | 
| 126 |         \note The code used by the function was generated with pycrc. There is no copyright assigned | 
| 127 |         to the generated code, however, the author of the script requests to show the line stating | 
| 128 |         that the code was generated by pycrc (see implementation). | 
| 129 |     */ | 
| 130 |     inline static quint16 calculateCRC(const char *data, qint32 len) | 
| 131 |     { | 
| 132 |         // Generated by pycrc v0.8.3, https://pycrc.org | 
| 133 |         // Width = 16, Poly = 0x8005, XorIn = 0xffff, ReflectIn = True, | 
| 134 |         // XorOut = 0x0000, ReflectOut = True, Algorithm = bit-by-bit-fast | 
| 135 |  | 
| 136 |         quint16 crc = 0xFFFF; | 
| 137 |         while (len--) { | 
| 138 |             const quint8 c = *data++; | 
| 139 |             for (qint32 i = 0x01; i & 0xFF; i <<= 1) { | 
| 140 |                 bool bit = crc & 0x8000; | 
| 141 |                 if (c & i) | 
| 142 |                     bit = !bit; | 
| 143 |                 crc <<= 1; | 
| 144 |                 if (bit) | 
| 145 |                     crc ^= 0x8005; | 
| 146 |             } | 
| 147 |             crc &= 0xFFFF; | 
| 148 |         } | 
| 149 |         crc = crc_reflect(data: crc & 0xFFFF, len: 16) ^ 0x0000; | 
| 150 |         return (crc >> 8) | (crc << 8); // swap bytes | 
| 151 |     } | 
| 152 |  | 
| 153 |     inline static QByteArray create(Type type, int serverAddress, const QModbusPdu &pdu, | 
| 154 |                                     char delimiter = '\n') { | 
| 155 |         QByteArray result; | 
| 156 |         QDataStream out(&result, QIODevice::WriteOnly); | 
| 157 |         out << quint8(serverAddress) << pdu; | 
| 158 |  | 
| 159 |         if (type == Ascii) { | 
| 160 |             out << calculateLRC(data: result, len: result.size()); | 
| 161 |             return ":"  + result.toHex() + "\r"  + delimiter; | 
| 162 |         } else { | 
| 163 |             out << calculateCRC(data: result, len: result.size()); | 
| 164 |         } | 
| 165 |         return result; | 
| 166 |     } | 
| 167 |  | 
| 168 | private: | 
| 169 |     inline static quint16 crc_reflect(quint16 data, qint32 len) | 
| 170 |     { | 
| 171 |         // Generated by pycrc v0.8.3, https://pycrc.org | 
| 172 |         // Width = 16, Poly = 0x8005, XorIn = 0xffff, ReflectIn = True, | 
| 173 |         // XorOut = 0x0000, ReflectOut = True, Algorithm = bit-by-bit-fast | 
| 174 |  | 
| 175 |         quint16 ret = data & 0x01; | 
| 176 |         for (qint32 i = 1; i < len; i++) { | 
| 177 |             data >>= 1; | 
| 178 |             ret = (ret << 1) | (data & 0x01); | 
| 179 |         } | 
| 180 |         return ret; | 
| 181 |     } | 
| 182 |  | 
| 183 | private: | 
| 184 |     Type m_type = Rtu; | 
| 185 |     QByteArray m_data; | 
| 186 |     QByteArray m_rawData; | 
| 187 | }; | 
| 188 |  | 
| 189 | QT_END_NAMESPACE | 
| 190 |  | 
| 191 | #endif // QMODBUSADU_P_H | 
| 192 |  |