summaryrefslogtreecommitdiffstats
path: root/src/knx/qknxtpdufactory_broadcast.cpp
blob: b6c892800afd1f14e5f08ba7e40dbe0787e82aff (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/******************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtKnx module.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
******************************************************************************/

#include "qknxtpdufactory_p.h"

QT_BEGIN_NAMESPACE

/*!
    \class QKnxTpduFactory::Broadcast
    \internal

    \inmodule QtKnx
    \brief The QKnxTpduFactory::Broadcast class is used to create a valid
    Transport protocol data unit (\l QKnxTpdu) for application services sent by
    broadcast.
*/

// -- A_NetworkParameter

static QKnxTpdu createNetworkParameterTpdu(QKnxTpdu::ApplicationControlField apci,
                                           QKnxInterfaceObjectType object,
                                           QKnxInterfaceObjectProperty property,
                                           const QKnxByteArray &data, // aka. testInfo
                                           const QKnxByteArray &testResult = {})
{
    if (!QKnxInterfaceObjectType::isMatch(object, property))
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast, apci,
        QKnxUtils::QUint16::bytes(quint16(object)) + QKnxUtils::QUint8::bytes(quint8(property))
        + data + testResult };
}

QKnxTpdu QKnxTpduFactory::Broadcast::createNetworkParameterReadTpdu(QKnxInterfaceObjectType object,
    QKnxInterfaceObjectProperty property, const QKnxByteArray &testInfo)
{
    if (testInfo.size() > 250) // L_Data_Extended -> max 254 bytes payload
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid}; // 4 bytes already used for APCI, object type, PID

    return createNetworkParameterTpdu(QKnxTpdu::ApplicationControlField::NetworkParameterRead,
        object, property, testInfo);
}

QKnxTpdu
QKnxTpduFactory::Broadcast::createNetworkParameterResponseTpdu(QKnxInterfaceObjectType object,
    QKnxInterfaceObjectProperty property, const QKnxByteArray &testInfo, const QKnxByteArray &testResult)
{
    if ((testInfo.size() + testResult.size()) > 250) // L_Data_Extended -> max 254 bytes payload
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid}; // 4 bytes already used for APCI, object type, PID

    return createNetworkParameterTpdu(QKnxTpdu::ApplicationControlField::NetworkParameterResponse,
        object, property, testInfo, testResult);
}

QKnxTpdu QKnxTpduFactory::Broadcast::createNetworkParameterWriteTpdu(QKnxInterfaceObjectType object,
    QKnxInterfaceObjectProperty property, const QKnxByteArray &value)
{
    if (value.size() > 250) // L_Data_Extended -> max 254 bytes payload
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid}; // 4 bytes already used for APCI, object type, property id

    return createNetworkParameterTpdu(QKnxTpdu::ApplicationControlField::NetworkParameterWrite,
        object, property, value);
}

QKnxTpdu
QKnxTpduFactory::Broadcast::createNetworkParameterInfoReportTpdu(QKnxInterfaceObjectType object,
    QKnxInterfaceObjectProperty property, const QKnxByteArray &testInfo, const QKnxByteArray &testResult)
{
    if ((testInfo.size() + testResult.size()) > 250) // L_Data_Extended -> max 254 bytes payload
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid}; // 4 bytes already used for APCI, object type, property id

    return createNetworkParameterTpdu(QKnxTpdu::ApplicationControlField::NetworkParameterInfoReport,
        object, property, testInfo, testResult);
}


// -- A_SystemNetworkParameter

static QKnxTpdu createSystemNetworkParameterTpdu(QKnxTpdu::ApplicationControlField apci,
                                                 QKnxInterfaceObjectType object,
                                                 quint16 property,
                                                 const QKnxByteArray &data, // aka. operand, testInfo
                                                 const QKnxByteArray &testResult = {})
{
    if (property > 0x0fff)
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    if ((data.size() + testResult.size()) > 249) // L_Data_Extended -> max 254 bytes payload
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid}; // 5 bytes already used for APCI, object type, PID

    return { QKnxTpdu::TransportControlField::DataSystemBroadcast, apci,
        QKnxUtils::QUint16::bytes(quint16(object)) + QKnxUtils::QUint8::bytes(quint8(property >> 4))
         + QKnxUtils::QUint8::bytes(quint8(property << 4)) + data + testResult };
}

