common_dbc.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_res(const std::string& dbcname, std::istream &istringstream);
  68. DBC* dbc_parse_from_stream(const std::string &dbc_name, std::istream &stream, ChecksumState *checksum = nullptr, bool allow_duplicate_msg_name=false);
  69. const DBC* dbc_lookup(const std::string& dbc_name);
  70. const DBC* dbc_lookup_res(const std::string& dbc_name, std::istream &istringstream);
  71. std::vector<std::string> get_dbc_names();