blob: a1442651754bcee6a56bdce95875658ac6dd7050 [file] [log] [blame]
Ravago Jones11472372023-03-04 15:57:56 -08001#ifndef FRC971_CAN_LOGGER_CAN_LOGGER_H_
2#define FRC971_CAN_LOGGER_CAN_LOGGER_H_
3
4#include <linux/can.h>
5#include <linux/can/raw.h>
6#include <linux/sockios.h>
7#include <net/if.h>
8#include <sys/ioctl.h>
9#include <sys/socket.h>
10#include <sys/time.h>
11
12#include <chrono>
13
Maxwell Henderson0604e6f2024-01-15 15:24:44 -080014#include "aos/events/shm_event_loop.h"
Ravago Jones11472372023-03-04 15:57:56 -080015#include "aos/realtime.h"
16#include "aos/scoped/scoped_fd.h"
17#include "frc971/can_logger/can_logging_generated.h"
18
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080019namespace frc971::can_logger {
Ravago Jones11472372023-03-04 15:57:56 -080020
21// This class listens to all the traffic on a SocketCAN interface and sends it
22// on the aos event loop so it can be logged with the aos logging
23// infrastructure.
24class CanLogger {
25 public:
26 static constexpr std::chrono::milliseconds kPollPeriod =
27 std::chrono::milliseconds(100);
28
Maxwell Henderson0604e6f2024-01-15 15:24:44 -080029 CanLogger(aos::ShmEventLoop *event_loop,
30 std::string_view channel_name = "/can",
Ravago Jones11472372023-03-04 15:57:56 -080031 std::string_view interface_name = "can0");
32
33 CanLogger(const CanLogger &) = delete;
34 CanLogger &operator=(const CanLogger &) = delete;
35
36 private:
37 void Poll();
38
39 // Read a CAN frame from the socket and send it on the event loop
40 // Returns true if successful and false if the recieve buffer is empty.
41 bool ReadFrame();
42
43 aos::ScopedFD fd_;
44 aos::Sender<CanFrame> frames_sender_;
45};
46
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080047} // namespace frc971::can_logger
Ravago Jones11472372023-03-04 15:57:56 -080048
49#endif // FRC971_CAN_LOGGER_CAN_LOGGER_H_