From f9f1f44a3563dcb6cef0444605d0d0a5e4456068 Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Mon, 5 Sep 2016 16:42:19 +0100 Subject: [PATCH 01/61] Extend and rework WiFi interface General refactoring of the API and new methods added: * get_rssi() - measures radio signal strenght * get_state() - returns current state (not connected, connecting, connected, error) * scan() - scans for available networks sync and async versions * connect_async() - connect to a network without blocking --- .../net/network-socket/NetworkInterface.h | 6 ++ features/net/network-socket/WiFiInterface.h | 92 ++++++++++++++----- features/net/network-socket/nsapi_types.h | 33 ++++++- 3 files changed, 107 insertions(+), 24 deletions(-) diff --git a/features/net/network-socket/NetworkInterface.h b/features/net/network-socket/NetworkInterface.h index 4defc0c5ec..381dc83e51 100644 --- a/features/net/network-socket/NetworkInterface.h +++ b/features/net/network-socket/NetworkInterface.h @@ -36,6 +36,12 @@ public: */ virtual const char *get_ip_address() = 0; + /** Get the local MAC address + * + * @return Null-terminated representation of the local MAC address + */ + virtual const char *get_mac_address() = 0; + protected: friend class Socket; friend class UDPSocket; diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index dada562aec..21e4f10ca4 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -1,5 +1,5 @@ /* WiFiInterface - * Copyright (c) 2015 ARM Limited + * Copyright (c) 2015 - 2016 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,22 +17,19 @@ #ifndef WIFI_INTERFACE_H #define WIFI_INTERFACE_H +#include #include "network-socket/NetworkInterface.h" +typedef struct wifi_ap { + char ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ + uint8_t bssid[6]; + nsapi_security_t security; + int8_t rssi; + uint8_t channel; +} wifi_ap_t; -/** Enum of WiFi encryption types - * - * The security type specifies a particular security to use when - * connected to a WiFi network - * - * @enum nsapi_protocol_t - */ -enum nsapi_security_t { - NSAPI_SECURITY_NONE = 0, /*!< open access point */ - NSAPI_SECURITY_WEP, /*!< phrase conforms to WEP */ - NSAPI_SECURITY_WPA, /*!< phrase conforms to WPA */ - NSAPI_SECURITY_WPA2, /*!< phrase conforms to WPA2 */ -}; +typedef void (*wifi_ap_scan_cb_t)(wifi_ap_t *ap, void *data); +typedef void (*wifi_connect_cb_t)(nsapi_error_t res, void *data); /** WiFiInterface class * @@ -43,28 +40,81 @@ class WiFiInterface: public NetworkInterface public: /** Start the interface * - * Attempts to connect to a WiFi network. If passphrase is invalid, - * NSAPI_ERROR_AUTH_ERROR is returned. + * Attempts to connect to a WiFi network. * * @param ssid Name of the network to connect to * @param pass Security passphrase to connect to the network * @param security Type of encryption for connection - * @return 0 on success, negative error code on failure + * @param timeout Timeout in milliseconds; 0 for no timeout + * @return 0 on success, or error code on failure */ - virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE) = 0; + virtual nsapi_error_t connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, + unsigned timeout = 0) = 0; + + /** Start the interface + * + * Attempts to connect to a WiFi network asynchronously, the call will return straight away. If the @a cb was NULL + * you'll need to query @a get_state until it's in NSAPI_IF_STATE_CONNECTED state, otherwise the @a cb will be + * called with connection results. + * + * Note: @a ssid and @a pass must be kept until the connection is made, that is the callback has been called or the + * state changed to @a NSAPI_IF_STATE_CONNECTED as they are passed by value. + * + * @param ssid Name of the network to connect to + * @param pass Security passphrase to connect to the network + * @param security Type of encryption for connection + * @param cb Function to be called when the connect finishes + * @param data Arbitrary user data to pass to @a cb function + */ + virtual void connect_async(const char *ssid, const char *pass,nsapi_security_t security = NSAPI_SECURITY_NONE, + wifi_connect_cb_t cb = NULL, void *data = NULL) = 0; /** Stop the interface * - * @return 0 on success, negative error code on failure + * @return 0 on success, or error code on failure */ - virtual int disconnect() = 0; + virtual nsapi_error_t disconnect() = 0; /** Get the local MAC address * * @return Null-terminated representation of the local MAC address */ virtual const char *get_mac_address() = 0; + + /** Get the current WiFi interface state + * + * @returns Interface state enum + */ + virtual nsapi_if_state_t get_state() = 0; + + /** Gets the current radio signal strength for active connection + * + * @return Connection strength in dBm (negative value), or 0 if measurement impossible + */ + virtual int8_t get_rssi() = 0; + + /** Scan for available networks + * + * This function will block. + * + * @param ap Pointer to allocated array to store discovered AP + * @param count Size of allocated @a res array, or 0 to only count available AP + * @param timeout Timeout in milliseconds; 0 for no timeout + * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error + * see @a nsapi_error + */ + virtual int scan(wifi_ap_t *res, unsigned count, unsigned timeout = 0) = 0; + + /** Scan for available networks + * + * This function won't block, it'll return immediately and call provided callback for each discovered network with + * AP data and user @a data. After the last discovered network @a cb will be called with NULL as @a ap, so it + * will be always called at least once. + * + * @param cb Function to be called for every discovered network + * @param data A user handle that will be passed to @a cb along with the AP data + */ + virtual void scan_async(wifi_ap_scan_cb_t cb, void *data = NULL) = 0; }; - #endif diff --git a/features/net/network-socket/nsapi_types.h b/features/net/network-socket/nsapi_types.h index 669ef40311..5044a877e5 100644 --- a/features/net/network-socket/nsapi_types.h +++ b/features/net/network-socket/nsapi_types.h @@ -24,7 +24,7 @@ extern "C" { #endif -/** Enum of standardized error codes +/** Enum of standardized error codes * * Valid error codes have negative values and may * be returned by any network operation. @@ -32,6 +32,7 @@ extern "C" { * @enum nsapi_error_t */ typedef enum nsapi_error { + NSAPI_ERROR_OK = 0, /*!< no error */ NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */ NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */ @@ -41,10 +42,36 @@ typedef enum nsapi_error { NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */ NSAPI_ERROR_DNS_FAILURE = -3008, /*!< DNS failed to complete successfully */ NSAPI_ERROR_DHCP_FAILURE = -3009, /*!< DHCP failed to complete successfully */ - NSAPI_ERROR_AUTH_FAILURE = -3010, /*!< connection to access point faield */ - NSAPI_ERROR_DEVICE_ERROR = -3011, /*!< failure interfacing with the network procesor */ + NSAPI_ERROR_AUTH_FAILURE = -3010, /*!< connection to access point failed */ + NSAPI_ERROR_DEVICE_ERROR = -3011, /*!< failure interfacing with the network processor */ + NSAPI_ERROR_TIMEOUT = -3012, /*!< operation timed out */ + NSAPI_ERROR_BAD_SSID = -3013, /*!< ssid not found */ } nsapi_error_t; +/** Enum of encryption types + * + * The security type specifies a particular security to use when + * connected to a WiFi network + */ +typedef enum nsapi_security { + NSAPI_SECURITY_NONE = 0x0, /*!< open access point */ + NSAPI_SECURITY_WEP = 0x1, /*!< phrase conforms to WEP */ + NSAPI_SECURITY_WPA = 0x2, /*!< phrase conforms to WPA */ + NSAPI_SECURITY_WPA2 = 0x3, /*!< phrase conforms to WPA2 */ + NSAPI_SECURITY_WPA_WPA2 = 0x4, /*!< phrase conforms to WPA/WPA2 */ + NSAPI_SECURITY_UNSSUPPORTED = 0xFF, /*!< unknown/unsupported security in scan results */ +} nsapi_security_t; + +/** Enum of interface states + * + * List of all possible states a WiFi network interface can be in + */ +typedef enum nsapi_if_state { + NSAPI_IF_STATE_NOT_CONNECTED = 0x0, + NSAPI_IF_STATE_CONNECTING = 0x01, + NSAPI_IF_STATE_CONNECTED = 0x02, + NSAPI_IF_STATE_ERROR = 0xFF +} nsapi_if_state_t; /** Maximum size of IP address representation */ From ed0dfb48ea8f3d71d9a14bfb734b6604088b133d Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Thu, 15 Sep 2016 12:20:08 +0100 Subject: [PATCH 02/61] HAL: Add Emac interface --- features/net/network-socket/emac_stack_mem.h | 95 +++++++++++ hal/hal/emac_api.h | 158 +++++++++++++++++++ 2 files changed, 253 insertions(+) create mode 100644 features/net/network-socket/emac_stack_mem.h create mode 100644 hal/hal/emac_api.h diff --git a/features/net/network-socket/emac_stack_mem.h b/features/net/network-socket/emac_stack_mem.h new file mode 100644 index 0000000000..edf48b5464 --- /dev/null +++ b/features/net/network-socket/emac_stack_mem.h @@ -0,0 +1,95 @@ +/* mbed Microcontroller Library + * Copyright (c) 2016 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_EMAC_STACK_MEM_H +#define MBED_EMAC_STACK_MEM_H + +#include + +/** + * Stack memory module + * + * This interface provides abstraction for memory modules used in different IP stacks (often to accommodate zero copy). + * Emac interface may be required to accept output packets and provide received data using this stack specific API. + * This header should be implemented for each IP stack, so that we keep emacs module independent. + */ +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); + +/** + * 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); + +/** + * 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); + +/** + * 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); + +/** + * 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); + +/** + * 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); + +/** + * 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); + +#endif /* EMAC_MBED_STACK_MEM_h */ diff --git a/hal/hal/emac_api.h b/hal/hal/emac_api.h new file mode 100644 index 0000000000..9f1db4a1ec --- /dev/null +++ b/hal/hal/emac_api.h @@ -0,0 +1,158 @@ +/* mbed Microcontroller Library + * Copyright (c) 2016 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_EMAC_API_H +#define MBED_EMAC_API_H + +#include "platform.h" + +#if DEVICE_EMAC + +#include +#include "emac_stack_mem.h" + +typedef struct emac_interface emac_interface_t; + +/** + * EmacInterface + * + * This interface should be used to abstract low level access to networking hardware + */ + +/** + * Callback to be register with Emac interface and to be called fore received packets + * + * @param data Arbitrary user data (IP stack) + * @param buf Received data + */ +typedef void (*emac_link_input_fn)(void *data, emac_stack_mem_chain_t *buf); + +/** + * Callback to be register with Emac interface and to be called for link status changes + * + * @param data Arbitrary user data (IP stack) + * @param up Link status + */ +typedef void (*emac_link_state_change_fn)(void *data, bool up); + +/** + * Return maximum transmission unit + * + * @param emac Emac interface + * @return MTU in bytes + */ +typedef uint32_t (*emac_get_mtu_size_fn)(emac_interface_t *emac); + +/** + * Return interface name + * + * @param emac Emac interface + * @param name Pointer to where the name should be written + * @param size Maximum number of character to copy + */ +typedef void (*emac_get_ifname_fn)(emac_interface_t *emac, char *name, uint8_t size); + +/** + * Returns size of the underlying interface HW address size + * + * @param emac Emac interface + * @return HW address size in bytes + */ +typedef uint8_t (*emac_get_hwaddr_size_fn)(emac_interface_t *emac); + +/** + * Return interface hw address + * + * Copies HW address to provided memory, @param addr has to be of correct size see @a get_hwaddr_size + * + * @param emac Emac interface + * @param addr HW address for underlying interface + */ +typedef void (*emac_get_hwaddr_fn)(emac_interface_t *emac, uint8_t *addr); + +/** + * Set HW address for interface + * + * Provided address has to be of correct size, see @a get_hwaddr_size + * + * @param emac Emac interface + * @param addr Address to be set + */ +typedef void (*emac_set_hwaddr_fn)(emac_interface_t *emac, uint8_t *addr); + +/** + * Sends the packet over the link + * + * That can not be called from an interrupt context. + * + * @param emac Emac interface + * @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); + +/** + * Initializes the HW + * + * @return True on success, False in case of an error. + */ +typedef bool (*emac_power_up_fn)(emac_interface_t *emac); + +/** + * Deinitializes the HW + * + * @param emac Emac interface + */ +typedef void (*emac_power_down_fn)(emac_interface_t *emac); + +/** + * Sets a callback that needs to be called for packets received for that interface + * + * @param emac Emac interface + * @param input_cb Function to be register as a callback + * @param data Arbitrary user data to be passed to the callback + */ +typedef void (*emac_set_link_input_cb_fn)(emac_interface_t *emac, emac_link_input_fn input_cb, void *data); + +/** + * Sets a callback that needs to be called on link status changes for given interface + * + * @param emac Emac interface + * @param state_cb Function to be register as a callback + * @param data Arbitrary user data to be passed to the callback + */ +typedef void (*emac_set_link_state_cb_fn)(emac_interface_t *emac, emac_link_state_change_fn state_cb, void *data); + +typedef struct emac_interface_ops { + emac_get_mtu_size_fn get_mtu_size; + emac_get_ifname_fn get_ifname; + emac_get_hwaddr_size_fn get_hwaddr_size; + emac_get_hwaddr_fn get_hwaddr; + emac_set_hwaddr_fn set_hwaddr; + emac_link_out_fn link_out; + emac_power_up_fn power_up; + 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_interface_ops_t; + +typedef struct emac_interface { + const emac_interface_ops_t ops; + void *hw; +} emac_interface_t; + +#endif /* DEVICE_EMAC */ +#endif /* MBED_EMAC_API_H */ From ac8c2f96c0edef2a687e0a16b14c54b6e35f6a9a Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Mon, 19 Sep 2016 15:58:15 +0100 Subject: [PATCH 03/61] EMAC: Add emac_stack_t implementation for LWIP --- .../lwip-interface/emac_stack_lwip.cpp | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 features/net/FEATURE_IPV4/lwip-interface/emac_stack_lwip.cpp diff --git a/features/net/FEATURE_IPV4/lwip-interface/emac_stack_lwip.cpp b/features/net/FEATURE_IPV4/lwip-interface/emac_stack_lwip.cpp new file mode 100644 index 0000000000..8042a81d2e --- /dev/null +++ b/features/net/FEATURE_IPV4/lwip-interface/emac_stack_lwip.cpp @@ -0,0 +1,83 @@ +/* mbed Microcontroller Library + * Copyright (c) 2016 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 "platform.h" + +#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) +{ + + struct pbuf *pbuf = pbuf_alloc(PBUF_RAW, size + align, PBUF_RAM); + if (pbuf == NULL) { + return NULL; + } + + if (align) { + uint32_t remainder = (uint32_t)pbuf->payload % align; + uint32_t offset = align - remainder; + if (offset >= align) { + offset = align; + } + + pbuf->payload = (void*)((char*)pbuf->payload + offset); + pbuf->tot_len -= offset; + pbuf->len -= offset; + } + + return (emac_stack_mem_t*)pbuf; +} + +void emac_stack_mem_free(emac_stack_t* stack, emac_stack_mem_t *mem) +{ + pbuf_free((struct pbuf*)mem); +} + +void *emac_stack_mem_ptr(emac_stack_t* stack, 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) +{ + return ((struct pbuf*)mem)->len; +} + +void emac_stack_mem_set_len(emac_stack_t* stack, 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) +{ + struct pbuf **list = (struct pbuf**)chain; + struct pbuf *head = *list; + *list = (*list)->next; + + return (emac_stack_mem_t *)head; +} + +uint32_t emac_stack_mem_chain_len(emac_stack_t* stack, emac_stack_mem_chain_t *chain) +{ + return ((struct pbuf*)chain)->tot_len; +} + +#endif /* DEVICE_EMAC */ From 4a6728cb2a131c50e058f1935d244a1af686cfd0 Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Tue, 20 Sep 2016 16:17:00 +0100 Subject: [PATCH 04/61] EMAC: Add LWIP adaptation layer --- .../FEATURE_IPV4/lwip-interface/emac_lwip.c | 111 ++++++++++++++++++ .../FEATURE_IPV4/lwip-interface/eth_arch.h | 5 + .../FEATURE_IPV4/lwip-interface/lwip_stack.c | 4 + 3 files changed, 120 insertions(+) create mode 100644 features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c diff --git a/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c b/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c new file mode 100644 index 0000000000..bcec801095 --- /dev/null +++ b/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c @@ -0,0 +1,111 @@ +/* mbed Microcontroller Library + * Copyright (c) 2016 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 "platform.h" + +#if DEVICE_EMAC + +#include "emac_api.h" +#include "emac_stack_mem.h" +#include "lwip/tcpip.h" +#include "lwip/tcp.h" +#include "lwip/ip.h" +#include "netif/etharp.h" +#include "netif/ppp_oe.h" + +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); + + return ret ? ERR_OK : ERR_IF; +} + +static err_t emac_lwip_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr) +{ + /* Only send packets if the link is up */ + if (netif->flags & NETIF_FLAG_UP) { + return etharp_output(netif, q, ipaddr); + } + + return ERR_CONN; +} + +static void emac_lwip_input(void *data, emac_stack_t *buf) +{ + struct eth_hdr *ethhdr; + struct pbuf *p = (struct pbuf *)buf; + struct netif *netif = (struct netif *)data; + + /* pass all packets to ethernet_input, which decides what packets it supports */ + if (netif->input(p, netif) != ERR_OK) { + LWIP_DEBUGF(NETIF_DEBUG, ("Emac LWIP: IP input error\n")); + + pbuf_free(p); + } +} + +static void emac_lwip_state_change(void *data, bool up) +{ + struct netif *netif = (struct netif *)data; + + if (up) { + tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, netif, 1); + } else { + tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, netif, 1); + } +} + +err_t emac_lwip_if_init(struct netif *netif) +{ + int err = ERR_OK; + emac_interface_t *mac = (emac_interface_t *)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); + + netif->hwaddr_len = mac->ops.get_hwaddr_size(mac); + mac->ops.get_hwaddr(mac, netif->hwaddr); + + netif->mtu = mac->ops.get_mtu_size(mac); + + /* Interface capabilities */ + netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP; + + mac->ops.get_ifname(mac, netif->name, 2); + + netif->output = emac_lwip_output; + netif->linkoutput = emac_lwip_low_level_output; + + if (!mac->ops.power_up(mac)) { + err = ERR_IF; + } + + return err; +} + +/* That's a compatibility layer for lwip_stack.c, it's already done by emac_interface in power_up and power_down */ +void eth_arch_enable_interrupts(void) +{ + +} + +void eth_arch_disable_interrupts(void) +{ + +} + +#endif /* DEVICE_EMAC */ diff --git a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h index 1ff54b1d38..d334713636 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h +++ b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h @@ -31,7 +31,12 @@ extern "C" { void eth_arch_enable_interrupts(void); void eth_arch_disable_interrupts(void); + +#if DEVICE_EMAC +err_t emac_lwip_if_init(struct netif *netif); +#else /* DEVICE_EMAC */ err_t eth_arch_enetif_init(struct netif *netif); +#endif /* DEVICE_MAC */ #ifdef __cplusplus } diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c index 1e04808275..be1e88e733 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c +++ b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c @@ -157,7 +157,11 @@ int lwip_bringup(void) sys_arch_sem_wait(&lwip_tcpip_inited, 0); memset(&lwip_netif, 0, sizeof lwip_netif); +#if DEVICE_EMAC + netif_add(&lwip_netif, 0, 0, 0, &emac_if, emac_lwip_if_init, tcpip_input); +#else /* DEVICE_EMAC */ netif_add(&lwip_netif, 0, 0, 0, NULL, eth_arch_enetif_init, tcpip_input); +#endif /* DEVICE_EMAC */ netif_set_default(&lwip_netif); netif_set_link_callback (&lwip_netif, lwip_netif_link_irq); From ddd65b4089bc3187e9b522c160a3a8850e2ab00d Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Thu, 22 Sep 2016 11:32:53 +0100 Subject: [PATCH 05/61] WiFi: Add IPStackInterface abstraction for IP stacks --- .../lwip-interface/EthernetInterface.cpp | 2 +- .../lwip-interface/LWIPIPStack.cpp | 40 +++++++++++++ .../FEATURE_IPV4/lwip-interface/lwip_stack.c | 5 +- .../FEATURE_IPV4/lwip-interface/lwip_stack.h | 3 +- .../net/network-socket/IPStackInterface.h | 57 +++++++++++++++++++ 5 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp create mode 100644 features/net/network-socket/IPStackInterface.h diff --git a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp index ba40a801ff..3c89951346 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp @@ -21,7 +21,7 @@ /* Interface implementation */ int EthernetInterface::connect() { - return lwip_bringup(); + return lwip_bringup(NULL); } int EthernetInterface::disconnect() diff --git a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp new file mode 100644 index 0000000000..e4a907f090 --- /dev/null +++ b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp @@ -0,0 +1,40 @@ +/* LWIPIPStackInterface + * Copyright (c) 2015 - 2016 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 "IPStackInterface.h" +#include "lwip_stack.h" + +/** LWIP specific implementation of IPStackInterface */ + +void IPStackInterface::bringup(emac_interface_t *emac) +{ + lwip_bringup(emac); +} + +void IPStackInterface::bringdown() +{ + lwip_bringdown(); +} + +const char * IPStackInterface::get_mac_address() +{ + return lwip_get_mac_address(); +} + +const char * IPStackInterface::get_ip_address() +{ + return lwip_get_ip_address(); +} diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c index be1e88e733..4a35bb1102 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c +++ b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c @@ -30,6 +30,7 @@ #include "lwip/tcp.h" #include "lwip/ip.h" +#include "emac_api.h" /* Static arena of sockets */ static struct lwip_socket { @@ -142,7 +143,7 @@ const char *lwip_get_ip_address(void) return lwip_ip_addr[0] ? lwip_ip_addr : 0; } -int lwip_bringup(void) +int lwip_bringup(emac_interface_t *emac) { // Check if we've already connected if (!lwip_get_mac_address()) { @@ -158,7 +159,7 @@ int lwip_bringup(void) memset(&lwip_netif, 0, sizeof lwip_netif); #if DEVICE_EMAC - netif_add(&lwip_netif, 0, 0, 0, &emac_if, emac_lwip_if_init, tcpip_input); + netif_add(&lwip_netif, 0, 0, 0, emac, emac_lwip_if_init, tcpip_input); #else /* DEVICE_EMAC */ netif_add(&lwip_netif, 0, 0, 0, NULL, eth_arch_enetif_init, tcpip_input); #endif /* DEVICE_EMAC */ diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.h b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.h index f09e73a8e8..08d44e2dbe 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.h +++ b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.h @@ -18,6 +18,7 @@ #define LWIP_STACK_H #include "nsapi.h" +#include "emac_api.h" #ifdef __cplusplus extern "C" { @@ -25,7 +26,7 @@ extern "C" { // Access to lwip through the nsapi -int lwip_bringup(void); +int lwip_bringup(emac_interface_t *emac); void lwip_bringdown(void); extern nsapi_stack_t lwip_stack; diff --git a/features/net/network-socket/IPStackInterface.h b/features/net/network-socket/IPStackInterface.h new file mode 100644 index 0000000000..a407c7448d --- /dev/null +++ b/features/net/network-socket/IPStackInterface.h @@ -0,0 +1,57 @@ +/* IPStackInterface + * Copyright (c) 2015 - 2016 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 IP_STACK_INTERFACE_H +#define IP_STACK_INTERFACE_H + +#include "emac_api.h" + +/** + * IPStackInterface + * + * Abstraction class on top of IP stack, enables WiFiInterface implementation to setup IP stack + */ +class IPStackInterface +{ +public: + /** + * Brings up the IP stack using provided @a emac interface + * + * @param emac Emack backend to use + */ + virtual void bringup(emac_interface_t *emac); + + /** + * Brings down the IP stack + */ + virtual void bringdown(); + + /** + * Returns MAC address + * + * @return MAC address in "00:11:22:33:44:55" form + */ + virtual const char *get_mac_address(); + + /** + * Returns interfaces IP address + * + * @return IP address in "10.11.12.13" form + */ + virtual const char *get_ip_address(); +}; + +#endif /* IP_STACK_INTERFACE_H */ From aad3e1f6c5344d87c0520960834f322f4e68aeb4 Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Thu, 22 Sep 2016 11:49:02 +0100 Subject: [PATCH 06/61] WiFi: Extend WiFiInterface API and documentation * Add details to documentation * Add timeout for async functions * Add channel parameter to connect * Add wifi_ap_t to connection cb function --- features/net/network-socket/WiFiInterface.h | 31 ++++++++++++--------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index 21e4f10ca4..b928480004 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -29,7 +29,7 @@ typedef struct wifi_ap { } wifi_ap_t; typedef void (*wifi_ap_scan_cb_t)(wifi_ap_t *ap, void *data); -typedef void (*wifi_connect_cb_t)(nsapi_error_t res, void *data); +typedef void (*wifi_connect_cb_t)(nsapi_error_t res, wifi_ap_t *ap, void *data); /** WiFiInterface class * @@ -44,30 +44,34 @@ public: * * @param ssid Name of the network to connect to * @param pass Security passphrase to connect to the network - * @param security Type of encryption for connection - * @param timeout Timeout in milliseconds; 0 for no timeout + * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) + * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) + * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) * @return 0 on success, or error code on failure */ virtual nsapi_error_t connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, - unsigned timeout = 0) = 0; + uint8_t channel = 0, unsigned timeout = 0) = 0; /** Start the interface * * Attempts to connect to a WiFi network asynchronously, the call will return straight away. If the @a cb was NULL * you'll need to query @a get_state until it's in NSAPI_IF_STATE_CONNECTED state, otherwise the @a cb will be - * called with connection results. + * called with connection results, connected AP details and user data. * * Note: @a ssid and @a pass must be kept until the connection is made, that is the callback has been called or the * state changed to @a NSAPI_IF_STATE_CONNECTED as they are passed by value. * * @param ssid Name of the network to connect to * @param pass Security passphrase to connect to the network - * @param security Type of encryption for connection - * @param cb Function to be called when the connect finishes - * @param data Arbitrary user data to pass to @a cb function + * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) + * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) + * @param cb Function to be called when the connect finishes (Default: NULL) + * @param data Arbitrary user data to pass to @a cb function (Default: NULL) + * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) */ virtual void connect_async(const char *ssid, const char *pass,nsapi_security_t security = NSAPI_SECURITY_NONE, - wifi_connect_cb_t cb = NULL, void *data = NULL) = 0; + uint8_t channel = 0, wifi_connect_cb_t cb = NULL, void *data = NULL, + unsigned timeout = 0) = 0; /** Stop the interface * @@ -77,7 +81,7 @@ public: /** Get the local MAC address * - * @return Null-terminated representation of the local MAC address + * @return Null-terminated representation of the local MAC address in "00:11:22:33:44:55" form */ virtual const char *get_mac_address() = 0; @@ -99,7 +103,7 @@ public: * * @param ap Pointer to allocated array to store discovered AP * @param count Size of allocated @a res array, or 0 to only count available AP - * @param timeout Timeout in milliseconds; 0 for no timeout + * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error * see @a nsapi_error */ @@ -112,9 +116,10 @@ public: * will be always called at least once. * * @param cb Function to be called for every discovered network - * @param data A user handle that will be passed to @a cb along with the AP data + * @param data User handle that will be passed to @a cb along with the AP data (Default: NULL) + * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) */ - virtual void scan_async(wifi_ap_scan_cb_t cb, void *data = NULL) = 0; + virtual void scan_async(wifi_ap_scan_cb_t cb, void *data = NULL, unsigned timeout = 0) = 0; }; #endif From e0a48e9f6b13cf175c4a24417fa5463aa9dff795 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Tue, 27 Sep 2016 10:23:45 +0100 Subject: [PATCH 07/61] K64F: add EMAC device_has --- hal/targets.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hal/targets.json b/hal/targets.json index dec1aa6c3e..74a0005b32 100644 --- a/hal/targets.json +++ b/hal/targets.json @@ -565,7 +565,7 @@ "inherits": ["Target"], "progen": {"target": "frdm-k64f"}, "detect_code": ["0240"], - "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "STORAGE"], + "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "STORAGE", "EMAC"], "features": ["IPV4", "STORAGE"], "release_versions": ["2", "5"] }, From bd3a81d0ec7341131039ed959079b0ad1cdeda65 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Tue, 27 Sep 2016 10:38:06 +0100 Subject: [PATCH 08/61] lwip: add emac interrupt API ethernet uses eth_arch_xxx_interrupts, emac_xxx_enable_interrupts --- features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c | 4 ++-- features/net/FEATURE_IPV4/lwip-interface/eth_arch.h | 6 ++++-- features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c | 4 ++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c b/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c index bcec801095..a7baea1985 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c +++ b/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c @@ -98,12 +98,12 @@ err_t emac_lwip_if_init(struct netif *netif) } /* That's a compatibility layer for lwip_stack.c, it's already done by emac_interface in power_up and power_down */ -void eth_arch_enable_interrupts(void) +void emac_lwip_enable_interrupts(void) { } -void eth_arch_disable_interrupts(void) +void emac_lwip_disable_interrupts(void) { } diff --git a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h index d334713636..ceb7be4cbe 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h +++ b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h @@ -29,12 +29,14 @@ extern "C" { #endif -void eth_arch_enable_interrupts(void); -void eth_arch_disable_interrupts(void); #if DEVICE_EMAC err_t emac_lwip_if_init(struct netif *netif); +void emac_lwip_enable_interrupts(void); +void emac_lwip_disable_interrupts(void); #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 /* DEVICE_MAC */ diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c index e584a18d70..453a47782a 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c +++ b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c @@ -188,7 +188,11 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * netif_set_link_callback (&lwip_netif, lwip_netif_link_irq); netif_set_status_callback(&lwip_netif, lwip_netif_status_irq); +#if DEVICE_EMAC + emac_lwip_enable_interrupts(); +#else eth_arch_enable_interrupts(); +#endif } // Zero out socket set From febf1a8d55f541ea361568864150432f8bb67887 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Wed, 28 Sep 2016 09:24:12 +0100 Subject: [PATCH 09/61] lwipipstack: lwip_bringup arguments update It now accepts additional arguments, that lwipipstack do not use, thus all 0 and false for dhcp. --- features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp index e4a907f090..76abe5694d 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp @@ -21,7 +21,8 @@ void IPStackInterface::bringup(emac_interface_t *emac) { - lwip_bringup(emac); + // no dhcp at this stage, neither other details + lwip_bringup(emac, false, 0, 0, 0); } void IPStackInterface::bringdown() From b256b2fdc7e806a65a72412e5d41190e5b88afb2 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Wed, 28 Sep 2016 09:24:56 +0100 Subject: [PATCH 10/61] network-sockets: revert API for static configuration WifiInterface - add set_credentials --- .../net/network-socket/NetworkInterface.h | 6 ---- features/net/network-socket/WiFiInterface.h | 31 +++++++++++++------ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/features/net/network-socket/NetworkInterface.h b/features/net/network-socket/NetworkInterface.h index d43b3862eb..92e8eb4693 100644 --- a/features/net/network-socket/NetworkInterface.h +++ b/features/net/network-socket/NetworkInterface.h @@ -136,12 +136,6 @@ public: */ virtual int add_dns_server(const SocketAddress &address); - /** Get the local MAC address - * - * @return Null-terminated representation of the local MAC address - */ - virtual const char *get_mac_address() = 0; - protected: friend class Socket; friend class UDPSocket; diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index fcfba4578e..8046b3197b 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -38,6 +38,20 @@ typedef void (*wifi_connect_cb_t)(nsapi_error_t res, wifi_ap_t *ap, void *data); class WiFiInterface: public NetworkInterface { public: + /** WiFiInterface lifetime + */ + WiFiInterface(); + virtual ~WiFiInterface(); + + /** Set the WiFi network credentials + * + * @param ssid Name of the network to connect to + * @param pass Security passphrase to connect to the network + * @param security Type of encryption for connection + * (defaults to NSAPI_SECURITY_NONE) + */ + virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); + /** Start the interface * * Attempts to connect to a WiFi network. @@ -49,7 +63,7 @@ public: * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) * @return 0 on success, or error code on failure */ - virtual nsapi_error_t connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, + virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, uint8_t channel = 0, unsigned timeout = 0) = 0; /** Start the interface @@ -80,19 +94,13 @@ public: * * @return 0 on success, negative error code on failure */ - virtual int connect(); + virtual int connect() = 0; /** Stop the interface * * @return 0 on success, or error code on failure */ - virtual nsapi_error_t disconnect() = 0; - - /** Get the local MAC address - * - * @return Null-terminated representation of the local MAC address in "00:11:22:33:44:55" form - */ - virtual const char *get_mac_address() = 0; + virtual int disconnect() = 0; /** Get the current WiFi interface state * @@ -129,6 +137,11 @@ public: * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) */ virtual void scan_async(wifi_ap_scan_cb_t cb, void *data = NULL, unsigned timeout = 0) = 0; + +private: + char *_ssid; + char *_pass; + nsapi_security_t _security; }; #endif From 3d14c20224ca3b9be56018bb8d009c5a56cdd095 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Wed, 28 Sep 2016 09:54:59 +0100 Subject: [PATCH 11/61] WiFiInterface: remove connect implementation We do not want to mix Ethernet and Wifi at the moment, thus WiFiInterface should implement own connect using emac. --- features/net/network-socket/WiFiInterface.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/features/net/network-socket/WiFiInterface.cpp b/features/net/network-socket/WiFiInterface.cpp index 128220845d..e9c390d4e2 100644 --- a/features/net/network-socket/WiFiInterface.cpp +++ b/features/net/network-socket/WiFiInterface.cpp @@ -60,12 +60,3 @@ int WiFiInterface::set_credentials(const char *ssid, const char *pass, nsapi_sec return 0; } -int WiFiInterface::connect() -{ - if (!_ssid || !_pass) { - return NSAPI_ERROR_PARAMETER; - } - - return connect(_ssid, _pass, _security); -} - From 81932ee3c39d0692e444fafb520b03a3174775db Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Thu, 29 Sep 2016 09:49:51 +0100 Subject: [PATCH 12/61] lwip emac - remove non-existent header inclusion --- features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c | 1 - 1 file changed, 1 deletion(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c b/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c index a7baea1985..a63d7c7b91 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c +++ b/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c @@ -24,7 +24,6 @@ #include "lwip/tcp.h" #include "lwip/ip.h" #include "netif/etharp.h" -#include "netif/ppp_oe.h" static err_t emac_lwip_low_level_output(struct netif *netif, struct pbuf *p) { From 517f0509ca9bd61c37b62ac5eabdb4cf2eb0dd9a Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Thu, 29 Sep 2016 09:50:11 +0100 Subject: [PATCH 13/61] IPStackInterface - add ip address Currently only for IPv4. lwip was updated to accept 2 arguments for getting the ip address (buffer and length). --- features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp | 2 +- features/net/network-socket/IPStackInterface.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp index 76abe5694d..d6550e0191 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp @@ -37,5 +37,5 @@ const char * IPStackInterface::get_mac_address() const char * IPStackInterface::get_ip_address() { - return lwip_get_ip_address(); + return lwip_get_ip_address(_ip_address, sizeof(_ip_address)); } diff --git a/features/net/network-socket/IPStackInterface.h b/features/net/network-socket/IPStackInterface.h index a407c7448d..2a94d94415 100644 --- a/features/net/network-socket/IPStackInterface.h +++ b/features/net/network-socket/IPStackInterface.h @@ -52,6 +52,8 @@ public: * @return IP address in "10.11.12.13" form */ virtual const char *get_ip_address(); +private: + char _ip_address[16]; //IPv4 }; #endif /* IP_STACK_INTERFACE_H */ From 33d95654dff6a27c7c82c85b44401eafa984f9fb Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Thu, 29 Sep 2016 10:41:43 +0100 Subject: [PATCH 14/61] WifiInterface - use Callback class for callbacks --- features/net/network-socket/WiFiInterface.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index 8046b3197b..3e6c19f686 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -18,6 +18,7 @@ #define WIFI_INTERFACE_H #include +#include "Callback.h" #include "network-socket/NetworkInterface.h" typedef struct wifi_ap { @@ -28,9 +29,6 @@ typedef struct wifi_ap { uint8_t channel; } wifi_ap_t; -typedef void (*wifi_ap_scan_cb_t)(wifi_ap_t *ap, void *data); -typedef void (*wifi_connect_cb_t)(nsapi_error_t res, wifi_ap_t *ap, void *data); - /** WiFiInterface class * * Common interface that is shared between WiFi devices @@ -84,7 +82,7 @@ public: * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) */ virtual void connect_async(const char *ssid, const char *pass,nsapi_security_t security = NSAPI_SECURITY_NONE, - uint8_t channel = 0, wifi_connect_cb_t cb = NULL, void *data = NULL, + uint8_t channel = 0, mbed::Callback cb = NULL, void *data = NULL, unsigned timeout = 0) = 0; /** Start the interface @@ -136,7 +134,7 @@ public: * @param data User handle that will be passed to @a cb along with the AP data (Default: NULL) * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) */ - virtual void scan_async(wifi_ap_scan_cb_t cb, void *data = NULL, unsigned timeout = 0) = 0; + virtual void scan_async(mbed::Callback cb, void *data = NULL, unsigned timeout = 0) = 0; private: char *_ssid; From da7529801acb711d7e096f0087c2d11b1166116f Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Thu, 29 Sep 2016 10:44:43 +0100 Subject: [PATCH 15/61] WiFiInterface: change scope of wifi_ap_t structure --- features/net/network-socket/WiFiInterface.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index 3e6c19f686..6c89a12b7d 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -21,14 +21,6 @@ #include "Callback.h" #include "network-socket/NetworkInterface.h" -typedef struct wifi_ap { - char ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ - uint8_t bssid[6]; - nsapi_security_t security; - int8_t rssi; - uint8_t channel; -} wifi_ap_t; - /** WiFiInterface class * * Common interface that is shared between WiFi devices @@ -36,6 +28,14 @@ typedef struct wifi_ap { class WiFiInterface: public NetworkInterface { public: + typedef struct wifi_ap { + char ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ + uint8_t bssid[6]; + nsapi_security_t security; + int8_t rssi; + uint8_t channel; + } wifi_ap_t; + /** WiFiInterface lifetime */ WiFiInterface(); From 5ed97d5ccacf942de34cd14f6294a98282f3679e Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Thu, 29 Sep 2016 10:46:21 +0100 Subject: [PATCH 16/61] nsapi: error no ssid name correction --- features/net/network-socket/nsapi_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/net/network-socket/nsapi_types.h b/features/net/network-socket/nsapi_types.h index 519521e666..7bc6559ac2 100644 --- a/features/net/network-socket/nsapi_types.h +++ b/features/net/network-socket/nsapi_types.h @@ -45,7 +45,7 @@ typedef enum nsapi_error { NSAPI_ERROR_AUTH_FAILURE = -3010, /*!< connection to access point failed */ NSAPI_ERROR_DEVICE_ERROR = -3011, /*!< failure interfacing with the network processor */ NSAPI_ERROR_TIMEOUT = -3012, /*!< operation timed out */ - NSAPI_ERROR_BAD_SSID = -3013, /*!< ssid not found */ + NSAPI_ERROR_NO_SSID = -3013, /*!< ssid not found */ } nsapi_error_t; /** Enum of encryption types From a30aa8f21f37adabbe695688ffb3f302e3b625ac Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Tue, 20 Sep 2016 16:17:00 +0100 Subject: [PATCH 17/61] EMAC: Add LWIP adaptation layer --- features/net/FEATURE_IPV4/lwip-interface/eth_arch.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h index ceb7be4cbe..8688233ee1 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h +++ b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h @@ -37,6 +37,10 @@ void emac_lwip_disable_interrupts(void); #else /* DEVICE_EMAC */ void eth_arch_enable_interrupts(void); void eth_arch_disable_interrupts(void); + +#if DEVICE_EMAC +err_t emac_lwip_if_init(struct netif *netif) +#else /* DEVICE_EMAC */ err_t eth_arch_enetif_init(struct netif *netif); #endif /* DEVICE_MAC */ From 7a3a580a4bb6f82d27462c4dc2aacaa7b191a195 Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Thu, 22 Sep 2016 11:32:53 +0100 Subject: [PATCH 18/61] WiFi: Add IPStackInterface abstraction for IP stacks --- features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp index 125c53b5e0..52536e954e 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp @@ -22,6 +22,7 @@ EthernetInterface::EthernetInterface() : _dhcp(true), _ip_address(), _netmask(), _gateway() { + return lwip_bringup(NULL); } int EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway) From 26c3eef0283f5f4949054df3e0a3a61cb5912be8 Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Thu, 22 Sep 2016 18:29:17 +0200 Subject: [PATCH 19/61] Fixed return type for disconnect --- features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp index 52536e954e..e064514aec 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp @@ -50,7 +50,8 @@ int EthernetInterface::connect() int EthernetInterface::disconnect() { - return lwip_bringdown(); + lwip_bringdown(); + return 0; } const char *EthernetInterface::get_mac_address() From 05974a6578f11215d84ca0a71dd7cbe444976c0b Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Thu, 22 Sep 2016 18:29:43 +0200 Subject: [PATCH 20/61] Added EMAC --- hal/targets.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hal/targets.json b/hal/targets.json index 5acf8d4d8e..029c0dfa1d 100644 --- a/hal/targets.json +++ b/hal/targets.json @@ -1217,7 +1217,7 @@ "extra_labels": ["STM", "STM32F4", "STM32F439", "STM32F439ZI"], "macros": ["HSE_VALUE=24000000", "HSE_STARTUP_TIMEOUT=5000", "CB_INTERFACE_SDIO","CB_CHIP_WL18XX","SUPPORT_80211D_ALWAYS","WLAN_ENABLED"], "inherits": ["Target"], - "device_has": ["ANALOGIN", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"], + "device_has": ["ANALOGIN", "CAN", "EMAC", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"], "features": ["IPV4"], "release_versions": ["5"] }, From 40cd23c4f73cf2624dca92fb3f091d0bee60717d Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Thu, 22 Sep 2016 18:31:50 +0200 Subject: [PATCH 21/61] Added pointer for wifi_ap_t in wifi_connect_cb_t to be able to use NULL --- features/net/network-socket/WiFiInterface.h | 1 + 1 file changed, 1 insertion(+) diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index 6c89a12b7d..ae16049a45 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -20,6 +20,7 @@ #include #include "Callback.h" #include "network-socket/NetworkInterface.h" +#include "nsapi.h" /** WiFiInterface class * From b07d60259a7c9b919c2790b93e87011c632517cf Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Thu, 22 Sep 2016 18:32:30 +0200 Subject: [PATCH 22/61] Added missing ; --- features/net/FEATURE_IPV4/lwip-interface/eth_arch.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h index 8688233ee1..667d2543f5 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h +++ b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h @@ -39,7 +39,7 @@ void eth_arch_enable_interrupts(void); void eth_arch_disable_interrupts(void); #if DEVICE_EMAC -err_t emac_lwip_if_init(struct netif *netif) +err_t emac_lwip_if_init(struct netif *netif); #else /* DEVICE_EMAC */ err_t eth_arch_enetif_init(struct netif *netif); #endif /* DEVICE_MAC */ From 2ce9039a1a3516c78b2ac851a7737702072f5805 Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Thu, 22 Sep 2016 18:36:45 +0200 Subject: [PATCH 23/61] Added wifi_emac_api.cpp --- .../TARGET_UBLOX_C029/wifi_emac_api.cpp | 299 ++++++++++++++++++ 1 file changed, 299 insertions(+) create mode 100644 hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp b/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp new file mode 100644 index 0000000000..a777e977cd --- /dev/null +++ b/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp @@ -0,0 +1,299 @@ +#include +#include "cb_main.h" +#include "cb_wlan.h" +#include "cb_wlan_types.h" +#include "cb_otp.h" +#include "cb_wlan_target_data.h" +#include "emac_api.h" +#include "mbed_assert.h" +#include "rtos.h" + +/*=========================================================================== +* DEFINES +*=========================================================================*/ +#define WIFI_EMAC_API_MTU_SIZE (1500U) + +/*=========================================================================== +* TYPES +*=========================================================================*/ +typedef struct { + emac_link_input_fn _wifi_input_cb; + emac_link_state_change_fn _wifi_state_cb; + void* _link_input_user_data; + void* _link_state_user_data; + bool linkStateRegistered; + Mutex send_mutex; +} wifi_emac_api_s; + +/*=========================================================================== +* DEFINITIONS +*=========================================================================*/ +static wifi_emac_api_s _admin; +static const char ifname[] = "WL0"; + +/*=========================================================================== +* DECLARATIONS +*=========================================================================*/ +static void statusIndication(void *dummy, cbWLAN_StatusIndicationInfo status, void *data); +static void packetIndication(void *dummy, cbWLAN_PacketIndicationInfo *packetInfo); +static cb_boolean handleWlanTargetCopyFromDataFrame(uint8_t* buffer, cbWLANTARGET_dataFrame* frame, uint32_t size, uint32_t offsetInFrame); +static cb_boolean handleWlanTargetCopyToDataFrame(cbWLANTARGET_dataFrame* frame, uint8_t* buffer, uint32_t size, uint32_t offsetInFrame); +static cbWLANTARGET_dataFrame* handleWlanTargetAllocDataFrame(uint32_t size); +static void handleWlanTargetFreeDataFrame(cbWLANTARGET_dataFrame* frame); +static cb_uint32 handleWlanTargetGetDataFrameSize(cbWLANTARGET_dataFrame* frame); +static cb_uint8 handleWlanTargetGetDataFrameTID(cbWLANTARGET_dataFrame* frame); + +static const cbWLANTARGET_Callback _wlanTargetCallback = +{ + handleWlanTargetCopyFromDataFrame, + handleWlanTargetCopyToDataFrame, + handleWlanTargetAllocDataFrame, + handleWlanTargetFreeDataFrame, + handleWlanTargetGetDataFrameSize, + handleWlanTargetGetDataFrameTID +}; + +/*=========================================================================== +* FUNCTIONS +*=========================================================================*/ +static void statusIndication(void *dummy, cbWLAN_StatusIndicationInfo status, void *data) +{ + bool linkUp = false; + bool sendCb = true; + (void)dummy; + (void)data; + + switch (status) { + case cbWLAN_STATUS_CONNECTED: + case cbWLAN_STATUS_AP_STA_ADDED: + linkUp = true; + break; + case cbWLAN_STATUS_STOPPED: + case cbWLAN_STATUS_ERROR: + case cbWLAN_STATUS_DISCONNECTED: + case cbWLAN_STATUS_CONNECTION_FAILURE: + break; + case cbWLAN_STATUS_CONNECTING: + default: + sendCb = false; + break; + } + if (sendCb) { + _admin._wifi_state_cb(_admin._link_state_user_data, linkUp); + } +} + +static void packetIndication(void *dummy, cbWLAN_PacketIndicationInfo *packetInfo) +{ + (void)dummy; + _admin._wifi_input_cb(_admin._link_input_user_data, (void*)packetInfo); +} + +static cb_boolean handleWlanTargetCopyFromDataFrame(uint8_t* buffer, cbWLANTARGET_dataFrame* frame, uint32_t size, uint32_t offsetInFrame) +{ + void* dummy = NULL; + emac_stack_mem_t** phead = (emac_stack_mem_chain_t **)&frame; + emac_stack_mem_t* pbuf; + uint32_t copySize, bytesCopied = 0, pbufOffset = 0; + + MBED_ASSERT(frame != NULL); + MBED_ASSERT(buffer != NULL); + + pbuf = emac_stack_mem_chain_dequeue(dummy, phead); + while (pbuf != NULL) { + if ((pbufOffset + emac_stack_mem_len(dummy, pbuf)) >= offsetInFrame) { + copySize = cb_MIN(size, emac_stack_mem_len(dummy, pbuf) - (offsetInFrame - pbufOffset)); + memcpy(buffer, (int8_t *)emac_stack_mem_ptr(dummy, pbuf) + (offsetInFrame - pbufOffset), copySize); + buffer += copySize; + bytesCopied += copySize; + pbuf = emac_stack_mem_chain_dequeue(dummy, phead); + break; + } + pbufOffset += emac_stack_mem_len(dummy, pbuf); + pbuf = emac_stack_mem_chain_dequeue(dummy, phead); + } + + while (pbuf != NULL && bytesCopied < size) { + copySize = cb_MIN(emac_stack_mem_len(dummy, pbuf), size - bytesCopied); + memcpy(buffer, emac_stack_mem_ptr(dummy, pbuf), copySize); + buffer += copySize; + bytesCopied += copySize; + pbuf = emac_stack_mem_chain_dequeue(dummy, phead); + } + + MBED_ASSERT(bytesCopied <= size); + + return (bytesCopied == size); +} + +static cb_boolean handleWlanTargetCopyToDataFrame(cbWLANTARGET_dataFrame* frame, uint8_t* buffer, uint32_t size, uint32_t offsetInFrame) +{ + void* dummy = NULL; + emac_stack_mem_t** phead = (emac_stack_mem_chain_t **)&frame; + emac_stack_mem_t* pbuf; + uint32_t copySize, bytesCopied = 0, pbufOffset = 0; + + MBED_ASSERT(frame != NULL); + MBED_ASSERT(buffer != NULL); + + pbuf = emac_stack_mem_chain_dequeue(dummy, phead); + while (pbuf != NULL) { + if ((pbufOffset + emac_stack_mem_len(dummy, pbuf)) >= offsetInFrame) { + copySize = cb_MIN(size, emac_stack_mem_len(dummy, pbuf) - (offsetInFrame - pbufOffset)); + memcpy((uint8_t *)emac_stack_mem_ptr(dummy, pbuf) + (offsetInFrame - pbufOffset), buffer, copySize); + buffer += copySize; + bytesCopied += copySize; + pbuf = emac_stack_mem_chain_dequeue(dummy, phead); + break; + } + pbufOffset += emac_stack_mem_len(dummy, pbuf); + pbuf = emac_stack_mem_chain_dequeue(dummy, phead); + } + + while (pbuf != NULL && bytesCopied < size) { + copySize = cb_MIN(emac_stack_mem_len(dummy, pbuf), size - bytesCopied); + memcpy(emac_stack_mem_ptr(dummy, pbuf), buffer, copySize); + buffer += copySize; + bytesCopied += copySize; + pbuf = emac_stack_mem_chain_dequeue(dummy, phead); + } + + MBED_ASSERT(bytesCopied <= size); + + return (bytesCopied == size); +} + +static cbWLANTARGET_dataFrame* handleWlanTargetAllocDataFrame(uint32_t size) +{ + void* dummy = NULL; + + return (cbWLANTARGET_dataFrame*)emac_stack_mem_alloc(dummy, size, 0); +} + +static void handleWlanTargetFreeDataFrame(cbWLANTARGET_dataFrame* frame) +{ + void* dummy = NULL; + + emac_stack_mem_free(dummy, (emac_stack_mem_t*)frame); +} + +static uint32_t handleWlanTargetGetDataFrameSize(cbWLANTARGET_dataFrame* frame) +{ + void* dummy = NULL; + return emac_stack_mem_chain_len(dummy, (emac_stack_mem_t*)frame); +} + +static uint8_t handleWlanTargetGetDataFrameTID(cbWLANTARGET_dataFrame* frame) +{ + (void)frame; + return (uint8_t)cbWLAN_AC_BE; +} + +/*=========================================================================== +* API FUNCTIONS +*=========================================================================*/ +static uint32_t wifi_get_mtu_size(emac_interface_t *emac) +{ + (void)emac; + + return WIFI_EMAC_API_MTU_SIZE; +} + +static void wifi_get_ifname(emac_interface_t *emac, char *name, uint8_t size) +{ + (void)emac; + MBED_ASSERT(name != NULL); + memcpy((void*)name, (void*)&ifname, cb_MIN(size, sizeof(ifname))); +} + +static uint8_t wifi_get_hwaddr_size(emac_interface_t *emac) +{ + (void)emac; + + return sizeof(cbWLAN_MACAddress); +} + +static void wifi_get_hwaddr(emac_interface_t *emac, uint8_t *addr) +{ + (void)emac; + + cbOTP_read(cbOTP_MAC_WLAN, sizeof(cbWLAN_MACAddress), addr); +} + +static void wifi_set_hwaddr(emac_interface_t *emac, uint8_t *addr) +{ + (void)emac; + (void)addr; + //@TODO: shall be handled by wifi driver + //cbOTP_write(cbOTP_MAC_WLAN, sizeof(cbWLAN_MACAddress), addr); +} + +static bool wifi_link_out(emac_interface_t *emac, emac_stack_mem_t *buf) +{ + (void)emac; + _admin.send_mutex.lock(); + cbWLAN_sendPacket((void*)buf); + _admin.send_mutex.unlock(); + return true; +} + +static bool wifi_power_up(emac_interface_t *emac) +{ + (void)emac; + + cbWLANTARGET_registerCallbacks((cbWLANTARGET_Callback*)&_wlanTargetCallback); + return true; +} + +static void wifi_power_down(emac_interface_t *emac) +{ + (void)emac; +} + +static void wifi_set_link_input_cb(emac_interface_t *emac, emac_link_input_fn input_cb, void *data) +{ + void *dummy = NULL; + (void)emac; + + _admin._wifi_input_cb = input_cb; + _admin._link_input_user_data = data; + + cbWLAN_registerPacketIndicationCallback(packetIndication, dummy); +} + +static void wifi_set_link_state_cb(emac_interface_t *emac, emac_link_state_change_fn state_cb, void *data) +{ + cbRTSL_Status result; + void *dummy = NULL; + (void)emac; + + _admin._wifi_state_cb = state_cb; + _admin._link_state_user_data = data; + + if (!_admin.linkStateRegistered) { + result = cbWLAN_registerStatusCallback(statusIndication, dummy); + if (result == cbSTATUS_OK) { + _admin.linkStateRegistered = true; + } + } + +} + +const emac_interface_ops_t wifi_emac_interface = { + .get_mtu_size = wifi_get_mtu_size, + .get_ifname = wifi_get_ifname, + .get_hwaddr_size = wifi_get_hwaddr_size, + .get_hwaddr = wifi_get_hwaddr, + .set_hwaddr = wifi_set_hwaddr, + .link_out = wifi_link_out, + .power_up = wifi_power_up, + .power_down = wifi_power_down, + .set_link_input_cb = wifi_set_link_input_cb, + .set_link_state_cb = wifi_set_link_state_cb +}; + +//@TODO: add declaration in a suitable include file +void get_emac_interface_ops(const emac_interface_ops_t* emac_interface) +{ + emac_interface = &wifi_emac_interface; +} \ No newline at end of file From c80641fe11aca66f4d13ddea760ca245b8281b11 Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Fri, 23 Sep 2016 15:41:21 +0100 Subject: [PATCH 24/61] EMAC: Fix "#if DEVICE_EMAC " usage --- .../net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp | 4 ++++ features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp | 6 ++++++ features/net/network-socket/IPStackInterface.h | 6 ++++++ features/net/network-socket/emac_stack_mem.h | 6 ++++++ 4 files changed, 22 insertions(+) diff --git a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp index e064514aec..e19af12e83 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp @@ -22,7 +22,11 @@ EthernetInterface::EthernetInterface() : _dhcp(true), _ip_address(), _netmask(), _gateway() { +#if DEVICE_EMAC return lwip_bringup(NULL); +#else /* DEVICE_EMAC */ + return lwip_bringup(); +#endif /* DEVICE_EMAC */ } int EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway) diff --git a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp index d6550e0191..61a2f9f085 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp @@ -14,6 +14,10 @@ * limitations under the License. */ +#include "platform.h" + +#if DEVICE_EMAC + #include "IPStackInterface.h" #include "lwip_stack.h" @@ -39,3 +43,5 @@ const char * IPStackInterface::get_ip_address() { return lwip_get_ip_address(_ip_address, sizeof(_ip_address)); } + +#endif /* DEVICE_EMAC */ diff --git a/features/net/network-socket/IPStackInterface.h b/features/net/network-socket/IPStackInterface.h index 2a94d94415..7ad239eefb 100644 --- a/features/net/network-socket/IPStackInterface.h +++ b/features/net/network-socket/IPStackInterface.h @@ -17,6 +17,10 @@ #ifndef IP_STACK_INTERFACE_H #define IP_STACK_INTERFACE_H +#include "platform.h" + +#if DEVICE_EMAC + #include "emac_api.h" /** @@ -56,4 +60,6 @@ private: char _ip_address[16]; //IPv4 }; +#endif /* DEVICE_EMAC */ + #endif /* IP_STACK_INTERFACE_H */ diff --git a/features/net/network-socket/emac_stack_mem.h b/features/net/network-socket/emac_stack_mem.h index edf48b5464..5a4fbe3401 100644 --- a/features/net/network-socket/emac_stack_mem.h +++ b/features/net/network-socket/emac_stack_mem.h @@ -16,6 +16,10 @@ #ifndef MBED_EMAC_STACK_MEM_H #define MBED_EMAC_STACK_MEM_H +#include "platform.h" + +#if DEVICE_EMAC + #include /** @@ -92,4 +96,6 @@ emac_stack_mem_t *emac_stack_mem_chain_dequeue(emac_stack_t* stack, emac_stack_m */ uint32_t emac_stack_mem_chain_len(emac_stack_t* stack, emac_stack_mem_chain_t *chain); +#endif /* DEVICE_EMAC */ + #endif /* EMAC_MBED_STACK_MEM_h */ From f122695fd389fbec24464b70765110e382c5d1aa Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Mon, 26 Sep 2016 16:53:18 +0100 Subject: [PATCH 25/61] WiFi: Decuple IP stack bringup and IP address provisioning --- .../lwip-interface/LWIPIPStack.cpp | 10 ++++++++++ .../FEATURE_IPV4/lwip-interface/lwip_stack.c | 6 +----- features/net/network-socket/IPStackInterface.h | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp index 61a2f9f085..624fbb0924 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp @@ -34,6 +34,16 @@ void IPStackInterface::bringdown() lwip_bringdown(); } +int IPStackInterface::start_dhcp(uint timeout) +{ + return lwip_start_dhcp(timeout); +} + +int IPStackINterface::start_static_ip(const char *ip, const char *netmask, const char *gw) +{ + return lwip_start_static_ip(); +} + const char * IPStackInterface::get_mac_address() { return lwip_get_mac_address(); diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c index fdd5a1666a..fedefc9b49 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c +++ b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c @@ -403,7 +403,7 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * } // Zero out socket set - lwip_arena_init(); + lwip_arena_init(15000); #if LWIP_IPV6 netif_create_ip6_linklocal_address(&lwip_netif, 1/*from MAC*/); @@ -510,10 +510,6 @@ int lwip_bringdown(void) } #endif - lwip_connected = false; - // TO DO - actually remove addresses from stack, and shut down properly - return 0; -} /* LWIP error remapping */ static int lwip_err_remap(err_t err) { diff --git a/features/net/network-socket/IPStackInterface.h b/features/net/network-socket/IPStackInterface.h index 7ad239eefb..88c3a63bed 100644 --- a/features/net/network-socket/IPStackInterface.h +++ b/features/net/network-socket/IPStackInterface.h @@ -43,6 +43,24 @@ public: */ virtual void bringdown(); + /** + * Sends the dhcp request + * + * @param timeout Request timeout in ms + * @return NSAPI_ERROR_OK in case of success, error code otherwise + */ + virtual int start_dhcp(uint timeout = 15000); + + /** + * Starts the interface with static IP + * + * @param ip Static IP to use (in XYZ.XYZ.XYZ.XYZ format) + * @param netmask Network mask to use (in XYZ.XYZ.XYZ.XYZ format) + * @param gw Gateway IP address (in XYZ.XYZ.XYZ.XYZ format) + * @return NSAPI_ERROR_OK in case of success, error code otherwise + */ + virtual int start_static_ip(const char *ip, const char *netmask, const char *gw); + /** * Returns MAC address * From 8af75511cadaa4d9d66c7eaa2a80f485e05e230e Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Tue, 27 Sep 2016 11:20:26 +0200 Subject: [PATCH 26/61] Removed check since the interface is not fully up yet so we can't block DHCP/ARP --- features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c b/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c index a63d7c7b91..2d597485dc 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c +++ b/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c @@ -35,12 +35,7 @@ static err_t emac_lwip_low_level_output(struct netif *netif, struct pbuf *p) static err_t emac_lwip_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr) { - /* Only send packets if the link is up */ - if (netif->flags & NETIF_FLAG_UP) { - return etharp_output(netif, q, ipaddr); - } - - return ERR_CONN; + return etharp_output(netif, q, ipaddr); } static void emac_lwip_input(void *data, emac_stack_t *buf) From 604056184132b5a60923c81efdbc12fd82e45c75 Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Tue, 27 Sep 2016 11:43:23 +0200 Subject: [PATCH 27/61] Added start_dhcp and start_static_ip --- features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp index 624fbb0924..8d18db291c 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp @@ -39,7 +39,7 @@ int IPStackInterface::start_dhcp(uint timeout) return lwip_start_dhcp(timeout); } -int IPStackINterface::start_static_ip(const char *ip, const char *netmask, const char *gw) +int IPStackInterface::start_static_ip(const char *ip, const char *netmask, const char *gw) { return lwip_start_static_ip(); } From 841c9b5809a0931c80768a81ed72d29d0fd3d199 Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Tue, 27 Sep 2016 11:51:46 +0200 Subject: [PATCH 28/61] Fixed missing timeout argument for lwip_start_dhcp --- features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c index fedefc9b49..de9cbd2e3c 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c +++ b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c @@ -403,7 +403,7 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * } // Zero out socket set - lwip_arena_init(15000); + lwip_arena_init(); #if LWIP_IPV6 netif_create_ip6_linklocal_address(&lwip_netif, 1/*from MAC*/); From 31a1a7fe9bc8b935fb384651d1f7cfa428ac26b9 Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Tue, 27 Sep 2016 12:11:30 +0200 Subject: [PATCH 29/61] Fixed bug in packetIndication for packetInfo->rxData + use mutexes + cleanup --- .../TARGET_UBLOX_C029/wifi_emac_api.cpp | 92 +++++++++++-------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp b/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp index a777e977cd..b69e1a1fa8 100644 --- a/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp +++ b/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp @@ -17,20 +17,13 @@ * TYPES *=========================================================================*/ typedef struct { - emac_link_input_fn _wifi_input_cb; - emac_link_state_change_fn _wifi_state_cb; - void* _link_input_user_data; - void* _link_state_user_data; + emac_link_input_fn wifi_input_cb; + emac_link_state_change_fn wifi_state_cb; + void* link_input_user_data; + void* link_state_user_data; bool linkStateRegistered; - Mutex send_mutex; } wifi_emac_api_s; -/*=========================================================================== -* DEFINITIONS -*=========================================================================*/ -static wifi_emac_api_s _admin; -static const char ifname[] = "WL0"; - /*=========================================================================== * DECLARATIONS *=========================================================================*/ @@ -43,6 +36,38 @@ static void handleWlanTargetFreeDataFrame(cbWLANTARGET_dataFrame* frame); static cb_uint32 handleWlanTargetGetDataFrameSize(cbWLANTARGET_dataFrame* frame); static cb_uint8 handleWlanTargetGetDataFrameTID(cbWLANTARGET_dataFrame* frame); +static uint32_t wifi_get_mtu_size(emac_interface_t *emac); +static void wifi_get_ifname(emac_interface_t *emac, char *name, uint8_t size); +static uint8_t wifi_get_hwaddr_size(emac_interface_t *emac); +static void wifi_get_hwaddr(emac_interface_t *emac, uint8_t *addr); +static void wifi_set_hwaddr(emac_interface_t *emac, uint8_t *addr); +static bool wifi_link_out(emac_interface_t *emac, emac_stack_mem_t *buf); +static bool wifi_power_up(emac_interface_t *emac); +static void wifi_power_down(emac_interface_t *emac); +static void wifi_set_link_input_cb(emac_interface_t *emac, emac_link_input_fn input_cb, void *data); +static void wifi_set_link_state_cb(emac_interface_t *emac, emac_link_state_change_fn state_cb, void *data); + +/*=========================================================================== +* DEFINITIONS +*=========================================================================*/ +static wifi_emac_api_s _admin; +static const char _ifname[] = "WL0"; + +const emac_interface_ops_t wifi_emac_interface = { + .get_mtu_size = wifi_get_mtu_size, + .get_ifname = wifi_get_ifname, + .get_hwaddr_size = wifi_get_hwaddr_size, + .get_hwaddr = wifi_get_hwaddr, + .set_hwaddr = wifi_set_hwaddr, + .link_out = wifi_link_out, + .power_up = wifi_power_up, + .power_down = wifi_power_down, + .set_link_input_cb = wifi_set_link_input_cb, + .set_link_state_cb = wifi_set_link_state_cb +}; + +static emac_interface_t _intf = { wifi_emac_interface, NULL }; + static const cbWLANTARGET_Callback _wlanTargetCallback = { handleWlanTargetCopyFromDataFrame, @@ -79,14 +104,14 @@ static void statusIndication(void *dummy, cbWLAN_StatusIndicationInfo status, vo break; } if (sendCb) { - _admin._wifi_state_cb(_admin._link_state_user_data, linkUp); + _admin.wifi_state_cb(_admin.link_state_user_data, linkUp); } } static void packetIndication(void *dummy, cbWLAN_PacketIndicationInfo *packetInfo) { (void)dummy; - _admin._wifi_input_cb(_admin._link_input_user_data, (void*)packetInfo); + _admin.wifi_input_cb(_admin.link_input_user_data, (void*)packetInfo->rxData); } static cb_boolean handleWlanTargetCopyFromDataFrame(uint8_t* buffer, cbWLANTARGET_dataFrame* frame, uint32_t size, uint32_t offsetInFrame) @@ -203,7 +228,7 @@ static void wifi_get_ifname(emac_interface_t *emac, char *name, uint8_t size) { (void)emac; MBED_ASSERT(name != NULL); - memcpy((void*)name, (void*)&ifname, cb_MIN(size, sizeof(ifname))); + memcpy((void*)name, (void*)&_ifname, cb_MIN(size, sizeof(_ifname))); } static uint8_t wifi_get_hwaddr_size(emac_interface_t *emac) @@ -231,17 +256,20 @@ static void wifi_set_hwaddr(emac_interface_t *emac, uint8_t *addr) static bool wifi_link_out(emac_interface_t *emac, emac_stack_mem_t *buf) { (void)emac; - _admin.send_mutex.lock(); + cbMAIN_driverLock(); cbWLAN_sendPacket((void*)buf); - _admin.send_mutex.unlock(); + cbMAIN_driverUnlock(); return true; } + static bool wifi_power_up(emac_interface_t *emac) { (void)emac; + cbMAIN_driverLock(); cbWLANTARGET_registerCallbacks((cbWLANTARGET_Callback*)&_wlanTargetCallback); + cbMAIN_driverUnlock(); return true; } @@ -255,10 +283,12 @@ static void wifi_set_link_input_cb(emac_interface_t *emac, emac_link_input_fn in void *dummy = NULL; (void)emac; - _admin._wifi_input_cb = input_cb; - _admin._link_input_user_data = data; + _admin.wifi_input_cb = input_cb; + _admin.link_input_user_data = data; + cbMAIN_driverLock(); cbWLAN_registerPacketIndicationCallback(packetIndication, dummy); + cbMAIN_driverUnlock(); } static void wifi_set_link_state_cb(emac_interface_t *emac, emac_link_state_change_fn state_cb, void *data) @@ -267,11 +297,13 @@ static void wifi_set_link_state_cb(emac_interface_t *emac, emac_link_state_chang void *dummy = NULL; (void)emac; - _admin._wifi_state_cb = state_cb; - _admin._link_state_user_data = data; + _admin.wifi_state_cb = state_cb; + _admin.link_state_user_data = data; if (!_admin.linkStateRegistered) { + cbMAIN_driverLock(); result = cbWLAN_registerStatusCallback(statusIndication, dummy); + cbMAIN_driverUnlock(); if (result == cbSTATUS_OK) { _admin.linkStateRegistered = true; } @@ -279,21 +311,7 @@ static void wifi_set_link_state_cb(emac_interface_t *emac, emac_link_state_chang } -const emac_interface_ops_t wifi_emac_interface = { - .get_mtu_size = wifi_get_mtu_size, - .get_ifname = wifi_get_ifname, - .get_hwaddr_size = wifi_get_hwaddr_size, - .get_hwaddr = wifi_get_hwaddr, - .set_hwaddr = wifi_set_hwaddr, - .link_out = wifi_link_out, - .power_up = wifi_power_up, - .power_down = wifi_power_down, - .set_link_input_cb = wifi_set_link_input_cb, - .set_link_state_cb = wifi_set_link_state_cb -}; - -//@TODO: add declaration in a suitable include file -void get_emac_interface_ops(const emac_interface_ops_t* emac_interface) +emac_interface_t* get_emac_interface() { - emac_interface = &wifi_emac_interface; -} \ No newline at end of file + return &_intf; +} From f8ad018e14d18430448e632a9e8045e1f40a59dc Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Tue, 27 Sep 2016 12:13:41 +0200 Subject: [PATCH 30/61] Added --- .../TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.h | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.h diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.h b/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.h new file mode 100644 index 0000000000..4401c6dfb1 --- /dev/null +++ b/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.h @@ -0,0 +1,8 @@ +#include "emac_api.h" + +#ifndef WIFI_EMAC_API_H +#define WIFI_EMAC_API_H + +emac_interface_t* get_emac_interface(); + +#endif From 1b797bbd16c1882ecd2f1199fd1c08dab44bc24d Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Thu, 29 Sep 2016 14:38:45 +0100 Subject: [PATCH 31/61] EthernetInterface: ctor uses connect() method --- .../net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp index e19af12e83..1886742916 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp @@ -22,11 +22,7 @@ EthernetInterface::EthernetInterface() : _dhcp(true), _ip_address(), _netmask(), _gateway() { -#if DEVICE_EMAC - return lwip_bringup(NULL); -#else /* DEVICE_EMAC */ - return lwip_bringup(); -#endif /* DEVICE_EMAC */ + connect(); } int EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway) From d40c19bc733048f1caa5fb53c525991939958963 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Thu, 29 Sep 2016 14:44:03 +0100 Subject: [PATCH 32/61] lwipstack: add start dhcp and static ip functions --- .../lwip-interface/LWIPIPStack.cpp | 2 +- .../FEATURE_IPV4/lwip-interface/lwip_stack.c | 42 ++++++++++++++++++- .../FEATURE_IPV4/lwip-interface/lwip_stack.h | 3 ++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp index 8d18db291c..c55e41fa0f 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp @@ -41,7 +41,7 @@ int IPStackInterface::start_dhcp(uint timeout) int IPStackInterface::start_static_ip(const char *ip, const char *netmask, const char *gw) { - return lwip_start_static_ip(); + return lwip_start_static_ip(ip, netmask, gw); } const char * IPStackInterface::get_mac_address() diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c index de9cbd2e3c..17e711f272 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c +++ b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c @@ -509,7 +509,7 @@ int lwip_bringdown(void) netif_set_down(&lwip_netif); } #endif - +} /* LWIP error remapping */ static int lwip_err_remap(err_t err) { @@ -537,6 +537,46 @@ static int lwip_err_remap(err_t err) { } } +int lwip_start_dhcp(unsigned int timeout) +{ + err_t err = NSAPI_ERROR_DNS_FAILURE; +#if LWIP_IPV4 + err = dhcp_start(&lwip_netif); + if (err) { + return NSAPI_ERROR_DHCP_FAILURE; + } +#endif + + // If doesn't have address + if (!lwip_get_ip_addr(true, &lwip_netif)) { + err = sys_arch_sem_wait(&lwip_netif_has_addr, timeout); + if (err == SYS_ARCH_TIMEOUT) { + return NSAPI_ERROR_DHCP_FAILURE; + } + lwip_connected = true; + } + return err; +} + +int lwip_start_static_ip(const char *ip, const char *netmask, const char *gw) +{ + +#if LWIP_IPV4 + ip4_addr_t ip_addr; + ip4_addr_t netmask_addr; + ip4_addr_t gw_addr; + + if (!inet_aton(ip, &ip_addr) || + !inet_aton(netmask, &netmask_addr) || + !inet_aton(gw, &gw_addr)) { + return NSAPI_ERROR_PARAMETER; + } + + netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr); +#endif + +} + /* LWIP network stack implementation */ static int lwip_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version) { diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.h b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.h index bc185c5813..0ad822a659 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.h +++ b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.h @@ -34,6 +34,9 @@ char *lwip_get_ip_address(char *buf, int buflen); char *lwip_get_netmask(char *buf, int buflen); char *lwip_get_gateway(char *buf, int buflen); +int lwip_start_dhcp(unsigned int timeout); +int lwip_start_static_ip(const char *ip, const char *netmask, const char *gw); + extern nsapi_stack_t lwip_stack; From ac8f5d1b8cc77c3a561a0c7fa5abbd227418536d Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Thu, 29 Sep 2016 14:44:53 +0100 Subject: [PATCH 33/61] eth arch: missing endif fix --- features/net/FEATURE_IPV4/lwip-interface/eth_arch.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h index 667d2543f5..5e63831adc 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h +++ b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h @@ -37,6 +37,7 @@ void emac_lwip_disable_interrupts(void); #else /* DEVICE_EMAC */ void eth_arch_enable_interrupts(void); void eth_arch_disable_interrupts(void); +#endif #if DEVICE_EMAC err_t emac_lwip_if_init(struct netif *netif); @@ -49,4 +50,3 @@ err_t eth_arch_enetif_init(struct netif *netif); #endif #endif // #ifndef ETHARCHINTERFACE_H_ - From 780ec1ebe436bad24fee399604cc4630538b6013 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Thu, 29 Sep 2016 17:36:13 +0100 Subject: [PATCH 34/61] eth arch: duplication of declarations removal --- features/net/FEATURE_IPV4/lwip-interface/eth_arch.h | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h index 5e63831adc..30df529d7b 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h +++ b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h @@ -29,21 +29,16 @@ extern "C" { #endif - #if DEVICE_EMAC err_t emac_lwip_if_init(struct netif *netif); void emac_lwip_enable_interrupts(void); void emac_lwip_disable_interrupts(void); + #else /* DEVICE_EMAC */ void eth_arch_enable_interrupts(void); void eth_arch_disable_interrupts(void); -#endif - -#if DEVICE_EMAC -err_t emac_lwip_if_init(struct netif *netif); -#else /* DEVICE_EMAC */ err_t eth_arch_enetif_init(struct netif *netif); -#endif /* DEVICE_MAC */ +#endif #ifdef __cplusplus } From 7c8afa91fb03bee5584884914db9a0aeb6df48ae Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Thu, 29 Sep 2016 18:01:38 +0100 Subject: [PATCH 35/61] IPStackInterface: removal of this layer Use NetworkInterface that defines the same API (extended to cover some additional requirements that comes from wifi). --- .../lwip-interface/LWIPIPStack.cpp | 57 ------------- .../net/network-socket/IPStackInterface.h | 83 ------------------- .../net/network-socket/NetworkInterface.h | 19 +++++ features/net/network-socket/WiFiInterface.h | 1 + 4 files changed, 20 insertions(+), 140 deletions(-) delete mode 100644 features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp delete mode 100644 features/net/network-socket/IPStackInterface.h diff --git a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp b/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp deleted file mode 100644 index c55e41fa0f..0000000000 --- a/features/net/FEATURE_IPV4/lwip-interface/LWIPIPStack.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/* LWIPIPStackInterface - * Copyright (c) 2015 - 2016 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 "platform.h" - -#if DEVICE_EMAC - -#include "IPStackInterface.h" -#include "lwip_stack.h" - -/** LWIP specific implementation of IPStackInterface */ - -void IPStackInterface::bringup(emac_interface_t *emac) -{ - // no dhcp at this stage, neither other details - lwip_bringup(emac, false, 0, 0, 0); -} - -void IPStackInterface::bringdown() -{ - lwip_bringdown(); -} - -int IPStackInterface::start_dhcp(uint timeout) -{ - return lwip_start_dhcp(timeout); -} - -int IPStackInterface::start_static_ip(const char *ip, const char *netmask, const char *gw) -{ - return lwip_start_static_ip(ip, netmask, gw); -} - -const char * IPStackInterface::get_mac_address() -{ - return lwip_get_mac_address(); -} - -const char * IPStackInterface::get_ip_address() -{ - return lwip_get_ip_address(_ip_address, sizeof(_ip_address)); -} - -#endif /* DEVICE_EMAC */ diff --git a/features/net/network-socket/IPStackInterface.h b/features/net/network-socket/IPStackInterface.h deleted file mode 100644 index 88c3a63bed..0000000000 --- a/features/net/network-socket/IPStackInterface.h +++ /dev/null @@ -1,83 +0,0 @@ -/* IPStackInterface - * Copyright (c) 2015 - 2016 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 IP_STACK_INTERFACE_H -#define IP_STACK_INTERFACE_H - -#include "platform.h" - -#if DEVICE_EMAC - -#include "emac_api.h" - -/** - * IPStackInterface - * - * Abstraction class on top of IP stack, enables WiFiInterface implementation to setup IP stack - */ -class IPStackInterface -{ -public: - /** - * Brings up the IP stack using provided @a emac interface - * - * @param emac Emack backend to use - */ - virtual void bringup(emac_interface_t *emac); - - /** - * Brings down the IP stack - */ - virtual void bringdown(); - - /** - * Sends the dhcp request - * - * @param timeout Request timeout in ms - * @return NSAPI_ERROR_OK in case of success, error code otherwise - */ - virtual int start_dhcp(uint timeout = 15000); - - /** - * Starts the interface with static IP - * - * @param ip Static IP to use (in XYZ.XYZ.XYZ.XYZ format) - * @param netmask Network mask to use (in XYZ.XYZ.XYZ.XYZ format) - * @param gw Gateway IP address (in XYZ.XYZ.XYZ.XYZ format) - * @return NSAPI_ERROR_OK in case of success, error code otherwise - */ - virtual int start_static_ip(const char *ip, const char *netmask, const char *gw); - - /** - * Returns MAC address - * - * @return MAC address in "00:11:22:33:44:55" form - */ - virtual const char *get_mac_address(); - - /** - * Returns interfaces IP address - * - * @return IP address in "10.11.12.13" form - */ - virtual const char *get_ip_address(); -private: - char _ip_address[16]; //IPv4 -}; - -#endif /* DEVICE_EMAC */ - -#endif /* IP_STACK_INTERFACE_H */ diff --git a/features/net/network-socket/NetworkInterface.h b/features/net/network-socket/NetworkInterface.h index 92e8eb4693..8d3cef8080 100644 --- a/features/net/network-socket/NetworkInterface.h +++ b/features/net/network-socket/NetworkInterface.h @@ -19,6 +19,7 @@ #include "network-socket/nsapi_types.h" #include "network-socket/SocketAddress.h" +#include "emac_api.h" // Predeclared class class NetworkStack; @@ -94,6 +95,24 @@ public: */ virtual int connect() = 0; + /** + * Sends the dhcp request + * + * @param timeout Request timeout in ms + * @return NSAPI_ERROR_OK in case of success, error code otherwise + */ + virtual int start_dhcp(uint timeout = 15000) = 0; + + /** + * Starts the interface with static IP + * + * @param ip Static IP to use (in XYZ.XYZ.XYZ.XYZ format) + * @param netmask Network mask to use (in XYZ.XYZ.XYZ.XYZ format) + * @param gw Gateway IP address (in XYZ.XYZ.XYZ.XYZ format) + * @return NSAPI_ERROR_OK in case of success, error code otherwise + */ + virtual int start_static_ip(const char *ip, const char *netmask, const char *gw) = 0; + /** Stop the interface * * @return 0 on success, negative error code on failure diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index ae16049a45..cf8b9c5c98 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -141,6 +141,7 @@ private: char *_ssid; char *_pass; nsapi_security_t _security; + char _ip_address[16]; //IPv4 only thus 16 }; #endif From 961cde39af877d44e6b9fff3768f14aaa85cc810 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 30 Sep 2016 08:51:16 +0100 Subject: [PATCH 36/61] WiFiInterface: change variables to protected A wifi instance should be able to set those data. --- features/net/network-socket/WiFiInterface.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index cf8b9c5c98..5675fa46ee 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -137,7 +137,7 @@ public: */ virtual void scan_async(mbed::Callback cb, void *data = NULL, unsigned timeout = 0) = 0; -private: +protected: char *_ssid; char *_pass; nsapi_security_t _security; From bad51a56a11b803ca93bb51d9bdd136681727f74 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 30 Sep 2016 09:42:14 +0100 Subject: [PATCH 37/61] targets: remove emac for k64f, not supported yet --- hal/targets.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hal/targets.json b/hal/targets.json index 029c0dfa1d..a4e053275d 100644 --- a/hal/targets.json +++ b/hal/targets.json @@ -565,7 +565,7 @@ "inherits": ["Target"], "progen": {"target": "frdm-k64f"}, "detect_code": ["0240"], - "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "STORAGE", "TRNG", "EMAC"], + "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "STORAGE", "TRNG"], "features": ["IPV4", "STORAGE"], "release_versions": ["2", "5"] }, From 372f8e4a8c2b48e164a117bf1850d20b38cbaf38 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 30 Sep 2016 09:59:48 +0100 Subject: [PATCH 38/61] lwip stack: dhcp in bringup not done for emac targets --- features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c index 17e711f272..be0e1bb170 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c +++ b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c @@ -455,6 +455,7 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * netif_set_up(&lwip_netif); +#if !DEVICE_EMAC #if LWIP_IPV4 // Connect to the network lwip_dhcp = dhcp; @@ -476,6 +477,7 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * } lwip_connected = true; } +#endif /* DEVICE_EMAC */ #if ADDR_TIMEOUT // If address is not for preferred stack waits a while to see From ad8b52fe852f2f7de355330e0b1cf970390e857e Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 30 Sep 2016 10:48:22 +0100 Subject: [PATCH 39/61] EthernetInterface: revert unrelated changes --- .../net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp index 1886742916..125c53b5e0 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp +++ b/features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp @@ -22,7 +22,6 @@ EthernetInterface::EthernetInterface() : _dhcp(true), _ip_address(), _netmask(), _gateway() { - connect(); } int EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway) @@ -50,8 +49,7 @@ int EthernetInterface::connect() int EthernetInterface::disconnect() { - lwip_bringdown(); - return 0; + return lwip_bringdown(); } const char *EthernetInterface::get_mac_address() From e31f198810292fe0370066f2610e8559a0ea72be Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 30 Sep 2016 10:57:49 +0100 Subject: [PATCH 40/61] lwip_stack: remove emac int enable/disable functions Already done in emac interface. --- features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c | 11 ----------- features/net/FEATURE_IPV4/lwip-interface/eth_arch.h | 2 -- features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c | 4 +--- 3 files changed, 1 insertion(+), 16 deletions(-) diff --git a/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c b/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c index 2d597485dc..b59015c4ba 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c +++ b/features/net/FEATURE_IPV4/lwip-interface/emac_lwip.c @@ -91,15 +91,4 @@ err_t emac_lwip_if_init(struct netif *netif) return err; } -/* That's a compatibility layer for lwip_stack.c, it's already done by emac_interface in power_up and power_down */ -void emac_lwip_enable_interrupts(void) -{ - -} - -void emac_lwip_disable_interrupts(void) -{ - -} - #endif /* DEVICE_EMAC */ diff --git a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h index 30df529d7b..25bdde38dd 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h +++ b/features/net/FEATURE_IPV4/lwip-interface/eth_arch.h @@ -31,8 +31,6 @@ extern "C" { #if DEVICE_EMAC err_t emac_lwip_if_init(struct netif *netif); -void emac_lwip_enable_interrupts(void); -void emac_lwip_disable_interrupts(void); #else /* DEVICE_EMAC */ void eth_arch_enable_interrupts(void); diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c index be0e1bb170..c25b62f826 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c +++ b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c @@ -395,9 +395,7 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * netif_set_link_callback (&lwip_netif, lwip_netif_link_irq); netif_set_status_callback(&lwip_netif, lwip_netif_status_irq); -#if DEVICE_EMAC - emac_lwip_enable_interrupts(); -#else +#if !DEVICE_EMAC eth_arch_enable_interrupts(); #endif } From ebe02274fff2c0d251c8d74fdd7b0beafcb501a8 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 30 Sep 2016 11:10:59 +0100 Subject: [PATCH 41/61] NetworkInterface- fix comments for dhcp and ip methods --- features/net/network-socket/NetworkInterface.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/features/net/network-socket/NetworkInterface.h b/features/net/network-socket/NetworkInterface.h index 8d3cef8080..f280bab9ee 100644 --- a/features/net/network-socket/NetworkInterface.h +++ b/features/net/network-socket/NetworkInterface.h @@ -95,16 +95,14 @@ public: */ virtual int connect() = 0; - /** - * Sends the dhcp request + /** Sends DHCP request * * @param timeout Request timeout in ms * @return NSAPI_ERROR_OK in case of success, error code otherwise */ virtual int start_dhcp(uint timeout = 15000) = 0; - /** - * Starts the interface with static IP + /** Starts the interface with static IP * * @param ip Static IP to use (in XYZ.XYZ.XYZ.XYZ format) * @param netmask Network mask to use (in XYZ.XYZ.XYZ.XYZ format) From e7b7fecc471b220c952ea6acc972198c98b8cf92 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 30 Sep 2016 11:33:35 +0100 Subject: [PATCH 42/61] NetworkInterface: fix start_dhcp argument type --- features/net/network-socket/NetworkInterface.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/features/net/network-socket/NetworkInterface.h b/features/net/network-socket/NetworkInterface.h index f280bab9ee..7e6ef4d0cc 100644 --- a/features/net/network-socket/NetworkInterface.h +++ b/features/net/network-socket/NetworkInterface.h @@ -19,7 +19,6 @@ #include "network-socket/nsapi_types.h" #include "network-socket/SocketAddress.h" -#include "emac_api.h" // Predeclared class class NetworkStack; @@ -100,7 +99,7 @@ public: * @param timeout Request timeout in ms * @return NSAPI_ERROR_OK in case of success, error code otherwise */ - virtual int start_dhcp(uint timeout = 15000) = 0; + virtual int start_dhcp(unsigned int timeout = 15000) = 0; /** Starts the interface with static IP * From 0fe1246bf4fab40d96fe5616b19da174eeaf362d Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 30 Sep 2016 18:22:26 +0100 Subject: [PATCH 43/61] WiFiInterface - remove data arguments Callback class provides API to support class callbacks. --- features/net/network-socket/WiFiInterface.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index 5675fa46ee..0ce51d462f 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -79,11 +79,10 @@ public: * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) * @param cb Function to be called when the connect finishes (Default: NULL) - * @param data Arbitrary user data to pass to @a cb function (Default: NULL) * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) */ virtual void connect_async(const char *ssid, const char *pass,nsapi_security_t security = NSAPI_SECURITY_NONE, - uint8_t channel = 0, mbed::Callback cb = NULL, void *data = NULL, + uint8_t channel = 0, mbed::Callback cb = NULL, unsigned timeout = 0) = 0; /** Start the interface @@ -135,7 +134,7 @@ public: * @param data User handle that will be passed to @a cb along with the AP data (Default: NULL) * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) */ - virtual void scan_async(mbed::Callback cb, void *data = NULL, unsigned timeout = 0) = 0; + virtual void scan_async(mbed::Callback cb, unsigned timeout = 0) = 0; protected: char *_ssid; From 63f0ff1b497c51e8a153305518912ce631a5bb2c Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 30 Sep 2016 18:36:02 +0100 Subject: [PATCH 44/61] NetworkInterface: remove dhcp and static ip methods These use cases are already supported by NetworkInterface class via set_dhcp, and set_network. --- features/net/network-socket/NetworkInterface.h | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/features/net/network-socket/NetworkInterface.h b/features/net/network-socket/NetworkInterface.h index 7e6ef4d0cc..92e8eb4693 100644 --- a/features/net/network-socket/NetworkInterface.h +++ b/features/net/network-socket/NetworkInterface.h @@ -94,22 +94,6 @@ public: */ virtual int connect() = 0; - /** Sends DHCP request - * - * @param timeout Request timeout in ms - * @return NSAPI_ERROR_OK in case of success, error code otherwise - */ - virtual int start_dhcp(unsigned int timeout = 15000) = 0; - - /** Starts the interface with static IP - * - * @param ip Static IP to use (in XYZ.XYZ.XYZ.XYZ format) - * @param netmask Network mask to use (in XYZ.XYZ.XYZ.XYZ format) - * @param gw Gateway IP address (in XYZ.XYZ.XYZ.XYZ format) - * @return NSAPI_ERROR_OK in case of success, error code otherwise - */ - virtual int start_static_ip(const char *ip, const char *netmask, const char *gw) = 0; - /** Stop the interface * * @return 0 on success, negative error code on failure From 56a223e3f4fb14a55cad28da1d64830cc94b8098 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 30 Sep 2016 19:00:04 +0100 Subject: [PATCH 45/61] emac interface - typedef should be available for non-emac targets As it's required by lwip_bringup function --- hal/hal/emac_api.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hal/hal/emac_api.h b/hal/hal/emac_api.h index 9f1db4a1ec..bebd56f707 100644 --- a/hal/hal/emac_api.h +++ b/hal/hal/emac_api.h @@ -19,6 +19,7 @@ #include "platform.h" + #if DEVICE_EMAC #include @@ -154,5 +155,9 @@ typedef struct emac_interface { void *hw; } emac_interface_t; +#else + +typedef void *emac_interface_t; + #endif /* DEVICE_EMAC */ #endif /* MBED_EMAC_API_H */ From cca026dc6d23775de996071c49401d4127b931d6 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 30 Sep 2016 18:55:34 +0100 Subject: [PATCH 46/61] WiFiInterface - revert removal of connect method --- features/net/network-socket/WiFiInterface.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/features/net/network-socket/WiFiInterface.cpp b/features/net/network-socket/WiFiInterface.cpp index e9c390d4e2..77fd06563b 100644 --- a/features/net/network-socket/WiFiInterface.cpp +++ b/features/net/network-socket/WiFiInterface.cpp @@ -60,3 +60,11 @@ int WiFiInterface::set_credentials(const char *ssid, const char *pass, nsapi_sec return 0; } +int WiFiInterface::connect() +{ + if (!_ssid || !_pass) { + return NSAPI_ERROR_PARAMETER; + } + + return connect(_ssid, _pass, _security); +} From 5b1253907475b58d29b7b6009280beb4912da38b Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 30 Sep 2016 15:28:43 -0500 Subject: [PATCH 47/61] wifi - Removed asynchronous functions Current asynchronous functions do not match the blocking/attach pattern of the current codebase. Unfortunately, the asynchronous api can not be updated in the current time constraints. The async functions will be removed for now and can be reconsidered at a later date. --- features/net/network-socket/WiFiInterface.cpp | 70 -------------- features/net/network-socket/WiFiInterface.h | 93 ++++++------------- features/net/network-socket/nsapi_types.h | 22 +---- 3 files changed, 35 insertions(+), 150 deletions(-) delete mode 100644 features/net/network-socket/WiFiInterface.cpp diff --git a/features/net/network-socket/WiFiInterface.cpp b/features/net/network-socket/WiFiInterface.cpp deleted file mode 100644 index 77fd06563b..0000000000 --- a/features/net/network-socket/WiFiInterface.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* Socket - * 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 "network-socket/WiFiInterface.h" -#include -#include - - -WiFiInterface::WiFiInterface() - : _ssid(0), _pass(0), _security(NSAPI_SECURITY_NONE) -{ -} - -WiFiInterface::~WiFiInterface() -{ - free(_ssid); - free(_pass); -} - -int WiFiInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) -{ - free(_ssid); - _ssid = 0; - free(_pass); - _pass = 0; - - if (ssid) { - _ssid = (char*)malloc(strlen(ssid)+1); - if (!_ssid) { - return NSAPI_ERROR_NO_MEMORY; - } - - strcpy(_ssid, ssid); - } - - if (pass) { - _pass = (char*)malloc(strlen(pass)+1); - if (!_pass) { - return NSAPI_ERROR_NO_MEMORY; - } - - strcpy(_pass, pass); - } - - _security = security; - - return 0; -} - -int WiFiInterface::connect() -{ - if (!_ssid || !_pass) { - return NSAPI_ERROR_PARAMETER; - } - - return connect(_ssid, _pass, _security); -} diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index 0ce51d462f..f997e63a60 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -39,17 +39,31 @@ public: /** WiFiInterface lifetime */ - WiFiInterface(); - virtual ~WiFiInterface(); + virtual ~WiFiInterface() {}; /** Set the WiFi network credentials * - * @param ssid Name of the network to connect to - * @param pass Security passphrase to connect to the network - * @param security Type of encryption for connection - * (defaults to NSAPI_SECURITY_NONE) + * @param ssid Name of the network to connect to + * @param pass Security passphrase to connect to the network + * @param security Type of encryption for connection + * (defaults to NSAPI_SECURITY_NONE) + * @return 0 on success, or error code on failure */ - virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); + virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE) = 0; + + /** Set the WiFi network channel + * + * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) + * @return 0 on success, or error code on failure + */ + virtual int set_channel(uint8_t channel) = 0; + + /** Gets the current radio signal strength for active connection + * + * @return Connection strength in dBm (negative value), + * or 0 if measurement impossible + */ + virtual int8_t get_rssi() = 0; /** Start the interface * @@ -59,31 +73,11 @@ public: * @param pass Security passphrase to connect to the network * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) - * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) * @return 0 on success, or error code on failure */ - virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, - uint8_t channel = 0, unsigned timeout = 0) = 0; - - /** Start the interface - * - * Attempts to connect to a WiFi network asynchronously, the call will return straight away. If the @a cb was NULL - * you'll need to query @a get_state until it's in NSAPI_IF_STATE_CONNECTED state, otherwise the @a cb will be - * called with connection results, connected AP details and user data. - * - * Note: @a ssid and @a pass must be kept until the connection is made, that is the callback has been called or the - * state changed to @a NSAPI_IF_STATE_CONNECTED as they are passed by value. - * - * @param ssid Name of the network to connect to - * @param pass Security passphrase to connect to the network - * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) - * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) - * @param cb Function to be called when the connect finishes (Default: NULL) - * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) - */ - virtual void connect_async(const char *ssid, const char *pass,nsapi_security_t security = NSAPI_SECURITY_NONE, - uint8_t channel = 0, mbed::Callback cb = NULL, - unsigned timeout = 0) = 0; + virtual int connect(const char *ssid, const char *pass, + nsapi_security_t security = NSAPI_SECURITY_NONE, + uint8_t channel = 0); /** Start the interface * @@ -96,25 +90,16 @@ public: /** Stop the interface * - * @return 0 on success, or error code on failure + * @return 0 on success, or error code on failure */ virtual int disconnect() = 0; - /** Get the current WiFi interface state - * - * @returns Interface state enum - */ - virtual nsapi_if_state_t get_state() = 0; - - /** Gets the current radio signal strength for active connection - * - * @return Connection strength in dBm (negative value), or 0 if measurement impossible - */ - virtual int8_t get_rssi() = 0; - /** Scan for available networks * - * This function will block. + * The scan will + * If the network interface is set to non-blocking mode, scan will attempt to scan + * for WiFi networks asynchronously and return NSAPI_ERROR_WOULD_BLOCK. If a callback + * is attached, the callback will be called when the operation has completed. * * @param ap Pointer to allocated array to store discovered AP * @param count Size of allocated @a res array, or 0 to only count available AP @@ -122,25 +107,7 @@ public: * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error * see @a nsapi_error */ - virtual int scan(wifi_ap_t *res, unsigned count, unsigned timeout = 0) = 0; - - /** Scan for available networks - * - * This function won't block, it'll return immediately and call provided callback for each discovered network with - * AP data and user @a data. After the last discovered network @a cb will be called with NULL as @a ap, so it - * will be always called at least once. - * - * @param cb Function to be called for every discovered network - * @param data User handle that will be passed to @a cb along with the AP data (Default: NULL) - * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) - */ - virtual void scan_async(mbed::Callback cb, unsigned timeout = 0) = 0; - -protected: - char *_ssid; - char *_pass; - nsapi_security_t _security; - char _ip_address[16]; //IPv4 only thus 16 + virtual int scan(wifi_ap_t *res, unsigned count) = 0; }; #endif diff --git a/features/net/network-socket/nsapi_types.h b/features/net/network-socket/nsapi_types.h index 7bc6559ac2..0efca8bea5 100644 --- a/features/net/network-socket/nsapi_types.h +++ b/features/net/network-socket/nsapi_types.h @@ -40,12 +40,11 @@ typedef enum nsapi_error { NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */ NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */ NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */ - NSAPI_ERROR_DNS_FAILURE = -3008, /*!< DNS failed to complete successfully */ - NSAPI_ERROR_DHCP_FAILURE = -3009, /*!< DHCP failed to complete successfully */ - NSAPI_ERROR_AUTH_FAILURE = -3010, /*!< connection to access point failed */ - NSAPI_ERROR_DEVICE_ERROR = -3011, /*!< failure interfacing with the network processor */ - NSAPI_ERROR_TIMEOUT = -3012, /*!< operation timed out */ - NSAPI_ERROR_NO_SSID = -3013, /*!< ssid not found */ + NSAPI_ERROR_NO_SSID = -3008, /*!< ssid not found */ + NSAPI_ERROR_DNS_FAILURE = -3009, /*!< DNS failed to complete successfully */ + NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */ + NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */ + NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */ } nsapi_error_t; /** Enum of encryption types @@ -62,17 +61,6 @@ typedef enum nsapi_security { NSAPI_SECURITY_UNSSUPPORTED = 0xFF, /*!< unknown/unsupported security in scan results */ } nsapi_security_t; -/** Enum of interface states - * - * List of all possible states a WiFi network interface can be in - */ -typedef enum nsapi_if_state { - NSAPI_IF_STATE_NOT_CONNECTED = 0x0, - NSAPI_IF_STATE_CONNECTING = 0x01, - NSAPI_IF_STATE_CONNECTED = 0x02, - NSAPI_IF_STATE_ERROR = 0xFF -} nsapi_if_state_t; - /** Maximum size of IP address representation */ #define NSAPI_IP_SIZE NSAPI_IPv6_SIZE From 0f40c121d52a254455096d4a831e8045b1e7956e Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 30 Sep 2016 15:54:13 -0500 Subject: [PATCH 48/61] wifi - Moved wifi_ap_t into its own class WiFiAccessPoint Using a class for this structure is more idiomatic C++, allows future deprecations, and more flexibility of the internal structure. Additionally, this matches to design of the SocketAddress class specific to the network-socket api. --- .../net/network-socket/WiFiAccessPoint.cpp | 37 ++++++++++ features/net/network-socket/WiFiAccessPoint.h | 70 +++++++++++++++++++ features/net/network-socket/WiFiInterface.h | 12 +--- features/net/network-socket/nsapi.h | 1 + features/net/network-socket/nsapi_types.h | 12 ++++ 5 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 features/net/network-socket/WiFiAccessPoint.cpp create mode 100644 features/net/network-socket/WiFiAccessPoint.h diff --git a/features/net/network-socket/WiFiAccessPoint.cpp b/features/net/network-socket/WiFiAccessPoint.cpp new file mode 100644 index 0000000000..f7a5932550 --- /dev/null +++ b/features/net/network-socket/WiFiAccessPoint.cpp @@ -0,0 +1,37 @@ +#include "network-socket/WiFiAccessPoint.h" + +WiFiAccessPoint::WiFiAccessPoint() +{ + memset(&_ap, 0, sizeof(_ap)); +} + +WiFiAccessPoint::WiFiAccessPoint(nsapi_wifi_ap_t ap) +{ + _ap = ap; +} + +const char *WiFiAccessPoint::get_ssid() const +{ + return _ap.ssid; +} + +const uint8_t *WiFiAccessPoint::get_bssid() const +{ + return _ap.bssid; +} + +nsapi_security_t WiFiAccessPoint::get_security() const +{ + return _ap.security; +} + +int8_t WiFiAccessPoint::get_rssi() const +{ + return _ap.rssi; +} + +uint8_t WiFiAccessPoint::get_channel() const +{ + return _ap.channel; +} + diff --git a/features/net/network-socket/WiFiAccessPoint.h b/features/net/network-socket/WiFiAccessPoint.h new file mode 100644 index 0000000000..a8c90bf6e1 --- /dev/null +++ b/features/net/network-socket/WiFiAccessPoint.h @@ -0,0 +1,70 @@ +/* WiFiInterface + * Copyright (c) 2015 - 2016 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 WIFI_ACCESS_POINT_H +#define WIFI_ACCESS_POINT_H + +#include +#include "network-socket/nsapi_types.h" + +/** WiFiAccessPoint class + * + * Class that represents a WiFi Access Point + * Common interface that is shared between WiFi devices + */ +class WiFiAccessPoint +{ + /** WiFiAccessPoint lifetime + */ + WiFiAccessPoint(); + WiFiAccessPoint(nsapi_wifi_ap_t ap); + + /** Get an access point's ssid + * + * @return The ssid of the access point + */ + const char *get_ssid() const; + + /** Get an access point's bssid + * + * @return The bssid of the access point + */ + const uint8_t *get_bssid() const; + + /** Get an access point's security + * + * @return The security type of the access point + */ + nsapi_security_t get_security() const; + + /** Gets the radio signal strength for the access point + * + * @return Connection strength in dBm (negative value), + * or 0 if measurement impossible + */ + int8_t get_rssi() const; + + /** Get the access point's channel + * + * @return The channel of the access point + */ + uint8_t get_channel() const; + +private: + nsapi_wifi_ap_t _ap; +}; + +#endif diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index f997e63a60..3169da7daf 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -20,7 +20,7 @@ #include #include "Callback.h" #include "network-socket/NetworkInterface.h" -#include "nsapi.h" +#include "network-socket/WiFiAccessPoint.h" /** WiFiInterface class * @@ -29,14 +29,6 @@ class WiFiInterface: public NetworkInterface { public: - typedef struct wifi_ap { - char ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ - uint8_t bssid[6]; - nsapi_security_t security; - int8_t rssi; - uint8_t channel; - } wifi_ap_t; - /** WiFiInterface lifetime */ virtual ~WiFiInterface() {}; @@ -107,7 +99,7 @@ public: * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error * see @a nsapi_error */ - virtual int scan(wifi_ap_t *res, unsigned count) = 0; + virtual int scan(WiFiAccessPoint *res, unsigned count) = 0; }; #endif diff --git a/features/net/network-socket/nsapi.h b/features/net/network-socket/nsapi.h index d61fb5502c..b5af7c3144 100644 --- a/features/net/network-socket/nsapi.h +++ b/features/net/network-socket/nsapi.h @@ -25,6 +25,7 @@ // entry point for C++ api #include "network-socket/SocketAddress.h" +#include "network-socket/WiFiAccessPoint.h" #include "network-socket/NetworkStack.h" #include "network-socket/NetworkInterface.h" diff --git a/features/net/network-socket/nsapi_types.h b/features/net/network-socket/nsapi_types.h index 0efca8bea5..6607199df9 100644 --- a/features/net/network-socket/nsapi_types.h +++ b/features/net/network-socket/nsapi_types.h @@ -163,6 +163,18 @@ typedef enum nsapi_option { NSAPI_RCVBUF, /*!< Sets recv buffer size */ } nsapi_option_t; +/** nsapi_wifi_ap structure + * + * Structure representing a WiFi Access Point + */ +typedef struct nsapi_wifi_ap { + char ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ + uint8_t bssid[6]; + nsapi_security_t security; + int8_t rssi; + uint8_t channel; +} nsapi_wifi_ap_t; + /** nsapi_stack structure * From 6440ffab986be9928bef228b0700de3e26c143a9 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 30 Sep 2016 16:34:23 -0500 Subject: [PATCH 49/61] cellular - Matched changes to the WiFiinterface in cellular --- .../net/network-socket/CellularInterface.cpp | 77 ------------------- .../net/network-socket/CellularInterface.h | 12 +-- 2 files changed, 3 insertions(+), 86 deletions(-) delete mode 100644 features/net/network-socket/CellularInterface.cpp diff --git a/features/net/network-socket/CellularInterface.cpp b/features/net/network-socket/CellularInterface.cpp deleted file mode 100644 index 206eba032f..0000000000 --- a/features/net/network-socket/CellularInterface.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/* Socket - * 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 "network-socket/CellularInterface.h" -#include -#include - - -CellularInterface::CellularInterface() - : _apn(0), _user(0), _pass(0) -{ -} - -CellularInterface::~CellularInterface() -{ - free(_apn); - free(_user); - free(_pass); -} - -int CellularInterface::set_credentials(const char *apn, const char *user, const char *pass) -{ - free(_apn); - _apn = 0; - free(_user); - _user = 0; - free(_pass); - _pass = 0; - - if (apn) { - _apn = (char*)malloc(strlen(apn)+1); - if (!_apn) { - return NSAPI_ERROR_NO_MEMORY; - } - - strcpy(_apn, apn); - } - - if (user) { - _user = (char*)malloc(strlen(user)+1); - if (!_user) { - return NSAPI_ERROR_NO_MEMORY; - } - - strcpy(_user, user); - } - - if (pass) { - _pass = (char*)malloc(strlen(pass)+1); - if (!_pass) { - return NSAPI_ERROR_NO_MEMORY; - } - - strcpy(_pass, pass); - } - - return 0; -} - -int CellularInterface::connect() -{ - return connect(_apn, _user, _pass); -} - diff --git a/features/net/network-socket/CellularInterface.h b/features/net/network-socket/CellularInterface.h index 2eaffdf71a..3890efae1b 100644 --- a/features/net/network-socket/CellularInterface.h +++ b/features/net/network-socket/CellularInterface.h @@ -29,8 +29,7 @@ class CellularInterface : public NetworkInterface public: /** CellularInterface lifetime */ - CellularInterface(); - virtual ~CellularInterface(); + virtual ~CellularInterface() {}; /** Set the cellular network APN and credentials * @@ -38,7 +37,7 @@ public: * @param user Optional username for the APN * @param pass Optional password fot the APN */ - virtual int set_credentials(const char *apn, const char *user = 0, const char *pass = 0); + virtual int set_credentials(const char *apn, const char *user = 0, const char *pass = 0) = 0; /** Start the interface * @@ -55,18 +54,13 @@ public: * * @return 0 on success, negative error code on failure */ - virtual int connect(); + virtual int connect() = 0; /** Stop the interface * * @return 0 on success, negative error code on failure */ virtual int disconnect() = 0; - -private: - char *_apn; - char *_user; - char *_pass; }; From 3f66a62f87c37cbeff2e33a37a931aec008d1c97 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Sat, 1 Oct 2016 19:35:10 +0100 Subject: [PATCH 50/61] UBLOX_C029: remove emac implementation It depends on the driver that will come separately, therefore this will be enabled once driver is in place. --- hal/targets.json | 2 +- .../TARGET_UBLOX_C029/wifi_emac_api.cpp | 317 ------------------ .../TARGET_UBLOX_C029/wifi_emac_api.h | 8 - 3 files changed, 1 insertion(+), 326 deletions(-) delete mode 100644 hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp delete mode 100644 hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.h diff --git a/hal/targets.json b/hal/targets.json index a4e053275d..dffaa479ba 100644 --- a/hal/targets.json +++ b/hal/targets.json @@ -1217,7 +1217,7 @@ "extra_labels": ["STM", "STM32F4", "STM32F439", "STM32F439ZI"], "macros": ["HSE_VALUE=24000000", "HSE_STARTUP_TIMEOUT=5000", "CB_INTERFACE_SDIO","CB_CHIP_WL18XX","SUPPORT_80211D_ALWAYS","WLAN_ENABLED"], "inherits": ["Target"], - "device_has": ["ANALOGIN", "CAN", "EMAC", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"], + "device_has": ["ANALOGIN", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"], "features": ["IPV4"], "release_versions": ["5"] }, diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp b/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp deleted file mode 100644 index b69e1a1fa8..0000000000 --- a/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.cpp +++ /dev/null @@ -1,317 +0,0 @@ -#include -#include "cb_main.h" -#include "cb_wlan.h" -#include "cb_wlan_types.h" -#include "cb_otp.h" -#include "cb_wlan_target_data.h" -#include "emac_api.h" -#include "mbed_assert.h" -#include "rtos.h" - -/*=========================================================================== -* DEFINES -*=========================================================================*/ -#define WIFI_EMAC_API_MTU_SIZE (1500U) - -/*=========================================================================== -* TYPES -*=========================================================================*/ -typedef struct { - emac_link_input_fn wifi_input_cb; - emac_link_state_change_fn wifi_state_cb; - void* link_input_user_data; - void* link_state_user_data; - bool linkStateRegistered; -} wifi_emac_api_s; - -/*=========================================================================== -* DECLARATIONS -*=========================================================================*/ -static void statusIndication(void *dummy, cbWLAN_StatusIndicationInfo status, void *data); -static void packetIndication(void *dummy, cbWLAN_PacketIndicationInfo *packetInfo); -static cb_boolean handleWlanTargetCopyFromDataFrame(uint8_t* buffer, cbWLANTARGET_dataFrame* frame, uint32_t size, uint32_t offsetInFrame); -static cb_boolean handleWlanTargetCopyToDataFrame(cbWLANTARGET_dataFrame* frame, uint8_t* buffer, uint32_t size, uint32_t offsetInFrame); -static cbWLANTARGET_dataFrame* handleWlanTargetAllocDataFrame(uint32_t size); -static void handleWlanTargetFreeDataFrame(cbWLANTARGET_dataFrame* frame); -static cb_uint32 handleWlanTargetGetDataFrameSize(cbWLANTARGET_dataFrame* frame); -static cb_uint8 handleWlanTargetGetDataFrameTID(cbWLANTARGET_dataFrame* frame); - -static uint32_t wifi_get_mtu_size(emac_interface_t *emac); -static void wifi_get_ifname(emac_interface_t *emac, char *name, uint8_t size); -static uint8_t wifi_get_hwaddr_size(emac_interface_t *emac); -static void wifi_get_hwaddr(emac_interface_t *emac, uint8_t *addr); -static void wifi_set_hwaddr(emac_interface_t *emac, uint8_t *addr); -static bool wifi_link_out(emac_interface_t *emac, emac_stack_mem_t *buf); -static bool wifi_power_up(emac_interface_t *emac); -static void wifi_power_down(emac_interface_t *emac); -static void wifi_set_link_input_cb(emac_interface_t *emac, emac_link_input_fn input_cb, void *data); -static void wifi_set_link_state_cb(emac_interface_t *emac, emac_link_state_change_fn state_cb, void *data); - -/*=========================================================================== -* DEFINITIONS -*=========================================================================*/ -static wifi_emac_api_s _admin; -static const char _ifname[] = "WL0"; - -const emac_interface_ops_t wifi_emac_interface = { - .get_mtu_size = wifi_get_mtu_size, - .get_ifname = wifi_get_ifname, - .get_hwaddr_size = wifi_get_hwaddr_size, - .get_hwaddr = wifi_get_hwaddr, - .set_hwaddr = wifi_set_hwaddr, - .link_out = wifi_link_out, - .power_up = wifi_power_up, - .power_down = wifi_power_down, - .set_link_input_cb = wifi_set_link_input_cb, - .set_link_state_cb = wifi_set_link_state_cb -}; - -static emac_interface_t _intf = { wifi_emac_interface, NULL }; - -static const cbWLANTARGET_Callback _wlanTargetCallback = -{ - handleWlanTargetCopyFromDataFrame, - handleWlanTargetCopyToDataFrame, - handleWlanTargetAllocDataFrame, - handleWlanTargetFreeDataFrame, - handleWlanTargetGetDataFrameSize, - handleWlanTargetGetDataFrameTID -}; - -/*=========================================================================== -* FUNCTIONS -*=========================================================================*/ -static void statusIndication(void *dummy, cbWLAN_StatusIndicationInfo status, void *data) -{ - bool linkUp = false; - bool sendCb = true; - (void)dummy; - (void)data; - - switch (status) { - case cbWLAN_STATUS_CONNECTED: - case cbWLAN_STATUS_AP_STA_ADDED: - linkUp = true; - break; - case cbWLAN_STATUS_STOPPED: - case cbWLAN_STATUS_ERROR: - case cbWLAN_STATUS_DISCONNECTED: - case cbWLAN_STATUS_CONNECTION_FAILURE: - break; - case cbWLAN_STATUS_CONNECTING: - default: - sendCb = false; - break; - } - if (sendCb) { - _admin.wifi_state_cb(_admin.link_state_user_data, linkUp); - } -} - -static void packetIndication(void *dummy, cbWLAN_PacketIndicationInfo *packetInfo) -{ - (void)dummy; - _admin.wifi_input_cb(_admin.link_input_user_data, (void*)packetInfo->rxData); -} - -static cb_boolean handleWlanTargetCopyFromDataFrame(uint8_t* buffer, cbWLANTARGET_dataFrame* frame, uint32_t size, uint32_t offsetInFrame) -{ - void* dummy = NULL; - emac_stack_mem_t** phead = (emac_stack_mem_chain_t **)&frame; - emac_stack_mem_t* pbuf; - uint32_t copySize, bytesCopied = 0, pbufOffset = 0; - - MBED_ASSERT(frame != NULL); - MBED_ASSERT(buffer != NULL); - - pbuf = emac_stack_mem_chain_dequeue(dummy, phead); - while (pbuf != NULL) { - if ((pbufOffset + emac_stack_mem_len(dummy, pbuf)) >= offsetInFrame) { - copySize = cb_MIN(size, emac_stack_mem_len(dummy, pbuf) - (offsetInFrame - pbufOffset)); - memcpy(buffer, (int8_t *)emac_stack_mem_ptr(dummy, pbuf) + (offsetInFrame - pbufOffset), copySize); - buffer += copySize; - bytesCopied += copySize; - pbuf = emac_stack_mem_chain_dequeue(dummy, phead); - break; - } - pbufOffset += emac_stack_mem_len(dummy, pbuf); - pbuf = emac_stack_mem_chain_dequeue(dummy, phead); - } - - while (pbuf != NULL && bytesCopied < size) { - copySize = cb_MIN(emac_stack_mem_len(dummy, pbuf), size - bytesCopied); - memcpy(buffer, emac_stack_mem_ptr(dummy, pbuf), copySize); - buffer += copySize; - bytesCopied += copySize; - pbuf = emac_stack_mem_chain_dequeue(dummy, phead); - } - - MBED_ASSERT(bytesCopied <= size); - - return (bytesCopied == size); -} - -static cb_boolean handleWlanTargetCopyToDataFrame(cbWLANTARGET_dataFrame* frame, uint8_t* buffer, uint32_t size, uint32_t offsetInFrame) -{ - void* dummy = NULL; - emac_stack_mem_t** phead = (emac_stack_mem_chain_t **)&frame; - emac_stack_mem_t* pbuf; - uint32_t copySize, bytesCopied = 0, pbufOffset = 0; - - MBED_ASSERT(frame != NULL); - MBED_ASSERT(buffer != NULL); - - pbuf = emac_stack_mem_chain_dequeue(dummy, phead); - while (pbuf != NULL) { - if ((pbufOffset + emac_stack_mem_len(dummy, pbuf)) >= offsetInFrame) { - copySize = cb_MIN(size, emac_stack_mem_len(dummy, pbuf) - (offsetInFrame - pbufOffset)); - memcpy((uint8_t *)emac_stack_mem_ptr(dummy, pbuf) + (offsetInFrame - pbufOffset), buffer, copySize); - buffer += copySize; - bytesCopied += copySize; - pbuf = emac_stack_mem_chain_dequeue(dummy, phead); - break; - } - pbufOffset += emac_stack_mem_len(dummy, pbuf); - pbuf = emac_stack_mem_chain_dequeue(dummy, phead); - } - - while (pbuf != NULL && bytesCopied < size) { - copySize = cb_MIN(emac_stack_mem_len(dummy, pbuf), size - bytesCopied); - memcpy(emac_stack_mem_ptr(dummy, pbuf), buffer, copySize); - buffer += copySize; - bytesCopied += copySize; - pbuf = emac_stack_mem_chain_dequeue(dummy, phead); - } - - MBED_ASSERT(bytesCopied <= size); - - return (bytesCopied == size); -} - -static cbWLANTARGET_dataFrame* handleWlanTargetAllocDataFrame(uint32_t size) -{ - void* dummy = NULL; - - return (cbWLANTARGET_dataFrame*)emac_stack_mem_alloc(dummy, size, 0); -} - -static void handleWlanTargetFreeDataFrame(cbWLANTARGET_dataFrame* frame) -{ - void* dummy = NULL; - - emac_stack_mem_free(dummy, (emac_stack_mem_t*)frame); -} - -static uint32_t handleWlanTargetGetDataFrameSize(cbWLANTARGET_dataFrame* frame) -{ - void* dummy = NULL; - return emac_stack_mem_chain_len(dummy, (emac_stack_mem_t*)frame); -} - -static uint8_t handleWlanTargetGetDataFrameTID(cbWLANTARGET_dataFrame* frame) -{ - (void)frame; - return (uint8_t)cbWLAN_AC_BE; -} - -/*=========================================================================== -* API FUNCTIONS -*=========================================================================*/ -static uint32_t wifi_get_mtu_size(emac_interface_t *emac) -{ - (void)emac; - - return WIFI_EMAC_API_MTU_SIZE; -} - -static void wifi_get_ifname(emac_interface_t *emac, char *name, uint8_t size) -{ - (void)emac; - MBED_ASSERT(name != NULL); - memcpy((void*)name, (void*)&_ifname, cb_MIN(size, sizeof(_ifname))); -} - -static uint8_t wifi_get_hwaddr_size(emac_interface_t *emac) -{ - (void)emac; - - return sizeof(cbWLAN_MACAddress); -} - -static void wifi_get_hwaddr(emac_interface_t *emac, uint8_t *addr) -{ - (void)emac; - - cbOTP_read(cbOTP_MAC_WLAN, sizeof(cbWLAN_MACAddress), addr); -} - -static void wifi_set_hwaddr(emac_interface_t *emac, uint8_t *addr) -{ - (void)emac; - (void)addr; - //@TODO: shall be handled by wifi driver - //cbOTP_write(cbOTP_MAC_WLAN, sizeof(cbWLAN_MACAddress), addr); -} - -static bool wifi_link_out(emac_interface_t *emac, emac_stack_mem_t *buf) -{ - (void)emac; - cbMAIN_driverLock(); - cbWLAN_sendPacket((void*)buf); - cbMAIN_driverUnlock(); - return true; -} - - -static bool wifi_power_up(emac_interface_t *emac) -{ - (void)emac; - - cbMAIN_driverLock(); - cbWLANTARGET_registerCallbacks((cbWLANTARGET_Callback*)&_wlanTargetCallback); - cbMAIN_driverUnlock(); - return true; -} - -static void wifi_power_down(emac_interface_t *emac) -{ - (void)emac; -} - -static void wifi_set_link_input_cb(emac_interface_t *emac, emac_link_input_fn input_cb, void *data) -{ - void *dummy = NULL; - (void)emac; - - _admin.wifi_input_cb = input_cb; - _admin.link_input_user_data = data; - - cbMAIN_driverLock(); - cbWLAN_registerPacketIndicationCallback(packetIndication, dummy); - cbMAIN_driverUnlock(); -} - -static void wifi_set_link_state_cb(emac_interface_t *emac, emac_link_state_change_fn state_cb, void *data) -{ - cbRTSL_Status result; - void *dummy = NULL; - (void)emac; - - _admin.wifi_state_cb = state_cb; - _admin.link_state_user_data = data; - - if (!_admin.linkStateRegistered) { - cbMAIN_driverLock(); - result = cbWLAN_registerStatusCallback(statusIndication, dummy); - cbMAIN_driverUnlock(); - if (result == cbSTATUS_OK) { - _admin.linkStateRegistered = true; - } - } - -} - -emac_interface_t* get_emac_interface() -{ - return &_intf; -} diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.h b/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.h deleted file mode 100644 index 4401c6dfb1..0000000000 --- a/hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/wifi_emac_api.h +++ /dev/null @@ -1,8 +0,0 @@ -#include "emac_api.h" - -#ifndef WIFI_EMAC_API_H -#define WIFI_EMAC_API_H - -emac_interface_t* get_emac_interface(); - -#endif From d0be5733db0f870b4321ec72d427182e66a1c7e2 Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Mon, 3 Oct 2016 12:25:07 +0100 Subject: [PATCH 51/61] EMAC: Fix LWIP_IPV4 and DEVICE_EMAC flag usage --- .../FEATURE_LWIP/lwip-interface/emac_lwip.c | 10 +- .../FEATURE_LWIP/lwip-interface/eth_arch.h | 6 +- .../FEATURE_LWIP/lwip-interface/lwip_stack.c | 135 ++++++++---------- 3 files changed, 67 insertions(+), 84 deletions(-) diff --git a/features/FEATURE_LWIP/lwip-interface/emac_lwip.c b/features/FEATURE_LWIP/lwip-interface/emac_lwip.c index b59015c4ba..34b71af25a 100644 --- a/features/FEATURE_LWIP/lwip-interface/emac_lwip.c +++ b/features/FEATURE_LWIP/lwip-interface/emac_lwip.c @@ -33,11 +33,6 @@ static err_t emac_lwip_low_level_output(struct netif *netif, struct pbuf *p) return ret ? ERR_OK : ERR_IF; } -static err_t emac_lwip_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr) -{ - return etharp_output(netif, q, ipaddr); -} - static void emac_lwip_input(void *data, emac_stack_t *buf) { struct eth_hdr *ethhdr; @@ -81,7 +76,10 @@ err_t emac_lwip_if_init(struct netif *netif) mac->ops.get_ifname(mac, netif->name, 2); - netif->output = emac_lwip_output; +#if LWIP_IPV4 + netif->output = etharp_output; +#endif /* LWIP_IPV4 */ + netif->linkoutput = emac_lwip_low_level_output; if (!mac->ops.power_up(mac)) { diff --git a/features/FEATURE_LWIP/lwip-interface/eth_arch.h b/features/FEATURE_LWIP/lwip-interface/eth_arch.h index 1ff54b1d38..25bdde38dd 100644 --- a/features/FEATURE_LWIP/lwip-interface/eth_arch.h +++ b/features/FEATURE_LWIP/lwip-interface/eth_arch.h @@ -29,13 +29,17 @@ 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_ - diff --git a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c b/features/FEATURE_LWIP/lwip-interface/lwip_stack.c index c25b62f826..432244d88f 100644 --- a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c +++ b/features/FEATURE_LWIP/lwip-interface/lwip_stack.c @@ -35,6 +35,14 @@ #include "emac_api.h" +#if DEVICE_EMAC + #define NETIF_INIT_FN emac_lwip_if_init +#else + #define NETIF_INIT_FN eth_arch_enetif_init +#endif + +#define DHCP_TIMEOUT 15000 + /* Static arena of sockets */ static struct lwip_socket { bool in_use; @@ -352,6 +360,47 @@ char *lwip_get_gateway(char *buf, int buflen) #endif } +int lwip_start_dhcp(unsigned int timeout) +{ + err_t err = 0; +#if LWIP_IPV4 + err = dhcp_start(&lwip_netif); + if (err) { + return NSAPI_ERROR_DHCP_FAILURE; + } +#endif + +#if DEVICE_EMAC + // If doesn't have address + if (!lwip_get_ip_addr(true, &lwip_netif)) { + err = sys_arch_sem_wait(&lwip_netif_has_addr, timeout); + if (err == SYS_ARCH_TIMEOUT) { + return NSAPI_ERROR_DHCP_FAILURE; + } + lwip_connected = true; + } +#endif /* DEVICE_EMAC */ + + return err; +} + +int lwip_start_static_ip(const char *ip, const char *netmask, const char *gw) +{ + +#if LWIP_IPV4 + ip4_addr_t ip_addr; + ip4_addr_t netmask_addr; + ip4_addr_t gw_addr; + + if (!inet_aton(ip, &ip_addr) || + !inet_aton(netmask, &netmask_addr) || + !inet_aton(gw, &gw_addr)) { + return NSAPI_ERROR_PARAMETER; + } + + netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr); +#endif +} int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw) { @@ -373,23 +422,14 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * sys_arch_sem_wait(&lwip_tcpip_inited, 0); memset(&lwip_netif, 0, sizeof lwip_netif); -#if DEVICE_EMAC if (!netif_add(&lwip_netif, #if LWIP_IPV4 0, 0, 0, #endif - emac, emac_lwip_if_init, tcpip_input)) { + emac, NETIF_INIT_FN, tcpip_input)) { return -1; } -#else /* DEVICE_EMAC */ - if (!netif_add(&lwip_netif, -#if LWIP_IPV4 - 0, 0, 0, -#endif - NULL, eth_arch_enetif_init, tcpip_input)) { - return -1; - } -#endif /* DEVICE_EMAC */ + netif_set_default(&lwip_netif); netif_set_link_callback (&lwip_netif, lwip_netif_link_irq); @@ -435,47 +475,26 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * } } -#if LWIP_IPV4 +#if !DEVICE_EMAC if (!dhcp) { - 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); + lwip_start_static_ip(ip, netmask, gw); } -#endif netif_set_up(&lwip_netif); -#if !DEVICE_EMAC -#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; - } + if (dhcp) { + lwip_start_dhcp(DHCP_TIMEOUT); } -#endif // If doesn't have address if (!lwip_get_ip_addr(true, &lwip_netif)) { - //ret = sys_arch_sem_wait(&lwip_netif_has_addr, 15000); - ret = sys_arch_sem_wait(&lwip_netif_has_addr, 30000); + ret = sys_arch_sem_wait(&lwip_netif_has_addr, DHCP_TIMEOUT); if (ret == SYS_ARCH_TIMEOUT) { return NSAPI_ERROR_DHCP_FAILURE; } lwip_connected = true; } -#endif /* DEVICE_EMAC */ #if ADDR_TIMEOUT // If address is not for preferred stack waits a while to see @@ -485,6 +504,8 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * } #endif +#endif /* DEVICE_EMAC */ + #if LWIP_IPV6 add_dns_addr(&lwip_netif); #endif @@ -537,46 +558,6 @@ static int lwip_err_remap(err_t err) { } } -int lwip_start_dhcp(unsigned int timeout) -{ - err_t err = NSAPI_ERROR_DNS_FAILURE; -#if LWIP_IPV4 - err = dhcp_start(&lwip_netif); - if (err) { - return NSAPI_ERROR_DHCP_FAILURE; - } -#endif - - // If doesn't have address - if (!lwip_get_ip_addr(true, &lwip_netif)) { - err = sys_arch_sem_wait(&lwip_netif_has_addr, timeout); - if (err == SYS_ARCH_TIMEOUT) { - return NSAPI_ERROR_DHCP_FAILURE; - } - lwip_connected = true; - } - return err; -} - -int lwip_start_static_ip(const char *ip, const char *netmask, const char *gw) -{ - -#if LWIP_IPV4 - ip4_addr_t ip_addr; - ip4_addr_t netmask_addr; - ip4_addr_t gw_addr; - - if (!inet_aton(ip, &ip_addr) || - !inet_aton(netmask, &netmask_addr) || - !inet_aton(gw, &gw_addr)) { - return NSAPI_ERROR_PARAMETER; - } - - netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr); -#endif - -} - /* LWIP network stack implementation */ static int lwip_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version) { From 98eb50f592a19087eb96554fdde63e5e1f00eb94 Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Tue, 4 Oct 2016 14:22:24 +0100 Subject: [PATCH 52/61] Refactor lwip_stack function names to include mbed prefix Mbed function names, prefixed with lwip are confusing, as the name suggest that they are part of lwip ip stack. --- .../lwip-interface/EthernetInterface.cpp | 12 +- .../FEATURE_LWIP/lwip-interface/lwip_stack.c | 173 +++++++++--------- .../FEATURE_LWIP/lwip-interface/lwip_stack.h | 17 +- 3 files changed, 101 insertions(+), 101 deletions(-) diff --git a/features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp b/features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp index 125c53b5e0..55e4f66dfc 100644 --- a/features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp +++ b/features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp @@ -41,7 +41,7 @@ int EthernetInterface::set_dhcp(bool dhcp) int EthernetInterface::connect() { - return lwip_bringup(NULL,_dhcp, + return mbed_lwip_bringup(NULL, _dhcp, _ip_address[0] ? _ip_address : 0, _netmask[0] ? _netmask : 0, _gateway[0] ? _gateway : 0); @@ -49,17 +49,17 @@ int EthernetInterface::connect() int EthernetInterface::disconnect() { - return lwip_bringdown(); + return mbed_lwip_bringdown(); } const char *EthernetInterface::get_mac_address() { - return lwip_get_mac_address(); + return mbed_lwip_get_mac_address(); } const char *EthernetInterface::get_ip_address() { - if (lwip_get_ip_address(_ip_address, sizeof _ip_address)) { + if (mbed_lwip_get_ip_address(_ip_address, sizeof _ip_address)) { return _ip_address; } @@ -68,7 +68,7 @@ const char *EthernetInterface::get_ip_address() const char *EthernetInterface::get_netmask() { - if (lwip_get_netmask(_netmask, sizeof _netmask)) { + if (mbed_lwip_get_netmask(_netmask, sizeof _netmask)) { return _netmask; } @@ -77,7 +77,7 @@ const char *EthernetInterface::get_netmask() const char *EthernetInterface::get_gateway() { - if (lwip_get_gateway(_gateway, sizeof _gateway)) { + if (mbed_lwip_get_gateway(_gateway, sizeof _gateway)) { return _gateway; } diff --git a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c b/features/FEATURE_LWIP/lwip-interface/lwip_stack.c index 432244d88f..218548cffe 100644 --- a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c +++ b/features/FEATURE_LWIP/lwip-interface/lwip_stack.c @@ -36,9 +36,9 @@ #include "emac_api.h" #if DEVICE_EMAC - #define NETIF_INIT_FN emac_lwip_if_init + #define MBED_NETIF_INIT_FN emac_lwip_if_init #else - #define NETIF_INIT_FN eth_arch_enetif_init + #define MBED_NETIF_INIT_FN eth_arch_enetif_init #endif #define DHCP_TIMEOUT 15000 @@ -57,12 +57,12 @@ static struct lwip_socket { static bool lwip_connected = false; -static void lwip_arena_init(void) +static void mbed_lwip_arena_init(void) { memset(lwip_arena, 0, sizeof lwip_arena); } -static struct lwip_socket *lwip_arena_alloc(void) +static struct lwip_socket *mbed_lwip_arena_alloc(void) { sys_prot_t prot = sys_arch_protect(); @@ -80,12 +80,12 @@ static struct lwip_socket *lwip_arena_alloc(void) return 0; } -static void lwip_arena_dealloc(struct lwip_socket *s) +static void mbed_lwip_arena_dealloc(struct lwip_socket *s) { s->in_use = false; } -static void lwip_socket_callback(struct netconn *nc, enum netconn_evt eh, u16_t len) +static void mbed_lwip_socket_callback(struct netconn *nc, enum netconn_evt eh, u16_t len) { sys_prot_t prot = sys_arch_protect(); @@ -174,7 +174,7 @@ static bool convert_lwip_addr_to_mbed(nsapi_addr_t *out, const ip_addr_t *in) return false; } -static const ip_addr_t *lwip_get_ipv4_addr(const struct netif *netif) +static const ip_addr_t *mbed_lwip_get_ipv4_addr(const struct netif *netif) { #if LWIP_IPV4 if (!netif_is_up(netif)) { @@ -189,7 +189,7 @@ static const ip_addr_t *lwip_get_ipv4_addr(const struct netif *netif) return NULL; } -static const ip_addr_t *lwip_get_ipv6_addr(const struct netif *netif) +static const ip_addr_t *mbed_lwip_get_ipv6_addr(const struct netif *netif) { #if LWIP_IPV6 if (!netif_is_up(netif)) { @@ -208,17 +208,17 @@ static const ip_addr_t *lwip_get_ipv6_addr(const struct netif *netif) } -const ip_addr_t *lwip_get_ip_addr(bool any_addr, const struct netif *netif) +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 = lwip_get_ipv4_addr(netif); - npref_ip_addr = lwip_get_ipv6_addr(netif); + pref_ip_addr = mbed_lwip_get_ipv4_addr(netif); + npref_ip_addr = mbed_lwip_get_ipv6_addr(netif); #else - pref_ip_addr = lwip_get_ipv6_addr(netif); - npref_ip_addr = lwip_get_ipv4_addr(netif); + pref_ip_addr = mbed_lwip_get_ipv6_addr(netif); + npref_ip_addr = mbed_lwip_get_ipv4_addr(netif); #endif if (pref_ip_addr) { @@ -233,7 +233,7 @@ const ip_addr_t *lwip_get_ip_addr(bool any_addr, const struct netif *netif) #if LWIP_IPV6 void add_dns_addr(struct netif *lwip_netif) { - const ip_addr_t *ip_addr = lwip_get_ip_addr(true, lwip_netif); + const ip_addr_t *ip_addr = mbed_lwip_get_ip_addr(true, lwip_netif); if (ip_addr) { if (IP_IS_V6(ip_addr)) { const ip_addr_t *dns_ip_addr; @@ -262,13 +262,13 @@ void add_dns_addr(struct netif *lwip_netif) #endif static sys_sem_t lwip_tcpip_inited; -static void lwip_tcpip_init_irq(void *eh) +static void mbed_lwip_tcpip_init_irq(void *eh) { sys_sem_signal(&lwip_tcpip_inited); } static sys_sem_t lwip_netif_linked; -static void lwip_netif_link_irq(struct netif *lwip_netif) +static void mbed_lwip_netif_link_irq(struct netif *lwip_netif) { if (netif_is_link_up(lwip_netif)) { sys_sem_signal(&lwip_netif_linked); @@ -276,24 +276,24 @@ static void lwip_netif_link_irq(struct netif *lwip_netif) } static sys_sem_t lwip_netif_has_addr; -static void lwip_netif_status_irq(struct netif *lwip_netif) +static void mbed_lwip_netif_status_irq(struct netif *lwip_netif) { static bool any_addr = true; // Indicates that has address - if (any_addr == true && lwip_get_ip_addr(true, lwip_netif)) { + 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 (lwip_get_ip_addr(false, lwip_netif)) { + if (mbed_lwip_get_ip_addr(false, lwip_netif)) { sys_sem_signal(&lwip_netif_has_addr); } } -static void lwip_set_mac_address(void) +static void mbed_lwip_set_mac_address(void) { #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE) snprintf(lwip_mac_address, 19, "%02x:%02x:%02x:%02x:%02x:%02x", @@ -308,14 +308,14 @@ static void lwip_set_mac_address(void) } /* LWIP interface implementation */ -const char *lwip_get_mac_address(void) +const char *mbed_lwip_get_mac_address(void) { return lwip_mac_address[0] ? lwip_mac_address : 0; } -char *lwip_get_ip_address(char *buf, int buflen) +char *mbed_lwip_get_ip_address(char *buf, int buflen) { - const ip_addr_t *addr = lwip_get_ip_addr(true, &lwip_netif); + const ip_addr_t *addr = mbed_lwip_get_ip_addr(true, &lwip_netif); if (!addr) { return NULL; } @@ -332,7 +332,7 @@ char *lwip_get_ip_address(char *buf, int buflen) return NULL; } -const char *lwip_get_netmask(char *buf, int buflen) +const char *mbed_lwip_get_netmask(char *buf, int buflen) { #if LWIP_IPV4 const ip4_addr_t *addr = netif_ip4_netmask(&lwip_netif); @@ -346,7 +346,7 @@ const char *lwip_get_netmask(char *buf, int buflen) #endif } -char *lwip_get_gateway(char *buf, int buflen) +char *mbed_lwip_get_gateway(char *buf, int buflen) { #if LWIP_IPV4 const ip4_addr_t *addr = netif_ip4_gw(&lwip_netif); @@ -360,7 +360,7 @@ char *lwip_get_gateway(char *buf, int buflen) #endif } -int lwip_start_dhcp(unsigned int timeout) +int mbed_lwip_start_dhcp(unsigned int timeout) { err_t err = 0; #if LWIP_IPV4 @@ -372,7 +372,7 @@ int lwip_start_dhcp(unsigned int timeout) #if DEVICE_EMAC // If doesn't have address - if (!lwip_get_ip_addr(true, &lwip_netif)) { + if (!mbed_lwip_get_ip_addr(true, &lwip_netif)) { err = sys_arch_sem_wait(&lwip_netif_has_addr, timeout); if (err == SYS_ARCH_TIMEOUT) { return NSAPI_ERROR_DHCP_FAILURE; @@ -384,7 +384,7 @@ int lwip_start_dhcp(unsigned int timeout) return err; } -int lwip_start_static_ip(const char *ip, const char *netmask, const char *gw) +int mbed_lwip_start_static_ip(const char *ip, const char *netmask, const char *gw) { #if LWIP_IPV4 @@ -402,7 +402,7 @@ int lwip_start_static_ip(const char *ip, const char *netmask, const char *gw) #endif } -int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw) +int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw) { // Check if we've already connected if (lwip_connected) { @@ -410,15 +410,15 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * } // Check if we've already brought up lwip - if (!lwip_get_mac_address()) { + if (!mbed_lwip_get_mac_address()) { // Set up network - lwip_set_mac_address(); + 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(lwip_tcpip_init_irq, NULL); + tcpip_init(mbed_lwip_tcpip_init_irq, NULL); sys_arch_sem_wait(&lwip_tcpip_inited, 0); memset(&lwip_netif, 0, sizeof lwip_netif); @@ -426,14 +426,14 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * #if LWIP_IPV4 0, 0, 0, #endif - emac, NETIF_INIT_FN, tcpip_input)) { + emac, MBED_NETIF_INIT_FN, tcpip_input)) { return -1; } netif_set_default(&lwip_netif); - netif_set_link_callback (&lwip_netif, lwip_netif_link_irq); - netif_set_status_callback(&lwip_netif, lwip_netif_status_irq); + 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(); @@ -441,7 +441,7 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * } // Zero out socket set - lwip_arena_init(); + mbed_lwip_arena_init(); #if LWIP_IPV6 netif_create_ip6_linklocal_address(&lwip_netif, 1/*from MAC*/); @@ -477,18 +477,18 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * #if !DEVICE_EMAC if (!dhcp) { - lwip_start_static_ip(ip, netmask, gw); + mbed_lwip_start_static_ip(ip, netmask, gw); } netif_set_up(&lwip_netif); lwip_dhcp = dhcp; if (dhcp) { - lwip_start_dhcp(DHCP_TIMEOUT); + mbed_lwip_start_dhcp(DHCP_TIMEOUT); } // If doesn't have address - if (!lwip_get_ip_addr(true, &lwip_netif)) { + if (!mbed_lwip_get_ip_addr(true, &lwip_netif)) { ret = sys_arch_sem_wait(&lwip_netif_has_addr, DHCP_TIMEOUT); if (ret == SYS_ARCH_TIMEOUT) { return NSAPI_ERROR_DHCP_FAILURE; @@ -499,7 +499,7 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * #if ADDR_TIMEOUT // If address is not for preferred stack waits a while to see // if preferred stack address is acquired - if (!lwip_get_ip_addr(false, &lwip_netif)) { + if (!mbed_lwip_get_ip_addr(false, &lwip_netif)) { ret = sys_arch_sem_wait(&lwip_netif_has_addr, ADDR_TIMEOUT * 1000); } #endif @@ -513,7 +513,8 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char * return 0; } -int lwip_bringdown(void) + +int mbed_lwip_bringdown(void) { // Check if we've connected if (!lwip_connected) { @@ -533,7 +534,7 @@ int lwip_bringdown(void) } /* LWIP error remapping */ -static int lwip_err_remap(err_t err) { +static int mbed_lwip_err_remap(err_t err) { switch (err) { case ERR_OK: case ERR_CLSD: @@ -559,7 +560,7 @@ static int lwip_err_remap(err_t err) { } /* LWIP network stack implementation */ -static int lwip_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version) +static int mbed_lwip_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version) { ip_addr_t lwip_addr; @@ -567,7 +568,7 @@ static int lwip_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr u8_t addr_type; if (version == NSAPI_UNSPEC) { const ip_addr_t *ip_addr; - ip_addr = lwip_get_ip_addr(true, &lwip_netif); + ip_addr = mbed_lwip_get_ip_addr(true, &lwip_netif); if (IP_IS_V6(ip_addr)) { addr_type = NETCONN_DNS_IPV6; } else { @@ -600,7 +601,7 @@ static int lwip_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr return 0; } -static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto) +static int mbed_lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto) { // check if network is connected if (!lwip_connected) { @@ -608,7 +609,7 @@ static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_ } // allocate a socket - struct lwip_socket *s = lwip_arena_alloc(); + struct lwip_socket *s = mbed_lwip_arena_alloc(); if (!s) { return NSAPI_ERROR_NO_SOCKET; } @@ -617,7 +618,7 @@ static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_ #if LWIP_IPV6 && LWIP_IPV4 const ip_addr_t *ip_addr; - ip_addr = lwip_get_ip_addr(true, &lwip_netif); + ip_addr = mbed_lwip_get_ip_addr(true, &lwip_netif); if (IP_IS_V6(ip_addr)) { // Enable IPv6 (or dual-stack). LWIP dual-stack support is @@ -631,10 +632,10 @@ static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_ lwip_proto |= NETCONN_TYPE_IPV6; #endif - s->conn = netconn_new_with_callback(lwip_proto, lwip_socket_callback); + s->conn = netconn_new_with_callback(lwip_proto, mbed_lwip_socket_callback); if (!s->conn) { - lwip_arena_dealloc(s); + mbed_lwip_arena_dealloc(s); return NSAPI_ERROR_NO_SOCKET; } @@ -643,16 +644,16 @@ static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_ return 0; } -static int lwip_socket_close(nsapi_stack_t *stack, nsapi_socket_t handle) +static int mbed_lwip_socket_close(nsapi_stack_t *stack, nsapi_socket_t handle) { struct lwip_socket *s = (struct lwip_socket *)handle; err_t err = netconn_delete(s->conn); - lwip_arena_dealloc(s); - return lwip_err_remap(err); + mbed_lwip_arena_dealloc(s); + return mbed_lwip_err_remap(err); } -static int lwip_socket_bind(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port) +static int 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; ip_addr_t ip_addr; @@ -667,18 +668,18 @@ static int lwip_socket_bind(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_a } err_t err = netconn_bind(s->conn, &ip_addr, port); - return lwip_err_remap(err); + return mbed_lwip_err_remap(err); } -static int lwip_socket_listen(nsapi_stack_t *stack, nsapi_socket_t handle, int backlog) +static int mbed_lwip_socket_listen(nsapi_stack_t *stack, nsapi_socket_t handle, int backlog) { struct lwip_socket *s = (struct lwip_socket *)handle; err_t err = netconn_listen_with_backlog(s->conn, backlog); - return lwip_err_remap(err); + return mbed_lwip_err_remap(err); } -static int lwip_socket_connect(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port) +static int 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; ip_addr_t ip_addr; @@ -691,21 +692,21 @@ static int lwip_socket_connect(nsapi_stack_t *stack, nsapi_socket_t handle, nsap err_t err = netconn_connect(s->conn, &ip_addr, port); netconn_set_nonblocking(s->conn, true); - return lwip_err_remap(err); + return mbed_lwip_err_remap(err); } -static int lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *handle, nsapi_addr_t *addr, uint16_t *port) +static int 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 = lwip_arena_alloc(); + struct lwip_socket *ns = mbed_lwip_arena_alloc(); if (!ns) { return NSAPI_ERROR_NO_SOCKET; } err_t err = netconn_accept(s->conn, &ns->conn); if (err != ERR_OK) { - lwip_arena_dealloc(ns); - return lwip_err_remap(err); + mbed_lwip_arena_dealloc(ns); + return mbed_lwip_err_remap(err); } netconn_set_recvtimeout(ns->conn, 1); @@ -717,20 +718,20 @@ static int lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t server, nsapi return 0; } -static int lwip_socket_send(nsapi_stack_t *stack, nsapi_socket_t handle, const void *data, unsigned size) +static int mbed_lwip_socket_send(nsapi_stack_t *stack, nsapi_socket_t handle, const void *data, unsigned size) { struct lwip_socket *s = (struct lwip_socket *)handle; size_t bytes_written = 0; err_t err = netconn_write_partly(s->conn, data, size, NETCONN_COPY, &bytes_written); if (err != ERR_OK) { - return lwip_err_remap(err); + return mbed_lwip_err_remap(err); } return (int)bytes_written; } -static int lwip_socket_recv(nsapi_stack_t *stack, nsapi_socket_t handle, void *data, unsigned size) +static int mbed_lwip_socket_recv(nsapi_stack_t *stack, nsapi_socket_t handle, void *data, unsigned size) { struct lwip_socket *s = (struct lwip_socket *)handle; @@ -739,7 +740,7 @@ static int lwip_socket_recv(nsapi_stack_t *stack, nsapi_socket_t handle, void *d s->offset = 0; if (err != ERR_OK) { - return lwip_err_remap(err); + return mbed_lwip_err_remap(err); } } @@ -754,7 +755,7 @@ static int lwip_socket_recv(nsapi_stack_t *stack, nsapi_socket_t handle, void *d return recv; } -static int lwip_socket_sendto(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port, const void *data, unsigned size) +static int mbed_lwip_socket_sendto(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port, const void *data, unsigned size) { struct lwip_socket *s = (struct lwip_socket *)handle; ip_addr_t ip_addr; @@ -767,26 +768,26 @@ static int lwip_socket_sendto(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi err_t err = netbuf_ref(buf, data, (u16_t)size); if (err != ERR_OK) { netbuf_free(buf); - return lwip_err_remap(err); + return mbed_lwip_err_remap(err); } err = netconn_sendto(s->conn, buf, &ip_addr, port); netbuf_delete(buf); if (err != ERR_OK) { - return lwip_err_remap(err); + return mbed_lwip_err_remap(err); } return size; } -static int lwip_socket_recvfrom(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t *addr, uint16_t *port, void *data, unsigned size) +static int mbed_lwip_socket_recvfrom(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t *addr, uint16_t *port, void *data, unsigned size) { struct lwip_socket *s = (struct lwip_socket *)handle; struct netbuf *buf; err_t err = netconn_recv(s->conn, &buf); if (err != ERR_OK) { - return lwip_err_remap(err); + return mbed_lwip_err_remap(err); } convert_lwip_addr_to_mbed(addr, netbuf_fromaddr(buf)); @@ -798,7 +799,7 @@ static int lwip_socket_recvfrom(nsapi_stack_t *stack, nsapi_socket_t handle, nsa return recv; } -static int lwip_setsockopt(nsapi_stack_t *stack, nsapi_socket_t handle, int level, int optname, const void *optval, unsigned optlen) +static int 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; @@ -844,7 +845,7 @@ static int lwip_setsockopt(nsapi_stack_t *stack, nsapi_socket_t handle, int leve } } -static void lwip_socket_attach(nsapi_stack_t *stack, nsapi_socket_t handle, void (*callback)(void *), void *data) +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; @@ -854,19 +855,19 @@ static void lwip_socket_attach(nsapi_stack_t *stack, nsapi_socket_t handle, void /* LWIP network stack */ const nsapi_stack_api_t lwip_stack_api = { - .gethostbyname = lwip_gethostbyname, - .socket_open = lwip_socket_open, - .socket_close = lwip_socket_close, - .socket_bind = lwip_socket_bind, - .socket_listen = lwip_socket_listen, - .socket_connect = lwip_socket_connect, - .socket_accept = lwip_socket_accept, - .socket_send = lwip_socket_send, - .socket_recv = lwip_socket_recv, - .socket_sendto = lwip_socket_sendto, - .socket_recvfrom = lwip_socket_recvfrom, - .setsockopt = lwip_setsockopt, - .socket_attach = lwip_socket_attach, + .gethostbyname = mbed_lwip_gethostbyname, + .socket_open = mbed_lwip_socket_open, + .socket_close = mbed_lwip_socket_close, + .socket_bind = mbed_lwip_socket_bind, + .socket_listen = mbed_lwip_socket_listen, + .socket_connect = mbed_lwip_socket_connect, + .socket_accept = mbed_lwip_socket_accept, + .socket_send = mbed_lwip_socket_send, + .socket_recv = mbed_lwip_socket_recv, + .socket_sendto = mbed_lwip_socket_sendto, + .socket_recvfrom = mbed_lwip_socket_recvfrom, + .setsockopt = mbed_lwip_setsockopt, + .socket_attach = mbed_lwip_socket_attach, }; nsapi_stack_t lwip_stack = { diff --git a/features/FEATURE_LWIP/lwip-interface/lwip_stack.h b/features/FEATURE_LWIP/lwip-interface/lwip_stack.h index 0ad822a659..06b227d74c 100644 --- a/features/FEATURE_LWIP/lwip-interface/lwip_stack.h +++ b/features/FEATURE_LWIP/lwip-interface/lwip_stack.h @@ -24,18 +24,17 @@ extern "C" { #endif - // Access to lwip through the nsapi -int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw); -int lwip_bringdown(void); +int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw); +int mbed_lwip_bringdown(void); -const char *lwip_get_mac_address(void); -char *lwip_get_ip_address(char *buf, int buflen); -char *lwip_get_netmask(char *buf, int buflen); -char *lwip_get_gateway(char *buf, int buflen); +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); -int lwip_start_dhcp(unsigned int timeout); -int lwip_start_static_ip(const char *ip, const char *netmask, const char *gw); +int mbed_lwip_start_dhcp(unsigned int timeout); +int mbed_lwip_start_static_ip(const char *ip, const char *netmask, const char *gw); extern nsapi_stack_t lwip_stack; From ae11b51b26da4bb3dc7e8fbe53beb0a9a94cec75 Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Tue, 4 Oct 2016 14:29:18 +0100 Subject: [PATCH 53/61] Split lwip initialisation and interface bringup Split IP stack initialisation from mbed_lwip_bringup to mbed_lwip_init. --- .../lwip-interface/EthernetInterface.cpp | 2 +- .../FEATURE_LWIP/lwip-interface/lwip_stack.c | 98 ++++++++----------- .../FEATURE_LWIP/lwip-interface/lwip_stack.h | 6 +- 3 files changed, 43 insertions(+), 63 deletions(-) diff --git a/features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp b/features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp index 55e4f66dfc..ae672a55ee 100644 --- a/features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp +++ b/features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp @@ -41,7 +41,7 @@ int EthernetInterface::set_dhcp(bool dhcp) int EthernetInterface::connect() { - return mbed_lwip_bringup(NULL, _dhcp, + return mbed_lwip_bringup(_dhcp, _ip_address[0] ? _ip_address : 0, _netmask[0] ? _netmask : 0, _gateway[0] ? _gateway : 0); diff --git a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c b/features/FEATURE_LWIP/lwip-interface/lwip_stack.c index 218548cffe..f592ffa5e6 100644 --- a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c +++ b/features/FEATURE_LWIP/lwip-interface/lwip_stack.c @@ -360,55 +360,8 @@ char *mbed_lwip_get_gateway(char *buf, int buflen) #endif } -int mbed_lwip_start_dhcp(unsigned int timeout) +int mbed_lwip_init(emac_interface_t *emac) { - err_t err = 0; -#if LWIP_IPV4 - err = dhcp_start(&lwip_netif); - if (err) { - return NSAPI_ERROR_DHCP_FAILURE; - } -#endif - -#if DEVICE_EMAC - // If doesn't have address - if (!mbed_lwip_get_ip_addr(true, &lwip_netif)) { - err = sys_arch_sem_wait(&lwip_netif_has_addr, timeout); - if (err == SYS_ARCH_TIMEOUT) { - return NSAPI_ERROR_DHCP_FAILURE; - } - lwip_connected = true; - } -#endif /* DEVICE_EMAC */ - - return err; -} - -int mbed_lwip_start_static_ip(const char *ip, const char *netmask, const char *gw) -{ - -#if LWIP_IPV4 - ip4_addr_t ip_addr; - ip4_addr_t netmask_addr; - ip4_addr_t gw_addr; - - if (!inet_aton(ip, &ip_addr) || - !inet_aton(netmask, &netmask_addr) || - !inet_aton(gw, &gw_addr)) { - return NSAPI_ERROR_PARAMETER; - } - - netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr); -#endif -} - -int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw) -{ - // Check if we've already connected - if (lwip_connected) { - return NSAPI_ERROR_PARAMETER; - } - // Check if we've already brought up lwip if (!mbed_lwip_get_mac_address()) { // Set up network @@ -427,12 +380,12 @@ int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const c 0, 0, 0, #endif emac, MBED_NETIF_INIT_FN, tcpip_input)) { - return -1; + return NSAPI_ERROR_DEVICE_ERROR; } netif_set_default(&lwip_netif); - netif_set_link_callback (&lwip_netif, mbed_lwip_netif_link_irq); + 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 @@ -440,6 +393,20 @@ int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const c #endif } + return NSAPI_ERROR_OK; +} + +int 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(); @@ -475,21 +442,39 @@ int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const c } } -#if !DEVICE_EMAC +#if LWIP_IPV4 if (!dhcp) { - mbed_lwip_start_static_ip(ip, netmask, gw); + 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 (dhcp) { - mbed_lwip_start_dhcp(DHCP_TIMEOUT); + + 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, DHCP_TIMEOUT); + ret = sys_arch_sem_wait(&lwip_netif_has_addr, 15000); if (ret == SYS_ARCH_TIMEOUT) { return NSAPI_ERROR_DHCP_FAILURE; } @@ -504,8 +489,6 @@ int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const c } #endif -#endif /* DEVICE_EMAC */ - #if LWIP_IPV6 add_dns_addr(&lwip_netif); #endif @@ -513,7 +496,6 @@ int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const c return 0; } - int mbed_lwip_bringdown(void) { // Check if we've connected diff --git a/features/FEATURE_LWIP/lwip-interface/lwip_stack.h b/features/FEATURE_LWIP/lwip-interface/lwip_stack.h index 06b227d74c..89161f61f4 100644 --- a/features/FEATURE_LWIP/lwip-interface/lwip_stack.h +++ b/features/FEATURE_LWIP/lwip-interface/lwip_stack.h @@ -25,7 +25,8 @@ extern "C" { #endif // Access to lwip through the nsapi -int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw); +int mbed_lwip_init(emac_interface_t *emac); +int mbed_lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw); int mbed_lwip_bringdown(void); const char *mbed_lwip_get_mac_address(void); @@ -33,9 +34,6 @@ 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); -int mbed_lwip_start_dhcp(unsigned int timeout); -int mbed_lwip_start_static_ip(const char *ip, const char *netmask, const char *gw); - extern nsapi_stack_t lwip_stack; From 1e7ba5fe798dde2f2cfb0ed5d4aa44ad6961fecc Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Tue, 4 Oct 2016 14:38:28 +0100 Subject: [PATCH 54/61] Fix merge issues --- features/FEATURE_LWIP/lwip-interface/emac_lwip.c | 1 - features/FEATURE_LWIP/lwip-interface/lwip_stack.c | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/features/FEATURE_LWIP/lwip-interface/emac_lwip.c b/features/FEATURE_LWIP/lwip-interface/emac_lwip.c index 34b71af25a..01f09faf0f 100644 --- a/features/FEATURE_LWIP/lwip-interface/emac_lwip.c +++ b/features/FEATURE_LWIP/lwip-interface/emac_lwip.c @@ -35,7 +35,6 @@ static err_t emac_lwip_low_level_output(struct netif *netif, struct pbuf *p) static void emac_lwip_input(void *data, emac_stack_t *buf) { - struct eth_hdr *ethhdr; struct pbuf *p = (struct pbuf *)buf; struct netif *netif = (struct netif *)data; diff --git a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c b/features/FEATURE_LWIP/lwip-interface/lwip_stack.c index f592ffa5e6..999674efc0 100644 --- a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c +++ b/features/FEATURE_LWIP/lwip-interface/lwip_stack.c @@ -513,6 +513,10 @@ int mbed_lwip_bringdown(void) netif_set_down(&lwip_netif); } #endif + + lwip_connected = false; + // TO DO - actually remove addresses from stack, and shut down properly + return 0; } /* LWIP error remapping */ From b418fce8ac9a44bcfdcfe5b3945bd034b72e837a Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Mon, 3 Oct 2016 09:43:32 +0200 Subject: [PATCH 55/61] Added function for increasing ref count. --- features/FEATURE_LWIP/lwip-interface/emac_stack_lwip.cpp | 5 +++++ features/netsocket/emac_stack_mem.h | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/features/FEATURE_LWIP/lwip-interface/emac_stack_lwip.cpp b/features/FEATURE_LWIP/lwip-interface/emac_stack_lwip.cpp index 8042a81d2e..d543529fd6 100644 --- a/features/FEATURE_LWIP/lwip-interface/emac_stack_lwip.cpp +++ b/features/FEATURE_LWIP/lwip-interface/emac_stack_lwip.cpp @@ -80,4 +80,9 @@ uint32_t emac_stack_mem_chain_len(emac_stack_t* stack, emac_stack_mem_chain_t *c return ((struct pbuf*)chain)->tot_len; } +void emac_stack_mem_ref(emac_stack_t* stack, emac_stack_mem_t *mem) +{ + pbuf_ref((struct pbuf*)mem); +} + #endif /* DEVICE_EMAC */ diff --git a/features/netsocket/emac_stack_mem.h b/features/netsocket/emac_stack_mem.h index 5a4fbe3401..b407308fd5 100644 --- a/features/netsocket/emac_stack_mem.h +++ b/features/netsocket/emac_stack_mem.h @@ -96,6 +96,14 @@ emac_stack_mem_t *emac_stack_mem_chain_dequeue(emac_stack_t* stack, emac_stack_m */ uint32_t emac_stack_mem_chain_len(emac_stack_t* stack, emac_stack_mem_chain_t *chain); +/** + * 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 */ #endif /* EMAC_MBED_STACK_MEM_h */ From 8ea46eae17d0fc87184ffa79d44f9b8e90005dc2 Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Tue, 4 Oct 2016 11:59:26 +0200 Subject: [PATCH 56/61] Fixed missing abstract connect method --- features/netsocket/WiFiInterface.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/netsocket/WiFiInterface.h b/features/netsocket/WiFiInterface.h index 76723602c1..faaf4f8854 100644 --- a/features/netsocket/WiFiInterface.h +++ b/features/netsocket/WiFiInterface.h @@ -69,7 +69,7 @@ public: */ virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, - uint8_t channel = 0); + uint8_t channel = 0) = 0; /** Start the interface * From e87f013c4d7fb62d409634e9b698eb670c12d948 Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Tue, 4 Oct 2016 11:59:45 +0200 Subject: [PATCH 57/61] Fixed missing public --- features/netsocket/WiFiAccessPoint.h | 1 + 1 file changed, 1 insertion(+) diff --git a/features/netsocket/WiFiAccessPoint.h b/features/netsocket/WiFiAccessPoint.h index 7ebcfc17f9..b590c45aa2 100644 --- a/features/netsocket/WiFiAccessPoint.h +++ b/features/netsocket/WiFiAccessPoint.h @@ -29,6 +29,7 @@ class WiFiAccessPoint { /** WiFiAccessPoint lifetime */ +public: WiFiAccessPoint(); WiFiAccessPoint(nsapi_wifi_ap_t ap); From f5675cc822ec325bd139a67798f220320e5d0707 Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Tue, 4 Oct 2016 11:59:59 +0200 Subject: [PATCH 58/61] Increased the stack size for the lwIP thread from 1024 to 1200. Needed for the u-blox ODIN-W2 driver. The actually needed size has not been measured. --- features/FEATURE_LWIP/lwip-interface/lwipopts.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/FEATURE_LWIP/lwip-interface/lwipopts.h b/features/FEATURE_LWIP/lwip-interface/lwipopts.h index 8e7dff9406..48cb655b48 100644 --- a/features/FEATURE_LWIP/lwip-interface/lwipopts.h +++ b/features/FEATURE_LWIP/lwip-interface/lwipopts.h @@ -81,9 +81,9 @@ #define DEFAULT_ACCEPTMBOX_SIZE 8 #ifdef LWIP_DEBUG -#define TCPIP_THREAD_STACKSIZE 1024*2 +#define TCPIP_THREAD_STACKSIZE 1200*2 #else -#define TCPIP_THREAD_STACKSIZE 1024 +#define TCPIP_THREAD_STACKSIZE 1200 #endif #define TCPIP_THREAD_PRIO (osPriorityNormal) From 26eb5f19f2712bda76582c2e92d5cf95948dc2bf Mon Sep 17 00:00:00 2001 From: "andreas.larsson" Date: Tue, 4 Oct 2016 16:40:15 +0200 Subject: [PATCH 59/61] Fixed mbed_lwip_get_netmask amd mbed_lwip_get_gateway --- features/FEATURE_LWIP/lwip-interface/lwip_stack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c b/features/FEATURE_LWIP/lwip-interface/lwip_stack.c index 999674efc0..89b4e7e8de 100644 --- a/features/FEATURE_LWIP/lwip-interface/lwip_stack.c +++ b/features/FEATURE_LWIP/lwip-interface/lwip_stack.c @@ -337,7 +337,7 @@ const char *mbed_lwip_get_netmask(char *buf, int buflen) #if LWIP_IPV4 const ip4_addr_t *addr = netif_ip4_netmask(&lwip_netif); if (!ip4_addr_isany(addr)) { - return inet_ntoa_r(addr, buf, buflen); + return ip4addr_ntoa_r(addr, buf, buflen); } else { return NULL; } @@ -351,7 +351,7 @@ char *mbed_lwip_get_gateway(char *buf, int buflen) #if LWIP_IPV4 const ip4_addr_t *addr = netif_ip4_gw(&lwip_netif); if (!ip4_addr_isany(addr)) { - return inet_ntoa_r(addr, buf, buflen); + return ip4addr_ntoa_r(addr, buf, buflen); } else { return NULL; } From febc341672f07cc170b371aa9cf0be092c34ed65 Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Tue, 4 Oct 2016 17:34:58 +0100 Subject: [PATCH 60/61] Fix spelling error: NSAPI_SECURITY_UNSUPPORTED --- features/netsocket/nsapi_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/netsocket/nsapi_types.h b/features/netsocket/nsapi_types.h index 6607199df9..4dfebd80f2 100644 --- a/features/netsocket/nsapi_types.h +++ b/features/netsocket/nsapi_types.h @@ -58,7 +58,7 @@ typedef enum nsapi_security { NSAPI_SECURITY_WPA = 0x2, /*!< phrase conforms to WPA */ NSAPI_SECURITY_WPA2 = 0x3, /*!< phrase conforms to WPA2 */ NSAPI_SECURITY_WPA_WPA2 = 0x4, /*!< phrase conforms to WPA/WPA2 */ - NSAPI_SECURITY_UNSSUPPORTED = 0xFF, /*!< unknown/unsupported security in scan results */ + NSAPI_SECURITY_UNSUPPORTED = 0xFF, /*!< unknown/unsupported security in scan results */ } nsapi_security_t; /** Maximum size of IP address representation From 4f1ededb588846df900264422ae11a44389d26bc Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 4 Oct 2016 14:23:36 -0500 Subject: [PATCH 61/61] Renamed NSAPI_SECURITY_UNSUPPORTED -> NSAPI_SECURITY_UNKNOWN Returning a wifi access point without information regarding the security type is only valid if the security type is unknown (from the perspective of the network-socket API). For clarity in situations in which scan may return an unsupported, but known security type, type name has been changed to NSAPI_SECURITY_UNKNOWN. --- features/netsocket/nsapi_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/netsocket/nsapi_types.h b/features/netsocket/nsapi_types.h index 4dfebd80f2..57e8f32622 100644 --- a/features/netsocket/nsapi_types.h +++ b/features/netsocket/nsapi_types.h @@ -58,7 +58,7 @@ typedef enum nsapi_security { NSAPI_SECURITY_WPA = 0x2, /*!< phrase conforms to WPA */ NSAPI_SECURITY_WPA2 = 0x3, /*!< phrase conforms to WPA2 */ NSAPI_SECURITY_WPA_WPA2 = 0x4, /*!< phrase conforms to WPA/WPA2 */ - NSAPI_SECURITY_UNSUPPORTED = 0xFF, /*!< unknown/unsupported security in scan results */ + NSAPI_SECURITY_UNKNOWN = 0xFF, /*!< unknown/unsupported security in scan results */ } nsapi_security_t; /** Maximum size of IP address representation