YARP
Yet Another Robot Platform
RosHeader.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "RosHeader.h"
7 #include "TcpRosLogComponent.h"
8 
9 #include <cstdio>
10 #include <cstring>
11 #include <yarp/os/Bytes.h>
12 #include <yarp/os/NetType.h>
13 
14 using namespace yarp::os;
15 
16 void RosHeader::appendInt32(char *&buf,int x) {
17  Bytes bytes(buf,4);
18  NetType::netInt(x,bytes);
19  buf += 4;
20 }
21 
22 void RosHeader::appendString(char *&buf,const std::string& str) {
23  memcpy(buf,str.c_str(),str.length());
24  buf += str.length();
25 }
26 
27 
28 std::string RosHeader::showMessage(std::string s) {
29  std::string result;
30  for (char ch : s) {
31  char buf[256];
32  sprintf(buf, "%c (%#x) ", (ch>=' ')?ch:'.', *reinterpret_cast<unsigned char*>(&ch));
33  result += buf;
34  }
35  return result;
36 }
37 
38 std::string RosHeader::writeHeader() {
39  size_t len = 0;
40  for (auto& it : data) {
41  std::string key = it.first;
42  std::string val = it.second;
43  len += 4 + key.length() + 1 + val.length();
44  }
45  std::string result(len,'\0');
46  char *buf = (char *)result.c_str();
47  for (auto& it : data) {
48  std::string key = it.first;
49  std::string val = it.second;
50  appendInt32(buf,key.length()+1+val.length());
51  appendString(buf,key);
52  appendString(buf,std::string("="));
53  appendString(buf,val);
54  }
55  return result;
56 }
57 
58 
59 bool RosHeader::readHeader(const std::string& bin) {
60  data.clear();
61 
62  unsigned int len = bin.length();
63  char *at = (char*) bin.c_str();
64 
65  while (len>0) {
66  Bytes bytes(at,4);
67  int slen = NetType::netInt(bytes);
68  at += 4;
69  len -= 4;
70  std::string keyval(at,slen);
71  size_t delim = keyval.find_first_of('=',0);
72  if (delim == std::string::npos) {
73  yCWarning(TCPROSCARRIER, "Corrupt ROS header");
74  }
75  std::string key = keyval.substr(0,delim);
76  std::string val = keyval.substr(delim+1);
77  yCTrace(TCPROSCARRIER, "key %s => val %s\n", key.c_str(), val.c_str());
78  data[key] = val;
79  at += slen;
80  len -= slen;
81  }
82  return true;
83 }
84 
85 
86 std::string RosHeader::toString() const {
87  std::string result;
88  for (const auto& it : data) {
89  std::string key = it.first;
90  std::string val = it.second;
91  result += key;
92  result += "->";
93  result += val;
94  result += " ";
95  }
96  return result;
97 }
const yarp::os::LogComponent & TCPROSCARRIER()
std::string writeHeader()
Definition: RosHeader.cpp:38
std::string toString() const
Definition: RosHeader.cpp:86
static void appendInt32(char *&buf, int x)
Definition: RosHeader.cpp:16
static void appendString(char *&buf, const std::string &str)
Definition: RosHeader.cpp:22
static std::string showMessage(std::string s)
Definition: RosHeader.cpp:28
bool readHeader(const std::string &bin)
Definition: RosHeader.cpp:59
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
static int netInt(const yarp::os::Bytes &code)
Definition: NetType.cpp:27
#define yCTrace(component,...)
Definition: LogComponent.h:85
#define yCWarning(component,...)
Definition: LogComponent.h:143
An interface to the operating system, including Port based communication.