From 6e6c26780e987ceace5797a8807d8e3ef933d2d1 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Wed, 7 Jul 2021 07:13:52 -0700 Subject: [PATCH] Make chip-echo-requester run on a single event loop (#8100) --- src/messaging/tests/echo/echo_requester.cpp | 93 +++++++++++---------- 1 file changed, 48 insertions(+), 45 deletions(-) diff --git a/src/messaging/tests/echo/echo_requester.cpp b/src/messaging/tests/echo/echo_requester.cpp index 0efcc71a1e43de..08b3a27e09e527 100644 --- a/src/messaging/tests/echo/echo_requester.cpp +++ b/src/messaging/tests/echo/echo_requester.cpp @@ -64,10 +64,6 @@ chip::Inet::IPAddress gDestAddr; // The last time a CHIP Echo was attempted to be sent. uint64_t gLastEchoTime = 0; -// True, if the EchoClient is waiting for an EchoResponse -// after sending an EchoRequest, false otherwise. -bool gWaitingForEchoResp = false; - // Count of the number of EchoRequests sent. uint64_t gEchoCount = 0; @@ -76,14 +72,42 @@ uint64_t gEchoRespCount = 0; bool gUseTCP = false; -bool EchoIntervalExpired(void) +CHIP_ERROR SendEchoRequest(); +void EchoTimerHandler(chip::System::Layer * systemLayer, void * appState, CHIP_ERROR error); + +void Shutdown() { - uint64_t now = chip::System::Timer::GetCurrentEpoch(); + chip::DeviceLayer::SystemLayer.CancelTimer(EchoTimerHandler, NULL); + gEchoClient.Shutdown(); + ShutdownChip(); +} - return (now >= gLastEchoTime + gEchoInterval); +void EchoTimerHandler(chip::System::Layer * systemLayer, void * appState, CHIP_ERROR error) +{ + if (gEchoRespCount != gEchoCount) + { + printf("No response received\n"); + + // Set gEchoRespCount to gEchoCount to start next ping if there is any. + gEchoRespCount = gEchoCount; + } + + if (gEchoCount < kMaxEchoCount) + { + CHIP_ERROR err = SendEchoRequest(); + if (err != CHIP_NO_ERROR) + { + printf("Send request failed: %s\n", chip::ErrorStr(err)); + Shutdown(); + } + } + else + { + Shutdown(); + } } -CHIP_ERROR SendEchoRequest(void) +CHIP_ERROR SendEchoRequest() { CHIP_ERROR err = CHIP_NO_ERROR; const char kRequestFormat[] = "Echo Message %" PRIu64 "\n"; @@ -99,18 +123,25 @@ CHIP_ERROR SendEchoRequest(void) gLastEchoTime = chip::System::Timer::GetCurrentEpoch(); + err = chip::DeviceLayer::SystemLayer.StartTimer(gEchoInterval, EchoTimerHandler, NULL); + if (err != CHIP_NO_ERROR) + { + printf("Unable to schedule timer\n"); + return CHIP_ERROR_INTERNAL; + } + printf("\nSend echo request message to Node: %" PRIu64 "\n", chip::kTestDeviceNodeId); err = gEchoClient.SendEchoRequest(std::move(payloadBuf)); if (err == CHIP_NO_ERROR) { - gWaitingForEchoResp = true; gEchoCount++; } else { printf("Send echo request failed, err: %s\n", chip::ErrorStr(err)); + chip::DeviceLayer::SystemLayer.CancelTimer(EchoTimerHandler, NULL); } return err; @@ -157,42 +188,12 @@ void HandleEchoResponseReceived(chip::Messaging::ExchangeContext * ec, chip::Sys uint32_t respTime = chip::System::Timer::GetCurrentEpoch(); uint32_t transitTime = respTime - gLastEchoTime; - gWaitingForEchoResp = false; gEchoRespCount++; printf("Echo Response: %" PRIu64 "/%" PRIu64 "(%.2f%%) len=%u time=%.3fms\n", gEchoRespCount, gEchoCount, static_cast(gEchoRespCount) * 100 / gEchoCount, payload->DataLength(), static_cast(transitTime) / 1000); } -void RunPinging() -{ - CHIP_ERROR err = CHIP_NO_ERROR; - - // Connection has been established. Now send the EchoRequests. - for (unsigned int i = 0; i < kMaxEchoCount; i++) - { - err = SendEchoRequest(); - if (err != CHIP_NO_ERROR) - { - printf("Send request failed: %s\n", chip::ErrorStr(err)); - break; - } - - // Wait for response until the Echo interval. - while (!EchoIntervalExpired()) - { - sleep(1); - } - - // Check if expected response was received. - if (gWaitingForEchoResp) - { - printf("No response received\n"); - gWaitingForEchoResp = false; - } - } -} - } // namespace int main(int argc, char * argv[]) @@ -227,8 +228,6 @@ int main(int argc, char * argv[]) InitializeChip(); - chip::DeviceLayer::PlatformMgr().StartEventLoopTask(); - adminInfo = admins.AssignAdminId(gAdminId, chip::kTestControllerNodeId); VerifyOrExit(adminInfo != nullptr, err = CHIP_ERROR_NO_MEMORY); @@ -272,13 +271,17 @@ int main(int argc, char * argv[]) // Arrange to get a callback whenever an Echo Response is received. gEchoClient.SetEchoResponseReceived(HandleEchoResponseReceived); - RunPinging(); - - gEchoClient.Shutdown(); + err = chip::DeviceLayer::SystemLayer.StartTimer(0, EchoTimerHandler, NULL); + SuccessOrExit(err); - ShutdownChip(); + chip::DeviceLayer::PlatformMgr().RunEventLoop(); exit: + if (err != CHIP_NO_ERROR) + { + Shutdown(); + } + if ((err != CHIP_NO_ERROR) || (gEchoRespCount != kMaxEchoCount)) { printf("ChipEchoClient failed: %s\n", chip::ErrorStr(err));