usb_packet.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // // ******************************************************************
  2. // // /\ /| @File Usb_packet.h
  3. // // \ V/ @Brief
  4. // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
  5. // // / | @Creation 2024-05-17
  6. // // / \\ @Modified 2024-06-24
  7. // // *(__\_\
  8. // // ******************************************************************
  9. #pragma once
  10. //#include <typeinfo>
  11. #include <vector>
  12. //#include "../BaseHelper//common_helper.h"
  13. //#include "../BaseEnums/protocol_enums.h"
  14. #include "../edge_pulse.h"
  15. #include "protocol_Usb_enums.h"
  16. #include "SYNC.h"
  17. namespace Protocol
  18. {
  19. class UsbPacket
  20. {
  21. public:
  22. UsbPacket();
  23. UsbPacket(TwoLevelEdgePulse* pid_node, int32_t& left_over_size, const Sync& sync, bool polarity,
  24. int32_t max_data_length);
  25. UsbEnums::UsbPacketType Type() const { return packet_type_; }
  26. uint8_t PID() const { return pid_; }
  27. void SetPID(const uint8_t value) { pid_ = value; }
  28. bool LastNRBZDataBit() const { return last_nrbz_data_bit_; }
  29. uint8_t RealPID() const { return static_cast<uint8_t>(pid_ & 0xF); }
  30. //const std::type_info& PIDType() const { return pidType; }
  31. //int PIDEndIndex() const { return pidEndIndex; }
  32. bool Polarity() const { return polarity_; }
  33. const Sync PacketSYNC() const { return packet_sync_; }
  34. const std::vector<uint8_t>& Address() const { return address_; }
  35. uint8_t EndPoint() const { return end_point_; }
  36. const std::vector<uint8_t>& Datas() const { return datas_; }
  37. uint16_t CRC16() const { return crc16_; }
  38. // //CRC数据有效位
  39. uint8_t CRCSignNum() const { return crc_sign_num_; }
  40. bool IsValid() const { return is_valid_; }
  41. bool IsCRCChecked() const { return is_crc_checked_; }
  42. UsbEnums::EventInfoTitles PacketTitle;
  43. int32_t GetEndIndex() const { return to_extracted_next_data_start_index_; }
  44. int32_t GetStartIndex() const { return packet_sync_.node_ptr->start_index; }
  45. private:
  46. UsbEnums::UsbPacketType packet_type_ = UsbEnums::UsbPacketType::NO_DEFINE;
  47. uint8_t pid_;
  48. bool last_nrbz_data_bit_;
  49. bool polarity_;
  50. Sync packet_sync_;
  51. std::vector<uint8_t> address_;
  52. uint8_t end_point_;
  53. std::vector<uint8_t> datas_;
  54. //CRC数据有效位
  55. uint8_t crc_sign_num_;
  56. uint16_t crc16_;
  57. bool is_valid_;
  58. bool is_crc_checked_;
  59. //待提取下个时域位标
  60. int32_t to_extracted_next_data_start_index_;
  61. //待nrzi解码下个时域位标
  62. int32_t to_nrzi_decode_next_data_start_index_;
  63. //最大可能数据时域长度
  64. int32_t max_possible_data_length_;
  65. static bool CheckNibblesInverse(uint8_t value);
  66. bool GetPID(TwoLevelEdgePulse* & pid_node);
  67. //bool DecodeNextByte(const Protocol::TwoLevelEdgePulse* node, int32_t& leftOverSize, uint8_t& outData);
  68. bool GetNRZIData(TwoLevelEdgePulse* & node, int32_t need_bit_count, int32_t start_index,
  69. std::vector<bool>& out_data);
  70. bool DecodeNextByteByDataField(TwoLevelEdgePulse* & node, uint8_t& out_data);
  71. void NRBZToNormalData(const TwoLevelEdgePulse* node, int32_t need_bit_count, std::vector<bool> nrzi_data,
  72. uint8_t& out_data);
  73. bool DecodeNextByte(TwoLevelEdgePulse* & node, int32_t start_index, uint8_t& out_data);
  74. bool DecodeTokenBytes(TwoLevelEdgePulse* & node, UsbEnums::TokenPackageType type);
  75. void DecodeFields(TwoLevelEdgePulse* & node);
  76. void DecodeFieldsByToken(TwoLevelEdgePulse* & node);
  77. void DecodeFieldsByData(TwoLevelEdgePulse* & node);
  78. //void DecodeFieldsByHandShake(Protocol::TwoLevelEdgePulse* node, SYNC sync);
  79. void DecodeFieldsBySpecial(TwoLevelEdgePulse* & node) const;
  80. bool IsTokenPackageType() const;
  81. bool IsSpecialPacketType() const;
  82. bool IsHandshakePackageType(UsbEnums::HandshakePackageType& type) const;
  83. bool IsDataPackageType();
  84. };
  85. }