common_dbc.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include <cstdint>
  3. #include <string>
  4. #include <unordered_map>
  5. #include <vector>
  6. struct SignalPackValue {
  7. std::string name;
  8. double value;
  9. };
  10. struct SignalValue {
  11. uint32_t address;
  12. uint64_t ts_nanos;
  13. std::string name;
  14. double value; // latest value
  15. std::vector<double> all_values; // all values from this cycle
  16. };
  17. enum SignalType {
  18. DEFAULT,
  19. COUNTER,
  20. HONDA_CHECKSUM,
  21. TOYOTA_CHECKSUM,
  22. PEDAL_CHECKSUM,
  23. VOLKSWAGEN_MQB_CHECKSUM,
  24. XOR_CHECKSUM,
  25. SUBARU_CHECKSUM,
  26. CHRYSLER_CHECKSUM,
  27. HKG_CAN_FD_CHECKSUM,
  28. };
  29. struct Signal {
  30. std::string name;
  31. int start_bit, msb, lsb, size;
  32. bool is_signed;
  33. double factor, offset;
  34. bool is_little_endian;
  35. SignalType type;
  36. unsigned int (*calc_checksum)(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
  37. };
  38. struct Msg {
  39. std::string name;
  40. uint32_t address;
  41. unsigned int size;
  42. std::vector<Signal> sigs;
  43. };
  44. struct Val {
  45. std::string name;
  46. uint32_t address;
  47. std::string def_val;
  48. std::vector<Signal> sigs;
  49. };
  50. struct DBC {
  51. std::string name;
  52. std::vector<Msg> msgs;
  53. std::vector<Val> vals;
  54. std::unordered_map<uint32_t, const Msg*> addr_to_msg;
  55. std::unordered_map<std::string, const Msg*> name_to_msg;
  56. };
  57. typedef struct ChecksumState {
  58. int checksum_size;
  59. int counter_size;
  60. int checksum_start_bit;
  61. int counter_start_bit;
  62. bool little_endian;
  63. SignalType checksum_type;
  64. unsigned int (*calc_checksum)(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
  65. } ChecksumState;
  66. DBC* dbc_parse(const std::string& dbc_path);
  67. DBC* dbc_parse_from_stream(const std::string &dbc_name, std::istream &stream, ChecksumState *checksum = nullptr, bool allow_duplicate_msg_name=false);
  68. const DBC* dbc_lookup(const std::string& dbc_name);
  69. std::vector<std::string> get_dbc_names();