Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
delay post send query if different type_id
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Jan 24, 2021
1 parent 88786ec commit cdf6aae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/telegram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,18 @@ void TxService::send() {
return;
}

// if there's nothing in the queue to transmit, send back a poll and quit
if (tx_telegrams_.empty()) {
// if there's nothing in the queue to transmit or sending should be delayed, send back a poll and quit
if (tx_telegrams_.empty() || (delayed_send_ && uuid::get_uptime() < (delayed_send_ + 2000))) {
send_poll();
return;
}
delayed_send_ = 0;

// if we're in read-only mode (tx_mode 0) forget the Tx call
if (tx_mode() != 0) {
if (tx_mode()) {
send_telegram(tx_telegrams_.front());
}


tx_telegrams_.pop_front(); // remove the telegram from the queue
}

Expand Down Expand Up @@ -379,6 +379,7 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) {
tx_state(telegram->operation); // tx now in a wait state
}

/*
// send an array of bytes as a telegram
// we need to calculate the CRC and append it before sending
// this function is fire-and-forget. there are no checks or post-send validations
Expand All @@ -400,6 +401,7 @@ void TxService::send_telegram(const uint8_t * data, const uint8_t length) {
increment_telegram_fail_count(); // another Tx fail
}
}
*/

// builds a Tx telegram and adds to queue
// given some details like the destination, type, offset and message block
Expand All @@ -416,13 +418,13 @@ void TxService::add(const uint8_t operation,
LOG_DEBUG(F("[DEBUG] New Tx [#%d] telegram, length %d"), tx_telegram_id_, message_length);
#endif

// if the queue is full, make room but removing the last one
// if the queue is full, make room but removing the oldest one
if (tx_telegrams_.size() >= MAX_TX_TELEGRAMS) {
tx_telegrams_.pop_front();
}

if (front) {
tx_telegrams_.emplace_front(tx_telegram_id_++, std::move(telegram), false); // add to back of queue
if (front || (operation == Telegram::Operation::TX_WRITE)) {
tx_telegrams_.emplace_front(tx_telegram_id_++, std::move(telegram), false); // add to front of queue
} else {
tx_telegrams_.emplace_back(tx_telegram_id_++, std::move(telegram), false); // add to back of queue
}
Expand Down Expand Up @@ -607,9 +609,10 @@ uint16_t TxService::post_send_query() {
uint8_t offset = (this->telegram_last_->type_id == post_typeid) ? ((this->telegram_last_->offset / 26) * 26) : 0;
uint8_t message_data[1] = {EMS_MAX_TELEGRAM_LENGTH}; // request all data, 32 bytes
this->add(Telegram::Operation::TX_READ, dest, post_typeid, offset, message_data, 1, true);
// read_request(telegram_last_post_send_query_, dest, 0); // no offset
LOG_DEBUG(F("Sending post validate read, type ID 0x%02X to dest 0x%02X"), post_typeid, dest);
set_post_send_query(0); // reset
// delay the request if we have a different type_id for post_send_query
delayed_send_ = (this->telegram_last_->type_id == post_typeid) ? 0 : uuid::get_uptime();
}

return post_typeid;
Expand Down
4 changes: 3 additions & 1 deletion src/telegram.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,10 @@ class TxService : public EMSbus {

uint8_t tx_telegram_id_ = 0; // queue counter

uint32_t delayed_send_ = 0;

void send_telegram(const QueuedTxTelegram & tx_telegram);
void send_telegram(const uint8_t * data, const uint8_t length);
// void send_telegram(const uint8_t * data, const uint8_t length);
};

} // namespace emsesp
Expand Down

0 comments on commit cdf6aae

Please sign in to comment.