Add protobuf COM wrapper

This commit is contained in:
2026-04-11 10:27:05 +08:00
parent 5d5e6277ab
commit e610f2dc1e
3 changed files with 184 additions and 0 deletions

View File

@@ -0,0 +1,142 @@
#include "COM/Com_PbCodec.h"
#include "COM/Com_CdcEncode.h"
namespace
{
quint32 Com_Pb_Func_CalcFrameChecksum(const keyboard::cdc::CdcFrame& Frame)
{
QByteArray ByteArray;
ByteArray.append(static_cast<char>(Frame.head1()));
ByteArray.append(static_cast<char>(Frame.head2()));
ByteArray.append(static_cast<char>(Frame.payload_length()));
ByteArray.append(static_cast<char>(Frame.type()));
ByteArray.append(QByteArray::fromStdString(Frame.payload()));
return Com_Cdc_Func_CalcChecksum(ByteArray);
}
keyboard::cdc::CdcFrame Com_Pb_Func_BuildFrame(
keyboard::cdc::CdcPacketType Type,
const keyboard::cdc::CdcPacketBody& Body)
{
keyboard::cdc::CdcFrame Frame;
std::string PayloadBytes;
Body.SerializeToString(&PayloadBytes);
Frame.set_head1(0xAA);
Frame.set_head2(0x55);
Frame.set_payload_length(static_cast<quint32>(PayloadBytes.size()));
Frame.set_type(Type);
Frame.set_payload(PayloadBytes);
Frame.set_checksum(Com_Pb_Func_CalcFrameChecksum(Frame));
return Frame;
}
bool Com_Pb_Func_IsValidFrame(const keyboard::cdc::CdcFrame& Frame)
{
return (Frame.head1() == 0xAA) &&
(Frame.head2() == 0x55) &&
(Frame.payload_length() == static_cast<quint32>(Frame.payload().size())) &&
(Frame.checksum() == Com_Pb_Func_CalcFrameChecksum(Frame));
}
} // namespace
bool Com_Pb_Func_SerializeFrame(
const keyboard::cdc::CdcFrame& Frame,
QByteArray* p_ByteArray)
{
if (p_ByteArray == nullptr)
{
return false;
}
std::string Bytes;
if (!Frame.SerializeToString(&Bytes))
{
p_ByteArray->clear();
return false;
}
*p_ByteArray = QByteArray(Bytes.data(), static_cast<int>(Bytes.size()));
return true;
}
bool Com_Pb_Func_ParseFrame(
const QByteArray& ByteArray,
keyboard::cdc::CdcFrame* p_Frame)
{
if (p_Frame == nullptr)
{
return false;
}
keyboard::cdc::CdcFrame Frame;
if (!Frame.ParseFromArray(ByteArray.constData(), ByteArray.size()))
{
return false;
}
if (!Com_Pb_Func_IsValidFrame(Frame))
{
return false;
}
*p_Frame = Frame;
return true;
}
QByteArray Com_Pb_Func_BuildHelloReqFrame()
{
keyboard::cdc::CdcPacketBody Body;
Body.mutable_hello_req()->set_protocol_version(1);
const keyboard::cdc::CdcFrame Frame =
Com_Pb_Func_BuildFrame(keyboard::cdc::CDC_PACKET_TYPE_HELLO_REQ, Body);
QByteArray ByteArray;
if (!Com_Pb_Func_SerializeFrame(Frame, &ByteArray))
{
return QByteArray();
}
return ByteArray;
}
bool Com_Pb_Func_ParseHelloRspFrame(
const QByteArray& ByteArray,
keyboard::cdc::HelloRsp* p_HelloRsp)
{
if (p_HelloRsp == nullptr)
{
return false;
}
keyboard::cdc::CdcFrame Frame;
if (!Com_Pb_Func_ParseFrame(ByteArray, &Frame))
{
return false;
}
if (Frame.type() != keyboard::cdc::CDC_PACKET_TYPE_HELLO_RSP)
{
return false;
}
keyboard::cdc::CdcPacketBody Body;
if (!Body.ParseFromString(Frame.payload()))
{
return false;
}
if (!Body.has_hello_rsp())
{
return false;
}
*p_HelloRsp = Body.hello_rsp();
return true;
}

View File

@@ -0,0 +1,17 @@
#pragma once
#include <QtCore/QByteArray>
#include "../generated/cpp/keyboard.pb.h"
bool Com_Pb_Func_SerializeFrame(
const keyboard::cdc::CdcFrame& Frame,
QByteArray* p_ByteArray);
bool Com_Pb_Func_ParseFrame(
const QByteArray& ByteArray,
keyboard::cdc::CdcFrame* p_Frame);
QByteArray Com_Pb_Func_BuildHelloReqFrame();
bool Com_Pb_Func_ParseHelloRspFrame(
const QByteArray& ByteArray,
keyboard::cdc::HelloRsp* p_HelloRsp);

View File

@@ -82,6 +82,31 @@ Implemented behavior:
- `Ack` - `Ack`
- `Error` - `Error`
### Node 3: protobuf thin wrapper
Files added in this step:
- `KeyBorad/KeyBorad/COM/Com_PbCodec.h`
- `KeyBorad/KeyBorad/COM/Com_PbCodec.cpp`
Design notes:
- stop expanding hand-written typed payload logic
- let protobuf generated code own message encode/decode
- keep only a thin Qt-friendly wrapper
Implemented behavior:
- serialize a full `CdcFrame`
- parse and validate a full `CdcFrame`
- build `HelloReq` using generated protobuf types
- parse `HelloRsp` using generated protobuf types
Current limit:
- not wired into the project yet
- protobuf runtime include and link setup is still pending
## COM done criteria ## COM done criteria
For the current project stage, COM is considered done when: For the current project stage, COM is considered done when: