Skip to content

Commit 4e9ee0f

Browse files
committed
Added timeliner
1 parent 59a6777 commit 4e9ee0f

5 files changed

Lines changed: 76 additions & 13 deletions

File tree

src/coreLib/TransmissionScheduler.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,13 @@ uint32_t TransmissionScheduler::add(uint8_t priority,
7272
std::shared_ptr<const TransmissionScheduler::Transmission> TransmissionScheduler::popNext(uint64_t time)
7373
{
7474
std::shared_ptr<Transmission> item = nullptr;
75+
7576
if (!mSchedule.empty())
7677
{
7778
if (time >= (*mSchedule.begin())->nextTxTimeUs)
7879
{
7980
item = (*mSchedule.begin());
8081
mSchedule.erase(mSchedule.begin());
81-
if (item->autoRepeatUs > 0)
82-
{
83-
// Determine how many intervals to add in cast this auto reload packet has overflowed
84-
uint32_t n = INT_DIVIDE_CEILING(time - item->nextTxTimeUs, item->autoRepeatUs);
85-
if (n == 0)
86-
{
87-
n = 1;
88-
}
89-
// Reinsert it back into the schedule with a new time
90-
item->nextTxTimeUs += (item->autoRepeatUs * n);
91-
add(item);
92-
}
9382
}
9483
else
9584
{
@@ -117,7 +106,21 @@ std::shared_ptr<const TransmissionScheduler::Transmission> TransmissionScheduler
117106
++iter;
118107
}
119108
}
109+
110+
if (item != nullptr && item->autoRepeatUs > 0)
111+
{
112+
// Determine how many intervals to add in cast this auto reload packet has overflowed
113+
uint32_t n = INT_DIVIDE_CEILING(time - item->nextTxTimeUs, item->autoRepeatUs);
114+
if (n == 0)
115+
{
116+
n = 1;
117+
}
118+
// Reinsert it back into the schedule with a new time
119+
item->nextTxTimeUs += (item->autoRepeatUs * n);
120+
add(item);
121+
}
120122
}
123+
121124
return item;
122125
}
123126

src/coreLib/TransmissionScheduler.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TransmissionScheduler
1818
const uint32_t autoRepeatUs;
1919
const uint32_t txDurationUs;
2020
uint64_t nextTxTimeUs;
21-
const std::shared_ptr<MaplePacket> packet;
21+
std::shared_ptr<const MaplePacket> packet;
2222

2323
Transmission(uint32_t transmissionId,
2424
bool priority,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "TransmissionTimeliner.hpp"
2+
#include <assert.h>
3+
4+
TransmissionTimeliner::TransmissionTimeliner(MapleBusInterface& bus, TransmissionScheduler& schedule):
5+
mBus(bus), mSchedule(schedule), mNextTx(nullptr)
6+
{}
7+
8+
uint32_t TransmissionTimeliner::recipientDisconnect(uint8_t recipientAddr)
9+
{
10+
return mSchedule.cancelByRecipient(recipientAddr);
11+
}
12+
13+
std::shared_ptr<const MaplePacket> TransmissionTimeliner::task(uint64_t time)
14+
{
15+
std::shared_ptr<const MaplePacket> pkt = nullptr;
16+
17+
if (mNextTx == nullptr)
18+
{
19+
mNextTx = mSchedule.popNext(time);
20+
}
21+
22+
if (mNextTx != nullptr)
23+
{
24+
pkt = mNextTx->packet;
25+
assert(pkt->isValid());
26+
if (mBus.write(*pkt, mNextTx->expectResponse, mNextTx->readTimeoutUs))
27+
{
28+
mNextTx = nullptr;
29+
}
30+
}
31+
32+
return pkt;
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include "MaplePacket.hpp"
4+
#include "MapleBusInterface.hpp"
5+
#include "TransmissionScheduler.hpp"
6+
7+
class TransmissionTimeliner
8+
{
9+
10+
public:
11+
TransmissionTimeliner(MapleBusInterface& bus, TransmissionScheduler& schedule);
12+
13+
uint32_t recipientDisconnect(uint8_t recipientAddr);
14+
15+
std::shared_ptr<const MaplePacket> task(uint64_t time);
16+
17+
protected:
18+
MapleBusInterface& mBus;
19+
TransmissionScheduler& mSchedule;
20+
std::shared_ptr<const TransmissionScheduler::Transmission> mNextTx;
21+
};

src/test/TransmissionSchedulerTests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ class TransmissionSchedulePopTestB : public TransmissionScheduleTest
388388
txTime = 2;
389389
expectedResponseNumPayloadWords = 3; // 267 us
390390
MaplePacket packet2(0x33, 0x02, 0x99887766);
391+
autoRepeatUs = 1111;
391392
scheduler.add(priority,
392393
txTime,
393394
packet2,
@@ -404,6 +405,11 @@ TEST_F(TransmissionSchedulePopTestB, popTestAutoReload2)
404405
std::shared_ptr<const TransmissionScheduler::Transmission> item = scheduler.popNext(2);
405406
ASSERT_NE(item, nullptr);
406407
EXPECT_EQ(item->transmissionId, 2);
408+
EXPECT_EQ(item->nextTxTimeUs, 1113);
409+
410+
// Transmission should auto reload
411+
const std::list<std::shared_ptr<TransmissionScheduler::Transmission>> schedule = scheduler.getSchedule();
412+
ASSERT_EQ(schedule.size(), 3);
407413
}
408414

409415
class TransmissionScheduleCancelTest : public TransmissionSchedulePopTestA

0 commit comments

Comments
 (0)