From e8e04d23d75bff8fcb895cfcf18e31f4a7e1de1c Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 2 Feb 2023 14:05:58 -0600 Subject: [PATCH 01/21] WIP --- platformio.ini | 2 +- protobufs | 2 +- src/mesh/generated/meshtastic/admin.pb.h | 10 + .../meshtastic/connection_status.pb.c | 27 +++ .../meshtastic/connection_status.pb.h | 189 ++++++++++++++++++ .../generated/meshtastic/device_metadata.pb.h | 13 +- src/mesh/generated/meshtastic/deviceonly.pb.h | 21 +- src/modules/AdminModule.cpp | 133 ++++++++---- src/modules/AdminModule.h | 65 +++--- 9 files changed, 387 insertions(+), 75 deletions(-) create mode 100644 src/mesh/generated/meshtastic/connection_status.pb.c create mode 100644 src/mesh/generated/meshtastic/connection_status.pb.h diff --git a/platformio.ini b/platformio.ini index 1d8083f70..0f3e4cc4d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -2,7 +2,7 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -;default_envs = tbeam +default_envs = tbeam ;default_envs = pico ;default_envs = tbeam-s3-core ;default_envs = tbeam0.7 diff --git a/protobufs b/protobufs index b9953ff48..4ef7e46a8 160000 --- a/protobufs +++ b/protobufs @@ -1 +1 @@ -Subproject commit b9953ff48b81a06e062e35ff3c2ea6de862ee144 +Subproject commit 4ef7e46a8b83d4d2147f027c7aaa7d1b481f8688 diff --git a/src/mesh/generated/meshtastic/admin.pb.h b/src/mesh/generated/meshtastic/admin.pb.h index c47fb886f..e35b79a57 100644 --- a/src/mesh/generated/meshtastic/admin.pb.h +++ b/src/mesh/generated/meshtastic/admin.pb.h @@ -9,6 +9,7 @@ #include "meshtastic/device_metadata.pb.h" #include "meshtastic/mesh.pb.h" #include "meshtastic/module_config.pb.h" +#include "meshtastic/connection_status.pb.h" #if PB_PROTO_HEADER_VERSION != 40 #error Regenerate this file with the current version of nanopb generator. @@ -91,6 +92,10 @@ typedef struct _meshtastic_AdminMessage { bool get_ringtone_request; /* Get the Ringtone in the response to this message. */ char get_ringtone_response[231]; + /* Request the node to send it's connection status */ + bool get_device_connection_status_request; + /* Device connection status response */ + meshtastic_DeviceConnectionStatus get_device_connection_status_response; /* Set the owner for this node */ meshtastic_User set_owner; /* Set channels (using the new API). @@ -166,6 +171,8 @@ extern "C" { #define meshtastic_AdminMessage_get_device_metadata_response_tag 13 #define meshtastic_AdminMessage_get_ringtone_request_tag 14 #define meshtastic_AdminMessage_get_ringtone_response_tag 15 +#define meshtastic_AdminMessage_get_device_connection_status_request_tag 16 +#define meshtastic_AdminMessage_get_device_connection_status_response_tag 17 #define meshtastic_AdminMessage_set_owner_tag 32 #define meshtastic_AdminMessage_set_channel_tag 33 #define meshtastic_AdminMessage_set_config_tag 34 @@ -197,6 +204,8 @@ X(a, STATIC, ONEOF, BOOL, (payload_variant,get_device_metadata_request, X(a, STATIC, ONEOF, MESSAGE, (payload_variant,get_device_metadata_response,get_device_metadata_response), 13) \ X(a, STATIC, ONEOF, BOOL, (payload_variant,get_ringtone_request,get_ringtone_request), 14) \ X(a, STATIC, ONEOF, STRING, (payload_variant,get_ringtone_response,get_ringtone_response), 15) \ +X(a, STATIC, ONEOF, BOOL, (payload_variant,get_device_connection_status_request,get_device_connection_status_request), 16) \ +X(a, STATIC, ONEOF, MESSAGE, (payload_variant,get_device_connection_status_response,get_device_connection_status_response), 17) \ X(a, STATIC, ONEOF, MESSAGE, (payload_variant,set_owner,set_owner), 32) \ X(a, STATIC, ONEOF, MESSAGE, (payload_variant,set_channel,set_channel), 33) \ X(a, STATIC, ONEOF, MESSAGE, (payload_variant,set_config,set_config), 34) \ @@ -218,6 +227,7 @@ X(a, STATIC, ONEOF, INT32, (payload_variant,nodedb_reset,nodedb_reset), #define meshtastic_AdminMessage_payload_variant_get_config_response_MSGTYPE meshtastic_Config #define meshtastic_AdminMessage_payload_variant_get_module_config_response_MSGTYPE meshtastic_ModuleConfig #define meshtastic_AdminMessage_payload_variant_get_device_metadata_response_MSGTYPE meshtastic_DeviceMetadata +#define meshtastic_AdminMessage_payload_variant_get_device_connection_status_response_MSGTYPE meshtastic_DeviceConnectionStatus #define meshtastic_AdminMessage_payload_variant_set_owner_MSGTYPE meshtastic_User #define meshtastic_AdminMessage_payload_variant_set_channel_MSGTYPE meshtastic_Channel #define meshtastic_AdminMessage_payload_variant_set_config_MSGTYPE meshtastic_Config diff --git a/src/mesh/generated/meshtastic/connection_status.pb.c b/src/mesh/generated/meshtastic/connection_status.pb.c new file mode 100644 index 000000000..0675bc815 --- /dev/null +++ b/src/mesh/generated/meshtastic/connection_status.pb.c @@ -0,0 +1,27 @@ +/* Automatically generated nanopb constant definitions */ +/* Generated by nanopb-0.4.7 */ + +#include "meshtastic/connection_status.pb.h" +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +PB_BIND(meshtastic_DeviceConnectionStatus, meshtastic_DeviceConnectionStatus, AUTO) + + +PB_BIND(meshtastic_WifiConnectionStatus, meshtastic_WifiConnectionStatus, AUTO) + + +PB_BIND(meshtastic_EthernetConnectionStatus, meshtastic_EthernetConnectionStatus, AUTO) + + +PB_BIND(meshtastic_NetworkConnectionStatus, meshtastic_NetworkConnectionStatus, AUTO) + + +PB_BIND(meshtastic_BluetoothConnectionStatus, meshtastic_BluetoothConnectionStatus, AUTO) + + +PB_BIND(meshtastic_SerialConnectionStatus, meshtastic_SerialConnectionStatus, AUTO) + + + diff --git a/src/mesh/generated/meshtastic/connection_status.pb.h b/src/mesh/generated/meshtastic/connection_status.pb.h new file mode 100644 index 000000000..c13ec2bc6 --- /dev/null +++ b/src/mesh/generated/meshtastic/connection_status.pb.h @@ -0,0 +1,189 @@ +/* Automatically generated nanopb header */ +/* Generated by nanopb-0.4.7 */ + +#ifndef PB_MESHTASTIC_MESHTASTIC_CONNECTION_STATUS_PB_H_INCLUDED +#define PB_MESHTASTIC_MESHTASTIC_CONNECTION_STATUS_PB_H_INCLUDED +#include + +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +/* Struct definitions */ +/* Ethernet or WiFi connection status */ +typedef struct _meshtastic_NetworkConnectionStatus { + /* IP address of device */ + uint32_t ip_address; + /* Whether the device has an active connection or not */ + bool is_connected; + /* Whether the device has an active connection to an MQTT broker or not */ + bool is_mqtt_connected; + /* Whether the device is actively remote syslogging or not */ + bool is_syslog_connected; +} meshtastic_NetworkConnectionStatus; + +/* Ethernet connection status */ +typedef struct _meshtastic_EthernetConnectionStatus { + /* Connection status */ + bool has_status; + meshtastic_NetworkConnectionStatus status; +} meshtastic_EthernetConnectionStatus; + +/* WiFi connection status */ +typedef struct _meshtastic_WifiConnectionStatus { + /* Connection status */ + bool has_status; + meshtastic_EthernetConnectionStatus status; + /* WiFi access point ssid */ + char ssid[33]; + /* Rssi of wireless connection */ + int32_t rssi; +} meshtastic_WifiConnectionStatus; + +/* Bluetooth connection status */ +typedef struct _meshtastic_BluetoothConnectionStatus { + /* The pairing pin for bluetooth */ + uint32_t pin; + /* Rssi of bluetooth connection */ + int32_t rssi; + /* Whether the device has an active connection or not */ + bool is_connected; +} meshtastic_BluetoothConnectionStatus; + +/* Serial connection status */ +typedef struct _meshtastic_SerialConnectionStatus { + /* The serial baud rate */ + uint32_t baud; + /* Whether the device has an active connection or not */ + bool is_connected; +} meshtastic_SerialConnectionStatus; + +typedef struct _meshtastic_DeviceConnectionStatus { + /* WiFi Status */ + bool has_wifi; + meshtastic_WifiConnectionStatus wifi; + /* WiFi Status */ + bool has_ethernet; + meshtastic_EthernetConnectionStatus ethernet; + /* Bluetooth Status */ + bool has_bluetooth; + meshtastic_BluetoothConnectionStatus bluetooth; + /* Serial Status */ + bool has_serial; + meshtastic_SerialConnectionStatus serial; +} meshtastic_DeviceConnectionStatus; + + +#ifdef __cplusplus +extern "C" { +#endif + +/* Initializer values for message structs */ +#define meshtastic_DeviceConnectionStatus_init_default {false, meshtastic_WifiConnectionStatus_init_default, false, meshtastic_EthernetConnectionStatus_init_default, false, meshtastic_BluetoothConnectionStatus_init_default, false, meshtastic_SerialConnectionStatus_init_default} +#define meshtastic_WifiConnectionStatus_init_default {false, meshtastic_EthernetConnectionStatus_init_default, "", 0} +#define meshtastic_EthernetConnectionStatus_init_default {false, meshtastic_NetworkConnectionStatus_init_default} +#define meshtastic_NetworkConnectionStatus_init_default {0, 0, 0, 0} +#define meshtastic_BluetoothConnectionStatus_init_default {0, 0, 0} +#define meshtastic_SerialConnectionStatus_init_default {0, 0} +#define meshtastic_DeviceConnectionStatus_init_zero {false, meshtastic_WifiConnectionStatus_init_zero, false, meshtastic_EthernetConnectionStatus_init_zero, false, meshtastic_BluetoothConnectionStatus_init_zero, false, meshtastic_SerialConnectionStatus_init_zero} +#define meshtastic_WifiConnectionStatus_init_zero {false, meshtastic_EthernetConnectionStatus_init_zero, "", 0} +#define meshtastic_EthernetConnectionStatus_init_zero {false, meshtastic_NetworkConnectionStatus_init_zero} +#define meshtastic_NetworkConnectionStatus_init_zero {0, 0, 0, 0} +#define meshtastic_BluetoothConnectionStatus_init_zero {0, 0, 0} +#define meshtastic_SerialConnectionStatus_init_zero {0, 0} + +/* Field tags (for use in manual encoding/decoding) */ +#define meshtastic_NetworkConnectionStatus_ip_address_tag 1 +#define meshtastic_NetworkConnectionStatus_is_connected_tag 2 +#define meshtastic_NetworkConnectionStatus_is_mqtt_connected_tag 3 +#define meshtastic_NetworkConnectionStatus_is_syslog_connected_tag 4 +#define meshtastic_EthernetConnectionStatus_status_tag 1 +#define meshtastic_WifiConnectionStatus_status_tag 1 +#define meshtastic_WifiConnectionStatus_ssid_tag 2 +#define meshtastic_WifiConnectionStatus_rssi_tag 3 +#define meshtastic_BluetoothConnectionStatus_pin_tag 1 +#define meshtastic_BluetoothConnectionStatus_rssi_tag 2 +#define meshtastic_BluetoothConnectionStatus_is_connected_tag 3 +#define meshtastic_SerialConnectionStatus_baud_tag 1 +#define meshtastic_SerialConnectionStatus_is_connected_tag 2 +#define meshtastic_DeviceConnectionStatus_wifi_tag 1 +#define meshtastic_DeviceConnectionStatus_ethernet_tag 2 +#define meshtastic_DeviceConnectionStatus_bluetooth_tag 3 +#define meshtastic_DeviceConnectionStatus_serial_tag 4 + +/* Struct field encoding specification for nanopb */ +#define meshtastic_DeviceConnectionStatus_FIELDLIST(X, a) \ +X(a, STATIC, OPTIONAL, MESSAGE, wifi, 1) \ +X(a, STATIC, OPTIONAL, MESSAGE, ethernet, 2) \ +X(a, STATIC, OPTIONAL, MESSAGE, bluetooth, 3) \ +X(a, STATIC, OPTIONAL, MESSAGE, serial, 4) +#define meshtastic_DeviceConnectionStatus_CALLBACK NULL +#define meshtastic_DeviceConnectionStatus_DEFAULT NULL +#define meshtastic_DeviceConnectionStatus_wifi_MSGTYPE meshtastic_WifiConnectionStatus +#define meshtastic_DeviceConnectionStatus_ethernet_MSGTYPE meshtastic_EthernetConnectionStatus +#define meshtastic_DeviceConnectionStatus_bluetooth_MSGTYPE meshtastic_BluetoothConnectionStatus +#define meshtastic_DeviceConnectionStatus_serial_MSGTYPE meshtastic_SerialConnectionStatus + +#define meshtastic_WifiConnectionStatus_FIELDLIST(X, a) \ +X(a, STATIC, OPTIONAL, MESSAGE, status, 1) \ +X(a, STATIC, SINGULAR, STRING, ssid, 2) \ +X(a, STATIC, SINGULAR, INT32, rssi, 3) +#define meshtastic_WifiConnectionStatus_CALLBACK NULL +#define meshtastic_WifiConnectionStatus_DEFAULT NULL +#define meshtastic_WifiConnectionStatus_status_MSGTYPE meshtastic_EthernetConnectionStatus + +#define meshtastic_EthernetConnectionStatus_FIELDLIST(X, a) \ +X(a, STATIC, OPTIONAL, MESSAGE, status, 1) +#define meshtastic_EthernetConnectionStatus_CALLBACK NULL +#define meshtastic_EthernetConnectionStatus_DEFAULT NULL +#define meshtastic_EthernetConnectionStatus_status_MSGTYPE meshtastic_NetworkConnectionStatus + +#define meshtastic_NetworkConnectionStatus_FIELDLIST(X, a) \ +X(a, STATIC, SINGULAR, FIXED32, ip_address, 1) \ +X(a, STATIC, SINGULAR, BOOL, is_connected, 2) \ +X(a, STATIC, SINGULAR, BOOL, is_mqtt_connected, 3) \ +X(a, STATIC, SINGULAR, BOOL, is_syslog_connected, 4) +#define meshtastic_NetworkConnectionStatus_CALLBACK NULL +#define meshtastic_NetworkConnectionStatus_DEFAULT NULL + +#define meshtastic_BluetoothConnectionStatus_FIELDLIST(X, a) \ +X(a, STATIC, SINGULAR, UINT32, pin, 1) \ +X(a, STATIC, SINGULAR, INT32, rssi, 2) \ +X(a, STATIC, SINGULAR, BOOL, is_connected, 3) +#define meshtastic_BluetoothConnectionStatus_CALLBACK NULL +#define meshtastic_BluetoothConnectionStatus_DEFAULT NULL + +#define meshtastic_SerialConnectionStatus_FIELDLIST(X, a) \ +X(a, STATIC, SINGULAR, UINT32, baud, 1) \ +X(a, STATIC, SINGULAR, BOOL, is_connected, 2) +#define meshtastic_SerialConnectionStatus_CALLBACK NULL +#define meshtastic_SerialConnectionStatus_DEFAULT NULL + +extern const pb_msgdesc_t meshtastic_DeviceConnectionStatus_msg; +extern const pb_msgdesc_t meshtastic_WifiConnectionStatus_msg; +extern const pb_msgdesc_t meshtastic_EthernetConnectionStatus_msg; +extern const pb_msgdesc_t meshtastic_NetworkConnectionStatus_msg; +extern const pb_msgdesc_t meshtastic_BluetoothConnectionStatus_msg; +extern const pb_msgdesc_t meshtastic_SerialConnectionStatus_msg; + +/* Defines for backwards compatibility with code written before nanopb-0.4.0 */ +#define meshtastic_DeviceConnectionStatus_fields &meshtastic_DeviceConnectionStatus_msg +#define meshtastic_WifiConnectionStatus_fields &meshtastic_WifiConnectionStatus_msg +#define meshtastic_EthernetConnectionStatus_fields &meshtastic_EthernetConnectionStatus_msg +#define meshtastic_NetworkConnectionStatus_fields &meshtastic_NetworkConnectionStatus_msg +#define meshtastic_BluetoothConnectionStatus_fields &meshtastic_BluetoothConnectionStatus_msg +#define meshtastic_SerialConnectionStatus_fields &meshtastic_SerialConnectionStatus_msg + +/* Maximum encoded size of messages (where known) */ +#define meshtastic_BluetoothConnectionStatus_size 19 +#define meshtastic_DeviceConnectionStatus_size 108 +#define meshtastic_EthernetConnectionStatus_size 13 +#define meshtastic_NetworkConnectionStatus_size 11 +#define meshtastic_SerialConnectionStatus_size 8 +#define meshtastic_WifiConnectionStatus_size 60 + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/src/mesh/generated/meshtastic/device_metadata.pb.h b/src/mesh/generated/meshtastic/device_metadata.pb.h index 384863601..44cf29079 100644 --- a/src/mesh/generated/meshtastic/device_metadata.pb.h +++ b/src/mesh/generated/meshtastic/device_metadata.pb.h @@ -5,6 +5,7 @@ #define PB_MESHTASTIC_MESHTASTIC_DEVICE_METADATA_PB_H_INCLUDED #include #include "meshtastic/config.pb.h" +#include "meshtastic/mesh.pb.h" #if PB_PROTO_HEADER_VERSION != 40 #error Regenerate this file with the current version of nanopb generator. @@ -29,6 +30,8 @@ typedef struct _meshtastic_DeviceMetadata { meshtastic_Config_DeviceConfig_Role role; /* Indicates the device's current enabled position flags */ uint32_t position_flags; + /* Device hardware model */ + meshtastic_HardwareModel hw_model; } meshtastic_DeviceMetadata; @@ -37,8 +40,8 @@ extern "C" { #endif /* Initializer values for message structs */ -#define meshtastic_DeviceMetadata_init_default {"", 0, 0, 0, 0, 0, _meshtastic_Config_DeviceConfig_Role_MIN, 0} -#define meshtastic_DeviceMetadata_init_zero {"", 0, 0, 0, 0, 0, _meshtastic_Config_DeviceConfig_Role_MIN, 0} +#define meshtastic_DeviceMetadata_init_default {"", 0, 0, 0, 0, 0, _meshtastic_Config_DeviceConfig_Role_MIN, 0, _meshtastic_HardwareModel_MIN} +#define meshtastic_DeviceMetadata_init_zero {"", 0, 0, 0, 0, 0, _meshtastic_Config_DeviceConfig_Role_MIN, 0, _meshtastic_HardwareModel_MIN} /* Field tags (for use in manual encoding/decoding) */ #define meshtastic_DeviceMetadata_firmware_version_tag 1 @@ -49,6 +52,7 @@ extern "C" { #define meshtastic_DeviceMetadata_hasEthernet_tag 6 #define meshtastic_DeviceMetadata_role_tag 7 #define meshtastic_DeviceMetadata_position_flags_tag 8 +#define meshtastic_DeviceMetadata_hw_model_tag 9 /* Struct field encoding specification for nanopb */ #define meshtastic_DeviceMetadata_FIELDLIST(X, a) \ @@ -59,7 +63,8 @@ X(a, STATIC, SINGULAR, BOOL, hasWifi, 4) \ X(a, STATIC, SINGULAR, BOOL, hasBluetooth, 5) \ X(a, STATIC, SINGULAR, BOOL, hasEthernet, 6) \ X(a, STATIC, SINGULAR, UENUM, role, 7) \ -X(a, STATIC, SINGULAR, UINT32, position_flags, 8) +X(a, STATIC, SINGULAR, UINT32, position_flags, 8) \ +X(a, STATIC, SINGULAR, UENUM, hw_model, 9) #define meshtastic_DeviceMetadata_CALLBACK NULL #define meshtastic_DeviceMetadata_DEFAULT NULL @@ -69,7 +74,7 @@ extern const pb_msgdesc_t meshtastic_DeviceMetadata_msg; #define meshtastic_DeviceMetadata_fields &meshtastic_DeviceMetadata_msg /* Maximum encoded size of messages (where known) */ -#define meshtastic_DeviceMetadata_size 41 +#define meshtastic_DeviceMetadata_size 44 #ifdef __cplusplus } /* extern "C" */ diff --git a/src/mesh/generated/meshtastic/deviceonly.pb.h b/src/mesh/generated/meshtastic/deviceonly.pb.h index 842431b28..aef8ee8be 100644 --- a/src/mesh/generated/meshtastic/deviceonly.pb.h +++ b/src/mesh/generated/meshtastic/deviceonly.pb.h @@ -5,6 +5,7 @@ #define PB_MESHTASTIC_MESHTASTIC_DEVICEONLY_PB_H_INCLUDED #include #include "meshtastic/channel.pb.h" +#include "meshtastic/localonly.pb.h" #include "meshtastic/mesh.pb.h" #if PB_PROTO_HEADER_VERSION != 40 @@ -85,6 +86,12 @@ typedef struct _meshtastic_OEMStore { char oem_text[40]; /* The default device encryption key, 16 or 32 byte */ meshtastic_OEMStore_oem_aes_key_t oem_aes_key; + /* A Preset LocalConfig to apply during factory reset */ + bool has_oem_local_config; + meshtastic_LocalConfig oem_local_config; + /* A Preset LocalModuleConfig to apply during factory reset */ + bool has_oem_local_module_config; + meshtastic_LocalModuleConfig oem_local_module_config; } meshtastic_OEMStore; @@ -105,10 +112,10 @@ extern "C" { /* Initializer values for message structs */ #define meshtastic_DeviceState_init_default {false, meshtastic_MyNodeInfo_init_default, false, meshtastic_User_init_default, 0, {meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default, meshtastic_NodeInfo_init_default}, 0, {meshtastic_MeshPacket_init_default}, false, meshtastic_MeshPacket_init_default, 0, 0, 0} #define meshtastic_ChannelFile_init_default {0, {meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default}, 0} -#define meshtastic_OEMStore_init_default {0, 0, {0, {0}}, _meshtastic_ScreenFonts_MIN, "", {0, {0}}} +#define meshtastic_OEMStore_init_default {0, 0, {0, {0}}, _meshtastic_ScreenFonts_MIN, "", {0, {0}}, false, meshtastic_LocalConfig_init_default, false, meshtastic_LocalModuleConfig_init_default} #define meshtastic_DeviceState_init_zero {false, meshtastic_MyNodeInfo_init_zero, false, meshtastic_User_init_zero, 0, {meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero, meshtastic_NodeInfo_init_zero}, 0, {meshtastic_MeshPacket_init_zero}, false, meshtastic_MeshPacket_init_zero, 0, 0, 0} #define meshtastic_ChannelFile_init_zero {0, {meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero}, 0} -#define meshtastic_OEMStore_init_zero {0, 0, {0, {0}}, _meshtastic_ScreenFonts_MIN, "", {0, {0}}} +#define meshtastic_OEMStore_init_zero {0, 0, {0, {0}}, _meshtastic_ScreenFonts_MIN, "", {0, {0}}, false, meshtastic_LocalConfig_init_zero, false, meshtastic_LocalModuleConfig_init_zero} /* Field tags (for use in manual encoding/decoding) */ #define meshtastic_DeviceState_my_node_tag 2 @@ -127,6 +134,8 @@ extern "C" { #define meshtastic_OEMStore_oem_font_tag 4 #define meshtastic_OEMStore_oem_text_tag 5 #define meshtastic_OEMStore_oem_aes_key_tag 6 +#define meshtastic_OEMStore_oem_local_config_tag 7 +#define meshtastic_OEMStore_oem_local_module_config_tag 8 /* Struct field encoding specification for nanopb */ #define meshtastic_DeviceState_FIELDLIST(X, a) \ @@ -159,9 +168,13 @@ X(a, STATIC, SINGULAR, UINT32, oem_icon_height, 2) \ X(a, STATIC, SINGULAR, BYTES, oem_icon_bits, 3) \ X(a, STATIC, SINGULAR, UENUM, oem_font, 4) \ X(a, STATIC, SINGULAR, STRING, oem_text, 5) \ -X(a, STATIC, SINGULAR, BYTES, oem_aes_key, 6) +X(a, STATIC, SINGULAR, BYTES, oem_aes_key, 6) \ +X(a, STATIC, OPTIONAL, MESSAGE, oem_local_config, 7) \ +X(a, STATIC, OPTIONAL, MESSAGE, oem_local_module_config, 8) #define meshtastic_OEMStore_CALLBACK NULL #define meshtastic_OEMStore_DEFAULT NULL +#define meshtastic_OEMStore_oem_local_config_MSGTYPE meshtastic_LocalConfig +#define meshtastic_OEMStore_oem_local_module_config_MSGTYPE meshtastic_LocalModuleConfig extern const pb_msgdesc_t meshtastic_DeviceState_msg; extern const pb_msgdesc_t meshtastic_ChannelFile_msg; @@ -175,7 +188,7 @@ extern const pb_msgdesc_t meshtastic_OEMStore_msg; /* Maximum encoded size of messages (where known) */ #define meshtastic_ChannelFile_size 638 #define meshtastic_DeviceState_size 21800 -#define meshtastic_OEMStore_size 2140 +#define meshtastic_OEMStore_size 2992 #ifdef __cplusplus } /* extern "C" */ diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 18557f0b3..4495e397f 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -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); } } diff --git a/src/modules/AdminModule.h b/src/modules/AdminModule.h index a0602b140..6924bfd7e 100644 --- a/src/modules/AdminModule.h +++ b/src/modules/AdminModule.h @@ -1,46 +1,49 @@ #pragma once #include "ProtobufModule.h" +#ifdef ARCH_ESP32 +#include "mesh/http/WiFiAPClient.h" +#endif /** * Admin module for admin messages */ class AdminModule : public ProtobufModule { - public: - /** Constructor - * name is for debugging output - */ - AdminModule(); +public: + /** Constructor + * name is for debugging output + */ + AdminModule(); - protected: - /** Called to handle a particular incoming message +protected: + /** Called to handle a particular incoming message - @return true if you've guaranteed you've handled this message and no other handlers should be considered for it - */ - virtual bool handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_AdminMessage *p) override; + @return true if you've guaranteed you've handled this message and no other handlers should be considered for it + */ + virtual bool handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_AdminMessage *p) override; - private: - bool hasOpenEditTransaction = false; +private: + bool hasOpenEditTransaction = false; - void saveChanges(int saveWhat, bool shouldReboot = true); - /** - * Getters - */ - void handleGetOwner(const meshtastic_MeshPacket &req); - void handleGetConfig(const meshtastic_MeshPacket &req, uint32_t configType); - void handleGetModuleConfig(const meshtastic_MeshPacket &req, uint32_t configType); - void handleGetChannel(const meshtastic_MeshPacket &req, uint32_t channelIndex); - void handleGetDeviceMetadata(const meshtastic_MeshPacket &req); - - /** - * Setters - */ - void handleSetOwner(const meshtastic_User &o); - void handleSetChannel(const meshtastic_Channel &cc); - void handleSetConfig(const meshtastic_Config &c); - void handleSetModuleConfig(const meshtastic_ModuleConfig &c); - void handleSetChannel(); - void reboot(int32_t seconds); + void saveChanges(int saveWhat, bool shouldReboot = true); + /** + * Getters + */ + void handleGetOwner(const meshtastic_MeshPacket &req); + void handleGetConfig(const meshtastic_MeshPacket &req, uint32_t configType); + void handleGetModuleConfig(const meshtastic_MeshPacket &req, uint32_t configType); + void handleGetChannel(const meshtastic_MeshPacket &req, uint32_t channelIndex); + void handleGetDeviceMetadata(const meshtastic_MeshPacket &req); + void handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &req); + /** + * Setters + */ + void handleSetOwner(const meshtastic_User &o); + void handleSetChannel(const meshtastic_Channel &cc); + void handleSetConfig(const meshtastic_Config &c); + void handleSetModuleConfig(const meshtastic_ModuleConfig &c); + void handleSetChannel(); + void reboot(int32_t seconds); }; extern AdminModule *adminModule; From 971ecd117c66b400ab914aa461d662861f9aa421 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 2 Feb 2023 14:11:48 -0600 Subject: [PATCH 02/21] Whoooosh before my battery dies --- src/modules/AdminModule.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 4495e397f..1df8d5ff4 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -13,6 +13,10 @@ #include "unistd.h" #endif +#if HAS_WIFI || HAS_ETHERNET +#include "mqtt/MQTT.h" +#endif + #define DEFAULT_REBOOT_SECONDS 5 AdminModule *adminModule; @@ -531,24 +535,29 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r { meshtastic_AdminMessage r = meshtastic_AdminMessage_init_default; - meshtastic_DeviceConnectionStatus connectionStatus; + meshtastic_DeviceConnectionStatus conn; - connectionStatus.has_wifi = HAS_WIFI; + conn.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 + 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) + { + 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 + } #endif - connectionStatus.has_bluetooth = HAS_BLUETOOTH; + conn.has_bluetooth = HAS_BLUETOOTH; #if HAS_BLUETOOTH #endif - connectionStatus.has_ethernet = HAS_ETHERNET; + conn.has_ethernet = HAS_ETHERNET; - connectionStatus.has_serial = true; // No serial-less devices + conn.has_serial = true; // No serial-less devices - r.get_device_connection_status_response = connectionStatus; + r.get_device_connection_status_response = conn; r.which_payload_variant = meshtastic_AdminMessage_get_device_connection_status_response_tag; myReply = allocDataProtobuf(r); } From a8dd497575a567fda9db3ee9ad368eccb636dae2 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 2 Feb 2023 14:40:18 -0600 Subject: [PATCH 03/21] Contain it --- src/modules/AdminModule.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 1df8d5ff4..16dee49a9 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -537,23 +537,31 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r meshtastic_DeviceConnectionStatus conn; - conn.has_wifi = HAS_WIFI; #if HAS_WIFI + conn.has_wifi = true; 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) { + conn.wifi.rssi = WiFi.RSSI(); 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 } #endif - conn.has_bluetooth = HAS_BLUETOOTH; -#if HAS_BLUETOOTH - +#if HAS_ETHERNET + conn.has_ethernet = true; + // conn.ethernet. +#endif + +#if HAS_BLUETOOTH + conn.has_bluetooth = HAS_BLUETOOTH; + // nimbleBluetooth-> + // #if ARCH_ESP32 + + // #elif #endif - conn.has_ethernet = HAS_ETHERNET; conn.has_serial = true; // No serial-less devices From d9031610ab00d35757f080eaeed8b1164595585a Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 08:50:10 -0600 Subject: [PATCH 04/21] Connection status admin message --- src/BluetoothCommon.h | 12 ++- src/main.cpp | 6 ++ src/main.h | 8 ++ src/modules/AdminModule.cpp | 140 +++++++++++--------------- src/modules/AdminModule.h | 62 ++++++------ src/nimble/NimbleBluetooth.cpp | 17 +++- src/nimble/NimbleBluetooth.h | 5 +- src/platform/esp32/main-esp32.cpp | 2 - src/platform/nrf52/NRF52Bluetooth.cpp | 10 ++ src/platform/nrf52/NRF52Bluetooth.h | 5 +- src/platform/nrf52/main-nrf52.cpp | 5 +- 11 files changed, 149 insertions(+), 123 deletions(-) diff --git a/src/BluetoothCommon.h b/src/BluetoothCommon.h index 4352eba13..586ffaa3c 100644 --- a/src/BluetoothCommon.h +++ b/src/BluetoothCommon.h @@ -17,4 +17,14 @@ extern const uint8_t MESH_SERVICE_UUID_16[], TORADIO_UUID_16[16u], FROMRADIO_UUID_16[], FROMNUM_UUID_16[]; /// Given a level between 0-100, update the BLE attribute -void updateBatteryLevel(uint8_t level); \ No newline at end of file +void updateBatteryLevel(uint8_t level); + +class BluetoothApi +{ + public: + virtual void setup(); + virtual void shutdown(); + virtual void clearBonds(); + virtual bool isConnected(); + virtual int getRssi() = 0; +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4f65ff51b..49963a415 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,12 @@ #ifdef ARCH_ESP32 #include "mesh/http/WebServer.h" #include "nimble/NimbleBluetooth.h" +NimbleBluetooth *nimbleBluetooth; +#endif + +#ifdef ARCH_NRF52 +#include "NRF52Bluetooth.h" +NRF52Bluetooth *nrf52Bluetooth; #endif #if HAS_WIFI diff --git a/src/main.h b/src/main.h index 9d965b0fc..b1bee1b06 100644 --- a/src/main.h +++ b/src/main.h @@ -9,6 +9,14 @@ #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) #include #endif +#if defined(ARCH_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32S2) +#include "nimble/NimbleBluetooth.h" +extern NimbleBluetooth *nimbleBluetooth; +#endif +#ifdef ARCH_NRF52 +#include "NRF52Bluetooth.h" +extern NRF52Bluetooth *nrf52Bluetooth; +#endif extern uint8_t screen_found; extern uint8_t screen_model; diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 16dee49a9..7beea8804 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -2,13 +2,16 @@ #include "Channels.h" #include "MeshService.h" #include "NodeDB.h" +#include "PowerFSM.h" #ifdef ARCH_ESP32 #include "BleOta.h" #endif #include "Router.h" #include "configuration.h" #include "main.h" - +#ifdef ARCH_NRF52 +#include "main.h" +#endif #ifdef ARCH_PORTDUINO #include "unistd.h" #endif @@ -30,8 +33,7 @@ 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); } } @@ -49,8 +51,7 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta bool handled = false; assert(r); - switch (r->which_payload_variant) - { + switch (r->which_payload_variant) { /** * Getters @@ -70,8 +71,7 @@ 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) @@ -110,22 +110,17 @@ 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); @@ -137,46 +132,45 @@ 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); break; } + case meshtastic_AdminMessage_get_device_connection_status_request_tag: { + LOG_INFO("Client is getting device connection status\n"); + handleGetDeviceConnectionStatus(mp); + break; + } #ifdef ARCH_PORTDUINO case meshtastic_AdminMessage_exit_simulator_tag: LOG_INFO("Exiting simulator\n"); @@ -188,16 +182,11 @@ 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); } @@ -205,8 +194,7 @@ 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); } @@ -222,31 +210,26 @@ 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); } @@ -257,8 +240,7 @@ 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; @@ -293,8 +275,7 @@ 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; @@ -310,8 +291,7 @@ 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; @@ -375,8 +355,7 @@ 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; @@ -390,10 +369,8 @@ 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; @@ -448,10 +425,8 @@ 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; @@ -541,8 +516,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.has_wifi = true; 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) - { + if (conn.wifi.status.status.is_connected) { conn.wifi.rssi = WiFi.RSSI(); conn.wifi.status.status.ip_address = WiFi.localIP(); conn.wifi.status.status.is_mqtt_connected = mqtt && mqtt->connected(); @@ -552,18 +526,27 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r #if HAS_ETHERNET conn.has_ethernet = true; - // conn.ethernet. + if (Ethernet.linkStatus() == LinkON) { + conn.conn.ethernet.status.is_connected = true; + conn.conn.ethernet.status.ip_address = Ethernet.localIP(); + conn.wifi.status.status.is_mqtt_connected = mqtt && mqtt->connected(); + conn.wifi.status.status.is_syslog_connected = false; // FIXME wire this up + } + conn.conn.ethernet.status.is_connected = false; #endif #if HAS_BLUETOOTH conn.has_bluetooth = HAS_BLUETOOTH; - // nimbleBluetooth-> - // #if ARCH_ESP32 - - // #elif + conn.bluetooth.pin = config.bluetooth.fixed_pin; +#endif +#ifdef ARCH_ESP32 + conn.bluetooth.is_connected = nimbleBluetooth->isConnected(); +#elif defined(ARCH_NRF52) + conn.bluetooth.is_connected = nrf52Bluetooth->isConnected(); #endif - conn.has_serial = true; // No serial-less devices + conn.serial.is_connected = powerFSM.getState() == &stateSERIAL; + conn.serial.baud = SERIAL_BAUD; r.get_device_connection_status_response = conn; r.which_payload_variant = meshtastic_AdminMessage_get_device_connection_status_response_tag; @@ -572,8 +555,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &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); @@ -591,17 +573,13 @@ 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); } } diff --git a/src/modules/AdminModule.h b/src/modules/AdminModule.h index 6924bfd7e..76f6f8033 100644 --- a/src/modules/AdminModule.h +++ b/src/modules/AdminModule.h @@ -9,41 +9,41 @@ */ class AdminModule : public ProtobufModule { -public: - /** Constructor - * name is for debugging output - */ - AdminModule(); + public: + /** Constructor + * name is for debugging output + */ + AdminModule(); -protected: - /** Called to handle a particular incoming message + protected: + /** Called to handle a particular incoming message - @return true if you've guaranteed you've handled this message and no other handlers should be considered for it - */ - virtual bool handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_AdminMessage *p) override; + @return true if you've guaranteed you've handled this message and no other handlers should be considered for it + */ + virtual bool handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_AdminMessage *p) override; -private: - bool hasOpenEditTransaction = false; + private: + bool hasOpenEditTransaction = false; - void saveChanges(int saveWhat, bool shouldReboot = true); - /** - * Getters - */ - void handleGetOwner(const meshtastic_MeshPacket &req); - void handleGetConfig(const meshtastic_MeshPacket &req, uint32_t configType); - void handleGetModuleConfig(const meshtastic_MeshPacket &req, uint32_t configType); - void handleGetChannel(const meshtastic_MeshPacket &req, uint32_t channelIndex); - void handleGetDeviceMetadata(const meshtastic_MeshPacket &req); - void handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &req); - /** - * Setters - */ - void handleSetOwner(const meshtastic_User &o); - void handleSetChannel(const meshtastic_Channel &cc); - void handleSetConfig(const meshtastic_Config &c); - void handleSetModuleConfig(const meshtastic_ModuleConfig &c); - void handleSetChannel(); - void reboot(int32_t seconds); + void saveChanges(int saveWhat, bool shouldReboot = true); + /** + * Getters + */ + void handleGetOwner(const meshtastic_MeshPacket &req); + void handleGetConfig(const meshtastic_MeshPacket &req, uint32_t configType); + void handleGetModuleConfig(const meshtastic_MeshPacket &req, uint32_t configType); + void handleGetChannel(const meshtastic_MeshPacket &req, uint32_t channelIndex); + void handleGetDeviceMetadata(const meshtastic_MeshPacket &req); + void handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &req); + /** + * Setters + */ + void handleSetOwner(const meshtastic_User &o); + void handleSetChannel(const meshtastic_Channel &cc); + void handleSetConfig(const meshtastic_Config &c); + void handleSetModuleConfig(const meshtastic_ModuleConfig &c); + void handleSetChannel(); + void reboot(int32_t seconds); }; extern AdminModule *adminModule; diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index 79be9a35b..1f06b25f2 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -93,7 +93,6 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks passkeyShowing = false; screen->stopBluetoothPinScreen(); } - // bluetoothPhoneAPI->setInitialState(); } virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) { LOG_INFO("BLE disconnect\n"); } @@ -117,6 +116,21 @@ bool NimbleBluetooth::isActive() return bleServer; } +bool NimbleBluetooth::isConnected() +{ + return bleServer->getConnectedCount() > 0; +} + +int NimbleBluetooth::getRssi() +{ + if (bleServer && isConnected()) { + auto service = bleServer->getServiceByUUID(MESH_SERVICE_UUID); + uint16_t handle = service->getHandle(); + return NimBLEDevice::getClientByID(handle)->getRssi(); + } + return 0; // FIXME figure out where to source this +} + void NimbleBluetooth::setup() { // Uncomment for testing @@ -135,7 +149,6 @@ void NimbleBluetooth::setup() NimbleBluetoothServerCallback *serverCallbacks = new NimbleBluetoothServerCallback(); bleServer->setCallbacks(serverCallbacks, true); - setupService(); startAdvertising(); } diff --git a/src/nimble/NimbleBluetooth.h b/src/nimble/NimbleBluetooth.h index ec0fe0841..4080a7cbc 100644 --- a/src/nimble/NimbleBluetooth.h +++ b/src/nimble/NimbleBluetooth.h @@ -1,12 +1,15 @@ #pragma once +#include "BluetoothCommon.h" -class NimbleBluetooth +class NimbleBluetooth : BluetoothApi { public: void setup(); void shutdown(); void clearBonds(); bool isActive(); + bool isConnected(); + int getRssi(); private: void setupService(); diff --git a/src/platform/esp32/main-esp32.cpp b/src/platform/esp32/main-esp32.cpp index ca757a6e5..f6bd4f50e 100644 --- a/src/platform/esp32/main-esp32.cpp +++ b/src/platform/esp32/main-esp32.cpp @@ -19,11 +19,9 @@ #include #if !defined(CONFIG_IDF_TARGET_ESP32S2) -NimbleBluetooth *nimbleBluetooth; void setBluetoothEnable(bool on) { - if (!isWifiAvailable() && config.bluetooth.enabled == true) { if (!nimbleBluetooth) { nimbleBluetooth = new NimbleBluetooth(); diff --git a/src/platform/nrf52/NRF52Bluetooth.cpp b/src/platform/nrf52/NRF52Bluetooth.cpp index 06b07f593..044b57ae6 100644 --- a/src/platform/nrf52/NRF52Bluetooth.cpp +++ b/src/platform/nrf52/NRF52Bluetooth.cpp @@ -213,6 +213,16 @@ void NRF52Bluetooth::shutdown() Bluefruit.Advertising.stop(); } +bool NRF52Bluetooth::isConnected() +{ + return Bluefruit.connected(connectionHandle); +} + +int NRF52Bluetooth::getRssi() +{ + return 0; // FIXME figure out where to source this +} + void NRF52Bluetooth::setup() { // Initialise the Bluefruit module diff --git a/src/platform/nrf52/NRF52Bluetooth.h b/src/platform/nrf52/NRF52Bluetooth.h index b4438ff39..193e86cf8 100644 --- a/src/platform/nrf52/NRF52Bluetooth.h +++ b/src/platform/nrf52/NRF52Bluetooth.h @@ -1,13 +1,16 @@ #pragma once +#include "BluetoothCommon.h" #include -class NRF52Bluetooth +class NRF52Bluetooth : BluetoothApi { public: void setup(); void shutdown(); void clearBonds(); + bool isConnected(); + int getRssi(); private: static void onConnectionSecured(uint16_t conn_handle); diff --git a/src/platform/nrf52/main-nrf52.cpp b/src/platform/nrf52/main-nrf52.cpp index 0384073a4..1cbe05631 100644 --- a/src/platform/nrf52/main-nrf52.cpp +++ b/src/platform/nrf52/main-nrf52.cpp @@ -8,9 +8,8 @@ #include // #include #include "NodeDB.h" - -#include "NRF52Bluetooth.h" #include "error.h" +#include "main.h" #ifdef BQ25703A_ADDR #include "BQ25713.h" @@ -63,8 +62,6 @@ static void initBrownout() // We don't bother with setting up brownout if soft device is disabled - because during production we always use softdevice } -NRF52Bluetooth *nrf52Bluetooth; - static bool bleOn = false; static const bool useSoftDevice = true; // Set to false for easier debugging From b97e61d8f81388c5610de161561d58ee2f4e0a1c Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 08:51:02 -0600 Subject: [PATCH 05/21] Whoops --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 0f3e4cc4d..1d8083f70 100644 --- a/platformio.ini +++ b/platformio.ini @@ -2,7 +2,7 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -default_envs = tbeam +;default_envs = tbeam ;default_envs = pico ;default_envs = tbeam-s3-core ;default_envs = tbeam0.7 From 835e6ab85e2f0f416897292ce19e4a44a74da1fb Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 08:52:32 -0600 Subject: [PATCH 06/21] Missed RSSI --- src/modules/AdminModule.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 7beea8804..e240c05bd 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -541,6 +541,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r #endif #ifdef ARCH_ESP32 conn.bluetooth.is_connected = nimbleBluetooth->isConnected(); + conn.bluetooth.rssi = nimbleBluetooth->getRssi(); #elif defined(ARCH_NRF52) conn.bluetooth.is_connected = nrf52Bluetooth->isConnected(); #endif From 5794a9ae06142d73a0d180a2c1413394f6ff3d89 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 09:04:22 -0600 Subject: [PATCH 07/21] Bad copy pasta --- src/modules/AdminModule.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index e240c05bd..b9b75a224 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -527,10 +527,10 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r #if HAS_ETHERNET conn.has_ethernet = true; if (Ethernet.linkStatus() == LinkON) { - conn.conn.ethernet.status.is_connected = true; - conn.conn.ethernet.status.ip_address = Ethernet.localIP(); - conn.wifi.status.status.is_mqtt_connected = mqtt && mqtt->connected(); - conn.wifi.status.status.is_syslog_connected = false; // FIXME wire this up + conn.ethernet.status.is_connected = true; + conn.ethernet.status.ip_address = Ethernet.localIP(); + conn.ethernet.status.status.is_mqtt_connected = mqtt && mqtt->connected(); + conn.ethernet.status.status.is_syslog_connected = false; // FIXME wire this up } conn.conn.ethernet.status.is_connected = false; #endif From 4aaf16270002202a79993c56e494ff5a77059d3e Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 09:17:28 -0600 Subject: [PATCH 08/21] Getting tired of looking at code --- src/modules/AdminModule.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index b9b75a224..cecb4655c 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -529,8 +529,8 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r if (Ethernet.linkStatus() == LinkON) { conn.ethernet.status.is_connected = true; conn.ethernet.status.ip_address = Ethernet.localIP(); - conn.ethernet.status.status.is_mqtt_connected = mqtt && mqtt->connected(); - conn.ethernet.status.status.is_syslog_connected = false; // FIXME wire this up + conn.ethernet.status.is_mqtt_connected = mqtt && mqtt->connected(); + conn.ethernet.status.is_syslog_connected = false; // FIXME wire this up } conn.conn.ethernet.status.is_connected = false; #endif From 51521462c4fa2cd35a80c0439421374176059d24 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 09:18:53 -0600 Subject: [PATCH 09/21] Eh --- src/modules/AdminModule.cpp | 117 ++++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 38 deletions(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index cecb4655c..2688a2959 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -33,7 +33,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); } } @@ -51,7 +52,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 @@ -71,7 +73,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) @@ -110,17 +113,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); @@ -132,41 +140,48 @@ 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); break; } - case meshtastic_AdminMessage_get_device_connection_status_request_tag: { + case meshtastic_AdminMessage_get_device_connection_status_request_tag: + { LOG_INFO("Client is getting device connection status\n"); handleGetDeviceConnectionStatus(mp); break; @@ -182,11 +197,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); } @@ -194,7 +214,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); } @@ -210,26 +231,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); } @@ -240,7 +266,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; @@ -275,7 +302,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; @@ -291,7 +319,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; @@ -355,7 +384,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; @@ -369,8 +399,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; @@ -425,8 +457,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; @@ -516,7 +550,8 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.has_wifi = true; 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) { + if (conn.wifi.status.status.is_connected) + { conn.wifi.rssi = WiFi.RSSI(); conn.wifi.status.status.ip_address = WiFi.localIP(); conn.wifi.status.status.is_mqtt_connected = mqtt && mqtt->connected(); @@ -526,13 +561,14 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r #if HAS_ETHERNET conn.has_ethernet = true; - if (Ethernet.linkStatus() == LinkON) { + if (Ethernet.linkStatus() == LinkON) + { conn.ethernet.status.is_connected = true; conn.ethernet.status.ip_address = Ethernet.localIP(); conn.ethernet.status.is_mqtt_connected = mqtt && mqtt->connected(); conn.ethernet.status.is_syslog_connected = false; // FIXME wire this up } - conn.conn.ethernet.status.is_connected = false; + conn.ethernet.status.is_connected = false; #endif #if HAS_BLUETOOTH @@ -556,7 +592,8 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &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); @@ -574,13 +611,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); } } From 1fa2ca6a14e7c97481e7069853ebfcc8e94e1397 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 09:50:49 -0600 Subject: [PATCH 10/21] Trunk it --- src/modules/AdminModule.cpp | 115 ++++++++++++------------------------ 1 file changed, 37 insertions(+), 78 deletions(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 2688a2959..f52aa4e0f 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -33,8 +33,7 @@ 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); } } @@ -52,8 +51,7 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta bool handled = false; assert(r); - switch (r->which_payload_variant) - { + switch (r->which_payload_variant) { /** * Getters @@ -73,8 +71,7 @@ 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) @@ -113,22 +110,17 @@ 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); @@ -140,48 +132,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); break; } - case meshtastic_AdminMessage_get_device_connection_status_request_tag: - { + case meshtastic_AdminMessage_get_device_connection_status_request_tag: { LOG_INFO("Client is getting device connection status\n"); handleGetDeviceConnectionStatus(mp); break; @@ -197,16 +182,11 @@ 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); } @@ -214,8 +194,7 @@ 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); } @@ -231,31 +210,26 @@ 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); } @@ -266,8 +240,7 @@ 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; @@ -302,8 +275,7 @@ 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; @@ -319,8 +291,7 @@ 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; @@ -384,8 +355,7 @@ 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; @@ -399,10 +369,8 @@ 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; @@ -457,10 +425,8 @@ 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; @@ -550,8 +516,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.has_wifi = true; 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) - { + if (conn.wifi.status.status.is_connected) { conn.wifi.rssi = WiFi.RSSI(); conn.wifi.status.status.ip_address = WiFi.localIP(); conn.wifi.status.status.is_mqtt_connected = mqtt && mqtt->connected(); @@ -561,8 +526,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r #if HAS_ETHERNET conn.has_ethernet = true; - if (Ethernet.linkStatus() == LinkON) - { + if (Ethernet.linkStatus() == LinkON) { conn.ethernet.status.is_connected = true; conn.ethernet.status.ip_address = Ethernet.localIP(); conn.ethernet.status.is_mqtt_connected = mqtt && mqtt->connected(); @@ -592,8 +556,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &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); @@ -611,17 +574,13 @@ 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); } } From 0414ca2dc0c1fbb518e0686775b765ffb4a48fac Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 10:49:44 -0600 Subject: [PATCH 11/21] Fixed it --- protobufs | 2 +- .../meshtastic/connection_status.pb.h | 34 ++--- src/modules/AdminModule.cpp | 132 ++++++++++++------ 3 files changed, 107 insertions(+), 61 deletions(-) diff --git a/protobufs b/protobufs index 4ef7e46a8..516074f2e 160000 --- a/protobufs +++ b/protobufs @@ -1 +1 @@ -Subproject commit 4ef7e46a8b83d4d2147f027c7aaa7d1b481f8688 +Subproject commit 516074f2e49743c234430abb2ea43ea3f66b0acb diff --git a/src/mesh/generated/meshtastic/connection_status.pb.h b/src/mesh/generated/meshtastic/connection_status.pb.h index c13ec2bc6..c824f8b82 100644 --- a/src/mesh/generated/meshtastic/connection_status.pb.h +++ b/src/mesh/generated/meshtastic/connection_status.pb.h @@ -22,6 +22,17 @@ typedef struct _meshtastic_NetworkConnectionStatus { bool is_syslog_connected; } meshtastic_NetworkConnectionStatus; +/* WiFi connection status */ +typedef struct _meshtastic_WifiConnectionStatus { + /* Connection status */ + bool has_status; + meshtastic_NetworkConnectionStatus status; + /* WiFi access point ssid */ + char ssid[33]; + /* Rssi of wireless connection */ + int32_t rssi; +} meshtastic_WifiConnectionStatus; + /* Ethernet connection status */ typedef struct _meshtastic_EthernetConnectionStatus { /* Connection status */ @@ -29,17 +40,6 @@ typedef struct _meshtastic_EthernetConnectionStatus { meshtastic_NetworkConnectionStatus status; } meshtastic_EthernetConnectionStatus; -/* WiFi connection status */ -typedef struct _meshtastic_WifiConnectionStatus { - /* Connection status */ - bool has_status; - meshtastic_EthernetConnectionStatus status; - /* WiFi access point ssid */ - char ssid[33]; - /* Rssi of wireless connection */ - int32_t rssi; -} meshtastic_WifiConnectionStatus; - /* Bluetooth connection status */ typedef struct _meshtastic_BluetoothConnectionStatus { /* The pairing pin for bluetooth */ @@ -80,13 +80,13 @@ extern "C" { /* Initializer values for message structs */ #define meshtastic_DeviceConnectionStatus_init_default {false, meshtastic_WifiConnectionStatus_init_default, false, meshtastic_EthernetConnectionStatus_init_default, false, meshtastic_BluetoothConnectionStatus_init_default, false, meshtastic_SerialConnectionStatus_init_default} -#define meshtastic_WifiConnectionStatus_init_default {false, meshtastic_EthernetConnectionStatus_init_default, "", 0} +#define meshtastic_WifiConnectionStatus_init_default {false, meshtastic_NetworkConnectionStatus_init_default, "", 0} #define meshtastic_EthernetConnectionStatus_init_default {false, meshtastic_NetworkConnectionStatus_init_default} #define meshtastic_NetworkConnectionStatus_init_default {0, 0, 0, 0} #define meshtastic_BluetoothConnectionStatus_init_default {0, 0, 0} #define meshtastic_SerialConnectionStatus_init_default {0, 0} #define meshtastic_DeviceConnectionStatus_init_zero {false, meshtastic_WifiConnectionStatus_init_zero, false, meshtastic_EthernetConnectionStatus_init_zero, false, meshtastic_BluetoothConnectionStatus_init_zero, false, meshtastic_SerialConnectionStatus_init_zero} -#define meshtastic_WifiConnectionStatus_init_zero {false, meshtastic_EthernetConnectionStatus_init_zero, "", 0} +#define meshtastic_WifiConnectionStatus_init_zero {false, meshtastic_NetworkConnectionStatus_init_zero, "", 0} #define meshtastic_EthernetConnectionStatus_init_zero {false, meshtastic_NetworkConnectionStatus_init_zero} #define meshtastic_NetworkConnectionStatus_init_zero {0, 0, 0, 0} #define meshtastic_BluetoothConnectionStatus_init_zero {0, 0, 0} @@ -97,10 +97,10 @@ extern "C" { #define meshtastic_NetworkConnectionStatus_is_connected_tag 2 #define meshtastic_NetworkConnectionStatus_is_mqtt_connected_tag 3 #define meshtastic_NetworkConnectionStatus_is_syslog_connected_tag 4 -#define meshtastic_EthernetConnectionStatus_status_tag 1 #define meshtastic_WifiConnectionStatus_status_tag 1 #define meshtastic_WifiConnectionStatus_ssid_tag 2 #define meshtastic_WifiConnectionStatus_rssi_tag 3 +#define meshtastic_EthernetConnectionStatus_status_tag 1 #define meshtastic_BluetoothConnectionStatus_pin_tag 1 #define meshtastic_BluetoothConnectionStatus_rssi_tag 2 #define meshtastic_BluetoothConnectionStatus_is_connected_tag 3 @@ -130,7 +130,7 @@ X(a, STATIC, SINGULAR, STRING, ssid, 2) \ X(a, STATIC, SINGULAR, INT32, rssi, 3) #define meshtastic_WifiConnectionStatus_CALLBACK NULL #define meshtastic_WifiConnectionStatus_DEFAULT NULL -#define meshtastic_WifiConnectionStatus_status_MSGTYPE meshtastic_EthernetConnectionStatus +#define meshtastic_WifiConnectionStatus_status_MSGTYPE meshtastic_NetworkConnectionStatus #define meshtastic_EthernetConnectionStatus_FIELDLIST(X, a) \ X(a, STATIC, OPTIONAL, MESSAGE, status, 1) @@ -176,11 +176,11 @@ extern const pb_msgdesc_t meshtastic_SerialConnectionStatus_msg; /* Maximum encoded size of messages (where known) */ #define meshtastic_BluetoothConnectionStatus_size 19 -#define meshtastic_DeviceConnectionStatus_size 108 +#define meshtastic_DeviceConnectionStatus_size 106 #define meshtastic_EthernetConnectionStatus_size 13 #define meshtastic_NetworkConnectionStatus_size 11 #define meshtastic_SerialConnectionStatus_size 8 -#define meshtastic_WifiConnectionStatus_size 60 +#define meshtastic_WifiConnectionStatus_size 58 #ifdef __cplusplus } /* extern "C" */ diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index f52aa4e0f..fecf3362c 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -33,7 +33,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); } } @@ -51,7 +52,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 @@ -71,7 +73,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) @@ -110,17 +113,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); @@ -132,41 +140,48 @@ 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); break; } - case meshtastic_AdminMessage_get_device_connection_status_request_tag: { + case meshtastic_AdminMessage_get_device_connection_status_request_tag: + { LOG_INFO("Client is getting device connection status\n"); handleGetDeviceConnectionStatus(mp); break; @@ -182,11 +197,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); } @@ -194,7 +214,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); } @@ -210,26 +231,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); } @@ -240,7 +266,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; @@ -275,7 +302,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; @@ -291,7 +319,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; @@ -355,7 +384,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; @@ -369,8 +399,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; @@ -425,8 +457,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; @@ -514,29 +548,36 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r #if HAS_WIFI conn.has_wifi = true; - conn.wifi.status.status.is_connected = WiFi.status() != WL_CONNECTED; + conn.wifi.has_status = true; + conn.wifi.status.is_connected = WiFi.status() != WL_CONNECTED; strncpy(conn.wifi.ssid, config.network.wifi_ssid, 33); - if (conn.wifi.status.status.is_connected) { + if (conn.wifi.status.is_connected) + { conn.wifi.rssi = WiFi.RSSI(); - 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 + conn.wifi.status.ip_address = WiFi.localIP(); + conn.wifi.status.is_mqtt_connected = mqtt && mqtt->connected(); + conn.wifi.status.is_syslog_connected = false; // FIXME wire this up } #endif #if HAS_ETHERNET conn.has_ethernet = true; - if (Ethernet.linkStatus() == LinkON) { + conn.ethernet.has_status = true; + if (Ethernet.linkStatus() == LinkON) + { conn.ethernet.status.is_connected = true; conn.ethernet.status.ip_address = Ethernet.localIP(); conn.ethernet.status.is_mqtt_connected = mqtt && mqtt->connected(); conn.ethernet.status.is_syslog_connected = false; // FIXME wire this up } - conn.ethernet.status.is_connected = false; + else + { + conn.ethernet.status.is_connected = false; + } #endif #if HAS_BLUETOOTH - conn.has_bluetooth = HAS_BLUETOOTH; + conn.has_bluetooth = true; conn.bluetooth.pin = config.bluetooth.fixed_pin; #endif #ifdef ARCH_ESP32 @@ -556,7 +597,8 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &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); @@ -574,13 +616,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); } } From 2b15d951cf35c606cf9b64e23841c5bf14a05207 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 10:50:03 -0600 Subject: [PATCH 12/21] Trunk --- src/modules/AdminModule.cpp | 119 ++++++++++++------------------------ 1 file changed, 38 insertions(+), 81 deletions(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index fecf3362c..79798b94b 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -33,8 +33,7 @@ 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); } } @@ -52,8 +51,7 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta bool handled = false; assert(r); - switch (r->which_payload_variant) - { + switch (r->which_payload_variant) { /** * Getters @@ -73,8 +71,7 @@ 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) @@ -113,22 +110,17 @@ 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); @@ -140,48 +132,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); break; } - case meshtastic_AdminMessage_get_device_connection_status_request_tag: - { + case meshtastic_AdminMessage_get_device_connection_status_request_tag: { LOG_INFO("Client is getting device connection status\n"); handleGetDeviceConnectionStatus(mp); break; @@ -197,16 +182,11 @@ 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); } @@ -214,8 +194,7 @@ 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); } @@ -231,31 +210,26 @@ 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); } @@ -266,8 +240,7 @@ 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; @@ -302,8 +275,7 @@ 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; @@ -319,8 +291,7 @@ 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; @@ -384,8 +355,7 @@ 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; @@ -399,10 +369,8 @@ 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; @@ -457,10 +425,8 @@ 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; @@ -551,8 +517,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.wifi.has_status = true; conn.wifi.status.is_connected = WiFi.status() != WL_CONNECTED; strncpy(conn.wifi.ssid, config.network.wifi_ssid, 33); - if (conn.wifi.status.is_connected) - { + if (conn.wifi.status.is_connected) { conn.wifi.rssi = WiFi.RSSI(); conn.wifi.status.ip_address = WiFi.localIP(); conn.wifi.status.is_mqtt_connected = mqtt && mqtt->connected(); @@ -563,15 +528,12 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r #if HAS_ETHERNET conn.has_ethernet = true; conn.ethernet.has_status = true; - if (Ethernet.linkStatus() == LinkON) - { + if (Ethernet.linkStatus() == LinkON) { conn.ethernet.status.is_connected = true; conn.ethernet.status.ip_address = Ethernet.localIP(); conn.ethernet.status.is_mqtt_connected = mqtt && mqtt->connected(); conn.ethernet.status.is_syslog_connected = false; // FIXME wire this up - } - else - { + } else { conn.ethernet.status.is_connected = false; } #endif @@ -597,8 +559,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &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); @@ -616,17 +577,13 @@ 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); } } From d6de0427830420ecce9fd3f2179e4f0d5a5f2364 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 11:00:05 -0600 Subject: [PATCH 13/21] Assume portduino is always connected --- src/modules/AdminModule.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 79798b94b..d48ed83d6 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -515,7 +515,11 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r #if HAS_WIFI conn.has_wifi = true; conn.wifi.has_status = true; +#ifdef ARCH_PORTDUINO + conn.wifi.status.is_connected = true; +#elif conn.wifi.status.is_connected = WiFi.status() != WL_CONNECTED; +#endif strncpy(conn.wifi.ssid, config.network.wifi_ssid, 33); if (conn.wifi.status.is_connected) { conn.wifi.rssi = WiFi.RSSI(); From ac90c27ae87956080a30a4a33a3dac124cd3425a Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 11:03:12 -0600 Subject: [PATCH 14/21] Macros --- src/modules/AdminModule.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index d48ed83d6..536cb82af 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -527,6 +527,8 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.wifi.status.is_mqtt_connected = mqtt && mqtt->connected(); conn.wifi.status.is_syslog_connected = false; // FIXME wire this up } +#else + conn.has_wifi = false; #endif #if HAS_ETHERNET @@ -540,6 +542,8 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r } else { conn.ethernet.status.is_connected = false; } +#else + conn.has_ethernet = false; #endif #if HAS_BLUETOOTH From ab77772e0ca0f64e18551e2f17b26eff2f0d2868 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 11:11:09 -0600 Subject: [PATCH 15/21] Bugger --- src/modules/AdminModule.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 536cb82af..3b1fdd26c 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -517,7 +517,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.wifi.has_status = true; #ifdef ARCH_PORTDUINO conn.wifi.status.is_connected = true; -#elif +#else conn.wifi.status.is_connected = WiFi.status() != WL_CONNECTED; #endif strncpy(conn.wifi.ssid, config.network.wifi_ssid, 33); From 9a950afd2acd6a4a95f2cabd01b50debd07a034e Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 11:30:36 -0600 Subject: [PATCH 16/21] Trunk fmt --- src/modules/AdminModule.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 3b1fdd26c..9e50ad938 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -560,7 +560,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.serial.is_connected = powerFSM.getState() == &stateSERIAL; conn.serial.baud = SERIAL_BAUD; - r.get_device_connection_status_response = conn; + r.get_device_connection_status_response = conn; // cppcheck-suppress uninitStructMember r.which_payload_variant = meshtastic_AdminMessage_get_device_connection_status_response_tag; myReply = allocDataProtobuf(r); } From c82d1de9cead83f2225b0ecc81f8d2c4251010c4 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 12:43:16 -0600 Subject: [PATCH 17/21] Check --- src/modules/AdminModule.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 9e50ad938..580076845 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -560,7 +560,8 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.serial.is_connected = powerFSM.getState() == &stateSERIAL; conn.serial.baud = SERIAL_BAUD; - r.get_device_connection_status_response = conn; // cppcheck-suppress uninitStructMember + // cppcheck-suppress uninitStructMember + r.get_device_connection_status_response = conn; r.which_payload_variant = meshtastic_AdminMessage_get_device_connection_status_response_tag; myReply = allocDataProtobuf(r); } From 9d420f403a3d50fe10178b04d62692f498006bc3 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 13:05:25 -0600 Subject: [PATCH 18/21] Try this --- src/modules/AdminModule.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 580076845..42ae03a74 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -560,8 +560,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.serial.is_connected = powerFSM.getState() == &stateSERIAL; conn.serial.baud = SERIAL_BAUD; - // cppcheck-suppress uninitStructMember - r.get_device_connection_status_response = conn; + r.get_device_connection_status_response = conn; // cppcheck-suppress [uninitStructMember,uninitvar] r.which_payload_variant = meshtastic_AdminMessage_get_device_connection_status_response_tag; myReply = allocDataProtobuf(r); } From 81d2486cf49ab3d153114fdb48baa057a044361d Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 13:17:39 -0600 Subject: [PATCH 19/21] Init defaults --- src/modules/AdminModule.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 42ae03a74..6854c5116 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -512,6 +512,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r meshtastic_DeviceConnectionStatus conn; + conn.wifi = meshtastic_WifiConnectionStatus_init_default; #if HAS_WIFI conn.has_wifi = true; conn.wifi.has_status = true; @@ -531,6 +532,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.has_wifi = false; #endif + conn.ethernet = meshtastic_EthernetConnectionStatus_init_default; #if HAS_ETHERNET conn.has_ethernet = true; conn.ethernet.has_status = true; @@ -560,7 +562,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.serial.is_connected = powerFSM.getState() == &stateSERIAL; conn.serial.baud = SERIAL_BAUD; - r.get_device_connection_status_response = conn; // cppcheck-suppress [uninitStructMember,uninitvar] + r.get_device_connection_status_response = conn; r.which_payload_variant = meshtastic_AdminMessage_get_device_connection_status_response_tag; myReply = allocDataProtobuf(r); } From 3a3451129aa9a8ed7e29d6e3d0b58c7e1b628115 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 13:59:54 -0600 Subject: [PATCH 20/21] Init gooder? --- src/modules/AdminModule.cpp | 123 ++++++++++++++++++++++++------------ 1 file changed, 83 insertions(+), 40 deletions(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 6854c5116..538e21c76 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -33,7 +33,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); } } @@ -51,7 +52,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 @@ -71,7 +73,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) @@ -110,17 +113,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); @@ -132,41 +140,48 @@ 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); break; } - case meshtastic_AdminMessage_get_device_connection_status_request_tag: { + case meshtastic_AdminMessage_get_device_connection_status_request_tag: + { LOG_INFO("Client is getting device connection status\n"); handleGetDeviceConnectionStatus(mp); break; @@ -182,11 +197,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); } @@ -194,7 +214,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); } @@ -210,26 +231,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); } @@ -240,7 +266,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; @@ -275,7 +302,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; @@ -291,7 +319,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; @@ -355,7 +384,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; @@ -369,8 +399,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; @@ -425,8 +457,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; @@ -512,7 +546,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r meshtastic_DeviceConnectionStatus conn; - conn.wifi = meshtastic_WifiConnectionStatus_init_default; + conn.wifi = {0}; #if HAS_WIFI conn.has_wifi = true; conn.wifi.has_status = true; @@ -522,7 +556,8 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.wifi.status.is_connected = WiFi.status() != WL_CONNECTED; #endif strncpy(conn.wifi.ssid, config.network.wifi_ssid, 33); - if (conn.wifi.status.is_connected) { + if (conn.wifi.status.is_connected) + { conn.wifi.rssi = WiFi.RSSI(); conn.wifi.status.ip_address = WiFi.localIP(); conn.wifi.status.is_mqtt_connected = mqtt && mqtt->connected(); @@ -532,16 +567,19 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.has_wifi = false; #endif - conn.ethernet = meshtastic_EthernetConnectionStatus_init_default; + conn.ethernet = {0}; #if HAS_ETHERNET conn.has_ethernet = true; conn.ethernet.has_status = true; - if (Ethernet.linkStatus() == LinkON) { + if (Ethernet.linkStatus() == LinkON) + { conn.ethernet.status.is_connected = true; conn.ethernet.status.ip_address = Ethernet.localIP(); conn.ethernet.status.is_mqtt_connected = mqtt && mqtt->connected(); conn.ethernet.status.is_syslog_connected = false; // FIXME wire this up - } else { + } + else + { conn.ethernet.status.is_connected = false; } #else @@ -569,7 +607,8 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &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); @@ -587,13 +626,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); } } From 6a2583e8727aaf5b974446cc817c15f41bbdaa64 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 3 Feb 2023 14:10:19 -0600 Subject: [PATCH 21/21] Trunk you mothertrunker --- src/modules/AdminModule.cpp | 119 ++++++++++++------------------------ 1 file changed, 38 insertions(+), 81 deletions(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 538e21c76..8d69289f0 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -33,8 +33,7 @@ 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); } } @@ -52,8 +51,7 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta bool handled = false; assert(r); - switch (r->which_payload_variant) - { + switch (r->which_payload_variant) { /** * Getters @@ -73,8 +71,7 @@ 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) @@ -113,22 +110,17 @@ 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); @@ -140,48 +132,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); break; } - case meshtastic_AdminMessage_get_device_connection_status_request_tag: - { + case meshtastic_AdminMessage_get_device_connection_status_request_tag: { LOG_INFO("Client is getting device connection status\n"); handleGetDeviceConnectionStatus(mp); break; @@ -197,16 +182,11 @@ 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); } @@ -214,8 +194,7 @@ 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); } @@ -231,31 +210,26 @@ 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); } @@ -266,8 +240,7 @@ 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; @@ -302,8 +275,7 @@ 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; @@ -319,8 +291,7 @@ 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; @@ -384,8 +355,7 @@ 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; @@ -399,10 +369,8 @@ 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; @@ -457,10 +425,8 @@ 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; @@ -556,8 +522,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r conn.wifi.status.is_connected = WiFi.status() != WL_CONNECTED; #endif strncpy(conn.wifi.ssid, config.network.wifi_ssid, 33); - if (conn.wifi.status.is_connected) - { + if (conn.wifi.status.is_connected) { conn.wifi.rssi = WiFi.RSSI(); conn.wifi.status.ip_address = WiFi.localIP(); conn.wifi.status.is_mqtt_connected = mqtt && mqtt->connected(); @@ -571,15 +536,12 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r #if HAS_ETHERNET conn.has_ethernet = true; conn.ethernet.has_status = true; - if (Ethernet.linkStatus() == LinkON) - { + if (Ethernet.linkStatus() == LinkON) { conn.ethernet.status.is_connected = true; conn.ethernet.status.ip_address = Ethernet.localIP(); conn.ethernet.status.is_mqtt_connected = mqtt && mqtt->connected(); conn.ethernet.status.is_syslog_connected = false; // FIXME wire this up - } - else - { + } else { conn.ethernet.status.is_connected = false; } #else @@ -607,8 +569,7 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &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); @@ -626,17 +587,13 @@ 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); } }