diff --git a/VERSION b/VERSION deleted file mode 100644 index 21a43345..00000000 --- a/VERSION +++ /dev/null @@ -1,22 +0,0 @@ -gwen: - crypto: 9ec59b5 - espnow: 79c549f - main: e271380 - minic: 9ec59b5 - net80211: 52b3b54 - pp: 1155a0b - pwm: 9ec59b5 - smartconfig:9ec59b5 - wpa: 52b3b54 - wps: ff84a8b - -gitlab: - driver: 7bee5263 - espconn: 3a998034 - freertos: a9985a9c - lwip: 1651e055 - mbedtls: 1ac9f1f4 - mqtt: 6c098065 - nopoll: 31f0ea07 - openssl: 1669353f - ssl: eefb383a \ No newline at end of file diff --git a/components/esp8266/lib/VERSION b/components/esp8266/lib/VERSION new file mode 100644 index 00000000..70ad465a --- /dev/null +++ b/components/esp8266/lib/VERSION @@ -0,0 +1,11 @@ +gwen: + crypto: 21b4121 + espnow: 21b4121 + main: 21b4121 + minic: 21b4121 + net80211: 21b4121 + pp: 21b4121 + pwm: 21b4121 + smartconfig:9ec59b5 + wpa: 21b4121 + wps: 21b4121 \ No newline at end of file diff --git a/components/esp8266/lib/libcore.a b/components/esp8266/lib/libcore.a index 142fd0ec..22bb7707 100644 Binary files a/components/esp8266/lib/libcore.a and b/components/esp8266/lib/libcore.a differ diff --git a/components/esp8266/lib/libcrypto.a b/components/esp8266/lib/libcrypto.a index e8a40978..3478873a 100644 Binary files a/components/esp8266/lib/libcrypto.a and b/components/esp8266/lib/libcrypto.a differ diff --git a/components/esp8266/lib/libespnow.a b/components/esp8266/lib/libespnow.a index 8314a127..9d56380e 100644 Binary files a/components/esp8266/lib/libespnow.a and b/components/esp8266/lib/libespnow.a differ diff --git a/components/esp8266/lib/libminic.a b/components/esp8266/lib/libminic.a index d2568f4f..1a00eb6d 100644 Binary files a/components/esp8266/lib/libminic.a and b/components/esp8266/lib/libminic.a differ diff --git a/components/esp8266/lib/libnet80211.a b/components/esp8266/lib/libnet80211.a index 45e9f8fd..8fa1ab86 100644 Binary files a/components/esp8266/lib/libnet80211.a and b/components/esp8266/lib/libnet80211.a differ diff --git a/components/esp8266/lib/libpp.a b/components/esp8266/lib/libpp.a index 93db2368..a8dd27d0 100644 Binary files a/components/esp8266/lib/libpp.a and b/components/esp8266/lib/libpp.a differ diff --git a/components/esp8266/lib/libpwm.a b/components/esp8266/lib/libpwm.a index 9f21de0c..5e705533 100644 Binary files a/components/esp8266/lib/libpwm.a and b/components/esp8266/lib/libpwm.a differ diff --git a/components/esp8266/lib/libwpa.a b/components/esp8266/lib/libwpa.a index 72ad0137..d3376f42 100644 Binary files a/components/esp8266/lib/libwpa.a and b/components/esp8266/lib/libwpa.a differ diff --git a/components/esp8266/lib/libwps.a b/components/esp8266/lib/libwps.a index 8bae1fb1..3ad3c698 100644 Binary files a/components/esp8266/lib/libwps.a and b/components/esp8266/lib/libwps.a differ diff --git a/components/lwip/include/lwip/lwip/dhcpserver.h b/components/lwip/include/lwip/lwip/dhcpserver.h index e07b5417..eb5ab5c6 100644 --- a/components/lwip/include/lwip/lwip/dhcpserver.h +++ b/components/lwip/include/lwip/lwip/dhcpserver.h @@ -3,49 +3,51 @@ #define USE_DNS -typedef struct dhcps_state{ - s16_t state; +#include "esp_wifi.h" + +typedef struct dhcps_state { + s16_t state; } dhcps_state; // ����dhcpclient�Զ����һ��DHCP msg�ṹ�� typedef struct dhcps_msg { - u8_t op, htype, hlen, hops; - u8_t xid[4]; - u16_t secs, flags; - u8_t ciaddr[4]; - u8_t yiaddr[4]; - u8_t siaddr[4]; - u8_t giaddr[4]; - u8_t chaddr[16]; - u8_t sname[64]; - u8_t file[128]; - u8_t options[312]; -}dhcps_msg; + u8_t op, htype, hlen, hops; + u8_t xid[4]; + u16_t secs, flags; + u8_t ciaddr[4]; + u8_t yiaddr[4]; + u8_t siaddr[4]; + u8_t giaddr[4]; + u8_t chaddr[16]; + u8_t sname[64]; + u8_t file[128]; + u8_t options[312]; +} dhcps_msg; #ifndef LWIP_OPEN_SRC struct dhcps_lease { - bool enable; - struct ip_addr start_ip; - struct ip_addr end_ip; + bool enable; + struct ip_addr start_ip; + struct ip_addr end_ip; }; -enum dhcps_offer_option{ - OFFER_START = 0x00, - OFFER_ROUTER = 0x01, - OFFER_END +enum dhcps_offer_option { + OFFER_START = 0x00, + OFFER_ROUTER = 0x01, + OFFER_END }; #endif -struct dhcps_pool{ - struct ip_addr ip; - u8_t mac[6]; - u32_t lease_timer; +struct dhcps_pool { + struct ip_addr ip; + u8_t mac[6]; + u32_t lease_timer; }; -typedef struct _list_node{ - void *pnode; - struct _list_node *pnext; -}list_node; +typedef struct _list_node { + void* pnode; + struct _list_node* pnext; +} list_node; extern u32_t dhcps_lease_time; #define DHCPS_COARSE_TIMER_SECS 1 @@ -53,7 +55,6 @@ extern u32_t dhcps_lease_time; #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 @@ -96,7 +97,7 @@ extern u32_t dhcps_lease_time; #define dhcps_router_enabled(offer) ((offer & OFFER_ROUTER) != 0) -void dhcps_start(struct ip_info *info); +void dhcps_start(struct ip_info* info); void dhcps_stop(void); #endif diff --git a/components/lwip/include/lwip/lwip/pbuf.h b/components/lwip/include/lwip/lwip/pbuf.h index e8448c73..7ea946dc 100644 --- a/components/lwip/include/lwip/lwip/pbuf.h +++ b/components/lwip/include/lwip/lwip/pbuf.h @@ -118,9 +118,6 @@ struct pbuf { * the stack itself, or pbuf->next pointers from a chain. */ u16_t ref; - - /* add a pointer for esf_buf */ - void * eb; }; #if LWIP_SUPPORT_CUSTOM_PBUF diff --git a/components/lwip/include/lwip/netif/if_llc.h b/components/lwip/include/lwip/netif/if_llc.h deleted file mode 100644 index ca09b386..00000000 --- a/components/lwip/include/lwip/netif/if_llc.h +++ /dev/null @@ -1,173 +0,0 @@ -/* $NetBSD: if_llc.h,v 1.12 1999/11/19 20:41:19 thorpej Exp $ */ - -/*- - * Copyright (c) 1988, 1993 - * The Regents of the University of California. 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. - * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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. - * - * @(#)if_llc.h 8.1 (Berkeley) 6/10/93 - * $FreeBSD$ - */ - -#ifndef _NET_IF_LLC_H_ -#define _NET_IF_LLC_H_ - -/* - * IEEE 802.2 Link Level Control headers, for use in conjunction with - * 802.{3,4,5} media access control methods. - * - * Headers here do not use bit fields due to shortcommings in many - * compilers. - */ - -struct llc { - uint8_t llc_dsap; - uint8_t llc_ssap; - union { - struct { - uint8_t control; - uint8_t format_id; - uint8_t class; - uint8_t window_x2; - } __packed type_u; - struct { - uint8_t num_snd_x2; - uint8_t num_rcv_x2; - } __packed type_i; - struct { - uint8_t control; - uint8_t num_rcv_x2; - } __packed type_s; - struct { - uint8_t control; - /* - * We cannot put the following fields in a structure because - * the structure rounding might cause padding. - */ - uint8_t frmr_rej_pdu0; - uint8_t frmr_rej_pdu1; - uint8_t frmr_control; - uint8_t frmr_control_ext; - uint8_t frmr_cause; - } __packed type_frmr; - struct { - uint8_t control; - uint8_t org_code[3]; - uint16_t ether_type; - } __packed type_snap; - struct { - uint8_t control; - uint8_t control_ext; - } __packed type_raw; - } __packed llc_un; -} __packed; - -struct frmrinfo { - uint8_t frmr_rej_pdu0; - uint8_t frmr_rej_pdu1; - uint8_t frmr_control; - uint8_t frmr_control_ext; - uint8_t frmr_cause; -} __packed; - -#define llc_control llc_un.type_u.control -#define llc_control_ext llc_un.type_raw.control_ext -#define llc_fid llc_un.type_u.format_id -#define llc_class llc_un.type_u.class -#define llc_window llc_un.type_u.window_x2 -#define llc_frmrinfo llc_un.type_frmr.frmr_rej_pdu0 -#define llc_frmr_pdu0 llc_un.type_frmr.frmr_rej_pdu0 -#define llc_frmr_pdu1 llc_un.type_frmr.frmr_rej_pdu1 -#define llc_frmr_control llc_un.type_frmr.frmr_control -#define llc_frmr_control_ext llc_un.type_frmr.frmr_control_ext -#define llc_frmr_cause llc_un.type_frmr.frmr_cause -#define llc_snap llc_un.type_snap - -/* - * Don't use sizeof(struct llc_un) for LLC header sizes - */ -#define LLC_ISFRAMELEN 4 -#define LLC_UFRAMELEN 3 -#define LLC_FRMRLEN 7 -#define LLC_SNAPFRAMELEN 8 - -#ifdef CTASSERT -CTASSERT(sizeof (struct llc) == LLC_SNAPFRAMELEN); -#endif - -/* - * Unnumbered LLC format commands - */ -#define LLC_UI 0x3 -#define LLC_UI_P 0x13 -#define LLC_DISC 0x43 -#define LLC_DISC_P 0x53 -#define LLC_UA 0x63 -#define LLC_UA_P 0x73 -#define LLC_TEST 0xe3 -#define LLC_TEST_P 0xf3 -#define LLC_FRMR 0x87 -#define LLC_FRMR_P 0x97 -#define LLC_DM 0x0f -#define LLC_DM_P 0x1f -#define LLC_XID 0xaf -#define LLC_XID_P 0xbf -#define LLC_SABME 0x6f -#define LLC_SABME_P 0x7f - -/* - * Supervisory LLC commands - */ -#define LLC_RR 0x01 -#define LLC_RNR 0x05 -#define LLC_REJ 0x09 - -/* - * Info format - dummy only - */ -#define LLC_INFO 0x00 - -/* - * ISO PDTR 10178 contains among others - */ -#define LLC_8021D_LSAP 0x42 -#define LLC_X25_LSAP 0x7e -#define LLC_SNAP_LSAP 0xaa -#define LLC_ISO_LSAP 0xfe - -#define RFC1042_LEN 6 -#define RFC1042 {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00} -#define ETHERNET_TUNNEL {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8} - -/* - * copied from sys/net/ethernet.h - */ -#define ETHERTYPE_AARP 0x80F3 /* AppleTalk AARP */ -#define ETHERTYPE_IPX 0x8137 /* Novell (old) NetWare IPX (ECONFIG E option) */ - - - -#endif /* _NET_IF_LLC_H_ */ diff --git a/components/lwip/include/lwip/netif/wlan_lwip_if.h b/components/lwip/include/lwip/netif/wlan_lwip_if.h deleted file mode 100644 index eee411ee..00000000 --- a/components/lwip/include/lwip/netif/wlan_lwip_if.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2010-2011 Espressif System - * -*/ - -#ifndef _WLAN_LWIP_IF_H_ -#define _WLAN_LWIP_IF_H_ - -#include "lwip/err.h" - -err_t ethernetif_init(struct netif *netif); -void ethernetif_input(struct netif *netif, struct pbuf *p); - -#ifndef IOT_SIP_MODE -sint8 ieee80211_output_pbuf(struct netif *ifp, struct pbuf* pb); -#else -sint8 ieee80211_output_pbuf(struct ieee80211_conn *conn, esf_buf *eb); -#endif - -#endif /* _WLAN_LWIP_IF_H_ */ diff --git a/components/lwip/source/core/dhcp.c b/components/lwip/source/core/dhcp.c index 77d6e93c..e8194dda 100644 --- a/components/lwip/source/core/dhcp.c +++ b/components/lwip/source/core/dhcp.c @@ -1130,9 +1130,6 @@ dhcp_bind(struct netif *netif) /* bring the interface up */ netif_set_up(netif); - // use old ip/mask/gw to check whether ip/mask/gw changed - system_station_got_ip_set(&ip, &mask, &gw); - /* netif is now bound to DHCP leased address */ dhcp_set_state(dhcp, DHCP_BOUND); } diff --git a/components/lwip/source/core/dhcpserver.c b/components/lwip/source/core/dhcpserver.c index 31ea2d06..9b809353 100644 --- a/components/lwip/source/core/dhcpserver.c +++ b/components/lwip/source/core/dhcpserver.c @@ -1,3 +1,17 @@ +// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "esp_common.h" #include "lwip/inet.h" #include "lwip/err.h" @@ -9,17 +23,18 @@ #ifndef LWIP_OPEN_SRC #include "net80211/ieee80211_var.h" #endif -#include "netif/wlan_lwip_if.h" #ifdef MEMLEAK_DEBUG static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__; #endif +extern struct netif *esp_netif[2]; + //////////////////////////////////////////////////////////////////////////////////// //static const uint8_t xid[4] = {0xad, 0xde, 0x12, 0x23}; //static u8_t old_xid[4] = {0}; static const u32_t magic_cookie ICACHE_RODATA_ATTR STORE_ATTR = 0x63538263; -static struct udp_pcb *pcb_dhcps = NULL; +static struct udp_pcb* pcb_dhcps = NULL; static struct ip_addr broadcast_dhcps; static struct ip_addr server_address; static struct ip_addr client_address;//added @@ -27,7 +42,7 @@ static struct ip_addr client_address_plus; static struct dhcps_lease dhcps_lease; //static bool dhcps_lease_flag = true; -static list_node *plist = NULL; +static list_node* plist = NULL; static u8_t offer = 0xFF; static bool renew = false; #define DHCPS_LEASE_TIME_DEF (120) @@ -38,37 +53,41 @@ u32_t dhcps_lease_time = DHCPS_LEASE_TIME_DEF; //minute * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ -void node_insert_to_list(list_node **phead, list_node* pinsert) +void node_insert_to_list(list_node** phead, list_node* pinsert) { - list_node *plist = NULL; - struct dhcps_pool *pdhcps_pool = NULL; - struct dhcps_pool *pdhcps_node = NULL; - if (*phead == NULL) - *phead = pinsert; - else { - plist = *phead; - pdhcps_node = pinsert->pnode; - pdhcps_pool = plist->pnode; + list_node* plist = NULL; + struct dhcps_pool* pdhcps_pool = NULL; + struct dhcps_pool* pdhcps_node = NULL; - if(pdhcps_node->ip.addr < pdhcps_pool->ip.addr) { - pinsert->pnext = plist; - *phead = pinsert; - } else { + if (*phead == NULL) { + *phead = pinsert; + } else { + plist = *phead; + pdhcps_node = pinsert->pnode; + pdhcps_pool = plist->pnode; + + if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) { + pinsert->pnext = plist; + *phead = pinsert; + } else { while (plist->pnext != NULL) { pdhcps_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 == NULL) { + if (plist->pnext == NULL) { plist->pnext = pinsert; } - } - } + } + } + // pinsert->pnext = NULL; } @@ -78,27 +97,29 @@ void node_insert_to_list(list_node **phead, list_node* pinsert) * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ -void node_remove_from_list(list_node **phead, list_node* pdelete) +void node_remove_from_list(list_node** phead, list_node* pdelete) { - list_node *plist = NULL; + list_node* plist = NULL; - plist = *phead; - if (plist == NULL){ - *phead = NULL; - } else { - if (plist == pdelete){ - *phead = plist->pnext; + plist = *phead; + + if (plist == NULL) { + *phead = NULL; + } else { + if (plist == pdelete) { + *phead = plist->pnext; pdelete->pnext = NULL; - } else { - while (plist != NULL) { - if (plist->pnext == pdelete){ - plist->pnext = pdelete->pnext; + } else { + while (plist != NULL) { + if (plist->pnext == pdelete) { + plist->pnext = pdelete->pnext; pdelete->pnext = NULL; - } - plist = plist->pnext; - } - } - } + } + + plist = plist->pnext; + } + } + } } /////////////////////////////////////////////////////////////////////////////////// /* @@ -110,13 +131,13 @@ void node_remove_from_list(list_node **phead, list_node* pdelete) * @return uint8_t* ����DHCP msgƫ�Ƶ�ַ */ /////////////////////////////////////////////////////////////////////////////////// -static u8_t* add_msg_type(u8_t *optptr, u8_t type) +static u8_t* add_msg_type(u8_t* optptr, u8_t type) { - *optptr++ = DHCP_OPTION_MSG_TYPE; - *optptr++ = 1; - *optptr++ = type; - return optptr; + *optptr++ = DHCP_OPTION_MSG_TYPE; + *optptr++ = 1; + *optptr++ = type; + return optptr; } /////////////////////////////////////////////////////////////////////////////////// /* @@ -127,105 +148,105 @@ static u8_t* add_msg_type(u8_t *optptr, u8_t type) * @return uint8_t* ����DHCP msgƫ�Ƶ�ַ */ /////////////////////////////////////////////////////////////////////////////////// -static u8_t* add_offer_options(u8_t *optptr) +static u8_t* add_offer_options(u8_t* optptr) { - struct ip_addr ipadd; + struct ip_addr ipadd; - ipadd.addr = *( (u32_t *) &server_address); + ipadd.addr = *((u32_t*) &server_address); #ifdef USE_CLASS_B_NET - *optptr++ = DHCP_OPTION_SUBNET_MASK; - *optptr++ = 4; //length - *optptr++ = 255; - *optptr++ = 240; - *optptr++ = 0; - *optptr++ = 0; + *optptr++ = DHCP_OPTION_SUBNET_MASK; + *optptr++ = 4; //length + *optptr++ = 255; + *optptr++ = 240; + *optptr++ = 0; + *optptr++ = 0; #else - *optptr++ = DHCP_OPTION_SUBNET_MASK; - *optptr++ = 4; - *optptr++ = 255; - *optptr++ = 255; - *optptr++ = 255; - *optptr++ = 0; + *optptr++ = DHCP_OPTION_SUBNET_MASK; + *optptr++ = 4; + *optptr++ = 255; + *optptr++ = 255; + *optptr++ = 255; + *optptr++ = 0; #endif - *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_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( &ipadd); - *optptr++ = ip4_addr2( &ipadd); - *optptr++ = ip4_addr3( &ipadd); - *optptr++ = ip4_addr4( &ipadd); + *optptr++ = DHCP_OPTION_SERVER_ID; + *optptr++ = 4; + *optptr++ = ip4_addr1(&ipadd); + *optptr++ = ip4_addr2(&ipadd); + *optptr++ = ip4_addr3(&ipadd); + *optptr++ = ip4_addr4(&ipadd); - if (dhcps_router_enabled(offer)){ - struct ip_info if_ip; - bzero(&if_ip, sizeof(struct ip_info)); - wifi_get_ip_info(SOFTAP_IF, &if_ip); + if (dhcps_router_enabled(offer)) { + struct ip_info if_ip; + bzero(&if_ip, sizeof(struct ip_info)); + wifi_get_ip_info(SOFTAP_IF, &if_ip); - *optptr++ = DHCP_OPTION_ROUTER; - *optptr++ = 4; - *optptr++ = ip4_addr1( &if_ip.gw); - *optptr++ = ip4_addr2( &if_ip.gw); - *optptr++ = ip4_addr3( &if_ip.gw); - *optptr++ = ip4_addr4( &if_ip.gw); - } + *optptr++ = DHCP_OPTION_ROUTER; + *optptr++ = 4; + *optptr++ = ip4_addr1(&if_ip.gw); + *optptr++ = ip4_addr2(&if_ip.gw); + *optptr++ = ip4_addr3(&if_ip.gw); + *optptr++ = ip4_addr4(&if_ip.gw); + } #ifdef USE_DNS - *optptr++ = DHCP_OPTION_DNS_SERVER; - *optptr++ = 4; - *optptr++ = ip4_addr1( &ipadd); - *optptr++ = ip4_addr2( &ipadd); - *optptr++ = ip4_addr3( &ipadd); - *optptr++ = ip4_addr4( &ipadd); + *optptr++ = DHCP_OPTION_DNS_SERVER; + *optptr++ = 4; + *optptr++ = ip4_addr1(&ipadd); + *optptr++ = ip4_addr2(&ipadd); + *optptr++ = ip4_addr3(&ipadd); + *optptr++ = ip4_addr4(&ipadd); #endif #ifdef CLASS_B_NET - *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; - *optptr++ = 4; - *optptr++ = ip4_addr1( &ipadd); - *optptr++ = 255; - *optptr++ = 255; - *optptr++ = 255; + *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; + *optptr++ = 4; + *optptr++ = ip4_addr1(&ipadd); + *optptr++ = 255; + *optptr++ = 255; + *optptr++ = 255; #else - *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; - *optptr++ = 4; - *optptr++ = ip4_addr1( &ipadd); - *optptr++ = ip4_addr2( &ipadd); - *optptr++ = ip4_addr3( &ipadd); - *optptr++ = 255; + *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; + *optptr++ = 4; + *optptr++ = ip4_addr1(&ipadd); + *optptr++ = ip4_addr2(&ipadd); + *optptr++ = ip4_addr3(&ipadd); + *optptr++ = 255; #endif - *optptr++ = DHCP_OPTION_INTERFACE_MTU; - *optptr++ = 2; + *optptr++ = DHCP_OPTION_INTERFACE_MTU; + *optptr++ = 2; #ifdef CLASS_B_NET - *optptr++ = 0x05; - *optptr++ = 0xdc; + *optptr++ = 0x05; + *optptr++ = 0xdc; #else - *optptr++ = 0x02; - *optptr++ = 0x40; + *optptr++ = 0x02; + *optptr++ = 0x40; #endif - *optptr++ = DHCP_OPTION_PERFORM_ROUTER_DISCOVERY; - *optptr++ = 1; - *optptr++ = 0x00; + *optptr++ = DHCP_OPTION_PERFORM_ROUTER_DISCOVERY; + *optptr++ = 1; + *optptr++ = 0x00; - *optptr++ = 43; - *optptr++ = 6; + *optptr++ = 43; + *optptr++ = 6; - *optptr++ = 0x01; - *optptr++ = 4; - *optptr++ = 0x00; - *optptr++ = 0x00; - *optptr++ = 0x00; - *optptr++ = 0x02; + *optptr++ = 0x01; + *optptr++ = 4; + *optptr++ = 0x00; + *optptr++ = 0x00; + *optptr++ = 0x00; + *optptr++ = 0x02; - return optptr; + return optptr; } /////////////////////////////////////////////////////////////////////////////////// /* @@ -236,40 +257,40 @@ static u8_t* add_offer_options(u8_t *optptr) * @return uint8_t* ����DHCP msgƫ�Ƶ�ַ */ /////////////////////////////////////////////////////////////////////////////////// -static u8_t* add_end(u8_t *optptr) +static u8_t* add_end(u8_t* optptr) { - *optptr++ = DHCP_OPTION_END; - return optptr; + *optptr++ = DHCP_OPTION_END; + return optptr; } /////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// -static void create_msg(struct dhcps_msg *m) +static void create_msg(struct dhcps_msg* m) { - struct ip_addr client; + struct ip_addr client; - client.addr = *( (uint32_t *) &client_address); + client.addr = *((uint32_t*) &client_address); - m->op = DHCP_REPLY; - m->htype = DHCP_HTYPE_ETHERNET; - m->hlen = 6; - m->hops = 0; + m->op = DHCP_REPLY; + m->htype = DHCP_HTYPE_ETHERNET; + m->hlen = 6; + m->hops = 0; // os_memcpy((char *) xid, (char *) m->xid, sizeof(m->xid)); - m->secs = 0; - m->flags = htons(BOOTP_BROADCAST); + m->secs = 0; + m->flags = htons(BOOTP_BROADCAST); - memcpy((char *) m->yiaddr, (char *) &client.addr, sizeof(m->yiaddr)); + 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->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)); + memset((char*) m->options, 0, sizeof(m->options)); - u32_t magic_cookie1 = magic_cookie; - memcpy((char *) m->options, &magic_cookie1, sizeof(magic_cookie1)); + u32_t magic_cookie1 = magic_cookie; + memcpy((char*) m->options, &magic_cookie1, sizeof(magic_cookie1)); } /////////////////////////////////////////////////////////////////////////////////// /* @@ -278,64 +299,74 @@ static void create_msg(struct dhcps_msg *m) * @param -- m ָ����Ҫ���͵�DHCP msg���� */ /////////////////////////////////////////////////////////////////////////////////// -static void send_offer(struct dhcps_msg *m) +static void send_offer(struct dhcps_msg* m) { - u8_t *end; - struct pbuf *p, *q; - u8_t *data; - u16_t cnt=0; - u16_t i; - err_t SendOffer_err_t; - create_msg(m); + u8_t* end; + struct pbuf* p, *q; + u8_t* data; + u16_t cnt = 0; + u16_t i; +#if DHCPS_DEBUG + err_t SendOffer_err_t; +#endif + create_msg(m); - end = add_msg_type(&m->options[4], DHCPOFFER); - end = add_offer_options(end); - end = add_end(end); + 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); + 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); + os_printf("udhcp: send_offer>>p->ref = %d\n", p->ref); #endif - if(p != NULL){ - -#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 != NULL){ - data = (u8_t *)q->payload; - for(i=0; ilen; i++) - { - data[i] = ((u8_t *) m)[cnt++]; -#if DHCPS_DEBUG - os_printf("%02x ",data[i]); - if((i+1)%16 == 0){ - os_printf("\n"); - } -#endif - } - q = q->next; - } - }else{ - + if (p != NULL) { + #if DHCPS_DEBUG - os_printf("dhcps: send_offer>>pbuf_alloc failed\n"); + 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 - return; - } - SendOffer_err_t = udp_sendto( pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT ); + q = p; + + while (q != NULL) { + data = (u8_t*)q->payload; + + for (i = 0; i < q->len; i++) { + data[i] = ((u8_t*) m)[cnt++]; #if DHCPS_DEBUG - os_printf("dhcps: send_offer>>udp_sendto result %x\n",SendOffer_err_t); + os_printf("%02x ", data[i]); + + if ((i + 1) % 16 == 0) { + os_printf("\n"); + } + #endif - if(p->ref != 0){ + } + + q = q->next; + } + } else { + #if DHCPS_DEBUG - os_printf("udhcp: send_offer>>free pbuf\n"); + os_printf("dhcps: send_offer>>pbuf_alloc failed\n"); #endif - pbuf_free(p); - } + return; + } + +#if DHCPS_DEBUG + SendOffer_err_t = udp_sendto(pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT); + os_printf("dhcps: send_offer>>udp_sendto result %x\n", SendOffer_err_t); +#else + udp_sendto(pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT); +#endif + + if (p->ref != 0) { +#if DHCPS_DEBUG + os_printf("udhcp: send_offer>>free pbuf\n"); +#endif + pbuf_free(p); + } } /////////////////////////////////////////////////////////////////////////////////// /* @@ -344,64 +375,74 @@ static void send_offer(struct dhcps_msg *m) * @param m ָ����Ҫ���͵�DHCP msg���� */ /////////////////////////////////////////////////////////////////////////////////// -static void send_nak(struct dhcps_msg *m) +static void send_nak(struct dhcps_msg* m) { - u8_t *end; - struct pbuf *p, *q; - u8_t *data; - u16_t cnt=0; - u16_t i; - err_t SendNak_err_t; - create_msg(m); + u8_t* end; + struct pbuf* p, *q; + u8_t* data; + u16_t cnt = 0; + u16_t i; +#if DHCPS_DEBUG + err_t SendNak_err_t; +#endif + create_msg(m); - end = add_msg_type(&m->options[4], DHCPNAK); - end = add_end(end); + end = add_msg_type(&m->options[4], DHCPNAK); + end = add_end(end); - p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); + 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); + os_printf("udhcp: send_nak>>p->ref = %d\n", p->ref); #endif - if(p != NULL){ - -#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 != NULL){ - data = (u8_t *)q->payload; - for(i=0; ilen; i++) - { - data[i] = ((u8_t *) m)[cnt++]; -#if DHCPS_DEBUG - os_printf("%02x ",data[i]); - if((i+1)%16 == 0){ - os_printf("\n"); - } -#endif - } - q = q->next; - } - }else{ - + if (p != NULL) { + #if DHCPS_DEBUG - os_printf("dhcps: send_nak>>pbuf_alloc failed\n"); + 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 - return; - } - SendNak_err_t = udp_sendto( pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT ); + q = p; + + while (q != NULL) { + data = (u8_t*)q->payload; + + for (i = 0; i < q->len; i++) { + data[i] = ((u8_t*) m)[cnt++]; #if DHCPS_DEBUG - os_printf("dhcps: send_nak>>udp_sendto result %x\n",SendNak_err_t); + os_printf("%02x ", data[i]); + + if ((i + 1) % 16 == 0) { + os_printf("\n"); + } + #endif - if(p->ref != 0){ -#if DHCPS_DEBUG - os_printf("udhcp: send_nak>>free pbuf\n"); + } + + q = q->next; + } + } else { + +#if DHCPS_DEBUG + os_printf("dhcps: send_nak>>pbuf_alloc failed\n"); #endif - pbuf_free(p); - } + return; + } + +#if DHCPS_DEBUG + SendNak_err_t = udp_sendto(pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT); + os_printf("dhcps: send_nak>>udp_sendto result %x\n", SendNak_err_t); +#else + udp_sendto(pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT); +#endif + + if (p->ref != 0) { +#if DHCPS_DEBUG + os_printf("udhcp: send_nak>>free pbuf\n"); +#endif + pbuf_free(p); + } } /////////////////////////////////////////////////////////////////////////////////// /* @@ -410,66 +451,75 @@ static void send_nak(struct dhcps_msg *m) * @param m ָ����Ҫ���͵�DHCP msg���� */ /////////////////////////////////////////////////////////////////////////////////// -static void send_ack(struct dhcps_msg *m) +static void send_ack(struct dhcps_msg* m) { - u8_t *end; - struct pbuf *p, *q; - u8_t *data; - u16_t cnt=0; - u16_t i; - err_t SendAck_err_t; - create_msg(m); + u8_t* end; + struct pbuf* p, *q; + u8_t* data; + u16_t cnt = 0; + u16_t i; +#if DHCPS_DEBUG + err_t SendAck_err_t; +#endif + 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 != NULL){ - -#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 != NULL){ - data = (u8_t *)q->payload; - for(i=0; ilen; i++) - { - data[i] = ((u8_t *) m)[cnt++]; -#if DHCPS_DEBUG - os_printf("%02x ",data[i]); - if((i+1)%16 == 0){ - os_printf("\n"); - } -#endif - } + end = add_msg_type(&m->options[4], DHCPACK); + end = add_offer_options(end); + end = add_end(end); - q = q->next; - } - }else{ - + p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); #if DHCPS_DEBUG - os_printf("dhcps: send_ack>>pbuf_alloc failed\n"); + os_printf("udhcp: send_ack>>p->ref = %d\n", p->ref); #endif - return; - } - SendAck_err_t = udp_sendto( pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT ); + + if (p != NULL) { + #if DHCPS_DEBUG - os_printf("dhcps: send_ack>>udp_sendto result %x\n",SendAck_err_t); + 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 - - if(p->ref != 0){ + q = p; + + while (q != NULL) { + data = (u8_t*)q->payload; + + for (i = 0; i < q->len; i++) { + data[i] = ((u8_t*) m)[cnt++]; #if DHCPS_DEBUG - os_printf("udhcp: send_ack>>free pbuf\n"); + os_printf("%02x ", data[i]); + + if ((i + 1) % 16 == 0) { + os_printf("\n"); + } + #endif - pbuf_free(p); - } + } + + q = q->next; + } + } else { + +#if DHCPS_DEBUG + os_printf("dhcps: send_ack>>pbuf_alloc failed\n"); +#endif + return; + } + +#if DHCPS_DEBUG + SendAck_err_t = udp_sendto(pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT); + os_printf("dhcps: send_ack>>udp_sendto result %x\n", SendAck_err_t); +#else + udp_sendto(pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT); +#endif + + if (p->ref != 0) { +#if DHCPS_DEBUG + os_printf("udhcp: send_ack>>free pbuf\n"); +#endif + pbuf_free(p); + } } /////////////////////////////////////////////////////////////////////////////////// /* @@ -481,112 +531,118 @@ static void send_ack(struct dhcps_msg *m) * @return uint8_t ���ش�����DHCP Server״ֵ̬ */ /////////////////////////////////////////////////////////////////////////////////// -static u8_t parse_options(u8_t *optptr, s16_t len) +static u8_t parse_options(u8_t* optptr, s16_t len) { - struct ip_addr client; - bool is_dhcp_parse_end = false; - struct dhcps_state s; + struct ip_addr client; + bool is_dhcp_parse_end = false; + struct dhcps_state s; - client.addr = *( (uint32_t *) &client_address);// Ҫ�����DHCP�ͻ��˵�IP + client.addr = *((uint32_t*) &client_address); // Ҫ�����DHCP�ͻ��˵�IP - u8_t *end = optptr + len; - u16_t type = 0; + u8_t* end = optptr + len; + u16_t type = 0; - s.state = DHCPS_STATE_IDLE; + s.state = DHCPS_STATE_IDLE; - while (optptr < end) { + while (optptr < end) { #if DHCPS_DEBUG - os_printf("dhcps: (s16_t)*optptr = %d\n", (s16_t)*optptr); + os_printf("dhcps: (s16_t)*optptr = %d\n", (s16_t)*optptr); #endif - switch ((s16_t) *optptr) { - case DHCP_OPTION_MSG_TYPE: //53 - type = *(optptr + 2); - break; + switch ((s16_t) *optptr) { - case DHCP_OPTION_REQ_IPADDR://50 - if( memcmp( (char *) &client.addr, (char *) optptr+2,4)==0 ) { + case DHCP_OPTION_MSG_TYPE: //53 + type = *(optptr + 2); + break; + + case DHCP_OPTION_REQ_IPADDR://50 + if (memcmp((char*) &client.addr, (char*) optptr + 2, 4) == 0) { #if DHCPS_DEBUG - os_printf("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n"); + os_printf("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n"); #endif - s.state = DHCPS_STATE_ACK; - }else { + s.state = DHCPS_STATE_ACK; + } else { #if DHCPS_DEBUG - os_printf("dhcps: DHCP_OPTION_REQ_IPADDR != 0 err\n"); + 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 - s.state = DHCPS_STATE_NAK; - } - break; - case DHCP_OPTION_END: - { - is_dhcp_parse_end = true; - } - break; } - if(is_dhcp_parse_end){ - break; - } + break; - optptr += optptr[1] + 2; - } + case DHCPDECLINE://4 + s.state = DHCPS_STATE_IDLE; +#if DHCPS_DEBUG + os_printf("dhcps: DHCPD_STATE_IDLE\n"); +#endif + break; - switch (type){ - - case DHCPDISCOVER://1 - s.state = DHCPS_STATE_OFFER; + case DHCPRELEASE://7 + s.state = DHCPS_STATE_RELEASE; #if DHCPS_DEBUG - os_printf("dhcps: DHCPD_STATE_OFFER\n"); + os_printf("dhcps: DHCPD_STATE_IDLE\n"); #endif - break; + 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"); + os_printf("dhcps: return s.state = %d\n", s.state); #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; + return s.state; } /////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// -static s16_t parse_msg(struct dhcps_msg *m, u16_t len) +static s16_t parse_msg(struct dhcps_msg* m, u16_t len) { - u32_t lease_timer = (dhcps_lease_time * 60)/DHCPS_COARSE_TIMER_SECS; - if(memcmp((char *)m->options, - &magic_cookie, - sizeof(magic_cookie)) == 0){ + u32_t lease_timer = (dhcps_lease_time * 60) / DHCPS_COARSE_TIMER_SECS; + + if (memcmp((char*)m->options, + &magic_cookie, + sizeof(magic_cookie)) == 0) { #if DHCPS_DEBUG - os_printf("dhcps: len = %d\n", len); + os_printf("dhcps: len = %d\n", len); #endif - /* - * ��¼��ǰ��xid���ﴦ���? - * �˺�ΪDHCP�ͻ����������û�ͳһ��ȡIPʱ�� - */ + /* + * ��¼��ǰ��xid���ﴦ���? + * �˺�ΪDHCP�ͻ����������û�ͳһ��ȡIPʱ�� + */ // if((old_xid[0] == 0) && // (old_xid[1] == 0) && // (old_xid[2] == 0) && @@ -604,134 +660,144 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len) // * // */ // if(memcmp((char *)old_xid, (char *)m->xid, sizeof(m->xid)) != 0){ - /* - * ��¼���ε�xid�ţ�ͬʱ�����IP���� - */ - struct ip_addr addr_tmp; + /* + * ��¼���ε�xid�ţ�ͬʱ�����IP���� + */ + struct ip_addr addr_tmp; // memcpy((char *)old_xid, (char *)m->xid, sizeof(m->xid)); // { - struct dhcps_pool *pdhcps_pool = NULL; - list_node *pnode = NULL; - list_node *pback_node = NULL; - struct ip_addr first_address; - bool flag = false; + struct dhcps_pool* pdhcps_pool = NULL; + list_node* pnode = NULL; + list_node* pback_node = NULL; + struct ip_addr first_address; + bool flag = false; // POOL_START: - first_address.addr = dhcps_lease.start_ip.addr; - client_address.addr = client_address_plus.addr; - renew = false; + first_address.addr = dhcps_lease.start_ip.addr; + client_address.addr = client_address_plus.addr; + renew = false; + // addr_tmp.addr = htonl(client_address_plus.addr); // addr_tmp.addr++; // client_address_plus.addr = htonl(addr_tmp.addr); - if (plist != NULL){ - for (pback_node = plist; pback_node != NULL;pback_node = pback_node->pnext) { - pdhcps_pool = pback_node->pnode; - if (memcmp(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)) == 0){ + if (plist != NULL) { + for (pback_node = plist; pback_node != NULL; pback_node = pback_node->pnext) { + pdhcps_pool = pback_node->pnode; + + if (memcmp(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)) == 0) { // os_printf("the same device request ip\n"); - if (memcmp(&pdhcps_pool->ip.addr, m->ciaddr, sizeof(pdhcps_pool->ip.addr)) == 0) { - renew = true; - } - client_address.addr = pdhcps_pool->ip.addr; - pdhcps_pool->lease_timer = lease_timer; - pnode = pback_node; - goto POOL_CHECK; - } else if (pdhcps_pool->ip.addr == client_address_plus.addr){ + if (memcmp(&pdhcps_pool->ip.addr, m->ciaddr, sizeof(pdhcps_pool->ip.addr)) == 0) { + renew = true; + } + + client_address.addr = pdhcps_pool->ip.addr; + pdhcps_pool->lease_timer = lease_timer; + pnode = pback_node; + goto POOL_CHECK; + } else if (pdhcps_pool->ip.addr == client_address_plus.addr) { // client_address.addr = client_address_plus.addr; // os_printf("the ip addr has been request\n"); - addr_tmp.addr = htonl(client_address_plus.addr); - addr_tmp.addr++; - client_address_plus.addr = htonl(addr_tmp.addr); - client_address.addr = client_address_plus.addr; - } + addr_tmp.addr = htonl(client_address_plus.addr); + addr_tmp.addr++; + client_address_plus.addr = htonl(addr_tmp.addr); + client_address.addr = client_address_plus.addr; + } - if(flag == false) { // search the fisrt unused ip - if(first_address.addr < pdhcps_pool->ip.addr) { - flag = true; - } else { - addr_tmp.addr = htonl(first_address.addr); - addr_tmp.addr++; - first_address.addr = htonl(addr_tmp.addr); - } - } - } - }else{ - client_address.addr = dhcps_lease.start_ip.addr; - } + if (flag == false) { // search the fisrt unused ip + if (first_address.addr < pdhcps_pool->ip.addr) { + flag = true; + } else { + addr_tmp.addr = htonl(first_address.addr); + addr_tmp.addr++; + first_address.addr = htonl(addr_tmp.addr); + } + } + } + } else { + client_address.addr = dhcps_lease.start_ip.addr; + } - if (client_address_plus.addr > dhcps_lease.end_ip.addr) { - client_address.addr = first_address.addr; - } - if (client_address.addr > dhcps_lease.end_ip.addr) { - client_address_plus.addr = dhcps_lease.start_ip.addr; - pdhcps_pool = NULL; - pnode = NULL; - } else { - pdhcps_pool = (struct dhcps_pool *)os_zalloc(sizeof(struct dhcps_pool)); - pdhcps_pool->ip.addr = client_address.addr; - memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)); - pdhcps_pool->lease_timer = lease_timer; - pnode = (list_node *)os_zalloc(sizeof(list_node )); - pnode->pnode = pdhcps_pool; - pnode->pnext = NULL; - node_insert_to_list(&plist,pnode); - if (client_address.addr == dhcps_lease.end_ip.addr) { - client_address_plus.addr = dhcps_lease.start_ip.addr; - } else { - addr_tmp.addr = htonl(client_address.addr); - addr_tmp.addr++; - client_address_plus.addr = htonl(addr_tmp.addr); - } - } + if (client_address_plus.addr > dhcps_lease.end_ip.addr) { + client_address.addr = first_address.addr; + } - POOL_CHECK: - if ((client_address.addr > dhcps_lease.end_ip.addr) || (ip_addr_isany(&client_address))){ - if(pnode != NULL) { - node_remove_from_list(&plist,pnode); - os_free(pnode); - pnode = NULL; - } + if (client_address.addr > dhcps_lease.end_ip.addr) { + client_address_plus.addr = dhcps_lease.start_ip.addr; + pdhcps_pool = NULL; + pnode = NULL; + } else { + pdhcps_pool = (struct dhcps_pool*)os_zalloc(sizeof(struct dhcps_pool)); + pdhcps_pool->ip.addr = client_address.addr; + memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)); + pdhcps_pool->lease_timer = lease_timer; + pnode = (list_node*)os_zalloc(sizeof(list_node)); + pnode->pnode = pdhcps_pool; + pnode->pnext = NULL; + node_insert_to_list(&plist, pnode); + + if (client_address.addr == dhcps_lease.end_ip.addr) { + client_address_plus.addr = dhcps_lease.start_ip.addr; + } else { + addr_tmp.addr = htonl(client_address.addr); + addr_tmp.addr++; + client_address_plus.addr = htonl(addr_tmp.addr); + } + } + +POOL_CHECK: + + if ((client_address.addr > dhcps_lease.end_ip.addr) || (ip_addr_isany(&client_address))) { + if (pnode != NULL) { + node_remove_from_list(&plist, pnode); + os_free(pnode); + pnode = NULL; + } + + if (pdhcps_pool != NULL) { + os_free(pdhcps_pool); + pdhcps_pool = NULL; + } - if (pdhcps_pool != NULL) { - os_free(pdhcps_pool); - pdhcps_pool = NULL; - } // client_address_plus.addr = dhcps_lease.start_ip.addr; - return 4; - } + return 4; + } - s16_t ret = parse_options(&m->options[4], len);; + s16_t ret = parse_options(&m->options[4], len);; - if(ret == DHCPS_STATE_RELEASE) { - if(pnode != NULL) { - node_remove_from_list(&plist,pnode); - os_free(pnode); - pnode = NULL; - } + if (ret == DHCPS_STATE_RELEASE) { + if (pnode != NULL) { + node_remove_from_list(&plist, pnode); + os_free(pnode); + pnode = NULL; + } - if (pdhcps_pool != NULL) { - os_free(pdhcps_pool); - pdhcps_pool = NULL; - } - memset(&client_address,0x0,sizeof(client_address)); - } + if (pdhcps_pool != NULL) { + os_free(pdhcps_pool); + pdhcps_pool = NULL; + } + + memset(&client_address, 0x0, sizeof(client_address)); + } + + if (wifi_softap_set_station_info(m->chaddr, &client_address) == false) { + return 0; + } - if (wifi_softap_set_station_info(m->chaddr, &client_address) == false) { - return 0; - } // } #if DHCPS_DEBUG - os_printf("dhcps: xid changed\n"); - os_printf("dhcps: client_address.addr = %x\n", client_address.addr); + os_printf("dhcps: xid changed\n"); + os_printf("dhcps: client_address.addr = %x\n", client_address.addr); #endif - + // } - + // } - return ret; - } - return 0; + return ret; + } + + return 0; } /////////////////////////////////////////////////////////////////////////////////// /* @@ -745,245 +811,273 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len) * @param port ���ʹ�UDP���Դ�����UDPͨ���˿ں� */ /////////////////////////////////////////////////////////////////////////////////// -static void handle_dhcp(void *arg, - struct udp_pcb *pcb, - struct pbuf *p, - struct ip_addr *addr, - u16_t port) +static void handle_dhcp(void* arg, + struct udp_pcb* pcb, + struct pbuf* p, + struct ip_addr* addr, + u16_t port) { - struct dhcps_msg *pmsg_dhcps = NULL; - s16_t tlen; - u16_t i; - u16_t dhcps_msg_cnt=0; - u8_t *p_dhcps_msg = NULL; - u8_t *data; + struct dhcps_msg* pmsg_dhcps = NULL; + s16_t tlen; + u16_t i; + u16_t dhcps_msg_cnt = 0; + u8_t* p_dhcps_msg = NULL; + u8_t* data; #if DHCPS_DEBUG - os_printf("dhcps: handle_dhcp-> receive a packet\n"); + os_printf("dhcps: handle_dhcp-> receive a packet\n"); #endif - if (p==NULL) return; - pmsg_dhcps = (struct dhcps_msg *)os_zalloc(sizeof(struct dhcps_msg)); - if (NULL == pmsg_dhcps){ - pbuf_free(p); - return; - } - p_dhcps_msg = (u8_t *)pmsg_dhcps; - tlen = p->tot_len; - data = p->payload; + if (p == NULL) { + return; + } -#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 + pmsg_dhcps = (struct dhcps_msg*)os_zalloc(sizeof(struct dhcps_msg)); - for(i=0; ilen; i++){ - p_dhcps_msg[dhcps_msg_cnt++] = data[i]; -#if DHCPS_DEBUG - os_printf("%02x ",data[i]); - if((i+1)%16 == 0){ - os_printf("\n"); - } -#endif - } - - if(p->next != NULL) { -#if DHCPS_DEBUG - os_printf("dhcps: handle_dhcp-> p->next != NULL\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 = p->next->payload; - for(i=0; inext->len; i++){ - p_dhcps_msg[dhcps_msg_cnt++] = data[i]; -#if DHCPS_DEBUG - os_printf("%02x ",data[i]); - if((i+1)%16 == 0){ - os_printf("\n"); - } -#endif - } - } - - /* - * DHCP �ͻ���������Ϣ���� - */ -#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); - 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 + if (NULL == pmsg_dhcps) { pbuf_free(p); - os_free(pmsg_dhcps); - pmsg_dhcps = NULL; + return; + } + + p_dhcps_msg = (u8_t*)pmsg_dhcps; + tlen = p->tot_len; + data = 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 DHCPS_DEBUG + os_printf("%02x ", data[i]); + + if ((i + 1) % 16 == 0) { + os_printf("\n"); + } + +#endif + } + + if (p->next != NULL) { +#if DHCPS_DEBUG + os_printf("dhcps: handle_dhcp-> p->next != NULL\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 = p->next->payload; + + for (i = 0; i < p->next->len; i++) { + p_dhcps_msg[dhcps_msg_cnt++] = data[i]; +#if DHCPS_DEBUG + os_printf("%02x ", data[i]); + + if ((i + 1) % 16 == 0) { + os_printf("\n"); + } + +#endif + } + } + + /* + * DHCP �ͻ���������Ϣ���� + */ +#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); + 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); + os_free(pmsg_dhcps); + pmsg_dhcps = NULL; } /////////////////////////////////////////////////////////////////////////////////// static void wifi_softap_init_dhcps_lease(u32_t ip) { - u32_t softap_ip = 0,local_ip = 0; - u32_t start_ip = 0; - u32_t 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; - } - } - } + u32_t softap_ip = 0, local_ip = 0; + u32_t start_ip = 0; + u32_t end_ip = 0; - 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 ++; + 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); + } - 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); - } // os_printf("start_ip = 0x%x, end_ip = 0x%x\n",dhcps_lease.start_ip, dhcps_lease.end_ip); } /////////////////////////////////////////////////////////////////////////////////// -void dhcps_start(struct ip_info *info) +void dhcps_start(struct ip_info* info) { - struct netif * apnetif = NULL; - apnetif = (struct netif *)wifi_get_netif(SOFTAP_IF); - - if(apnetif->dhcps_pcb != NULL) { + struct netif* apnetif = esp_netif[0x01]; + + if (apnetif->dhcps_pcb != NULL) { udp_remove(apnetif->dhcps_pcb); } - pcb_dhcps = udp_new(); - if (pcb_dhcps == NULL || info ==NULL) { - os_printf("dhcps_start(): could not obtain pcb\n"); - } - apnetif->dhcps_pcb = pcb_dhcps; + pcb_dhcps = udp_new(); - IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255); + if (pcb_dhcps == NULL || info == NULL) { + os_printf("dhcps_start(): could not obtain pcb\n"); + } - server_address = info->ip; - wifi_softap_init_dhcps_lease(server_address.addr); - client_address_plus.addr = dhcps_lease.start_ip.addr; - udp_bind(pcb_dhcps, IP_ADDR_ANY, DHCPS_SERVER_PORT); - udp_recv(pcb_dhcps, handle_dhcp, NULL); + apnetif->dhcps_pcb = pcb_dhcps; + + IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255); + + server_address = info->ip; + wifi_softap_init_dhcps_lease(server_address.addr); + client_address_plus.addr = dhcps_lease.start_ip.addr; + udp_bind(pcb_dhcps, IP_ADDR_ANY, DHCPS_SERVER_PORT); + udp_recv(pcb_dhcps, handle_dhcp, NULL); #if DHCPS_DEBUG - os_printf("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n"); + os_printf("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n"); #endif - + } void dhcps_stop(void) { - struct netif * apnetif = (struct netif *)wifi_get_netif(0x01); + struct netif* apnetif = esp_netif[0x01]; + + udp_disconnect(pcb_dhcps); - udp_disconnect(pcb_dhcps); // dhcps_lease_flag = true; - if(apnetif->dhcps_pcb != NULL) { + if (apnetif->dhcps_pcb != NULL) { udp_remove(apnetif->dhcps_pcb); apnetif->dhcps_pcb = NULL; } - //udp_remove(pcb_dhcps); - list_node *pnode = NULL; - list_node *pback_node = NULL; - pnode = plist; - while (pnode != NULL) { - pback_node = pnode; - pnode = pback_node->pnext; - node_remove_from_list(&plist, pback_node); - os_free(pback_node->pnode); - pback_node->pnode = NULL; - os_free(pback_node); - pback_node = NULL; - } + //udp_remove(pcb_dhcps); + list_node* pnode = NULL; + list_node* pback_node = NULL; + pnode = plist; + + while (pnode != NULL) { + pback_node = pnode; + pnode = pback_node->pnext; + node_remove_from_list(&plist, pback_node); + os_free(pback_node->pnode); + pback_node->pnode = NULL; + os_free(pback_node); + pback_node = NULL; + } } -bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please) +bool wifi_softap_set_dhcps_lease(struct dhcps_lease* please) { - struct ip_info info; - u32_t softap_ip = 0; - u32_t start_ip = 0; - u32_t end_ip = 0; + struct ip_info info; + u32_t softap_ip = 0; + u32_t start_ip = 0; + u32_t end_ip = 0; - uint8 opmode = wifi_get_opmode(); + uint8 opmode = wifi_get_opmode(); - if (opmode == STATION_MODE || opmode == NULL_MODE) { - return false; - } + if (opmode == STATION_MODE || opmode == NULL_MODE) { + return false; + } - if (please == NULL || wifi_softap_dhcps_status() == DHCP_STARTED) - return false; + if (please == NULL || wifi_softap_dhcps_status() == DHCP_STARTED) { + return false; + } - if(please->enable) { - 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); + if (please->enable) { + 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); - /*config ip information can't contain local ip*/ - if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) - return false; + /*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; - } + /*config ip information must be in the same segment as the local ip*/ + softap_ip >>= 8; - if (end_ip - start_ip > DHCPS_MAX_LEASE) - return false; + if ((start_ip >> 8 != softap_ip) + || (end_ip >> 8 != softap_ip)) { + return false; + } - bzero(&dhcps_lease, sizeof(dhcps_lease)); + 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.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; + return true; } /****************************************************************************** @@ -993,100 +1087,115 @@ bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please) * Little-Endian. * Returns : true or false *******************************************************************************/ -bool wifi_softap_get_dhcps_lease(struct dhcps_lease *please) +bool wifi_softap_get_dhcps_lease(struct dhcps_lease* please) { - u8_t opmode = wifi_get_opmode(); + u8_t opmode = wifi_get_opmode(); - if (opmode == STATION_MODE || opmode == NULL_MODE) { - return false; - } + if (opmode == STATION_MODE || opmode == NULL_MODE) { + return false; + } - if (NULL == please) - return false; - if (dhcps_lease.enable == FALSE){ - if (wifi_softap_dhcps_status() == DHCP_STOPPED) - return false; - } else { - } - please->start_ip.addr = dhcps_lease.start_ip.addr; - please->end_ip.addr = dhcps_lease.end_ip.addr; - return true; + if (NULL == please) { + return false; + } + + if (dhcps_lease.enable == FALSE) { + if (wifi_softap_dhcps_status() == DHCP_STOPPED) { + return false; + } + } else { + } + + please->start_ip.addr = dhcps_lease.start_ip.addr; + please->end_ip.addr = dhcps_lease.end_ip.addr; + return true; } static void kill_oldest_dhcps_pool(void) { - list_node *pre = NULL, *p = NULL; - list_node *minpre = NULL, *minp = NULL; - struct dhcps_pool *pdhcps_pool = NULL, *pmin_pool = NULL; - pre = plist; - p = pre->pnext; - minpre = pre; - minp = p; - while (p != NULL){ - pdhcps_pool = p->pnode; - pmin_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; - os_free(minp->pnode); - minp->pnode = NULL; - os_free(minp); - minp = NULL; + list_node* pre = NULL, *p = NULL; + list_node* minpre = NULL, *minp = NULL; + struct dhcps_pool* pdhcps_pool = NULL, *pmin_pool = NULL; + pre = plist; + p = pre->pnext; + minpre = pre; + minp = p; + + while (p != NULL) { + pdhcps_pool = p->pnode; + pmin_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; + os_free(minp->pnode); + minp->pnode = NULL; + os_free(minp); + minp = NULL; } void dhcps_coarse_tmr(void) { - u8_t num_dhcps_pool = 0; - list_node *pback_node = NULL; - list_node *pnode = NULL; - struct dhcps_pool *pdhcps_pool = NULL; - pnode = plist; - while (pnode != NULL) { - pdhcps_pool = pnode->pnode; - pdhcps_pool->lease_timer --; - if (pdhcps_pool->lease_timer == 0){ - pback_node = pnode; - pnode = pback_node->pnext; - node_remove_from_list(&plist,pback_node); - os_free(pback_node->pnode); - pback_node->pnode = NULL; - os_free(pback_node); - pback_node = NULL; - } else { - pnode = pnode ->pnext; - num_dhcps_pool ++; - } - } + u8_t num_dhcps_pool = 0; + list_node* pback_node = NULL; + list_node* pnode = NULL; + struct dhcps_pool* pdhcps_pool = NULL; + pnode = plist; - if (num_dhcps_pool >= MAX_STATION_NUM) - kill_oldest_dhcps_pool(); + while (pnode != NULL) { + pdhcps_pool = pnode->pnode; + pdhcps_pool->lease_timer --; + + if (pdhcps_pool->lease_timer == 0) { + pback_node = pnode; + pnode = pback_node->pnext; + node_remove_from_list(&plist, pback_node); + os_free(pback_node->pnode); + pback_node->pnode = NULL; + os_free(pback_node); + pback_node = NULL; + } else { + pnode = pnode ->pnext; + num_dhcps_pool ++; + } + } + + if (num_dhcps_pool >= MAX_STATION_NUM) { + kill_oldest_dhcps_pool(); + } } bool wifi_softap_set_dhcps_offer_option(u8_t level, void* optarg) { - bool offer_flag = true; - u8_t option = 0; - if (optarg == NULL && wifi_softap_dhcps_status() == false) - return false; + bool offer_flag = true; - if (level <= OFFER_START || level >= OFFER_END) - return false; + if (optarg == NULL && wifi_softap_dhcps_status() == false) { + return false; + } - switch (level){ - case OFFER_ROUTER: - offer = (*(uint8 *)optarg) & 0x01; - offer_flag = true; - break; - default : - offer_flag = false; - break; - } - return offer_flag; + 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 wifi_softap_set_dhcps_lease_time(u32_t minute) @@ -1101,9 +1210,10 @@ bool wifi_softap_set_dhcps_lease_time(u32_t minute) return false; } - if(minute == 0) { + if (minute == 0) { return false; } + dhcps_lease_time = minute; return true; } @@ -1119,6 +1229,7 @@ bool wifi_softap_reset_dhcps_lease_time(void) if (wifi_softap_dhcps_status() == DHCP_STARTED) { return false; } + dhcps_lease_time = DHCPS_LEASE_TIME_DEF; return true; } diff --git a/components/lwip/source/core/pbuf.c b/components/lwip/source/core/pbuf.c index d7c4ec8b..b3e8514f 100644 --- a/components/lwip/source/core/pbuf.c +++ b/components/lwip/source/core/pbuf.c @@ -357,8 +357,7 @@ pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type) p->len = p->tot_len = length; p->next = NULL; p->type = type; - p->eb = NULL; - + LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned", ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0); break; @@ -736,9 +735,6 @@ pbuf_free(struct pbuf *p) || type == PBUF_ESF_RX #endif //EBUF_LWIP ) { -#ifdef EBUF_LWIP - system_pp_recycle_rx_pkt(p->eb); -#endif //EBUF_LWIP memp_free(MEMP_PBUF, p); /* type == PBUF_RAM */ } else { diff --git a/components/lwip/source/netif/ethernetif.c b/components/lwip/source/netif/ethernetif.c index 3e76a4f5..c2abca8b 100644 --- a/components/lwip/source/netif/ethernetif.c +++ b/components/lwip/source/netif/ethernetif.c @@ -1,64 +1,28 @@ -/** - * @file - * Ethernet Interface Skeleton - * - */ +// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at -/* - * Copyright (c) 2001-2004 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. -#include "lwip/opt.h" - -#include "lwip/def.h" -#include "lwip/mem.h" #include "lwip/pbuf.h" -#include "lwip/stats.h" -#include "lwip/snmp.h" #include "lwip/ethip6.h" #include "netif/etharp.h" -#include "netif/ppp_oe.h" +#include "esp_libc.h" +#include "esp_wifi.h" +#include "tcpip_adapter.h" -#include "esp_common.h" -#ifdef MEMLEAK_DEBUG -static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__; -#endif - -/* Define those to better describe your network interface. */ -#define IFNAME0 'e' -#define IFNAME1 'n' - -char *hostname; -bool default_hostname = 1; +int8_t ieee80211_output_pbuf(uint8_t fd, uint8_t* dataptr, uint16_t datalen); +int8_t wifi_get_netif(uint8_t fd); +void wifi_station_set_default_hostname(uint8_t* hwaddr); /** * In this function, the hardware should be initialized. @@ -67,23 +31,27 @@ bool default_hostname = 1; * @param netif the already initialized lwip network interface structure * for this ethernetif */ -static void -low_level_init(struct netif *netif) +static void low_level_init(struct netif* netif) { - /* set MAC hardware address length */ - netif->hwaddr_len = ETHARP_HWADDR_LEN; + if (netif == NULL) { + TCPIP_ATAPTER_LOG("ERROR netif is NULL\n"); + return; + } - /* maximum transfer unit */ - netif->mtu = 1500; - - /* device capabilities */ - /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ - netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; + /* set MAC hardware address length */ + netif->hwaddr_len = ETHARP_HWADDR_LEN; + + /* maximum transfer unit */ + netif->mtu = 1500; + + /* device capabilities */ + /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ + netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; #if LWIP_IGMP - netif->flags |= NETIF_FLAG_IGMP; + netif->flags |= NETIF_FLAG_IGMP; #endif - /* Do whatever else is needed to initialize interface. */ + /* Do whatever else is needed to initialize interface. */ } /** @@ -94,7 +62,7 @@ low_level_init(struct netif *netif) * @param netif the lwip network interface structure for this ethernetif * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type) * @return ERR_OK if the packet could be sent - * an err_t value if the packet couldn't be sent + * an int8_t value if the packet couldn't be sent * * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to * strange results. You might consider waiting for space in the DMA queue @@ -102,43 +70,48 @@ low_level_init(struct netif *netif) * dropped because of memory failure (except for the TCP timers). */ -static err_t -low_level_output(struct netif *netif, struct pbuf *p) +static int8_t low_level_output(struct netif* netif, struct pbuf* p) { - struct ethernetif *ethernetif = netif->state; - struct pbuf *q; - err_t err = ERR_OK; -// initiate transfer(); - + int8_t err = ERR_OK; + + if (netif == NULL) { + TCPIP_ATAPTER_LOG("ERROR netif is NULL\n"); + return ERR_ARG; + } + #if ETH_PAD_SIZE - pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ + pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ #endif - struct pbuf *tmp; - for(q = p; q != NULL; q = q->next) { - /* Send the data from the pbuf to the interface, one pbuf at a - time. The size of the data in each pbuf is kept in the ->len - variable. */ - // send data from(q->payload, q->len); - tmp = q->next; + uint8_t* outputbuf = (uint8_t*)os_malloc(p->len + 36); + if (outputbuf == NULL) { + TCPIP_ATAPTER_LOG("ERROR no memory\n"); + return ERR_MEM; + } + + outputbuf += 36; + memcpy(outputbuf, p->payload, p->len); + + if (netif == esp_netif[TCPIP_ADAPTER_IF_STA]) { + err = ieee80211_output_pbuf(TCPIP_ADAPTER_IF_STA, outputbuf, p->len); + } else { + err = ieee80211_output_pbuf(TCPIP_ADAPTER_IF_AP, outputbuf, p->len); + } - err = ieee80211_output_pbuf(netif, q); if (err == ERR_MEM) { err = ERR_OK; } - q->next = tmp; - break; - } // signal that packet should be sent(); #if ETH_PAD_SIZE - pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ + pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ #endif - - LINK_STATS_INC(link.xmit); - return err; +#if LWIP_STATS + LINK_STATS_INC(link.xmit); +#endif + return err; } /** @@ -150,60 +123,65 @@ low_level_output(struct netif *netif, struct pbuf *p) * * @param netif the lwip network interface structure for this ethernetif */ -void -ethernetif_input(struct netif *netif, struct pbuf *p) +void ethernetif_input(struct netif* netif, struct pbuf* p) { - struct ethernetif *ethernetif; - struct eth_hdr *ethhdr; + struct eth_hdr* ethhdr; - if(p == NULL) - goto _exit; + if (p == NULL) { + TCPIP_ATAPTER_LOG("ERROR pbuf is NULL\n"); + goto _exit; + } - if(p->payload == NULL) { - pbuf_free(p); - goto _exit; - } + if (p->payload == NULL) { + TCPIP_ATAPTER_LOG("ERROR payload is NULL\n"); + pbuf_free(p); + goto _exit; + } - if(netif == NULL) { - goto _exit; - } + if (netif == NULL) { + TCPIP_ATAPTER_LOG("ERROR netif is NULL\n"); + goto _exit; + } - if (!(netif->flags & NETIF_FLAG_LINK_UP)) { - pbuf_free(p); - p = NULL; - goto _exit; - } + if (!(netif->flags & NETIF_FLAG_LINK_UP)) { + TCPIP_ATAPTER_LOG("ERROR netif is not up\n"); + pbuf_free(p); + p = NULL; + goto _exit; + } - ethernetif = netif->state; + /* points to packet payload, which starts with an Ethernet header */ + ethhdr = p->payload; - /* points to packet payload, which starts with an Ethernet header */ - ethhdr = p->payload; - - switch (htons(ethhdr->type)) { - /* IP or ARP packet? */ - case ETHTYPE_IP: - case ETHTYPE_IPV6: - case ETHTYPE_ARP: + switch (htons(ethhdr->type)) { + /* IP or ARP packet? */ + case ETHTYPE_IP: + case ETHTYPE_IPV6: + case ETHTYPE_ARP: #if PPPOE_SUPPORT - /* PPPoE packet? */ - case ETHTYPE_PPPOEDISC: - case ETHTYPE_PPPOE: -#endif /* PPPOE_SUPPORT */ - /* full packet send to tcpip_thread to process */ - if (netif->input(p, netif)!=ERR_OK) - { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); - pbuf_free(p); - p = NULL; - } - break; - default: - pbuf_free(p); - p = NULL; - break; - } + /* PPPoE packet? */ + case ETHTYPE_PPPOEDISC: + case ETHTYPE_PPPOE: +#endif /* PPPOE_SUPPORT */ + + /* full packet send to tcpip_thread to process */ + if (netif->input(p, netif) != ERR_OK) { + TCPIP_ATAPTER_LOG("ERROR IP input error\n"); + pbuf_free(p); + p = NULL; + } + + break; + + default: + pbuf_free(p); + p = NULL; + break; + } + _exit: -; + ; } /** @@ -216,59 +194,64 @@ _exit: * @param netif the lwip network interface structure for this ethernetif * @return ERR_OK if the loopif is initialized * ERR_MEM if private data couldn't be allocated - * any other err_t on error + * any other int8_t on error */ -err_t ethernetif_init(struct netif *netif) +int8_t ethernetif_init(struct netif* netif) { - LWIP_ASSERT("netif != NULL", (netif != NULL)); + uint8_t mac[NETIF_MAX_HWADDR_LEN]; - u8_t mac[NETIF_MAX_HWADDR_LEN]; + if (netif == NULL) { + TCPIP_ATAPTER_LOG("ERROR netif is NULL\n"); + } - /* set MAC hardware address */ - if ((struct netif *)wifi_get_netif(STATION_IF) == netif) { - wifi_get_macaddr(STATION_IF, mac); - } else { - wifi_get_macaddr(SOFTAP_IF, mac); - } - memcpy(netif->hwaddr, mac, NETIF_MAX_HWADDR_LEN); + /* set MAC hardware address */ + if (wifi_get_netif(TCPIP_ADAPTER_IF_STA) == TCPIP_ADAPTER_IF_STA) { + wifi_get_macaddr(TCPIP_ADAPTER_IF_STA, mac); + } else { + wifi_get_macaddr(TCPIP_ADAPTER_IF_AP, mac); + } + + memcpy(netif->hwaddr, mac, NETIF_MAX_HWADDR_LEN); #if LWIP_NETIF_HOSTNAME - if ((struct netif *)wifi_get_netif(STATION_IF) == netif) { - if (default_hostname == 1) { - wifi_station_set_default_hostname(netif->hwaddr); - } - /* Initialize interface hostname */ - netif->hostname = hostname; - } else { - netif->hostname = NULL; - } + if (wifi_get_netif(TCPIP_ADAPTER_IF_STA) == TCPIP_ADAPTER_IF_STA) { + if (default_hostname == 1) { + wifi_station_set_default_hostname(netif->hwaddr); + } + + /* Initialize interface hostname */ + netif->hostname = hostname; + } else { + netif->hostname = NULL; + } + #endif /* LWIP_NETIF_HOSTNAME */ - /* - * Initialize the snmp variables and counters inside the struct netif. - * The last argument should be replaced with your link speed, in units - * of bits per second. - */ - NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS); + /* + * Initialize the snmp variables and counters inside the struct netif. + * The last argument should be replaced with your link speed, in units + * of bits per second. + */ + NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS); - netif->name[0] = IFNAME0; - netif->name[1] = IFNAME1; - /* We directly use etharp_output() here to save a function call. - * You can instead declare your own function an call etharp_output() - * from it if you have to do some checks before sending (e.g. if link - * is available...) */ - netif->output = etharp_output; + netif->name[0] = IFNAME0; + netif->name[1] = IFNAME1; + /* We directly use etharp_output() here to save a function call. + * You can instead declare your own function an call etharp_output() + * from it if you have to do some checks before sending (e.g. if link + * is available...) */ + netif->output = etharp_output; #if LWIP_IPV6 - netif->output_ip6 = ethip6_output; + netif->output_ip6 = ethip6_output; #endif /* LWIP_IPV6 */ - netif->linkoutput = low_level_output; + netif->linkoutput = low_level_output; - extern void wifi_station_dhcpc_event(void); - netif->dhcp_event = wifi_station_dhcpc_event; + extern void wifi_station_dhcpc_event(void); + netif->dhcp_event = wifi_station_dhcpc_event; - /* initialize the hardware */ - low_level_init(netif); + /* initialize the hardware */ + low_level_init(netif); - return ERR_OK; + return ERR_OK; } diff --git a/components/tcpip_adapter/Kconfig b/components/tcpip_adapter/Kconfig new file mode 100755 index 00000000..86eb7a3c --- /dev/null +++ b/components/tcpip_adapter/Kconfig @@ -0,0 +1,7 @@ +menu "tcpip adapter" + +config TCPIP_ADAPER_DEBUG + bool "Enable tcpip adaptor debug" + default 1 + +endmenu diff --git a/components/tcpip_adapter/component.mk b/components/tcpip_adapter/component.mk new file mode 100644 index 00000000..98e2802f --- /dev/null +++ b/components/tcpip_adapter/component.mk @@ -0,0 +1,8 @@ +# +# Component Makefile +# +COMPONENT_ADD_INCLUDEDIRS += include + +COMPONENT_SRCDIRS := ./ + +CFLAGS += -DLWIP_OPEN_SRC -DMEMLEAK_DEBUG -U__FILE__ -D__FILE__='"$(subst $(dir $<),,$<)"' diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h new file mode 100644 index 00000000..e53fa6d8 --- /dev/null +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -0,0 +1,62 @@ +// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _TCPIP_ADAPTER_H_ +#define _TCPIP_ADAPTER_H_ + +#include "sdkconfig.h" +/** + * @brief TCPIP adapter library + * + * The aim of this adapter is to provide an abstract layer upon TCPIP stack. + * With this layer, switch to other TCPIP stack is possible and easy in ESP8266_RTOS_SDK. + * + * If users want to use other TCPIP stack, all those functions should be implemented + * by using the specific APIs of that stack. + * + * tcpip_adapter_init should be called in the start of app_main for only once. + * + * Currently most adapter APIs are called in event_default_handlers.c. + * + * We recommend users only use set/get IP APIs, DHCP server/client APIs, + * get free station list APIs in application side. Other APIs are used in ESP8266_RTOS_SDK internal, + * otherwise the state maybe wrong. + * + */ + +typedef enum { + TCPIP_ADAPTER_IF_STA = 0, /**< ESP8266 station interface */ + TCPIP_ADAPTER_IF_AP, /**< ESP8266 soft-AP interface */ + TCPIP_ADAPTER_IF_MAX +} tcpip_adapter_if_t; + +struct netif *esp_netif[TCPIP_ADAPTER_IF_MAX]; +char* hostname; +bool default_hostname; + +#define TCPIP_ADAPTER_IF_VALID(fd) ((fd < TCPIP_ADAPTER_IF_MAX) ? 1 : 0) + +/* Define those to better describe your network interface. */ +#define IFNAME0 'e' +#define IFNAME1 'n' + +#ifdef CONFIG_TCPIP_ADAPER_DEBUG +#define TAG "" +#define TCPIP_ATAPTER_LOG(str, ...) printf(TAG __FILE__ " line: %d " str, __LINE__, ##__VA_ARGS__) +#else +#define TCPIP_ATAPTER_LOG(str, ...) +#endif + + +#endif /* _TCPIP_ADAPTER_H_ */ \ No newline at end of file diff --git a/components/tcpip_adapter/tcpip_adapter_wifi.c b/components/tcpip_adapter/tcpip_adapter_wifi.c new file mode 100644 index 00000000..7202f0b9 --- /dev/null +++ b/components/tcpip_adapter/tcpip_adapter_wifi.c @@ -0,0 +1,491 @@ +// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "lwip/netif.h" +#include "lwip/tcpip.h" +#include "lwip/dhcp.h" +#include "lwip/dhcpserver.h" +#include "netif/etharp.h" +#include "esp_wifi.h" +#include "esp_timer.h" +#include "esp_misc.h" +#include "tcpip_adapter.h" + +/* Avoid warning. No header file has include these function */ +err_t ethernetif_init(struct netif* netif); +void system_station_got_ip_set(); +void netif_create_ip4_linklocal_address(struct netif* netif); + +static os_timer_t* get_ip_timer; +static uint8_t dhcp_fail_time; +static bool dhcps_flag = true; +static bool dhcpc_flag = true; +static struct ip_info esp_ip[TCPIP_ADAPTER_IF_MAX]; + +void esp_wifi_station_dhcpc_event(uint8_t netif_index) +{ + if (TCPIP_ADAPTER_IF_VALID(netif_index)) { + TCPIP_ATAPTER_LOG("wifi station dhcpc start\n"); + dhcp_stop(esp_netif[netif_index]); + dhcp_cleanup(esp_netif[netif_index]); + dhcp_inform(esp_netif[netif_index]); + } else { + TCPIP_ATAPTER_LOG("ERROR bad netif index:%d\n", netif_index); + } +} + +static void tcpip_adapter_dhcpc_done() +{ +#define DHCP_BOUND 10 + os_timer_disarm(get_ip_timer); + + if (esp_netif[TCPIP_ADAPTER_IF_STA]->dhcp->state == DHCP_BOUND) { + /*send event here*/ + system_station_got_ip_set(); + printf("ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR "\n", IP2STR(&(esp_netif[0]->ip_addr)), + IP2STR(&(esp_netif[0]->netmask)), IP2STR(&(esp_netif[0]->gw))); + } else if (dhcp_fail_time < 30) { + TCPIP_ATAPTER_LOG("dhcpc time(ms): %d\n", dhcp_fail_time * 200); + dhcp_fail_time ++; + os_timer_setfn(get_ip_timer, tcpip_adapter_dhcpc_done, NULL); + os_timer_arm(get_ip_timer, 200, 1); + } else { + TCPIP_ATAPTER_LOG("ERROR dhcp get ip error\n"); + free(get_ip_timer); + } +} + +static void tcpip_adapter_station_dhcp_start() +{ + err_t ret; + get_ip_timer = (os_timer_t*)malloc(sizeof(*get_ip_timer)); + + if (get_ip_timer == NULL) { + TCPIP_ATAPTER_LOG("ERROR NO MEMORY\n"); + } + + TCPIP_ATAPTER_LOG("dhcpc start\n"); + ret = dhcp_start(esp_netif[TCPIP_ADAPTER_IF_STA]); + dhcp_fail_time = 0; + + if (ret == 0) { + os_timer_disarm(get_ip_timer); + os_timer_setfn(get_ip_timer, tcpip_adapter_dhcpc_done, NULL); + os_timer_arm(get_ip_timer, 100, 1); + } +} + +void tcpip_adapter_start(uint8_t netif_index, bool authed) +{ + if (!TCPIP_ADAPTER_IF_VALID(netif_index)) { + TCPIP_ATAPTER_LOG("ERROR bad netif index:%d\n", netif_index); + return; + } + + TCPIP_ATAPTER_LOG("start netif[%d]\n", netif_index); + + if (netif_index == TCPIP_ADAPTER_IF_STA) { + if (authed == 0) { + if (esp_netif[netif_index] == NULL) { + esp_netif[netif_index] = (struct netif*)os_malloc(sizeof(*esp_netif[netif_index])); + TCPIP_ATAPTER_LOG("Malloc netif:%d\n", netif_index); + TCPIP_ATAPTER_LOG("Add netif:%d\n", netif_index); + netif_add(esp_netif[netif_index], NULL, NULL, NULL, NULL, ethernetif_init, tcpip_input); + } + } else { + if ((esp_netif[netif_index]->flags & NETIF_FLAG_DHCP) == 0) { + if (dhcpc_flag) { + printf("dhcp client start...\n"); + tcpip_adapter_station_dhcp_start(); + } else { + if (esp_ip[TCPIP_ADAPTER_IF_STA].ip.addr != 0) { + netif_set_addr(esp_netif[netif_index], &esp_ip[TCPIP_ADAPTER_IF_STA].ip, + &esp_ip[TCPIP_ADAPTER_IF_STA].netmask, &esp_ip[TCPIP_ADAPTER_IF_STA].gw); + netif_set_up(esp_netif[netif_index]); + system_station_got_ip_set(); + printf("ip: 0.0.0.0,mask: 0.0.0.0,gw: 0.0.0.0\n"); + } else { + printf("check your static ip\n"); + } + } + + } + + } + } else if (netif_index == TCPIP_ADAPTER_IF_AP) { + if (dhcps_flag) { + IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].ip, 192, 168 , 4, 1); + IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].gw, 192, 168 , 4, 1); + IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].netmask, 255, 255 , 255, 0); + } + + if (esp_netif[netif_index] == NULL) { + TCPIP_ATAPTER_LOG("Malloc netif:%d\n", netif_index); + esp_netif[netif_index] = (struct netif*)os_malloc(sizeof(*esp_netif[netif_index])); + netif_add(esp_netif[netif_index], &esp_ip[TCPIP_ADAPTER_IF_AP].ip, + &esp_ip[TCPIP_ADAPTER_IF_AP].netmask, &esp_ip[TCPIP_ADAPTER_IF_AP].gw, NULL, ethernetif_init, tcpip_input); + } + + if (dhcps_flag) { + dhcps_start(&esp_ip[TCPIP_ADAPTER_IF_AP]); + printf("dhcp server start:("); + printf("ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR, IP2STR(&(esp_netif[TCPIP_ADAPTER_IF_AP]->ip_addr)), + IP2STR(&(esp_netif[TCPIP_ADAPTER_IF_AP]->netmask)), IP2STR(&(esp_netif[TCPIP_ADAPTER_IF_AP]->gw))); + printf(")\n"); + } + + netif_set_up(esp_netif[netif_index]); + netif_set_default(esp_netif[netif_index]); + } + + uint8_t opmode = wifi_get_opmode(); + + if (opmode == STATION_MODE) { + netif_set_default(esp_netif[netif_index]); + } +} + +void tcpip_adapter_stop(uint8_t netif_index) +{ + if (!TCPIP_ADAPTER_IF_VALID(netif_index)) { + TCPIP_ATAPTER_LOG("ERROR bad netif index:%d\n", netif_index); + return; + } + if (esp_netif[netif_index == NULL]) + return; + + if (netif_index == TCPIP_ADAPTER_IF_STA) { + TCPIP_ATAPTER_LOG("dhcp stop netif index:%d\n", netif_index); + dhcp_stop(esp_netif[netif_index]); + } + + if (netif_index == TCPIP_ADAPTER_IF_AP) { + if(dhcps_flag){ + TCPIP_ATAPTER_LOG("dhcp stop netif index:%d\n", netif_index); + dhcps_stop(); + } + } + + TCPIP_ATAPTER_LOG("stop netif[%d]\n", netif_index); + netif_remove(esp_netif[netif_index]); + os_free(esp_netif[netif_index]); + esp_netif[netif_index] = NULL; +} + +void ieee80211_input(uint8_t netif_index, uint8_t* input, uint16_t len) +{ + struct pbuf* pb; + + if (!TCPIP_ADAPTER_IF_VALID(netif_index)) { + TCPIP_ATAPTER_LOG("ERROR bad netif index:%d\n", netif_index); + return; + } + + pb = pbuf_alloc(PBUF_MAC, len, PBUF_RAM); + + if (pb == NULL) { + TCPIP_ATAPTER_LOG("ERROR NO MEMORY\n"); + return; + } + + memcpy((uint8_t*)(pb->payload), (uint8_t*)input, len); + ethernet_input(pb, esp_netif[netif_index]); +} + +bool wifi_set_ip_info(WIFI_INTERFACE netif_index, struct ip_info* if_ip) +{ + if (!TCPIP_ADAPTER_IF_VALID((uint8_t)netif_index)) { + TCPIP_ATAPTER_LOG("ERROR bad netif index:%d\n", netif_index); + return false; + } + + TCPIP_ATAPTER_LOG("Set netif[%d] ip info\n", netif_index); + netif_set_addr(esp_netif[netif_index], &if_ip->ip, &if_ip->netmask, &if_ip->gw); + return true; +} + +bool wifi_get_ip_info(WIFI_INTERFACE netif_index, struct ip_info* if_ip) +{ + if (!TCPIP_ADAPTER_IF_VALID((uint8_t)netif_index)) { + TCPIP_ATAPTER_LOG("ERROR bad netif index:%d\n", netif_index); + return false; + } + + TCPIP_ATAPTER_LOG("Get netif[%d] ip info\n", netif_index); + if_ip->ip = esp_netif[netif_index]->ip_addr; + if_ip->netmask = esp_netif[netif_index]->netmask; + if_ip->gw = esp_netif[netif_index]->gw; + return true; +} + +bool wifi_create_linklocal_ip(uint8_t netif_index, bool ipv6) +{ + if (!TCPIP_ADAPTER_IF_VALID(netif_index)) { + TCPIP_ATAPTER_LOG("ERROR bad netif index:%d\n", netif_index); + return false; + } + + netif_create_ip4_linklocal_address(esp_netif[netif_index]); + return true; +} + +bool wifi_get_linklocal_ip(uint8_t netif_index, ipX_addr_t* linklocal) +{ + if (TCPIP_ADAPTER_IF_VALID(netif_index)) { + memcpy(linklocal, &esp_netif[netif_index]->link_local_addr, sizeof(*linklocal)); + } else { + TCPIP_ATAPTER_LOG("ERROR bad netif index:%d\n", netif_index); + return false; + } + + return true; +} + +bool wifi_get_ipinfo_v6(uint8_t netif_index, uint8_t ip_index, ipX_addr_t* ipv6) +{ +#if LWIP_IPV6 + + if (TCPIP_ADAPTER_IF_VALID(netif_index)) { + memcpy(ipv6, &esp_netif[netif_index]->ip6_addr[ip_index], sizeof(ip6_addr_t)); + } else { + TCPIP_ATAPTER_LOG("ERROR bad netif index:%d\n", netif_index); + return false; + } + +#endif + return true; +} + +bool wifi_softap_dhcps_start(void) +{ + uint8_t opmode = NULL_MODE; + TCPIP_ATAPTER_LOG("start softap dhcps\n"); + taskENTER_CRITICAL(); + opmode = wifi_get_opmode(); + + if ((opmode == STATION_MODE) || (opmode == NULL_MODE)) { + taskEXIT_CRITICAL(); + TCPIP_ATAPTER_LOG("ERROR you shoud enable wifi softap before start dhcp server\n"); + return false; + } + + if (dhcps_flag == false) { + struct ip_info ipinfo; + wifi_get_ip_info(SOFTAP_IF, &ipinfo); + TCPIP_ATAPTER_LOG("start softap dhcpserver\n"); + dhcps_start(&ipinfo); + } + + dhcps_flag = true; + taskEXIT_CRITICAL(); + return true; +} + +enum dhcp_status wifi_softap_dhcps_status() +{ + return dhcps_flag; +} + +void tcpip_adapter_sta_leave() +{ + TCPIP_ATAPTER_LOG("station leave\n"); + + if (esp_netif[TCPIP_ADAPTER_IF_STA] == NULL) { + return; + } + + netif_set_down(esp_netif[TCPIP_ADAPTER_IF_STA]); + + if (esp_netif[TCPIP_ADAPTER_IF_STA]->flags & NETIF_FLAG_DHCP) { + dhcp_release(esp_netif[TCPIP_ADAPTER_IF_STA]); + dhcp_stop(esp_netif[TCPIP_ADAPTER_IF_STA]); + dhcp_cleanup(esp_netif[TCPIP_ADAPTER_IF_STA]); + } + + ip_addr_set_zero(&esp_netif[TCPIP_ADAPTER_IF_STA]->ip_addr); + ip_addr_set_zero(&esp_netif[TCPIP_ADAPTER_IF_STA]->netmask); + ip_addr_set_zero(&esp_netif[TCPIP_ADAPTER_IF_STA]->gw); +} + +bool wifi_softap_dhcps_stop() +{ + uint8_t opmode = NULL_MODE; + taskENTER_CRITICAL(); + opmode = wifi_get_opmode(); + + if ((opmode == STATION_MODE) || (opmode == NULL_MODE)) { + taskEXIT_CRITICAL(); + TCPIP_ATAPTER_LOG("ERROR you shoud enable wifi softap before start dhcp server\n"); + return false; + } + + if (dhcps_flag == true) { + TCPIP_ATAPTER_LOG("dhcps stop\n"); + dhcps_stop(); + } + + dhcps_flag = false; + taskEXIT_CRITICAL(); + return true; + +} + +bool wifi_station_dhcpc_start() +{ + uint8_t opmode = NULL_MODE; + s8 ret; + taskENTER_CRITICAL(); + opmode = wifi_get_opmode(); + + if ((opmode == SOFTAP_MODE) || (opmode == NULL_MODE)) { + taskEXIT_CRITICAL(); + TCPIP_ATAPTER_LOG("ERROR you shoud enable wifi station mode before start dhcp client\n"); + return false; + } + + if (dhcpc_flag == false) { + if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_STA])) { + ip_addr_set_zero(&esp_netif[TCPIP_ADAPTER_IF_STA]->ip_addr); + ip_addr_set_zero(&esp_netif[TCPIP_ADAPTER_IF_STA]->netmask); + ip_addr_set_zero(&esp_netif[TCPIP_ADAPTER_IF_STA]->gw); + } else { + taskEXIT_CRITICAL(); + TCPIP_ATAPTER_LOG("ERROR please init station netif\n"); + return false; + } + + ret = dhcp_start(esp_netif[TCPIP_ADAPTER_IF_STA]); + + if (ret != ERR_OK) { + taskEXIT_CRITICAL(); + TCPIP_ATAPTER_LOG("ERROR start dhcp client failed.ret=%d\n", ret); + return false; + } + } + + dhcps_flag = true; + taskEXIT_CRITICAL(); + TCPIP_ATAPTER_LOG("dhcp client start\n"); + return true; +} + +bool wifi_station_dhcpc_stop() +{ + uint8_t opmode = NULL_MODE; + taskENTER_CRITICAL(); + opmode = wifi_get_opmode(); + + if ((opmode == SOFTAP_MODE) || (opmode == NULL_MODE)) { + taskEXIT_CRITICAL(); + TCPIP_ATAPTER_LOG("ERROR you shoud enable wifi station mode before stop dhcp client\n"); + return false; + } + + if (dhcpc_flag == true) { + dhcp_stop(esp_netif[TCPIP_ADAPTER_IF_STA]); + } else { + TCPIP_ATAPTER_LOG("WARING dhcp client have not start yet\n"); + } + + dhcpc_flag = false; + taskEXIT_CRITICAL(); + TCPIP_ATAPTER_LOG("stop dhcp client\n"); + return true; +} + +enum dhcp_status wifi_station_dhcpc_status() +{ + return dhcpc_flag; +} + +bool wifi_station_dhcpc_set_maxtry(uint8_t num) +{ + DHCP_MAXRTX = num; + return true; +} + +bool tcpip_adapter_set_macaddr(uint8_t netif_index, uint8_t* macaddr) +{ + if (esp_netif[netif_index] == NULL || macaddr == NULL) { + TCPIP_ATAPTER_LOG("set macaddr fail\n"); + return false; + } + + memcpy(esp_netif[netif_index]->hwaddr, macaddr, 6); + TCPIP_ATAPTER_LOG("set macaddr ok\n"); + return true; +} + +bool tcpip_adapter_get_macaddr(uint8_t netif_index, uint8_t* macaddr) +{ + if (esp_netif[netif_index] == NULL || macaddr == NULL) { + return false; + } + + if (esp_netif[netif_index]->hwaddr[0] == 0 && esp_netif[netif_index]->hwaddr[1] == 0 + && esp_netif[netif_index]->hwaddr[2] == 0 && esp_netif[netif_index]->hwaddr[3] == 0 + && esp_netif[netif_index]->hwaddr[4] == 0 && esp_netif[netif_index]->hwaddr[5] == 0) + return false; + + memcpy(macaddr, esp_netif[netif_index]->hwaddr, 6); + return true; +} + + +bool wifi_station_set_hostname(char* name) +{ + if (name == NULL) { + return false; + } + + uint32 len = strlen(name); + + if (len > 32) { + return false; + } + + uint8_t opmode = wifi_get_opmode(); + + if (opmode == STATION_MODE || opmode == STATIONAP_MODE) { + default_hostname = 0; + + if (hostname != NULL) { + free(hostname); + hostname = NULL; + } + + hostname = (char*)malloc(len + 1); + + if (hostname != NULL) { + strcpy(hostname, name); + esp_netif[opmode - 1]->hostname = hostname; + } else { + return false; + } + } else { + return false; + } + + return true; +} + +struct netif* eagle_lwip_getif(uint8_t netif_index) +{ + if (TCPIP_ADAPTER_IF_VALID(netif_index)) { + return esp_netif[netif_index]; + } else { + TCPIP_ATAPTER_LOG("ERROR bad netif index:%d\n", netif_index); + return NULL; + } +} \ No newline at end of file