142 lines
5.0 KiB
C
142 lines
5.0 KiB
C
/*
|
|
* cmd_monitor.c
|
|
*
|
|
* Copyright (c) 2025 Umber Networks & Robert McMahon
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
* contributors may be used to endorse or promote products derived from
|
|
* this software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "esp_log.h"
|
|
#include "esp_console.h"
|
|
#include "argtable3/argtable3.h"
|
|
#include "wifi_controller.h"
|
|
#include "app_console.h"
|
|
|
|
// --- Subcommand Arguments ---
|
|
static struct {
|
|
struct arg_int *channel;
|
|
struct arg_end *end;
|
|
} start_args;
|
|
|
|
static struct {
|
|
struct arg_int *channel;
|
|
struct arg_end *end;
|
|
} channel_args;
|
|
|
|
static void print_monitor_usage(void) {
|
|
printf("Usage: monitor <subcommand> [args]\n");
|
|
printf("Subcommands:\n");
|
|
printf(" start [-c <n>] Start Monitor Mode (optional: set channel)\n");
|
|
printf(" stop Stop Monitor Mode\n");
|
|
printf(" status Show current status\n");
|
|
printf(" channel <n> Switch channel (while running)\n");
|
|
printf(" save Save current config to NVS\n");
|
|
printf(" reload Reload config from NVS\n");
|
|
printf(" clear Clear NVS config\n");
|
|
}
|
|
|
|
// --- Subcommand Handlers ---
|
|
|
|
static int do_monitor_start(int argc, char **argv) {
|
|
start_args.channel = arg_int0("c", "channel", "<n>", "Channel (1-13)");
|
|
start_args.end = arg_end(1);
|
|
|
|
int nerrors = arg_parse(argc, argv, (void **)&start_args);
|
|
if (nerrors > 0) {
|
|
arg_print_errors(stderr, start_args.end, argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
int ch = 0;
|
|
if (start_args.channel->count > 0) {
|
|
ch = start_args.channel->ival[0];
|
|
}
|
|
|
|
printf("Starting Monitor Mode%s...\n", ch ? " on specific channel" : "");
|
|
wifi_ctl_monitor_start(ch);
|
|
return 0;
|
|
}
|
|
|
|
static int do_monitor_channel(int argc, char **argv) {
|
|
channel_args.channel = arg_int1(NULL, NULL, "<n>", "Channel (1-13)");
|
|
channel_args.end = arg_end(1);
|
|
|
|
int nerrors = arg_parse(argc, argv, (void **)&channel_args);
|
|
if (nerrors > 0) {
|
|
arg_print_errors(stderr, channel_args.end, argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
int ch = channel_args.channel->ival[0];
|
|
printf("Switching to Channel %d...\n", ch);
|
|
wifi_ctl_set_channel(ch);
|
|
return 0;
|
|
}
|
|
|
|
static int cmd_monitor(int argc, char **argv) {
|
|
if (argc < 2) {
|
|
print_monitor_usage();
|
|
return 0;
|
|
}
|
|
|
|
if (strcmp(argv[1], "start") == 0) return do_monitor_start(argc - 1, &argv[1]);
|
|
if (strcmp(argv[1], "stop") == 0) { wifi_ctl_stop(); return 0; }
|
|
if (strcmp(argv[1], "status") == 0) { wifi_ctl_status(); return 0; }
|
|
if (strcmp(argv[1], "save") == 0) { wifi_ctl_param_save(NULL); printf("Saved.\n"); return 0; }
|
|
if (strcmp(argv[1], "reload") == 0) { wifi_ctl_param_init(); printf("Reloaded.\n"); return 0; }
|
|
if (strcmp(argv[1], "clear") == 0) { wifi_ctl_param_clear(); printf("Cleared.\n"); return 0; }
|
|
|
|
if (strcmp(argv[1], "channel") == 0) return do_monitor_channel(argc - 1, &argv[1]);
|
|
|
|
if (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0) {
|
|
print_monitor_usage();
|
|
return 0;
|
|
}
|
|
|
|
printf("Unknown subcommand '%s'.\n", argv[1]);
|
|
print_monitor_usage();
|
|
return 1;
|
|
}
|
|
|
|
void register_monitor_cmd(void) {
|
|
start_args.channel = arg_int0("c", "channel", "<n>", "Channel");
|
|
start_args.end = arg_end(1);
|
|
|
|
channel_args.channel = arg_int1(NULL, NULL, "<n>", "Channel");
|
|
channel_args.end = arg_end(1);
|
|
|
|
const esp_console_cmd_t cmd = {
|
|
.command = "monitor",
|
|
.help = "Monitor Mode: start, stop, channel, status",
|
|
.hint = "<subcommand>",
|
|
.func = &cmd_monitor,
|
|
};
|
|
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
|
|
}
|