Skip to content

Commit 57ba80c

Browse files
authored
implement virtual methods (#2305)
1 parent 36e1609 commit 57ba80c

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

src/AudioTools/Communication/ESPNowStream.h

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class ESPNowStream : public BaseStream {
134134
bool begin() { return begin(cfg); }
135135

136136
/// Initialization of ESPNow incl WIFI
137-
bool begin(ESPNowStreamConfig cfg) {
137+
virtual bool begin(ESPNowStreamConfig cfg) {
138138
this->cfg = cfg;
139139
if (WiFi.getMode() == WIFI_MODE_NULL) {
140140
WiFi.mode(cfg.wifi_mode);
@@ -165,7 +165,7 @@ class ESPNowStream : public BaseStream {
165165
}
166166

167167
/// DeInitialization
168-
void end() {
168+
virtual void end() {
169169
if (is_init) {
170170
if (esp_now_deinit() != ESP_OK) {
171171
LOGE("esp_now_deinit");
@@ -459,7 +459,7 @@ class ESPNowStream : public BaseStream {
459459
}
460460

461461
/// Sends a single packet with retry logic
462-
bool sendPacket(const uint8_t* data, size_t len, int& retry_count,
462+
virtual bool sendPacket(const uint8_t* data, size_t len, int& retry_count,
463463
const uint8_t* destination = nullptr) {
464464
TRACED();
465465
const uint8_t* target = destination;
@@ -667,77 +667,87 @@ class ESPNowStream : public BaseStream {
667667
return ESPNowStreamSelf->buffer.availableForWrite();
668668
}
669669

670-
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
671-
static void default_recv_cb(const esp_now_recv_info* info,
672-
const uint8_t* data, int data_len)
673-
#else
674-
static void default_recv_cb(const uint8_t* mac_addr, const uint8_t* data,
675-
int data_len)
676-
#endif
677-
{
670+
virtual void handle_recv_cb(const uint8_t* mac_addr, const uint8_t* data,
671+
int data_len, bool broadcast, uint8_t rssi) {
678672
LOGD("rec_cb: %d", data_len);
679673
// make sure that the receive buffer is available - moved from begin to
680674
// make sure that it is only allocated when needed
681-
ESPNowStreamSelf->setupReceiveBuffer();
675+
setupReceiveBuffer();
682676

683677
// update last io time
684-
ESPNowStreamSelf->last_io_success_time = millis();
678+
last_io_success_time = millis();
685679

686680
// blocking write
687-
size_t result = ESPNowStreamSelf->buffer.writeArray(data, data_len);
681+
size_t result = buffer.writeArray(data, data_len);
688682
if (result != data_len) {
689683
LOGE("writeArray %d -> %d", data_len, result);
690684
}
691685
// manage ready state
692-
if (ESPNowStreamSelf->read_ready == false) {
693-
if (ESPNowStreamSelf->cfg.start_read_threshold_percent == 0) {
694-
ESPNowStreamSelf->read_ready = true;
686+
if (read_ready == false) {
687+
if (cfg.start_read_threshold_percent == 0) {
688+
read_ready = true;
695689
} else {
696-
float percent = ESPNowStreamSelf->getBufferPercent();
697-
ESPNowStreamSelf->read_ready =
698-
percent >= ESPNowStreamSelf->cfg.start_read_threshold_percent;
690+
read_ready = getBufferPercent() >= cfg.start_read_threshold_percent;
699691
}
700692
}
701693
}
702694

703-
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
704695

705-
static void default_send_cb(const wifi_tx_info_t* tx_info,
706-
esp_now_send_status_t status) {
707-
const uint8_t* mac_addr = tx_info->des_addr;
696+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
697+
static void default_recv_cb(const esp_now_recv_info* info,
698+
const uint8_t* data, int data_len) {
699+
const bool broadcast = memcmp(info->des_addr, BROADCAST_MAC, ESP_NOW_ETH_ALEN) == 0;
700+
ESPNowStreamSelf->handle_recv_cb(info->src_addr, data, data_len, broadcast, info->rx_ctrl->rssi);
701+
}
708702
#else
709-
static void default_send_cb(const uint8_t* mac_addr,
710-
esp_now_send_status_t status) {
703+
static void default_recv_cb(const uint8_t* mac_addr, const uint8_t* data,
704+
int data_len) {
705+
ESPNowStreamSelf->handle_recv_cb(mac_addr, data, data_len, false, 255);
706+
}
711707
#endif
708+
709+
virtual void handle_send_cb(const uint8_t* mac_addr,
710+
esp_now_send_status_t status) {
712711
static uint8_t first_mac[ESP_NOW_KEY_LEN] = {0};
713712
// we use the first confirming mac_addr for further confirmations and
714713
// ignore others
715714
if (first_mac[0] == 0) {
716715
strncpy((char*)first_mac, (char*)mac_addr, ESP_NOW_KEY_LEN);
717716
}
718-
LOGD("default_send_cb - %s -> %s", ESPNowStreamSelf->mac2str(mac_addr),
717+
LOGD("default_send_cb - %s -> %s", this->mac2str(mac_addr),
719718
status == ESP_NOW_SEND_SUCCESS ? "+" : "-");
720719

721720
// ignore others
722721
if (strncmp((char*)mac_addr, (char*)first_mac, ESP_NOW_KEY_LEN) == 0) {
723-
ESPNowStreamSelf->available_to_write = ESPNowStreamSelf->cfg.buffer_size;
722+
this->available_to_write = this->cfg.buffer_size;
724723

725724
// Track send success/failure
726-
ESPNowStreamSelf->last_send_success = (status == ESP_NOW_SEND_SUCCESS);
725+
this->last_send_success = (status == ESP_NOW_SEND_SUCCESS);
727726

728727
if (status == ESP_NOW_SEND_SUCCESS) {
729-
ESPNowStreamSelf->last_io_success_time = millis();
728+
last_io_success_time = millis();
730729
} else {
731730
LOGI(
732731
"Send Error to %s! Status: %d (Possible causes: out of range, "
733732
"receiver busy/offline, channel mismatch, or buffer full)",
734-
ESPNowStreamSelf->mac2str(mac_addr), status);
733+
this->mac2str(mac_addr), status);
735734
}
736735

737736
// Release semaphore to allow write to check status and retry if needed
738-
xSemaphoreGive(ESPNowStreamSelf->xSemaphore);
737+
xSemaphoreGive(this->xSemaphore);
739738
}
740739
}
740+
741+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
742+
static void default_send_cb(const wifi_tx_info_t* tx_info,
743+
esp_now_send_status_t status) {
744+
const uint8_t* mac_addr = tx_info->des_addr;
745+
#else
746+
static void default_send_cb(const uint8_t* mac_addr,
747+
esp_now_send_status_t status) {
748+
#endif
749+
ESPNowStreamSelf->handle_send_cb(mac_addr, status);
750+
}
741751
};
742752

743753
} // namespace audio_tools

0 commit comments

Comments
 (0)