-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacketQueue.cpp
201 lines (191 loc) · 6.75 KB
/
PacketQueue.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
/* PacketQueue
*/
#include <Arduino.h>
#include "PacketQueue.h"
PacketQueue::PacketQueue()
: _beg_index(0)
, _end_index(0)
, _size(0)
, _capacity(0)
, _dataBufferSize(PacketShared::DATA_BUFFER_SIZE)
{
// //preallocate memory for all the slots
// _slots = (PacketShared::Packet*) calloc(_capacity, sizeof(PacketShared::Packet));
// PacketShared::Packet *pkt_slot;
// for(size_t i=0; i < _capacity; i++){
// pkt_slot = &(_slots[i]); //pull out the slot by address
// //pkt_slot->data = (byte*) calloc(_dataBufferSize, sizeof(byte));
// pkt_slot->length = 0;
// pkt_slot->flags = 0x00;
// }
}
PacketShared::STATUS PacketQueue::begin(size_t capacity)
{
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println(F("# In PacketQueue::begin"));
#endif
//preallocate memory for all the slots
_slots = (PacketShared::Packet*) calloc(capacity, sizeof(PacketShared::Packet));
if (_slots == NULL){
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println("### Error failed to allocate memory for the queue!");
#endif
return PacketShared::ERROR_MEMALLOC_FAIL;
}
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.print("# \tallocated &_slots=");DEBUG_PORT.println((int) &_slots, HEX);
#endif
PacketShared::Packet *pkt_slot;
for(size_t i=0; i < _capacity; i++){
pkt_slot = &(_slots[i]); //pull out the slot by address
//pkt_slot->data = (byte*) calloc(_dataBufferSize, sizeof(byte));
pkt_slot->length = 0;
pkt_slot->timestamp = 0;
pkt_slot->flags = 0x00;
}
_capacity = capacity; //make sure to cache
return PacketShared::SUCCESS;
}
PacketShared::STATUS PacketQueue::end()
{
//PacketShared::Packet *pkt_slot;
//for(size_t i=0; i < _capacity; i++){
// pkt_slot = &(_slots[i]); //pull out the slot by address
// //free(pkt_slot->data);
//}
free(_slots);
return PacketShared::SUCCESS;
}
PacketShared::STATUS PacketQueue::reset()
{
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println(F("# In PacketQueue::reset"));
#endif
_size= 0;
_beg_index = 0;
_end_index = 0;
return PacketShared::SUCCESS;
}
PacketShared::STATUS PacketQueue::enqueue(PacketShared::Packet& pkt)
{
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println(F("# In PacketQueue::enqueue"));
#endif
if ((_size + 1) <= _capacity){
_put_at(_end_index, pkt);
//adjust the size and indices
_size++;
_end_index = (_end_index + 1) % _capacity; //wrap around if needed
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println(F("# (enqueue) after copy"));
PACKETQUEUE_DEBUG_PORT.print(F("# \t_end_index="));DEBUG_PORT.println(_end_index);
PACKETQUEUE_DEBUG_PORT.print(F("# \t_size="));DEBUG_PORT.println(_size);
#endif
return PacketShared::SUCCESS;
}
else{
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println(F("\t### Error: Queue Overflow"));
#endif
return PacketShared::ERROR_QUEUE_OVERFLOW;
}
}
PacketShared::STATUS PacketQueue::dequeue(PacketShared::Packet& pkt)
{
#ifdef PACKETQUEUE_DEBUG
//DEBUG_PORT.println(F("# In PacketQueue::dequeue"));
#endif
if (_size > 0){
_get_from(_beg_index, pkt);
//adjust the size and indices
_beg_index = (_beg_index + 1) % _capacity; //wrap around if needed
_size--;
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println(F("# (dequeue) after copy"));
PACKETQUEUE_DEBUG_PORT.print(F("# \t_beg_index="));DEBUG_PORT.println(_beg_index);
PACKETQUEUE_DEBUG_PORT.print(F("# \t_size="));DEBUG_PORT.println(_size);
#endif
return PacketShared::SUCCESS;
}
else{
#ifdef PACKETQUEUE_DEBUG
//DEBUG_PORT.println(F("### Error: Queue Underflow"));
#endif
pkt.length = 0; //set to safe value
return PacketShared::ERROR_QUEUE_UNDERFLOW;
}
}
PacketShared::STATUS PacketQueue::requeue(PacketShared::Packet& pkt)
{
//pushes packet onto the front of the queue
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println(F("# In PacketCommand::requeue"));
#endif
if ((_size + 1) <= _capacity){
//update the size and indices ahead of time
_size++;
_beg_index = (_beg_index == 0)? (_capacity - 1) : (_beg_index - 1); //wrap around if needed
_put_at(_beg_index, pkt);
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println(F("# (requeue) after copy"));
PACKETQUEUE_DEBUG_PORT.print(F("# \t_beg_index="));DEBUG_PORT.println(_beg_index);
PACKETQUEUE_DEBUG_PORT.print(F("# \t_size="));DEBUG_PORT.println(_size);
#endif
return PacketShared::SUCCESS;
}
else{
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println(F("### Error: Queue Overflow"));
#endif
return PacketShared::ERROR_QUEUE_OVERFLOW;
}
}
void PacketQueue::_put_at(size_t index, PacketShared::Packet& pkt)
{
PacketShared::Packet *pkt_slot = &(_slots[index]); //pull out the slot by address
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println(F("# In PacketQueue::_put_at"));
PACKETQUEUE_DEBUG_PORT.print(F("# \tindex="));DEBUG_PORT.println(index);
PACKETQUEUE_DEBUG_PORT.print(F("# \t&pkt="));DEBUG_PORT.println((unsigned int) &pkt,HEX);
PACKETQUEUE_DEBUG_PORT.print(F("# \tpkt_slot="));DEBUG_PORT.println((unsigned int) pkt_slot,HEX);
PACKETQUEUE_DEBUG_PORT.print(F("# \tcopying data: "));
#endif
//copy the packet object into the slot
for(size_t i=0; i < pkt.length; i++){
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.print(pkt.data[i], HEX);DEBUG_PORT.print(F(" "));
#endif
pkt_slot->data[i] = pkt.data[i];
}
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println();
#endif
pkt_slot->length = pkt.length; //update length field
pkt_slot->timestamp = pkt.timestamp;
pkt_slot->flags = pkt.flags;
}
void PacketQueue::_get_from(size_t index, PacketShared::Packet& pkt)
{
PacketShared::Packet *pkt_slot = &(_slots[index]); //pull out the slot by address
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println(F("# In PacketQueue::_get_from"));
PACKETQUEUE_DEBUG_PORT.print(F("# \tindex="));DEBUG_PORT.println(index);
PACKETQUEUE_DEBUG_PORT.print(F("# \t&pkt="));DEBUG_PORT.println((unsigned int) &pkt,HEX);
PACKETQUEUE_DEBUG_PORT.print(F("# \tpkt_slot="));DEBUG_PORT.println((unsigned int) pkt_slot,HEX);
PACKETQUEUE_DEBUG_PORT.print(F("# \tpkt_slot->length="));DEBUG_PORT.println(pkt_slot->length);
PACKETQUEUE_DEBUG_PORT.print(F("# \tcopying data: 0x"));
#endif
//copy the slot data to the current the referenced packet object
for(size_t i=0; (i < pkt_slot->length) && (i < _dataBufferSize); i++){
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.print(pkt_slot->data[i], HEX);DEBUG_PORT.print(F(" "));
#endif
pkt.data[i] = pkt_slot->data[i];
}
#ifdef PACKETQUEUE_DEBUG
PACKETQUEUE_DEBUG_PORT.println();
#endif
pkt.length = min(pkt_slot->length,_dataBufferSize);
pkt.timestamp = pkt_slot->timestamp;
pkt.flags = pkt_slot->flags;
}