@@ -80,6 +80,7 @@ static void register_set_led_gpio(void);
8080static void register_set_led_lowactive (void );
8181static void register_set_led_strip (void );
8282static void register_set_ttl (void );
83+ static void register_client_stats_cmd (void );
8384static void register_set_tx_power (void );
8485#if defined(CONFIG_IDF_TARGET_ESP32C6 )
8586static void register_set_rf_switch (void );
@@ -297,6 +298,7 @@ void register_router(void)
297298 register_set_led_lowactive ();
298299 register_set_led_strip ();
299300 register_set_ttl ();
301+ register_client_stats_cmd ();
300302 register_set_tx_power ();
301303 register_set_ap_hidden ();
302304 register_set_ap_auth ();
@@ -1294,13 +1296,18 @@ static int show(int argc, char **argv)
12941296 int count = get_connected_clients (clients , 8 );
12951297
12961298 if (count > 0 ) {
1297- // Fetch per-client traffic stats
1299+ // Fetch per-client traffic stats only when enabled
12981300 client_stats_entry_t stats [CLIENT_STATS_MAX ];
1299- int stats_count = client_stats_get_all (stats , CLIENT_STATS_MAX );
1301+ int stats_count = client_stats_enabled ? client_stats_get_all (stats , CLIENT_STATS_MAX ) : 0 ;
13001302
13011303 printf ("\nClient Details:\n" );
1302- printf ("MAC Address IP Address Device Name TX / RX\n" );
1303- printf ("---------------- --------------- ------------------- ------------------\n" );
1304+ if (client_stats_enabled ) {
1305+ printf ("MAC Address IP Address Device Name TX / RX\n" );
1306+ printf ("---------------- --------------- ------------------- ------------------\n" );
1307+ } else {
1308+ printf ("MAC Address IP Address Device Name\n" );
1309+ printf ("---------------- --------------- -------------------\n" );
1310+ }
13041311
13051312 for (int i = 0 ; i < count ; i ++ ) {
13061313 char mac_str [18 ];
@@ -1315,19 +1322,21 @@ static int show(int argc, char **argv)
13151322 sprintf (ip_str , IPSTR , IP2STR (& addr ));
13161323 }
13171324
1318- // Find matching traffic stats by MAC
1319- char traffic_str [32 ] = "-" ;
1320- for (int j = 0 ; j < stats_count ; j ++ ) {
1321- if (memcmp (stats [j ].mac , clients [i ].mac , 6 ) == 0 ) {
1322- char tx_buf [12 ], rx_buf [12 ];
1323- format_bytes_human (stats [j ].bytes_sent , tx_buf , sizeof (tx_buf ));
1324- format_bytes_human (stats [j ].bytes_received , rx_buf , sizeof (rx_buf ));
1325- snprintf (traffic_str , sizeof (traffic_str ), "%s / %s" , tx_buf , rx_buf );
1326- break ;
1325+ if (client_stats_enabled ) {
1326+ char traffic_str [32 ] = "-" ;
1327+ for (int j = 0 ; j < stats_count ; j ++ ) {
1328+ if (memcmp (stats [j ].mac , clients [i ].mac , 6 ) == 0 ) {
1329+ char tx_buf [12 ], rx_buf [12 ];
1330+ format_bytes_human (stats [j ].bytes_sent , tx_buf , sizeof (tx_buf ));
1331+ format_bytes_human (stats [j ].bytes_received , rx_buf , sizeof (rx_buf ));
1332+ snprintf (traffic_str , sizeof (traffic_str ), "%s / %s" , tx_buf , rx_buf );
1333+ break ;
1334+ }
13271335 }
1336+ printf ("%-17s %-15s %-19s %s\n" , mac_str , ip_str , clients [i ].name , traffic_str );
1337+ } else {
1338+ printf ("%-17s %-15s %s\n" , mac_str , ip_str , clients [i ].name );
13281339 }
1329-
1330- printf ("%-17s %-15s %-19s %s\n" , mac_str , ip_str , clients [i ].name , traffic_str );
13311340 }
13321341 }
13331342 }
@@ -2073,6 +2082,46 @@ static void register_set_ttl(void)
20732082 ESP_ERROR_CHECK ( esp_console_cmd_register (& cmd ) );
20742083}
20752084
2085+ /* 'client_stats' command - enable/disable per-client traffic statistics */
2086+ static int client_stats_cmd (int argc , char * * argv )
2087+ {
2088+ if (argc < 2 ) {
2089+ printf ("Per-client stats: %s\n" , client_stats_enabled ? "enabled" : "disabled" );
2090+ printf ("Usage: client_stats <enable|disable>\n" );
2091+ return 0 ;
2092+ }
2093+
2094+ bool enable ;
2095+ if (strcmp (argv [1 ], "enable" ) == 0 ) {
2096+ enable = true;
2097+ } else if (strcmp (argv [1 ], "disable" ) == 0 ) {
2098+ enable = false;
2099+ } else {
2100+ printf ("Usage: client_stats <enable|disable>\n" );
2101+ return 1 ;
2102+ }
2103+
2104+ esp_err_t err = set_config_param_int ("cstats_en" , enable ? 1 : 0 );
2105+ if (err != ESP_OK ) {
2106+ printf ("Failed to save setting\n" );
2107+ return 1 ;
2108+ }
2109+ client_stats_enabled = enable ;
2110+ printf ("Per-client stats %s.\n" , enable ? "enabled" : "disabled" );
2111+ return 0 ;
2112+ }
2113+
2114+ static void register_client_stats_cmd (void )
2115+ {
2116+ const esp_console_cmd_t cmd = {
2117+ .command = "client_stats" ,
2118+ .help = "Enable or disable per-client traffic statistics" ,
2119+ .hint = NULL ,
2120+ .func = & client_stats_cmd ,
2121+ };
2122+ ESP_ERROR_CHECK ( esp_console_cmd_register (& cmd ) );
2123+ }
2124+
20762125/* 'set_tx_power' command - set WiFi TX power */
20772126static int set_tx_power_cmd (int argc , char * * argv )
20782127{
0 commit comments