blob: 6107494899becddd27a4bd772219743ea291a247 [file] [log] [blame]
Austin Schuh6b9c4152019-11-29 12:45:24 -08001#ifndef AOS_EVENTS_PING_LIB_H_
2#define AOS_EVENTS_PING_LIB_H_
3
4#include <chrono>
5
6#include "aos/events/event_loop.h"
7#include "aos/events/pong_generated.h"
8#include "aos/events/ping_generated.h"
9
10namespace aos {
11
12// Class which sends out a Ping message every X ms, and times the response.
13class Ping {
14 public:
15 Ping(EventLoop *event_loop);
16
Austin Schuh4c3b9702020-08-30 11:34:55 -070017 void set_quiet(bool quiet) { quiet_ = quiet; }
18
Austin Schuh6b9c4152019-11-29 12:45:24 -080019 private:
20 // Sends out the ping message with an incrementing count.
21 void SendPing();
22
23 // Receives the reply and measures the latency.
24 void HandlePong(const examples::Pong &pong);
25
26 aos::EventLoop *event_loop_;
27 aos::Sender<examples::Ping> sender_;
28 // Timer handle which sends the Ping message.
29 aos::TimerHandler *timer_handle_;
Austin Schuh6b9c4152019-11-29 12:45:24 -080030 // Number of pings sent.
31 int count_ = 0;
Austin Schuh5d89cf52019-12-28 16:27:42 -080032 // Last pong value received so we can detect missed pongs.
33 int last_pong_value_ = 0;
Austin Schuh4c3b9702020-08-30 11:34:55 -070034
35 bool quiet_ = false;
Austin Schuh6b9c4152019-11-29 12:45:24 -080036};
37
38} // namespace aos
39
40#endif // AOS_EVENTS_PING_LIB_H_