allocation_request_manager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <gtest/gtest.h>
8 #include "event_tracer.hpp"
9 #include "../helpers.hpp"
10 
11 
13 
15 {
16  std::vector<std::pair<UniqueID, uavcan::NodeID> > requests_;
17 
18 public:
20 
22 
23  virtual void handleAllocationRequest(const UniqueID& unique_id, uavcan::NodeID preferred_node_id)
24  {
25  requests_.push_back(std::pair<UniqueID, uavcan::NodeID>(unique_id, preferred_node_id));
26  }
27 
29  {
30  return can_followup;
31  }
32 
33  bool matchAndPopLastRequest(const UniqueID& unique_id, uavcan::NodeID preferred_node_id)
34  {
35  if (requests_.empty())
36  {
37  std::cout << "No pending requests" << std::endl;
38  return false;
39  }
40 
41  const std::pair<UniqueID, uavcan::NodeID> pair = requests_.at(requests_.size() - 1U);
42  requests_.pop_back();
43 
44  if (pair.first != unique_id)
45  {
46  std::cout << "Unique ID mismatch" << std::endl;
47  return false;
48  }
49 
50  if (pair.second != preferred_node_id)
51  {
52  std::cout << "Node ID mismatch (" << pair.second.get() << ", " << preferred_node_id.get() << ")"
53  << std::endl;
54  return false;
55  }
56 
57  return true;
58  }
59 
60  void reset() { requests_.clear(); }
61 };
62 
63 
64 TEST(dynamic_node_id_server_AllocationRequestManager, Basic)
65 {
66  using namespace uavcan::protocol::dynamic_node_id;
67  using namespace uavcan::protocol::dynamic_node_id::server;
68  using namespace uavcan::dynamic_node_id_server;
69 
72 
73  // Node A is Allocator, Node B is Allocatee
75 
76  uavcan::DynamicNodeIDClient client(nodes.b);
77 
78  /*
79  * Client initialization
80  */
81  uavcan::protocol::HardwareVersion::FieldTypes::unique_id unique_id;
82  for (uavcan::uint8_t i = 0; i < unique_id.size(); i++)
83  {
84  unique_id[i] = i;
85  }
86  const uavcan::NodeID PreferredNodeID = 42;
87  ASSERT_LE(0, client.start(unique_id, PreferredNodeID));
88 
89  /*
90  * Request manager initialization
91  */
92  EventTracer tracer;
94  handler.can_followup = true;
95 
96  AllocationRequestManager manager(nodes.a, tracer, handler);
97 
99 
100  /*
101  * Allocation
102  */
104 
105  ASSERT_TRUE(handler.matchAndPopLastRequest(unique_id, PreferredNodeID));
106 
107  ASSERT_LE(0, manager.broadcastAllocationResponse(unique_id, PreferredNodeID));
108 
110 
111  /*
112  * Checking the client
113  */
114  ASSERT_TRUE(client.isAllocationComplete());
115 
116  ASSERT_EQ(PreferredNodeID, client.getAllocatedNodeID());
117 }
uavcan::DefaultDataTypeRegistrator
Definition: global_data_type_registry.hpp:186
uavcan::DynamicNodeIDClient::start
int start(const UniqueID &unique_id, const NodeID preferred_node_id=NodeID::Broadcast, const TransferPriority transfer_priority=TransferPriority::OneHigherThanLowest)
Definition: uc_dynamic_node_id_client.cpp:147
AllocationRequestHandler::handleAllocationRequest
virtual void handleAllocationRequest(const UniqueID &unique_id, uavcan::NodeID preferred_node_id)
Definition: allocation_request_manager.cpp:23
uavcan::NodeID::get
uint8_t get() const
Definition: transfer.hpp:132
uavcan::NodeID
Definition: transfer.hpp:112
uavcan::DurationBase< MonotonicDuration >::fromMSec
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
uavcan::dynamic_node_id_server::AllocationRequestManager::broadcastAllocationResponse
int broadcastAllocationResponse(const UniqueID &unique_id, NodeID allocated_node_id)
Definition: allocation_request_manager.hpp:261
TEST
TEST(dynamic_node_id_server_AllocationRequestManager, Basic)
Definition: allocation_request_manager.cpp:64
uavcan::DynamicNodeIDClient::isAllocationComplete
bool isAllocationComplete() const
Definition: dynamic_node_id_client.hpp:94
uavcan::dynamic_node_id_server::AllocationRequestManager::init
int init(const TransferPriority priority)
Definition: allocation_request_manager.hpp:242
uavcan::dynamic_node_id_server::IAllocationRequestHandler
Definition: allocation_request_manager.hpp:25
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
uavcan::TransferPriority::OneHigherThanLowest
static const TransferPriority OneHigherThanLowest
Definition: transfer.hpp:40
AllocationRequestHandler::requests_
std::vector< std::pair< UniqueID, uavcan::NodeID > > requests_
Definition: allocation_request_manager.cpp:16
AllocationRequestHandler::AllocationRequestHandler
AllocationRequestHandler()
Definition: allocation_request_manager.cpp:21
AllocationRequestHandler::matchAndPopLastRequest
bool matchAndPopLastRequest(const UniqueID &unique_id, uavcan::NodeID preferred_node_id)
Definition: allocation_request_manager.cpp:33
uavcan::DynamicNodeIDClient
Definition: dynamic_node_id_client.hpp:31
AllocationRequestHandler::reset
void reset()
Definition: allocation_request_manager.cpp:60
InterlinkedTestNodes
Definition: test_node.hpp:149
uavcan::NodeID::Broadcast
static const NodeID Broadcast
Definition: transfer.hpp:122
uavcan::DynamicNodeIDClient::getAllocatedNodeID
NodeID getAllocatedNodeID() const
Definition: dynamic_node_id_client.hpp:102
dynamic_node_id_client.hpp
AllocationRequestHandler
Definition: allocation_request_manager.cpp:14
allocation_request_manager.hpp
AllocationRequestHandler::can_followup
bool can_followup
Definition: allocation_request_manager.cpp:19
InterlinkedTestNodes::a
TestNode a
Definition: test_node.hpp:155
InterlinkedTestNodes::spinBoth
int spinBoth(uavcan::MonotonicDuration duration)
Definition: test_node.hpp:176
uavcan::dynamic_node_id_server
Definition: abstract_server.hpp:16
event_tracer.hpp
uavcan::GlobalDataTypeRegistry::instance
static GlobalDataTypeRegistry & instance()
Definition: uc_global_data_type_registry.cpp:128
AllocationRequestHandler::canPublishFollowupAllocationResponse
virtual bool canPublishFollowupAllocationResponse() const
Definition: allocation_request_manager.cpp:28
uavcan::dynamic_node_id_server::AllocationRequestManager
Definition: allocation_request_manager.hpp:45
InterlinkedTestNodes::b
TestNode b
Definition: test_node.hpp:156
uavcan::dynamic_node_id_server::UniqueID
protocol::dynamic_node_id::server::Entry::FieldTypes::unique_id UniqueID
Definition: protocol/dynamic_node_id_server/types.hpp:22


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02