string.cpp
Go to the documentation of this file.
2 #include <iomanip>
3 
4 namespace can {
5 
6 bool hex2dec(uint8_t& d, const char& h) {
7  if ('0' <= h && h <= '9')
8  d = h - '0';
9  else if ('a' <= h && h <= 'f')
10  d = h - 'a' + 10;
11  else if ('A' <= h && h <= 'F')
12  d = h - 'A' + 10;
13  else
14  return false;
15 
16  return true;
17 }
18 
19 bool hex2buffer(std::string& out, const std::string& in_raw, bool pad) {
20  std::string in(in_raw);
21  if ((in.size() % 2) != 0) {
22  if (pad)
23  in.insert(0, "0");
24  else
25  return false;
26  }
27  out.resize(in.size() >> 1);
28  for (size_t i = 0; i < out.size(); ++i) {
29  uint8_t hi, lo;
30  if (!hex2dec(hi, in[i << 1]) || !hex2dec(lo, in[(i << 1) + 1]))
31  return false;
32 
33  out[i] = (hi << 4) | lo;
34  }
35  return true;
36 }
37 
38 bool dec2hex(char& h, const uint8_t& d, bool lc) {
39  if (d < 10) {
40  h = '0' + d;
41  } else if (d < 16 && lc) {
42  h = 'a' + (d - 10);
43  } else if (d < 16 && !lc) {
44  h = 'A' + (d - 10);
45  } else {
46  h='?';
47  return false;
48  }
49  return true;
50 }
51 
52 std::string byte2hex(const uint8_t& d, bool pad, bool lc) {
53  uint8_t hi = d >> 4;
54  char c=0;
55  std::string s;
56  if (hi || pad) {
57  dec2hex(c, hi, lc);
58  s += c;
59  }
60  dec2hex(c, d & 0xf, lc);
61  s += c;
62  return s;
63 }
64 
65 std::string buffer2hex(const std::string& in, bool lc) {
66  std::string s;
67  s.reserve(in.size() * 2);
68  for (size_t i = 0; i < in.size(); ++i) {
69  std::string b = byte2hex(in[i], true, lc);
70  if (b.empty())
71  return b;
72 
73  s += b;
74  }
75  return s;
76 }
77 
78 std::string tostring(const Header& h, bool lc) {
79  std::string s;
80  s.reserve(8);
81  std::stringstream buf;
82  buf << std::hex;
83  if (lc)
84  buf << std::nouppercase;
85  else
86  buf << std::uppercase;
87 
88  if (h.is_extended)
89  buf << std::setfill('0') << std::setw(8);
90 
91  buf << (h.fullid() & ~Header::EXTENDED_MASK);
92  return buf.str();
93 }
94 
95 uint32_t tohex(const std::string& s) {
96  unsigned int h = 0;
97  std::stringstream stream;
98  stream << std::hex << s;
99  stream >> h;
100  return h;
101 }
102 
103 Header toheader(const std::string& s) {
104  unsigned int h = tohex(s);
105  unsigned int id = h & Header::ID_MASK;
106  return Header(id, h & Header::EXTENDED_MASK || (s.size() == 8 && id >= (1<<11)),
108 }
109 
110 std::string tostring(const Frame& f, bool lc) {
111  std::string s;
112  s.resize(f.dlc);
113  for (uint8_t i = 0; i < f.dlc; ++i) {
114  s[i] = f.data[i];
115  }
116  return tostring((const Header&) (f), lc) + '#' + buffer2hex(s, lc);
117 }
118 
119 Frame toframe(const std::string& s) {
120  size_t sep = s.find('#');
121  if (sep == std::string::npos)
122  return MsgHeader(0xfff);
123 
124  Header header = toheader(s.substr(0, sep));
125  Frame frame(header);
126  std::string buffer;
127  if (header.isValid() && hex2buffer(buffer, s.substr(sep + 1), false)) {
128  if (buffer.size() > 8)
129  return MsgHeader(0xfff);
130 
131  for (size_t i = 0; i < buffer.size(); ++i) {
132  frame.data[i] = buffer[i];
133  }
134  frame.dlc = buffer.size();
135  }
136  return frame;
137 }
138 
139 template<> FrameFilterSharedPtr tofilter(const std::string &s){
140  FrameFilter * filter = 0;
141  size_t delim = s.find_first_of(":~-_");
142 
143  uint32_t second = FrameMaskFilter::MASK_RELAXED;
144  bool invert = false;
145  char type = ':';
146 
147  if(delim != std::string::npos) {
148  type = s.at(delim);
149  second = tohex(s.substr(delim +1));
150  }
151  uint32_t first = toheader(s.substr(0, delim)).fullid();
152  switch (type) {
153  case '~':
154  invert = true;
155  case ':':
156  filter = new FrameMaskFilter(first, second, invert);
157  break;
158  case '_':
159  invert = true;
160  case '-':
161  filter = new FrameRangeFilter(first, second, invert);
162  break;
163  }
164  return FrameFilterSharedPtr(filter);
165 }
166 template<> FrameFilterSharedPtr tofilter(const uint32_t &id){
167  return FrameFilterSharedPtr(new FrameMaskFilter(id));
168 }
169 
171  return tofilter<std::string>(s);
172 }
173 
174 std::ostream& operator <<(std::ostream& stream, const Header& h) {
175  return stream << can::tostring(h, true);
176 }
177 
178 std::ostream& operator <<(std::ostream& stream, const Frame& f) {
179  return stream << can::tostring(f, true);
180 }
181 
182 }
can::Header::RTR_MASK
static const unsigned int RTR_MASK
Definition: interface.h:20
can::Header::EXTENDED_MASK
static const unsigned int EXTENDED_MASK
Definition: interface.h:21
can::Frame
Definition: interface.h:62
can::tostring
std::string tostring(const Header &h, bool lc)
Definition: string.cpp:78
can::Header::fullid
unsigned int fullid() const
Definition: interface.h:31
can::FrameMaskFilter::MASK_RELAXED
static const uint32_t MASK_RELAXED
Definition: filter.h:20
can::Header::is_extended
unsigned int is_extended
frame uses 29 bit CAN identifier
Definition: interface.h:26
can::tofilter
FrameFilterSharedPtr tofilter(const T &ct)
string.h
can::operator<<
std::ostream & operator<<(std::ostream &stream, const Header &h)
Definition: string.cpp:174
can::FrameMaskFilter
Definition: filter.h:17
can::Frame::dlc
unsigned char dlc
len of data
Definition: interface.h:65
can::FrameFilter
Definition: filter.h:10
can::dec2hex
bool dec2hex(char &h, const uint8_t &d, bool lc)
Definition: string.cpp:38
can::MsgHeader
Definition: interface.h:49
can::byte2hex
std::string byte2hex(const uint8_t &d, bool pad, bool lc)
Definition: string.cpp:52
can::buffer2hex
std::string buffer2hex(const std::string &in, bool lc)
Definition: string.cpp:65
can::Frame::data
std::array< value_type, 8 > data
array for 8 data bytes with bounds checking
Definition: interface.h:64
can::toheader
Header toheader(const std::string &s)
Definition: string.cpp:103
can
Definition: asio_base.h:11
can::FrameFilterSharedPtr
std::shared_ptr< FrameFilter > FrameFilterSharedPtr
Definition: filter.h:15
can::hex2dec
bool hex2dec(uint8_t &d, const char &h)
Definition: string.cpp:6
can::toframe
Frame toframe(const std::string &s)
Definition: string.cpp:119
can::Header
Definition: interface.h:17
can::Header::ERROR_MASK
static const unsigned int ERROR_MASK
Definition: interface.h:19
can::Header::isValid
bool isValid() const
Definition: interface.h:28
can::FrameRangeFilter
Definition: filter.h:34
can::tohex
uint32_t tohex(const std::string &s)
Definition: string.cpp:95
can::Header::ID_MASK
static const unsigned int ID_MASK
Definition: interface.h:18
can::hex2buffer
bool hex2buffer(std::string &out, const std::string &in_raw, bool pad)
Definition: string.cpp:19


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Wed Mar 2 2022 00:52:25