nsapi_error_t mbed_lwip_init(emac_interface_t *emac) { // Check if we've already brought up lwip if (!mbed_lwip_get_mac_address()) { // Set up network mbed_lwip_set_mac_address(); sys_sem_new(&lwip_tcpip_inited, 0); sys_sem_new(&lwip_netif_linked, 0); sys_sem_new(&lwip_netif_has_addr, 0); tcpip_init(mbed_lwip_tcpip_init_irq, NULL); sys_arch_sem_wait(&lwip_tcpip_inited, 0); memset(&lwip_netif, 0, sizeof lwip_netif); if (!netif_add(&lwip_netif, #if LWIP_IPV4 0, 0, 0, #endif emac, MBED_NETIF_INIT_FN, tcpip_input)) { return NSAPI_ERROR_DEVICE_ERROR; } netif_set_default(&lwip_netif); netif_set_link_callback(&lwip_netif, mbed_lwip_netif_link_irq); netif_set_status_callback(&lwip_netif, mbed_lwip_netif_status_irq); #if !DEVICE_EMAC eth_arch_enable_interrupts(); #endif } return NSAPI_ERROR_OK; }
/** * @brief Initializes the lwIP stack * @param None * @retval None */ static void Netif_Config(void) { struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; /* IP address default setting */ IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); /* add the network interface */ netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ð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); } /* Set the link callback function, this function is called on change of link status*/ netif_set_link_callback(&gnetif, ethernetif_update_config); }
/* Internal API to preserve existing PPP functionality - revise to better match mbed_ipstak_add_ethernet_interface later */ nsapi_error_t LWIP::_add_ppp_interface(void *hw, bool default_if, nsapi_ip_stack_t stack, LWIP::Interface **interface_out) { #if LWIP_PPP_API Interface *interface = new (std::nothrow) Interface(); if (!interface) { return NSAPI_ERROR_NO_MEMORY; } interface->hw = hw; interface->ppp = true; nsapi_error_t ret = ppp_lwip_if_init(hw, &interface->netif, stack); if (ret != NSAPI_ERROR_OK) { free(interface); return ret; } if (default_if) { netif_set_default(&interface->netif); default_interface = interface; } netif_set_link_callback(&interface->netif, &LWIP::Interface::netif_link_irq); netif_set_status_callback(&interface->netif, &LWIP::Interface::netif_status_irq); *interface_out = interface; return NSAPI_ERROR_OK; #else return NSAPI_ERROR_UNSUPPORTED; #endif //LWIP_PPP_API }
/** * @brief Initializes the lwIP stack * @param None * @retval None */ static void Netif_Config(void) { struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; /* IP address setting */ IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); /* - netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask, struct ip_addr *gw, void *state, err_t (* init)(struct netif *netif), err_t (* input)(struct pbuf *p, struct netif *netif)) Adds your network interface to the netif_list. Allocate a struct netif and pass a pointer to this structure as the first argument. Give pointers to cleared ip_addr structures when using DHCP, or fill them with sane numbers otherwise. The state pointer may be NULL. The init function pointer must point to a initialization function for your ethernet netif interface. The following code illustrates it's use.*/ netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_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); } /* Set the link callback function, this function is called on change of link status*/ netif_set_link_callback(&gnetif, ethernetif_update_config); /* create a binary semaphore used for informing ethernetif of frame reception */ osSemaphoreDef(Netif_SEM); Netif_LinkSemaphore = osSemaphoreCreate(osSemaphore(Netif_SEM) , 1 ); link_arg.netif = &gnetif; link_arg.semaphore = Netif_LinkSemaphore; /* Create the Ethernet link handler thread */ #if defined(__GNUC__) osThreadDef(LinkThr, ethernetif_set_link, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 5); #else osThreadDef(LinkThr, ethernetif_set_link, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 2); #endif osThreadCreate (osThread(LinkThr), &link_arg); }
nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out) { #if LWIP_ETHERNET Interface *interface = new (std::nothrow) Interface(); if (!interface) { return NSAPI_ERROR_NO_MEMORY; } interface->emac = &emac; interface->memory_manager = &memory_manager; interface->ppp = false; #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE) netif->interface.hwaddr[0] = MBED_MAC_ADDR_0; netif->interface.hwaddr[1] = MBED_MAC_ADDR_1; netif->interface.hwaddr[2] = MBED_MAC_ADDR_2; netif->interface.hwaddr[3] = MBED_MAC_ADDR_3; netif->interface.hwaddr[4] = MBED_MAC_ADDR_4; netif->interface.hwaddr[5] = MBED_MAC_ADDR_5; #else mbed_mac_address((char *) interface->netif.hwaddr); #endif interface->netif.hwaddr_len = 6; if (!netif_add(&interface->netif, #if LWIP_IPV4 0, 0, 0, #endif interface, &LWIP::Interface::emac_if_init, tcpip_input)) { return NSAPI_ERROR_DEVICE_ERROR; } if (default_if) { netif_set_default(&interface->netif); default_interface = interface; } netif_set_link_callback(&interface->netif, &LWIP::Interface::netif_link_irq); netif_set_status_callback(&interface->netif, &LWIP::Interface::netif_status_irq); *interface_out = interface; /* Use mac address as additional seed to random number generator */ uint64_t seed = interface->netif.hwaddr[0]; for (uint8_t i = 1; i < 8; i++) { seed <<= 8; seed |= interface->netif.hwaddr[i % 6]; } lwip_add_random_seed(seed); return NSAPI_ERROR_OK; #else return NSAPI_ERROR_UNSUPPORTED; #endif //LWIP_ETHERNET }
static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { tcpip_init(tcpip_init_done, NULL); tcpip_inited.wait(); memset((void*) &lpcNetif, 0, sizeof(lpcNetif)); netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, tcpip_input); netif_set_default(&lpcNetif); netif_set_link_callback (&lpcNetif, netif_link_callback); netif_set_status_callback(&lpcNetif, netif_status_callback); }
void LWIP_SOCKETS_Driver::TcpipInitDone(void* arg) { struct netif *pNetIf; for (int i = 0; i<g_NetworkConfig.NetworkInterfaceCount; i++) { int interfaceNumber; SOCK_NetworkConfiguration *pNetCfg = &g_NetworkConfig.NetworkInterfaces[i]; /* Bind and Open the Ethernet driver */ Network_Interface_Bind(i); interfaceNumber = Network_Interface_Open(i); if (interfaceNumber == SOCK_SOCKET_ERROR) { DEBUG_HANDLE_SOCKET_ERROR("Network init", FALSE); debug_printf("SocketError: %d\n", errno); continue; } g_LWIP_SOCKETS_Driver.m_interfaces[i].m_interfaceNumber = interfaceNumber; UpdateAdapterConfiguration(i, SOCK_NETWORKCONFIGURATION_UPDATE_DHCP | SOCK_NETWORKCONFIGURATION_UPDATE_DNS, pNetCfg); pNetIf = netif_find_interface(interfaceNumber); if (pNetIf) { netif_set_link_callback(pNetIf, Link_callback); if (netif_is_link_up(pNetIf)) Link_callback(pNetIf); netif_set_status_callback(pNetIf, Status_callback); if (netif_is_up(pNetIf)) Status_callback(pNetIf); // default debugger interface if (0 == i) { UINT8* addr = (UINT8*)&pNetIf->ip_addr.addr; lcd_printf("\f\n\n\n\n\n\n\nip address: %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]); debug_printf("ip address from interface info: %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]); } } } }
nsapi_error_t LWIP::add_l3ip_interface(L3IP &l3ip, bool default_if, OnboardNetworkStack::Interface **interface_out) { #if LWIP_L3IP Interface *interface = new (std::nothrow) Interface(); if (!interface) { return NSAPI_ERROR_NO_MEMORY; } interface->l3ip = &l3ip; interface->memory_manager = &memory_manager; interface->ppp = false; // interface->netif.hwaddr_len = 0; should we set? if (!netif_add(&interface->netif, #if LWIP_IPV4 0, 0, 0, #endif interface, &LWIP::Interface::emac_if_init, ip_input)) { return NSAPI_ERROR_DEVICE_ERROR; } if (default_if) { netif_set_default(&interface->netif); default_interface = interface; } netif_set_link_callback(&interface->netif, &LWIP::Interface::netif_link_irq); netif_set_status_callback(&interface->netif, &LWIP::Interface::netif_status_irq); *interface_out = interface; //lwip_add_random_seed(seed); to do? return NSAPI_ERROR_OK; #else return NSAPI_ERROR_UNSUPPORTED; #endif //LWIP_L3IP }
int lwip_bringup(void) { // Check if we've already connected if (lwip_get_ip_address()) { return NSAPI_ERROR_PARAMETER; } // Check if we've already brought up lwip if (!lwip_get_mac_address()) { // Set up network lwip_set_mac_address(); sys_sem_new(&lwip_tcpip_inited, 0); sys_sem_new(&lwip_netif_linked, 0); sys_sem_new(&lwip_netif_up, 0); tcpip_init(lwip_tcpip_init_irq, NULL); sys_arch_sem_wait(&lwip_tcpip_inited, 0); memset(&lwip_netif, 0, sizeof lwip_netif); netif_add(&lwip_netif, 0, 0, 0, NULL, eth_arch_enetif_init, tcpip_input); netif_set_default(&lwip_netif); netif_set_link_callback (&lwip_netif, lwip_netif_link_irq); netif_set_status_callback(&lwip_netif, lwip_netif_status_irq); eth_arch_enable_interrupts(); } // Zero out socket set lwip_arena_init(); // Connect to the network dhcp_start(&lwip_netif); // Wait for an IP Address u32_t ret = sys_arch_sem_wait(&lwip_netif_up, 15000); if (ret == SYS_ARCH_TIMEOUT) { return NSAPI_ERROR_DHCP_FAILURE; } return 0; }
/** * @brief Initializes the lwIP stack * @param None * @retval None */ void LwIP_Init(void) { struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; #if defined(USE_LCD) && !defined(USE_DHCP) uint8_t iptab[4]; uint8_t iptxt[20]; #endif /* Create tcp_ip stack thread */ tcpip_init( NULL, NULL ); /* IP address setting & display on STM32_evalboard LCD*/ #ifdef USE_DHCP ipaddr.addr = 0; netmask.addr = 0; gw.addr = 0; #else IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); #ifdef USE_LCD iptab[0] = IP_ADDR3; iptab[1] = IP_ADDR2; iptab[2] = IP_ADDR1; iptab[3] = IP_ADDR0; sprintf((char*)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]); LCD_DisplayStringLine(Line8, (uint8_t*)" Static IP address "); LCD_DisplayStringLine(Line9, iptxt); #endif #endif /* - netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask, struct ip_addr *gw, void *state, err_t (* init)(struct netif *netif), err_t (* input)(struct pbuf *p, struct netif *netif)) Adds your network interface to the netif_list. Allocate a struct netif and pass a pointer to this structure as the first argument. Give pointers to cleared ip_addr structures when using DHCP, or fill them with sane numbers otherwise. The state pointer may be NULL. The init function pointer must point to a initialization function for your ethernet netif interface. The following code illustrates it's use.*/ netif_add(&xnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input); /* Registers the default network interface */ netif_set_default(&xnetif); if (netif_is_link_up(&xnetif)) { /* When the netif is fully configured this function must be called */ netif_set_up(&xnetif); } else { /* When the netif link is down this function must be called */ netif_set_down(&xnetif); } /* Set the link callback function, this function is called on change of link status */ netif_set_link_callback(&xnetif, ethernetif_update_config); }
/* This function initializes all network interfaces */ static void msvc_netif_init() { #if USE_ETHERNET ip_addr_t ipaddr, netmask, gw; #endif /* USE_ETHERNET */ #if PPP_SUPPORT const char *username = NULL, *password = NULL; #ifdef PPP_USERNAME username = PPP_USERNAME; #endif #ifdef PPP_PASSWORD password = PPP_PASSWORD; #endif printf("pppInit\n"); pppInit(); pppSetAuth(PPPAUTHTYPE_ANY, username, password); printf("pppOpen: COM%d\n", (int)sio_idx); #if PPPOS_SUPPORT ppp_sio = sio_open(sio_idx); if (ppp_sio == NULL) { printf("sio_open error\n"); } else { ppp_desc = pppOpen(ppp_sio, pppLinkStatusCallback, NULL); } #endif /* PPPOS_SUPPORT */ #endif /* PPP_SUPPORT */ #if USE_ETHERNET ip_addr_set_zero(&gw); ip_addr_set_zero(&ipaddr); ip_addr_set_zero(&netmask); #if USE_ETHERNET_TCPIP #if USE_DHCP printf("Starting lwIP, local interface IP is dhcp-enabled\n"); #elif USE_AUTOIP printf("Starting lwIP, local interface IP is autoip-enabled\n"); #else /* USE_DHCP */ LWIP_PORT_INIT_GW(&gw); LWIP_PORT_INIT_IPADDR(&ipaddr); LWIP_PORT_INIT_NETMASK(&netmask); printf("Starting lwIP, local interface IP is %s\n", ip_ntoa(&ipaddr)); #endif /* USE_DHCP */ #endif /* USE_ETHERNET_TCPIP */ #if NO_SYS #if LWIP_ARP netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, pcapif_init, ethernet_input)); #else /* LWIP_ARP */ netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, pcapif_init, ip_input)); #endif /* LWIP_ARP */ #else /* NO_SYS */ netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, pcapif_init, tcpip_input)); #if LWIP_IPV6 netif_create_ip6_linklocal_address(&netif, 1); printf("ip6 linklocal address: "); ip6_addr_debug_print(0xFFFFFFFF & ~LWIP_DBG_HALT, &netif.ip6_addr[0]); printf("\n"); #endif /* LWIP_IPV6 */ #endif /* NO_SYS */ #if LWIP_NETIF_STATUS_CALLBACK netif_set_status_callback(&netif, status_callback); #endif /* LWIP_NETIF_STATUS_CALLBACK */ #if LWIP_NETIF_LINK_CALLBACK netif_set_link_callback(&netif, link_callback); #endif /* LWIP_NETIF_LINK_CALLBACK */ #if USE_ETHERNET_TCPIP #if LWIP_AUTOIP autoip_set_struct(&netif, &netif_autoip); #endif /* LWIP_AUTOIP */ #if LWIP_DHCP dhcp_set_struct(&netif, &netif_dhcp); #endif /* LWIP_DHCP */ #if USE_DHCP dhcp_start(&netif); #elif USE_AUTOIP autoip_start(&netif); #else /* USE_DHCP */ netif_set_up(&netif); #endif /* USE_DHCP */ #else /* USE_ETHERNET_TCPIP */ /* Use ethernet for PPPoE only */ netif.flags &= ~(NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP); /* no ARP */ netif.flags |= NETIF_FLAG_ETHERNET; /* but pure ethernet */ #endif /* USE_ETHERNET_TCPIP */ #if PPP_SUPPORT && PPPOE_SUPPORT /* start PPPoE after ethernet netif is added! */ ppp_desc = pppOverEthernetOpen(&netif, NULL, NULL, pppLinkStatusCallback, NULL); #endif /* PPP_SUPPORT && PPPOE_SUPPORT */ #endif /* USE_ETHERNET */ }
nsapi_error_t mbed_lwip_bringup_2(bool dhcp, bool ppp, const char *ip, const char *netmask, const char *gw, const nsapi_ip_stack_t stack) { // Check if we've already connected if (lwip_connected == NSAPI_STATUS_GLOBAL_UP) { return NSAPI_ERROR_IS_CONNECTED; } else if (lwip_connected == NSAPI_STATUS_CONNECTING) { return NSAPI_ERROR_ALREADY; } lwip_connected = NSAPI_STATUS_CONNECTING; lwip_ppp = ppp; #if LWIP_DHCP lwip_dhcp_has_to_be_set = true; if (stack != IPV6_STACK) { lwip_dhcp = dhcp; } else { lwip_dhcp = false; } #endif mbed_lwip_core_init(); nsapi_error_t ret; if (netif_inited) { /* Can't cope with changing mode */ if (netif_is_ppp == ppp) { ret = NSAPI_ERROR_OK; } else { ret = NSAPI_ERROR_PARAMETER; } } else { if (ppp) { ret = ppp_lwip_if_init(&lwip_netif, stack); } else { ret = mbed_lwip_emac_init(NULL); } } if (ret != NSAPI_ERROR_OK) { lwip_connected = NSAPI_STATUS_DISCONNECTED; return ret; } if (lwip_client_callback) { lwip_client_callback(lwip_status_cb_handle, NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_CONNECTING); } netif_inited = true; if (ppp) { netif_is_ppp = ppp; } netif_set_default(&lwip_netif); netif_set_link_callback(&lwip_netif, mbed_lwip_netif_link_irq); netif_set_status_callback(&lwip_netif, mbed_lwip_netif_status_irq); #if LWIP_IPV6 if (stack != IPV4_STACK) { if (lwip_netif.hwaddr_len == ETH_HWADDR_LEN) { netif_create_ip6_linklocal_address(&lwip_netif, 1/*from MAC*/); } #if LWIP_IPV6_MLD /* * For hardware/netifs that implement MAC filtering. * All-nodes link-local is handled by default, so we must let the hardware know * to allow multicast packets in. * Should set mld_mac_filter previously. */ if (lwip_netif.mld_mac_filter != NULL) { ip6_addr_t ip6_allnodes_ll; ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll); lwip_netif.mld_mac_filter(&lwip_netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER); } #endif /* LWIP_IPV6_MLD */ #if LWIP_IPV6_AUTOCONFIG /* IPv6 address autoconfiguration not enabled by default */ lwip_netif.ip6_autoconfig_enabled = 1; } else { // Disable router solidifications lwip_netif.rs_count = 0; } #endif /* LWIP_IPV6_AUTOCONFIG */ #endif // LWIP_IPV6 #if LWIP_IPV4 if (stack != IPV6_STACK) { if (!dhcp && !ppp) { ip4_addr_t ip_addr; ip4_addr_t netmask_addr; ip4_addr_t gw_addr; if (!inet_aton(ip, &ip_addr) || !inet_aton(netmask, &netmask_addr) || !inet_aton(gw, &gw_addr)) { lwip_connected = NSAPI_STATUS_DISCONNECTED; if (lwip_client_callback) { lwip_client_callback(lwip_status_cb_handle, NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED); } return NSAPI_ERROR_PARAMETER; } netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr); } } #endif if (ppp) { err_t err = ppp_lwip_connect(); if (err) { lwip_connected = NSAPI_STATUS_DISCONNECTED; if (lwip_client_callback) { lwip_client_callback(lwip_status_cb_handle, NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED); } return mbed_lwip_err_remap(err); } } if (!netif_is_link_up(&lwip_netif)) { if (lwip_blocking) { if (sys_arch_sem_wait(&lwip_netif_linked, 15000) == SYS_ARCH_TIMEOUT) { if (ppp) { ppp_lwip_disconnect(); } return NSAPI_ERROR_NO_CONNECTION; } } } else { ret = mbed_set_dhcp(&lwip_netif); if (ret != NSAPI_ERROR_OK) { return ret; } } if (lwip_blocking) { // If doesn't have address if (!mbed_lwip_get_ip_addr(true, &lwip_netif)) { if (sys_arch_sem_wait(&lwip_netif_has_any_addr, DHCP_TIMEOUT * 1000) == SYS_ARCH_TIMEOUT) { if (ppp) { ppp_lwip_disconnect(); } return NSAPI_ERROR_DHCP_FAILURE; } } } else { return NSAPI_ERROR_OK; } #if PREF_ADDR_TIMEOUT if (stack != IPV4_STACK && stack != IPV6_STACK) { // If address is not for preferred stack waits a while to see // if preferred stack address is acquired if (!mbed_lwip_get_ip_addr(false, &lwip_netif)) { sys_arch_sem_wait(&lwip_netif_has_pref_addr, PREF_ADDR_TIMEOUT * 1000); } } #endif #if BOTH_ADDR_TIMEOUT if (stack != IPV4_STACK && stack != IPV6_STACK) { // If addresses for both stacks are not available waits a while to // see if address for both stacks are acquired if (!(mbed_lwip_get_ipv4_addr(&lwip_netif) && mbed_lwip_get_ipv6_addr(&lwip_netif))) { sys_arch_sem_wait(&lwip_netif_has_both_addr, BOTH_ADDR_TIMEOUT * 1000); } } #endif add_dns_addr(&lwip_netif); return NSAPI_ERROR_OK; }
/* This function initializes all network interfaces */ static void msvc_netif_init() { #if USE_ETHERNET ip_addr_t ipaddr, netmask, gw; #endif /* USE_ETHERNET */ #if USE_SLIPIF u8_t num_slip1 = 0; ip_addr_t ipaddr_slip1, netmask_slip1, gw_slip1; #if USE_SLIPIF > 1 u8_t num_slip2 = 1; ip_addr_t ipaddr_slip2, netmask_slip2, gw_slip2; #endif /* USE_SLIPIF > 1 */ #endif /* USE_SLIPIF */ #if PPP_SUPPORT const char *username = NULL, *password = NULL; #ifdef PPP_USERNAME username = PPP_USERNAME; #endif #ifdef PPP_PASSWORD password = PPP_PASSWORD; #endif printf("pppInit\n"); pppInit(); pppSetAuth(PPPAUTHTYPE_ANY, username, password); printf("pppOpen: COM%d\n", (int)sio_idx); #if PPPOS_SUPPORT ppp_sio = sio_open(sio_idx); if (ppp_sio == NULL) { printf("sio_open error\n"); } else { ppp_desc = pppOpen(ppp_sio, pppLinkStatusCallback, NULL); } #endif /* PPPOS_SUPPORT */ #endif /* PPP_SUPPORT */ #if USE_ETHERNET ip_addr_set_zero(&gw); ip_addr_set_zero(&ipaddr); ip_addr_set_zero(&netmask); #if USE_ETHERNET_TCPIP #if USE_DHCP printf("Starting lwIP, local interface IP is dhcp-enabled\n"); #elif USE_AUTOIP printf("Starting lwIP, local interface IP is autoip-enabled\n"); #else /* USE_DHCP */ LWIP_PORT_INIT_GW(&gw); LWIP_PORT_INIT_IPADDR(&ipaddr); LWIP_PORT_INIT_NETMASK(&netmask); printf("Starting lwIP, local interface IP is %s\n", ip_ntoa(&ipaddr)); #endif /* USE_DHCP */ #endif /* USE_ETHERNET_TCPIP */ #if NO_SYS #if LWIP_ARP netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, pcapif_init, ethernet_input)); #else /* LWIP_ARP */ netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, pcapif_init, ip_input)); #endif /* LWIP_ARP */ #else /* NO_SYS */ netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, pcapif_init, tcpip_input)); #endif /* NO_SYS */ #if LWIP_NETIF_STATUS_CALLBACK netif_set_status_callback(&netif, status_callback); #endif /* LWIP_NETIF_STATUS_CALLBACK */ #if LWIP_NETIF_LINK_CALLBACK netif_set_link_callback(&netif, link_callback); #endif /* LWIP_NETIF_LINK_CALLBACK */ #if USE_ETHERNET_TCPIP #if LWIP_AUTOIP autoip_set_struct(&netif, &netif_autoip); #endif /* LWIP_AUTOIP */ #if LWIP_DHCP dhcp_set_struct(&netif, &netif_dhcp); #endif /* LWIP_DHCP */ #if USE_DHCP dhcp_start(&netif); #elif USE_AUTOIP autoip_start(&netif); #else /* USE_DHCP */ netif_set_up(&netif); #endif /* USE_DHCP */ #else /* USE_ETHERNET_TCPIP */ /* Use ethernet for PPPoE only */ netif.flags &= ~(NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP); /* no ARP */ netif.flags |= NETIF_FLAG_ETHERNET; /* but pure ethernet */ #endif /* USE_ETHERNET_TCPIP */ #if PPP_SUPPORT && PPPOE_SUPPORT /* start PPPoE after ethernet netif is added! */ ppp_desc = pppOverEthernetOpen(&netif, NULL, NULL, pppLinkStatusCallback, NULL); #endif /* PPP_SUPPORT && PPPOE_SUPPORT */ #endif /* USE_ETHERNET */ #if USE_SLIPIF LWIP_PORT_INIT_SLIP1_IPADDR(&ipaddr_slip1); LWIP_PORT_INIT_SLIP1_GW(&gw_slip1); LWIP_PORT_INIT_SLIP1_NETMASK(&netmask_slip1); printf("Starting lwIP slipif, local interface IP is %s\n", ip_ntoa(&ipaddr_slip1)); #if SIO_USE_COMPORT num_slip1++; /* COM ports cannot be 0-based */ #endif netif_add(&slipif1, &ipaddr_slip1, &netmask_slip1, &gw_slip1, &num_slip1, slipif_init, ip_input); #if !USE_ETHERNET netif_set_default(&slipif1); #endif /* !USE_ETHERNET */ #if LWIP_NETIF_STATUS_CALLBACK netif_set_status_callback(&slipif1, status_callback); #endif /* LWIP_NETIF_STATUS_CALLBACK */ #if LWIP_NETIF_LINK_CALLBACK netif_set_link_callback(&slipif1, link_callback); #endif /* LWIP_NETIF_LINK_CALLBACK */ netif_set_up(&slipif1); #if USE_SLIPIF > 1 LWIP_PORT_INIT_SLIP2_IPADDR(&ipaddr_slip2); LWIP_PORT_INIT_SLIP2_GW(&gw_slip2); LWIP_PORT_INIT_SLIP2_NETMASK(&netmask_slip2); printf("Starting lwIP SLIP if #2, local interface IP is %s\n", ip_ntoa(&ipaddr_slip2)); #if SIO_USE_COMPORT num_slip2++; /* COM ports cannot be 0-based */ #endif netif_add(&slipif2, &ipaddr_slip2, &netmask_slip2, &gw_slip2, &num_slip2, slipif_init, ip_input); #if LWIP_NETIF_STATUS_CALLBACK netif_set_status_callback(&slipif2, status_callback); #endif /* LWIP_NETIF_STATUS_CALLBACK */ #if LWIP_NETIF_LINK_CALLBACK netif_set_link_callback(&slipif2, link_callback); #endif /* LWIP_NETIF_LINK_CALLBACK */ netif_set_up(&slipif2); #endif /* USE_SLIPIF > 1*/ #endif /* USE_SLIPIF */ }
/** * @brief Initializes the lwIP stack * @param None * @retval None */ void LwIP_Init(void) { struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; #ifndef USE_DHCP uint8_t iptab[4] = {0}; uint8_t iptxt[20]; #endif /* Initializes the dynamic memory heap defined by MEM_SIZE.*/ mem_init(); /* Initializes the memory pools defined by MEMP_NUM_x.*/ memp_init(); #ifdef USE_DHCP ipaddr.addr = 0; netmask.addr = 0; gw.addr = 0; #else IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); #endif /* - netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask, struct ip_addr *gw, void *state, err_t (* init)(struct netif *netif), err_t (* input)(struct pbuf *p, struct netif *netif)) Adds your network interface to the netif_list. Allocate a struct netif and pass a pointer to this structure as the first argument. Give pointers to cleared ip_addr structures when using DHCP, or fill them with sane numbers otherwise. The state pointer may be NULL. The init function pointer must point to a initialization function for your ethernet netif interface. The following code illustrates it's use.*/ netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input); /* Registers the default network interface.*/ netif_set_default(&gnetif); if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG)) { /* Set Ethernet link flag */ gnetif.flags |= NETIF_FLAG_LINK_UP; /* When the netif is fully configured this function must be called.*/ netif_set_up(&gnetif); #ifdef USE_DHCP DHCP_state = DHCP_START; #else #ifdef USE_LCD iptab[0] = IP_ADDR3; iptab[1] = IP_ADDR2; iptab[2] = IP_ADDR1; iptab[3] = IP_ADDR0; sprintf((char*)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]); LCD_DisplayStringLine(Line8, (uint8_t*)" Static IP address "); LCD_DisplayStringLine(Line9, iptxt); #endif #endif /* USE_DHCP */ } else { /* When the netif link is down this function must be called.*/ netif_set_down(&gnetif); #ifdef USE_DHCP DHCP_state = DHCP_LINK_DOWN; #endif /* USE_DHCP */ /* Set the LCD Text Color */ // LCD_SetTextColor(Red); /* Display message on the LCD */ // LCD_DisplayStringLine(Line5, (uint8_t*)" Network Cable is "); // LCD_DisplayStringLine(Line6, (uint8_t*)" not connected "); /* Set the LCD Text Color */ // LCD_SetTextColor(White); } /* Set the link callback function, this function is called on change of link status*/ netif_set_link_callback(&gnetif, ETH_link_callback); }
/* This function initializes all network interfaces */ static void msvc_netif_init(void) { #if LWIP_IPV4 && USE_ETHERNET ip4_addr_t ipaddr, netmask, gw; #endif /* LWIP_IPV4 && USE_ETHERNET */ #if USE_SLIPIF u8_t num_slip1 = 0; #if LWIP_IPV4 ip4_addr_t ipaddr_slip1, netmask_slip1, gw_slip1; #endif #if USE_SLIPIF > 1 u8_t num_slip2 = 1; #if LWIP_IPV4 ip4_addr_t ipaddr_slip2, netmask_slip2, gw_slip2; #endif #endif /* USE_SLIPIF > 1 */ #endif /* USE_SLIPIF */ #if USE_DHCP || USE_AUTOIP err_t err; #endif #if USE_PPP const char *username = NULL, *password = NULL; #ifdef PPP_USERNAME username = PPP_USERNAME; #endif #ifdef PPP_PASSWORD password = PPP_PASSWORD; #endif printf("ppp_connect: COM%d\n", (int)sio_idx); #if PPPOS_SUPPORT ppp_sio = sio_open(sio_idx); if (ppp_sio == NULL) { printf("sio_open error\n"); } else { ppp = pppos_create(&ppp_netif, ppp_output_cb, pppLinkStatusCallback, NULL); if (ppp == NULL) { printf("pppos_create error\n"); } else { ppp_set_auth(ppp, PPPAUTHTYPE_ANY, username, password); ppp_connect(ppp, 0); } } #endif /* PPPOS_SUPPORT */ #endif /* USE_PPP */ #if USE_ETHERNET #if LWIP_IPV4 #define NETIF_ADDRS &ipaddr, &netmask, &gw, ip4_addr_set_zero(&gw); ip4_addr_set_zero(&ipaddr); ip4_addr_set_zero(&netmask); #if USE_ETHERNET_TCPIP #if USE_DHCP printf("Starting lwIP, local interface IP is dhcp-enabled\n"); #elif USE_AUTOIP printf("Starting lwIP, local interface IP is autoip-enabled\n"); #else /* USE_DHCP */ LWIP_PORT_INIT_GW(&gw); LWIP_PORT_INIT_IPADDR(&ipaddr); LWIP_PORT_INIT_NETMASK(&netmask); printf("Starting lwIP, local interface IP is %s\n", ip4addr_ntoa(&ipaddr)); #endif /* USE_DHCP */ #endif /* USE_ETHERNET_TCPIP */ #else /* LWIP_IPV4 */ #define NETIF_ADDRS printf("Starting lwIP, IPv4 disable\n"); #endif /* LWIP_IPV4 */ #if NO_SYS netif_set_default(netif_add(&netif, NETIF_ADDRS NULL, pcapif_init, netif_input)); #else /* NO_SYS */ netif_set_default(netif_add(&netif, NETIF_ADDRS NULL, pcapif_init, tcpip_input)); #endif /* NO_SYS */ #if LWIP_IPV6 netif_create_ip6_linklocal_address(&netif, 1); printf("ip6 linklocal address: "); ip6_addr_debug_print(0xFFFFFFFF & ~LWIP_DBG_HALT, netif_ip6_addr(&netif, 0)); printf("\n"); #endif /* LWIP_IPV6 */ #if LWIP_NETIF_STATUS_CALLBACK netif_set_status_callback(&netif, status_callback); #endif /* LWIP_NETIF_STATUS_CALLBACK */ #if LWIP_NETIF_LINK_CALLBACK netif_set_link_callback(&netif, link_callback); #endif /* LWIP_NETIF_LINK_CALLBACK */ #if USE_ETHERNET_TCPIP #if LWIP_AUTOIP autoip_set_struct(&netif, &netif_autoip); #endif /* LWIP_AUTOIP */ #if LWIP_DHCP dhcp_set_struct(&netif, &netif_dhcp); #endif /* LWIP_DHCP */ netif_set_up(&netif); #if USE_DHCP err = dhcp_start(&netif); LWIP_ASSERT("dhcp_start failed", err == ERR_OK); #elif USE_AUTOIP err = autoip_start(&netif); LWIP_ASSERT("autoip_start failed", err == ERR_OK); #endif /* USE_DHCP */ #else /* USE_ETHERNET_TCPIP */ /* Use ethernet for PPPoE only */ netif.flags &= ~(NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP); /* no ARP */ netif.flags |= NETIF_FLAG_ETHERNET; /* but pure ethernet */ #endif /* USE_ETHERNET_TCPIP */ #if USE_PPP && PPPOE_SUPPORT /* start PPPoE after ethernet netif is added! */ ppp = pppoe_create(&ppp_netif, &netif, NULL, NULL, pppLinkStatusCallback, NULL); if (ppp == NULL) { printf("pppos_create error\n"); } else { ppp_set_auth(ppp, PPPAUTHTYPE_ANY, username, password); ppp_connect(ppp, 0); } #endif /* USE_PPP && PPPOE_SUPPORT */ #endif /* USE_ETHERNET */ #if USE_SLIPIF #if LWIP_IPV4 #define SLIP1_ADDRS &ipaddr_slip1, &netmask_slip1, &gw_slip1, LWIP_PORT_INIT_SLIP1_IPADDR(&ipaddr_slip1); LWIP_PORT_INIT_SLIP1_GW(&gw_slip1); LWIP_PORT_INIT_SLIP1_NETMASK(&netmask_slip1); printf("Starting lwIP slipif, local interface IP is %s\n", ip4addr_ntoa(&ipaddr_slip1)); #else #define SLIP1_ADDRS printf("Starting lwIP slipif\n"); #endif #if defined(SIO_USE_COMPORT) && SIO_USE_COMPORT num_slip1++; /* COM ports cannot be 0-based */ #endif netif_add(&slipif1, SLIP1_ADDRS &num_slip1, slipif_init, ip_input); #if !USE_ETHERNET netif_set_default(&slipif1); #endif /* !USE_ETHERNET */ #if LWIP_IPV6 netif_create_ip6_linklocal_address(&slipif1, 1); printf("SLIP ip6 linklocal address: "); ip6_addr_debug_print(0xFFFFFFFF & ~LWIP_DBG_HALT, netif_ip6_addr(&slipif1, 0)); printf("\n"); #endif /* LWIP_IPV6 */ #if LWIP_NETIF_STATUS_CALLBACK netif_set_status_callback(&slipif1, status_callback); #endif /* LWIP_NETIF_STATUS_CALLBACK */ #if LWIP_NETIF_LINK_CALLBACK netif_set_link_callback(&slipif1, link_callback); #endif /* LWIP_NETIF_LINK_CALLBACK */ netif_set_up(&slipif1); #if USE_SLIPIF > 1 #if LWIP_IPV4 #define SLIP2_ADDRS &ipaddr_slip2, &netmask_slip2, &gw_slip2, LWIP_PORT_INIT_SLIP2_IPADDR(&ipaddr_slip2); LWIP_PORT_INIT_SLIP2_GW(&gw_slip2); LWIP_PORT_INIT_SLIP2_NETMASK(&netmask_slip2); printf("Starting lwIP SLIP if #2, local interface IP is %s\n", ip4addr_ntoa(&ipaddr_slip2)); #else #define SLIP2_ADDRS printf("Starting lwIP SLIP if #2\n"); #endif #if defined(SIO_USE_COMPORT) && SIO_USE_COMPORT num_slip2++; /* COM ports cannot be 0-based */ #endif netif_add(&slipif2, SLIP2_ADDRS &num_slip2, slipif_init, ip_input); #if LWIP_IPV6 netif_create_ip6_linklocal_address(&slipif1, 1); printf("SLIP2 ip6 linklocal address: "); ip6_addr_debug_print(0xFFFFFFFF & ~LWIP_DBG_HALT, netif_ip6_addr(&slipif2, 0)); printf("\n"); #endif /* LWIP_IPV6 */ #if LWIP_NETIF_STATUS_CALLBACK netif_set_status_callback(&slipif2, status_callback); #endif /* LWIP_NETIF_STATUS_CALLBACK */ #if LWIP_NETIF_LINK_CALLBACK netif_set_link_callback(&slipif2, link_callback); #endif /* LWIP_NETIF_LINK_CALLBACK */ netif_set_up(&slipif2); #endif /* USE_SLIPIF > 1*/ #endif /* USE_SLIPIF */ }