lookup.cpp
Go to the documentation of this file.
1 #include "lookup.hpp"
2 
3 #include <algorithm> // For std::transform
4 
5 namespace hebi {
6 
8 
10 
11 std::shared_ptr<Group> Lookup::getGroupFromNames(const std::vector<std::string>& families,
12  const std::vector<std::string>& names, int32_t timeout_ms) {
13  std::shared_ptr<Group> ptr;
14  std::vector<const char*> names_cstrs;
15  std::vector<const char*> families_cstrs;
16  names_cstrs.reserve(names.size());
17  families_cstrs.reserve(families.size());
18 
19  std::transform(std::begin(names), std::end(names), std::back_inserter(names_cstrs),
20  [](const std::string& name) { return name.c_str(); });
21  std::transform(std::begin(families), std::end(families), std::back_inserter(families_cstrs),
22  [](const std::string& family) { return family.c_str(); });
23 
24  HebiGroupPtr group = hebiGroupCreateFromNames(lookup_, families_cstrs.data(), families_cstrs.size(),
25  names_cstrs.data(), names_cstrs.size(), timeout_ms);
26  if (group != nullptr)
27  return std::make_shared<Group>(group, initial_group_feedback_frequency_, initial_group_command_lifetime_);
28  return ptr;
29 }
30 
31 std::shared_ptr<Group> Lookup::getGroupFromMacs(const std::vector<MacAddress>& addresses, int32_t timeout_ms) {
32  std::shared_ptr<Group> ptr;
33  std::vector<const HebiMacAddress*> addresses_c;
34  addresses_c.reserve(addresses.size());
35  std::transform(std::begin(addresses), std::end(addresses), std::back_inserter(addresses_c),
36  [](const MacAddress& addr) { return &addr.internal_; });
37  HebiGroupPtr group = hebiGroupCreateFromMacs(lookup_, addresses_c.data(), addresses.size(), timeout_ms);
38  if (group != nullptr)
39  return std::make_shared<Group>(group, initial_group_feedback_frequency_, initial_group_command_lifetime_);
40  return ptr;
41 }
42 
43 std::shared_ptr<Group> Lookup::getGroupFromFamily(const std::string& family, int32_t timeout_ms) {
44  std::shared_ptr<Group> ptr;
45  HebiGroupPtr group = hebiGroupCreateFromFamily(lookup_, family.c_str(), timeout_ms);
46  if (group != nullptr)
47  return std::make_shared<Group>(group, initial_group_feedback_frequency_, initial_group_command_lifetime_);
48  return ptr;
49 }
50 
51 std::shared_ptr<Group> Lookup::getConnectedGroupFromName(const std::string& family_name, const std::string& name,
52  int32_t timeout_ms) {
53  std::shared_ptr<Group> ptr;
54  HebiGroupPtr group = hebiGroupCreateConnectedFromName(lookup_, family_name.c_str(), name.c_str(), timeout_ms);
55  if (group != nullptr)
56  return std::make_shared<Group>(group, initial_group_feedback_frequency_, initial_group_command_lifetime_);
57  return ptr;
58 }
59 
60 std::shared_ptr<Group> Lookup::getConnectedGroupFromMac(const MacAddress& address, int32_t timeout_ms) {
61  std::shared_ptr<Group> ptr;
62  HebiGroupPtr group = hebiGroupCreateConnectedFromMac(lookup_, &(address.internal_), timeout_ms);
63  if (group != nullptr)
64  return std::make_shared<Group>(group, initial_group_feedback_frequency_, initial_group_command_lifetime_);
65  return ptr;
66 }
67 
69 
71 
73 
75 
77 
78 bool Lookup::setLookupFrequencyHz(double frequency) {
80 }
81 
82 Lookup::EntryList::Iterator::Iterator(const EntryList& list, size_t current) : list_(list), current_(current) {}
83 
85 
87  ++current_;
88  return *this;
89 }
90 
92  Lookup::EntryList::Iterator tmp = *this;
93  ++current_;
94  return tmp;
95 }
96 
98  --current_;
99  return *this;
100 }
101 
103  Lookup::EntryList::Iterator tmp = *this;
104  --current_;
105  return tmp;
106 }
107 
109  return this->current_ == rhs.current_;
110 }
111 
112 bool Lookup::EntryList::Iterator::operator!=(const Lookup::EntryList::Iterator& rhs) const { return !(*this == rhs); }
113 
116  lookup_list_ = nullptr;
117 }
118 
120  size_t required_size;
121  hebiLookupEntryListGetName(lookup_list_, index, nullptr, &required_size);
122  auto buffer = new char[required_size];
123  hebiLookupEntryListGetName(lookup_list_, index, buffer, &required_size);
124  std::string name(buffer, required_size - 1);
125  delete[] buffer;
126 
127  hebiLookupEntryListGetFamily(lookup_list_, index, nullptr, &required_size);
128  buffer = new char[required_size];
129  hebiLookupEntryListGetFamily(lookup_list_, index, buffer, &required_size);
130  std::string family(buffer, required_size - 1);
131  delete[] buffer;
132 
133  HebiMacAddress mac_int;
135  MacAddress mac;
136  mac.internal_ = mac_int;
137 
138  Entry e = {name, family, mac};
139  return e;
140 }
141 
143 
145 
147 
148 std::shared_ptr<Lookup::EntryList> Lookup::getEntryList() {
149  std::shared_ptr<Lookup::EntryList> ptr;
150  auto entry_list = hebiCreateLookupEntryList(lookup_);
151  if (entry_list != nullptr)
152  return std::make_shared<Lookup::EntryList>(entry_list);
153  return ptr;
154 }
155 
156 } // namespace hebi
hebi::Lookup::Lookup
Lookup()
Creates a Lookup object which can create Module and Group references. Typically, only one Lookup obje...
Definition: lookup.cpp:7
hebi::Lookup::EntryList::lookup_list_
HebiLookupEntryListPtr lookup_list_
Definition: lookup.hpp:221
hebi::Lookup::lookup_
HebiLookupPtr lookup_
Definition: lookup.hpp:31
hebiLookupSetLookupFrequencyHz
HebiStatusCode hebiLookupSetLookupFrequencyHz(HebiLookupPtr lookup, double frequency)
sets the lookup request rate [Hz]
hebi::Lookup::getLookupFrequencyHz
double getLookupFrequencyHz() const
Gets the rate [Hz] at which "discovery" packets are broadcast.
Definition: lookup.cpp:76
HebiStatusSuccess
@ HebiStatusSuccess
Definition: hebi.h:24
hebi::Lookup::EntryList::Iterator::operator!=
bool operator!=(const Iterator &rhs) const
Definition: lookup.cpp:112
hebiGroupCreateConnectedFromName
HebiGroupPtr hebiGroupCreateConnectedFromName(HebiLookupPtr lookup, const char *family, const char *name, int32_t timeout_ms)
Create a group with all modules connected to module with the given name and family.
hebiCreateLookupEntryList
HebiLookupEntryListPtr hebiCreateLookupEntryList(HebiLookupPtr lookup)
Return a snapshot of the contents of the module registry – i.e., which modules have been found by the...
lookup.hpp
hebi::Lookup::EntryList::Iterator::operator*
reference operator*() const
Definition: lookup.cpp:84
hebi::Lookup::getGroupFromNames
std::shared_ptr< Group > getGroupFromNames(const std::vector< std::string > &families, const std::vector< std::string > &names, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from modules with the given names and families.
Definition: lookup.cpp:11
HebiGroupPtr
struct HebiGroup_ * HebiGroupPtr
The C-style's API representation of a group.
Definition: hebi.h:493
hebiGroupCreateFromFamily
HebiGroupPtr hebiGroupCreateFromFamily(HebiLookupPtr lookup, const char *family, int32_t timeout_ms)
Create a group with all modules known to the lookup with the given family.
hebi::Lookup::EntryList
Definition: lookup.hpp:210
hebi::Lookup::initial_group_command_lifetime_
int32_t initial_group_command_lifetime_
Definition: lookup.hpp:41
hebi::Lookup::EntryList::Iterator::operator==
bool operator==(const Iterator &rhs) const
Definition: lookup.cpp:108
hebiGroupCreateConnectedFromMac
HebiGroupPtr hebiGroupCreateConnectedFromMac(HebiLookupPtr lookup, const HebiMacAddress *address, int32_t timeout_ms)
Create a group with all modules connected to module with the given MAC address.
hebi::Lookup::getInitialGroupFeedbackFrequencyHz
float getInitialGroupFeedbackFrequencyHz()
Gets the default feedback frequency value for groups created from this lookup.
Definition: lookup.cpp:68
hebi::Lookup::EntryList::begin
Iterator begin() const
Definition: lookup.cpp:144
hebi::Lookup::EntryList::Iterator
Definition: lookup.hpp:227
hebi::Lookup::EntryList::~EntryList
~EntryList() noexcept
Definition: lookup.cpp:114
hebiLookupEntryListGetFamily
HebiStatusCode hebiLookupEntryListGetFamily(HebiLookupEntryListPtr lookup_list, size_t index, char *buffer, size_t *length)
hebi::Lookup::EntryList::size
size_t size() const
Definition: lookup.cpp:142
hebi::MacAddress
A simple wrapper class for internal C-API HebiMacAddress objects to allow interfacing with API calls ...
Definition: mac_address.hpp:13
hebi::Lookup::getGroupFromMacs
std::shared_ptr< Group > getGroupFromMacs(const std::vector< MacAddress > &addresses, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from modules with the given mac addresses.
Definition: lookup.cpp:31
hebiLookupRelease
void hebiLookupRelease(HebiLookupPtr lookup)
Frees resources created by the lookup object.
hebi::Lookup::setInitialGroupCommandLifetimeMs
void setInitialGroupCommandLifetimeMs(int32_t ms)
Sets the default command lifetime value for groups created from this lookup.
Definition: lookup.cpp:74
hebi::Lookup::EntryList::Iterator::operator--
Iterator & operator--()
Definition: lookup.cpp:97
hebiLookupEntryListGetMacAddress
HebiStatusCode hebiLookupEntryListGetMacAddress(HebiLookupEntryListPtr lookup_list, size_t index, HebiMacAddress *mac_address)
hebi::Lookup::EntryList::Iterator::current_
size_t current_
Definition: lookup.hpp:255
hebi::Lookup::getGroupFromFamily
std::shared_ptr< Group > getGroupFromFamily(const std::string &family, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from all known modules with the given family.
Definition: lookup.cpp:43
hebi
Definition: arm.cpp:5
hebi::Lookup::EntryList::Entry
Definition: lookup.hpp:211
hebi::Lookup::EntryList::end
Iterator end() const
Definition: lookup.cpp:146
hebi::Lookup::EntryList::Iterator::operator++
Iterator & operator++()
Definition: lookup.cpp:86
hebi::Lookup::EntryList::operator[]
Entry operator[](size_t index) const
Definition: lookup.cpp:119
hebi::Lookup::getConnectedGroupFromMac
std::shared_ptr< Group > getConnectedGroupFromMac(const MacAddress &address, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from all modules known to connect to a module with the given mac address.
Definition: lookup.cpp:60
hebiLookupEntryListGetSize
size_t hebiLookupEntryListGetSize(HebiLookupEntryListPtr lookup_list)
hebiLookupEntryListGetName
HebiStatusCode hebiLookupEntryListGetName(HebiLookupEntryListPtr lookup_list, size_t index, char *buffer, size_t *length)
hebiLookupEntryListRelease
void hebiLookupEntryListRelease(HebiLookupEntryListPtr lookup_list)
Release resources for a given lookup entry list; list should not be used after this call.
hebi::Lookup::getConnectedGroupFromName
std::shared_ptr< Group > getConnectedGroupFromName(const std::string &family, const std::string &name, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from all modules known to connect to a module with the given name and family.
Definition: lookup.cpp:51
hebi::Lookup::setLookupFrequencyHz
bool setLookupFrequencyHz(double frequency)
Sets the lookup rate [Hz].
Definition: lookup.cpp:78
hebi::MacAddress::internal_
HebiMacAddress internal_
Definition: mac_address.hpp:21
hebi::Lookup::getEntryList
std::shared_ptr< EntryList > getEntryList()
Definition: lookup.cpp:148
hebi::Lookup::~Lookup
~Lookup() noexcept
Destructor frees all resources created by Lookup object, and stops the background query thread.
Definition: lookup.cpp:9
hebiGroupCreateFromNames
HebiGroupPtr hebiGroupCreateFromNames(HebiLookupPtr lookup, const char *const *families, size_t num_families, const char *const *names, size_t num_names, int32_t timeout_ms)
Create a group with modules matching the given names and families.
hebiLookupCreate
HebiLookupPtr hebiLookupCreate(const char *const *ifaces, size_t ifaces_length)
Lookup API.
HebiMacAddress_
Structures.
Definition: hebi.h:560
hebi::Lookup::getInitialGroupCommandLifetimeMs
int32_t getInitialGroupCommandLifetimeMs()
Gets the default command lifetime value for groups created from this lookup.
Definition: lookup.cpp:72
hebi::Lookup::setInitialGroupFeedbackFrequencyHz
void setInitialGroupFeedbackFrequencyHz(float frequency)
Sets the default feedback frequency value for groups created from this lookup.
Definition: lookup.cpp:70
hebi::Lookup::initial_group_feedback_frequency_
float initial_group_feedback_frequency_
Definition: lookup.hpp:36
hebiGroupCreateFromMacs
HebiGroupPtr hebiGroupCreateFromMacs(HebiLookupPtr lookup, const HebiMacAddress *const *addresses, size_t num_addresses, int32_t timeout_ms)
Create a group of modules with the given MAC addresses.
hebi::Lookup::EntryList::Iterator::Iterator
Iterator()=default
hebiLookupGetLookupFrequencyHz
double hebiLookupGetLookupFrequencyHz(HebiLookupPtr lookup)
gets the lookup request rate [Hz]


hebi_cpp_api_ros
Author(s): Chris Bollinger , Matthew Tesch
autogenerated on Fri Aug 2 2024 08:35:18