Merge pull request #12106 from barbudor/fix_ping_and_rules

Fix PING race condition breaks JSON
This commit is contained in:
Theo Arends 2021-05-15 19:33:21 +02:00 committed by GitHub
commit 8c53a2d88b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -246,14 +246,13 @@ extern "C" {
int32_t t_ping_start(const char *hostname, uint32_t count) {
IPAddress ipfull;
if (!WiFi.hostByName(hostname, ipfull)) {
return -2;
ipfull = 0xFFFFFFFF;
}
uint32_t ip = ipfull;
if (0xFFFFFFFF == ip) { return -2; } // invalid address
// check if pings are already ongoing for this IP
if (t_ping_find(ip)) {
if (0xFFFFFFFF != ip && t_ping_find(ip)) {
return -1;
}
@ -271,6 +270,12 @@ extern "C" {
ping->next = ping_head;
ping_head = ping; // insert at head
if (0xFFFFFFFF == ip) { // If invalid address, set as completed
ping->done = true;
return -2;
}
// send
t_ping_register_pcb();
t_ping_send(t_ping_pcb, ping);
@ -293,24 +298,34 @@ void PingResponsePoll(void) {
uint32_t success = ping->success_count;
uint32_t ip = ping->ip;
Response_P(PSTR("{\"" D_JSON_PING "\":{\"%s\":{"
"\"Reachable\":%s"
",\"IP\":\"%d.%d.%d.%d\""
",\"Success\":%d"
",\"Timeout\":%d"
",\"MinTime\":%d"
",\"MaxTime\":%d"
",\"AvgTime\":%d"
"}}}"),
ping->hostname.c_str(),
success ? PSTR("true") : PSTR("false"),
ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24,
success,
ping->timeout_count,
success ? ping->min_time : 0,
ping->max_time,
success ? ping->sum_time / success : 0
);
if (0xFFFFFFFF == ip) {
Response_P(PSTR("{\"" D_JSON_PING "\":{\"%s\":{"
"\"Reachable\":false"
",\"IP\":\"\""
",\"Success\":false"
"}}}"),
ping->hostname.c_str()
);
} else {
Response_P(PSTR("{\"" D_JSON_PING "\":{\"%s\":{"
"\"Reachable\":%s"
",\"IP\":\"%d.%d.%d.%d\""
",\"Success\":%d"
",\"Timeout\":%d"
",\"MinTime\":%d"
",\"MaxTime\":%d"
",\"AvgTime\":%d"
"}}}"),
ping->hostname.c_str(),
success ? PSTR("true") : PSTR("false"),
ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24,
success,
ping->timeout_count,
success ? ping->min_time : 0,
ping->max_time,
success ? ping->sum_time / success : 0
);
}
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_PING));
// remove from linked list
@ -342,14 +357,6 @@ void CmndPing(void) {
} else if (-1 == res) {
ResponseCmndChar_P(PSTR("Ping already ongoing for this IP"));
} else {
Response_P(PSTR("{\"" D_JSON_PING "\":{\"%s\":{"
"\"Reachable\":false"
",\"IP\":\"\""
",\"Success\":false"
"}}}"),
XdrvMailbox.data
);
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_PING));
ResponseCmndChar_P(PSTR("Unable to resolve IP address"));
}
}