Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/traction_modem/Link.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ TEST_F(LinkTest, LinkUpMaxDelay)
Defs::append_uint8(&b->data()->payload, 0);
Defs::append_uint8(&b->data()->payload, 0);
Defs::append_crc(&b->data()->payload);
mRxFlow_.trigger_packet_rx_callback();
static_cast<PacketFlowInterface*>(&linkManager_)->send(b);
wait_for_main_executor();
testing::Mock::VerifyAndClearExpectations(&mTxFlow_);
Expand Down
23 changes: 17 additions & 6 deletions src/traction_modem/Link.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ public:
, useDefaultBaud_(use_default_baud)
{
link_->register_link_status(this);
link_->get_rx_iface()->register_packet_rx_callback(
std::bind(&LinkManager::packet_rx_callback, this));
}

#if defined(GTEST)
Expand All @@ -227,6 +229,19 @@ private:
/// Alias for short response timeout used during link establishment.
static constexpr long long RESP_TIMEOUT = Defs::RESP_TIMEOUT_SHORT;

// Callback for when any packet is received.
void packet_rx_callback()
{
if (link_->is_link_up())
{
// Note: link_is_down() is technically the "next" state, if
// the link fails (timer_ expires without pong).
HASSERT(is_state(STATE(link_is_down)));
// Everything is good, don't trigger an early timer_ wakeup.
timer_.restart();
}
}

/// Called when the link is started.
void on_link_start() override
{
Expand Down Expand Up @@ -338,14 +353,10 @@ private:
switch(buf->data()->command())
{
case Defs::RESP_PING:
/// @todo This is a good place to extract the revision info.
if (link_->is_link_up())
{
// Note: link_is_down() is technically the "next" state, if
// the link fails (timer_ expires without pong).
HASSERT(is_state(STATE(link_is_down)));
// Everything is good, don't trigger an early timer_ wakeup.
timer_.restart();
return;
return;
}
break;
case Defs::RESP_BAUD_RATE_QUERY:
Expand Down
13 changes: 13 additions & 0 deletions src/traction_modem/RxFlow.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,18 @@ public:
{
return resyncCount_;
}

/// Register for a callback when any packet is received.
virtual void register_packet_rx_callback(std::function<void()> callback)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this probably should not be virtual, since the only meaningful implementation is inline here.

{
pktRxCallback_ = callback;
}

protected:
/// Track the number of times that we try to resync.
unsigned resyncCount_{0};
/// Callback for when any packet is received.
std::function<void()> pktRxCallback_;
};

/// Object responsible for reading in a stream of bytes over the modem
Expand Down Expand Up @@ -323,6 +332,10 @@ private:

auto *b = dispatcher_.alloc();
b->data()->payload = std::move(payload_);
if (pktRxCallback_)
{
pktRxCallback_();
}
dispatcher_.send(b);

if (payload_tmp.size())
Expand Down
10 changes: 10 additions & 0 deletions src/traction_modem/modem_test_helper.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public:
void(PacketFlowInterface*, Message::id_type, Message::id_type));
MOCK_METHOD1(unregister_handler_all, void(PacketFlowInterface*));
MOCK_METHOD1(register_fallback_handler, void(PacketFlowInterface*));

void trigger_packet_rx_callback()
{
if (pktRxCallback_)
{
pktRxCallback_();
}
}
};

class MyMockRxFlow : public MockRxFlow
Expand Down Expand Up @@ -104,6 +112,7 @@ protected:
Defs::append_uint8(&b->data()->payload, 0);
Defs::append_uint8(&b->data()->payload, 0);
Defs::append_crc(&b->data()->payload);
mRxFlow_.trigger_packet_rx_callback();
static_cast<PacketFlowInterface*>(&linkManager_)->send(b);
}
}
Expand Down Expand Up @@ -147,6 +156,7 @@ protected:
Defs::append_uint8(&b->data()->payload, 0);
Defs::append_uint8(&b->data()->payload, 0);
Defs::append_crc(&b->data()->payload);
mRxFlow_.trigger_packet_rx_callback();
static_cast<PacketFlowInterface*>(&linkManager_)->send(b);
wait_for_main_executor();
EXPECT_TRUE(link_.is_link_up());
Expand Down