Hue/Wemo remove delay before sending response

This commit is contained in:
Stephan Hadinger 2021-01-11 08:50:23 +01:00
parent 86ee7a411c
commit d93ed5f055
1 changed files with 42 additions and 37 deletions

View File

@ -22,11 +22,12 @@
#ifndef UDP_BUFFER_SIZE
#define UDP_BUFFER_SIZE 120 // Max UDP buffer size needed for M-SEARCH message
#endif
#define UDP_MSEARCH_SEND_DELAY 1500 // Delay in ms before M-Search response is send
#include <Ticker.h>
Ticker TickerMSearch;
#define UDP_MSEARCH_DEBOUNCE 300 // Don't send new response if same request within 300 ms
uint32_t udp_last_received = 0; // timestamp of last udp received packet
// if non-zero we keep silend and don't send response
// there is a very low probability that after 53 days the timestamp is
// genuingly zero. This is not an issue and would result in an extra response sent.
IPAddress udp_remote_ip; // M-Search remote IP address
uint16_t udp_remote_port; // M-Search remote port
@ -109,6 +110,9 @@ bool UdpConnect(void)
void PollUdp(void)
{
if (udp_connected) {
if (TimeReached(udp_last_received + UDP_MSEARCH_DEBOUNCE)) {
udp_last_received = 0; // re-init timer
}
#ifdef ESP8266
while (UdpCtx.next()) {
UdpPacket<UDP_BUFFER_SIZE> *packet;
@ -116,7 +120,7 @@ void PollUdp(void)
if (packet->len >= UDP_BUFFER_SIZE) {
packet->len--; // leave space for NULL terminator
}
packet->buf[packet->len] = 0; // add NULL at the end of the packer
packet->buf[packet->len] = 0; // add NULL at the end of the packet
char * packet_buffer = (char*) &packet->buf;
int32_t len = packet->len;
#endif // ESP8266
@ -137,6 +141,8 @@ void PollUdp(void)
#else
if (TasmotaGlobal.devices_present && !udp_response_mutex && (strstr_P(packet_buffer, PSTR("M-SEARCH")) != nullptr)) {
#endif
if (0 == udp_last_received) {
udp_last_received = millis();
udp_response_mutex = true;
#ifdef ESP8266
@ -150,21 +156,19 @@ void PollUdp(void)
// AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR("UDP: M-SEARCH Packet from %s:%d\n%s"),
// udp_remote_ip.toString().c_str(), udp_remote_port, packet_buffer);
uint32_t response_delay = UDP_MSEARCH_SEND_DELAY + ((millis() &0x7) * 100); // 1500 - 2200 msec
LowerCase(packet_buffer, packet_buffer);
RemoveSpace(packet_buffer);
#ifdef USE_EMULATION_WEMO
if (EMUL_WEMO == Settings.flag2.emulation) {
if (strstr_P(packet_buffer, URN_BELKIN_DEVICE) != nullptr) { // type1 echo dot 2g, echo 1g's
TickerMSearch.once_ms(response_delay, WemoRespondToMSearch, 1);
WemoRespondToMSearch(1);
return;
}
else if ((strstr_P(packet_buffer, UPNP_ROOTDEVICE) != nullptr) || // type2 Echo 2g (echo & echo plus)
(strstr_P(packet_buffer, SSDPSEARCH_ALL) != nullptr) ||
(strstr_P(packet_buffer, SSDP_ALL) != nullptr)) {
TickerMSearch.once_ms(response_delay, WemoRespondToMSearch, 2);
WemoRespondToMSearch(2);
return;
}
}
@ -176,7 +180,7 @@ void PollUdp(void)
(strstr_P(packet_buffer, UPNP_ROOTDEVICE) != nullptr) ||
(strstr_P(packet_buffer, SSDPSEARCH_ALL) != nullptr) ||
(strstr_P(packet_buffer, SSDP_ALL) != nullptr)) {
TickerMSearch.once_ms(response_delay, HueRespondToMSearch);
HueRespondToMSearch();
return;
}
}
@ -187,6 +191,7 @@ void PollUdp(void)
}
}
}
}
optimistic_yield(100);
}
}