Ravago Jones | 1147237 | 2023-03-04 15:57:56 -0800 | [diff] [blame] | 1 | #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 Henderson | 0604e6f | 2024-01-15 15:24:44 -0800 | [diff] [blame] | 14 | #include "aos/events/shm_event_loop.h" |
Ravago Jones | 1147237 | 2023-03-04 15:57:56 -0800 | [diff] [blame] | 15 | #include "aos/realtime.h" |
| 16 | #include "aos/scoped/scoped_fd.h" |
| 17 | #include "frc971/can_logger/can_logging_generated.h" |
| 18 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 19 | namespace frc971::can_logger { |
Ravago Jones | 1147237 | 2023-03-04 15:57:56 -0800 | [diff] [blame] | 20 | |
| 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. |
| 24 | class CanLogger { |
| 25 | public: |
| 26 | static constexpr std::chrono::milliseconds kPollPeriod = |
| 27 | std::chrono::milliseconds(100); |
| 28 | |
Maxwell Henderson | 0604e6f | 2024-01-15 15:24:44 -0800 | [diff] [blame] | 29 | CanLogger(aos::ShmEventLoop *event_loop, |
| 30 | std::string_view channel_name = "/can", |
Ravago Jones | 1147237 | 2023-03-04 15:57:56 -0800 | [diff] [blame] | 31 | std::string_view interface_name = "can0"); |
| 32 | |
| 33 | CanLogger(const CanLogger &) = delete; |
| 34 | CanLogger &operator=(const CanLogger &) = delete; |
| 35 | |
Maxwell Henderson | 92b9d4e | 2024-02-13 20:21:08 -0800 | [diff] [blame] | 36 | ~CanLogger() { shm_event_loop_->epoll()->DeleteFd(fd_.get()); } |
| 37 | |
Ravago Jones | 1147237 | 2023-03-04 15:57:56 -0800 | [diff] [blame] | 38 | 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 Henderson | 92b9d4e | 2024-02-13 20:21:08 -0800 | [diff] [blame] | 45 | aos::ShmEventLoop *shm_event_loop_; |
Ravago Jones | 1147237 | 2023-03-04 15:57:56 -0800 | [diff] [blame] | 46 | aos::ScopedFD fd_; |
| 47 | aos::Sender<CanFrame> frames_sender_; |
| 48 | }; |
| 49 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 50 | } // namespace frc971::can_logger |
Ravago Jones | 1147237 | 2023-03-04 15:57:56 -0800 | [diff] [blame] | 51 | |
| 52 | #endif // FRC971_CAN_LOGGER_CAN_LOGGER_H_ |