From f1d39b2e252af037093346d04eab988e70c8aebd Mon Sep 17 00:00:00 2001 From: lr <1234@qq.com> Date: Tue, 21 May 2024 15:30:11 +0800 Subject: [PATCH] lwip server with !NO_SYS init --- Ubiquitous/XiZi_AIoT/services/app/test_net.c | 5 +- .../services/lib/usyscall/usyscall.c | 2 +- .../services/lib/usyscall/usyscall.h | 2 +- .../services/net/libnet/lwip_service.c | 6 +- .../services/net/libnet/lwip_service.h | 4 +- .../services/net/net_server/Makefile | 4 +- .../services/net/net_server/api/tcpip.c | 4 +- .../services/net/net_server/arch/sys_arch.c | 459 +++++++++--------- .../services/net/net_server/arch/sys_arch.h | 63 ++- .../net/net_server/include/lwip/sys.h | 2 +- .../XiZi_AIoT/services/net/net_server/lwip.mk | 1 + .../services/net/net_server/lwip_server.c | 47 +- 12 files changed, 295 insertions(+), 304 deletions(-) diff --git a/Ubiquitous/XiZi_AIoT/services/app/test_net.c b/Ubiquitous/XiZi_AIoT/services/app/test_net.c index 210bb35d5..c21192f59 100644 --- a/Ubiquitous/XiZi_AIoT/services/app/test_net.c +++ b/Ubiquitous/XiZi_AIoT/services/app/test_net.c @@ -23,10 +23,7 @@ int main(int argc, char* argv[]) struct Session sess; connect_session(&sess, "LWIPServer", 4096); - char lwip_ipaddr[4] = { 192, 168, 130, 77 }; - char lwip_netmask[4] = { 255, 255, 254, 0 }; - char lwip_gwaddr[4] = { 192, 168, 130, 1 }; - LWIP_init(&sess, lwip_ipaddr, lwip_netmask, lwip_ipaddr); + LWIP_test(&sess); free_session(&sess); diff --git a/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.c b/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.c index 8038de4d6..424660154 100644 --- a/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.c +++ b/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.c @@ -26,7 +26,7 @@ int spawn(struct Session* session, int fd, ipc_read_fn ipc_read, ipc_fsize_fn ip return ret; } -int thread(void* entry, char* name, char** argv) +int thread(void* entry, const char* name, char** argv) { return syscall(SYSCALL_THREAD, (uintptr_t)entry, (uintptr_t)name, (uintptr_t)argv, 0); } diff --git a/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.h b/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.h index eda606788..014e556e8 100644 --- a/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.h +++ b/Ubiquitous/XiZi_AIoT/services/lib/usyscall/usyscall.h @@ -65,7 +65,7 @@ typedef int (*ipc_write_fn)(struct Session* session, int fd, char* src, int offs int syscall(int sys_num, intptr_t a1, intptr_t a2, intptr_t a3, intptr_t a4); int spawn(struct Session* session, int fd, ipc_read_fn ipc_read, ipc_fsize_fn ipc_fsize, char* name, char** argv); -int thread(void* entry, char* name, char** argv); +int thread(void* entry, const char* name, char** argv); void exit(int status); int yield(task_yield_reason reason); int kill(int pid); diff --git a/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.c b/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.c index 9b525c889..541e6fb0d 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.c +++ b/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.c @@ -12,9 +12,9 @@ #include "lwip_service.h" #include -IPC_INTERFACE(Ipc_LWIP_init, 3, ip, mask, gw, sizeof(char[4]), sizeof(char[4]), sizeof(char[4])) -void LWIP_init(struct Session* session, char* ip, char* mask, char* gw){ - IPC_CALL(Ipc_LWIP_init)(session, ip, mask, gw); +IPC_INTERFACE(Ipc_LWIP_test, 1, ignore, sizeof(int)) +void LWIP_test(struct Session* session){ + IPC_CALL(Ipc_LWIP_test)(session, NULL); } \ No newline at end of file diff --git a/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.h b/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.h index 0cb5de5f1..17d8ab300 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.h +++ b/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.h @@ -12,6 +12,6 @@ #include "libipc.h" -IPC_SERVICES(IpcLWIPServer, Ipc_LWIP_init) +IPC_SERVICES(IpcLWIPServer, Ipc_LWIP_test) -void LWIP_init(struct Session* session, char* ip, char* mask, char* gw); +void LWIP_test(struct Session* session); diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/Makefile b/Ubiquitous/XiZi_AIoT/services/net/net_server/Makefile index 5fdc64583..e429bbfa3 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/Makefile +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/Makefile @@ -52,9 +52,9 @@ usyscall = $(KERNEL_ROOT)/services/app/usyscall.o arch_usyscall = $(KERNEL_ROOT)/services/app/arch_usyscall.o session = $(KERNEL_ROOT)/services/app/session.o libipc = $(KERNEL_ROOT)/services/app/libipc.o - +libsem = $(KERNEL_ROOT)/services/app/libsemaphore.o lwip: COMPILER lwip_server.o | bin - @${ld} ${user_ldflags} -e main -o $@ ${sys} ${api} ${core} ${ipv4} ${netif} lwip_server.o ${libserial} ${printf} ${libmem} ${usyscall} ${arch_usyscall} ${session} ${libipc} ${board_specs} + @${ld} ${user_ldflags} -e main -o $@ ${sys} ${api} ${core} ${ipv4} ${netif} lwip_server.o ${libserial} ${printf} ${libmem} ${usyscall} ${arch_usyscall} ${session} ${libipc} ${libsem} ${board_specs} @${objdump} -S $@ > $@.asm @mv *.o bin @mv $@ $(KERNEL_ROOT)/services/app diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/api/tcpip.c b/Ubiquitous/XiZi_AIoT/services/net/net_server/api/tcpip.c index 2ee532a7c..783d1c485 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/api/tcpip.c +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/api/tcpip.c @@ -124,10 +124,10 @@ again: * @param arg unused argument */ static void -tcpip_thread(void *arg) +tcpip_thread(int argc, char **argv) { struct tcpip_msg *msg; - LWIP_UNUSED_ARG(arg); + LWIP_UNUSED_ARG(argv); LWIP_MARK_TCPIP_THREAD(); diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/sys_arch.c b/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/sys_arch.c index b31106b6a..87c1b6591 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/sys_arch.c +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/sys_arch.c @@ -26,11 +26,13 @@ #if !NO_SYS #include "sys_arch.h" +#include "libserial.h" +#include "usyscall.h" #endif #define SYS_THREAD_MAX 4 - +static char sem_server_name[] = "DefaultSemaphoreServer"; void sys_init(void) { @@ -55,215 +57,294 @@ sys_prot_t sys_arch_protect(void) 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); + struct Session sem_session; + sem_t semaphore; + connect_session(&sem_session, sem_server_name, 4096); + sem_create(&sem_session, &semaphore, (int)count); + sem->sem = semaphore; + sem->sess = sem_session; + #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 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; -// } - return 1; + if (sem->sem >= 0) + return ERR_OK; + else { +#if SYS_STATS + ++lwip_stats.sys.sem.err; +#endif /* SYS_STATS */ + printf("[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; +#if SYS_STATS + --lwip_stats.sys.sem.used; +#endif /* SYS_STATS */ + sem_delete(&sem->sess, &sem->sem); + free_session(&sem->sess); + free(sem); + sem = SYS_SEM_NULL; } int sys_sem_valid(sys_sem_t* sem) { - // return (*sem > SYS_SEM_NULL); - return 1; + return (sem->sem >= 0); } void sys_sem_set_invalid(sys_sem_t* sem) { - // *sem = SYS_SEM_NULL; + sem->sem = -1; } + u32_t sys_arch_sem_wait(sys_sem_t* sem, u32_t timeout) { - // x_ticks_t start_tick = 0; - // int32 wait_time = 0; + s32_t wait_time = 0; - // if (*sem == SYS_SEM_NULL) - // return SYS_ARCH_TIMEOUT; + if (sem->sem < 0) + 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; - return 1; + sem_wait(&sem->sess, &sem->sem, 0); + return 0; } void sys_sem_signal(sys_sem_t* sem) { - // if (KSemaphoreAbandon(*sem) != EOK) - // KPrintf("[sys_arch]:sem signal fail!\n"); + sem_signal(&sem->sess, &sem->sem); } 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; - // } - return 1; + return sys_sem_new(mutex, 1); } void sys_mutex_free(sys_mutex_t* mutex) { - // KMutexDelete(*mutex); + sys_sem_free(mutex); } void sys_mutex_set_invalid(sys_mutex_t* mutex) { - // *mutex = SYS_MRTEX_NULL; + sys_sem_set_invalid(mutex); } void sys_mutex_lock(sys_mutex_t* mutex) { - // KMutexObtain(*mutex, WAITING_FOREVER); + sys_arch_sem_wait(mutex, 0); } void sys_mutex_unlock(sys_mutex_t* mutex) { - // KMutexAbandon(*mutex); + sys_sem_signal(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; - return 1; + sys_thread_t handle = -1; + char* task_param[] = { arg, NULL }; + handle = thread(function, name, task_param); + return handle; } err_t sys_mbox_new(sys_mbox_t* mbox, int size) { - // *mbox = KCreateMsgQueue(sizeof(void*), size); + + mbox->first = mbox->last = 0; -// #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_error("lw: [%s] alloc %d mbox %p failed\n", __func__, size, mbox); -// return ERR_MEM; -// } + sys_sem_t not_empty; + sys_sem_new(¬_empty, 0); + mbox->not_empty = ¬_empty; -// // lw_print("lw: [%s] alloc %d mbox %p ok!\n", __func__, size, mbox); -// return ERR_OK; - return 1; + sys_sem_t not_full; + sys_sem_new(¬_full, 0); + mbox->not_full = ¬_full; + + sys_sem_t mutex; + sys_sem_new(&mutex, 1); + mbox->mutex = &mutex; + + mbox->wait_send = 0; + +#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 == SYS_MBOX_NULL) { + printf("lw: [%s] alloc %d mbox %p failed\n", __func__, size, mbox); + return ERR_MEM; + } + + printf("lw: [%s] alloc %d mbox %p ok!\n", __func__, size, mbox); + return ERR_OK; } void sys_mbox_free(sys_mbox_t* mbox) { - // KDeleteMsgQueue(*mbox); + if (mbox != SYS_MBOX_NULL){ + sys_arch_sem_wait(mbox->mutex, 0); + sys_sem_free(mbox->not_empty); + sys_sem_free(mbox->not_full); + sys_sem_free(mbox->mutex); + mbox->not_empty = mbox->not_full = mbox->mutex = NULL; + free(mbox); + mbox = NULL; + } } int sys_mbox_valid(sys_mbox_t* mbox) { - // if (*mbox <= SYS_MBOX_NULL) - // return 0; - // else - // return 1; - return 1; + return (mbox != SYS_MBOX_NULL); } void sys_mbox_set_invalid(sys_mbox_t* mbox) { - // *mbox = SYS_MBOX_NULL; + mbox = SYS_MBOX_NULL; } void sys_mbox_post(sys_mbox_t* q, void* msg) { - // KMsgQueueSendwait(*q, &msg, sizeof(void*), WAITING_FOREVER); + u8_t first; + if (q == NULL) + printf("lw: [%s] alloc %d mbox %p failed\n", __func__, q); + + sys_arch_sem_wait(q->mutex, 0); + + printf("sys_mbox_post: mbox %p msg %p\n", (void *)q, (void *)msg); + + while ((q->last + 1) >= (q->first + SYS_MBOX_SIZE)) { + q->wait_send++; + sys_sem_signal(q->mutex); + sys_arch_sem_wait(q->not_full, 0); + sys_arch_sem_wait(q->mutex, 0); + q->wait_send--; + } + + q->msgs[q->last % SYS_MBOX_SIZE] = msg; + + if (q->last == q->first) { + first = 1; + } else { + first = 0; + } + + q->last++; + + if (first) { + sys_sem_signal(q->not_empty); + } + sys_sem_signal(q->mutex); } 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; - return 1; + u8_t first; + if (q == NULL) + printf("lw: [%s] alloc %d mbox %p failed\n", __func__, q); + + sys_arch_sem_wait(q->mutex, 0); + + printf("sys_mbox_trypost: mbox %p msg %p\n",(void *)q, (void *)msg); + + if ((q->last + 1) >= (q->first + SYS_MBOX_SIZE)) { + sys_sem_signal(q->mutex); + return ERR_MEM; + } + + q->msgs[q->last % SYS_MBOX_SIZE] = msg; + + if (q->last == q->first) { + first = 1; + } else { + first = 0; + } + + q->last++; + + if (first) { + sys_sem_signal(q->not_empty); + } + + sys_sem_signal(q->mutex); + + return ERR_OK; } err_t sys_mbox_trypost_fromisr(sys_mbox_t* q, void* msg) { - // return sys_mbox_trypost(q, msg); - return 1; + 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; + if (q == NULL) + printf("lw: [%s] alloc %d mbox %p failed\n", __func__, q); + + sys_arch_sem_wait(q->mutex, 0); - // start_tick = CurrentTicksGain(); + while (q->first == q->last) { + sys_sem_signal(q->mutex); + sys_arch_sem_wait(q->not_empty, 0); + sys_arch_sem_wait(q->mutex, 0); + } - // if (0 == timeout) - // wait_time = WAITING_FOREVER; - // else - // wait_time = timeout; + if (msg != NULL) { + printf("sys_mbox_fetch: mbox %p msg %p\n", (void *)q, *msg); + *msg = q->msgs[q->first % SYS_MBOX_SIZE]; + } + else{ + printf("sys_mbox_fetch: mbox %p, null msg\n", (void *)q); + } - // if (KMsgQueueRecv(*q, &(*msg), sizeof(void*), wait_time) == EOK) { - // return CalculateTimeMsFromTick(CurrentTicksGain() - start_tick); - // } else { - // return SYS_ARCH_TIMEOUT; - // } - return 1; + q->first++; + + if (q->wait_send) { + sys_sem_signal(q->not_full); + } + + sys_sem_signal(q->mutex); + + return 0; } 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; - return 1; + if (q == NULL) + printf("lw: [%s] alloc %d mbox %p failed\n", __func__, q); + + sys_arch_sem_wait(q->mutex, 0); + + if (q->first == q->last) { + sys_sem_signal(q->mutex); + return SYS_MBOX_EMPTY; + } + + if (msg != NULL) { + printf("sys_mbox_tryfetch: mbox %p msg %p\n", (void *)q, *msg); + *msg = q->msgs[q->first % SYS_MBOX_SIZE]; + } + else{ + printf("sys_mbox_tryfetch: mbox %p, null msg\n", (void *)q); + } + + q->first++; + + if (q->wait_send) { + sys_sem_signal(q->not_full); + } + + sys_sem_signal(q->mutex); + + return 0; } #if LWIP_NETCONN_SEM_PER_THREAD @@ -273,109 +354,55 @@ u32_t sys_arch_mbox_tryfetch(sys_mbox_t* q, void** msg) #endif /* !NO_SYS */ /* Variables Initialization */ -ip4_addr_t ipaddr; -ip4_addr_t netmask; -ip4_addr_t gw; +ip4_addr_t net_ipaddr; +ip4_addr_t net_netmask; +ip4_addr_t net_gw; +struct netif gnetif; -void lwip_config_input(int eport, struct netif* net) +int lwip_config_tcp(char* ip, char* mask, char* gw) { -// sys_thread_t th_id = 0; + printf("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 (eport == 0) { -// th_id = sys_thread_new("eth_input", ethernetif_input, net, LWIP_TASK_STACK_SIZE, 30); -// } else if (eport == 1) { -// #ifdef NETIF_ENET1_INIT_FUNC -// th_id = sys_thread_new("eth_input2", ethernetif_input2, net, LWIP_TASK_STACK_SIZE, 30); -// #endif -// } -} -void lwip_config_tcp(uint8_t enet_port, char* ip, char* mask, char* gw) -{ -// static char is_init_0 = 0; -// static char is_init_1 = 0; -// if (is_init_0 == 0 && is_init_1 == 0) { -// sys_sem_new(get_eth_recv_sem(), 0); -// sys_sem_new(get_eth_recv_sem2(), 0); + tcpip_init(NULL, NULL); -// if (chk_lwip_bit(LWIP_INIT_FLAG)) { -// lw_print("lw: [%s] already ...\n", __func__); -// } +#ifndef NETIF_ENET_INIT_FUNC + printf("Not Netif driver for Eport 0\n"); + return 0; +#endif -// set_lwip_bit(LWIP_INIT_FLAG); +#ifdef NETIF_ENET_INIT_FUNC + netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, NULL, + NETIF_ENET_INIT_FUNC, &tcpip_input); -// tcpip_init(NULL, NULL); -// } + /* Registers the default network interface */ + netif_set_default(&gnetif); -// lw_print("lw: [%s] start ...\n", __func__); - -// char* eth_cfg = ethernetif_config_enet_set(enet_port); -// ip4_addr_t net_ipaddr, net_netmask, net_gw; -// 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) { -// if (is_init_0 == 1) { -// return; -// } -// #ifndef NETIF_ENET0_INIT_FUNC -// lw_print("Not Netif driver for Eport 0\n"); -// return; -// #endif -// #ifdef NETIF_ENET0_INIT_FUNC -// lw_print("Add netif eport 0\n"); -// netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, eth_cfg, NETIF_ENET0_INIT_FUNC, -// tcpip_input); - -// 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(enet_port, &gnetif); -// is_init_0 = 1; -// #endif - -// } else if (1 == enet_port) { -// if (is_init_1 == 1) { -// return; -// } -// #ifndef NETIF_ENET1_INIT_FUNC -// lw_print("Not Netif driver for Eport 1\n"); -// return; -// #endif -// #ifdef NETIF_ENET1_INIT_FUNC -// lw_print("Add netif eport 1\n"); -// netif_add(&gnetif2, &net_ipaddr, &net_netmask, &net_gw, eth_cfg, NETIF_ENET1_INIT_FUNC, -// tcpip_input); - -// // netif_set_default(&gnetif2); -// netif_set_up(&gnetif2); - -// 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(enet_port, &gnetif2); -// is_init_1 = 1; -// #endif -// } + if (netif_is_link_up(&gnetif)) + { + /*When the netif is fully configured this function must be called */ + netif_set_up(&gnetif); + } + else + { + /* When the netif link is down this function must be called */ + netif_set_down(&gnetif); + return -1; + } + printf("\r\n************************************************\r\n"); + printf(" Network Configuration\r\n"); + printf("************************************************\r\n"); + printf(" 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]); + printf(" 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]); + printf(" 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]); + printf("************************************************\r\n"); + return 0; +#endif } diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/sys_arch.h b/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/sys_arch.h index 73c24943d..01a20a2f9 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/sys_arch.h +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/sys_arch.h @@ -18,6 +18,9 @@ #include // #include "tcpip.h" +#include "libsemaphore.h" +#include "libipc.h" + /* LWIP task parameter */ #define LWIP_LOCAL_PORT 4840 #define LWIP_TARGET_PORT LWIP_LOCAL_PORT @@ -31,49 +34,37 @@ // #define configMAC_ADDR_ETH1 {0x02, 0x12, 0x13, 0x10, 0x15, 0x12} /* USER CODE END 0 */ -#define SYS_MBOX_NULL -1 -#define SYS_SEM_NULL -1 +#define SYS_MBOX_NULL NULL +#define SYS_SEM_NULL NULL #define SYS_MRTEX_NULL SYS_SEM_NULL +#define SYS_MBOX_SIZE 128 -typedef s32_t sys_sem_t; -typedef s32_t sys_mutex_t; -typedef s32_t sys_mbox_t; -typedef s32_t sys_thread_t; +struct sys_sem +{ + sem_t sem; + struct Session sess; +}; +typedef struct sys_sem sys_sem_t; +typedef struct sys_sem sys_mutex_t; + + +struct sys_mbox{ + int first, last; + void *msgs[SYS_MBOX_SIZE]; + struct sys_sem *not_empty; + struct sys_sem *not_full; + struct sys_sem *mutex; + int wait_send; +}; +typedef struct sys_mbox sys_mbox_t; + +typedef int sys_thread_t; typedef s64_t sys_prot_t; #define MS_PER_SYSTICK (float)(1000 / TICK_PER_SECOND) #define TICKS_PER_MS (TICK_PER_SECOND / 1000) -//debug rtos with IRQ -//#define FSL_RTOS_XIUOS - -extern char lwip_flag; - -#define LWIP_INIT_FLAG (1 << 0) -#define LWIP_PRINT_FLAG (1 << 1) - -#define set_lwip_bit(__bit) lwip_flag |= (__bit) -#define clr_lwip_bit(__bit) lwip_flag &= ~(__bit) -#define chk_lwip_bit(__bit) ((lwip_flag & (__bit)) == (__bit)) - -extern char lwip_ipaddr[]; -extern char lwip_netmask[]; -extern char lwip_gwaddr[]; - -extern char lwip_eth0_ipaddr[]; -extern char lwip_eth0_netmask[]; -extern char lwip_eth0_gwaddr[]; - -extern char lwip_eth1_ipaddr[]; -extern char lwip_eth1_netmask[]; -extern struct netif gnetif; -extern sys_sem_t* get_eth_recv_sem(); -extern struct netif gnetif2; -extern sys_sem_t* get_eth_recv_sem2(); - -void lwip_tcp_init(void); -void lwip_config_net(uint8_t enet_port, char *ip, char *mask, char *gw); -void lwip_config_tcp(uint8_t enet_port, char *ip, char *mask, char *gw); +int lwip_config_tcp(char *ip, char *mask, char *gw); #endif /* LWIP_ARCH_SYS_ARCH_H */ diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/include/lwip/sys.h b/Ubiquitous/XiZi_AIoT/services/net/net_server/include/lwip/sys.h index cf13e1dcc..d2243cf71 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/include/lwip/sys.h +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/include/lwip/sys.h @@ -95,7 +95,7 @@ typedef u8_t sys_mbox_t; #include "arch/sys_arch.h" /** Function prototype for thread functions */ -typedef void (*lwip_thread_fn)(void *arg); +typedef void (*lwip_thread_fn)(int argc, char **argv); /* Function prototypes for functions to be implemented by platform ports (in sys_arch.c) */ diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip.mk b/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip.mk index 897f96289..3c3b7b336 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip.mk +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip.mk @@ -26,6 +26,7 @@ INC_DIR = -I$(KERNEL_ROOT)/services/net/libnet \ -I$(KERNEL_ROOT)/services/net/net_server/include/lwip/prot \ -I$(KERNEL_ROOT)/services/net/net_server/include/netif \ -I$(KERNEL_ROOT)/services/net/net_server/include/compat \ + -I$(KERNEL_ROOT)/services//semaphore \ -I$(KERNEL_ROOT)/services/lib/ipc \ -I$(KERNEL_ROOT)/services/lib/memory \ -I$(KERNEL_ROOT)/services/lib/serial \ diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip_server.c b/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip_server.c index 9b2eeecad..8d2ee21bd 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip_server.c +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip_server.c @@ -19,49 +19,24 @@ #include "lwip/ip4_addr.h" #include "lwip/netif.h" #include "netif/ethernet.h" +#include "sys_arch.h" -static struct netif gnetif; -int IPC_DO_SERVE_FUNC(Ipc_LWIP_init)(char* ip, char* mask, char* gw){ - - ip4_addr_t net_ipaddr, net_netmask, net_gw; - 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(); - -#ifndef NETIF_ENET_INIT_FUNC - printf("Not Netif driver for Eport 0\n"); - return 0; -#endif - -#ifdef NETIF_ENET_INIT_FUNC - netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, NULL, - NETIF_ENET_INIT_FUNC, ðernet_input); - - /* Registers the default network interface */ - netif_set_default(&gnetif); - - if (netif_is_link_up(&gnetif)) - { - /*When the netif is fully configured this function must be called */ - netif_set_up(&gnetif); - } - else - { - /* When the netif link is down this function must be called */ - netif_set_down(&gnetif); - } - return 0; -#endif +int IPC_DO_SERVE_FUNC(Ipc_LWIP_test)(struct Session* session){ + printf("LWIP init success"); + return 1; } -IPC_SERVER_INTERFACE(Ipc_LWIP_init, 3); -IPC_SERVER_REGISTER_INTERFACES(IpcLWIPServer, 1, Ipc_LWIP_init); +IPC_SERVER_INTERFACE(Ipc_LWIP_test, 1); +IPC_SERVER_REGISTER_INTERFACES(IpcLWIPServer, 1, Ipc_LWIP_test); int main(int argc, char* argv[]){ + char lwip_ipaddr[4] = { 192, 168, 130, 77 }; + char lwip_netmask[4] = { 255, 255, 254, 0 }; + char lwip_gwaddr[4] = { 192, 168, 130, 1 }; + + lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr); if (register_server("LWIPServer") < 0) { printf("register server name: %s failed.\n", "LWIPServer"); exit(0);