Branch data Line data Source code
1 : : #include "tx_state_machine.hpp"
2 : : #include "protocol_types.hpp"
3 : : #include "esp_log.h"
4 : :
5 : : static const char* TAG = "TxStateMachine";
6 : :
7 : 15 : TxStateMachine::TxStateMachine()
8 : 15 : : current_state_(TxState::IDLE)
9 : 15 : , pending_ack_(std::nullopt)
10 : 15 : , send_fail_count_(0)
11 : : {
12 : 15 : }
13 : :
14 : 2 : void TxStateMachine::reset()
15 : : {
16 : 2 : ESP_LOGV(TAG, "Resetting state machine");
17 : 2 : current_state_ = TxState::IDLE;
18 [ + + ]: 2 : pending_ack_.reset();
19 : 2 : send_fail_count_ = 0;
20 : 2 : }
21 : :
22 : 4 : void TxStateMachine::set_pending_ack(const PendingAck& pending_ack)
23 : : {
24 : 4 : pending_ack_ = pending_ack;
25 : 4 : }
26 : :
27 : 2 : TxState TxStateMachine::on_packet_sent(bool requires_ack)
28 : : {
29 : 2 : ESP_LOGV(TAG, "Packet sent notification");
30 : :
31 [ + + ]: 2 : if (requires_ack) {
32 : 1 : current_state_ = TxState::WAITING_FOR_ACK;
33 : : }
34 : : else {
35 : : // Fire-and-forget packets: no ACK needed, return to IDLE immediately
36 : 1 : current_state_ = TxState::IDLE;
37 : : }
38 : 2 : return current_state_;
39 : : }
40 : :
41 : 2 : TxState TxStateMachine::on_ack_received()
42 : : {
43 : 2 : ESP_LOGV(TAG, "Ack received notification");
44 : 2 : send_fail_count_ = 0;
45 [ + + ]: 2 : pending_ack_.reset();
46 : 2 : current_state_ = TxState::IDLE;
47 : 2 : return current_state_;
48 : : }
49 : :
50 : 2 : void TxStateMachine::on_link_alive()
51 : : {
52 : 2 : ESP_LOGV(TAG, "Link alive notification");
53 : 2 : send_fail_count_ = 0;
54 : 2 : }
55 : :
56 : 7 : TxState TxStateMachine::on_ack_timeout()
57 : : {
58 : 7 : current_state_ = TxState::RETRYING;
59 : 7 : return current_state_;
60 : : }
61 : :
62 : 11 : bool TxStateMachine::on_delivery_failure()
63 : : {
64 : 11 : ESP_LOGV(TAG, "Delivery failure notification, fail count: %d", send_fail_count_);
65 : :
66 : 11 : send_fail_count_++;
67 : :
68 [ + + ]: 11 : if (send_fail_count_ >= MAX_FAILURES) {
69 : : // Reset for next packet
70 : 2 : send_fail_count_ = 0;
71 [ + + ]: 2 : if (pending_ack_) {
72 : 1 : pending_ack_.reset();
73 : : }
74 : 2 : current_state_ = TxState::IDLE;
75 : 2 : return true;
76 : : }
77 : :
78 : 9 : current_state_ = TxState::RETRYING;
79 : 9 : return false;
80 : : }
81 : :
82 : 1 : void TxStateMachine::on_delivery_success()
83 : : {
84 : 1 : send_fail_count_ = 0;
85 : 1 : }
86 : :
87 : 2 : TxState TxStateMachine::on_max_retries()
88 : : {
89 [ + + ]: 2 : pending_ack_.reset();
90 : :
91 : 2 : current_state_ = TxState::IDLE;
92 : 2 : return current_state_;
93 : : }
|