Merge branch 'meshtastic:master' into use_detected_ina_addr

This commit is contained in:
Michael Gjelsø 2025-02-16 15:26:05 +01:00 committed by GitHub
commit 61912b0dd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 82 additions and 12 deletions

View File

@ -162,7 +162,9 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta
case meshtastic_AdminMessage_set_module_config_tag: case meshtastic_AdminMessage_set_module_config_tag:
LOG_INFO("Client set module config"); LOG_INFO("Client set module config");
handleSetModuleConfig(r->set_module_config); if (!handleSetModuleConfig(r->set_module_config)) {
myReply = allocErrorResponse(meshtastic_Routing_Error_BAD_REQUEST, &mp);
}
break; break;
case meshtastic_AdminMessage_set_channel_tag: case meshtastic_AdminMessage_set_channel_tag:
@ -648,15 +650,23 @@ void AdminModule::handleSetConfig(const meshtastic_Config &c)
saveChanges(changes, requiresReboot); saveChanges(changes, requiresReboot);
} }
void AdminModule::handleSetModuleConfig(const meshtastic_ModuleConfig &c) bool AdminModule::handleSetModuleConfig(const meshtastic_ModuleConfig &c)
{ {
if (!hasOpenEditTransaction) if (!hasOpenEditTransaction)
disableBluetooth(); disableBluetooth();
switch (c.which_payload_variant) { switch (c.which_payload_variant) {
case meshtastic_ModuleConfig_mqtt_tag: case meshtastic_ModuleConfig_mqtt_tag:
#if MESHTASTIC_EXCLUDE_MQTT
LOG_WARN("Set module config: MESHTASTIC_EXCLUDE_MQTT is defined. Not setting MQTT config");
return false;
#else
LOG_INFO("Set module config: MQTT"); LOG_INFO("Set module config: MQTT");
if (!MQTT::isValidConfig(c.payload_variant.mqtt)) {
return false;
}
moduleConfig.has_mqtt = true; moduleConfig.has_mqtt = true;
moduleConfig.mqtt = c.payload_variant.mqtt; moduleConfig.mqtt = c.payload_variant.mqtt;
#endif
break; break;
case meshtastic_ModuleConfig_serial_tag: case meshtastic_ModuleConfig_serial_tag:
LOG_INFO("Set module config: Serial"); LOG_INFO("Set module config: Serial");
@ -724,6 +734,7 @@ void AdminModule::handleSetModuleConfig(const meshtastic_ModuleConfig &c)
break; break;
} }
saveChanges(SEGMENT_MODULECONFIG); saveChanges(SEGMENT_MODULECONFIG);
return true;
} }
void AdminModule::handleSetChannel(const meshtastic_Channel &cc) void AdminModule::handleSetChannel(const meshtastic_Channel &cc)
@ -1160,4 +1171,4 @@ void disableBluetooth()
nrf52Bluetooth->shutdown(); nrf52Bluetooth->shutdown();
#endif #endif
#endif #endif
} }

View File

@ -50,7 +50,7 @@ class AdminModule : public ProtobufModule<meshtastic_AdminMessage>, public Obser
void handleSetOwner(const meshtastic_User &o); void handleSetOwner(const meshtastic_User &o);
void handleSetChannel(const meshtastic_Channel &cc); void handleSetChannel(const meshtastic_Channel &cc);
void handleSetConfig(const meshtastic_Config &c); void handleSetConfig(const meshtastic_Config &c);
void handleSetModuleConfig(const meshtastic_ModuleConfig &c); bool handleSetModuleConfig(const meshtastic_ModuleConfig &c);
void handleSetChannel(); void handleSetChannel();
void handleSetHamMode(const meshtastic_HamParameters &req); void handleSetHamMode(const meshtastic_HamParameters &req);
void handleStoreDeviceUIConfig(const meshtastic_DeviceUIConfig &uicfg); void handleStoreDeviceUIConfig(const meshtastic_DeviceUIConfig &uicfg);

View File

