Skip to content

Commit

Permalink
LwIP v2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
ourairquality committed Jun 6, 2017
1 parent ce298e1 commit 8b93452
Show file tree
Hide file tree
Showing 19 changed files with 531 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "lwip/lwip"]
path = lwip/lwip
url = https://github.com/SuperHouse/esp-lwip.git
url = https://github.com/ourairquality/lwip.git
[submodule "extras/mbedtls/mbedtls"]
path = extras/mbedtls/mbedtls
url = https://github.com/ARMmbed/mbedtls.git
Expand Down
9 changes: 1 addition & 8 deletions core/include/sdk_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,7 @@ _Static_assert(offsetof(struct netif, state) == 28, "netif->state offset wrong!"
// lwip, so just ensure it is at the expected offset.
_Static_assert(offsetof(struct netif, hwaddr) == 41, "netif->hwaddr offset wrong!");

// Most sdk uses of the netif->flags have been converted to source code. One
// known sdk binary read of the flags remains in wl_cnx.o:sdk_cnx_sta_leave
// which checks the NETIF_FLAG_DHCP flag. The NETIF_FLAG_DHCP has been removed
// in lwip v2, so some lwip hacks are needed to handle this for now until
// wl_cnx.o is converted so source code too.
_Static_assert(offsetof(struct netif, flags) == 47, "netif->flags offset wrong!");

_Static_assert(offsetof(struct pbuf, eb) == 16, "pbuf->eb offset wrong!");
_Static_assert(offsetof(struct pbuf, esf_buf) == 16, "pbuf->esf_buf offset wrong!");


/// Misc.
Expand Down
7 changes: 4 additions & 3 deletions examples/http_get/http_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#define WEB_SERVER "chainxor.org"
#define WEB_PORT 80
#define WEB_URL "http://chainxor.org/"
#define WEB_PATH "/"

void http_get_task(void *pvParameters)
{
Expand Down Expand Up @@ -75,7 +75,8 @@ void http_get_task(void *pvParameters)
freeaddrinfo(res);

const char *req =
"GET "WEB_URL"\r\n"
"GET "WEB_PATH" HTTP/1.1\r\n"
"Host: "WEB_SERVER"\r\n"
"User-Agent: esp-open-rtos/0.1 esp8266\r\n"
"\r\n";
if (write(s, req, strlen(req)) < 0) {
Expand Down Expand Up @@ -126,6 +127,6 @@ void user_init(void)
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&config);

xTaskCreate(&http_get_task, "get_task", 256, NULL, 2, NULL);
xTaskCreate(&http_get_task, "get_task", 384, NULL, 2, NULL);
}

19 changes: 10 additions & 9 deletions extras/dhcpserver/dhcpserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
full-sized clients send us more than this. */
#define DHCP_OPTIONS_LEN 312

#include <lwip/dhcp.h>
#include <lwip/ip.h>
#include <lwip/prot/dhcp.h>

_Static_assert(sizeof(struct dhcp_msg) == offsetof(struct dhcp_msg, options) + 312, "dhcp_msg_t should have extended options size");

Expand All @@ -41,7 +42,7 @@ typedef struct {
typedef struct {
struct netconn *nc;
uint8_t max_leases;
ip_addr_t first_client_addr;
ip4_addr_t first_client_addr;
struct netif *server_if;
dhcp_lease_t *leases; /* length max_leases */
} server_state_t;
Expand All @@ -68,7 +69,7 @@ static uint8_t *add_dhcp_option_bytes(uint8_t *opt, uint8_t type, void *value, u
static dhcp_lease_t *find_lease_slot(uint8_t *hwaddr);

/* Copy IP address as dotted decimal to 'dest', must be at least 16 bytes long */
inline static void sprintf_ipaddr(const ip_addr_t *addr, char *dest)
inline static void sprintf_ipaddr(const ip4_addr_t *addr, char *dest)
{
if(addr == NULL)
sprintf(dest, "NULL");
Expand All @@ -77,7 +78,7 @@ inline static void sprintf_ipaddr(const ip_addr_t *addr, char *dest)
ip4_addr2(addr), ip4_addr3(addr), ip4_addr4(addr));
}

void dhcpserver_start(const ip_addr_t *first_client_addr, uint8_t max_leases)
void dhcpserver_start(const ip4_addr_t *first_client_addr, uint8_t max_leases)
{
/* Stop any existing running dhcpserver */
if(dhcpserver_task_handle)
Expand All @@ -89,7 +90,7 @@ void dhcpserver_start(const ip_addr_t *first_client_addr, uint8_t max_leases)
// state->server_if is assigned once the task is running - see comment in dhcpserver_task()
ip_addr_copy(state->first_client_addr, *first_client_addr);

xTaskCreate(dhcpserver_task, "DHCPServer", 768, NULL, 8, &dhcpserver_task_handle);
xTaskCreate(dhcpserver_task, "DHCP Server", 448, NULL, 2, &dhcpserver_task_handle);
}

void dhcpserver_stop(void)
Expand All @@ -113,6 +114,7 @@ static void dhcpserver_task(void *pxParameter)
}

netconn_bind(state->nc, IP_ADDR_ANY, DHCP_SERVER_PORT);
netconn_bind_if(state->nc, netif_get_index(state->server_if));

while(1)
{
Expand All @@ -134,7 +136,7 @@ static void dhcpserver_task(void *pxParameter)
state->leases[i].expires = 0;
}

ip_addr_t received_ip;
ip4_addr_t received_ip;
u16_t port;
netconn_addr(state->nc, &received_ip, &port);

Expand Down Expand Up @@ -199,8 +201,7 @@ static void handle_dhcp_discover(struct dhcp_msg *dhcpmsg)
dhcpmsg->op = DHCP_BOOTREPLY;
bzero(dhcpmsg->options, DHCP_OPTIONS_LEN);

ip_addr_copy(dhcpmsg->yiaddr, state->first_client_addr);
ip4_addr4(&(dhcpmsg->yiaddr)) += (freelease - state->leases);
dhcpmsg->yiaddr.addr = lwip_htonl(lwip_ntohl(state->first_client_addr.addr) + freelease - state->leases);

uint8_t *opt = (uint8_t *)&dhcpmsg->options;
opt = add_dhcp_option_byte(opt, DHCP_OPTION_MESSAGE_TYPE, DHCP_OFFER);
Expand All @@ -223,7 +224,7 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg)
if(dhcpmsg->hlen > NETIF_MAX_HWADDR_LEN)
return;

