Merge branch 'refactor' into 'master'

Make internal libs independent with lwip

See merge request sdk/ESP8266_RTOS_SDK!67
This commit is contained in:
Wu Jian Gang
2018-04-17 21:39:55 +08:00
23 changed files with 1606 additions and 1157 deletions

22
VERSION
View File

@ -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

View File

@ -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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -3,6 +3,8 @@
#define USE_DNS
#include "esp_wifi.h"
typedef struct dhcps_state {
s16_t state;
} dhcps_state;
@ -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

View File

@ -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

View File

@ -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_ */

View File

@ -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_ */

View File

@ -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);
}

View File

@ -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,12 +23,13 @@
#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};
@ -43,9 +58,10 @@ 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)
if (*phead == NULL) {
*phead = pinsert;
else {
} else {
plist = *phead;
pdhcps_node = pinsert->pnode;
pdhcps_pool = plist->pnode;
@ -56,11 +72,13 @@ void node_insert_to_list(list_node **phead, list_node* 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;
}
@ -69,6 +87,7 @@ void node_insert_to_list(list_node **phead, list_node* pinsert)
}
}
}
// pinsert->pnext = NULL;
}
@ -83,6 +102,7 @@ void node_remove_from_list(list_node **phead, list_node* pdelete)
list_node* plist = NULL;
plist = *phead;
if (plist == NULL) {
*phead = NULL;
} else {
@ -95,6 +115,7 @@ void node_remove_from_list(list_node **phead, list_node* pdelete)
plist->pnext = pdelete->pnext;
pdelete->pnext = NULL;
}
plist = plist->pnext;
}
}
@ -285,7 +306,9 @@ static void send_offer(struct dhcps_msg *m)
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);
@ -296,6 +319,7 @@ static void send_offer(struct dhcps_msg *m)
#if DHCPS_DEBUG
os_printf("udhcp: send_offer>>p->ref = %d\n", p->ref);
#endif
if (p != NULL) {
#if DHCPS_DEBUG
@ -304,16 +328,19 @@ static void send_offer(struct dhcps_msg *m)
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; i<q->len; i++)
{
for (i = 0; i < q->len; i++) {
data[i] = ((u8_t*) m)[cnt++];
#if DHCPS_DEBUG
os_printf("%02x ", data[i]);
if ((i + 1) % 16 == 0) {
os_printf("\n");
}
#endif
}
@ -326,10 +353,14 @@ static void send_offer(struct dhcps_msg *m)
#endif
return;
}
SendOffer_err_t = udp_sendto( pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT );
#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");
@ -352,7 +383,9 @@ static void send_nak(struct dhcps_msg *m)
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);
@ -362,6 +395,7 @@ static void send_nak(struct dhcps_msg *m)
#if DHCPS_DEBUG
os_printf("udhcp: send_nak>>p->ref = %d\n", p->ref);
#endif
if (p != NULL) {
#if DHCPS_DEBUG
@ -370,16 +404,19 @@ static void send_nak(struct dhcps_msg *m)
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; i<q->len; i++)
{
for (i = 0; i < q->len; i++) {
data[i] = ((u8_t*) m)[cnt++];
#if DHCPS_DEBUG
os_printf("%02x ", data[i]);
if ((i + 1) % 16 == 0) {
os_printf("\n");
}
#endif
}
@ -392,10 +429,14 @@ static void send_nak(struct dhcps_msg *m)
#endif
return;
}
SendNak_err_t = udp_sendto( pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT );
#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");
@ -418,7 +459,9 @@ static void send_ack(struct dhcps_msg *m)
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);
@ -429,6 +472,7 @@ static void send_ack(struct dhcps_msg *m)
#if DHCPS_DEBUG
os_printf("udhcp: send_ack>>p->ref = %d\n", p->ref);
#endif
if (p != NULL) {
#if DHCPS_DEBUG
@ -437,16 +481,19 @@ static void send_ack(struct dhcps_msg *m)
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; i<q->len; i++)
{
for (i = 0; i < q->len; i++) {
data[i] = ((u8_t*) m)[cnt++];
#if DHCPS_DEBUG
os_printf("%02x ", data[i]);
if ((i + 1) % 16 == 0) {
os_printf("\n");
}
#endif
}
@ -459,9 +506,12 @@ static void send_ack(struct dhcps_msg *m)
#endif
return;
}
SendAck_err_t = udp_sendto( pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT );
#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) {
@ -498,6 +548,7 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
#if DHCPS_DEBUG
os_printf("dhcps: (s16_t)*optptr = %d\n", (s16_t)*optptr);
#endif
switch ((s16_t) *optptr) {
case DHCP_OPTION_MSG_TYPE: //53
@ -516,9 +567,10 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
#endif
s.state = DHCPS_STATE_NAK;
}
break;
case DHCP_OPTION_END:
{
case DHCP_OPTION_END: {
is_dhcp_parse_end = true;
}
break;
@ -547,10 +599,12 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
} else {
s.state = DHCPS_STATE_NAK;
}
#if DHCPS_DEBUG
os_printf("dhcps: DHCPD_STATE_NAK\n");
#endif
}
break;
case DHCPDECLINE://4
@ -567,6 +621,7 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
#endif
break;
}
#if DHCPS_DEBUG
os_printf("dhcps: return s.state = %d\n", s.state);
#endif
@ -577,6 +632,7 @@ static u8_t parse_options(u8_t *optptr, s16_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) {
@ -621,17 +677,20 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
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) {
// 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;
@ -662,6 +721,7 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
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;
@ -675,6 +735,7 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
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 {
@ -685,6 +746,7 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
}
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);
@ -696,6 +758,7 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
os_free(pdhcps_pool);
pdhcps_pool = NULL;
}
// client_address_plus.addr = dhcps_lease.start_ip.addr;
return 4;
}
@ -713,12 +776,14 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
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 DHCPS_DEBUG
@ -731,6 +796,7 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
// }
return ret;
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////
@ -761,13 +827,18 @@ static void handle_dhcp(void *arg,
#if DHCPS_DEBUG
os_printf("dhcps: handle_dhcp-> receive a packet\n");
#endif
if (p==NULL) return;
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;
@ -781,9 +852,11 @@ static void handle_dhcp(void *arg,
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
}
@ -795,13 +868,16 @@ static void handle_dhcp(void *arg,
#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
}
}
@ -821,21 +897,25 @@ static void handle_dhcp(void *arg,
#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
@ -849,16 +929,19 @@ 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;
@ -870,10 +953,12 @@ static void wifi_softap_init_dhcps_lease(u32_t ip)
local_ip = softap_ip = htonl(ip);
softap_ip &= 0xFFFFFF00;
local_ip &= 0xFF;
if (local_ip >= 0x80)
if (local_ip >= 0x80) {
local_ip -= DHCPS_MAX_LEASE;
else
} else {
local_ip ++;
}
bzero(&dhcps_lease, sizeof(dhcps_lease));
dhcps_lease.start_ip.addr = softap_ip | local_ip;
@ -881,22 +966,24 @@ static void wifi_softap_init_dhcps_lease(u32_t ip)
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)
{
struct netif * apnetif = NULL;
apnetif = (struct netif *)wifi_get_netif(SOFTAP_IF);
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;
IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255);
@ -914,9 +1001,10 @@ void dhcps_start(struct ip_info *info)
void dhcps_stop(void)
{
struct netif * apnetif = (struct netif *)wifi_get_netif(0x01);
struct netif* apnetif = esp_netif[0x01];
udp_disconnect(pcb_dhcps);
// dhcps_lease_flag = true;
if (apnetif->dhcps_pcb != NULL) {
udp_remove(apnetif->dhcps_pcb);
@ -927,6 +1015,7 @@ void dhcps_stop(void)
list_node* pnode = NULL;
list_node* pback_node = NULL;
pnode = plist;
while (pnode != NULL) {
pback_node = pnode;
pnode = pback_node->pnext;
@ -951,8 +1040,9 @@ bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please)
return false;
}
if (please == NULL || wifi_softap_dhcps_status() == DHCP_STARTED)
if (please == NULL || wifi_softap_dhcps_status() == DHCP_STARTED) {
return false;
}
if (please->enable) {
bzero(&info, sizeof(struct ip_info));
@ -962,18 +1052,21 @@ bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please)
end_ip = htonl(please->end_ip.addr);
/*config ip information can't contain local ip*/
if ((start_ip <= softap_ip) && (softap_ip <= end_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)
if (end_ip - start_ip > DHCPS_MAX_LEASE) {
return false;
}
bzero(&dhcps_lease, sizeof(dhcps_lease));
// dhcps_lease.start_ip.addr = start_ip;
@ -981,6 +1074,7 @@ bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please)
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;
@ -1001,13 +1095,17 @@ bool wifi_softap_get_dhcps_lease(struct dhcps_lease *please)
return false;
}
if (NULL == please)
if (NULL == please) {
return false;
}
if (dhcps_lease.enable == FALSE) {
if (wifi_softap_dhcps_status() == DHCP_STOPPED)
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;
@ -1022,16 +1120,20 @@ static void kill_oldest_dhcps_pool(void)
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;
@ -1046,9 +1148,11 @@ void dhcps_coarse_tmr(void)
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;
@ -1063,29 +1167,34 @@ void dhcps_coarse_tmr(void)
}
}
if (num_dhcps_pool >= MAX_STATION_NUM)
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;
if (level <= OFFER_START || level >= OFFER_END)
if (optarg == NULL && wifi_softap_dhcps_status() == false) {
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;
}
@ -1104,6 +1213,7 @@ bool wifi_softap_set_dhcps_lease_time(u32_t minute)
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;
}

