LwIP v2 support

This commit is contained in:
Our Air Quality 2017-06-06 12:47:21 +10:00
parent 1cfded6389
commit cd23acaa4a
39 changed files with 1357 additions and 664 deletions

View file

@ -3,8 +3,306 @@
Copyright (C) 2015 Espressif Systems. Derived from MIT Licensed SDK libraries.
BSD Licensed as described in the file LICENSE
*/
#include "open_esplibs.h"
#if OPEN_LIBNET80211_HOSTAP
// The contents of this file are only built if OPEN_LIBNET80211_HOSTAP is set to true
#endif /* OPEN_LIBNET80211_HOSTAP */
#include <string.h>
#include "tcpip.h"
#include "espressif/esp_wifi.h"
#include "espressif/esp_misc.h"
#include "etstimer.h"
#include "esplibs/libmain.h"
#include "esplibs/libnet80211.h"
#include "esplibs/libpp.h"
#include "esplibs/libwpa.h"
static uint8_t hostap_flags = 0;
static ETSTimer hostap_timer;
static struct esf_buf *hostap_timer_parg = NULL;
void IRAM *zalloc(size_t nbytes);
static void IRAM hostap_timer_func(struct esf_buf *esf_buf) {
struct sdk_cnx_node *cnx_node = sdk_g_ic.v.softap_netif_info->cnx_nodes[0];
int32_t mode = sdk_wifi_get_phy_mode();
uint8_t *frame = esf_buf->frame;
*(uint16_t *)(frame + 22) = (cnx_node->_unknown9c - 1) << 4;
cnx_node->_unknown9c += 1;
if (sdk_g_ic.s.wifi_led_enable) {
uint32_t gpio = sdk_g_ic.s.wifi_led_gpio;
uint32_t state = sdk_g_ic.s.wifi_led_state;
sdk_gpio_output_set(state << gpio, (((state & 1) == 0) ? 1 : 0) << gpio,
1 << gpio, 0);
sdk_g_ic.s.wifi_led_state = (state & 1) ? 0 : 1;
}
uint8_t *frame2 = frame + sdk_g_ic.s._unknown288 + (mode == 1 ? 23 : 27) ;
memcpy(frame2 + 29, &sdk_g_ic.v._unknown1d2, 1);
uint32_t v1 = frame2[26];
if (v1 == 0) {
v1 = frame2[27];
}
frame2[26] = v1 - 1;
uint32_t v2 = sdk_ieee80211_chan2ieee(sdk_g_ic.v._unknown14c);
frame2[23] = v2;
sdk_g_ic.s._unknown30d = v2;
int32_t v3 = *((volatile int32_t *)0x3ff20c00); // mactime
*(uint32_t *)(esf_buf->extra + 16) = v3;
*(uint32_t *)(frame + 24) = v3;
*(uint32_t *)(frame + 28) = 0;
if (sdk_chm_check_same_channel()) {
hostap_flags |= 1;
sdk_ppTxPkt(esf_buf);
return;
}
sdk_ets_timer_disarm(&hostap_timer);
sdk_ets_timer_arm(&hostap_timer, sdk_wDev_Get_Next_TBTT(), 0);
}
static void IRAM hostap_tx_callback() {
uint32_t flags = hostap_flags & 0xfe;
if (sdk_TmpSTAAPCloseAP == 0) {
hostap_flags = flags;
sdk_ets_timer_disarm(&hostap_timer);
sdk_ets_timer_arm(&hostap_timer, sdk_wDev_Get_Next_TBTT(), 0);
return;
}
sdk_PendFreeBcnEb = 1;
if (flags & 2) {
hostap_flags = flags & 0xfd;
sdk_wifi_softap_start();
return;
}
hostap_flags = flags;
}
static void hostap_attach_misc() {
struct sdk_g_ic_netif_info *netif_info = sdk_g_ic.v.softap_netif_info;
struct _unknown_softap1 *ptr1 = zalloc(28); // 0x1c
netif_info->_unknownb4 = ptr1;
struct _unknown_softap2 *ptr2 = zalloc(204); // 0xcc
ptr1->_unknown04 = ptr2;
struct _unknown_wpa1 *ptr3 = zalloc(76); // 0x4c
uint32_t v1 = sdk_g_ic.s._unknown30e;
if (v1 == 2) {
ptr3->_unknown00 = 1;
} else if (v1 == 3) {
ptr3->_unknown00 = 2;
} else if (v1 == 4) {
ptr3->_unknown00 = 3;
}
ptr3->_unknown28 = 2;
ptr3->_unknown04 = 2;
ptr3->_unknown0c = 2;
ptr3->_unknown08 = 10;
ptr3->_unknown20 = 10;
int s1 = (sdk_g_ic.s._unknown28a << 16) | sdk_g_ic.s._unknown288;
memcpy(&ptr2->_unknown10, &sdk_g_ic.s._unknown28c, s1);
ptr2->_unknown30 = s1;
uint8_t *ptr4 = zalloc(64); // 0x40
ptr2->_unknown3c = ptr4;
char *str1 = sdk_g_ic.s._unknown2ac;
memcpy(ptr4, str1, strlen(str1));
struct sdk_cnx_node *cnx_node = netif_info->cnx_nodes[0];
ptr2->_unknownb4 = 300;
netif_info->_unknown4c = 3;
netif_info->_unknown48 |= 16;
cnx_node->_unknown08 |= 1;
sdk_hostapd_setup_wpa_psk(ptr2);
struct netif *netif = netif_info->netif;
ptr1->_unknown18 = sdk_wpa_init(&netif->hwaddr, ptr3, 0);
free(ptr3);
}
static void softap_stop_free() {
struct sdk_g_ic_netif_info *netif_info = sdk_g_ic.v.softap_netif_info;
netif_info->_unknown4c = 0;
netif_info->_unknown48 &= 0xffffffef;
netif_info->cnx_nodes[0]->_unknown08 = 0;
struct _unknown_softap1 *unkb4 = netif_info->_unknownb4;
if (!unkb4) return;
uint32_t *unk18 = unkb4->_unknown18;
if (unk18) {
uint32_t *ptr1 = ((uint32_t **)unk18)[20];
if (ptr1)
free(ptr1);
uint32_t *ptr2 = *(uint32_t **)unk18;
if (ptr2)
free(ptr2);
free(unk18);
}
struct _unknown_softap2 *unk04 = unkb4->_unknown04;
if (unk04) {
uint32_t *unk38 = unk04->_unknown38;
if (unk38)
free(unk38);
uint8_t *unk3c = unk04->_unknown3c;
if (unk3c)
free(unk3c);
free(unk04);
}
free(unkb4);
netif_info->_unknownb4 = NULL;
}
void sdk_ieee80211_hostap_attach(struct sdk_g_ic_st *ic) {
uint32_t scratch[12]; // ??
struct sdk_g_ic_netif_info *netif_info = ic->v.softap_netif_info;
uint32_t v1 = ic->s._unknown30e;
if (v1 >= 2 && v1 < 5)
hostap_attach_misc();
struct netif *netif = netif_info->netif;
sdk_ic_bss_info_update(1, &netif->hwaddr, 2, 100);
ic->v._unknown1d0 = 0;
netif_info->_unknown3c = 5;
sdk_ppRegisterTxCallback(hostap_tx_callback, 4);
hostap_timer_parg = sdk_ieee80211_beacon_alloc(netif_info, scratch);
sdk_ets_timer_disarm(&hostap_timer);
sdk_ets_timer_setfn(&hostap_timer, (ETSTimerFunc *)hostap_timer_func, hostap_timer_parg);
sdk_wDev_Reset_TBTT();
sdk_ets_timer_arm(&hostap_timer, sdk_wDev_Get_Next_TBTT(), 0);
}
bool sdk_wifi_softap_start() {
struct sdk_g_ic_netif_info *netif_info = sdk_g_ic.v.softap_netif_info;
if (!netif_info) return 0;
if (netif_info->started) return 1;
uint8_t flags = hostap_flags;
if (flags & 1) {
hostap_flags = flags | 2;
return 1;
}
uint8_t (*mac_addr)[6] = &sdk_info.softap_mac_addr;
if (!netif_info->netif) {
struct netif *netif = (struct netif *)malloc(sizeof(struct netif));
netif_info->netif = netif;
memcpy(&netif->hwaddr, mac_addr, 6);
netif_add(netif, &sdk_info.softap_ipaddr, &sdk_info.softap_netmask,
&sdk_info.softap_gw, netif_info, ethernetif_init, tcpip_input);
}
sdk_ic_set_vif(1, 1, mac_addr, 1, 0);
netif_set_up(netif_info->netif);
if (sdk_wifi_get_opmode() != 3 ||
!sdk_g_ic.v.station_netif_info ||
sdk_g_ic.v.station_netif_info->_unknown3c < 2) {
uint32_t i1 = (sdk_g_ic.s._unknown30d - 1) & 0xff;
int nmi_on = sdk_NMIIrqIsOn;
if (!nmi_on) {
vPortEnterCritical();
do {
DPORT.DPORT0 = DPORT.DPORT0 & 0xffffffe0;
} while (DPORT.DPORT0 & 1);
}
// current channel?
uint32_t *chan = &sdk_g_ic.v._unknown84[i1 * 3];
sdk_g_ic.v._unknown14c = chan;
if (!nmi_on) {
DPORT.DPORT0 = (DPORT.DPORT0 & 0xffffffe0) | 1;
vPortExitCritical();
}
sdk_chm_set_current_channel(chan);
}
sdk_ieee80211_hostap_attach(&sdk_g_ic);
sdk_TmpSTAAPCloseAP = 0;
netif_info->started = 1;
return 1;
}
bool sdk_wifi_softap_stop() {
struct sdk_g_ic_netif_info *netif_info = sdk_g_ic.v.softap_netif_info;
if (!netif_info)
return 0;
if (!netif_info->started)
return 1;
uint32_t end = sdk_g_ic.s._unknown310 + 2;
uint32_t count = 1;
// Note this defensive test seems dead code, the value is loaded
// as a uint8_t value so adding 2 ensures this test always passes.
if (end >= 2) {
do {
struct sdk_cnx_node *cnx_node = netif_info->cnx_nodes[count];
if (cnx_node) {
struct sdk_cnx_node *cnx_node2 = netif_info->_unknown88;
netif_info->_unknown88 = cnx_node;
sdk_ieee80211_send_mgmt(netif_info, 160, 4);
sdk_ieee80211_send_mgmt(netif_info, 192, 2);
netif_info->_unknown88 = cnx_node2;
sdk_cnx_node_leave(netif_info, netif_info->cnx_nodes[count]);
// Number of entries might have changed, perhaps
// should have if one was removed above?
end = sdk_g_ic.s._unknown310 + 2;
}
count++;
} while (count < end);
}
netif_set_down(netif_info->netif);
sdk_TmpSTAAPCloseAP = 1;
sdk_ets_timer_disarm(&hostap_timer);
sdk_ic_bss_info_update(1, &sdk_info.softap_mac_addr, 2, 0);
sdk_ic_set_vif(1, 0, NULL, 1, 0);
softap_stop_free();
if ((hostap_flags & 1) == 0)
sdk_esf_buf_recycle(hostap_timer_parg, 4);
netif_info->started = 0;
return 1;
}

