[Network] Add AT command write to PPP modem

This commit is contained in:
Gijs Noorlander
2025-08-21 16:39:33 +06:00
parent a7ddb44a36
commit 9f949f6e29
7 changed files with 71 additions and 9 deletions
@@ -378,6 +378,11 @@ LongTermTimer::Duration NWPluginData_base::getConnectedDuration_ms() {
return 0;
}
bool NWPluginData_base::handle_nwplugin_write(EventStruct *event, String& str)
{
return false;
}
#ifdef ESP32
bool NWPluginData_base::handle_priority_route_changed()
@@ -98,6 +98,7 @@ struct NWPluginData_base {
nwpluginID_t getNWPluginID() const { return _nw_data_pluginID; }
virtual LongTermTimer::Duration getConnectedDuration_ms();
virtual bool handle_nwplugin_write(EventStruct *event, String& str);
#ifdef ESP32
virtual bool handle_priority_route_changed();
@@ -9,6 +9,7 @@
# include <esp_netif.h>
# include <esp_netif_types.h>
#define NW_PLUGIN_LOG_EVENTS false
namespace ESPEasy {
namespace net {
@@ -52,23 +53,25 @@ void NWPluginData_static_runtime::mark_start()
_netif->setRoutePrio(_route_prio);
}
# endif // if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
#if NW_PLUGIN_LOG_EVENTS
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
addLog(LOG_LEVEL_INFO, strformat(
F("%s: Started"),
_netif->desc()));
}
#endif
}
void NWPluginData_static_runtime::mark_stop()
{
_startStopStats.setOff();
#if NW_PLUGIN_LOG_EVENTS
if (_netif && loglevelActiveFor(LOG_LEVEL_INFO)) {
addLog(LOG_LEVEL_INFO, strformat(
F("%s: Stopped"),
_netif->desc()));
}
#endif
}
void NWPluginData_static_runtime::mark_got_IP()
@@ -86,7 +89,7 @@ void NWPluginData_static_runtime::mark_got_IP()
auto tmp = _netif->dnsIP(i);
_dns_cache[i] = tmp; // Also set the 'empty' ones so we won't set left-over DNS server from when another interface was active.
#if NW_PLUGIN_LOG_EVENTS
if ((tmp != INADDR_NONE) && loglevelActiveFor(LOG_LEVEL_INFO)) {
addLog(LOG_LEVEL_INFO, strformat(
F("%s: DNS Cache %d set to %s"),
@@ -94,8 +97,9 @@ void NWPluginData_static_runtime::mark_got_IP()
i,
tmp.toString(true).c_str()));
}
#endif
}
#if NW_PLUGIN_LOG_EVENTS
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
addLog(LOG_LEVEL_INFO, strformat(
F("%s: Got IP: %s"),
@@ -103,6 +107,7 @@ void NWPluginData_static_runtime::mark_got_IP()
_netif->localIP().toString().c_str()
));
}
#endif
}
@@ -117,7 +122,7 @@ void NWPluginData_static_runtime::mark_got_IPv6(ip_event_got_ip6_t *event)
if (!_netif->isDefault()) {
nonDefaultNetworkInterface_gotIP = true;
}
#if NW_PLUGIN_LOG_EVENTS
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
if (event) {
esp_ip6_addr_type_t addr_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip);
@@ -136,6 +141,7 @@ void NWPluginData_static_runtime::mark_got_IPv6(ip_event_got_ip6_t *event)
_netif->desc()));
}
}
#endif
}
# endif // if FEATURE_USE_IPV6
@@ -146,12 +152,13 @@ void NWPluginData_static_runtime::mark_lost_IP()
# if FEATURE_USE_IPV6
_gotIP6Stats.setOff();
# endif
#if NW_PLUGIN_LOG_EVENTS
if (_netif && loglevelActiveFor(LOG_LEVEL_INFO)) {
addLog(LOG_LEVEL_INFO, strformat(
F("%s: Lost IP"),
_netif->desc()));
}
#endif
}
void NWPluginData_static_runtime::mark_begin_establish_connection()
@@ -166,7 +173,7 @@ void NWPluginData_static_runtime::mark_connected()
_establishConnectStats.setOff();
_connectedStats.setOn();
#if NW_PLUGIN_LOG_EVENTS
if (logDuration) {
addLog(LOG_LEVEL_INFO, strformat(
F("%s: Connected, took: %s in %d attempts"),
@@ -179,6 +186,7 @@ void NWPluginData_static_runtime::mark_connected()
F("%s: Connected"),
_netif->desc()));
}
#endif
_establishConnectStats.resetCount();
}
@@ -187,7 +195,7 @@ void NWPluginData_static_runtime::mark_disconnected()
_connectedStats.setOff();
// TODO TD-er: Also clear _gotIPStats and _gotIP6Stats ?
#if NW_PLUGIN_LOG_EVENTS
if (_netif && loglevelActiveFor(LOG_LEVEL_INFO)) {
addLog(LOG_LEVEL_INFO, strformat(
F("%s: Disconnected. Connected for: %s"),
@@ -195,6 +203,7 @@ void NWPluginData_static_runtime::mark_disconnected()
format_msec_duration_HMS(
_connectedStats.getLastOnDuration_ms()).c_str()));
}
#endif
}
} // namespace net
@@ -2,6 +2,8 @@
#ifdef USES_NW005
# include "../../../src/ESPEasyCore/Controller.h" // For SendStatus (should we move that function?)
# include "../../../src/Globals/Settings.h"
# include "../../../src/Helpers/ESPEasy_time_calc.h"
@@ -898,12 +900,35 @@ String NW005_data_struct_PPP_modem::write_AT_cmd(const String& cmd, int timeout)
if (NW_PLUGIN_INTERFACE.mode() != ESP_MODEM_MODE_CMUX) {
NW_PLUGIN_INTERFACE.mode(ESP_MODEM_MODE_CMUX);
res = NW_PLUGIN_INTERFACE.cmd(cmd.c_str(), timeout);
}
res = NW_PLUGIN_INTERFACE.cmd(cmd.c_str(), timeout);
NW_PLUGIN_INTERFACE.mode(cur_mode);
return res;
}
bool NW005_data_struct_PPP_modem::handle_nwplugin_write(EventStruct *event, String& str)
{
bool success = false;
const String command = parseString(str, 1);
if (equals(command, F("ppp"))) {
const String subcommand = parseString(str, 2);
if (equals(subcommand, F("write"))) {
const String writeCommand = parseStringToEnd(str, 3);
const String res = write_AT_cmd(writeCommand);
addLog(LOG_LEVEL_INFO, strformat(
F("PPP cmd: %s -> %s"),
writeCommand.c_str(),
res.c_str()));
success = true;
SendStatus(event, res);
}
}
return success;
}
# if FEATURE_PLUGIN_STATS
bool NW005_data_struct_PPP_modem::initPluginStats()
@@ -50,6 +50,8 @@ struct NW005_data_struct_PPP_modem : public NWPluginData_base {
bool exit(EventStruct *event);
bool handle_nwplugin_write(EventStruct *event, String& str) override;
String write_AT_cmd(const String& cmd,
int timeout = 1000);
+10
View File
@@ -224,6 +224,16 @@ bool NWPlugin_005(NWPlugin::Function function, EventStruct *event, String& strin
break;
}
case NWPlugin::Function::NWPLUGIN_WRITE:
{
auto *NW_data = getNWPluginData(event->NetworkIndex);
if (NW_data) {
success = NW_data->handle_nwplugin_write(event, string);
}
break;
}
default:
break;
+10
View File
@@ -12,6 +12,9 @@
#include "../Helpers/StringConverter.h"
#include "../Helpers/StringParser.h"
#include "../../ESPEasy/net/Globals/NWPlugins.h"
ExecuteCommandArgs::ExecuteCommandArgs(EventValueSource::Enum source,
const char *Line) :
_taskIndex(INVALID_TASK_INDEX),
@@ -267,6 +270,13 @@ bool ExecuteCommand(ExecuteCommandArgs&& args, bool addToQueue)
// if (handled) addLog(LOG_LEVEL_INFO, F("CPLUGIN_WRITE accepted"));
}
if (!handled) {
// Try a controller
handled = ESPEasy::net::NWPluginCall(NWPlugin::Function::NWPLUGIN_WRITE, &TempEvent, tmpAction);
// if (handled) addLog(LOG_LEVEL_INFO, F("CPLUGIN_WRITE accepted"));
}
if (handled) {
SendStatus(&TempEvent, return_command_success());
return true;