Frame::toString() with tests

This commit is contained in:
Pavel Kirienko
2014-02-10 13:39:27 +04:00
parent 6c76e8a25d
commit 12111e63d4
4 changed files with 78 additions and 1 deletions
+36
View File
@@ -3,6 +3,8 @@
*/
#include <cassert>
#include <cstdio>
#include <sstream>
#include <uavcan/internal/transport/transfer.hpp>
namespace uavcan
@@ -74,4 +76,38 @@ CanFrame Frame::compile() const
return frame;
}
std::string Frame::toString() const
{
/*
* Frame ID fields, according to UAVCAN specs:
* - Data Type ID
* - Transfer Type
* - Source Node ID
* - Frame Index
* - Last Frame
* - Transfer ID
*/
static const int BUFLEN = 100;
char buf[BUFLEN];
int ofs = std::snprintf(buf, BUFLEN, "dtid=%i tt=%i snid=%i idx=%i last=%i tid=%i payload=[",
int(data_type_id), int(transfer_type), int(source_node_id), int(frame_index),
int(last_frame), int(transfer_id.get()));
for (int i = 0; i < payload_len; i++)
{
ofs += std::snprintf(buf + ofs, BUFLEN - ofs, "%02x", payload[i]);
if ((i + 1) < payload_len)
ofs += std::snprintf(buf + ofs, BUFLEN - ofs, " ");
}
ofs += std::snprintf(buf + ofs, BUFLEN - ofs, "]");
return std::string(buf);
}
std::string RxFrame::toString() const
{
std::ostringstream os; // C++03 doesn't support long long, so we need ostream to print the timestamp
os << Frame::toString() << " ts=" << timestamp << " iface=" << int(iface_index);
return os.str();
}
}