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 5.7 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "pybind11/pybind11.h"
  2. #include "pybind11/stl.h"
  3. #include <sstream> /**< std::stringstream */
  4. #include <cstring> /**< memmove */
  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::Motorola)
  15. .value("Intel", ad::dbcc::ByteOrder::Intel)
  16. .export_values();
  17. enum_<ad::dbcc::Sign>(m, "Sign")
  18. .value("Unsigned", ad::dbcc::Sign::Unsigned)
  19. .value("Signed", ad::dbcc::Sign::Signed)
  20. .export_values();
  21. enum_<ad::dbcc::Multiplexor>(m, "Multiplexor")
  22. .value("None", ad::dbcc::Multiplexor::None)
  23. .value("Multiplexed", ad::dbcc::Multiplexor::Multiplexed)
  24. .value("Multiplexor", ad::dbcc::Multiplexor::Multiplexor)
  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::byteLines)
  29. .def_readonly("byte_line_range", &ad::dbcc::Signal::LayoutInfo::byteLineRange);
  30. class_<ad::dbcc::Signal>(m, "Signal")
  31. .def("__str__", [](ad::dbcc::Signal &sig) {
  32. std::stringstream ss;
  33. ss << "Signal(" << sig.name() << "[" << (sig.isFloat() ? "float" : "int") << "]: "
  34. << sig.startBit() << "," << sig.length() << " "
  35. << "(" << sig.factor() << "," << sig.offset() << ") "
  36. << "[" << sig.minimum() << "," << sig.maximum() << "]";
  37. return ss.str();
  38. })
  39. .def("__repr__", [](ad::dbcc::Signal &s) {
  40. std::stringstream ss;
  41. ss << "<dbcc.Signal name: " << s.name() << ">";
  42. return ss.str();
  43. })
  44. .def("encode", [](ad::dbcc::Signal &sig, double value, bytearray &ba) {
  45. Py_buffer buffer;
  46. if (PyObject_GetBuffer(ba.ptr(), &buffer, PyBUF_WRITABLE) < 0)
  47. {
  48. return;
  49. }
  50. sig.encode(value, reinterpret_cast<uint8_t *>(buffer.buf), buffer.len);
  51. PyBuffer_Release(&buffer);
  52. })
  53. .def("decode", [](ad::dbcc::Signal &sig, bytearray &ba) -> double {
  54. double ret = 0;
  55. Py_buffer buffer;
  56. if (PyObject_GetBuffer(ba.ptr(), &buffer, PyBUF_SIMPLE) == 0)
  57. {
  58. sig.decode(reinterpret_cast<uint8_t *>(buffer.buf), buffer.len, ret);
  59. PyBuffer_Release(&buffer);
  60. }
  61. return ret;
  62. })
  63. .def("bits_layout", [](ad::dbcc::Signal &sig) -> ad::dbcc::Signal::LayoutInfo {
  64. ad::dbcc::Signal::LayoutInfo ret;
  65. sig.bitsLayout(ret);
  66. return ret;
  67. })
  68. .def_property_readonly("name", &ad::dbcc::Signal::name)
  69. .def_property_readonly("byte_order", &ad::dbcc::Signal::byteOrder)
  70. .def_property_readonly("start_bit", &ad::dbcc::Signal::startBit)
  71. .def_property_readonly("length", &ad::dbcc::Signal::length)
  72. .def_property_readonly("sign", &ad::dbcc::Signal::sign)
  73. .def_property_readonly("min", &ad::dbcc::Signal::minimum)
  74. .def_property_readonly("max", &ad::dbcc::Signal::maximum)
  75. .def_property_readonly("factor", &ad::dbcc::Signal::factor)
  76. .def_property_readonly("offset", &ad::dbcc::Signal::offset)
  77. .def_property_readonly("unit", &ad::dbcc::Signal::unit)
  78. .def_property_readonly("is_float", &ad::dbcc::Signal::isFloat)
  79. .def_property_readonly("multiplexor", &ad::dbcc::Signal::multiplexor)
  80. .def_property_readonly("multiplexed_num", &ad::dbcc::Signal::multiplexedNumber)
  81. .def_property_readonly("to", &ad::dbcc::Signal::to);
  82. class_<ad::dbcc::Message>(m, "Message")
  83. .def("__len__", &ad::dbcc::Message::size)
  84. /* Essential: keep object alive while iterator exists */
  85. .def("__iter__", [](ad::dbcc::Message &s) {
  86. return make_iterator(s.begin(), s.end());
  87. }, keep_alive<0, 1>())
  88. .def("__getitem__", [](ad::dbcc::Message &s, size_t i) {
  89. if (i >= s.size())
  90. {
  91. throw index_error();
  92. }
  93. return s[i];
  94. })
  95. .def("__str__", [](ad::dbcc::Message &s) {
  96. std::stringstream ss;
  97. ss << "Message(" << s.name() << "[0x" << std::hex << s.id() << std::dec << ", " << s.id() << "])";
  98. return ss.str();
  99. })
  100. .def("__repr__", [](ad::dbcc::Message &s) {
  101. std::stringstream ss;
  102. ss << "<dbcc.Message name: " << s.name() << ", id: 0x" << std::hex << s.id()
  103. << ", " << std::dec << s.id() << ">";
  104. return ss.str();
  105. })
  106. .def_property_readonly("name", &ad::dbcc::Message::name)
  107. .def_property_readonly("id", &ad::dbcc::Message::id)
  108. .def_property_readonly("dlc", &ad::dbcc::Message::dlc)
  109. .def_property_readonly("from", &ad::dbcc::Message::from)
  110. .def_property_readonly("to", &ad::dbcc::Message::to);
  111. class_<ad::dbcc::DbcIterator>(m, "DbcParser")
  112. .def(init<const std::string &>())
  113. .def("__len__", &ad::dbcc::DbcIterator::size)
  114. /* Essential: keep object alive while iterator exists */
  115. .def("__iter__", [](ad::dbcc::DbcIterator &s) {
  116. return make_iterator(s.begin(), s.end());
  117. }, keep_alive<0, 1>())
  118. .def("__getitem__", [](ad::dbcc::DbcIterator &s, size_t i) {
  119. if (i >= s.size())
  120. {
  121. throw index_error();
  122. }
  123. return s[i];
  124. });
  125. }