mac_address.cpp
Go to the documentation of this file.
1 #include "mac_address.hpp"
2 
3 namespace hebi {
4 
5 uint8_t MacAddress::hexToInt(char c) {
6  if (c >= '0' && c <= '9')
7  return c - '0';
8  if (c >= 'a' && c <= 'f')
9  return c - 'a' + 10;
10  if (c >= 'A' && c <= 'F')
11  return c - 'A' + 10;
12  return 0;
13 }
14 
15 uint8_t MacAddress::byteFromHexPair(char c1, char c2) { return hexToInt(c1) * 16 + hexToInt(c2); }
16 
18  internal_.bytes_[0] = 0;
19  internal_.bytes_[1] = 0;
20  internal_.bytes_[2] = 0;
21  internal_.bytes_[3] = 0;
22  internal_.bytes_[4] = 0;
23  internal_.bytes_[5] = 0;
24 }
25 
26 MacAddress MacAddress::fromBytes(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f) {
27  MacAddress addr;
28  addr.internal_.bytes_[0] = a;
29  addr.internal_.bytes_[1] = b;
30  addr.internal_.bytes_[2] = c;
31  addr.internal_.bytes_[3] = d;
32  addr.internal_.bytes_[4] = e;
33  addr.internal_.bytes_[5] = f;
34  return addr;
35 }
36 
37 bool MacAddress::setToHexString(std::string mac_str) {
38  if (!isHexStringValid(mac_str))
39  return false;
40  internal_.bytes_[0] = byteFromHexPair(mac_str[0], mac_str[1]);
41  internal_.bytes_[1] = byteFromHexPair(mac_str[3], mac_str[4]);
42  internal_.bytes_[2] = byteFromHexPair(mac_str[6], mac_str[7]);
43  internal_.bytes_[3] = byteFromHexPair(mac_str[9], mac_str[10]);
44  internal_.bytes_[4] = byteFromHexPair(mac_str[12], mac_str[13]);
45  internal_.bytes_[5] = byteFromHexPair(mac_str[15], mac_str[16]);
46  return true;
47 }
48 
49 uint8_t& MacAddress::operator[](std::size_t idx) { return internal_.bytes_[idx]; }
50 
51 const uint8_t& MacAddress::operator[](std::size_t idx) const { return internal_.bytes_[idx]; }
52 
54  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
55 }
56 
57 bool MacAddress::isHexStringValid(std::string mac_str) {
58  int len = mac_str.size();
59  if (len != 17) // 6 * 2 (digits) + 5 (":"s)
60  return false;
61  for (int j = 2; j < len; j += 3)
62  if (mac_str[j] != ':')
63  return false;
64  for (int j = 0; j < len; j += 3)
65  if (!isHexDigitValid(mac_str[j]) || !isHexDigitValid(mac_str[j + 1]))
66  return false;
67  return true;
68 }
69 
70 } // namespace hebi
static uint8_t byteFromHexPair(char c1, char c2)
Definition: mac_address.cpp:15
Definition: arm.cpp:5
uint8_t bytes_[6]
Definition: hebi.h:561
A simple wrapper class for internal C-API HebiMacAddress objects to allow interfacing with API calls ...
Definition: mac_address.hpp:13
bool setToHexString(std::string mac_str)
Sets the value of the current MacAddress to the value given in &#39;mac_str&#39;.
Definition: mac_address.cpp:37
HebiMacAddress internal_
Definition: mac_address.hpp:21
static bool isHexDigitValid(char c)
Definition: mac_address.cpp:53
static uint8_t hexToInt(char c)
Definition: mac_address.cpp:5
static MacAddress fromBytes(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f)
Creates a MacAddress from individual bytes.
Definition: mac_address.cpp:26
static bool isHexStringValid(std::string mac_str)
Is mac_str a valid string of format dd:dd:dd:dd:dd:dd, where &#39;d&#39; is a hex digit 0-F. Lowercase values accepted. 1 if yes, 0 if no.
Definition: mac_address.cpp:57
uint8_t & operator[](std::size_t idx)
Definition: mac_address.cpp:49
MacAddress()
Creates MAC address 00:00:00:00:00:00.
Definition: mac_address.cpp:17


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