mirror of https://github.com/ARMmbed/mbed-os.git
EMAC: Fix LWIP_IPV4 and DEVICE_EMAC flag usage
parent
5cd2d7869e
commit
d0be5733db
|
|
@ -33,11 +33,6 @@ static err_t emac_lwip_low_level_output(struct netif *netif, struct pbuf *p)
|
||||||
return ret ? ERR_OK : ERR_IF;
|
return ret ? ERR_OK : ERR_IF;
|
||||||
}
|
}
|
||||||
|
|
||||||
static err_t emac_lwip_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr)
|
|
||||||
{
|
|
||||||
return etharp_output(netif, q, ipaddr);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void emac_lwip_input(void *data, emac_stack_t *buf)
|
static void emac_lwip_input(void *data, emac_stack_t *buf)
|
||||||
{
|
{
|
||||||
struct eth_hdr *ethhdr;
|
struct eth_hdr *ethhdr;
|
||||||
|
|
@ -81,7 +76,10 @@ err_t emac_lwip_if_init(struct netif *netif)
|
||||||
|
|
||||||
mac->ops.get_ifname(mac, netif->name, 2);
|
mac->ops.get_ifname(mac, netif->name, 2);
|
||||||
|
|
||||||
netif->output = emac_lwip_output;
|
#if LWIP_IPV4
|
||||||
|
netif->output = etharp_output;
|
||||||
|
#endif /* LWIP_IPV4 */
|
||||||
|
|
||||||
netif->linkoutput = emac_lwip_low_level_output;
|
netif->linkoutput = emac_lwip_low_level_output;
|
||||||
|
|
||||||
if (!mac->ops.power_up(mac)) {
|
if (!mac->ops.power_up(mac)) {
|
||||||
|
|
|
||||||
|
|
@ -29,13 +29,17 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if DEVICE_EMAC
|
||||||
|
err_t emac_lwip_if_init(struct netif *netif);
|
||||||
|
|
||||||
|
#else /* DEVICE_EMAC */
|
||||||
void eth_arch_enable_interrupts(void);
|
void eth_arch_enable_interrupts(void);
|
||||||
void eth_arch_disable_interrupts(void);
|
void eth_arch_disable_interrupts(void);
|
||||||
err_t eth_arch_enetif_init(struct netif *netif);
|
err_t eth_arch_enetif_init(struct netif *netif);
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // #ifndef ETHARCHINTERFACE_H_
|
#endif // #ifndef ETHARCHINTERFACE_H_
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,14 @@
|
||||||
|
|
||||||
#include "emac_api.h"
|
#include "emac_api.h"
|
||||||
|
|
||||||
|
#if DEVICE_EMAC
|
||||||
|
#define NETIF_INIT_FN emac_lwip_if_init
|
||||||
|
#else
|
||||||
|
#define NETIF_INIT_FN eth_arch_enetif_init
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define DHCP_TIMEOUT 15000
|
||||||
|
|
||||||
/* Static arena of sockets */
|
/* Static arena of sockets */
|
||||||
static struct lwip_socket {
|
static struct lwip_socket {
|
||||||
bool in_use;
|
bool in_use;
|
||||||
|
|
@ -352,6 +360,47 @@ char *lwip_get_gateway(char *buf, int buflen)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int lwip_start_dhcp(unsigned int timeout)
|
||||||
|
{
|
||||||
|
err_t err = 0;
|
||||||
|
#if LWIP_IPV4
|
||||||
|
err = dhcp_start(&lwip_netif);
|
||||||
|
if (err) {
|
||||||
|
return NSAPI_ERROR_DHCP_FAILURE;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if DEVICE_EMAC
|
||||||
|
// If doesn't have address
|
||||||
|
if (!lwip_get_ip_addr(true, &lwip_netif)) {
|
||||||
|
err = sys_arch_sem_wait(&lwip_netif_has_addr, timeout);
|
||||||
|
if (err == SYS_ARCH_TIMEOUT) {
|
||||||
|
return NSAPI_ERROR_DHCP_FAILURE;
|
||||||
|
}
|
||||||
|
lwip_connected = true;
|
||||||
|
}
|
||||||
|
#endif /* DEVICE_EMAC */
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lwip_start_static_ip(const char *ip, const char *netmask, const char *gw)
|
||||||
|
{
|
||||||
|
|
||||||
|
#if LWIP_IPV4
|
||||||
|
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)) {
|
||||||
|
return NSAPI_ERROR_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw)
|
int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw)
|
||||||
{
|
{
|
||||||
|
|
@ -373,23 +422,14 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *
|
||||||
sys_arch_sem_wait(&lwip_tcpip_inited, 0);
|
sys_arch_sem_wait(&lwip_tcpip_inited, 0);
|
||||||
|
|
||||||
memset(&lwip_netif, 0, sizeof lwip_netif);
|
memset(&lwip_netif, 0, sizeof lwip_netif);
|
||||||
#if DEVICE_EMAC
|
|
||||||
if (!netif_add(&lwip_netif,
|
if (!netif_add(&lwip_netif,
|
||||||
#if LWIP_IPV4
|
#if LWIP_IPV4
|
||||||
0, 0, 0,
|
0, 0, 0,
|
||||||
#endif
|
#endif
|
||||||
emac, emac_lwip_if_init, tcpip_input)) {
|
emac, NETIF_INIT_FN, tcpip_input)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#else /* DEVICE_EMAC */
|
|
||||||
if (!netif_add(&lwip_netif,
|
|
||||||
#if LWIP_IPV4
|
|
||||||
0, 0, 0,
|
|
||||||
#endif
|
|
||||||
NULL, eth_arch_enetif_init, tcpip_input)) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
#endif /* DEVICE_EMAC */
|
|
||||||
netif_set_default(&lwip_netif);
|
netif_set_default(&lwip_netif);
|
||||||
|
|
||||||
netif_set_link_callback (&lwip_netif, lwip_netif_link_irq);
|
netif_set_link_callback (&lwip_netif, lwip_netif_link_irq);
|
||||||
|
|
@ -435,47 +475,26 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if LWIP_IPV4
|
#if !DEVICE_EMAC
|
||||||
if (!dhcp) {
|
if (!dhcp) {
|
||||||
ip4_addr_t ip_addr;
|
lwip_start_static_ip(ip, netmask, gw);
|
||||||
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)) {
|
|
||||||
return NSAPI_ERROR_PARAMETER;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
netif_set_up(&lwip_netif);
|
netif_set_up(&lwip_netif);
|
||||||
|
|
||||||
#if !DEVICE_EMAC
|
|
||||||
#if LWIP_IPV4
|
|
||||||
// Connect to the network
|
|
||||||
lwip_dhcp = dhcp;
|
lwip_dhcp = dhcp;
|
||||||
|
if (dhcp) {
|
||||||
if (lwip_dhcp) {
|
lwip_start_dhcp(DHCP_TIMEOUT);
|
||||||
err_t err = dhcp_start(&lwip_netif);
|
|
||||||
if (err) {
|
|
||||||
return NSAPI_ERROR_DHCP_FAILURE;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// If doesn't have address
|
// If doesn't have address
|
||||||
if (!lwip_get_ip_addr(true, &lwip_netif)) {
|
if (!lwip_get_ip_addr(true, &lwip_netif)) {
|
||||||
//ret = sys_arch_sem_wait(&lwip_netif_has_addr, 15000);
|
ret = sys_arch_sem_wait(&lwip_netif_has_addr, DHCP_TIMEOUT);
|
||||||
ret = sys_arch_sem_wait(&lwip_netif_has_addr, 30000);
|
|
||||||
if (ret == SYS_ARCH_TIMEOUT) {
|
if (ret == SYS_ARCH_TIMEOUT) {
|
||||||
return NSAPI_ERROR_DHCP_FAILURE;
|
return NSAPI_ERROR_DHCP_FAILURE;
|
||||||
}
|
}
|
||||||
lwip_connected = true;
|
lwip_connected = true;
|
||||||
}
|
}
|
||||||
#endif /* DEVICE_EMAC */
|
|
||||||
|
|
||||||
#if ADDR_TIMEOUT
|
#if ADDR_TIMEOUT
|
||||||
// If address is not for preferred stack waits a while to see
|
// If address is not for preferred stack waits a while to see
|
||||||
|
|
@ -485,6 +504,8 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif /* DEVICE_EMAC */
|
||||||
|
|
||||||
#if LWIP_IPV6
|
#if LWIP_IPV6
|
||||||
add_dns_addr(&lwip_netif);
|
add_dns_addr(&lwip_netif);
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -537,46 +558,6 @@ static int lwip_err_remap(err_t err) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int lwip_start_dhcp(unsigned int timeout)
|
|
||||||
{
|
|
||||||
err_t err = NSAPI_ERROR_DNS_FAILURE;
|
|
||||||
#if LWIP_IPV4
|
|
||||||
err = dhcp_start(&lwip_netif);
|
|
||||||
if (err) {
|
|
||||||
return NSAPI_ERROR_DHCP_FAILURE;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// If doesn't have address
|
|
||||||
if (!lwip_get_ip_addr(true, &lwip_netif)) {
|
|
||||||
err = sys_arch_sem_wait(&lwip_netif_has_addr, timeout);
|
|
||||||
if (err == SYS_ARCH_TIMEOUT) {
|
|
||||||
return NSAPI_ERROR_DHCP_FAILURE;
|
|
||||||
}
|
|
||||||
lwip_connected = true;
|
|
||||||
}
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
int lwip_start_static_ip(const char *ip, const char *netmask, const char *gw)
|
|
||||||
{
|
|
||||||
|
|
||||||
#if LWIP_IPV4
|
|
||||||
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)) {
|
|
||||||
return NSAPI_ERROR_PARAMETER;
|
|
||||||
}
|
|
||||||
|
|
||||||
netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* LWIP network stack implementation */
|
/* LWIP network stack implementation */
|
||||||
static int lwip_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version)
|
static int lwip_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue