CanMessage.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
  3. This file is part of cangaroo.
  4. cangaroo is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. cangaroo is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "CanMessage.h"
  16. #include <core/portable_endian.h>
  17. enum {
  18. id_flag_extended = 0x80000000,
  19. id_flag_rtr = 0x40000000,
  20. id_flag_error = 0x20000000,
  21. id_mask_extended = 0x1FFFFFFF,
  22. id_mask_standard = 0x7FF
  23. };
  24. CanMessage::CanMessage()
  25. : _raw_id(0), _dlc(0), _isFD(false), _interface(0), _u8()
  26. {
  27. _timestamp.tv_sec = 0;
  28. _timestamp.tv_usec = 0;
  29. }
  30. CanMessage::CanMessage(uint32_t can_id)
  31. : _dlc(0), _interface(0), _u8()
  32. {
  33. _timestamp.tv_sec = 0;
  34. _timestamp.tv_usec = 0;
  35. setId(can_id);
  36. }
  37. CanMessage::CanMessage(const CanMessage &msg)
  38. {
  39. cloneFrom(msg);
  40. }
  41. void CanMessage::cloneFrom(const CanMessage &msg)
  42. {
  43. _raw_id = msg._raw_id;
  44. _dlc = msg._dlc;
  45. // Copy data
  46. for(int i=0; i<64; i++)
  47. {
  48. _u8[i] = msg._u8[i];
  49. }
  50. _interface = msg._interface;
  51. _timestamp = msg._timestamp;
  52. //从文件发送数据pushback 某些变量值不对的问题
  53. _isFD=msg._isFD;
  54. _isBRS=msg._isBRS;
  55. }
  56. uint32_t CanMessage::getRawId() const {
  57. return _raw_id;
  58. }
  59. void CanMessage::setRawId(const uint32_t raw_id) {
  60. _raw_id = raw_id;
  61. }
  62. uint32_t CanMessage::getId() const {
  63. if (isExtended()) {
  64. return _raw_id & id_mask_extended;
  65. } else {
  66. return _raw_id & id_mask_standard;
  67. }
  68. }
  69. void CanMessage::setId(const uint32_t id) {
  70. _raw_id &= ~ id_mask_extended;
  71. _raw_id = id;
  72. if (id>0x7FF) {
  73. setExtended(true);
  74. }
  75. }
  76. bool CanMessage::isExtended() const {
  77. return (_raw_id & id_flag_extended) != 0;
  78. }
  79. void CanMessage::setExtended(const bool isExtended) {
  80. if (isExtended) {
  81. _raw_id |= id_flag_extended;
  82. } else {
  83. _raw_id &= ~id_flag_extended;
  84. }
  85. }
  86. bool CanMessage::isRTR() const {
  87. return (_raw_id & id_flag_rtr) != 0;
  88. }
  89. void CanMessage::setRTR(const bool isRTR) {
  90. if (isRTR) {
  91. _raw_id |= id_flag_rtr;
  92. } else {
  93. _raw_id &= ~id_flag_rtr;
  94. }
  95. }
  96. bool CanMessage::isFD() const {
  97. return _isFD;
  98. }
  99. void CanMessage::setFD(const bool isFD) {
  100. _isFD = isFD;
  101. }
  102. bool CanMessage::isBRS() const {
  103. return _isBRS;
  104. }
  105. void CanMessage::setBRS(const bool isBRS) {
  106. _isBRS = isBRS;
  107. }
  108. bool CanMessage::isErrorFrame() const {
  109. return (_raw_id & id_flag_error) != 0;
  110. }
  111. void CanMessage::setErrorFrame(const bool isErrorFrame) {
  112. if (isErrorFrame) {
  113. _raw_id |= id_flag_error;
  114. } else {
  115. _raw_id &= ~id_flag_error;
  116. }
  117. }
  118. CanInterfaceId CanMessage::getInterfaceId() const
  119. {
  120. return _interface;
  121. }
  122. void CanMessage::setInterfaceId(CanInterfaceId interface)
  123. {
  124. _interface = interface;
  125. }
  126. uint8_t CanMessage::getLength() const {
  127. return _dlc;
  128. }
  129. void CanMessage::setLength(const uint8_t dlc) {
  130. // Limit to CANFD max length
  131. if (dlc<=64) {
  132. _dlc = dlc;
  133. } else {
  134. _dlc = 8;
  135. }
  136. }
  137. uint8_t CanMessage::getByte(const uint8_t index) const {
  138. if (index<sizeof(_u8)) {
  139. return _u8[index];
  140. } else {
  141. return 0;
  142. }
  143. }
  144. void CanMessage::setByte(const uint8_t index, const uint8_t value) {
  145. if (index<sizeof(_u8)) {
  146. _u8[index] = value;
  147. }
  148. }
  149. uint64_t CanMessage::extractRawSignal(uint8_t start_bit, const uint8_t length, const bool isBigEndian) const
  150. {
  151. // if ((start_bit+length) > (getLength()*8)) {
  152. // return 0;
  153. // }
  154. // FIXME: This only gives access to data bytes 0-8. Need to rework for CANFD.
  155. uint64_t data = le64toh(_u64[0]);
  156. data >>= start_bit;
  157. uint64_t mask = 0xFFFFFFFFFFFFFFFF;
  158. mask <<= length;
  159. mask = ~mask;
  160. data &= mask;
  161. // If the length is greater than 8, we need to byteswap to preserve endianness
  162. if (isBigEndian && (length > 8))
  163. {
  164. // Swap bytes
  165. data = __builtin_bswap64(data);
  166. // Shift out unused bits
  167. data >>= 64 - length;
  168. }
  169. return data;
  170. }
  171. void CanMessage::setDataAt(uint8_t position, uint8_t data)
  172. {
  173. if(position < 64)
  174. _u8[position] = data;
  175. else
  176. return;
  177. }
  178. void CanMessage::setData(const uint8_t d0) {
  179. _dlc = 1;
  180. _u8[0] = d0;
  181. }
  182. void CanMessage::setData(const uint8_t d0, const uint8_t d1) {
  183. _dlc = 2;
  184. _u8[0] = d0;
  185. _u8[1] = d1;
  186. }
  187. void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2) {
  188. _dlc = 3;
  189. _u8[0] = d0;
  190. _u8[1] = d1;
  191. _u8[2] = d2;
  192. }
  193. void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2,
  194. const uint8_t d3) {
  195. _dlc = 4;
  196. _u8[0] = d0;
  197. _u8[1] = d1;
  198. _u8[2] = d2;
  199. _u8[3] = d3;
  200. }
  201. void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2,
  202. const uint8_t d3, const uint8_t d4) {
  203. _dlc = 5;
  204. _u8[0] = d0;
  205. _u8[1] = d1;
  206. _u8[2] = d2;
  207. _u8[3] = d3;
  208. _u8[4] = d4;
  209. }
  210. void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2,
  211. const uint8_t d3, const uint8_t d4, const uint8_t d5) {
  212. _dlc = 6;
  213. _u8[0] = d0;
  214. _u8[1] = d1;
  215. _u8[2] = d2;
  216. _u8[3] = d3;
  217. _u8[4] = d4;
  218. _u8[5] = d5;
  219. }
  220. void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2,
  221. const uint8_t d3, const uint8_t d4, const uint8_t d5,
  222. const uint8_t d6) {
  223. _dlc = 7;
  224. _u8[0] = d0;
  225. _u8[1] = d1;
  226. _u8[2] = d2;
  227. _u8[3] = d3;
  228. _u8[4] = d4;
  229. _u8[5] = d5;
  230. _u8[6] = d6;
  231. }
  232. void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2,
  233. const uint8_t d3, const uint8_t d4, const uint8_t d5, const uint8_t d6,
  234. const uint8_t d7) {
  235. _dlc = 8;
  236. _u8[0] = d0;
  237. _u8[1] = d1;
  238. _u8[2] = d2;
  239. _u8[3] = d3;
  240. _u8[4] = d4;
  241. _u8[5] = d5;
  242. _u8[6] = d6;
  243. _u8[7] = d7;
  244. }
  245. timeval CanMessage::getTimestamp() const
  246. {
  247. return _timestamp;
  248. }
  249. void CanMessage::setTimestamp(const timeval timestamp)
  250. {
  251. _timestamp = timestamp;
  252. }
  253. void CanMessage::setTimestamp(const uint64_t seconds, const uint32_t micro_seconds)
  254. {
  255. _timestamp.tv_sec = seconds;
  256. _timestamp.tv_usec = micro_seconds;
  257. }
  258. double CanMessage::getFloatTimestamp() const
  259. {
  260. return (double)_timestamp.tv_sec + ((double)_timestamp.tv_usec/1000000);
  261. }
  262. QDateTime CanMessage::getDateTime() const
  263. {
  264. return QDateTime::fromMSecsSinceEpoch((qint64)(1000*getFloatTimestamp()));
  265. }
  266. QString CanMessage::getIdString() const
  267. {
  268. if (isExtended()) {
  269. return QString().sprintf("0x%08X", getId());
  270. } else {
  271. return QString().sprintf("0x%03X", getId());
  272. }
  273. }
  274. QString CanMessage::getDataHexString() const
  275. {
  276. if(getLength() == 0)
  277. return "";
  278. QString outstr = "";
  279. for(int i=0; i<getLength(); i++)
  280. {
  281. if((i==0)||(i==16)||(i==32)||(i==48))
  282. {
  283. outstr += QString().sprintf("::%02X ", getByte(i)); //20230418,显示数据分组方便数字节
  284. }
  285. else
  286. outstr += QString().sprintf("%02X ", getByte(i));
  287. }
  288. return outstr;
  289. }