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 {
7  if (c >= '0' && c <= '9')
8  return c - '0';
9  if (c >= 'a' && c <= 'f')
10  return c - 'a' + 10;
11  if (c >= 'A' && c <= 'F')
12  return c - 'A' + 10;
13  return 0;
14 }
15 
16 uint8_t MacAddress::byteFromHexPair(char c1, char c2)
17 {
18  return hexToInt(c1) * 16 + hexToInt(c2);
19 }
20 
22 {
23  internal_.bytes_[0] = 0;
24  internal_.bytes_[1] = 0;
25  internal_.bytes_[2] = 0;
26  internal_.bytes_[3] = 0;
27  internal_.bytes_[4] = 0;
28  internal_.bytes_[5] = 0;
29 }
30 
31 MacAddress MacAddress::fromBytes(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f)
32 {
33  MacAddress addr;
34  addr.internal_.bytes_[0] = a;
35  addr.internal_.bytes_[1] = b;
36  addr.internal_.bytes_[2] = c;
37  addr.internal_.bytes_[3] = d;
38  addr.internal_.bytes_[4] = e;
39  addr.internal_.bytes_[5] = f;
40  return addr;
41 }
42 
43 bool MacAddress::setToHexString(std::string mac_str)
44 {
45  if (!isHexStringValid(mac_str))
46  return false;
47  internal_.bytes_[0] = byteFromHexPair(mac_str[0], mac_str[1]);
48  internal_.bytes_[1] = byteFromHexPair(mac_str[3], mac_str[4]);
49  internal_.bytes_[2] = byteFromHexPair(mac_str[6], mac_str[7]);
50  internal_.bytes_[3] = byteFromHexPair(mac_str[9], mac_str[10]);
51  internal_.bytes_[4] = byteFromHexPair(mac_str[12], mac_str[13]);
52  internal_.bytes_[5] = byteFromHexPair(mac_str[15], mac_str[16]);
53  return true;
54 }
55 
56 uint8_t& MacAddress::operator[](std::size_t idx)
57 {
58  return internal_.bytes_[idx];
59 }
60 
61 const uint8_t& MacAddress::operator[](std::size_t idx) const
62 {
63  return internal_.bytes_[idx];
64 }
65 
67 {
68  return ((c >= '0' && c <= '9') ||
69  (c >= 'a' && c <= 'f') ||
70  (c >= 'A' && c <= 'F'));
71 }
72 
73 
74 bool MacAddress::isHexStringValid(std::string mac_str)
75 {
76  int len = mac_str.size();
77  if (len != 17) // 6 * 2 (digits) + 5 (":"s)
78  return false;
79  for (int j = 2; j < len; j += 3)
80  if (mac_str[j] != ':')
81  return false;
82  for (int j = 0; j < len; j += 3)
83  if (!isHexDigitValid(mac_str[j]) || !isHexDigitValid(mac_str[j+1]))
84  return false;
85  return true;
86 }
87 
88 } // namespace hebi
uint8_t bytes_[6]
Definition: hebi.h:422
static uint8_t byteFromHexPair(char c1, char c2)
Definition: mac_address.cpp:16
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
Definition: color.hpp:5
A simple wrapper class for internal C-API HebiMacAddress objects to allow interfacing with API calls ...
Definition: mac_address.hpp:12
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:43
HebiMacAddress internal_
Definition: mac_address.hpp:21
static bool isHexDigitValid(char c)
Definition: mac_address.cpp:66
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:31
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:74
uint8_t & operator[](std::size_t idx)
Definition: mac_address.cpp:56
MacAddress()
Creates MAC address 00:00:00:00:00:00.
Definition: mac_address.cpp:21
EIGEN_DEVICE_FUNC const Scalar & b


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:21