22
22
namespace emsesp {
23
23
24
24
// CRC lookup table with poly 12 for faster checking
25
- const uint8_t ems_crc_table[] = {0x00 , 0x02 , 0x04 , 0x06 , 0x08 , 0x0A , 0x0C , 0x0E , 0x10 , 0x12 , 0x14 , 0x16 , 0x18 , 0x1A , 0x1C , 0x1E , 0x20 , 0x22 , 0x24 , 0x26 ,
26
- 0x28 , 0x2A , 0x2C , 0x2E , 0x30 , 0x32 , 0x34 , 0x36 , 0x38 , 0x3A , 0x3C , 0x3E , 0x40 , 0x42 , 0x44 , 0x46 , 0x48 , 0x4A , 0x4C , 0x4E ,
27
- 0x50 , 0x52 , 0x54 , 0x56 , 0x58 , 0x5A , 0x5C , 0x5E , 0x60 , 0x62 , 0x64 , 0x66 , 0x68 , 0x6A , 0x6C , 0x6E , 0x70 , 0x72 , 0x74 , 0x76 ,
28
- 0x78 , 0x7A , 0x7C , 0x7E , 0x80 , 0x82 , 0x84 , 0x86 , 0x88 , 0x8A , 0x8C , 0x8E , 0x90 , 0x92 , 0x94 , 0x96 , 0x98 , 0x9A , 0x9C , 0x9E ,
29
- 0xA0 , 0xA2 , 0xA4 , 0xA6 , 0xA8 , 0xAA , 0xAC , 0xAE , 0xB0 , 0xB2 , 0xB4 , 0xB6 , 0xB8 , 0xBA , 0xBC , 0xBE , 0xC0 , 0xC2 , 0xC4 , 0xC6 ,
30
- 0xC8 , 0xCA , 0xCC , 0xCE , 0xD0 , 0xD2 , 0xD4 , 0xD6 , 0xD8 , 0xDA , 0xDC , 0xDE , 0xE0 , 0xE2 , 0xE4 , 0xE6 , 0xE8 , 0xEA , 0xEC , 0xEE ,
31
- 0xF0 , 0xF2 , 0xF4 , 0xF6 , 0xF8 , 0xFA , 0xFC , 0xFE , 0x19 , 0x1B , 0x1D , 0x1F , 0x11 , 0x13 , 0x15 , 0x17 , 0x09 , 0x0B , 0x0D , 0x0F ,
32
- 0x01 , 0x03 , 0x05 , 0x07 , 0x39 , 0x3B , 0x3D , 0x3F , 0x31 , 0x33 , 0x35 , 0x37 , 0x29 , 0x2B , 0x2D , 0x2F , 0x21 , 0x23 , 0x25 , 0x27 ,
33
- 0x59 , 0x5B , 0x5D , 0x5F , 0x51 , 0x53 , 0x55 , 0x57 , 0x49 , 0x4B , 0x4D , 0x4F , 0x41 , 0x43 , 0x45 , 0x47 , 0x79 , 0x7B , 0x7D , 0x7F ,
34
- 0x71 , 0x73 , 0x75 , 0x77 , 0x69 , 0x6B , 0x6D , 0x6F , 0x61 , 0x63 , 0x65 , 0x67 , 0x99 , 0x9B , 0x9D , 0x9F , 0x91 , 0x93 , 0x95 , 0x97 ,
35
- 0x89 , 0x8B , 0x8D , 0x8F , 0x81 , 0x83 , 0x85 , 0x87 , 0xB9 , 0xBB , 0xBD , 0xBF , 0xB1 , 0xB3 , 0xB5 , 0xB7 , 0xA9 , 0xAB , 0xAD , 0xAF ,
36
- 0xA1 , 0xA3 , 0xA5 , 0xA7 , 0xD9 , 0xDB , 0xDD , 0xDF , 0xD1 , 0xD3 , 0xD5 , 0xD7 , 0xC9 , 0xCB , 0xCD , 0xCF , 0xC1 , 0xC3 , 0xC5 , 0xC7 ,
37
- 0xF9 , 0xFB , 0xFD , 0xFF , 0xF1 , 0xF3 , 0xF5 , 0xF7 , 0xE9 , 0xEB , 0xED , 0xEF , 0xE1 , 0xE3 , 0xE5 , 0xE7 };
25
+ const uint8_t ems_crc_table[] = {0x00 , 0x02 , 0x04 , 0x06 , 0x08 , 0x0A , 0x0C , 0x0E , 0x10 , 0x12 , 0x14 , 0x16 , 0x18 , 0x1A , 0x1C , 0x1E , 0x20 , 0x22 , 0x24 , 0x26 , 0x28 , 0x2A , 0x2C , 0x2E , 0x30 , 0x32 , 0x34 , 0x36 , 0x38 ,
26
+ 0x3A , 0x3C , 0x3E , 0x40 , 0x42 , 0x44 , 0x46 , 0x48 , 0x4A , 0x4C , 0x4E , 0x50 , 0x52 , 0x54 , 0x56 , 0x58 , 0x5A , 0x5C , 0x5E , 0x60 , 0x62 , 0x64 , 0x66 , 0x68 , 0x6A , 0x6C , 0x6E , 0x70 , 0x72 ,
27
+ 0x74 , 0x76 , 0x78 , 0x7A , 0x7C , 0x7E , 0x80 , 0x82 , 0x84 , 0x86 , 0x88 , 0x8A , 0x8C , 0x8E , 0x90 , 0x92 , 0x94 , 0x96 , 0x98 , 0x9A , 0x9C , 0x9E , 0xA0 , 0xA2 , 0xA4 , 0xA6 , 0xA8 , 0xAA , 0xAC ,
28
+ 0xAE , 0xB0 , 0xB2 , 0xB4 , 0xB6 , 0xB8 , 0xBA , 0xBC , 0xBE , 0xC0 , 0xC2 , 0xC4 , 0xC6 , 0xC8 , 0xCA , 0xCC , 0xCE , 0xD0 , 0xD2 , 0xD4 , 0xD6 , 0xD8 , 0xDA , 0xDC , 0xDE , 0xE0 , 0xE2 , 0xE4 , 0xE6 ,
29
+ 0xE8 , 0xEA , 0xEC , 0xEE , 0xF0 , 0xF2 , 0xF4 , 0xF6 , 0xF8 , 0xFA , 0xFC , 0xFE , 0x19 , 0x1B , 0x1D , 0x1F , 0x11 , 0x13 , 0x15 , 0x17 , 0x09 , 0x0B , 0x0D , 0x0F , 0x01 , 0x03 , 0x05 , 0x07 , 0x39 ,
30
+ 0x3B , 0x3D , 0x3F , 0x31 , 0x33 , 0x35 , 0x37 , 0x29 , 0x2B , 0x2D , 0x2F , 0x21 , 0x23 , 0x25 , 0x27 , 0x59 , 0x5B , 0x5D , 0x5F , 0x51 , 0x53 , 0x55 , 0x57 , 0x49 , 0x4B , 0x4D , 0x4F , 0x41 , 0x43 ,
31
+ 0x45 , 0x47 , 0x79 , 0x7B , 0x7D , 0x7F , 0x71 , 0x73 , 0x75 , 0x77 , 0x69 , 0x6B , 0x6D , 0x6F , 0x61 , 0x63 , 0x65 , 0x67 , 0x99 , 0x9B , 0x9D , 0x9F , 0x91 , 0x93 , 0x95 , 0x97 , 0x89 , 0x8B , 0x8D ,
32
+ 0x8F , 0x81 , 0x83 , 0x85 , 0x87 , 0xB9 , 0xBB , 0xBD , 0xBF , 0xB1 , 0xB3 , 0xB5 , 0xB7 , 0xA9 , 0xAB , 0xAD , 0xAF , 0xA1 , 0xA3 , 0xA5 , 0xA7 , 0xD9 , 0xDB , 0xDD , 0xDF , 0xD1 , 0xD3 , 0xD5 , 0xD7 ,
33
+ 0xC9 , 0xCB , 0xCD , 0xCF , 0xC1 , 0xC3 , 0xC5 , 0xC7 , 0xF9 , 0xFB , 0xFD , 0xFF , 0xF1 , 0xF3 , 0xF5 , 0xF7 , 0xE9 , 0xEB , 0xED , 0xEF , 0xE1 , 0xE3 , 0xE5 , 0xE7 };
38
34
39
35
uint32_t EMSbus::last_bus_activity_ = 0 ; // timestamp of last time a valid Rx came in
40
36
bool EMSbus::bus_connected_ = false ; // start assuming the bus hasn't been connected
@@ -59,13 +55,7 @@ uint8_t EMSbus::calculate_crc(const uint8_t * data, const uint8_t length) {
59
55
60
56
// creates a telegram object
61
57
// stores header in separate member objects and the rest in the message_data block
62
- Telegram::Telegram (const uint8_t operation,
63
- const uint8_t src,
64
- const uint8_t dest,
65
- const uint16_t type_id,
66
- const uint8_t offset,
67
- const uint8_t * data,
68
- const uint8_t message_length)
58
+ Telegram::Telegram (const uint8_t operation, const uint8_t src, const uint8_t dest, const uint16_t type_id, const uint8_t offset, const uint8_t * data, const uint8_t message_length)
69
59
: operation(operation)
70
60
, src(src)
71
61
, dest(dest)
@@ -202,8 +192,7 @@ void RxService::add(uint8_t * data, uint8_t length) {
202
192
// if we're watching and "raw" print out actual telegram as bytes to the console
203
193
if (EMSESP::watch () == EMSESP::Watch::WATCH_RAW) {
204
194
uint16_t trace_watch_id = EMSESP::watch_id ();
205
- if ((trace_watch_id == WATCH_ID_NONE) || (type_id == trace_watch_id)
206
- || ((trace_watch_id < 0x80 ) && ((src == trace_watch_id) || (dest == trace_watch_id)))) {
195
+ if ((trace_watch_id == WATCH_ID_NONE) || (type_id == trace_watch_id) || ((trace_watch_id < 0x80 ) && ((src == trace_watch_id) || (dest == trace_watch_id)))) {
207
196
LOG_NOTICE (F (" Rx: %s" ), Helpers::data_to_hex (data, length).c_str ());
208
197
} else if (EMSESP::trace_raw ()) {
209
198
LOG_TRACE (F (" Rx: %s" ), Helpers::data_to_hex (data, length).c_str ());
@@ -234,13 +223,6 @@ void RxService::add(uint8_t * data, uint8_t length) {
234
223
}
235
224
236
225
rx_telegrams_.emplace_back (rx_telegram_id_++, std::move (telegram)); // add to queue
237
-
238
- /*
239
- QueuedRxTelegram qrxt;
240
- qrxt.telegram_ = std::make_shared<Telegram>(operation, src, dest, type_id, offset, message_data, message_length);
241
- qrxt.id_ = rx_telegram_id_++;
242
- rx_telegrams_.push(qrxt);
243
- */
244
226
}
245
227
246
228
// start and initialize Tx
@@ -285,15 +267,11 @@ void TxService::send() {
285
267
}
286
268
delayed_send_ = 0 ;
287
269
288
- // auto telegram = tx_telegrams_.pop(); // get the Telegram, also removes from queue
289
-
290
270
// if we're in read-only mode (tx_mode 0) forget the Tx call
291
271
if (tx_mode () != 0 ) {
292
- // send_telegram(telegram);
293
272
send_telegram (tx_telegrams_.front ());
294
273
}
295
274
296
- // auto telegram = tx_telegrams_.pop();
297
275
tx_telegrams_.pop_front (); // remove the telegram from the queue
298
276
}
299
277
@@ -371,10 +349,7 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) {
371
349
372
350
length++; // add one since we want to now include the CRC
373
351
374
- LOG_DEBUG (F (" Sending %s Tx [#%d], telegram: %s" ),
375
- (telegram->operation == Telegram::Operation::TX_WRITE) ? F (" write" ) : F (" read" ),
376
- tx_telegram.id_ ,
377
- Helpers::data_to_hex (telegram_raw, length).c_str ());
352
+ LOG_DEBUG (F (" Sending %s Tx [#%d], telegram: %s" ), (telegram->operation == Telegram::Operation::TX_WRITE) ? F (" write" ) : F (" read" ), tx_telegram.id_ , Helpers::data_to_hex (telegram_raw, length).c_str ());
378
353
379
354
set_post_send_query (tx_telegram.validateid_ );
380
355
// send the telegram to the UART Tx
@@ -414,14 +389,7 @@ void TxService::send_telegram(const uint8_t * data, const uint8_t length) {
414
389
}
415
390
*/
416
391
417
- void TxService::add (const uint8_t operation,
418
- const uint8_t dest,
419
- const uint16_t type_id,
420
- const uint8_t offset,
421
- uint8_t * message_data,
422
- const uint8_t message_length,
423
- const uint16_t validateid,
424
- const bool front) {
392
+ void TxService::add (const uint8_t operation, const uint8_t dest, const uint16_t type_id, const uint8_t offset, uint8_t * message_data, const uint8_t message_length, const uint16_t validateid, const bool front) {
425
393
auto telegram = std::make_shared<Telegram>(operation, ems_bus_id (), dest, type_id, offset, message_data, message_length);
426
394
427
395
#ifdef EMSESP_DEBUG
@@ -490,7 +458,7 @@ void TxService::add(uint8_t operation, const uint8_t * data, const uint8_t lengt
490
458
if (dest & 0x80 ) {
491
459
operation = Telegram::Operation::TX_READ;
492
460
} else {
493
- operation = Telegram::Operation::TX_WRITE;
461
+ operation = Telegram::Operation::TX_WRITE;
494
462
validate_id = type_id;
495
463
}
496
464
EMSESP::set_read_id (type_id);
@@ -507,13 +475,6 @@ void TxService::add(uint8_t operation, const uint8_t * data, const uint8_t lengt
507
475
LOG_DEBUG (F (" [DEBUG] New Tx [#%d] telegram, length %d" ), tx_telegram_id_, message_length);
508
476
#endif
509
477
510
- /*
511
- QueuedTxTelegram qtxt;
512
- qtxt.id_ = tx_telegram_id_++;
513
- qtxt.retry_ = false;
514
- qtxt.telegram_ = std::make_shared<Telegram>(operation, ems_bus_id(), dest, type_id, offset, message_data, message_length);
515
- */
516
-
517
478
if (front) {
518
479
// tx_telegrams_.push_front(qtxt); // add to front of queue
519
480
tx_telegrams_.emplace_front (tx_telegram_id_++, std::move (telegram), false , validate_id); // add to front of queue
@@ -580,9 +541,7 @@ void TxService::retry_tx(const uint8_t operation, const uint8_t * data, const ui
580
541
reset_retry_count (); // give up
581
542
increment_telegram_fail_count (); // another Tx fail
582
543
583
- LOG_ERROR (F (" Last Tx %s operation failed after %d retries. Ignoring request." ),
584
- (operation == Telegram::Operation::TX_WRITE) ? F (" Write" ) : F (" Read" ),
585
- MAXIMUM_TX_RETRIES);
544
+ LOG_ERROR (F (" Last Tx %s operation failed after %d retries. Ignoring request." ), (operation == Telegram::Operation::TX_WRITE) ? F (" Write" ) : F (" Read" ), MAXIMUM_TX_RETRIES);
586
545
return ;
587
546
}
588
547
@@ -600,14 +559,6 @@ void TxService::retry_tx(const uint8_t operation, const uint8_t * data, const ui
600
559
}
601
560
602
561
tx_telegrams_.emplace_front (tx_telegram_id_++, std::move (telegram_last_), true , get_post_send_query ());
603
-
604
- /*
605
- QueuedTxTelegram qtxt;
606
- qtxt.id_ = tx_telegram_id_++;
607
- qtxt.retry_ = true; // this time it is a retry
608
- qtxt.telegram_ = telegram_last_;
609
- tx_telegrams_.push_front(qtxt); // add to front of queue
610
- */
611
562
}
612
563
613
564
uint16_t TxService::read_next_tx () {
@@ -635,7 +586,7 @@ uint16_t TxService::post_send_query() {
635
586
uint8_t dest = (this ->telegram_last_ ->dest & 0x7F );
636
587
// when set a value with large offset before and validate on same type, we have to add offset 0, 26, 52, ...
637
588
uint8_t offset = (this ->telegram_last_ ->type_id == post_typeid) ? ((this ->telegram_last_ ->offset / 26 ) * 26 ) : 0 ;
638
- uint8_t message_data[1 ] = {EMS_MAX_TELEGRAM_LENGTH}; // request all data, 32 bytes
589
+ uint8_t message_data[1 ] = {EMS_MAX_TELEGRAM_LENGTH}; // request all data, 32 bytes
639
590
this ->add (Telegram::Operation::TX_READ, dest, post_typeid, offset, message_data, 1 , 0 , true ); // add to top/front of queue
640
591
// read_request(telegram_last_post_send_query_, dest, 0); // no offset
641
592
LOG_DEBUG (F (" Sending post validate read, type ID 0x%02X to dest 0x%02X" ), post_typeid, dest);
0 commit comments