test_socket.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <iostream>
  5. #include <vector>
  6. #include <cerrno>
  7. #include <uavcan_linux/uavcan_linux.hpp>
  8. #include "debug.hpp"
  9. static uavcan::CanFrame makeFrame(std::uint32_t id, const std::string& data)
  10. {
  11. return uavcan::CanFrame(id, reinterpret_cast<const std::uint8_t*>(data.c_str()), data.length());
  12. }
  13. static uavcan::MonotonicTime tsMonoOffsetMs(std::int64_t ms)
  14. {
  15. return uavcan_linux::SystemClock().getMonotonic() + uavcan::MonotonicDuration::fromMSec(ms);
  16. }
  17. static void testNonexistentIface()
  18. {
  19. const int sock1 = uavcan_linux::SocketCanIface::openSocket("noif9");
  20. ENFORCE(sock1 < 0);
  21. const int sock2 = uavcan_linux::SocketCanIface::openSocket("verylongifacenameverylongifacenameverylongifacename");
  22. ENFORCE(sock2 < 0);
  23. }
  24. static void testSocketRxTx(const std::string& iface_name)
  25. {
  26. const int sock1 = uavcan_linux::SocketCanIface::openSocket(iface_name);
  27. const int sock2 = uavcan_linux::SocketCanIface::openSocket(iface_name);
  28. ENFORCE(sock1 >= 0 && sock2 >= 0);
  29. /*
  30. * Clocks will have some offset from the true system time
  31. * SocketCAN driver must handle this correctly
  32. */
  33. uavcan_linux::SystemClock clock_impl(uavcan_linux::ClockAdjustmentMode::PerDriverPrivate);
  34. clock_impl.adjustUtc(uavcan::UtcDuration::fromMSec(100000));
  35. const uavcan_linux::SystemClock& clock = clock_impl;
  36. uavcan_linux::SocketCanIface if1(clock, sock1);
  37. uavcan_linux::SocketCanIface if2(clock, sock2);
  38. /*
  39. * Sending two frames, one of which must be returned back
  40. */
  41. ENFORCE(1 == if1.send(makeFrame(123, "if1-1"), tsMonoOffsetMs(100), 0));
  42. ENFORCE(1 == if1.send(makeFrame(456, "if1-2"), tsMonoOffsetMs(100), uavcan::CanIOFlagLoopback));
  43. if1.poll(true, true);
  44. if1.poll(true, true);
  45. ENFORCE(0 == if1.getErrorCount());
  46. ENFORCE(!if1.hasReadyTx());
  47. ENFORCE(if1.hasReadyRx()); // Second loopback
  48. /*
  49. * Second iface, same thing
  50. */
  51. ENFORCE(1 == if2.send(makeFrame(321, "if2-1"), tsMonoOffsetMs(100), 0));
  52. ENFORCE(1 == if2.send(makeFrame(654, "if2-2"), tsMonoOffsetMs(100), uavcan::CanIOFlagLoopback));
  53. ENFORCE(1 == if2.send(makeFrame(1, "discard"), tsMonoOffsetMs(-1), uavcan::CanIOFlagLoopback)); // Will timeout
  54. if2.poll(true, true);
  55. if2.poll(true, true);
  56. ENFORCE(1 == if2.getErrorCount()); // One timed out
  57. ENFORCE(!if2.hasReadyTx());
  58. ENFORCE(if2.hasReadyRx());
  59. /*
  60. * No-op
  61. */
  62. if1.poll(true, true);
  63. if2.poll(true, true);
  64. uavcan::CanFrame frame;
  65. uavcan::MonotonicTime ts_mono;
  66. uavcan::UtcTime ts_utc;
  67. uavcan::CanIOFlags flags = 0;
  68. /*
  69. * Read first
  70. */
  71. ENFORCE(1 == if1.receive(frame, ts_mono, ts_utc, flags));
  72. ENFORCE(frame == makeFrame(456, "if1-2"));
  73. ENFORCE(flags == uavcan::CanIOFlagLoopback);
  74. ENFORCE((clock.getMonotonic() - ts_mono).getAbs().toMSec() < 10);
  75. ENFORCE((clock.getUtc() - ts_utc).getAbs().toMSec() < 10);
  76. ENFORCE(1 == if1.receive(frame, ts_mono, ts_utc, flags));
  77. ENFORCE(frame == makeFrame(321, "if2-1"));
  78. ENFORCE(flags == 0);
  79. ENFORCE(!ts_mono.isZero());
  80. ENFORCE(!ts_utc.isZero());
  81. ENFORCE((clock.getMonotonic() - ts_mono).getAbs().toMSec() < 10);
  82. ENFORCE((clock.getUtc() - ts_utc).getAbs().toMSec() < 10);
  83. ENFORCE(1 == if1.receive(frame, ts_mono, ts_utc, flags));
  84. ENFORCE(frame == makeFrame(654, "if2-2"));
  85. ENFORCE(flags == 0);
  86. ENFORCE((clock.getMonotonic() - ts_mono).getAbs().toMSec() < 10);
  87. ENFORCE((clock.getUtc() - ts_utc).getAbs().toMSec() < 10);
  88. ENFORCE(0 == if1.receive(frame, ts_mono, ts_utc, flags));
  89. ENFORCE(!if1.hasReadyTx());
  90. ENFORCE(!if1.hasReadyRx());
  91. /*
  92. * Read second
  93. */
  94. ENFORCE(1 == if2.receive(frame, ts_mono, ts_utc, flags));
  95. ENFORCE(frame == makeFrame(123, "if1-1"));
  96. ENFORCE(flags == 0);
  97. ENFORCE((clock.getMonotonic() - ts_mono).getAbs().toMSec() < 10);
  98. ENFORCE((clock.getUtc() - ts_utc).getAbs().toMSec() < 10);
  99. ENFORCE(1 == if2.receive(frame, ts_mono, ts_utc, flags));
  100. ENFORCE(frame == makeFrame(456, "if1-2"));
  101. ENFORCE(flags == 0);
  102. ENFORCE((clock.getMonotonic() - ts_mono).getAbs().toMSec() < 10);
  103. ENFORCE((clock.getUtc() - ts_utc).getAbs().toMSec() < 10);
  104. ENFORCE(1 == if2.receive(frame, ts_mono, ts_utc, flags));
  105. ENFORCE(frame == makeFrame(654, "if2-2"));
  106. ENFORCE(flags == uavcan::CanIOFlagLoopback);
  107. ENFORCE((clock.getMonotonic() - ts_mono).getAbs().toMSec() < 10);
  108. ENFORCE((clock.getUtc() - ts_utc).getAbs().toMSec() < 10);
  109. ENFORCE(0 == if2.receive(frame, ts_mono, ts_utc, flags));
  110. ENFORCE(!if2.hasReadyTx());
  111. ENFORCE(!if2.hasReadyRx());
  112. }
  113. static void testSocketFilters(const std::string& iface_name)
  114. {
  115. using uavcan::CanFrame;
  116. const int sock1 = uavcan_linux::SocketCanIface::openSocket(iface_name);
  117. const int sock2 = uavcan_linux::SocketCanIface::openSocket(iface_name);
  118. ENFORCE(sock1 >= 0 && sock2 >= 0);
  119. /*
  120. * Clocks will have some offset from the true system time
  121. * SocketCAN driver must handle this correctly
  122. */
  123. uavcan_linux::SystemClock clock_impl(uavcan_linux::ClockAdjustmentMode::PerDriverPrivate);
  124. clock_impl.adjustUtc(uavcan::UtcDuration::fromMSec(-1000));
  125. const uavcan_linux::SystemClock& clock = clock_impl;
  126. uavcan_linux::SocketCanIface if1(clock, sock1);
  127. uavcan_linux::SocketCanIface if2(clock, sock2);
  128. /*
  129. * Configuring filters
  130. */
  131. uavcan::CanFilterConfig fcs[3];
  132. // STD/EXT 123
  133. fcs[0].id = 123;
  134. fcs[0].mask = CanFrame::MaskExtID;
  135. // Only EXT 456789
  136. fcs[1].id = 456789 | CanFrame::FlagEFF;
  137. fcs[1].mask = CanFrame::MaskExtID | CanFrame::FlagEFF;
  138. // Only STD 0
  139. fcs[2].id = 0;
  140. fcs[2].mask = CanFrame::MaskExtID | CanFrame::FlagEFF;
  141. ENFORCE(0 == if2.configureFilters(fcs, 3));
  142. /*
  143. * Sending data from 1 to 2, making sure only filtered data will be accepted
  144. */
  145. const auto EFF = CanFrame::FlagEFF;
  146. ENFORCE(1 == if1.send(makeFrame(123, "1"), tsMonoOffsetMs(100), 0)); // Accept 0
  147. ENFORCE(1 == if1.send(makeFrame(123 | EFF, "2"), tsMonoOffsetMs(100), 0)); // Accept 0
  148. ENFORCE(1 == if1.send(makeFrame(456, "3"), tsMonoOffsetMs(100), 0)); // Drop
  149. ENFORCE(1 == if1.send(makeFrame(456789, "4"), tsMonoOffsetMs(100), 0)); // Drop
  150. ENFORCE(1 == if1.send(makeFrame(456789 | EFF, "5"), tsMonoOffsetMs(100), 0)); // Accept 1
  151. ENFORCE(1 == if1.send(makeFrame(0, "6"), tsMonoOffsetMs(100), 0)); // Accept 2
  152. ENFORCE(1 == if1.send(makeFrame(EFF, "7"), tsMonoOffsetMs(100), 0)); // Drop
  153. for (int i = 0; i < 7; i++)
  154. {
  155. if1.poll(true, true);
  156. if2.poll(true, false);
  157. }
  158. ENFORCE(!if1.hasReadyTx());
  159. ENFORCE(!if1.hasReadyRx());
  160. ENFORCE(0 == if1.getErrorCount());
  161. ENFORCE(if2.hasReadyRx());
  162. /*
  163. * Checking RX on 2
  164. */
  165. uavcan::CanFrame frame;
  166. uavcan::MonotonicTime ts_mono;
  167. uavcan::UtcTime ts_utc;
  168. uavcan::CanIOFlags flags = 0;
  169. ENFORCE(1 == if2.receive(frame, ts_mono, ts_utc, flags));
  170. ENFORCE(frame == makeFrame(123, "1"));
  171. ENFORCE(1 == if2.receive(frame, ts_mono, ts_utc, flags));
  172. ENFORCE(frame == makeFrame(123 | EFF, "2"));
  173. ENFORCE(1 == if2.receive(frame, ts_mono, ts_utc, flags));
  174. ENFORCE(frame == makeFrame(456789 | EFF, "5"));
  175. ENFORCE(1 == if2.receive(frame, ts_mono, ts_utc, flags));
  176. ENFORCE(frame == makeFrame(0, "6"));
  177. ENFORCE(flags == 0);
  178. ENFORCE(!if2.hasReadyRx());
  179. }
  180. static void testDriver(const std::vector<std::string>& iface_names)
  181. {
  182. /*
  183. * Clocks will have some offset from the true system time
  184. * SocketCAN driver must handle this correctly
  185. */
  186. uavcan_linux::SystemClock clock_impl(uavcan_linux::ClockAdjustmentMode::PerDriverPrivate);
  187. clock_impl.adjustUtc(uavcan::UtcDuration::fromMSec(9000000));
  188. const uavcan_linux::SystemClock& clock = clock_impl;
  189. uavcan_linux::SocketCanDriver driver(clock);
  190. for (auto ifn : iface_names)
  191. {
  192. std::cout << "Adding iface " << ifn << std::endl;
  193. ENFORCE(0 == driver.addIface(ifn));
  194. }
  195. ENFORCE(-1 == driver.addIface("noif9"));
  196. ENFORCE(-1 == driver.addIface("noif9"));
  197. ENFORCE(-1 == driver.addIface("noif9"));
  198. ENFORCE(driver.getNumIfaces() == iface_names.size());
  199. ENFORCE(nullptr == driver.getIface(255));
  200. ENFORCE(nullptr == driver.getIface(driver.getNumIfaces()));
  201. const uavcan::CanFrame* pending_tx[uavcan::MaxCanIfaces] = {};
  202. const unsigned AllIfacesMask = (1 << driver.getNumIfaces()) - 1;
  203. /*
  204. * Send, no loopback
  205. */
  206. std::cout << "select() 1" << std::endl;
  207. uavcan::CanSelectMasks masks; // Driver provides masks for all available events
  208. ENFORCE(driver.getNumIfaces() == driver.select(masks, pending_tx, tsMonoOffsetMs(1000)));
  209. ENFORCE(masks.read == 0);
  210. ENFORCE(masks.write == AllIfacesMask);
  211. for (int i = 0; i < driver.getNumIfaces(); i++)
  212. {
  213. ENFORCE(1 == driver.getIface(i)->send(makeFrame(123, std::to_string(i)), tsMonoOffsetMs(10), 0));
  214. }
  215. std::cout << "select() 2" << std::endl;
  216. ENFORCE(driver.getNumIfaces() == driver.select(masks, pending_tx, tsMonoOffsetMs(1000)));
  217. ENFORCE(masks.read == 0);
  218. ENFORCE(masks.write == AllIfacesMask);
  219. /*
  220. * Send with loopback
  221. */
  222. for (int i = 0; i < driver.getNumIfaces(); i++)
  223. {
  224. ENFORCE(1 == driver.getIface(i)->send(makeFrame(456, std::to_string(i)), tsMonoOffsetMs(10),
  225. uavcan::CanIOFlagLoopback));
  226. ENFORCE(1 == driver.getIface(i)->send(makeFrame(789, std::to_string(i)), tsMonoOffsetMs(-1), // Will timeout
  227. uavcan::CanIOFlagLoopback));
  228. }
  229. std::cout << "select() 3" << std::endl;
  230. ENFORCE(driver.getNumIfaces() == driver.select(masks, pending_tx, tsMonoOffsetMs(1000)));
  231. ENFORCE(masks.read == AllIfacesMask);
  232. ENFORCE(masks.write == AllIfacesMask);
  233. /*
  234. * Receive loopback
  235. */
  236. for (int i = 0; i < driver.getNumIfaces(); i++)
  237. {
  238. uavcan::CanFrame frame;
  239. uavcan::MonotonicTime ts_mono;
  240. uavcan::UtcTime ts_utc;
  241. uavcan::CanIOFlags flags = 0;
  242. ENFORCE(1 == driver.getIface(i)->receive(frame, ts_mono, ts_utc, flags));
  243. ENFORCE(frame == makeFrame(456, std::to_string(i)));
  244. ENFORCE(flags == uavcan::CanIOFlagLoopback);
  245. ENFORCE((clock.getMonotonic() - ts_mono).getAbs().toMSec() < 10);
  246. ENFORCE((clock.getUtc() - ts_utc).getAbs().toMSec() < 10);
  247. ENFORCE(!driver.getIface(i)->hasReadyTx());
  248. ENFORCE(!driver.getIface(i)->hasReadyRx());
  249. }
  250. std::cout << "select() 4" << std::endl;
  251. masks.write = 0;
  252. ENFORCE(driver.getNumIfaces() == driver.select(masks, pending_tx, tsMonoOffsetMs(1000)));
  253. ENFORCE(masks.read == 0);
  254. ENFORCE(masks.write == AllIfacesMask);
  255. std::cout << "exit" << std::endl;
  256. /*
  257. * Error checks
  258. */
  259. for (int i = 0; i < driver.getNumIfaces(); i++)
  260. {
  261. for (auto kv : driver.getIface(i)->getErrors())
  262. {
  263. switch (kv.first)
  264. {
  265. case uavcan_linux::SocketCanError::SocketReadFailure:
  266. case uavcan_linux::SocketCanError::SocketWriteFailure:
  267. {
  268. ENFORCE(kv.second == 0);
  269. break;
  270. }
  271. case uavcan_linux::SocketCanError::TxTimeout:
  272. {
  273. ENFORCE(kv.second == 1); // One timed out frame from the above
  274. break;
  275. }
  276. default:
  277. {
  278. ENFORCE(false);
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. }
  285. int main(int argc, const char** argv)
  286. {
  287. try
  288. {
  289. if (argc < 2)
  290. {
  291. std::cerr << "Usage:\n\t" << argv[0] << " <can-iface-name-1> [can-iface-name-N...]" << std::endl;
  292. return 1;
  293. }
  294. std::vector<std::string> iface_names;
  295. for (int i = 1; i < argc; i++)
  296. {
  297. iface_names.emplace_back(argv[i]);
  298. }
  299. testNonexistentIface();
  300. testSocketRxTx(iface_names[0]);
  301. testSocketFilters(iface_names[0]);
  302. testDriver(iface_names);
  303. return 0;
  304. }
  305. catch (const std::exception& ex)
  306. {
  307. std::cerr << "Exception: " << ex.what() << std::endl;
  308. return 1;
  309. }
  310. }