📄 net_io.c
字号:
if (res || 0 == delay_count--) break; if (tcp_sock.state != _ESTABLISHED) break; CYGACC_CALL_IF_DELAY_US(100); } if (!res && tcp_sock.state == _CLOSED) { // The connection is gone // Revert RedBoot command line console. net_io_revert_console(); net_io_init(); } CYGARC_HAL_RESTORE_GP(); return res;}static intnet_io_control(void *__ch_data, __comm_control_cmd_t __func, ...){ static int vector = 0; int ret = 0; static int irq_state = 0; CYGARC_HAL_SAVE_GP(); switch (__func) { case __COMMCTL_IRQ_ENABLE: irq_state = 1; if (vector == 0) { vector = eth_drv_int_vector(); } HAL_INTERRUPT_UNMASK(vector); break; case __COMMCTL_IRQ_DISABLE: ret = irq_state; irq_state = 0; HAL_INTERRUPT_MASK(vector); break; case __COMMCTL_DBG_ISR_VECTOR: ret = vector; break; case __COMMCTL_SET_TIMEOUT: { va_list ap; va_start(ap, __func); ret = _timeout; _timeout = va_arg(ap, cyg_uint32); va_end(ap); break; } case __COMMCTL_FLUSH_OUTPUT: net_io_flush(); break; default: break; } CYGARC_HAL_RESTORE_GP(); return ret;}static intnet_io_isr(void *__ch_data, int* __ctrlc, CYG_ADDRWORD __vector, CYG_ADDRWORD __data){ char ch; cyg_drv_interrupt_acknowledge(__vector); *__ctrlc = 0; if (net_io_getc_nonblock(__ch_data, &ch)) { if (ch == 0x03) { *__ctrlc = 1; } } return CYG_ISR_HANDLED;}// TEMPint start_console(void){ int cur_console; cur_console = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); CYGACC_CALL_IF_SET_CONSOLE_COMM(0); return cur_console;}voidend_console(int old_console){ // Restore original console CYGACC_CALL_IF_SET_CONSOLE_COMM(old_console);}// TEMPstatic bool net_has_console = false;static voidnet_io_revert_console(void){ if (net_has_console) {#ifdef CYGPKG_REDBOOT_ANY_CONSOLE console_selected = false;#endif CYGACC_CALL_IF_SET_CONSOLE_COMM(orig_console); CYGACC_CALL_IF_SET_DEBUG_COMM(orig_debug); console_echo = true; net_has_console = false; }}static voidnet_io_assume_console(void){ if (!net_has_console) {#ifdef CYGPKG_REDBOOT_ANY_CONSOLE console_selected = true;#endif console_echo = false; orig_console = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); CYGACC_CALL_IF_SET_CONSOLE_COMM(TCP_CHANNEL); orig_debug = CYGACC_CALL_IF_SET_DEBUG_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); CYGACC_CALL_IF_SET_DEBUG_COMM(TCP_CHANNEL); net_has_console = true; }}static voidnet_io_init(void){ static int init = 0; if (!init) { hal_virtual_comm_table_t* comm; int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); // Setup procs in the vector table CYGACC_CALL_IF_SET_CONSOLE_COMM(TCP_CHANNEL); comm = CYGACC_CALL_IF_CONSOLE_PROCS(); //CYGACC_COMM_IF_CH_DATA_SET(*comm, chan); CYGACC_COMM_IF_WRITE_SET(*comm, net_io_write); CYGACC_COMM_IF_READ_SET(*comm, net_io_read); CYGACC_COMM_IF_PUTC_SET(*comm, net_io_putc); CYGACC_COMM_IF_GETC_SET(*comm, net_io_getc); CYGACC_COMM_IF_CONTROL_SET(*comm, net_io_control); CYGACC_COMM_IF_DBG_ISR_SET(*comm, net_io_isr); CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, net_io_getc_timeout); // Restore original console CYGACC_CALL_IF_SET_CONSOLE_COMM(cur); init = 1; } __tcp_listen(&tcp_sock, gdb_port); state = tcp_sock.state; #ifdef DEBUG_TCP printf("show tcp = %p\n", (void *)&show_tcp);#endif}static voidnet_io_wait_for_connection(void){ net_io_init(); do { __tcp_poll(); if (state != tcp_sock.state) { // Something has changed if (tcp_sock.state == _ESTABLISHED) { // A new connection has arrived in_bufp = in_buf; in_buflen = 0; out_bufp = out_buf; out_buflen = 0; } if (tcp_sock.state == _CLOSED) { net_io_revert_console(); net_io_init(); } state = tcp_sock.state; } } while(state != _ESTABLISHED);}voidnet_io_test(void){ __tcp_poll(); if (state != tcp_sock.state) { // Something has changed if (tcp_sock.state == _ESTABLISHED) { // A new connection has arrived net_io_assume_console(); in_bufp = in_buf; in_buflen = 1; *in_bufp = '\r'; out_bufp = out_buf; out_buflen = 0; } if (tcp_sock.state == _CLOSED) { net_io_revert_console(); net_io_init(); // Get ready for another connection } } state = tcp_sock.state;}//// Network initialization//#include <eth_drv.h>#include <netdev.h>#include <cyg/hal/hal_tables.h>// Define table boundariesCYG_HAL_TABLE_BEGIN( __NETDEVTAB__, netdev );CYG_HAL_TABLE_END( __NETDEVTAB_END__, netdev );//RedBoot_init(net_init, RedBoot_INIT_LAST);voidnet_init(void){ cyg_netdevtab_entry_t *t; ip_addr_t local_ip; struct in_addr server_addr; bool l_use_flash = false; /* Local switch of whether use flash ip conf */ printf("net_init start!!!\n"); if (!config_ok) { // Set defaults as appropriate use_bootp = true; net_debug = false; gdb_port = CYGNUM_REDBOOT_NETWORKING_TCP_PORT;#ifdef CYGSEM_REDBOOT_FLASH_CONFIG } else { flash_get_config("net_debug", &net_debug, CONFIG_BOOL); flash_get_config("gdb_port", &gdb_port, CONFIG_INT); flash_get_config("bootp", &use_bootp, CONFIG_BOOL); //flash_get_config("bootp_my_ip", &__local_ip_addr, CONFIG_IP); //flash_get_config("bootp_server_ip", &my_bootp_info.bp_siaddr, CONFIG_IP); flash_get_config("bootp_my_ip", &local_ip, CONFIG_IP); flash_get_config("bootp_server_ip", &server_addr, CONFIG_IP);#endif } have_net = false; // Make sure the recv buffers are set up eth_drv_buffers_init(); __pktbuf_init(); // Initialize all network devices for (t = &__NETDEVTAB__[0]; t != &__NETDEVTAB_END__; t++) { if (t->init(t)) { t->status = CYG_NETDEVTAB_STATUS_AVAIL; } else { // What to do if device init fails? t->status = 0; // Device not [currently] available } } if (!__local_enet_sc) { printf("No network interfaces found\n"); return; } // Initialize the network [if present] if (use_bootp) { printf("Begin to get BOOTP info"); if (__bootp_find_local_ip(&my_bootp_info) == 0) { printf("\nBOOTP got IP.\n"); have_net = true; } else { printf("\nCan't get BOOTP info -- ");#ifdef CYGSEM_REDBOOT_FLASH_CONFIG printf("Use FLASH Configuration...\n"); l_use_flash = true; have_net = true;#endif } } else { l_use_flash = true; have_net = true; // Assume values in FLASH were OK } if (l_use_flash) { memcpy(&__local_ip_addr, &local_ip, sizeof(local_ip)); memcpy(&my_bootp_info.bp_siaddr, &server_addr, sizeof(server_addr)); } if (have_net) { printf("IP: %s", inet_ntoa((in_addr_t *)&__local_ip_addr)); printf(", Default server: %s\n", inet_ntoa(&my_bootp_info.bp_siaddr)); net_io_init(); } printf("net_init end!\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -