-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFMessageControl.cpp
303 lines (259 loc) · 7.81 KB
/
RFMessageControl.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "RFMessageControl.h"
#include "Arduino.h"
/*
* TODO:
* centralize properties for logging
*
*/
AbstractRFMessageControl::AbstractRFMessageControl(BaseSenderReceiver * transceiver)
{
m_transceiver = transceiver;
m_lastDecrementRun = 0;
m_lastMessageId = 0;
m_sendingSorter.init(m_sending);
m_receivedSorter.init(m_received);
notifyDiscartedItem = NULL;
_max_send_time = -1;
_min_send_time = -1;
_listen_grace = MIN_SEND_TIMEOUT;
_valid_received_message_count = 0;
_invalid_received_message_count = 0;
}
bool AbstractRFMessageControl::sendMessage(uint8_t toChannelID, uint8_t * message, uint8_t messageLength){
MessageQueueItem * item = NULL;
uint8_t channel = getChannel(toChannelID);
bool succes = m_sendingSorter.getUnusedItem(&item);
if (succes)
{
item->init(channel, m_lastMessageId++, message, messageLength);
}
return succes;
}
void AbstractRFMessageControl::acknowledge(MessageQueueItem * acknowledgement){
MessageQueueItem * item = NULL;
if(findMessage(acknowledgement->getChannel(), acknowledgement->getMessageId(), -1, &item, m_sending)){
item->destroy();
}
}
bool MessageChannelEquals(MessageQueueItem * item, uint8_t channel)
{
return item->getChannel() == channel;
}
bool MessageMessageIdEquals(MessageQueueItem * item, uint8_t messageId)
{
return item->getMessageId() == messageId;
}
bool MessageRetriesLeftEquals(MessageQueueItem * item, uint8_t retriesLeft)
{
return item->getRetriesLeft() == retriesLeft;
}
bool AbstractRFMessageControl::findMessage(int channel, int messageId, int retriesLeft, MessageQueueItem ** item, MessageQueueItem * queue)
{
MessageParameterEquals testFunctions[3];
uint8_t values[3];
int testCount = 0;
if(channel != -1){
values[testCount] = (uint8_t)channel;
testFunctions[testCount] = MessageChannelEquals;
testCount++;
}
if(messageId != -1){
values[testCount] = (uint8_t)messageId;
testFunctions[testCount] = MessageMessageIdEquals;
testCount++;
}
if(retriesLeft != -1){
values[testCount] = (uint8_t)retriesLeft;
testFunctions[testCount] = MessageRetriesLeftEquals;
testCount++;
}
MessageQueueItem * candidate;
int i, f;
bool found;
for (i=0; i < MAXMESSAGECOUNT; i++)
{
candidate = &queue[i];
found = true;
for(f=0; f < testCount; f++){
found = testFunctions[f](candidate, values[f]);
if (!found){
break;
}
}
if(found){
*item = candidate;
break;
}
}
return found;
}
/*
* create a map of booleans and flag if a channel has been seen
* send only one message per channel in each run
*/
void AbstractRFMessageControl::sendRemainingMessages(){
unsigned long int now = millis();
unsigned long int delta = now - m_lastSendAt;
if(delta < MIN_SEND_TIMEOUT)
return;
MessageQueueItem * item;
MessageQueueItem ** sortedQueue = m_sendingSorter.reorder();
bool channelSeen[MAXMESSAGECOUNT] = {false};
bool reorder_needed = false;
int i, j;
for (i=0; i < MAXMESSAGECOUNT; i++)
{
item = sortedQueue[i];
if(!channelSeen[i]){
/* update the rest of the channelSeen map */
for(j=i; j < MAXMESSAGECOUNT; j++)
{
if(sortedQueue[j]->getChannel() == item->getChannel())
channelSeen[j] = true;
}
if (item->getRetriesLeft() > 0){
item->decrementRetriesLeft();
if(item->isDestroyed() && notifyDiscartedItem != NULL){
reorder_needed = true;
notifyDiscartedItem(item);
}else{
send(item);
logSendTime(now, millis());
m_lastSendAt = now;
break;
}
}
}
}
if(reorder_needed)
m_sendingSorter.reorder();
}
void AbstractRFMessageControl::logSendTime(long unsigned int start, long unsigned int end)
{
int delta = end - start;
if( _max_send_time == -1 || delta > _max_send_time)
_max_send_time = delta;
if( _min_send_time == -1 || delta <= _min_send_time)
_min_send_time = delta;
}
void AbstractRFMessageControl::reportSendTime( int &min, int &max)
{
min = _min_send_time;
max = _max_send_time;
}
/* sent full buffer for now
* optimize to send only relevant data later
*/
bool AbstractRFMessageControl::send(MessageQueueItem * item){
//uint8_t length = sizeof(message_data_t);
uint8_t length = item->getLength() + sizeof(message_header_t);
return m_transceiver->send(item->getBuffer(),length);
}
void AbstractRFMessageControl::sendAcknowledge(MessageQueueItem * existing){
//existing->transition(messageType);
send(existing);
}
/*
* channel = sender|receiver
* 8Bit = 4Bit|4Bit
*/
bool AbstractRFMessageControl::isRequest(uint8_t channel)
{
uint8_t receiver = channel & 0xF;
return receiver == m_ourChannelID;
}
bool AbstractRFMessageControl::isReply(uint8_t channel)
{
uint8_t sender = (channel >> 4) & 0xF;
return sender == m_ourChannelID;
}
void AbstractRFMessageControl::handleIncommingMessages(){
uint8_t buffer[sizeof(message_data_t)] = {0};
MessageQueueItem received = MessageQueueItem();
MessageQueueItem * existing = NULL;
uint8_t length = sizeof(message_data_t);
while (m_transceiver->have_message()){
bool valid_message = m_transceiver->get_message(buffer, &length);
if(valid_message){
_valid_received_message_count++;
received.init(buffer);
uint8_t messageId = received.getMessageId();
uint8_t channel = received.getChannel();
if(isReply(channel)){
acknowledge(&received);
// received can contain a command that needs to be handled
handleIncommingReply(&received);
} else if(isRequest(channel)){
/* lookup if we already have that message */
bool found = findMessage(channel, messageId, -1, &existing, m_received);
if(!found){
found = m_receivedSorter.getUnusedItem(&existing);
if(found){
existing->init(buffer);
existing->setRetriesLeft(MAXRETRIES + 1);
handleIncommingMessage(existing);
}
}
if(found){
sendAcknowledge(existing);
}
_listen_grace = MIN_SEND_TIMEOUT;
} else {
_invalid_received_message_count++;
// listen longer and keep mouth shut
_listen_grace = MIN_SEND_TIMEOUT + EXTRA_LISTEN_GRACE;
}
}
}
}
void AbstractRFMessageControl::handleIncommingMessage(MessageQueueItem* received){}
void AbstractRFMessageControl::handleIncommingReply(MessageQueueItem* received){}
void AbstractRFMessageControl::decrementReceivedMessagesRetriesLeft()
{
MessageQueueItem * item;
int i;
bool reorder_needed = false;
for (i=0; i < MAXMESSAGECOUNT; i++)
{
item = &m_received[i];
if (item->getRetriesLeft() > 0 && item->decrementRetriesLeft() == 0)
reorder_needed = true;
}
if(reorder_needed)
m_receivedSorter.reorder();
}
void AbstractRFMessageControl::update(){
unsigned long now = millis();
if (now - m_lastDecrementRun > _listen_grace){
decrementReceivedMessagesRetriesLeft();
m_lastDecrementRun = now;
}
sendRemainingMessages();
handleIncommingMessages();
}
uint8_t AbstractRFMessageControl::getChannelID(){
return m_ourChannelID;
}
/*
* channel = sender|receiver
* 8Bit = 4Bit|4Bit
*/
uint8_t AbstractRFMessageControl::getChannel(uint8_t toChannelID){
return ((m_ourChannelID & 0xF) << 4) | (toChannelID & 0xF);
}
void AbstractRFMessageControl::reportMessageCount(int& failures, int& succeses)
{
failures = _invalid_received_message_count;
succeses = _valid_received_message_count;
}
void AbstractRFMessageControl::setChannelID(uint8_t channelId){
m_ourChannelID = channelId;
}
void RFMessageControl::setMessageReceivedEventHandler(MessageReceivedEventHandler eh){
callback = eh;
}
void RFMessageControl::handleIncommingMessage(MessageQueueItem * item){
if(callback != NULL && !item->isDestroyed()){
callback(*item);
}
}