ip_addr_t requested_ip;
ip4_addr_t requested_ip;
uint8_t *requested_ip_opt = find_dhcp_option(dhcpmsg, DHCP_OPTION_REQUESTED_IP, 4, NULL);
if(requested_ip_opt) {
memcpy(&requested_ip.addr, requested_ip_opt, 4);
Expand Down
4 changes: 2 additions & 2 deletions extras/dhcpserver/include/dhcpserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ extern "C" {
to a client. Subsequent lease addresses are calculated by
incrementing the final octet of the IPv4 address, up to max_leases.
*/
void dhcpserver_start(const ip_addr_t *first_client_addr, uint8_t max_leases);
void dhcpserver_start(const ip4_addr_t *first_client_addr, uint8_t max_leases);

void dhcpserver_get_lease(const ip_addr_t *first_client_addr, uint8_t max_leases);
void dhcpserver_get_lease(const ip4_addr_t *first_client_addr, uint8_t max_leases);

/* Stop DHCP server.
*/
Expand Down
10 changes: 5 additions & 5 deletions extras/sntp/sntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@
#define SNTP_STARTUP_DELAY 0
#endif

/** SNTP receive timeout - in milliseconds
/** SNTP receive timeout - in seconds
* Also used as retry timeout - this shouldn't be too low.
* Default is 3 seconds.
*/
#ifndef SNTP_RECV_TIMEOUT
#define SNTP_RECV_TIMEOUT 3000
#define SNTP_RECV_TIMEOUT 3
#endif

/** SNTP update delay - in milliseconds
Expand Down Expand Up @@ -384,8 +384,8 @@ sntp_request(void *arg)
/* bind to local address */
if (lwip_bind(sock, (struct sockaddr *)&local, sizeof(local)) == 0) {
/* set recv timeout */
timeout = SNTP_RECV_TIMEOUT;
lwip_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
const struct timeval timeout = { SNTP_RECV_TIMEOUT, 0 };
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));

/* prepare SNTP request */
sntp_initialize_request(&sntpmsg);
Expand Down Expand Up @@ -611,7 +611,7 @@ sntp_send_request(ip_addr_t *server_addr)
pbuf_free(p);

/* set up receive timeout: try next server or retry on timeout */
sys_timeout((u32_t)SNTP_RECV_TIMEOUT, sntp_try_next_server, NULL);
sys_timeout((u32_t)SNTP_RECV_TIMEOUT * 1000, sntp_try_next_server, NULL);
#if SNTP_CHECK_RESPONSE >= 1
/* save server address to verify it in sntp_recv */
ip_addr_set(&sntp_last_server_address, server_addr);
Expand Down
6 changes: 3 additions & 3 deletions include/espressif/esp_wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ enum {
};

struct ip_info {
struct ip_addr ip;
struct ip_addr netmask;
struct ip_addr gw;
struct ip4_addr ip;
struct ip4_addr netmask;
struct ip4_addr gw;
};

bool sdk_wifi_get_ip_info(uint8_t if_index, struct ip_info *info);
Expand Down
4 changes: 2 additions & 2 deletions include/espressif/sdk_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
extern "C" {
#endif

struct ip_addr;
struct ip4_addr;

/*********************************************
* Defined in libmain.a
Expand All @@ -45,7 +45,7 @@ void sdk_os_putc(char c);

/* Called when an IP gets set on the "station" (client) interface.
*/
void sdk_system_station_got_ip_set(struct ip_addr *ip_addr, struct ip_addr *sn_mask, struct ip_addr *gw_addr);
void sdk_system_station_got_ip_set(struct ip4_addr *ip_addr, struct ip4_addr *sn_mask, struct ip4_addr *gw_addr);

/* This is a no-op wrapper around ppRecycleRxPkt, which is defined in libpp.a
Expand Down
Binary file modified lib/libnet80211.a
Binary file not shown.
3 changes: 0 additions & 3 deletions lwip/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ INC_DIRS += $(LWIP_DIR)include $(ROOT)lwip/include $(lwip_ROOT)include $(LWIP_DI
lwip_INC_DIR = # all in INC_DIRS, needed for normal operation
lwip_SRC_DIR = $(lwip_ROOT) $(LWIP_DIR)api $(LWIP_DIR)core $(LWIP_DIR)core/ipv4 $(LWIP_DIR)netif

# LWIP 1.4.1 generates a single warning so we need to disable -Werror when building it
lwip_CFLAGS = $(CFLAGS) -Wno-address

$(eval $(call component_compile_rules,lwip))

# Helpful error if git submodule not initialised
Expand Down
12 changes: 9 additions & 3 deletions lwip/esp_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ low_level_output(struct netif *netif, struct pbuf *p)
{
struct pbuf *q;

for(q = p; q != NULL; q = q->next) {
sdk_ieee80211_output_pbuf(netif, q);
if (p->next) {
q = pbuf_clone(PBUF_RAW, PBUF_RAM, p);
if (q == NULL) {
return ERR_MEM;
}
p = q;
}

sdk_ieee80211_output_pbuf(netif, p);

LINK_STATS_INC(link.xmit);

return ERR_OK;
Expand Down Expand Up @@ -88,7 +94,7 @@ err_t ethernetif_init(struct netif *netif)

/* low_level_init components */
netif->hwaddr_len = 6;
/* hwaddr seems to be set elsewhere, or (more likely) is set on tx by MAC layer */
/* The hwaddr is set by sdk_wifi_station_start or sdk_wifi_softap_start. */
netif->mtu = 1500;
netif->flags = NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;

Expand Down
13 changes: 2 additions & 11 deletions lwip/include/arch/cc.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,6 @@

#define ERRNO

#define BYTE_ORDER LITTLE_ENDIAN

/** @todo fix some warnings: don't use #pragma if compiling with cygwin gcc */
#ifndef __GNUC__
#include <limits.h>
#pragma warning (disable: 4244) /* disable conversion warning (implicit integer promotion!) */
#pragma warning (disable: 4127) /* conditional expression is constant */
#pragma warning (disable: 4996) /* 'strncpy' was declared deprecated */
#pragma warning (disable: 4103) /* structure packing changed by including file */
#endif

/* Define generic types used in lwIP */
typedef uint8_t u8_t;
typedef int8_t s8_t;
Expand Down Expand Up @@ -112,4 +101,6 @@ typedef int sys_prot_t;
#define LWIP_PLATFORM_HTONS(_n) ((u16_t)((((_n) & 0xff) << 8) | (((_n) >> 8) & 0xff)))
#define LWIP_PLATFORM_HTONL(_n) ((u32_t)( (((_n) & 0xff) << 24) | (((_n) & 0xff00) << 8) | (((_n) >> 8) & 0xff00) | (((_n) >> 24) & 0xff) ))

#define LWIP_RAND() rand()

#endif /* __ARCH_CC_H__ */
Loading

0 comments on commit 8b93452

Please sign in to comment.