From 24ac907780ddae6f8731756652c04739bda838d2 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 15 Mar 2020 17:51:57 -0700 Subject: [PATCH] auto generate channel numbers from name NOTE: All radios on a channel will need to be updated to this release before they can talk together again. --- proto | 2 +- src/MeshRadio.cpp | 26 +++++++++++++++++++------- src/mesh.pb.h | 13 +++++-------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/proto b/proto index 66e926740..398fdf362 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit 66e926740acb30518d1fdcb901d1cc0b0d48122c +Subproject commit 398fdf362518e9d6869247cef09f2e071b715639 diff --git a/src/MeshRadio.cpp b/src/MeshRadio.cpp index 3c1d79fa9..c4d51d5d7 100644 --- a/src/MeshRadio.cpp +++ b/src/MeshRadio.cpp @@ -9,8 +9,6 @@ #include "configuration.h" #include "NodeDB.h" -#define DEFAULT_CHANNEL_NUM 3 // we randomly pick one - /// 16 bytes of random PSK for our _public_ default channel that all devices power up on static const uint8_t defaultpsk[] = {0xd4, 0xf1, 0xbb, 0x3a, 0x20, 0x29, 0x07, 0x59, 0xf0, 0xbc, 0xff, 0xab, 0xcf, 0x4e, 0x69, 0xbf}; @@ -39,7 +37,6 @@ MeshRadio::MeshRadio(MemoryPool &_pool, PointerQueue &_r channelSettings.modem_config = ChannelSettings_ModemConfig_Bw125Cr48Sf4096; // slow and long range channelSettings.tx_power = 23; - channelSettings.channel_num = DEFAULT_CHANNEL_NUM; memcpy(&channelSettings.psk, &defaultpsk, sizeof(channelSettings.psk)); strcpy(channelSettings.name, "Default"); // Can't print strings this early - serial not setup yet @@ -81,6 +78,22 @@ bool MeshRadio::init() return true; } +/** hash a string into an integer + * + * djb2 by Dan Bernstein. + * http://www.cse.yorku.ca/~oz/hash.html + */ +unsigned long hash(char *str) +{ + unsigned long hash = 5381; + int c; + + while ((c = *str++) != 0) + hash = ((hash << 5) + hash) + (unsigned char)c; /* hash * 33 + c */ + + return hash; +} + void MeshRadio::reloadConfig() { rf95.setModeIdle(); // Need to be idle before doing init @@ -91,10 +104,9 @@ void MeshRadio::reloadConfig() // setModemConfig(Bw125Cr48Sf4096); // slow and reliable? // rf95.setPreambleLength(8); // Default is 8 - assert(channelSettings.channel_num < NUM_CHANNELS); // If the phone tries to tell us to use an illegal channel then panic - // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM - float center_freq = CH0 + CH_SPACING * channelSettings.channel_num; + int channel_num = hash(channelSettings.name) % NUM_CHANNELS; + float center_freq = CH0 + CH_SPACING * channel_num; if (!rf95.setFrequency(center_freq)) { DEBUG_MSG("setFrequency failed\n"); @@ -109,7 +121,7 @@ void MeshRadio::reloadConfig() // FIXME - can we do this? It seems to be in the Heltec board. rf95.setTxPower(channelSettings.tx_power, false); - DEBUG_MSG("Set radio: name=%s. config=%u, ch=%d, txpower=%d\n", channelSettings.name, channelSettings.modem_config, channelSettings.channel_num, channelSettings.tx_power); + DEBUG_MSG("Set radio: name=%s. config=%u, ch=%d, txpower=%d\n", channelSettings.name, channelSettings.modem_config, channel_num, channelSettings.tx_power); // Done with init tell radio to start receiving rf95.setModeRx(); diff --git a/src/mesh.pb.h b/src/mesh.pb.h index 2422659ef..d1a613ca3 100644 --- a/src/mesh.pb.h +++ b/src/mesh.pb.h @@ -34,7 +34,6 @@ typedef enum _ChannelSettings_ModemConfig { /* Struct definitions */ typedef struct _ChannelSettings { int32_t tx_power; - uint32_t channel_num; ChannelSettings_ModemConfig modem_config; pb_byte_t psk[16]; char name[12]; @@ -173,7 +172,7 @@ typedef struct _ToRadio { #define User_init_default {"", "", "", {0}} #define SubPacket_init_default {0, {Position_init_default}, 0} #define MeshPacket_init_default {0, 0, false, SubPacket_init_default, 0} -#define ChannelSettings_init_default {0, 0, _ChannelSettings_ModemConfig_MIN, {0}, ""} +#define ChannelSettings_init_default {0, _ChannelSettings_ModemConfig_MIN, {0}, ""} #define RadioConfig_init_default {false, RadioConfig_UserPreferences_init_default, false, ChannelSettings_init_default} #define RadioConfig_UserPreferences_init_default {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} #define NodeInfo_init_default {0, false, User_init_default, false, Position_init_default, 0, 0} @@ -186,7 +185,7 @@ typedef struct _ToRadio { #define User_init_zero {"", "", "", {0}} #define SubPacket_init_zero {0, {Position_init_zero}, 0} #define MeshPacket_init_zero {0, 0, false, SubPacket_init_zero, 0} -#define ChannelSettings_init_zero {0, 0, _ChannelSettings_ModemConfig_MIN, {0}, ""} +#define ChannelSettings_init_zero {0, _ChannelSettings_ModemConfig_MIN, {0}, ""} #define RadioConfig_init_zero {false, RadioConfig_UserPreferences_init_zero, false, ChannelSettings_init_zero} #define RadioConfig_UserPreferences_init_zero {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} #define NodeInfo_init_zero {0, false, User_init_zero, false, Position_init_zero, 0, 0} @@ -197,7 +196,6 @@ typedef struct _ToRadio { /* Field tags (for use in manual encoding/decoding) */ #define ChannelSettings_tx_power_tag 1 -#define ChannelSettings_channel_num_tag 2 #define ChannelSettings_modem_config_tag 3 #define ChannelSettings_psk_tag 4 #define ChannelSettings_name_tag 5 @@ -303,7 +301,6 @@ X(a, STATIC, SINGULAR, UINT32, rx_time, 4) #define ChannelSettings_FIELDLIST(X, a) \ X(a, STATIC, SINGULAR, INT32, tx_power, 1) \ -X(a, STATIC, SINGULAR, UINT32, channel_num, 2) \ X(a, STATIC, SINGULAR, UENUM, modem_config, 3) \ X(a, STATIC, SINGULAR, FIXED_LENGTH_BYTES, psk, 4) \ X(a, STATIC, SINGULAR, STRING, name, 5) @@ -421,12 +418,12 @@ extern const pb_msgdesc_t ToRadio_msg; #define User_size 72 #define SubPacket_size 261 #define MeshPacket_size 292 -#define ChannelSettings_size 50 -#define RadioConfig_size 126 +#define ChannelSettings_size 44 +#define RadioConfig_size 120 #define RadioConfig_UserPreferences_size 72 #define NodeInfo_size 155 #define MyNodeInfo_size 63 -#define DeviceState_size 15064 +#define DeviceState_size 15058 #define FromRadio_size 301 #define ToRadio_size 295