forked from ClusterDuck-Protocol/ClusterDuck-Protocol
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDuckRadio.cpp
385 lines (325 loc) · 12.7 KB
/
DuckRadio.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "include/DuckRadio.h"
#ifdef CDPCFG_RADIO_SX126X
#define DUCK_RADIO_IRQ_TIMEOUT RADIOLIB_SX126X_IRQ_TIMEOUT
#define DUCK_RADIO_IRQ_TX_DONE RADIOLIB_SX126X_IRQ_TX_DONE
#define DUCK_RADIO_IRQ_RX_DONE RADIOLIB_SX126X_IRQ_RX_DONE
#define DUCK_RADIO_IRQ_CRC_ERROR RADIOLIB_SX126X_IRQ_CRC_ERR
#define DUCK_RADIO_IRQ_HEADER_ERR RADIOLIB_SX126X_IRQ_HEADER_ERR
#else
#define DUCK_RADIO_IRQ_TIMEOUT RADIOLIB_SX127X_CLEAR_IRQ_FLAG_RX_TIMEOUT
#define DUCK_RADIO_IRQ_TX_DONE RADIOLIB_SX127X_CLEAR_IRQ_FLAG_TX_DONE
#define DUCK_RADIO_IRQ_RX_DONE RADIOLIB_SX127X_CLEAR_IRQ_FLAG_RX_DONE
#define DUCK_RADIO_IRQ_CRC_ERROR RADIOLIB_SX127X_CLEAR_IRQ_FLAG_PAYLOAD_CRC_ERROR
#endif
#if !defined(CDPCFG_HELTEC_CUBE_CELL)
#include "include/DuckUtils.h"
#include <RadioLib.h>
#if defined(CDPCFG_PIN_LORA_SCLK) & defined(CDPCFG_RADIO_SX126X)
#include <SPI.h>
SPIClass spi;
SPISettings spiSettings;
CDPCFG_LORA_CLASS lora =
new Module(CDPCFG_PIN_LORA_CS, CDPCFG_PIN_LORA_DIO1, CDPCFG_PIN_LORA_RST,
CDPCFG_PIN_LORA_BUSY, spi);
#elif CDPCFG_RADIO_SX126X
#else
lora = new Module(CDPCFG_PIN_LORA_CS, CDPCFG_PIN_LORA_DIO0,
CDPCFG_PIN_LORA_RST, CDPCFG_PIN_LORA_DIO1);
lora.setDio1Action(config.func);
// set the interrupt handler to execute when packet tx or rx is done.
lora.setDio0Action(config.func);
#endif
#if defined(CDPCFG_PIN_LORA_SCLK) & defined(CDPCFG_RADIO_SX126X)
#include <SPI.h>
SPIClass spi;
SPISettings spiSettings;
CDPCFG_LORA_CLASS lora =
new Module(CDPCFG_PIN_LORA_CS, CDPCFG_PIN_LORA_DIO1, CDPCFG_PIN_LORA_RST,
CDPCFG_PIN_LORA_BUSY, spi);
#elif CDPCFG_RADIO_SX126X
CDPCFG_LORA_CLASS lora = new Module(CDPCFG_PIN_LORA_CS, CDPCFG_PIN_LORA_DIO1 /*IRQ*/,
CDPCFG_PIN_LORA_RST, CDPCFG_PIN_LORA_BUSY /*GPIO*/);
#else
lora = new Module(CDPCFG_PIN_LORA_CS, CDPCFG_PIN_LORA_DIO0,
CDPCFG_PIN_LORA_RST, CDPCFG_PIN_LORA_DIO1);
lora.setDio1Action(config.func);
// set the interrupt handler to execute when packet tx or rx is done.
lora.setDio0Action(config.func);
#endif
volatile uint16_t DuckRadio::interruptFlags = 0;
volatile bool DuckRadio::receivedFlag = false;
DuckRadio::DuckRadio() {}
int DuckRadio::setupRadio(LoraConfigParams config) {
#if defined(CDPCFG_PIN_LORA_SCLK) & defined(CDPCFG_RADIO_SX126X)
log_n("_spi.begin(CDPCFG_PIN_LORA_SPI_SCK, CDPCFG_PIN_LORA_SPI_MISO, "
"CDPCFG_PIN_LORA_SPI_MOSI, CDPCFG_PIN_LORA_CS)");
spi.begin(CDPCFG_PIN_LORA_SCLK, CDPCFG_PIN_LORA_MISO,
CDPCFG_PIN_LORA_MOSI, CDPCFG_PIN_LORA_CS);
#endif
// TODO: Display should be setup outside the radio setup
// display->setupDisplay(DuckType::MAMA, "mama");
int rc = lora.begin();
if (rc != RADIOLIB_ERR_NONE) {
logerr("ERROR initializing LoRa driver. state = ");
logerr(rc);
return DUCKLORA_ERR_BEGIN;
}
// Lora is started, we need to set all the radio parameters, before it can
// start receiving packets
rc = lora.setFrequency(config.band);
if (rc == RADIOLIB_ERR_INVALID_FREQUENCY) {
logerr("ERROR frequency is invalid");
return DUCKLORA_ERR_SETUP;
}
rc = lora.setBandwidth(config.bw);
if (rc == RADIOLIB_ERR_INVALID_BANDWIDTH) {
logerr("ERROR bandwidth is invalid");
return DUCKLORA_ERR_SETUP;
}
rc = lora.setSpreadingFactor(config.sf);
if (rc == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) {
logerr("ERROR spreading factor is invalid");
return DUCKLORA_ERR_SETUP;
}
rc = lora.setOutputPower(config.txPower);
if (rc == RADIOLIB_ERR_INVALID_OUTPUT_POWER) {
logerr("ERROR output power is invalid");
return DUCKLORA_ERR_SETUP;
}
#if !defined (CDPCFG_RADIO_SX126X)
rc = lora.setGain(CDPCFG_RF_LORA_GAIN);
if (rc == RADIOLIB_ERR_INVALID_GAIN) {
logerr("ERROR gain is invalid");
return DUCKLORA_ERR_SETUP;
}
// set the interrupt handler to execute when packet tx or rx is done.
lora.setDio0Action(config.func);
#else
// set the interrupt handler to execute when packet tx or rx is done.
lora.setDio1Action(config.func);
#endif
// set sync word to private network
rc = lora.setSyncWord(0x12);
if (rc != RADIOLIB_ERR_NONE) {
logerr("ERROR sync word is invalid");
return DUCKLORA_ERR_SETUP;
}
rc = lora.startReceive();
if (rc != RADIOLIB_ERR_NONE) {
logerr("ERROR Failed to start receive");
return DUCKLORA_ERR_RECEIVE;
}
return DUCK_ERR_NONE;
}
void DuckRadio::setSyncWord(byte syncWord) {
int error = lora.setSyncWord(syncWord);
if (error != RADIOLIB_ERR_NONE) {
logerr("ERROR sync word is invalid");
}
lora.startReceive();
}
int DuckRadio::readReceivedData(std::vector<byte>* packetBytes) {
int packet_length = 0;
int err = DUCK_ERR_NONE;
packet_length = lora.getPacketLength();
if (packet_length < MIN_PACKET_LENGTH) {
logerr("ERROR handlePacket rx data size invalid: " +
String(packet_length));
DuckRadio::setReceiveFlag(false);
int rxState = startReceive();
return DUCKLORA_ERR_HANDLE_PACKET;
}
loginfo("readReceivedData() - packet length returns: " +
String(packet_length));
packetBytes->resize(packet_length);
err = lora.readData(packetBytes->data(), packet_length);
loginfo("readReceivedData() - lora.readData returns: " + String(err));
DuckRadio::setReceiveFlag(false);
int rxState = startReceive();
if (err != RADIOLIB_ERR_NONE) {
logerr("ERROR readReceivedData failed. err: " + String(err));
return DUCKLORA_ERR_HANDLE_PACKET;
}
loginfo("Rx packet: " + duckutils::convertToHex(packetBytes->data(), packetBytes->size()));
loginfo("Rx packet: " + duckutils::toString(*packetBytes));
loginfo("readReceivedData: checking path offset integrity");
// Do some sanity checks on the received packet here before we continue
// further RadioLib v4.0.5 has a bug where corrupt packets are still returned
// to the app despite CRC check being enabled in the radio by both sender and
// receiver.
byte* data = packetBytes->data();
loginfo("readReceivedData: checking data section CRC");
std::vector<byte> data_section;
data_section.insert(data_section.end(), &data[DATA_POS], &data[packet_length]);
uint32_t packet_data_crc = duckutils::toUnit32(&data[DATA_CRC_POS]);
uint32_t computed_data_crc =
CRC32::calculate(data_section.data(), data_section.size());
if (computed_data_crc != packet_data_crc) {
logerr("ERROR data crc mismatch: received: " + String(packet_data_crc) +
" calculated:" + String(computed_data_crc));
return DUCKLORA_ERR_HANDLE_PACKET;
}
// we have a good packet
loginfo("RX: rssi: " + String(lora.getRSSI()) +
" snr: " + String(lora.getSNR()) +
" size: " + String(packet_length));
#ifndef CDPCFG_RADIO_SX126X
loginfo("RX: rssi: " + String(lora.getRSSI()) +
" snr: " + String(lora.getSNR()) +
" fe: " + String(lora.getFrequencyError(true)) +
" size: " + String(packet_length));
#else
loginfo("RX: rssi: " + String(lora.getRSSI()) +
" snr: " + String(lora.getSNR()) +
" size: " + String(packet_length));
#endif
if (rxState != RADIOLIB_ERR_NONE) {
return rxState;
}
return err;
}
int DuckRadio::sendData(byte* data, int length) {
// display->log("sendData...");
return startTransmitData(data, length);
}
int DuckRadio::relayPacket(DuckPacket* packet) {
// display->log("relayPacket...");
return startTransmitData(packet->getBuffer().data(),
packet->getBuffer().size());
}
int DuckRadio::sendData(std::vector<byte> data) {
// display->log("sendData...");
return startTransmitData(data.data(), data.size());
}
int DuckRadio::startReceive() {
int state = lora.startReceive();
if (state != RADIOLIB_ERR_NONE) {
logerr("ERROR startReceive failed, code " + String(state));
return DUCKLORA_ERR_RECEIVE;
}
return DUCK_ERR_NONE;
}
int DuckRadio::getRSSI() { return lora.getRSSI(); }
// TODO: implement this
int DuckRadio::ping() { return DUCK_ERR_NOT_SUPPORTED; }
int DuckRadio::standBy() { return lora.standby(); }
int DuckRadio::sleep() { return lora.sleep(); }
void DuckRadio::processRadioIrq() {}
void DuckRadio::setChannel(int channelNum) {
logdbg("Setting Channel to : ");
logdbg(channelNum);
int err;
switch(channelNum) {
case 2:
loginfo("Set channel: " + String(channelNum));
err = lora.setFrequency(CHANNEL_2);
lora.startReceive();
break;
case 3:
loginfo("Set channel: " + String(channelNum));
err = lora.setFrequency(CHANNEL_3);
lora.startReceive();
break;
case 4:
loginfo("Set channel: " + String(channelNum));
err = lora.setFrequency(CHANNEL_4);
lora.startReceive();
break;
case 5:
loginfo("Set channel: " + String(channelNum));
err = lora.setFrequency(CHANNEL_5);
lora.startReceive();
break;
case 6:
loginfo("Set channel: " + String(channelNum));
err = lora.setFrequency(CHANNEL_6);
lora.startReceive();
break;
case 1:
default:
loginfo("Set channel: " + String(channelNum));
err = lora.setFrequency(CHANNEL_1);
lora.startReceive();
break;
}
if (err != RADIOLIB_ERR_NONE) {
logerr("ERROR Failed to set channel");
} else {
lora.startReceive();
channel = channelNum;
loginfo("Channel Set");
}
}
void DuckRadio::serviceInterruptFlags() {
if (DuckRadio::interruptFlags != 0) {
if (DuckRadio::interruptFlags & RADIOLIB_SX127X_CLEAR_IRQ_FLAG_RX_TIMEOUT) {
loginfo("Interrupt flag was set: timeout");
}
if (DuckRadio::interruptFlags & RADIOLIB_SX127X_CLEAR_IRQ_FLAG_RX_DONE) {
loginfo("Interrupt flag was set: packet reception complete");
DuckRadio::setReceiveFlag(true);
}
if (DuckRadio::interruptFlags & RADIOLIB_SX127X_CLEAR_IRQ_FLAG_PAYLOAD_CRC_ERROR) {
loginfo("Interrupt flag was set: payload CRC error");
}
if (DuckRadio::interruptFlags & RADIOLIB_SX127X_CLEAR_IRQ_FLAG_VALID_HEADER) {
loginfo("Interrupt flag was set: valid header received");
}
if (DuckRadio::interruptFlags & RADIOLIB_SX127X_CLEAR_IRQ_FLAG_TX_DONE) {
loginfo("Interrupt flag was set: payload transmission complete");
startReceive();
}
if (DuckRadio::interruptFlags & RADIOLIB_SX127X_CLEAR_IRQ_FLAG_CAD_DONE) {
loginfo("Interrupt flag was set: CAD complete");
}
if (DuckRadio::interruptFlags & RADIOLIB_SX127X_CLEAR_IRQ_FLAG_FHSS_CHANGE_CHANNEL) {
loginfo("Interrupt flag was set: FHSS change channel");
}
if (DuckRadio::interruptFlags & RADIOLIB_SX127X_CLEAR_IRQ_FLAG_CAD_DETECTED) {
loginfo("Interrupt flag was set: valid LoRa signal detected during CAD operation");
}
DuckRadio::interruptFlags = 0;
}
}
// IMPORTANT: this function MUST be 'void' type and MUST NOT have any arguments!
void DuckRadio::onInterrupt(void) {
#ifdef CDPCFG_RADIO_SX126X
interruptFlags = lora.getIrqStatus();
#else
interruptFlags = lora.getIRQFlags();
#endif
}
int DuckRadio::startTransmitData(byte* data, int length) {
int err = DUCK_ERR_NONE;
int tx_err = RADIOLIB_ERR_NONE;
loginfo("TX data");
logdbg(" -> " + duckutils::convertToHex(data, length));
logdbg(" -> length: " + String(length));
long t1 = millis();
// this is going to wait for transmission to complete or to timeout
// when transmit is complete, the Di0 interrupt will be triggered
tx_err = lora.transmit(data, length);
switch (tx_err) {
case RADIOLIB_ERR_NONE:
loginfo("TX data done in : " + String((millis() - t1)) + "ms");
// display->log(">> txdone");
break;
case RADIOLIB_ERR_PACKET_TOO_LONG:
// the supplied packet was longer than 256 bytes
logerr("ERROR startTransmitData too long!");
err = DUCKLORA_ERR_MSG_TOO_LARGE;
break;
case RADIOLIB_ERR_TX_TIMEOUT:
logerr("ERROR startTransmitData timeout!");
display->log("tx-tmout");
err = DUCKLORA_ERR_TIMEOUT;
break;
default:
logerr("ERROR startTransmitData failed, err: " + String(tx_err));
display->log("tx-fail");
err = DUCKLORA_ERR_TRANSMIT;
break;
}
return err;
}
#endif