QKnxTpdu QKnxTpduFactory::Broadcast
::createSystemNetworkParameterReadTpdu(QKnxInterfaceObjectType object, quint16 property,
    const QKnxByteArray &testInfo)
{
    return createSystemNetworkParameterTpdu(
        QKnxTpdu::ApplicationControlField::SystemNetworkParameterRead, object, property, testInfo);
}

QKnxTpdu
QKnxTpduFactory::Broadcast::createSystemNetworkParameterResponseTpdu(QKnxInterfaceObjectType object,
    quint16 property, const QKnxByteArray &testInfo, const QKnxByteArray &testResult)
{
    return createSystemNetworkParameterTpdu(
        QKnxTpdu::ApplicationControlField::SystemNetworkParameterResponse, object, property,
        testInfo, testResult);
}

QKnxTpdu
QKnxTpduFactory::Broadcast::createSystemNetworkParameterWriteTpdu(QKnxInterfaceObjectType object,
    quint16 property, const QKnxByteArray &value)
{
    return createSystemNetworkParameterTpdu(
        QKnxTpdu::ApplicationControlField::SystemNetworkParameterWrite, object, property, value);
}


// -- A_IndividualAddress

/*!
    \internal

    Returns a TPDU for Individual Address Read Application Service.
*/
QKnxTpdu QKnxTpduFactory::Broadcast::createIndividualAddressReadTpdu()
{
    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::IndividualAddressRead };
}

/*!
    \internal

    Returns a TPDU for Individual Address Write Application Service with the
    given \l QKnxAddress \a address; or an empty TPDU if the \a address is not
    of type \l QKnxAddress::Individual.
*/
QKnxTpdu QKnxTpduFactory::Broadcast::createIndividualAddressWriteTpdu(const QKnxAddress &address)
{
    if (address.type() != QKnxAddress::Type::Individual)
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::IndividualAddressWrite, address.bytes() };
}

QKnxTpdu
QKnxTpduFactory::Broadcast::createIndividualAddressResponseTpdu()
{
    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::IndividualAddressResponse };
}


// -- A_IndividualAddressSerialNumber

/*!
    \internal

    Returns a TPDU for Individual Address Serial Number Read Application Service
    with the given \a sn set; or an empty TPDU if the \a sn
    has a different size then six octets.
*/
QKnxTpdu
QKnxTpduFactory::Broadcast::createIndividualAddressSerialNumberReadTpdu(const QKnxByteArray &sn)
{
    if (sn.size() != 6)
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::IndividualAddressSerialNumberRead, sn };
}

QKnxTpdu
QKnxTpduFactory::Broadcast::createIndividualAddressSerialNumberResponseTpdu(const QKnxByteArray &sn,
    const QKnxAddress &domainAddress)
{
    if ((sn.size() != 6) || (domainAddress.type() != QKnxAddress::Type::Individual))
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::IndividualAddressSerialNumberResponse, sn
        + domainAddress.bytes() + QKnxUtils::QUint16::bytes(0u) };
}

/*!
    \internal

    Returns a TPDU for Individual Address Serial Number Write Application Service
    with the given \a sn and \l QKnxAddress \a newAddress set; or otherwise an
    empty TPDU if the \a sn has a different size then six octets or the type of
    \a newAddress is not of type \l QKnxAddress::Individual.
*/
QKnxTpdu
QKnxTpduFactory::Broadcast::createIndividualAddressSerialNumberWriteTpdu(const QKnxByteArray &sn,
    const QKnxAddress &newAddress)
{
    if ((sn.size() != 6) || (newAddress.type() != QKnxAddress::Type::Individual))
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::IndividualAddressSerialNumberWrite, sn + newAddress
        .bytes() + QKnxUtils::QUint32::bytes(0u) };
}


// -- A_DomainAddress

