/* * cmd_ip.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 #include #include "esp_log.h" #include "esp_console.h" #include "argtable3/argtable3.h" #include "esp_netif.h" #include "esp_mac.h" #include "lwip/inet.h" #include "app_console.h" static struct { struct arg_lit *addr; struct arg_lit *help; struct arg_end *end; } ip_args; static void print_iface_details(const char* key) { esp_netif_t *netif = esp_netif_get_handle_from_ifkey(key); if (!netif) return; const char *desc = esp_netif_get_desc(netif); bool is_up = esp_netif_is_netif_up(netif); uint8_t mac[6] = {0}; esp_netif_get_mac(netif, mac); esp_netif_ip_info_t ip_info; esp_netif_get_ip_info(netif, &ip_info); printf("%s (%s): <%s>\n", key, desc ? desc : "auth", is_up ? "UP" : "DOWN"); printf(" link/ether %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); if (is_up && ip_info.ip.addr != 0) { printf(" inet " IPSTR " netmask " IPSTR " broadcast " IPSTR "\n", IP2STR(&ip_info.ip), IP2STR(&ip_info.netmask), IP2STR(&ip_info.gw)); } } static int cmd_ip(int argc, char **argv) { int nerrors = arg_parse(argc, argv, (void **)&ip_args); if (nerrors > 0) { arg_print_errors(stderr, ip_args.end, argv[0]); return 1; } if (ip_args.help->count > 0) { printf("Usage: ip [addr]\n"); return 0; } // Explicitly check standard interfaces print_iface_details("WIFI_STA_DEF"); print_iface_details("WIFI_AP_DEF"); print_iface_details("ETH_DEF"); return 0; } void register_ip_cmd(void) { ip_args.addr = arg_lit0("a", "addr", "Show addresses"); ip_args.help = arg_lit0("h", "help", "Help"); ip_args.end = arg_end(1); const esp_console_cmd_t cmd = { .command = "ip", .help = "Show network interfaces", .func = &cmd_ip, .argtable = &ip_args }; ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); }