node_status_provider.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/protocol/node_status_provider.hpp>
  6. #include "helpers.hpp"
  7. TEST(NodeStatusProvider, Basic)
  8. {
  9. InterlinkedTestNodesWithSysClock nodes;
  10. uavcan::NodeStatusProvider nsp(nodes.a);
  11. /*
  12. * Initialization
  13. */
  14. uavcan::protocol::HardwareVersion hwver;
  15. hwver.major = 3;
  16. hwver.minor = 14;
  17. uavcan::protocol::SoftwareVersion swver;
  18. swver.major = 2;
  19. swver.minor = 18;
  20. swver.vcs_commit = 0x600DF00D;
  21. nsp.setHardwareVersion(hwver);
  22. nsp.setSoftwareVersion(swver);
  23. ASSERT_TRUE(nsp.getName().empty());
  24. nsp.setName("superluminal_communication_unit");
  25. ASSERT_STREQ("superluminal_communication_unit", nsp.getName().c_str());
  26. ASSERT_EQ(uavcan::protocol::NodeStatus::HEALTH_OK, nsp.getHealth());
  27. ASSERT_EQ(uavcan::protocol::NodeStatus::MODE_INITIALIZATION, nsp.getMode());
  28. nsp.setHealthError();
  29. nsp.setModeOperational();
  30. ASSERT_EQ(uavcan::protocol::NodeStatus::HEALTH_ERROR, nsp.getHealth());
  31. ASSERT_EQ(uavcan::protocol::NodeStatus::MODE_OPERATIONAL, nsp.getMode());
  32. // Will fail - types are not registered
  33. uavcan::GlobalDataTypeRegistry::instance().reset();
  34. ASSERT_GT(0, nsp.startAndPublish());
  35. uavcan::GlobalDataTypeRegistry::instance().reset();
  36. uavcan::DefaultDataTypeRegistrator<uavcan::protocol::NodeStatus> _reg1;
  37. uavcan::DefaultDataTypeRegistrator<uavcan::protocol::GetNodeInfo> _reg2;
  38. ASSERT_LE(0, nsp.startAndPublish());
  39. // Checking the publishing rate settings
  40. ASSERT_EQ(uavcan::MonotonicDuration::fromMSec(uavcan::protocol::NodeStatus::MAX_BROADCASTING_PERIOD_MS),
  41. nsp.getStatusPublicationPeriod());
  42. nsp.setStatusPublicationPeriod(uavcan::MonotonicDuration());
  43. ASSERT_EQ(uavcan::MonotonicDuration::fromMSec(uavcan::protocol::NodeStatus::MIN_BROADCASTING_PERIOD_MS),
  44. nsp.getStatusPublicationPeriod());
  45. nsp.setStatusPublicationPeriod(uavcan::MonotonicDuration::fromMSec(3600 * 1000 * 24));
  46. ASSERT_EQ(uavcan::MonotonicDuration::fromMSec(uavcan::protocol::NodeStatus::MAX_BROADCASTING_PERIOD_MS),
  47. nsp.getStatusPublicationPeriod());
  48. /*
  49. * Initial status publication
  50. */
  51. SubscriberWithCollector<uavcan::protocol::NodeStatus> status_sub(nodes.b);
  52. ASSERT_LE(0, status_sub.start());
  53. ASSERT_FALSE(status_sub.collector.msg.get()); // No data yet
  54. nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10));
  55. ASSERT_TRUE(status_sub.collector.msg.get()); // Was published at startup
  56. ASSERT_EQ(uavcan::protocol::NodeStatus::HEALTH_ERROR, status_sub.collector.msg->health);
  57. ASSERT_EQ(0, status_sub.collector.msg->vendor_specific_status_code);
  58. ASSERT_GE(1, status_sub.collector.msg->uptime_sec);
  59. /*
  60. * Altering the vendor-specific status code, forcePublish()-ing it and checking the result
  61. */
  62. ASSERT_EQ(0, nsp.getVendorSpecificStatusCode());
  63. nsp.setVendorSpecificStatusCode(1234);
  64. ASSERT_EQ(1234, nsp.getVendorSpecificStatusCode());
  65. ASSERT_LE(0, nsp.forcePublish());
  66. nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10));
  67. ASSERT_EQ(uavcan::protocol::NodeStatus::HEALTH_ERROR, status_sub.collector.msg->health);
  68. ASSERT_EQ(1234, status_sub.collector.msg->vendor_specific_status_code);
  69. ASSERT_GE(1, status_sub.collector.msg->uptime_sec);
  70. /*
  71. * Explicit node info request
  72. */
  73. ServiceClientWithCollector<uavcan::protocol::GetNodeInfo> gni_cln(nodes.b);
  74. nsp.setHealthCritical();
  75. ASSERT_FALSE(gni_cln.collector.result.get()); // No data yet
  76. ASSERT_LE(0, gni_cln.call(1, uavcan::protocol::GetNodeInfo::Request()));
  77. nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10));
  78. ASSERT_TRUE(gni_cln.collector.result.get()); // Response must have been delivered
  79. ASSERT_TRUE(gni_cln.collector.result->isSuccessful());
  80. ASSERT_EQ(1, gni_cln.collector.result->getCallID().server_node_id.get());
  81. ASSERT_EQ(uavcan::protocol::NodeStatus::HEALTH_CRITICAL,
  82. gni_cln.collector.result->getResponse().status.health);
  83. ASSERT_TRUE(hwver == gni_cln.collector.result->getResponse().hardware_version);
  84. ASSERT_TRUE(swver == gni_cln.collector.result->getResponse().software_version);
  85. ASSERT_EQ("superluminal_communication_unit", gni_cln.collector.result->getResponse().name);
  86. }