can_driver.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include "can.hpp"
  6. #include <uavcan/driver/can.hpp>
  7. TEST(CanFrame, FrameProperties)
  8. {
  9. EXPECT_TRUE(makeCanFrame(0, "", EXT).isExtended());
  10. EXPECT_FALSE(makeCanFrame(0, "", STD).isExtended());
  11. EXPECT_FALSE(makeCanFrame(0, "", EXT).isRemoteTransmissionRequest());
  12. EXPECT_FALSE(makeCanFrame(0, "", STD).isRemoteTransmissionRequest());
  13. uavcan::CanFrame frame = makeCanFrame(123, "", STD);
  14. frame.id |= uavcan::CanFrame::FlagRTR;
  15. EXPECT_TRUE(frame.isRemoteTransmissionRequest());
  16. EXPECT_FALSE(frame.isErrorFrame());
  17. frame.id |= uavcan::CanFrame::FlagERR;
  18. EXPECT_TRUE(frame.isErrorFrame());
  19. }
  20. TEST(CanFrame, Arbitration)
  21. {
  22. using uavcan::CanFrame;
  23. CanFrame a;
  24. CanFrame b;
  25. /*
  26. * Simple
  27. */
  28. a.id = 123;
  29. b.id = 122;
  30. ASSERT_TRUE(a.priorityLowerThan(b));
  31. ASSERT_TRUE(b.priorityHigherThan(a));
  32. a.id = 123 | CanFrame::FlagEFF;
  33. b.id = 122 | CanFrame::FlagEFF;
  34. ASSERT_TRUE(a.priorityLowerThan(b));
  35. ASSERT_TRUE(b.priorityHigherThan(a));
  36. a.id = 8;
  37. b.id = 8;
  38. ASSERT_FALSE(a.priorityLowerThan(b));
  39. ASSERT_FALSE(b.priorityHigherThan(a));
  40. /*
  41. * EXT vs STD
  42. */
  43. a.id = 1000; // 1000
  44. b.id = 2000 | CanFrame::FlagEFF; // 2000 >> 18, wins
  45. ASSERT_TRUE(a.priorityLowerThan(b));
  46. ASSERT_TRUE(b.priorityHigherThan(a));
  47. a.id = 0x400;
  48. b.id = 0x10000000 | CanFrame::FlagEFF; // (0x400 << 18), 11 most significant bits are the same --> EFF loses
  49. ASSERT_TRUE(a.priorityHigherThan(b));
  50. ASSERT_TRUE(b.priorityLowerThan(a));
  51. /*
  52. * RTR vs Data
  53. */
  54. a.id = 123 | CanFrame::FlagRTR; // On the same ID, RTR loses
  55. b.id = 123;
  56. ASSERT_TRUE(a.priorityLowerThan(b));
  57. ASSERT_TRUE(b.priorityHigherThan(a));
  58. a.id = CanFrame::MaskStdID | CanFrame::FlagRTR; // RTR is STD, so it wins
  59. b.id = CanFrame::MaskExtID | CanFrame::FlagEFF; // Lowest possible priority for data frame
  60. ASSERT_TRUE(a.priorityHigherThan(b));
  61. ASSERT_TRUE(b.priorityLowerThan(a));
  62. a.id = 123 | CanFrame::FlagRTR; // Both RTR arbitrate as usually
  63. b.id = 122 | CanFrame::FlagRTR;
  64. ASSERT_TRUE(a.priorityLowerThan(b));
  65. ASSERT_TRUE(b.priorityHigherThan(a));
  66. }
  67. TEST(CanFrame, ToString)
  68. {
  69. uavcan::CanFrame frame = makeCanFrame(123, "\x01\x02\x03\x04" "1234", EXT);
  70. EXPECT_EQ("0x0000007b 01 02 03 04 31 32 33 34 '....1234'", frame.toString());
  71. EXPECT_TRUE(frame.toString() == frame.toString(uavcan::CanFrame::StrAligned));
  72. frame = makeCanFrame(123, "z", EXT);
  73. EXPECT_EQ("0x0000007b 7a 'z'", frame.toString(uavcan::CanFrame::StrAligned));
  74. EXPECT_EQ("0x0000007b 7a 'z'", frame.toString());
  75. EXPECT_EQ(" 0x141 61 62 63 64 aa bb cc dd 'abcd....'",
  76. makeCanFrame(321, "abcd" "\xaa\xbb\xcc\xdd", STD).toString(uavcan::CanFrame::StrAligned));
  77. EXPECT_EQ(" 0x100 ''",
  78. makeCanFrame(256, "", STD).toString(uavcan::CanFrame::StrAligned));
  79. EXPECT_EQ("0x100 ''",
  80. makeCanFrame(256, "", STD).toString());
  81. EXPECT_EQ("0x141 61 62 63 64 aa bb cc dd 'abcd....'",
  82. makeCanFrame(321, "abcd" "\xaa\xbb\xcc\xdd", STD).toString());
  83. }