diff --git a/cores/esp8266/LwipDhcpServer-NonOS.cpp b/cores/esp8266/LwipDhcpServer-NonOS.cpp new file mode 100644 index 0000000000..0a0f0b77b1 --- /dev/null +++ b/cores/esp8266/LwipDhcpServer-NonOS.cpp @@ -0,0 +1,64 @@ +/* + lwIPDhcpServer-NonOS.cpp - DHCP server wrapper + + Copyright (c) 2020 esp8266 arduino. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +// STARTS/STOPS DHCP SERVER ON WIFI AP INTERFACE +// these functions must exists as-is with "C" interface, +// nonos-sdk calls them at boot time and later + +#include // LWIP_VERSION + +#include +#include "LwipDhcpServer.h" + +extern netif netif_git[2]; + +// global DHCP instance for softAP interface +DhcpServer dhcpSoftAP(&netif_git[SOFTAP_IF]); + +extern "C" +{ + + void dhcps_start(struct ip_info *info, netif* apnetif) + { + // apnetif is esp interface, replaced by lwip2's + // netif_git[SOFTAP_IF] interface in constructor + (void)apnetif; + +#if 0 + // can't use C++ now, global ctors are not initialized yet + dhcpSoftAP.begin(info); +#else + (void)info; + // initial version: emulate nonos-sdk in DhcpServer class before + // trying to change legacy behavor + // `fw_has_started_softap_dhcps` will be read in DhcpServer::DhcpServer + // which is called when c++ ctors are initialized, specifically + // dhcpSoftAP intialized with AP interface number above. + fw_has_started_softap_dhcps = 1; +#endif + } + + void dhcps_stop() + { + dhcpSoftAP.end(); + } + +} // extern "C" diff --git a/cores/esp8266/LwipDhcpServer.cpp b/cores/esp8266/LwipDhcpServer.cpp new file mode 100644 index 0000000000..2fa0b12d84 --- /dev/null +++ b/cores/esp8266/LwipDhcpServer.cpp @@ -0,0 +1,1613 @@ +/* + lwIPDhcpServer.c - DHCP server + + Copyright (c) 2016 Espressif. All rights reserved. + Copyright (c) 2020 esp8266 arduino. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + // original sources (no license provided) + // ESP8266_NONOS_SDK/third_party/lwip/app/dhcpserver.c + // ESP8266_NONOS_SDK/third_party/include/lwip/app/dhcpserver.h +*/ + +// lwIPDhcpServer.{cc,h} encapsulate original nonos-sdk dhcp server +// nearly as-is. This is an initial version to guaranty legacy behavior +// with same default values. + +// vv this comment is supposed to be removed after the first commit +// Logic and coding style in this file can be wrong but left to the closest +// of the initial version for easier issue tracking. +// (better is enemy of [good = already working]) +// ^^ this comment is supposed to be removed after the first commit + +#include // LWIP_VERSION + +#define DHCPS_LEASE_TIME_DEF (120) + +#define USE_DNS + +#include "lwip/inet.h" +#include "lwip/err.h" +#include "lwip/pbuf.h" +#include "lwip/udp.h" +#include "lwip/mem.h" +#include "osapi.h" + +#include "LwipDhcpServer.h" + +#include "user_interface.h" +#include "mem.h" + +typedef struct dhcps_state +{ + sint16_t state; +} dhcps_state; + +typedef struct dhcps_msg +{ + uint8_t op, htype, hlen, hops; + uint8_t xid[4]; + uint16_t secs, flags; + uint8_t ciaddr[4]; + uint8_t yiaddr[4]; + uint8_t siaddr[4]; + uint8_t giaddr[4]; + uint8_t chaddr[16]; + uint8_t sname[64]; + uint8_t file[128]; + uint8_t options[312]; +} dhcps_msg; + +#ifndef LWIP_OPEN_SRC +struct dhcps_lease +{ + bool enable; + struct ipv4_addr start_ip; + struct ipv4_addr end_ip; +}; + +enum dhcps_offer_option +{ + OFFER_START = 0x00, + OFFER_ROUTER = 0x01, + OFFER_END +}; +#endif + +typedef enum +{ + DHCPS_TYPE_DYNAMIC, + DHCPS_TYPE_STATIC +} dhcps_type_t; + +typedef enum +{ + DHCPS_STATE_ONLINE, + DHCPS_STATE_OFFLINE +} dhcps_state_t; + +struct dhcps_pool +{ + struct ipv4_addr ip; + uint8 mac[6]; + uint32 lease_timer; + dhcps_type_t type; + dhcps_state_t state; + +}; + +#define DHCPS_LEASE_TIMER dhcps_lease_time //0x05A0 +#define DHCPS_MAX_LEASE 0x64 +#define BOOTP_BROADCAST 0x8000 + +#define DHCP_REQUEST 1 +#define DHCP_REPLY 2 +#define DHCP_HTYPE_ETHERNET 1 +#define DHCP_HLEN_ETHERNET 6 +#define DHCP_MSG_LEN 236 + +#define DHCPS_SERVER_PORT 67 +#define DHCPS_CLIENT_PORT 68 + +#define DHCPDISCOVER 1 +#define DHCPOFFER 2 +#define DHCPREQUEST 3 +#define DHCPDECLINE 4 +#define DHCPACK 5 +#define DHCPNAK 6 +#define DHCPRELEASE 7 + +#define DHCP_OPTION_SUBNET_MASK 1 +#define DHCP_OPTION_ROUTER 3 +#define DHCP_OPTION_DNS_SERVER 6 +#define DHCP_OPTION_REQ_IPADDR 50 +#define DHCP_OPTION_LEASE_TIME 51 +#define DHCP_OPTION_MSG_TYPE 53 +#define DHCP_OPTION_SERVER_ID 54 +#define DHCP_OPTION_INTERFACE_MTU 26 +#define DHCP_OPTION_PERFORM_ROUTER_DISCOVERY 31 +#define DHCP_OPTION_BROADCAST_ADDRESS 28 +#define DHCP_OPTION_REQ_LIST 55 +#define DHCP_OPTION_END 255 + +//#define USE_CLASS_B_NET 1 +#define DHCPS_DEBUG 0 +#define MAX_STATION_NUM 8 + +#define DHCPS_STATE_OFFER 1 +#define DHCPS_STATE_DECLINE 2 +#define DHCPS_STATE_ACK 3 +#define DHCPS_STATE_NAK 4 +#define DHCPS_STATE_IDLE 5 +#define DHCPS_STATE_RELEASE 6 + +#define dhcps_router_enabled(offer) ((offer & OFFER_ROUTER) != 0) + +#ifdef MEMLEAK_DEBUG +const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__; +#endif + +#if DHCPS_DEBUG +#define LWIP_IS_OK(what,err) ({ int ret = 1, errval = (err); if (errval != ERR_OK) { os_printf("DHCPS ERROR: %s (lwip:%d)\n", what, errval); ret = 0; } ret; }) +#else +#define LWIP_IS_OK(what,err) ((err) == ERR_OK) +#endif + +const uint32 DhcpServer::magic_cookie = 0x63538263; // https://tools.ietf.org/html/rfc1497 + +int fw_has_started_softap_dhcps = 0; + +//////////////////////////////////////////////////////////////////////////////////// + +DhcpServer::DhcpServer(netif* netif): _netif(netif) +{ + pcb_dhcps = nullptr; + dns_address.addr = 0; + plist = nullptr; + offer = 0xFF; + renew = false; + dhcps_lease_time = DHCPS_LEASE_TIME_DEF; //minute + + if (netif->num == SOFTAP_IF && fw_has_started_softap_dhcps == 1) + { + // When nonos-sdk starts DHCPS at boot: + // 1. `fw_has_started_softap_dhcps` is already initialized to 1 + // 2. global ctor DhcpServer's `dhcpSoftAP(&netif_git[SOFTAP_IF])` is called + // 3. (that's here) => begin(legacy-values) is called + ip_info ip = + { + { 0x0104a8c0 }, // IP 192.168.4.1 + { 0x00ffffff }, // netmask 255.255.255.0 + { 0 } // gateway 0.0.0.0 + }; + begin(&ip); + fw_has_started_softap_dhcps = 2; // not 1, ending intial boot sequence + } +}; + +// wifi_softap_set_station_info is missing in user_interface.h: +extern "C" void wifi_softap_set_station_info(uint8_t* mac, struct ipv4_addr*); + +void DhcpServer::dhcps_set_dns(int num, const ipv4_addr_t* dns) +{ + (void)num; + if (!ip4_addr_isany(dns)) + { + ip4_addr_copy(dns_address, *dns); + } +} + +/****************************************************************************** + FunctionName : node_insert_to_list + Description : insert the node to the list + Parameters : arg -- Additional argument to pass to the callback function + Returns : none +*******************************************************************************/ +void DhcpServer::node_insert_to_list(list_node **phead, list_node* pinsert) +{ + list_node *plist = nullptr; + struct dhcps_pool *pdhcps_pool = nullptr; + struct dhcps_pool *pdhcps_node = nullptr; + if (*phead == nullptr) + { + *phead = pinsert; + } + else + { + plist = *phead; + pdhcps_node = (struct dhcps_pool*)pinsert->pnode; + pdhcps_pool = (struct dhcps_pool*)plist->pnode; + + if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) + { + pinsert->pnext = plist; + *phead = pinsert; + } + else + { + while (plist->pnext != nullptr) + { + pdhcps_pool = (struct dhcps_pool*)plist->pnext->pnode; + if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) + { + pinsert->pnext = plist->pnext; + plist->pnext = pinsert; + break; + } + plist = plist->pnext; + } + + if (plist->pnext == nullptr) + { + plist->pnext = pinsert; + } + } + } + // pinsert->pnext = nullptr; +} + +/****************************************************************************** + FunctionName : node_delete_from_list + Description : remove the node from list + Parameters : arg -- Additional argument to pass to the callback function + Returns : none +*******************************************************************************/ +void DhcpServer::node_remove_from_list(list_node **phead, list_node* pdelete) +{ + list_node *plist = nullptr; + + plist = *phead; + if (plist == nullptr) + { + *phead = nullptr; + } + else + { + if (plist == pdelete) + { + *phead = plist->pnext; + pdelete->pnext = nullptr; + } + else + { + while (plist != nullptr) + { + if (plist->pnext == pdelete) + { + plist->pnext = pdelete->pnext; + pdelete->pnext = nullptr; + } + plist = plist->pnext; + } + } + } +} + +/****************************************************************************** + FunctionName : add_dhcps_lease + Description : add static lease on the list, this will be the next available @ + Parameters : mac address + Returns : true if ok and false if this mac already exist or if all ip are already reserved +*******************************************************************************/ +bool DhcpServer::add_dhcps_lease(uint8 *macaddr) +{ + struct dhcps_pool *pdhcps_pool = nullptr; + list_node *pback_node = nullptr; + + uint32 start_ip = dhcps_lease.start_ip.addr; + uint32 end_ip = dhcps_lease.end_ip.addr; + + for (pback_node = plist; pback_node != nullptr; pback_node = pback_node->pnext) + { + pdhcps_pool = (dhcps_pool*)pback_node->pnode; + if (memcmp(pdhcps_pool->mac, macaddr, sizeof(pdhcps_pool->mac)) == 0) + { +#if DHCPS_DEBUG + os_printf("this mac already exist"); +#endif + return false; + } + else + { + start_ip = htonl((ntohl(start_ip) + 1)); + } + } + + if (start_ip > end_ip) + { +#if DHCPS_DEBUG + os_printf("no more ip available"); +#endif + return false; + } + + pdhcps_pool = (struct dhcps_pool *)zalloc(sizeof(struct dhcps_pool)); + pdhcps_pool->ip.addr = start_ip; + memcpy(pdhcps_pool->mac, macaddr, sizeof(pdhcps_pool->mac)); + pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER; + pdhcps_pool->type = DHCPS_TYPE_STATIC; + pdhcps_pool->state = DHCPS_STATE_ONLINE; + pback_node = (list_node *)zalloc(sizeof(list_node)); + pback_node->pnode = pdhcps_pool; + pback_node->pnext = nullptr; + node_insert_to_list(&plist, pback_node); + + return true; +} + +/////////////////////////////////////////////////////////////////////////////////// +/* + DHCP msg + + @param optptr -- DHCP msg + @param type -- option + + @return uint8_t* DHCP msg +*/ +/////////////////////////////////////////////////////////////////////////////////// +uint8_t* DhcpServer::add_msg_type(uint8_t *optptr, uint8_t type) +{ + + *optptr++ = DHCP_OPTION_MSG_TYPE; + *optptr++ = 1; + *optptr++ = type; + return optptr; +} +/////////////////////////////////////////////////////////////////////////////////// +/* + DHCP msg offer + + @param optptr -- DHCP msg + + @return uint8_t* DHCP msg +*/ +/////////////////////////////////////////////////////////////////////////////////// +uint8_t* DhcpServer::add_offer_options(uint8_t *optptr) +{ + //struct ipv4_addr ipadd; + //ipadd.addr = server_address.addr; +#define ipadd (_netif->ip_addr) + + //struct ip_info if_ip; + //bzero(&if_ip, sizeof(struct ip_info)); + //wifi_get_ip_info(SOFTAP_IF, &if_ip); +#define if_ip (*_netif) + + *optptr++ = DHCP_OPTION_SUBNET_MASK; + *optptr++ = 4; + *optptr++ = ip4_addr1(ip_2_ip4(&if_ip.netmask)); + *optptr++ = ip4_addr2(ip_2_ip4(&if_ip.netmask)); + *optptr++ = ip4_addr3(ip_2_ip4(&if_ip.netmask)); + *optptr++ = ip4_addr4(ip_2_ip4(&if_ip.netmask)); + + *optptr++ = DHCP_OPTION_LEASE_TIME; + *optptr++ = 4; + *optptr++ = ((DHCPS_LEASE_TIMER * 60) >> 24) & 0xFF; + *optptr++ = ((DHCPS_LEASE_TIMER * 60) >> 16) & 0xFF; + *optptr++ = ((DHCPS_LEASE_TIMER * 60) >> 8) & 0xFF; + *optptr++ = ((DHCPS_LEASE_TIMER * 60) >> 0) & 0xFF; + + *optptr++ = DHCP_OPTION_SERVER_ID; + *optptr++ = 4; + *optptr++ = ip4_addr1(ip_2_ip4(&ipadd)); + *optptr++ = ip4_addr2(ip_2_ip4(&ipadd)); + *optptr++ = ip4_addr3(ip_2_ip4(&ipadd)); + *optptr++ = ip4_addr4(ip_2_ip4(&ipadd)); + + if (dhcps_router_enabled(offer) && ip_2_ip4(&if_ip.gw)->addr) + { + *optptr++ = DHCP_OPTION_ROUTER; + *optptr++ = 4; + *optptr++ = ip4_addr1(ip_2_ip4(&if_ip.gw)); + *optptr++ = ip4_addr2(ip_2_ip4(&if_ip.gw)); + *optptr++ = ip4_addr3(ip_2_ip4(&if_ip.gw)); + *optptr++ = ip4_addr4(ip_2_ip4(&if_ip.gw)); + } + +#ifdef USE_DNS + *optptr++ = DHCP_OPTION_DNS_SERVER; + *optptr++ = 4; + if (dns_address.addr == 0) + { + *optptr++ = ip4_addr1(ip_2_ip4(&ipadd)); + *optptr++ = ip4_addr2(ip_2_ip4(&ipadd)); + *optptr++ = ip4_addr3(ip_2_ip4(&ipadd)); + *optptr++ = ip4_addr4(ip_2_ip4(&ipadd)); + } + else + { + *optptr++ = ip4_addr1(&dns_address); + *optptr++ = ip4_addr2(&dns_address); + *optptr++ = ip4_addr3(&dns_address); + *optptr++ = ip4_addr4(&dns_address); + } +#endif + + *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; + *optptr++ = 4; + // XXXFIXME do better than that, we have netmask + *optptr++ = ip4_addr1(ip_2_ip4(&ipadd)); + *optptr++ = ip4_addr2(ip_2_ip4(&ipadd)); + *optptr++ = ip4_addr3(ip_2_ip4(&ipadd)); + *optptr++ = 255; + + *optptr++ = DHCP_OPTION_INTERFACE_MTU; + *optptr++ = 2; + *optptr++ = 0x05; + *optptr++ = 0xdc; // 1500 + + *optptr++ = DHCP_OPTION_PERFORM_ROUTER_DISCOVERY; + *optptr++ = 1; + *optptr++ = 0x00; + +#if 0 // vendor specific unititialized (??) + *optptr++ = 43; // vendor specific + *optptr++ = 6; + // unitialized ? +#endif + +#if 0 // already set (DHCP_OPTION_SUBNET_MASK==1) (??) + *optptr++ = 0x01; + *optptr++ = 4; + *optptr++ = 0; + *optptr++ = 0; + *optptr++ = 0; + *optptr++ = 2; +#endif + + return optptr; + +#undef ipadd +#undef if_ip +} +/////////////////////////////////////////////////////////////////////////////////// +/* + DHCP msg + + @param optptr -- DHCP msg + + @return uint8_t* DHCP msg +*/ +/////////////////////////////////////////////////////////////////////////////////// +uint8_t* DhcpServer::add_end(uint8_t *optptr) +{ + + *optptr++ = DHCP_OPTION_END; + return optptr; +} +/////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////// +void DhcpServer::create_msg(struct dhcps_msg *m) +{ + struct ipv4_addr client; + + client.addr = client_address.addr; + + m->op = DHCP_REPLY; + m->htype = DHCP_HTYPE_ETHERNET; + m->hlen = 6; + m->hops = 0; + m->secs = 0; + m->flags = htons(BOOTP_BROADCAST); + + memcpy((char *) m->yiaddr, (char *) &client.addr, sizeof(m->yiaddr)); + memset((char *) m->ciaddr, 0, sizeof(m->ciaddr)); + memset((char *) m->siaddr, 0, sizeof(m->siaddr)); + memset((char *) m->giaddr, 0, sizeof(m->giaddr)); + memset((char *) m->sname, 0, sizeof(m->sname)); + memset((char *) m->file, 0, sizeof(m->file)); + memset((char *) m->options, 0, sizeof(m->options)); + memcpy((char *) m->options, &magic_cookie, sizeof(magic_cookie)); +} +/////////////////////////////////////////////////////////////////////////////////// +/* + OFFER + + @param -- m DHCP msg +*/ +/////////////////////////////////////////////////////////////////////////////////// +void DhcpServer::send_offer(struct dhcps_msg *m) +{ + uint8_t *end; + struct pbuf *p, *q; + u8_t *data; + u16_t cnt = 0; + u16_t i; + create_msg(m); + + end = add_msg_type(&m->options[4], DHCPOFFER); + end = add_offer_options(end); + end = add_end(end); + + p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); +#if DHCPS_DEBUG + os_printf("udhcp: send_offer>>p->ref = %d\n", p->ref); +#endif + if (p != nullptr) + { + +#if DHCPS_DEBUG + os_printf("dhcps: send_offer>>pbuf_alloc succeed\n"); + os_printf("dhcps: send_offer>>p->tot_len = %d\n", p->tot_len); + os_printf("dhcps: send_offer>>p->len = %d\n", p->len); +#endif + q = p; + while (q != nullptr) + { + data = (u8_t *)q->payload; + for (i = 0; i < q->len; i++) + { + data[i] = ((u8_t *) m)[cnt++]; + } + + q = q->next; + } + } + else + { + +#if DHCPS_DEBUG + os_printf("dhcps: send_offer>>pbuf_alloc failed\n"); +#endif + return; + } + if (!LWIP_IS_OK("send_offer", udp_sendto(pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT))) + { +#if DHCPS_DEBUG + os_printf("dhcps: send_offer>>udp_sendto\n"); +#endif + } + if (p->ref != 0) + { +#if DHCPS_DEBUG + os_printf("udhcp: send_offer>>free pbuf\n"); +#endif + pbuf_free(p); + } +} +/////////////////////////////////////////////////////////////////////////////////// +/* + NAK + + @param m DHCP msg +*/ +/////////////////////////////////////////////////////////////////////////////////// +void DhcpServer::send_nak(struct dhcps_msg *m) +{ + + u8_t *end; + struct pbuf *p, *q; + u8_t *data; + u16_t cnt = 0; + u16_t i; + create_msg(m); + + end = add_msg_type(&m->options[4], DHCPNAK); + end = add_end(end); + + p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); +#if DHCPS_DEBUG + os_printf("udhcp: send_nak>>p->ref = %d\n", p->ref); +#endif + if (p != nullptr) + { + +#if DHCPS_DEBUG + os_printf("dhcps: send_nak>>pbuf_alloc succeed\n"); + os_printf("dhcps: send_nak>>p->tot_len = %d\n", p->tot_len); + os_printf("dhcps: send_nak>>p->len = %d\n", p->len); +#endif + q = p; + while (q != nullptr) + { + data = (u8_t *)q->payload; + for (i = 0; i < q->len; i++) + { + data[i] = ((u8_t *) m)[cnt++]; + } + + q = q->next; + } + } + else + { + +#if DHCPS_DEBUG + os_printf("dhcps: send_nak>>pbuf_alloc failed\n"); +#endif + return; + } + LWIP_IS_OK("dhcps send nak", udp_sendto(pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT)); + if (p->ref != 0) + { +#if DHCPS_DEBUG + os_printf("udhcp: send_nak>>free pbuf\n"); +#endif + pbuf_free(p); + } +} +/////////////////////////////////////////////////////////////////////////////////// +/* + ACK DHCP + + @param m DHCP msg +*/ +/////////////////////////////////////////////////////////////////////////////////// +void DhcpServer::send_ack(struct dhcps_msg *m) +{ + + u8_t *end; + struct pbuf *p, *q; + u8_t *data; + u16_t cnt = 0; + u16_t i; + create_msg(m); + + end = add_msg_type(&m->options[4], DHCPACK); + end = add_offer_options(end); + end = add_end(end); + + p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); +#if DHCPS_DEBUG + os_printf("udhcp: send_ack>>p->ref = %d\n", p->ref); +#endif + if (p != nullptr) + { + +#if DHCPS_DEBUG + os_printf("dhcps: send_ack>>pbuf_alloc succeed\n"); + os_printf("dhcps: send_ack>>p->tot_len = %d\n", p->tot_len); + os_printf("dhcps: send_ack>>p->len = %d\n", p->len); +#endif + q = p; + while (q != nullptr) + { + data = (u8_t *)q->payload; + for (i = 0; i < q->len; i++) + { + data[i] = ((u8_t *) m)[cnt++]; + } + + q = q->next; + } + } + else + { + +#if DHCPS_DEBUG + os_printf("dhcps: send_ack>>pbuf_alloc failed\n"); +#endif + return; + } + if (!LWIP_IS_OK("dhcps send ack", udp_sendto(pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT))) + { +#if DHCPS_DEBUG + os_printf("dhcps: send_ack>>udp_sendto\n"); +#endif + } + + if (p->ref != 0) + { +#if DHCPS_DEBUG + os_printf("udhcp: send_ack>>free pbuf\n"); +#endif + pbuf_free(p); + } +} +/////////////////////////////////////////////////////////////////////////////////// +/* + DHCP + + @param optptr DHCP msg ะต + @param len + + @return uint8_t* DHCP Server +*/ +/////////////////////////////////////////////////////////////////////////////////// +uint8_t DhcpServer::parse_options(uint8_t *optptr, sint16_t len) +{ + struct ipv4_addr client; + bool is_dhcp_parse_end = false; + struct dhcps_state s; + + client.addr = client_address.addr; + + u8_t *end = optptr + len; + u16_t type = 0; + + s.state = DHCPS_STATE_IDLE; + + while (optptr < end) + { +#if DHCPS_DEBUG + os_printf("dhcps: (sint16_t)*optptr = %d\n", (sint16_t)*optptr); +#endif + switch ((sint16_t) *optptr) + { + + case DHCP_OPTION_MSG_TYPE: //53 + type = *(optptr + 2); + break; + + case DHCP_OPTION_REQ_IPADDR://50 + //os_printf("dhcps:0x%08x,0x%08x\n",client.addr,*(uint32*)(optptr+2)); + if (memcmp((char *) &client.addr, (char *) optptr + 2, 4) == 0) + { +#if DHCPS_DEBUG + os_printf("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n"); +#endif + s.state = DHCPS_STATE_ACK; + } + else + { +#if DHCPS_DEBUG + os_printf("dhcps: DHCP_OPTION_REQ_IPADDR != 0 err\n"); +#endif + s.state = DHCPS_STATE_NAK; + } + break; + case DHCP_OPTION_END: + { + is_dhcp_parse_end = true; + } + break; + } + + if (is_dhcp_parse_end) + { + break; + } + + optptr += optptr[1] + 2; + } + + switch (type) + { + case DHCPDISCOVER://1 + s.state = DHCPS_STATE_OFFER; +#if DHCPS_DEBUG + os_printf("dhcps: DHCPD_STATE_OFFER\n"); +#endif + break; + + case DHCPREQUEST://3 + if (!(s.state == DHCPS_STATE_ACK || s.state == DHCPS_STATE_NAK)) + { + if (renew == true) + { + s.state = DHCPS_STATE_ACK; + } + else + { + s.state = DHCPS_STATE_NAK; + } +#if DHCPS_DEBUG + os_printf("dhcps: DHCPD_STATE_NAK\n"); +#endif + } + break; + + case DHCPDECLINE://4 + s.state = DHCPS_STATE_IDLE; +#if DHCPS_DEBUG + os_printf("dhcps: DHCPD_STATE_IDLE\n"); +#endif + break; + + case DHCPRELEASE://7 + s.state = DHCPS_STATE_RELEASE; +#if DHCPS_DEBUG + os_printf("dhcps: DHCPD_STATE_IDLE\n"); +#endif + break; + } +#if DHCPS_DEBUG + os_printf("dhcps: return s.state = %d\n", s.state); +#endif + return s.state; +} +/////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////// +sint16_t DhcpServer::parse_msg(struct dhcps_msg *m, u16_t len) +{ + if (memcmp((char *)m->options, + &magic_cookie, + sizeof(magic_cookie)) == 0) + { + struct ipv4_addr ip; + memcpy(&ip.addr, m->ciaddr, sizeof(ip.addr)); + client_address.addr = dhcps_client_update(m->chaddr, &ip); + + sint16_t ret = parse_options(&m->options[4], len); + + if (ret == DHCPS_STATE_RELEASE) + { + dhcps_client_leave(m->chaddr, &ip, true); // force to delete + client_address.addr = ip.addr; + } + + return ret; + } + return 0; +} +/////////////////////////////////////////////////////////////////////////////////// +/* + DHCP + udp_recv() callback + + @param arg + @param pcb + @param p + @param addr + @param port +*/ +/////////////////////////////////////////////////////////////////////////////////// + +void DhcpServer::S_handle_dhcp(void *arg, + struct udp_pcb *pcb, + struct pbuf *p, + const ip_addr_t *addr, + uint16_t port) +{ + DhcpServer* instance = reinterpret_cast(arg); + instance->handle_dhcp(pcb, p, addr, port); +} + +void DhcpServer::handle_dhcp( + struct udp_pcb *pcb, + struct pbuf *p, + const ip_addr_t *addr, + uint16_t port) +{ + (void)pcb; + (void)addr; + (void)port; + + struct dhcps_msg *pmsg_dhcps = nullptr; + sint16_t tlen = 0; + u16_t i = 0; + u16_t dhcps_msg_cnt = 0; + u8_t *p_dhcps_msg = nullptr; + u8_t *data = nullptr; + +#if DHCPS_DEBUG + os_printf("dhcps: handle_dhcp-> receive a packet\n"); +#endif + if (p == nullptr) + { + return; + } + + pmsg_dhcps = (struct dhcps_msg *)zalloc(sizeof(struct dhcps_msg)); + if (nullptr == pmsg_dhcps) + { + pbuf_free(p); + return; + } + p_dhcps_msg = (u8_t *)pmsg_dhcps; + tlen = p->tot_len; + data = (u8_t*)p->payload; + +#if DHCPS_DEBUG + os_printf("dhcps: handle_dhcp-> p->tot_len = %d\n", tlen); + os_printf("dhcps: handle_dhcp-> p->len = %d\n", p->len); +#endif + + for (i = 0; i < p->len; i++) + { + p_dhcps_msg[dhcps_msg_cnt++] = data[i]; + } + + if (p->next != nullptr) + { +#if DHCPS_DEBUG + os_printf("dhcps: handle_dhcp-> p->next != nullptr\n"); + os_printf("dhcps: handle_dhcp-> p->next->tot_len = %d\n", p->next->tot_len); + os_printf("dhcps: handle_dhcp-> p->next->len = %d\n", p->next->len); +#endif + + data = (u8_t*)p->next->payload; + for (i = 0; i < p->next->len; i++) + { + p_dhcps_msg[dhcps_msg_cnt++] = data[i]; + } + } + +#if DHCPS_DEBUG + os_printf("dhcps: handle_dhcp-> parse_msg(p)\n"); +#endif + + switch (parse_msg(pmsg_dhcps, tlen - 240)) + { + + case DHCPS_STATE_OFFER://1 +#if DHCPS_DEBUG + os_printf("dhcps: handle_dhcp-> DHCPD_STATE_OFFER\n"); +#endif + send_offer(pmsg_dhcps); + break; + case DHCPS_STATE_ACK://3 +#if DHCPS_DEBUG + os_printf("dhcps: handle_dhcp-> DHCPD_STATE_ACK\n"); +#endif + send_ack(pmsg_dhcps); + if (_netif->num == SOFTAP_IF) + { + wifi_softap_set_station_info(pmsg_dhcps->chaddr, &client_address); + } + break; + case DHCPS_STATE_NAK://4 +#if DHCPS_DEBUG + os_printf("dhcps: handle_dhcp-> DHCPD_STATE_NAK\n"); +#endif + send_nak(pmsg_dhcps); + break; + default : + break; + } +#if DHCPS_DEBUG + os_printf("dhcps: handle_dhcp-> pbuf_free(p)\n"); +#endif + pbuf_free(p); + free(pmsg_dhcps); + pmsg_dhcps = nullptr; +} +/////////////////////////////////////////////////////////////////////////////////// +void DhcpServer::init_dhcps_lease(uint32 ip) +{ + uint32 softap_ip = 0, local_ip = 0; + uint32 start_ip = 0; + uint32 end_ip = 0; + if (dhcps_lease.enable == true) + { + softap_ip = htonl(ip); + start_ip = htonl(dhcps_lease.start_ip.addr); + end_ip = htonl(dhcps_lease.end_ip.addr); + /*config ip information can't contain local ip*/ + if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) + { + dhcps_lease.enable = false; + } + else + { + /*config ip information must be in the same segment as the local ip*/ + softap_ip >>= 8; + if (((start_ip >> 8 != softap_ip) || (end_ip >> 8 != softap_ip)) + || (end_ip - start_ip > DHCPS_MAX_LEASE)) + { + dhcps_lease.enable = false; + } + } + } + + if (dhcps_lease.enable == false) + { + local_ip = softap_ip = htonl(ip); + softap_ip &= 0xFFFFFF00; + local_ip &= 0xFF; + if (local_ip >= 0x80) + { + local_ip -= DHCPS_MAX_LEASE; + } + else + { + local_ip ++; + } + + bzero(&dhcps_lease, sizeof(dhcps_lease)); + dhcps_lease.start_ip.addr = softap_ip | local_ip; + dhcps_lease.end_ip.addr = softap_ip | (local_ip + DHCPS_MAX_LEASE - 1); + dhcps_lease.start_ip.addr = htonl(dhcps_lease.start_ip.addr); + dhcps_lease.end_ip.addr = htonl(dhcps_lease.end_ip.addr); + } + // dhcps_lease.start_ip.addr = htonl(dhcps_lease.start_ip.addr); + // dhcps_lease.end_ip.addr= htonl(dhcps_lease.end_ip.addr); + // os_printf("start_ip = 0x%x, end_ip = 0x%x\n",dhcps_lease.start_ip, dhcps_lease.end_ip); +} +/////////////////////////////////////////////////////////////////////////////////// + +bool DhcpServer::begin(struct ip_info *info) +{ + if (pcb_dhcps != nullptr) + { + udp_remove(pcb_dhcps); + } + + pcb_dhcps = udp_new(); + if (pcb_dhcps == nullptr || info == nullptr) + { + os_printf("dhcps_start(): could not obtain pcb\n"); + return false; + } + + // wrong: answer will go to sta IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255); + // good: going to ap IP4_ADDR(&broadcast_dhcps, 192, 168, 4, 255); + // semi proper way: + broadcast_dhcps = _netif->ip_addr; + ip_2_ip4(&broadcast_dhcps)->addr &= ip_2_ip4(&_netif->netmask)->addr; + ip_2_ip4(&broadcast_dhcps)->addr |= ~ip_2_ip4(&_netif->netmask)->addr; + //XXXFIXMEIPV6 broadcast address? + + server_address = info->ip; + init_dhcps_lease(server_address.addr); + + udp_bind(pcb_dhcps, IP_ADDR_ANY, DHCPS_SERVER_PORT); + udp_recv(pcb_dhcps, S_handle_dhcp, this); +#if DHCPS_DEBUG + os_printf("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n"); +#endif + + if (_netif->num == SOFTAP_IF) + { + wifi_set_ip_info(SOFTAP_IF, info); // added for lwip-git, not sure whether useful + } + _netif->flags |= NETIF_FLAG_UP | NETIF_FLAG_LINK_UP; // added for lwip-git + + return true; +} + +DhcpServer::~DhcpServer() +{ + end(); +} + +void DhcpServer::end() +{ + if (!pcb_dhcps) + { + return; + } + + udp_disconnect(pcb_dhcps); + udp_remove(pcb_dhcps); + pcb_dhcps = nullptr; + + //udp_remove(pcb_dhcps); + list_node *pnode = nullptr; + list_node *pback_node = nullptr; + struct dhcps_pool* dhcp_node = nullptr; + struct ipv4_addr ip_zero; + + memset(&ip_zero, 0x0, sizeof(ip_zero)); + pnode = plist; + while (pnode != nullptr) + { + pback_node = pnode; + pnode = pback_node->pnext; + node_remove_from_list(&plist, pback_node); + dhcp_node = (struct dhcps_pool*)pback_node->pnode; + //dhcps_client_leave(dhcp_node->mac,&dhcp_node->ip,true); // force to delete + if (_netif->num == SOFTAP_IF) + { + wifi_softap_set_station_info(dhcp_node->mac, &ip_zero); + } + free(pback_node->pnode); + pback_node->pnode = nullptr; + free(pback_node); + pback_node = nullptr; + } +} + +bool DhcpServer::isRunning() +{ + return !!_netif->state; +} + + +/****************************************************************************** + FunctionName : set_dhcps_lease + Description : set the lease information of DHCP server + Parameters : please -- Additional argument to set the lease information, + Little-Endian. + Returns : true or false +*******************************************************************************/ +bool DhcpServer::set_dhcps_lease(struct dhcps_lease *please) +{ + uint32 softap_ip = 0; + uint32 start_ip = 0; + uint32 end_ip = 0; + + if (_netif->num == SOFTAP_IF || _netif->num == STATION_IF) + { + uint8 opmode = wifi_get_opmode(); + if (opmode == STATION_MODE || opmode == NULL_MODE) + { + return false; + } + } + + if (please == nullptr || isRunning()) + { + return false; + } + + if (please->enable) + { + // logic below is subject for improvement + // - is wrong + // - limited to /24 address plans +#if 1 + softap_ip = ip_2_ip4(&_netif->ip_addr)->addr; +#else + struct ip_info info; + bzero(&info, sizeof(struct ip_info)); + wifi_get_ip_info(SOFTAP_IF, &info); + softap_ip = htonl(info.ip.addr); + start_ip = htonl(please->start_ip.addr); + end_ip = htonl(please->end_ip.addr); +#endif + /*config ip information can't contain local ip*/ + if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) + { + return false; + } + + /*config ip information must be in the same segment as the local ip*/ + softap_ip >>= 8; + if ((start_ip >> 8 != softap_ip) + || (end_ip >> 8 != softap_ip)) + { + return false; + } + + if (end_ip - start_ip > DHCPS_MAX_LEASE) + { + return false; + } + + bzero(&dhcps_lease, sizeof(dhcps_lease)); + // dhcps_lease.start_ip.addr = start_ip; + // dhcps_lease.end_ip.addr = end_ip; + dhcps_lease.start_ip.addr = please->start_ip.addr; + dhcps_lease.end_ip.addr = please->end_ip.addr; + } + dhcps_lease.enable = please->enable; + // dhcps_lease_flag = false; + return true; +} + +/****************************************************************************** + FunctionName : get_dhcps_lease + Description : get the lease information of DHCP server + Parameters : please -- Additional argument to get the lease information, + Little-Endian. + Returns : true or false +*******************************************************************************/ +bool DhcpServer::get_dhcps_lease(struct dhcps_lease *please) +{ + if (_netif->num == SOFTAP_IF) + { + uint8 opmode = wifi_get_opmode(); + if (opmode == STATION_MODE || opmode == NULL_MODE) + { + return false; + } + } + + if (nullptr == please) + { + return false; + } + + // if (dhcps_lease_flag){ + if (dhcps_lease.enable == false) + { + if (isRunning()) + { + return false; + } + } + else + { + // bzero(please, sizeof(dhcps_lease)); + // if (!isRunning()){ + // please->start_ip.addr = htonl(dhcps_lease.start_ip.addr); + // please->end_ip.addr = htonl(dhcps_lease.end_ip.addr); + // } + } + + // if (isRunning()){ + // bzero(please, sizeof(dhcps_lease)); + // please->start_ip.addr = dhcps_lease.start_ip.addr; + // please->end_ip.addr = dhcps_lease.end_ip.addr; + // } + please->start_ip.addr = dhcps_lease.start_ip.addr; + please->end_ip.addr = dhcps_lease.end_ip.addr; + return true; +} + +void DhcpServer::kill_oldest_dhcps_pool(void) +{ + list_node *pre = nullptr, *p = nullptr; + list_node *minpre = nullptr, *minp = nullptr; + struct dhcps_pool *pdhcps_pool = nullptr, *pmin_pool = nullptr; + pre = plist; + p = pre->pnext; + minpre = pre; + minp = p; + while (p != nullptr) + { + pdhcps_pool = (struct dhcps_pool*)p->pnode; + pmin_pool = (struct dhcps_pool*)minp->pnode; + if (pdhcps_pool->lease_timer < pmin_pool->lease_timer) + { + minp = p; + minpre = pre; + } + pre = p; + p = p->pnext; + } + minpre->pnext = minp->pnext; pdhcps_pool->state = DHCPS_STATE_OFFLINE; + free(minp->pnode); + minp->pnode = nullptr; + free(minp); + minp = nullptr; +} + +void DhcpServer::dhcps_coarse_tmr(void) +{ + uint8 num_dhcps_pool = 0; + list_node *pback_node = nullptr; + list_node *pnode = nullptr; + struct dhcps_pool *pdhcps_pool = nullptr; + pnode = plist; + while (pnode != nullptr) + { + pdhcps_pool = (struct dhcps_pool*)pnode->pnode; + if (pdhcps_pool->type == DHCPS_TYPE_DYNAMIC) + { + pdhcps_pool->lease_timer --; + } + if (pdhcps_pool->lease_timer == 0) + { + pback_node = pnode; + pnode = pback_node->pnext; + node_remove_from_list(&plist, pback_node); + free(pback_node->pnode); + pback_node->pnode = nullptr; + free(pback_node); + pback_node = nullptr; + } + else + { + pnode = pnode ->pnext; + num_dhcps_pool ++; + } + } + + if (num_dhcps_pool >= MAX_STATION_NUM) + { + kill_oldest_dhcps_pool(); + } +} + +bool DhcpServer::set_dhcps_offer_option(uint8 level, void* optarg) +{ + bool offer_flag = true; + //uint8 option = 0; + if (optarg == nullptr && !isRunning()) + { + return false; + } + + if (level <= OFFER_START || level >= OFFER_END) + { + return false; + } + + switch (level) + { + case OFFER_ROUTER: + offer = (*(uint8 *)optarg) & 0x01; + offer_flag = true; + break; + default : + offer_flag = false; + break; + } + return offer_flag; +} + +bool DhcpServer::set_dhcps_lease_time(uint32 minute) +{ + if (_netif->num == SOFTAP_IF) + { + uint8 opmode = wifi_get_opmode(); + if (opmode == STATION_MODE || opmode == NULL_MODE) + { + return false; + } + } + + if (isRunning()) + { + return false; + } + + if (minute == 0) + { + return false; + } + dhcps_lease_time = minute; + return true; +} + +bool DhcpServer::reset_dhcps_lease_time(void) +{ + if (_netif->num == SOFTAP_IF) + { + uint8 opmode = wifi_get_opmode(); + if (opmode == STATION_MODE || opmode == NULL_MODE) + { + return false; + } + } + + if (isRunning()) + { + return false; + } + dhcps_lease_time = DHCPS_LEASE_TIME_DEF; + return true; +} + +uint32 DhcpServer::get_dhcps_lease_time(void) // minute +{ + return dhcps_lease_time; +} + +void DhcpServer::dhcps_client_leave(u8 *bssid, struct ipv4_addr *ip, bool force) +{ + struct dhcps_pool *pdhcps_pool = nullptr; + list_node *pback_node = nullptr; + + if ((bssid == nullptr) || (ip == nullptr)) + { + return; + } + + for (pback_node = plist; pback_node != nullptr; pback_node = pback_node->pnext) + { + pdhcps_pool = (struct dhcps_pool*)pback_node->pnode; + if (memcmp(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac)) == 0) + { + if (memcmp(&pdhcps_pool->ip.addr, &ip->addr, sizeof(pdhcps_pool->ip.addr)) == 0) + { + if ((pdhcps_pool->type == DHCPS_TYPE_STATIC) || (force)) + { + if (pback_node != nullptr) + { + node_remove_from_list(&plist, pback_node); + free(pback_node); + pback_node = nullptr; + } + + if (pdhcps_pool != nullptr) + { + free(pdhcps_pool); + pdhcps_pool = nullptr; + } + } + else + { + pdhcps_pool->state = DHCPS_STATE_OFFLINE; + } + + struct ipv4_addr ip_zero; + memset(&ip_zero, 0x0, sizeof(ip_zero)); + if (_netif->num == SOFTAP_IF) + { + wifi_softap_set_station_info(bssid, &ip_zero); + } + break; + } + } + } +} + +uint32 DhcpServer::dhcps_client_update(u8 *bssid, struct ipv4_addr *ip) +{ + struct dhcps_pool *pdhcps_pool = nullptr; + list_node *pback_node = nullptr; + list_node *pmac_node = nullptr; + list_node *pip_node = nullptr; + bool flag = false; + uint32 start_ip = dhcps_lease.start_ip.addr; + uint32 end_ip = dhcps_lease.end_ip.addr; + dhcps_type_t type = DHCPS_TYPE_DYNAMIC; + if (bssid == nullptr) + { + return IPADDR_ANY; + } + + if (ip) + { + if (IPADDR_BROADCAST == ip->addr) + { + return IPADDR_ANY; + } + else if (IPADDR_ANY == ip->addr) + { + ip = nullptr; + } + else + { + type = DHCPS_TYPE_STATIC; + } + } + + renew = false; + for (pback_node = plist; pback_node != nullptr; pback_node = pback_node->pnext) + { + pdhcps_pool = (struct dhcps_pool*)pback_node->pnode; + //os_printf("mac:"MACSTR"bssid:"MACSTR"\r\n",MAC2STR(pdhcps_pool->mac),MAC2STR(bssid)); + if (memcmp(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac)) == 0) + { + pmac_node = pback_node; + if (ip == nullptr) + { + flag = true; + break; + } + } + if (ip != nullptr) + { + if (memcmp(&pdhcps_pool->ip.addr, &ip->addr, sizeof(pdhcps_pool->ip.addr)) == 0) + { + pip_node = pback_node; + } + } + else if (flag == false) + { + if (memcmp(&pdhcps_pool->ip.addr, &start_ip, sizeof(pdhcps_pool->ip.addr)) != 0) + { + flag = true; + } + else + { + start_ip = htonl((ntohl(start_ip) + 1)); + } + } + } + + if ((ip == nullptr) && (flag == false)) + { + if (plist == nullptr) + { + if (start_ip <= end_ip) + { + flag = true; + } + else + { + return IPADDR_ANY; + } + } + else + { + if (start_ip > end_ip) + { + return IPADDR_ANY; + } + //start_ip = htonl((ntohl(start_ip) + 1)); + flag = true; + } + } + + if (pmac_node != nullptr) // update new ip + { + if (pip_node != nullptr) + { + pdhcps_pool = (struct dhcps_pool*)pip_node->pnode; + + if (pip_node != pmac_node) + { + if (pdhcps_pool->state != DHCPS_STATE_OFFLINE) // ip is used + { + return IPADDR_ANY; + } + + // mac exists and ip exists in other node,delete mac + node_remove_from_list(&plist, pmac_node); + free(pmac_node->pnode); + pmac_node->pnode = nullptr; + free(pmac_node); + pmac_node = pip_node; + memcpy(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac)); + } + else + { + renew = true; + type = DHCPS_TYPE_DYNAMIC; + } + + pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER; + pdhcps_pool->type = type; + pdhcps_pool->state = DHCPS_STATE_ONLINE; + + } + else + { + pdhcps_pool = (struct dhcps_pool*)pmac_node->pnode; + if (ip != nullptr) + { + pdhcps_pool->ip.addr = ip->addr; + } + else if (flag == true) + { + pdhcps_pool->ip.addr = start_ip; + } + else // no ip to distribute + { + return IPADDR_ANY; + } + + node_remove_from_list(&plist, pmac_node); + pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER; + pdhcps_pool->type = type; + pdhcps_pool->state = DHCPS_STATE_ONLINE; + node_insert_to_list(&plist, pmac_node); + } + } + else // new station + { + if (pip_node != nullptr) // maybe ip has used + { + pdhcps_pool = (struct dhcps_pool*)pip_node->pnode; + if (pdhcps_pool->state != DHCPS_STATE_OFFLINE) + { + return IPADDR_ANY; + } + memcpy(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac)); + pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER; + pdhcps_pool->type = type; + pdhcps_pool->state = DHCPS_STATE_ONLINE; + } + else + { + pdhcps_pool = (struct dhcps_pool *)zalloc(sizeof(struct dhcps_pool)); + if (ip != nullptr) + { + pdhcps_pool->ip.addr = ip->addr; + } + else if (flag == true) + { + pdhcps_pool->ip.addr = start_ip; + } + else // no ip to distribute + { + free(pdhcps_pool); + return IPADDR_ANY; + } + if (pdhcps_pool->ip.addr > end_ip) + { + free(pdhcps_pool); + return IPADDR_ANY; + } + memcpy(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac)); + pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER; + pdhcps_pool->type = type; + pdhcps_pool->state = DHCPS_STATE_ONLINE; + pback_node = (list_node *)zalloc(sizeof(list_node)); + pback_node->pnode = pdhcps_pool; + pback_node->pnext = nullptr; + node_insert_to_list(&plist, pback_node); + } + } + + return pdhcps_pool->ip.addr; +} diff --git a/cores/esp8266/LwipDhcpServer.h b/cores/esp8266/LwipDhcpServer.h new file mode 100644 index 0000000000..e93166981d --- /dev/null +++ b/cores/esp8266/LwipDhcpServer.h @@ -0,0 +1,124 @@ +/* + lwIPDhcpServer.h - DHCP server + + Copyright (c) 2016 Espressif. All rights reserved. + Copyright (c) 2020 esp8266 arduino. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + // original sources (no license provided) + // ESP8266_NONOS_SDK/third_party/lwip/app/dhcpserver.c + // ESP8266_NONOS_SDK/third_party/include/lwip/app/dhcpserver.h +*/ + +// lwIPDhcpServer.{cc,h} encapsulate original nonos-sdk dhcp server +// nearly as-is. This is an initial version to guaranty legacy behavior +// with same default values. + +#ifndef __DHCPS_H__ +#define __DHCPS_H__ + +#include // LWIP_VERSION + +class DhcpServer +{ +public: + + DhcpServer(netif* netif); + ~DhcpServer(); + + void setDns(int num, const ipv4_addr_t* dns); + + bool begin(ip_info* info); + void end(); + bool isRunning(); + + // this is the C interface encapsulated in a class + // (originally dhcpserver.c in lwIP-v1.4 in NonOS-SDK) + // (not changing everything at once) + // the API below is subject to change + + // legacy public C structure and API to eventually turn into C++ + + void init_dhcps_lease(uint32 ip); + bool set_dhcps_lease(struct dhcps_lease *please); + bool get_dhcps_lease(struct dhcps_lease *please); + bool set_dhcps_offer_option(uint8 level, void* optarg); + bool set_dhcps_lease_time(uint32 minute); + bool reset_dhcps_lease_time(void); + uint32 get_dhcps_lease_time(void); + bool add_dhcps_lease(uint8 *macaddr); + + void dhcps_set_dns(int num, const ipv4_addr_t* dns); + +protected: + + // legacy C structure and API to eventually turn into C++ + + typedef struct _list_node + { + void *pnode; + struct _list_node *pnext; + } list_node; + + void node_insert_to_list(list_node **phead, list_node* pinsert); + void node_remove_from_list(list_node **phead, list_node* pdelete); + uint8_t* add_msg_type(uint8_t *optptr, uint8_t type); + uint8_t* add_offer_options(uint8_t *optptr); + uint8_t* add_end(uint8_t *optptr); + void create_msg(struct dhcps_msg *m); + void send_offer(struct dhcps_msg *m); + void send_nak(struct dhcps_msg *m); + void send_ack(struct dhcps_msg *m); + uint8_t parse_options(uint8_t *optptr, sint16_t len); + sint16_t parse_msg(struct dhcps_msg *m, u16_t len); + static void S_handle_dhcp(void *arg, + struct udp_pcb *pcb, + struct pbuf *p, + const ip_addr_t *addr, + uint16_t port); + void handle_dhcp( + struct udp_pcb *pcb, + struct pbuf *p, + const ip_addr_t *addr, + uint16_t port); + void kill_oldest_dhcps_pool(void); + void dhcps_coarse_tmr(void); // CURRENTLY NOT CALLED + void dhcps_client_leave(u8 *bssid, struct ipv4_addr *ip, bool force); + uint32 dhcps_client_update(u8 *bssid, struct ipv4_addr *ip); + + netif* _netif; + + struct udp_pcb *pcb_dhcps; + ip_addr_t broadcast_dhcps; + struct ipv4_addr server_address; + struct ipv4_addr client_address; + struct ipv4_addr dns_address; + uint32 dhcps_lease_time; + + struct dhcps_lease dhcps_lease; + list_node *plist; + uint8 offer; + bool renew; + + static const uint32 magic_cookie; +}; + +// SoftAP DHCP server always exists and is started on boot +extern DhcpServer dhcpSoftAP; +extern "C" int fw_has_started_softap_dhcps; + +#endif // __DHCPS_H__ diff --git a/cores/esp8266/LwipIntf.cpp b/cores/esp8266/LwipIntf.cpp new file mode 100644 index 0000000000..d2746745cc --- /dev/null +++ b/cores/esp8266/LwipIntf.cpp @@ -0,0 +1,156 @@ + +extern "C" { +#include "lwip/err.h" +#include "lwip/ip_addr.h" +#include "lwip/dns.h" +#include "lwip/dhcp.h" +#include "lwip/init.h" // LWIP_VERSION_ +#if LWIP_IPV6 +#include "lwip/netif.h" // struct netif +#endif + +#include +} + +#include "debug.h" +#include "LwipIntf.h" + +// args | esp order arduino order +// ---- + --------- ------------- +// local_ip | local_ip local_ip +// arg1 | gateway dns1 +// arg2 | netmask gateway +// arg3 | dns1 netmask +// +// result stored into gateway/netmask/dns1 + +bool LwipIntf::ipAddressReorder(const IPAddress& local_ip, const IPAddress& arg1, const IPAddress& arg2, const IPAddress& arg3, + IPAddress& gateway, IPAddress& netmask, IPAddress& dns1) +{ + //To allow compatibility, check first octet of 3rd arg. If 255, interpret as ESP order, otherwise Arduino order. + gateway = arg1; + netmask = arg2; + dns1 = arg3; + + if (netmask[0] != 255) + { + //octet is not 255 => interpret as Arduino order + gateway = arg2; + netmask = arg3[0] == 0 ? IPAddress(255, 255, 255, 0) : arg3; //arg order is arduino and 4th arg not given => assign it arduino default + dns1 = arg1; + } + + // check whether all is IPv4 (or gateway not set) + if (!(local_ip.isV4() && netmask.isV4() && (!gateway.isSet() || gateway.isV4()))) + { + return false; + } + + //ip and gateway must be in the same netmask + if (gateway.isSet() && (local_ip.v4() & netmask.v4()) != (gateway.v4() & netmask.v4())) + { + return false; + } + + return true; +} + +/** + Get ESP8266 station DHCP hostname + @return hostname +*/ +String LwipIntf::hostname(void) +{ + return wifi_station_get_hostname(); +} + +/** + Set ESP8266 station DHCP hostname + @param aHostname max length:24 + @return ok +*/ +bool LwipIntf::hostname(const char* aHostname) +{ + /* + vvvv RFC952 vvvv + ASSUMPTIONS + 1. A "name" (Net, Host, Gateway, or Domain name) is a text string up + to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus + sign (-), and period (.). Note that periods are only allowed when + they serve to delimit components of "domain style names". (See + RFC-921, "Domain Name System Implementation Schedule", for + background). No blank or space characters are permitted as part of a + name. No distinction is made between upper and lower case. The first + character must be an alpha character. The last character must not be + a minus sign or period. A host which serves as a GATEWAY should have + "-GATEWAY" or "-GW" as part of its name. Hosts which do not serve as + Internet gateways should not use "-GATEWAY" and "-GW" as part of + their names. A host which is a TAC should have "-TAC" as the last + part of its host name, if it is a DoD host. Single character names + or nicknames are not allowed. + ^^^^ RFC952 ^^^^ + + - 24 chars max + - only a..z A..Z 0..9 '-' + - no '-' as last char + */ + + size_t len = strlen(aHostname); + + if (len == 0 || len > 32) + { + // nonos-sdk limit is 32 + // (dhcp hostname option minimum size is ~60) + DEBUGV("WiFi.(set)hostname(): empty or large(>32) name\n"); + return false; + } + + // check RFC compliance + bool compliant = (len <= 24); + for (size_t i = 0; compliant && i < len; i++) + if (!isalnum(aHostname[i]) && aHostname[i] != '-') + { + compliant = false; + } + if (aHostname[len - 1] == '-') + { + compliant = false; + } + + if (!compliant) + { + DEBUGV("hostname '%s' is not compliant with RFC952\n", aHostname); + } + + bool ret = wifi_station_set_hostname(aHostname); + if (!ret) + { + DEBUGV("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname); + return false; + } + + // now we should inform dhcp server for this change, using lwip_renew() + // looping through all existing interface + // harmless for AP, also compatible with ethernet adapters (to come) + for (netif* intf = netif_list; intf; intf = intf->next) + { + + // unconditionally update all known interfaces + intf->hostname = wifi_station_get_hostname(); + + if (netif_dhcp_data(intf) != nullptr) + { + // renew already started DHCP leases + err_t lwipret = dhcp_renew(intf); + if (lwipret != ERR_OK) + { + DEBUGV("WiFi.hostname(%s): lwIP error %d on interface %c%c (index %d)\n", + intf->hostname, (int)lwipret, intf->name[0], intf->name[1], intf->num); + ret = false; + } + } + } + + return ret && compliant; +} + diff --git a/cores/esp8266/LwipIntf.h b/cores/esp8266/LwipIntf.h index cb8bb92dcc..972a7c8008 100644 --- a/cores/esp8266/LwipIntf.h +++ b/cores/esp8266/LwipIntf.h @@ -3,6 +3,7 @@ #define _LWIPINTF_H #include +#include #include @@ -12,15 +13,31 @@ class LwipIntf using CBType = std::function ; - static bool stateUpCB (LwipIntf::CBType&& cb); - -private: - - LwipIntf () { } // private, cannot be directly allocated + static bool stateUpCB(LwipIntf::CBType&& cb); + + // reorder WiFi.config() parameters for a esp8266/official Arduino dual-compatibility API + // args | esp order arduino order + // ---- + --------- ------------- + // local_ip | local_ip local_ip + // arg1 | gateway dns1 + // arg2 | netmask [Agateway + // arg3 | dns1 netmask + // + // result stored into gateway/netmask/dns1 + static + bool ipAddressReorder(const IPAddress& local_ip, const IPAddress& arg1, const IPAddress& arg2, const IPAddress& arg3, + IPAddress& gateway, IPAddress& netmask, IPAddress& dns1); + + String hostname(); + bool hostname(const String& aHostname) + { + return hostname(aHostname.c_str()); + } + bool hostname(const char* aHostname); protected: - static bool stateChangeSysCB (LwipIntf::CBType&& cb); + static bool stateChangeSysCB(LwipIntf::CBType&& cb); }; #endif // _LWIPINTF_H diff --git a/cores/esp8266/LwipIntfCB.cpp b/cores/esp8266/LwipIntfCB.cpp index b978918a72..1e495c5fd3 100644 --- a/cores/esp8266/LwipIntfCB.cpp +++ b/cores/esp8266/LwipIntfCB.cpp @@ -8,14 +8,16 @@ static int netifStatusChangeListLength = 0; LwipIntf::CBType netifStatusChangeList [NETIF_STATUS_CB_SIZE]; -extern "C" void netif_status_changed (struct netif* netif) +extern "C" void netif_status_changed(struct netif* netif) { // override the default empty weak function for (int i = 0; i < netifStatusChangeListLength; i++) + { netifStatusChangeList[i](netif); + } } -bool LwipIntf::stateChangeSysCB (LwipIntf::CBType&& cb) +bool LwipIntf::stateChangeSysCB(LwipIntf::CBType&& cb) { if (netifStatusChangeListLength >= NETIF_STATUS_CB_SIZE) { @@ -29,14 +31,14 @@ bool LwipIntf::stateChangeSysCB (LwipIntf::CBType&& cb) return true; } -bool LwipIntf::stateUpCB (LwipIntf::CBType&& cb) +bool LwipIntf::stateUpCB(LwipIntf::CBType&& cb) { - return stateChangeSysCB([cb](netif* nif) + return stateChangeSysCB([cb](netif * nif) { if (netif_is_up(nif)) schedule_function([cb, nif]() - { - cb(nif); - }); + { + cb(nif); + }); }); } diff --git a/cores/esp8266/LwipIntfDev.h b/cores/esp8266/LwipIntfDev.h new file mode 100644 index 0000000000..dded934992 --- /dev/null +++ b/cores/esp8266/LwipIntfDev.h @@ -0,0 +1,386 @@ + +#ifndef _LWIPINTFDEV_H +#define _LWIPINTFDEV_H + +// TODO: +// remove all Serial.print +// unchain pbufs + +#include +#include +#include +#include +#include +#include + +#include // wifi_get_macaddr() + +#include "SPI.h" +#include "Schedule.h" +#include "LwipIntf.h" +#include "wl_definitions.h" + +#ifndef DEFAULT_MTU +#define DEFAULT_MTU 1500 +#endif + +template +class LwipIntfDev: public LwipIntf, public RawDev +{ + +public: + + LwipIntfDev(int8_t cs = SS, SPIClass& spi = SPI, int8_t intr = -1): + RawDev(cs, spi, intr), + _mtu(DEFAULT_MTU), + _intrPin(intr), + _started(false), + _default(false) + { + memset(&_netif, 0, sizeof(_netif)); + } + + boolean config(const IPAddress& local_ip, const IPAddress& arg1, const IPAddress& arg2, const IPAddress& arg3, const IPAddress& dns2); + + // default mac-address is inferred from esp8266's STA interface + boolean begin(const uint8_t *macAddress = nullptr, const uint16_t mtu = DEFAULT_MTU); + + const netif* getNetIf() const + { + return &_netif; + } + + IPAddress localIP() const + { + return IPAddress(ip4_addr_get_u32(ip_2_ip4(&_netif.ip_addr))); + } + IPAddress subnetMask() const + { + return IPAddress(ip4_addr_get_u32(ip_2_ip4(&_netif.netmask))); + } + IPAddress gatewayIP() const + { + return IPAddress(ip4_addr_get_u32(ip_2_ip4(&_netif.gw))); + } + + void setDefault(); + + bool connected() + { + return !!ip4_addr_get_u32(ip_2_ip4(&_netif.ip_addr)); + } + + // ESP8266WiFi API compatibility + + wl_status_t status(); + +protected: + + err_t netif_init(); + void netif_status_callback(); + + static err_t netif_init_s(netif* netif); + static err_t linkoutput_s(netif *netif, struct pbuf *p); + static void netif_status_callback_s(netif* netif); + + // called on a regular basis or on interrupt + err_t handlePackets(); + + // members + + netif _netif; + + uint16_t _mtu; + int8_t _intrPin; + uint8_t _macAddress[6]; + bool _started; + bool _default; + +}; + +template +boolean LwipIntfDev::config(const IPAddress& localIP, const IPAddress& gateway, const IPAddress& netmask, const IPAddress& dns1, const IPAddress& dns2) +{ + if (_started) + { + DEBUGV("LwipIntfDev: use config() then begin()\n"); + return false; + } + + IPAddress realGateway, realNetmask, realDns1; + if (!ipAddressReorder(localIP, gateway, netmask, dns1, realGateway, realNetmask, realDns1)) + { + return false; + } + ip4_addr_set_u32(ip_2_ip4(&_netif.ip_addr), localIP.v4()); + ip4_addr_set_u32(ip_2_ip4(&_netif.gw), realGateway.v4()); + ip4_addr_set_u32(ip_2_ip4(&_netif.netmask), realNetmask.v4()); + + return true; +} + +template +boolean LwipIntfDev::begin(const uint8_t* macAddress, const uint16_t mtu) +{ + if (mtu) + { + _mtu = mtu; + } + + if (macAddress) + { + memcpy(_macAddress, macAddress, 6); + } + else + { + _netif.num = 2; + for (auto n = netif_list; n; n = n->next) + if (n->num >= _netif.num) + { + _netif.num = n->num + 1; + } + +#if 1 + // forge a new mac-address from the esp's wifi sta one + // I understand this is cheating with an official mac-address + wifi_get_macaddr(STATION_IF, (uint8*)_macAddress); +#else + // https://serverfault.com/questions/40712/what-range-of-mac-addresses-can-i-safely-use-for-my-virtual-machines + memset(_macAddress, 0, 6); + _macAddress[0] = 0xEE; +#endif + _macAddress[3] += _netif.num; // alter base mac address + _macAddress[0] &= 0xfe; // set as locally administered, unicast, per + _macAddress[0] |= 0x02; // https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local + } + + if (!RawDev::begin(_macAddress)) + { + return false; + } + + // setup lwIP netif + + _netif.hwaddr_len = sizeof _macAddress; + memcpy(_netif.hwaddr, _macAddress, sizeof _macAddress); + + // due to netif_add() api: ... + ip_addr_t ip_addr, netmask, gw; + ip_addr_copy(ip_addr, _netif.ip_addr); + ip_addr_copy(netmask, _netif.netmask); + ip_addr_copy(gw, _netif.gw); + + if (!netif_add(&_netif, ip_2_ip4(&ip_addr), ip_2_ip4(&netmask), ip_2_ip4(&gw), this, netif_init_s, ethernet_input)) + { + return false; + } + + _netif.flags |= NETIF_FLAG_UP; + + if (localIP().v4() == 0) + { + switch (dhcp_start(&_netif)) + { + case ERR_OK: + break; + + case ERR_IF: + return false; + + default: + netif_remove(&_netif); + return false; + } + } + + _started = true; + + if (_intrPin >= 0) + { + if (RawDev::interruptIsPossible()) + { + //attachInterrupt(_intrPin, [&]() { this->handlePackets(); }, FALLING); + } + else + { + ::printf((PGM_P)F("lwIP_Intf: Interrupt not implemented yet, enabling transparent polling\r\n")); + _intrPin = -1; + } + } + + if (_intrPin < 0 && !schedule_recurrent_function_us([&]() +{ + this->handlePackets(); + return true; + }, 100)) + { + netif_remove(&_netif); + return false; + } + + return true; +} + +template +wl_status_t LwipIntfDev::status() +{ + return _started ? (connected() ? WL_CONNECTED : WL_DISCONNECTED) : WL_NO_SHIELD; +} + +template +err_t LwipIntfDev::linkoutput_s(netif *netif, struct pbuf *pbuf) +{ + LwipIntfDev* ths = (LwipIntfDev*)netif->state; + + if (pbuf->len != pbuf->tot_len || pbuf->next) + { + Serial.println("ERRTOT\r\n"); + } + + uint16_t len = ths->sendFrame((const uint8_t*)pbuf->payload, pbuf->len); + +#if PHY_HAS_CAPTURE + if (phy_capture) + { + phy_capture(ths->_netif.num, (const char*)pbuf->payload, pbuf->len, /*out*/1, /*success*/len == pbuf->len); + } +#endif + + return len == pbuf->len ? ERR_OK : ERR_MEM; +} + +template +err_t LwipIntfDev::netif_init_s(struct netif* netif) +{ + return ((LwipIntfDev*)netif->state)->netif_init(); +} + +template +void LwipIntfDev::netif_status_callback_s(struct netif* netif) +{ + ((LwipIntfDev*)netif->state)->netif_status_callback(); +} + +template +err_t LwipIntfDev::netif_init() +{ + _netif.name[0] = 'e'; + _netif.name[1] = '0' + _netif.num; + _netif.mtu = _mtu; + _netif.chksum_flags = NETIF_CHECKSUM_ENABLE_ALL; + _netif.flags = + NETIF_FLAG_ETHARP + | NETIF_FLAG_IGMP + | NETIF_FLAG_BROADCAST + | NETIF_FLAG_LINK_UP; + + // lwIP's doc: This function typically first resolves the hardware + // address, then sends the packet. For ethernet physical layer, this is + // usually lwIP's etharp_output() + _netif.output = etharp_output; + + // lwIP's doc: This function outputs the pbuf as-is on the link medium + // (this must points to the raw ethernet driver, meaning: us) + _netif.linkoutput = linkoutput_s; + + _netif.status_callback = netif_status_callback_s; + + return ERR_OK; +} + +template +void LwipIntfDev::netif_status_callback() +{ + if (connected()) + { + if (_default) + { + netif_set_default(&_netif); + } + sntp_stop(); + sntp_init(); + } + else if (netif_default == &_netif) + { + netif_set_default(nullptr); + } +} + +template +err_t LwipIntfDev::handlePackets() +{ + int pkt = 0; + while (1) + { + if (++pkt == 10) + // prevent starvation + { + return ERR_OK; + } + + uint16_t tot_len = RawDev::readFrameSize(); + if (!tot_len) + { + return ERR_OK; + } + + // from doc: use PBUF_RAM for TX, PBUF_POOL from RX + // however: + // PBUF_POOL can return chained pbuf (not in one piece) + // and WiznetDriver does not have the proper API to deal with that + // so in the meantime, we use PBUF_RAM instead which is currently + // guarantying to deliver a continuous chunk of memory. + // TODO: tweak the wiznet driver to allow copying partial chunk + // of received data and use PBUF_POOL. + pbuf* pbuf = pbuf_alloc(PBUF_RAW, tot_len, PBUF_RAM); + if (!pbuf || pbuf->len < tot_len) + { + if (pbuf) + { + pbuf_free(pbuf); + } + RawDev::discardFrame(tot_len); + return ERR_BUF; + } + + uint16_t len = RawDev::readFrameData((uint8_t*)pbuf->payload, tot_len); + if (len != tot_len) + { + // tot_len is given by readFrameSize() + // and is supposed to be honoured by readFrameData() + // todo: ensure this test is unneeded, remove the print + Serial.println("read error?\r\n"); + pbuf_free(pbuf); + return ERR_BUF; + } + + err_t err = _netif.input(pbuf, &_netif); + +#if PHY_HAS_CAPTURE + if (phy_capture) + { + phy_capture(_netif.num, (const char*)pbuf->payload, tot_len, /*out*/0, /*success*/err == ERR_OK); + } +#endif + + if (err != ERR_OK) + { + pbuf_free(pbuf); + return err; + } + // (else) allocated pbuf is now lwIP's responsibility + + } +} + +template +void LwipIntfDev::setDefault() +{ + _default = true; + if (connected()) + { + netif_set_default(&_netif); + } +} + +#endif // _LWIPINTFDEV_H diff --git a/libraries/ESP8266WiFi/src/include/wl_definitions.h b/cores/esp8266/wl_definitions.h similarity index 96% rename from libraries/ESP8266WiFi/src/include/wl_definitions.h rename to cores/esp8266/wl_definitions.h index e20ee928ca..b58085ac59 100644 --- a/libraries/ESP8266WiFi/src/include/wl_definitions.h +++ b/cores/esp8266/wl_definitions.h @@ -69,7 +69,7 @@ enum wl_enc_type { /* Values map to 802.11 encryption suites... */ ENC_TYPE_AUTO = 8 }; -#if !defined(LWIP_INTERNAL) && !defined(__LWIP_TCP_H__) +#if !defined(LWIP_INTERNAL) && !defined(__LWIP_TCP_H__) && !defined(LWIP_HDR_TCPBASE_H) enum wl_tcp_state { CLOSED = 0, LISTEN = 1, diff --git a/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino b/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino index 9676857b1c..a98e894873 100644 --- a/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino +++ b/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino @@ -13,7 +13,7 @@ #include #include #include -#include +#include #define NAPT 1000 #define NAPT_PORT 10 @@ -57,8 +57,8 @@ void setup() { WiFi.dnsIP(1).toString().c_str()); // give DNS servers to AP side - dhcps_set_dns(0, WiFi.dnsIP(0)); - dhcps_set_dns(1, WiFi.dnsIP(1)); + dhcpSoftAP.dhcps_set_dns(0, WiFi.dnsIP(0)); + dhcpSoftAP.dhcps_set_dns(1, WiFi.dnsIP(1)); WiFi.softAPConfig( // enable AP, with android-compatible google domain IPAddress(172, 217, 28, 254), diff --git a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino index 4beaabe2b6..e4520c4720 100644 --- a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino +++ b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino @@ -4,6 +4,7 @@ #include #include #include +#include /* Set these to your desired credentials. */ const char *ssid = "ESPap"; @@ -75,8 +76,8 @@ void setup() { ... any client not listed will use next IP address available from the range (here 192.168.0.102 and more) */ - wifi_softap_add_dhcps_lease(mac_CAM); // always 192.168.0.100 - wifi_softap_add_dhcps_lease(mac_PC); // always 192.168.0.101 + dhcpSoftAP.add_dhcps_lease(mac_CAM); // always 192.168.0.100 + dhcpSoftAP.add_dhcps_lease(mac_PC); // always 192.168.0.101 /* Start Access Point. You can remove the password parameter if you want the AP to be open. */ WiFi.softAP(ssid, password); Serial.print("AP IP address: "); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index d26e1db6bd..819739eaea 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -25,7 +25,7 @@ #include extern "C" { -#include "include/wl_definitions.h" +#include } #include "IPAddress.h" diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 418a48b5cc..202042c780 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -33,11 +33,11 @@ extern "C" { #include "osapi.h" #include "mem.h" #include "user_interface.h" +#include // LWIP_VERSION_* } #include "debug.h" - - +#include "LwipDhcpServer.h" // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------- Private functions ------------------------------------------------ @@ -156,13 +156,7 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch DEBUG_WIFI("[AP] softap config unchanged\n"); } - if(wifi_softap_dhcps_status() != DHCP_STARTED) { - DEBUG_WIFI("[AP] DHCP not started, starting...\n"); - if(!wifi_softap_dhcps_start()) { - DEBUG_WIFI("[AP] wifi_softap_dhcps_start failed!\n"); - ret = false; - } - } + dhcpSoftAP.end(); // check IP config struct ip_info ip; @@ -182,6 +176,8 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch ret = false; } + dhcpSoftAP.begin(&ip); + return ret; } @@ -237,19 +233,22 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA dhcp_lease.end_ip.addr = ip.v4(); DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str()); - if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) { + if(!dhcpSoftAP.set_dhcps_lease(&dhcp_lease)) + { DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n"); ret = false; } // set lease time to 720min --> 12h - if(!wifi_softap_set_dhcps_lease_time(720)) { + if(!dhcpSoftAP.set_dhcps_lease_time(720)) + { DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n"); ret = false; } uint8 mode = info.gw.addr ? 1 : 0; - if(!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) { + if(!dhcpSoftAP.set_dhcps_offer_option(OFFER_ROUTER, &mode)) + { DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n"); ret = false; } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index 0959bba7c7..a75c115c26 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -26,6 +26,7 @@ #include "ESP8266WiFiGeneric.h" #include "ESP8266WiFiSTA.h" #include "PolledTimeout.h" +#include "LwipIntf.h" #include "c_types.h" #include "ets_sys.h" @@ -281,28 +282,9 @@ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress arg1, IPAddress a return true; } - //To allow compatibility, check first octet of 3rd arg. If 255, interpret as ESP order, otherwise Arduino order. - IPAddress gateway = arg1; - IPAddress subnet = arg2; - IPAddress dns1 = arg3; - - if(subnet[0] != 255) - { - //octet is not 255 => interpret as Arduino order - gateway = arg2; - subnet = arg3[0] == 0 ? IPAddress(255,255,255,0) : arg3; //arg order is arduino and 4th arg not given => assign it arduino default - dns1 = arg1; - } - - // check whether all is IPv4 (or gateway not set) - if (!(local_ip.isV4() && subnet.isV4() && (!gateway.isSet() || gateway.isV4()))) { - return false; - } - - //ip and gateway must be in the same subnet - if((local_ip.v4() & subnet.v4()) != (gateway.v4() & subnet.v4())) { + IPAddress gateway, subnet, dns1; + if (!ipAddressReorder(local_ip, arg1, arg2, arg3, gateway, subnet, dns1)) return false; - } #if !CORE_MOCK // get current->previous IP address @@ -522,94 +504,6 @@ IPAddress ESP8266WiFiSTAClass::dnsIP(uint8_t dns_no) { return IPAddress(dns_getserver(dns_no)); } - -/** - * Get ESP8266 station DHCP hostname - * @return hostname - */ -String ESP8266WiFiSTAClass::hostname(void) { - return wifi_station_get_hostname(); -} - -/** - * Set ESP8266 station DHCP hostname - * @param aHostname max length:24 - * @return ok - */ -bool ESP8266WiFiSTAClass::hostname(const char* aHostname) { - /* - vvvv RFC952 vvvv - ASSUMPTIONS - 1. A "name" (Net, Host, Gateway, or Domain name) is a text string up - to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus - sign (-), and period (.). Note that periods are only allowed when - they serve to delimit components of "domain style names". (See - RFC-921, "Domain Name System Implementation Schedule", for - background). No blank or space characters are permitted as part of a - name. No distinction is made between upper and lower case. The first - character must be an alpha character. The last character must not be - a minus sign or period. A host which serves as a GATEWAY should have - "-GATEWAY" or "-GW" as part of its name. Hosts which do not serve as - Internet gateways should not use "-GATEWAY" and "-GW" as part of - their names. A host which is a TAC should have "-TAC" as the last - part of its host name, if it is a DoD host. Single character names - or nicknames are not allowed. - ^^^^ RFC952 ^^^^ - - - 24 chars max - - only a..z A..Z 0..9 '-' - - no '-' as last char - */ - - size_t len = strlen(aHostname); - - if (len == 0 || len > 32) { - // nonos-sdk limit is 32 - // (dhcp hostname option minimum size is ~60) - DEBUG_WIFI_GENERIC("WiFi.(set)hostname(): empty or large(>32) name\n"); - return false; - } - - // check RFC compliance - bool compliant = (len <= 24); - for (size_t i = 0; compliant && i < len; i++) - if (!isalnum(aHostname[i]) && aHostname[i] != '-') - compliant = false; - if (aHostname[len - 1] == '-') - compliant = false; - - if (!compliant) { - DEBUG_WIFI_GENERIC("hostname '%s' is not compliant with RFC952\n", aHostname); - } - - bool ret = wifi_station_set_hostname(aHostname); - if (!ret) { - DEBUG_WIFI_GENERIC("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname); - return false; - } - - // now we should inform dhcp server for this change, using lwip_renew() - // looping through all existing interface - // harmless for AP, also compatible with ethernet adapters (to come) - for (netif* intf = netif_list; intf; intf = intf->next) { - - // unconditionally update all known interfaces - intf->hostname = wifi_station_get_hostname(); - - if (netif_dhcp_data(intf) != nullptr) { - // renew already started DHCP leases - err_t lwipret = dhcp_renew(intf); - if (lwipret != ERR_OK) { - DEBUG_WIFI_GENERIC("WiFi.hostname(%s): lwIP error %d on interface %c%c (index %d)\n", - intf->hostname, (int)lwipret, intf->name[0], intf->name[1], intf->num); - ret = false; - } - } - } - - return ret && compliant; -} - /** * Return Connection status. * @return one of the value defined in wl_status_t diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h index d845b290ca..6dbb6406c1 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h @@ -27,9 +27,10 @@ #include "ESP8266WiFiType.h" #include "ESP8266WiFiGeneric.h" #include "user_interface.h" +#include "LwipIntf.h" -class ESP8266WiFiSTAClass { +class ESP8266WiFiSTAClass: public LwipIntf { // ---------------------------------------------------------------------------------------------- // ---------------------------------------- STA function ---------------------------------------- // ---------------------------------------------------------------------------------------------- @@ -69,10 +70,6 @@ class ESP8266WiFiSTAClass { IPAddress gatewayIP(); IPAddress dnsIP(uint8_t dns_no = 0); - String hostname(); - bool hostname(const String& aHostname) { return hostname(aHostname.c_str()); } - bool hostname(const char* aHostname); - // STA WiFi info wl_status_t status(); String SSID() const; diff --git a/libraries/ESP8266WiFi/src/WiFiClient.cpp b/libraries/ESP8266WiFi/src/WiFiClient.cpp index 825f2ed839..5aa09b8874 100644 --- a/libraries/ESP8266WiFi/src/WiFiClient.cpp +++ b/libraries/ESP8266WiFi/src/WiFiClient.cpp @@ -24,7 +24,7 @@ extern "C" { - #include "include/wl_definitions.h" + #include "wl_definitions.h" #include "osapi.h" #include "ets_sys.h" } diff --git a/libraries/ESP8266WiFi/src/WiFiServer.h b/libraries/ESP8266WiFi/src/WiFiServer.h index 2023840545..59aea48faf 100644 --- a/libraries/ESP8266WiFi/src/WiFiServer.h +++ b/libraries/ESP8266WiFi/src/WiFiServer.h @@ -23,7 +23,7 @@ #define wifiserver_h extern "C" { - #include "include/wl_definitions.h" + #include "wl_definitions.h" struct tcp_pcb; } diff --git a/libraries/ESP8266WiFi/src/WiFiUdp.cpp b/libraries/ESP8266WiFi/src/WiFiUdp.cpp index a0a5c1d4e4..fc4bfe324e 100644 --- a/libraries/ESP8266WiFi/src/WiFiUdp.cpp +++ b/libraries/ESP8266WiFi/src/WiFiUdp.cpp @@ -25,7 +25,7 @@ extern "C" { - #include "include/wl_definitions.h" + #include "wl_definitions.h" #include "osapi.h" #include "ets_sys.h" } diff --git a/libraries/lwIP_PPP/examples/PPPServer/PPPServer.ino b/libraries/lwIP_PPP/examples/PPPServer/PPPServer.ino new file mode 100644 index 0000000000..ed2943c278 --- /dev/null +++ b/libraries/lwIP_PPP/examples/PPPServer/PPPServer.ino @@ -0,0 +1,111 @@ + +// This is still beta / a work in progress + +// To run this sketch an (other) USB-serial converter is needed connected to RX-TX ports (below) +// hardware serial is used for logging +// software serial is used for the PPP link +// this example is subject for changes once everything is stabilized + +// testing on linux: +// sudo /usr/sbin/pppd /dev/ttyUSB1 38400 noipdefault nocrtscts local defaultroute noauth nodetach debug dump +// sudo /usr/sbin/pppd /dev/ttyUSB1 38400 noipdefault nocrtscts local defaultroute noauth + +// proxy arp is needed but we don't have it +// http://lwip.100.n7.nabble.com/PPP-proxy-arp-support-tp33286p33345.html +// using NAT instead + +#if LWIP_FEATURES && !LWIP_IPV6 + +#include +#include +#include +#include +#include +#include + +#ifndef STASSID +#define STASSID "your-ssid" +#define STAPSK "your-password" +#endif + +#define LOGGERBAUD 115200 +#define PPPLINKBAUD 38400 + +#define NAPT 200 +#define NAPT_PORT 3 + +#define RX 13 // d1mini D7 +#define TX 15 // d1mini D8 + +SoftwareSerial ppplink(RX, TX); +HardwareSerial& logger = Serial; +PPPServer ppp(&ppplink); + +void PPPConnectedCallback(netif* nif) { + logger.printf("ppp: ip=%s/mask=%s/gw=%s\n", + IPAddress(&nif->ip_addr).toString().c_str(), + IPAddress(&nif->netmask).toString().c_str(), + IPAddress(&nif->gw).toString().c_str()); + + logger.printf("Heap before: %d\n", ESP.getFreeHeap()); + err_t ret = ip_napt_init(NAPT, NAPT_PORT); + logger.printf("ip_napt_init(%d,%d): ret=%d (OK=%d)\n", NAPT, NAPT_PORT, (int)ret, (int)ERR_OK); + if (ret == ERR_OK) { + ret = ip_napt_enable_no(nif->num, 1); + logger.printf("ip_napt_enable(nif): ret=%d (OK=%d)\n", (int)ret, (int)ERR_OK); + if (ret == ERR_OK) { + logger.printf("PPP client is NATed\n"); + } + + // could not make this work yet, + // but packets are arriving on ppp client (= linux host) + logger.printf("redirect22=%d\n", ip_portmap_add(IP_PROTO_TCP, ip_2_ip4(&nif->ip_addr)->addr, 22, ip_2_ip4(&nif->gw)->addr, 22)); + logger.printf("redirect80=%d\n", ip_portmap_add(IP_PROTO_TCP, ip_2_ip4(&nif->ip_addr)->addr, 80, ip_2_ip4(&nif->gw)->addr, 80)); + logger.printf("redirect443=%d\n", ip_portmap_add(IP_PROTO_TCP, ip_2_ip4(&nif->ip_addr)->addr, 443, ip_2_ip4(&nif->gw)->addr, 443)); + } + logger.printf("Heap after napt init: %d\n", ESP.getFreeHeap()); + if (ret != ERR_OK) { + logger.printf("NAPT initialization failed\n"); + } +} + +void setup() { + logger.begin(LOGGERBAUD); + + WiFi.persistent(false); + WiFi.mode(WIFI_STA); + WiFi.begin(STASSID, STAPSK); + while (WiFi.status() != WL_CONNECTED) { + logger.print('.'); + delay(500); + } + logger.printf("\nSTA: %s (dns: %s / %s)\n", + WiFi.localIP().toString().c_str(), + WiFi.dnsIP(0).toString().c_str(), + WiFi.dnsIP(1).toString().c_str()); + + ppplink.begin(PPPLINKBAUD); + ppplink.enableIntTx(true); + logger.println(); + logger.printf("\n\nhey, trying to be a PPP server here\n\n"); + logger.printf("Now try this on your linux host:\n\n"); + logger.printf("connect a serial<->usb module (e.g. to /dev/ttyUSB1) and link it to the ESP (esprx=%d esptx=%d), then run:\n\n", RX, TX); + logger.printf("sudo /usr/sbin/pppd /dev/ttyUSB1 %d noipdefault nocrtscts local defaultroute noauth nodetach debug dump\n\n", PPPLINKBAUD); + + ppp.ifUpCb(PPPConnectedCallback); + bool ret = ppp.begin(WiFi.localIP()); + logger.printf("ppp: %d\n", ret); +} + + +#else + +void setup() { + Serial.begin(115200); + Serial.printf("\n\nPPP/NAPT not supported in this configuration\n"); +} + +#endif + +void loop() { +} diff --git a/libraries/lwIP_PPP/library.properties b/libraries/lwIP_PPP/library.properties new file mode 100644 index 0000000000..9b2293dbfb --- /dev/null +++ b/libraries/lwIP_PPP/library.properties @@ -0,0 +1,10 @@ +name=lwIP_PPP +version=1 +author=lwIP +maintainer=esp8266/Arduino +sentence=PPP interface +paragraph=PPP interface for esp8266 arduino +category=Network +url=https://github.com/esp8266/Arduino +architectures=esp8266 +dot_a_linkage=true diff --git a/libraries/lwIP_PPP/src/PPPServer.cpp b/libraries/lwIP_PPP/src/PPPServer.cpp new file mode 100644 index 0000000000..98fddd7777 --- /dev/null +++ b/libraries/lwIP_PPP/src/PPPServer.cpp @@ -0,0 +1,196 @@ + +// This is still beta / a work in progress + +// testing on linux: +// sudo /usr/sbin/pppd /dev/ttyUSB1 38400 noipdefault nocrtscts local defaultroute noauth nodetach debug dump +// sudo /usr/sbin/pppd /dev/ttyUSB1 38400 noipdefault nocrtscts local defaultroute noauth + +// proxy arp is needed but we don't have it +// http://lwip.100.n7.nabble.com/PPP-proxy-arp-support-tp33286p33345.html +// using NAT instead (see in example) + +#include +#include +#include +#include + +#include "PPPServer.h" + +PPPServer::PPPServer(Stream* sio): _sio(sio), _cb(netif_status_cb_s), _enabled(false) +{ +} + +bool PPPServer::handlePackets() +{ + size_t avail; + if ((avail = _sio->available()) > 0) + { + // XXX block peeking would be useful here + if (avail > _bufsize) + { + avail = _bufsize; + } + avail = _sio->readBytes(_buf, avail); + pppos_input(_ppp, _buf, avail); + } + return _enabled; +} + +void PPPServer::link_status_cb_s(ppp_pcb* pcb, int err_code, void* ctx) +{ + bool stop = true; + netif* nif = ppp_netif(pcb); + + switch (err_code) + { + case PPPERR_NONE: /* No error. */ + { +#if LWIP_DNS + const ip_addr_t *ns; +#endif /* LWIP_DNS */ + ets_printf("ppp_link_status_cb: PPPERR_NONE\n\r"); +#if LWIP_IPV4 + ets_printf(" our_ip4addr = %s\n\r", ip4addr_ntoa(netif_ip4_addr(nif))); + ets_printf(" his_ipaddr = %s\n\r", ip4addr_ntoa(netif_ip4_gw(nif))); + ets_printf(" netmask = %s\n\r", ip4addr_ntoa(netif_ip4_netmask(nif))); +#endif /* LWIP_IPV4 */ +#if LWIP_IPV6 + ets_printf(" our_ip6addr = %s\n\r", ip6addr_ntoa(netif_ip6_addr(nif, 0))); +#endif /* LWIP_IPV6 */ + +#if LWIP_DNS + ns = dns_getserver(0); + ets_printf(" dns1 = %s\n\r", ipaddr_ntoa(ns)); + ns = dns_getserver(1); + ets_printf(" dns2 = %s\n\r", ipaddr_ntoa(ns)); +#endif /* LWIP_DNS */ +#if PPP_IPV6_SUPPORT + ets_printf(" our6_ipaddr = %s\n\r", ip6addr_ntoa(netif_ip6_addr(nif, 0))); +#endif /* PPP_IPV6_SUPPORT */ + } + stop = false; + break; + + case PPPERR_PARAM: /* Invalid parameter. */ + ets_printf("ppp_link_status_cb: PPPERR_PARAM\n"); + break; + + case PPPERR_OPEN: /* Unable to open PPP session. */ + ets_printf("ppp_link_status_cb: PPPERR_OPEN\n"); + break; + + case PPPERR_DEVICE: /* Invalid I/O device for PPP. */ + ets_printf("ppp_link_status_cb: PPPERR_DEVICE\n"); + break; + + case PPPERR_ALLOC: /* Unable to allocate resources. */ + ets_printf("ppp_link_status_cb: PPPERR_ALLOC\n"); + break; + + case PPPERR_USER: /* User interrupt. */ + ets_printf("ppp_link_status_cb: PPPERR_USER\n"); + break; + + case PPPERR_CONNECT: /* Connection lost. */ + ets_printf("ppp_link_status_cb: PPPERR_CONNECT\n"); + break; + + case PPPERR_AUTHFAIL: /* Failed authentication challenge. */ + ets_printf("ppp_link_status_cb: PPPERR_AUTHFAIL\n"); + break; + + case PPPERR_PROTOCOL: /* Failed to meet protocol. */ + ets_printf("ppp_link_status_cb: PPPERR_PROTOCOL\n"); + break; + + case PPPERR_PEERDEAD: /* Connection timeout. */ + ets_printf("ppp_link_status_cb: PPPERR_PEERDEAD\n"); + break; + + case PPPERR_IDLETIMEOUT: /* Idle Timeout. */ + ets_printf("ppp_link_status_cb: PPPERR_IDLETIMEOUT\n"); + break; + + case PPPERR_CONNECTTIME: /* PPPERR_CONNECTTIME. */ + ets_printf("ppp_link_status_cb: PPPERR_CONNECTTIME\n"); + break; + + case PPPERR_LOOPBACK: /* Connection timeout. */ + ets_printf("ppp_link_status_cb: PPPERR_LOOPBACK\n"); + break; + + default: + ets_printf("ppp_link_status_cb: unknown errCode %d\n", err_code); + break; + } + + if (stop) + { + netif_remove(&static_cast(ctx)->_netif); + } +} + +u32_t PPPServer::output_cb_s(ppp_pcb* pcb, u8_t* data, u32_t len, void* ctx) +{ + (void)pcb; + (void)ctx; + return static_cast(ctx)->_sio->write(data, len); +} + +void PPPServer::netif_status_cb_s(netif* nif) +{ + ets_printf("PPPNETIF: %c%c%d is %s\n", nif->name[0], nif->name[1], nif->num, + netif_is_up(nif) ? "UP" : "DOWN"); +#if LWIP_IPV4 + ets_printf("IPV4: Host at %s ", ip4addr_ntoa(netif_ip4_addr(nif))); + ets_printf("mask %s ", ip4addr_ntoa(netif_ip4_netmask(nif))); + ets_printf("gateway %s\n", ip4addr_ntoa(netif_ip4_gw(nif))); +#endif /* LWIP_IPV4 */ +#if LWIP_IPV6 + ets_printf("IPV6: Host at %s\n", ip6addr_ntoa(netif_ip6_addr(nif, 0))); +#endif /* LWIP_IPV6 */ + ets_printf("FQDN: %s\n", netif_get_hostname(nif)); +} + +bool PPPServer::begin(const IPAddress& ourAddress, const IPAddress& peer) +{ + // lwip2-src/doc/ppp.txt + + _ppp = pppos_create(&_netif, PPPServer::output_cb_s, PPPServer::link_status_cb_s, this); + if (!_ppp) + { + return false; + } + + ppp_set_ipcp_ouraddr(_ppp, ip_2_ip4((const ip_addr_t*)ourAddress)); + ppp_set_ipcp_hisaddr(_ppp, ip_2_ip4((const ip_addr_t*)peer)); + + //ip4_addr_t addr; + //IP4_ADDR(&addr, 10,0,1,254); + //ppp_set_ipcp_dnsaddr(_ppp, 0, &addr); + + //ppp_set_auth(_ppp, PPPAUTHTYPE_ANY, "login", "password"); + //ppp_set_auth_required(_ppp, 1); + + ppp_set_silent(_ppp, 1); + ppp_listen(_ppp); + netif_set_status_callback(&_netif, _cb); + + _enabled = true; + if (!schedule_recurrent_function_us([&]() +{ + return this->handlePackets(); + }, 1000)) + { + netif_remove(&_netif); + return false; + } + + return true; +} + +void PPPServer::stop() +{ + _enabled = false; + ppp_close(_ppp, 0); +} diff --git a/libraries/lwIP_PPP/src/PPPServer.h b/libraries/lwIP_PPP/src/PPPServer.h new file mode 100644 index 0000000000..e2c95658a6 --- /dev/null +++ b/libraries/lwIP_PPP/src/PPPServer.h @@ -0,0 +1,77 @@ +/* + 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. + + Author: Dirk Ziegelmeier + +*/ + + +#ifndef __PPPSERVER_H +#define __PPPSERVER_H + +#include +#include +#include +#include +#include + +class PPPServer +{ +public: + + PPPServer(Stream* sio); + + bool begin(const IPAddress& ourAddress, const IPAddress& peer = IPAddress(172, 31, 255, 254)); + void stop(); + + void ifUpCb(void (*cb)(netif*)) + { + _cb = cb; + } + const ip_addr_t* getPeerAddress() const + { + return &_netif.gw; + } + +protected: + + static constexpr size_t _bufsize = 128; + Stream* _sio; + ppp_pcb* _ppp; + netif _netif; + void (*_cb)(netif*); + uint8_t _buf[_bufsize]; + bool _enabled; + + // feed ppp from stream - to call on a regular basis or on interrupt + bool handlePackets(); + + static u32_t output_cb_s(ppp_pcb* pcb, u8_t* data, u32_t len, void* ctx); + static void link_status_cb_s(ppp_pcb* pcb, int err_code, void* ctx); + static void netif_status_cb_s(netif* nif); + +}; + +#endif // __PPPSERVER_H diff --git a/libraries/lwIP_enc28j60/library.properties b/libraries/lwIP_enc28j60/library.properties new file mode 100644 index 0000000000..444e0f1c41 --- /dev/null +++ b/libraries/lwIP_enc28j60/library.properties @@ -0,0 +1,10 @@ +name=lwIP_enc28j60 +version=1 +author=Nicholas Humfrey +maintainer=esp8266/Arduino +sentence=Ethernet driver +paragraph=ENC28J60 ethernet drivers for lwIP and esp8266 Arduino from https://github.com/njh/EtherSia/tree/master/src/enc28j60.cpp +category=Network +url=https://github.com/esp8266/Arduino +architectures=esp8266 +dot_a_linkage=true diff --git a/libraries/lwIP_enc28j60/src/ENC28J60lwIP.h b/libraries/lwIP_enc28j60/src/ENC28J60lwIP.h new file mode 100644 index 0000000000..e525e94069 --- /dev/null +++ b/libraries/lwIP_enc28j60/src/ENC28J60lwIP.h @@ -0,0 +1,10 @@ + +#ifndef _ENC28J60LWIP_H +#define _ENC28J60LWIP_H + +#include +#include + +using ENC28J60lwIP = LwipIntfDev; + +#endif // _ENC28J60LWIP_H diff --git a/libraries/lwIP_enc28j60/src/utility/enc28j60.cpp b/libraries/lwIP_enc28j60/src/utility/enc28j60.cpp new file mode 100644 index 0000000000..42d1af7fc2 --- /dev/null +++ b/libraries/lwIP_enc28j60/src/utility/enc28j60.cpp @@ -0,0 +1,744 @@ +/* + Copyright (c) 2012-2013, Thingsquare, http://www.thingsquare.com/. + Copyright (c) 2016, Nicholas Humfrey + 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. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``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 + COPYRIGHT HOLDER OR CONTRIBUTORS 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. + +*/ + +// original sources: https://github.com/njh/EtherSia/tree/master/src/enc28j60.cpp + +#include +#include + +#include +#include +#include +#include + +#include "enc28j60.h" + + +void serial_printf(const char *fmt, ...) +{ + char buf[128]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, 128, fmt, args); + va_end(args); + Serial.print(buf); +} + +#define DEBUG 0 +#if DEBUG +#define PRINTF(...) printf(__VA_ARGS__) +#else +#define PRINTF(...) do { (void)0; } while (0) +#endif + +#define EIE 0x1b +#define EIR 0x1c +#define ESTAT 0x1d +#define ECON2 0x1e +#define ECON1 0x1f + +#define ESTAT_CLKRDY 0x01 +#define ESTAT_TXABRT 0x02 + +#define ECON1_RXEN 0x04 +#define ECON1_TXRTS 0x08 + +#define ECON2_AUTOINC 0x80 +#define ECON2_PKTDEC 0x40 + +#define EIR_TXIF 0x08 + +#define ERXTX_BANK 0x00 + +#define ERDPTL 0x00 +#define ERDPTH 0x01 +#define EWRPTL 0x02 +#define EWRPTH 0x03 +#define ETXSTL 0x04 +#define ETXSTH 0x05 +#define ETXNDL 0x06 +#define ETXNDH 0x07 +#define ERXSTL 0x08 +#define ERXSTH 0x09 +#define ERXNDL 0x0a +#define ERXNDH 0x0b +#define ERXRDPTL 0x0c +#define ERXRDPTH 0x0d + +#define RX_BUF_START 0x0000 +#define RX_BUF_END 0x0fff + +#define TX_BUF_START 0x1200 + +/* MACONx registers are in bank 2 */ +#define MACONX_BANK 0x02 + +#define MACON1 0x00 +#define MACON3 0x02 +#define MACON4 0x03 +#define MABBIPG 0x04 +#define MAIPGL 0x06 +#define MAIPGH 0x07 +#define MAMXFLL 0x0a +#define MAMXFLH 0x0b + +#define MACON1_TXPAUS 0x08 +#define MACON1_RXPAUS 0x04 +#define MACON1_MARXEN 0x01 + +#define MACON3_PADCFG_FULL 0xe0 +#define MACON3_TXCRCEN 0x10 +#define MACON3_FRMLNEN 0x02 +#define MACON3_FULDPX 0x01 + +#define MAX_MAC_LENGTH 1518 + +#define MAADRX_BANK 0x03 +#define MAADR1 0x04 /* MAADR<47:40> */ +#define MAADR2 0x05 /* MAADR<39:32> */ +#define MAADR3 0x02 /* MAADR<31:24> */ +#define MAADR4 0x03 /* MAADR<23:16> */ +#define MAADR5 0x00 /* MAADR<15:8> */ +#define MAADR6 0x01 /* MAADR<7:0> */ +#define MISTAT 0x0a +#define EREVID 0x12 + +#define EPKTCNT_BANK 0x01 +#define ERXFCON 0x18 +#define EPKTCNT 0x19 + +#define ERXFCON_UCEN 0x80 +#define ERXFCON_ANDOR 0x40 +#define ERXFCON_CRCEN 0x20 +#define ERXFCON_MCEN 0x02 +#define ERXFCON_BCEN 0x01 + +// The ENC28J60 SPI Interface supports clock speeds up to 20 MHz +static const SPISettings spiSettings(20000000, MSBFIRST, SPI_MODE0); + +ENC28J60::ENC28J60(int8_t cs, SPIClass& spi, int8_t intr): + _bank(ERXTX_BANK), _cs(cs), _spi(spi) +{ + (void)intr; +} + +void +ENC28J60::enc28j60_arch_spi_select(void) +{ + SPI.beginTransaction(spiSettings); + digitalWrite(_cs, LOW); +} + +void +ENC28J60::enc28j60_arch_spi_deselect(void) +{ + digitalWrite(_cs, HIGH); + SPI.endTransaction(); +} + + +/*---------------------------------------------------------------------------*/ +uint8_t +ENC28J60::is_mac_mii_reg(uint8_t reg) +{ + /* MAC or MII register (otherwise, ETH register)? */ + switch (_bank) + { + case MACONX_BANK: + return reg < EIE; + case MAADRX_BANK: + return reg <= MAADR2 || reg == MISTAT; + case ERXTX_BANK: + case EPKTCNT_BANK: + default: + return 0; + } +} +/*---------------------------------------------------------------------------*/ +uint8_t +ENC28J60::readreg(uint8_t reg) +{ + uint8_t r; + enc28j60_arch_spi_select(); + SPI.transfer(0x00 | (reg & 0x1f)); + if (is_mac_mii_reg(reg)) + { + /* MAC and MII registers require that a dummy byte be read first. */ + SPI.transfer(0); + } + r = SPI.transfer(0); + enc28j60_arch_spi_deselect(); + return r; +} +/*---------------------------------------------------------------------------*/ +void +ENC28J60::writereg(uint8_t reg, uint8_t data) +{ + enc28j60_arch_spi_select(); + SPI.transfer(0x40 | (reg & 0x1f)); + SPI.transfer(data); + enc28j60_arch_spi_deselect(); +} +/*---------------------------------------------------------------------------*/ +void +ENC28J60::setregbitfield(uint8_t reg, uint8_t mask) +{ + if (is_mac_mii_reg(reg)) + { + writereg(reg, readreg(reg) | mask); + } + else + { + enc28j60_arch_spi_select(); + SPI.transfer(0x80 | (reg & 0x1f)); + SPI.transfer(mask); + enc28j60_arch_spi_deselect(); + } +} +/*---------------------------------------------------------------------------*/ +void +ENC28J60::clearregbitfield(uint8_t reg, uint8_t mask) +{ + if (is_mac_mii_reg(reg)) + { + writereg(reg, readreg(reg) & ~mask); + } + else + { + enc28j60_arch_spi_select(); + SPI.transfer(0xa0 | (reg & 0x1f)); + SPI.transfer(mask); + enc28j60_arch_spi_deselect(); + } +} +/*---------------------------------------------------------------------------*/ +void +ENC28J60::setregbank(uint8_t new_bank) +{ + writereg(ECON1, (readreg(ECON1) & 0xfc) | (new_bank & 0x03)); + _bank = new_bank; +} +/*---------------------------------------------------------------------------*/ +void +ENC28J60::writedata(const uint8_t *data, int datalen) +{ + int i; + enc28j60_arch_spi_select(); + /* The Write Buffer Memory (WBM) command is 0 1 1 1 1 0 1 0 */ + SPI.transfer(0x7a); + for (i = 0; i < datalen; i++) + { + SPI.transfer(data[i]); + } + enc28j60_arch_spi_deselect(); +} +/*---------------------------------------------------------------------------*/ +void +ENC28J60::writedatabyte(uint8_t byte) +{ + writedata(&byte, 1); +} +/*---------------------------------------------------------------------------*/ +int +ENC28J60::readdata(uint8_t *buf, int len) +{ + int i; + enc28j60_arch_spi_select(); + /* THe Read Buffer Memory (RBM) command is 0 0 1 1 1 0 1 0 */ + SPI.transfer(0x3a); + for (i = 0; i < len; i++) + { + buf[i] = SPI.transfer(0); + } + enc28j60_arch_spi_deselect(); + return i; +} +/*---------------------------------------------------------------------------*/ +uint8_t +ENC28J60::readdatabyte(void) +{ + uint8_t r; + readdata(&r, 1); + return r; +} + +/*---------------------------------------------------------------------------*/ +void +ENC28J60::softreset(void) +{ + enc28j60_arch_spi_select(); + /* The System Command (soft reset) is 1 1 1 1 1 1 1 1 */ + SPI.transfer(0xff); + enc28j60_arch_spi_deselect(); + _bank = ERXTX_BANK; +} + +/*---------------------------------------------------------------------------*/ +//#if DEBUG +uint8_t +ENC28J60::readrev(void) +{ + uint8_t rev; + setregbank(MAADRX_BANK); + rev = readreg(EREVID); + switch (rev) + { + case 2: + return 1; + case 6: + return 7; + default: + return rev; + } +} +//#endif + +/*---------------------------------------------------------------------------*/ + +bool +ENC28J60::reset(void) +{ + PRINTF("enc28j60: resetting chip\n"); + + pinMode(_cs, OUTPUT); + digitalWrite(_cs, HIGH); + SPI.begin(); + + /* + 6.0 INITIALIZATION + + Before the ENC28J60 can be used to transmit and receive packets, + certain device settings must be initialized. Depending on the + application, some configuration options may need to be + changed. Normally, these tasks may be accomplished once after + Reset and do not need to be changed thereafter. + + 6.1 Receive Buffer + + Before receiving any packets, the receive buffer must be + initialized by programming the ERXST and ERXND pointers. All + memory between and including the ERXST and ERXND addresses will be + dedicated to the receive hardware. It is recommended that the + ERXST pointer be programmed with an even address. + + Applications expecting large amounts of data and frequent packet + delivery may wish to allocate most of the memory as the receive + buffer. Applications that may need to save older packets or have + several packets ready for transmission should allocate less + memory. + + When programming the ERXST pointer, the ERXWRPT registers will + automatically be updated with the same values. The address in + ERXWRPT will be used as the starting location when the receive + hardware begins writing received data. For tracking purposes, the + ERXRDPT registers should additionally be programmed with the same + value. To program ERXRDPT, the host controller must write to + ERXRDPTL first, followed by ERXRDPTH. See Section 7.2.4 โ€œFreeing + Receive Buffer Space for more information + + 6.2 Transmission Buffer + + All memory which is not used by the receive buffer is considered + the transmission buffer. Data which is to be transmitted should be + written into any unused space. After a packet is transmitted, + however, the hardware will write a seven-byte status vector into + memory after the last byte in the packet. Therefore, the host + controller should leave at least seven bytes between each packet + and the beginning of the receive buffer. No explicit action is + required to initialize the transmission buffer. + + 6.3 Receive Filters + + The appropriate receive filters should be enabled or disabled by + writing to the ERXFCON register. See Section 8.0 โ€œReceive Filters + for information on how to configure it. + + 6.4 Waiting For OST + + If the initialization procedure is being executed immediately + following a Power-on Reset, the ESTAT.CLKRDY bit should be polled + to make certain that enough time has elapsed before proceeding to + modify the MAC and PHY registers. For more information on the OST, + see Section 2.2 โ€œOscillator Start-up Timer. + */ + + softreset(); + + /* Workaround for erratum #2. */ + delayMicroseconds(1000); + + /* Wait for OST */ + PRINTF("waiting for ESTAT_CLKRDY\n"); + while ((readreg(ESTAT) & ESTAT_CLKRDY) == 0) {}; + PRINTF("ESTAT_CLKRDY\n"); + + setregbank(ERXTX_BANK); + /* Set up receive buffer */ + writereg(ERXSTL, RX_BUF_START & 0xff); + writereg(ERXSTH, RX_BUF_START >> 8); + writereg(ERXNDL, RX_BUF_END & 0xff); + writereg(ERXNDH, RX_BUF_END >> 8); + writereg(ERDPTL, RX_BUF_START & 0xff); + writereg(ERDPTH, RX_BUF_START >> 8); + writereg(ERXRDPTL, RX_BUF_END & 0xff); + writereg(ERXRDPTH, RX_BUF_END >> 8); + + /* Receive filters */ + setregbank(EPKTCNT_BANK); + writereg(ERXFCON, ERXFCON_UCEN | ERXFCON_CRCEN | ERXFCON_MCEN); + + /* + 6.5 MAC Initialization Settings + + Several of the MAC registers require configuration during + initialization. This only needs to be done once; the order of + programming is unimportant. + + 1. Set the MARXEN bit in MACON1 to enable the MAC to receive + frames. If using full duplex, most applications should also set + TXPAUS and RXPAUS to allow IEEE defined flow control to function. + + 2. Configure the PADCFG, TXCRCEN and FULDPX bits of MACON3. Most + applications should enable automatic padding to at least 60 bytes + and always append a valid CRC. For convenience, many applications + may wish to set the FRMLNEN bit as well to enable frame length + status reporting. The FULDPX bit should be set if the application + will be connected to a full-duplex configured remote node; + otherwise, it should be left clear. + + 3. Configure the bits in MACON4. For conformance to the IEEE 802.3 + standard, set the DEFER bit. + + 4. Program the MAMXFL registers with the maximum frame length to + be permitted to be received or transmitted. Normal network nodes + are designed to handle packets that are 1518 bytes or less. + + 5. Configure the Back-to-Back Inter-Packet Gap register, + MABBIPG. Most applications will program this register with 15h + when Full-Duplex mode is used and 12h when Half-Duplex mode is + used. + + 6. Configure the Non-Back-to-Back Inter-Packet Gap register low + byte, MAIPGL. Most applications will program this register with + 12h. + + 7. If half duplex is used, the Non-Back-to-Back Inter-Packet Gap + register high byte, MAIPGH, should be programmed. Most + applications will program this register to 0Ch. + + 8. If Half-Duplex mode is used, program the Retransmission and + Collision Window registers, MACLCON1 and MACLCON2. Most + applications will not need to change the default Reset values. If + the network is spread over exceptionally long cables, the default + value of MACLCON2 may need to be increased. + + 9. Program the local MAC address into the MAADR1:MAADR6 registers. + */ + + setregbank(MACONX_BANK); + + /* Turn on reception and IEEE-defined flow control */ + setregbitfield(MACON1, MACON1_MARXEN | MACON1_TXPAUS | MACON1_RXPAUS); + + /* Set padding, crc, full duplex */ + setregbitfield(MACON3, MACON3_PADCFG_FULL | MACON3_TXCRCEN | MACON3_FULDPX | + MACON3_FRMLNEN); + + /* Don't modify MACON4 */ + + /* Set maximum frame length in MAMXFL */ + writereg(MAMXFLL, MAX_MAC_LENGTH & 0xff); + writereg(MAMXFLH, MAX_MAC_LENGTH >> 8); + + /* Set back-to-back inter packet gap */ + writereg(MABBIPG, 0x15); + + /* Set non-back-to-back packet gap */ + writereg(MAIPGL, 0x12); + + /* Set MAC address */ + setregbank(MAADRX_BANK); + writereg(MAADR6, _localMac[5]); + writereg(MAADR5, _localMac[4]); + writereg(MAADR4, _localMac[3]); + writereg(MAADR3, _localMac[2]); + writereg(MAADR2, _localMac[1]); + writereg(MAADR1, _localMac[0]); + + /* + 6.6 PHY Initialization Settings + + Depending on the application, bits in three of the PHY moduleโ€™s + registers may also require configuration. The PHCON1.PDPXMD bit + partially controls the deviceโ€™s half/full-duplex + configuration. Normally, this bit is initialized correctly by the + external circuitry (see Section 2.6 โ€œLED Configuration). If the + external circuitry is not present or incorrect, however, the host + controller must program the bit properly. Alternatively, for an + externally configurable system, the PDPXMD bit may be read and the + FULDPX bit be programmed to match. + + For proper duplex operation, the PHCON1.PDPXMD bit must also match + the value of the MACON3.FULDPX bit. + + If using half duplex, the host controller may wish to set the + PHCON2.HDLDIS bit to prevent automatic loopback of the data which + is transmitted. The PHY register, PHLCON, controls the outputs of + LEDA and LEDB. If an application requires a LED configuration + other than the default, PHLCON must be altered to match the new + requirements. The settings for LED operation are discussed in + Section 2.6 โ€œLED Configuration. The PHLCON register is shown in + Register 2-2 (page 9). + */ + + /* Don't worry about PHY configuration for now */ + + /* Turn on autoincrement for buffer access */ + setregbitfield(ECON2, ECON2_AUTOINC); + + /* Turn on reception */ + writereg(ECON1, ECON1_RXEN); + + return true; +} +/*---------------------------------------------------------------------------*/ +boolean +ENC28J60::begin(const uint8_t *address) +{ + _localMac = address; + + bool ret = reset(); + uint8_t rev = readrev(); + + PRINTF("ENC28J60 rev. B%d\n", rev); + + return ret && rev != 255; +} + +/*---------------------------------------------------------------------------*/ + +uint16_t +ENC28J60::sendFrame(const uint8_t *data, uint16_t datalen) +{ + uint16_t dataend; + + /* + 1. Appropriately program the ETXST pointer to point to an unused + location in memory. It will point to the per packet control + byte. In the example, it would be programmed to 0120h. It is + recommended that an even address be used for ETXST. + + 2. Use the WBM SPI command to write the per packet control byte, + the destination address, the source MAC address, the + type/length and the data payload. + + 3. Appropriately program the ETXND pointer. It should point to the + last byte in the data payload. In the example, it would be + programmed to 0156h. + + 4. Clear EIR.TXIF, set EIE.TXIE and set EIE.INTIE to enable an + interrupt when done (if desired). + + 5. Start the transmission process by setting + ECON1.TXRTS. + */ + + setregbank(ERXTX_BANK); + /* Set up the transmit buffer pointer */ + writereg(ETXSTL, TX_BUF_START & 0xff); + writereg(ETXSTH, TX_BUF_START >> 8); + writereg(EWRPTL, TX_BUF_START & 0xff); + writereg(EWRPTH, TX_BUF_START >> 8); + + /* Write the transmission control register as the first byte of the + output packet. We write 0x00 to indicate that the default + configuration (the values in MACON3) will be used. */ + writedatabyte(0x00); /* MACON3 */ + + writedata(data, datalen); + + /* Write a pointer to the last data byte. */ + dataend = TX_BUF_START + datalen; + writereg(ETXNDL, dataend & 0xff); + writereg(ETXNDH, dataend >> 8); + + /* Clear EIR.TXIF */ + clearregbitfield(EIR, EIR_TXIF); + + /* Don't care about interrupts for now */ + + /* Send the packet */ + setregbitfield(ECON1, ECON1_TXRTS); + while ((readreg(ECON1) & ECON1_TXRTS) > 0); + +#if DEBUG + if ((readreg(ESTAT) & ESTAT_TXABRT) != 0) + { + uint16_t erdpt; + uint8_t tsv[7]; + erdpt = (readreg(ERDPTH) << 8) | readreg(ERDPTL); + writereg(ERDPTL, (dataend + 1) & 0xff); + writereg(ERDPTH, (dataend + 1) >> 8); + readdata(tsv, sizeof(tsv)); + writereg(ERDPTL, erdpt & 0xff); + writereg(ERDPTH, erdpt >> 8); + PRINTF("enc28j60: tx err: %d: %02x:%02x:%02x:%02x:%02x:%02x\n" + " tsv: %02x%02x%02x%02x%02x%02x%02x\n", datalen, + 0xff & data[0], 0xff & data[1], 0xff & data[2], + 0xff & data[3], 0xff & data[4], 0xff & data[5], + tsv[6], tsv[5], tsv[4], tsv[3], tsv[2], tsv[1], tsv[0]); + } + else + { + PRINTF("enc28j60: tx: %d: %02x:%02x:%02x:%02x:%02x:%02x\n", datalen, + 0xff & data[0], 0xff & data[1], 0xff & data[2], + 0xff & data[3], 0xff & data[4], 0xff & data[5]); + } +#endif + + //sent_packets++; + //PRINTF("enc28j60: sent_packets %d\n", sent_packets); + return datalen; +} + +/*---------------------------------------------------------------------------*/ + +uint16_t +ENC28J60::readFrame(uint8_t *buffer, uint16_t bufsize) +{ + readFrameSize(); + return readFrameData(buffer, bufsize); +} + +uint16_t +ENC28J60::readFrameSize() +{ + uint8_t n; + + uint8_t nxtpkt[2]; + uint8_t status[2]; + uint8_t length[2]; + + setregbank(EPKTCNT_BANK); + n = readreg(EPKTCNT); + + if (n == 0) + { + return 0; + } + + PRINTF("enc28j60: EPKTCNT 0x%02x\n", n); + + setregbank(ERXTX_BANK); + /* Read the next packet pointer */ + nxtpkt[0] = readdatabyte(); + nxtpkt[1] = readdatabyte(); + _next = (nxtpkt[1] << 8) + nxtpkt[0]; + + PRINTF("enc28j60: nxtpkt 0x%02x%02x\n", _nxtpkt[1], _nxtpkt[0]); + + length[0] = readdatabyte(); + length[1] = readdatabyte(); + _len = (length[1] << 8) + length[0]; + + PRINTF("enc28j60: length 0x%02x%02x\n", length[1], length[0]); + + status[0] = readdatabyte(); + status[1] = readdatabyte(); + + /* This statement is just to avoid a compiler warning: */ + (void)status[0]; + PRINTF("enc28j60: status 0x%02x%02x\n", status[1], status[0]); + + return _len; +} + +void +ENC28J60::discardFrame(uint16_t framesize) +{ + (void)framesize; + (void)readFrameData(nullptr, 0); +} + +uint16_t +ENC28J60::readFrameData(uint8_t *buffer, uint16_t framesize) +{ + if (framesize < _len) + { + + buffer = nullptr; + + /* flush rx fifo */ + for (uint16_t i = 0; i < _len; i++) + { + readdatabyte(); + } + } + else + { + readdata(buffer, _len); + } + + /* Read an additional byte at odd lengths, to avoid FIFO corruption */ + if ((_len % 2) != 0) + { + readdatabyte(); + } + + /* Errata #14 */ + if (_next == RX_BUF_START) + { + _next = RX_BUF_END; + } + else + { + _next = _next - 1; + } + writereg(ERXRDPTL, _next & 0xff); + writereg(ERXRDPTH, _next >> 8); + + setregbitfield(ECON2, ECON2_PKTDEC); + + if (!buffer) + { + PRINTF("enc28j60: rx err: flushed %d\n", _len); + return 0; + } + PRINTF("enc28j60: rx: %d: %02x:%02x:%02x:%02x:%02x:%02x\n", _len, + 0xff & buffer[0], 0xff & buffer[1], 0xff & buffer[2], + 0xff & buffer[3], 0xff & buffer[4], 0xff & buffer[5]); + + //received_packets++; + //PRINTF("enc28j60: received_packets %d\n", received_packets); + + return _len; +} diff --git a/libraries/lwIP_enc28j60/src/utility/enc28j60.h b/libraries/lwIP_enc28j60/src/utility/enc28j60.h new file mode 100644 index 0000000000..0545039026 --- /dev/null +++ b/libraries/lwIP_enc28j60/src/utility/enc28j60.h @@ -0,0 +1,149 @@ +/** + Header file for direct Ethernet frame access to the ENC28J60 controller + @file enc28j60.h +*/ + +/* + Copyright (c) 2012-2013, Thingsquare, http://www.thingsquare.com/. + 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. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``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 + COPYRIGHT HOLDER OR CONTRIBUTORS 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. + +*/ + +// original sources: https://github.com/njh/EtherSia/tree/master/src/enc28j60.h + +#ifndef ENC28J60_H +#define ENC28J60_H + +#include + +/** + Send and receive Ethernet frames directly using a ENC28J60 controller. +*/ +class ENC28J60 +{ + +public: + /** + Constructor that uses the default hardware SPI pins + @param cs the Arduino Chip Select / Slave Select pin (default 10 on Uno) + */ + ENC28J60(int8_t cs = SS, SPIClass& spi = SPI, int8_t intr = -1); + + /** + Initialise the Ethernet controller + Must be called before sending or receiving Ethernet frames + + @param address the local MAC address for the Ethernet interface + @return Returns true if setting up the Ethernet interface was successful + */ + boolean begin(const uint8_t *address); + + /** + Send an Ethernet frame + @param data a pointer to the data to send + @param datalen the length of the data in the packet + @return the number of bytes transmitted + */ + virtual uint16_t sendFrame(const uint8_t *data, uint16_t datalen); + + /** + Read an Ethernet frame + @param buffer a pointer to a buffer to write the packet to + @param bufsize the available space in the buffer + @return the length of the received packet + or 0 if no packet was received + */ + virtual uint16_t readFrame(uint8_t *buffer, uint16_t bufsize); + +protected: + + static constexpr bool interruptIsPossible() + { + return false; + } + + /** + Read an Ethernet frame size + @return the length of data do receive + or 0 if no frame was received + */ + uint16_t readFrameSize(); + + /** + discard an Ethernet frame + @param framesize readFrameSize()'s result + */ + void discardFrame(uint16_t framesize); + + /** + Read an Ethernet frame data + readFrameSize() must be called first, + its result must be passed into framesize parameter + @param buffer a pointer to a buffer to write the frame to + @param framesize readFrameSize()'s result + @return the length of the received frame + or 0 if a problem occured + */ + uint16_t readFrameData(uint8_t *frame, uint16_t framesize); + +private: + + uint8_t is_mac_mii_reg(uint8_t reg); + uint8_t readreg(uint8_t reg); + void writereg(uint8_t reg, uint8_t data); + void setregbitfield(uint8_t reg, uint8_t mask); + void clearregbitfield(uint8_t reg, uint8_t mask); + void setregbank(uint8_t new_bank); + void writedata(const uint8_t *data, int datalen); + void writedatabyte(uint8_t byte); + int readdata(uint8_t *buf, int len); + uint8_t readdatabyte(void); + void softreset(void); + uint8_t readrev(void); + bool reset(void); + + void enc28j60_arch_spi_init(void); + uint8_t enc28j60_arch_spi_write(uint8_t data); + uint8_t enc28j60_arch_spi_read(void); + void enc28j60_arch_spi_select(void); + void enc28j60_arch_spi_deselect(void); + + // Previously defined in contiki/core/sys/clock.h + void clock_delay_usec(uint16_t dt); + + uint8_t _bank; + int8_t _cs; + SPIClass& _spi; + + const uint8_t *_localMac; + + /* readFrame*() state */ + uint16_t _next, _len; +}; + +#endif /* ENC28J60_H */ diff --git a/libraries/lwIP_w5100/library.properties b/libraries/lwIP_w5100/library.properties new file mode 100644 index 0000000000..b65d4fdb64 --- /dev/null +++ b/libraries/lwIP_w5100/library.properties @@ -0,0 +1,10 @@ +name=lwIP_w5500 +version=1 +author=Nicholas Humfrey +maintainer=esp8266/Arduino +sentence=Ethernet driver +paragraph=Wiznet5100 ethernet drivers for lwIP and esp8266 Arduino from https://github.com/njh/W5100MacRaw +category=Network +url=https://github.com/esp8266/Arduino +architectures=esp8266 +dot_a_linkage=true diff --git a/libraries/lwIP_w5100/src/W5100lwIP.h b/libraries/lwIP_w5100/src/W5100lwIP.h new file mode 100644 index 0000000000..c8f549529d --- /dev/null +++ b/libraries/lwIP_w5100/src/W5100lwIP.h @@ -0,0 +1,10 @@ + +#ifndef _W5100LWIP_H +#define _W5100LWIP_H + +#include +#include + +using Wiznet5100lwIP = LwipIntfDev; + +#endif // _W5500LWIP_H diff --git a/libraries/lwIP_w5100/src/utility/w5100.cpp b/libraries/lwIP_w5100/src/utility/w5100.cpp new file mode 100644 index 0000000000..6377aa5b63 --- /dev/null +++ b/libraries/lwIP_w5100/src/utility/w5100.cpp @@ -0,0 +1,369 @@ +/* + Copyright (c) 2013, WIZnet Co., Ltd. + Copyright (c) 2016, Nicholas Humfrey + 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. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``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 + COPYRIGHT HOLDER OR CONTRIBUTORS 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. + +*/ + +// original sources: https://github.com/njh/W5100MacRaw + +#include +#include "w5100.h" + + +uint8_t Wiznet5100::wizchip_read(uint16_t address) +{ + uint8_t ret; + + wizchip_cs_select(); + _spi.transfer(0x0F); + _spi.transfer((address & 0xFF00) >> 8); + _spi.transfer((address & 0x00FF) >> 0); + ret = _spi.transfer(0); + wizchip_cs_deselect(); + + return ret; +} + +uint16_t Wiznet5100::wizchip_read_word(uint16_t address) +{ + return ((uint16_t)wizchip_read(address) << 8) + wizchip_read(address + 1); +} + + +void Wiznet5100::wizchip_read_buf(uint16_t address, uint8_t* pBuf, uint16_t len) +{ + for (uint16_t i = 0; i < len; i++) + { + pBuf[i] = wizchip_read(address + i); + } +} + +void Wiznet5100::wizchip_write(uint16_t address, uint8_t wb) +{ + wizchip_cs_select(); + _spi.transfer(0xF0); + _spi.transfer((address & 0xFF00) >> 8); + _spi.transfer((address & 0x00FF) >> 0); + _spi.transfer(wb); // Data write (write 1byte data) + wizchip_cs_deselect(); +} + +void Wiznet5100::wizchip_write_word(uint16_t address, uint16_t word) +{ + wizchip_write(address, (uint8_t)(word >> 8)); + wizchip_write(address + 1, (uint8_t) word); +} + +void Wiznet5100::wizchip_write_buf(uint16_t address, const uint8_t* pBuf, uint16_t len) +{ + for (uint16_t i = 0; i < len; i++) + { + wizchip_write(address + i, pBuf[i]); + } +} + +void Wiznet5100::setSn_CR(uint8_t cr) +{ + // Write the command to the Command Register + wizchip_write(Sn_CR, cr); + + // Now wait for the command to complete + while (wizchip_read(Sn_CR)); +} + +uint16_t Wiznet5100::getSn_TX_FSR() +{ + uint16_t val = 0, val1 = 0; + do + { + val1 = wizchip_read_word(Sn_TX_FSR); + if (val1 != 0) + { + val = wizchip_read_word(Sn_TX_FSR); + } + } while (val != val1); + return val; +} + + +uint16_t Wiznet5100::getSn_RX_RSR() +{ + uint16_t val = 0, val1 = 0; + do + { + val1 = wizchip_read_word(Sn_RX_RSR); + if (val1 != 0) + { + val = wizchip_read_word(Sn_RX_RSR); + } + } while (val != val1); + return val; +} + +void Wiznet5100::wizchip_send_data(const uint8_t *wizdata, uint16_t len) +{ + uint16_t ptr; + uint16_t size; + uint16_t dst_mask; + uint16_t dst_ptr; + + ptr = getSn_TX_WR(); + + dst_mask = ptr & TxBufferMask; + dst_ptr = TxBufferAddress + dst_mask; + + if (dst_mask + len > TxBufferLength) + { + size = TxBufferLength - dst_mask; + wizchip_write_buf(dst_ptr, wizdata, size); + wizdata += size; + size = len - size; + dst_ptr = TxBufferAddress; + wizchip_write_buf(dst_ptr, wizdata, size); + } + else + { + wizchip_write_buf(dst_ptr, wizdata, len); + } + + ptr += len; + + setSn_TX_WR(ptr); +} + +void Wiznet5100::wizchip_recv_data(uint8_t *wizdata, uint16_t len) +{ + uint16_t ptr; + uint16_t size; + uint16_t src_mask; + uint16_t src_ptr; + + ptr = getSn_RX_RD(); + + src_mask = ptr & RxBufferMask; + src_ptr = RxBufferAddress + src_mask; + + + if ((src_mask + len) > RxBufferLength) + { + size = RxBufferLength - src_mask; + wizchip_read_buf(src_ptr, wizdata, size); + wizdata += size; + size = len - size; + src_ptr = RxBufferAddress; + wizchip_read_buf(src_ptr, wizdata, size); + } + else + { + wizchip_read_buf(src_ptr, wizdata, len); + } + + ptr += len; + + setSn_RX_RD(ptr); +} + +void Wiznet5100::wizchip_recv_ignore(uint16_t len) +{ + uint16_t ptr; + + ptr = getSn_RX_RD(); + ptr += len; + setSn_RX_RD(ptr); +} + +void Wiznet5100::wizchip_sw_reset() +{ + setMR(MR_RST); + getMR(); // for delay + + setSHAR(_mac_address); +} + + +Wiznet5100::Wiznet5100(int8_t cs, SPIClass& spi, int8_t intr): + _spi(spi), _cs(cs) +{ + (void)intr; +} + +boolean Wiznet5100::begin(const uint8_t *mac_address) +{ + memcpy(_mac_address, mac_address, 6); + + pinMode(_cs, OUTPUT); + wizchip_cs_deselect(); + +#if 0 + _spi.begin(); + _spi.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz? + _spi.setBitOrder(MSBFIRST); + _spi.setDataMode(SPI_MODE0); +#endif + + wizchip_sw_reset(); + + // Set the size of the Rx and Tx buffers + wizchip_write(RMSR, RxBufferSize); + wizchip_write(TMSR, TxBufferSize); + + // Set our local MAC address + setSHAR(_mac_address); + + // Open Socket 0 in MACRaw mode + setSn_MR(Sn_MR_MACRAW); + setSn_CR(Sn_CR_OPEN); + if (getSn_SR() != SOCK_MACRAW) + { + // Failed to put socket 0 into MACRaw mode + return false; + } + + // Success + return true; +} + +void Wiznet5100::end() +{ + setSn_CR(Sn_CR_CLOSE); + + // clear all interrupt of the socket + setSn_IR(0xFF); + + // Wait for socket to change to closed + while (getSn_SR() != SOCK_CLOSED); +} + +uint16_t Wiznet5100::readFrame(uint8_t *buffer, uint16_t bufsize) +{ + uint16_t data_len = readFrameSize(); + + if (data_len == 0) + { + return 0; + } + + if (data_len > bufsize) + { + // Packet is bigger than buffer - drop the packet + discardFrame(data_len); + return 0; + } + + return readFrameData(buffer, data_len); +} + +uint16_t Wiznet5100::readFrameSize() +{ + uint16_t len = getSn_RX_RSR(); + + if (len == 0) + { + return 0; + } + + uint8_t head[2]; + uint16_t data_len = 0; + + wizchip_recv_data(head, 2); + setSn_CR(Sn_CR_RECV); + + data_len = head[0]; + data_len = (data_len << 8) + head[1]; + data_len -= 2; + + return data_len; +} + +void Wiznet5100::discardFrame(uint16_t framesize) +{ + wizchip_recv_ignore(framesize); + setSn_CR(Sn_CR_RECV); +} + +uint16_t Wiznet5100::readFrameData(uint8_t *buffer, uint16_t framesize) +{ + wizchip_recv_data(buffer, framesize); + setSn_CR(Sn_CR_RECV); + +#if 1 + // let lwIP deal with mac address filtering + return framesize; +#else + // W5100 doesn't have any built-in MAC address filtering + if ((buffer[0] & 0x01) || memcmp(&buffer[0], _mac_address, 6) == 0) + { + // Addressed to an Ethernet multicast address or our unicast address + return framesize; + } + else + { + return 0; + } +#endif +} + +uint16_t Wiznet5100::sendFrame(const uint8_t *buf, uint16_t len) +{ + // Wait for space in the transmit buffer + while (1) + { + uint16_t freesize = getSn_TX_FSR(); + if (getSn_SR() == SOCK_CLOSED) + { + return -1; + } + if (len <= freesize) + { + break; + } + }; + + wizchip_send_data(buf, len); + setSn_CR(Sn_CR_SEND); + + while (1) + { + uint8_t tmp = getSn_IR(); + if (tmp & Sn_IR_SENDOK) + { + setSn_IR(Sn_IR_SENDOK); + // Packet sent ok + break; + } + else if (tmp & Sn_IR_TIMEOUT) + { + setSn_IR(Sn_IR_TIMEOUT); + // There was a timeout + return -1; + } + } + + return len; +} diff --git a/libraries/lwIP_w5100/src/utility/w5100.h b/libraries/lwIP_w5100/src/utility/w5100.h new file mode 100644 index 0000000000..300cb334a4 --- /dev/null +++ b/libraries/lwIP_w5100/src/utility/w5100.h @@ -0,0 +1,499 @@ +/* + Copyright (c) 2013, WIZnet Co., Ltd. + Copyright (c) 2016, Nicholas Humfrey + 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. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``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 + COPYRIGHT HOLDER OR CONTRIBUTORS 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. + +*/ + +// original sources: https://github.com/njh/W5100MacRaw + +#ifndef W5100_H +#define W5100_H + +#include +#include +#include + + +class Wiznet5100 +{ + +public: + /** + Constructor that uses the default hardware SPI pins + @param cs the Arduino Chip Select / Slave Select pin (default 10) + */ + Wiznet5100(int8_t cs = SS, SPIClass& spi = SPI, int8_t intr = -1); + + /** + Initialise the Ethernet controller + Must be called before sending or receiving Ethernet frames + + @param address the local MAC address for the Ethernet interface + @return Returns true if setting up the Ethernet interface was successful + */ + boolean begin(const uint8_t *address); + + /** + Shut down the Ethernet controlled + */ + void end(); + + /** + Send an Ethernet frame + @param data a pointer to the data to send + @param datalen the length of the data in the packet + @return the number of bytes transmitted + */ + uint16_t sendFrame(const uint8_t *data, uint16_t datalen); + + /** + Read an Ethernet frame + @param buffer a pointer to a buffer to write the packet to + @param bufsize the available space in the buffer + @return the length of the received packet + or 0 if no packet was received + */ + uint16_t readFrame(uint8_t *buffer, uint16_t bufsize); + +protected: + + static constexpr bool interruptIsPossible() + { + return false; + } + + /** + Read an Ethernet frame size + @return the length of data do receive + or 0 if no frame was received + */ + uint16_t readFrameSize(); + + /** + discard an Ethernet frame + @param framesize readFrameSize()'s result + */ + void discardFrame(uint16_t framesize); + + /** + Read an Ethernet frame data + readFrameSize() must be called first, + its result must be passed into framesize parameter + @param buffer a pointer to a buffer to write the frame to + @param framesize readFrameSize()'s result + @return the length of the received frame + or 0 if a problem occured + */ + uint16_t readFrameData(uint8_t *frame, uint16_t framesize); + + +private: + static const uint16_t TxBufferAddress = 0x4000; /* Internal Tx buffer address of the iinchip */ + static const uint16_t RxBufferAddress = 0x6000; /* Internal Rx buffer address of the iinchip */ + static const uint8_t TxBufferSize = 0x3; /* Buffer size configuration: 0=1kb, 1=2kB, 2=4kB, 3=8kB */ + static const uint8_t RxBufferSize = 0x3; /* Buffer size configuration: 0=1kb, 1=2kB, 2=4kB, 3=8kB */ + static const uint16_t TxBufferLength = (1 << TxBufferSize) << 10; /* Length of Tx buffer in bytes */ + static const uint16_t RxBufferLength = (1 << RxBufferSize) << 10; /* Length of Rx buffer in bytes */ + static const uint16_t TxBufferMask = TxBufferLength - 1; + static const uint16_t RxBufferMask = RxBufferLength - 1; + + + SPIClass& _spi; + int8_t _cs; + uint8_t _mac_address[6]; + + /** + Default function to select chip. + @note This function help not to access wrong address. If you do not describe this function or register any functions, + null function is called. + */ + inline void wizchip_cs_select() + { + digitalWrite(_cs, LOW); + } + + /** + Default function to deselect chip. + @note This function help not to access wrong address. If you do not describe this function or register any functions, + null function is called. + */ + inline void wizchip_cs_deselect() + { + digitalWrite(_cs, HIGH); + } + + /** + Read a 1 byte value from a register. + @param address Register address + @return The value of register + */ + uint8_t wizchip_read(uint16_t address); + + /** + Reads a 2 byte value from a register. + @param address Register address + @return The value of register + */ + uint16_t wizchip_read_word(uint16_t address); + + /** + It reads sequence data from registers. + @param address Register address + @param pBuf Pointer buffer to read data + @param len Data length + */ + void wizchip_read_buf(uint16_t address, uint8_t* pBuf, uint16_t len); + + /** + Write a 1 byte value to a register. + @param address Register address + @param wb Write data + @return void + */ + void wizchip_write(uint16_t address, uint8_t wb); + + /** + Write a 2 byte value to a register. + @param address Register address + @param wb Write data + @return void + */ + void wizchip_write_word(uint16_t address, uint16_t word); + + /** + It writes sequence data to registers. + @param address Register address + @param pBuf Pointer buffer to write data + @param len Data length + */ + void wizchip_write_buf(uint16_t address, const uint8_t* pBuf, uint16_t len); + + + /** + Reset WIZCHIP by softly. + */ + void wizchip_sw_reset(void); + + /** + It copies data to internal TX memory + + @details This function reads the Tx write pointer register and after that, + it copies the wizdata(pointer buffer) of the length of len(variable) bytes to internal TX memory + and updates the Tx write pointer register. + This function is being called by send() and sendto() function also. + + @param wizdata Pointer buffer to write data + @param len Data length + @sa wizchip_recv_data() + */ + void wizchip_send_data(const uint8_t *wizdata, uint16_t len); + + /** + It copies data to your buffer from internal RX memory + + @details This function read the Rx read pointer register and after that, + it copies the received data from internal RX memory + to wizdata(pointer variable) of the length of len(variable) bytes. + This function is being called by recv() also. + + @param wizdata Pointer buffer to read data + @param len Data length + @sa wizchip_send_data() + */ + void wizchip_recv_data(uint8_t *wizdata, uint16_t len); + + /** + It discard the received data in RX memory. + @details It discards the data of the length of len(variable) bytes in internal RX memory. + @param len Data length + */ + void wizchip_recv_ignore(uint16_t len); + + /** + Get @ref Sn_TX_FSR register + @return uint16_t. Value of @ref Sn_TX_FSR. + */ + uint16_t getSn_TX_FSR(); + + /** + Get @ref Sn_RX_RSR register + @return uint16_t. Value of @ref Sn_RX_RSR. + */ + uint16_t getSn_RX_RSR(); + + + /** Common registers */ + enum + { + MR = 0x0000, ///< Mode Register address (R/W) + GAR = 0x0001, ///< Gateway IP Register address (R/W) + SUBR = 0x0005, ///< Subnet mask Register address (R/W) + SHAR = 0x0009, ///< Source MAC Register address (R/W) + SIPR = 0x000F, ///< Source IP Register address (R/W) + IR = 0x0015, ///< Interrupt Register (R/W) + IMR = 0x0016, ///< Socket Interrupt Mask Register (R/W) + RTR = 0x0017, ///< Timeout register address (1 is 100us) (R/W) + RCR = 0x0019, ///< Retry count register (R/W) + RMSR = 0x001A, ///< Receive Memory Size + TMSR = 0x001B, ///< Transmit Memory Size + }; + + /** Socket registers */ + enum + { + Sn_MR = 0x0400, ///< Socket Mode register(R/W) + Sn_CR = 0x0401, ///< Socket command register (R/W) + Sn_IR = 0x0402, ///< Socket interrupt register (R) + Sn_SR = 0x0403, ///< Socket status register (R) + Sn_PORT = 0x0404, ///< Source port register (R/W) + Sn_DHAR = 0x0406, ///< Peer MAC register address (R/W) + Sn_DIPR = 0x040C, ///< Peer IP register address (R/W) + Sn_DPORT = 0x0410, ///< Peer port register address (R/W) + Sn_MSSR = 0x0412, ///< Maximum Segment Size(Sn_MSSR0) register address (R/W) + Sn_PROTO = 0x0414, ///< IP Protocol(PROTO) Register (R/W) + Sn_TOS = 0x0415, ///< IP Type of Service(TOS) Register (R/W) + Sn_TTL = 0x0416, ///< IP Time to live(TTL) Register (R/W) + Sn_TX_FSR = 0x0420, ///< Transmit free memory size register (R) + Sn_TX_RD = 0x0422, ///< Transmit memory read pointer register address (R) + Sn_TX_WR = 0x0424, ///< Transmit memory write pointer register address (R/W) + Sn_RX_RSR = 0x0426, ///< Received data size register (R) + Sn_RX_RD = 0x0428, ///< Read point of Receive memory (R/W) + Sn_RX_WR = 0x042A, ///< Write point of Receive memory (R) + }; + + /** Mode register values */ + enum + { + MR_RST = 0x80, ///< Reset + MR_PB = 0x10, ///< Ping block + MR_AI = 0x02, ///< Address Auto-Increment in Indirect Bus Interface + MR_IND = 0x01, ///< Indirect Bus Interface mode + }; + + /** Socket Mode Register values @ref Sn_MR */ + enum + { + Sn_MR_CLOSE = 0x00, ///< Unused socket + Sn_MR_TCP = 0x01, ///< TCP + Sn_MR_UDP = 0x02, ///< UDP + Sn_MR_IPRAW = 0x03, ///< IP LAYER RAW SOCK + Sn_MR_MACRAW = 0x04, ///< MAC LAYER RAW SOCK + Sn_MR_ND = 0x20, ///< No Delayed Ack(TCP) flag + Sn_MR_MF = 0x40, ///< Use MAC filter + Sn_MR_MULTI = 0x80, ///< support multicating + }; + + /** Socket Command Register values */ + enum + { + Sn_CR_OPEN = 0x01, ///< Initialise or open socket + Sn_CR_CLOSE = 0x10, ///< Close socket + Sn_CR_SEND = 0x20, ///< Update TX buffer pointer and send data + Sn_CR_SEND_MAC = 0x21, ///< Send data with MAC address, so without ARP process + Sn_CR_SEND_KEEP = 0x22, ///< Send keep alive message + Sn_CR_RECV = 0x40, ///< Update RX buffer pointer and receive data + }; + + /** Socket Interrupt register values */ + enum + { + Sn_IR_CON = 0x01, ///< CON Interrupt + Sn_IR_DISCON = 0x02, ///< DISCON Interrupt + Sn_IR_RECV = 0x04, ///< RECV Interrupt + Sn_IR_TIMEOUT = 0x08, ///< TIMEOUT Interrupt + Sn_IR_SENDOK = 0x10, ///< SEND_OK Interrupt + }; + + /** Socket Status Register values */ + enum + { + SOCK_CLOSED = 0x00, ///< Closed + SOCK_INIT = 0x13, ///< Initiate state + SOCK_LISTEN = 0x14, ///< Listen state + SOCK_SYNSENT = 0x15, ///< Connection state + SOCK_SYNRECV = 0x16, ///< Connection state + SOCK_ESTABLISHED = 0x17, ///< Success to connect + SOCK_FIN_WAIT = 0x18, ///< Closing state + SOCK_CLOSING = 0x1A, ///< Closing state + SOCK_TIME_WAIT = 0x1B, ///< Closing state + SOCK_CLOSE_WAIT = 0x1C, ///< Closing state + SOCK_LAST_ACK = 0x1D, ///< Closing state + SOCK_UDP = 0x22, ///< UDP socket + SOCK_IPRAW = 0x32, ///< IP raw mode socket + SOCK_MACRAW = 0x42, ///< MAC raw mode socket + }; + + /** + Set Mode Register + @param (uint8_t)mr The value to be set. + @sa getMR() + */ + inline void setMR(uint8_t mode) + { + wizchip_write(MR, mode); + } + + /** + Get Mode Register + @return uint8_t. The value of Mode register. + @sa setMR() + */ + inline uint8_t getMR() + { + return wizchip_read(MR); + } + + /** + Set local MAC address + @param (uint8_t*)shar Pointer variable to set local MAC address. It should be allocated 6 bytes. + @sa getSHAR() + */ + inline void setSHAR(const uint8_t* macaddr) + { + wizchip_write_buf(SHAR, macaddr, 6); + } + + /** + Get local MAC address + @param (uint8_t*)shar Pointer variable to get local MAC address. It should be allocated 6 bytes. + @sa setSHAR() + */ + inline void getSHAR(uint8_t* macaddr) + { + wizchip_read_buf(SHAR, macaddr, 6); + } + + /** + Get @ref Sn_TX_WR register + @param (uint16_t)txwr Value to set @ref Sn_TX_WR + @sa GetSn_TX_WR() + */ + inline uint16_t getSn_TX_WR() + { + return wizchip_read_word(Sn_TX_WR); + } + + /** + Set @ref Sn_TX_WR register + @param (uint16_t)txwr Value to set @ref Sn_TX_WR + @sa GetSn_TX_WR() + */ + inline void setSn_TX_WR(uint16_t txwr) + { + wizchip_write_word(Sn_TX_WR, txwr); + } + + /** + Get @ref Sn_RX_RD register + @regurn uint16_t. Value of @ref Sn_RX_RD. + @sa setSn_RX_RD() + */ + inline uint16_t getSn_RX_RD() + { + return wizchip_read_word(Sn_RX_RD); + } + + /** + Set @ref Sn_RX_RD register + @param (uint16_t)rxrd Value to set @ref Sn_RX_RD + @sa getSn_RX_RD() + */ + inline void setSn_RX_RD(uint16_t rxrd) + { + wizchip_write_word(Sn_RX_RD, rxrd); + } + + /** + Set @ref Sn_MR register + @param (uint8_t)mr Value to set @ref Sn_MR + @sa getSn_MR() + */ + inline void setSn_MR(uint8_t mr) + { + wizchip_write(Sn_MR, mr); + } + + /** + Get @ref Sn_MR register + @return uint8_t. Value of @ref Sn_MR. + @sa setSn_MR() + */ + inline uint8_t getSn_MR() + { + return wizchip_read(Sn_MR); + } + + /** + Set @ref Sn_CR register, then wait for the command to execute + @param (uint8_t)cr Value to set @ref Sn_CR + @sa getSn_CR() + */ + void setSn_CR(uint8_t cr); + + /** + Get @ref Sn_CR register + @return uint8_t. Value of @ref Sn_CR. + @sa setSn_CR() + */ + inline uint8_t getSn_CR() + { + return wizchip_read(Sn_CR); + } + + /** + Get @ref Sn_SR register + @return uint8_t. Value of @ref Sn_SR. + */ + inline uint8_t getSn_SR() + { + return wizchip_read(Sn_SR); + } + + /** + Get @ref Sn_IR register + @return uint8_t. Value of @ref Sn_IR. + @sa setSn_IR() + */ + inline uint8_t getSn_IR() + { + return wizchip_read(Sn_IR); + } + + /** + Set @ref Sn_IR register + @param (uint8_t)ir Value to set @ref Sn_IR + @sa getSn_IR() + */ + inline void setSn_IR(uint8_t ir) + { + wizchip_write(Sn_IR, ir); + } +}; + +#endif // W5100_H diff --git a/libraries/lwIP_w5500/examples/TCPClient/TCPClient.ino b/libraries/lwIP_w5500/examples/TCPClient/TCPClient.ino new file mode 100644 index 0000000000..56dcb8092b --- /dev/null +++ b/libraries/lwIP_w5500/examples/TCPClient/TCPClient.ino @@ -0,0 +1,102 @@ +/* + This sketch establishes a TCP connection to a "quote of the day" service. + It sends a "hello" message, and then prints received data. +*/ + +#include +#include +//or #include +//or #include + +#include // WiFiClient (-> TCPClient) +#include // ESP8266WiFiClass::preinitWiFiOff() + +const char* host = "djxmmx.net"; +const uint16_t port = 17; + +using TCPClient = WiFiClient; + +#define CSPIN 16 // wemos/lolin/nodemcu D0 +Wiznet5500lwIP eth(CSPIN); + +void preinit() { + // (no C++ in function) + // disable wifi + ESP8266WiFiClass::preinitWiFiOff(); +} + +void setup() { + Serial.begin(115200); + + SPI.begin(); + SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz? + SPI.setBitOrder(MSBFIRST); + SPI.setDataMode(SPI_MODE0); + eth.setDefault(); // use ethernet for default route + if (!eth.begin()) { + Serial.println("ethernet hardware not found ... sleeping"); + while (1) { + delay(1000); + } + } else { + Serial.print("connecting ethernet"); + while (!eth.connected()) { + Serial.print("."); + delay(1000); + } + } + Serial.println(); + Serial.print("ethernet IP address: "); + Serial.println(eth.localIP()); +} + +void loop() { + static bool wait = false; + + Serial.print("connecting to "); + Serial.print(host); + Serial.print(':'); + Serial.println(port); + + TCPClient client; + if (!client.connect(host, port)) { + Serial.println("connection failed"); + delay(5000); + return; + } + + // This will send a string to the server + Serial.println("sending data to server"); + if (client.connected()) { + client.println("hello from ESP8266"); + } + + // wait for data to be available + unsigned long timeout = millis(); + while (client.available() == 0) { + if (millis() - timeout > 5000) { + Serial.println(">>> Client Timeout !"); + client.stop(); + delay(60000); + return; + } + } + + // Read all the lines of the reply from server and print them to Serial + Serial.println("receiving from remote server"); + // not testing 'client.connected()' since we do not need to send data here + while (client.available()) { + char ch = static_cast(client.read()); + Serial.print(ch); + } + + // Close the connection + Serial.println(); + Serial.println("closing connection"); + client.stop(); + + if (wait) { + delay(300000); // execute once every 5 minutes, don't flood remote service + } + wait = true; +} diff --git a/libraries/lwIP_w5500/library.properties b/libraries/lwIP_w5500/library.properties new file mode 100644 index 0000000000..8cb76d861d --- /dev/null +++ b/libraries/lwIP_w5500/library.properties @@ -0,0 +1,10 @@ +name=lwIP_w5500 +version=1 +author=Nicholas Humfrey +maintainer=esp8266/Arduino +sentence=Ethernet driver +paragraph=Wiznet5500 ethernet drivers for lwIP and esp8266 Arduino from https://github.com/njh/W5500MacRaw +category=Network +url=https://github.com/esp8266/Arduino +architectures=esp8266 +dot_a_linkage=true diff --git a/libraries/lwIP_w5500/src/W5500lwIP.h b/libraries/lwIP_w5500/src/W5500lwIP.h new file mode 100644 index 0000000000..1e8f201331 --- /dev/null +++ b/libraries/lwIP_w5500/src/W5500lwIP.h @@ -0,0 +1,10 @@ + +#ifndef _W5500LWIP_H +#define _W5500LWIP_H + +#include +#include + +using Wiznet5500lwIP = LwipIntfDev; + +#endif // _W5500LWIP_H diff --git a/libraries/lwIP_w5500/src/utility/w5500.cpp b/libraries/lwIP_w5500/src/utility/w5500.cpp new file mode 100644 index 0000000000..b3c3ce0162 --- /dev/null +++ b/libraries/lwIP_w5500/src/utility/w5500.cpp @@ -0,0 +1,442 @@ +/* + Copyright (c) 2013, WIZnet Co., Ltd. + Copyright (c) 2016, Nicholas Humfrey + 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. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``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 + COPYRIGHT HOLDER OR CONTRIBUTORS 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. + +*/ + +// original sources: https://github.com/njh/W5500MacRaw + +#include +#include "w5500.h" + + +uint8_t Wiznet5500::wizchip_read(uint8_t block, uint16_t address) +{ + uint8_t ret; + + wizchip_cs_select(); + + block |= AccessModeRead; + + wizchip_spi_write_byte((address & 0xFF00) >> 8); + wizchip_spi_write_byte((address & 0x00FF) >> 0); + wizchip_spi_write_byte(block); + + ret = wizchip_spi_read_byte(); + + wizchip_cs_deselect(); + return ret; +} + +uint16_t Wiznet5500::wizchip_read_word(uint8_t block, uint16_t address) +{ + return ((uint16_t)wizchip_read(block, address) << 8) + wizchip_read(block, address + 1); +} + +void Wiznet5500::wizchip_read_buf(uint8_t block, uint16_t address, uint8_t* pBuf, uint16_t len) +{ + uint16_t i; + + wizchip_cs_select(); + + block |= AccessModeRead; + + wizchip_spi_write_byte((address & 0xFF00) >> 8); + wizchip_spi_write_byte((address & 0x00FF) >> 0); + wizchip_spi_write_byte(block); + for (i = 0; i < len; i++) + { + pBuf[i] = wizchip_spi_read_byte(); + } + + wizchip_cs_deselect(); +} + +void Wiznet5500::wizchip_write(uint8_t block, uint16_t address, uint8_t wb) +{ + wizchip_cs_select(); + + block |= AccessModeWrite; + + wizchip_spi_write_byte((address & 0xFF00) >> 8); + wizchip_spi_write_byte((address & 0x00FF) >> 0); + wizchip_spi_write_byte(block); + wizchip_spi_write_byte(wb); + + wizchip_cs_deselect(); +} + +void Wiznet5500::wizchip_write_word(uint8_t block, uint16_t address, uint16_t word) +{ + wizchip_write(block, address, (uint8_t)(word >> 8)); + wizchip_write(block, address + 1, (uint8_t) word); +} + +void Wiznet5500::wizchip_write_buf(uint8_t block, uint16_t address, const uint8_t* pBuf, uint16_t len) +{ + uint16_t i; + + wizchip_cs_select(); + + block |= AccessModeWrite; + + wizchip_spi_write_byte((address & 0xFF00) >> 8); + wizchip_spi_write_byte((address & 0x00FF) >> 0); + wizchip_spi_write_byte(block); + for (i = 0; i < len; i++) + { + wizchip_spi_write_byte(pBuf[i]); + } + + wizchip_cs_deselect(); +} + +void Wiznet5500::setSn_CR(uint8_t cr) +{ + // Write the command to the Command Register + wizchip_write(BlockSelectSReg, Sn_CR, cr); + + // Now wait for the command to complete + while (wizchip_read(BlockSelectSReg, Sn_CR)); +} + +uint16_t Wiznet5500::getSn_TX_FSR() +{ + uint16_t val = 0, val1 = 0; + do + { + val1 = wizchip_read_word(BlockSelectSReg, Sn_TX_FSR); + if (val1 != 0) + { + val = wizchip_read_word(BlockSelectSReg, Sn_TX_FSR); + } + } while (val != val1); + return val; +} + + +uint16_t Wiznet5500::getSn_RX_RSR() +{ + uint16_t val = 0, val1 = 0; + do + { + val1 = wizchip_read_word(BlockSelectSReg, Sn_RX_RSR); + if (val1 != 0) + { + val = wizchip_read_word(BlockSelectSReg, Sn_RX_RSR); + } + } while (val != val1); + return val; +} + +void Wiznet5500::wizchip_send_data(const uint8_t *wizdata, uint16_t len) +{ + uint16_t ptr = 0; + + if (len == 0) + { + return; + } + ptr = getSn_TX_WR(); + wizchip_write_buf(BlockSelectTxBuf, ptr, wizdata, len); + + ptr += len; + + setSn_TX_WR(ptr); +} + +void Wiznet5500::wizchip_recv_data(uint8_t *wizdata, uint16_t len) +{ + uint16_t ptr; + + if (len == 0) + { + return; + } + ptr = getSn_RX_RD(); + wizchip_read_buf(BlockSelectRxBuf, ptr, wizdata, len); + ptr += len; + + setSn_RX_RD(ptr); +} + +void Wiznet5500::wizchip_recv_ignore(uint16_t len) +{ + uint16_t ptr; + + ptr = getSn_RX_RD(); + ptr += len; + setSn_RX_RD(ptr); +} + +void Wiznet5500::wizchip_sw_reset() +{ + setMR(MR_RST); + getMR(); // for delay + + setSHAR(_mac_address); +} + +int8_t Wiznet5500::wizphy_getphylink() +{ + int8_t tmp; + if (getPHYCFGR() & PHYCFGR_LNK_ON) + { + tmp = PHY_LINK_ON; + } + else + { + tmp = PHY_LINK_OFF; + } + return tmp; +} + +int8_t Wiznet5500::wizphy_getphypmode() +{ + int8_t tmp = 0; + if (getPHYCFGR() & PHYCFGR_OPMDC_PDOWN) + { + tmp = PHY_POWER_DOWN; + } + else + { + tmp = PHY_POWER_NORM; + } + return tmp; +} + +void Wiznet5500::wizphy_reset() +{ + uint8_t tmp = getPHYCFGR(); + tmp &= PHYCFGR_RST; + setPHYCFGR(tmp); + tmp = getPHYCFGR(); + tmp |= ~PHYCFGR_RST; + setPHYCFGR(tmp); +} + +int8_t Wiznet5500::wizphy_setphypmode(uint8_t pmode) +{ + uint8_t tmp = 0; + tmp = getPHYCFGR(); + if ((tmp & PHYCFGR_OPMD) == 0) + { + return -1; + } + tmp &= ~PHYCFGR_OPMDC_ALLA; + if (pmode == PHY_POWER_DOWN) + { + tmp |= PHYCFGR_OPMDC_PDOWN; + } + else + { + tmp |= PHYCFGR_OPMDC_ALLA; + } + setPHYCFGR(tmp); + wizphy_reset(); + tmp = getPHYCFGR(); + if (pmode == PHY_POWER_DOWN) + { + if (tmp & PHYCFGR_OPMDC_PDOWN) + { + return 0; + } + } + else + { + if (tmp & PHYCFGR_OPMDC_ALLA) + { + return 0; + } + } + return -1; +} + + +Wiznet5500::Wiznet5500(int8_t cs, SPIClass& spi, int8_t intr): + _spi(spi), _cs(cs) +{ + (void)intr; +} + +boolean Wiznet5500::begin(const uint8_t *mac_address) +{ + memcpy(_mac_address, mac_address, 6); + + pinMode(_cs, OUTPUT); + wizchip_cs_deselect(); + +#if 0 + _spi.begin(); + _spi.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz? + _spi.setBitOrder(MSBFIRST); + _spi.setDataMode(SPI_MODE0); +#endif + + wizchip_sw_reset(); + + // Use the full 16Kb of RAM for Socket 0 + setSn_RXBUF_SIZE(16); + setSn_TXBUF_SIZE(16); + + // Set our local MAC address + setSHAR(_mac_address); + + // Open Socket 0 in MACRaw mode + setSn_MR(Sn_MR_MACRAW); + setSn_CR(Sn_CR_OPEN); + if (getSn_SR() != SOCK_MACRAW) + { + // Failed to put socket 0 into MACRaw mode + return false; + } + + // Success + return true; +} + +void Wiznet5500::end() +{ + setSn_CR(Sn_CR_CLOSE); + + // clear all interrupt of the socket + setSn_IR(0xFF); + + // Wait for socket to change to closed + while (getSn_SR() != SOCK_CLOSED); +} + +uint16_t Wiznet5500::readFrame(uint8_t *buffer, uint16_t bufsize) +{ + uint16_t data_len = readFrameSize(); + + if (data_len == 0) + { + return 0; + } + + if (data_len > bufsize) + { + // Packet is bigger than buffer - drop the packet + discardFrame(data_len); + return 0; + } + + return readFrameData(buffer, data_len); +} + +uint16_t Wiznet5500::readFrameSize() +{ + uint16_t len = getSn_RX_RSR(); + + if (len == 0) + { + return 0; + } + + uint8_t head[2]; + uint16_t data_len = 0; + + wizchip_recv_data(head, 2); + setSn_CR(Sn_CR_RECV); + + data_len = head[0]; + data_len = (data_len << 8) + head[1]; + data_len -= 2; + + return data_len; +} + +void Wiznet5500::discardFrame(uint16_t framesize) +{ + wizchip_recv_ignore(framesize); + setSn_CR(Sn_CR_RECV); +} + +uint16_t Wiznet5500::readFrameData(uint8_t *buffer, uint16_t framesize) +{ + wizchip_recv_data(buffer, framesize); + setSn_CR(Sn_CR_RECV); + +#if 1 + // let lwIP deal with mac address filtering + return framesize; +#else + // Had problems with W5500 MAC address filtering (the Sn_MR_MFEN option) + // Do it in software instead: + if ((buffer[0] & 0x01) || memcmp(&buffer[0], _mac_address, 6) == 0) + { + // Addressed to an Ethernet multicast address or our unicast address + return framesize; + } + else + { + return 0; + } +#endif +} + +uint16_t Wiznet5500::sendFrame(const uint8_t *buf, uint16_t len) +{ + // Wait for space in the transmit buffer + while (1) + { + uint16_t freesize = getSn_TX_FSR(); + if (getSn_SR() == SOCK_CLOSED) + { + return -1; + } + if (len <= freesize) + { + break; + } + }; + + wizchip_send_data(buf, len); + setSn_CR(Sn_CR_SEND); + + while (1) + { + uint8_t tmp = getSn_IR(); + if (tmp & Sn_IR_SENDOK) + { + setSn_IR(Sn_IR_SENDOK); + // Packet sent ok + break; + } + else if (tmp & Sn_IR_TIMEOUT) + { + setSn_IR(Sn_IR_TIMEOUT); + // There was a timeout + return -1; + } + } + + return len; +} diff --git a/libraries/lwIP_w5500/src/utility/w5500.h b/libraries/lwIP_w5500/src/utility/w5500.h new file mode 100644 index 0000000000..428689b8e5 --- /dev/null +++ b/libraries/lwIP_w5500/src/utility/w5500.h @@ -0,0 +1,767 @@ +/* + Copyright (c) 2013, WIZnet Co., Ltd. + Copyright (c) 2016, Nicholas Humfrey + 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. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``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 + COPYRIGHT HOLDER OR CONTRIBUTORS 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. + +*/ + +// original sources: https://github.com/njh/W5500MacRaw + +#ifndef W5500_H +#define W5500_H + +#include +#include +#include + + + +class Wiznet5500 +{ + +public: + /** + Constructor that uses the default hardware SPI pins + @param cs the Arduino Chip Select / Slave Select pin (default 10) + */ + Wiznet5500(int8_t cs = SS, SPIClass& spi = SPI, int8_t intr = -1); + + + /** + Initialise the Ethernet controller + Must be called before sending or receiving Ethernet frames + + @param address the local MAC address for the Ethernet interface + @return Returns true if setting up the Ethernet interface was successful + */ + boolean begin(const uint8_t *address); + + /** + Shut down the Ethernet controlled + */ + void end(); + + /** + Send an Ethernet frame + @param data a pointer to the data to send + @param datalen the length of the data in the packet + @return the number of bytes transmitted + */ + uint16_t sendFrame(const uint8_t *data, uint16_t datalen); + + /** + Read an Ethernet frame + @param buffer a pointer to a buffer to write the packet to + @param bufsize the available space in the buffer + @return the length of the received packet + or 0 if no packet was received + */ + uint16_t readFrame(uint8_t *buffer, uint16_t bufsize); + +protected: + + static constexpr bool interruptIsPossible() + { + return false; + } + + /** + Read an Ethernet frame size + @return the length of data do receive + or 0 if no frame was received + */ + uint16_t readFrameSize(); + + /** + discard an Ethernet frame + @param framesize readFrameSize()'s result + */ + void discardFrame(uint16_t framesize); + + /** + Read an Ethernet frame data + readFrameSize() must be called first, + its result must be passed into framesize parameter + @param buffer a pointer to a buffer to write the frame to + @param framesize readFrameSize()'s result + @return the length of the received frame + or 0 if a problem occured + */ + uint16_t readFrameData(uint8_t *frame, uint16_t framesize); + + +private: + + //< SPI interface Read operation in Control Phase + static const uint8_t AccessModeRead = (0x00 << 2); + + //< SPI interface Read operation in Control Phase + static const uint8_t AccessModeWrite = (0x01 << 2); + + //< Common register block in Control Phase + static const uint8_t BlockSelectCReg = (0x00 << 3); + + //< Socket 0 register block in Control Phase + static const uint8_t BlockSelectSReg = (0x01 << 3); + + //< Socket 0 Tx buffer address block + static const uint8_t BlockSelectTxBuf = (0x02 << 3); + + //< Socket 0 Rx buffer address block + static const uint8_t BlockSelectRxBuf = (0x03 << 3); + + + + SPIClass& _spi; + int8_t _cs; + uint8_t _mac_address[6]; + + /** + Default function to select chip. + @note This function help not to access wrong address. If you do not describe this function or register any functions, + null function is called. + */ + inline void wizchip_cs_select() + { + digitalWrite(_cs, LOW); + } + + /** + Default function to deselect chip. + @note This function help not to access wrong address. If you do not describe this function or register any functions, + null function is called. + */ + inline void wizchip_cs_deselect() + { + digitalWrite(_cs, HIGH); + } + + /** + Default function to read in SPI interface. + @note This function help not to access wrong address. If you do not describe this function or register any functions, + null function is called. + */ + inline uint8_t wizchip_spi_read_byte() + { + return _spi.transfer(0); + } + + /** + Default function to write in SPI interface. + @note This function help not to access wrong address. If you do not describe this function or register any functions, + null function is called. + */ + inline void wizchip_spi_write_byte(uint8_t wb) + { + _spi.transfer(wb); + } + + + /** + Read a 1 byte value from a register. + @param address Register address + @return The value of register + */ + uint8_t wizchip_read(uint8_t block, uint16_t address); + + /** + Reads a 2 byte value from a register. + @param address Register address + @return The value of register + */ + uint16_t wizchip_read_word(uint8_t block, uint16_t address); + + /** + It reads sequence data from registers. + @param address Register address + @param pBuf Pointer buffer to read data + @param len Data length + */ + void wizchip_read_buf(uint8_t block, uint16_t address, uint8_t* pBuf, uint16_t len); + + /** + Write a 1 byte value to a register. + @param address Register address + @param wb Write data + @return void + */ + void wizchip_write(uint8_t block, uint16_t address, uint8_t wb); + + /** + Write a 2 byte value to a register. + @param address Register address + @param wb Write data + @return void + */ + void wizchip_write_word(uint8_t block, uint16_t address, uint16_t word); + + /** + It writes sequence data to registers. + @param address Register address + @param pBuf Pointer buffer to write data + @param len Data length + */ + void wizchip_write_buf(uint8_t block, uint16_t address, const uint8_t* pBuf, uint16_t len); + + /** + Get @ref Sn_TX_FSR register + @return uint16_t. Value of @ref Sn_TX_FSR. + */ + uint16_t getSn_TX_FSR(); + + /** + Get @ref Sn_RX_RSR register + @return uint16_t. Value of @ref Sn_RX_RSR. + */ + uint16_t getSn_RX_RSR(); + + + /** + Reset WIZCHIP by softly. + */ + void wizchip_sw_reset(); + + /** + Get the link status of phy in WIZCHIP + */ + int8_t wizphy_getphylink(); + + /** + Get the power mode of PHY in WIZCHIP + */ + int8_t wizphy_getphypmode(); + + /** + Reset Phy + */ + void wizphy_reset(); + + /** + set the power mode of phy inside WIZCHIP. Refer to @ref PHYCFGR in W5500, @ref PHYSTATUS in W5200 + @param pmode Settig value of power down mode. + */ + int8_t wizphy_setphypmode(uint8_t pmode); + + /** + It copies data to internal TX memory + + @details This function reads the Tx write pointer register and after that, + it copies the wizdata(pointer buffer) of the length of len(variable) bytes to internal TX memory + and updates the Tx write pointer register. + This function is being called by send() and sendto() function also. + + @param wizdata Pointer buffer to write data + @param len Data length + @sa wizchip_recv_data() + */ + void wizchip_send_data(const uint8_t *wizdata, uint16_t len); + + /** + It copies data to your buffer from internal RX memory + + @details This function read the Rx read pointer register and after that, + it copies the received data from internal RX memory + to wizdata(pointer variable) of the length of len(variable) bytes. + This function is being called by recv() also. + + @param wizdata Pointer buffer to read data + @param len Data length + @sa wizchip_send_data() + */ + void wizchip_recv_data(uint8_t *wizdata, uint16_t len); + + /** + It discard the received data in RX memory. + @details It discards the data of the length of len(variable) bytes in internal RX memory. + @param len Data length + */ + void wizchip_recv_ignore(uint16_t len); + + + + /** Common registers */ + enum + { + MR = 0x0000, ///< Mode Register address (R/W) + SHAR = 0x0009, ///< Source MAC Register address (R/W) + INTLEVEL = 0x0013, ///< Set Interrupt low level timer register address (R/W) + IR = 0x0015, ///< Interrupt Register (R/W) + _IMR_ = 0x0016, ///< Interrupt mask register (R/W) + SIR = 0x0017, ///< Socket Interrupt Register (R/W) + SIMR = 0x0018, ///< Socket Interrupt Mask Register (R/W) + _RTR_ = 0x0019, ///< Timeout register address (1 is 100us) (R/W) + _RCR_ = 0x001B, ///< Retry count register (R/W) + UIPR = 0x0028, ///< Unreachable IP register address in UDP mode (R) + UPORTR = 0x002C, ///< Unreachable Port register address in UDP mode (R) + PHYCFGR = 0x002E, ///< PHY Status Register (R/W) + VERSIONR = 0x0039, ///< Chip version register address (R) + }; + + /** Socket registers */ + enum + { + Sn_MR = 0x0000, ///< Socket Mode register (R/W) + Sn_CR = 0x0001, ///< Socket command register (R/W) + Sn_IR = 0x0002, ///< Socket interrupt register (R) + Sn_SR = 0x0003, ///< Socket status register (R) + Sn_PORT = 0x0004, ///< Source port register (R/W) + Sn_DHAR = 0x0006, ///< Peer MAC register address (R/W) + Sn_DIPR = 0x000C, ///< Peer IP register address (R/W) + Sn_DPORT = 0x0010, ///< Peer port register address (R/W) + Sn_MSSR = 0x0012, ///< Maximum Segment Size(Sn_MSSR0) register address (R/W) + Sn_TOS = 0x0015, ///< IP Type of Service(TOS) Register (R/W) + Sn_TTL = 0x0016, ///< IP Time to live(TTL) Register (R/W) + Sn_RXBUF_SIZE = 0x001E, ///< Receive memory size register (R/W) + Sn_TXBUF_SIZE = 0x001F, ///< Transmit memory size register (R/W) + Sn_TX_FSR = 0x0020, ///< Transmit free memory size register (R) + Sn_TX_RD = 0x0022, ///< Transmit memory read pointer register address (R) + Sn_TX_WR = 0x0024, ///< Transmit memory write pointer register address (R/W) + Sn_RX_RSR = 0x0026, ///< Received data size register (R) + Sn_RX_RD = 0x0028, ///< Read point of Receive memory (R/W) + Sn_RX_WR = 0x002A, ///< Write point of Receive memory (R) + Sn_IMR = 0x002C, ///< Socket interrupt mask register (R) + Sn_FRAG = 0x002D, ///< Fragment field value in IP header register (R/W) + Sn_KPALVTR = 0x002F, ///< Keep Alive Timer register (R/W) + }; + + /** Mode register values */ + enum + { + MR_RST = 0x80, ///< Reset + MR_WOL = 0x20, ///< Wake on LAN + MR_PB = 0x10, ///< Ping block + MR_PPPOE = 0x08, ///< Enable PPPoE + MR_FARP = 0x02, ///< Enable UDP_FORCE_ARP CHECHK + }; + + /* Interrupt Register values */ + enum + { + IR_CONFLICT = 0x80, ///< Check IP conflict + IR_UNREACH = 0x40, ///< Get the destination unreachable message in UDP sending + IR_PPPoE = 0x20, ///< Get the PPPoE close message + IR_MP = 0x10, ///< Get the magic packet interrupt + }; + + /* Interrupt Mask Register values */ + enum + { + IM_IR7 = 0x80, ///< IP Conflict Interrupt Mask + IM_IR6 = 0x40, ///< Destination unreachable Interrupt Mask + IM_IR5 = 0x20, ///< PPPoE Close Interrupt Mask + IM_IR4 = 0x10, ///< Magic Packet Interrupt Mask + }; + + /** Socket Mode Register values @ref Sn_MR */ + enum + { + Sn_MR_CLOSE = 0x00, ///< Unused socket + Sn_MR_TCP = 0x01, ///< TCP + Sn_MR_UDP = 0x02, ///< UDP + Sn_MR_MACRAW = 0x04, ///< MAC LAYER RAW SOCK + Sn_MR_UCASTB = 0x10, ///< Unicast Block in UDP Multicasting + Sn_MR_ND = 0x20, ///< No Delayed Ack(TCP), Multicast flag + Sn_MR_BCASTB = 0x40, ///< Broadcast block in UDP Multicasting + Sn_MR_MULTI = 0x80, ///< Support UDP Multicasting + Sn_MR_MIP6B = 0x10, ///< IPv6 packet Blocking in @ref Sn_MR_MACRAW mode + Sn_MR_MMB = 0x20, ///< Multicast Blocking in @ref Sn_MR_MACRAW mode + Sn_MR_MFEN = 0x80, ///< MAC filter enable in @ref Sn_MR_MACRAW mode + }; + + /** Socket Command Register values */ + enum + { + Sn_CR_OPEN = 0x01, ///< Initialise or open socket + Sn_CR_LISTEN = 0x02, ///< Wait connection request in TCP mode (Server mode) + Sn_CR_CONNECT = 0x04, ///< Send connection request in TCP mode (Client mode) + Sn_CR_DISCON = 0x08, ///< Send closing request in TCP mode + Sn_CR_CLOSE = 0x10, ///< Close socket + Sn_CR_SEND = 0x20, ///< Update TX buffer pointer and send data + Sn_CR_SEND_MAC = 0x21, ///< Send data with MAC address, so without ARP process + Sn_CR_SEND_KEEP = 0x22, ///< Send keep alive message + Sn_CR_RECV = 0x40, ///< Update RX buffer pointer and receive data + }; + + /** Socket Interrupt register values */ + enum + { + Sn_IR_CON = 0x01, ///< CON Interrupt + Sn_IR_DISCON = 0x02, ///< DISCON Interrupt + Sn_IR_RECV = 0x04, ///< RECV Interrupt + Sn_IR_TIMEOUT = 0x08, ///< TIMEOUT Interrupt + Sn_IR_SENDOK = 0x10, ///< SEND_OK Interrupt + }; + + /** Socket Status Register values */ + enum + { + SOCK_CLOSED = 0x00, ///< Closed + SOCK_INIT = 0x13, ///< Initiate state + SOCK_LISTEN = 0x14, ///< Listen state + SOCK_SYNSENT = 0x15, ///< Connection state + SOCK_SYNRECV = 0x16, ///< Connection state + SOCK_ESTABLISHED = 0x17, ///< Success to connect + SOCK_FIN_WAIT = 0x18, ///< Closing state + SOCK_CLOSING = 0x1A, ///< Closing state + SOCK_TIME_WAIT = 0x1B, ///< Closing state + SOCK_CLOSE_WAIT = 0x1C, ///< Closing state + SOCK_LAST_ACK = 0x1D, ///< Closing state + SOCK_UDP = 0x22, ///< UDP socket + SOCK_MACRAW = 0x42, ///< MAC raw mode socket + }; + + + /* PHYCFGR register value */ + enum + { + PHYCFGR_RST = ~(1 << 7), //< For PHY reset, must operate AND mask. + PHYCFGR_OPMD = (1 << 6), // Configre PHY with OPMDC value + PHYCFGR_OPMDC_ALLA = (7 << 3), + PHYCFGR_OPMDC_PDOWN = (6 << 3), + PHYCFGR_OPMDC_NA = (5 << 3), + PHYCFGR_OPMDC_100FA = (4 << 3), + PHYCFGR_OPMDC_100F = (3 << 3), + PHYCFGR_OPMDC_100H = (2 << 3), + PHYCFGR_OPMDC_10F = (1 << 3), + PHYCFGR_OPMDC_10H = (0 << 3), + PHYCFGR_DPX_FULL = (1 << 2), + PHYCFGR_DPX_HALF = (0 << 2), + PHYCFGR_SPD_100 = (1 << 1), + PHYCFGR_SPD_10 = (0 << 1), + PHYCFGR_LNK_ON = (1 << 0), + PHYCFGR_LNK_OFF = (0 << 0), + }; + + enum + { + PHY_SPEED_10 = 0, ///< Link Speed 10 + PHY_SPEED_100 = 1, ///< Link Speed 100 + PHY_DUPLEX_HALF = 0, ///< Link Half-Duplex + PHY_DUPLEX_FULL = 1, ///< Link Full-Duplex + PHY_LINK_OFF = 0, ///< Link Off + PHY_LINK_ON = 1, ///< Link On + PHY_POWER_NORM = 0, ///< PHY power normal mode + PHY_POWER_DOWN = 1, ///< PHY power down mode + }; + + + /** + Set Mode Register + @param (uint8_t)mr The value to be set. + @sa getMR() + */ + inline void setMR(uint8_t mode) + { + wizchip_write(BlockSelectCReg, MR, mode); + } + + /** + Get Mode Register + @return uint8_t. The value of Mode register. + @sa setMR() + */ + inline uint8_t getMR() + { + return wizchip_read(BlockSelectCReg, MR); + } + + /** + Set local MAC address + @param (uint8_t*)shar Pointer variable to set local MAC address. It should be allocated 6 bytes. + @sa getSHAR() + */ + inline void setSHAR(const uint8_t* macaddr) + { + wizchip_write_buf(BlockSelectCReg, SHAR, macaddr, 6); + } + + /** + Get local MAC address + @param (uint8_t*)shar Pointer variable to get local MAC address. It should be allocated 6 bytes. + @sa setSHAR() + */ + inline void getSHAR(uint8_t* macaddr) + { + wizchip_read_buf(BlockSelectCReg, SHAR, macaddr, 6); + } + + /** + Set @ref IR register + @param (uint8_t)ir Value to set @ref IR register. + @sa getIR() + */ + inline void setIR(uint8_t ir) + { + wizchip_write(BlockSelectCReg, IR, (ir & 0xF0)); + } + + /** + Get @ref IR register + @return uint8_t. Value of @ref IR register. + @sa setIR() + */ + inline uint8_t getIR() + { + return wizchip_read(BlockSelectCReg, IR) & 0xF0; + } + + /** + Set @ref _IMR_ register + @param (uint8_t)imr Value to set @ref _IMR_ register. + @sa getIMR() + */ + inline void setIMR(uint8_t imr) + { + wizchip_write(BlockSelectCReg, _IMR_, imr); + } + + /** + Get @ref _IMR_ register + @return uint8_t. Value of @ref _IMR_ register. + @sa setIMR() + */ + inline uint8_t getIMR() + { + return wizchip_read(BlockSelectCReg, _IMR_); + } + + /** + Set @ref PHYCFGR register + @param (uint8_t)phycfgr Value to set @ref PHYCFGR register. + @sa getPHYCFGR() + */ + inline void setPHYCFGR(uint8_t phycfgr) + { + wizchip_write(BlockSelectCReg, PHYCFGR, phycfgr); + } + + /** + Get @ref PHYCFGR register + @return uint8_t. Value of @ref PHYCFGR register. + @sa setPHYCFGR() + */ + inline uint8_t getPHYCFGR() + { + return wizchip_read(BlockSelectCReg, PHYCFGR); + } + + /** + Get @ref VERSIONR register + @return uint8_t. Value of @ref VERSIONR register. + */ + inline uint8_t getVERSIONR() + { + return wizchip_read(BlockSelectCReg, VERSIONR); + } + + /** + Set @ref Sn_MR register + @param (uint8_t)mr Value to set @ref Sn_MR + @sa getSn_MR() + */ + inline void setSn_MR(uint8_t mr) + { + wizchip_write(BlockSelectSReg, Sn_MR, mr); + } + + /** + Get @ref Sn_MR register + @return uint8_t. Value of @ref Sn_MR. + @sa setSn_MR() + */ + inline uint8_t getSn_MR() + { + return wizchip_read(BlockSelectSReg, Sn_MR); + } + + /** + Set @ref Sn_CR register, then wait for the command to execute + @param (uint8_t)cr Value to set @ref Sn_CR + @sa getSn_CR() + */ + void setSn_CR(uint8_t cr); + + /** + Get @ref Sn_CR register + @return uint8_t. Value of @ref Sn_CR. + @sa setSn_CR() + */ + inline uint8_t getSn_CR() + { + return wizchip_read(BlockSelectSReg, Sn_CR); + } + + /** + Set @ref Sn_IR register + @param (uint8_t)ir Value to set @ref Sn_IR + @sa getSn_IR() + */ + inline void setSn_IR(uint8_t ir) + { + wizchip_write(BlockSelectSReg, Sn_IR, (ir & 0x1F)); + } + + /** + Get @ref Sn_IR register + @return uint8_t. Value of @ref Sn_IR. + @sa setSn_IR() + */ + inline uint8_t getSn_IR() + { + return (wizchip_read(BlockSelectSReg, Sn_IR) & 0x1F); + } + + /** + Set @ref Sn_IMR register + @param (uint8_t)imr Value to set @ref Sn_IMR + @sa getSn_IMR() + */ + inline void setSn_IMR(uint8_t imr) + { + wizchip_write(BlockSelectSReg, Sn_IMR, (imr & 0x1F)); + } + + /** + Get @ref Sn_IMR register + @return uint8_t. Value of @ref Sn_IMR. + @sa setSn_IMR() + */ + inline uint8_t getSn_IMR() + { + return (wizchip_read(BlockSelectSReg, Sn_IMR) & 0x1F); + } + + /** + Get @ref Sn_SR register + @return uint8_t. Value of @ref Sn_SR. + */ + inline uint8_t getSn_SR() + { + return wizchip_read(BlockSelectSReg, Sn_SR); + } + + /** + Set @ref Sn_RXBUF_SIZE register + @param (uint8_t)rxbufsize Value to set @ref Sn_RXBUF_SIZE + @sa getSn_RXBUF_SIZE() + */ + inline void setSn_RXBUF_SIZE(uint8_t rxbufsize) + { + wizchip_write(BlockSelectSReg, Sn_RXBUF_SIZE, rxbufsize); + } + + /** + Get @ref Sn_RXBUF_SIZE register + @return uint8_t. Value of @ref Sn_RXBUF_SIZE. + @sa setSn_RXBUF_SIZE() + */ + inline uint8_t getSn_RXBUF_SIZE() + { + return wizchip_read(BlockSelectSReg, Sn_RXBUF_SIZE); + } + + /** + Set @ref Sn_TXBUF_SIZE register + @param (uint8_t)txbufsize Value to set @ref Sn_TXBUF_SIZE + @sa getSn_TXBUF_SIZE() + */ + inline void setSn_TXBUF_SIZE(uint8_t txbufsize) + { + wizchip_write(BlockSelectSReg, Sn_TXBUF_SIZE, txbufsize); + } + + /** + Get @ref Sn_TXBUF_SIZE register + @return uint8_t. Value of @ref Sn_TXBUF_SIZE. + @sa setSn_TXBUF_SIZE() + */ + inline uint8_t getSn_TXBUF_SIZE() + { + return wizchip_read(BlockSelectSReg, Sn_TXBUF_SIZE); + } + + /** + Get @ref Sn_TX_RD register + @return uint16_t. Value of @ref Sn_TX_RD. + */ + inline uint16_t getSn_TX_RD() + { + return wizchip_read_word(BlockSelectSReg, Sn_TX_RD); + } + + /** + Set @ref Sn_TX_WR register + @param (uint16_t)txwr Value to set @ref Sn_TX_WR + @sa GetSn_TX_WR() + */ + inline void setSn_TX_WR(uint16_t txwr) + { + wizchip_write_word(BlockSelectSReg, Sn_TX_WR, txwr); + } + + /** + Get @ref Sn_TX_WR register + @return uint16_t. Value of @ref Sn_TX_WR. + @sa setSn_TX_WR() + */ + inline uint16_t getSn_TX_WR() + { + return wizchip_read_word(BlockSelectSReg, Sn_TX_WR); + } + + /** + Set @ref Sn_RX_RD register + @param (uint16_t)rxrd Value to set @ref Sn_RX_RD + @sa getSn_RX_RD() + */ + inline void setSn_RX_RD(uint16_t rxrd) + { + wizchip_write_word(BlockSelectSReg, Sn_RX_RD, rxrd); + } + + /** + Get @ref Sn_RX_RD register + @return uint16_t. Value of @ref Sn_RX_RD. + @sa setSn_RX_RD() + */ + inline uint16_t getSn_RX_RD() + { + return wizchip_read_word(BlockSelectSReg, Sn_RX_RD); + } + + /** + Get @ref Sn_RX_WR register + @return uint16_t. Value of @ref Sn_RX_WR. + */ + inline uint16_t getSn_RX_WR() + { + return wizchip_read_word(BlockSelectSReg, Sn_RX_WR); + } +}; + +#endif // W5500_H diff --git a/tests/host/Makefile b/tests/host/Makefile index 45b87522fc..65b36e7d66 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -299,6 +299,7 @@ ARDUINO_LIBS := \ IPAddress.cpp \ Updater.cpp \ base64.cpp \ + LwipIntf.cpp \ LwipIntfCB.cpp \ ) \ $(addprefix $(abspath ../../libraries/ESP8266WiFi/src)/,\ diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index f7061e38a5..b3a302df18 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -41,6 +41,49 @@ #include "MocklwIP.h" +#include + +bool DhcpServer::set_dhcps_lease(struct dhcps_lease *please) +{ + (void)please; + return false; +} + +bool DhcpServer::set_dhcps_lease_time(uint32 minute) +{ + (void)minute; + return false; +} + +bool DhcpServer::set_dhcps_offer_option(uint8 level, void* optarg) +{ + (void)level; + (void)optarg; + return false; +} + +void DhcpServer::end () +{ +} + +bool DhcpServer::begin (struct ip_info *info) +{ + (void)info; + return false; +} + +DhcpServer::DhcpServer (netif* netif) +{ + (void)netif; +} + +DhcpServer::~DhcpServer () +{ + end(); +} + +DhcpServer dhcpSoftAP(nullptr); + extern "C" { diff --git a/tests/restyle.sh b/tests/restyle.sh index c50c5f6413..f2961d63b4 100755 --- a/tests/restyle.sh +++ b/tests/restyle.sh @@ -13,6 +13,8 @@ test -d libraries all=" libraries/ESP8266mDNS libraries/Wire +libraries/lwIP* +cores/esp8266/Lwip* cores/esp8266/core_esp8266_si2c.cpp libraries/Netdump " diff --git a/tools/sdk/include/ipv4_addr.h b/tools/sdk/include/ipv4_addr.h index 8d8747866e..e48d5fee45 100644 --- a/tools/sdk/include/ipv4_addr.h +++ b/tools/sdk/include/ipv4_addr.h @@ -34,13 +34,17 @@ // hence ipv4_addr/t is IPv4 version/copy of IPv4 ip_addr/_t // when IPv6 is enabled so we can deal with IPv4 use from firmware API. +#define ipv4_addr ip4_addr +#define ipv4_addr_t ip4_addr_t + // official lwIP's definitions #include "lwip/ip_addr.h" +#if LWIP_VERSION_MAJOR == 1 +struct ip4_addr { uint32_t addr; }; +typedef struct ip4_addr ip4_addr_t; +#else #include -#define ipv4_addr ip4_addr -#define ipv4_addr_t ip4_addr_t - // defined in lwip-v1.4 sources only, used in fw struct ip_info { struct ipv4_addr ip; @@ -48,4 +52,5 @@ struct ip_info { struct ipv4_addr gw; }; +#endif #endif // __IPV4_ADDR_H__ diff --git a/tools/sdk/include/user_interface.h b/tools/sdk/include/user_interface.h index 04d60645a4..e69821b372 100644 --- a/tools/sdk/include/user_interface.h +++ b/tools/sdk/include/user_interface.h @@ -382,13 +382,17 @@ void wifi_softap_free_station_info(void); bool wifi_softap_dhcps_start(void); bool wifi_softap_dhcps_stop(void); +#if 1 // dhcp server +// these functions are open-source, in dhcp server, +// which is now moved to lwIPDhcpServer.cpp (lwip2) +// (but still there with lwip1) bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please); bool wifi_softap_get_dhcps_lease(struct dhcps_lease *please); uint32 wifi_softap_get_dhcps_lease_time(void); bool wifi_softap_set_dhcps_lease_time(uint32 minute); bool wifi_softap_reset_dhcps_lease_time(void); - bool wifi_softap_add_dhcps_lease(uint8 *macaddr); // add static lease on the list, this will be the next available @ +#endif // dhcp server enum dhcp_status wifi_softap_dhcps_status(void); bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg); diff --git a/tools/sdk/lib/liblwip2-1460-feat.a b/tools/sdk/lib/liblwip2-1460-feat.a index 094ce1a472..3ba746cd2b 100644 Binary files a/tools/sdk/lib/liblwip2-1460-feat.a and b/tools/sdk/lib/liblwip2-1460-feat.a differ diff --git a/tools/sdk/lib/liblwip2-1460.a b/tools/sdk/lib/liblwip2-1460.a index 48fbcdce95..3b0c847bc0 100644 Binary files a/tools/sdk/lib/liblwip2-1460.a and b/tools/sdk/lib/liblwip2-1460.a differ diff --git a/tools/sdk/lib/liblwip2-536-feat.a b/tools/sdk/lib/liblwip2-536-feat.a index 360564bbdc..b841a47c61 100644 Binary files a/tools/sdk/lib/liblwip2-536-feat.a and b/tools/sdk/lib/liblwip2-536-feat.a differ diff --git a/tools/sdk/lib/liblwip2-536.a b/tools/sdk/lib/liblwip2-536.a index 013f7d53ca..9b2f8725ac 100644 Binary files a/tools/sdk/lib/liblwip2-536.a and b/tools/sdk/lib/liblwip2-536.a differ diff --git a/tools/sdk/lib/liblwip6-1460-feat.a b/tools/sdk/lib/liblwip6-1460-feat.a index 4994f41b47..4701a56d70 100644 Binary files a/tools/sdk/lib/liblwip6-1460-feat.a and b/tools/sdk/lib/liblwip6-1460-feat.a differ diff --git a/tools/sdk/lib/liblwip6-536-feat.a b/tools/sdk/lib/liblwip6-536-feat.a index 941543f6cb..956514dd1e 100644 Binary files a/tools/sdk/lib/liblwip6-536-feat.a and b/tools/sdk/lib/liblwip6-536-feat.a differ diff --git a/tools/sdk/lwip2/builder b/tools/sdk/lwip2/builder index 8dfe4663f2..7d498d9d39 160000 --- a/tools/sdk/lwip2/builder +++ b/tools/sdk/lwip2/builder @@ -1 +1 @@ -Subproject commit 8dfe4663f290caea702aab97483b0d2b034930fe +Subproject commit 7d498d9d39998c9eafb575609a0803d78893c8c3 diff --git a/tools/sdk/lwip2/include/arch/cc.h b/tools/sdk/lwip2/include/arch/cc.h index 961c51aa32..1b879c84cd 100644 --- a/tools/sdk/lwip2/include/arch/cc.h +++ b/tools/sdk/lwip2/include/arch/cc.h @@ -63,6 +63,7 @@ typedef uint32_t sys_prot_t; #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev #define SYS_ARCH_PROTECT(lev) lev = lwip_xt_rsil(15) #define SYS_ARCH_UNPROTECT(lev) lwip_xt_wsr_ps(lev) +#define sys_jiffies() (0) // only used for increased randomness in PPP #define LWIP_NO_CTYPE_H 1 diff --git a/tools/sdk/lwip2/include/lwip-git-hash.h b/tools/sdk/lwip2/include/lwip-git-hash.h index d7bb7f742f..d2f2682f68 100644 --- a/tools/sdk/lwip2/include/lwip-git-hash.h +++ b/tools/sdk/lwip2/include/lwip-git-hash.h @@ -1,5 +1,5 @@ // generated by makefiles/make-lwip2-hash #ifndef LWIP_HASH_H #define LWIP_HASH_H -#define LWIP_HASH_STR "STABLE-2_1_2_RELEASE/glue:1.2-43-ge20f213" +#define LWIP_HASH_STR "STABLE-2_1_2_RELEASE/glue:1.2-46-g7d498d9" #endif // LWIP_HASH_H diff --git a/tools/sdk/lwip2/include/lwip/priv/tcp_priv.h b/tools/sdk/lwip2/include/lwip/priv/tcp_priv.h index 72f9126d46..394d9633af 100644 --- a/tools/sdk/lwip2/include/lwip/priv/tcp_priv.h +++ b/tools/sdk/lwip2/include/lwip/priv/tcp_priv.h @@ -357,7 +357,7 @@ extern struct tcp_pcb ** const tcp_pcb_lists[NUM_TCP_PCB_LISTS]; #if TCP_DEBUG_PCB_LISTS #define TCP_REG(pcbs, npcb) do {\ struct tcp_pcb *tcp_tmp_pcb; \ - LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %"U16_F"\n", (void *)(npcb), (npcb)->local_port)); \ + LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %" U16_F "\n", (void *)(npcb), (npcb)->local_port)); \ for (tcp_tmp_pcb = *(pcbs); \ tcp_tmp_pcb != NULL; \ tcp_tmp_pcb = tcp_tmp_pcb->next) { \ diff --git a/tools/sdk/lwip2/include/lwipopts.h b/tools/sdk/lwip2/include/lwipopts.h index d09d646b0e..91b41fdebf 100644 --- a/tools/sdk/lwip2/include/lwipopts.h +++ b/tools/sdk/lwip2/include/lwipopts.h @@ -3139,7 +3139,7 @@ * u8_t *ptr = (u8_t*)pbuf_get_contiguous(p, buf, sizeof(buf), LWIP_MIN(option_len, sizeof(buf)), offset); */ #ifdef __DOXYGEN__ -#define LWIP_HOOK_DHCP_PARSE_OPTION(netif, dhcp, state, msg, msg_type, option, len, pbuf, offset) +//#define LWIP_HOOK_DHCP_PARSE_OPTION(netif, dhcp, state, msg, msg_type, option, len, pbuf, offset) #endif /** @@ -3551,6 +3551,12 @@ #error LWIP_FEATURES must be defined #endif +#define PPPOS_SUPPORT IP_NAPT // because we don't have proxyarp yet +#define PPP_SUPPORT PPPOS_SUPPORT +#define PPP_SERVER 1 +#define PPP_DEBUG ULWIPDEBUG +#define PRINTPKT_SUPPORT ULWIPDEBUG + #ifdef __cplusplus extern "C" { #endif @@ -3560,6 +3566,24 @@ extern "C" { */ #define TCP_RANDOM_PORT 1 +/* + -------------------------------------------------- + ------------------ DHCP options ------------------ + -------------------------------------------------- +*/ + +#define LWIP_HOOK_DHCP_PARSE_OPTION(netif, dhcp, state, msg, msg_type, option, len, pbuf, option_value_offset) \ + lwip_hook_dhcp_parse_option(netif, dhcp, state, msg, msg_type, option, len, pbuf, option_value_offset) + +// search for LWIP_HOOK_DHCP_PARSE_OPTION above for an arguments explanation +struct netif; +struct dhcp; +struct dhcp_msg; +struct pbuf; +extern void lwip_hook_dhcp_parse_option(struct netif *netif, struct dhcp *dhcp, int state, struct dhcp_msg *msg, + int msg_type, int option, int option_len, struct pbuf *pbuf, + int option_value_offset); + /* -------------------------------------------------- ------------------ SNTP options ------------------ diff --git a/tools/sdk/lwip2/include/netif/ppp/fsm.h b/tools/sdk/lwip2/include/netif/ppp/fsm.h new file mode 100644 index 0000000000..8dec700e07 --- /dev/null +++ b/tools/sdk/lwip2/include/netif/ppp/fsm.h @@ -0,0 +1,182 @@ +/* + * fsm.h - {Link, IP} Control Protocol Finite State Machine definitions. + * + * Copyright (c) 1984-2000 Carnegie Mellon University. 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 "Carnegie Mellon University" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For permission or any legal + * details, please contact + * Office of Technology Transfer + * Carnegie Mellon University + * 5000 Forbes Avenue + * Pittsburgh, PA 15213-3890 + * (412) 268-4387, fax: (412) 268-7395 + * tech-transfer@andrew.cmu.edu + * + * 4. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by Computing Services + * at Carnegie Mellon University (http://www.cmu.edu/computing/)." + * + * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO + * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE + * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * $Id: fsm.h,v 1.10 2004/11/13 02:28:15 paulus Exp $ + */ + +#include "netif/ppp/ppp_opts.h" +#if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */ + +#ifndef FSM_H +#define FSM_H + +#include "ppp.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Packet header = Code, id, length. + */ +#define HEADERLEN 4 + + +/* + * CP (LCP, IPCP, etc.) codes. + */ +#define CONFREQ 1 /* Configuration Request */ +#define CONFACK 2 /* Configuration Ack */ +#define CONFNAK 3 /* Configuration Nak */ +#define CONFREJ 4 /* Configuration Reject */ +#define TERMREQ 5 /* Termination Request */ +#define TERMACK 6 /* Termination Ack */ +#define CODEREJ 7 /* Code Reject */ + + +/* + * Each FSM is described by an fsm structure and fsm callbacks. + */ +typedef struct fsm { + ppp_pcb *pcb; /* PPP Interface */ + const struct fsm_callbacks *callbacks; /* Callback routines */ + const char *term_reason; /* Reason for closing protocol */ + u8_t seen_ack; /* Have received valid Ack/Nak/Rej to Req */ + /* -- This is our only flag, we might use u_int :1 if we have more flags */ + u16_t protocol; /* Data Link Layer Protocol field value */ + u8_t state; /* State */ + u8_t flags; /* Contains option bits */ + u8_t id; /* Current id */ + u8_t reqid; /* Current request id */ + u8_t retransmits; /* Number of retransmissions left */ + u8_t nakloops; /* Number of nak loops since last ack */ + u8_t rnakloops; /* Number of naks received */ + u8_t maxnakloops; /* Maximum number of nak loops tolerated + (necessary because IPCP require a custom large max nak loops value) */ + u8_t term_reason_len; /* Length of term_reason */ +} fsm; + + +typedef struct fsm_callbacks { + void (*resetci) /* Reset our Configuration Information */ + (fsm *); + int (*cilen) /* Length of our Configuration Information */ + (fsm *); + void (*addci) /* Add our Configuration Information */ + (fsm *, u_char *, int *); + int (*ackci) /* ACK our Configuration Information */ + (fsm *, u_char *, int); + int (*nakci) /* NAK our Configuration Information */ + (fsm *, u_char *, int, int); + int (*rejci) /* Reject our Configuration Information */ + (fsm *, u_char *, int); + int (*reqci) /* Request peer's Configuration Information */ + (fsm *, u_char *, int *, int); + void (*up) /* Called when fsm reaches PPP_FSM_OPENED state */ + (fsm *); + void (*down) /* Called when fsm leaves PPP_FSM_OPENED state */ + (fsm *); + void (*starting) /* Called when we want the lower layer */ + (fsm *); + void (*finished) /* Called when we don't want the lower layer */ + (fsm *); + void (*protreject) /* Called when Protocol-Reject received */ + (int); + void (*retransmit) /* Retransmission is necessary */ + (fsm *); + int (*extcode) /* Called when unknown code received */ + (fsm *, int, int, u_char *, int); + const char *proto_name; /* String name for protocol (for messages) */ +} fsm_callbacks; + + +/* + * Link states. + */ +#define PPP_FSM_INITIAL 0 /* Down, hasn't been opened */ +#define PPP_FSM_STARTING 1 /* Down, been opened */ +#define PPP_FSM_CLOSED 2 /* Up, hasn't been opened */ +#define PPP_FSM_STOPPED 3 /* Open, waiting for down event */ +#define PPP_FSM_CLOSING 4 /* Terminating the connection, not open */ +#define PPP_FSM_STOPPING 5 /* Terminating, but open */ +#define PPP_FSM_REQSENT 6 /* We've sent a Config Request */ +#define PPP_FSM_ACKRCVD 7 /* We've received a Config Ack */ +#define PPP_FSM_ACKSENT 8 /* We've sent a Config Ack */ +#define PPP_FSM_OPENED 9 /* Connection available */ + + +/* + * Flags - indicate options controlling FSM operation + */ +#define OPT_PASSIVE 1 /* Don't die if we don't get a response */ +#define OPT_RESTART 2 /* Treat 2nd OPEN as DOWN, UP */ +#define OPT_SILENT 4 /* Wait for peer to speak first */ + + +/* + * Timeouts. + */ +#if 0 /* moved to ppp_opts.h */ +#define DEFTIMEOUT 3 /* Timeout time in seconds */ +#define DEFMAXTERMREQS 2 /* Maximum Terminate-Request transmissions */ +#define DEFMAXCONFREQS 10 /* Maximum Configure-Request transmissions */ +#define DEFMAXNAKLOOPS 5 /* Maximum number of nak loops */ +#endif /* moved to ppp_opts.h */ + + +/* + * Prototypes + */ +void fsm_init(fsm *f); +void fsm_lowerup(fsm *f); +void fsm_lowerdown(fsm *f); +void fsm_open(fsm *f); +void fsm_close(fsm *f, const char *reason); +void fsm_input(fsm *f, u_char *inpacket, int l); +void fsm_protreject(fsm *f); +void fsm_sdata(fsm *f, u_char code, u_char id, const u_char *data, int datalen); + +#ifdef __cplusplus +} +#endif + +#endif /* FSM_H */ +#endif /* PPP_SUPPORT */ diff --git a/tools/sdk/lwip2/include/netif/ppp/ipcp.h b/tools/sdk/lwip2/include/netif/ppp/ipcp.h new file mode 100644 index 0000000000..32fdd1c641 --- /dev/null +++ b/tools/sdk/lwip2/include/netif/ppp/ipcp.h @@ -0,0 +1,134 @@ +/* + * ipcp.h - IP Control Protocol definitions. + * + * Copyright (c) 1984-2000 Carnegie Mellon University. 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 "Carnegie Mellon University" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For permission or any legal + * details, please contact + * Office of Technology Transfer + * Carnegie Mellon University + * 5000 Forbes Avenue + * Pittsburgh, PA 15213-3890 + * (412) 268-4387, fax: (412) 268-7395 + * tech-transfer@andrew.cmu.edu + * + * 4. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by Computing Services + * at Carnegie Mellon University (http://www.cmu.edu/computing/)." + * + * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO + * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE + * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * $Id: ipcp.h,v 1.14 2002/12/04 23:03:32 paulus Exp $ + */ + +#include "netif/ppp/ppp_opts.h" +#if PPP_SUPPORT && PPP_IPV4_SUPPORT /* don't build if not configured for use in lwipopts.h */ + +#ifndef IPCP_H +#define IPCP_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Options. + */ +#define CI_ADDRS 1 /* IP Addresses */ +#if VJ_SUPPORT +#define CI_COMPRESSTYPE 2 /* Compression Type */ +#endif /* VJ_SUPPORT */ +#define CI_ADDR 3 + +#if LWIP_DNS +#define CI_MS_DNS1 129 /* Primary DNS value */ +#define CI_MS_DNS2 131 /* Secondary DNS value */ +#endif /* LWIP_DNS */ +#if 0 /* UNUSED - WINS */ +#define CI_MS_WINS1 130 /* Primary WINS value */ +#define CI_MS_WINS2 132 /* Secondary WINS value */ +#endif /* UNUSED - WINS */ + +#if VJ_SUPPORT +#define MAX_STATES 16 /* from slcompress.h */ + +#define IPCP_VJMODE_OLD 1 /* "old" mode (option # = 0x0037) */ +#define IPCP_VJMODE_RFC1172 2 /* "old-rfc"mode (option # = 0x002d) */ +#define IPCP_VJMODE_RFC1332 3 /* "new-rfc"mode (option # = 0x002d, */ + /* maxslot and slot number compression) */ + +#define IPCP_VJ_COMP 0x002d /* current value for VJ compression option*/ +#define IPCP_VJ_COMP_OLD 0x0037 /* "old" (i.e, broken) value for VJ */ + /* compression option*/ +#endif /* VJ_SUPPORT */ + +typedef struct ipcp_options { + unsigned int neg_addr :1; /* Negotiate IP Address? */ + unsigned int old_addrs :1; /* Use old (IP-Addresses) option? */ + unsigned int req_addr :1; /* Ask peer to send IP address? */ +#if 0 /* UNUSED */ + unsigned int default_route :1; /* Assign default route through interface? */ + unsigned int replace_default_route :1; /* Replace default route through interface? */ +#endif /* UNUSED */ +#if 0 /* UNUSED - PROXY ARP */ + unsigned int proxy_arp :1; /* Make proxy ARP entry for peer? */ +#endif /* UNUSED - PROXY ARP */ +#if VJ_SUPPORT + unsigned int neg_vj :1; /* Van Jacobson Compression? */ + unsigned int old_vj :1; /* use old (short) form of VJ option? */ + unsigned int cflag :1; +#endif /* VJ_SUPPORT */ + unsigned int accept_local :1; /* accept peer's value for ouraddr */ + unsigned int accept_remote :1; /* accept peer's value for hisaddr */ +#if LWIP_DNS + unsigned int req_dns1 :1; /* Ask peer to send primary DNS address? */ + unsigned int req_dns2 :1; /* Ask peer to send secondary DNS address? */ +#endif /* LWIP_DNS */ + + u32_t ouraddr, hisaddr; /* Addresses in NETWORK BYTE ORDER */ +#if LWIP_DNS + u32_t dnsaddr[2]; /* Primary and secondary MS DNS entries */ +#endif /* LWIP_DNS */ +#if 0 /* UNUSED - WINS */ + u32_t winsaddr[2]; /* Primary and secondary MS WINS entries */ +#endif /* UNUSED - WINS */ + +#if VJ_SUPPORT + u16_t vj_protocol; /* protocol value to use in VJ option */ + u8_t maxslotindex; /* values for RFC1332 VJ compression neg. */ +#endif /* VJ_SUPPORT */ +} ipcp_options; + +#if 0 /* UNUSED, already defined by lwIP */ +char *ip_ntoa (u32_t); +#endif /* UNUSED, already defined by lwIP */ + +extern const struct protent ipcp_protent; + +#ifdef __cplusplus +} +#endif + +#endif /* IPCP_H */ +#endif /* PPP_SUPPORT && PPP_IPV4_SUPPORT */ diff --git a/tools/sdk/lwip2/include/netif/ppp/lcp.h b/tools/sdk/lwip2/include/netif/ppp/lcp.h new file mode 100644 index 0000000000..18ad1cb23b --- /dev/null +++ b/tools/sdk/lwip2/include/netif/ppp/lcp.h @@ -0,0 +1,179 @@ +/* + * lcp.h - Link Control Protocol definitions. + * + * Copyright (c) 1984-2000 Carnegie Mellon University. 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 "Carnegie Mellon University" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For permission or any legal + * details, please contact + * Office of Technology Transfer + * Carnegie Mellon University + * 5000 Forbes Avenue + * Pittsburgh, PA 15213-3890 + * (412) 268-4387, fax: (412) 268-7395 + * tech-transfer@andrew.cmu.edu + * + * 4. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by Computing Services + * at Carnegie Mellon University (http://www.cmu.edu/computing/)." + * + * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO + * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE + * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * $Id: lcp.h,v 1.20 2004/11/14 22:53:42 carlsonj Exp $ + */ + +#include "netif/ppp/ppp_opts.h" +#if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */ + +#ifndef LCP_H +#define LCP_H + +#include "ppp.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Options. + */ +#define CI_VENDOR 0 /* Vendor Specific */ +#define CI_MRU 1 /* Maximum Receive Unit */ +#define CI_ASYNCMAP 2 /* Async Control Character Map */ +#define CI_AUTHTYPE 3 /* Authentication Type */ +#define CI_QUALITY 4 /* Quality Protocol */ +#define CI_MAGICNUMBER 5 /* Magic Number */ +#define CI_PCOMPRESSION 7 /* Protocol Field Compression */ +#define CI_ACCOMPRESSION 8 /* Address/Control Field Compression */ +#define CI_FCSALTERN 9 /* FCS-Alternatives */ +#define CI_SDP 10 /* Self-Describing-Pad */ +#define CI_NUMBERED 11 /* Numbered-Mode */ +#define CI_CALLBACK 13 /* callback */ +#define CI_MRRU 17 /* max reconstructed receive unit; multilink */ +#define CI_SSNHF 18 /* short sequence numbers for multilink */ +#define CI_EPDISC 19 /* endpoint discriminator */ +#define CI_MPPLUS 22 /* Multi-Link-Plus-Procedure */ +#define CI_LDISC 23 /* Link-Discriminator */ +#define CI_LCPAUTH 24 /* LCP Authentication */ +#define CI_COBS 25 /* Consistent Overhead Byte Stuffing */ +#define CI_PREFELIS 26 /* Prefix Elision */ +#define CI_MPHDRFMT 27 /* MP Header Format */ +#define CI_I18N 28 /* Internationalization */ +#define CI_SDL 29 /* Simple Data Link */ + +/* + * LCP-specific packet types (code numbers). + */ +#define PROTREJ 8 /* Protocol Reject */ +#define ECHOREQ 9 /* Echo Request */ +#define ECHOREP 10 /* Echo Reply */ +#define DISCREQ 11 /* Discard Request */ +#define IDENTIF 12 /* Identification */ +#define TIMEREM 13 /* Time Remaining */ + +/* Value used as data for CI_CALLBACK option */ +#define CBCP_OPT 6 /* Use callback control protocol */ + +#if 0 /* moved to ppp_opts.h */ +#define DEFMRU 1500 /* Try for this */ +#define MINMRU 128 /* No MRUs below this */ +#define MAXMRU 16384 /* Normally limit MRU to this */ +#endif /* moved to ppp_opts.h */ + +/* An endpoint discriminator, used with multilink. */ +#define MAX_ENDP_LEN 20 /* maximum length of discriminator value */ +struct epdisc { + unsigned char class_; /* -- The word "class" is reserved in C++. */ + unsigned char length; + unsigned char value[MAX_ENDP_LEN]; +}; + +/* + * The state of options is described by an lcp_options structure. + */ +typedef struct lcp_options { + unsigned int passive :1; /* Don't die if we don't get a response */ + unsigned int silent :1; /* Wait for the other end to start first */ +#if 0 /* UNUSED */ + unsigned int restart :1; /* Restart vs. exit after close */ +#endif /* UNUSED */ + unsigned int neg_mru :1; /* Negotiate the MRU? */ + unsigned int neg_asyncmap :1; /* Negotiate the async map? */ +#if PAP_SUPPORT + unsigned int neg_upap :1; /* Ask for UPAP authentication? */ +#endif /* PAP_SUPPORT */ +#if CHAP_SUPPORT + unsigned int neg_chap :1; /* Ask for CHAP authentication? */ +#endif /* CHAP_SUPPORT */ +#if EAP_SUPPORT + unsigned int neg_eap :1; /* Ask for EAP authentication? */ +#endif /* EAP_SUPPORT */ + unsigned int neg_magicnumber :1; /* Ask for magic number? */ + unsigned int neg_pcompression :1; /* HDLC Protocol Field Compression? */ + unsigned int neg_accompression :1; /* HDLC Address/Control Field Compression? */ +#if LQR_SUPPORT + unsigned int neg_lqr :1; /* Negotiate use of Link Quality Reports */ +#endif /* LQR_SUPPORT */ + unsigned int neg_cbcp :1; /* Negotiate use of CBCP */ +#ifdef HAVE_MULTILINK + unsigned int neg_mrru :1; /* negotiate multilink MRRU */ +#endif /* HAVE_MULTILINK */ + unsigned int neg_ssnhf :1; /* negotiate short sequence numbers */ + unsigned int neg_endpoint :1; /* negotiate endpoint discriminator */ + + u16_t mru; /* Value of MRU */ +#ifdef HAVE_MULTILINK + u16_t mrru; /* Value of MRRU, and multilink enable */ +#endif /* MULTILINK */ +#if CHAP_SUPPORT + u8_t chap_mdtype; /* which MD types (hashing algorithm) */ +#endif /* CHAP_SUPPORT */ + u32_t asyncmap; /* Value of async map */ + u32_t magicnumber; + u8_t numloops; /* Number of loops during magic number neg. */ +#if LQR_SUPPORT + u32_t lqr_period; /* Reporting period for LQR 1/100ths second */ +#endif /* LQR_SUPPORT */ + struct epdisc endpoint; /* endpoint discriminator */ +} lcp_options; + +void lcp_open(ppp_pcb *pcb); +void lcp_close(ppp_pcb *pcb, const char *reason); +void lcp_lowerup(ppp_pcb *pcb); +void lcp_lowerdown(ppp_pcb *pcb); +void lcp_sprotrej(ppp_pcb *pcb, u_char *p, int len); /* send protocol reject */ + +extern const struct protent lcp_protent; + +#if 0 /* moved to ppp_opts.h */ +/* Default number of times we receive our magic number from the peer + before deciding the link is looped-back. */ +#define DEFLOOPBACKFAIL 10 +#endif /* moved to ppp_opts.h */ + +#ifdef __cplusplus +} +#endif + +#endif /* LCP_H */ +#endif /* PPP_SUPPORT */ diff --git a/tools/sdk/lwip2/include/netif/ppp/ppp.h b/tools/sdk/lwip2/include/netif/ppp/ppp.h new file mode 100644 index 0000000000..3d73c36570 --- /dev/null +++ b/tools/sdk/lwip2/include/netif/ppp/ppp.h @@ -0,0 +1,698 @@ +/***************************************************************************** +* ppp.h - Network Point to Point Protocol header file. +* +* Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc. +* portions Copyright (c) 1997 Global Election Systems Inc. +* +* The authors hereby grant permission to use, copy, modify, distribute, +* and license this software and its documentation for any purpose, provided +* that existing copyright notices are retained in all copies and that this +* notice and the following disclaimer are included verbatim in any +* distributions. No written agreement, license, or royalty fee is required +* for any of the authorized uses. +* +* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *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 CONTRIBUTORS 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. +* +****************************************************************************** +* REVISION HISTORY +* +* 03-01-01 Marc Boucher +* Ported to lwIP. +* 97-11-05 Guy Lancaster , Global Election Systems Inc. +* Original derived from BSD codes. +*****************************************************************************/ + +#include "netif/ppp/ppp_opts.h" +#if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */ + +#ifndef PPP_H +#define PPP_H + +#include "lwip/def.h" +#include "lwip/stats.h" +#include "lwip/mem.h" +#include "lwip/netif.h" +#include "lwip/sys.h" +#include "lwip/timeouts.h" +#if PPP_IPV6_SUPPORT +#include "lwip/ip6_addr.h" +#endif /* PPP_IPV6_SUPPORT */ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Disable non-working or rarely used PPP feature, so rarely that we don't want to bloat ppp_opts.h with them */ +#ifndef PPP_OPTIONS +#define PPP_OPTIONS 0 +#endif + +#ifndef PPP_NOTIFY +#define PPP_NOTIFY 0 +#endif + +#ifndef PPP_REMOTENAME +#define PPP_REMOTENAME 0 +#endif + +#ifndef PPP_IDLETIMELIMIT +#define PPP_IDLETIMELIMIT 0 +#endif + +#ifndef PPP_LCP_ADAPTIVE +#define PPP_LCP_ADAPTIVE 0 +#endif + +#ifndef PPP_MAXCONNECT +#define PPP_MAXCONNECT 0 +#endif + +#ifndef PPP_ALLOWED_ADDRS +#define PPP_ALLOWED_ADDRS 0 +#endif + +#ifndef PPP_PROTOCOLNAME +#define PPP_PROTOCOLNAME 0 +#endif + +#ifndef PPP_STATS_SUPPORT +#define PPP_STATS_SUPPORT 0 +#endif + +#ifndef DEFLATE_SUPPORT +#define DEFLATE_SUPPORT 0 +#endif + +#ifndef BSDCOMPRESS_SUPPORT +#define BSDCOMPRESS_SUPPORT 0 +#endif + +#ifndef PREDICTOR_SUPPORT +#define PREDICTOR_SUPPORT 0 +#endif + +/************************* +*** PUBLIC DEFINITIONS *** +*************************/ + +/* + * The basic PPP frame. + */ +#define PPP_HDRLEN 4 /* octets for standard ppp header */ +#define PPP_FCSLEN 2 /* octets for FCS */ + +/* + * Values for phase. + */ +#define PPP_PHASE_DEAD 0 +#define PPP_PHASE_MASTER 1 +#define PPP_PHASE_HOLDOFF 2 +#define PPP_PHASE_INITIALIZE 3 +#define PPP_PHASE_SERIALCONN 4 +#define PPP_PHASE_DORMANT 5 +#define PPP_PHASE_ESTABLISH 6 +#define PPP_PHASE_AUTHENTICATE 7 +#define PPP_PHASE_CALLBACK 8 +#define PPP_PHASE_NETWORK 9 +#define PPP_PHASE_RUNNING 10 +#define PPP_PHASE_TERMINATE 11 +#define PPP_PHASE_DISCONNECT 12 + +/* Error codes. */ +#define PPPERR_NONE 0 /* No error. */ +#define PPPERR_PARAM 1 /* Invalid parameter. */ +#define PPPERR_OPEN 2 /* Unable to open PPP session. */ +#define PPPERR_DEVICE 3 /* Invalid I/O device for PPP. */ +#define PPPERR_ALLOC 4 /* Unable to allocate resources. */ +#define PPPERR_USER 5 /* User interrupt. */ +#define PPPERR_CONNECT 6 /* Connection lost. */ +#define PPPERR_AUTHFAIL 7 /* Failed authentication challenge. */ +#define PPPERR_PROTOCOL 8 /* Failed to meet protocol. */ +#define PPPERR_PEERDEAD 9 /* Connection timeout */ +#define PPPERR_IDLETIMEOUT 10 /* Idle Timeout */ +#define PPPERR_CONNECTTIME 11 /* Max connect time reached */ +#define PPPERR_LOOPBACK 12 /* Loopback detected */ + +/* Whether auth support is enabled at all */ +#define PPP_AUTH_SUPPORT (PAP_SUPPORT || CHAP_SUPPORT || EAP_SUPPORT) + +/************************ +*** PUBLIC DATA TYPES *** +************************/ + +/* + * Other headers require ppp_pcb definition for prototypes, but ppp_pcb + * require some structure definition from other headers as well, we are + * fixing the dependency loop here by declaring the ppp_pcb type then + * by including headers containing necessary struct definition for ppp_pcb + */ +typedef struct ppp_pcb_s ppp_pcb; + +/* Type definitions for BSD code. */ +#ifndef __u_char_defined +typedef unsigned long u_long; +typedef unsigned int u_int; +typedef unsigned short u_short; +typedef unsigned char u_char; +#endif + +#include "fsm.h" +#include "lcp.h" +#if CCP_SUPPORT +#include "ccp.h" +#endif /* CCP_SUPPORT */ +#if MPPE_SUPPORT +#include "mppe.h" +#endif /* MPPE_SUPPORT */ +#if PPP_IPV4_SUPPORT +#include "ipcp.h" +#endif /* PPP_IPV4_SUPPORT */ +#if PPP_IPV6_SUPPORT +#include "ipv6cp.h" +#endif /* PPP_IPV6_SUPPORT */ +#if PAP_SUPPORT +#include "upap.h" +#endif /* PAP_SUPPORT */ +#if CHAP_SUPPORT +#include "chap-new.h" +#endif /* CHAP_SUPPORT */ +#if EAP_SUPPORT +#include "eap.h" +#endif /* EAP_SUPPORT */ +#if VJ_SUPPORT +#include "vj.h" +#endif /* VJ_SUPPORT */ + +/* Link status callback function prototype */ +typedef void (*ppp_link_status_cb_fn)(ppp_pcb *pcb, int err_code, void *ctx); + +/* + * PPP configuration. + */ +typedef struct ppp_settings_s { + +#if PPP_SERVER && PPP_AUTH_SUPPORT + unsigned int auth_required :1; /* Peer is required to authenticate */ + unsigned int null_login :1; /* Username of "" and a password of "" are acceptable */ +#endif /* PPP_SERVER && PPP_AUTH_SUPPORT */ +#if PPP_REMOTENAME + unsigned int explicit_remote :1; /* remote_name specified with remotename opt */ +#endif /* PPP_REMOTENAME */ +#if PAP_SUPPORT + unsigned int refuse_pap :1; /* Don't proceed auth. with PAP */ +#endif /* PAP_SUPPORT */ +#if CHAP_SUPPORT + unsigned int refuse_chap :1; /* Don't proceed auth. with CHAP */ +#endif /* CHAP_SUPPORT */ +#if MSCHAP_SUPPORT + unsigned int refuse_mschap :1; /* Don't proceed auth. with MS-CHAP */ + unsigned int refuse_mschap_v2 :1; /* Don't proceed auth. with MS-CHAPv2 */ +#endif /* MSCHAP_SUPPORT */ +#if EAP_SUPPORT + unsigned int refuse_eap :1; /* Don't proceed auth. with EAP */ +#endif /* EAP_SUPPORT */ +#if LWIP_DNS + unsigned int usepeerdns :1; /* Ask peer for DNS adds */ +#endif /* LWIP_DNS */ + unsigned int persist :1; /* Persist mode, always try to open the connection */ +#if PRINTPKT_SUPPORT + unsigned int hide_password :1; /* Hide password in dumped packets */ +#endif /* PRINTPKT_SUPPORT */ + unsigned int noremoteip :1; /* Let him have no IP address */ + unsigned int lax_recv :1; /* accept control chars in asyncmap */ + unsigned int noendpoint :1; /* don't send/accept endpoint discriminator */ +#if PPP_LCP_ADAPTIVE + unsigned int lcp_echo_adaptive :1; /* request echo only if the link was idle */ +#endif /* PPP_LCP_ADAPTIVE */ +#if MPPE_SUPPORT + unsigned int require_mppe :1; /* Require MPPE (Microsoft Point to Point Encryption) */ + unsigned int refuse_mppe_40 :1; /* Allow MPPE 40-bit mode? */ + unsigned int refuse_mppe_128 :1; /* Allow MPPE 128-bit mode? */ + unsigned int refuse_mppe_stateful :1; /* Allow MPPE stateful mode? */ +#endif /* MPPE_SUPPORT */ + + u16_t listen_time; /* time to listen first (ms), waiting for peer to send LCP packet */ + +#if PPP_IDLETIMELIMIT + u16_t idle_time_limit; /* Disconnect if idle for this many seconds */ +#endif /* PPP_IDLETIMELIMIT */ +#if PPP_MAXCONNECT + u32_t maxconnect; /* Maximum connect time (seconds) */ +#endif /* PPP_MAXCONNECT */ + +#if PPP_AUTH_SUPPORT + /* auth data */ + const char *user; /* Username for PAP */ + const char *passwd; /* Password for PAP, secret for CHAP */ +#if PPP_REMOTENAME + char remote_name[MAXNAMELEN + 1]; /* Peer's name for authentication */ +#endif /* PPP_REMOTENAME */ + +#if PAP_SUPPORT + u8_t pap_timeout_time; /* Timeout (seconds) for auth-req retrans. */ + u8_t pap_max_transmits; /* Number of auth-reqs sent */ +#if PPP_SERVER + u8_t pap_req_timeout; /* Time to wait for auth-req from peer */ +#endif /* PPP_SERVER */ +#endif /* PAP_SUPPPORT */ + +#if CHAP_SUPPORT + u8_t chap_timeout_time; /* Timeout (seconds) for retransmitting req */ + u8_t chap_max_transmits; /* max # times to send challenge */ +#if PPP_SERVER + u8_t chap_rechallenge_time; /* Time to wait for auth-req from peer */ +#endif /* PPP_SERVER */ +#endif /* CHAP_SUPPPORT */ + +#if EAP_SUPPORT + u8_t eap_req_time; /* Time to wait (for retransmit/fail) */ + u8_t eap_allow_req; /* Max Requests allowed */ +#if PPP_SERVER + u8_t eap_timeout_time; /* Time to wait (for retransmit/fail) */ + u8_t eap_max_transmits; /* Max Requests allowed */ +#endif /* PPP_SERVER */ +#endif /* EAP_SUPPORT */ + +#endif /* PPP_AUTH_SUPPORT */ + + u8_t fsm_timeout_time; /* Timeout time in seconds */ + u8_t fsm_max_conf_req_transmits; /* Maximum Configure-Request transmissions */ + u8_t fsm_max_term_transmits; /* Maximum Terminate-Request transmissions */ + u8_t fsm_max_nak_loops; /* Maximum number of nak loops tolerated */ + + u8_t lcp_loopbackfail; /* Number of times we receive our magic number from the peer + before deciding the link is looped-back. */ + u8_t lcp_echo_interval; /* Interval between LCP echo-requests */ + u8_t lcp_echo_fails; /* Tolerance to unanswered echo-requests */ + +} ppp_settings; + +#if PPP_SERVER +struct ppp_addrs { +#if PPP_IPV4_SUPPORT + ip4_addr_t our_ipaddr, his_ipaddr, netmask; +#if LWIP_DNS + ip4_addr_t dns1, dns2; +#endif /* LWIP_DNS */ +#endif /* PPP_IPV4_SUPPORT */ +#if PPP_IPV6_SUPPORT + ip6_addr_t our6_ipaddr, his6_ipaddr; +#endif /* PPP_IPV6_SUPPORT */ +}; +#endif /* PPP_SERVER */ + +/* + * PPP interface control block. + */ +struct ppp_pcb_s { + ppp_settings settings; + const struct link_callbacks *link_cb; + void *link_ctx_cb; + void (*link_status_cb)(ppp_pcb *pcb, int err_code, void *ctx); /* Status change callback */ +#if PPP_NOTIFY_PHASE + void (*notify_phase_cb)(ppp_pcb *pcb, u8_t phase, void *ctx); /* Notify phase callback */ +#endif /* PPP_NOTIFY_PHASE */ + void *ctx_cb; /* Callbacks optional pointer */ + struct netif *netif; /* PPP interface */ + u8_t phase; /* where the link is at */ + u8_t err_code; /* Code indicating why interface is down. */ + + /* flags */ +#if PPP_IPV4_SUPPORT + unsigned int ask_for_local :1; /* request our address from peer */ + unsigned int ipcp_is_open :1; /* haven't called np_finished() */ + unsigned int ipcp_is_up :1; /* have called ipcp_up() */ + unsigned int if4_up :1; /* True when the IPv4 interface is up. */ +#if 0 /* UNUSED - PROXY ARP */ + unsigned int proxy_arp_set :1; /* Have created proxy arp entry */ +#endif /* UNUSED - PROXY ARP */ +#endif /* PPP_IPV4_SUPPORT */ +#if PPP_IPV6_SUPPORT + unsigned int ipv6cp_is_up :1; /* have called ip6cp_up() */ + unsigned int if6_up :1; /* True when the IPv6 interface is up. */ +#endif /* PPP_IPV6_SUPPORT */ + unsigned int lcp_echo_timer_running :1; /* set if a timer is running */ +#if VJ_SUPPORT + unsigned int vj_enabled :1; /* Flag indicating VJ compression enabled. */ +#endif /* VJ_SUPPORT */ +#if CCP_SUPPORT + unsigned int ccp_all_rejected :1; /* we rejected all peer's options */ +#endif /* CCP_SUPPORT */ +#if MPPE_SUPPORT + unsigned int mppe_keys_set :1; /* Have the MPPE keys been set? */ +#endif /* MPPE_SUPPORT */ + +#if PPP_AUTH_SUPPORT + /* auth data */ +#if PPP_SERVER && defined(HAVE_MULTILINK) + char peer_authname[MAXNAMELEN + 1]; /* The name by which the peer authenticated itself to us. */ +#endif /* PPP_SERVER && defined(HAVE_MULTILINK) */ + u16_t auth_pending; /* Records which authentication operations haven't completed yet. */ + u16_t auth_done; /* Records which authentication operations have been completed. */ + +#if PAP_SUPPORT + upap_state upap; /* PAP data */ +#endif /* PAP_SUPPORT */ + +#if CHAP_SUPPORT + chap_client_state chap_client; /* CHAP client data */ +#if PPP_SERVER + chap_server_state chap_server; /* CHAP server data */ +#endif /* PPP_SERVER */ +#endif /* CHAP_SUPPORT */ + +#if EAP_SUPPORT + eap_state eap; /* EAP data */ +#endif /* EAP_SUPPORT */ +#endif /* PPP_AUTH_SUPPORT */ + + fsm lcp_fsm; /* LCP fsm structure */ + lcp_options lcp_wantoptions; /* Options that we want to request */ + lcp_options lcp_gotoptions; /* Options that peer ack'd */ + lcp_options lcp_allowoptions; /* Options we allow peer to request */ + lcp_options lcp_hisoptions; /* Options that we ack'd */ + u16_t peer_mru; /* currently negotiated peer MRU */ + u8_t lcp_echos_pending; /* Number of outstanding echo msgs */ + u8_t lcp_echo_number; /* ID number of next echo frame */ + + u8_t num_np_open; /* Number of network protocols which we have opened. */ + u8_t num_np_up; /* Number of network protocols which have come up. */ + +#if VJ_SUPPORT + struct vjcompress vj_comp; /* Van Jacobson compression header. */ +#endif /* VJ_SUPPORT */ + +#if CCP_SUPPORT + fsm ccp_fsm; /* CCP fsm structure */ + ccp_options ccp_wantoptions; /* what to request the peer to use */ + ccp_options ccp_gotoptions; /* what the peer agreed to do */ + ccp_options ccp_allowoptions; /* what we'll agree to do */ + ccp_options ccp_hisoptions; /* what we agreed to do */ + u8_t ccp_localstate; /* Local state (mainly for handling reset-reqs and reset-acks). */ + u8_t ccp_receive_method; /* Method chosen on receive path */ + u8_t ccp_transmit_method; /* Method chosen on transmit path */ +#if MPPE_SUPPORT + ppp_mppe_state mppe_comp; /* MPPE "compressor" structure */ + ppp_mppe_state mppe_decomp; /* MPPE "decompressor" structure */ +#endif /* MPPE_SUPPORT */ +#endif /* CCP_SUPPORT */ + +#if PPP_IPV4_SUPPORT + fsm ipcp_fsm; /* IPCP fsm structure */ + ipcp_options ipcp_wantoptions; /* Options that we want to request */ + ipcp_options ipcp_gotoptions; /* Options that peer ack'd */ + ipcp_options ipcp_allowoptions; /* Options we allow peer to request */ + ipcp_options ipcp_hisoptions; /* Options that we ack'd */ +#endif /* PPP_IPV4_SUPPORT */ + +#if PPP_IPV6_SUPPORT + fsm ipv6cp_fsm; /* IPV6CP fsm structure */ + ipv6cp_options ipv6cp_wantoptions; /* Options that we want to request */ + ipv6cp_options ipv6cp_gotoptions; /* Options that peer ack'd */ + ipv6cp_options ipv6cp_allowoptions; /* Options we allow peer to request */ + ipv6cp_options ipv6cp_hisoptions; /* Options that we ack'd */ +#endif /* PPP_IPV6_SUPPORT */ +}; + +/************************ + *** PUBLIC FUNCTIONS *** + ************************/ + +/* + * WARNING: For multi-threads environment, all ppp_set_* functions most + * only be called while the PPP is in the dead phase (i.e. disconnected). + */ + +#if PPP_AUTH_SUPPORT +/* + * Set PPP authentication. + * + * Warning: Using PPPAUTHTYPE_ANY might have security consequences. + * RFC 1994 says: + * + * In practice, within or associated with each PPP server, there is a + * database which associates "user" names with authentication + * information ("secrets"). It is not anticipated that a particular + * named user would be authenticated by multiple methods. This would + * make the user vulnerable to attacks which negotiate the least secure + * method from among a set (such as PAP rather than CHAP). If the same + * secret was used, PAP would reveal the secret to be used later with + * CHAP. + * + * Instead, for each user name there should be an indication of exactly + * one method used to authenticate that user name. If a user needs to + * make use of different authentication methods under different + * circumstances, then distinct user names SHOULD be employed, each of + * which identifies exactly one authentication method. + * + * Default is none auth type, unset (NULL) user and passwd. + */ +#define PPPAUTHTYPE_NONE 0x00 +#define PPPAUTHTYPE_PAP 0x01 +#define PPPAUTHTYPE_CHAP 0x02 +#define PPPAUTHTYPE_MSCHAP 0x04 +#define PPPAUTHTYPE_MSCHAP_V2 0x08 +#define PPPAUTHTYPE_EAP 0x10 +#define PPPAUTHTYPE_ANY 0xff +void ppp_set_auth(ppp_pcb *pcb, u8_t authtype, const char *user, const char *passwd); + +/* + * If set, peer is required to authenticate. This is mostly necessary for PPP server support. + * + * Default is false. + */ +#define ppp_set_auth_required(ppp, boolval) (ppp->settings.auth_required = boolval) +#endif /* PPP_AUTH_SUPPORT */ + +#if PPP_IPV4_SUPPORT +/* + * Set PPP interface "our" and "his" IPv4 addresses. This is mostly necessary for PPP server + * support but it can also be used on a PPP link where each side choose its own IP address. + * + * Default is unset (0.0.0.0). + */ +#define ppp_set_ipcp_ouraddr(ppp, addr) do { ppp->ipcp_wantoptions.ouraddr = ip4_addr_get_u32(addr); \ + ppp->ask_for_local = ppp->ipcp_wantoptions.ouraddr != 0; } while(0) +#define ppp_set_ipcp_hisaddr(ppp, addr) (ppp->ipcp_wantoptions.hisaddr = ip4_addr_get_u32(addr)) +#if LWIP_DNS +/* + * Set DNS server addresses that are sent if the peer asks for them. This is mostly necessary + * for PPP server support. + * + * Default is unset (0.0.0.0). + */ +#define ppp_set_ipcp_dnsaddr(ppp, index, addr) (ppp->ipcp_allowoptions.dnsaddr[index] = ip4_addr_get_u32(addr)) + +/* + * If set, we ask the peer for up to 2 DNS server addresses. Received DNS server addresses are + * registered using the dns_setserver() function. + * + * Default is false. + */ +#define ppp_set_usepeerdns(ppp, boolval) (ppp->settings.usepeerdns = boolval) +#endif /* LWIP_DNS */ +#endif /* PPP_IPV4_SUPPORT */ + +#if MPPE_SUPPORT +/* Disable MPPE (Microsoft Point to Point Encryption). This parameter is exclusive. */ +#define PPP_MPPE_DISABLE 0x00 +/* Require the use of MPPE (Microsoft Point to Point Encryption). */ +#define PPP_MPPE_ENABLE 0x01 +/* Allow MPPE to use stateful mode. Stateless mode is still attempted first. */ +#define PPP_MPPE_ALLOW_STATEFUL 0x02 +/* Refuse the use of MPPE with 40-bit encryption. Conflict with PPP_MPPE_REFUSE_128. */ +#define PPP_MPPE_REFUSE_40 0x04 +/* Refuse the use of MPPE with 128-bit encryption. Conflict with PPP_MPPE_REFUSE_40. */ +#define PPP_MPPE_REFUSE_128 0x08 +/* + * Set MPPE configuration + * + * Default is disabled. + */ +void ppp_set_mppe(ppp_pcb *pcb, u8_t flags); +#endif /* MPPE_SUPPORT */ + +/* + * Wait for up to intval milliseconds for a valid PPP packet from the peer. + * At the end of this time, or when a valid PPP packet is received from the + * peer, we commence negotiation by sending our first LCP packet. + * + * Default is 0. + */ +#define ppp_set_listen_time(ppp, intval) (ppp->settings.listen_time = intval) + +/* + * If set, we will attempt to initiate a connection but if no reply is received from + * the peer, we will then just wait passively for a valid LCP packet from the peer. + * + * Default is false. + */ +#define ppp_set_passive(ppp, boolval) (ppp->lcp_wantoptions.passive = boolval) + +/* + * If set, we will not transmit LCP packets to initiate a connection until a valid + * LCP packet is received from the peer. This is what we usually call the server mode. + * + * Default is false. + */ +#define ppp_set_silent(ppp, boolval) (ppp->lcp_wantoptions.silent = boolval) + +/* + * If set, enable protocol field compression negotiation in both the receive and + * the transmit direction. + * + * Default is true. + */ +#define ppp_set_neg_pcomp(ppp, boolval) (ppp->lcp_wantoptions.neg_pcompression = \ + ppp->lcp_allowoptions.neg_pcompression = boolval) + +/* + * If set, enable Address/Control compression in both the receive and the transmit + * direction. + * + * Default is true. + */ +#define ppp_set_neg_accomp(ppp, boolval) (ppp->lcp_wantoptions.neg_accompression = \ + ppp->lcp_allowoptions.neg_accompression = boolval) + +/* + * If set, enable asyncmap negotiation. Otherwise forcing all control characters to + * be escaped for both the transmit and the receive direction. + * + * Default is true. + */ +#define ppp_set_neg_asyncmap(ppp, boolval) (ppp->lcp_wantoptions.neg_asyncmap = \ + ppp->lcp_allowoptions.neg_asyncmap = boolval) + +/* + * This option sets the Async-Control-Character-Map (ACCM) for this end of the link. + * The ACCM is a set of 32 bits, one for each of the ASCII control characters with + * values from 0 to 31, where a 1 bit indicates that the corresponding control + * character should not be used in PPP packets sent to this system. The map is + * an unsigned 32 bits integer where the least significant bit (00000001) represents + * character 0 and the most significant bit (80000000) represents character 31. + * We will then ask the peer to send these characters as a 2-byte escape sequence. + * + * Default is 0. + */ +#define ppp_set_asyncmap(ppp, intval) (ppp->lcp_wantoptions.asyncmap = intval) + +/* + * Set a PPP interface as the default network interface + * (used to output all packets for which no specific route is found). + */ +#define ppp_set_default(ppp) netif_set_default(ppp->netif) + +#if PPP_NOTIFY_PHASE +/* + * Set a PPP notify phase callback. + * + * This can be used for example to set a LED pattern depending on the + * current phase of the PPP session. + */ +typedef void (*ppp_notify_phase_cb_fn)(ppp_pcb *pcb, u8_t phase, void *ctx); +void ppp_set_notify_phase_callback(ppp_pcb *pcb, ppp_notify_phase_cb_fn notify_phase_cb); +#endif /* PPP_NOTIFY_PHASE */ + +/* + * Initiate a PPP connection. + * + * This can only be called if PPP is in the dead phase. + * + * Holdoff is the time to wait (in seconds) before initiating + * the connection. + * + * If this port connects to a modem, the modem connection must be + * established before calling this. + */ +err_t ppp_connect(ppp_pcb *pcb, u16_t holdoff); + +#if PPP_SERVER +/* + * Listen for an incoming PPP connection. + * + * This can only be called if PPP is in the dead phase. + * + * If this port connects to a modem, the modem connection must be + * established before calling this. + */ +err_t ppp_listen(ppp_pcb *pcb); +#endif /* PPP_SERVER */ + +/* + * Initiate the end of a PPP connection. + * Any outstanding packets in the queues are dropped. + * + * Setting nocarrier to 1 close the PPP connection without initiating the + * shutdown procedure. Always using nocarrier = 0 is still recommended, + * this is going to take a little longer time if your link is down, but + * is a safer choice for the PPP state machine. + * + * Return 0 on success, an error code on failure. + */ +err_t ppp_close(ppp_pcb *pcb, u8_t nocarrier); + +/* + * Release the control block. + * + * This can only be called if PPP is in the dead phase. + * + * You must use ppp_close() before if you wish to terminate + * an established PPP session. + * + * Return 0 on success, an error code on failure. + */ +err_t ppp_free(ppp_pcb *pcb); + +/* + * PPP IOCTL commands. + * + * Get the up status - 0 for down, non-zero for up. The argument must + * point to an int. + */ +#define PPPCTLG_UPSTATUS 0 + +/* + * Get the PPP error code. The argument must point to an int. + * Returns a PPPERR_* value. + */ +#define PPPCTLG_ERRCODE 1 + +/* + * Get the fd associated with a PPP over serial + */ +#define PPPCTLG_FD 2 + +/* + * Get and set parameters for the given connection. + * Return 0 on success, an error code on failure. + */ +err_t ppp_ioctl(ppp_pcb *pcb, u8_t cmd, void *arg); + +/* Get the PPP netif interface */ +#define ppp_netif(ppp) (ppp->netif) + +/* Set an lwIP-style status-callback for the selected PPP device */ +#define ppp_set_netif_statuscallback(ppp, status_cb) \ + netif_set_status_callback(ppp->netif, status_cb); + +/* Set an lwIP-style link-callback for the selected PPP device */ +#define ppp_set_netif_linkcallback(ppp, link_cb) \ + netif_set_link_callback(ppp->netif, link_cb); + +#ifdef __cplusplus +} +#endif + +#endif /* PPP_H */ + +#endif /* PPP_SUPPORT */ diff --git a/tools/sdk/lwip2/include/netif/ppp/ppp_opts.h b/tools/sdk/lwip2/include/netif/ppp/ppp_opts.h new file mode 100644 index 0000000000..6702bec618 --- /dev/null +++ b/tools/sdk/lwip2/include/netif/ppp/ppp_opts.h @@ -0,0 +1,610 @@ +/* + * 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. + * + */ + +#ifndef LWIP_PPP_OPTS_H +#define LWIP_PPP_OPTS_H + +#include "lwip/opt.h" + +/** + * PPP_SUPPORT==1: Enable PPP. + */ +#ifndef PPP_SUPPORT +#define PPP_SUPPORT 0 +#endif + +/** + * PPPOE_SUPPORT==1: Enable PPP Over Ethernet + */ +#ifndef PPPOE_SUPPORT +#define PPPOE_SUPPORT 0 +#endif + +/** + * PPPOL2TP_SUPPORT==1: Enable PPP Over L2TP + */ +#ifndef PPPOL2TP_SUPPORT +#define PPPOL2TP_SUPPORT 0 +#endif + +/** + * PPPOL2TP_AUTH_SUPPORT==1: Enable PPP Over L2TP Auth (enable MD5 support) + */ +#ifndef PPPOL2TP_AUTH_SUPPORT +#define PPPOL2TP_AUTH_SUPPORT PPPOL2TP_SUPPORT +#endif + +/** + * PPPOS_SUPPORT==1: Enable PPP Over Serial + */ +#ifndef PPPOS_SUPPORT +#define PPPOS_SUPPORT PPP_SUPPORT +#endif + +/** + * LWIP_PPP_API==1: Enable PPP API (in pppapi.c) + */ +#ifndef LWIP_PPP_API +#define LWIP_PPP_API (PPP_SUPPORT && (NO_SYS == 0)) +#endif + +#if PPP_SUPPORT + +/** + * MEMP_NUM_PPP_PCB: the number of simultaneously active PPP + * connections (requires the PPP_SUPPORT option) + */ +#ifndef MEMP_NUM_PPP_PCB +#define MEMP_NUM_PPP_PCB 1 +#endif + +/** + * PPP_NUM_TIMEOUTS_PER_PCB: the number of sys_timeouts running in parallel per + * ppp_pcb. See the detailed explanation at the end of ppp_impl.h about simultaneous + * timers analysis. + */ +#ifndef PPP_NUM_TIMEOUTS_PER_PCB +#define PPP_NUM_TIMEOUTS_PER_PCB (1 + PPP_IPV4_SUPPORT + PPP_IPV6_SUPPORT + CCP_SUPPORT) +#endif + +/* The number of sys_timeouts required for the PPP module */ +#define PPP_NUM_TIMEOUTS (PPP_SUPPORT * PPP_NUM_TIMEOUTS_PER_PCB * MEMP_NUM_PPP_PCB) + +/** + * MEMP_NUM_PPPOS_INTERFACES: the number of concurrently active PPPoS + * interfaces (only used with PPPOS_SUPPORT==1) + */ +#ifndef MEMP_NUM_PPPOS_INTERFACES +#define MEMP_NUM_PPPOS_INTERFACES MEMP_NUM_PPP_PCB +#endif + +/** + * MEMP_NUM_PPPOE_INTERFACES: the number of concurrently active PPPoE + * interfaces (only used with PPPOE_SUPPORT==1) + */ +#ifndef MEMP_NUM_PPPOE_INTERFACES +#define MEMP_NUM_PPPOE_INTERFACES 1 +#endif + +/** + * MEMP_NUM_PPPOL2TP_INTERFACES: the number of concurrently active PPPoL2TP + * interfaces (only used with PPPOL2TP_SUPPORT==1) + */ +#ifndef MEMP_NUM_PPPOL2TP_INTERFACES +#define MEMP_NUM_PPPOL2TP_INTERFACES 1 +#endif + +/** + * MEMP_NUM_PPP_API_MSG: Number of concurrent PPP API messages (in pppapi.c) + */ +#ifndef MEMP_NUM_PPP_API_MSG +#define MEMP_NUM_PPP_API_MSG 5 +#endif + +/** + * PPP_DEBUG: Enable debugging for PPP. + */ +#ifndef PPP_DEBUG +#define PPP_DEBUG LWIP_DBG_OFF +#endif + +/** + * PPP_INPROC_IRQ_SAFE==1 call pppos_input() using tcpip_callback(). + * + * Please read the "PPPoS input path" chapter in the PPP documentation about this option. + */ +#ifndef PPP_INPROC_IRQ_SAFE +#define PPP_INPROC_IRQ_SAFE 0 +#endif + +/** + * PRINTPKT_SUPPORT==1: Enable PPP print packet support + * + * Mandatory for debugging, it displays exchanged packet content in debug trace. + */ +#ifndef PRINTPKT_SUPPORT +#define PRINTPKT_SUPPORT 0 +#endif + +/** + * PPP_IPV4_SUPPORT==1: Enable PPP IPv4 support + */ +#ifndef PPP_IPV4_SUPPORT +#define PPP_IPV4_SUPPORT (LWIP_IPV4) +#endif + +/** + * PPP_IPV6_SUPPORT==1: Enable PPP IPv6 support + */ +#ifndef PPP_IPV6_SUPPORT +#define PPP_IPV6_SUPPORT (LWIP_IPV6) +#endif + +/** + * PPP_NOTIFY_PHASE==1: Support PPP notify phase support + * + * PPP notify phase support allows you to set a callback which is + * called on change of the internal PPP state machine. + * + * This can be used for example to set a LED pattern depending on the + * current phase of the PPP session. + */ +#ifndef PPP_NOTIFY_PHASE +#define PPP_NOTIFY_PHASE 0 +#endif + +/** + * pbuf_type PPP is using for LCP, PAP, CHAP, EAP, CCP, IPCP and IP6CP packets. + * + * Memory allocated must be single buffered for PPP to works, it requires pbuf + * that are not going to be chained when allocated. This requires setting + * PBUF_POOL_BUFSIZE to at least 512 bytes, which is quite huge for small systems. + * + * Setting PPP_USE_PBUF_RAM to 1 makes PPP use memory from heap where continuous + * buffers are required, allowing you to use a smaller PBUF_POOL_BUFSIZE. + */ +#ifndef PPP_USE_PBUF_RAM +#define PPP_USE_PBUF_RAM 0 +#endif + +/** + * PPP_FCS_TABLE: Keep a 256*2 byte table to speed up FCS calculation for PPPoS + */ +#ifndef PPP_FCS_TABLE +#define PPP_FCS_TABLE 1 +#endif + +/** + * PAP_SUPPORT==1: Support PAP. + */ +#ifndef PAP_SUPPORT +#define PAP_SUPPORT 0 +#endif + +/** + * CHAP_SUPPORT==1: Support CHAP. + */ +#ifndef CHAP_SUPPORT +#define CHAP_SUPPORT 0 +#endif + +/** + * MSCHAP_SUPPORT==1: Support MSCHAP. + */ +#ifndef MSCHAP_SUPPORT +#define MSCHAP_SUPPORT 0 +#endif +#if MSCHAP_SUPPORT +/* MSCHAP requires CHAP support */ +#undef CHAP_SUPPORT +#define CHAP_SUPPORT 1 +#endif /* MSCHAP_SUPPORT */ + +/** + * EAP_SUPPORT==1: Support EAP. + */ +#ifndef EAP_SUPPORT +#define EAP_SUPPORT 0 +#endif + +/** + * CCP_SUPPORT==1: Support CCP. + */ +#ifndef CCP_SUPPORT +#define CCP_SUPPORT 0 +#endif + +/** + * MPPE_SUPPORT==1: Support MPPE. + */ +#ifndef MPPE_SUPPORT +#define MPPE_SUPPORT 0 +#endif +#if MPPE_SUPPORT +/* MPPE requires CCP support */ +#undef CCP_SUPPORT +#define CCP_SUPPORT 1 +/* MPPE requires MSCHAP support */ +#undef MSCHAP_SUPPORT +#define MSCHAP_SUPPORT 1 +/* MSCHAP requires CHAP support */ +#undef CHAP_SUPPORT +#define CHAP_SUPPORT 1 +#endif /* MPPE_SUPPORT */ + +/** + * CBCP_SUPPORT==1: Support CBCP. CURRENTLY NOT SUPPORTED! DO NOT SET! + */ +#ifndef CBCP_SUPPORT +#define CBCP_SUPPORT 0 +#endif + +/** + * ECP_SUPPORT==1: Support ECP. CURRENTLY NOT SUPPORTED! DO NOT SET! + */ +#ifndef ECP_SUPPORT +#define ECP_SUPPORT 0 +#endif + +/** + * DEMAND_SUPPORT==1: Support dial on demand. CURRENTLY NOT SUPPORTED! DO NOT SET! + */ +#ifndef DEMAND_SUPPORT +#define DEMAND_SUPPORT 0 +#endif + +/** + * LQR_SUPPORT==1: Support Link Quality Report. Do nothing except exchanging some LCP packets. + */ +#ifndef LQR_SUPPORT +#define LQR_SUPPORT 0 +#endif + +/** + * PPP_SERVER==1: Enable PPP server support (waiting for incoming PPP session). + * + * Currently only supported for PPPoS. + */ +#ifndef PPP_SERVER +#define PPP_SERVER 0 +#endif + +#if PPP_SERVER +/* + * PPP_OUR_NAME: Our name for authentication purposes + */ +#ifndef PPP_OUR_NAME +#define PPP_OUR_NAME "lwIP" +#endif +#endif /* PPP_SERVER */ + +/** + * VJ_SUPPORT==1: Support VJ header compression. + */ +#ifndef VJ_SUPPORT +#define VJ_SUPPORT 1 +#endif +/* VJ compression is only supported for TCP over IPv4 over PPPoS. */ +#if !PPPOS_SUPPORT || !PPP_IPV4_SUPPORT || !LWIP_TCP +#undef VJ_SUPPORT +#define VJ_SUPPORT 0 +#endif /* !PPPOS_SUPPORT */ + +/** + * PPP_MD5_RANDM==1: Use MD5 for better randomness. + * Enabled by default if CHAP, EAP, or L2TP AUTH support is enabled. + */ +#ifndef PPP_MD5_RANDM +#define PPP_MD5_RANDM (CHAP_SUPPORT || EAP_SUPPORT || PPPOL2TP_AUTH_SUPPORT) +#endif + +/** + * PolarSSL embedded library + * + * + * lwIP contains some files fetched from the latest BSD release of + * the PolarSSL project (PolarSSL 0.10.1-bsd) for ciphers and encryption + * methods we need for lwIP PPP support. + * + * The PolarSSL files were cleaned to contain only the necessary struct + * fields and functions needed for lwIP. + * + * The PolarSSL API was not changed at all, so if you are already using + * PolarSSL you can choose to skip the compilation of the included PolarSSL + * library into lwIP. + * + * If you are not using the embedded copy you must include external + * libraries into your arch/cc.h port file. + * + * Beware of the stack requirements which can be a lot larger if you are not + * using our cleaned PolarSSL library. + */ + +/** + * LWIP_USE_EXTERNAL_POLARSSL: Use external PolarSSL library + */ +#ifndef LWIP_USE_EXTERNAL_POLARSSL +#define LWIP_USE_EXTERNAL_POLARSSL 0 +#endif + +/** + * LWIP_USE_EXTERNAL_MBEDTLS: Use external mbed TLS library + */ +#ifndef LWIP_USE_EXTERNAL_MBEDTLS +#define LWIP_USE_EXTERNAL_MBEDTLS 0 +#endif + +/* + * PPP Timeouts + */ + +/** + * FSM_DEFTIMEOUT: Timeout time in seconds + */ +#ifndef FSM_DEFTIMEOUT +#define FSM_DEFTIMEOUT 6 +#endif + +/** + * FSM_DEFMAXTERMREQS: Maximum Terminate-Request transmissions + */ +#ifndef FSM_DEFMAXTERMREQS +#define FSM_DEFMAXTERMREQS 2 +#endif + +/** + * FSM_DEFMAXCONFREQS: Maximum Configure-Request transmissions + */ +#ifndef FSM_DEFMAXCONFREQS +#define FSM_DEFMAXCONFREQS 10 +#endif + +/** + * FSM_DEFMAXNAKLOOPS: Maximum number of nak loops + */ +#ifndef FSM_DEFMAXNAKLOOPS +#define FSM_DEFMAXNAKLOOPS 5 +#endif + +/** + * UPAP_DEFTIMEOUT: Timeout (seconds) for retransmitting req + */ +#ifndef UPAP_DEFTIMEOUT +#define UPAP_DEFTIMEOUT 6 +#endif + +/** + * UPAP_DEFTRANSMITS: Maximum number of auth-reqs to send + */ +#ifndef UPAP_DEFTRANSMITS +#define UPAP_DEFTRANSMITS 10 +#endif + +#if PPP_SERVER +/** + * UPAP_DEFREQTIME: Time to wait for auth-req from peer + */ +#ifndef UPAP_DEFREQTIME +#define UPAP_DEFREQTIME 30 +#endif +#endif /* PPP_SERVER */ + +/** + * CHAP_DEFTIMEOUT: Timeout (seconds) for retransmitting req + */ +#ifndef CHAP_DEFTIMEOUT +#define CHAP_DEFTIMEOUT 6 +#endif + +/** + * CHAP_DEFTRANSMITS: max # times to send challenge + */ +#ifndef CHAP_DEFTRANSMITS +#define CHAP_DEFTRANSMITS 10 +#endif + +#if PPP_SERVER +/** + * CHAP_DEFRECHALLENGETIME: If this option is > 0, rechallenge the peer every n seconds + */ +#ifndef CHAP_DEFRECHALLENGETIME +#define CHAP_DEFRECHALLENGETIME 0 +#endif +#endif /* PPP_SERVER */ + +/** + * EAP_DEFREQTIME: Time to wait for peer request + */ +#ifndef EAP_DEFREQTIME +#define EAP_DEFREQTIME 6 +#endif + +/** + * EAP_DEFALLOWREQ: max # times to accept requests + */ +#ifndef EAP_DEFALLOWREQ +#define EAP_DEFALLOWREQ 10 +#endif + +#if PPP_SERVER +/** + * EAP_DEFTIMEOUT: Timeout (seconds) for rexmit + */ +#ifndef EAP_DEFTIMEOUT +#define EAP_DEFTIMEOUT 6 +#endif + +/** + * EAP_DEFTRANSMITS: max # times to transmit + */ +#ifndef EAP_DEFTRANSMITS +#define EAP_DEFTRANSMITS 10 +#endif +#endif /* PPP_SERVER */ + +/** + * LCP_DEFLOOPBACKFAIL: Default number of times we receive our magic number from the peer + * before deciding the link is looped-back. + */ +#ifndef LCP_DEFLOOPBACKFAIL +#define LCP_DEFLOOPBACKFAIL 10 +#endif + +/** + * LCP_ECHOINTERVAL: Interval in seconds between keepalive echo requests, 0 to disable. + */ +#ifndef LCP_ECHOINTERVAL +#define LCP_ECHOINTERVAL 0 +#endif + +/** + * LCP_MAXECHOFAILS: Number of unanswered echo requests before failure. + */ +#ifndef LCP_MAXECHOFAILS +#define LCP_MAXECHOFAILS 3 +#endif + +/** + * PPP_MAXIDLEFLAG: Max Xmit idle time (in ms) before resend flag char. + */ +#ifndef PPP_MAXIDLEFLAG +#define PPP_MAXIDLEFLAG 100 +#endif + +/** + * PPP Packet sizes + */ + +/** + * PPP_MRU: Default MRU + */ +#ifndef PPP_MRU +#define PPP_MRU 1500 +#endif + +/** + * PPP_DEFMRU: Default MRU to try + */ +#ifndef PPP_DEFMRU +#define PPP_DEFMRU 1500 +#endif + +/** + * PPP_MAXMRU: Normally limit MRU to this (pppd default = 16384) + */ +#ifndef PPP_MAXMRU +#define PPP_MAXMRU 1500 +#endif + +/** + * PPP_MINMRU: No MRUs below this + */ +#ifndef PPP_MINMRU +#define PPP_MINMRU 128 +#endif + +/** + * PPPOL2TP_DEFMRU: Default MTU and MRU for L2TP + * Default = 1500 - PPPoE(6) - PPP Protocol(2) - IPv4 header(20) - UDP Header(8) + * - L2TP Header(6) - HDLC Header(2) - PPP Protocol(2) - MPPE Header(2) - PPP Protocol(2) + */ +#if PPPOL2TP_SUPPORT +#ifndef PPPOL2TP_DEFMRU +#define PPPOL2TP_DEFMRU 1450 +#endif +#endif /* PPPOL2TP_SUPPORT */ + +/** + * MAXNAMELEN: max length of hostname or name for auth + */ +#ifndef MAXNAMELEN +#define MAXNAMELEN 256 +#endif + +/** + * MAXSECRETLEN: max length of password or secret + */ +#ifndef MAXSECRETLEN +#define MAXSECRETLEN 256 +#endif + +/* ------------------------------------------------------------------------- */ + +/* + * Build triggers for embedded PolarSSL + */ +#if !LWIP_USE_EXTERNAL_POLARSSL && !LWIP_USE_EXTERNAL_MBEDTLS + +/* CHAP, EAP, L2TP AUTH and MD5 Random require MD5 support */ +#if CHAP_SUPPORT || EAP_SUPPORT || PPPOL2TP_AUTH_SUPPORT || PPP_MD5_RANDM +#define LWIP_INCLUDED_POLARSSL_MD5 1 +#endif /* CHAP_SUPPORT || EAP_SUPPORT || PPPOL2TP_AUTH_SUPPORT || PPP_MD5_RANDM */ + +#if MSCHAP_SUPPORT + +/* MSCHAP require MD4 support */ +#define LWIP_INCLUDED_POLARSSL_MD4 1 +/* MSCHAP require SHA1 support */ +#define LWIP_INCLUDED_POLARSSL_SHA1 1 +/* MSCHAP require DES support */ +#define LWIP_INCLUDED_POLARSSL_DES 1 + +/* MS-CHAP support is required for MPPE */ +#if MPPE_SUPPORT +/* MPPE require ARC4 support */ +#define LWIP_INCLUDED_POLARSSL_ARC4 1 +#endif /* MPPE_SUPPORT */ + +#endif /* MSCHAP_SUPPORT */ + +#endif /* !LWIP_USE_EXTERNAL_POLARSSL && !LWIP_USE_EXTERNAL_MBEDTLS */ + +/* Default value if unset */ +#ifndef LWIP_INCLUDED_POLARSSL_MD4 +#define LWIP_INCLUDED_POLARSSL_MD4 0 +#endif /* LWIP_INCLUDED_POLARSSL_MD4 */ +#ifndef LWIP_INCLUDED_POLARSSL_MD5 +#define LWIP_INCLUDED_POLARSSL_MD5 0 +#endif /* LWIP_INCLUDED_POLARSSL_MD5 */ +#ifndef LWIP_INCLUDED_POLARSSL_SHA1 +#define LWIP_INCLUDED_POLARSSL_SHA1 0 +#endif /* LWIP_INCLUDED_POLARSSL_SHA1 */ +#ifndef LWIP_INCLUDED_POLARSSL_DES +#define LWIP_INCLUDED_POLARSSL_DES 0 +#endif /* LWIP_INCLUDED_POLARSSL_DES */ +#ifndef LWIP_INCLUDED_POLARSSL_ARC4 +#define LWIP_INCLUDED_POLARSSL_ARC4 0 +#endif /* LWIP_INCLUDED_POLARSSL_ARC4 */ + +#endif /* PPP_SUPPORT */ + +/* Default value if unset */ +#ifndef PPP_NUM_TIMEOUTS +#define PPP_NUM_TIMEOUTS 0 +#endif /* PPP_NUM_TIMEOUTS */ + +#endif /* LWIP_PPP_OPTS_H */ diff --git a/tools/sdk/lwip2/include/netif/ppp/pppos.h b/tools/sdk/lwip2/include/netif/ppp/pppos.h new file mode 100644 index 0000000000..380a965ce6 --- /dev/null +++ b/tools/sdk/lwip2/include/netif/ppp/pppos.h @@ -0,0 +1,126 @@ +/** + * @file + * Network Point to Point Protocol over Serial header file. + * + */ + +/* + * 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. + * + */ + +#include "netif/ppp/ppp_opts.h" +#if PPP_SUPPORT && PPPOS_SUPPORT /* don't build if not configured for use in lwipopts.h */ + +#ifndef PPPOS_H +#define PPPOS_H + +#include "lwip/sys.h" + +#include "ppp.h" +#include "vj.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* PPP packet parser states. Current state indicates operation yet to be + * completed. */ +enum { + PDIDLE = 0, /* Idle state - waiting. */ + PDSTART, /* Process start flag. */ + PDADDRESS, /* Process address field. */ + PDCONTROL, /* Process control field. */ + PDPROTOCOL1, /* Process protocol field 1. */ + PDPROTOCOL2, /* Process protocol field 2. */ + PDDATA /* Process data byte. */ +}; + +/* PPPoS serial output callback function prototype */ +typedef u32_t (*pppos_output_cb_fn)(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx); + +/* + * Extended asyncmap - allows any character to be escaped. + */ +typedef u8_t ext_accm[32]; + +/* + * PPPoS interface control block. + */ +typedef struct pppos_pcb_s pppos_pcb; +struct pppos_pcb_s { + /* -- below are data that will NOT be cleared between two sessions */ + ppp_pcb *ppp; /* PPP PCB */ + pppos_output_cb_fn output_cb; /* PPP serial output callback */ + + /* -- below are data that will be cleared between two sessions + * + * last_xmit must be the first member of cleared members, because it is + * used to know which part must not be cleared. + */ + u32_t last_xmit; /* Time of last transmission. */ + ext_accm out_accm; /* Async-Ctl-Char-Map for output. */ + + /* flags */ + unsigned int open :1; /* Set if PPPoS is open */ + unsigned int pcomp :1; /* Does peer accept protocol compression? */ + unsigned int accomp :1; /* Does peer accept addr/ctl compression? */ + + /* PPPoS rx */ + ext_accm in_accm; /* Async-Ctl-Char-Map for input. */ + struct pbuf *in_head, *in_tail; /* The input packet. */ + u16_t in_protocol; /* The input protocol code. */ + u16_t in_fcs; /* Input Frame Check Sequence value. */ + u8_t in_state; /* The input process state. */ + u8_t in_escaped; /* Escape next character. */ +}; + +/* Create a new PPPoS session. */ +ppp_pcb *pppos_create(struct netif *pppif, pppos_output_cb_fn output_cb, + ppp_link_status_cb_fn link_status_cb, void *ctx_cb); + +#if !NO_SYS && !PPP_INPROC_IRQ_SAFE +/* Pass received raw characters to PPPoS to be decoded through lwIP TCPIP thread. */ +err_t pppos_input_tcpip(ppp_pcb *ppp, u8_t *s, int l); +#endif /* !NO_SYS && !PPP_INPROC_IRQ_SAFE */ + +/* PPP over Serial: this is the input function to be called for received data. */ +void pppos_input(ppp_pcb *ppp, u8_t* data, int len); + + +/* + * Functions called from lwIP + * DO NOT CALL FROM lwIP USER APPLICATION. + */ +#if !NO_SYS && !PPP_INPROC_IRQ_SAFE +err_t pppos_input_sys(struct pbuf *p, struct netif *inp); +#endif /* !NO_SYS && !PPP_INPROC_IRQ_SAFE */ + +#ifdef __cplusplus +} +#endif + +#endif /* PPPOS_H */ +#endif /* PPP_SUPPORT && PPPOL2TP_SUPPORT */ diff --git a/tools/sdk/lwip2/include/netif/ppp/vj.h b/tools/sdk/lwip2/include/netif/ppp/vj.h new file mode 100644 index 0000000000..77d9976c52 --- /dev/null +++ b/tools/sdk/lwip2/include/netif/ppp/vj.h @@ -0,0 +1,169 @@ +/* + * Definitions for tcp compression routines. + * + * $Id: vj.h,v 1.7 2010/02/22 17:52:09 goldsimon Exp $ + * + * Copyright (c) 1989 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + * + * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989: + * - Initial distribution. + */ + +#include "netif/ppp/ppp_opts.h" +#if PPP_SUPPORT && VJ_SUPPORT /* don't build if not configured for use in lwipopts.h */ + +#ifndef VJ_H +#define VJ_H + +#include "lwip/ip.h" +#include "lwip/priv/tcp_priv.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define MAX_SLOTS 16 /* must be > 2 and < 256 */ +#define MAX_HDR 128 + +/* + * Compressed packet format: + * + * The first octet contains the packet type (top 3 bits), TCP + * 'push' bit, and flags that indicate which of the 4 TCP sequence + * numbers have changed (bottom 5 bits). The next octet is a + * conversation number that associates a saved IP/TCP header with + * the compressed packet. The next two octets are the TCP checksum + * from the original datagram. The next 0 to 15 octets are + * sequence number changes, one change per bit set in the header + * (there may be no changes and there are two special cases where + * the receiver implicitly knows what changed -- see below). + * + * There are 5 numbers which can change (they are always inserted + * in the following order): TCP urgent pointer, window, + * acknowlegement, sequence number and IP ID. (The urgent pointer + * is different from the others in that its value is sent, not the + * change in value.) Since typical use of SLIP links is biased + * toward small packets (see comments on MTU/MSS below), changes + * use a variable length coding with one octet for numbers in the + * range 1 - 255 and 3 octets (0, MSB, LSB) for numbers in the + * range 256 - 65535 or 0. (If the change in sequence number or + * ack is more than 65535, an uncompressed packet is sent.) + */ + +/* + * Packet types (must not conflict with IP protocol version) + * + * The top nibble of the first octet is the packet type. There are + * three possible types: IP (not proto TCP or tcp with one of the + * control flags set); uncompressed TCP (a normal IP/TCP packet but + * with the 8-bit protocol field replaced by an 8-bit connection id -- + * this type of packet syncs the sender & receiver); and compressed + * TCP (described above). + * + * LSB of 4-bit field is TCP "PUSH" bit (a worthless anachronism) and + * is logically part of the 4-bit "changes" field that follows. Top + * three bits are actual packet type. For backward compatibility + * and in the interest of conserving bits, numbers are chosen so the + * IP protocol version number (4) which normally appears in this nibble + * means "IP packet". + */ + +/* packet types */ +#define TYPE_IP 0x40 +#define TYPE_UNCOMPRESSED_TCP 0x70 +#define TYPE_COMPRESSED_TCP 0x80 +#define TYPE_ERROR 0x00 + +/* Bits in first octet of compressed packet */ +#define NEW_C 0x40 /* flag bits for what changed in a packet */ +#define NEW_I 0x20 +#define NEW_S 0x08 +#define NEW_A 0x04 +#define NEW_W 0x02 +#define NEW_U 0x01 + +/* reserved, special-case values of above */ +#define SPECIAL_I (NEW_S|NEW_W|NEW_U) /* echoed interactive traffic */ +#define SPECIAL_D (NEW_S|NEW_A|NEW_W|NEW_U) /* unidirectional data */ +#define SPECIALS_MASK (NEW_S|NEW_A|NEW_W|NEW_U) + +#define TCP_PUSH_BIT 0x10 + + +/* + * "state" data for each active tcp conversation on the wire. This is + * basically a copy of the entire IP/TCP header from the last packet + * we saw from the conversation together with a small identifier + * the transmit & receive ends of the line use to locate saved header. + */ +struct cstate { + struct cstate *cs_next; /* next most recently used state (xmit only) */ + u16_t cs_hlen; /* size of hdr (receive only) */ + u8_t cs_id; /* connection # associated with this state */ + u8_t cs_filler; + union { + char csu_hdr[MAX_HDR]; + struct ip_hdr csu_ip; /* ip/tcp hdr from most recent packet */ + } vjcs_u; +}; +#define cs_ip vjcs_u.csu_ip +#define cs_hdr vjcs_u.csu_hdr + + +struct vjstat { + u32_t vjs_packets; /* outbound packets */ + u32_t vjs_compressed; /* outbound compressed packets */ + u32_t vjs_searches; /* searches for connection state */ + u32_t vjs_misses; /* times couldn't find conn. state */ + u32_t vjs_uncompressedin; /* inbound uncompressed packets */ + u32_t vjs_compressedin; /* inbound compressed packets */ + u32_t vjs_errorin; /* inbound unknown type packets */ + u32_t vjs_tossed; /* inbound packets tossed because of error */ +}; + +/* + * all the state data for one serial line (we need one of these per line). + */ +struct vjcompress { + struct cstate *last_cs; /* most recently used tstate */ + u8_t last_recv; /* last rcvd conn. id */ + u8_t last_xmit; /* last sent conn. id */ + u16_t flags; + u8_t maxSlotIndex; + u8_t compressSlot; /* Flag indicating OK to compress slot ID. */ +#if LINK_STATS + struct vjstat stats; +#endif + struct cstate tstate[MAX_SLOTS]; /* xmit connection states */ + struct cstate rstate[MAX_SLOTS]; /* receive connection states */ +}; + +/* flag values */ +#define VJF_TOSS 1U /* tossing rcvd frames because of input err */ + +extern void vj_compress_init (struct vjcompress *comp); +extern u8_t vj_compress_tcp (struct vjcompress *comp, struct pbuf **pb); +extern void vj_uncompress_err (struct vjcompress *comp); +extern int vj_uncompress_uncomp(struct pbuf *nb, struct vjcompress *comp); +extern int vj_uncompress_tcp (struct pbuf **nb, struct vjcompress *comp); + +#ifdef __cplusplus +} +#endif + +#endif /* VJ_H */ + +#endif /* PPP_SUPPORT && VJ_SUPPORT */