Skip to content

Commit

Permalink
Merge pull request #134 from pennam/lwip-wifi-fix
Browse files Browse the repository at this point in the history
Fix WiFiStation buffer management
  • Loading branch information
facchinm authored Oct 2, 2023
2 parents 00f90bd + 33b975a commit 9c748ff
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
9 changes: 9 additions & 0 deletions libraries/ESPhost/src/CEspCommunication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ bool CEspCom::getMsgForStation(CMsg &msg) {
return false;
}

/* -------------------------------------------------------------------------- */
int CEspCom::peekMsgSizeForStation() {
/* -------------------------------------------------------------------------- */
if(CEspCom::rxStationQueue.size() > 0) {
return CEspCom::rxStationQueue.front().get_size();
}
return 0;
}

/* -------------------------------------------------------------------------- */
bool CEspCom::getMsgForSoftAp(CMsg &msg) {
/* -------------------------------------------------------------------------- */
Expand Down
1 change: 1 addition & 0 deletions libraries/ESPhost/src/CEspCommunication.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class CEspCom {
static bool storeStationMsg(CMsg &msg);
static bool storeSoftApMsg(CMsg &msg);
static bool getMsgForStation(CMsg &msg);
static int peekMsgSizeForStation();
static bool getMsgForSoftAp(CMsg &msg);

static void clearStationRx();
Expand Down
13 changes: 11 additions & 2 deletions libraries/ESPhost/src/CEspControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ int CEspControl::process_test_messages(CCtrlMsgWrapper* response) {
return 0;
}


/* -------------------------------------------------------------------------- */
uint8_t *CEspControl::getStationRx(uint8_t &if_num, uint16_t &dim) {
/* -------------------------------------------------------------------------- */
uint8_t *rv = nullptr;
CMsg msg;
CMsg msg;
__disable_irq();
bool res = CEspCom::getMsgForStation(msg);
if(!res) {
Expand All @@ -91,6 +90,16 @@ uint8_t *CEspControl::getStationRx(uint8_t &if_num, uint16_t &dim) {
return rv;
}

/* -------------------------------------------------------------------------- */
uint16_t CEspControl::peekStationRxMsgSize() {
/* -------------------------------------------------------------------------- */
uint16_t res;
__disable_irq();
res = CEspCom::peekMsgSizeForStation();
__enable_irq();
return res;
}

/* -------------------------------------------------------------------------- */
uint8_t *CEspControl::getSoftApRx(uint8_t &if_num, uint16_t &dim) {
/* -------------------------------------------------------------------------- */
Expand Down
1 change: 1 addition & 0 deletions libraries/ESPhost/src/CEspControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class CEspControl {
int sendBuffer(ESP_INTERFACE_TYPE type, uint8_t num, uint8_t *buf, uint16_t dim);

uint8_t *getStationRx(uint8_t &if_num, uint16_t &dim);
uint16_t peekStationRxMsgSize();
uint8_t *getSoftApRx(uint8_t &if_num, uint16_t &dim);


Expand Down
23 changes: 12 additions & 11 deletions libraries/lwIpWrapper/src/CNetIf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1374,27 +1374,28 @@ void CWifiStation::task()
/* -------------------------------------------------------------------------- */
/* get messages and process it */
uint8_t if_num;
uint16_t dim;
uint16_t dim = 0;
uint8_t* buf = nullptr;
struct pbuf* p = nullptr;

/* shall we verify something about if_num??? */
do {
buf = CEspControl::getInstance().getStationRx(if_num, dim);

if (buf != nullptr) {
// Serial.println("Wifi Station - msg rx");

struct pbuf* p = pbuf_alloc(PBUF_RAW, dim, PBUF_RAM);
if (p != NULL) {
dim = CEspControl::getInstance().peekStationRxMsgSize();
if (dim > 0)
{
p = pbuf_alloc(PBUF_RAW, dim, PBUF_RAM);
if (p != nullptr)
{
buf = CEspControl::getInstance().getStationRx(if_num, dim);
/* Copy ethernet frame into pbuf */
pbuf_take((struct pbuf*)p, (uint8_t*)buf, (uint32_t)dim);
delete[] buf;

if (ni.input(p, &ni) != ERR_OK) {
pbuf_free(p);
}
delete[] buf;
}
}
} while(buf != nullptr);
} while(dim > 0 && p != nullptr);


#if LWIP_DHCP
Expand Down

0 comments on commit 9c748ff

Please sign in to comment.