#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "idatachannel.hpp" namespace iflytop { #define CANUSB_TTY_BAUD_RATE_DEFAULT 2000000 typedef enum { CANUSB_SPEED_1000000 = 0x01, CANUSB_SPEED_800000 = 0x02, CANUSB_SPEED_500000 = 0x03, CANUSB_SPEED_400000 = 0x04, CANUSB_SPEED_250000 = 0x05, CANUSB_SPEED_200000 = 0x06, CANUSB_SPEED_125000 = 0x07, CANUSB_SPEED_100000 = 0x08, CANUSB_SPEED_50000 = 0x09, CANUSB_SPEED_20000 = 0x0a, CANUSB_SPEED_10000 = 0x0b, CANUSB_SPEED_5000 = 0x0c, } CANUSB_SPEED; typedef enum { CANUSB_MODE_NORMAL = 0x00, CANUSB_MODE_LOOPBACK = 0x01, CANUSB_MODE_SILENT = 0x02, CANUSB_MODE_LOOPBACK_SILENT = 0x03, } CANUSB_MODE; typedef enum { CANUSB_FRAME_STANDARD = 0x01, CANUSB_FRAME_EXTENDED = 0x02, } CANUSB_FRAME; typedef enum { CANUSB_INJECT_PAYLOAD_MODE_RANDOM = 0, CANUSB_INJECT_PAYLOAD_MODE_INCREMENTAL = 1, CANUSB_INJECT_PAYLOAD_MODE_FIXED = 2, } CANUSB_PAYLOAD_MODE; typedef struct { uint8_t packet_header0; uint8_t packet_header1; uint8_t type; uint8_t frame_type; uint8_t frame_format; uint8_t frame_id_data_1; uint8_t frame_id_data_2; uint8_t frame_id_data_3; uint8_t frame_id_data_4; uint8_t frame_data_length; uint8_t frame_data_1; uint8_t frame_data_2; uint8_t frame_data_3; uint8_t frame_data_4; uint8_t frame_data_5; uint8_t frame_data_6; uint8_t frame_data_7; uint8_t frame_data_8; uint8_t reserve; uint8_t check_code; } waveshare_can_packet_t; class WaveshareCan { public: typedef struct { uint32_t id; uint8_t dlc; uint8_t data[8]; } can_rx_frame_t; typedef function frame_callback_t; private: unique_ptr m_thread; frame_callback_t m_frame_callback; IDataChannel* m_ch = nullptr; mutex lock_; uint8_t m_rxcache[1024] = {0}; uint8_t m_rxlen = 0; public: bool init(IDataChannel* ch, frame_callback_t framecb); void setCanpPrameter(CANUSB_SPEED speed, CANUSB_MODE mode, CANUSB_FRAME frame); bool sendStdframe(uint32_t id, unsigned char data[], int data_length_code) { return sendframe(CANUSB_FRAME_STANDARD, id, data, data_length_code); } bool sendExtframe(uint32_t id, unsigned char data[], int data_length_code) { return sendframe(CANUSB_FRAME_EXTENDED, id, data, data_length_code); } bool sendframe(CANUSB_FRAME frame, uint32_t id, unsigned char data[], int data_length_code); private: int onReceivePacket(uint8_t* data, size_t len); }; } // namespace iflytop