-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathKnxHelper.cs
418 lines (366 loc) · 16.5 KB
/
KnxHelper.cs
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
using System;
using System.Linq;
using KNXLib.Exceptions;
namespace KNXLib
{
internal class KnxHelper
{
#region Address Processing
// +-----------------------------------------------+
// 16 bits | INDIVIDUAL ADDRESS |
// +-----------------------+-----------------------+
// | OCTET 0 (high byte) | OCTET 1 (low byte) |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// bits | 7| 6| 5| 4| 3| 2| 1| 0| 7| 6| 5| 4| 3| 2| 1| 0|
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | Subnetwork Address | |
// +-----------+-----------+ Device Address |
// |(Area Adrs)|(Line Adrs)| |
// +-----------------------+-----------------------+
// +-----------------------------------------------+
// 16 bits | GROUP ADDRESS (3 level) |
// +-----------------------+-----------------------+
// | OCTET 0 (high byte) | OCTET 1 (low byte) |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// bits | 7| 6| 5| 4| 3| 2| 1| 0| 7| 6| 5| 4| 3| 2| 1| 0|
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | | Main Grp | Midd G | Sub Group |
// +--+--------------------+-----------------------+
// +-----------------------------------------------+
// 16 bits | GROUP ADDRESS (2 level) |
// +-----------------------+-----------------------+
// | OCTET 0 (high byte) | OCTET 1 (low byte) |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// bits | 7| 6| 5| 4| 3| 2| 1| 0| 7| 6| 5| 4| 3| 2| 1| 0|
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | | Main Grp | Sub Group |
// +--+--------------------+-----------------------+
public static bool IsAddressIndividual(string address)
{
return address.Contains('.');
}
public static string GetIndividualAddress(byte[] addr)
{
return GetAddress(addr, '.', false);
}
public static string GetGroupAddress(byte[] addr, bool threeLevelAddressing)
{
return GetAddress(addr, '/', threeLevelAddressing);
}
private static string GetAddress(byte[] addr, char separator, bool threeLevelAddressing)
{
var group = separator.Equals('/');
string address;
if (group && !threeLevelAddressing)
{
// 2 level group
address = (addr[0] >> 3).ToString();
address += separator;
address += (((addr[0] & 0x07) << 8) + addr[1]).ToString(); // this may not work, must be checked
}
else
{
// 3 level individual or group
address = group
? ((addr[0] & 0x7F) >> 3).ToString()
: (addr[0] >> 4).ToString();
address += separator;
if (group)
address += (addr[0] & 0x07).ToString();
else
address += (addr[0] & 0x0F).ToString();
address += separator;
address += addr[1].ToString();
}
return address;
}
public static byte[] GetAddress(string address)
{
try
{
var addr = new byte[2];
var threeLevelAddressing = true;
string[] parts;
var group = address.Contains('/');
if (!group)
{
// individual address
parts = address.Split('.');
if (parts.Length != 3 || parts[0].Length > 2 || parts[1].Length > 2 || parts[2].Length > 3)
throw new InvalidKnxAddressException(address);
}
else
{
// group address
parts = address.Split('/');
if (parts.Length != 3 || parts[0].Length > 2 || parts[1].Length > 1 || parts[2].Length > 3)
{
if (parts.Length != 2 || parts[0].Length > 2 || parts[1].Length > 4)
throw new InvalidKnxAddressException(address);
threeLevelAddressing = false;
}
}
if (!threeLevelAddressing)
{
var part = int.Parse(parts[0]);
if (part > 15)
throw new InvalidKnxAddressException(address);
addr[0] = (byte)(part << 3);
part = int.Parse(parts[1]);
if (part > 2047)
throw new InvalidKnxAddressException(address);
var part2 = BitConverter.GetBytes(part);
if (part2.Length > 2)
throw new InvalidKnxAddressException(address);
addr[0] = (byte)(addr[0] | part2[0]);
addr[1] = part2[1];
}
else
{
var part = int.Parse(parts[0]);
if (part > 15)
throw new InvalidKnxAddressException(address);
addr[0] = group
? (byte)(part << 3)
: (byte)(part << 4);
part = int.Parse(parts[1]);
if ((group && part > 7) || (!group && part > 15))
throw new InvalidKnxAddressException(address);
addr[0] = (byte)(addr[0] | part);
part = int.Parse(parts[2]);
if (part > 255)
throw new InvalidKnxAddressException(address);
addr[1] = (byte)part;
}
return addr;
}
catch (Exception)
{
throw new InvalidKnxAddressException(address);
}
}
#endregion
#region Control Fields
// Bit order
// +---+---+---+---+---+---+---+---+
// | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
// +---+---+---+---+---+---+---+---+
// Control Field 1
// Bit |
// ------+---------------------------------------------------------------
// 7 | Frame Type - 0x0 for extended frame
// | 0x1 for standard frame
// ------+---------------------------------------------------------------
// 6 | Reserved
// |
// ------+---------------------------------------------------------------
// 5 | Repeat Flag - 0x0 repeat frame on medium in case of an error
// | 0x1 do not repeat
// ------+---------------------------------------------------------------
// 4 | System Broadcast - 0x0 system broadcast
// | 0x1 broadcast
// ------+---------------------------------------------------------------
// 3 | Priority - 0x0 system
// | 0x1 normal (also called alarm priority)
// ------+ 0x2 urgent (also called high priority)
// 2 | 0x3 low
// |
// ------+---------------------------------------------------------------
// 1 | Acknowledge Request - 0x0 no ACK requested
// | (L_Data.req) 0x1 ACK requested
// ------+---------------------------------------------------------------
// 0 | Confirm - 0x0 no error
// | (L_Data.con) - 0x1 error
// ------+---------------------------------------------------------------
// Control Field 2
// Bit |
// ------+---------------------------------------------------------------
// 7 | Destination Address Type - 0x0 individual address
// | - 0x1 group address
// ------+---------------------------------------------------------------
// 6-4 | Hop Count (0-7)
// ------+---------------------------------------------------------------
// 3-0 | Extended Frame Format - 0x0 standard frame
// ------+---------------------------------------------------------------
public enum KnxDestinationAddressType
{
INDIVIDUAL = 0,
GROUP = 1
}
public static KnxDestinationAddressType GetKnxDestinationAddressType(byte control_field_2)
{
return (0x80 & control_field_2) != 0
? KnxDestinationAddressType.GROUP
: KnxDestinationAddressType.INDIVIDUAL;
}
#endregion
#region Data Processing
// In the Common EMI frame, the APDU payload is defined as follows:
// +--------+--------+--------+--------+--------+
// | TPCI + | APCI + | Data | Data | Data |
// | APCI | Data | | | |
// +--------+--------+--------+--------+--------+
// byte 1 byte 2 byte 3 ... byte 16
// For data that is 6 bits or less in length, only the first two bytes are used in a Common EMI
// frame. Common EMI frame also carries the information of the expected length of the Protocol
// Data Unit (PDU). Data payload can be at most 14 bytes long. <p>
// The first byte is a combination of transport layer control information (TPCI) and application
// layer control information (APCI). First 6 bits are dedicated for TPCI while the two least
// significant bits of first byte hold the two most significant bits of APCI field, as follows:
// Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 Bit 8 Bit 1 Bit 2
// +--------+--------+--------+--------+--------+--------+--------+--------++--------+----....
// | | | | | | | | || |
// | TPCI | TPCI | TPCI | TPCI | TPCI | TPCI | APCI | APCI || APCI |
// | | | | | | |(bit 1) |(bit 2) ||(bit 3) |
// +--------+--------+--------+--------+--------+--------+--------+--------++--------+----....
// + B Y T E 1 || B Y T E 2
// +-----------------------------------------------------------------------++-------------....
//Total number of APCI control bits can be either 4 or 10. The second byte bit structure is as follows:
// Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 Bit 8 Bit 1 Bit 2
// +--------+--------+--------+--------+--------+--------+--------+--------++--------+----....
// | | | | | | | | || |
// | APCI | APCI | APCI/ | APCI/ | APCI/ | APCI/ | APCI/ | APCI/ || Data | Data
// |(bit 3) |(bit 4) | Data | Data | Data | Data | Data | Data || |
// +--------+--------+--------+--------+--------+--------+--------+--------++--------+----....
// + B Y T E 2 || B Y T E 3
// +-----------------------------------------------------------------------++-------------....
public static string GetData(int dataLength, byte[] apdu)
{
switch (dataLength)
{
case 0:
return string.Empty;
case 1:
return Convert.ToChar(0x3F & apdu[1]).ToString();
case 2:
return Convert.ToChar(apdu[2]).ToString();
default:
var data = string.Empty;
for (var i = 2; i < apdu.Length; i++)
data += Convert.ToChar(apdu[i]);
return data;
}
}
public static int GetDataLength(byte[] data)
{
if (data.Length <= 0)
return 0;
if (data.Length == 1 && data[0] < 0x3F)
return 1;
if (data[0] < 0x3F)
return data.Length;
return data.Length + 1;
}
public static void WriteData(byte[] datagram, byte[] data, int dataStart)
{
if (data.Length == 1)
{
if (data[0] < 0x3F)
{
datagram[dataStart] = (byte)(datagram[dataStart] | data[0]);
}
else
{
datagram[dataStart + 1] = data[0];
}
}
else if (data.Length > 1)
{
if (data[0] < 0x3F)
{
datagram[dataStart] = (byte)(datagram[dataStart] | data[0]);
for (var i = 1; i < data.Length; i++)
{
datagram[dataStart + i] = data[i];
}
}
else
{
for (var i = 0; i < data.Length; i++)
{
datagram[dataStart + 1 + i] = data[i];
}
}
}
}
#endregion
#region Service Type
public enum SERVICE_TYPE
{
//0x0201
SEARCH_REQUEST,
//0x0202
SEARCH_RESPONSE,
//0x0203
DESCRIPTION_REQUEST,
//0x0204
DESCRIPTION_RESPONSE,
//0x0205
CONNECT_REQUEST,
//0x0206
CONNECT_RESPONSE,
//0x0207
CONNECTIONSTATE_REQUEST,
//0x0208
CONNECTIONSTATE_RESPONSE,
//0x0209
DISCONNECT_REQUEST,
//0x020A
DISCONNECT_RESPONSE,
//0x0310
DEVICE_CONFIGURATION_REQUEST,
//0x0311
DEVICE_CONFIGURATION_ACK,
//0x0420
TUNNELLING_REQUEST,
//0x0421
TUNNELLING_ACK,
//0x0530
ROUTING_INDICATION,
//0x0531
ROUTING_LOST_MESSAGE,
// UNKNOWN
UNKNOWN
}
public static SERVICE_TYPE GetServiceType(byte[] datagram)
{
switch (datagram[2])
{
case (0x02):
{
switch (datagram[3])
{
case (0x06):
return SERVICE_TYPE.CONNECT_RESPONSE;
case (0x09):
return SERVICE_TYPE.DISCONNECT_REQUEST;
case (0x0a):
return SERVICE_TYPE.DISCONNECT_RESPONSE;
case (0x08):
return SERVICE_TYPE.CONNECTIONSTATE_RESPONSE;
}
}
break;
case (0x04):
{
switch (datagram[3])
{
case (0x20):
return SERVICE_TYPE.TUNNELLING_REQUEST;
case (0x21):
return SERVICE_TYPE.TUNNELLING_ACK;
}
}
break;
}
return SERVICE_TYPE.UNKNOWN;
}
public static int GetChannelId(byte[] datagram)
{
if (datagram.Length > 6)
return datagram[6];
return -1;
}
#endregion
}
}