blob: 432bac82df4c632e4db4beaf7e18b30f341db27c [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
19namespace frc971 {
20namespace can_logger {
21
22// This class listens to all the traffic on a SocketCAN interface and sends it
23// on the aos event loop so it can be logged with the aos logging
24// infrastructure.
25class CanLogger {
26 public:
27 static constexpr std::chrono::milliseconds kPollPeriod =
28 std::chrono::milliseconds(100);
29
Maxwell Henderson0604e6f2024-01-15 15:24:44 -080030 CanLogger(aos::ShmEventLoop *event_loop,
31 std::string_view channel_name = "/can",
Ravago Jones11472372023-03-04 15:57:56 -080032 std::string_view interface_name = "can0");
33
34 CanLogger(const CanLogger &) = delete;
35 CanLogger &operator=(const CanLogger &) = delete;
36
37 private:
38 void Poll();
39
40 // Read a CAN frame from the socket and send it on the event loop
41 // Returns true if successful and false if the recieve buffer is empty.
42 bool ReadFrame();
43
44 aos::ScopedFD fd_;
45 aos::Sender<CanFrame> frames_sender_;
46};
47
48} // namespace can_logger
49} // namespace frc971
50
51#endif // FRC971_CAN_LOGGER_CAN_LOGGER_H_