restart_request_server.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/protocol/restart_request_server.hpp>
  6. #include "helpers.hpp"
  7. struct RestartHandler : public uavcan::IRestartRequestHandler
  8. {
  9. bool accept;
  10. bool handleRestartRequest(uavcan::NodeID request_source)
  11. {
  12. std::cout << "Restart request from " << int(request_source.get()) << " will be "
  13. << (accept ? "accepted" : "rejected") << std::endl;
  14. return accept;
  15. }
  16. };
  17. TEST(RestartRequestServer, Basic)
  18. {
  19. InterlinkedTestNodesWithSysClock nodes;
  20. uavcan::RestartRequestServer rrs(nodes.a);
  21. ServiceClientWithCollector<uavcan::protocol::RestartNode> rrs_cln(nodes.b);
  22. uavcan::GlobalDataTypeRegistry::instance().reset();
  23. uavcan::DefaultDataTypeRegistrator<uavcan::protocol::RestartNode> _reg1;
  24. ASSERT_LE(0, rrs.start());
  25. uavcan::protocol::RestartNode::Request request;
  26. request.magic_number = uavcan::protocol::RestartNode::Request::MAGIC_NUMBER;
  27. /*
  28. * Rejected - handler was not set
  29. */
  30. ASSERT_LE(0, rrs_cln.call(1, request));
  31. nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10));
  32. ASSERT_TRUE(rrs_cln.collector.result.get());
  33. ASSERT_TRUE(rrs_cln.collector.result->isSuccessful());
  34. ASSERT_FALSE(rrs_cln.collector.result->getResponse().ok);
  35. /*
  36. * Accepted
  37. */
  38. RestartHandler handler;
  39. handler.accept = true;
  40. rrs.setHandler(&handler);
  41. ASSERT_LE(0, rrs_cln.call(1, request));
  42. nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10));
  43. ASSERT_TRUE(rrs_cln.collector.result->isSuccessful());
  44. ASSERT_TRUE(rrs_cln.collector.result->getResponse().ok);
  45. /*
  46. * Rejected by handler
  47. */
  48. handler.accept = false;
  49. ASSERT_LE(0, rrs_cln.call(1, request));
  50. nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10));
  51. ASSERT_TRUE(rrs_cln.collector.result->isSuccessful());
  52. ASSERT_FALSE(rrs_cln.collector.result->getResponse().ok);
  53. /*
  54. * Rejected because of invalid magic number
  55. */
  56. handler.accept = true;
  57. ASSERT_LE(0, rrs_cln.call(1, uavcan::protocol::RestartNode::Request()));
  58. nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10));
  59. ASSERT_TRUE(rrs_cln.collector.result->isSuccessful());
  60. ASSERT_FALSE(rrs_cln.collector.result->getResponse().ok);
  61. }