From 85d6292b8f8738bb6d56055c3aeb1bb2031839ef Mon Sep 17 00:00:00 2001 From: Dmitry Frank Date: Wed, 19 Oct 2016 22:12:12 +0300 Subject: [PATCH] Add nRF port for Mongoose Example is not yet added PUBLISHED_FROM=2732386091a0d4cd8d4c6e64dc16467780ec72a5 --- mongoose.c | 59 ++++++++++++++++++++++++++++++++++++++++-------------- mongoose.h | 32 +++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 15 deletions(-) diff --git a/mongoose.c b/mongoose.c index 5ec12537..26e42e9e 100644 --- a/mongoose.c +++ b/mongoose.c @@ -11327,6 +11327,35 @@ void mg_lwip_mgr_schedule_poll(struct mg_mgr *mgr); /* Amalgamated: #include "common/cs_dbg.h" */ +/* + * Depending on whether Mongoose is compiled with ipv6 support, use right + * lwip functions + */ +#if MG_ENABLE_IPV6 +# define TCP_NEW tcp_new_ip6 +# define TCP_BIND tcp_bind_ip6 +# define UDP_BIND udp_bind_ip6 +# define IPADDR_NTOA ip6addr_ntoa +# define SET_ADDR(dst, src) \ + memcpy((dst)->sin6.sin6_addr.s6_addr, (src)->ip6.addr, \ + sizeof((dst)->sin6.sin6_addr.s6_addr)) +#else +# define TCP_NEW tcp_new +# define TCP_BIND tcp_bind +# define UDP_BIND udp_bind +# define IPADDR_NTOA ipaddr_ntoa +# define SET_ADDR(dst, src) (dst)->sin.sin_addr.s_addr = GET_IPV4(src) +#endif + +/* + * If lwip is compiled with ipv6 support, then API changes even for ipv4 + */ +#if !defined(LWIP_IPV6) || !LWIP_IPV6 +# define GET_IPV4(ipX_addr) ((ipX_addr)->addr) +#else +# define GET_IPV4(ipX_addr) ((ipX_addr)->ip4.addr) +#endif + void mg_lwip_ssl_do_hs(struct mg_connection *nc); void mg_lwip_ssl_send(struct mg_connection *nc); void mg_lwip_ssl_recv(struct mg_connection *nc); @@ -11354,7 +11383,7 @@ void mg_lwip_set_keepalive_params(struct mg_connection *nc, int idle, static err_t mg_lwip_tcp_conn_cb(void *arg, struct tcp_pcb *tpcb, err_t err) { struct mg_connection *nc = (struct mg_connection *) arg; - DBG(("%p connect to %s:%u = %d", nc, ipaddr_ntoa(&tpcb->remote_ip), + DBG(("%p connect to %s:%u = %d", nc, IPADDR_NTOA(&tpcb->remote_ip), tpcb->remote_port, err)); if (nc == NULL) { tcp_abort(tpcb); @@ -11486,7 +11515,7 @@ static err_t mg_lwip_tcp_sent_cb(void *arg, struct tcp_pcb *tpcb, void mg_if_connect_tcp(struct mg_connection *nc, const union socket_address *sa) { struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock; - struct tcp_pcb *tpcb = tcp_new(); + struct tcp_pcb *tpcb = TCP_NEW(); cs->pcb.tcp = tpcb; ip_addr_t *ip = (ip_addr_t *) &sa->sin.sin_addr.s_addr; u16_t port = ntohs(sa->sin.sin_port); @@ -11494,7 +11523,7 @@ void mg_if_connect_tcp(struct mg_connection *nc, tcp_err(tpcb, mg_lwip_tcp_error_cb); tcp_sent(tpcb, mg_lwip_tcp_sent_cb); tcp_recv(tpcb, mg_lwip_tcp_recv_cb); - cs->err = tcp_bind(tpcb, IP_ADDR_ANY, 0 /* any port */); + cs->err = TCP_BIND(tpcb, IP_ADDR_ANY, 0 /* any port */); DBG(("%p tcp_bind = %d", nc, cs->err)); if (cs->err != ERR_OK) { mg_lwip_post_signal(MG_SIG_CONNECT_RESULT, nc); @@ -11515,7 +11544,7 @@ static void mg_lwip_udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p, char *data = (char *) malloc(len); union socket_address sa; (void) pcb; - DBG(("%p %s:%u %u", nc, ipaddr_ntoa(addr), port, p->len)); + DBG(("%p %s:%u %u", nc, IPADDR_NTOA(addr), port, p->len)); if (data == NULL) { DBG(("OOM")); pbuf_free(p); @@ -11531,7 +11560,7 @@ static void mg_lwip_udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p, void mg_if_connect_udp(struct mg_connection *nc) { struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock; struct udp_pcb *upcb = udp_new(); - cs->err = udp_bind(upcb, IP_ADDR_ANY, 0 /* any port */); + cs->err = UDP_BIND(upcb, IP_ADDR_ANY, 0 /* any port */); DBG(("%p udp_bind %p = %d", nc, upcb, cs->err)); if (cs->err == ERR_OK) { udp_recv(upcb, mg_lwip_udp_recv_cb, nc); @@ -11544,7 +11573,7 @@ void mg_if_connect_udp(struct mg_connection *nc) { void mg_lwip_accept_conn(struct mg_connection *nc, struct tcp_pcb *tpcb) { union socket_address sa; - sa.sin.sin_addr.s_addr = tpcb->remote_ip.addr; + SET_ADDR(&sa, &tpcb->remote_ip); sa.sin.sin_port = htons(tpcb->remote_port); mg_if_accept_tcp_cb(nc, &sa, sizeof(sa.sin)); } @@ -11552,7 +11581,7 @@ void mg_lwip_accept_conn(struct mg_connection *nc, struct tcp_pcb *tpcb) { static err_t mg_lwip_accept_cb(void *arg, struct tcp_pcb *newtpcb, err_t err) { struct mg_connection *lc = (struct mg_connection *) arg; (void) err; - DBG(("%p conn %p from %s:%u", lc, newtpcb, ipaddr_ntoa(&newtpcb->remote_ip), + DBG(("%p conn %p from %s:%u", lc, newtpcb, IPADDR_NTOA(&newtpcb->remote_ip), newtpcb->remote_port)); struct mg_connection *nc = mg_if_accept_new_conn(lc); if (nc == NULL) { @@ -11585,11 +11614,11 @@ static err_t mg_lwip_accept_cb(void *arg, struct tcp_pcb *newtpcb, err_t err) { int mg_if_listen_tcp(struct mg_connection *nc, union socket_address *sa) { struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock; - struct tcp_pcb *tpcb = tcp_new(); + struct tcp_pcb *tpcb = TCP_NEW(); ip_addr_t *ip = (ip_addr_t *) &sa->sin.sin_addr.s_addr; u16_t port = ntohs(sa->sin.sin_port); - cs->err = tcp_bind(tpcb, ip, port); - DBG(("%p tcp_bind(%s:%u) = %d", nc, ipaddr_ntoa(ip), port, cs->err)); + cs->err = TCP_BIND(tpcb, ip, port); + DBG(("%p tcp_bind(%s:%u) = %d", nc, IPADDR_NTOA(ip), port, cs->err)); if (cs->err != ERR_OK) { tcp_close(tpcb); return -1; @@ -11606,8 +11635,8 @@ int mg_if_listen_udp(struct mg_connection *nc, union socket_address *sa) { struct udp_pcb *upcb = udp_new(); ip_addr_t *ip = (ip_addr_t *) &sa->sin.sin_addr.s_addr; u16_t port = ntohs(sa->sin.sin_port); - cs->err = udp_bind(upcb, ip, port); - DBG(("%p udb_bind(%s:%u) = %d", nc, ipaddr_ntoa(ip), port, cs->err)); + cs->err = UDP_BIND(upcb, ip, port); + DBG(("%p udb_bind(%s:%u) = %d", nc, IPADDR_NTOA(ip), port, cs->err)); if (cs->err != ERR_OK) { udp_remove(upcb); return -1; @@ -11759,16 +11788,16 @@ void mg_if_get_conn_addr(struct mg_connection *nc, int remote, memcpy(sa, &nc->sa, sizeof(*sa)); } else { sa->sin.sin_port = htons(upcb->local_port); - sa->sin.sin_addr.s_addr = upcb->local_ip.addr; + SET_ADDR(sa, &upcb->local_ip); } } else { struct tcp_pcb *tpcb = cs->pcb.tcp; if (remote) { sa->sin.sin_port = htons(tpcb->remote_port); - sa->sin.sin_addr.s_addr = tpcb->remote_ip.addr; + SET_ADDR(sa, &tpcb->remote_ip); } else { sa->sin.sin_port = htons(tpcb->local_port); - sa->sin.sin_addr.s_addr = tpcb->local_ip.addr; + SET_ADDR(sa, &tpcb->local_ip); } } } diff --git a/mongoose.h b/mongoose.h index ff5fd114..e6edf74b 100644 --- a/mongoose.h +++ b/mongoose.h @@ -51,6 +51,7 @@ #define CS_P_MBED 7 #define CS_P_WINCE 8 #define CS_P_NXP_KINETIS 9 +#define CS_P_NRF52 10 /* If not specified explicitly, we guess platform by defines. */ #ifndef CS_PLATFORM @@ -88,6 +89,7 @@ /* Amalgamated: #include "common/platforms/platform_cc3200.h" */ /* Amalgamated: #include "common/platforms/platform_cc3100.h" */ /* Amalgamated: #include "common/platforms/platform_mbed.h" */ +/* Amalgamated: #include "common/platforms/platform_nrf52.h" */ /* Amalgamated: #include "common/platforms/platform_wince.h" */ /* Amalgamated: #include "common/platforms/platform_nxp_kinetis.h" */ @@ -766,6 +768,36 @@ int _stat(const char *pathname, struct stat *st); #endif /* CS_PLATFORM == CS_P_MBED */ #endif /* CS_COMMON_PLATFORMS_PLATFORM_MBED_H_ */ #ifdef MG_MODULE_LINES +#line 1 "common/platforms/platform_nrf52.h" +#endif +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ +#ifndef CS_COMMON_PLATFORMS_PLATFORM_NRF52_H_ +#define CS_COMMON_PLATFORMS_PLATFORM_NRF52_H_ +#if CS_PLATFORM == CS_P_NRF52 + +#include +#include +#include +#include +#include +#include +#include + +#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL +#define LWIP_TIMEVAL_PRIVATE 0 +#define LWIP_PROVIDE_ERRNO 1 +#define MG_LWIP 1 +#define MG_ENABLE_IPV6 1 + +#define INT64_FMT PRId64 +#define SIZE_T_FMT "u" + +#endif /* CS_PLATFORM == CS_P_NRF52 */ +#endif /* CS_COMMON_PLATFORMS_PLATFORM_NRF52_H_ */ +#ifdef MG_MODULE_LINES #line 1 "common/platforms/simplelink/cs_simplelink.h" #endif /*