libs
libuavcan
libuavcan
include
uavcan
protocol
dynamic_node_id_server
node_id_selector.hpp
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
3
*/
4
5
#ifndef UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_SERVER_NODE_ID_SELECTOR_HPP_INCLUDED
6
#define UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_SERVER_NODE_ID_SELECTOR_HPP_INCLUDED
7
8
#include <
uavcan/build_config.hpp
>
9
#include <cassert>
10
11
namespace
uavcan
12
{
13
namespace
dynamic_node_id_server
14
{
18
template
<
typename
Owner>
19
class
NodeIDSelector
20
{
21
typedef
bool (Owner::*
IsNodeIDTakenMethod
)(
const
NodeID
node_id)
const
;
22
23
const
Owner*
const
owner_
;
24
const
IsNodeIDTakenMethod
is_node_id_taken_
;
25
26
public
:
27
NodeIDSelector
(
const
Owner* owner,
IsNodeIDTakenMethod
is_node_id_taken)
28
:
owner_
(owner)
29
,
is_node_id_taken_
(is_node_id_taken)
30
{
31
UAVCAN_ASSERT
(
owner_
!=
UAVCAN_NULLPTR
);
32
UAVCAN_ASSERT
(
is_node_id_taken_
!=
UAVCAN_NULLPTR
);
33
}
34
38
NodeID
findFreeNodeID
(
const
NodeID
preferred)
const
39
{
40
uint8_t
candidate = preferred.
isUnicast
() ? preferred.
get
() :
NodeID::MaxRecommendedForRegularNodes
;
41
42
// Up
43
while
(candidate <=
NodeID::MaxRecommendedForRegularNodes
)
44
{
45
if
(!(
owner_
->*
is_node_id_taken_
)(candidate))
46
{
47
return
candidate;
48
}
49
candidate++;
50
}
51
52
candidate = preferred.
isUnicast
() ? preferred.
get
() :
NodeID::MaxRecommendedForRegularNodes
;
53
54
// Down
55
while
(candidate > 0)
56
{
57
if
(!(
owner_
->*
is_node_id_taken_
)(candidate))
58
{
59
return
candidate;
60
}
61
candidate--;
62
}
63
64
return
NodeID
();
65
}
66
};
67
68
}
69
}
70
71
#endif // Include guard
UAVCAN_NULLPTR
#define UAVCAN_NULLPTR
Definition:
libuavcan/libuavcan/include/uavcan/build_config.hpp:51
uavcan::dynamic_node_id_server::NodeIDSelector::is_node_id_taken_
const IsNodeIDTakenMethod is_node_id_taken_
Definition:
node_id_selector.hpp:24
uavcan::NodeID::get
uint8_t get() const
Definition:
transfer.hpp:132
uavcan::dynamic_node_id_server::NodeIDSelector::IsNodeIDTakenMethod
bool(Owner::* IsNodeIDTakenMethod)(const NodeID node_id) const
Definition:
node_id_selector.hpp:21
uavcan::NodeID
Definition:
transfer.hpp:112
uavcan::dynamic_node_id_server::NodeIDSelector::owner_
const Owner *const owner_
Definition:
node_id_selector.hpp:23
uavcan::uint8_t
std::uint8_t uint8_t
Definition:
std.hpp:24
uavcan::NodeID::isUnicast
bool isUnicast() const
Definition:
transfer.hpp:136
uavcan::NodeID::MaxRecommendedForRegularNodes
static const uint8_t MaxRecommendedForRegularNodes
Definition:
transfer.hpp:121
uavcan::dynamic_node_id_server::NodeIDSelector::NodeIDSelector
NodeIDSelector(const Owner *owner, IsNodeIDTakenMethod is_node_id_taken)
Definition:
node_id_selector.hpp:27
build_config.hpp
uavcan::dynamic_node_id_server::NodeIDSelector
Definition:
node_id_selector.hpp:19
uavcan::dynamic_node_id_server::NodeIDSelector::findFreeNodeID
NodeID findFreeNodeID(const NodeID preferred) const
Definition:
node_id_selector.hpp:38
uavcan
Definition:
libuavcan/libuavcan/include/uavcan/build_config.hpp:204
UAVCAN_ASSERT
#define UAVCAN_ASSERT(x)
Definition:
libuavcan/libuavcan/include/uavcan/build_config.hpp:184
uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02