1 | // Copyright (C) 2017 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QMODBUSDEVICEIDENTIFICATION_P_H |
5 | #define QMODBUSDEVICEIDENTIFICATION_P_H |
6 | |
7 | #include <QtCore/qmap.h> |
8 | #include <QtCore/qmetatype.h> |
9 | #include <QtSerialBus/qtserialbusglobal.h> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | class QModbusDeviceIdentification |
14 | { |
15 | public: |
16 | enum ObjectId { |
17 | /* Basic mandatory */ |
18 | VendorNameObjectId = 0x00, |
19 | ProductCodeObjectId = 0x01, |
20 | MajorMinorRevisionObjectId = 0x02, |
21 | |
22 | /* Regular optional */ |
23 | VendorUrlObjectId = 0x03, |
24 | ProductNameObjectId = 0x04, |
25 | ModelNameObjectId = 0x05, |
26 | UserApplicationNameObjectId = 0x06, |
27 | ReservedObjectId = 0x07, |
28 | |
29 | /* Extended optional */ |
30 | ProductDependentObjectId = 0x80, |
31 | |
32 | UndefinedObjectId = 0x100 |
33 | }; |
34 | |
35 | enum ReadDeviceIdCode { |
36 | BasicReadDeviceIdCode = 0x01, |
37 | RegularReadDeviceIdCode = 0x02, |
38 | ExtendedReadDeviceIdCode = 0x03, |
39 | IndividualReadDeviceIdCode = 0x04 |
40 | }; |
41 | |
42 | enum ConformityLevel { |
43 | BasicConformityLevel = 0x01, |
44 | RegularConformityLevel = 0x02, |
45 | ExtendedConformityLevel = 0x03, |
46 | BasicIndividualConformityLevel = 0x81, |
47 | RegularIndividualConformityLevel = 0x82, |
48 | ExtendedIndividualConformityLevel = 0x83 |
49 | }; |
50 | |
51 | QModbusDeviceIdentification() = default; |
52 | |
53 | bool isValid() const { |
54 | return !m_objects.value(key: VendorNameObjectId).isEmpty() |
55 | && !m_objects.value(key: ProductCodeObjectId).isEmpty() |
56 | && !m_objects.value(key: MajorMinorRevisionObjectId).isEmpty(); |
57 | } |
58 | |
59 | QList<int> objectIds() const { return m_objects.keys(); } |
60 | void remove(uint objectId) { m_objects.remove(key: objectId); } |
61 | bool contains(uint objectId) const { return m_objects.contains(key: objectId); } |
62 | |
63 | bool insert(uint objectId, const QByteArray &data) { |
64 | if ((data.size() > 245) || (objectId >= ObjectId::UndefinedObjectId)) |
65 | return false; |
66 | m_objects[objectId] = data; |
67 | return true; |
68 | } |
69 | QByteArray value(uint objectId) const { return m_objects.value(key: objectId); } |
70 | |
71 | ConformityLevel conformityLevel() const { return m_conformityLevel; } |
72 | void setConformityLevel(ConformityLevel level) { m_conformityLevel = level; } |
73 | |
74 | static Q_SERIALBUS_EXPORT QModbusDeviceIdentification fromByteArray(const QByteArray &ba); |
75 | |
76 | private: |
77 | QMap<int, QByteArray> m_objects; |
78 | ConformityLevel m_conformityLevel = BasicConformityLevel; |
79 | }; |
80 | Q_DECLARE_TYPEINFO(QModbusDeviceIdentification, Q_RELOCATABLE_TYPE); |
81 | Q_DECLARE_TYPEINFO(QModbusDeviceIdentification::ObjectId, Q_PRIMITIVE_TYPE); |
82 | Q_DECLARE_TYPEINFO(QModbusDeviceIdentification::ReadDeviceIdCode, Q_PRIMITIVE_TYPE); |
83 | Q_DECLARE_TYPEINFO(QModbusDeviceIdentification::ConformityLevel, Q_PRIMITIVE_TYPE); |
84 | |
85 | QT_END_NAMESPACE |
86 | |
87 | Q_DECLARE_METATYPE(QModbusDeviceIdentification) |
88 | |
89 | #endif // QMODBUSDEVICEIDENTIFICATION_P_H |
90 | |