This commit is contained in:
Ben Meadors
2023-02-02 14:05:58 -06:00
parent 3c6bbff4f9
commit e8e04d23d7
9 changed files with 387 additions and 75 deletions

View File

@@ -26,7 +26,8 @@ static const char *secretReserved = "sekrit";
/// If buf is the reserved secret word, replace the buffer with currentVal
static void writeSecret(char *buf, size_t bufsz, const char *currentVal)
{
if (strcmp(buf, secretReserved) == 0) {
if (strcmp(buf, secretReserved) == 0)
{
strncpy(buf, currentVal, bufsz);
}
}
@@ -44,7 +45,8 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta
bool handled = false;
assert(r);
switch (r->which_payload_variant) {
switch (r->which_payload_variant)
{
/**
* Getters
@@ -64,7 +66,8 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta
handleGetModuleConfig(mp, r->get_module_config_request);
break;
case meshtastic_AdminMessage_get_channel_request_tag: {
case meshtastic_AdminMessage_get_channel_request_tag:
{
uint32_t i = r->get_channel_request - 1;
LOG_INFO("Client is getting channel %u\n", i);
if (i >= MAX_NUM_CHANNELS)
@@ -103,17 +106,22 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta
/**
* Other
*/
case meshtastic_AdminMessage_reboot_seconds_tag: {
case meshtastic_AdminMessage_reboot_seconds_tag:
{
reboot(r->reboot_seconds);
break;
}
case meshtastic_AdminMessage_reboot_ota_seconds_tag: {
case meshtastic_AdminMessage_reboot_ota_seconds_tag:
{
int32_t s = r->reboot_ota_seconds;
#ifdef ARCH_ESP32
if (BleOta::getOtaAppVersion().isEmpty()) {
if (BleOta::getOtaAppVersion().isEmpty())
{
LOG_INFO("No OTA firmware available, scheduling regular reboot in %d seconds\n", s);
screen->startRebootScreen();
} else {
}
else
{
screen->startFirmwareUpdateScreen();
BleOta::switchToOtaApp();
LOG_INFO("Rebooting to OTA in %d seconds\n", s);
@@ -125,35 +133,41 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta
rebootAtMsec = (s < 0) ? 0 : (millis() + s * 1000);
break;
}
case meshtastic_AdminMessage_shutdown_seconds_tag: {
case meshtastic_AdminMessage_shutdown_seconds_tag:
{
int32_t s = r->shutdown_seconds;
LOG_INFO("Shutdown in %d seconds\n", s);
shutdownAtMsec = (s < 0) ? 0 : (millis() + s * 1000);
break;
}
case meshtastic_AdminMessage_get_device_metadata_request_tag: {
case meshtastic_AdminMessage_get_device_metadata_request_tag:
{
LOG_INFO("Client is getting device metadata\n");
handleGetDeviceMetadata(mp);
break;
}
case meshtastic_AdminMessage_factory_reset_tag: {
case meshtastic_AdminMessage_factory_reset_tag:
{
LOG_INFO("Initiating factory reset\n");
nodeDB.factoryReset();
reboot(DEFAULT_REBOOT_SECONDS);
break;
}
case meshtastic_AdminMessage_nodedb_reset_tag: {
case meshtastic_AdminMessage_nodedb_reset_tag:
{
LOG_INFO("Initiating node-db reset\n");
nodeDB.resetNodes();
reboot(DEFAULT_REBOOT_SECONDS);
break;
}
case meshtastic_AdminMessage_begin_edit_settings_tag: {
case meshtastic_AdminMessage_begin_edit_settings_tag:
{
LOG_INFO("Beginning transaction for editing settings\n");
hasOpenEditTransaction = true;
break;
}
case meshtastic_AdminMessage_commit_edit_settings_tag: {
case meshtastic_AdminMessage_commit_edit_settings_tag:
{
LOG_INFO("Committing transaction for edited settings\n");
hasOpenEditTransaction = false;
saveChanges(SEGMENT_CONFIG | SEGMENT_MODULECONFIG | SEGMENT_DEVICESTATE | SEGMENT_CHANNELS);
@@ -170,11 +184,16 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
AdminMessageHandleResult handleResult = MeshModule::handleAdminMessageForAllPlugins(mp, r, &res);
if (handleResult == AdminMessageHandleResult::HANDLED_WITH_RESPONSE) {
if (handleResult == AdminMessageHandleResult::HANDLED_WITH_RESPONSE)
{
myReply = allocDataProtobuf(res);
} else if (mp.decoded.want_response) {
}
else if (mp.decoded.want_response)
{
LOG_DEBUG("We did not responded to a request that wanted a respond. req.variant=%d\n", r->which_payload_variant);
} else if (handleResult != AdminMessageHandleResult::HANDLED) {
}
else if (handleResult != AdminMessageHandleResult::HANDLED)
{
// Probably a message sent by us or sent to our local node. FIXME, we should avoid scanning these messages
LOG_INFO("Ignoring nonrelevant admin %d\n", r->which_payload_variant);
}
@@ -182,7 +201,8 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta
}
// If asked for a response and it is not yet set, generate an 'ACK' response
if (mp.decoded.want_response && !myReply) {
if (mp.decoded.want_response && !myReply)
{
myReply = allocErrorResponse(meshtastic_Routing_Error_NONE, &mp);
}
@@ -198,26 +218,31 @@ void AdminModule::handleSetOwner(const meshtastic_User &o)
int changed = 0;
bool licensed_changed = false;
if (*o.long_name) {
if (*o.long_name)
{
changed |= strcmp(owner.long_name, o.long_name);
strncpy(owner.long_name, o.long_name, sizeof(owner.long_name));
}
if (*o.short_name) {
if (*o.short_name)
{
changed |= strcmp(owner.short_name, o.short_name);
strncpy(owner.short_name, o.short_name, sizeof(owner.short_name));
}
if (*o.id) {
if (*o.id)
{
changed |= strcmp(owner.id, o.id);
strncpy(owner.id, o.id, sizeof(owner.id));
}
if (owner.is_licensed != o.is_licensed) {
if (owner.is_licensed != o.is_licensed)
{
changed = 1;
licensed_changed = true;
owner.is_licensed = o.is_licensed;
config.lora.override_duty_cycle = owner.is_licensed; // override duty cycle for licensed operators
}
if (changed) { // If nothing really changed, don't broadcast on the network or write to flash
if (changed)
{ // If nothing really changed, don't broadcast on the network or write to flash
service.reloadOwner(!hasOpenEditTransaction);
licensed_changed ? saveChanges(SEGMENT_CONFIG | SEGMENT_DEVICESTATE) : saveChanges(SEGMENT_DEVICESTATE);
}
@@ -228,7 +253,8 @@ void AdminModule::handleSetConfig(const meshtastic_Config &c)
auto existingRole = config.device.role;
bool isRegionUnset = (config.lora.region == meshtastic_Config_LoRaConfig_RegionCode_UNSET);
switch (c.which_payload_variant) {
switch (c.which_payload_variant)
{
case meshtastic_Config_device_tag:
LOG_INFO("Setting config: Device\n");
config.has_device = true;
@@ -263,7 +289,8 @@ void AdminModule::handleSetConfig(const meshtastic_Config &c)
LOG_INFO("Setting config: LoRa\n");
config.has_lora = true;
config.lora = c.payload_variant.lora;
if (isRegionUnset && config.lora.region > meshtastic_Config_LoRaConfig_RegionCode_UNSET) {
if (isRegionUnset && config.lora.region > meshtastic_Config_LoRaConfig_RegionCode_UNSET)
{
config.lora.tx_enabled = true;
}
break;
@@ -279,7 +306,8 @@ void AdminModule::handleSetConfig(const meshtastic_Config &c)
void AdminModule::handleSetModuleConfig(const meshtastic_ModuleConfig &c)
{
switch (c.which_payload_variant) {
switch (c.which_payload_variant)
{
case meshtastic_ModuleConfig_mqtt_tag:
LOG_INFO("Setting module config: MQTT\n");
moduleConfig.has_mqtt = true;
@@ -343,7 +371,8 @@ void AdminModule::handleSetChannel(const meshtastic_Channel &cc)
void AdminModule::handleGetOwner(const meshtastic_MeshPacket &req)
{
if (req.decoded.want_response) {
if (req.decoded.want_response)
{
// We create the reply here
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
res.get_owner_response = owner;
@@ -357,8 +386,10 @@ void AdminModule::handleGetConfig(const meshtastic_MeshPacket &req, const uint32
{
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
if (req.decoded.want_response) {
switch (configType) {
if (req.decoded.want_response)
{
switch (configType)
{
case meshtastic_AdminMessage_ConfigType_DEVICE_CONFIG:
LOG_INFO("Getting config: Device\n");
res.get_config_response.which_payload_variant = meshtastic_Config_device_tag;
@@ -413,8 +444,10 @@ void AdminModule::handleGetModuleConfig(const meshtastic_MeshPacket &req, const
{
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
if (req.decoded.want_response) {
switch (configType) {
if (req.decoded.want_response)
{
switch (configType)
{
case meshtastic_AdminMessage_ModuleConfigType_MQTT_CONFIG:
LOG_INFO("Getting module config: MQTT\n");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_mqtt_tag;
@@ -487,15 +520,43 @@ void AdminModule::handleGetDeviceMetadata(const meshtastic_MeshPacket &req)
deviceMetadata.hasEthernet = HAS_ETHERNET;
deviceMetadata.role = config.device.role;
deviceMetadata.position_flags = config.position.position_flags;
deviceMetadata.hw_model = HW_VENDOR;
r.get_device_metadata_response = deviceMetadata;
r.which_payload_variant = meshtastic_AdminMessage_get_device_metadata_response_tag;
myReply = allocDataProtobuf(r);
}
void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &req)
{
meshtastic_AdminMessage r = meshtastic_AdminMessage_init_default;
meshtastic_DeviceConnectionStatus connectionStatus;
connectionStatus.has_wifi = HAS_WIFI;
#if HAS_WIFI
connectionStatus.wifi.status.status.is_connected = WiFi.status() != WL_CONNECTED;
strncpy(connectionStatus.wifi.ssid, config.network.wifi_ssid, 33);
connectionStatus.wifi.status.status.is_syslog_connected = false; // FIXME wire this up
#endif
connectionStatus.has_bluetooth = HAS_BLUETOOTH;
#if HAS_BLUETOOTH
#endif
connectionStatus.has_ethernet = HAS_ETHERNET;
connectionStatus.has_serial = true; // No serial-less devices
r.get_device_connection_status_response = connectionStatus;
r.which_payload_variant = meshtastic_AdminMessage_get_device_connection_status_response_tag;
myReply = allocDataProtobuf(r);
}
void AdminModule::handleGetChannel(const meshtastic_MeshPacket &req, uint32_t channelIndex)
{
if (req.decoded.want_response) {
if (req.decoded.want_response)
{
// We create the reply here
meshtastic_AdminMessage r = meshtastic_AdminMessage_init_default;
r.get_channel_response = channels.getByIndex(channelIndex);
@@ -513,13 +574,17 @@ void AdminModule::reboot(int32_t seconds)
void AdminModule::saveChanges(int saveWhat, bool shouldReboot)
{
if (!hasOpenEditTransaction) {
if (!hasOpenEditTransaction)
{
LOG_INFO("Saving changes to disk\n");
service.reloadConfig(saveWhat); // Calls saveToDisk among other things
} else {
}
else
{
LOG_INFO("Delaying save of changes to disk until the open transaction is committed\n");
}
if (shouldReboot) {
if (shouldReboot)
{
reboot(DEFAULT_REBOOT_SECONDS);
}
}