23/07/27 1.Add netdev module and for edu-arm32, fit Lwip to it; 2.Fit Webnet to edu-arm32;

This commit is contained in:
涂煜洋
2023-07-27 15:05:50 +08:00
parent 1a6ee0234b
commit 0307fb2671
29 changed files with 1884 additions and 627 deletions
@@ -31,55 +31,54 @@
*/
/**
* @file sys_arch.c
* @brief In order to adapt to XiZi, some changes have been made to implement the LwIP interface.
* @version 1.0
* @author AIIT XUOS Lab
* @date 2021-05-29
*/
* @file sys_arch.c
* @brief In order to adapt to XiZi, some changes have been made to implement the LwIP interface.
* @version 1.0
* @author AIIT XUOS Lab
* @date 2021-05-29
*/
#include "debug.h"
#include <lwip/opt.h>
#include <lwip/arch.h>
#include <lwip/opt.h>
#include "tcpip.h"
#include "lwip/dhcp.h"
#include "lwip/init.h"
#include "lwip/netif.h"
#include "lwip/sio.h"
#include <lwip/stats.h>
#include <lwip/debug.h>
#include <lwip/sys.h>
#include "lwip/dhcp.h"
#include "tcpip.h"
#include "tcpip_priv.h"
#include <lwip/debug.h>
#include <lwip/stats.h>
#include <lwip/sys.h>
#if !NO_SYS
#include "sys_arch.h"
#endif
#include <string.h>
#include <xs_assign.h>
#include <xs_ktask.h>
#include <xs_ktick.h>
#include <xs_assign.h>
#include <xs_sem.h>
#include <xs_mutex.h>
#include <xs_ktask.h>
#include <xs_msg.h>
#include <xs_mutex.h>
#include <xs_sem.h>
#include "board.h"
#include "ethernet.h"
#include "connect_ethernet.h"
#include "ethernet.h"
char lwip_ipaddr[20] = {192, 168, 130, 77};
char lwip_netmask[20] = {255, 255, 254, 0};
char lwip_gwaddr[20] = {192, 168, 130, 1};
char lwip_ipaddr[20] = { 192, 168, 130, 77 };
char lwip_netmask[20] = { 255, 255, 254, 0 };
char lwip_gwaddr[20] = { 192, 168, 130, 1 };
char lwip_eth0_ipaddr[20] = {192, 168, 130, 77};
char lwip_eth0_netmask[20] = {255, 255, 254, 0};
char lwip_eth0_gwaddr[20] = {192, 168, 130, 1};
char lwip_eth0_ipaddr[20] = { 192, 168, 130, 77 };
char lwip_eth0_netmask[20] = { 255, 255, 254, 0 };
char lwip_eth0_gwaddr[20] = { 192, 168, 130, 1 };
char lwip_eth1_ipaddr[20] = {192, 168, 130, 99};
char lwip_eth1_netmask[20] = {255, 255, 254, 0};
char lwip_eth1_gwaddr[20] = {192, 168, 130, 23};
char lwip_eth1_ipaddr[20] = { 192, 168, 130, 99 };
char lwip_eth1_netmask[20] = { 255, 255, 254, 0 };
char lwip_eth1_gwaddr[20] = { 192, 168, 130, 23 };
char lwip_flag = 0;
@@ -88,220 +87,238 @@ x_ticks_t lwip_sys_now;
#define SYS_THREAD_MAX 4
struct netif gnetif;
sys_sem_t* get_eth_recv_sem() {
sys_sem_t* get_eth_recv_sem()
{
static sys_sem_t g_recv_sem = 0;
return &g_recv_sem;
}
void sys_init(void) {
// do nothing
void sys_init(void)
{
// do nothing
}
u32_t
sys_jiffies(void) {
lwip_sys_now = CurrentTicksGain();
return lwip_sys_now;
u32_t sys_jiffies(void)
{
lwip_sys_now = CurrentTicksGain();
return lwip_sys_now;
}
u32_t
sys_now(void) {
lwip_sys_now = CurrentTicksGain();
return CalculateTimeMsFromTick(lwip_sys_now);
u32_t sys_now(void)
{
lwip_sys_now = CurrentTicksGain();
return CalculateTimeMsFromTick(lwip_sys_now);
}
sys_prot_t sys_arch_protect(void) {
return CriticalAreaLock();
sys_prot_t sys_arch_protect(void)
{
return CriticalAreaLock();
}
void sys_arch_unprotect(sys_prot_t pval) {
CriticalAreaUnLock(pval);
void sys_arch_unprotect(sys_prot_t pval)
{
CriticalAreaUnLock(pval);
}
#if !NO_SYS
err_t
sys_sem_new(sys_sem_t *sem, u8_t count) {
*sem = KSemaphoreCreate((uint16)count);
err_t sys_sem_new(sys_sem_t* sem, u8_t count)
{
*sem = KSemaphoreCreate((uint16)count);
#if SYS_STATS
++lwip_stats.sys.sem.used;
if (lwip_stats.sys.sem.max < lwip_stats.sys.sem.used) {
lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
}
#endif /* SYS_STATS */
if(*sem >= 0)
return ERR_OK;
else {
#if SYS_STATS
++lwip_stats.sys.sem.err;
#endif /* SYS_STATS */
KPrintf("[sys_arch]:new sem fail!\n");
return ERR_MEM;
}
}
void
sys_sem_free(sys_sem_t *sem) {
#if SYS_STATS
--lwip_stats.sys.sem.used;
#endif /* SYS_STATS */
KSemaphoreDelete(*sem);
*sem = SYS_SEM_NULL;
}
int sys_sem_valid(sys_sem_t *sem) {
return (*sem > SYS_SEM_NULL);
}
void
sys_sem_set_invalid(sys_sem_t *sem) {
*sem = SYS_SEM_NULL;
}
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) {
x_ticks_t start_tick = 0 ;
int32 wait_time = 0;
if(*sem == SYS_SEM_NULL)
return SYS_ARCH_TIMEOUT;
start_tick = CurrentTicksGain();
if (0 == timeout)
wait_time = WAITING_FOREVER;
else
wait_time = timeout;
if(KSemaphoreObtain(*sem, wait_time) == EOK)
return CalculateTimeMsFromTick(CurrentTicksGain()-start_tick);
else
return SYS_ARCH_TIMEOUT;
}
void sys_sem_signal(sys_sem_t *sem) {
if(KSemaphoreAbandon(*sem) != EOK)
KPrintf("[sys_arch]:sem signal fail!\n");
}
err_t sys_mutex_new(sys_mutex_t *mutex) {
*mutex = KMutexCreate();
if (*mutex > SYS_MRTEX_NULL)
return ERR_OK;
else {
KPrintf("[sys_arch]:new mutex fail!\n");
return ERR_MEM;
}
}
void sys_mutex_free(sys_mutex_t *mutex) {
KMutexDelete(*mutex);
}
void sys_mutex_set_invalid(sys_mutex_t *mutex) {
*mutex = SYS_MRTEX_NULL;
}
void sys_mutex_lock(sys_mutex_t *mutex) {
KMutexObtain(*mutex, WAITING_FOREVER);
}
void sys_mutex_unlock(sys_mutex_t *mutex) {
KMutexAbandon(*mutex);
}
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn function, void *arg, int stacksize, int prio) {
sys_thread_t handle = -1;
handle = KTaskCreate(name,
function,
arg,
(uint32)stacksize,
(uint8)prio);
if (handle >= 0) {
StartupKTask(handle);
lw_print("lw: [%s] create %s handle %x\n", __func__, name, handle);
return handle;
}
lw_print("lw: [%s] create %s failed\n", __func__, name);
return -ERROR;
}
err_t sys_mbox_new(sys_mbox_t *mbox, int size) {
*mbox = KCreateMsgQueue(sizeof(void *), size);
#if SYS_STATS
++lwip_stats.sys.mbox.used;
if (lwip_stats.sys.mbox.max < lwip_stats.sys.mbox.used) {
lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
++lwip_stats.sys.sem.used;
if (lwip_stats.sys.sem.max < lwip_stats.sys.sem.used) {
lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
}
#endif /* SYS_STATS */
if(*mbox < 0) {
lw_print("lw: [%s] alloc %d mbox %p failed\n", __func__, size, mbox);
return ERR_MEM;
}
lw_print("lw: [%s] alloc %d mbox %p ok!\n", __func__, size, mbox);
return ERR_OK;
if (*sem >= 0)
return ERR_OK;
else {
#if SYS_STATS
++lwip_stats.sys.sem.err;
#endif /* SYS_STATS */
KPrintf("[sys_arch]:new sem fail!\n");
return ERR_MEM;
}
}
void sys_mbox_free(sys_mbox_t *mbox) {
KDeleteMsgQueue(*mbox);
void sys_sem_free(sys_sem_t* sem)
{
#if SYS_STATS
--lwip_stats.sys.sem.used;
#endif /* SYS_STATS */
KSemaphoreDelete(*sem);
*sem = SYS_SEM_NULL;
}
int sys_mbox_valid(sys_mbox_t *mbox) {
if (*mbox <= SYS_MBOX_NULL)
return 0;
else
return 1;
int sys_sem_valid(sys_sem_t* sem)
{
return (*sem > SYS_SEM_NULL);
}
void sys_mbox_set_invalid(sys_mbox_t *mbox) {
*mbox = SYS_MBOX_NULL;
void sys_sem_set_invalid(sys_sem_t* sem)
{
*sem = SYS_SEM_NULL;
}
void sys_mbox_post(sys_mbox_t *q, void *msg) {
KMsgQueueSendwait(*q, &msg, sizeof(void *), WAITING_FOREVER);
u32_t sys_arch_sem_wait(sys_sem_t* sem, u32_t timeout)
{
x_ticks_t start_tick = 0;
int32 wait_time = 0;
if (*sem == SYS_SEM_NULL)
return SYS_ARCH_TIMEOUT;
start_tick = CurrentTicksGain();
if (0 == timeout)
wait_time = WAITING_FOREVER;
else
wait_time = timeout;
if (KSemaphoreObtain(*sem, wait_time) == EOK)
return CalculateTimeMsFromTick(CurrentTicksGain() - start_tick);
else
return SYS_ARCH_TIMEOUT;
}
err_t sys_mbox_trypost(sys_mbox_t *q, void *msg) {
// if(KMsgQueueSend(*q, &msg, sizeof(void *)) == EOK)
if(KMsgQueueSend(*q, &msg, sizeof(void *)) == EOK)
void sys_sem_signal(sys_sem_t* sem)
{
if (KSemaphoreAbandon(*sem) != EOK)
KPrintf("[sys_arch]:sem signal fail!\n");
}
err_t sys_mutex_new(sys_mutex_t* mutex)
{
*mutex = KMutexCreate();
if (*mutex > SYS_MRTEX_NULL)
return ERR_OK;
else {
KPrintf("[sys_arch]:new mutex fail!\n");
return ERR_MEM;
}
}
void sys_mutex_free(sys_mutex_t* mutex)
{
KMutexDelete(*mutex);
}
void sys_mutex_set_invalid(sys_mutex_t* mutex)
{
*mutex = SYS_MRTEX_NULL;
}
void sys_mutex_lock(sys_mutex_t* mutex)
{
KMutexObtain(*mutex, WAITING_FOREVER);
}
void sys_mutex_unlock(sys_mutex_t* mutex)
{
KMutexAbandon(*mutex);
}
sys_thread_t sys_thread_new(const char* name, lwip_thread_fn function, void* arg, int stacksize, int prio)
{
sys_thread_t handle = -1;
handle = KTaskCreate(name,
function,
arg,
(uint32)stacksize,
(uint8)prio);
if (handle >= 0) {
StartupKTask(handle);
lw_print("lw: [%s] create %s handle %x\n", __func__, name, handle);
return handle;
}
lw_print("lw: [%s] create %s failed\n", __func__, name);
return -ERROR;
}
err_t sys_mbox_new(sys_mbox_t* mbox, int size)
{
*mbox = KCreateMsgQueue(sizeof(void*), size);
#if SYS_STATS
++lwip_stats.sys.mbox.used;
if (lwip_stats.sys.mbox.max < lwip_stats.sys.mbox.used) {
lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
}
#endif /* SYS_STATS */
if (*mbox < 0) {
lw_print("lw: [%s] alloc %d mbox %p failed\n", __func__, size, mbox);
return ERR_MEM;
}
lw_print("lw: [%s] alloc %d mbox %p ok!\n", __func__, size, mbox);
return ERR_OK;
else
return ERR_MEM;
}
err_t sys_mbox_trypost_fromisr(sys_mbox_t *q, void *msg) {
return sys_mbox_trypost(q, msg);
void sys_mbox_free(sys_mbox_t* mbox)
{
KDeleteMsgQueue(*mbox);
}
u32_t sys_arch_mbox_fetch(sys_mbox_t *q, void **msg, u32_t timeout) {
x_ticks_t start_tick = 0 ;
int32 wait_time = 0;
start_tick = CurrentTicksGain();
if (0 == timeout)
wait_time = WAITING_FOREVER;
else
wait_time = timeout;
if(KMsgQueueRecv(*q, &(*msg), sizeof(void *), wait_time) == EOK) {
return CalculateTimeMsFromTick(CurrentTicksGain() - start_tick);
} else {
return SYS_ARCH_TIMEOUT;
}
int sys_mbox_valid(sys_mbox_t* mbox)
{
if (*mbox <= SYS_MBOX_NULL)
return 0;
else
return 1;
}
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *q, void **msg) {
if (KMsgQueueRecv(*q, &(*msg), sizeof(void *), 0) == EOK)
return ERR_OK;
else
return SYS_MBOX_EMPTY;
void sys_mbox_set_invalid(sys_mbox_t* mbox)
{
*mbox = SYS_MBOX_NULL;
}
void sys_mbox_post(sys_mbox_t* q, void* msg)
{
KMsgQueueSendwait(*q, &msg, sizeof(void*), WAITING_FOREVER);
}
err_t sys_mbox_trypost(sys_mbox_t* q, void* msg)
{
// if(KMsgQueueSend(*q, &msg, sizeof(void *)) == EOK)
if (KMsgQueueSend(*q, &msg, sizeof(void*)) == EOK)
return ERR_OK;
else
return ERR_MEM;
}
err_t sys_mbox_trypost_fromisr(sys_mbox_t* q, void* msg)
{
return sys_mbox_trypost(q, msg);
}
u32_t sys_arch_mbox_fetch(sys_mbox_t* q, void** msg, u32_t timeout)
{
x_ticks_t start_tick = 0;
int32 wait_time = 0;
start_tick = CurrentTicksGain();
if (0 == timeout)
wait_time = WAITING_FOREVER;
else
wait_time = timeout;
if (KMsgQueueRecv(*q, &(*msg), sizeof(void*), wait_time) == EOK) {
return CalculateTimeMsFromTick(CurrentTicksGain() - start_tick);
} else {
return SYS_ARCH_TIMEOUT;
}
}
u32_t sys_arch_mbox_tryfetch(sys_mbox_t* q, void** msg)
{
if (KMsgQueueRecv(*q, &(*msg), sizeof(void*), 0) == EOK)
return ERR_OK;
else
return SYS_MBOX_EMPTY;
}
#if LWIP_NETCONN_SEM_PER_THREAD
@@ -315,128 +332,129 @@ ip4_addr_t ipaddr;
ip4_addr_t netmask;
ip4_addr_t gw;
void lwip_config_input(struct netif *net) {
sys_thread_t th_id = 0;
void lwip_config_input(struct netif* net)
{
sys_thread_t th_id = 0;
th_id = sys_thread_new("eth_input", ethernetif_input, net, LWIP_TASK_STACK_SIZE, 20);
th_id = sys_thread_new("eth_input", ethernetif_input, net, LWIP_TASK_STACK_SIZE, 20);
if (th_id >= 0) {
lw_print("%s %d successfully!\n", __func__, th_id);
} else {
lw_print("%s failed!\n", __func__);
}
if (th_id >= 0) {
lw_print("%s %d successfully!\n", __func__, th_id);
} else {
lw_print("%s failed!\n", __func__);
}
}
void lwip_config_tcp(uint8_t enet_port, char *ip, char *mask, char *gw) {
ip4_addr_t net_ipaddr, net_netmask, net_gw;
char* eth_cfg;
void lwip_config_tcp(uint8_t enet_port, char* ip, char* mask, char* gw)
{
ip4_addr_t net_ipaddr, net_netmask, net_gw;
char* eth_cfg;
eth_cfg = ethernetif_config_enet_set(enet_port);
eth_cfg = ethernetif_config_enet_set(enet_port);
if(chk_lwip_bit(LWIP_INIT_FLAG)) {
lw_print("lw: [%s] already ...\n", __func__);
return;
}
if (chk_lwip_bit(LWIP_INIT_FLAG)) {
lw_print("lw: [%s] already ...\n", __func__);
return;
}
set_lwip_bit(LWIP_INIT_FLAG);
set_lwip_bit(LWIP_INIT_FLAG);
tcpip_init(NULL, NULL);
tcpip_init(NULL, NULL);
lw_print("lw: [%s] start ...\n", __func__);
IP4_ADDR(&net_ipaddr, ip[0], ip[1], ip[2], ip[3]);
IP4_ADDR(&net_netmask, mask[0], mask[1], mask[2], mask[3]);
IP4_ADDR(&net_gw, gw[0], gw[1], gw[2], gw[3]);
if (0 == enet_port) {
#ifdef NETIF_ENET0_INIT_FUNC
printf("[%s:%d] call netif_add\n", __func__, __LINE__);
netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, eth_cfg, NETIF_ENET0_INIT_FUNC,
tcpip_input);
#endif
} else if (1 == enet_port) {
#ifdef NETIF_ENET1_INIT_FUNC
netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, eth_cfg, NETIF_ENET1_INIT_FUNC,
tcpip_input);
#endif
}
netif_set_default(&gnetif);
netif_set_up(&gnetif);
lw_print("\r\n************************************************\r\n");
lw_print(" Network Configuration\r\n");
lw_print("************************************************\r\n");
lw_print(" IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&net_ipaddr)[0], ((u8_t *)&net_ipaddr)[1],
((u8_t *)&net_ipaddr)[2], ((u8_t *)&net_ipaddr)[3]);
lw_print(" IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&net_netmask)[0], ((u8_t *)&net_netmask)[1],
((u8_t *)&net_netmask)[2], ((u8_t *)&net_netmask)[3]);
lw_print(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&net_gw)[0], ((u8_t *)&net_gw)[1],
((u8_t *)&net_gw)[2], ((u8_t *)&net_gw)[3]);
lw_print("************************************************\r\n");
lwip_config_input(&gnetif);
}
void lwip_config_net(uint8_t enet_port, char *ip, char *mask, char *gw) {
ip4_addr_t net_ipaddr, net_netmask, net_gw;
char* eth_cfg;
eth_cfg = ethernetif_config_enet_set(enet_port);
if(chk_lwip_bit(LWIP_INIT_FLAG)) {
lw_print("lw: [%s] already ...\n", __func__);
lw_print("lw: [%s] start ...\n", __func__);
IP4_ADDR(&net_ipaddr, ip[0], ip[1], ip[2], ip[3]);
IP4_ADDR(&net_netmask, mask[0], mask[1], mask[2], mask[3]);
IP4_ADDR(&net_gw, gw[0], gw[1], gw[2], gw[3]);
// update ip addr
netif_set_down(&gnetif);
netif_set_gw(&gnetif, &net_gw);
netif_set_netmask(&gnetif, &net_netmask);
netif_set_ipaddr(&gnetif, &net_ipaddr);
netif_set_up(&gnetif);
return;
}
set_lwip_bit(LWIP_INIT_FLAG);
lw_print("lw: [%s] start ...\n", __func__);
IP4_ADDR(&net_ipaddr, ip[0], ip[1], ip[2], ip[3]);
IP4_ADDR(&net_netmask, mask[0], mask[1], mask[2], mask[3]);
IP4_ADDR(&net_gw, gw[0], gw[1], gw[2], gw[3]);
lwip_init();
if(0 == enet_port) {
if (0 == enet_port) {
#ifdef NETIF_ENET0_INIT_FUNC
netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, eth_cfg, NETIF_ENET0_INIT_FUNC,
ethernet_input);
printf("[%s:%d] call netif_add\n", __func__, __LINE__);
netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, eth_cfg, NETIF_ENET0_INIT_FUNC,
tcpip_input);
#endif
} else if (1 == enet_port) {
} else if (1 == enet_port) {
#ifdef NETIF_ENET1_INIT_FUNC
netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, eth_cfg, NETIF_ENET1_INIT_FUNC,
ethernet_input);
netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, eth_cfg, NETIF_ENET1_INIT_FUNC,
tcpip_input);
#endif
}
netif_set_default(&gnetif);
netif_set_up(&gnetif);
}
if(chk_lwip_bit(LWIP_PRINT_FLAG)) {
lw_notice("\r\n************************************************\r\n");
lw_notice(" Network Configuration\r\n");
lw_notice("************************************************\r\n");
lw_notice(" IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&net_ipaddr)[0], ((u8_t *)&net_ipaddr)[1],
((u8_t *)&net_ipaddr)[2], ((u8_t *)&net_ipaddr)[3]);
lw_notice(" IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&net_netmask)[0], ((u8_t *)&net_netmask)[1],
((u8_t *)&net_netmask)[2], ((u8_t *)&net_netmask)[3]);
lw_notice(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&net_gw)[0], ((u8_t *)&net_gw)[1],
((u8_t *)&net_gw)[2], ((u8_t *)&net_gw)[3]);
lw_notice("************************************************\r\n");
}
lwip_config_input(&gnetif);
netif_set_default(&gnetif);
netif_set_up(&gnetif);
lw_print("\r\n************************************************\r\n");
lw_print(" Network Configuration\r\n");
lw_print("************************************************\r\n");
lw_print(" IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t*)&net_ipaddr)[0], ((u8_t*)&net_ipaddr)[1],
((u8_t*)&net_ipaddr)[2], ((u8_t*)&net_ipaddr)[3]);
lw_print(" IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t*)&net_netmask)[0], ((u8_t*)&net_netmask)[1],
((u8_t*)&net_netmask)[2], ((u8_t*)&net_netmask)[3]);
lw_print(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t*)&net_gw)[0], ((u8_t*)&net_gw)[1],
((u8_t*)&net_gw)[2], ((u8_t*)&net_gw)[3]);
lw_print("************************************************\r\n");
lwip_config_input(&gnetif);
}
void lwip_config_net(uint8_t enet_port, char* ip, char* mask, char* gw)
{
ip4_addr_t net_ipaddr, net_netmask, net_gw;
char* eth_cfg;
eth_cfg = ethernetif_config_enet_set(enet_port);
if (chk_lwip_bit(LWIP_INIT_FLAG)) {
lw_print("lw: [%s] already ...\n", __func__);
IP4_ADDR(&net_ipaddr, ip[0], ip[1], ip[2], ip[3]);
IP4_ADDR(&net_netmask, mask[0], mask[1], mask[2], mask[3]);
IP4_ADDR(&net_gw, gw[0], gw[1], gw[2], gw[3]);
// update ip addr
netif_set_down(&gnetif);
netif_set_gw(&gnetif, &net_gw);
netif_set_netmask(&gnetif, &net_netmask);
netif_set_ipaddr(&gnetif, &net_ipaddr);
netif_set_up(&gnetif);
return;
}
set_lwip_bit(LWIP_INIT_FLAG);
lw_print("lw: [%s] start ...\n", __func__);
IP4_ADDR(&net_ipaddr, ip[0], ip[1], ip[2], ip[3]);
IP4_ADDR(&net_netmask, mask[0], mask[1], mask[2], mask[3]);
IP4_ADDR(&net_gw, gw[0], gw[1], gw[2], gw[3]);
lwip_init();
if (0 == enet_port) {
#ifdef NETIF_ENET0_INIT_FUNC
netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, eth_cfg, NETIF_ENET0_INIT_FUNC,
ethernet_input);
#endif
} else if (1 == enet_port) {
#ifdef NETIF_ENET1_INIT_FUNC
netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, eth_cfg, NETIF_ENET1_INIT_FUNC,
ethernet_input);
#endif
}
netif_set_default(&gnetif);
netif_set_up(&gnetif);
if (chk_lwip_bit(LWIP_PRINT_FLAG)) {
lw_notice("\r\n************************************************\r\n");
lw_notice(" Network Configuration\r\n");
lw_notice("************************************************\r\n");
lw_notice(" IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t*)&net_ipaddr)[0], ((u8_t*)&net_ipaddr)[1],
((u8_t*)&net_ipaddr)[2], ((u8_t*)&net_ipaddr)[3]);
lw_notice(" IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t*)&net_netmask)[0], ((u8_t*)&net_netmask)[1],
((u8_t*)&net_netmask)[2], ((u8_t*)&net_netmask)[3]);
lw_notice(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t*)&net_gw)[0], ((u8_t*)&net_gw)[1],
((u8_t*)&net_gw)[2], ((u8_t*)&net_gw)[3]);
lw_notice("************************************************\r\n");
}
lwip_config_input(&gnetif);
}
@@ -314,7 +314,7 @@ ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen)
*rp++ = '.';
ap++;
}
*--rp = 0;
*--rp = '\0';
return buf;
}
@@ -1,4 +1,4 @@
SRC_DIR += cmd_lwip
SRC_DIR += cmd_lwip netdev
LWIP_DIR := LwIP
include $(KERNEL_ROOT)/compiler.mk
@@ -19,48 +19,45 @@
*/
#include <board.h>
#include <sys_arch.h>
#include <shell.h>
#include <sys.h>
#include <string.h>
#include <sys.h>
#include <sys_arch.h>
#include <xs_kdbg.h>
#include <netdev.h>
/******************************************************************************/
uint8_t enet_id = 0;
static void LwipSetIPTask(void *param)
static void LwipSetIPTask(void* param)
{
uint8_t enet_port = *(uint8_t *)param; ///< test enet port
uint8_t enet_port = *(uint8_t*)param; ///< test enet port
printf("lw: [%s] config netport id[%d]\n", __func__, enet_port);
// lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
lwip_config_tcp(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
}
void LwipSetIPTest(int argc, char *argv[])
void LwipSetIPTest(int argc, char* argv[])
{
if(argc >= 4)
{
if (argc >= 4) {
printf("lw: [%s] ip %s mask %s gw %s netport %s\n", __func__, argv[1], argv[2], argv[3], argv[4]);
sscanf(argv[1], "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1], &lwip_ipaddr[2], &lwip_ipaddr[3]);
sscanf(argv[2], "%d.%d.%d.%d", &lwip_netmask[0], &lwip_netmask[1], &lwip_netmask[2], &lwip_netmask[3]);
sscanf(argv[3], "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1], &lwip_gwaddr[2], &lwip_gwaddr[3]);
sscanf(argv[4], "%d", &enet_id);
if(0 == enet_id)
{
if (0 == enet_id) {
printf("save eth0 info\n");
memcpy(lwip_eth0_ipaddr, lwip_ipaddr, 20);
memcpy(lwip_eth0_netmask, lwip_netmask, 20);
memcpy(lwip_eth0_gwaddr, lwip_gwaddr, 20);
}
else if(1 == enet_id)
{
} else if (1 == enet_id) {
printf("save eth1 info\n");
memcpy(lwip_eth1_ipaddr, lwip_ipaddr, 20);
memcpy(lwip_eth1_netmask, lwip_netmask, 20);
memcpy(lwip_eth1_gwaddr, lwip_gwaddr, 20);
}
}
else if(argc == 2)
{
} else if (argc == 2) {
printf("lw: [%s] set eth0 ipaddr %s \n", __func__, argv[1]);
sscanf(argv[1], "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1], &lwip_ipaddr[2], &lwip_ipaddr[3]);
memcpy(lwip_eth0_ipaddr, lwip_ipaddr, strlen(lwip_ipaddr));
@@ -70,39 +67,56 @@ void LwipSetIPTest(int argc, char *argv[])
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(5),
setip, LwipSetIPTest, setip [IP] [Netmask] [Gateway] [port]);
setip, LwipSetIPTest, setip[IP][Netmask][Gateway][port]);
void LwipShowIPTask(int argc, char *argv[])
void LwipShowIPTask(int argc, char* argv[])
{
#ifdef configMAC_ADDR
char mac_addr0[] = configMAC_ADDR;
#endif
// find default netdev
struct netdev* netdev = netdev_get_by_name("en\0");
if (netdev == NULL) {
lw_notice("[%s] Netdev not found by name en\n");
struct netdev* default_netdev = NETDEV_DEFAULT;
}
lw_notice("\r\n************************************************\r\n");
lw_notice(" Network Configuration\r\n");
lw_notice("************************************************\r\n");
lw_notice(" ETH0 IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_eth0_ipaddr)[0], ((u8_t *)&lwip_eth0_ipaddr)[1],
((u8_t *)&lwip_eth0_ipaddr)[2], ((u8_t *)&lwip_eth0_ipaddr)[3]);
lw_notice(" ETH0 IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_eth0_netmask)[0], ((u8_t *)&lwip_eth0_netmask)[1],
((u8_t *)&lwip_eth0_netmask)[2], ((u8_t *)&lwip_eth0_netmask)[3]);
lw_notice(" ETH0 IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_gwaddr)[0], ((u8_t *)&lwip_eth0_gwaddr)[1],
((u8_t *)&lwip_eth0_gwaddr)[2], ((u8_t *)&lwip_eth0_gwaddr)[3]);
// lw_notice(" ETH0 IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t*)&lwip_eth0_ipaddr)[0], ((u8_t*)&lwip_eth0_ipaddr)[1],
// ((u8_t*)&lwip_eth0_ipaddr)[2], ((u8_t*)&lwip_eth0_ipaddr)[3]);
// lw_notice(" ETH0 IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t*)&lwip_eth0_netmask)[0], ((u8_t*)&lwip_eth0_netmask)[1],
// ((u8_t*)&lwip_eth0_netmask)[2], ((u8_t*)&lwip_eth0_netmask)[3]);
// lw_notice(" ETH0 IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t*)&lwip_gwaddr)[0], ((u8_t*)&lwip_eth0_gwaddr)[1],
// ((u8_t*)&lwip_eth0_gwaddr)[2], ((u8_t*)&lwip_eth0_gwaddr)[3]);
// #ifdef configMAC_ADDR
// lw_notice(" ETH0 MAC Address : %x:%x:%x:%x:%x:%x\r\n", mac_addr0[0], mac_addr0[1], mac_addr0[2],
// mac_addr0[3], mac_addr0[4], mac_addr0[5]);
// #endif
lw_notice(" ETH0 IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t*)&lwip_eth0_ipaddr)[0], ((u8_t*)&lwip_eth0_ipaddr)[1],
((u8_t*)&lwip_eth0_ipaddr)[2], ((u8_t*)&lwip_eth0_ipaddr)[3]);
lw_notice(" ETH0 IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t*)&lwip_eth0_netmask)[0], ((u8_t*)&lwip_eth0_netmask)[1],
((u8_t*)&lwip_eth0_netmask)[2], ((u8_t*)&lwip_eth0_netmask)[3]);
lw_notice(" ETH0 IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t*)&lwip_gwaddr)[0], ((u8_t*)&lwip_eth0_gwaddr)[1],
((u8_t*)&lwip_eth0_gwaddr)[2], ((u8_t*)&lwip_eth0_gwaddr)[3]);
#ifdef configMAC_ADDR
lw_notice(" ETH0 MAC Address : %x:%x:%x:%x:%x:%x\r\n", mac_addr0[0], mac_addr0[1], mac_addr0[2],
mac_addr0[3], mac_addr0[4], mac_addr0[5]);
#endif
#ifdef BOARD_NET_COUNT
if(BOARD_NET_COUNT > 1)
{
if (BOARD_NET_COUNT > 1) {
char mac_addr1[] = configMAC_ADDR_ETH1;
lw_notice("\r\n");
lw_notice(" ETH1 IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_eth1_ipaddr)[0], ((u8_t *)&lwip_eth1_ipaddr)[1],
((u8_t *)&lwip_eth1_ipaddr)[2], ((u8_t *)&lwip_eth1_ipaddr)[3]);
lw_notice(" ETH1 IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_eth1_netmask)[0], ((u8_t *)&lwip_eth1_netmask)[1],
((u8_t *)&lwip_eth1_netmask)[2], ((u8_t *)&lwip_eth1_netmask)[3]);
lw_notice(" ETH1 IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_eth1_gwaddr)[0], ((u8_t *)&lwip_eth1_gwaddr)[1],
((u8_t *)&lwip_eth1_gwaddr)[2], ((u8_t *)&lwip_eth1_gwaddr)[3]);
lw_notice(" ETH1 IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t*)&lwip_eth1_ipaddr)[0], ((u8_t*)&lwip_eth1_ipaddr)[1],
((u8_t*)&lwip_eth1_ipaddr)[2], ((u8_t*)&lwip_eth1_ipaddr)[3]);
lw_notice(" ETH1 IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t*)&lwip_eth1_netmask)[0], ((u8_t*)&lwip_eth1_netmask)[1],
((u8_t*)&lwip_eth1_netmask)[2], ((u8_t*)&lwip_eth1_netmask)[3]);
lw_notice(" ETH1 IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t*)&lwip_eth1_gwaddr)[0], ((u8_t*)&lwip_eth1_gwaddr)[1],
((u8_t*)&lwip_eth1_gwaddr)[2], ((u8_t*)&lwip_eth1_gwaddr)[3]);
lw_notice(" ETH1 MAC Address : %x:%x:%x:%x:%x:%x\r\n", mac_addr1[0], mac_addr1[1], mac_addr1[2],
mac_addr1[3], mac_addr1[4], mac_addr1[5]);
}
@@ -111,5 +125,4 @@ void LwipShowIPTask(int argc, char *argv[])
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
showip, LwipShowIPTask, GetIp [IP] [Netmask] [Gateway]);
showip, LwipShowIPTask, GetIp[IP][Netmask][Gateway]);
@@ -0,0 +1,4 @@
SRC_FILES += netdev_register.c netdev_manipulate.c netdev_lowlevel.c
include $(KERNEL_ROOT)/compiler.mk
@@ -0,0 +1,220 @@
#include <netdev.h>
#include <string.h>
#include <xs_kdbg.h>
/**
* This function will set network interface device IP address.
* @NOTE it can only be called in the network interface device driver.
*
* @param netdev the network interface device to change
* @param ip_addr the new IP address
*/
void netdev_low_level_set_ipaddr(struct netdev* netdev, const ip_addr_t* ip_addr)
{
CHECK(ip_addr);
if (netdev && ip_addr_cmp(&(netdev->ip_addr), ip_addr) == 0) {
ip_addr_copy(netdev->ip_addr, *ip_addr);
/* execute IP address change callback function */
if (netdev->addr_callback) {
netdev->addr_callback(netdev, NETDEV_CB_ADDR_IP);
}
}
}
/**
* This function will set network interface device netmask address.
* @NOTE it can only be called in the network interface device driver.
*
* @param netdev the network interface device to change
* @param netmask the new netmask address
*/
void netdev_low_level_set_netmask(struct netdev* netdev, const ip_addr_t* netmask)
{
CHECK(netmask);
if (netdev && ip_addr_cmp(&(netdev->netmask), netmask) == 0) {
ip_addr_copy(netdev->netmask, *netmask);
/* execute netmask address change callback function */
if (netdev->addr_callback) {
netdev->addr_callback(netdev, NETDEV_CB_ADDR_NETMASK);
}
}
}
/**
* This function will set network interface device gateway address.
* @NOTE it can only be called in the network interface device driver.
*
* @param netdev the network interface device to change
* @param gw the new gateway address
*/
void netdev_low_level_set_gw(struct netdev* netdev, const ip_addr_t* gw)
{
CHECK(gw);
if (netdev && ip_addr_cmp(&(netdev->gw), gw) == 0) {
ip_addr_copy(netdev->gw, *gw);
/* execute gateway address change callback function */
if (netdev->addr_callback) {
netdev->addr_callback(netdev, NETDEV_CB_ADDR_GATEWAY);
}
}
}
/**
* This function will set network interface device DNS server address.
* @NOTE it can only be called in the network interface device driver.
*
* @param netdev the network interface device to change
* @param dns_num the number of the DNS server
* @param dns_server the new DNS server address
*
*/
void netdev_low_level_set_dns_server(struct netdev* netdev, uint8_t dns_num, const ip_addr_t* dns_server)
{
unsigned int index;
CHECK(dns_server);
if (netdev == NULL) {
return;
}
/* check DNS servers is exist */
for (index = 0; index < NETDEV_DNS_SERVERS_NUM; index++) {
if (ip_addr_cmp(&(netdev->dns_servers[index]), dns_server)) {
return;
}
}
if (dns_num < NETDEV_DNS_SERVERS_NUM) {
ip_addr_copy(netdev->dns_servers[dns_num], *dns_server);
/* execute DNS servers address change callback function */
if (netdev->addr_callback) {
netdev->addr_callback(netdev, NETDEV_CB_ADDR_DNS_SERVER);
}
}
}
/* Change to the first link_up network interface device automatically */
static void netdev_auto_change_default(struct netdev* netdev)
{
struct netdev* new_netdev = NULL;
if (netdev->flags & NETDEV_FLAG_LINK_UP) {
if (!(NETDEV_DEFAULT->flags & NETDEV_FLAG_LINK_UP)) {
netdev_set_default(netdev);
}
return;
}
if (memcmp(netdev, NETDEV_DEFAULT, sizeof(struct netdev)) == 0) {
new_netdev = netdev_get_first_by_flags(NETDEV_FLAG_LINK_UP);
if (new_netdev) {
netdev_set_default(new_netdev);
}
}
}
/**
* This function will set network interface device status.
* @NOTE it can only be called in the network interface device driver.
*
* @param netdev the network interface device to change
* @param is_up the new status
*/
void netdev_low_level_set_status(struct netdev* netdev, bool is_up)
{
if (netdev && netdev_is_up(netdev) != is_up) {
if (is_up) {
netdev->flags |= NETDEV_FLAG_UP;
} else {
netdev->flags &= ~NETDEV_FLAG_UP;
/* change to the first link_up network interface device automatically */
netdev_auto_change_default(netdev);
}
/* execute network interface device status change callback function */
if (netdev->status_callback) {
netdev->status_callback(netdev, is_up ? NETDEV_CB_STATUS_UP : NETDEV_CB_STATUS_DOWN);
}
}
}
/**
* This function will set network interface device active link status.
* @NOTE it can only be called in the network interface device driver.
*
* @param netdev the network interface device to change
* @param is_up the new link status
*/
void netdev_low_level_set_link_status(struct netdev* netdev, bool is_up)
{
if (netdev && netdev_is_link_up(netdev) != is_up) {
if (is_up) {
netdev->flags |= NETDEV_FLAG_LINK_UP;
} else {
netdev->flags &= ~NETDEV_FLAG_LINK_UP;
/* set network interface device flags to internet down */
netdev->flags &= ~NETDEV_FLAG_INTERNET_UP;
netdev_auto_change_default(netdev);
}
/* execute link status change callback function */
if (netdev->status_callback) {
netdev->status_callback(netdev, is_up ? NETDEV_CB_STATUS_LINK_UP : NETDEV_CB_STATUS_LINK_DOWN);
}
}
}
/**
* This function will set network interface device active internet status.
* @NOTE it can only be called in the network interface device driver.
*
* @param netdev the network interface device to change
* @param is_up the new internet status
*/
void netdev_low_level_set_internet_status(struct netdev* netdev, bool is_up)
{
if (netdev && netdev_is_internet_up(netdev) != is_up) {
if (is_up) {
netdev->flags |= NETDEV_FLAG_INTERNET_UP;
} else {
netdev->flags &= ~NETDEV_FLAG_INTERNET_UP;
}
/* execute network interface device status change callback function */
if (netdev->status_callback) {
netdev->status_callback(netdev, is_up ? NETDEV_CB_STATUS_INTERNET_UP : NETDEV_CB_STATUS_INTERNET_DOWN);
}
}
}
/**
* This function will set network interface device DHCP status.
* @NOTE it can only be called in the network interface device driver.
*
* @param netdev the network interface device to change
* @param is_up the new DHCP status
*/
void netdev_low_level_set_dhcp_status(struct netdev* netdev, bool is_enable)
{
if (netdev && netdev_is_dhcp_enabled(netdev) != is_enable) {
if (is_enable) {
netdev->flags |= NETDEV_FLAG_DHCP;
} else {
netdev->flags &= ~NETDEV_FLAG_DHCP;
}
/* execute DHCP status change callback function */
if (netdev->status_callback) {
netdev->status_callback(netdev, is_enable ? NETDEV_CB_STATUS_DHCP_ENABLE : NETDEV_CB_STATUS_DHCP_DISABLE);
}
}
}
@@ -0,0 +1,288 @@
#include <netdev.h>
#include <string.h>
#include <xs_isr.h>
#include <xs_kdbg.h>
inline int netdev_check_set_addr_condition(struct netdev* netdev)
{
if (NULL == netdev->ops || NULL == netdev->ops->set_addr_info) {
SYS_KDEBUG_LOG(NETDEV_DEBUG, ("The network interface device(%s) not support to set IP address.\n", netdev->name));
return -ERROR;
}
// check whether dhcp enabled
if (netdev_is_dhcp_enabled(netdev)) {
SYS_KDEBUG_LOG(NETDEV_DEBUG, ("The network interface device(%s) DHCP capability is enable, not support set IP address.\n", netdev->name));
return -ERROR;
}
return EOK;
}
/**
* This function will set network interface device IP address.
*
* @param netdev the network interface device to change
* @param ip_addr the new IP address
*
* @return 0: set IP address successfully
* -1: set IP address failed
*/
int netdev_set_ipaddr(struct netdev* netdev, const ip_addr_t* ipaddr)
{
CHECK(netdev);
CHECK(ipaddr);
if (EOK != netdev_check_set_addr_condition(netdev)) {
return -ERROR;
}
return netdev->ops->set_addr_info(netdev, (ip_addr_t*)ipaddr, NULL, NULL);
}
/**
* This function will set network interface device netmask address.
*
* @param netdev the network interface device to change
* @param netmask the new netmask address
*
* @return 0: set netmask address successfully
* -1: set netmask address failed
*/
int netdev_set_netmask(struct netdev* netdev, const ip_addr_t* netmask)
{
CHECK(netdev);
CHECK(netmask);
if (EOK != netdev_check_set_addr_condition(netdev)) {
return -ERROR;
}
return netdev->ops->set_addr_info(netdev, NULL, (ip_addr_t*)netmask, NULL);
}
/**
* This function will set network interface device gateway address.
*
* @param netdev the network interface device to change
* @param gw the new gateway address
*
* @return 0: set gateway address successfully
* -1: set gateway address failed
*/
int netdev_set_gw(struct netdev* netdev, const ip_addr_t* gw)
{
CHECK(netdev);
CHECK(gw);
if (EOK != netdev_check_set_addr_condition(netdev)) {
return -ERROR;
}
/* execute network interface device set gateway address operations */
return netdev->ops->set_addr_info(netdev, NULL, NULL, (ip_addr_t*)gw);
}
/**
* This function will set network interface device DNS server address.
*
* @param netdev the network interface device to change
* @param dns_num the number of the DNS server
* @param dns_server the new DNS server address
*
* @return 0: set netmask address successfully
* -1: set netmask address failed
*/
int netdev_set_dns_server(struct netdev* netdev, uint8_t dns_num, const ip_addr_t* dns_server)
{
CHECK(netdev);
CHECK(dns_server);
// check dns server number
if (dns_num >= NETDEV_DNS_SERVERS_NUM) {
SYS_KDEBUG_LOG(NETDEV_DEBUG, ("The number of DNS servers(%d) set exceeds the maximum number(%d).\n", dns_num + 1, NETDEV_DNS_SERVERS_NUM));
return -ERROR;
}
// check dns set function existence
if (NULL == netdev->ops || NULL == netdev->ops->set_dns_server) {
SYS_KDEBUG_LOG(NETDEV_DEBUG, ("The network interface device(%s) not support to set DNS server address.\n", netdev->name));
return -ERROR;
}
return netdev->ops->set_dns_server(netdev, dns_num, (ip_addr_t*)dns_server);
}
/**
* This function will set callback to be called when the network interface device status has been changed.
*
* @param netdev the network interface device to change
* @param status_callback the callback be called when the status has been changed.
*/
void netdev_set_status_callback(struct netdev* netdev, netdev_callback_fn status_callback)
{
CHECK(netdev);
CHECK(status_callback);
netdev->status_callback = status_callback;
}
/**
* This function will set callback to be called when the network interface device address has been changed.
*
* @param netdev the network interface device to change
* @param addr_callback the callback be called when the address has been changed.
*/
void netdev_set_addr_callback(struct netdev* netdev, netdev_callback_fn addr_callback)
{
CHECK(netdev);
CHECK(addr_callback);
netdev->addr_callback = addr_callback;
}
/**
* This function will enable network interface device .
*
* @param netdev the network interface device to change
*
* @return 0: set status successfully
* -1: set status failed
*/
int netdev_set_up(struct netdev* netdev)
{
CHECK(netdev);
if (!netdev->ops || !netdev->ops->set_up) {
SYS_KDEBUG_LOG(NETDEV_DEBUG, ("The network interface device(%s) not support to set status.\n", netdev->name));
return -ERROR;
}
/* network interface device status flags check */
if (netdev_is_up(netdev)) {
return EOK;
}
/* execute enable network interface device operations by network interface device driver */
return netdev->ops->set_up(netdev);
}
/**
* This function will disable network interface device.
*
* @param netdev the network interface device to change
*
* @return 0: set status successfully
* -1: set sttaus failed
*/
int netdev_set_down(struct netdev* netdev)
{
CHECK(netdev);
if (!netdev->ops || !netdev->ops->set_down) {
SYS_KDEBUG_LOG(NETDEV_DEBUG, ("The network interface device(%s) not support to set status.\n", netdev->name));
return -ERROR;
}
/* network interface device status flags check */
if (!netdev_is_up(netdev)) {
return EOK;
}
/* execute disable network interface device operations by network interface driver */
return netdev->ops->set_down(netdev);
}
/**
* This function will get the first network interface device
* with the flags in network interface device list.
*
* @param flags the network interface device flags
*
* @return != NULL: network interface device object
* NULL: get failed
*/
struct netdev* netdev_get_first_by_flags(uint16_t flags)
{
if (NETDEV_LISTHEAD == NULL) {
return NULL;
}
// get netdev from list
x_base lock = DISABLE_INTERRUPT();
struct netdev* current_dev = NETDEV_LISTHEAD;
SINGLE_LINKLIST_FOR_EACH_ENTRY(current_dev, &(NETDEV_LISTHEAD->list), list)
{
if (NULL != current_dev && 0 != (current_dev->flags & flags)) {
ENABLE_INTERRUPT(lock);
return current_dev;
}
}
ENABLE_INTERRUPT(lock);
return NULL;
}
/**
* This function will get the first network interface device
* in network interface device list by IP address.
*
* @param ip_addr the network interface device IP address
*
* @return != NULL: network interface device object
* NULL: get failed
*/
struct netdev* netdev_get_by_ipaddr(ip_addr_t* ip_addr)
{
if (NETDEV_LISTHEAD == NULL) {
return NULL;
}
// get netdev from list
x_base lock = DISABLE_INTERRUPT();
struct netdev* current_dev = NETDEV_LISTHEAD;
SINGLE_LINKLIST_FOR_EACH_ENTRY(current_dev, &(NETDEV_LISTHEAD->list), list)
{
if (NULL != current_dev && ip_addr_cmp(&(current_dev->ip_addr), ip_addr)) {
ENABLE_INTERRUPT(lock);
return current_dev;
}
}
ENABLE_INTERRUPT(lock);
return NULL;
}
/**
* This function will get network interface device
* in network interface device list by netdev name.
*
* @param name the network interface device name
*
* @return != NULL: network interface device object
* NULL: get failed
*/
struct netdev* netdev_get_by_name(const char* name)
{
if (NETDEV_LISTHEAD == NULL) {
return NULL;
}
// get netdev from list
x_base lock = DISABLE_INTERRUPT();
struct netdev* current_dev = NETDEV_LISTHEAD;
SINGLE_LINKLIST_FOR_EACH_ENTRY(current_dev, &(NETDEV_LISTHEAD->list), list)
{
if (NULL == current_dev) {
continue;
}
uint32_t name_len = strlen(current_dev->name);
if (NULL != current_dev && (strncmp(current_dev->name, name, strlen(current_dev->name) < NAME_NUM_MAX ? strlen(current_dev->name) : NAME_NUM_MAX) == 0)) {
ENABLE_INTERRUPT(lock);
return current_dev;
}
}
ENABLE_INTERRUPT(lock);
return NULL;
}
@@ -0,0 +1,184 @@
#include <assert.h>
#include <def.h>
#include <netdev.h>
#include <netdev_ipaddr.h>
#include <string.h>
#include <xs_isr.h>
#include <xs_kdbg.h>
struct netdev** get_netdev_listhead()
{
static struct netdev* netdev_listhead = NULL;
return &netdev_listhead;
}
struct netdev** get_default_netdev()
{
static struct netdev* netdev_default = NULL;
return &netdev_default;
}
static netdev_callback_fn g_netdev_register_callback = NULL;
static netdev_callback_fn g_netdev_default_change_callback = NULL;
int netdev_register(struct netdev* netdev, const char* name, void* user_data)
{
CHECK(netdev != NULL);
CHECK(name != NULL);
// set flag mask, assert network is down
uint16_t flag_mask = 0;
flag_mask = NETDEV_FLAG_UP | NETDEV_FLAG_LINK_UP | NETDEV_FLAG_INTERNET_UP | NETDEV_FLAG_DHCP;
netdev->flags &= ~flag_mask;
// clear dev setting
ip_addr_set_zero(&(netdev->ip_addr));
ip_addr_set_zero(&(netdev->netmask));
ip_addr_set_zero(&(netdev->gw));
IP_SET_TYPE_VAL(netdev->ip_addr, IPADDR_TYPE_V4);
IP_SET_TYPE_VAL(netdev->netmask, IPADDR_TYPE_V4);
IP_SET_TYPE_VAL(netdev->gw, IPADDR_TYPE_V4);
#if NETDEV_IPV6
for (index = 0; index < NETDEV_IPV6_NUM_ADDRESSES; index++) {
ip_addr_set_zero(&(netdev->ip6_addr[index]));
IP_SET_TYPE_VAL(netdev->ip_addr, IPADDR_TYPE_V6);
}
#endif /* NETDEV_IPV6 */
// clear DNS servers
for (uint16_t idx = 0; idx < NETDEV_DNS_SERVERS_NUM; idx++) {
ip_addr_set_zero(&(netdev->dns_servers[idx]));
IP_SET_TYPE_VAL(netdev->ip_addr, IPADDR_TYPE_V4);
}
// clear callback fn
netdev->addr_callback = NULL;
netdev->status_callback = NULL;
// validate name
uint32_t name_len = strlen(name);
if (name_len < NAME_NUM_MAX) {
strncpy(netdev->name, name, name_len);
netdev->name[name_len] = '\0';
} else {
SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] name too long.\n", __func__));
strncpy(netdev->name, name, NAME_NUM_MAX - 1);
netdev->name[NAME_NUM_MAX - 1] = '\0';
}
netdev->user_data = user_data;
InitSingleLinkList(&(netdev->list));
// insert netdev to global list
x_base lock = DISABLE_INTERRUPT();
if (NETDEV_LISTHEAD == NULL) {
NETDEV_LISTHEAD = netdev;
} else {
SingleLinkListNodeInsert(&(NETDEV_LISTHEAD->list), &(netdev->list));
}
ENABLE_INTERRUPT(lock);
if (NETDEV_DEFAULT == NULL) {
// set first met netdev to default netdev
netdev_set_default(NETDEV_LISTHEAD);
}
if (g_netdev_register_callback) {
g_netdev_register_callback(netdev, NETDEV_CB_REGISTER);
}
return EOK;
}
/**
* This function will unregister network interface device and
* delete it from network interface device list.
*
* @param netdev the network interface device object
*
* @return 0: unregistered successfully
* -1: unregistered failed
*/
int netdev_unregister(struct netdev* netdev)
{
CHECK(netdev);
if (NETDEV_LISTHEAD == NULL) {
return -ERROR;
}
// remove netdev from netdev list
x_base lock = DISABLE_INTERRUPT();
struct netdev* current_dev = NETDEV_LISTHEAD;
SINGLE_LINKLIST_FOR_EACH_ENTRY(current_dev, &(NETDEV_LISTHEAD->list), list)
{
// found netdev in list
if (current_dev == netdev) {
if (NETDEV_LISTHEAD == current_dev && NULL == SingleLinkListGetNextNode(&(current_dev->list))) {
// netdev is the only one in list
NETDEV_LISTHEAD = NULL;
} else {
SingleLinkListRmNode(&(NETDEV_LISTHEAD->list), &(current_dev->list));
}
// deal default netdev
if (current_dev == NETDEV_DEFAULT) {
NETDEV_DEFAULT = NULL;
}
break;
}
}
ENABLE_INTERRUPT(lock);
if (NETDEV_DEFAULT == NULL) {
netdev_set_default(NETDEV_LISTHEAD);
}
// clean netdev if found one
if (current_dev == netdev) {
memset(netdev, 0, sizeof(*netdev));
}
return EOK;
}
/**
* This function will set default network interface device.
*
* @param netdev the network interface device to change
*/
void netdev_set_default(struct netdev* netdev)
{
if (netdev && (netdev != NETDEV_DEFAULT)) {
NETDEV_DEFAULT = netdev;
// set default function
if (netdev->ops && netdev->ops->set_default) {
netdev->ops->set_default(netdev);
}
// default change callback
if (g_netdev_default_change_callback) {
g_netdev_default_change_callback(netdev, NETDEV_CB_DEFAULT_CHANGE);
}
SYS_KDEBUG_LOG(NETDEV_DEBUG, ("Setting default network interface device name(%s) successfully.\n", netdev->name));
}
}
/**
* This function will set register callback
*
* @param register_callback the network register callback
*
*/
void netdev_set_register_callback(netdev_callback_fn register_callback)
{
g_netdev_register_callback = register_callback;
}
void netdev_set_default_change_callback(netdev_callback_fn default_change_cb)
{
g_netdev_default_change_callback = default_change_cb;
}