diff --git a/KeyBorad/KeyBorad/LOGIC/Lgc_Core.cpp b/KeyBorad/KeyBorad/LOGIC/Lgc_Core.cpp index caadf38..72e4039 100644 --- a/KeyBorad/KeyBorad/LOGIC/Lgc_Core.cpp +++ b/KeyBorad/KeyBorad/LOGIC/Lgc_Core.cpp @@ -1,5 +1,67 @@ #include "LOGIC/Lgc_Core.h" +#include "COM/Com_ProtoCodec.h" + +namespace +{ + +void Lgc_Func_ApplyHelloRsp( + Lgc_Core* p_Core, + const Packet_HelloRsp& HelloRsp) +{ + p_Core->State.IsConnected = true; + p_Core->State.IsHandshakeDone = true; + p_Core->State.ProtocolVersion = HelloRsp.ProtocolVersion; + p_Core->State.VendorId = HelloRsp.VendorId; + p_Core->State.ProductId = HelloRsp.ProductId; + p_Core->State.FirmwareMajor = HelloRsp.FirmwareMajor; + p_Core->State.FirmwareMinor = HelloRsp.FirmwareMinor; + p_Core->State.CapabilityFlags = HelloRsp.CapabilityFlags; + p_Core->State.ActiveTransport = Lgc_TransportType::Cdc; + p_Core->State.ActiveEndpointName = p_Core->Session.CdcPort.PortName; + p_Core->State.ConnectionText = QStringLiteral("CDC connected: %1") + .arg(p_Core->Session.CdcPort.PortName); + p_Core->State.LastActionText = QStringLiteral("Handshake completed."); +} + +void Lgc_Func_UpdateActionText(Lgc_Core* p_Core, const Packet& PacketData) +{ + switch (PacketData.type) + { + case Com_Type_HelloRsp: + p_Core->State.LastActionText = QStringLiteral("Received HelloRsp."); + break; + case Com_Type_Bitmap: + p_Core->State.LastActionText = QStringLiteral("Received Bitmap."); + break; + case Com_Type_FunctionKeyEvent: + p_Core->State.LastActionText = QStringLiteral("Received FunctionKeyEvent."); + break; + case Com_Type_LedState: + p_Core->State.LastActionText = QStringLiteral("Received LedState."); + break; + case Com_Type_TimeSync: + p_Core->State.LastActionText = QStringLiteral("Received TimeSync."); + break; + case Com_Type_ThemeRgb: + p_Core->State.LastActionText = QStringLiteral("Received ThemeRgb."); + break; + case Com_Type_Ack: + p_Core->State.LastActionText = QStringLiteral("Received Ack."); + break; + case Com_Type_Error: + p_Core->State.LastActionText = QStringLiteral("Received Error."); + break; + default: + p_Core->State.LastActionText = QStringLiteral("Received packet type 0x%1.") + .arg(static_cast(PacketData.type), 2, 16, QLatin1Char('0')) + .toUpper(); + break; + } +} + +} // namespace + void Lgc_Core_Init(Lgc_Core* p_Core) { if (p_Core == nullptr) @@ -11,6 +73,45 @@ void Lgc_Core_Init(Lgc_Core* p_Core) Lgc_Session_Init(&p_Core->Session); } +bool Lgc_Core_Start(Lgc_Core* p_Core) +{ + if (p_Core == nullptr) + { + return false; + } + + Dri_Cdc_Struct_OpenConfig Config; + Packet_HelloRsp HelloRsp; + if (Lgc_Session_TryStartCdc(&p_Core->Session, Config, &HelloRsp)) + { + Lgc_Func_ApplyHelloRsp(p_Core, HelloRsp); + return true; + } + + p_Core->State.IsConnected = false; + p_Core->State.IsHandshakeDone = false; + p_Core->State.ConnectionText = QStringLiteral("No device found."); + p_Core->State.LastActionText = QStringLiteral("Handshake failed."); + return false; +} + +bool Lgc_Core_Poll(Lgc_Core* p_Core) +{ + if (p_Core == nullptr) + { + return false; + } + + Packet PacketData; + if (!Lgc_Session_ReadNextPacket(&p_Core->Session, &PacketData)) + { + return false; + } + + Lgc_Func_UpdateActionText(p_Core, PacketData); + return true; +} + void Lgc_Core_Close(Lgc_Core* p_Core) { if (p_Core == nullptr) @@ -21,3 +122,8 @@ void Lgc_Core_Close(Lgc_Core* p_Core) Lgc_Session_Close(&p_Core->Session); p_Core->State = Lgc_State(); } + +const Lgc_State& Lgc_Core_GetState(const Lgc_Core* p_Core) +{ + return p_Core->State; +} diff --git a/KeyBorad/KeyBorad/LOGIC/Lgc_Core.h b/KeyBorad/KeyBorad/LOGIC/Lgc_Core.h index 19aace6..126be6a 100644 --- a/KeyBorad/KeyBorad/LOGIC/Lgc_Core.h +++ b/KeyBorad/KeyBorad/LOGIC/Lgc_Core.h @@ -10,4 +10,7 @@ struct Lgc_Core }; void Lgc_Core_Init(Lgc_Core* p_Core); +bool Lgc_Core_Start(Lgc_Core* p_Core); +bool Lgc_Core_Poll(Lgc_Core* p_Core); void Lgc_Core_Close(Lgc_Core* p_Core); +const Lgc_State& Lgc_Core_GetState(const Lgc_Core* p_Core); diff --git a/docs/host_logic_rebuild.md b/docs/host_logic_rebuild.md index 50bd357..c0b9bd7 100644 --- a/docs/host_logic_rebuild.md +++ b/docs/host_logic_rebuild.md @@ -63,13 +63,15 @@ Implemented behavior: ### Node 4: core facade -Files planned after session: +Files completed in this step: - `KeyBorad/KeyBorad/LOGIC/Lgc_Core.h` - `KeyBorad/KeyBorad/LOGIC/Lgc_Core.cpp` -Target behavior: +Implemented behavior: - give APP one small entry point -- forward start/poll/close to session -- refresh state from handshake results +- start CDC handshake through session +- expose `Start / Poll / Close / GetState` +- refresh state from `HelloRsp` +- update a simple action text when packets arrive