LWIPBP3595Interface library for mbed-os.

Dependents:   LWIPBP3595Interface_STA_for_mbed-os

Fork of LWIPBP3595Interface by Rohm

Revision:
5:d03489ec5033
Parent:
3:2ff2514e4fca
Child:
6:993197aaf5a4
--- a/lwip_wifi_stack.c	Fri Oct 28 06:27:14 2016 +0000
+++ b/lwip_wifi_stack.c	Thu Nov 24 09:13:23 2016 +0000
@@ -104,7 +104,7 @@
 /* TCP/IP and Network Interface Initialisation */
 static struct netif lwip_netif;
 static bool lwip_dhcp = false;
-static char lwip_mac_address[NSAPI_MAC_SIZE] = "\0";
+static char lwip_mac_address[NSAPI_MAC_SIZE];
 
 #if !LWIP_IPV4 || !LWIP_IPV6
 static bool all_zeros(const uint8_t *p, int len)
@@ -130,7 +130,8 @@
 #if !LWIP_IPV4
     /* For bind() and other purposes, need to accept "null" of other type */
     /* (People use IPv4 0.0.0.0 as a general null) */
-    if (in->version == NSAPI_IPv4 && all_zeros(in->bytes, 4)) {
+    if (in->version == NSAPI_UNSPEC ||
+        (in->version == NSAPI_IPv4 && all_zeros(in->bytes, 4))) {
         ip_addr_set_zero_ip6(out);
         return true;
     }
@@ -145,13 +146,25 @@
     }
 #if !LWIP_IPV6
     /* For symmetry with above, accept IPv6 :: as a general null */
-    if (in->version == NSAPI_IPv6 && all_zeros(in->bytes, 16)) {
+    if (in->version == NSAPI_UNSPEC ||
+        (in->version == NSAPI_IPv6 && all_zeros(in->bytes, 16))) {
         ip_addr_set_zero_ip4(out);
         return true;
     }
 #endif
 #endif
 
+#if LWIP_IPV4 && LWIP_IPV6
+    if (in->version == NSAPI_UNSPEC) {
+#if IP_VERSION_PREF == PREF_IPV4
+        ip_addr_set_zero_ip4(out);
+#else
+        ip_addr_set_zero_ip6(out);
+#endif
+        return true;
+    }
+#endif
+
     return false;
 }
 
@@ -295,7 +308,7 @@
 
 static void mbed_lwip_wifi_set_mac_address(void)
 {
-    snprintf(lwip_mac_address, 19, "%02x:%02x:%02x:%02x:%02x:%02x", lwip_netif.hwaddr[0], lwip_netif.hwaddr[1],
+    snprintf(lwip_mac_address, NSAPI_MAC_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x", lwip_netif.hwaddr[0], lwip_netif.hwaddr[1],
              lwip_netif.hwaddr[2], lwip_netif.hwaddr[3], lwip_netif.hwaddr[4], lwip_netif.hwaddr[5]);
 }
 
@@ -305,7 +318,7 @@
     return lwip_mac_address[0] ? lwip_mac_address : 0;
 }
 
