candbc.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #include <map>
  3. #include <string>
  4. #include <utility>
  5. #include <unordered_map>
  6. #include <vector>
  7. #include "logger.h"
  8. #include "common_dbc.h"
  9. #define INFO printf
  10. #define WARN printf
  11. #define DEBUG(...)
  12. //#define DEBUG printf
  13. #define MAX_BAD_COUNTER 5
  14. #define CAN_INVALID_CNT 5
  15. // Car specific functions
  16. unsigned int honda_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
  17. unsigned int toyota_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
  18. unsigned int subaru_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
  19. unsigned int chrysler_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
  20. unsigned int volkswagen_mqb_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
  21. unsigned int xor_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
  22. unsigned int hkg_can_fd_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
  23. unsigned int pedal_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
  24. struct CanFrame {
  25. long src;
  26. uint32_t address;
  27. std::vector<uint8_t> dat;
  28. };
  29. struct CanData {
  30. uint64_t nanos;
  31. std::vector<CanFrame> frames;
  32. };
  33. class MessageState {
  34. public:
  35. std::string name;
  36. uint32_t address;
  37. unsigned int size;
  38. std::vector<Signal> parse_sigs;
  39. std::vector<double> vals;
  40. std::vector<std::vector<double>> all_vals;
  41. uint64_t last_seen_nanos;
  42. uint64_t check_threshold;
  43. uint8_t counter;
  44. uint8_t counter_fail;
  45. bool ignore_checksum = false;
  46. bool ignore_counter = false;
  47. bool parse(uint64_t nanos, const std::vector<uint8_t> &dat);
  48. bool update_counter_generic(int64_t v, int cnt_size);
  49. };
  50. class CANParser {
  51. private:
  52. const int bus;
  53. const DBC *dbc = NULL;
  54. std::unordered_map<uint32_t, MessageState> message_states;
  55. public:
  56. bool can_valid = false;
  57. bool bus_timeout = false;
  58. uint64_t first_nanos = 0;
  59. uint64_t last_nanos = 0;
  60. uint64_t last_nonempty_nanos = 0;
  61. uint64_t bus_timeout_threshold = 0;
  62. uint64_t can_invalid_cnt = CAN_INVALID_CNT;
  63. CANParser(int abus, const std::string& dbc_name,
  64. const std::vector<std::pair<uint32_t, int>> &messages);
  65. CANParser(int abus, const std::string& dbc_name,std::istringstream & stringstream,
  66. const std::vector<std::pair<uint32_t, int>> &messages);
  67. CANParser(int abus, const std::string& dbc_name, bool ignore_checksum, bool ignore_counter);
  68. CANParser(int abus, const std::string& dbc_name,std::istringstream & stringstream);
  69. void update(const std::vector<CanData> &can_data, std::vector<SignalValue> &vals);
  70. void query_latest(std::vector<SignalValue> &vals, uint64_t last_ts = 0);
  71. const DBC * GetDBC();
  72. protected:
  73. void UpdateCans(const CanData &can);
  74. void UpdateValid(uint64_t nanos);
  75. };
  76. class CANPacker {
  77. private:
  78. const DBC *dbc = NULL;
  79. std::map<std::pair<uint32_t, std::string>, Signal> signal_lookup;
  80. std::map<uint32_t, uint32_t> counters;
  81. public:
  82. CANPacker(const std::string& dbc_name);
  83. CANPacker(const std::string& dbc_name,std::istringstream & stringstream);
  84. std::vector<uint8_t> pack(uint32_t address, const std::vector<SignalPackValue> &values);
  85. const Msg* lookup_message(uint32_t address);
  86. const DBC * GetDBC();
  87. };