-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTargetUartConnection.cpp
More file actions
70 lines (61 loc) · 2.08 KB
/
Copy pathTargetUartConnection.cpp
File metadata and controls
70 lines (61 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "postmaster/programmer/TargetUartConnection.hpp"
namespace application
{
TargetUartConnection::TargetUartConnection(infra::ByteRange buffer, hal::SerialCommunication& serial)
: queue{ buffer, [this]()
{
ReceivedSerialData();
} }
, serial(serial)
{
serial.ReceiveData([this](infra::ConstByteRange data)
{
data.shrink_from_back_to(queue.Capacity() - queue.Size());
queue.AddFromInterrupt(data);
});
}
TargetUartConnection::~TargetUartConnection()
{
serial.ReceiveData(nullptr);
}
void TargetUartConnection::SendStreamAvailable(infra::SharedPtr<infra::StreamWriter>&& streamWriter)
{
streamRequested = false;
infra::DataOutputStream::WithErrorPolicy stream(*streamWriter);
while (stream.Available() != 0 && !queue.Empty())
{
auto data = queue.ContiguousRange(0);
queue.Consume(data.size());
data.shrink_from_back_to(stream.Available());
stream << data;
}
streamWriter = nullptr;
ReceivedSerialData();
}
void TargetUartConnection::DataReceived()
{
if (receiveStream == nullptr && IsAttached())
{
receiveStream = Subject().ReceiveStream();
if (!receiveStream->Empty())
serial.SendData(receiveStream->ExtractContiguousRange(std::numeric_limits<std::size_t>::max()), [this]()
{
Subject().AckReceived();
infra::WeakPtr self = Subject().ObserverPtr();
receiveStream = nullptr;
if (self.lock())
DataReceived();
});
else
receiveStream = nullptr;
}
}
void TargetUartConnection::ReceivedSerialData()
{
if (!queue.Empty() && !streamRequested)
{
streamRequested = true;
Subject().RequestSendStream(Subject().MaxSendStreamSize());
}
}
}