-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloramgr.cpp
More file actions
136 lines (111 loc) · 4.47 KB
/
loramgr.cpp
File metadata and controls
136 lines (111 loc) · 4.47 KB
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
/*
SILy
Copyright (C) 2024-2025 Pierre Gaufillet
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "loramgr.h"
#define RADIO_SCLK_PIN 5
#define RADIO_MISO_PIN 3
#define RADIO_MOSI_PIN 6
#define RADIO_CS_PIN 7
#define RADIO_DIO1_PIN 33
#define RADIO_BUSY_PIN 34
#define RADIO_RST_PIN 8
SPIClass silySPI(HSPI);
void LoraMgr::setConfig(SILyPreferences *silyPrefs) {
config.module = LoraMesher::LoraModules::SX1262_MOD;
config.loraCs = RADIO_CS_PIN;
config.loraIrq = RADIO_DIO1_PIN;
config.loraIo1 = RADIO_BUSY_PIN;
config.loraRst = RADIO_RST_PIN;
silySPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN, RADIO_CS_PIN);
config.spi = &silySPI;
config.freq = silyPrefs->get("lora", "frequency").toFloat();
config.bw = silyPrefs->get("lora", "bandwidth").toFloat();
config.sf = silyPrefs->get("lora", "spreadfactor").toInt();
String coding_rate = silyPrefs->get("lora", "coderate");
config.cr = coding_rate.substring(coding_rate.length() - 1).toInt();
config.power = silyPrefs->get("lora", "txpower").toInt();
if (silyPrefs->get("general", "role") == "Gateway") {
radio.addRole(ROLE_GATEWAY);
}
radio.begin(config);
}
void LoraMgr::start() {
// Start the LoRaMesher
int res = xTaskCreate(processReceivedPackets, "", 4096, this, 2,
&receiveLoRaMessage_Handle);
if (res != pdPASS) {
Serial.printf("[ERROR] Receive Task creation error: %d\n", res);
}
radio.setReceiveAppDataTaskHandle(receiveLoRaMessage_Handle);
radio.start();
}
LoraMgr::LoraMgr() {}
void LoraMgr::registerPacketProcessor(uint8_t packet_type,
PacketCallback callback, void *context) {
taskENTER_CRITICAL(&lock);
processors[packet_type].callback = callback;
processors[packet_type].context = context;
taskEXIT_CRITICAL(&lock);
}
void LoraMgr::unregisterPacketProcessor(uint8_t packet_type) {
taskENTER_CRITICAL(&lock);
processors[packet_type].callback = nullptr;
processors[packet_type].context = nullptr;
taskEXIT_CRITICAL(&lock);
}
void LoraMgr::createPacketAndSend(uint16_t dst, uint8_t packetType,
uint8_t *payload, uint8_t payloadSize) {
uint8_t *full_payload = (uint8_t *)malloc(payloadSize + 1);
if (!full_payload)
return; // allocation failure
full_payload[0] = packetType;
memcpy(full_payload + 1, payload, payloadSize);
radio.createPacketAndSend(dst, full_payload, payloadSize + 1);
free(full_payload);
}
void LoraMgr::processReceivedPackets(void *parameters) {
LoraMgr *singleton = static_cast<LoraMgr *>(parameters);
for (;;) {
// Wait for the notification of processReceivedPackets and enter blocking
ulTaskNotifyTake(pdPASS, portMAX_DELAY);
// Get the receivedAppPackets and get all the elements
while (singleton->radio.getReceivedQueueSize() > 0) {
// Get the first element inside the Received User Packets FiFo
AppPacket<uint8_t> *packet = singleton->radio.getNextAppPacket<uint8_t>();
// Serial.printf("[DEBUG] From %04X (%d): ", packet->src,
// packet->payloadSize);
// for (size_t i = 0; i < packet->getPayloadLength(); i++) {
// Serial.printf("%02X ", packet->payload[i]);
// }
// Serial.println();
size_t payloadLength = packet->getPayloadLength();
if (payloadLength > 0) {
PacketCallback cb;
void *ctx;
taskENTER_CRITICAL(&(singleton->lock));
cb = singleton->processors[packet->payload[0]].callback;
ctx = singleton->processors[packet->payload[0]].context;
taskEXIT_CRITICAL(&(singleton->lock));
// Serial.printf("[DEBUG] Processing packet type %02X from %04X\n",
// packet->payload[0], packet->src);
if (cb != nullptr) {
cb(packet->payload + 1, payloadLength - 1, packet->dst, packet->src,
ctx);
}
// Then delete the packet
singleton->radio.deletePacket(packet);
}
}
}
}