void fapp_debug_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t ** argv ) { FNET_COMP_UNUSED_ARG(desc); FNET_COMP_UNUSED_ARG(argc); FNET_COMP_UNUSED_ARG(argv); /* Put here you debugging information.*/ #if 0 fnet_fec_debug_mii_print_regs(fnet_netif_get_default()); #endif #if 0 { fnet_index_t i = 0; fnet_ip6_addr_t addr_dns; fnet_uint8_t ip_str[FNET_IP_ADDR_STR_SIZE]={0}; while(fnet_netif_get_ip6_dns(fnet_netif_get_default(), i, &addr_dns ) == FNET_TRUE) { fnet_inet_ntop(AF_INET6, (fnet_uint8_t*)(&addr_dns), ip_str, sizeof(ip_str)); fnet_shell_println(desc, "DNS[%d]=%s", i, ip_str); i++; } } #endif #if 0 /* Test ctacrypt library.*/ extern void ctaocrypt_test(void); ctaocrypt_test(); #endif }
/************************************************************************ * NAME: fapp_dhcp_on_ctrlc * * DESCRIPTION: ************************************************************************/ static void fapp_dhcp_on_ctrlc(fnet_shell_desc_t desc) { /* Release DHCP. */ fapp_dhcp_release(); /* Restore old ip address, as DHCP set it to zero. */ fnet_netif_set_ip4_addr( fnet_netif_get_default(), fapp_dhcp_ip_old ); fnet_shell_println( desc, FAPP_CANCELLED_STR); }
/************************************************************************ * NAME: fapp_init * * DESCRIPTION: FNET Application initialization. ************************************************************************/ void fapp_init(void) { static fnet_uint8_t stack_heap[FNET_CFG_HEAP_SIZE]; struct fnet_init_params init_params; struct fnet_shell_params shell_params; /* Input parameters for FNET stack initialization */ init_params.netheap_ptr = stack_heap; init_params.netheap_size = FNET_CFG_HEAP_SIZE; /* Add event handler on duplicated IP address */ #if FNET_CFG_IP4 fnet_netif_dupip_handler_init (fapp_dup_ip_handler); #endif /* Init FNET stack */ if(fnet_init(&init_params) != FNET_ERR) { #if FAPP_CFG_PARAMS_READ_FLASH /* During bootup, the most recently stored customer configuration data will be read and used to configure the interfaces.*/ if(fapp_params_from_flash() == FNET_OK) { fnet_printf(FAPP_PARAMS_LOAD_STR); } #endif if(fnet_netif_get_default() == FNET_NULL) { fnet_printf(FAPP_NET_ERR); } #if (FAPP_CFG_EXP_CMD && FNET_CFG_FS) || (FAPP_CFG_HTTP_CMD && FNET_CFG_HTTP) fapp_fs_mount(); /* Init FS and mount FS Image. */ #endif /* Init main shell. */ shell_params.shell = &fapp_shell; shell_params.cmd_line_buffer = fapp_cmd_line_buffer; shell_params.cmd_line_buffer_size = sizeof(fapp_cmd_line_buffer); shell_params.stream = FNET_SERIAL_STREAM_DEFAULT; shell_params.echo = FNET_TRUE; if((fapp_shell_desc = fnet_shell_init(&shell_params)) != FNET_ERR) { fapp_boot(fapp_shell_desc); } else { fnet_printf(FAPP_INIT_ERR, "Shell"); } } else { fnet_printf(FAPP_INIT_ERR, "FNET"); } }
int wait_dhcp(int tries) { fnet_netif_desc_t netif; struct fnet_dhcp_params dhcp_params; struct callback_params_t callback_params; /* Get Default netif */ netif = fnet_netif_get_default(); if (!netif) { return -1; } /* Clear params struct */ fnet_memset_zero(&dhcp_params, sizeof(struct fnet_dhcp_params)); /* Turn on link-local support */ dhcp_params.retries_until_link_local = tries; /* Initialise DHCP client */ if(fnet_dhcp_init(netif, &dhcp_params) == FNET_ERR) { return -1; } /* Setup param for callbacks */ callback_params.max_tries = tries + 1; /* Add one so we get link local before timeout */ callback_params.cur_try = 1; callback_params.state = WAITING; /* Print message */ fnet_printf("\nDHCP: Waiting for server \n"); /* Register call backs */ fnet_dhcp_handler_updated_set(handler_updated, &callback_params); fnet_dhcp_handler_discover_set(handler_discover, &callback_params); /* Poll background services until address found or n_tries exceeded */ while (callback_params.state == WAITING) { fnet_poll_services(); } /* Now evaluate response */ if (callback_params.state == SUCCESS) { fnet_printf("DHCP: Success! \n"); return 0; /* Success */ } else { fnet_printf("DHCP: Failed! Using link local address \n"); /* Keep DHCP server running in background */ return -1; /* Failure */ } }
/************************************************************************ * NAME: fapp_dhcp_cmd * * DESCRIPTION: Enable DHCP client. ************************************************************************/ void fapp_dhcp_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t ** argv ) { struct fnet_dhcp_params dhcp_params; fnet_dhcp_desc_t dhcp_desc; fnet_netif_desc_t netif = fnet_netif_get_default(); if(argc == 1u /* By default is "init".*/ #if 0 /* DHCP reboot feature not used too much. */ || fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0 #endif ) /* [reboot] */ { fnet_memset_zero(&dhcp_params, sizeof(struct fnet_dhcp_params)); fapp_dhcp_discover_counter = FAPP_CFG_DHCP_CMD_DISCOVER_MAX; /* reset counter.*/ #if 0 /* DHCP reboot feature not used too much. */ if(fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0) /* [reboot] */ dhcp_params.requested_ip_address.s_addr = fnet_netif_get_ip4_addr(netif); #endif fapp_dhcp_ip_old = fnet_netif_get_ip4_addr(netif); /* Save ip to restore if cancelled. */ /* Enable DHCP client */ dhcp_desc = fnet_dhcp_init(netif, &dhcp_params); if(dhcp_desc != FNET_ERR) { fapp_dhcp_desc = dhcp_desc; /* Register DHCP event handlers. */ fnet_dhcp_handler_updated_set(fapp_dhcp_desc, fapp_dhcp_handler_updated, (void *)desc); fnet_dhcp_handler_discover_set(fapp_dhcp_desc, fapp_dhcp_handler_discover, (void *)desc); fnet_shell_println(desc, FAPP_TOCANCEL_STR); fnet_shell_block(desc, fapp_dhcp_on_ctrlc); /* Block shell. */ } else { fnet_shell_println(desc, FAPP_INIT_ERR, "DHCP"); } } else if((argc == 2u) && (fnet_strcasecmp(&FAPP_COMMAND_RELEASE[0], argv[1]) == 0)) /* [release] */ { fapp_dhcp_release(); } else { fnet_shell_println(desc, FAPP_PARAM_ERR, argv[1]); } }
static void fapp_stat_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t ** argv ) { struct fnet_netif_statistics statistics; fnet_netif_desc_t netif = fnet_netif_get_default(); FNET_COMP_UNUSED_ARG(argc); FNET_COMP_UNUSED_ARG(argv); /* Print Packet statistics. */ if(fnet_netif_get_statistics(netif, &statistics) == FNET_OK) { fnet_shell_println(desc, "\nPackets:"); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "TX Packets", statistics.tx_packet); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "RX Packets", statistics.rx_packet); } #if FNET_CFG_IP6 { fnet_index_t i; fnet_netif_ip6_prefix_t ip6_prefix; fnet_netif_ip6_neighbor_cache_t ip6_neighbor_cache; fnet_char_t numaddr[FNET_IP6_ADDR_STR_SIZE]; fnet_char_t mac_str[FNET_MAC_ADDR_STR_SIZE]; /* Print content of IPv6 Prefix List. */ fnet_shell_println(desc, "\nIPv6 Prefix List:"); for(i=0U; fnet_netif_get_ip6_prefix(netif, i, &ip6_prefix) == FNET_TRUE; i++) { fnet_shell_println(desc," [%d] %s/%d\n", i, fnet_inet_ntop(AF_INET6, &ip6_prefix.prefix, numaddr, sizeof(numaddr)), ip6_prefix.prefix_length); } /* Print content of IPv6 Neighbor Cache. */ for(i=0U; fnet_netif_get_ip6_neighbor_cache(netif, i, &ip6_neighbor_cache) == FNET_TRUE; i++) { if(i == 0U) { fnet_shell_println(desc, "\nIPv6 Neighbor Cache:"); } fnet_shell_println(desc," [%d] %s = %s (%s)\n", i, fnet_inet_ntop(AF_INET6, &ip6_neighbor_cache.ip_addr, numaddr, sizeof(numaddr)), fnet_mac_to_str(ip6_neighbor_cache.ll_addr, mac_str), (ip6_neighbor_cache.is_router == FNET_TRUE) ? "router" : "host"); } } #endif }
static void fapp_unbind_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t ** argv ) { fnet_netif_desc_t netif = fnet_netif_get_default(); fnet_ip6_addr_t addr; FNET_COMP_UNUSED_ARG(argc); if(fnet_inet_pton(AF_INET6, argv[1], &addr, sizeof(addr)) == FNET_OK) { fnet_netif_unbind_ip6_addr(netif, &addr); } else { fnet_shell_println(desc, FAPP_PARAM_ERR, argv[1]); } }
/************************************************************************ * NAME: fapp_dhcp_info * * DESCRIPTION: *************************************************************************/ void fapp_dhcp_info(fnet_shell_desc_t desc) { fnet_char_t ip_str[FNET_IP4_ADDR_STR_SIZE]; fnet_bool_t dhcp_is_enabled = fnet_dhcp_is_enabled(fapp_dhcp_desc); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "DHCP Client", fapp_enabled_str[dhcp_is_enabled]); if(dhcp_is_enabled && (fnet_netif_get_ip4_addr_type(fnet_netif_get_default()) == FNET_NETIF_IP_ADDR_TYPE_DHCP)) { struct fnet_dhcp_options options; fnet_dhcp_get_options(fapp_dhcp_desc, &options); fnet_inet_ntoa(*(struct in_addr *)( &options.dhcp_server), ip_str); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "DHCP Server", ip_str); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Lease Time", fnet_ntohl(options.lease_time)); } }
/************************************************************************ * NAME: fapp_dhcp_info * * DESCRIPTION: *************************************************************************/ void fapp_dhcp_info(fnet_shell_desc_t desc) { fnet_char_t ip_str[FNET_IP4_ADDR_STR_SIZE]; fnet_bool_t dhcp_enabled = fnet_dhcp_enabled(fapp_dhcp_desc); fnet_bool_t address_automatic = fnet_netif_get_ip4_addr_automatic(fnet_netif_get_default()); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "DHCP Client", dhcp_enabled ? FAPP_SHELL_INFO_ENABLED : FAPP_SHELL_INFO_DISABLED); if(dhcp_enabled && address_automatic) { struct fnet_dhcp_options options; fnet_dhcp_get_options(fapp_dhcp_desc, &options); fnet_inet_ntoa(*(struct in_addr *)( &options.dhcp_server), ip_str); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "DHCP Server", ip_str); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Lease Time", fnet_ntohl(options.lease_time)); } }
/************************************************************************ * NAME: fapp_info_print * * DESCRIPTION: Display detailed information about the stack. ************************************************************************/ static void fapp_info_print( fnet_shell_desc_t desc ) { fnet_char_t mac_str[FNET_MAC_ADDR_STR_SIZE]; fnet_mac_addr_t macaddr; fnet_netif_desc_t netif = fnet_netif_get_default(); fapp_netif_info_print(desc, netif); /* HW address, if any */ if(fnet_netif_get_hw_addr(netif, macaddr, sizeof(fnet_mac_addr_t)) == FNET_OK) { fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "MAC Address", fnet_mac_to_str(macaddr, mac_str)); } fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "MTU", fnet_netif_get_mtu(netif)); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "Link Status", fnet_netif_connected(netif) ? "connected" : "unconnected"); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Free Heap", fnet_free_mem_status()); #if FAPP_CFG_HTTP_CMD && FNET_CFG_HTTP fapp_http_info(desc); #endif #if FAPP_CFG_DHCP_CMD && FNET_CFG_DHCP && FNET_CFG_IP4 fapp_dhcp_info(desc); #endif #if FAPP_CFG_TELNET_CMD && FNET_CFG_TELNET fapp_telnet_info(desc); #endif #if FAPP_CFG_TFTPS_CMD && FNET_CFG_TFTP_SRV fapp_tftps_info(desc); #endif #if FAPP_CFG_LLMNR_CMD && FNET_CFG_LLMNR fapp_llmnr_info(desc); #endif }
/************************************************************************ * NAME: fapp_bench_udp_rx * * DESCRIPTION: Start Benchmark UDP server. ************************************************************************/ static void fapp_bench_udp_rx (fnet_shell_desc_t desc, fnet_address_family_t family, struct sockaddr *multicast_address /* optional, set to 0*/) { struct sockaddr local_addr; const fnet_size_t bufsize_option = FAPP_BENCH_SOCKET_BUF_SIZE; fnet_int32_t received; fnet_char_t ip_str[FNET_IP_ADDR_STR_SIZE]; struct sockaddr addr; fnet_size_t addr_len; fnet_bool_t is_first = FNET_TRUE; fnet_bool_t exit_flag = FNET_FALSE; /* Create listen socket */ if((fapp_bench.socket_listen = fnet_socket(family, SOCK_DGRAM, 0)) == FNET_ERR) { FNET_DEBUG("BENCH: Socket creation error.\n"); goto ERROR_1; } /*Bind.*/ fnet_memset_zero(&local_addr, sizeof(local_addr)); local_addr.sa_port = FAPP_BENCH_PORT; local_addr.sa_family = family; if(fnet_socket_bind(fapp_bench.socket_listen, &local_addr, sizeof(local_addr)) == FNET_ERR) { FNET_DEBUG("BENCH: Socket bind error.\n"); goto ERROR_2; } /* Set socket options. */ if( /* Set socket buffer size. */ (fnet_socket_setopt(fapp_bench.socket_listen, SOL_SOCKET, SO_RCVBUF, (fnet_uint8_t *) &bufsize_option, sizeof(bufsize_option))== FNET_ERR) || (fnet_socket_setopt(fapp_bench.socket_listen, SOL_SOCKET, SO_SNDBUF, (fnet_uint8_t *) &bufsize_option, sizeof(bufsize_option))== FNET_ERR) ) { FNET_DEBUG("BENCH: Socket setsockopt error.\n"); goto ERROR_2; } /* Join multicast group, if set. */ if(multicast_address) { #if FNET_CFG_IP4 if(multicast_address->sa_family == AF_INET) { struct ip_mreq mreq; /* Multicast group information.*/ mreq.imr_multiaddr.s_addr = ((struct sockaddr_in*)multicast_address)->sin_addr.s_addr; mreq.imr_interface = 0; /* Default Interface.*/ /* Join multicast group. */ if(fnet_socket_setopt(fapp_bench.socket_listen, IPPROTO_IP, IP_ADD_MEMBERSHIP, (fnet_uint8_t *)&mreq, sizeof(mreq)) == FNET_ERR) { FNET_DEBUG("BENCH: Joining to multicast group is failed.\n"); goto ERROR_2; } } #endif #if FNET_CFG_IP6 if(multicast_address->sa_family == AF_INET6) { struct ipv6_mreq mreq6; /* Multicast group information.*/ FNET_IP6_ADDR_COPY(&((struct sockaddr_in6*)multicast_address)->sin6_addr.s6_addr, &mreq6.ipv6imr_multiaddr.s6_addr); mreq6.ipv6imr_interface = ((struct sockaddr_in6*)multicast_address)->sin6_scope_id; /* Join multicast group. */ if(fnet_socket_setopt(fapp_bench.socket_listen, IPPROTO_IPV6, IPV6_JOIN_GROUP, (fnet_uint8_t *)&mreq6, sizeof(mreq6)) == FNET_ERR) { FNET_DEBUG("BENCH: Joining to multicast group is failed.\n"); goto ERROR_2; } } #endif } /* ------ Start test.----------- */ fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, " UDP RX Test" ); fnet_shell_println(desc, FAPP_DELIMITER_STR); fapp_netif_addr_print(desc, family, fnet_netif_get_default(), FNET_FALSE); if(multicast_address) { fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "Multicast Group", fnet_inet_ntop(multicast_address->sa_family, (fnet_uint8_t*)(multicast_address->sa_data), ip_str, sizeof(ip_str)) ); } fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Local Port", FNET_NTOHS(FAPP_BENCH_PORT)); fnet_shell_println(desc, FAPP_TOCANCEL_STR); fnet_shell_println(desc, FAPP_DELIMITER_STR); while(exit_flag == FNET_FALSE) /* Main loop */ { fnet_shell_println(desc, "Waiting."); fapp_bench.bytes = 0; fapp_bench.remote_bytes = 0; addr_len = sizeof(addr); is_first = FNET_TRUE; while(exit_flag == FNET_FALSE) /* Test loop. */ { /* Receive data */ received = fnet_socket_recvfrom (fapp_bench.socket_listen, (fnet_uint8_t*)(&fapp_bench.buffer[0]), FAPP_BENCH_BUFFER_SIZE, 0, &addr, &addr_len ); if(received >= FAPP_BENCH_UDP_END_BUFFER_LENGTH) { /* Reset timeout. */ fapp_bench.last_time = fnet_timer_ticks(); if(is_first) { if( received > FAPP_BENCH_UDP_END_BUFFER_LENGTH ) { fnet_shell_println(desc,"Receiving from %s:%d", fnet_inet_ntop(addr.sa_family, (fnet_uint8_t*)(addr.sa_data), ip_str, sizeof(ip_str)), fnet_ntohs(addr.sa_port)); fapp_bench.first_time = fnet_timer_ticks(); is_first = FNET_FALSE; } } else { if(received == FAPP_BENCH_UDP_END_BUFFER_LENGTH ) /* End of transfer. */ { /* Send ACK containing amount of received data.*/ fnet_uint32_t ack_bytes = fnet_htonl(fapp_bench.bytes); /* Send several times, just to be sure that it is received/not lost.*/ fnet_socket_sendto(fapp_bench.socket_listen, (fnet_uint8_t*)(&ack_bytes), sizeof(ack_bytes), 0, (struct sockaddr*)&addr, sizeof(addr)); fnet_socket_sendto(fapp_bench.socket_listen, (fnet_uint8_t*)(&ack_bytes), sizeof(ack_bytes), 0, (struct sockaddr*)&addr, sizeof(addr)); fnet_socket_sendto(fapp_bench.socket_listen, (fnet_uint8_t*)(&ack_bytes), sizeof(ack_bytes), 0, (struct sockaddr*)&addr, sizeof(addr)); /* Print benchmark results.*/ fapp_bench_print_results (desc); break; } else fapp_bench.bytes += received; } } else { /* Check error. Check timeout */ if(received == FNET_ERR) { fnet_shell_println(desc, "BENCH: Error (%d).", fnet_error_get()); break; } /* Check timeout. */ if((is_first == FNET_FALSE) && (fnet_timer_get_interval(fapp_bench.last_time, fnet_timer_ticks()) > (FAPP_UDP_TIMEOUT_MS/FNET_TIMER_PERIOD_MS))) { fnet_shell_println(desc, "BENCH: Exit on timeout."); fapp_bench_print_results (desc); break; } } exit_flag = fnet_shell_ctrlc (desc); } } ERROR_2: fnet_socket_close(fapp_bench.socket_listen); ERROR_1: fnet_shell_println(desc, FAPP_BENCH_COMPLETED_STR); }
/************************************************************************ * NAME: fapp_bench_tcp_rx * * DESCRIPTION: Start Benchmark TCP server. ************************************************************************/ static void fapp_bench_tcp_rx (fnet_shell_desc_t desc, fnet_address_family_t family) { struct sockaddr local_addr; fnet_int32_t received; fnet_char_t ip_str[FNET_IP_ADDR_STR_SIZE]; struct linger linger_option = {FNET_TRUE, /*l_onoff*/ 4 /*l_linger*/ }; fnet_size_t bufsize_option = FAPP_BENCH_SOCKET_BUF_SIZE; fnet_int32_t keepalive_option = 1; fnet_int32_t keepcnt_option = FAPP_BENCH_TCP_KEEPCNT; fnet_int32_t keepintvl_option = FAPP_BENCH_TCP_KEEPINTVL; fnet_int32_t keepidle_option = FAPP_BENCH_TCP_KEEPIDLE; struct sockaddr foreign_addr; fnet_size_t addr_len; fnet_bool_t exit_flag = FNET_FALSE; fapp_bench.socket_foreign = FNET_ERR; /* Create listen socket */ if((fapp_bench.socket_listen = fnet_socket(family, SOCK_STREAM, 0)) == FNET_ERR) { FNET_DEBUG("BENCH: Socket creation error."); goto ERROR_1; } /* Bind socket.*/ fnet_memset_zero(&local_addr, sizeof(local_addr)); local_addr.sa_port = FAPP_BENCH_PORT; local_addr.sa_family = family; if(fnet_socket_bind(fapp_bench.socket_listen, &local_addr, sizeof(local_addr)) == FNET_ERR) { FNET_DEBUG("BENCH: Socket bind error."); goto ERROR_2; } /* Set Socket options. */ if( /* Setup linger option. */ (fnet_socket_setopt (fapp_bench.socket_listen, SOL_SOCKET, SO_LINGER, (fnet_uint8_t *)&linger_option, sizeof(linger_option)) == FNET_ERR) || /* Set socket buffer size. */ (fnet_socket_setopt(fapp_bench.socket_listen, SOL_SOCKET, SO_RCVBUF, (fnet_uint8_t *) &bufsize_option, sizeof(bufsize_option))== FNET_ERR) || (fnet_socket_setopt(fapp_bench.socket_listen, SOL_SOCKET, SO_SNDBUF, (fnet_uint8_t *) &bufsize_option, sizeof(bufsize_option))== FNET_ERR) || /* Enable keepalive_option option. */ (fnet_socket_setopt (fapp_bench.socket_listen, SOL_SOCKET, SO_KEEPALIVE, (fnet_uint8_t *)&keepalive_option, sizeof(keepalive_option)) == FNET_ERR) || /* Keepalive probe retransmit limit. */ (fnet_socket_setopt (fapp_bench.socket_listen, IPPROTO_TCP, TCP_KEEPCNT, (fnet_uint8_t *)&keepcnt_option, sizeof(keepcnt_option)) == FNET_ERR) || /* Keepalive retransmit interval.*/ (fnet_socket_setopt (fapp_bench.socket_listen, IPPROTO_TCP, TCP_KEEPINTVL, (fnet_uint8_t *)&keepintvl_option, sizeof(keepintvl_option)) == FNET_ERR) || /* Time between keepalive probes.*/ (fnet_socket_setopt (fapp_bench.socket_listen, IPPROTO_TCP, TCP_KEEPIDLE, (fnet_uint8_t *)&keepidle_option, sizeof(keepidle_option)) == FNET_ERR) ) { FNET_DEBUG("BENCH: Socket setsockopt error.\n"); goto ERROR_2; } /* Listen. */ if(fnet_socket_listen(fapp_bench.socket_listen, 1) == FNET_ERR) { FNET_DEBUG("BENCH: Socket listen error.\n"); goto ERROR_2; } /* ------ Start test.----------- */ fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, " TCP RX Test"); fnet_shell_println(desc, FAPP_DELIMITER_STR); fapp_netif_addr_print(desc, family, fnet_netif_get_default(), FNET_FALSE); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Local Port", FNET_NTOHS(FAPP_BENCH_PORT)); fnet_shell_println(desc, FAPP_TOCANCEL_STR); fnet_shell_println(desc, FAPP_DELIMITER_STR); while(exit_flag == FNET_FALSE) { fnet_shell_println(desc, "Waiting."); fapp_bench.bytes = 0; fapp_bench.remote_bytes = 0; if(fapp_bench.socket_foreign != FNET_ERR) { fnet_socket_close(fapp_bench.socket_foreign); fapp_bench.socket_foreign = FNET_ERR; } while((fapp_bench.socket_foreign == FNET_ERR) && (exit_flag == FNET_FALSE)) { /*Accept*/ addr_len = sizeof(foreign_addr); fapp_bench.socket_foreign = fnet_socket_accept(fapp_bench.socket_listen, &foreign_addr, &addr_len); exit_flag = fnet_shell_ctrlc (desc); if(fapp_bench.socket_foreign != FNET_ERR) { fnet_shell_println(desc,"Receiving from %s:%d", fnet_inet_ntop(foreign_addr.sa_family, (fnet_uint8_t*)(foreign_addr.sa_data), ip_str, sizeof(ip_str)), fnet_ntohs(foreign_addr.sa_port)); fapp_bench.first_time = fnet_timer_ticks(); while(1) /* Receiving data.*/ { received = fnet_socket_recv(fapp_bench.socket_foreign, (fnet_uint8_t*)(&fapp_bench.buffer[0]), FAPP_BENCH_BUFFER_SIZE, 0); if ((received == FNET_ERR) || exit_flag) { fapp_bench.last_time = fnet_timer_ticks(); /* Print benchmark results.*/ fapp_bench_print_results (desc); break; } else { fapp_bench.bytes += received; } exit_flag = fnet_shell_ctrlc (desc); /* Check [Ctrl+c]*/ } } } } fnet_socket_close(fapp_bench.socket_foreign); ERROR_2: fnet_socket_close(fapp_bench.socket_listen); ERROR_1: fnet_shell_println(desc, FAPP_BENCH_COMPLETED_STR); }
/************************************************************************ * NAME: fapp_dhcp_cmd * * DESCRIPTION: Enable DHCP client. ************************************************************************/ void fapp_dhcp_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t **argv ) { struct fnet_dhcp_params dhcp_params; fnet_dhcp_desc_t dhcp_desc; fnet_netif_desc_t netif = fnet_netif_get_default(); if(argc == 1u /* By default is "init".*/ #if 0 /* DHCP reboot feature not used too much. */ ||((argc == 2u) && (fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0)) /* [reboot] */ #endif ||((argc == 2u) && (fnet_strcasecmp(&FAPP_DHCP_COMMAND_AUTOIP[0], argv[1]) == 0)) /* [autoip] */ ) { fnet_memset_zero(&dhcp_params, sizeof(struct fnet_dhcp_params)); fapp_dhcp_discover_counter = FAPP_CFG_DHCP_CMD_DISCOVER_MAX; /* reset counter.*/ #if 0 /* DHCP reboot feature not used too much. */ if(fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0) /* [reboot] */ dhcp_params.requested_ip_address.s_addr = fnet_netif_get_ip4_addr(netif); #endif /* Check Auto-IP flag.*/ if((argc == 2u) && (fnet_strcasecmp(&FAPP_DHCP_COMMAND_AUTOIP[0], argv[1]) == 0)) { fapp_dhcp_autoip = FNET_TRUE; } else { fapp_dhcp_autoip = FNET_FALSE; } dhcp_params.probe_addr = FNET_TRUE; /* Enable probing of the newly received address.*/ /* Save current IP address only if it was allocated manually/statically. */ if(fnet_netif_get_ip4_addr_type(netif) == FNET_NETIF_IP_ADDR_TYPE_MANUAL) { fapp_dhcp_ip_old = fnet_netif_get_ip4_addr(netif); /* Save ip to restore if cancelled. */ } else { fapp_dhcp_ip_old = 0; } /* Start DHCP client */ dhcp_desc = fnet_dhcp_init(netif, &dhcp_params); if(dhcp_desc) { fapp_dhcp_desc = dhcp_desc; /* Register DHCP event handler callbacks. */ fnet_dhcp_set_callback_updated(fapp_dhcp_desc, fapp_dhcp_callback_updated, (void *)desc); fnet_dhcp_set_callback_discover(fapp_dhcp_desc, fapp_dhcp_callback_discover, (void *)desc); fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, " DHCPv4 Client started."); fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, FAPP_TOCANCEL_STR); fnet_shell_block(desc, fapp_dhcp_on_ctrlc); /* Block shell. */ } else { fnet_shell_println(desc, FAPP_INIT_ERR, "DHCP"); } } else if((argc == 2u) && (fnet_strcasecmp(&FAPP_COMMAND_RELEASE[0], argv[1]) == 0)) /* [release] */ { fapp_dhcp_release(); } else { fnet_shell_println(desc, FAPP_PARAM_ERR, argv[1]); } }