From df9e9bf9a4cc2b54114be835e7fa5cd93178e53c Mon Sep 17 00:00:00 2001
From: Angus Gratton <gus@projectgus.com>
Date: Thu, 21 May 2015 12:06:55 +1000
Subject: [PATCH] Working from-source IP stack

---
 examples/http_get/main.c |  2 +-
 lwip/esp_interface.c     | 88 ++++++++++++++++++++++++++++------------
 lwip/include/arch/cc.h   | 12 +++---
 lwip/include/lwipopts.h  |  3 ++
 4 files changed, 71 insertions(+), 34 deletions(-)

diff --git a/examples/http_get/main.c b/examples/http_get/main.c
index 7d1b4d8..e08d895 100644
--- a/examples/http_get/main.c
+++ b/examples/http_get/main.c
@@ -116,7 +116,7 @@ void user_init(void)
     };
 
     /* required to call wifi_set_opmode before station_set_config */
-    wifi_set_opmode(STATIONAP_MODE);
+    wifi_set_opmode(STATION_MODE);
     wifi_station_set_config(&config);
 
     xTaskCreate(&http_get_task, (signed char *)"get_task", 256, NULL, 2, NULL);
diff --git a/lwip/esp_interface.c b/lwip/esp_interface.c
index 587f8d8..034c1b6 100644
--- a/lwip/esp_interface.c
+++ b/lwip/esp_interface.c
@@ -1,7 +1,42 @@
-/* LWIP interface to the ESP SDK MAC layer
+/* LWIP interface to the ESP WLAN MAC layer driver.
 
-   These two symbols are called from libnet80211.a
-*/
+   Based on the sample driver in ethernetif.c. Tweaks based on
+   examining esp-lwip eagle_if.c and observed behaviour of the ESP
+   RTOS liblwip.a.
+
+   libnet80211.a calls into ethernetif_init & ethernetif_input.
+
+ * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
+ *
+ * All rights reserved. 
+ * 
+ * Redistribution and use in source and binary forms, with or without modification, 
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission. 
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
+ * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
+ * OF SUCH DAMAGE.
+ *
+ * This file is part of the lwIP TCP/IP stack.
+ * 
+ * Original Author: Simon Goldschmidt
+ * Modified by Angus Gratton based on work by @kadamski/Espressif via esp-lwip project.
+ */
 #include "lwip/opt.h"
 
 #include "lwip/def.h"
@@ -10,11 +45,9 @@
 #include <lwip/stats.h>
 #include <lwip/snmp.h>
 #include "netif/etharp.h"
-#include "netif/ppp_oe.h"
 
 /* declared in libnet80211.a */
 int8_t ieee80211_output_pbuf(struct netif *ifp, struct pbuf* pb);
-//sint8 ieee80211_output_pbuf(struct ieee80211_conn *conn, esf_buf *eb);
 
 static err_t
 low_level_output(struct netif *netif, struct pbuf *p)
@@ -47,18 +80,17 @@ err_t ethernetif_init(struct netif *netif)
    */
   NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
 
-  //netif->state = (void *)0xDEADBEEF; /* this appears unused by esp layer */
+  // don't touch netif->state here, the field is used internally in the ESP SDK layers
   netif->name[0] = 'e';
   netif->name[1] = 'n';
   netif->output = etharp_output;
-  /* NB: in the SDK there's a static wrapper function to this, not sure what it does!! */
   netif->linkoutput = low_level_output;
 
-  /* low level init components */
+  /* low_level_init components */
   netif->hwaddr_len = 6;
-  /* hwaddr seems to be set elsewhere, or (more likely) is per-packet by MAC layer */
+  /* hwaddr seems to be set elsewhere, or (more likely) is set on tx by MAC layer */
   netif->mtu = 1500;
-  netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
+  netif->flags = NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
 
   return ERR_OK;
 }
@@ -69,22 +101,24 @@ void ethernetif_input(struct netif *netif, struct pbuf *p)
     struct eth_hdr *ethhdr = p->payload;
   /* examine packet payloads ethernet header */
 
-  switch (htons(ethhdr->type)) {
-  /* IP or ARP packet? */
-  case ETHTYPE_IP:
-  case ETHTYPE_ARP:
-//  case ETHTYPE_IPV6:
-    /* full packet send to tcpip_thread to process */
-    if (netif->input(p, netif)!=ERR_OK)
-     { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
-       pbuf_free(p);
-       p = NULL;
-     }
-    break;
 
-  default:
-    pbuf_free(p);
-    p = NULL;
-    break;
-  }
+    switch(htons(ethhdr->type)) {
+	/* IP or ARP packet? */
+    case ETHTYPE_IP:
+    case ETHTYPE_ARP:
+//  case ETHTYPE_IPV6:
+	/* full packet send to tcpip_thread to process */
+	if (netif->input(p, netif)!=ERR_OK)
+	{
+	    LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
+	    pbuf_free(p);
+	    p = NULL;
+	}
+	break;
+
+    default:
+	pbuf_free(p);
+	p = NULL;
+	break;
+    }
 }
diff --git a/lwip/include/arch/cc.h b/lwip/include/arch/cc.h
index 828b381..67989f6 100644
--- a/lwip/include/arch/cc.h
+++ b/lwip/include/arch/cc.h
@@ -63,12 +63,12 @@ typedef int sys_prot_t;
 
 /* Define (sn)printf formatters for these lwIP types */
 #define X8_F  "02x"
-#define U16_F "hu"
-#define S16_F "hd"
-#define X16_F "hx"
-#define U32_F "lu"
-#define S32_F "ld"
-#define X32_F "lx"
+#define U16_F "u"
+#define S16_F "d"
+#define X16_F "x"
+#define U32_F "u"
+#define S32_F "d"
+#define X32_F "x"
 #define SZT_F U32_F
 
 /* Compiler hints for packing structures */
diff --git a/lwip/include/lwipopts.h b/lwip/include/lwipopts.h
index 761ea33..e4b3955 100644
--- a/lwip/include/lwipopts.h
+++ b/lwip/include/lwipopts.h
@@ -38,6 +38,9 @@
 #define EBUF_LWIP                           1
 #define ESP_TIMEWAIT_THRESHOLD              10000
 
+// Uncomment this line, and set the debug options you want below, for IP stack debug output
+//#define LWIP_DEBUG
+
 /*
    -----------------------------------------------
    ---------- Platform specific locking ----------