-
Notifications
You must be signed in to change notification settings - Fork 9
/
ReliableStream.h
132 lines (113 loc) · 3.51 KB
/
ReliableStream.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef RELIABLE_STREAM_H
#define RELIABLE_STREAM_H
#include <stdint.h>
#include "Protocol.h"
#include "SPIMaster.h"
#include "Utils.h"
class ReliableStream {
public:
ReliableStream(SPIMaster* spiMaster) { this->spiMaster = spiMaster; }
bool send(void* data,
uint32_t totalPackets,
uint32_t syncCommand,
uint32_t startIndex = 0) {
uint32_t index = startIndex;
lastReceivedPacket = 0;
while (index < totalPackets) {
uint32_t packetToSend = ((uint32_t*)data)[index];
if (!sendPacket(packetToSend, &index, totalPackets, syncCommand,
startIndex))
return false;
}
return true;
}
bool sync(uint32_t command) {
lastReceivedPacket = 0;
uint32_t local = command + CMD_RPI_OFFSET;
uint32_t remote = command + CMD_GBA_OFFSET;
uint32_t confirmation;
while (true) {
bool isOnSync = (confirmation = spiMaster->exchange(local)) == remote;
if (isOnSync)
return true;
else {
if (IS_RESET(confirmation)) {
logReset("Reset! (sync)", local, remote);
return false;
}
lastReceivedPacket = confirmation;
}
}
}
bool finishSyncIfNeeded(uint32_t packet, uint32_t command) {
if (packet == command + CMD_GBA_OFFSET) {
spiMaster->exchange(command + CMD_RPI_OFFSET);
return true;
}
return false;
}
private:
SPIMaster* spiMaster;
uint32_t lastReceivedPacket = 0;
bool sendPacket(uint32_t packet,
uint32_t* index,
uint32_t totalPackets,
uint32_t syncCommand,
uint32_t startIndex) {
return *index == startIndex || *index % TRANSFER_SYNC_PERIOD == 0 ||
*index == totalPackets - 1
? reliablySend(packet, index, totalPackets, syncCommand)
: unreliablySend(packet, index);
}
bool reliablySend(uint32_t packet,
uint32_t* index,
uint32_t totalPackets,
uint32_t syncCommand) {
again:
uint32_t requestedIndex = spiMaster->exchange(packet);
if (finishSyncIfNeeded(requestedIndex, syncCommand))
goto again;
if (!IS_RESET(requestedIndex))
lastReceivedPacket = requestedIndex;
if (requestedIndex == CMD_RECOVERY + CMD_GBA_OFFSET) {
// (recovery command)
if (!sync(CMD_RECOVERY))
return false;
requestedIndex = spiMaster->exchange(0);
if (requestedIndex >= totalPackets) {
logReset("Reset! (recovery)", packet, *index);
return false;
}
*index = requestedIndex;
return true;
} else if (IS_RESET(requestedIndex)) {
// (reset command)
logReset("Reset! (stream - " + std::to_string(*index) + "/" +
std::to_string(totalPackets) + ")",
packet, *index);
return false;
} else if (requestedIndex == *index) {
// (on sync)
(*index)++;
return true;
} else {
// (probably garbage => ignore)
return true;
}
}
bool unreliablySend(uint32_t packet, uint32_t* index) {
spiMaster->send(packet);
(*index)++;
return true;
}
void logReset(std::string title, uint32_t sent, uint32_t expected) {
#ifdef PROFILE_VERBOSE
LOG(title);
LOG(" [sent, expected, actual]");
std::cout << " 0x" << std::hex << sent << "\n";
std::cout << " 0x" << std::hex << expected << "\n";
std::cout << " 0x" << std::hex << lastReceivedPacket << "\n";
#endif
}
};
#endif // RELIABLE_STREAM_H