This commit is contained in:
Paul
2020-05-25 17:13:05 +02:00
parent b2bb8e2b5a
commit d3953d90ca
29 changed files with 461 additions and 657 deletions

View File

@@ -202,7 +202,7 @@ void RxService::flush_rx_queue() {
// start and initialize the Rx incoming buffer. Not currently used.
void RxService::start() {
// DEBUG_LOG(F("RxStart"));
// LOG_DEBUG(F("RxStart"));
}
// Rx loop, run as many times as you can
@@ -240,7 +240,7 @@ void RxService::add(uint8_t * data, uint8_t length) {
// validate the CRC
uint8_t crc = calculate_crc(data, length - 1);
if (data[length - 1] != crc) {
TRACE_LOG(F("Rx: %s %s(BAD, CRC %02X != %02X)%s"), Helpers::data_to_hex(data, length).c_str(), COLOR_RED, data[length - 1], crc, COLOR_RESET);
LOG_TRACE(F("Rx: %s %s(BAD, CRC %02X != %02X)%s"), Helpers::data_to_hex(data, length).c_str(), COLOR_RED, data[length - 1], crc, COLOR_RESET);
increment_telegram_error_count();
return;
}
@@ -253,9 +253,9 @@ void RxService::add(uint8_t * data, uint8_t length) {
ems_mask(data[0]);
}
// if we're in read only mode, just dump out to console
if (EMSESP::ems_read_only()) {
TRACE_LOG(F("Rx: %s"), Helpers::data_to_hex(data, length).c_str());
// if we're in "trace" and "raw" print out actual telegram
if (logger_.enabled(Level::TRACE) && EMSESP::trace_raw()) {
LOG_TRACE(F("Rx: %s"), Helpers::data_to_hex(data, length).c_str());
}
// src, dest and offset are always in fixed positions
@@ -307,7 +307,7 @@ void RxService::add(uint8_t * data, uint8_t length) {
}
// add to queue, with timestamp
DEBUG_LOG(F("New Rx [#%d] telegram added, length %d"), rx_telegram_id_, message_length);
LOG_DEBUG(F("New Rx [#%d] telegram, length %d"), rx_telegram_id_, message_length);
rx_telegrams_.emplace_back(rx_telegram_id_++, std::move(telegram));
}
@@ -325,7 +325,7 @@ void TxService::flush_tx_queue() {
// start and initialize Tx
void TxService::start() {
// DEBUG_LOG(F("TxStart()"));
// LOG_DEBUG(F("TxStart()"));
// grab the bus ID
Settings settings;
@@ -431,11 +431,12 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) {
length++; // add one since we want to now include the CRC
DEBUG_LOG(F("Sending %s Tx [#%d], telegram: %s"),
LOG_DEBUG(F("Sending %s Tx [#%d], telegram: %s"),
(telegram->operation == Telegram::Operation::TX_WRITE) ? F("write") : F("read"),
tx_telegram.id_,
telegram->to_string(telegram_raw, length).c_str());
// if we're watching an ID, then always show
if ((logger_.enabled(Level::TRACE))
&& ((telegram->src == EMSESP::trace_watch_id()) || (telegram->dest == EMSESP::trace_watch_id()) || (telegram->type_id == EMSESP::trace_watch_id()))) {
logger_.trace(F("Sending %s Tx [#%d], telegram: %s"),
@@ -448,7 +449,7 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) {
EMSUART_STATUS status = EMSuart::transmit(telegram_raw, length);
if (status != EMS_TX_STATUS_OK) {
logger_.err(F("Failed to transmit Tx via UART. Error: %s"), status == EMS_TX_WTD_TIMEOUT ? F("Timeout") : F("BRK"));
LOG_ERROR(F("Failed to transmit Tx via UART. Error: %s"), status == EMS_TX_WTD_TIMEOUT ? F("Timeout") : F("BRK"));
}
tx_waiting(true); // tx now in a wait state
@@ -465,12 +466,12 @@ void TxService::send_telegram(const uint8_t * data, const uint8_t length) {
}
telegram_raw[length] = calculate_crc(telegram_raw, length); // apppend CRC
DEBUG_LOG(F("Sending Raw telegram: %s (length=%d)"), Helpers::data_to_hex(telegram_raw, length).c_str(), length);
LOG_DEBUG(F("Sending Raw telegram: %s (length=%d)"), Helpers::data_to_hex(telegram_raw, length).c_str(), length);
// send the telegram to the UART Tx
EMSUART_STATUS status = EMSuart::transmit(telegram_raw, length);
if (status != EMS_TX_STATUS_OK) {
logger_.err(F("Failed to transmit Tx via UART. Error: %s"), status == EMS_TX_WTD_TIMEOUT ? F("Timeout") : F("BRK"));
LOG_ERROR(F("Failed to transmit Tx via UART. Error: %s"), status == EMS_TX_WTD_TIMEOUT ? F("Timeout") : F("BRK"));
}
}
@@ -478,7 +479,7 @@ void TxService::send_telegram(const uint8_t * data, const uint8_t length) {
// given some details like the destination, type, offset and message block
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) {
auto telegram = std::make_shared<Telegram>(operation, ems_bus_id(), dest, type_id, offset, message_data, message_length);
DEBUG_LOG(F("New Tx [#%d] telegram added, length %d"), tx_telegram_id_, message_length);
LOG_DEBUG(F("New Tx [#%d] telegram, length %d"), tx_telegram_id_, message_length);
// if the queue is full, make room but removing the last one
if (tx_telegrams_.size() >= maximum_tx_telegrams_) {
@@ -493,7 +494,7 @@ void TxService::add(const uint8_t operation, const uint8_t dest, const uint16_t
void TxService::add(uint8_t * data, const uint8_t length) {
uint8_t message_length = length - 4;
if (!message_length) {
logger_.err(F("Bad Tx telegram, too short (message length is %d)"), message_length);
LOG_ERROR(F("Bad Tx telegram, too short (message length is %d)"), message_length);
return;
}
@@ -511,13 +512,13 @@ void TxService::add(uint8_t * data, const uint8_t length) {
tx_telegrams_.pop_front();
}
DEBUG_LOG(F("New Tx [#%d] telegram added, length %d"), tx_telegram_id_, message_length);
LOG_DEBUG(F("New Tx [#%d] telegram, length %d"), tx_telegram_id_, message_length);
tx_telegrams_.emplace_back(tx_telegram_id_++, std::move(telegram));
}
// send a Tx telegram to request data from an EMS device
void TxService::read_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset) {
DEBUG_LOG(F("Tx read request to device 0x%02X for type ID 0x%02X"), dest, type_id);
LOG_DEBUG(F("Tx read request to device 0x%02X for type ID 0x%02X"), dest, type_id);
uint8_t message_data[1] = {EMS_MAX_TELEGRAM_LENGTH}; // request all data, 32 bytes
add(Telegram::Operation::TX_READ, dest, type_id, offset, message_data, 1);
@@ -582,7 +583,7 @@ uint8_t TxService::retry_tx() {
// and incoming Rx dest must be us (our ems_bus_id)
// for both src and dest we strip the MSB 8th bit
bool TxService::is_last_tx(const uint8_t src, const uint8_t dest) const {
// DEBUG_LOG(F("Comparing %02X=%02X , %02X,%02X"), (telegram_last_[1] & 0x7F), (src & 0x7F), (dest & 0x7F), ems_bus_id());
// LOG_DEBUG(F("Comparing %02X=%02X , %02X,%02X"), (telegram_last_[1] & 0x7F), (src & 0x7F), (dest & 0x7F), ems_bus_id());
return (((telegram_last_[1] & 0x7F) == (src & 0x7F)) && ((dest & 0x7F) == ems_bus_id()));
}
@@ -590,7 +591,7 @@ bool TxService::is_last_tx(const uint8_t src, const uint8_t dest) const {
void TxService::post_send_query() {
if (telegram_last_post_send_query_) {
uint8_t dest = (telegram_last_[1] & 0x7F);
DEBUG_LOG(F("Sending post validate read, type ID 0x%02X to dest 0x%02X"), telegram_last_post_send_query_, dest);
LOG_DEBUG(F("Sending post validate read, type ID 0x%02X to dest 0x%02X"), telegram_last_post_send_query_, dest);
read_request(telegram_last_post_send_query_, dest, 0); // no offset
}