diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/connectivity/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/connectivity/main.cpp index 199649cabd2..4f1e2c8a32e 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/connectivity/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/connectivity/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #include "mbed.h" diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname/main.cpp index a3d9914dbd8..3c5a8d253e4 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #include "mbed.h" diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo/main.cpp index 6a6750b19ca..a91aca464ca 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #include "mbed.h" diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo_parallel/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo_parallel/main.cpp index 15036a2f4ad..3abd914c7ac 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo_parallel/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo_parallel/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #include "mbed.h" diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_hello_world/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_hello_world/main.cpp index c6ddb33bfa5..a5392e60bca 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_hello_world/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_hello_world/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #include diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_packet_pressure/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_packet_pressure/main.cpp index 23ff33c0a5a..b5956aa7de8 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_packet_pressure/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_packet_pressure/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #ifndef MBED_EXTENDED_TESTS #error [NOT_SUPPORTED] Pressure tests are not supported by default diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_packet_pressure_parallel/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_packet_pressure_parallel/main.cpp index 6be470db777..60789a107d3 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_packet_pressure_parallel/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_packet_pressure_parallel/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #ifndef MBED_EXTENDED_TESTS #error [NOT_SUPPORTED] Parallel pressure tests are not supported by default diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_dtls_handshake/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_dtls_handshake/main.cpp index d2368eebb0c..95b888cf617 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_dtls_handshake/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_dtls_handshake/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #include "mbed.h" diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo/main.cpp index cbd15604fc3..14ad88204d0 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #include "mbed.h" diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel/main.cpp index 3f42d00c3b2..dc1b76f6d7b 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #include "mbed.h" diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_packet_pressure/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_packet_pressure/main.cpp index 762c63391d2..9736d454e2a 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_packet_pressure/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_packet_pressure/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #ifndef MBED_EXTENDED_TESTS #error [NOT_SUPPORTED] Pressure tests are not supported by default diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_packet_pressure_parallel/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_packet_pressure_parallel/main.cpp index 85f7dcb03a3..ef5053397d2 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_packet_pressure_parallel/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_packet_pressure_parallel/main.cpp @@ -1,8 +1,8 @@ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif -#if DEVICE_EMAC - #error [NOT_SUPPORTED] Not supported for WiFi targets +#if !DEVICE_ETH + #error [NOT_SUPPORTED] Ethernet not supported for this target #endif #ifndef MBED_EXTENDED_TESTS #error [NOT_SUPPORTED] Parallel pressure tests are not supported by default diff --git a/features/FEATURE_LWIP/lwip-interface/emac_lwip.c b/features/FEATURE_LWIP/lwip-interface/emac_lwip.c index f79be5d4717..d1ba12325d9 100644 --- a/features/FEATURE_LWIP/lwip-interface/emac_lwip.c +++ b/features/FEATURE_LWIP/lwip-interface/emac_lwip.c @@ -14,8 +14,6 @@ * limitations under the License. */ -#if DEVICE_EMAC - #include "emac_api.h" #include "emac_stack_mem.h" #include "lwip/tcpip.h" @@ -26,12 +24,11 @@ static err_t emac_lwip_low_level_output(struct netif *netif, struct pbuf *p) { emac_interface_t *mac = (emac_interface_t *)netif->state; - bool ret = mac->ops.link_out(mac, (emac_stack_mem_t *)p); - + bool ret = mac->ops->link_out(mac, (emac_stack_mem_chain_t *)p); return ret ? ERR_OK : ERR_IF; } -static void emac_lwip_input(void *data, emac_stack_t *buf) +static void emac_lwip_input(void *data, emac_stack_mem_t *buf) { struct pbuf *p = (struct pbuf *)buf; struct netif *netif = (struct netif *)data; @@ -55,35 +52,128 @@ static void emac_lwip_state_change(void *data, bool up) } } +#if LWIP_IGMP + +#include "lwip/igmp.h" +/** + * IPv4 address filtering setup. + * + * \param[in] netif the lwip network interface structure + * \param[in] group IPv4 group to modify + * \param[in] action + * \return ERR_OK or error code + */ +static err_t igmp_mac_filter(struct netif *netif, const ip4_addr_t *group, u8_t action) +{ + emac_interface_t *emac = netif->state; + if (emac->ops->add_multicast_group == NULL) { + return ERR_OK; /* This function is not mandatory */ + } + + switch (action) { + case NETIF_ADD_MAC_FILTER: + { + uint32_t group23 = ntohl(group->addr) & 0x007FFFFF; + uint8_t addr[6]; + addr[0] = LL_IP4_MULTICAST_ADDR_0; + addr[1] = LL_IP4_MULTICAST_ADDR_1; + addr[2] = LL_IP4_MULTICAST_ADDR_2; + addr[3] = group23 >> 16; + addr[4] = group23 >> 8; + addr[5] = group23; + emac->ops->add_multicast_group(emac, addr); + return ERR_OK; + } + case NETIF_DEL_MAC_FILTER: + /* As we don't reference count, silently ignore delete requests */ + return ERR_OK; + default: + return ERR_ARG; + } +} +#endif + +#if LWIP_IPV6_MLD + +#include "lwip/mld6.h" +/** + * IPv6 address filtering setup. + * + * \param[in] netif the lwip network interface structure + * \param[in] group IPv6 group to modify + * \param[in] action + * \return ERR_OK or error code + */ +static err_t mld_mac_filter(struct netif *netif, const ip6_addr_t *group, u8_t action) +{ + emac_interface_t *emac = netif->state; + if (emac->ops->add_multicast_group == NULL) { + return ERR_OK; /* This function is not mandatory */ + } + + switch (action) { + case NETIF_ADD_MAC_FILTER: + { + uint32_t group32 = ntohl(group->addr[3]); + uint8_t addr[6]; + addr[0] = LL_IP6_MULTICAST_ADDR_0; + addr[1] = LL_IP6_MULTICAST_ADDR_1; + addr[2] = group32 >> 24; + addr[3] = group32 >> 16; + addr[4] = group32 >> 8; + addr[5] = group32; + emac->ops->add_multicast_group(emac, addr); + return ERR_OK; + } + case NETIF_DEL_MAC_FILTER: + /* As we don't reference count, silently ignore delete requests */ + return ERR_OK; + default: + return ERR_ARG; + } +} +#endif + err_t emac_lwip_if_init(struct netif *netif) { int err = ERR_OK; - emac_interface_t *mac = (emac_interface_t *)netif->state; + emac_interface_t *emac = netif->state; - mac->ops.set_link_input_cb(mac, emac_lwip_input, netif); - mac->ops.set_link_state_cb(mac, emac_lwip_state_change, netif); + emac->ops->set_link_input_cb(emac, emac_lwip_input, netif); + emac->ops->set_link_state_cb(emac, emac_lwip_state_change, netif); - netif->hwaddr_len = mac->ops.get_hwaddr_size(mac); - mac->ops.get_hwaddr(mac, netif->hwaddr); - - netif->mtu = mac->ops.get_mtu_size(mac); + netif->hwaddr_len = emac->ops->get_hwaddr_size(emac); + emac->ops->get_hwaddr(emac, netif->hwaddr); + netif->mtu = emac->ops->get_mtu_size(emac); /* Interface capabilities */ - netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP; + netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET; - mac->ops.get_ifname(mac, netif->name, 2); + emac->ops->get_ifname(emac, netif->name, 2); #if LWIP_IPV4 netif->output = etharp_output; +#if LWIP_IGMP + netif->igmp_mac_filter = igmp_mac_filter; + netif->flags |= NETIF_FLAG_IGMP; +#endif /* LWIP_IGMP */ #endif /* LWIP_IPV4 */ +#if LWIP_IPV6 + netif->output_ip6 = ethip6_output; +#if LWIP_IPV6_MLD + netif->mld_mac_filter = mld_mac_filter; + netif->flags |= NETIF_FLAG_MLD6; +#else +// Would need to enable all multicasts here - no API in fsl_enet to do that +#error "IPv6 multicasts won't be received if LWIP_IPV6_MLD is disabled, breaking the system" +#endif +#endif netif->linkoutput = emac_lwip_low_level_output; - if (!mac->ops.power_up(mac)) { + if (!emac->ops->power_up(emac)) { err = ERR_IF; } return err; } - -#endif /* DEVICE_EMAC */ diff --git a/features/FEATURE_LWIP/lwip-interface/eth_arch.h b/features/FEATURE_LWIP/lwip-interface/emac_lwip.h similarity index 77% rename from features/FEATURE_LWIP/lwip-interface/eth_arch.h rename to features/FEATURE_LWIP/lwip-interface/emac_lwip.h index 25bdde38dda..6ec310e3c86 100644 --- a/features/FEATURE_LWIP/lwip-interface/eth_arch.h +++ b/features/FEATURE_LWIP/lwip-interface/emac_lwip.h @@ -1,4 +1,4 @@ -/* EthernetInterface.h */ +/* emac_lwip.h */ /* Copyright (C) 2012 mbed.org, MIT License * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software @@ -17,11 +17,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -// Architecture specific Ethernet interface -// Must be implemented by each target - -#ifndef ETHARCH_H_ -#define ETHARCH_H_ +#ifndef EMAC_LWIP_H_ +#define EMAC_LWIP_H_ #include "lwip/netif.h" @@ -29,17 +26,10 @@ extern "C" { #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_disable_interrupts(void); -err_t eth_arch_enetif_init(struct netif *netif); -#endif - #ifdef __cplusplus } #endif -#endif // #ifndef ETHARCHINTERFACE_H_ +#endif // #ifndef EMAC_LWIP_H_ diff --git a/features/FEATURE_LWIP/lwip-interface/emac_stack_lwip.cpp b/features/FEATURE_LWIP/lwip-interface/emac_stack_mem_lwip.c similarity index 68% rename from features/FEATURE_LWIP/lwip-interface/emac_stack_lwip.cpp rename to features/FEATURE_LWIP/lwip-interface/emac_stack_mem_lwip.c index 42ec7ff82a8..7849dcf71b6 100644 --- a/features/FEATURE_LWIP/lwip-interface/emac_stack_lwip.cpp +++ b/features/FEATURE_LWIP/lwip-interface/emac_stack_mem_lwip.c @@ -14,12 +14,10 @@ * limitations under the License. */ -#if DEVICE_EMAC - #include "emac_stack_mem.h" #include "pbuf.h" -emac_stack_mem_t *emac_stack_mem_alloc(emac_stack_t* stack, uint32_t size, uint32_t align) +emac_stack_mem_t *emac_stack_mem_alloc(uint32_t size, uint32_t align) { struct pbuf *pbuf = pbuf_alloc(PBUF_RAW, size + align, PBUF_RAM); @@ -42,34 +40,34 @@ emac_stack_mem_t *emac_stack_mem_alloc(emac_stack_t* stack, uint32_t size, uint3 return (emac_stack_mem_t*)pbuf; } -void emac_stack_mem_free(emac_stack_t* stack, emac_stack_mem_t *mem) +void emac_stack_mem_free(emac_stack_mem_t *mem) { pbuf_free((struct pbuf*)mem); } -void emac_stack_mem_copy(emac_stack_t* stack, emac_stack_mem_t *to, emac_stack_mem_t *from) +void emac_stack_mem_copy(emac_stack_mem_t *to, emac_stack_mem_t *from) { pbuf_copy((struct pbuf*)to, (struct pbuf*)from); } -void *emac_stack_mem_ptr(emac_stack_t* stack, emac_stack_mem_t *mem) +void *emac_stack_mem_ptr(emac_stack_mem_t *mem) { return ((struct pbuf*)mem)->payload; } -uint32_t emac_stack_mem_len(emac_stack_t* stack, emac_stack_mem_t *mem) +uint32_t emac_stack_mem_len(emac_stack_mem_t *mem) { return ((struct pbuf*)mem)->len; } -void emac_stack_mem_set_len(emac_stack_t* stack, emac_stack_mem_t *mem, uint32_t len) +void emac_stack_mem_set_len(emac_stack_mem_t *mem, uint32_t len) { struct pbuf *pbuf = (struct pbuf*)mem; pbuf->len = len; } -emac_stack_mem_t *emac_stack_mem_chain_dequeue(emac_stack_t* stack, emac_stack_mem_chain_t **chain) +emac_stack_mem_t *emac_stack_mem_chain_dequeue(emac_stack_mem_chain_t **chain) { struct pbuf **list = (struct pbuf**)chain; struct pbuf *head = *list; @@ -78,14 +76,19 @@ emac_stack_mem_t *emac_stack_mem_chain_dequeue(emac_stack_t* stack, emac_stack_m return (emac_stack_mem_t *)head; } -uint32_t emac_stack_mem_chain_len(emac_stack_t* stack, emac_stack_mem_chain_t *chain) +uint32_t emac_stack_mem_chain_len(emac_stack_mem_chain_t *chain) { return ((struct pbuf*)chain)->tot_len; } -void emac_stack_mem_ref(emac_stack_t* stack, emac_stack_mem_t *mem) +void emac_stack_mem_set_chain_len(emac_stack_mem_chain_t *chain, uint32_t len) { - pbuf_ref((struct pbuf*)mem); + struct pbuf *pbuf = (struct pbuf*)chain; + + pbuf->tot_len = len; } -#endif /* DEVICE_EMAC */ +void emac_stack_mem_ref(emac_stack_mem_t *mem) +{ + pbuf_ref((struct pbuf*)mem); +} diff --git a/features/FEATURE_LWIP/lwip-interface/ipstack_lwip.c b/features/FEATURE_LWIP/lwip-interface/ipstack_lwip.c new file mode 100644 index 00000000000..2a903d818d5 --- /dev/null +++ b/features/FEATURE_LWIP/lwip-interface/ipstack_lwip.c @@ -0,0 +1,356 @@ +/* LWIP implementation of NetworkInterfaceAPI + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nsapi.h" +#include "mbed_interface.h" +#include "mbed_assert.h" +#include +#include +#include + +#include "lwip/opt.h" +#include "lwip/api.h" +#include "lwip/inet.h" +#include "lwip/netif.h" +#include "lwip/dhcp.h" +#include "lwip/tcpip.h" +#include "lwip/tcp.h" +#include "lwip/ip.h" +#include "lwip/mld6.h" +#include "lwip/dns.h" +#include "lwip/udp.h" + +#include "emac_api.h" +#include "emac_lwip.h" +#include "mbed_ipstack.h" +#include "lwip_tools.h" + +#ifdef __cplusplus +extern "C" { +#endif + +static bool mbed_lwip_inited = false; +static sys_sem_t lwip_tcpip_inited; + +static void add_dns_addr(struct netif *lwip_netif) +{ + // Do nothing if not brought up + const ip_addr_t *ip_addr = mbed_lwip_get_ip_addr(true, lwip_netif); + if (!ip_addr) { + return; + } + + // Check for existing dns server + for (char numdns = 0; numdns < DNS_MAX_SERVERS; numdns++) { + const ip_addr_t *dns_ip_addr = dns_getserver(numdns); + if (!ip_addr_isany(dns_ip_addr)) { + return; + } + } + +#if LWIP_IPV6 + if (IP_IS_V6(ip_addr)) { + /* 2001:4860:4860::8888 google */ + ip_addr_t ipv6_dns_addr = IPADDR6_INIT( + PP_HTONL(0x20014860UL), + PP_HTONL(0x48600000UL), + PP_HTONL(0x00000000UL), + PP_HTONL(0x00008888UL)); + dns_setserver(0, &ipv6_dns_addr); + } +#endif + +#if LWIP_IPV4 + if (IP_IS_V4(ip_addr)) { + /* 8.8.8.8 google */ + ip_addr_t ipv4_dns_addr = IPADDR4_INIT(0x08080808); + dns_setserver(0, &ipv4_dns_addr); + } +#endif +} + +static void mbed_lwip_tcpip_init_irq(void *eh) +{ + sys_sem_signal(&lwip_tcpip_inited); +} + +static void mbed_lwip_netif_link_irq(struct netif *netif) +{ + if (netif_is_link_up(netif)) { + emac_interface_t *emac = netif->state; + sys_sem_signal(&emac->linked); + } +} + +static void mbed_lwip_netif_status_irq(struct netif *netif) +{ + static bool any_addr = true; + emac_interface_t *emac = netif->state; + + if (netif_is_up(netif)) { + // Indicates that has address + if (any_addr == true && mbed_lwip_get_ip_addr(true, netif)) { + sys_sem_signal(&emac->has_addr); + any_addr = false; + return; + } + + // Indicates that has preferred address + if (mbed_lwip_get_ip_addr(false, netif)) { + sys_sem_signal(&emac->has_addr); + } + } else { + any_addr = true; + } +} + +#if LWIP_IPV6 +static void mbed_lwip_clear_ipv6_addresses(struct netif *netif) +{ + for (u8_t i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { + netif_ip6_addr_set_state(netif, i, IP6_ADDR_INVALID); + } +} +#endif + +char *mbed_ipstack_get_mac_address(emac_interface_t *emac) +{ + return emac->hwaddr; +} + +char *mbed_ipstack_get_ip_address(emac_interface_t *emac, char *buf, nsapi_size_t buflen) +{ + const ip_addr_t *addr = mbed_lwip_get_ip_addr(true, &emac->netif); + if (!addr) { + return NULL; + } +#if LWIP_IPV6 + if (IP_IS_V6(addr)) { + return ip6addr_ntoa_r(ip_2_ip6(addr), buf, buflen); + } +#endif +#if LWIP_IPV4 + if (IP_IS_V4(addr)) { + return ip4addr_ntoa_r(ip_2_ip4(addr), buf, buflen); + } +#endif + return NULL; +} + +char *mbed_ipstack_get_netmask(emac_interface_t *emac, char *buf, nsapi_size_t buflen) +{ +#if LWIP_IPV4 + const ip4_addr_t *addr = netif_ip4_netmask(&emac->netif); + if (!ip4_addr_isany(addr)) { + return ip4addr_ntoa_r(addr, buf, buflen); + } else { + return NULL; + } +#else + return NULL; +#endif +} + +char *mbed_ipstack_get_gateway(emac_interface_t *emac, char *buf, nsapi_size_t buflen) +{ +#if LWIP_IPV4 + const ip4_addr_t *addr = netif_ip4_gw(&emac->netif); + if (!ip4_addr_isany(addr)) { + return ip4addr_ntoa_r(addr, buf, buflen); + } else { + return NULL; + } +#else + return NULL; +#endif +} + +void mbed_ipstack_init(void) +{ + if(mbed_lwip_inited) + return; + + sys_sem_new(&lwip_tcpip_inited, 0); + tcpip_init(mbed_lwip_tcpip_init_irq, NULL); + sys_arch_sem_wait(&lwip_tcpip_inited, 0); + + // Zero out socket set + mbed_lwip_arena_init(); + + mbed_lwip_inited = true; +} + +nsapi_error_t mbed_ipstack_add_netif(emac_interface_t *emac, bool default_if) +{ + emac->connected = false; + emac->dhcp = true; + sys_sem_new(&emac->linked, 0); + sys_sem_new(&emac->has_addr, 0); + memset(&emac->netif, 0, sizeof(emac->netif)); + emac->netif.state = emac; + + mbed_mac_address(emac->hwaddr); + + if (!netif_add(&emac->netif, +#if LWIP_IPV4 + 0, 0, 0, +#endif + emac, emac_lwip_if_init, tcpip_input)) { + return NSAPI_ERROR_DEVICE_ERROR; + } + + if (default_if) + netif_set_default(&emac->netif); + + netif_set_link_callback(&emac->netif, mbed_lwip_netif_link_irq); + netif_set_status_callback(&emac->netif, mbed_lwip_netif_status_irq); + + return NSAPI_ERROR_OK; +} + +nsapi_error_t mbed_ipstack_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw) +{ + // Check if we've already connected + if (emac->connected) { + return NSAPI_ERROR_PARAMETER; + } + + mbed_ipstack_init(); + +#if LWIP_IPV6 + netif_create_ip6_linklocal_address(&emac->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 (emac->netif.mld_mac_filter != NULL) { + ip6_addr_t ip6_allnodes_ll; + ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll); + emac->netif.mld_mac_filter(&emac->netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER); + } +#endif /* LWIP_IPV6_MLD */ + +#if LWIP_IPV6_AUTOCONFIG + /* IPv6 address autoconfiguration not enabled by default */ + emac->netif.ip6_autoconfig_enabled = 1; +#endif /* LWIP_IPV6_AUTOCONFIG */ + +#endif /* LWIP_IPV6 */ + + u32_t ret; + + if (!netif_is_link_up(&emac->netif)) { + ret = sys_arch_sem_wait(&emac->linked, 15000); + + if (ret == SYS_ARCH_TIMEOUT) { + return NSAPI_ERROR_NO_CONNECTION; + } + } + +#if LWIP_IPV4 + if (!dhcp) { + 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(&emac->netif, &ip_addr, &netmask_addr, &gw_addr); + } +#endif + + netif_set_up(&emac->netif); + +#if LWIP_IPV4 + // Connect to the network + emac->dhcp = dhcp; + + if (dhcp) { + err_t err = dhcp_start(&emac->netif); + if (err) { + return NSAPI_ERROR_DHCP_FAILURE; + } + } +#endif + + // If doesn't have address + if (!mbed_lwip_get_ip_addr(true, &emac->netif)) { + ret = sys_arch_sem_wait(&emac->has_addr, 15000); + if (ret == SYS_ARCH_TIMEOUT) { + return NSAPI_ERROR_DHCP_FAILURE; + } + + emac->connected = true; + } + +#if ADDR_TIMEOUT + // 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, &emac->netif)) { + ret = sys_arch_sem_wait(&emac->has_addr, ADDR_TIMEOUT * 1000); + } +#endif + + add_dns_addr(&emac->netif); + + return 0; +} + +nsapi_error_t mbed_ipstack_bringdown(emac_interface_t *emac) +{ + // Check if we've connected + if (!emac->connected) { + return NSAPI_ERROR_PARAMETER; + } + +#if LWIP_IPV4 + // Disconnect from the network + if (emac->dhcp) { + dhcp_release(&emac->netif); + dhcp_stop(&emac->netif); + emac->dhcp = false; + } +#endif + + netif_set_down(&emac->netif); + +#if LWIP_IPV6 + mbed_lwip_clear_ipv6_addresses(&emac->netif); +#endif + + sys_sem_free(&emac->has_addr); + sys_sem_new(&emac->has_addr, 0); + emac->connected = false; + return 0; +} + +extern const nsapi_stack_api_t lwip_stack_api; /* Defined in nsapi_stack_lwip.c */ + +void mbed_ipstack_set_stack(emac_interface_t *emac, nsapi_stack_t *stack) +{ + stack->emac = emac; + stack->stack_api = &lwip_stack_api; +} + +#ifdef __cplusplus +} +#endif diff --git a/features/FEATURE_LWIP/lwip-interface/lwip-eth/arch/TARGET_Freescale/k64f_emac.c b/features/FEATURE_LWIP/lwip-interface/lwip-eth/arch/TARGET_Freescale/k64f_emac.c index 3afa22d1a6b..61ac4e094cb 100644 --- a/features/FEATURE_LWIP/lwip-interface/lwip-eth/arch/TARGET_Freescale/k64f_emac.c +++ b/features/FEATURE_LWIP/lwip-interface/lwip-eth/arch/TARGET_Freescale/k64f_emac.c @@ -1,28 +1,19 @@ -#include "lwip/opt.h" -#include "lwip/sys.h" -#include "lwip/def.h" -#include "lwip/mem.h" -#include "lwip/pbuf.h" -#include "lwip/stats.h" -#include "lwip/snmp.h" -#include "lwip/tcpip.h" -#include "lwip/ethip6.h" -#include "lwip/igmp.h" -#include "lwip/mld6.h" -#include "netif/etharp.h" -#include "netif/ppp/pppoe.h" - -#include "eth_arch.h" -#include "sys_arch.h" - -#include "fsl_phy.h" -#include "k64f_emac_config.h" #include #include #include #include +#include "cmsis_os.h" +#include "fsl_phy.h" + +#include "k64f_emac_config.h" + #include "mbed_interface.h" +#include "emac_api.h" +#include "emac_stack_mem.h" +#include "mbed_assert.h" +#include "mbed_error.h" +#include "nsapi_types.h" enet_handle_t g_handle; // TX Buffer descriptors @@ -30,12 +21,15 @@ uint8_t *tx_desc_start_addr; // RX Buffer descriptors uint8_t *rx_desc_start_addr; // RX packet buffer pointers -struct pbuf *rx_buff[ENET_RX_RING_LEN]; +emac_stack_mem_t *rx_buff[ENET_RX_RING_LEN]; // TX packet buffer pointers -struct pbuf *tx_buff[ENET_RX_RING_LEN]; +emac_stack_mem_t *tx_buff[ENET_RX_RING_LEN]; // RX packet payload pointers uint32_t *rx_ptr[ENET_RX_RING_LEN]; +#define K64_ETH_MTU_SIZE 1500 +#define K64_ETH_IF_NAME "en" + /******************************************************************************** * Internal data ********************************************************************************/ @@ -51,12 +45,36 @@ extern void k66f_init_eth_hardware(void); /* K64F EMAC driver data structure */ struct k64f_enetdata { - struct netif *netif; /**< Reference back to LWIP parent netif */ - sys_sem_t RxReadySem; /**< RX packet ready semaphore */ - sys_sem_t TxCleanSem; /**< TX cleanup thread wakeup semaphore */ - sys_mutex_t TXLockMutex; /**< TX critical section mutex */ - sys_sem_t xTXDCountSem; /**< TX free buffer counting semaphore */ + osSemaphoreId RxReadySem; /**< RX packet ready semaphore */ + osSemaphoreDef_t RxReadySem_def; /**< RX semaphore: definition struct */ + uint32_t RxReadySem_data[2]; /**< RX semaphore: object mem */ + + osSemaphoreId TxCleanSem; /**< TX cleanup thread wakeup semaphore */ + osSemaphoreDef_t TxCleanSem_def; /**< TX semaphore: definition struct */ + uint32_t TxCleanSem_data[2]; /**< TX semaphore: object mem */ + + osMutexId TXLockMutex; /**< TX critical section mutex */ + osMutexDef_t TXLockMutex_def; /**< TX mutex: definition struct */ + int32_t TXLockMutex_data[4]; /**< TX mutex: object mem */ + + osSemaphoreId xTXDCountSem; /**< TX free buffer counting semaphore */ + osSemaphoreDef_t xTXDCountSem_def; /**< TX free buffer semaphore: definition struct */ + uint32_t xTXDCountSem_data[2]; /**< TX free buffer semaphore: object mem */ + + osThreadId RxThread; /**< Packet reception thread */ + osThreadDef_t RxThread_def; /**< Packet reception thread: definition struct */ + + osThreadId TxCleanThread; /**< Packet transmission cleanup thread */ + osThreadDef_t TxCleanThread_def; /**< Packet transmission cleanup thread: definition struct */ + + osThreadId PhyThread; /**< PHY monitoring thread */ + osThreadDef_t PhyThread_def; /**< PHY monitoring thread: definition struct */ + uint8_t tx_consume_index, tx_produce_index; /**< TX buffers ring */ + emac_link_input_fn emac_link_input_cb; /**< Callback for incoming data */ + void *emac_link_input_cb_data; /**< Data to be passed to input cb */ + emac_link_state_change_fn emac_link_state_cb; /**< Link state change callback */ + void *emac_link_state_cb_data; /**< Data to be passed to link state cb */ }; static struct k64f_enetdata k64f_enetdata; @@ -102,66 +120,67 @@ static void update_read_buffer(uint8_t *buf) /** \brief Free TX buffers that are complete * - * \param[in] k64f_enet Pointer to driver data structure + * \param[in] k64f Pointer to driver data structure */ -static void k64f_tx_reclaim(struct k64f_enetdata *k64f_enet) +static void k64f_tx_reclaim(struct k64f_enetdata *enet) { /* Get exclusive access */ - sys_mutex_lock(&k64f_enet->TXLockMutex); + osMutexWait(enet->TXLockMutex, osWaitForever); // Traverse all descriptors, looking for the ones modified by the uDMA - while((k64f_enet->tx_consume_index != k64f_enet->tx_produce_index) && - (!(g_handle.txBdDirty->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK))) { - pbuf_free(tx_buff[k64f_enet->tx_consume_index % ENET_TX_RING_LEN]); - if (g_handle.txBdDirty->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK) - g_handle.txBdDirty = g_handle.txBdBase; - else - g_handle.txBdDirty++; - - k64f_enet->tx_consume_index += 1; - osSemaphoreRelease(k64f_enet->xTXDCountSem.id); + while((enet->tx_consume_index != enet->tx_produce_index) && + (!(g_handle.txBdDirty->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK))) { + emac_stack_mem_free(tx_buff[enet->tx_consume_index % ENET_TX_RING_LEN]); + if (g_handle.txBdDirty->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK) + g_handle.txBdDirty = g_handle.txBdBase; + else + g_handle.txBdDirty++; + + enet->tx_consume_index += 1; + osSemaphoreRelease(enet->xTXDCountSem); } /* Restore access */ - sys_mutex_unlock(&k64f_enet->TXLockMutex); + osMutexRelease(enet->TXLockMutex); } /** \brief Ethernet receive interrupt handler * * This function handles the receive interrupt of K64F. */ -void enet_mac_rx_isr() +void enet_mac_rx_isr(struct k64f_enetdata *enet) { - sys_sem_signal(&k64f_enetdata.RxReadySem); + osSemaphoreRelease(enet->RxReadySem); } -void enet_mac_tx_isr() +void enet_mac_tx_isr(struct k64f_enetdata *enet) { - sys_sem_signal(&k64f_enetdata.TxCleanSem); + osSemaphoreRelease(enet->TxCleanSem); } void ethernet_callback(ENET_Type *base, enet_handle_t *handle, enet_event_t event, void *param) { - switch (event) - { - case kENET_RxEvent: - enet_mac_rx_isr(); - break; - case kENET_TxEvent: - enet_mac_tx_isr(); - break; - default: - break; - } + struct k64f_enetdata *enet = param; + switch (event) + { + case kENET_RxEvent: + enet_mac_rx_isr(enet); + break; + case kENET_TxEvent: + enet_mac_tx_isr(enet); + break; + default: + break; + } } /** \brief Low level init of the MAC and PHY. * - * \param[in] netif Pointer to LWIP netif structure + * \param[in] enet Pointer to K64F enet structure + * \param[in] hwaddr MAC address */ -static err_t low_level_init(struct netif *netif) +static err_t low_level_init(struct k64f_enetdata *enet, char *hwaddr) { - struct k64f_enetdata *k64f_enet = netif->state; uint8_t i; uint32_t sysClock; phy_speed_t phy_speed; @@ -169,6 +188,7 @@ static err_t low_level_init(struct netif *netif) uint32_t phyAddr = 0; bool link = false; enet_config_t config; + void *payload; // Allocate RX descriptors rx_desc_start_addr = (uint8_t *)calloc(1, sizeof(enet_rx_bd_struct_t) * ENET_RX_RING_LEN + ENET_BUFF_ALIGNMENT); @@ -185,7 +205,7 @@ static err_t low_level_init(struct netif *netif) /* Create buffers for each receive BD */ for (i = 0; i < ENET_RX_RING_LEN; i++) { - rx_buff[i] = pbuf_alloc(PBUF_RAW, ENET_ETH_MAX_FLEN + ENET_BUFF_ALIGNMENT, PBUF_RAM); + rx_buff[i] = emac_stack_mem_alloc(ENET_ETH_MAX_FLEN, ENET_BUFF_ALIGNMENT); if (NULL == rx_buff[i]) return ERR_MEM; @@ -193,11 +213,12 @@ static err_t low_level_init(struct netif *netif) RX descriptors (16 bytes alignment). However, by doing so, we're effectively changing a data structure which is internal to lwIP. This might not prove to be a good idea in the long run, but a better fix would probably involve modifying lwIP itself */ - rx_buff[i]->payload = (void*)ENET_ALIGN((uint32_t)rx_buff[i]->payload, ENET_BUFF_ALIGNMENT); - rx_ptr[i] = rx_buff[i]->payload; + payload = emac_stack_mem_ptr(rx_buff[i]); + payload = (void*)ENET_ALIGN((uint32_t)payload, ENET_BUFF_ALIGNMENT); + rx_ptr[i] = payload; } - k64f_enet->tx_consume_index = k64f_enet->tx_produce_index = 0; + enet->tx_consume_index = enet->tx_produce_index = 0; /* prepare the buffer configuration. */ enet_buffer_config_t buffCfg = { @@ -237,143 +258,32 @@ static err_t low_level_init(struct netif *netif) config.macSpecialConfig = kENET_ControlFlowControlEnable; config.txAccelerConfig = kENET_TxAccelIsShift16Enabled; config.rxAccelerConfig = kENET_RxAccelisShift16Enabled | kENET_RxAccelMacCheckEnabled; - ENET_Init(ENET, &g_handle, &config, &buffCfg, netif->hwaddr, sysClock); - ENET_SetCallback(&g_handle, ethernet_callback, netif); + ENET_Init(ENET, &g_handle, &config, &buffCfg, (uint8_t*)hwaddr, sysClock); + ENET_SetCallback(&g_handle, ethernet_callback, enet); ENET_ActiveRead(ENET); return ERR_OK; } - -/** - * This function is the ipv4 ethernet packet send function. It calls - * etharp_output after checking link status. - * - * \param[in] netif the lwip network interface structure for this enetif - * \param[in] q Pointer to pbug to send - * \param[in] ipaddr IP address - * \return ERR_OK or error code - */ -#if LWIP_IPV4 -err_t k64f_etharp_output_ipv4(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr) -{ - /* Only send packet is link is up */ - if (netif->flags & NETIF_FLAG_LINK_UP) { - return etharp_output(netif, q, ipaddr); - } - - return ERR_CONN; -} -#endif - -/** - * This function is the ipv6 ethernet packet send function. It calls - * ethip6_output after checking link status. - * - * \param[in] netif the lwip network interface structure for this enetif - * \param[in] q Pointer to pbug to send - * \param[in] ipaddr IP address - * \return ERR_OK or error code - */ -#if LWIP_IPV6 -err_t k64f_etharp_output_ipv6(struct netif *netif, struct pbuf *q, const ip6_addr_t *ipaddr) -{ - /* Only send packet is link is up */ - if (netif->flags & NETIF_FLAG_LINK_UP) { - return ethip6_output(netif, q, ipaddr); - } - - return ERR_CONN; -} -#endif - -#if LWIP_IGMP -/** - * IPv4 address filtering setup. - * - * \param[in] netif the lwip network interface structure for this enetif - * \param[in] group IPv4 group to modify - * \param[in] action - * \return ERR_OK or error code - */ -err_t igmp_mac_filter(struct netif *netif, const ip4_addr_t *group, enum netif_mac_filter_action action) -{ - switch (action) { - case NETIF_ADD_MAC_FILTER: - { - uint32_t group23 = ntohl(group->addr) & 0x007FFFFF; - uint8_t addr[6]; - addr[0] = LL_IP4_MULTICAST_ADDR_0; - addr[1] = LL_IP4_MULTICAST_ADDR_1; - addr[2] = LL_IP4_MULTICAST_ADDR_2; - addr[3] = group23 >> 16; - addr[4] = group23 >> 8; - addr[5] = group23; - ENET_AddMulticastGroup(ENET, addr); - return ERR_OK; - } - case NETIF_DEL_MAC_FILTER: - /* As we don't reference count, silently ignore delete requests */ - return ERR_OK; - default: - return ERR_ARG; - } -} -#endif - -#if LWIP_IPV6_MLD -/** - * IPv6 address filtering setup. - * - * \param[in] netif the lwip network interface structure for this enetif - * \param[in] group IPv6 group to modify - * \param[in] action - * \return ERR_OK or error code - */ -err_t mld_mac_filter(struct netif *netif, const ip6_addr_t *group, enum netif_mac_filter_action action) -{ - switch (action) { - case NETIF_ADD_MAC_FILTER: - { - uint32_t group32 = ntohl(group->addr[3]); - uint8_t addr[6]; - addr[0] = LL_IP6_MULTICAST_ADDR_0; - addr[1] = LL_IP6_MULTICAST_ADDR_1; - addr[2] = group32 >> 24; - addr[3] = group32 >> 16; - addr[4] = group32 >> 8; - addr[5] = group32; - ENET_AddMulticastGroup(ENET, addr); - return ERR_OK; - } - case NETIF_DEL_MAC_FILTER: - /* As we don't reference count, silently ignore delete requests */ - return ERR_OK; - default: - return ERR_ARG; - } -} -#endif - -/** \brief Allocates a pbuf and returns the data from the incoming packet. +/** \brief Allocates a emac_stack_mem_t and returns the data from the incoming packet. * - * \param[in] netif the lwip network interface structure + * \param[in] enet Pointer to K64F enet structure * \param[in] idx index of packet to be read - * \return a pbuf filled with the received packet (including MAC header) + * \return a emac_stack_mem_t filled with the received packet (including MAC header) */ -static struct pbuf *k64f_low_level_input(struct netif *netif, int idx) +static emac_stack_mem_t *k64f_low_level_input(struct k64f_enetdata *enet, int idx) { volatile enet_rx_bd_struct_t *bdPtr = g_handle.rxBdCurrent; - struct pbuf *p = NULL; - struct pbuf *temp_rxbuf = NULL; + emac_stack_mem_t *p = NULL; + emac_stack_mem_t *temp_rxbuf = NULL; u32_t length = 0; const u16_t err_mask = ENET_BUFFDESCRIPTOR_RX_TRUNC_MASK | ENET_BUFFDESCRIPTOR_RX_CRC_MASK | ENET_BUFFDESCRIPTOR_RX_NOOCTET_MASK | ENET_BUFFDESCRIPTOR_RX_LENVLIOLATE_MASK; - +void *payload; #ifdef LOCK_RX_THREAD /* Get exclusive access */ - sys_mutex_lock(&k64f_enet->TXLockMutex); + osMutexWait(enet->TXLockMutex, osWaitForever); #endif /* Determine if a frame has been received */ @@ -393,10 +303,10 @@ static struct pbuf *k64f_low_level_input(struct netif *netif, int idx) /* Zero-copy */ p = rx_buff[idx]; - p->len = length; + emac_stack_mem_set_len(p, length); /* Attempt to queue new buffer */ - temp_rxbuf = pbuf_alloc(PBUF_RAW, ENET_ETH_MAX_FLEN + ENET_BUFF_ALIGNMENT, PBUF_RAM); + temp_rxbuf = emac_stack_mem_alloc(ENET_ETH_MAX_FLEN, ENET_BUFF_ALIGNMENT); if (NULL == temp_rxbuf) { /* Drop frame (out of memory) */ LINK_STATS_INC(link.drop); @@ -408,7 +318,7 @@ static struct pbuf *k64f_low_level_input(struct netif *netif, int idx) ("k64f_low_level_input: Packet index %d dropped for OOM\n", idx)); #ifdef LOCK_RX_THREAD - sys_mutex_unlock(&k64f_enet->TXLockMutex); + osMutexRelease(enet->TXLockMutex); #endif return NULL; @@ -419,21 +329,22 @@ static struct pbuf *k64f_low_level_input(struct netif *netif, int idx) RX descriptors (16 bytes alignment). However, by doing so, we're effectively changing a data structure which is internal to lwIP. This might not prove to be a good idea in the long run, but a better fix would probably involve modifying lwIP itself */ - rx_buff[idx]->payload = (void*)ENET_ALIGN((uint32_t)rx_buff[idx]->payload, ENET_BUFF_ALIGNMENT); - rx_ptr[idx] = rx_buff[idx]->payload; + payload = emac_stack_mem_ptr(rx_buff[idx]); + payload = (void*)ENET_ALIGN((uint32_t)payload, ENET_BUFF_ALIGNMENT); + rx_ptr[idx] = payload; - update_read_buffer(rx_buff[idx]->payload); + update_read_buffer(payload); LWIP_DEBUGF(UDP_LPC_EMAC | LWIP_DBG_TRACE, ("k64f_low_level_input: Packet received: %p, size %"PRIu32" (index=%d)\n", p, length, idx)); /* Save size */ - p->tot_len = (u16_t) length; + emac_stack_mem_set_chain_len(p, length); LINK_STATS_INC(link.recv); } #ifdef LOCK_RX_THREAD - sys_mutex_unlock(&k64f_enet->TXLockMutex); + osMutexRelease(enet->TXLockMutex); #endif return p; @@ -441,24 +352,18 @@ static struct pbuf *k64f_low_level_input(struct netif *netif, int idx) /** \brief Attempt to read a packet from the EMAC interface. * - * \param[in] netif the lwip network interface structure * \param[in] idx index of packet to be read */ -void k64f_enetif_input(struct netif *netif, int idx) +void k64f_enetif_input(struct k64f_enetdata *enet, int idx) { - struct pbuf *p; + emac_stack_mem_t *p; - /* move received packet into a new pbuf */ - p = k64f_low_level_input(netif, idx); + /* move received packet into a new buf */ + p = k64f_low_level_input(enet, idx); if (p == NULL) return; - /* pass all packets to ethernet_input, which decides what packets it supports */ - if (netif->input(p, netif) != ERR_OK) { - LWIP_DEBUGF(NETIF_DEBUG, ("k64f_enetif_input: input error\n")); - /* Free buffer */ - pbuf_free(p); - } + enet->emac_link_input_cb(enet->emac_link_input_cb_data, p); } /** \brief Packet reception task @@ -469,15 +374,15 @@ void k64f_enetif_input(struct netif *netif, int idx) * \param[in] pvParameters pointer to the interface data */ static void packet_rx(void* pvParameters) { - struct k64f_enetdata *k64f_enet = pvParameters; + struct k64f_enetdata *enet = pvParameters; int idx = 0; while (1) { /* Wait for receive task to wakeup */ - sys_arch_sem_wait(&k64f_enet->RxReadySem, 0); + osSemaphoreWait(enet->RxReadySem, osWaitForever); while ((g_handle.rxBdCurrent->control & ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK) == 0) { - k64f_enetif_input(k64f_enet->netif, idx); + k64f_enetif_input(enet, idx); idx = (idx + 1) % ENET_RX_RING_LEN; } } @@ -486,18 +391,18 @@ static void packet_rx(void* pvParameters) { /** \brief Transmit cleanup task * * This task is called when a transmit interrupt occurs and - * reclaims the pbuf and descriptor used for the packet once + * reclaims the buffer and descriptor used for the packet once * the packet has been transferred. * * \param[in] pvParameters pointer to the interface data */ static void packet_tx(void* pvParameters) { - struct k64f_enetdata *k64f_enet = pvParameters; + struct k64f_enetdata *enet = pvParameters; while (1) { /* Wait for transmit cleanup task to wakeup */ - sys_arch_sem_wait(&k64f_enet->TxCleanSem, 0); - k64f_tx_reclaim(k64f_enet); + osSemaphoreWait(enet->TxCleanSem, osWaitForever); + k64f_tx_reclaim(enet); } } @@ -505,47 +410,48 @@ static void packet_tx(void* pvParameters) { * interrupt context, as it may block until TX descriptors * become available. * - * \param[in] netif the lwip network interface structure for this netif - * \param[in] p the MAC packet to send (e.g. IP packet including MAC addresses and type) + * \param[in] emac Emac driver for the network interface + * \param[in] buf the MAC packet to send (e.g. IP packet including MAC addresses and type) * \return ERR_OK if the packet could be sent or an err_t value if the packet couldn't be sent */ -static err_t k64f_low_level_output(struct netif *netif, struct pbuf *p) +static bool k64f_eth_link_out(emac_interface_t *emac, emac_stack_mem_chain_t *chain) { - struct k64f_enetdata *k64f_enet = netif->state; - struct pbuf *q; - struct pbuf *temp_pbuf; + struct k64f_enetdata *enet = emac->hw; + emac_stack_mem_t *q; + emac_stack_mem_t *temp_buf; uint8_t *psend = NULL, *dst; - temp_pbuf = pbuf_alloc(PBUF_RAW, p->tot_len + ENET_BUFF_ALIGNMENT, PBUF_RAM); - if (NULL == temp_pbuf) - return ERR_MEM; + temp_buf = emac_stack_mem_alloc(emac_stack_mem_chain_len(chain), ENET_BUFF_ALIGNMENT); + if (NULL == temp_buf) + return false; /* K64F note: the next line ensures that the RX buffer is properly aligned for the K64F RX descriptors (16 bytes alignment). However, by doing so, we're effectively changing a data structure which is internal to lwIP. This might not prove to be a good idea in the long run, but a better fix would probably involve modifying lwIP itself */ - psend = (uint8_t *)ENET_ALIGN((uint32_t)temp_pbuf->payload, ENET_BUFF_ALIGNMENT); + psend = (uint8_t *)ENET_ALIGN((uint32_t)emac_stack_mem_ptr(temp_buf), ENET_BUFF_ALIGNMENT); - for (q = p, dst = psend; q != NULL; q = q->next) { - MEMCPY(dst, q->payload, q->len); - dst += q->len; + for (q = emac_stack_mem_chain_dequeue(&chain), dst = psend; q != NULL; q = emac_stack_mem_chain_dequeue(&chain)) { + memcpy(dst, emac_stack_mem_ptr(q), emac_stack_mem_len(q)); + dst += emac_stack_mem_len(q); } /* Check if a descriptor is available for the transfer. */ - int32_t count = osSemaphoreWait(k64f_enet->xTXDCountSem.id, 0); + int32_t count = osSemaphoreWait(enet->xTXDCountSem, 0); if (count < 1) - return ERR_BUF; + return false; /* Get exclusive access */ - sys_mutex_lock(&k64f_enet->TXLockMutex); + osMutexWait(enet->TXLockMutex, osWaitForever); /* Save the buffer so that it can be freed when transmit is done */ - tx_buff[k64f_enet->tx_produce_index % ENET_TX_RING_LEN] = temp_pbuf; - k64f_enet->tx_produce_index += 1; + tx_buff[enet->tx_produce_index % ENET_TX_RING_LEN] = temp_buf; + enet->tx_produce_index += 1; /* Setup transfers */ g_handle.txBdCurrent->buffer = psend; - g_handle.txBdCurrent->length = p->tot_len; + g_handle.txBdCurrent->length = emac_stack_mem_len(temp_buf); + g_handle.txBdCurrent->control |= (ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK); /* Increase the buffer descriptor address. */ @@ -560,9 +466,9 @@ static err_t k64f_low_level_output(struct netif *netif, struct pbuf *p) LINK_STATS_INC(link.xmit); /* Restore access */ - sys_mutex_unlock(&k64f_enet->TXLockMutex); + osMutexRelease(enet->TXLockMutex); - return ERR_OK; + return true; } /******************************************************************************* @@ -587,7 +493,7 @@ int phy_link_status() { } static void k64f_phy_task(void *data) { - struct netif *netif = (struct netif*)data; + struct k64f_enetdata *enet = data; bool connection_status; PHY_STATE crt_state = {STATE_UNKNOWN, (phy_speed_t)STATE_UNKNOWN, (phy_duplex_t)STATE_UNKNOWN}; PHY_STATE prev_state; @@ -604,10 +510,7 @@ static void k64f_phy_task(void *data) { // Compare with previous state if (crt_state.connected != prev_state.connected) { - if (crt_state.connected) - tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, (void*) netif, 1); - else - tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, (void*) netif, 1); + enet->emac_link_state_cb(enet->emac_link_state_cb_data, crt_state.connected); } if (crt_state.speed != prev_state.speed) { @@ -622,125 +525,150 @@ static void k64f_phy_task(void *data) { } } -/** - * Should be called at the beginning of the program to set up the - * network interface. - * - * This function should be passed as a parameter to netif_add(). - * - * @param[in] netif the lwip network interface structure for this netif - * @return ERR_OK if the loopif is initialized - * ERR_MEM if private data couldn't be allocated - * any other err_t on error - */ -err_t eth_arch_enetif_init(struct netif *netif) +static bool k64f_eth_power_up(emac_interface_t *emac) { err_t err; - LWIP_ASSERT("netif != NULL", (netif != NULL)); - - k64f_enetdata.netif = netif; - - /* set MAC hardware address */ -#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE) - netif->hwaddr[0] = MBED_MAC_ADDR_0; - netif->hwaddr[1] = MBED_MAC_ADDR_1; - netif->hwaddr[2] = MBED_MAC_ADDR_2; - netif->hwaddr[3] = MBED_MAC_ADDR_3; - netif->hwaddr[4] = MBED_MAC_ADDR_4; - netif->hwaddr[5] = MBED_MAC_ADDR_5; -#else - mbed_mac_address((char *)netif->hwaddr); -#endif - - /* Ethernet address length */ - netif->hwaddr_len = ETH_HWADDR_LEN; - - /* maximum transfer unit */ - netif->mtu = 1500; - - /* device capabilities */ - // TODOETH: check if the flags are correct below - netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET; - /* Initialize the hardware */ - netif->state = &k64f_enetdata; - err = low_level_init(netif); + err = low_level_init(&k64f_enetdata, emac->hwaddr); if (err != ERR_OK) - return err; - -#if LWIP_NETIF_HOSTNAME - /* Initialize interface hostname */ - netif->hostname = "lwipk64f"; -#endif /* LWIP_NETIF_HOSTNAME */ - - netif->name[0] = 'e'; - netif->name[1] = 'n'; - -#if LWIP_IPV4 - netif->output = k64f_etharp_output_ipv4; -#if LWIP_IGMP - netif->igmp_mac_filter = igmp_mac_filter; - netif->flags |= NETIF_FLAG_IGMP; -#endif -#endif -#if LWIP_IPV6 - netif->output_ip6 = k64f_etharp_output_ipv6; -#if LWIP_IPV6_MLD - netif->mld_mac_filter = mld_mac_filter; - netif->flags |= NETIF_FLAG_MLD6; -#else - // Would need to enable all multicasts here - no API in fsl_enet to do that - #error "IPv6 multicasts won't be received if LWIP_IPV6_MLD is disabled, breaking the system" -#endif -#endif - netif->linkoutput = k64f_low_level_output; + return false; /* CMSIS-RTOS, start tasks */ -#ifdef CMSIS_OS_RTX - memset(k64f_enetdata.xTXDCountSem.data, 0, sizeof(k64f_enetdata.xTXDCountSem.data)); - k64f_enetdata.xTXDCountSem.def.semaphore = k64f_enetdata.xTXDCountSem.data; -#endif - k64f_enetdata.xTXDCountSem.id = osSemaphoreCreate(&k64f_enetdata.xTXDCountSem.def, ENET_TX_RING_LEN); + memset(k64f_enetdata.xTXDCountSem_data, 0, sizeof(k64f_enetdata.xTXDCountSem_data)); + k64f_enetdata.xTXDCountSem_def.semaphore = k64f_enetdata.xTXDCountSem_data; + k64f_enetdata.xTXDCountSem = osSemaphoreCreate(&k64f_enetdata.xTXDCountSem_def, ENET_TX_RING_LEN); + MBED_ASSERT(k64f_enetdata.xTXDCountSem != NULL); - LWIP_ASSERT("xTXDCountSem creation error", (k64f_enetdata.xTXDCountSem.id != NULL)); - - err = sys_mutex_new(&k64f_enetdata.TXLockMutex); - LWIP_ASSERT("TXLockMutex creation error", (err == ERR_OK)); + /* Transmission lock mutex */ + memset(k64f_enetdata.TXLockMutex_data, 0, sizeof(k64f_enetdata.TXLockMutex_data)); + k64f_enetdata.TXLockMutex_def.mutex = k64f_enetdata.TXLockMutex_data; + k64f_enetdata.TXLockMutex = osMutexCreate(&k64f_enetdata.TXLockMutex_def); + MBED_ASSERT(k64f_enetdata.TXLockMutex != NULL); /* Packet receive task */ - err = sys_sem_new(&k64f_enetdata.RxReadySem, 0); - LWIP_ASSERT("RxReadySem creation error", (err == ERR_OK)); - + memset(k64f_enetdata.RxReadySem_data, 0, sizeof(k64f_enetdata.RxReadySem_data)); + k64f_enetdata.RxReadySem_def.semaphore = k64f_enetdata.RxReadySem_data; + k64f_enetdata.RxReadySem = osSemaphoreCreate(&k64f_enetdata.RxReadySem_def, 0); + MBED_ASSERT(k64f_enetdata.RxReadySem != NULL); + + /* Packet reception thread */ + k64f_enetdata.RxThread_def.pthread = (os_pthread)packet_rx; + k64f_enetdata.RxThread_def.tpriority = RX_PRIORITY; #ifdef LWIP_DEBUG - sys_thread_new("receive_thread", packet_rx, netif->state, DEFAULT_THREAD_STACKSIZE*5, RX_PRIORITY); + k64f_enetdata.RxThread_def.stacksize = DEFAULT_THREAD_STACKSIZE*5; #else - sys_thread_new("receive_thread", packet_rx, netif->state, DEFAULT_THREAD_STACKSIZE, RX_PRIORITY); + k64f_enetdata.RxThread_def.stacksize = DEFAULT_THREAD_STACKSIZE; #endif + k64f_enetdata.RxThread_def.stack_pointer = (uint32_t*)malloc(k64f_enetdata.RxThread_def.stacksize); + if (k64f_enetdata.RxThread_def.stack_pointer == NULL) + error("RxThread: Error allocating the stack memory"); + k64f_enetdata.RxThread = osThreadCreate(&k64f_enetdata.RxThread_def, &k64f_enetdata); + if (k64f_enetdata.RxThread == NULL) + error("RxThread: create error\n"); /* Transmit cleanup task */ - err = sys_sem_new(&k64f_enetdata.TxCleanSem, 0); - LWIP_ASSERT("TxCleanSem creation error", (err == ERR_OK)); - sys_thread_new("txclean_thread", packet_tx, netif->state, DEFAULT_THREAD_STACKSIZE, TX_PRIORITY); + memset(k64f_enetdata.TxCleanSem_data, 0, sizeof(k64f_enetdata.TxCleanSem_data)); + k64f_enetdata.TxCleanSem_def.semaphore = k64f_enetdata.TxCleanSem_data; + k64f_enetdata.TxCleanSem = osSemaphoreCreate(&k64f_enetdata.TxCleanSem_def, 0); + MBED_ASSERT(k64f_enetdata.TxCleanSem != NULL); + + /* Transmission cleanup thread */ + k64f_enetdata.TxCleanThread_def.pthread = (os_pthread)packet_tx; + k64f_enetdata.TxCleanThread_def.tpriority = TX_PRIORITY; + k64f_enetdata.TxCleanThread_def.stacksize = DEFAULT_THREAD_STACKSIZE; + k64f_enetdata.TxCleanThread_def.stack_pointer = (uint32_t*)malloc(k64f_enetdata.TxCleanThread_def.stacksize); + if (k64f_enetdata.TxCleanThread_def.stack_pointer == NULL) + error("TxCleanThread: Error allocating the stack memory"); + k64f_enetdata.TxCleanThread = osThreadCreate(&k64f_enetdata.TxCleanThread_def, &k64f_enetdata); + if (k64f_enetdata.TxCleanThread == NULL) + error("TxCleanThread: create error\n"); /* PHY monitoring task */ - sys_thread_new("phy_thread", k64f_phy_task, netif, DEFAULT_THREAD_STACKSIZE, PHY_PRIORITY); + k64f_enetdata.PhyThread_def.pthread = (os_pthread)k64f_phy_task; + k64f_enetdata.PhyThread_def.tpriority = PHY_PRIORITY; + k64f_enetdata.PhyThread_def.stacksize = DEFAULT_THREAD_STACKSIZE; + k64f_enetdata.PhyThread_def.stack_pointer = (uint32_t*)malloc(k64f_enetdata.PhyThread_def.stacksize); + if (k64f_enetdata.PhyThread_def.stack_pointer == NULL) + error("PhyThread: Error allocating the stack memory"); + k64f_enetdata.PhyThread = osThreadCreate(&k64f_enetdata.PhyThread_def, &k64f_enetdata); + if (k64f_enetdata.PhyThread == NULL) + error("PhyThread: create error\n"); /* Allow the PHY task to detect the initial link state and set up the proper flags */ osDelay(10); - return ERR_OK; + return true; } -void eth_arch_enable_interrupts(void) { - //NVIC_SetPriority(ENET_Receive_IRQn, 6U); - //NVIC_SetPriority(ENET_Transmit_IRQn, 6U); +static uint32_t k64f_eth_get_mtu_size(emac_interface_t *emac) +{ + return K64_ETH_MTU_SIZE; +} + +static void k64f_eth_get_ifname(emac_interface_t *emac, char *name, uint8_t size) +{ + memcpy(name, K64_ETH_IF_NAME, (size < sizeof(K64_ETH_IF_NAME)) ? size : sizeof(K64_ETH_IF_NAME)); +} + +static uint8_t k64f_eth_get_hwaddr_size(emac_interface_t *emac) +{ + return NSAPI_MAC_BYTES; +} + +static void k64f_eth_get_hwaddr(emac_interface_t *emac, uint8_t *addr) +{ + mbed_mac_address((char*)addr); +} + +static void k64f_eth_set_hwaddr(emac_interface_t *emac, uint8_t *addr) +{ + /* No-op at this stage */ +} + +static void k64f_eth_set_link_input_cb(emac_interface_t *emac, emac_link_input_fn input_cb, void *data) +{ + struct k64f_enetdata *enet = emac->hw; + + enet->emac_link_input_cb = input_cb; + enet->emac_link_input_cb_data = data; +} + +static void k64f_eth_set_link_state_cb(emac_interface_t *emac, emac_link_state_change_fn state_cb, void *data) +{ + struct k64f_enetdata *enet = emac->hw; + + enet->emac_link_state_cb = state_cb; + enet->emac_link_state_cb_data = data; } -void eth_arch_disable_interrupts(void) { +static void k64f_eth_add_multicast_group(emac_interface_t *emac, uint8_t *addr) +{ + ENET_AddMulticastGroup(ENET, addr); +} +static void k64f_eth_power_down(emac_interface_t *emac) +{ + /* No-op at this stage */ } + +const emac_interface_ops_t k64f_eth_emac_ops = { + .get_mtu_size = k64f_eth_get_mtu_size, + .get_ifname = k64f_eth_get_ifname, + .get_hwaddr_size = k64f_eth_get_hwaddr_size, + .get_hwaddr = k64f_eth_get_hwaddr, + .set_hwaddr = k64f_eth_set_hwaddr, + .link_out = k64f_eth_link_out, + .power_up = k64f_eth_power_up, + .power_down = k64f_eth_power_down, + .set_link_input_cb = k64f_eth_set_link_input_cb, + .set_link_state_cb = k64f_eth_set_link_state_cb, + .add_multicast_group = k64f_eth_add_multicast_group +}; + +emac_interface_t mbed_emac_eth_default = {&k64f_eth_emac_ops, &k64f_enetdata}; + /** * @} */ diff --git a/features/FEATURE_LWIP/lwip-interface/lwip_stack.h b/features/FEATURE_LWIP/lwip-interface/lwip_stack.h deleted file mode 100644 index afbb8d2f5b0..00000000000 --- a/features/FEATURE_LWIP/lwip-interface/lwip_stack.h +++ /dev/null @@ -1,44 +0,0 @@ -/* LWIP implementation of NetworkInterfaceAPI - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef LWIP_STACK_H -#define LWIP_STACK_H - -#include "nsapi.h" -#include "emac_api.h" - -#ifdef __cplusplus -extern "C" { -#endif - -// Access to lwip through the nsapi -nsapi_error_t mbed_lwip_init(emac_interface_t *emac); -nsapi_error_t mbed_lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw); -nsapi_error_t mbed_lwip_bringdown(void); - -const char *mbed_lwip_get_mac_address(void); -char *mbed_lwip_get_ip_address(char *buf, int buflen); -char *mbed_lwip_get_netmask(char *buf, int buflen); -char *mbed_lwip_get_gateway(char *buf, int buflen); - -extern nsapi_stack_t lwip_stack; - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/features/FEATURE_LWIP/lwip-interface/lwip_tools.c b/features/FEATURE_LWIP/lwip-interface/lwip_tools.c new file mode 100644 index 00000000000..825a7b20ce6 --- /dev/null +++ b/features/FEATURE_LWIP/lwip-interface/lwip_tools.c @@ -0,0 +1,112 @@ +/* LWIP common helpers + * Copyright (c) 2017 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "lwip/opt.h" +#include "lwip/netif.h" +#include "lwip/ip.h" +#include "lwip/api.h" + +#include "lwip_tools.h" + +/* Static arena of sockets */ +struct mbed_lwip_socket mbed_lwip_arena[MEMP_NUM_NETCONN]; + +static const ip_addr_t *mbed_lwip_get_ipv4_addr(const struct netif *netif) +{ +#if LWIP_IPV4 + if (!netif_is_up(netif)) { + return NULL; + } + + if (!ip4_addr_isany(netif_ip4_addr(netif))) { + return netif_ip_addr4(netif); + } +#endif + + return NULL; +} + +static const ip_addr_t *mbed_lwip_get_ipv6_addr(const struct netif *netif) +{ +#if LWIP_IPV6 + if (!netif_is_up(netif)) { + return NULL; + } + + for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { + if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && + !ip6_addr_islinklocal(netif_ip6_addr(netif, i))) { + return netif_ip_addr6(netif, i); + } + } +#endif + + return NULL; + +} + +const ip_addr_t *mbed_lwip_get_ip_addr(bool any_addr, const struct netif *netif) +{ + const ip_addr_t *pref_ip_addr = 0; + const ip_addr_t *npref_ip_addr = 0; + +#if IP_VERSION_PREF == PREF_IPV4 + pref_ip_addr = mbed_lwip_get_ipv4_addr(netif); + npref_ip_addr = mbed_lwip_get_ipv6_addr(netif); +#else + pref_ip_addr = mbed_lwip_get_ipv6_addr(netif); + npref_ip_addr = mbed_lwip_get_ipv4_addr(netif); +#endif + + if (pref_ip_addr) { + return pref_ip_addr; + } else if (npref_ip_addr && any_addr) { + return npref_ip_addr; + } + + return NULL; +} + +void mbed_lwip_arena_init(void) +{ + memset(mbed_lwip_arena, 0, sizeof(mbed_lwip_arena)); +} + +struct mbed_lwip_socket *mbed_lwip_arena_alloc(void) +{ + sys_prot_t prot = sys_arch_protect(); + + for (int i = 0; i < MEMP_NUM_NETCONN; i++) { + if (!mbed_lwip_arena[i].in_use) { + struct mbed_lwip_socket *s = &mbed_lwip_arena[i]; + memset(s, 0, sizeof(*s)); + s->in_use = true; + sys_arch_unprotect(prot); + return s; + } + } + + sys_arch_unprotect(prot); + return 0; +} + +void mbed_lwip_arena_dealloc(struct mbed_lwip_socket *s) +{ + s->in_use = false; +} diff --git a/features/FEATURE_LWIP/lwip-interface/lwip_tools.h b/features/FEATURE_LWIP/lwip-interface/lwip_tools.h new file mode 100644 index 00000000000..35c60edb60c --- /dev/null +++ b/features/FEATURE_LWIP/lwip-interface/lwip_tools.h @@ -0,0 +1,43 @@ +/* LWIP common helpers + * Copyright (c) 2017 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LWIP_TOOLS_H +#define LWIP_TOOLS_H + +#include + +#include "lwip/netif.h" +#include "lwip/ip.h" + +struct mbed_lwip_socket { + bool in_use; + + struct netconn *conn; + struct netbuf *buf; + u16_t offset; + + void (*cb)(void *); + void *data; +}; + +extern struct mbed_lwip_socket mbed_lwip_arena[MEMP_NUM_NETCONN]; + +const ip_addr_t *mbed_lwip_get_ip_addr(bool any_addr, const struct netif *netif); +void mbed_lwip_arena_init(void); +struct mbed_lwip_socket *mbed_lwip_arena_alloc(void); +void mbed_lwip_arena_dealloc(struct mbed_lwip_socket *s); + +#endif /* LWIP_TOOLS_H */ diff --git a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c b/features/FEATURE_LWIP/lwip-interface/nsapi_stack_lwip.c similarity index 52% rename from features/FEATURE_LWIP/lwip-interface/lwip_stack.c rename to features/FEATURE_LWIP/lwip-interface/nsapi_stack_lwip.c index dffc1610fb7..32d4fe6a900 100644 --- a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c +++ b/features/FEATURE_LWIP/lwip-interface/nsapi_stack_lwip.c @@ -1,5 +1,5 @@ -/* LWIP implementation of NetworkInterfaceAPI - * Copyright (c) 2015 ARM Limited +/* LWIP implementation of NSAPI NetworkStack + * Copyright (c) 2017 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,14 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "nsapi.h" #include "mbed_interface.h" +#include "mbed_assert.h" #include #include #include -#include "eth_arch.h" #include "lwip/opt.h" #include "lwip/api.h" #include "lwip/inet.h" @@ -34,57 +33,12 @@ #include "lwip/udp.h" #include "emac_api.h" +#include "lwip_tools.h" -#if DEVICE_EMAC - #define MBED_NETIF_INIT_FN emac_lwip_if_init -#else - #define MBED_NETIF_INIT_FN eth_arch_enetif_init +#ifdef __cplusplus +extern "C" { #endif -#define DHCP_TIMEOUT 15000 - -/* Static arena of sockets */ -static struct lwip_socket { - bool in_use; - - struct netconn *conn; - struct netbuf *buf; - u16_t offset; - - void (*cb)(void *); - void *data; -} lwip_arena[MEMP_NUM_NETCONN]; - -static bool lwip_connected = false; - -static void mbed_lwip_arena_init(void) -{ - memset(lwip_arena, 0, sizeof lwip_arena); -} - -static struct lwip_socket *mbed_lwip_arena_alloc(void) -{ - sys_prot_t prot = sys_arch_protect(); - - for (int i = 0; i < MEMP_NUM_NETCONN; i++) { - if (!lwip_arena[i].in_use) { - struct lwip_socket *s = &lwip_arena[i]; - memset(s, 0, sizeof *s); - s->in_use = true; - sys_arch_unprotect(prot); - return s; - } - } - - sys_arch_unprotect(prot); - return 0; -} - -static void mbed_lwip_arena_dealloc(struct lwip_socket *s) -{ - s->in_use = false; -} - static void mbed_lwip_socket_callback(struct netconn *nc, enum netconn_evt eh, u16_t len) { // Filter send minus events @@ -95,21 +49,42 @@ static void mbed_lwip_socket_callback(struct netconn *nc, enum netconn_evt eh, u sys_prot_t prot = sys_arch_protect(); for (int i = 0; i < MEMP_NUM_NETCONN; i++) { - if (lwip_arena[i].in_use - && lwip_arena[i].conn == nc - && lwip_arena[i].cb) { - lwip_arena[i].cb(lwip_arena[i].data); + if (mbed_lwip_arena[i].in_use + && mbed_lwip_arena[i].conn == nc + && mbed_lwip_arena[i].cb) { + mbed_lwip_arena[i].cb(mbed_lwip_arena[i].data); } } sys_arch_unprotect(prot); } - -/* TCP/IP and Network Interface Initialisation */ -static struct netif lwip_netif; -static bool lwip_dhcp = false; -static char lwip_mac_address[NSAPI_MAC_SIZE]; +/* LWIP error remapping */ +static nsapi_error_t mbed_lwip_err_remap(err_t err) { + switch (err) { + case ERR_OK: + case ERR_CLSD: + return 0; + case ERR_MEM: + return NSAPI_ERROR_NO_MEMORY; + case ERR_CONN: + case ERR_RST: + case ERR_ABRT: + return NSAPI_ERROR_NO_CONNECTION; + case ERR_TIMEOUT: + case ERR_RTE: + case ERR_INPROGRESS: + case ERR_WOULDBLOCK: + return NSAPI_ERROR_WOULD_BLOCK; + case ERR_VAL: + case ERR_USE: + case ERR_ISCONN: + case ERR_ARG: + return NSAPI_ERROR_PARAMETER; + default: + return NSAPI_ERROR_DEVICE_ERROR; + } +} #if !LWIP_IPV4 || !LWIP_IPV6 static bool all_zeros(const uint8_t *p, int len) @@ -124,6 +99,25 @@ static bool all_zeros(const uint8_t *p, int len) } #endif +static bool convert_lwip_addr_to_mbed(nsapi_addr_t *out, const ip_addr_t *in) +{ +#if LWIP_IPV6 + if (IP_IS_V6(in)) { + out->version = NSAPI_IPv6; + MEMCPY(out->bytes, ip_2_ip6(in), sizeof(ip6_addr_t)); + return true; + } +#endif +#if LWIP_IPV4 + if (IP_IS_V4(in)) { + out->version = NSAPI_IPv4; + MEMCPY(out->bytes, ip_2_ip4(in), sizeof(ip4_addr_t)); + return true; + } +#endif + return false; +} + static bool convert_mbed_addr_to_lwip(ip_addr_t *out, const nsapi_addr_t *in) { #if LWIP_IPV6 @@ -173,419 +167,6 @@ static bool convert_mbed_addr_to_lwip(ip_addr_t *out, const nsapi_addr_t *in) return false; } -static bool convert_lwip_addr_to_mbed(nsapi_addr_t *out, const ip_addr_t *in) -{ -#if LWIP_IPV6 - if (IP_IS_V6(in)) { - out->version = NSAPI_IPv6; - MEMCPY(out->bytes, ip_2_ip6(in), sizeof(ip6_addr_t)); - return true; - } -#endif -#if LWIP_IPV4 - if (IP_IS_V4(in)) { - out->version = NSAPI_IPv4; - MEMCPY(out->bytes, ip_2_ip4(in), sizeof(ip4_addr_t)); - return true; - } -#endif - return false; -} - -static const ip_addr_t *mbed_lwip_get_ipv4_addr(const struct netif *netif) -{ -#if LWIP_IPV4 - if (!netif_is_up(netif)) { - return NULL; - } - - if (!ip4_addr_isany(netif_ip4_addr(netif))) { - return netif_ip_addr4(netif); - } -#endif - - return NULL; -} - -static const ip_addr_t *mbed_lwip_get_ipv6_addr(const struct netif *netif) -{ -#if LWIP_IPV6 - if (!netif_is_up(netif)) { - return NULL; - } - - for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { - if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && - !ip6_addr_islinklocal(netif_ip6_addr(netif, i))) { - return netif_ip_addr6(netif, i); - } - } -#endif - - return NULL; - -} - -const ip_addr_t *mbed_lwip_get_ip_addr(bool any_addr, const struct netif *netif) -{ - const ip_addr_t *pref_ip_addr = 0; - const ip_addr_t *npref_ip_addr = 0; - -#if IP_VERSION_PREF == PREF_IPV4 - pref_ip_addr = mbed_lwip_get_ipv4_addr(netif); - npref_ip_addr = mbed_lwip_get_ipv6_addr(netif); -#else - pref_ip_addr = mbed_lwip_get_ipv6_addr(netif); - npref_ip_addr = mbed_lwip_get_ipv4_addr(netif); -#endif - - if (pref_ip_addr) { - return pref_ip_addr; - } else if (npref_ip_addr && any_addr) { - return npref_ip_addr; - } - - return NULL; -} - -void add_dns_addr(struct netif *lwip_netif) -{ - // Do nothing if not brought up - const ip_addr_t *ip_addr = mbed_lwip_get_ip_addr(true, lwip_netif); - if (!ip_addr) { - return; - } - - // Check for existing dns server - for (char numdns = 0; numdns < DNS_MAX_SERVERS; numdns++) { - const ip_addr_t *dns_ip_addr = dns_getserver(numdns); - if (!ip_addr_isany(dns_ip_addr)) { - return; - } - } - -#if LWIP_IPV6 - if (IP_IS_V6(ip_addr)) { - /* 2001:4860:4860::8888 google */ - ip_addr_t ipv6_dns_addr = IPADDR6_INIT( - PP_HTONL(0x20014860UL), - PP_HTONL(0x48600000UL), - PP_HTONL(0x00000000UL), - PP_HTONL(0x00008888UL)); - dns_setserver(0, &ipv6_dns_addr); - } -#endif - -#if LWIP_IPV4 - if (IP_IS_V4(ip_addr)) { - /* 8.8.8.8 google */ - ip_addr_t ipv4_dns_addr = IPADDR4_INIT(0x08080808); - dns_setserver(0, &ipv4_dns_addr); - } -#endif -} - -static sys_sem_t lwip_tcpip_inited; -static void mbed_lwip_tcpip_init_irq(void *eh) -{ - sys_sem_signal(&lwip_tcpip_inited); -} - -static sys_sem_t lwip_netif_linked; -static void mbed_lwip_netif_link_irq(struct netif *lwip_netif) -{ - if (netif_is_link_up(lwip_netif)) { - sys_sem_signal(&lwip_netif_linked); - } -} - -static sys_sem_t lwip_netif_has_addr; -static void mbed_lwip_netif_status_irq(struct netif *lwip_netif) -{ - static bool any_addr = true; - - if (netif_is_up(lwip_netif)) { - // Indicates that has address - if (any_addr == true && mbed_lwip_get_ip_addr(true, lwip_netif)) { - sys_sem_signal(&lwip_netif_has_addr); - any_addr = false; - return; - } - - // Indicates that has preferred address - if (mbed_lwip_get_ip_addr(false, lwip_netif)) { - sys_sem_signal(&lwip_netif_has_addr); - } - } else { - any_addr = true; - } -} - -static void mbed_lwip_set_mac_address(void) -{ -#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE) - snprintf(lwip_mac_address, NSAPI_MAC_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x", - MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2, - MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5); -#else - char mac[6]; - mbed_mac_address(mac); - snprintf(lwip_mac_address, NSAPI_MAC_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x", - mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); -#endif -} - -/* LWIP interface implementation */ -const char *mbed_lwip_get_mac_address(void) -{ - return lwip_mac_address[0] ? lwip_mac_address : 0; -} - -char *mbed_lwip_get_ip_address(char *buf, nsapi_size_t buflen) -{ - const ip_addr_t *addr = mbed_lwip_get_ip_addr(true, &lwip_netif); - if (!addr) { - return NULL; - } -#if LWIP_IPV6 - if (IP_IS_V6(addr)) { - return ip6addr_ntoa_r(ip_2_ip6(addr), buf, buflen); - } -#endif -#if LWIP_IPV4 - if (IP_IS_V4(addr)) { - return ip4addr_ntoa_r(ip_2_ip4(addr), buf, buflen); - } -#endif - return NULL; -} - -const char *mbed_lwip_get_netmask(char *buf, nsapi_size_t buflen) -{ -#if LWIP_IPV4 - const ip4_addr_t *addr = netif_ip4_netmask(&lwip_netif); - if (!ip4_addr_isany(addr)) { - return ip4addr_ntoa_r(addr, buf, buflen); - } else { - return NULL; - } -#else - return NULL; -#endif -} - -char *mbed_lwip_get_gateway(char *buf, nsapi_size_t buflen) -{ -#if LWIP_IPV4 - const ip4_addr_t *addr = netif_ip4_gw(&lwip_netif); - if (!ip4_addr_isany(addr)) { - return ip4addr_ntoa_r(addr, buf, buflen); - } else { - return NULL; - } -#else - return NULL; -#endif -} - -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; -} - -nsapi_error_t mbed_lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw) -{ - // Check if we've already connected - if (lwip_connected) { - return NSAPI_ERROR_PARAMETER; - } - - if(mbed_lwip_init(NULL) != NSAPI_ERROR_OK) { - return NSAPI_ERROR_DEVICE_ERROR; - } - - // Zero out socket set - mbed_lwip_arena_init(); - -#if LWIP_IPV6 - 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; -#endif /* LWIP_IPV6_AUTOCONFIG */ - -#endif - - u32_t ret; - - if (!netif_is_link_up(&lwip_netif)) { - ret = sys_arch_sem_wait(&lwip_netif_linked, 15000); - - if (ret == SYS_ARCH_TIMEOUT) { - return NSAPI_ERROR_NO_CONNECTION; - } - } - -#if LWIP_IPV4 - if (!dhcp) { - 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 - - netif_set_up(&lwip_netif); - -#if LWIP_IPV4 - // Connect to the network - lwip_dhcp = dhcp; - - if (lwip_dhcp) { - err_t err = dhcp_start(&lwip_netif); - if (err) { - return NSAPI_ERROR_DHCP_FAILURE; - } - } -#endif - - // If doesn't have address - if (!mbed_lwip_get_ip_addr(true, &lwip_netif)) { - ret = sys_arch_sem_wait(&lwip_netif_has_addr, 15000); - if (ret == SYS_ARCH_TIMEOUT) { - return NSAPI_ERROR_DHCP_FAILURE; - } - } - -#if ADDR_TIMEOUT - // 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)) { - ret = sys_arch_sem_wait(&lwip_netif_has_addr, ADDR_TIMEOUT * 1000); - } -#endif - - add_dns_addr(&lwip_netif); - - lwip_connected = true; - return 0; -} - -#if LWIP_IPV6 -void mbed_lwip_clear_ipv6_addresses(struct netif *lwip_netif) -{ - for (u8_t i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { - netif_ip6_addr_set_state(lwip_netif, i, IP6_ADDR_INVALID); - } -} -#endif - -nsapi_error_t mbed_lwip_bringdown(void) -{ - // Check if we've connected - if (!lwip_connected) { - return NSAPI_ERROR_PARAMETER; - } - -#if LWIP_IPV4 - // Disconnect from the network - if (lwip_dhcp) { - dhcp_release(&lwip_netif); - dhcp_stop(&lwip_netif); - lwip_dhcp = false; - } -#endif - - netif_set_down(&lwip_netif); - -#if LWIP_IPV6 - mbed_lwip_clear_ipv6_addresses(&lwip_netif); -#endif - - sys_sem_free(&lwip_netif_has_addr); - sys_sem_new(&lwip_netif_has_addr, 0); - lwip_connected = false; - return 0; -} - -/* LWIP error remapping */ -static nsapi_error_t mbed_lwip_err_remap(err_t err) { - switch (err) { - case ERR_OK: - case ERR_CLSD: - return 0; - case ERR_MEM: - return NSAPI_ERROR_NO_MEMORY; - case ERR_CONN: - case ERR_RST: - case ERR_ABRT: - return NSAPI_ERROR_NO_CONNECTION; - case ERR_TIMEOUT: - case ERR_RTE: - case ERR_INPROGRESS: - case ERR_WOULDBLOCK: - return NSAPI_ERROR_WOULD_BLOCK; - case ERR_VAL: - case ERR_USE: - case ERR_ISCONN: - case ERR_ARG: - return NSAPI_ERROR_PARAMETER; - default: - return NSAPI_ERROR_DEVICE_ERROR; - } -} - /* LWIP network stack implementation */ static nsapi_error_t mbed_lwip_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version) { @@ -595,7 +176,7 @@ static nsapi_error_t mbed_lwip_gethostbyname(nsapi_stack_t *stack, const char *h u8_t addr_type; if (version == NSAPI_UNSPEC) { const ip_addr_t *ip_addr; - ip_addr = mbed_lwip_get_ip_addr(true, &lwip_netif); + ip_addr = mbed_lwip_get_ip_addr(true, &stack->emac->netif); if (IP_IS_V6(ip_addr)) { addr_type = NETCONN_DNS_IPV6; } else { @@ -647,12 +228,12 @@ static nsapi_error_t mbed_lwip_add_dns_server(nsapi_stack_t *stack, nsapi_addr_t static nsapi_error_t mbed_lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto) { // check if network is connected - if (!lwip_connected) { + if (!stack->emac->connected) { return NSAPI_ERROR_NO_CONNECTION; } // allocate a socket - struct lwip_socket *s = mbed_lwip_arena_alloc(); + struct mbed_lwip_socket *s = mbed_lwip_arena_alloc(); if (!s) { return NSAPI_ERROR_NO_SOCKET; } @@ -661,7 +242,7 @@ static nsapi_error_t mbed_lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t #if LWIP_IPV6 && LWIP_IPV4 const ip_addr_t *ip_addr; - ip_addr = mbed_lwip_get_ip_addr(true, &lwip_netif); + ip_addr = mbed_lwip_get_ip_addr(true, &stack->emac->netif); if (IP_IS_V6(ip_addr)) { // Enable IPv6 (or dual-stack). LWIP dual-stack support is @@ -683,13 +264,13 @@ static nsapi_error_t mbed_lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t } netconn_set_recvtimeout(s->conn, 1); - *(struct lwip_socket **)handle = s; + *(struct mbed_lwip_socket **)handle = s; return 0; } static nsapi_error_t mbed_lwip_socket_close(nsapi_stack_t *stack, nsapi_socket_t handle) { - struct lwip_socket *s = (struct lwip_socket *)handle; + struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; netbuf_delete(s->buf); err_t err = netconn_delete(s->conn); @@ -699,7 +280,7 @@ static nsapi_error_t mbed_lwip_socket_close(nsapi_stack_t *stack, nsapi_socket_t static nsapi_error_t mbed_lwip_socket_bind(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port) { - struct lwip_socket *s = (struct lwip_socket *)handle; + struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; ip_addr_t ip_addr; if ((s->conn->type == NETCONN_TCP && s->conn->pcb.tcp->local_port != 0) || @@ -717,7 +298,7 @@ static nsapi_error_t mbed_lwip_socket_bind(nsapi_stack_t *stack, nsapi_socket_t static nsapi_error_t mbed_lwip_socket_listen(nsapi_stack_t *stack, nsapi_socket_t handle, int backlog) { - struct lwip_socket *s = (struct lwip_socket *)handle; + struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; err_t err = netconn_listen_with_backlog(s->conn, backlog); return mbed_lwip_err_remap(err); @@ -725,7 +306,7 @@ static nsapi_error_t mbed_lwip_socket_listen(nsapi_stack_t *stack, nsapi_socket_ static nsapi_error_t mbed_lwip_socket_connect(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port) { - struct lwip_socket *s = (struct lwip_socket *)handle; + struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; ip_addr_t ip_addr; if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) { @@ -741,8 +322,8 @@ static nsapi_error_t mbed_lwip_socket_connect(nsapi_stack_t *stack, nsapi_socket static nsapi_error_t mbed_lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *handle, nsapi_addr_t *addr, uint16_t *port) { - struct lwip_socket *s = (struct lwip_socket *)server; - struct lwip_socket *ns = mbed_lwip_arena_alloc(); + struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)server; + struct mbed_lwip_socket *ns = mbed_lwip_arena_alloc(); if (!ns) { return NSAPI_ERROR_NO_SOCKET; } @@ -754,7 +335,7 @@ static nsapi_error_t mbed_lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_ } netconn_set_recvtimeout(ns->conn, 1); - *(struct lwip_socket **)handle = ns; + *(struct mbed_lwip_socket **)handle = ns; ip_addr_t peer_addr; (void) netconn_peer(ns->conn, &peer_addr, port); @@ -767,7 +348,7 @@ static nsapi_error_t mbed_lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_ static nsapi_size_or_error_t mbed_lwip_socket_send(nsapi_stack_t *stack, nsapi_socket_t handle, const void *data, nsapi_size_t size) { - struct lwip_socket *s = (struct lwip_socket *)handle; + struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; size_t bytes_written = 0; err_t err = netconn_write_partly(s->conn, data, size, NETCONN_COPY, &bytes_written); @@ -780,7 +361,7 @@ static nsapi_size_or_error_t mbed_lwip_socket_send(nsapi_stack_t *stack, nsapi_s static nsapi_size_or_error_t mbed_lwip_socket_recv(nsapi_stack_t *stack, nsapi_socket_t handle, void *data, nsapi_size_t size) { - struct lwip_socket *s = (struct lwip_socket *)handle; + struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; if (!s->buf) { err_t err = netconn_recv(s->conn, &s->buf); @@ -804,7 +385,7 @@ static nsapi_size_or_error_t mbed_lwip_socket_recv(nsapi_stack_t *stack, nsapi_s static nsapi_size_or_error_t mbed_lwip_socket_sendto(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port, const void *data, nsapi_size_t size) { - struct lwip_socket *s = (struct lwip_socket *)handle; + struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; ip_addr_t ip_addr; if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) { @@ -829,7 +410,7 @@ static nsapi_size_or_error_t mbed_lwip_socket_sendto(nsapi_stack_t *stack, nsapi static nsapi_size_or_error_t mbed_lwip_socket_recvfrom(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t *addr, uint16_t *port, void *data, nsapi_size_t size) { - struct lwip_socket *s = (struct lwip_socket *)handle; + struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; struct netbuf *buf; err_t err = netconn_recv(s->conn, &buf); @@ -848,7 +429,7 @@ static nsapi_size_or_error_t mbed_lwip_socket_recvfrom(nsapi_stack_t *stack, nsa static nsapi_error_t mbed_lwip_setsockopt(nsapi_stack_t *stack, nsapi_socket_t handle, int level, int optname, const void *optval, unsigned optlen) { - struct lwip_socket *s = (struct lwip_socket *)handle; + struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; switch (optname) { case NSAPI_KEEPALIVE: @@ -894,7 +475,7 @@ static nsapi_error_t mbed_lwip_setsockopt(nsapi_stack_t *stack, nsapi_socket_t h static void mbed_lwip_socket_attach(nsapi_stack_t *stack, nsapi_socket_t handle, void (*callback)(void *), void *data) { - struct lwip_socket *s = (struct lwip_socket *)handle; + struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; s->cb = callback; s->data = data; @@ -918,6 +499,6 @@ const nsapi_stack_api_t lwip_stack_api = { .socket_attach = mbed_lwip_socket_attach, }; -nsapi_stack_t lwip_stack = { - .stack_api = &lwip_stack_api, -}; +#ifdef __cplusplus +} +#endif diff --git a/features/netsocket/EthInterface.h b/features/netsocket/EthInterface.h deleted file mode 100644 index 6d4ee9ad24b..00000000000 --- a/features/netsocket/EthInterface.h +++ /dev/null @@ -1,37 +0,0 @@ - -/** \addtogroup netsocket */ -/** @{*/ -/* EthInterface - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ETH_INTERFACE_H -#define ETH_INTERFACE_H - -#include "netsocket/NetworkInterface.h" - - -/** EthInterface class - * - * Common interface that is shared between ethernet hardware. - */ -class EthInterface : public NetworkInterface -{ -}; - - -#endif - -/** @}*/ diff --git a/features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp b/features/netsocket/EthernetInterface.cpp similarity index 67% rename from features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp rename to features/netsocket/EthernetInterface.cpp index 9a05a981302..c497f3fc43e 100644 --- a/features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp +++ b/features/netsocket/EthernetInterface.cpp @@ -15,13 +15,13 @@ */ #include "EthernetInterface.h" -#include "lwip_stack.h" - +#include "mbed_ipstack.h" /* Interface implementation */ -EthernetInterface::EthernetInterface() - : _dhcp(true), _ip_address(), _netmask(), _gateway() +EthernetInterface::EthernetInterface(emac_interface_t *emac) + : _dhcp(true), _ip_address(), _netmask(), _gateway(), _stack() { + mbed_ipstack_set_stack(emac, &_stack); } nsapi_error_t EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway) @@ -41,7 +41,16 @@ nsapi_error_t EthernetInterface::set_dhcp(bool dhcp) nsapi_error_t EthernetInterface::connect() { - return mbed_lwip_bringup(_dhcp, + nsapi_error_t err; + if (_stack.emac == NULL) + return NSAPI_ERROR_UNSUPPORTED; + + mbed_ipstack_init(); + err = mbed_ipstack_add_netif(_stack.emac, true); + if (err != NSAPI_ERROR_OK) + return err; + + return mbed_ipstack_bringup(_stack.emac, _dhcp, _ip_address[0] ? _ip_address : 0, _netmask[0] ? _netmask : 0, _gateway[0] ? _gateway : 0); @@ -49,17 +58,17 @@ nsapi_error_t EthernetInterface::connect() nsapi_error_t EthernetInterface::disconnect() { - return mbed_lwip_bringdown(); + return mbed_ipstack_bringdown(_stack.emac); } const char *EthernetInterface::get_mac_address() { - return mbed_lwip_get_mac_address(); + return mbed_ipstack_get_mac_address(_stack.emac); } const char *EthernetInterface::get_ip_address() { - if (mbed_lwip_get_ip_address(_ip_address, sizeof _ip_address)) { + if (mbed_ipstack_get_ip_address(_stack.emac, _ip_address, sizeof(_ip_address))) { return _ip_address; } @@ -68,7 +77,7 @@ const char *EthernetInterface::get_ip_address() const char *EthernetInterface::get_netmask() { - if (mbed_lwip_get_netmask(_netmask, sizeof _netmask)) { + if (mbed_ipstack_get_netmask(_stack.emac, _netmask, sizeof(_netmask))) { return _netmask; } @@ -77,7 +86,7 @@ const char *EthernetInterface::get_netmask() const char *EthernetInterface::get_gateway() { - if (mbed_lwip_get_gateway(_gateway, sizeof _gateway)) { + if (mbed_ipstack_get_gateway(_stack.emac, _gateway, sizeof(_gateway))) { return _gateway; } @@ -86,5 +95,5 @@ const char *EthernetInterface::get_gateway() NetworkStack *EthernetInterface::get_stack() { - return nsapi_create_stack(&lwip_stack); + return nsapi_create_stack(&_stack); } diff --git a/features/FEATURE_LWIP/lwip-interface/EthernetInterface.h b/features/netsocket/EthernetInterface.h similarity index 90% rename from features/FEATURE_LWIP/lwip-interface/EthernetInterface.h rename to features/netsocket/EthernetInterface.h index f92f70e4ebf..8dd6e30aa7c 100644 --- a/features/FEATURE_LWIP/lwip-interface/EthernetInterface.h +++ b/features/netsocket/EthernetInterface.h @@ -19,21 +19,24 @@ #include "nsapi.h" #include "rtos.h" -#include "lwip/netif.h" +#include "hal/emac_api.h" + // Forward declaration class NetworkStack; /** EthernetInterface class - * Implementation of the NetworkStack for LWIP + * Implementation of the NetworkStack for mbed OS IP stack and EMAC APIs */ -class EthernetInterface : public EthInterface +class EthernetInterface : public NetworkInterface { public: /** EthernetInterface lifetime + * + * @param emac EMAC HAL implementation for network interface to be used by this class (Default: platform default) */ - EthernetInterface(); + EthernetInterface(emac_interface_t *emac = &mbed_emac_eth_default); /** Set a static IP address * @@ -73,7 +76,7 @@ class EthernetInterface : public EthInterface * Provided MAC address is intended for info or debug purposes and * may not be provided if the underlying network interface does not * provide a MAC address - * + * * @return Null-terminated representation of the local MAC address * or null if no MAC address is available */ @@ -111,7 +114,7 @@ class EthernetInterface : public EthInterface char _ip_address[IPADDR_STRLEN_MAX]; char _netmask[NSAPI_IPv4_SIZE]; char _gateway[NSAPI_IPv4_SIZE]; + nsapi_stack_t _stack; }; - #endif diff --git a/features/netsocket/emac_stack_mem.h b/features/netsocket/emac_stack_mem.h index dc81317b5d9..c04b9584ab6 100644 --- a/features/netsocket/emac_stack_mem.h +++ b/features/netsocket/emac_stack_mem.h @@ -16,8 +16,6 @@ #ifndef MBED_EMAC_STACK_MEM_H #define MBED_EMAC_STACK_MEM_H -#if DEVICE_EMAC - #include /** @@ -29,88 +27,84 @@ */ typedef void emac_stack_mem_t; typedef void emac_stack_mem_chain_t; -typedef void emac_stack_t; /** * Allocates stack memory * - * @param stack Emac stack context * @param size Size of memory to allocate * @param align Memory alignment requirements * @return Allocated memory struct, or NULL in case of error */ -emac_stack_mem_t *emac_stack_mem_alloc(emac_stack_t* stack, uint32_t size, uint32_t align); +emac_stack_mem_t *emac_stack_mem_alloc(uint32_t size, uint32_t align); /** * Free memory allocated using @a stack_mem_alloc * - * @param stack Emac stack context * @param mem Memory to be freed */ -void emac_stack_mem_free(emac_stack_t* stack, emac_stack_mem_t *mem); +void emac_stack_mem_free(emac_stack_mem_t *mem); /** * Copy memory * - * @param stack Emac stack context * @param to Memory to copy to * @param from Memory to copy from */ -void emac_stack_mem_copy(emac_stack_t* stack, emac_stack_mem_t *to, emac_stack_mem_t *from); +void emac_stack_mem_copy(emac_stack_mem_t *to, emac_stack_mem_t *from); /** * Return pointer to the payload * - * @param stack Emac stack context * @param mem Memory structure * @return Pointer to the payload */ -void *emac_stack_mem_ptr(emac_stack_t* stack, emac_stack_mem_t *mem); +void *emac_stack_mem_ptr(emac_stack_mem_t *mem); /** * Return actual payload size * - * @param stack Emac stack context * @param mem Memory structure * @return Size in bytes */ -uint32_t emac_stack_mem_len(emac_stack_t* stack, emac_stack_mem_t *mem); +uint32_t emac_stack_mem_len(emac_stack_mem_t *mem); /** * Sets the actual payload size (the allocated payload size will not change) * - * @param stack Emac stack context * @param mem Memory structure * @param len Actual payload size */ -void emac_stack_mem_set_len(emac_stack_t* stack, emac_stack_mem_t *mem, uint32_t len); +void emac_stack_mem_set_len(emac_stack_mem_t *mem, uint32_t len); /** * Returns first memory structure from the list and move the head to point to the next node * - * @param stack Emac stack context * @param list Pointer to the list * @return First memory structure from the list */ -emac_stack_mem_t *emac_stack_mem_chain_dequeue(emac_stack_t* stack, emac_stack_mem_chain_t **chain); +emac_stack_mem_t *emac_stack_mem_chain_dequeue(emac_stack_mem_chain_t **chain); /** * Return total length of the memory chain * - * @param stack Emac stack context * @param chain Memory chain * @return Chain length */ -uint32_t emac_stack_mem_chain_len(emac_stack_t* stack, emac_stack_mem_chain_t *chain); +uint32_t emac_stack_mem_chain_len(emac_stack_mem_chain_t *chain); + +/** + * Set total length of the memory chain + * + * @param chain Memory chain + * @param len Total chain length + */ +void emac_stack_mem_set_chain_len(emac_stack_mem_chain_t *chain, uint32_t len); /** * Increases the reference counter for the memory * - * @param stack Emac stack context * @param mem Memory structure */ -void emac_stack_mem_ref(emac_stack_t* stack, emac_stack_mem_t *mem); - -#endif /* DEVICE_EMAC */ +void emac_stack_mem_ref(emac_stack_mem_t *mem); #endif /* EMAC_MBED_STACK_MEM_h */ diff --git a/features/netsocket/mbed_ipstack.h b/features/netsocket/mbed_ipstack.h new file mode 100644 index 00000000000..7f8c320abbb --- /dev/null +++ b/features/netsocket/mbed_ipstack.h @@ -0,0 +1,123 @@ +/* mbed OS IP stack API + * Copyright (c) 2015-2017 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MBED_IPSTACK_H +#define MBED_IPSTACK_H + +#include "nsapi.h" +#include "emac_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * mbed OS API for IP stack abstraction + * + * This interface should be used by targets to initialize IP stack, create, bring up and bring down network interfaces. + */ + +/** Initialize IP stack + * + * This function should be called before any of the network interfaces is added. It's up to target's setup code, to make + * sure that the stack is initialized and all the existing interfaces are registered with the stack. + * This function can be safely called multiple times, it will do nothing and return NSAPI_ERROR_OK if stack is already + * initialized. + * + * @return NSAPI_ERROR_OK on success, or error code + */ +void mbed_ipstack_init(void); + +/** Register a network interface with the IP stack + * + * Connects EMAC layer with the IP stack and initializes all the required infrastructure. + * This function should be called only once for each available interface. + * + * @param emac EMAC HAL implementation for this network interface + * @param default_if true if the interface should be treated as the default one + * @return NSAPI_ERROR_OK on success, or error code + */ +nsapi_error_t mbed_ipstack_add_netif(emac_interface_t *emac, bool default_if); + +/** Connect network stack with the IP stack + * + * @param emac EMAC HAL implementation + * @param stack Pointer to nsapi_stack_t to be set for this interface + */ +void mbed_ipstack_set_stack(emac_interface_t *emac, nsapi_stack_t *stack); + +/** Connect the interface to the network + * + * Sets up a connection on specified network interface, using DHCP or provided network details. If the @a dhcp is set to + * true all the remaining parameters are ignored. + * + * @param emac EMAC HAL implementation for this network interface + * @param dhcp true if the network details should be acquired using DHCP + * @param ip IP address to be used for the interface as "W:X:Y:Z" or NULL + * @param netmask Net mask to be used for the interface as "W:X:Y:Z" or NULL + * @param gw Gateway address to be used for the interface as "W:X:Y:Z" or NULL + * @return NSAPI_ERROR_OK on success, or error code + */ +nsapi_error_t mbed_ipstack_bringup(emac_interface_t *emac, bool dhcp, const char *ip, + const char *netmask, const char *gw); + +/** Disconnect interface from the network + * + * After this call the network interface is inactive, to use it again user needs to call @mbed_lwip_bringup again. + * + * @return NSAPI_ERROR_OK on success, or error code + */ +nsapi_error_t mbed_ipstack_bringdown(emac_interface_t *emac); + +/** Return MAC address of the network interface + * + * @param emac EMAC HAL implementation for this network interface + * @return MAC address as "V:W:X:Y:Z" + */ +char *mbed_ipstack_get_mac_address(emac_interface_t *emac); + +/** Copies IP address of the network interface to user supplied buffer + * + * @param emac EMAC HAL implementation for this network interface + * @param buf buffer to which IP address will be copied as "W:X:Y:Z" + * @param buflen size of supplied buffer + * @return Pointer to a buffer, or NULL if the buffer is too small + */ +char *mbed_ipstack_get_ip_address(emac_interface_t *emac, char *buf, nsapi_size_t buflen); + +/** Copies netmask of the network interface to user supplied buffer + * + * @param emac EMAC HAL implementation for this network interface + * @param buf buffer to which netmask will be copied as "W:X:Y:Z" + * @param buflen size of supplied buffer + * @return Pointer to a buffer, or NULL if the buffer is too small + */ +char *mbed_ipstack_get_netmask(emac_interface_t *emac, char *buf, nsapi_size_t buflen); + +/** Copies gateway address of the network interface to user supplied buffer + * + * @param emac EMAC HAL implementation for this network interface + * @param buf buffer to which gateway address will be copied as "W:X:Y:Z" + * @param buflen size of supplied buffer + * @return Pointer to a buffer, or NULL if the buffer is too small + */ +char *mbed_ipstack_get_gateway(emac_interface_t *emac, char *buf, nsapi_size_t buflen); + +#ifdef __cplusplus +} +#endif + +#endif /* MBED_IPSTACK_H */ diff --git a/features/netsocket/nsapi.h b/features/netsocket/nsapi.h index c9ed8bd6e47..0202323ff89 100644 --- a/features/netsocket/nsapi.h +++ b/features/netsocket/nsapi.h @@ -31,7 +31,6 @@ #include "netsocket/NetworkStack.h" #include "netsocket/NetworkInterface.h" -#include "netsocket/EthInterface.h" #include "netsocket/WiFiInterface.h" #include "netsocket/CellularInterface.h" #include "netsocket/MeshInterface.h" diff --git a/features/netsocket/nsapi_types.h b/features/netsocket/nsapi_types.h index c1823adf52a..096d536faf1 100644 --- a/features/netsocket/nsapi_types.h +++ b/features/netsocket/nsapi_types.h @@ -21,6 +21,7 @@ #define NSAPI_TYPES_H #include +#include "hal/emac_api.h" #ifdef __cplusplus extern "C" { @@ -246,8 +247,13 @@ typedef struct nsapi_stack { */ void *stack; - // Internal nsapi buffer + /** Internal nsapi buffer + */ unsigned _stack_buffer[16]; + + /** EMAC HAL implementation for the network interface + */ + emac_interface_t *emac; } nsapi_stack_t; /** nsapi_stack_api structure diff --git a/hal/emac_api.h b/hal/emac_api.h index e5fbd1a9419..b1045022d1e 100644 --- a/hal/emac_api.h +++ b/hal/emac_api.h @@ -17,10 +17,10 @@ #ifndef MBED_EMAC_API_H #define MBED_EMAC_API_H -#if DEVICE_EMAC - #include #include "emac_stack_mem.h" +#include "arch/sys_arch.h" +#include "lwip/netif.h" typedef struct emac_interface emac_interface_t; @@ -30,6 +30,11 @@ typedef struct emac_interface emac_interface_t; * This interface should be used to abstract low level access to networking hardware */ +/** This structure needs to be defined by targets wishing to provide ethernet driver using EMAC interface. It will + * be used by the EthernetInterface class to initialize the networking subsystem. + */ +extern emac_interface_t mbed_emac_eth_default; + /** * Callback to be register with Emac interface and to be called fore received packets * @@ -100,7 +105,7 @@ typedef void (*emac_set_hwaddr_fn)(emac_interface_t *emac, uint8_t *addr); * @param buf Packet to be send * @return True if the packet was send successfully, False otherwise */ -typedef bool (*emac_link_out_fn)(emac_interface_t *emac, emac_stack_mem_t *buf); +typedef bool (*emac_link_out_fn)(emac_interface_t *emac, emac_stack_mem_chain_t *buf); /** * Initializes the HW @@ -134,6 +139,13 @@ typedef void (*emac_set_link_input_cb_fn)(emac_interface_t *emac, emac_link_inpu */ typedef void (*emac_set_link_state_cb_fn)(emac_interface_t *emac, emac_link_state_change_fn state_cb, void *data); +/** Add device to a multicast group + * + * @param emac Emac interface + * @param address An multicast group IPv4 address + */ +typedef void (*emac_add_multicast_group)(emac_interface_t *emac, uint8_t *address); + typedef struct emac_interface_ops { emac_get_mtu_size_fn get_mtu_size; emac_get_ifname_fn get_ifname; @@ -145,16 +157,21 @@ typedef struct emac_interface_ops { emac_power_down_fn power_down; emac_set_link_input_cb_fn set_link_input_cb; emac_set_link_state_cb_fn set_link_state_cb; + emac_add_multicast_group add_multicast_group; } emac_interface_ops_t; typedef struct emac_interface { - const emac_interface_ops_t ops; - void *hw; + /* Members implemented by vendor */ + const emac_interface_ops_t *ops; /**< HW specific emac implementation */ + void *hw; /**< EMAC implementation specific user data */ + + /* Private members used by the stack */ + sys_sem_t linked; + sys_sem_t has_addr; + bool connected; + bool dhcp; + char hwaddr[6]; + struct netif netif; } emac_interface_t; -#else - -typedef void *emac_interface_t; - -#endif /* DEVICE_EMAC */ #endif /* MBED_EMAC_API_H */ diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/sdk/wifi_emac/wifi_emac_api.cpp b/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/sdk/wifi_emac/wifi_emac_api.cpp index 67eb1bb24b4..d33fddb01d8 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/sdk/wifi_emac/wifi_emac_api.cpp +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/sdk/wifi_emac/wifi_emac_api.cpp @@ -1,4 +1,4 @@ -#if DEVICE_EMAC +#if DEVICE_WIFI #include #include "cb_main.h" diff --git a/targets/targets.json b/targets/targets.json index 3ec7f735a6c..7ad663df234 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -580,7 +580,7 @@ "macros": ["CPU_MK64FN1M0VMD12", "FSL_RTOS_MBED"], "inherits": ["Target"], "detect_code": ["0240"], - "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE", "STDIO_MESSAGES", "STORAGE", "TRNG", "FLASH"], + "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "ETH", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE", "STDIO_MESSAGES", "STORAGE", "TRNG", "FLASH"], "features": ["LWIP", "STORAGE"], "release_versions": ["2", "5"], "device_name": "MK64FN1M0xxx12", @@ -1328,7 +1328,7 @@ "extra_labels": ["STM", "STM32F4", "STM32F439", "STM32F439ZI","STM32F439xx", "FLASH_CMSIS_ALGO"], "macros": ["HSE_VALUE=24000000", "HSE_STARTUP_TIMEOUT=5000", "CB_INTERFACE_SDIO","CB_CHIP_WL18XX","SUPPORT_80211D_ALWAYS","WLAN_ENABLED","MBEDTLS_ARC4_C","MBEDTLS_DES_C","MBEDTLS_MD4_C","MBEDTLS_MD5_C","MBEDTLS_SHA1_C"], "inherits": ["Target"], - "device_has": ["ANALOGIN", "CAN", "EMAC", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG", "FLASH"], + "device_has": ["ANALOGIN", "CAN", "ETH", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG", "FLASH", "WIFI"], "features": ["LWIP"], "release_versions": ["5"], "device_name": "STM32F439ZI",