You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dbcc_module_wrapper.cpp 6.1 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "pybind11/pybind11.h"
  2. #include "pybind11/stl.h"
  3. #include <cstring> /**< memmove */
  4. #include <sstream> /**< std::stringstream */
  5. #include "dbcc/dbc_iterator.h"
  6. #include "dbcc/message.h"
  7. #include "dbcc/signal.h"
  8. #include "dbcc/version.h"
  9. using namespace pybind11;
  10. PYBIND11_MODULE(_dbcc, m)
  11. {
  12. m.def("version", &ad::dbcc::version);
  13. enum_<ad::dbcc::ByteOrder>(m, "ByteOrder")
  14. .value("Motorola", ad::dbcc::ByteOrder::kMotorola)
  15. .value("Intel", ad::dbcc::ByteOrder::kIntel)
  16. .export_values();
  17. enum_<ad::dbcc::Sign>(m, "Sign")
  18. .value("Unsigned", ad::dbcc::Sign::kUnsigned)
  19. .value("Signed", ad::dbcc::Sign::kSigned)
  20. .export_values();
  21. enum_<ad::dbcc::Multiplexor>(m, "Multiplexor")
  22. .value("None", ad::dbcc::Multiplexor::kNone)
  23. .value("Multiplexed", ad::dbcc::Multiplexor::kMultiplexed)
  24. .value("Multiplexor", ad::dbcc::Multiplexor::kMultiplexor)
  25. .export_values();
  26. class_<ad::dbcc::Signal::LayoutInfo>(m, "SignalLayoutInfo")
  27. .def_readonly("bits", &ad::dbcc::Signal::LayoutInfo::bits)
  28. .def_readonly("byte_lines", &ad::dbcc::Signal::LayoutInfo::byte_lines)
  29. .def_readonly("byte_line_range", &ad::dbcc::Signal::LayoutInfo::byte_line_range);
  30. class_<ad::dbcc::Signal>(m, "Signal")
  31. .def("__str__",
  32. [](ad::dbcc::Signal& sig) {
  33. std::stringstream ss;
  34. ss << "Signal(" << sig.Name() << "[" << (sig.IsFloat() ? "float" : "int")
  35. << "]: " << sig.StartBit() << "," << sig.Length() << " "
  36. << "(" << sig.Factor() << "," << sig.Offset() << ") "
  37. << "[" << sig.Minimum() << "," << sig.Maximum() << "]";
  38. return ss.str();
  39. })
  40. .def("__repr__",
  41. [](ad::dbcc::Signal& s) {
  42. std::stringstream ss;
  43. ss << "<dbcc.Signal name: " << s.Name() << ">";
  44. return ss.str();
  45. })
  46. .def("encode",
  47. [](ad::dbcc::Signal& sig, double value, bytearray& ba) {
  48. Py_buffer buffer;
  49. if (PyObject_GetBuffer(ba.ptr(), &buffer, PyBUF_WRITABLE) < 0) {
  50. return;
  51. }
  52. sig.Encode(value, reinterpret_cast<uint8_t*>(buffer.buf), buffer.len);
  53. PyBuffer_Release(&buffer);
  54. })
  55. .def("decode",
  56. [](ad::dbcc::Signal& sig, bytearray& ba) -> double {
  57. double ret = 0;
  58. Py_buffer buffer;
  59. if (PyObject_GetBuffer(ba.ptr(), &buffer, PyBUF_SIMPLE) == 0) {
  60. sig.Decode(reinterpret_cast<uint8_t*>(buffer.buf), buffer.len, ret);
  61. PyBuffer_Release(&buffer);
  62. }
  63. return ret;
  64. })
  65. .def("bits_layout",
  66. [](ad::dbcc::Signal& sig) -> ad::dbcc::Signal::LayoutInfo {
  67. ad::dbcc::Signal::LayoutInfo ret;
  68. sig.BitsLayout(ret);
  69. return ret;
  70. })
  71. .def_property_readonly("name", &ad::dbcc::Signal::Name)
  72. .def_property_readonly("byte_order", &ad::dbcc::Signal::GetByteOrder)
  73. .def_property_readonly("start_bit", &ad::dbcc::Signal::StartBit)
  74. .def_property_readonly("length", &ad::dbcc::Signal::Length)
  75. .def_property_readonly("sign", &ad::dbcc::Signal::GetSign)
  76. .def_property_readonly("min", &ad::dbcc::Signal::Minimum)
  77. .def_property_readonly("max", &ad::dbcc::Signal::Maximum)
  78. .def_property_readonly("factor", &ad::dbcc::Signal::Factor)
  79. .def_property_readonly("offset", &ad::dbcc::Signal::Offset)
  80. .def_property_readonly("unit", &ad::dbcc::Signal::Unit)
  81. .def_property_readonly("is_float", &ad::dbcc::Signal::IsFloat)
  82. .def_property_readonly("multiplexor", &ad::dbcc::Signal::GetMultiplexor)
  83. .def_property_readonly("multiplexed_num", &ad::dbcc::Signal::MultiplexedNumber)
  84. .def_property_readonly("to", &ad::dbcc::Signal::To);
  85. class_<ad::dbcc::Message>(m, "Message")
  86. .def("__len__", &ad::dbcc::Message::SignalCount)
  87. /* Essential: keep object alive while iterator exists */
  88. .def(
  89. "__iter__",
  90. [](ad::dbcc::Message& s) {
  91. return make_iterator(s.begin(), s.end());
  92. },
  93. keep_alive<0, 1>())
  94. .def("__getitem__",
  95. [](ad::dbcc::Message& s, size_t i) {
  96. if (i >= s.SignalCount()) {
  97. throw index_error();
  98. }
  99. return s[i];
  100. })
  101. .def("__str__",
  102. [](ad::dbcc::Message& s) {
  103. std::stringstream ss;
  104. ss << "Message(" << s.Name() << "[0x" << std::hex << s.Id() << std::dec << ", "
  105. << s.Id() << "])";
  106. return ss.str();
  107. })
  108. .def("__repr__",
  109. [](ad::dbcc::Message& s) {
  110. std::stringstream ss;
  111. ss << "<dbcc.Message name: " << s.Name() << ", id: 0x" << std::hex << s.Id()
  112. << ", " << std::dec << s.Id() << ">";
  113. return ss.str();
  114. })
  115. .def_property_readonly("name", &ad::dbcc::Message::Name)
  116. .def_property_readonly("id", &ad::dbcc::Message::Id)
  117. .def_property_readonly("dlc", &ad::dbcc::Message::Dlc)
  118. .def_property_readonly("from", &ad::dbcc::Message::From)
  119. .def_property_readonly("to", &ad::dbcc::Message::To);
  120. class_<ad::dbcc::DbcIterator>(m, "DbcParser")
  121. .def(init<const std::string&>())
  122. .def("__len__", &ad::dbcc::DbcIterator::size)
  123. /* Essential: keep object alive while iterator exists */
  124. .def(
  125. "__iter__",
  126. [](ad::dbcc::DbcIterator& s) {
  127. return make_iterator(s.begin(), s.end());
  128. },
  129. keep_alive<0, 1>())
  130. .def("__getitem__", [](ad::dbcc::DbcIterator& s, size_t i) {
  131. if (i >= s.size()) {
  132. throw index_error();
  133. }
  134. return s[i];
  135. });
  136. }