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.

signal.cpp 14 kB

2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year 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
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
1 year 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
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
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
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
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #include "dbcc/signal.h"
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <limits>
  5. #include <sstream>
  6. #include <vector> /**< std::vector */
  7. #include "dbcc/helper/signal_helper.h"
  8. // FIXME: We ignore the properties and comment of Signal
  9. namespace ad {
  10. namespace dbcc {
  11. static inline std::string& Trim(std::string& str, const std::string& toTrim = " ")
  12. {
  13. std::string::size_type pos = str.find_last_not_of(toTrim);
  14. if (pos != std::string::npos) {
  15. str.erase(pos + 1);
  16. str.erase(0, str.find_first_not_of(toTrim));
  17. }
  18. return str;
  19. }
  20. static inline std::vector<std::string>& Split(const std::string& s, char delim,
  21. std::vector<std::string>& elems)
  22. {
  23. std::stringstream ss(s);
  24. std::string item;
  25. while (std::getline(ss, item, delim)) {
  26. elems.push_back(Trim(item));
  27. }
  28. return elems;
  29. }
  30. static inline std::vector<std::string> Split(const std::string& s, char delim)
  31. {
  32. std::vector<std::string> elems;
  33. Split(s, delim, elems);
  34. return elems;
  35. }
  36. static inline bool ConsumeNumber(std::istream& in, double& num)
  37. {
  38. int64_t i = 0;
  39. auto pos = in.tellg();
  40. in >> i;
  41. char c = in.peek();
  42. if (c == '.') {
  43. // Revert to the previous position
  44. in.seekg(pos);
  45. // Convert as a double
  46. double d = 0;
  47. in >> d;
  48. num = d;
  49. return true;
  50. }
  51. num = i;
  52. return false;
  53. }
  54. std::istream& operator>>(std::istream& in, Signal& sig)
  55. {
  56. sig.is_float_ = false;
  57. // Parse the signal name
  58. in >> sig.name_;
  59. std::string multi;
  60. in >> multi;
  61. // This case happens if there is not Multiplexor present
  62. if (multi == ":") {
  63. sig.multiplexor_ = Multiplexor::kNone;
  64. } else {
  65. if (multi == "M") {
  66. sig.multiplexor_ = Multiplexor::kMultiplexor;
  67. } else {
  68. // The multiplexor looks like that 'm12' so we ignore the m and parse it as integer
  69. std::istringstream multistream(multi);
  70. multistream.ignore(1);
  71. uint16_t multi_num;
  72. multistream >> multi_num;
  73. sig.multiplexor_ = Multiplexor::kMultiplexor;
  74. sig.multiplexed_number_ = multi_num;
  75. }
  76. // Ignore the next character which is a ':'
  77. in >> multi;
  78. }
  79. in >> sig.start_bit_;
  80. in.ignore(1);
  81. in >> sig.length_;
  82. in.ignore(1);
  83. int order;
  84. in >> order;
  85. if (order == 0) {
  86. sig.byte_order_ = ByteOrder::kMotorola;
  87. } else {
  88. sig.byte_order_ = ByteOrder::kIntel;
  89. }
  90. char sign;
  91. in >> sign;
  92. if (sign == '+') {
  93. sig.sign_ = Sign::kUnsigned;
  94. } else {
  95. sig.sign_ = Sign::kSigned;
  96. }
  97. bool is_float = false;
  98. // (factor,offset) [max|min]
  99. in.ignore(std::numeric_limits<std::streamsize>::max(), '(');
  100. is_float = ConsumeNumber(in, sig.factor_) || is_float;
  101. in.ignore(1); // ,
  102. is_float = ConsumeNumber(in, sig.offset_) || is_float;
  103. in.ignore(1); // )
  104. in.ignore(std::numeric_limits<std::streamsize>::max(), '[');
  105. is_float = ConsumeNumber(in, sig.minimum_) || is_float;
  106. in.ignore(1); // |
  107. is_float = ConsumeNumber(in, sig.maximum_) || is_float;
  108. in.ignore(1); // ]
  109. sig.is_float_ = is_float;
  110. // Unit string
  111. std::stringstream unit;
  112. in.ignore(std::numeric_limits<std::streamsize>::max(), '\"');
  113. while (in.peek() != '\"') {
  114. unit.put(in.get());
  115. }
  116. if (in.eof() || !in) {
  117. in.setstate(std::ios_base::failbit);
  118. return in;
  119. }
  120. in.ignore(1); // the tail "
  121. sig.unit_ = unit.str();
  122. sig.unit_ = Trim(sig.unit_, "\"");
  123. sig.unit_ = Trim(sig.unit_);
  124. std::string to;
  125. getline(in, to);
  126. if (!to.empty() && *to.rbegin() == '\r') {
  127. to.erase(to.length() - 1, 1);
  128. }
  129. if (!to.empty()) {
  130. std::vector<std::string> toStrings = Split(to, ',');
  131. std::move(toStrings.begin(), toStrings.end(), std::inserter(sig.to_, sig.to_.begin()));
  132. }
  133. return in;
  134. }
  135. Signal::~Signal()
  136. {}
  137. bool Signal::Compile()
  138. {
  139. ad::dbcc::helper::SignalHelper sig_helper(*this);
  140. for (auto seg : sig_helper.Segments(true)) {
  141. CompiledSignalParameter param;
  142. if (seg.direction == ad::dbcc::helper::SignalHelper::ShiftDirection::kLeft) {
  143. param.left = 1;
  144. } else {
  145. param.left = 0;
  146. }
  147. param.index = seg.index;
  148. param.shift = seg.shift;
  149. param.mask = seg.mask;
  150. params_.emplace_back(param);
  151. }
  152. if (GetSign() == ad::dbcc::Sign::kSigned) {
  153. uint32_t mask = ((1 << (sig_helper.TypeLength() - Length())) - 1);
  154. if (mask != 0) {
  155. mask <<= Length();
  156. signed_length_mask_ = mask;
  157. }
  158. }
  159. #if defined(DEBUG) && defined(VERBOSE)
  160. std::cout << *this << std::endl;
  161. #endif /* DEBUG && VERBOSE */
  162. return true;
  163. }
  164. static uint8_t PackLeftShift(uint64_t value, uint32_t shift, uint32_t mask)
  165. {
  166. return (uint8_t)((uint8_t)(value << shift) & mask);
  167. }
  168. static uint8_t PackRightShift(uint64_t value, uint32_t shift, uint32_t mask)
  169. {
  170. return (uint8_t)((uint8_t)(value >> shift) & mask);
  171. }
  172. static int64_t UnpackLeftShift(uint8_t value, uint32_t shift, uint32_t mask)
  173. {
  174. return static_cast<int64_t>(static_cast<int64_t>(value & mask) << shift);
  175. }
  176. static int64_t UnpackRightShift(uint8_t value, uint32_t shift, uint32_t mask)
  177. {
  178. return static_cast<int64_t>(static_cast<int64_t>(value & mask) >> shift);
  179. }
  180. bool Signal::Encode(const ParsedValue& pv, uint8_t* data, size_t /* length */)
  181. {
  182. if (params_.size() == 0) {
  183. Compile();
  184. }
  185. if (pv.is_integer == IsFloat()) {
  186. return false;
  187. }
  188. int64_t temp = 0;
  189. if (pv.is_integer) {
  190. temp = static_cast<int64_t>((pv.i - Offset()) / Factor());
  191. } else {
  192. temp = static_cast<int64_t>((pv.f - Offset()) / Factor());
  193. }
  194. for (auto& p : params_) {
  195. if (p.left) {
  196. data[p.index] |= PackRightShift(temp, p.shift, p.mask);
  197. } else {
  198. data[p.index] |= PackLeftShift(temp, p.shift, p.mask);
  199. }
  200. }
  201. return true;
  202. }
  203. bool Signal::Decode(const uint8_t* data, size_t /* length */, ParsedValue& pv)
  204. {
  205. if (params_.size() == 0) {
  206. Compile();
  207. }
  208. int64_t result = 0;
  209. pv.is_integer = !IsFloat();
  210. for (auto& p : params_) {
  211. if (p.left) {
  212. result |= UnpackLeftShift(data[p.index], p.shift, p.mask);
  213. } else {
  214. result |= UnpackRightShift(data[p.index], p.shift, p.mask);
  215. }
  216. }
  217. if (signed_length_mask_ != 0) {
  218. if ((result & (static_cast<int64_t>(1) << (Length() - 1))) != 0) {
  219. result |= signed_length_mask_;
  220. }
  221. }
  222. if (pv.is_integer) {
  223. pv.i = static_cast<int32_t>(result * Factor() + Offset());
  224. } else {
  225. pv.f = static_cast<float>(result * Factor() + Offset());
  226. }
  227. return true;
  228. }
  229. bool Signal::Encode(double value, uint8_t* data, size_t /* length */)
  230. {
  231. if (params_.size() == 0) {
  232. Compile();
  233. }
  234. int64_t temp = 0;
  235. if (!IsFloat()) {
  236. int32_t i = static_cast<int32_t>(value);
  237. temp = static_cast<int64_t>((i - Offset()) / Factor());
  238. } else {
  239. temp = static_cast<int64_t>((value - Offset()) / Factor());
  240. }
  241. for (auto& p : params_) {
  242. if (p.left) {
  243. data[p.index] |= PackRightShift(temp, p.shift, p.mask);
  244. } else {
  245. data[p.index] |= PackLeftShift(temp, p.shift, p.mask);
  246. }
  247. }
  248. return true;
  249. }
  250. bool Signal::Decode(const uint8_t* data, size_t /* length */, double& value)
  251. {
  252. if (params_.size() == 0) {
  253. Compile();
  254. }
  255. int64_t result = 0;
  256. for (auto& p : params_) {
  257. if (p.left) {
  258. result |= UnpackLeftShift(data[p.index], p.shift, p.mask);
  259. } else {
  260. result |= UnpackRightShift(data[p.index], p.shift, p.mask);
  261. }
  262. }
  263. if (signed_length_mask_ != 0) {
  264. if ((result & (static_cast<int64_t>(1) << (Length() - 1))) != 0) {
  265. result |= signed_length_mask_;
  266. }
  267. }
  268. if (!IsFloat()) {
  269. value = static_cast<double>(static_cast<int32_t>(result * Factor() + Offset()));
  270. } else {
  271. value = static_cast<double>(result * Factor() + Offset());
  272. }
  273. return true;
  274. }
  275. // Here I assume the layout:
  276. //
  277. // Bit
  278. //
  279. // 7 6 5 4 3 2 1 0
  280. // +---+---+---+---+---+---+---+---+
  281. // 0 |<-x|<---------------------x|<--|
  282. // +---+---+---+---+---+---+---+---+
  283. // 1 |-------------------------------|
  284. // +---+---+---+---+---+---+---+---+
  285. // 2 |----------x| | | | | |
  286. // B +---+---+---+---+---+---+---+---+
  287. // y 3 | | | | | | | | |
  288. // t +---+---+---+---+---+---+---+---+
  289. // e 4 | | | | | | | | |
  290. // +---+---+---+---+---+---+---+---+
  291. // 5 | | | | | | | | |
  292. // +---+---+---+---+---+---+---+---+
  293. // 6 | | | | | | | | |
  294. // +---+---+---+---+---+---+---+---+
  295. // 7 | | | | | | | | |
  296. // +---+---+---+---+---+---+---+---+
  297. //
  298. // Reference: https://github.com/cantools/cantools#the-dump-subcommand
  299. bool Signal::BitsLayout(LayoutInfo& info)
  300. {
  301. info.bits.clear();
  302. info.byte_lines.clear();
  303. info.byte_line_range.clear();
  304. if (byte_order_ == ByteOrder::kIntel) //< little-endian
  305. {
  306. int line_start = start_bit_ / 8;
  307. int remainder = 8 - start_bit_ % 8;
  308. int line_num = ((length_ - remainder + 7) / 8) + 1;
  309. int start_bit = start_bit_;
  310. int end_bit = start_bit;
  311. if (remainder > length_) {
  312. end_bit += length_;
  313. } else {
  314. end_bit += remainder;
  315. }
  316. remainder = length_;
  317. for (int line = line_start; line < line_start + line_num; ++line) {
  318. for (int bit_idx = start_bit; bit_idx < end_bit; bit_idx++) {
  319. info.bits.push_back(bit_idx);
  320. }
  321. info.byte_lines.push_back(line);
  322. info.byte_line_range.emplace_back(
  323. std::pair<int, int>{(9 - start_bit % 8) - 1, (8 - (end_bit - 1) % 8) - 1});
  324. remainder = remainder - (start_bit - end_bit);
  325. start_bit = 8 * (line + 1);
  326. if (remainder >= 8) {
  327. end_bit = start_bit + 8;
  328. } else {
  329. end_bit = start_bit + remainder;
  330. }
  331. }
  332. } else { //< big-endian
  333. int line_start = start_bit_ / 8;
  334. int remainder = (8 - (8 - (start_bit_ + 1) % 8) % 8);
  335. int line_num = ((length_ - remainder + 7) / 8) + 1;
  336. int start_bit = start_bit_;
  337. int line_remainder = (start_bit % 8) + 1;
  338. int end_bit = start_bit;
  339. if (line_remainder > length_) {
  340. end_bit -= length_;
  341. } else {
  342. end_bit -= line_remainder;
  343. }
  344. remainder = length_;
  345. for (int line = line_start; line < line_start + line_num; ++line) {
  346. for (int bit_idx = start_bit; bit_idx > end_bit; bit_idx--) {
  347. info.bits.push_back(bit_idx);
  348. }
  349. info.byte_lines.push_back(line);
  350. info.byte_line_range.emplace_back(
  351. std::pair<int, int>{(9 - (end_bit + 1) % 8) - 1, (8 - start_bit % 8) - 1});
  352. remainder = remainder - (start_bit - end_bit);
  353. start_bit = 8 * (line + 2) - 1;
  354. if (remainder >= 8) {
  355. end_bit = start_bit - 8;
  356. } else {
  357. end_bit = start_bit - remainder;
  358. }
  359. }
  360. }
  361. return true;
  362. }
  363. std::ostream& operator<<(std::ostream& out, const Signal::CompiledSignalParameter& p)
  364. {
  365. out << "CompiledSignalParameter: { index: " << p.index << ", "
  366. << "shift: " << p.shift << ", "
  367. << "mask: 0x" << std::hex << p.mask << std::dec << ", "
  368. << "dir: " << (p.left == 1 ? "Left" : "Right") << " }";
  369. return out;
  370. }
  371. std::ostream& operator<<(std::ostream& out, const Signal::LayoutInfo& p)
  372. {
  373. out << "Bits: { ";
  374. for (auto bit : p.bits) {
  375. out << bit << ", ";
  376. }
  377. out << " }\n";
  378. for (size_t idx = 0; idx < p.byte_lines.size(); idx++) {
  379. out << "Line(" << p.byte_lines[idx] << "): [" << p.byte_line_range[idx].first << ", "
  380. << p.byte_line_range[idx].second << "]\n";
  381. }
  382. return out;
  383. }
  384. std::ostream& operator<<(std::ostream& out, const Signal& cs)
  385. {
  386. int i = 0;
  387. out << "Message: " << cs.GetMessageId() << ", Signal: " << cs.Name()
  388. << (cs.IsFloat() ? ", float" : "") << std::endl;
  389. for (auto& p : cs.params_) {
  390. out << " param" << i << ": " << p << std::endl;
  391. i++;
  392. }
  393. if (cs.signed_length_mask_ != 0) {
  394. out << " mask: 0x" << std::hex << cs.signed_length_mask_ << std::dec << std::endl;
  395. }
  396. out << " factor: " << cs.Factor() << ", offset: " << cs.Offset();
  397. return out;
  398. }
  399. std::string Signal::ToDbcString() const
  400. {
  401. std::ostringstream oss;
  402. oss << "SG_ " << Name();
  403. if (GetMultiplexor() == Multiplexor::kMultiplexor) {
  404. oss << " M" << MultiplexedNumber() << ": ";
  405. } else if (GetMultiplexor() == Multiplexor::kNone) {
  406. oss << " : ";
  407. } else {
  408. oss << " M: ";
  409. }
  410. oss << StartBit() << "|" << Length() << "@"
  411. << (GetByteOrder() == ByteOrder::kMotorola ? 0 : 1)
  412. << (GetSign() == Sign::kUnsigned ? "+" : "-") << " ("
  413. << Factor() << "," << Offset() << ") ["
  414. << Minimum() << "|" << Maximum() << "] \""
  415. << Unit() << "\"";
  416. if (!to_.empty()) {
  417. char delimiter = ' ';
  418. for (auto t : to_) {
  419. oss << delimiter << t;
  420. delimiter = ',';
  421. }
  422. }
  423. return oss.str();
  424. }
  425. } // namespace dbcc
  426. } // namespace ad