/* * CAN bus driver interface. * Copyright (C) 2014 Pavel Kirienko */ #include #include #include namespace uavcan { const uint32_t CanFrame::MaskStdID; const uint32_t CanFrame::MaskExtID; const uint32_t CanFrame::FlagEFF; const uint32_t CanFrame::FlagRTR; std::string CanFrame::toString(StringRepresentation mode) const { using std::snprintf; assert(mode == StrTight || mode == StrAligned); static const int ASCII_COLUMN_OFFSET = 36; char buf[50]; char* wpos = buf, *epos = buf + sizeof(buf); std::fill(buf, buf + sizeof(buf), 0); if (id & FlagEFF) { wpos += snprintf(wpos, epos - wpos, "0x%08x ", (unsigned int)(id & MaskExtID)); } else { const char* const fmt = (mode == StrAligned) ? " 0x%03x " : "0x%03x "; wpos += snprintf(wpos, epos - wpos, fmt, (unsigned int)(id & MaskStdID)); } if (id & FlagRTR) { wpos += snprintf(wpos, epos - wpos, " RTR"); } else { for (int dlen = 0; dlen < dlc; dlen++) // hex bytes wpos += snprintf(wpos, epos - wpos, " %02x", (unsigned int)data[dlen]); while (mode == StrAligned && wpos < buf + ASCII_COLUMN_OFFSET) // alignment *wpos++ = ' '; wpos += snprintf(wpos, epos - wpos, " \'"); // ascii for (int dlen = 0; dlen < dlc; dlen++) { uint8_t ch = data[dlen]; if (ch < 0x20 || ch > 0x7E) ch = '.'; wpos += snprintf(wpos, epos - wpos, "%c", ch); } wpos += snprintf(wpos, epos - wpos, "\'"); } return std::string(buf); } }