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 #ifndef UDP_BUFFER_SIZE
#define UDP_BUFFER_SIZE 120 // Max UDP buffer size needed for M-SEARCH message #define UDP_BUFFER_SIZE 120 // Max UDP buffer size needed for M-SEARCH message
#endif #endif
#define UDP_MSEARCH_SEND_DELAY 1500 // Delay in ms before M-Search response is send #define UDP_MSEARCH_DEBOUNCE 300 // Don't send new response if same request within 300 ms
#include <Ticker.h>
Ticker TickerMSearch;
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 IPAddress udp_remote_ip; // M-Search remote IP address
uint16_t udp_remote_port; // M-Search remote port uint16_t udp_remote_port; // M-Search remote port
@ -109,6 +110,9 @@ bool UdpConnect(void)
void PollUdp(void) void PollUdp(void)
{ {
if (udp_connected) { if (udp_connected) {
if (TimeReached(udp_last_received + UDP_MSEARCH_DEBOUNCE)) {
udp_last_received = 0; // re-init timer
}
#ifdef ESP8266 #ifdef ESP8266
while (UdpCtx.next()) { while (UdpCtx.next()) {
UdpPacket<UDP_BUFFER_SIZE> *packet; UdpPacket<UDP_BUFFER_SIZE> *packet;
@ -116,7 +120,7 @@ void PollUdp(void)
if (packet->len >= UDP_BUFFER_SIZE) { if (packet->len >= UDP_BUFFER_SIZE) {
packet->len--; // leave space for NULL terminator 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; char * packet_buffer = (char*) &packet->buf;
int32_t len = packet->len; int32_t len = packet->len;
#endif // ESP8266 #endif // ESP8266
@ -137,53 +141,54 @@ void PollUdp(void)
#else #else
if (TasmotaGlobal.devices_present && !udp_response_mutex && (strstr_P(packet_buffer, PSTR("M-SEARCH")) != nullptr)) { if (TasmotaGlobal.devices_present && !udp_response_mutex && (strstr_P(packet_buffer, PSTR("M-SEARCH")) != nullptr)) {
#endif #endif
udp_response_mutex = true; if (0 == udp_last_received) {
udp_last_received = millis();
udp_response_mutex = true;
#ifdef ESP8266 #ifdef ESP8266
udp_remote_ip = packet->srcaddr; udp_remote_ip = packet->srcaddr;
udp_remote_port = packet->srcport; udp_remote_port = packet->srcport;
#else #else
udp_remote_ip = PortUdp.remoteIP(); udp_remote_ip = PortUdp.remoteIP();
udp_remote_port = PortUdp.remotePort(); udp_remote_port = PortUdp.remotePort();
#endif #endif
// AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR("UDP: M-SEARCH Packet from %s:%d\n%s"), // 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); // 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);
LowerCase(packet_buffer, packet_buffer);
RemoveSpace(packet_buffer);
#ifdef USE_EMULATION_WEMO #ifdef USE_EMULATION_WEMO
if (EMUL_WEMO == Settings.flag2.emulation) { if (EMUL_WEMO == Settings.flag2.emulation) {
if (strstr_P(packet_buffer, URN_BELKIN_DEVICE) != nullptr) { // type1 echo dot 2g, echo 1g's 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; 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)) {
WemoRespondToMSearch(2);
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);
return;
}
}
#endif // USE_EMULATION_WEMO #endif // USE_EMULATION_WEMO
#ifdef USE_EMULATION_HUE #ifdef USE_EMULATION_HUE
if (EMUL_HUE == Settings.flag2.emulation) { if (EMUL_HUE == Settings.flag2.emulation) {
if ((strstr_P(packet_buffer, PSTR(":device:basic:1")) != nullptr) || if ((strstr_P(packet_buffer, PSTR(":device:basic:1")) != nullptr) ||
(strstr_P(packet_buffer, UPNP_ROOTDEVICE) != nullptr) || (strstr_P(packet_buffer, UPNP_ROOTDEVICE) != nullptr) ||
(strstr_P(packet_buffer, SSDPSEARCH_ALL) != nullptr) || (strstr_P(packet_buffer, SSDPSEARCH_ALL) != nullptr) ||
(strstr_P(packet_buffer, SSDP_ALL) != nullptr)) { (strstr_P(packet_buffer, SSDP_ALL) != nullptr)) {
TickerMSearch.once_ms(response_delay, HueRespondToMSearch); HueRespondToMSearch();
return; return;
}
} }
}
#endif // USE_EMULATION_HUE #endif // USE_EMULATION_HUE
udp_response_mutex = false; udp_response_mutex = false;
continue; continue;
}
} }
} }
} }