View file

@ -3,9 +3,6 @@
Copyright (C) 2015 Espressif Systems. Derived from MIT Licensed SDK libraries.
BSD Licensed as described in the file LICENSE
*/
#include "open_esplibs.h"
#if OPEN_LIBNET80211_INPUT
// The contents of this file are only built if OPEN_LIBNET80211_INPUT is set to true
#include "esplibs/libpp.h"
@ -14,10 +11,9 @@ void IRAM sdk_ieee80211_deliver_data(struct sdk_g_ic_netif_info *netif_info, str
if (netif->flags & NETIF_FLAG_LINK_UP) {
uint16_t length = esf_buf->length;
struct pbuf *pbuf = pbuf_alloc(PBUF_RAW, length, PBUF_REF);
pbuf->payload = esf_buf->pbuf2->payload;
struct pbuf *pbuf = pbuf_alloc_reference(esf_buf->pbuf2->payload, length, PBUF_ALLOC_FLAG_RX | PBUF_TYPE_ALLOC_SRC_MASK_ESP_RX);
esf_buf->pbuf1 = pbuf;
pbuf->eb = (void *)esf_buf;
pbuf->esf_buf = (void *)esf_buf;
ethernetif_input(netif, pbuf);
return;
}
@ -27,5 +23,3 @@ void IRAM sdk_ieee80211_deliver_data(struct sdk_g_ic_netif_info *netif_info, str
return;
}
#endif /* OPEN_LIBNET80211_INPUT */

View file

@ -3,9 +3,6 @@
Copyright (C) 2015 Espressif Systems. Derived from MIT Licensed SDK libraries.
BSD Licensed as described in the file LICENSE
*/
#include "open_esplibs.h"
#if OPEN_LIBNET80211_STA
// The contents of this file are only built if OPEN_LIBNET80211_STA is set to true
#include <string.h>
#include "esplibs/libmain.h"
@ -72,5 +69,3 @@ bool sdk_wifi_station_stop() {
return 1;
}
#endif /* OPEN_LIBNET80211_STA */

View file

@ -3,18 +3,29 @@
Copyright (C) 2015 Espressif Systems. Derived from MIT Licensed SDK libraries.
BSD Licensed as described in the file LICENSE
*/
#include "open_esplibs.h"
#if OPEN_LIBNET80211_WL_CNX
// The contents of this file are only built if OPEN_LIBNET80211_WL_CNX is set to true
#include "espressif/esp_misc.h"
#include "esplibs/libnet80211.h"
#include "esplibs/libpp.h"
#include "esplibs/libwpa.h"
#include <string.h>
#include "lwip/dhcp.h"
ETSTimer sdk_sta_con_timer;
void *sdk_g_cnx_probe_rc_list_cb;
/*
* Called from the ESP sdk_cnx_sta_leave function. Split out via a hack to the
* binary library to allow modification to track changes to lwip, for example
* changes to the offset of the netif->flags removal of the NETIF_FLAG_DHCP flag
* lwip v2 etc.
*/
void dhcp_if_down(struct netif *netif)
{
dhcp_release_and_stop(netif);
netif_set_down(netif);
}
#if 0
// Most of the code in this file assesses static data so it will be all or none.
@ -24,7 +35,6 @@ static uint8_t Ldata004;
static uint32_t Ldata006;
static void *Ldate007;
// Use of the netif->flags and the NETIF_FLAG_DHCP flag removed in lwip v2.
void sdk_cnx_sta_leave(struct sdk_g_ic_netif_info *netif_info, void *arg1) {
struct netif *netif = netif_info->netif;
@ -32,13 +42,9 @@ void sdk_cnx_sta_leave(struct sdk_g_ic_netif_info *netif_info, void *arg1) {
uint16_t v1 = *(uint16_t *)(arg1 + 0x1a) & 0xfff;
sdk_ic_set_sta(0, 0, arg1, 0, v1, phy_type, 0, 0);
netif_set_down(netif);
// The NETIF_FLAG_DHCP flags is removed in lwip v2?
if (netif->flags & 0x8) {
dhcp_release(netif);
dhcp_stop(netif);
}
// Note the SDK binary was modified here as it made use of the
// netif flags which changed in lwip v2.
dhcp_if_down(netif);
uint32_t v2 = *(uint8_t *)(arg1 + 0xe8);
free(sdk_g_ic.v._unknown190[v2]);
@ -77,7 +83,30 @@ void sdk_cnx_sta_leave(struct sdk_g_ic_netif_info *netif_info, void *arg1) {
}
#endif
void IRAM *sdk_cnx_node_search(uint8_t mac[6])
void sdk_cnx_node_remove(struct sdk_cnx_node *cnx_node) {
const uint32_t num = sdk_g_ic.s._unknown310 + 2;
if ((int32_t)num < (int32_t)2) {
return;
}
struct sdk_g_ic_netif_info *netif_info = sdk_g_ic.v.softap_netif_info;
uint32_t i = 1;
do {
if (netif_info->cnx_nodes[i] == cnx_node) {
uint32_t v2 = cnx_node->_unknowne8;
sdk_ic_remove_key(v2 + 2);
sdk_wpa_auth_sta_deinit(cnx_node->_unknowne4);
free(sdk_g_ic.v._unknown190[v2]);
sdk_g_ic.v._unknown190[v2] = NULL;
free(cnx_node);
netif_info->cnx_nodes[i] = NULL;
return;
}
i += 1;
} while (i < num);
}
struct sdk_cnx_node *sdk_cnx_node_search(uint8_t mac[6])
{
int end = sdk_g_ic.s._unknown310 + 2;
@ -86,15 +115,15 @@ void IRAM *sdk_cnx_node_search(uint8_t mac[6])
if (end < 1)
return NULL;
struct sdk_netif_conninfo **conninfo = sdk_g_ic.v.softap_netif_info->conninfo;
struct sdk_cnx_node **cnx_nodes = sdk_g_ic.v.softap_netif_info->cnx_nodes;
int i = 0;
do {
struct sdk_netif_conninfo *info = conninfo[i];
struct sdk_cnx_node *cnx_node = cnx_nodes[i];
if (info) {
if (memcmp(mac, info->mac_addr, 6) == 0) {
return info;
if (cnx_node) {
if (memcmp(mac, cnx_node->mac_addr, 6) == 0) {
return cnx_node;
}
}
i++;
@ -102,5 +131,3 @@ void IRAM *sdk_cnx_node_search(uint8_t mac[6])
return NULL;
}
#endif /* OPEN_LIBNET80211_WL_CNX */