@ -41,6 +41,7 @@ MQTT *mqtt;
namespace namespace
{ {
constexpr int reconnectMax = 5; constexpr int reconnectMax = 5;
constexpr uint16_t mqttPort = 1883;
// FIXME - this size calculation is super sloppy, but it will go away once we dynamically alloc meshpackets // FIXME - this size calculation is super sloppy, but it will go away once we dynamically alloc meshpackets
static uint8_t bytes[meshtastic_MqttClientProxyMessage_size + 30]; // 12 for channel name and 16 for nodeid static uint8_t bytes[meshtastic_MqttClientProxyMessage_size + 30]; // 12 for channel name and 16 for nodeid
@ -245,6 +246,11 @@ std::pair<String, uint16_t> parseHostAndPort(String server, uint16_t port = 0)
} }
return std::make_pair(std::move(server), port); return std::make_pair(std::move(server), port);
} }
bool isDefaultServer(const String &host)
{
return host.length() == 0 || host == default_mqtt_address;
}
} // namespace } // namespace
void MQTT::mqttCallback(char *topic, byte *payload, unsigned int length) void MQTT::mqttCallback(char *topic, byte *payload, unsigned int length)
@ -324,7 +330,7 @@ MQTT::MQTT() : concurrency::OSThread("mqtt"), mqttQueue(MAX_MQTT_QUEUE)
} }
String host = parseHostAndPort(moduleConfig.mqtt.address).first; String host = parseHostAndPort(moduleConfig.mqtt.address).first;
isConfiguredForDefaultServer = host.length() == 0 || host == default_mqtt_address; isConfiguredForDefaultServer = isDefaultServer(host);
IPAddress ip; IPAddress ip;
isMqttServerAddressPrivate = ip.fromString(host.c_str()) && isPrivateIpAddress(ip); isMqttServerAddressPrivate = ip.fromString(host.c_str()) && isPrivateIpAddress(ip);
@ -408,7 +414,7 @@ void MQTT::reconnect()
} }
#if HAS_NETWORKING #if HAS_NETWORKING
// Defaults // Defaults
int serverPort = 1883; int serverPort = mqttPort;
const char *serverAddr = default_mqtt_address; const char *serverAddr = default_mqtt_address;
const char *mqttUsername = default_mqtt_username; const char *mqttUsername = default_mqtt_username;
const char *mqttPassword = default_mqtt_password; const char *mqttPassword = default_mqtt_password;
@ -561,6 +567,23 @@ int32_t MQTT::runOnce()
return 30000; return 30000;
} }
bool MQTT::isValidConfig(const meshtastic_ModuleConfig_MQTTConfig &config)
{
String host;
uint16_t port;
std::tie(host, port) = parseHostAndPort(config.address, mqttPort);
const bool defaultServer = isDefaultServer(host);
if (defaultServer && config.tls_enabled) {
LOG_ERROR("Invalid MQTT config: TLS was enabled, but the default server does not support TLS");
return false;
}
if (defaultServer && port != mqttPort) {
LOG_ERROR("Invalid MQTT config: Unsupported port '%d' for the default MQTT server", port);
return false;
}
return true;
}
void MQTT::publishNodeInfo() void MQTT::publishNodeInfo()
{ {
// TODO: NodeInfo broadcast over MQTT only (NODENUM_BROADCAST_NO_LORA) // TODO: NodeInfo broadcast over MQTT only (NODENUM_BROADCAST_NO_LORA)

View File

@ -61,6 +61,8 @@ class MQTT : private concurrency::OSThread
bool isUsingDefaultServer() { return isConfiguredForDefaultServer; } bool isUsingDefaultServer() { return isConfiguredForDefaultServer; }
static bool isValidConfig(const meshtastic_ModuleConfig_MQTTConfig &config);
protected: protected:
struct QueueEntry { struct QueueEntry {
std::string topic; std::string topic;

View File

@ -800,6 +800,38 @@ void test_customMqttRoot(void)
[] { return pubsub->subscriptions_.count("custom/2/e/test/+") && pubsub->subscriptions_.count("custom/2/e/PKI/+"); })); [] { return pubsub->subscriptions_.count("custom/2/e/test/+") && pubsub->subscriptions_.count("custom/2/e/PKI/+"); }));
} }
// Empty configuration is valid.
void test_configurationEmptyIsValid(void)
{
meshtastic_ModuleConfig_MQTTConfig config;
TEST_ASSERT_TRUE(MQTT::isValidConfig(config));
}
// Configuration with the default server is valid.
void test_configWithDefaultServer(void)
{
meshtastic_ModuleConfig_MQTTConfig config = {.address = default_mqtt_address};
TEST_ASSERT_TRUE(MQTT::isValidConfig(config));
}
// Configuration with the default server and port 8888 is invalid.
void test_configWithDefaultServerAndInvalidPort(void)
{
meshtastic_ModuleConfig_MQTTConfig config = {.address = default_mqtt_address ":8888"};
TEST_ASSERT_FALSE(MQTT::isValidConfig(config));
}
// Configuration with the default server and tls_enabled = true is invalid.
void test_configWithDefaultServerAndInvalidTLSEnabled(void)
{
meshtastic_ModuleConfig_MQTTConfig config = {.tls_enabled = true};
TEST_ASSERT_FALSE(MQTT::isValidConfig(config));
}
void setup() void setup()
{ {
initializeTestEnvironment(); initializeTestEnvironment();
@ -843,6 +875,10 @@ void setup()
RUN_TEST(test_enabled); RUN_TEST(test_enabled);
RUN_TEST(test_disabled); RUN_TEST(test_disabled);
RUN_TEST(test_customMqttRoot); RUN_TEST(test_customMqttRoot);
RUN_TEST(test_configurationEmptyIsValid);
RUN_TEST(test_configWithDefaultServer);
RUN_TEST(test_configWithDefaultServerAndInvalidPort);
RUN_TEST(test_configWithDefaultServerAndInvalidTLSEnabled);
exit(UNITY_END()); exit(UNITY_END());
} }
#else #else

View File

@ -84,17 +84,15 @@ static const uint8_t A5 = PIN_A5;
#define PIN_NFC2 (31) #define PIN_NFC2 (31)
// RX and TX pins // RX and TX pins
#define PIN_SERIAL1_RX (6) #define PIN_SERIAL1_RX (-1)
#define PIN_SERIAL1_TX (7) #define PIN_SERIAL1_TX (-1)
// complains if not defined // complains if not defined
#define PIN_SERIAL2_RX (-1) #define PIN_SERIAL2_RX (-1)
#define PIN_SERIAL2_TX (-1) #define PIN_SERIAL2_TX (-1)
// 4 is used as RF_SW and 5 for USR button so... // 4 is used as RF_SW and 5 for USR button so...
#define PIN_WIRE_SDA (-1) #define PIN_WIRE_SDA (6)
#define PIN_WIRE_SCL (-1) #define PIN_WIRE_SCL (7)
// #define PIN_WIRE_SDA (6)
// #define PIN_WIRE_SCL (7)
static const uint8_t SDA = PIN_WIRE_SDA; static const uint8_t SDA = PIN_WIRE_SDA;
static const uint8_t SCL = PIN_WIRE_SCL; static const uint8_t SCL = PIN_WIRE_SCL;