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
float initial_group_feedback_frequency_
Definition: lookup.hpp:36
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.
Iterator begin() const
Definition: lookup.cpp:144
Lookup()
Creates a Lookup object which can create Module and Group references. Typically, only one Lookup obje...
Definition: lookup.cpp:7
Definition: arm.cpp:5
A simple wrapper class for internal C-API HebiMacAddress objects to allow interfacing with API calls ...
Definition: mac_address.hpp:13
HebiLookupPtr hebiLookupCreate(const char *const *ifaces, size_t ifaces_length)
Lookup API.
HebiStatusCode hebiLookupEntryListGetName(HebiLookupEntryListPtr lookup_list, size_t index, char *buffer, size_t *length)
size_t size() const
Definition: lookup.cpp:142
bool operator==(const Iterator &rhs) const
Definition: lookup.cpp:108
std::shared_ptr< EntryList > getEntryList()
Definition: lookup.cpp:148
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.
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
size_t hebiLookupEntryListGetSize(HebiLookupEntryListPtr lookup_list)
~EntryList() noexcept
Definition: lookup.cpp:114
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.
~Lookup() noexcept
Destructor frees all resources created by Lookup object, and stops the background query thread...
Definition: lookup.cpp:9
HebiLookupEntryListPtr hebiCreateLookupEntryList(HebiLookupPtr lookup)
Return a snapshot of the contents of the module registry – i.e., which modules have been found by th...
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
reference operator*() const
Definition: lookup.cpp:84
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
HebiStatusCode hebiLookupEntryListGetFamily(HebiLookupEntryListPtr lookup_list, size_t index, char *buffer, size_t *length)
int32_t getInitialGroupCommandLifetimeMs()
Gets the default command lifetime value for groups created from this lookup.
Definition: lookup.cpp:72
HebiLookupEntryListPtr lookup_list_
Definition: lookup.hpp:221
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.
HebiMacAddress internal_
Definition: mac_address.hpp:21
bool operator!=(const Iterator &rhs) const
Definition: lookup.cpp:112
void setInitialGroupFeedbackFrequencyHz(float frequency)
Sets the default feedback frequency value for groups created from this lookup.
Definition: lookup.cpp:70
HebiLookupPtr lookup_
Definition: lookup.hpp:31
double getLookupFrequencyHz() const
Gets the rate [Hz] at which "discovery" packets are broadcast.
Definition: lookup.cpp:76
bool setLookupFrequencyHz(double frequency)
Sets the lookup rate [Hz].
Definition: lookup.cpp:78
HebiStatusCode hebiLookupSetLookupFrequencyHz(HebiLookupPtr lookup, double frequency)
sets the lookup request rate [Hz]
void setInitialGroupCommandLifetimeMs(int32_t ms)
Sets the default command lifetime value for groups created from this lookup.
Definition: lookup.cpp:74
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
Structures.
Definition: hebi.h:560
Entry operator[](size_t index) const
Definition: lookup.cpp:119
HebiStatusCode hebiLookupEntryListGetMacAddress(HebiLookupEntryListPtr lookup_list, size_t index, HebiMacAddress *mac_address)
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.
double hebiLookupGetLookupFrequencyHz(HebiLookupPtr lookup)
gets the lookup request rate [Hz]
Iterator end() const
Definition: lookup.cpp:146
int32_t initial_group_command_lifetime_
Definition: lookup.hpp:41
float getInitialGroupFeedbackFrequencyHz()
Gets the default feedback frequency value for groups created from this lookup.
Definition: lookup.cpp:68
struct HebiGroup_ * HebiGroupPtr
The C-style&#39;s API representation of a group.
Definition: hebi.h:493
void hebiLookupEntryListRelease(HebiLookupEntryListPtr lookup_list)
Release resources for a given lookup entry list; list should not be used after this call...
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
void hebiLookupRelease(HebiLookupPtr lookup)
Frees resources created by the lookup object.


hebi_cpp_api_ros
Author(s): Chris Bollinger , Matthew Tesch
autogenerated on Thu May 28 2020 03:14:45