/* * Copyright (c) 2023 Fancy Code. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PROTOCOL_HANDLE_H #define PROTOCOL_HANDLE_H #include "StatusCode.h" #include #include #include constexpr char ORDER_BIG_ENDIAN = 0x01; constexpr char ORDER_LITTLE_ENDIAN = 0x02; #pragma pack(1) typedef struct protocol_packet { short mHead; unsigned int mSerialNumber; short mCommand; short mLength; char *mData; short mCheckCode; } ProtocolPacket; #pragma pack() enum PROTOCOL_COMMAND { ASK_IPC_MISSION = 0x8101, REPLY_IPC_MISSION = 0x0101, ASK_CUT_OFF_PWOER_SUPPLY = 0x8102, PROTOCOL_COMMAND_END }; class VProtocolParam { public: VProtocolParam(const PROTOCOL_COMMAND &command) : mCommand(command) {} virtual ~VProtocolParam() = default; const PROTOCOL_COMMAND mCommand; }; template class ProtocolParam : public VProtocolParam { public: ProtocolParam(const PROTOCOL_COMMAND &command, T &value) : VProtocolParam(command), mData(value) {} virtual ~ProtocolParam() = default; public: /** * @brief * Protocol data. */ T mData; }; using MakePacketFunc = std::function &)>; using AnalyzePacketFunc = std::function; class ProtocolHandle { public: ProtocolHandle(const std::shared_ptr ¶m); ProtocolHandle(const void *data, const size_t &length); virtual ~ProtocolHandle(); void *GetProtocolDataBuff(void) { return mProtocolData; } size_t GetProtocolDataLength(void) { return mProtocolDataLength; } unsigned int GetSerialNumber(void) { return mProtocolSerialNumber; } /** * @brief These functions assemble scattered data into continuous memory protocol packets and complete the sending * of protocol packets. */ private: void MakeProtocolPacket(const std::shared_ptr ¶m); void MakeNoUserDataPacket(const std::shared_ptr ¶m); void MakeAskIpcMissionPacket(const std::shared_ptr ¶m); void MakeAskCutOffPowerSupplyPacket(const std::shared_ptr ¶m); /** * @brief These function implementations parse the received frame by frame continuous data into the data required by * the application, completing the protocol unpacking. */ private: void AnalyzeProtocolPacket(void); void AnalyzeReplyIpcMissionPacket(const ProtocolPacket &packet); private: virtual void BigEndianConversion(ProtocolPacket &packet) {} virtual void HostByteOrderConversion(ProtocolPacket &packet) {} virtual bool CheckoutTheCheckCode(const ProtocolPacket &packet); protected: std::shared_ptr mParam; std::map mMakePacketFunc; std::map mAnalyzePacketFunc; unsigned char *mProtocolData; size_t mProtocolDataLength; /** * @brief Single protocol package serial number. * */ unsigned int mProtocolSerialNumber; /** * @brief The global protocol packet serial number is used to bind the data content of the one question one answer * protocol. * */ static unsigned int mSerialNumber; public: static std::shared_ptr CreateProtocolData(const std::shared_ptr ¶m); static void ProtocolAnalysis(const void *data, const size_t &length); static size_t GetKeyHeadLength(void); static StatusCode GetDataLength(const void *keyHead, const size_t &headLength, size_t &dataLength); static char GetByteOrder(void); static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log = nullptr); }; #endif