View File

@ -357,7 +357,6 @@ 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);
@ -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 {

View File

@ -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 <adam@sics.se>
*
*/
// 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,9 +31,13 @@ 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)
{
if (netif == NULL) {
TCPIP_ATAPTER_LOG("ERROR netif is NULL\n");
return;
}
/* set MAC hardware address length */
netif->hwaddr_len = ETHARP_HWADDR_LEN;
@ -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,33 +70,37 @@ 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 */
#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();
@ -136,8 +108,9 @@ low_level_output(struct netif *netif, struct pbuf *p)
pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif
#if LWIP_STATS
LINK_STATS_INC(link.xmit);
#endif
return err;
}
@ -150,32 +123,33 @@ 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;
if(p == NULL)
if (p == NULL) {
TCPIP_ATAPTER_LOG("ERROR pbuf is NULL\n");
goto _exit;
}
if (p->payload == NULL) {
TCPIP_ATAPTER_LOG("ERROR payload is NULL\n");
pbuf_free(p);
goto _exit;
}
if (netif == NULL) {
TCPIP_ATAPTER_LOG("ERROR netif is NULL\n");
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;
@ -185,16 +159,19 @@ ethernetif_input(struct netif *netif, struct pbuf *p)
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"));
if (netif->input(p, netif) != ERR_OK) {
TCPIP_ATAPTER_LOG("ERROR IP input error\n");
pbuf_free(p);
p = NULL;
}
break;
default:
@ -202,6 +179,7 @@ ethernetif_input(struct netif *netif, struct pbuf *p)
p = NULL;
break;
}
_exit:
;
}
@ -216,24 +194,28 @@ _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);
if (wifi_get_netif(TCPIP_ADAPTER_IF_STA) == TCPIP_ADAPTER_IF_STA) {
wifi_get_macaddr(TCPIP_ADAPTER_IF_STA, mac);
} else {
wifi_get_macaddr(SOFTAP_IF, mac);
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 (wifi_get_netif(TCPIP_ADAPTER_IF_STA) == TCPIP_ADAPTER_IF_STA) {
if (default_hostname == 1) {
wifi_station_set_default_hostname(netif->hwaddr);
}
@ -243,6 +225,7 @@ err_t ethernetif_init(struct netif *netif)
} else {
netif->hostname = NULL;
}
#endif /* LWIP_NETIF_HOSTNAME */
/*

View File

@ -0,0 +1,7 @@
menu "tcpip adapter"
config TCPIP_ADAPER_DEBUG
bool "Enable tcpip adaptor debug"
default 1
endmenu

View File

@ -0,0 +1,8 @@
#
# Component Makefile
#
COMPONENT_ADD_INCLUDEDIRS += include
COMPONENT_SRCDIRS := ./
CFLAGS += -DLWIP_OPEN_SRC -DMEMLEAK_DEBUG -U__FILE__ -D__FILE__='"$(subst $(dir $<),,$<)"'

View File

@ -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_ */

View File

@ -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;
}
}