/** * Much like tcpip_apimsg, but calls the lower part of a pppapi_* * function. * * @param pppapimsg a struct containing the function to call and its parameters * @return error code given back by the function that was called */ err_t tcpip_pppapi(struct pppapi_msg* pppapimsg) { struct tcpip_msg msg; if (sys_mbox_valid(&mbox)) { err_t err = sys_sem_new(&pppapimsg->msg.sem, 0); if (err != ERR_OK) { pppapimsg->msg.err = err; return err; } msg.type = TCPIP_MSG_PPPAPI; msg.msg.pppapimsg = pppapimsg; sys_mbox_post(&mbox, &msg); sys_sem_wait(&pppapimsg->msg.sem); sys_sem_free(&pppapimsg->msg.sem); return pppapimsg->msg.err; } return ERR_VAL; }
/** * Much like tcpip_apimsg, but calls the lower part of a netifapi_* * function. * * @param netifapimsg a struct containing the function to call and its parameters * @return error code given back by the function that was called */ err_t tcpip_netifapi(struct netifapi_msg* netifapimsg) { struct tcpip_msg msg; if (mbox != SYS_MBOX_NULL) { netifapimsg->msg.sem = sys_sem_new(0); if (netifapimsg->msg.sem == SYS_SEM_NULL) { netifapimsg->msg.err = ERR_MEM; return netifapimsg->msg.err; } msg.type = TCPIP_MSG_NETIFAPI; msg.msg.netifapimsg = netifapimsg; sys_mbox_post(mbox, &msg); sys_sem_wait(netifapimsg->msg.sem); sys_sem_free(netifapimsg->msg.sem); return netifapimsg->msg.err; } return ERR_VAL; }
/** * Much like tcpip_apimsg, but calls the lower part of a netifapi_* * function. * * @param netifapimsg a struct containing the function to call and its parameters * @return error code given back by the function that was called */ err_t tcpip_netifapi(struct netifapi_msg* netifapimsg) { struct tcpip_msg msg; if (sys_mbox_valid(&mbox)) { err_t err = sys_sem_new(&netifapimsg->msg.sem, 0); if (err != ERR_OK) { netifapimsg->msg.err = err; return err; } msg.type = TCPIP_MSG_NETIFAPI; msg.msg.netifapimsg = netifapimsg; sys_mbox_post(&mbox, &msg); sys_sem_wait(&netifapimsg->msg.sem); sys_sem_free(&netifapimsg->msg.sem); return netifapimsg->msg.err; } return ERR_VAL; }
int _start(int argc,char** argv) { sys_sem_t Sema; int iRet; dbgprintf("PS2IP: Module Loaded.\n"); if ((iRet=RegisterLibraryEntries(&_exp_ps2ip))!=0) { printf("PS2IP: RegisterLibraryEntries returned: %d\n",iRet); } sys_init(); mem_init(); memp_init(); pbuf_init(); dbgprintf("PS2IP: sys_init, mem_init, memp_init, pbuf_init called\n"); netif_init(); dbgprintf("PS2IP: netif_init called\n"); Sema=sys_sem_new(0); dbgprintf("PS2IP: Calling tcpip_init\n"); tcpip_init(InitDone,&Sema); sys_arch_sem_wait(Sema,0); sys_sem_free(Sema); dbgprintf("PS2IP: tcpip_init called\n"); AddLoopIF(); InitTimer(); dbgprintf("PS2IP: System Initialised\n"); return iRet; }
static void net_free_socket(struct tls_netconn *conn) { int index; u32 cpu_sr; if(conn == NULL || conn->used == false) return; TLS_DBGPRT_INFO("conn ptr = 0x%x\n", conn); if (NULL != conn->op_completed) sys_sem_free(conn->op_completed); conn->used = false; if(conn->client && conn->list.prev != NULL && conn->list.prev != &conn->list) { TLS_DBGPRT_INFO("del from list.\n"); cpu_sr = tls_os_set_critical(); dl_list_del(&conn->list); tls_os_release_critical(cpu_sr); } index = conn->skt_num - 1;//TLS_MAX_NETCONN_NUM - tls_mem_free(conn); cpu_sr = tls_os_set_critical(); conn = NULL; p_net_conn[index] = NULL; tls_os_release_critical(cpu_sr); }
void lwip_init(u8_t* lwip_memory){ sys_sem_t sem; extern u8_t** ADI_TOOLS_ram_ptr; extern u32_t ADI_TOOLS_ram_len; extern u8_t** ADI_TOOLS_memp_ptr; extern u32_t ADI_TOOLS_memp_len; extern u8_t** ADI_TOOLS_pbuf_pool_ptr; extern u32_t ADI_TOOLS_pbuf_pool_len; // caller must ensure that the area of memory supplied as 'lwip_memory' // is at least as long as the sum of the 'len' values *ADI_TOOLS_ram_ptr = lwip_memory; *ADI_TOOLS_memp_ptr = lwip_memory + ADI_TOOLS_ram_len; stats_init(); sys_init(); mem_init(); memp_init(); pbuf_init(); netif_init(); sys_sem_new(&sem,0); tcpip_init(tcpip_init_done, &sem); sys_sem_wait(&sem); sys_sem_free(&sem); #if LWIP_SOCKET lwip_socket_init(); #endif /* LWIP_SOCKET */ ip_init(); #if LWIP_ARP etharp_init(); #endif /* LWIP_ARP */ #if LWIP_RAW raw_init(); #endif /* LWIP_RAW */ #if LWIP_UDP udp_init(); #endif /* LWIP_UDP */ #if LWIP_TCP //tcp_init(); #endif /* LWIP_TCP */ #if LWIP_AUTOIP autoip_init(); #endif /* LWIP_AUTOIP */ #if LWIP_IGMP igmp_init(); #endif /* LWIP_IGMP */ #if LWIP_DNS dns_init(); #endif /* LWIP_DNS */ }
int mutexDestroy(void *p) { if (p==0) return 0; sys_sem_free(p); return 1; }
void zot_network_task(cyg_addrword_t arg) { sys_sem_t sem; sys_init(); mem_init(); memp_init(); pbuf_init(); sem = sys_sem_new(0); tcpip_init(tcpip_init_done, &sem); #ifdef PRINT_DIAGNOSTIC // if( !diag_flag ) if( 1 ) #endif { if(EEPROM_Data.PrintServerMode & PS_DHCP_ON) { #ifdef LINKLOCAL_IP #if defined(N716U2W) || defined(N716U2) if(EEPROM_Data.RENVEnable == 1) #endif Link_local_ip_init(); #endif mib_DHCP_p->IPAddr = 0; mib_DHCP_p->SubnetMask = 0; mib_DHCP_p->GwyAddr = 0; /* memset( EEPROM_Data.BoxIPAddress, 0, 4); memset( EEPROM_Data.SubNetMask, 0, 4); memset( EEPROM_Data.GetwayAddress, 0, 4); */ //Create DHCP Thread cyg_thread_create(DHCP_TASK_PRI, dhcp_init, 0, "dhcp_init", (void *) (DHCP_Stack), DHCP_TASK_STACK_SIZE, &DHCP_TaskHdl, &DHCP_Task); //Start DHCP Thread cyg_thread_resume(DHCP_TaskHdl); } #ifdef RENDEZVOUS else { ppause(3000); cyg_semaphore_post( &rendezvous_sem); } #endif } sys_sem_wait(sem); sys_sem_free(sem); }
/*-----------------------------------------------------------------------------------*/ static void main_thread(void *arg) { struct ip_addr ipaddr, netmask, gw; sys_sem_t sem; #if PPP_SUPPORT sio_fd_t ppp_sio; #endif netif_init(); sem = sys_sem_new(0); tcpip_init(tcpip_init_done, &sem); sys_sem_wait(sem); sys_sem_free(sem); printf("TCP/IP initialized.\n"); #if PPP_SUPPORT pppInit(); #if PPP_PTY_TEST ppp_sio = sio_open(2); #else ppp_sio = sio_open(0); #endif if(!ppp_sio) { perror("Error opening device: "); exit(1); } #ifdef LWIP_PPP_CHAP_TEST pppSetAuth(PPPAUTHTYPE_CHAP, "lwip", "mysecret"); #endif pppOpen(ppp_sio, pppLinkStatusCallback, NULL); #endif /* PPP_SUPPORT */ #if LWIP_DHCP { IP4_ADDR(&gw, 0,0,0,0); IP4_ADDR(&ipaddr, 0,0,0,0); IP4_ADDR(&netmask, 0,0,0,0); netif_add(&netif, &ipaddr, &netmask, &gw, NULL, tapif_init, tcpip_input); netif_set_default(&netif); dhcp_init(); dhcp_start(&netif); } #else IP4_ADDR(&gw, 192,168,0,1); IP4_ADDR(&ipaddr, 192,168,0,2); IP4_ADDR(&netmask, 255,255,255,0); netif_set_default(netif_add(&netif,&ipaddr, &netmask, &gw, NULL, tapif_init, tcpip_input)); netif_set_up(&netif); #endif /* Only used for testing purposes: */ /* IP4_ADDR(&gw, 193,10,66,1); IP4_ADDR(&ipaddr, 193,10,66,107); IP4_ADDR(&netmask, 255,255,252,0); netif_add(&ipaddr, &netmask, &gw, NULL, pcapif_init, tcpip_input);*/ #if LWIP_HAVE_LOOPIF IP4_ADDR(&gw, 127,0,0,1); IP4_ADDR(&ipaddr, 127,0,0,1); IP4_ADDR(&netmask, 255,0,0,0); netif_set_default(netif_add(&loopif, &ipaddr, &netmask, &gw, NULL, loopif_init, tcpip_input)); #endif #if LWIP_TCP tcpecho_init(); shell_init(); httpd_init(); #endif #if LWIP_UDP udpecho_init(); #endif #if LWIP_RAW sys_thread_new(ping_thread, NULL, DEFAULT_THREAD_PRIO); #endif printf("Applications started.\n"); /* sys_timeout(5000, tcp_timeout, NULL);*/ #ifdef MEM_PERF mem_perf_init("/tmp/memstats.client"); #endif /* MEM_PERF */ #if 0 stats_display(); #endif /* Block for ever. */ sem = sys_sem_new(0); sys_sem_wait(sem); }
int sem_free(sem_t sem_id) { return sys_sem_free(sem_id); }
int main() { sys_sem_t sem; sys_init(); if(sys_sem_new(&sem, 0) != ERR_OK) { LWIP_ASSERT("failed to create semaphore", 0); } tcpip_init(tcpip_init_done, &sem); sys_sem_wait(&sem); sys_sem_free(&sem); /////////////////////////////////////////////////////////////////////////////////////////////////// struct netconn *conn, *newconn; err_t err; /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); netconn_set_noautorecved(conn, 0); tcp_nagle_disable(conn->pcb.tcp); /* Bind connection to well known port number 7. */ netconn_bind(conn, NULL, 80); /* Tell connection to go into listening mode. */ netconn_listen(conn); while (1) { /* Grab new connection. */ err = netconn_accept(conn, &newconn); printf("accepted new connection %p\n", newconn); /* Process the new connection. */ if (err == ERR_OK) { struct netbuf *buf; void *data; u16_t len; u64_t total_rcvd = 0; u64_t eal_tsc_resolution_hz = rte_get_timer_hz(); u64_t end = rte_get_timer_cycles() + eal_tsc_resolution_hz; while ((err = netconn_recv(newconn, &buf)) == ERR_OK) { netbuf_data(buf, &data, &len); if (len > 0) { total_rcvd += len; } if (rte_get_timer_cycles() >= end) { printf("%llu \n", (unsigned long long)total_rcvd); total_rcvd = 0; end = rte_get_timer_cycles() + eal_tsc_resolution_hz; } #if 0 if (err != ERR_OK) { printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(err)); } #endif //} while (netbuf_next(buf) >= 0); netbuf_delete(buf); } /*printf("Got EOF, looping\n");*/ /* Close connection and discard connection identifier. */ netconn_close(newconn); netconn_delete(newconn); } } while (1); }
/** * Call the lower part of a netconn_* function * This function is then running in the thread context * of tcpip_thread and has exclusive access to lwIP core code. * * @param apimsg a struct containing the function to call and its parameters * @return ERR_OK if the function was called, another err_t if not */ static err_t tcpip_apimsg(struct api_msg *apimsg) { #ifdef LWIP_DEBUG /* catch functions that don't set err */ apimsg->msg.err = ERR_VAL; #endif #if LWIP_NETCONN_SEM_PER_THREAD apimsg->msg.op_completed_sem = LWIP_NETCONN_THREAD_SEM_GET(); LWIP_ASSERT("netconn semaphore not initialized", sys_sem_valid(apimsg->msg.op_completed_sem)); #endif #ifdef LWIP_ESP8266 //#if 0 sys_sem_t *op_sem_tmp = NULL; if(apimsg->function == lwip_netconn_do_write) op_sem_tmp = LWIP_API_MSG_SND_SEM(&apimsg->msg); else op_sem_tmp = LWIP_API_MSG_SEM(&apimsg->msg); if (tcpip_send_api_msg(apimsg->function, &apimsg->msg, op_sem_tmp) == ERR_OK) { #else if (tcpip_send_api_msg(apimsg->function, &apimsg->msg, LWIP_API_MSG_SEM(&apimsg->msg)) == ERR_OK) { #endif return apimsg->msg.err; } return ERR_VAL; } #endif /* !LWIP_TCPIP_CORE_LOCKING */ /** * Create a new netconn (of a specific type) that has a callback function. * The corresponding pcb is also created. * * @param t the type of 'connection' to create (@see enum netconn_type) * @param proto the IP protocol for RAW IP pcbs * @param callback a function to call on status changes (RX available, TX'ed) * @return a newly allocated struct netconn or * NULL on memory error */ struct netconn* netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, netconn_callback callback) { struct netconn *conn; API_MSG_VAR_DECLARE(msg); conn = netconn_alloc(t, callback); if (conn != NULL) { err_t err; API_MSG_VAR_ALLOC_DONTFAIL(msg); API_MSG_VAR_REF(msg).msg.msg.n.proto = proto; API_MSG_VAR_REF(msg).msg.conn = conn; TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_newconn, err); API_MSG_VAR_FREE(msg); if (err != ERR_OK) { LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL); LWIP_ASSERT("conn has no recvmbox", sys_mbox_valid(&conn->recvmbox)); #if LWIP_TCP LWIP_ASSERT("conn->acceptmbox shouldn't exist", !sys_mbox_valid(&conn->acceptmbox)); #endif /* LWIP_TCP */ #if !LWIP_NETCONN_SEM_PER_THREAD LWIP_ASSERT("conn has no op_completed", sys_sem_valid(&conn->op_completed)); sys_sem_free(&conn->op_completed); #ifdef LWIP_ESP8266 sys_sem_free(&conn->snd_op_completed); #endif #endif /* !LWIP_NETCONN_SEM_PER_THREAD */ sys_mbox_free(&conn->recvmbox); memp_free(MEMP_NETCONN, conn); return NULL; } } return conn; } /** * Close a netconn 'connection' and free its resources. * UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate * after this returns. * * @param conn the netconn to delete * @return ERR_OK if the connection was deleted */ err_t netconn_delete(struct netconn *conn) { err_t err; API_MSG_VAR_DECLARE(msg); /* No ASSERT here because possible to get a (conn == NULL) if we got an accept error */ if (conn == NULL) { return ERR_OK; } API_MSG_VAR_ALLOC(msg); API_MSG_VAR_REF(msg).msg.conn = conn; #if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER /* get the time we started, which is later compared to sys_now() + conn->send_timeout */ API_MSG_VAR_REF(msg).msg.msg.sd.time_started = sys_now(); #else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ #if LWIP_TCP API_MSG_VAR_REF(msg).msg.msg.sd.polls_left = ((LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT + TCP_SLOW_INTERVAL - 1) / TCP_SLOW_INTERVAL) + 1; #endif /* LWIP_TCP */ #endif /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_delconn, err); API_MSG_VAR_FREE(msg); if (err != ERR_OK) { return err; } netconn_free(conn); return ERR_OK; }
/* This is somewhat different to other ports: we have a main loop here: * a dedicated task that waits for packets to arrive. This would normally be * done from interrupt context with embedded hardware, but we don't get an * interrupt in windows for that :-) */ static void main_loop(void) { #if !NO_SYS err_t err; sys_sem_t init_sem; #endif /* NO_SYS */ #if USE_PPP #if !USE_ETHERNET int count; u8_t rxbuf[1024]; #endif volatile int callClosePpp = 0; #endif /* USE_PPP */ /* initialize lwIP stack, network interfaces and applications */ #if NO_SYS lwip_init(); test_init(NULL); #else /* NO_SYS */ err = sys_sem_new(&init_sem, 0); LWIP_ASSERT("failed to create init_sem", err == ERR_OK); tcpip_init(test_init, &init_sem); /* we have to wait for initialization to finish before * calling update_adapter()! */ sys_sem_wait(&init_sem); sys_sem_free(&init_sem); #endif /* NO_SYS */ #if (LWIP_SOCKET || LWIP_NETCONN) && LWIP_NETCONN_SEM_PER_THREAD netconn_thread_init(); #endif /* MAIN LOOP for driver update (and timers if NO_SYS) */ while (!_kbhit()) { #if NO_SYS /* handle timers (already done in tcpip.c when NO_SYS=0) */ sys_check_timeouts(); #endif /* NO_SYS */ #if USE_ETHERNET #if !PCAPIF_RX_USE_THREAD /* check for packets and link status*/ pcapif_poll(&netif); /* When pcapif_poll comes back, there are not packets, so sleep to prevent 100% CPU load. Don't do this in an embedded system since it increases latency! */ sys_msleep(1); #else /* !PCAPIF_RX_USE_THREAD */ sys_msleep(50); #endif /* !PCAPIF_RX_USE_THREAD */ #else /* USE_ETHERNET */ /* try to read characters from serial line and pass them to PPPoS */ count = sio_read(ppp_sio, (u8_t*)rxbuf, 1024); if(count > 0) { pppos_input(ppp, rxbuf, count); } else { /* nothing received, give other tasks a chance to run */ sys_msleep(1); } #endif /* USE_ETHERNET */ #if USE_SLIPIF slipif_poll(&slipif1); #if USE_SLIPIF > 1 slipif_poll(&slipif2); #endif /* USE_SLIPIF > 1 */ #endif /* USE_SLIPIF */ #if ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING /* check for loopback packets on all netifs */ netif_poll_all(); #endif /* ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING */ #if USE_PPP { int do_hup = 0; if(do_hup) { ppp_close(ppp, 1); do_hup = 0; } } if(callClosePpp && ppp) { /* make sure to disconnect PPP before stopping the program... */ callClosePpp = 0; #if NO_SYS ppp_close(ppp, 0); #else pppapi_close(ppp, 0); #endif ppp = NULL; } #endif /* USE_PPP */ } #if USE_PPP if(ppp) { u32_t started; printf("Closing PPP connection...\n"); /* make sure to disconnect PPP before stopping the program... */ #if NO_SYS ppp_close(ppp, 0); #else pppapi_close(ppp, 0); #endif ppp = NULL; /* Wait for some time to let PPP finish... */ started = sys_now(); do { #if USE_ETHERNET && !PCAPIF_RX_USE_THREAD pcapif_poll(&netif); #else /* USE_ETHERNET && !PCAPIF_RX_USE_THREAD */ sys_msleep(50); #endif /* USE_ETHERNET && !PCAPIF_RX_USE_THREAD */ /* @todo: need a better check here: only wait until PPP is down */ } while(sys_now() - started < 5000); } #endif /* USE_PPP */ #if (LWIP_SOCKET || LWIP_NETCONN) && LWIP_NETCONN_SEM_PER_THREAD netconn_thread_cleanup(); #endif #if USE_ETHERNET /* release the pcap library... */ pcapif_shutdown(&netif); #endif /* USE_ETHERNET */ }
/* This is somewhat different to other ports: we have a main loop here: * a dedicated task that waits for packets to arrive. This would normally be * done from interrupt context with embedded hardware, but we don't get an * interrupt in windows for that :-) */ void main_loop() { #if !NO_SYS err_t err; sys_sem_t init_sem; #endif /* NO_SYS */ #if PPP_SUPPORT #if !USE_ETHERNET int count; u8_t rxbuf[1024]; #endif volatile int callClosePpp = 0; #endif /* PPP_SUPPORT */ /* initialize lwIP stack, network interfaces and applications */ #if NO_SYS lwip_init(); test_init(NULL); #else /* NO_SYS */ err = sys_sem_new(&init_sem, 0); tcpip_init(test_init, &init_sem); /* we have to wait for initialization to finish before * calling update_adapter()! */ sys_sem_wait(&init_sem); sys_sem_free(&init_sem); #endif /* NO_SYS */ /* MAIN LOOP for driver update (and timers if NO_SYS) */ while (!_kbhit()) { #if NO_SYS /* handle timers (already done in tcpip.c when NO_SYS=0) */ sys_check_timeouts(); #endif /* NO_SYS */ #if USE_ETHERNET #if !PCAPIF_RX_USE_THREAD /* check for packets and link status*/ pcapif_poll(&netif); /* When pcapif_poll comes back, there are not packets, so sleep to prevent 100% CPU load. Don't do this in an embedded system since it increases latency! */ sys_msleep(1); #else /* !PCAPIF_RX_USE_THREAD */ sys_msleep(50); #endif /* !PCAPIF_RX_USE_THREAD */ #else /* USE_ETHERNET */ #if 0 /* set this to 1 if PPP_INPROC_OWNTHREAD==0 or not defined (see ppp.c) */ /* try to read characters from serial line and pass them to PPPoS */ count = sio_read(ppp_sio, (u8_t*)rxbuf, 1024); if(count > 0) { pppos_input(ppp_desc, rxbuf, count); } else #endif { /* nothing received, give other tasks a chance to run */ sys_msleep(1); } #endif /* USE_ETHERNET */ #if !LWIP_NETIF_LOOPBACK_MULTITHREADING /* check for loopback packets on all netifs */ netif_poll_all(); #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */ #if PPP_SUPPORT { int do_hup = 0; if(do_hup) { pppSigHUP(ppp_desc); do_hup = 0; } } if(callClosePpp && (ppp_desc >= 0)) { /* make sure to disconnect PPP before stopping the program... */ callClosePpp = 0; #if NO_SYS pppClose(ppp_desc); #else tcpip_callback_with_block(pppCloseCallback, (void*)ppp_desc, 0); #endif ppp_desc = -1; } #endif /* PPP_SUPPORT */ } #if PPP_SUPPORT if(ppp_desc >= 0) { u32_t started; printf("Closing PPP connection...\n"); /* make sure to disconnect PPP before stopping the program... */ #if NO_SYS pppClose(ppp_desc); #else tcpip_callback_with_block(pppCloseCallback, (void*)ppp_desc, 0); #endif ppp_desc = -1; /* Wait for some time to let PPP finish... */ started = sys_now(); do { #if USE_ETHERNET && !PCAPIF_RX_USE_THREAD pcapif_poll(&netif); #else /* USE_ETHERNET && !PCAPIF_RX_USE_THREAD */ sys_msleep(50); #endif /* USE_ETHERNET && !PCAPIF_RX_USE_THREAD */ /* @todo: need a better check here: only wait until PPP is down */ } while(sys_now() - started < 5000); } #endif /* PPP_SUPPORT */ #if USE_ETHERNET /* release the pcap library... */ pcapif_shutdown(&netif); #endif /* USE_ETHERNET */ }
int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout) { int i; int nready; fd_set lreadset, lwriteset, lexceptset; u32_t msectimeout; struct lwip_select_cb select_cb; struct lwip_select_cb *p_selcb; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select(%d, %p, %p, %p, tvsec=%ld tvusec=%ld)\n", maxfdp1, (void *)readset, (void *) writeset, (void *) exceptset, timeout ? timeout->tv_sec : -1L, timeout ? timeout->tv_usec : -1L)); select_cb.next = 0; select_cb.readset = readset; select_cb.writeset = writeset; select_cb.exceptset = exceptset; select_cb.sem_signalled = 0; /* Protect ourselves searching through the list */ if (!selectsem) selectsem = sys_sem_new(1); sys_sem_wait(selectsem); if (readset) lreadset = *readset; else FD_ZERO(&lreadset); if (writeset) lwriteset = *writeset; else FD_ZERO(&lwriteset); if (exceptset) lexceptset = *exceptset; else FD_ZERO(&lexceptset); /* Go through each socket in each list to count number of sockets which currently match */ nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset); /* If we don't have any current events, then suspend if we are supposed to */ if (!nready) { if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) { sys_sem_signal(selectsem); if (readset) FD_ZERO(readset); if (writeset) FD_ZERO(writeset); if (exceptset) FD_ZERO(exceptset); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n")); set_errno(0); return 0; } /* add our semaphore to list */ /* We don't actually need any dynamic memory. Our entry on the * list is only valid while we are in this function, so it's ok * to use local variables */ select_cb.sem = sys_sem_new(0); /* Note that we are still protected */ /* Put this select_cb on top of list */ select_cb.next = select_cb_list; select_cb_list = &select_cb; /* Now we can safely unprotect */ sys_sem_signal(selectsem); /* Now just wait to be woken */ if (timeout == 0) /* Wait forever */ msectimeout = 0; else msectimeout = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500)/1000)); i = sys_sem_wait_timeout(select_cb.sem, msectimeout); /* Take us off the list */ sys_sem_wait(selectsem); if (select_cb_list == &select_cb) select_cb_list = select_cb.next; else for (p_selcb = select_cb_list; p_selcb; p_selcb = p_selcb->next) if (p_selcb->next == &select_cb) { p_selcb->next = select_cb.next; break; } sys_sem_signal(selectsem); sys_sem_free(select_cb.sem); if (i == 0) /* Timeout */ { if (readset) FD_ZERO(readset); if (writeset) FD_ZERO(writeset); if (exceptset) FD_ZERO(exceptset); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n")); set_errno(0); return 0; } if (readset) lreadset = *readset; else FD_ZERO(&lreadset); if (writeset) lwriteset = *writeset; else FD_ZERO(&lwriteset); if (exceptset) lexceptset = *exceptset; else FD_ZERO(&lexceptset); /* See what's set */ nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset); } else sys_sem_signal(selectsem); if (readset) *readset = lreadset; if (writeset) *writeset = lwriteset; if (exceptset) *exceptset = lexceptset; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready)); set_errno(0); return nready; }
/** * Create a new netconn (of a specific type) that has a callback function. * The corresponding pcb is NOT created! * * @param t the type of 'connection' to create (@see enum netconn_type) * @param proto the IP protocol for RAW IP pcbs * @param callback a function to call on status changes (RX available, TX'ed) * @return a newly allocated struct netconn or * NULL on memory error */ struct netconn* netconn_alloc(enum netconn_type t, netconn_callback callback) { struct netconn *conn; int size; conn = memp_malloc(MEMP_NETCONN); if (conn == NULL) { return NULL; } conn->err = ERR_OK; conn->type = t; conn->pcb.tcp = NULL; #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \ (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE) size = DEFAULT_RAW_RECVMBOX_SIZE; #else switch(NETCONNTYPE_GROUP(t)) { #if LWIP_RAW case NETCONN_RAW: size = DEFAULT_RAW_RECVMBOX_SIZE; break; #endif /* LWIP_RAW */ #if LWIP_UDP case NETCONN_UDP: size = DEFAULT_UDP_RECVMBOX_SIZE; break; #endif /* LWIP_UDP */ #if LWIP_TCP case NETCONN_TCP: size = DEFAULT_TCP_RECVMBOX_SIZE; break; #endif /* LWIP_TCP */ default: LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0); break; } #endif if ((conn->op_completed = sys_sem_new(0)) == SYS_SEM_NULL) { memp_free(MEMP_NETCONN, conn); return NULL; } if ((conn->recvmbox = sys_mbox_new(size)) == SYS_MBOX_NULL) { sys_sem_free(conn->op_completed); memp_free(MEMP_NETCONN, conn); return NULL; } conn->acceptmbox = SYS_MBOX_NULL; conn->state = NETCONN_NONE; /* initialize socket to -1 since 0 is a valid socket */ conn->socket = -1; conn->callback = callback; conn->recv_avail = 0; #if LWIP_TCP conn->write_msg = NULL; conn->write_offset = 0; #if LWIP_TCPIP_CORE_LOCKING conn->write_delayed = 0; #endif /* LWIP_TCPIP_CORE_LOCKING */ #endif /* LWIP_TCP */ #if LWIP_SO_RCVTIMEO conn->recv_timeout = 0; #endif /* LWIP_SO_RCVTIMEO */ #if LWIP_SO_RCVBUF conn->recv_bufsize = RECV_BUFSIZE_DEFAULT; #endif /* LWIP_SO_RCVBUF */ return conn; }
err_t netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy) { struct api_msg *msg; u16_t len; if (conn == NULL) { return ERR_VAL; } if (conn->err != ERR_OK) { return conn->err; } if (conn->sem == SYS_SEM_NULL) { conn->sem = sys_sem_new(0); if (conn->sem == SYS_SEM_NULL) { return ERR_MEM; } } if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) { return (conn->err = ERR_MEM); } msg->type = API_MSG_WRITE; msg->msg.conn = conn; conn->state = NETCONN_WRITE; while (conn->err == ERR_OK && size > 0) { msg->msg.msg.w.dataptr = dataptr; msg->msg.msg.w.copy = copy; if (conn->type == NETCONN_TCP) { if (tcp_sndbuf(conn->pcb.tcp) == 0) { sys_sem_wait(conn->sem); if (conn->err != ERR_OK) { goto ret; } } if (size > tcp_sndbuf(conn->pcb.tcp)) { /* We cannot send more than one send buffer's worth of data at a time. */ len = tcp_sndbuf(conn->pcb.tcp); } else { len = size; } } else { len = size; } LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_write: writing %d bytes (%d)\n", len, copy)); msg->msg.msg.w.len = len; api_msg_post(msg); sys_mbox_fetch(conn->mbox, NULL); if (conn->err == ERR_OK) { dataptr = (void *)((char *)dataptr + len); size -= len; } else if (conn->err == ERR_MEM) { conn->err = ERR_OK; sys_sem_wait(conn->sem); } else { goto ret; } } ret: memp_free(MEMP_API_MSG, msg); conn->state = NETCONN_NONE; if (conn->sem != SYS_SEM_NULL) { sys_sem_free(conn->sem); conn->sem = SYS_SEM_NULL; } return conn->err; }
/** * Create a new netconn (of a specific type) that has a callback function. * The corresponding pcb is NOT created! * * @param t the type of 'connection' to create (@see enum netconn_type) * @param proto the IP protocol for RAW IP pcbs * @param callback a function to call on status changes (RX available, TX'ed) * @return a newly allocated struct netconn or * NULL on memory error */ struct netconn* netconn_alloc(enum netconn_type t, netconn_callback callback) { struct netconn *conn; int size; conn = (struct netconn *)memp_malloc(MEMP_NETCONN); if (conn == NULL) { return NULL; } conn->last_err = ERR_OK; conn->type = t; conn->pcb.tcp = NULL; #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \ (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE) size = DEFAULT_RAW_RECVMBOX_SIZE; #else switch(NETCONNTYPE_GROUP(t)) { #if LWIP_RAW case NETCONN_RAW: size = DEFAULT_RAW_RECVMBOX_SIZE; break; #endif /* LWIP_RAW */ #if LWIP_UDP case NETCONN_UDP: size = DEFAULT_UDP_RECVMBOX_SIZE; break; #endif /* LWIP_UDP */ #if LWIP_TCP case NETCONN_TCP: size = DEFAULT_TCP_RECVMBOX_SIZE; break; #endif /* LWIP_TCP */ default: LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0); goto free_and_return; } #endif if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) { goto free_and_return; } if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) { sys_sem_free(&conn->op_completed); goto free_and_return; } #if LWIP_TCP sys_mbox_set_invalid(&conn->acceptmbox); #endif conn->state = NETCONN_NONE; #if LWIP_SOCKET /* initialize socket to -1 since 0 is a valid socket */ conn->socket = -1; #endif /* LWIP_SOCKET */ conn->callback = callback; #if LWIP_TCP conn->current_msg = NULL; conn->write_offset = 0; #endif /* LWIP_TCP */ #if LWIP_SO_SNDTIMEO conn->send_timeout = 0; #endif /* LWIP_SO_SNDTIMEO */ #if LWIP_SO_RCVTIMEO conn->recv_timeout = 0; #endif /* LWIP_SO_RCVTIMEO */ #if LWIP_SO_RCVBUF conn->recv_bufsize = RECV_BUFSIZE_DEFAULT; conn->recv_avail = 0; #endif /* LWIP_SO_RCVBUF */ conn->flags = 0; return conn; free_and_return: memp_free(MEMP_NETCONN, conn); return NULL; }
/*-----------------------------------------------------------------------------------*/ static void main_thread(void *arg) { struct ip_addr ipaddr, netmask, gw; sys_sem_t sem; netif_init(); sem = sys_sem_new(0); tcpip_init(tcpip_init_done, &sem); sys_sem_wait(sem); sys_sem_free(sem); printf("TCP/IP initialized.\n"); #if LWIP_DHCP { struct netif *netif; IP4_ADDR(&gw, 0,0,0,0); IP4_ADDR(&ipaddr, 0,0,0,0); IP4_ADDR(&netmask, 0,0,0,0); netif = netif_add(&ipaddr, &netmask, &gw, tapif_init, tcpip_input); netif_set_default(netif); dhcp_init(); dhcp_start(netif); } #else IP4_ADDR(&gw, 192,168,0,1); IP4_ADDR(&ipaddr, 192,168,0,2); IP4_ADDR(&netmask, 255,255,255,0); /* netif_set_default(netif_add(&ipaddr, &netmask, &gw, tapif_init, tcpip_input));*/ netif_set_default(netif_add(&ipaddr, &netmask, &gw, tapif_init, tcpip_input)); #endif /* Only used for testing purposes: */ /* IP4_ADDR(&gw, 193,10,66,1); IP4_ADDR(&ipaddr, 193,10,66,107); IP4_ADDR(&netmask, 255,255,252,0); netif_add(&ipaddr, &netmask, &gw, pcapif_init, tcpip_input);*/ IP4_ADDR(&gw, 127,0,0,1); IP4_ADDR(&ipaddr, 127,0,0,1); IP4_ADDR(&netmask, 255,0,0,0); netif_add(&ipaddr, &netmask, &gw, loopif_init, tcpip_input); tcpecho_init(); shell_init(); httpd_init(); udpecho_init(); printf("Applications started.\n"); /* sys_timeout(5000, tcp_timeout, NULL);*/ #ifdef MEM_PERF mem_perf_init("/tmp/memstats.client"); #endif /* MEM_PERF */ /* Block for ever. */ sem = sys_sem_new(0); sys_sem_wait(sem); }