Files
luban-lite-t3e-pro/packages/third-party/lwip/nettools/nettools.c

125 lines
2.9 KiB
C
Raw Normal View History

2024-04-03 16:40:57 +08:00
/*
* Copyright (c) 2023, ArtInChip Technology Co., Ltd
*
* SPDX-License-Identifier: Apache-2.0
2024-06-04 19:00:30 +08:00
*
* Authors: lv.wu <lv.wu@artinchip.com>
2024-04-03 16:40:57 +08:00
*/
#include "lwip/netif.h"
#include "stdio.h"
#include "string.h"
#include "lwip/dhcp.h"
2024-06-04 19:00:30 +08:00
#if defined(RT_USING_FINSH)
#include <finsh.h>
#elif defined(AIC_CONSOLE_BARE_DRV)
#include <console.h>
#endif
2024-04-03 16:40:57 +08:00
static void list_ifconfig(void)
{
struct netif *netif;
for (netif = netif_list; netif != NULL; netif = netif->next) {
2024-06-04 19:00:30 +08:00
printf("%c%c%d:\n",netif->name[0], netif->name[1], netif->num);
printf(" IPv4 Address : %s\n", ip_ntoa(&netif->ip_addr));
printf(" Default Gateway: %s\n", ip_ntoa(&netif->gw));
printf(" Subnet mask : %s\n", ip_ntoa(&netif->netmask));
#if LWIP_IPV6
printf(" IPv6 Local address : %s\n", ip6addr_ntoa(netif_ip6_addr(netif, 0)));
#endif
2024-04-03 16:40:57 +08:00
printf(" MAC addr : %02x:%02x:%02x:%02x:%02x:%02x\n",
netif->hwaddr[0],
netif->hwaddr[1],
netif->hwaddr[2],
netif->hwaddr[3],
netif->hwaddr[4],
netif->hwaddr[5]);
}
}
2024-06-04 19:00:30 +08:00
static int ifconfig(int argc, char *argv[])
2024-04-03 16:40:57 +08:00
{
struct netif *netif;
ip4_addr_t ip;
ip4_addr_t gw;
ip4_addr_t netmask;
if (argc == 1) {
list_ifconfig();
2024-06-04 19:00:30 +08:00
return 0;
2024-04-03 16:40:57 +08:00
}
if (argc != 5)
goto usage;
netif = netif_find(argv[1]);
if (netif == NULL) {
printf("Can't find interface name %s\n", argv[1]);
2024-06-04 19:00:30 +08:00
return -1;
2024-04-03 16:40:57 +08:00
}
if (!(ip4addr_aton(argv[2], &ip) && \
ip4addr_aton(argv[3], &gw) && \
ip4addr_aton(argv[4], &netmask))) {
printf("Error ip information\n\tip:%s, gw:%s, netmask: %s\n",
argv[2], argv[3], argv[4]);
2024-06-04 19:00:30 +08:00
return -1;
2024-04-03 16:40:57 +08:00
}
#if LWIP_DHCP
dhcp_stop(netif);
#endif
netif_set_down(netif);
netif_set_gw(netif, &gw);
netif_set_netmask(netif, &netmask);
netif_set_ipaddr(netif, &ip);
netif_set_up(netif);
2024-06-04 19:00:30 +08:00
return 0;
2024-04-03 16:40:57 +08:00
usage:
printf("ifconfig [interface] [ip] [gw] [netmask]\n");
2024-06-04 19:00:30 +08:00
return -1;
2024-04-03 16:40:57 +08:00
}
#ifdef RT_USING_FINSH
MSH_CMD_EXPORT(ifconfig, list/config all net information);
2024-06-04 19:00:30 +08:00
#elif defined(AIC_CONSOLE_BARE_DRV)
CONSOLE_CMD(ifconfig, ifconfig, "list/config all net information");
2024-04-03 16:40:57 +08:00
#endif
#if LWIP_DHCP
2024-06-04 19:00:30 +08:00
static int dhcpc(int argc, char *argv[])
2024-04-03 16:40:57 +08:00
{
struct netif *netif;
if (argc != 3)
goto usage;
netif = netif_find(argv[1]);
if (netif == NULL) {
printf("Can't find interface name %s\n", argv[1]);
2024-06-04 19:00:30 +08:00
return -1;
2024-04-03 16:40:57 +08:00
}
if (!strcmp(argv[2], "start"))
dhcp_start(netif);
else if (!strcmp(argv[2], "stop"))
dhcp_stop(netif);
2024-06-04 19:00:30 +08:00
return 0;
2024-04-03 16:40:57 +08:00
usage:
printf("dhcpc [interface] [start/stop]\n");
2024-06-04 19:00:30 +08:00
return -1;
2024-04-03 16:40:57 +08:00
}
#ifdef RT_USING_FINSH
2024-06-04 19:00:30 +08:00
MSH_CMD_EXPORT(dhcpc, dhcp client start or stop);
#elif defined(AIC_CONSOLE_BARE_DRV)
CONSOLE_CMD(dhcpc, dhcpc, "dhcp client start or stop");
2024-04-03 16:40:57 +08:00
#endif
#endif /* LWIP_DHCP */