blob: 6bad877f38a25ad4c1964713e4e37e4230098cae [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
Maxwell Henderson92b9d4e2024-02-13 20:21:08 -080036 ~CanLogger() { shm_event_loop_->epoll()->DeleteFd(fd_.get()); }
37
Ravago Jones11472372023-03-04 15:57:56 -080038 private:
39 void Poll();
40
41 // Read a CAN frame from the socket and send it on the event loop
42 // Returns true if successful and false if the recieve buffer is empty.
43 bool ReadFrame();
44
Maxwell Henderson92b9d4e2024-02-13 20:21:08 -080045 aos::ShmEventLoop *shm_event_loop_;
Ravago Jones11472372023-03-04 15:57:56 -080046 aos::ScopedFD fd_;
47 aos::Sender<CanFrame> frames_sender_;
48};
49
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080050} // namespace frc971::can_logger
Ravago Jones11472372023-03-04 15:57:56 -080051
52#endif // FRC971_CAN_LOGGER_CAN_LOGGER_H_