2020-07-31 23:14:31 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-12-16 17:11:45 -08:00
|
|
|
#include <cstdint>
|
|
|
|
|
#include <ctime>
|
2021-11-24 12:15:22 +08:00
|
|
|
#include <functional>
|
2021-12-16 17:11:45 -08:00
|
|
|
#include <list>
|
2022-11-04 16:36:27 -07:00
|
|
|
#include <memory>
|
2021-01-28 23:21:18 +08:00
|
|
|
#include <optional>
|
2021-05-09 13:15:17 +08:00
|
|
|
#include <vector>
|
2020-07-31 23:14:31 +02:00
|
|
|
|
2021-12-16 17:11:45 -08:00
|
|
|
#include "cereal/gen/cpp/car.capnp.h"
|
2020-07-31 23:14:31 +02:00
|
|
|
#include "cereal/gen/cpp/log.capnp.h"
|
2022-01-27 19:22:44 -08:00
|
|
|
#include "panda/board/health.h"
|
2022-11-29 16:05:46 -08:00
|
|
|
#include "panda/board/can_definitions.h"
|
2022-11-04 16:36:27 -07:00
|
|
|
#include "selfdrive/boardd/panda_comms.h"
|
|
|
|
|
|
2021-12-16 17:11:45 -08:00
|
|
|
#define USB_TX_SOFT_LIMIT (0x100U)
|
|
|
|
|
#define USBPACKET_MAX_SIZE (0x40)
|
2022-11-04 16:36:27 -07:00
|
|
|
|
|
|
|
|
#define RECV_SIZE (0x4000U)
|
2022-11-29 16:05:46 -08:00
|
|
|
|
|
|
|
|
#define CAN_REJECTED_BUS_OFFSET 0xC0U
|
|
|
|
|
#define CAN_RETURNED_BUS_OFFSET 0x80U
|
2020-07-31 23:14:31 +02:00
|
|
|
|
2021-11-17 16:00:18 -08:00
|
|
|
struct __attribute__((packed)) can_header {
|
|
|
|
|
uint8_t reserved : 1;
|
|
|
|
|
uint8_t bus : 3;
|
|
|
|
|
uint8_t data_len_code : 4;
|
|
|
|
|
uint8_t rejected : 1;
|
|
|
|
|
uint8_t returned : 1;
|
|
|
|
|
uint8_t extended : 1;
|
|
|
|
|
uint32_t addr : 29;
|
2023-01-13 15:01:55 -08:00
|
|
|
uint8_t checksum : 8;
|
2021-11-17 16:00:18 -08:00
|
|
|
};
|
|
|
|
|
|
2021-10-29 13:04:26 +02:00
|
|
|
struct can_frame {
|
2022-11-04 16:36:27 -07:00
|
|
|
long address;
|
|
|
|
|
std::string dat;
|
|
|
|
|
long busTime;
|
|
|
|
|
long src;
|
2021-10-29 13:04:26 +02:00
|
|
|
};
|
2020-08-13 10:10:50 +02:00
|
|
|
|
2022-11-04 16:36:27 -07:00
|
|
|
|
2021-12-16 17:11:45 -08:00
|
|
|
class Panda {
|
2022-11-04 16:36:27 -07:00
|
|
|
private:
|
|
|
|
|
std::unique_ptr<PandaCommsHandle> handle;
|
2021-12-16 17:11:45 -08:00
|
|
|
|
2022-11-04 16:36:27 -07:00
|
|
|
public:
|
2021-10-29 13:04:26 +02:00
|
|
|
Panda(std::string serial="", uint32_t bus_offset=0);
|
2020-07-31 23:14:31 +02:00
|
|
|
|
2021-02-16 21:39:32 -08:00
|
|
|
cereal::PandaState::PandaType hw_type = cereal::PandaState::PandaType::UNKNOWN;
|
2020-07-31 23:14:31 +02:00
|
|
|
bool has_rtc = false;
|
2021-10-29 13:04:26 +02:00
|
|
|
const uint32_t bus_offset;
|
2020-07-31 23:14:31 +02:00
|
|
|
|
2022-11-04 16:36:27 -07:00
|
|
|
bool connected();
|
|
|
|
|
bool comms_healthy();
|
2023-01-21 19:42:10 -08:00
|
|
|
std::string hw_serial();
|
2022-11-04 16:36:27 -07:00
|
|
|
|
2021-12-16 17:11:45 -08:00
|
|
|
// Static functions
|
2023-05-25 19:17:58 -07:00
|
|
|
static std::vector<std::string> list(bool usb_only=false);
|
2021-12-16 17:11:45 -08:00
|
|
|
|
2020-07-31 23:14:31 +02:00
|
|
|
// Panda functionality
|
2021-02-16 21:39:32 -08:00
|
|
|
cereal::PandaState::PandaType get_hw_type();
|
2022-05-02 13:34:31 -07:00
|
|
|
void set_safety_model(cereal::CarParams::SafetyModel safety_model, uint16_t safety_param=0U);
|
2022-03-24 14:59:16 -07:00
|
|
|
void set_alternative_experience(uint16_t alternative_experience);
|
2020-07-31 23:14:31 +02:00
|
|
|
void set_rtc(struct tm sys_time);
|
|
|
|
|
struct tm get_rtc();
|
|
|
|
|
void set_fan_speed(uint16_t fan_speed);
|
|
|
|
|
uint16_t get_fan_speed();
|
|
|
|
|
void set_ir_pwr(uint16_t ir_pwr);
|
2022-02-10 13:52:24 +01:00
|
|
|
std::optional<health_t> get_state();
|
2022-09-20 12:39:12 -07:00
|
|
|
std::optional<can_health_t> get_can_state(uint16_t can_number);
|
2020-07-31 23:14:31 +02:00
|
|
|
void set_loopback(bool loopback);
|
2021-01-28 23:25:05 +08:00
|
|
|
std::optional<std::vector<uint8_t>> get_firmware_version();
|
2023-07-09 11:06:10 +08:00
|
|
|
bool up_to_date();
|
2021-01-28 23:21:18 +08:00
|
|
|
std::optional<std::string> get_serial();
|
2020-07-31 23:14:31 +02:00
|
|
|
void set_power_saving(bool power_saving);
|
2022-03-07 18:55:16 -08:00
|
|
|
void enable_deepsleep();
|
2021-12-15 17:58:16 -08:00
|
|
|
void send_heartbeat(bool engaged);
|
2021-11-15 16:10:39 -08:00
|
|
|
void set_can_speed_kbps(uint16_t bus, uint16_t speed);
|
|
|
|
|
void set_data_speed_kbps(uint16_t bus, uint16_t speed);
|
2022-10-12 15:40:19 -07:00
|
|
|
void set_canfd_non_iso(uint16_t bus, bool non_iso);
|
2020-07-31 23:14:31 +02:00
|
|
|
void can_send(capnp::List<cereal::CanData>::Reader can_data_list);
|
2021-10-29 13:04:26 +02:00
|
|
|
bool can_receive(std::vector<can_frame>& out_vec);
|
2023-01-13 15:01:55 -08:00
|
|
|
void can_reset_communications();
|
2021-11-24 12:15:22 +08:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// for unit tests
|
2023-01-13 15:01:55 -08:00
|
|
|
uint8_t receive_buffer[RECV_SIZE + sizeof(can_header) + 64];
|
|
|
|
|
uint32_t receive_buffer_size = 0;
|
|
|
|
|
|
2021-11-24 12:15:22 +08:00
|
|
|
Panda(uint32_t bus_offset) : bus_offset(bus_offset) {}
|
|
|
|
|
void pack_can_buffer(const capnp::List<cereal::CanData>::Reader &can_data_list,
|
|
|
|
|
std::function<void(uint8_t *, size_t)> write_func);
|
2023-01-13 15:01:55 -08:00
|
|
|
bool unpack_can_buffer(uint8_t *data, uint32_t &size, std::vector<can_frame> &out_vec);
|
|
|
|
|
uint8_t calculate_checksum(uint8_t *data, uint32_t len);
|
2020-07-31 23:14:31 +02:00
|
|
|
};
|