forked from KONNEKTING/EnOceanGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnOcean.cpp
532 lines (474 loc) · 13.9 KB
/
EnOcean.cpp
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
* EnOcean.cpp
*
* Created on: 08.03.2018
* Author: Jens
*/
#include "EnOcean.h"
// EnOcean unique instance creation
EnOcean EnOcean::Eno;
EnOcean& enOcean = EnOcean::Eno;
uint8_t EnOcean::u8CRC8Table[256] = { 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31, 0x24,
0x23, 0x2a, 0x2d, 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d,
0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5, 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd, 0x90, 0x97, 0x9e,
0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd, 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc,
0xd5, 0xd2, 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea, 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2, 0x8f,
0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a, 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, 0x1f, 0x18, 0x11, 0x16,
0x03, 0x04, 0x0d, 0x0a, 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42, 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d,
0x7a, 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4, 0xf9, 0xfe,
0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4, 0x69, 0x6e, 0x67, 0x60, 0x75,
0x72, 0x7b, 0x7c, 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44, 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c,
0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34, 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, 0x76, 0x71, 0x78,
0x7f, 0x6A, 0x6d, 0x64, 0x63, 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d,
0x14, 0x13, 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8D, 0x84, 0x83, 0xde,
0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3 };
EnOcean::EnOcean()
{
u8CRC = 0x00;
u8RxByte = 0x00;
u8RetVal = 0x00;
u8State = GET_SYNC_STATE;
isInited = false;
lastComObj = 0;
lastParam = 0;
lastDevice = 0;
m_Pkt_st =
{ 0,0,0,0};
}
EnOcean::~EnOcean()
{
}
bool EnOcean::sendPacket(PACKET_SERIAL_TYPE *pPacket)
{
return ENOCEAN_OK == uart_sendPacket(pPacket);
}
void EnOcean::handleKnxEvents(byte index)
{
for (int i = 0; i < lastDevice; ++i)
{
#ifdef KDEBUG
Debug.println(F("trying Device %u"), i);
#endif
deviceRegistry[i]->handleKnxEvents(index);
}
}
void EnOcean::task()
{
#ifdef KDEBUG
static uint32_t lastTime = 0;
if (millis() - lastTime > 5000)
{
Debug.println(F("Alive, %u devices, runtime: %u"), lastDevice, lastTime);
lastTime = millis();
}
#endif
// EnOcean IN -> KNX OUT
u8RetVal = ENOCEAN_NO_RX_TEL;
u8RetVal = uart_getPacket(&m_Pkt_st, (uint16_t) DATBUF_SZ);
getEnOceanMSG(u8RetVal, &m_Pkt_st);
{
// in order to avoid performance issues, only one device per cycle
static uint8_t lastHandledDevice = 0;
if (lastHandledDevice == lastDevice)
lastHandledDevice = 0;
deviceRegistry[lastHandledDevice]->task();
lastHandledDevice++;
}
}
void EnOcean::configureDevice(IEnOceanDevice &device)
{
device.init(lastComObj, lastParam);
lastComObj += device.numberOfComObjects;
lastParam += device.numberOfParameters;
deviceRegistry[lastDevice] = &device;
lastDevice++;
}
void EnOcean::init()
{
if (isInited)
return;
//****************** Init Enocean Gateway Interface************************************
Serial.begin(57600); // Change to Serial wenn original Platine
m_Pkt_st.u8DataBuffer = &u8datBuf[0];
//****************** Read EnOcean Gateway Base ID ************************************
// communicates via Enocean UART channel
readBaseId(&lui8_BaseID_p[0]);
#ifdef KDEBUG
Debug.print("BASEID: ");
for (int i = 0; i < BASEID_BYTES; i++)
{
Debug.print("%X", lui8_BaseID_p[i]);
}
Debug.println("");
#endif
isInited = true;
}
void EnOcean::obtainSenderId(uint8_t* senderAdress)
{
if (!isInited)
init();
lastSenderIdOffset++;
senderAdress[0] = lui8_BaseID_p[0];
senderAdress[1] = lui8_BaseID_p[1];
senderAdress[2] = lui8_BaseID_p[2];
senderAdress[3] = lui8_BaseID_p[3] + lastSenderIdOffset;
#ifdef KDEBUG
Debug.print("SENDER_ID for offset ");
Debug.print("%u", lastSenderIdOffset);
Debug.print(": ");
for (int i = 0; i < BASEID_BYTES; i++)
{
Debug.print("%X", senderAdress[i]);
}
Debug.println("");
#endif
}
uint8_t* EnOcean::getBaseId()
{
return &lui8_BaseID_p[0];
}
void EnOcean::readBaseId(uint8_t* fui8_BaseID_p)
{
PACKET_SERIAL_TYPE lRdBaseIDPkt_st;
uint8_t lu8SndBuf = u8CO_RD_IDBASE;
lRdBaseIDPkt_st.u16DataLength = 0x0001;
lRdBaseIDPkt_st.u8OptionLength = 0x00;
lRdBaseIDPkt_st.u8Type = u8RORG_COMMON_COMMAND;
lRdBaseIDPkt_st.u8DataBuffer = &lu8SndBuf;
// Swap data length bytes to little endian
// uint8_t temp;
// temp = lRdBaseIDPkt_st.u16DataLength[0];
// lRdBaseIDPkt_st.u16DataLength[0] = lRdBaseIDPkt_st.u16DataLength[1];
// lRdBaseIDPkt_st.u16DataLength[1] = temp;
#ifdef KDEBUG
Debug.println("Sending telegram (read base ID).");
#endif
if ( ENOCEAN_OK == uart_sendPacket(&lRdBaseIDPkt_st))
{
u8RetVal = ENOCEAN_NO_RX_TEL;
#ifdef KDEBUG
Debug.println("Receiving telegram (read base ID).");
#endif
while (u8RetVal == ENOCEAN_NO_RX_TEL)
{
u8RetVal = uart_getPacket(&m_Pkt_st, (uint16_t) DATBUF_SZ);
}
switch (u8RetVal) {
case ENOCEAN_OK:
{
#ifdef KDEBUG
Debug.println("Data: ");
for (int i = 0; i < m_Pkt_st.u16DataLength + (uint16_t) m_Pkt_st.u8OptionLength; i++)
{
Debug.print("%X", m_Pkt_st.u8DataBuffer[i]);
Debug.print(" ");
}
Debug.println("");
#endif
switch (m_Pkt_st.u8Type) {
case u8RESPONSE:
{
#ifdef KDEBUG
Debug.println("Received Response.");
#endif
for (int i = 0; i < BASEID_BYTES; i++)
{
memcpy((void*) &(fui8_BaseID_p[i]), (void*) &(m_Pkt_st.u8DataBuffer[i + 1]), 1);
}
}
break;
default:
{
#ifdef KDEBUG
Debug.print("Wrong packet type. Expected response. Received: ");
Debug.println("%X", m_Pkt_st.u8Type);
#endif
}
}
}
break;
default:
{
#ifdef KDEBUG
Debug.println("Error receiving base ID");
#endif
}
}
}
}
void EnOcean::getEnOceanMSG(uint8_t u8RetVal, PACKET_SERIAL_TYPE* f_Pkt_st)
{
if (u8RetVal == ENOCEAN_OK)
{
#ifdef KDEBUG
Debug.println(F("Received Data: "));
for (int i = 0; i < f_Pkt_st->u16DataLength + (uint16_t) f_Pkt_st->u8OptionLength; i++)
{
Debug.print(F("%X"), f_Pkt_st->u8DataBuffer[i]);
Debug.print(F(" "));
}
Debug.println(F(""));
#endif
if (f_Pkt_st->u8Type == u8RADIO_ERP1)
{
#ifdef KDEBUG
if (f_Pkt_st->u8DataBuffer[0] == u8RORG_RPS)
Debug.println(F("Received RPS telegram."));
else
if (f_Pkt_st->u8DataBuffer[0] == u8RORG_VLD)
Debug.println(F("Received VLD telegram."));
#endif
bool packetWasHandled = false;
for (int i = 0; i < lastDevice; ++i)
{
if (deviceRegistry[i]->handleEnOceanPacket(f_Pkt_st))
{
packetWasHandled = true;
}
}
#ifdef KDEBUG
if (!packetWasHandled)
{
Debug.println(F("Data was not handled!"));
}
else
{
Debug.println(F("Data was handled :-)"));
}
#endif
}
}
}
#define proc_crc8(u8CRC, u8Data) (u8CRC8Table[u8CRC ^ u8Data])
uint8_t EnOcean::uart_getPacket(PACKET_SERIAL_TYPE *pPacket, uint16_t u16BufferLength)
{
//! UART received byte code
uint8_t u8RxByte;
//! Checksum calculation
static uint8_t u8CRC = 0;
//! Nr. of bytes received
static uint16_t u16Count = 0;
//! State machine counter
//static STATES_GET_PACKET u8State = GET_SYNC_STATE;
//! Timeout measurement
static uint8_t u8TickCount = 0;
// Byte buffer pointing at the paquet address
uint8_t *u8Raw = (uint8_t*) pPacket;
// Temporal variable
uint8_t i;
// Check for timeout between two bytes
// TODO
//if (((uint8)ug32SystemTimer) - u8TickCount > SER_INTERBYTE_TIME_OUT)
//{
// Reset state machine to init state
//u8State = GET_SYNC_STATE;
//}
// State machine goes on when a new byte is received
if (Serial.available() > 0)
{
while (Serial.readBytes(&u8RxByte, 1) == 1)
{
// Comment out for debugging
//SerialUSB.println(u8RxByte, HEX);
// Tick count of last received byte
// TODO
//u8TickCount = (uint8)ug32SystemTimer;
// State machine to load incoming packet bytes
switch (u8State) {
// Waiting for packet sync byte 0x55
case GET_SYNC_STATE:
if (u8RxByte == SER_SYNCH_CODE)
{
u8State = GET_HEADER_STATE;
u16Count = 0;
u8CRC = 0;
}
break;
// Read the header bytes
case GET_HEADER_STATE:
// Copy received data to buffer
u8Raw[u16Count++] = u8RxByte;
u8CRC = proc_crc8(u8CRC, u8RxByte);
// All header bytes received?
if (u16Count == SER_HEADER_NR_BYTES)
{
// SerialUSB.print("Received all header bytes.\n");
// SerialUSB.print("pPacket->u16DataLength: ");
// SerialUSB.println(pPacket->u16DataLength, HEX);
// SerialUSB.print("u8Raw[1]");
// SerialUSB.println(u8Raw[1], HEX);
// SerialUSB.print("u8Raw[2]");
// SerialUSB.println(u8Raw[2], HEX);
uint8_t temp;
temp = u8Raw[0];
u8Raw[0] = u8Raw[1];
u8Raw[1] = temp;
// SerialUSB.print("pPacket->u16DataLength: ");
// SerialUSB.println(pPacket->u16DataLength, HEX);
// SerialUSB.print("u8Raw[1]");
// SerialUSB.println(u8Raw[1], HEX);
// SerialUSB.print("u8Raw[2]");
// SerialUSB.println(u8Raw[2], HEX);
u8State = CHECK_CRC8H_STATE;
}
break;
// Check header checksum & try to resynchonise if error happened
case CHECK_CRC8H_STATE:
// Header CRC correct?
if (u8CRC != u8RxByte)
{
#ifdef KDEBUG
SerialUSB.print("CRC check failed.");
#endif
// No. Check if there is a sync byte (0x55) in the header
int a = -1;
for (i = 0; i < SER_HEADER_NR_BYTES; i++)
if (u8Raw[i] == SER_SYNCH_CODE)
{
// indicates the next position to the sync byte found
a = i + 1;
break;
};
if ((a == -1) && (u8RxByte != SER_SYNCH_CODE))
{
// Header and CRC8H does not contain the sync code
u8State = GET_SYNC_STATE;
break;
}
else
if ((a == -1) && (u8RxByte == SER_SYNCH_CODE))
{
// Header does not have sync code but CRC8H does.
// The sync code could be the beginning of a packet
u8State = GET_HEADER_STATE;
u16Count = 0;
u8CRC = 0;
break;
}
// Header has a sync byte. It could be a new telegram.
// Shift all bytes from the 0x55 code in the buffer.
// Recalculate CRC8 for those bytes
u8CRC = 0;
for (i = 0; i < (SER_HEADER_NR_BYTES - a); i++)
{
u8Raw[i] = u8Raw[a + i];
u8CRC = proc_crc8(u8CRC, u8Raw[i]);
}
u16Count = SER_HEADER_NR_BYTES - a;
// u16Count = i; // Seems also valid and more intuitive than u16Count -= a;
// Copy the just received byte to buffer
u8Raw[u16Count++] = u8RxByte;
u8CRC = proc_crc8(u8CRC, u8RxByte);
if (u16Count < SER_HEADER_NR_BYTES)
{
u8State = GET_HEADER_STATE;
break;
}
break;
}
// CRC8H correct. Length fields values valid?
if ((pPacket->u16DataLength + pPacket->u8OptionLength) == 0)
{
//No. Sync byte received?
if ((u8RxByte == SER_SYNCH_CODE))
{
//yes
u8State = GET_HEADER_STATE;
u16Count = 0;
u8CRC = 0;
break;
}
// Packet with correct CRC8H but wrong length fields.
u8State = GET_SYNC_STATE;
return ENOCEAN_OUT_OF_RANGE;
}
// Correct header CRC8. Go to the reception of data.
u8State = GET_DATA_STATE;
u16Count = 0;
u8CRC = 0;
break;
// Copy the information bytes
case GET_DATA_STATE:
// Copy byte in the packet buffer only if the received bytes have enough room
if (u16Count < u16BufferLength)
{
pPacket->u8DataBuffer[u16Count] = u8RxByte;
u8CRC = proc_crc8(u8CRC, u8RxByte);
}
// When all expected bytes received, go to calculate data checksum
if (++u16Count == (pPacket->u16DataLength + pPacket->u8OptionLength))
{
u8State = CHECK_CRC8D_STATE;
}
//SerialUSB.print(u16Count);
//SerialUSB.println(u16Count, DEC);
break;
// Check the data CRC8
case CHECK_CRC8D_STATE:
// In all cases the state returns to the first state: waiting for next sync byte
u8State = GET_SYNC_STATE;
// Received packet bigger than space to allocate bytes?
if (u16Count > u16BufferLength)
return ENOCEAN_OUT_OF_RANGE;
// Enough space to allocate packet. Equals last byte the calculated CRC8?
if (u8CRC == u8RxByte)
return ENOCEAN_OK; // Correct packet received
// False CRC8.
// If the received byte equals sync code, then it could be sync byte for next paquet.
if ((u8RxByte == SER_SYNCH_CODE))
{
u8State = GET_HEADER_STATE;
u16Count = 0;
u8CRC = 0;
}
return ENOCEAN_NOT_VALID_CHKSUM;
default:
// Yes. Go to the reception of info.
u8State = GET_SYNC_STATE;
break;
}
}
}
return (u8State == GET_SYNC_STATE) ? ENOCEAN_NO_RX_TEL : ENOCEAN_NEW_RX_BYTE;
}
uint8_t EnOcean::uart_sendPacket(PACKET_SERIAL_TYPE *pPacket)
{
uint16_t i;
uint8_t u8CRC;
uint8_t *u8Raw = (uint8_t*) pPacket;
// When both length fields are 0, then this telegram is not allowed.
if ((pPacket->u16DataLength || pPacket->u8OptionLength) == 0)
{
return ENOCEAN_OUT_OF_RANGE;
}
uint16_t lui16_PacketLength = pPacket->u16DataLength;
uint8_t temp;
temp = u8Raw[0];
u8Raw[0] = u8Raw[1];
u8Raw[1] = temp;
// Sync
while (Serial.write(0x55) != 1)
;
// Header
Serial.write((uint8_t*) pPacket, 4);
// Header CRC
u8CRC = 0;
u8CRC = proc_crc8(u8CRC, ((uint8_t* )pPacket)[0]);
u8CRC = proc_crc8(u8CRC, ((uint8_t* )pPacket)[1]);
u8CRC = proc_crc8(u8CRC, ((uint8_t* )pPacket)[2]);
u8CRC = proc_crc8(u8CRC, ((uint8_t* )pPacket)[3]);
while (Serial.write(u8CRC) != 1)
;
// Data
u8CRC = 0;
for (i = 0; i < lui16_PacketLength + pPacket->u8OptionLength; i++)
{
u8CRC = proc_crc8(u8CRC, pPacket->u8DataBuffer[i]);
while (Serial.write(pPacket->u8DataBuffer[i]) != 1)
;
}
// Data CRC
while (Serial.write(u8CRC) != 1)
;
return ENOCEAN_OK;
}