Skip to content

Commit

Permalink
prepare lwip2 (#3129)
Browse files Browse the repository at this point in the history
* minimum changes for libraries to compile with lwip2

* add WiFiClient::availableForWrite() (similar to Serial::) which indicates how much data can be sent without buffering
  • Loading branch information
d-a-v authored and igrr committed Apr 25, 2017
1 parent a922426 commit a41f55c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
12 changes: 11 additions & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern "C" {
#include "lwip/opt.h"
#include "lwip/err.h"
#include "lwip/dns.h"
#include "lwip/init.h" // LWIP_VERSION_
}

#include "WiFiClient.h"
Expand Down Expand Up @@ -419,7 +420,11 @@ bool ESP8266WiFiGenericClass::forceSleepWake() {
// ------------------------------------------------ Generic Network function ---------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------

#if LWIP_VERSION_MAJOR == 1
void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg);
#else
void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg);
#endif

/**
* Resolve the given hostname to an IP address.
Expand Down Expand Up @@ -465,7 +470,12 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
* @param ipaddr
* @param callback_arg
*/
void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg) {
#if LWIP_VERSION_MAJOR == 1
void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg)
#else
void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
#endif
{
(void) name;
if(ipaddr) {
(*reinterpret_cast<IPAddress*>(callback_arg)) = ipaddr->addr;
Expand Down
6 changes: 6 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern "C" {
#include "smartconfig.h"
#include "lwip/err.h"
#include "lwip/dns.h"
#include "lwip/init.h" // LWIP_VERSION_
}

#include "debug.h"
Expand Down Expand Up @@ -400,8 +401,13 @@ IPAddress ESP8266WiFiSTAClass::gatewayIP() {
* @return IPAddress DNS Server IP
*/
IPAddress ESP8266WiFiSTAClass::dnsIP(uint8_t dns_no) {
#if LWIP_VERSION_MAJOR == 1
ip_addr_t dns_ip = dns_getserver(dns_no);
return IPAddress(dns_ip.addr);
#else
const ip_addr_t* dns_ip = dns_getserver(dns_no);
return IPAddress(dns_ip->addr);
#endif
}


Expand Down
7 changes: 7 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
// if the default interface is down, tcp_connect exits early without
// ever calling tcp_err
// http://lists.gnu.org/archive/html/lwip-devel/2010-05/msg00001.html
#if LWIP_VERSION_MAJOR == 1
netif* interface = ip_route(&addr);
if (!interface) {
DEBUGV("no route to host\r\n");
return 0;
}
#endif

tcp_pcb* pcb = tcp_new();
if (!pcb)
Expand Down Expand Up @@ -163,6 +165,11 @@ bool WiFiClient::getNoDelay() {
return _client->getNoDelay();
}

size_t WiFiClient::availableForWrite ()
{
return _client->availableForWrite();
}

size_t WiFiClient::write(uint8_t b)
{
return write(&b, 1);
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class WiFiClient : public Client, public SList<WiFiClient> {
void setNoDelay(bool nodelay);
static void setLocalPortStart(uint16_t port) { _localPort = port; }

size_t availableForWrite();

friend class WiFiServer;

using Print::write;
Expand Down
5 changes: 5 additions & 0 deletions libraries/ESP8266WiFi/src/include/ClientContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ class ClientContext
}
}

size_t availableForWrite ()
{
return _pcb? tcp_sndbuf(_pcb): 0;
}

void setNoDelay(bool nodelay)
{
if(!_pcb) {
Expand Down
23 changes: 20 additions & 3 deletions libraries/ESP8266WiFi/src/include/UdpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@

class UdpContext;

extern "C" void esp_yield();
extern "C" void esp_schedule();
extern "C" {
void esp_yield();
void esp_schedule();
#include "lwip/init.h" // LWIP_VERSION_
}


#define GET_IP_HDR(pb) reinterpret_cast<ip_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN - IP_HLEN);
#define GET_UDP_HDR(pb) reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN);
Expand Down Expand Up @@ -104,10 +108,17 @@ class UdpContext
udp_disconnect(_pcb);
}

#if LWIP_VERSION_MAJOR == 1
void setMulticastInterface(ip_addr_t addr)
{
udp_set_multicast_netif_addr(_pcb, addr);
}
#else
void setMulticastInterface(const ip_addr_t& addr)
{
udp_set_multicast_netif_addr(_pcb, &addr);
}
#endif

void setMulticastTTL(int ttl)
{
Expand Down Expand Up @@ -328,7 +339,7 @@ class UdpContext
}

void _recv(udp_pcb *upcb, pbuf *pb,
ip_addr_t *addr, u16_t port)
const ip_addr_t *addr, u16_t port)
{
(void) upcb;
(void) addr;
Expand All @@ -353,9 +364,15 @@ class UdpContext
}


#if LWIP_VERSION_MAJOR == 1
static void _s_recv(void *arg,
udp_pcb *upcb, pbuf *p,
ip_addr_t *addr, u16_t port)
#else
static void _s_recv(void *arg,
udp_pcb *upcb, pbuf *p,
const ip_addr_t *addr, u16_t port)
#endif
{
reinterpret_cast<UdpContext*>(arg)->_recv(upcb, p, addr, port);
}
Expand Down

0 comments on commit a41f55c

Please sign in to comment.