Skip to content

Commit e1af45e

Browse files
improving task syncrhonization between timer and main context
1 parent 56c1d07 commit e1af45e

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed

libraries/lwIpWrapper/src/CNetIf.cpp

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static void _getHostByNameCBK(const char *name, const ip_addr_t *ipaddr, void *c
2525

2626
#ifdef LWIP_USE_TIMER
2727
static void timer_cb(timer_callback_args_t* arg);
28-
#endif
28+
#endif // LWIP_USE_TIMER
2929

3030
// Custom Pbuf definition used to handle RX zero copy
3131
// TODO Move this in a separate file (understand if it is required)
@@ -40,16 +40,13 @@ static void zerocopy_pbuf_mem_free(struct pbuf *p) {
4040
// SYS_ARCH_DECL_PROTECT(zerocopy_pbuf_free);
4141
zerocopy_pbuf_t* zcpbuf = (zerocopy_pbuf_t*) p;
4242

43-
// arduino::lock();
4443
// SYS_ARCH_PROTECT(zerocopy_pbuf_free);
4544

4645
// FIXME pbufs may be allocated in a different memory pool, deallocate them accordingly
4746
zcpbuf->buffer_free(zcpbuf->buffer);
4847
zcpbuf->buffer = nullptr;
4948
mem_free(zcpbuf); // TODO understand if pbuf_free deletes the pbuf
5049
// SYS_ARCH_UNPROTECT(zerocopy_pbuf_free);
51-
52-
// arduino::unlock();
5350
}
5451

5552
static inline zerocopy_pbuf_t* get_zerocopy_pbuf(uint8_t *buffer, uint32_t size, void(*buffer_free)(void*) = mem_free) {
@@ -512,7 +509,6 @@ err_t CEth::output(struct netif* ni, struct pbuf* p) {
512509

513510
void CEth::consume_callback(uint8_t* buffer, uint32_t len) {
514511
// TODO understand if this callback can be moved into the base class
515-
// arduino::lock();
516512

517513
const uint16_t trimmed_size = len;
518514

@@ -536,7 +532,6 @@ void CEth::consume_callback(uint8_t* buffer, uint32_t len) {
536532
} else {
537533
// NETIF_STATS_INCREMENT_RX_BYTES(this->stats, p->len);
538534
}
539-
// arduino::unlock();
540535
}
541536

542537
/* ########################################################################## */
@@ -556,7 +551,6 @@ int CWifiStation::begin(const IPAddress &ip, const IPAddress &nm, const IPAddres
556551
int res = 0;
557552
int time_num = 0;
558553

559-
// arduino::lock();
560554
CEspControl::getInstance().listenForStationDisconnectEvent([this] (CCtrlMsgWrapper *resp) -> int {
561555
netif_set_link_down(&this->ni);
562556
return ESP_CONTROL_OK;
@@ -578,10 +572,12 @@ int CWifiStation::begin(const IPAddress &ip, const IPAddress &nm, const IPAddres
578572
time_num++;
579573
}
580574

575+
CLwipIf::getInstance().sync_timer();
581576
res = CEspControl::getInstance().setWifiMode(WIFI_MODE_STA);
577+
CLwipIf::getInstance().enable_timer();
578+
582579
CNetIf::begin(ip, nm, gw);
583580
exit:
584-
// arduino::unlock();
585581
return res;
586582
}
587583

@@ -590,8 +586,6 @@ int CWifiStation::connectToAP(const char* ssid, const char *passphrase) {
590586
int rv = ESP_CONTROL_CTRL_ERROR; // FIXME this should be set with an error meaning AP not found
591587
bool found = false;
592588
int8_t best_index = -1; // this index is used to find the ap with the best rssi
593-
// AccessPoint_t* best_matching_ap;
594-
// arduino::lock();
595589

596590
if((rv=this->scanForAp()) != WL_SCAN_COMPLETED) {
597591
goto exit;
@@ -606,62 +600,60 @@ int CWifiStation::connectToAP(const char* ssid, const char *passphrase) {
606600
}
607601
}
608602
if(best_index != -1) {
609-
// memset(ap.ssid, 0x00, SSID_LENGTH); // I shouldn't need to zero the ssid string pointer
610603
strncpy((char*)ap.ssid, ssid, SSID_LENGTH);
611-
// memcpy(ap.ssid, access_points[best_index].ssid, SSID_LENGTH);
612604

613-
// memset(ap.pwd, 0x00, PASSWORD_LENGTH);
614605
if(passphrase != nullptr) {
615606
auto slen = strlen(passphrase)+1;
616607
strncpy((char*)ap.pwd, passphrase, (slen < PASSWORD_LENGTH) ? slen : PASSWORD_LENGTH);
617-
// memcpy(ap.pwd, passphrase, (slen < PASSWORD_LENGTH) ? slen : PASSWORD_LENGTH);
618608
} else {
619-
// memset(ap.pwd, 0x00, PASSWORD_LENGTH);
620609
ap.pwd[0] = '\0';
621610
}
622611

623612
memset(ap.bssid, 0x00, BSSID_LENGTH);
624613
memcpy(ap.bssid, access_points[best_index].bssid, BSSID_LENGTH);
625614

626-
// arduino::lock();
627-
CEspControl::getInstance().communicateWithEsp();
628-
615+
CLwipIf::getInstance().sync_timer();
629616
rv=CEspControl::getInstance().connectAccessPoint(ap);
630-
// arduino::unlock();
631617

632618
if (rv == ESP_CONTROL_OK) {
633619
CEspControl::getInstance().getAccessPointConfig(access_point_cfg);
634620

635621
netif_set_link_up(&this->ni);
636622
}
637-
// arduino::unlock();
623+
CLwipIf::getInstance().enable_timer();
638624
}
639625

640626
exit:
641-
// arduino::unlock();
642-
643627
return rv;
644628
}
645629

646630
int CWifiStation::scanForAp() {
647-
// arduino::lock();
648631
access_points.clear();
649632

633+
CLwipIf::getInstance().sync_timer();
634+
650635
int res = CEspControl::getInstance().getAccessPointScanList(access_points);
636+
CLwipIf::getInstance().enable_timer();
637+
651638
if (res == ESP_CONTROL_OK) {
652639
res = WL_SCAN_COMPLETED;
653640
} else {
654641
res = WL_NO_SSID_AVAIL;
655642
}
656643

657-
// arduino::unlock();
658644

659645
return res;
660646
}
661647

662648
// disconnect
663649
int CWifiStation::disconnectFromAp() {
664-
return CEspControl::getInstance().disconnectAccessPoint();
650+
CLwipIf::getInstance().sync_timer();
651+
652+
auto res = CEspControl::getInstance().disconnectAccessPoint();
653+
654+
CLwipIf::getInstance().enable_timer();
655+
656+
return res;
665657
}
666658

667659
err_t CWifiStation::init(struct netif* ni) {
@@ -698,7 +690,6 @@ err_t CWifiStation::output(struct netif* _ni, struct pbuf* p) {
698690
// NETIF_STATS_INCREMENT_TX_TRANSMIT_CALLS(this->stats);
699691
// NETIF_STATS_TX_TIME_START(this->stats);
700692

701-
// arduino::lock();
702693
// p may be a chain of pbufs
703694
if(p->next != nullptr) {
704695
buf = (uint8_t*) malloc(size*sizeof(uint8_t));
@@ -731,7 +722,6 @@ err_t CWifiStation::output(struct netif* _ni, struct pbuf* p) {
731722
if(p->next != nullptr && buf != nullptr) {
732723
free(buf);
733724
}
734-
// arduino::unlock();
735725
return errval;
736726
}
737727

@@ -745,7 +735,6 @@ void CWifiStation::task() {
745735
struct pbuf* p = nullptr;
746736

747737
// NETIF_STATS_RX_TIME_START(this->stats);
748-
// arduino::lock();
749738
// TODO do not perform this when not connected to an AP
750739
if(hw_init) {
751740
CEspControl::getInstance().communicateWithEsp();
@@ -779,7 +768,6 @@ void CWifiStation::task() {
779768
buffer = CEspControl::getInstance().getStationRx(if_num, dim);
780769
}
781770
// NETIF_STATS_RX_TIME_AVERAGE(this->stats);
782-
// arduino::unlock();
783771
}
784772

785773
// void CWifiStation::consume_callback(uint8_t* buffer, uint32_t len) {
@@ -877,7 +865,6 @@ int CWifiSoftAp::begin(const IPAddress &ip, const IPAddress &nm, const IPAddress
877865
int res = 0;
878866
int time_num = 0;
879867

880-
// arduino::lock();
881868
CEspControl::getInstance().listenForInitEvent([this] (CCtrlMsgWrapper *resp) -> int {
882869
// Serial.println("init");
883870
this->hw_init = true;
@@ -895,21 +882,23 @@ int CWifiSoftAp::begin(const IPAddress &ip, const IPAddress &nm, const IPAddress
895882
time_num++;
896883
}
897884

885+
CLwipIf::getInstance().sync_timer();
898886
res = CEspControl::getInstance().setWifiMode(WIFI_MODE_AP);
887+
CLwipIf::getInstance().enable_timer();
899888

900889
CNetIf::begin(
901890
default_dhcp_server_ip,
902891
default_nm,
903892
default_dhcp_server_ip
904893
);
905894
exit:
906-
// arduino::unlock();
907895
return res;
908896
}
909897

910898
// TODO scan the other access point first and then set the channel if 0
911899
// TODO there are requirements for ssid and password
912900
int CWifiSoftAp::startSoftAp(const char* ssid, const char* passphrase, uint8_t channel) {
901+
CLwipIf::getInstance().sync_timer();
913902
SoftApCfg_t cfg;
914903

915904
strncpy((char*)cfg.ssid, ssid, SSID_LENGTH);
@@ -942,7 +931,7 @@ int CWifiSoftAp::startSoftAp(const char* ssid, const char* passphrase, uint8_t c
942931
// wifi_status = WL_AP_FAILED;
943932
}
944933

945-
934+
CLwipIf::getInstance().enable_timer();
946935
return rv;
947936
}
948937

@@ -980,7 +969,6 @@ err_t CWifiSoftAp::output(struct netif* _ni, struct pbuf* p) {
980969
// NETIF_STATS_INCREMENT_TX_TRANSMIT_CALLS(this->stats);
981970
// NETIF_STATS_TX_TIME_START(this->stats);
982971

983-
// arduino::lock();
984972
// p may be a chain of pbufs
985973
if(p->next != nullptr) {
986974
buf = (uint8_t*) malloc(size*sizeof(uint8_t));
@@ -1013,7 +1001,6 @@ err_t CWifiSoftAp::output(struct netif* _ni, struct pbuf* p) {
10131001
if(p->next != nullptr && buf != nullptr) {
10141002
free(buf);
10151003
}
1016-
// arduino::unlock();
10171004
return errval;
10181005
}
10191006

@@ -1030,7 +1017,6 @@ void CWifiSoftAp::task() {
10301017
struct pbuf* p = nullptr;
10311018

10321019
// NETIF_STATS_RX_TIME_START(this->stats);
1033-
// arduino::lock();
10341020
// TODO do not perform this when not connected to an AP
10351021
if(hw_init) {
10361022
CEspControl::getInstance().communicateWithEsp();
@@ -1065,7 +1051,6 @@ void CWifiSoftAp::task() {
10651051
buffer = CEspControl::getInstance().getStationRx(if_num, dim);
10661052
}
10671053
// NETIF_STATS_RX_TIME_AVERAGE(this->stats);
1068-
// arduino::unlock();
10691054
}
10701055

10711056
const char* CWifiSoftAp::getSSID() {
@@ -1086,11 +1071,19 @@ uint8_t CWifiSoftAp::getChannel() {
10861071
}
10871072

10881073
int CWifiSoftAp::setLowPowerMode() {
1089-
return CEspControl::getInstance().setPowerSaveMode(1);
1074+
CLwipIf::getInstance().sync_timer();
1075+
auto res = CEspControl::getInstance().setPowerSaveMode(1);
1076+
CLwipIf::getInstance().enable_timer();
1077+
1078+
return res;
10901079
}
10911080

10921081
int CWifiSoftAp::resetLowPowerMode() {
1093-
return CEspControl::getInstance().setPowerSaveMode(1);
1082+
CLwipIf::getInstance().sync_timer();
1083+
auto res = CEspControl::getInstance().setPowerSaveMode(1);
1084+
CLwipIf::getInstance().enable_timer();
1085+
1086+
return res;
10941087
}
10951088

10961089
/* ##########################################################################

libraries/lwIpWrapper/src/CNetIf.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,27 @@ class CLwipIf {
382382
// lwip stores the netif in a linked list called: netif_list
383383

384384
friend class CNetIf;
385+
friend class CWifiSoftAp;
386+
friend class CWifiStation;
385387

386388
#ifdef LWIP_USE_TIMER
387389
FspTimer timer;
388-
#endif
390+
391+
inline void sync_timer() {
392+
timer.disable_overflow_irq();
393+
this->task();
394+
}
395+
396+
inline void enable_timer() {
397+
timer.enable_overflow_irq();
398+
}
399+
#else // LWIP_USE_TIMER
400+
inline void sync_timer() {
401+
this->task();
402+
}
403+
404+
inline void enable_timer() { }
405+
#endif // LWIP_USE_TIMER
389406
};
390407

391408
extern CEth Ethernet;

0 commit comments

Comments
 (0)