58 lines
2.1 KiB
C++
58 lines
2.1 KiB
C++
#include <cassert>
|
|
#include <cstring>
|
|
|
|
#include <QtEndian>
|
|
#include <QByteArray>
|
|
|
|
#include "message_bus/ingress/EdgeUplinkWire.h"
|
|
|
|
using softbus::message_bus::ingress::edge_uplink::FrameKind;
|
|
using softbus::message_bus::ingress::edge_uplink::encodeOuterFrame;
|
|
using softbus::message_bus::ingress::edge_uplink::tryPopOneOuterFrame;
|
|
|
|
int main()
|
|
{
|
|
const QByteArray inner = QByteArray("hello");
|
|
const QByteArray frame = encodeOuterFrame(FrameKind::Hello, inner);
|
|
assert(frame.size() == 16 + inner.size());
|
|
assert(frame.at(0) == 'S' && frame.at(1) == 'B' && frame.at(2) == 'U' && frame.at(3) == 'P');
|
|
|
|
QByteArray buf = frame;
|
|
const auto popped = tryPopOneOuterFrame(buf, 1024 * 1024);
|
|
assert(popped.has_value());
|
|
assert(popped->kind == FrameKind::Hello);
|
|
assert(popped->payload == inner);
|
|
assert(buf.isEmpty());
|
|
|
|
// DATA_RAWBUS round-trip meta
|
|
QByteArray raw;
|
|
raw.resize(48 + 5 + 3);
|
|
unsigned char* w = reinterpret_cast<unsigned char*>(raw.data());
|
|
qToBigEndian(static_cast<quint16>(5), w + 0); // trace len
|
|
qToBigEndian(static_cast<qint32>(7), reinterpret_cast<uchar*>(w + 2));
|
|
qToBigEndian(static_cast<quint32>(0xAABBCCDD), w + 6);
|
|
qToBigEndian(static_cast<quint16>(0), w + 10);
|
|
qToBigEndian(static_cast<quint16>(0), w + 12);
|
|
qToBigEndian(static_cast<quint32>(1), w + 14);
|
|
qToBigEndian(static_cast<quint64>(2), w + 18);
|
|
qToBigEndian(static_cast<quint64>(0), w + 26);
|
|
qToBigEndian(static_cast<qint64>(123456789LL), reinterpret_cast<uchar*>(w + 34));
|
|
w[42] = static_cast<unsigned char>(1); // direction
|
|
std::memset(w + 43, 0, 5);
|
|
std::memcpy(w + 48, "trace", 5);
|
|
std::memcpy(w + 53, "AB\x00", 3);
|
|
|
|
softbus::core::models::RawBusMessage msg;
|
|
QByteArray busOut;
|
|
QString err;
|
|
const bool ok = softbus::message_bus::ingress::edge_uplink::decodeDataRawBusPayload(raw, msg, busOut, &err);
|
|
assert(ok);
|
|
assert(msg.deviceId == 7);
|
|
assert(msg.endpointHash == 0xAABBCCDDu);
|
|
assert(msg.traceId == QStringLiteral("trace"));
|
|
assert(busOut.size() == 3);
|
|
assert(busOut.at(0) == 'A');
|
|
|
|
return 0;
|
|
}
|