forked from gioblu/PJON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalFile.h
270 lines (235 loc) · 7.62 KB
/
LocalFile.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
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
#pragma once
#include "FileLockFunctions.h"
#include "PJON.h"
// The maximum number of messages in the content file
#ifndef LF_QUEUESIZE
#define LF_QUEUESIZE 20
#endif
// The name of the content file
#ifndef LF_FILENAME
#define LF_FILENAME "../PJONLocalFile.dat"
#endif
// Delay in ms between each check for new packets on the disk
#ifndef LF_POLLDELAY
#define LF_POLLDELAY 10
#endif
// Recommended receive time for this strategy, in microseconds
#ifndef LF_RECEIVE_TIME
#define LF_RECEIVE_TIME 0
#endif
#define PJON_LF_DEBUG
class LocalFile {
private:
int fn = -1;
/* The last record read from file, this is remembered to decide which
records have been read and which are unread. Note that record number 0
is never used, even when overflowing/wrapping around. */
uint16_t lastRecordIdRead = 0;
uint16_t last_send_result = PJON_ACK;
struct Record {
uint16_t length;
uint8_t message[PJON_PACKET_MAX_LENGTH];
Record() { memset(this, 0, sizeof(Record)); }
};
void doOpen(const bool create = false) {
if(fn != -1) return; // Already open
int mode = O_RDWR,
permissions = S_IREAD | S_IWRITE;
if(create) mode |= O_CREAT;
#ifdef _WIN32
mode |= O_RANDOM | O_BINARY;
_sopen_s(&fn, LF_FILENAME, mode, _SH_DENYNO, permissions);
#else
permissions |= S_IRGRP | S_IWGRP | S_IROTH;
// rw for owner+group, r for others
fn = open(LF_FILENAME, mode, permissions);
#endif
};
bool openContentFile() {
if(fn != -1) return true;
bool file_exists = CheckIfFile(LF_FILENAME);
if(!file_exists) {
// Create and initialize the file
uint16_t lastRecordId = 0, index[LF_QUEUESIZE];
memset(index, 0, LF_QUEUESIZE * sizeof(uint16_t));
doOpen(true);
if(fn != -1) {
lock();
write(fn, (const char*)&lastRecordId, sizeof(lastRecordId));
write(fn, (const char*)index, LF_QUEUESIZE*sizeof(uint16_t));
Record record;
for(uint8_t i=0; i<LF_QUEUESIZE; i++)
write(fn, (const char*)&record, sizeof(Record));
unlock();
}
}
if(fn == -1) doOpen();
return true;
};
void closeContentFile() {
if(fn != -1) { close(fn); fn = -1; }
};
bool readIndex(uint16_t &lastRecordId, uint16_t index[LF_QUEUESIZE]) {
lseek(fn, 0, SEEK_SET);
read(fn, (char*)&lastRecordId, sizeof(uint16_t));
read(fn, (char *)index, LF_QUEUESIZE*sizeof(uint16_t));
return true;
};
bool updateIndex(uint16_t recordId, uint8_t indexPos) {
lseek(fn, 0, SEEK_SET);
write(fn, (const char*)&recordId, sizeof(uint16_t));
lseek(fn, sizeof recordId + indexPos*sizeof(uint16_t), SEEK_SET);
write(fn, (const char*)&recordId, sizeof(uint16_t));
return true;
};
void lock() {
if(fn != -1) LockFileSection(fn, 0, 1, 1, 1, 0);
};
void unlock() {
if(fn != -1) LockFileSection(fn, 0, 1, 0, 1, 0);
};
bool writePacketToFile(const Record &record) {
bool success = false;
lock();
uint16_t lastRecordId, index[LF_QUEUESIZE];
if(readIndex(lastRecordId, index)) {
// Find a position then write the record to the file
uint8_t recordPos = findReusablePosition(lastRecordId, index);
if(recordPos != PJON_NOT_ASSIGNED) {
#ifdef PJON_LF_DEBUG
uint16_t diff = (uint16_t)(lastRecordId - index[recordPos]);
bool rollover = lastRecordId < index[recordPos];
if(
(!rollover && (diff+1 != LF_QUEUESIZE)) ||
(rollover && (diff != LF_QUEUESIZE))
) printf(
"countermismatch=%d/%d/%d/(%d) ",
recordPos,
index[recordPos],
lastRecordId,
diff
);
#endif
uint32_t filePos =
sizeof(uint16_t) +
LF_QUEUESIZE * sizeof(uint16_t) +
recordPos * sizeof(Record)
;
lseek(fn, filePos, SEEK_SET);
write(fn, (const char*)&record, sizeof(Record));
uint16_t nextRecordId = (uint16_t)(lastRecordId + 1);
if(nextRecordId == 0)
nextRecordId = 1; // Avoid record id 0, used to mark unused slot
success = updateIndex(nextRecordId, recordPos);
}
}
unlock();
return success;
};
bool readNextPacketFromFile(Record &record) {
bool success = false;
uint16_t lastRecordId, index[LF_QUEUESIZE];
lock();
if(readIndex(lastRecordId, index)) {
// Find a position then read the record from the file
if(lastRecordId != 0) {
uint8_t recordPos = findNextUnreadPosition(lastRecordId, index);
if(recordPos != PJON_NOT_ASSIGNED) {
uint32_t filePos =
sizeof(uint16_t) +
LF_QUEUESIZE * sizeof(uint16_t) +
recordPos * sizeof(Record)
;
lseek(fn, filePos, SEEK_SET);
read(fn, (char*)&record, sizeof(Record));
lastRecordIdRead = index[recordPos];
success = true;
}
}
}
unlock();
return success;
};
uint8_t findReusablePosition(
const uint16_t lastRecordId,
const uint16_t index[LF_QUEUESIZE]
) {
uint16_t maxDiff = 0;
uint8_t pos = PJON_NOT_ASSIGNED;
for(uint8_t i = 0; i < LF_QUEUESIZE; i++) {
if(index[i] == 0) return i; // Never used
uint16_t diff = (uint16_t)(lastRecordId - index[i]);
if(diff > maxDiff) {
maxDiff = diff;
pos = i;
}
}
return pos;
};
uint8_t findNextUnreadPosition(
const uint16_t lastRecordId,
const uint16_t index[LF_QUEUESIZE]
) {
uint8_t pos = PJON_NOT_ASSIGNED;
if(lastRecordIdRead == 0)
lastRecordIdRead = lastRecordId - LF_QUEUESIZE;
if(lastRecordId == lastRecordIdRead)
return pos; // Nothing new has arrived
uint16_t minDiff = 0xFFFF;
for(uint8_t i = 0; i < LF_QUEUESIZE; i++) {
if(index[i] == 0 || index[i] == lastRecordIdRead)
continue; // Never used or already read
uint16_t diff = (uint16_t)(index[i] - lastRecordIdRead);
if(diff < 0x8FFF && diff < minDiff) {
minDiff = diff;
pos = i;
}
}
return pos;
};
public:
~LocalFile() {
closeContentFile();
};
uint32_t back_off(uint8_t attempts) {
return 1000 * attempts;
};
bool begin(uint8_t did) {
return openContentFile();
};
void handle_collision() {
PJON_DELAY(10);
};
bool can_start(){
if(fn == -1) openContentFile();
return fn != -1;
};
uint8_t get_max_attempts() {
return 10;
};
static uint16_t get_receive_time() {
return LF_RECEIVE_TIME;
};
uint16_t receive_frame(uint8_t *data, uint16_t max_length) {
Record record;
if(readNextPacketFromFile(record)) {
uint16_t length =
record.length < max_length ? record.length : max_length;
memcpy(data, record.message, length);
return length;
// Relax polling to avoid stressing the disk and CPU too much
} else PJON_DELAY(LF_POLLDELAY);
return PJON_FAIL;
};
uint16_t receive_response() {
return last_send_result;
};
void send_response(uint8_t response) { };
void send_frame(uint8_t *data, uint16_t length) {
Record record;
memcpy(&record.message, data, length);
record.length = length;
bool ok = writePacketToFile(record);
last_send_result = ok ? PJON_ACK : PJON_FAIL;
};
};