Skip to content

Commit 9c748ff

Browse files
authored
Merge pull request #134 from pennam/lwip-wifi-fix
Fix WiFiStation buffer management
2 parents 00f90bd + 33b975a commit 9c748ff

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

Diff for: libraries/ESPhost/src/CEspCommunication.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ bool CEspCom::getMsgForStation(CMsg &msg) {
137137
return false;
138138
}
139139

140+
/* -------------------------------------------------------------------------- */
141+
int CEspCom::peekMsgSizeForStation() {
142+
/* -------------------------------------------------------------------------- */
143+
if(CEspCom::rxStationQueue.size() > 0) {
144+
return CEspCom::rxStationQueue.front().get_size();
145+
}
146+
return 0;
147+
}
148+
140149
/* -------------------------------------------------------------------------- */
141150
bool CEspCom::getMsgForSoftAp(CMsg &msg) {
142151
/* -------------------------------------------------------------------------- */

Diff for: libraries/ESPhost/src/CEspCommunication.h

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class CEspCom {
6060
static bool storeStationMsg(CMsg &msg);
6161
static bool storeSoftApMsg(CMsg &msg);
6262
static bool getMsgForStation(CMsg &msg);
63+
static int peekMsgSizeForStation();
6364
static bool getMsgForSoftAp(CMsg &msg);
6465

6566
static void clearStationRx();

Diff for: libraries/ESPhost/src/CEspControl.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,11 @@ int CEspControl::process_test_messages(CCtrlMsgWrapper* response) {
6868
return 0;
6969
}
7070

71-
7271
/* -------------------------------------------------------------------------- */
7372
uint8_t *CEspControl::getStationRx(uint8_t &if_num, uint16_t &dim) {
7473
/* -------------------------------------------------------------------------- */
7574
uint8_t *rv = nullptr;
76-
CMsg msg;
75+
CMsg msg;
7776
__disable_irq();
7877
bool res = CEspCom::getMsgForStation(msg);
7978
if(!res) {
@@ -91,6 +90,16 @@ uint8_t *CEspControl::getStationRx(uint8_t &if_num, uint16_t &dim) {
9190
return rv;
9291
}
9392

93+
/* -------------------------------------------------------------------------- */
94+
uint16_t CEspControl::peekStationRxMsgSize() {
95+
/* -------------------------------------------------------------------------- */
96+
uint16_t res;
97+
__disable_irq();
98+
res = CEspCom::peekMsgSizeForStation();
99+
__enable_irq();
100+
return res;
101+
}
102+
94103
/* -------------------------------------------------------------------------- */
95104
uint8_t *CEspControl::getSoftApRx(uint8_t &if_num, uint16_t &dim) {
96105
/* -------------------------------------------------------------------------- */

Diff for: libraries/ESPhost/src/CEspControl.h

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class CEspControl {
137137
int sendBuffer(ESP_INTERFACE_TYPE type, uint8_t num, uint8_t *buf, uint16_t dim);
138138

139139
uint8_t *getStationRx(uint8_t &if_num, uint16_t &dim);
140+
uint16_t peekStationRxMsgSize();
140141
uint8_t *getSoftApRx(uint8_t &if_num, uint16_t &dim);
141142

142143

Diff for: libraries/lwIpWrapper/src/CNetIf.cpp

+12-11
Original file line numberDiff line numberDiff line change
@@ -1374,27 +1374,28 @@ void CWifiStation::task()
13741374
/* -------------------------------------------------------------------------- */
13751375
/* get messages and process it */
13761376
uint8_t if_num;
1377-
uint16_t dim;
1377+
uint16_t dim = 0;
13781378
uint8_t* buf = nullptr;
1379+
struct pbuf* p = nullptr;
1380+
13791381
/* shall we verify something about if_num??? */
13801382
do {
1381-
buf = CEspControl::getInstance().getStationRx(if_num, dim);
1382-
1383-
if (buf != nullptr) {
1384-
// Serial.println("Wifi Station - msg rx");
1385-
1386-
struct pbuf* p = pbuf_alloc(PBUF_RAW, dim, PBUF_RAM);
1387-
if (p != NULL) {
1383+
dim = CEspControl::getInstance().peekStationRxMsgSize();
1384+
if (dim > 0)
1385+
{
1386+
p = pbuf_alloc(PBUF_RAW, dim, PBUF_RAM);
1387+
if (p != nullptr)
1388+
{
1389+
buf = CEspControl::getInstance().getStationRx(if_num, dim);
13881390
/* Copy ethernet frame into pbuf */
13891391
pbuf_take((struct pbuf*)p, (uint8_t*)buf, (uint32_t)dim);
1390-
delete[] buf;
1391-
13921392
if (ni.input(p, &ni) != ERR_OK) {
13931393
pbuf_free(p);
13941394
}
1395+
delete[] buf;
13951396
}
13961397
}
1397-
} while(buf != nullptr);
1398+
} while(dim > 0 && p != nullptr);
13981399

13991400

14001401
#if LWIP_DHCP

0 commit comments

Comments
 (0)