/*!
    \internal

    Returns a TPDU for Domain Address Read Application Service.
*/
QKnxTpdu QKnxTpduFactory::Broadcast::createDomainAddressReadTpdu()
{
    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::DomainAddressRead };
}

QKnxTpdu QKnxTpduFactory::Broadcast::createDomainAddressResponseTpdu(const QKnxByteArray &address)
{
    if ((address.size() != 2) && (address.size() != 6))
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::DomainAddressResponse, address };
}

/*!
    \internal

    Returns a TPDU for Domain Address Write Application Service with the given
    \l QKnxAddress \a address set.
*/
QKnxTpdu QKnxTpduFactory::Broadcast::createDomainAddressWriteTpdu(const QKnxByteArray &address)
{
    if ((address.size() != 2) && (address.size() != 6))
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::DomainAddressWrite, address };
}


// -- A_CreateDomainAddressSerialNumber

/*!
    \internal

    Returns a TPDU for Domain Address Serial Number Read Application Service
    with the given \a sn set; or an empty TPDU if the \a sn has a different size
    then six octets.
*/
QKnxTpdu
QKnxTpduFactory::Broadcast::createDomainAddressSerialNumberReadTpdu(const QKnxByteArray &sn)
{
    if (sn.size() != 6)
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::DomainAddressSerialNumberRead, sn };
}

QKnxTpdu
QKnxTpduFactory::Broadcast::createDomainAddressSerialNumberResponseTpdu(const QKnxByteArray &sn,
    const QKnxByteArray &domainAddress)
{
    if ((sn.size() != 6) || ((domainAddress.size() != 2) && (domainAddress.size() != 6)))
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::DomainAddressSerialNumberResponse, sn + domainAddress };
}

/*!
    \internal

    Returns a TPDU for Domain Address Serial Number Write Application Service
    with the given \a sn and \l QKnxAddress \a domainAddress set; or otherwise
    an empty TPDU if the \a sn has a different size then six octets.
*/
QKnxTpdu QKnxTpduFactory::Broadcast::createDomainAddressSerialNumberWriteTpdu(const QKnxByteArray &sn,
    const QKnxByteArray &domainAddress)
{
    if ((sn.size() != 6) || ((domainAddress.size() != 2) && (domainAddress.size() != 6)))
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::DomainAddressSerialNumberWrite, sn + domainAddress };
}


// -- A_DomainAddressSelective_Read

QKnxTpdu QKnxTpduFactory::Broadcast::createPll110DomainAddressSelectiveReadTpdu(quint8 domainAddress,
    const QKnxAddress &startAddress, quint8 range)
{
    if (startAddress.type() != QKnxAddress::Type::Individual)
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::DomainAddressSelectiveRead, QKnxUtils::QUint8::bytes(0u)
        + QKnxUtils::QUint8::bytes(domainAddress) + startAddress.bytes()
        + QKnxUtils::QUint8::bytes(range) };
}

QKnxTpdu
QKnxTpduFactory::Broadcast::createRfDomainAddressSelectiveReadTpdu(const QKnxByteArray &startAddress,
    const QKnxByteArray &endAddress)
{
    if ((startAddress.size() != 6) && (endAddress.size() != 6))
        return {QKnxTpdu::TransportControlField::Invalid,
            QKnxTpdu::ApplicationControlField::Invalid};

    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::DomainAddressSelectiveRead, startAddress + endAddress
        + QKnxUtils::QUint8::bytes(0u) };
}

QKnxTpdu QKnxTpduFactory::Broadcast::createFeDomainAddressSelectiveReadTpdu(quint16 manufacturerId,
    QKnxInterfaceObjectType obj, QKnxInterfaceObjectProperty property, quint16 parameters)
{
    return { QKnxTpdu::TransportControlField::DataBroadcast,
        QKnxTpdu::ApplicationControlField::DomainAddressSelectiveRead,
        QKnxUtils::QUint16::bytes(manufacturerId) + QKnxUtils::QUint16::bytes(quint16(obj))
        + QKnxUtils::QUint8::bytes(quint8(property)) + QKnxUtils::QUint16::bytes(parameters) };
}

QT_END_NAMESPACE