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 | |
| 14 | #include "aos/events/event_loop.h" |
| 15 | #include "aos/realtime.h" |
| 16 | #include "aos/scoped/scoped_fd.h" |
| 17 | #include "frc971/can_logger/can_logging_generated.h" |
| 18 | |
| 19 | namespace frc971 { |
| 20 | namespace 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. |
| 25 | class CanLogger { |
| 26 | public: |
| 27 | static constexpr std::chrono::milliseconds kPollPeriod = |
| 28 | std::chrono::milliseconds(100); |
| 29 | |
| 30 | CanLogger(aos::EventLoop *event_loop, |
| 31 | 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 | |
| 47 | } // namespace can_logger |
| 48 | } // namespace frc971 |
| 49 | |
| 50 | #endif // FRC971_CAN_LOGGER_CAN_LOGGER_H_ |