#include "meshtastic_presets.h" #include #include namespace roro::meshtastic { const uint8_t kDefaultKey[16] = {0xd4, 0xf1, 0xbb, 0x3a, 0x20, 0x29, 0x07, 0x59, 0xf0, 0xbc, 0xff, 0xab, 0xcf, 0x4e, 0x69, 0x01}; // firmware src/mesh/MeshRadio.h (modemPresetToParams) and RadioInterface.cpp (PRESETS_EU_868). const Preset kEu868Presets[] = { {"LongFast", 250, 11, 5}, {"LongSlow", 125, 12, 8}, {"MediumSlow", 250, 10, 5}, {"MediumFast", 250, 9, 5}, {"ShortSlow", 250, 8, 5}, {"ShortFast", 250, 7, 5}, {"LongMod", 125, 11, 8}, }; const size_t kEu868PresetCount = sizeof kEu868Presets / sizeof kEu868Presets[0]; const Preset* findPreset(const char* name) { for (const Preset& p : kEu868Presets) if (std::strcmp(p.name, name) == 0) return &p; return nullptr; } uint32_t djb2(const char* s) { uint32_t h = 5381; for (; *s; ++s) h = (h << 5) + h + static_cast(*s); return h; } uint8_t channelHash(const char* name, const uint8_t* key, size_t keyLen) { uint8_t h = 0; for (; *name; ++name) h ^= static_cast(*name); for (size_t i = 0; i < keyLen; ++i) h ^= key[i]; return h; } uint32_t eu868FrequencyHz(const Preset& preset, const char* channelName) { constexpr double kStartMHz = 869.4, kEndMHz = 869.65; double slotMHz = preset.bwKHz / 1000.0; uint32_t slots = static_cast(std::lround((kEndMHz - kStartMHz) / slotMHz)); const char* name = channelName && *channelName ? channelName : preset.name; uint32_t slot = slots ? djb2(name) % slots : 0; double mhz = kStartMHz + slotMHz / 2 + slot * slotMHz; return static_cast(std::lround(mhz * 1e6)); } } // namespace roro::meshtastic