-char *mbed_lwip_wifi_get_ip_address(char *buf, int buflen)
+char *mbed_lwip_wifi_get_ip_address(char *buf, nsapi_size_t buflen)
 {
     const ip_addr_t *addr = mbed_lwip_wifi_get_ip_addr(true, &lwip_netif);
     if (!addr) {
@@ -324,7 +337,7 @@
     return NULL;
 }
 
-const char *mbed_lwip_wifi_get_netmask(char *buf, int buflen)
+const char *mbed_lwip_wifi_get_netmask(char *buf, nsapi_size_t buflen)
 {
 #if LWIP_IPV4
     const ip4_addr_t *addr = netif_ip4_netmask(&lwip_netif);
@@ -338,7 +351,7 @@
 #endif
 }
 
-char *mbed_lwip_wifi_get_gateway(char *buf, int buflen)
+char *mbed_lwip_wifi_get_gateway(char *buf, nsapi_size_t buflen)
 {
 #if LWIP_IPV4
     const ip4_addr_t *addr = netif_ip4_gw(&lwip_netif);
@@ -352,7 +365,7 @@
 #endif
 }
 
-int mbed_lwip_wifi_init(emac_interface_t *emac)
+nsapi_error_t mbed_lwip_wifi_init(emac_interface_t *emac)
 {
     // Check if we've already brought up lwip
     if (!mbed_lwip_wifi_get_mac_address()) {
@@ -388,7 +401,7 @@
     return NSAPI_ERROR_OK;
 }
 
-int mbed_lwip_wifi_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
+nsapi_error_t mbed_lwip_wifi_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
 {
     // Check if we've already connected
     if (lwip_connected) {
@@ -488,7 +501,7 @@
     return 0;
 }
 
-int mbed_lwip_wifi_bringdown(void)
+nsapi_error_t mbed_lwip_wifi_bringdown(void)
 {
     // Check if we've connected
     if (!lwip_connected) {
@@ -512,7 +525,7 @@
 }
 
 /* LWIP error remapping */
-static int mbed_lwip_wifi_err_remap(err_t err) {
+static nsapi_error_t mbed_lwip_wifi_err_remap(err_t err) {
     switch (err) {
         case ERR_OK:
         case ERR_CLSD:
@@ -538,7 +551,7 @@
 }
 
 /* LWIP network stack implementation */
-static int mbed_lwip_wifi_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version)
+static nsapi_error_t mbed_lwip_wifi_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version)
 {
     ip_addr_t lwip_addr;
 
@@ -579,7 +592,7 @@
     return 0;
 }
 
-static int mbed_lwip_wifi_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto)
+static nsapi_error_t mbed_lwip_wifi_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto)
 {
     // check if network is connected
     if (!lwip_connected) {
@@ -622,7 +635,7 @@
     return 0;
 }
 
-static int mbed_lwip_wifi_socket_close(nsapi_stack_t *stack, nsapi_socket_t handle)
+static nsapi_error_t mbed_lwip_wifi_socket_close(nsapi_stack_t *stack, nsapi_socket_t handle)
 {
     struct lwip_socket *s = (struct lwip_socket *)handle;
 
@@ -631,7 +644,7 @@
     return mbed_lwip_wifi_err_remap(err);
 }
 
-static int mbed_lwip_wifi_socket_bind(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port)
+static nsapi_error_t mbed_lwip_wifi_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;
@@ -649,7 +662,7 @@
     return mbed_lwip_wifi_err_remap(err);
 }
 
-static int mbed_lwip_wifi_socket_listen(nsapi_stack_t *stack, nsapi_socket_t handle, int backlog)
+static nsapi_error_t mbed_lwip_wifi_socket_listen(nsapi_stack_t *stack, nsapi_socket_t handle, int backlog)
 {
     struct lwip_socket *s = (struct lwip_socket *)handle;
 
@@ -657,7 +670,7 @@
     return mbed_lwip_wifi_err_remap(err);
 }
 
-static int mbed_lwip_wifi_socket_connect(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port)
+static nsapi_error_t mbed_lwip_wifi_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;
@@ -673,7 +686,7 @@
     return mbed_lwip_wifi_err_remap(err);
 }
 
-static int mbed_lwip_wifi_socket_accept(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *handle, nsapi_addr_t *addr, uint16_t *port)
+static nsapi_error_t mbed_lwip_wifi_socket_accept(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *handle, nsapi_addr_t *addr, uint16_t *port)
 {
     struct lwip_socket *s = (struct lwip_socket *)server;
     struct lwip_socket *ns = mbed_lwip_wifi_arena_alloc();
@@ -697,7 +710,7 @@
     return 0;
 }
 
-static int mbed_lwip_wifi_socket_send(nsapi_stack_t *stack, nsapi_socket_t handle, const void *data, unsigned size)
+static nsapi_size_or_error_t mbed_lwip_wifi_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;
@@ -707,10 +720,10 @@
         return mbed_lwip_wifi_err_remap(err);
     }
 
-    return (int)bytes_written;
+    return (nsapi_size_or_error_t)bytes_written;
 }
 
-static int mbed_lwip_wifi_socket_recv(nsapi_stack_t *stack, nsapi_socket_t handle, void *data, unsigned size)
+static nsapi_size_or_error_t mbed_lwip_wifi_socket_recv(nsapi_stack_t *stack, nsapi_socket_t handle, void *data, unsigned size)
 {
     struct lwip_socket *s = (struct lwip_socket *)handle;
 
@@ -734,7 +747,7 @@
     return recv;
 }
 
-static int mbed_lwip_wifi_socket_sendto(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port, const void *data, unsigned size)
+static nsapi_size_or_error_t mbed_lwip_wifi_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;
@@ -759,7 +772,7 @@
     return size;
 }
 
-static int mbed_lwip_wifi_socket_recvfrom(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t *addr, uint16_t *port, void *data, unsigned size)
+static nsapi_size_or_error_t mbed_lwip_wifi_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;
@@ -778,7 +791,7 @@
     return recv;
 }
 
-static int mbed_lwip_wifi_setsockopt(nsapi_stack_t *stack, nsapi_socket_t handle, int level, int optname, const void *optval, unsigned optlen)
+static nsapi_error_t mbed_lwip_wifi_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;