/* Console example — Ethernet commands This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ #include #include #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_netif.h" #include "esp_log.h" #include "esp_console.h" #include "esp_event.h" #include "esp_eth.h" #include "esp_mac.h" #include "driver/gpio.h" #include "argtable3/argtable3.h" #include "sdkconfig.h" #include "protocol_examples_common.h" static const char *TAG = "cmd"; /* "ethernet" command */ static struct { struct arg_lit *info; struct arg_str *ip; struct arg_str *netmask; struct arg_str *gw; struct arg_lit *dhcp; struct arg_end *end; } eth_control_args; static int eth_cmd_control(int argc, char **argv) { esp_netif_ip_info_t ip; int nerrors = arg_parse(argc, argv, (void **)ð_control_args); if (nerrors != 0) { arg_print_errors(stderr, eth_control_args.end, argv[0]); return 1; } esp_eth_handle_t eth_handle = get_example_eth_handle(); esp_netif_t *eth_netif = get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH); if(eth_handle == NULL || eth_netif == NULL) { ESP_LOGE(TAG,"not find any ethernet interface information"); return 0; } esp_netif_get_ip_info(eth_netif, &ip); // if (!strncmp(eth_control_args.info->sval[0], "info", 4)) { if(eth_control_args.info->count > 0){ uint8_t mac_addr[6]; esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr); ESP_LOGI(TAG,"HW ADDR: " MACSTR "", MAC2STR(mac_addr)); ESP_LOGI(TAG,"ETHIP: " IPSTR "", IP2STR(&ip.ip)); ESP_LOGI(TAG,"ETHMASK: " IPSTR "", IP2STR(&ip.netmask)); ESP_LOGI(TAG,"ETHGW: " IPSTR "", IP2STR(&ip.gw)); } // int hasValidArgs = (options.ip != NULL || options.netmask != NULL || options.gw != NULL); // int hasConflictingArgs = (options.info && hasValidArgs) || (options.dhcp && hasValidArgs); // if (nerrors > 0 || hasConflictingArgs) if(eth_control_args.dhcp->count > 0) { if(esp_netif_dhcpc_start(eth_netif) != ESP_OK) { ESP_LOGE(TAG,"dhcp failed"); } } else { esp_netif_dhcpc_stop(eth_netif); if (eth_control_args.ip->count > 0 ) { ip.ip.addr = esp_ip4addr_aton(eth_control_args.ip->sval[0]); } if (eth_control_args.gw->count > 0 ){ ip.gw.addr = esp_ip4addr_aton(eth_control_args.gw->sval[0]); } if (eth_control_args.netmask->count> 0 ){ ip.netmask.addr = esp_ip4addr_aton(eth_control_args.netmask->sval[0]); } if(esp_netif_set_ip_info(eth_netif,&ip) != ESP_OK) { ESP_LOGE(TAG,"set ip info failed"); } } return 0; } void register_ethernet(void) { // eth_control_args.info = arg_str1(NULL, NULL, "", "Get info of Ethernet"); eth_control_args.info = arg_lit0("i", "info", "list eth card information"); eth_control_args.ip = arg_str0(NULL, "ip", "", "set IP address"); eth_control_args.netmask = arg_str0(NULL, "netmask", "", "set netmask"); eth_control_args.gw = arg_str0(NULL, "gw", "", "set gateway address"); eth_control_args.dhcp = arg_lit0(NULL, "dhcp", "Use DHCP for IP assignment"); eth_control_args.end = arg_end(1); const esp_console_cmd_t cmd = { .command = "eth", .help = "Ethernet interface IO control", .hint = NULL, .func = eth_cmd_control, .argtable = ð_control_args }; ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); }