Fixed issue #6 - Frequency slot calculation
Also added the option to choose a channel slot (CC_CHANNEL_SLOT in main.h)
This commit is contained in:
parent
ae9800c96c
commit
5e743cd1f0
|
@ -50,7 +50,7 @@ build_type = release
|
||||||
extends = common
|
extends = common
|
||||||
platform = heltec-cubecell
|
platform = heltec-cubecell
|
||||||
board = cubecell_board
|
board = cubecell_board
|
||||||
platform_packages = platformio/toolchain-gccarmnoneeabi@^1.120301.0
|
platform_packages = platformio/toolchain-gccarmnoneeabi@~1.120301.0
|
||||||
;platform_packages = platformio/toolchain-gccarmnoneeabi@^1.130201.0 ;requires manual installation of the package
|
;platform_packages = platformio/toolchain-gccarmnoneeabi@^1.130201.0 ;requires manual installation of the package
|
||||||
build_flags = ${common.build_flags} -Isrc/platform/cubecell
|
build_flags = ${common.build_flags} -Isrc/platform/cubecell
|
||||||
-D CUBECELL
|
-D CUBECELL
|
||||||
|
|
16
src/main.cpp
16
src/main.cpp
|
@ -3,6 +3,7 @@
|
||||||
void setup() {
|
void setup() {
|
||||||
#ifndef SILENT
|
#ifndef SILENT
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
delay(5000);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
msgID.clear();
|
msgID.clear();
|
||||||
|
@ -11,7 +12,7 @@ void setup() {
|
||||||
|
|
||||||
MSG("[INF][CryptoEngine]Initializing ... ");
|
MSG("[INF][CryptoEngine]Initializing ... ");
|
||||||
memcpy(psk.bytes, mypsk, sizeof(mypsk));
|
memcpy(psk.bytes, mypsk, sizeof(mypsk));
|
||||||
psk.length = sizeof(psk.bytes);
|
psk.length = sizeof(mypsk);
|
||||||
crypto->setKey(psk);
|
crypto->setKey(psk);
|
||||||
|
|
||||||
initRegion(); // create regions[] and load myRegion
|
initRegion(); // create regions[] and load myRegion
|
||||||
|
@ -247,8 +248,8 @@ bool perhapsDecode(Packet_t* p) {
|
||||||
mp.decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP;
|
mp.decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
memset(theNode.user.short_name, 0, 5);
|
memset(theNode.user.short_name, 0, sizeof(theNode.user.short_name));
|
||||||
memset(theNode.user.long_name, 0, 40);
|
memset(theNode.user.long_name, 0, sizeof(theNode.user.long_name));
|
||||||
theNode.num = mp.from;
|
theNode.num = mp.from;
|
||||||
theNode.has_user = false;
|
theNode.has_user = false;
|
||||||
theNode.has_position = false;
|
theNode.has_position = false;
|
||||||
|
@ -355,7 +356,6 @@ void printVariants(void){
|
||||||
// /modules/RoutingModule.cpp
|
// /modules/RoutingModule.cpp
|
||||||
if (d.portnum == meshtastic_PortNum_ROUTING_APP){
|
if (d.portnum == meshtastic_PortNum_ROUTING_APP){
|
||||||
MSG("ROUTING \n\r");
|
MSG("ROUTING \n\r");
|
||||||
/*
|
|
||||||
meshtastic_Routing r;
|
meshtastic_Routing r;
|
||||||
if (!pb_decode_from_bytes(d.payload.bytes, d.payload.size, &meshtastic_Routing_msg, &r)) {
|
if (!pb_decode_from_bytes(d.payload.bytes, d.payload.size, &meshtastic_Routing_msg, &r)) {
|
||||||
MSG("*** Error ***\n\r");
|
MSG("*** Error ***\n\r");
|
||||||
|
@ -363,6 +363,7 @@ void printVariants(void){
|
||||||
}
|
}
|
||||||
if (r.which_variant == sizeof(meshtastic_Routing_Error) ) {
|
if (r.which_variant == sizeof(meshtastic_Routing_Error) ) {
|
||||||
MSG("RoutingError=%i\n\r", r.error_reason);
|
MSG("RoutingError=%i\n\r", r.error_reason);
|
||||||
|
/*
|
||||||
} else {
|
} else {
|
||||||
MSG("RouteRequest [");
|
MSG("RouteRequest [");
|
||||||
for (uint8_t i=0; i < r.route_request.route_count; i++) MSG("0x%X ", r.route_request.route[i]);
|
for (uint8_t i=0; i < r.route_request.route_count; i++) MSG("0x%X ", r.route_request.route[i]);
|
||||||
|
@ -370,9 +371,8 @@ void printVariants(void){
|
||||||
MSG("RouteReply [");
|
MSG("RouteReply [");
|
||||||
for (uint8_t i=0; i < r.route_reply.route_count; i++) MSG("0x%X ", r.route_reply.route[i]);
|
for (uint8_t i=0; i < r.route_reply.route_count; i++) MSG("0x%X ", r.route_reply.route[i]);
|
||||||
MSG("]\n\r");
|
MSG("]\n\r");
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
*/
|
||||||
|
}
|
||||||
NodeDB.update(&theNode); // update last heard
|
NodeDB.update(&theNode); // update last heard
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -494,14 +494,14 @@ void printVariants(void){
|
||||||
|
|
||||||
void PacketQueueClass::add(Packet_t* p) {
|
void PacketQueueClass::add(Packet_t* p) {
|
||||||
uint8_t idx = MAX_TX_QUEUE;
|
uint8_t idx = MAX_TX_QUEUE;
|
||||||
for (uint8_t i=0; i<(MAX_TX_QUEUE -1); i++) {
|
for (uint8_t i=0; i<(MAX_TX_QUEUE-1); i++) {
|
||||||
if (Queue[i].size == 0) { // search for a free slot
|
if (Queue[i].size == 0) { // search for a free slot
|
||||||
idx = i;
|
idx = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (idx == MAX_TX_QUEUE) { // no free slot, overwrite oldest packet
|
if (idx == MAX_TX_QUEUE) { // no free slot, overwrite oldest packet
|
||||||
for (uint8_t i=1; i<(MAX_TX_QUEUE -1); i++) {
|
for (uint8_t i=1; i<(MAX_TX_QUEUE-1); i++) {
|
||||||
if (Queue[idx].packetTime < Queue[i].packetTime) idx = i;
|
if (Queue[idx].packetTime < Queue[i].packetTime) idx = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
48
src/main.h
48
src/main.h
|
@ -5,7 +5,8 @@
|
||||||
#define CC_MY_REGION meshtastic_Config_LoRaConfig_RegionCode_EU_868 // see regions[] below
|
#define CC_MY_REGION meshtastic_Config_LoRaConfig_RegionCode_EU_868 // see regions[] below
|
||||||
#define CC_MY_LORA_PRESET meshtastic_Config_LoRaConfig_ModemPreset_LONG_FAST // LONG FAST is default preset
|
#define CC_MY_LORA_PRESET meshtastic_Config_LoRaConfig_ModemPreset_LONG_FAST // LONG FAST is default preset
|
||||||
#define CC_LORA_USE_PRESET true // set true to use modem preset
|
#define CC_LORA_USE_PRESET true // set true to use modem preset
|
||||||
#define CC_CHANNEL_NAME "Primary Channel"
|
#define CC_CHANNEL_NAME "LongFast"
|
||||||
|
#define CC_CHANNEL_SLOT 0 // "0" for calculated slot. Any other number = chosen slot. Count starts with 1.
|
||||||
|
|
||||||
#define CC_MY_LORA_BW 125.0 // use these settings, if not using a modem preset
|
#define CC_MY_LORA_BW 125.0 // use these settings, if not using a modem preset
|
||||||
#define CC_MY_LORA_SF 9
|
#define CC_MY_LORA_SF 9
|
||||||
|
@ -19,11 +20,6 @@
|
||||||
#define MAX_NODE_LIST 20 // number of stored known nodes
|
#define MAX_NODE_LIST 20 // number of stored known nodes
|
||||||
#define MAX_TX_QUEUE 8 // max number of packets which can be waiting for transmission
|
#define MAX_TX_QUEUE 8 // max number of packets which can be waiting for transmission
|
||||||
#define MAX_RHPACKETLEN 256
|
#define MAX_RHPACKETLEN 256
|
||||||
#define PACKET_FLAGS_HOP_LIMIT_MASK 0x07
|
|
||||||
#define PACKET_FLAGS_WANT_ACK_MASK 0x08
|
|
||||||
#define PACKET_FLAGS_VIA_MQTT_MASK 0x10
|
|
||||||
#define PACKET_FLAGS_HOP_START_MASK 0xE0
|
|
||||||
#define PACKET_FLAGS_HOP_START_SHIFT 5
|
|
||||||
|
|
||||||
/// 16 bytes of random PSK for our _public_ default channel that all devices power up on (AES128)
|
/// 16 bytes of random PSK for our _public_ default channel that all devices power up on (AES128)
|
||||||
/// Meshtastic default key (AQ==):
|
/// Meshtastic default key (AQ==):
|
||||||
|
@ -50,6 +46,11 @@ static const uint8_t mypsk[] = {0xd4, 0xf1, 0xbb, 0x3a, 0x20, 0x29, 0x07, 0x59,
|
||||||
using std::min;
|
using std::min;
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
|
|
||||||
|
#define PACKET_FLAGS_HOP_LIMIT_MASK 0x07
|
||||||
|
#define PACKET_FLAGS_WANT_ACK_MASK 0x08
|
||||||
|
#define PACKET_FLAGS_VIA_MQTT_MASK 0x10
|
||||||
|
#define PACKET_FLAGS_HOP_START_MASK 0xE0
|
||||||
|
#define PACKET_FLAGS_HOP_START_SHIFT 5
|
||||||
|
|
||||||
#include <RadioLib.h>
|
#include <RadioLib.h>
|
||||||
|
|
||||||
|
@ -65,12 +66,12 @@ SX1262 radio = new Module(RADIOLIB_BUILTIN_MODULE);
|
||||||
#include <pb_decode.h>
|
#include <pb_decode.h>
|
||||||
#include <pb_encode.h>
|
#include <pb_encode.h>
|
||||||
#include <CryptoEngine.h>
|
#include <CryptoEngine.h>
|
||||||
|
/*
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
#include <mesh/compression/unishox2.h>
|
#include <mesh/compression/unishox2.h>
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
// struct to store the raw packet data (buf, size) and the time of receiving
|
// struct to store the raw packet data (buf, size) and the time of receiving
|
||||||
typedef struct {
|
typedef struct {
|
||||||
size_t size;
|
size_t size;
|
||||||
|
@ -388,17 +389,17 @@ uint8_t xorHash(const uint8_t *p, size_t len)
|
||||||
* If no suitable channel could be found, return -1
|
* If no suitable channel could be found, return -1
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int16_t generateHash(ChannelIndex channelNum)
|
int16_t generateHash(const char *name)
|
||||||
{
|
{
|
||||||
auto k = psk; //getKey(channelNum);
|
/* auto k = getKey(channelNum);
|
||||||
if (k.length < 0)
|
if (k.length < 0)
|
||||||
return -1; // invalid
|
return -1; // invalid
|
||||||
else {
|
else {*/
|
||||||
const char *name = CC_CHANNEL_NAME; //getName(channelNum);
|
//const char *name = getName(channelNum);
|
||||||
uint8_t h = xorHash((const uint8_t *)name, strlen(name));
|
uint8_t h = xorHash((const uint8_t *)name, strlen(name));
|
||||||
h ^= xorHash(k.bytes, k.length);
|
h ^= xorHash(psk.bytes, psk.length);
|
||||||
return h;
|
return h;
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getPacketTime(uint32_t pl)
|
uint32_t getPacketTime(uint32_t pl)
|
||||||
|
@ -499,9 +500,16 @@ void applyModemConfig()
|
||||||
uint32_t numChannels = floor((myRegion->freqEnd - myRegion->freqStart) / (myRegion->spacing + (bw / 1000)));
|
uint32_t numChannels = floor((myRegion->freqEnd - myRegion->freqStart) / (myRegion->spacing + (bw / 1000)));
|
||||||
|
|
||||||
const char *channelName = CC_CHANNEL_NAME;
|
const char *channelName = CC_CHANNEL_NAME;
|
||||||
|
int channel_num;
|
||||||
|
|
||||||
int channel_num = hash(channelName) % numChannels;
|
if (CC_CHANNEL_SLOT == 0) {
|
||||||
|
channel_num = hash(channelName) % numChannels;
|
||||||
|
} else {
|
||||||
|
channel_num = (CC_CHANNEL_SLOT-1) % numChannels;
|
||||||
|
}
|
||||||
|
|
||||||
|
MSG("[INF]Channel name is \"%s\"\n\r", channelName);
|
||||||
|
MSG("[INF]Channel slot is %i (%i available frequency slots).\n\r", channel_num + 1, numChannels);
|
||||||
freq = myRegion->freqStart + (bw / 2000) + (channel_num * (bw / 1000));
|
freq = myRegion->freqStart + (bw / 2000) + (channel_num * (bw / 1000));
|
||||||
|
|
||||||
// override if we have a verbatim frequency
|
// override if we have a verbatim frequency
|
||||||
|
@ -510,7 +518,7 @@ void applyModemConfig()
|
||||||
channel_num = -1;
|
channel_num = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
MSG("[INF]Using Region %s freq %d bw %i sf %i cr %i power %i ... ",myRegion->name, lround(freq*1E6), lround(bw*1000), sf, cr, power);
|
MSG("[INF]Using Region %s : freq=%d bw=%i sf=%i cr=%i power=%i ... ",myRegion->name, lround(freq*1E6), lround(bw*1000), sf, cr, power);
|
||||||
|
|
||||||
// Syncword is 0x2b, see RadioLibInterface.h
|
// Syncword is 0x2b, see RadioLibInterface.h
|
||||||
// preamble length is 16, see RadioInterface.h
|
// preamble length is 16, see RadioInterface.h
|
||||||
|
@ -520,7 +528,7 @@ void applyModemConfig()
|
||||||
if (err == RADIOLIB_ERR_NONE) {
|
if (err == RADIOLIB_ERR_NONE) {
|
||||||
MSG("success!\n");
|
MSG("success!\n");
|
||||||
} else {
|
} else {
|
||||||
MSG("\n[ERROR] [SX1262} Init failed, code: %i\n\n ** Full Stop **", err);
|
MSG("\n[ERROR] [SX1262} Init failed, code: %i\n\n\r ** Full Stop **", err);
|
||||||
while (true);
|
while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -528,7 +536,7 @@ void applyModemConfig()
|
||||||
slotTimeMsec = 8.5 * pow(2, sf) / bw + 0.2 + 0.4 + 7;
|
slotTimeMsec = 8.5 * pow(2, sf) / bw + 0.2 + 0.4 + 7;
|
||||||
preambleTimeMsec = getPacketTime((uint32_t)0);
|
preambleTimeMsec = getPacketTime((uint32_t)0);
|
||||||
maxPacketTimeMsec = getPacketTime(meshtastic_Constants_DATA_PAYLOAD_LEN + sizeof(PacketHeader));
|
maxPacketTimeMsec = getPacketTime(meshtastic_Constants_DATA_PAYLOAD_LEN + sizeof(PacketHeader));
|
||||||
MSG("[INF]SlotTime=%ims PreambleTime=%ims maxPacketTime=%ims\n", slotTimeMsec, preambleTimeMsec, maxPacketTimeMsec);
|
MSG("[INF]SlotTime=%ims PreambleTime=%ims maxPacketTime=%ims\n\r", slotTimeMsec, preambleTimeMsec, maxPacketTimeMsec);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The delay to use when we want to flood a message */
|
/** The delay to use when we want to flood a message */
|
||||||
|
@ -565,12 +573,12 @@ bool isActivelyReceiving()
|
||||||
} else if ((now - activeReceiveStart > 2 * preambleTimeMsec) && !(irq & RADIOLIB_SX126X_IRQ_HEADER_VALID)) {
|
} else if ((now - activeReceiveStart > 2 * preambleTimeMsec) && !(irq & RADIOLIB_SX126X_IRQ_HEADER_VALID)) {
|
||||||
// The HEADER_VALID flag should be set by now if it was really a packet, so ignore PREAMBLE_DETECTED flag
|
// The HEADER_VALID flag should be set by now if it was really a packet, so ignore PREAMBLE_DETECTED flag
|
||||||
activeReceiveStart = 0;
|
activeReceiveStart = 0;
|
||||||
MSG("Ignore false preamble detection.\n");
|
MSG("Ignore false preamble detection.\n\r");
|
||||||
return false;
|
return false;
|
||||||
} else if (now - activeReceiveStart > maxPacketTimeMsec) {
|
} else if (now - activeReceiveStart > maxPacketTimeMsec) {
|
||||||
// We should have gotten an RX_DONE IRQ by now if it was really a packet, so ignore HEADER_VALID flag
|
// We should have gotten an RX_DONE IRQ by now if it was really a packet, so ignore HEADER_VALID flag
|
||||||
activeReceiveStart = 0;
|
activeReceiveStart = 0;
|
||||||
MSG("Ignore false header detection.\n");
|
MSG("Ignore false header detection.\n\r");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue