Files
firmware/src/modules/AdminModule.cpp

614 lines
23 KiB
C++
Raw Normal View History

#include "AdminModule.h"
2021-02-26 15:34:00 +08:00
#include "Channels.h"
2021-02-21 14:03:44 +08:00
#include "MeshService.h"
#include "NodeDB.h"
2022-10-09 11:19:33 +02:00
#ifdef ARCH_ESP32
2022-10-08 11:10:29 +02:00
#include "BleOta.h"
2022-10-09 11:19:33 +02:00
#endif
2021-02-21 14:03:44 +08:00
#include "Router.h"
2022-05-02 08:53:44 +10:00
#include "configuration.h"
2021-02-21 14:03:44 +08:00
#include "main.h"
#ifdef ARCH_PORTDUINO
2021-03-18 19:09:31 +08:00
#include "unistd.h"
#endif
2023-02-02 14:11:48 -06:00
#if HAS_WIFI || HAS_ETHERNET
#include "mqtt/MQTT.h"
#endif
#define DEFAULT_REBOOT_SECONDS 5
2022-02-27 02:21:02 -08:00
AdminModule *adminModule;
bool hasOpenEditTransaction;
2021-02-21 14:03:44 +08:00
/// A special reserved string to indicate strings we can not share with external nodes. We will use this 'reserved' word instead.
2022-05-02 08:53:44 +10:00
/// Also, to make setting work correctly, if someone tries to set a string to this reserved value we assume they don't really want
/// a change.
static const char *secretReserved = "sekrit";
/// If buf is the reserved secret word, replace the buffer with currentVal
2023-01-16 11:08:48 +01:00
static void writeSecret(char *buf, size_t bufsz, const char *currentVal)
2022-05-02 08:53:44 +10:00
{
2023-02-02 14:05:58 -06:00
if (strcmp(buf, secretReserved) == 0)
{
2023-01-16 11:08:48 +01:00
strncpy(buf, currentVal, bufsz);
}
}
2022-05-02 22:00:24 +10:00
/**
* @brief Handle recieved protobuf message
*
* @param mp Recieved MeshPacket
* @param r Decoded AdminMessage
* @return bool
*/
bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_AdminMessage *r)
2021-02-21 14:03:44 +08:00
{
// if handled == false, then let others look at this message also if they want
bool handled = false;
2021-02-21 14:03:44 +08:00
assert(r);
2022-05-02 22:00:24 +10:00
2023-02-02 14:05:58 -06:00
switch (r->which_payload_variant)
{
2022-05-02 22:00:24 +10:00
/**
* Getters
*/
case meshtastic_AdminMessage_get_owner_request_tag:
LOG_INFO("Client is getting owner\n");
2022-05-02 22:00:24 +10:00
handleGetOwner(mp);
2021-02-26 15:34:00 +08:00
break;
case meshtastic_AdminMessage_get_config_request_tag:
LOG_INFO("Client is getting config\n");
2022-05-29 16:31:30 +02:00
handleGetConfig(mp, r->get_config_request);
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_get_module_config_request_tag:
LOG_INFO("Client is getting module config\n");
2022-05-03 22:16:50 +10:00
handleGetModuleConfig(mp, r->get_module_config_request);
2021-02-26 15:34:00 +08:00
break;
2023-02-02 14:05:58 -06:00
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)
myReply = allocErrorResponse(meshtastic_Routing_Error_BAD_REQUEST, &mp);
else
handleGetChannel(mp, i);
2021-02-26 15:34:00 +08:00
break;
}
2021-02-26 15:34:00 +08:00
2022-05-02 22:00:24 +10:00
/**
* Setters
*/
case meshtastic_AdminMessage_set_owner_tag:
LOG_INFO("Client is setting owner\n");
2022-05-02 22:00:24 +10:00
handleSetOwner(r->set_owner);
2021-02-26 15:34:00 +08:00
break;
case meshtastic_AdminMessage_set_config_tag:
LOG_INFO("Client is setting the config\n");
2022-05-02 08:53:44 +10:00
handleSetConfig(r->set_config);
break;
case meshtastic_AdminMessage_set_module_config_tag:
LOG_INFO("Client is setting the module config\n");
2022-05-02 22:00:24 +10:00
handleSetModuleConfig(r->set_module_config);
2022-03-10 09:14:56 +11:00
break;
case meshtastic_AdminMessage_set_channel_tag:
LOG_INFO("Client is setting channel %d\n", r->set_channel.index);
2022-05-02 22:00:24 +10:00
if (r->set_channel.index < 0 || r->set_channel.index >= (int)MAX_NUM_CHANNELS)
myReply = allocErrorResponse(meshtastic_Routing_Error_BAD_REQUEST, &mp);
2022-05-02 22:00:24 +10:00
else
handleSetChannel(r->set_channel);
break;
/**
* Other
*/
2023-02-02 14:05:58 -06:00
case meshtastic_AdminMessage_reboot_seconds_tag:
{
2022-10-19 19:19:04 -05:00
reboot(r->reboot_seconds);
2021-03-27 10:19:59 +08:00
break;
}
2023-02-02 14:05:58 -06:00
case meshtastic_AdminMessage_reboot_ota_seconds_tag:
{
2022-10-08 11:10:29 +02:00
int32_t s = r->reboot_ota_seconds;
#ifdef ARCH_ESP32
2023-02-02 14:05:58 -06:00
if (BleOta::getOtaAppVersion().isEmpty())
{
LOG_INFO("No OTA firmware available, scheduling regular reboot in %d seconds\n", s);
screen->startRebootScreen();
2023-02-02 14:05:58 -06:00
}
else
{
screen->startFirmwareUpdateScreen();
2022-10-08 11:10:29 +02:00
BleOta::switchToOtaApp();
LOG_INFO("Rebooting to OTA in %d seconds\n", s);
2022-10-08 11:10:29 +02:00
}
#else
LOG_INFO("Not on ESP32, scheduling regular reboot in %d seconds\n", s);
screen->startRebootScreen();
#endif
2022-10-08 11:10:29 +02:00
rebootAtMsec = (s < 0) ? 0 : (millis() + s * 1000);
break;
}
2023-02-02 14:05:58 -06:00
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;
}
2023-02-02 14:05:58 -06:00
case meshtastic_AdminMessage_get_device_metadata_request_tag:
{
LOG_INFO("Client is getting device metadata\n");
handleGetDeviceMetadata(mp);
break;
}
2023-02-02 14:05:58 -06:00
case meshtastic_AdminMessage_factory_reset_tag:
{
LOG_INFO("Initiating factory reset\n");
nodeDB.factoryReset();
reboot(DEFAULT_REBOOT_SECONDS);
break;
}
2023-02-02 14:05:58 -06:00
case meshtastic_AdminMessage_nodedb_reset_tag:
{
LOG_INFO("Initiating node-db reset\n");
2022-11-23 14:12:44 -06:00
nodeDB.resetNodes();
reboot(DEFAULT_REBOOT_SECONDS);
break;
}
2023-02-02 14:05:58 -06:00
case meshtastic_AdminMessage_begin_edit_settings_tag:
{
LOG_INFO("Beginning transaction for editing settings\n");
hasOpenEditTransaction = true;
break;
}
2023-02-02 14:05:58 -06:00
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);
2022-09-28 16:47:26 -05:00
break;
}
#ifdef ARCH_PORTDUINO
2023-01-21 18:49:28 +01:00
case meshtastic_AdminMessage_exit_simulator_tag:
LOG_INFO("Exiting simulator\n");
2021-03-18 19:09:31 +08:00
_exit(0);
break;
#endif
2021-02-26 15:34:00 +08:00
default:
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
2022-05-03 22:16:50 +10:00
AdminMessageHandleResult handleResult = MeshModule::handleAdminMessageForAllPlugins(mp, r, &res);
2023-02-02 14:05:58 -06:00
if (handleResult == AdminMessageHandleResult::HANDLED_WITH_RESPONSE)
{
2022-05-03 22:16:50 +10:00
myReply = allocDataProtobuf(res);
2023-02-02 14:05:58 -06:00
}
else if (mp.decoded.want_response)
{
2022-12-29 20:41:37 -06:00
LOG_DEBUG("We did not responded to a request that wanted a respond. req.variant=%d\n", r->which_payload_variant);
2023-02-02 14:05:58 -06:00
}
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);
}
2021-02-26 15:34:00 +08:00
break;
2021-02-21 14:03:44 +08:00
}
2022-11-25 20:33:12 +01:00
// If asked for a response and it is not yet set, generate an 'ACK' response
2023-02-02 14:05:58 -06:00
if (mp.decoded.want_response && !myReply)
{
myReply = allocErrorResponse(meshtastic_Routing_Error_NONE, &mp);
2022-11-25 20:33:12 +01:00
}
return handled;
2021-02-21 14:03:44 +08:00
}
2022-05-02 22:00:24 +10:00
/**
* Setter methods
*/
void AdminModule::handleSetOwner(const meshtastic_User &o)
2021-02-21 14:03:44 +08:00
{
int changed = 0;
bool licensed_changed = false;
2021-02-21 14:03:44 +08:00
2023-02-02 14:05:58 -06:00
if (*o.long_name)
{
2021-02-21 14:03:44 +08:00
changed |= strcmp(owner.long_name, o.long_name);
strncpy(owner.long_name, o.long_name, sizeof(owner.long_name));
2021-02-21 14:03:44 +08:00
}
2023-02-02 14:05:58 -06:00
if (*o.short_name)
{
2021-02-21 14:03:44 +08:00
changed |= strcmp(owner.short_name, o.short_name);
strncpy(owner.short_name, o.short_name, sizeof(owner.short_name));
2021-02-21 14:03:44 +08:00
}
2023-02-02 14:05:58 -06:00
if (*o.id)
{
2021-02-21 14:03:44 +08:00
changed |= strcmp(owner.id, o.id);
strncpy(owner.id, o.id, sizeof(owner.id));
2021-02-21 14:03:44 +08:00
}
2023-02-02 14:05:58 -06:00
if (owner.is_licensed != o.is_licensed)
{
2021-11-02 13:27:56 +00:00
changed = 1;
licensed_changed = true;
2021-04-10 11:39:13 +08:00
owner.is_licensed = o.is_licensed;
config.lora.override_duty_cycle = owner.is_licensed; // override duty cycle for licensed operators
2021-04-10 11:39:13 +08:00
}
2021-02-21 14:03:44 +08:00
2023-02-02 14:05:58 -06:00
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);
2022-09-22 08:02:58 -05:00
}
2021-02-21 14:03:44 +08:00
}
void AdminModule::handleSetConfig(const meshtastic_Config &c)
2022-05-02 08:53:44 +10:00
{
2023-01-28 14:32:57 -06:00
auto existingRole = config.device.role;
bool isRegionUnset = (config.lora.region == meshtastic_Config_LoRaConfig_RegionCode_UNSET);
2023-02-02 14:05:58 -06:00
switch (c.which_payload_variant)
{
case meshtastic_Config_device_tag:
LOG_INFO("Setting config: Device\n");
config.has_device = true;
config.device = c.payload_variant.device;
// If we're setting router role for the first time, install its intervals
2023-01-28 14:32:57 -06:00
if (existingRole != c.payload_variant.device.role)
nodeDB.installRoleDefaults(c.payload_variant.device.role);
break;
case meshtastic_Config_position_tag:
LOG_INFO("Setting config: Position\n");
config.has_position = true;
config.position = c.payload_variant.position;
// Save nodedb as well in case we got a fixed position packet
saveChanges(SEGMENT_DEVICESTATE, false);
break;
case meshtastic_Config_power_tag:
LOG_INFO("Setting config: Power\n");
config.has_power = true;
config.power = c.payload_variant.power;
break;
case meshtastic_Config_network_tag:
LOG_INFO("Setting config: WiFi\n");
config.has_network = true;
config.network = c.payload_variant.network;
break;
case meshtastic_Config_display_tag:
LOG_INFO("Setting config: Display\n");
config.has_display = true;
config.display = c.payload_variant.display;
break;
case meshtastic_Config_lora_tag:
LOG_INFO("Setting config: LoRa\n");
config.has_lora = true;
config.lora = c.payload_variant.lora;
2023-02-02 14:05:58 -06:00
if (isRegionUnset && config.lora.region > meshtastic_Config_LoRaConfig_RegionCode_UNSET)
{
config.lora.tx_enabled = true;
}
break;
case meshtastic_Config_bluetooth_tag:
LOG_INFO("Setting config: Bluetooth\n");
config.has_bluetooth = true;
config.bluetooth = c.payload_variant.bluetooth;
break;
2022-05-02 22:00:24 +10:00
}
saveChanges(SEGMENT_CONFIG);
2022-05-02 22:00:24 +10:00
}
void AdminModule::handleSetModuleConfig(const meshtastic_ModuleConfig &c)
2022-05-02 22:00:24 +10:00
{
2023-02-02 14:05:58 -06:00
switch (c.which_payload_variant)
{
case meshtastic_ModuleConfig_mqtt_tag:
LOG_INFO("Setting module config: MQTT\n");
moduleConfig.has_mqtt = true;
moduleConfig.mqtt = c.payload_variant.mqtt;
break;
case meshtastic_ModuleConfig_serial_tag:
LOG_INFO("Setting module config: Serial\n");
moduleConfig.has_serial = true;
moduleConfig.serial = c.payload_variant.serial;
break;
case meshtastic_ModuleConfig_external_notification_tag:
LOG_INFO("Setting module config: External Notification\n");
moduleConfig.has_external_notification = true;
moduleConfig.external_notification = c.payload_variant.external_notification;
break;
case meshtastic_ModuleConfig_store_forward_tag:
LOG_INFO("Setting module config: Store & Forward\n");
moduleConfig.has_store_forward = true;
moduleConfig.store_forward = c.payload_variant.store_forward;
break;
case meshtastic_ModuleConfig_range_test_tag:
LOG_INFO("Setting module config: Range Test\n");
moduleConfig.has_range_test = true;
moduleConfig.range_test = c.payload_variant.range_test;
break;
case meshtastic_ModuleConfig_telemetry_tag:
LOG_INFO("Setting module config: Telemetry\n");
moduleConfig.has_telemetry = true;
moduleConfig.telemetry = c.payload_variant.telemetry;
break;
case meshtastic_ModuleConfig_canned_message_tag:
LOG_INFO("Setting module config: Canned Message\n");
moduleConfig.has_canned_message = true;
moduleConfig.canned_message = c.payload_variant.canned_message;
break;
case meshtastic_ModuleConfig_audio_tag:
LOG_INFO("Setting module config: Audio\n");
moduleConfig.has_audio = true;
moduleConfig.audio = c.payload_variant.audio;
break;
case meshtastic_ModuleConfig_remote_hardware_tag:
LOG_INFO("Setting module config: Remote Hardware\n");
moduleConfig.has_remote_hardware = true;
moduleConfig.remote_hardware = c.payload_variant.remote_hardware;
break;
2022-05-02 08:53:44 +10:00
}
saveChanges(SEGMENT_MODULECONFIG);
2022-05-02 08:53:44 +10:00
}
void AdminModule::handleSetChannel(const meshtastic_Channel &cc)
2022-05-02 22:00:24 +10:00
{
channels.setChannel(cc);
channels.onConfigChanged(); // tell the radios about this change
saveChanges(SEGMENT_CHANNELS, false);
2022-05-02 22:00:24 +10:00
}
/**
2022-05-03 22:16:50 +10:00
* Getters
2022-05-02 22:00:24 +10:00
*/
void AdminModule::handleGetOwner(const meshtastic_MeshPacket &req)
2022-05-02 22:00:24 +10:00
{
2023-02-02 14:05:58 -06:00
if (req.decoded.want_response)
{
2022-05-02 22:00:24 +10:00
// We create the reply here
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
2022-05-03 22:16:50 +10:00
res.get_owner_response = owner;
2022-05-02 22:00:24 +10:00
res.which_payload_variant = meshtastic_AdminMessage_get_owner_response_tag;
2022-05-03 22:16:50 +10:00
myReply = allocDataProtobuf(res);
2022-05-02 22:00:24 +10:00
}
}
void AdminModule::handleGetConfig(const meshtastic_MeshPacket &req, const uint32_t configType)
2022-05-02 22:00:24 +10:00
{
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
2022-05-02 22:00:24 +10:00
2023-02-02 14:05:58 -06:00
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;
res.get_config_response.payload_variant.device = config.device;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_POSITION_CONFIG:
LOG_INFO("Getting config: Position\n");
res.get_config_response.which_payload_variant = meshtastic_Config_position_tag;
res.get_config_response.payload_variant.position = config.position;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_POWER_CONFIG:
LOG_INFO("Getting config: Power\n");
res.get_config_response.which_payload_variant = meshtastic_Config_power_tag;
res.get_config_response.payload_variant.power = config.power;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_NETWORK_CONFIG:
LOG_INFO("Getting config: Network\n");
res.get_config_response.which_payload_variant = meshtastic_Config_network_tag;
res.get_config_response.payload_variant.network = config.network;
writeSecret(res.get_config_response.payload_variant.network.wifi_psk,
sizeof(res.get_config_response.payload_variant.network.wifi_psk), config.network.wifi_psk);
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_DISPLAY_CONFIG:
LOG_INFO("Getting config: Display\n");
res.get_config_response.which_payload_variant = meshtastic_Config_display_tag;
res.get_config_response.payload_variant.display = config.display;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_LORA_CONFIG:
LOG_INFO("Getting config: LoRa\n");
res.get_config_response.which_payload_variant = meshtastic_Config_lora_tag;
res.get_config_response.payload_variant.lora = config.lora;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_BLUETOOTH_CONFIG:
LOG_INFO("Getting config: Bluetooth\n");
res.get_config_response.which_payload_variant = meshtastic_Config_bluetooth_tag;
res.get_config_response.payload_variant.bluetooth = config.bluetooth;
break;
2022-05-02 22:00:24 +10:00
}
// NOTE: The phone app needs to know the ls_secs value so it can properly expect sleep behavior.
2022-05-02 22:00:24 +10:00
// So even if we internally use 0 to represent 'use default' we still need to send the value we are
// using to the app (so that even old phone apps work with new device loads).
// r.get_radio_response.preferences.ls_secs = getPref_ls_secs();
// hideSecret(r.get_radio_response.preferences.wifi_ssid); // hmm - leave public for now, because only minimally private
// and useful for users to know current provisioning) hideSecret(r.get_radio_response.preferences.wifi_password);
2022-05-07 20:31:21 +10:00
// r.get_config_response.which_payloadVariant = Config_ModuleConfig_telemetry_tag;
res.which_payload_variant = meshtastic_AdminMessage_get_config_response_tag;
2022-05-03 22:16:50 +10:00
myReply = allocDataProtobuf(res);
2022-05-02 22:00:24 +10:00
}
}
void AdminModule::handleGetModuleConfig(const meshtastic_MeshPacket &req, const uint32_t configType)
2022-05-02 22:00:24 +10:00
{
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
2022-05-02 22:00:24 +10:00
2023-02-02 14:05:58 -06:00
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;
res.get_module_config_response.payload_variant.mqtt = moduleConfig.mqtt;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_SERIAL_CONFIG:
LOG_INFO("Getting module config: Serial\n");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_serial_tag;
res.get_module_config_response.payload_variant.serial = moduleConfig.serial;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_EXTNOTIF_CONFIG:
LOG_INFO("Getting module config: External Notification\n");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_external_notification_tag;
res.get_module_config_response.payload_variant.external_notification = moduleConfig.external_notification;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_STOREFORWARD_CONFIG:
LOG_INFO("Getting module config: Store & Forward\n");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_store_forward_tag;
res.get_module_config_response.payload_variant.store_forward = moduleConfig.store_forward;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_RANGETEST_CONFIG:
LOG_INFO("Getting module config: Range Test\n");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_range_test_tag;
res.get_module_config_response.payload_variant.range_test = moduleConfig.range_test;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_TELEMETRY_CONFIG:
LOG_INFO("Getting module config: Telemetry\n");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_telemetry_tag;
res.get_module_config_response.payload_variant.telemetry = moduleConfig.telemetry;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_CANNEDMSG_CONFIG:
LOG_INFO("Getting module config: Canned Message\n");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_canned_message_tag;
res.get_module_config_response.payload_variant.canned_message = moduleConfig.canned_message;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_AUDIO_CONFIG:
LOG_INFO("Getting module config: Audio\n");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_audio_tag;
2022-11-08 15:04:24 -06:00
res.get_module_config_response.payload_variant.audio = moduleConfig.audio;
break;
case meshtastic_AdminMessage_ModuleConfigType_REMOTEHARDWARE_CONFIG:
LOG_INFO("Getting module config: Remote Hardware\n");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_remote_hardware_tag;
res.get_module_config_response.payload_variant.remote_hardware = moduleConfig.remote_hardware;
break;
2022-05-02 22:00:24 +10:00
}
// NOTE: The phone app needs to know the ls_secsvalue so it can properly expect sleep behavior.
2022-05-02 22:00:24 +10:00
// So even if we internally use 0 to represent 'use default' we still need to send the value we are
// using to the app (so that even old phone apps work with new device loads).
// r.get_radio_response.preferences.ls_secs = getPref_ls_secs();
// hideSecret(r.get_radio_response.preferences.wifi_ssid); // hmm - leave public for now, because only minimally private
// and useful for users to know current provisioning) hideSecret(r.get_radio_response.preferences.wifi_password);
2022-05-07 20:31:21 +10:00
// r.get_config_response.which_payloadVariant = Config_ModuleConfig_telemetry_tag;
res.which_payload_variant = meshtastic_AdminMessage_get_module_config_response_tag;
2022-05-03 22:16:50 +10:00
myReply = allocDataProtobuf(res);
2022-05-02 22:00:24 +10:00
}
}
void AdminModule::handleGetDeviceMetadata(const meshtastic_MeshPacket &req)
{
meshtastic_AdminMessage r = meshtastic_AdminMessage_init_default;
meshtastic_DeviceMetadata deviceMetadata;
strncpy(deviceMetadata.firmware_version, myNodeInfo.firmware_version, 18);
deviceMetadata.device_state_version = DEVICESTATE_CUR_VER;
deviceMetadata.canShutdown = pmu_found || HAS_CPU_SHUTDOWN;
deviceMetadata.hasBluetooth = HAS_BLUETOOTH;
deviceMetadata.hasWifi = HAS_WIFI;
deviceMetadata.hasEthernet = HAS_ETHERNET;
deviceMetadata.role = config.device.role;
deviceMetadata.position_flags = config.position.position_flags;
2023-02-02 14:05:58 -06:00
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);
}
2023-02-02 14:05:58 -06:00
void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &req)
{
meshtastic_AdminMessage r = meshtastic_AdminMessage_init_default;
2023-02-02 14:11:48 -06:00
meshtastic_DeviceConnectionStatus conn;
2023-02-02 14:05:58 -06:00
#if HAS_WIFI
2023-02-02 14:40:18 -06:00
conn.has_wifi = true;
2023-02-02 14:11:48 -06:00
conn.wifi.status.status.is_connected = WiFi.status() != WL_CONNECTED;
strncpy(conn.wifi.ssid, config.network.wifi_ssid, 33);
if (conn.wifi.status.status.is_connected)
{
2023-02-02 14:40:18 -06:00
conn.wifi.rssi = WiFi.RSSI();
2023-02-02 14:11:48 -06:00
conn.wifi.status.status.ip_address = WiFi.localIP();
conn.wifi.status.status.is_mqtt_connected = mqtt && mqtt->connected();
conn.wifi.status.status.is_syslog_connected = false; // FIXME wire this up
}
2023-02-02 14:05:58 -06:00
#endif
2023-02-02 14:40:18 -06:00
#if HAS_ETHERNET
conn.has_ethernet = true;
// conn.ethernet.
#endif
2023-02-02 14:05:58 -06:00
#if HAS_BLUETOOTH
2023-02-02 14:40:18 -06:00
conn.has_bluetooth = HAS_BLUETOOTH;
// nimbleBluetooth->
// #if ARCH_ESP32
2023-02-02 14:05:58 -06:00
2023-02-02 14:40:18 -06:00
// #elif
2023-02-02 14:05:58 -06:00
#endif
2023-02-02 14:11:48 -06:00
conn.has_serial = true; // No serial-less devices
2023-02-02 14:05:58 -06:00
2023-02-02 14:11:48 -06:00
r.get_device_connection_status_response = conn;
2023-02-02 14:05:58 -06:00
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)
2022-05-02 22:00:24 +10:00
{
2023-02-02 14:05:58 -06:00
if (req.decoded.want_response)
{
2022-05-02 22:00:24 +10:00
// We create the reply here
meshtastic_AdminMessage r = meshtastic_AdminMessage_init_default;
2022-05-02 22:00:24 +10:00
r.get_channel_response = channels.getByIndex(channelIndex);
r.which_payload_variant = meshtastic_AdminMessage_get_channel_response_tag;
2022-05-02 22:00:24 +10:00
myReply = allocDataProtobuf(r);
}
}
void AdminModule::reboot(int32_t seconds)
2022-10-19 19:19:04 -05:00
{
LOG_INFO("Rebooting in %d seconds\n", seconds);
2022-10-19 19:19:04 -05:00
screen->startRebootScreen();
rebootAtMsec = (seconds < 0) ? 0 : (millis() + seconds * 1000);
}
void AdminModule::saveChanges(int saveWhat, bool shouldReboot)
{
2023-02-02 14:05:58 -06:00
if (!hasOpenEditTransaction)
{
LOG_INFO("Saving changes to disk\n");
service.reloadConfig(saveWhat); // Calls saveToDisk among other things
2023-02-02 14:05:58 -06:00
}
else
{
LOG_INFO("Delaying save of changes to disk until the open transaction is committed\n");
}
2023-02-02 14:05:58 -06:00
if (shouldReboot)
{
reboot(DEFAULT_REBOOT_SECONDS);
}
}
AdminModule::AdminModule() : ProtobufModule("Admin", meshtastic_PortNum_ADMIN_APP, &meshtastic_AdminMessage_msg)
2021-02-26 15:34:00 +08:00
{
// restrict to the admin channel for rx
2021-03-13 13:14:27 +08:00
boundChannel = Channels::adminChannel;
2021-02-21 14:03:44 +08:00
}