You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.6 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #pragma once
  2. #include <fstream>
  3. #include <functional>
  4. #include <iostream>
  5. #include <list>
  6. #include <map>
  7. #include <memory>
  8. #include <mutex>
  9. #include <set>
  10. #include <sstream>
  11. #include <string>
  12. #include <thread>
  13. #include <vector>
  14. //
  15. #include "app_protocols/zscanprotocol/zscanprotocol.hpp"
  16. #include "protocol/waveshare_can/waveshare_can.hpp"
  17. #include "zqui/zqui/zqui.hpp"
  18. namespace iflytop {
  19. using namespace std;
  20. namespace zscanprotocol {
  21. typedef function<void(packet_type_t type, uint8_t from, uint8_t to, uint8_t *hex, uint32_t hexlen)> on_raw_data_t;
  22. typedef function<string(int ecode)> ecode2str_t;
  23. class RxReceiptContext {
  24. public:
  25. bool waittingForReceipt;
  26. bool receiptIsReady;
  27. uint8_t waittingIndex;
  28. uint8_t receipt[1024];
  29. size_t receiptLen;
  30. };
  31. class Receipt {
  32. public:
  33. uint8_t receipt[1024];
  34. size_t receiptLen;
  35. zcanbus_packet_t *getPacket() { return (zcanbus_packet_t *)receipt; }
  36. int32_t getContent(int index) { return getPacket()->params[index]; }
  37. uint8_t *getByte() { return (uint8_t *)getPacket()->params; }
  38. };
  39. class CanPacketRxBuffer {
  40. public:
  41. int32_t id = 0;
  42. uint8_t from = 0;
  43. uint8_t to = 0;
  44. uint8_t rxdata[2048] = {0};
  45. int rxdataSize = 0;
  46. int rxPacketNum = 0;
  47. };
  48. class ZSCanProtocolCom {
  49. WaveshareCan *m_channel = nullptr;
  50. uint8_t m_rxcache[1024] = {0};
  51. int32_t m_rxlen = 0;
  52. bool m_rxcache_is_full = false;
  53. mutex lock_;
  54. unique_ptr<thread> m_thread;
  55. RxReceiptContext m_rxReceiptContext;
  56. mutex m_rxReceiptContext_lock;
  57. /*******************************************************************************
  58. * TX CONTEXT *
  59. *******************************************************************************/
  60. mutex m_tx_lock;
  61. uint8_t m_txbuf[1024] = {0};
  62. int32_t m_rxsize = 0;
  63. uint8_t m_txindex = 0;
  64. CanPacketRxBuffer m_rxbuf[255];
  65. on_raw_data_t on_rx_raw;
  66. uint8_t m_receipt_cache[1024];
  67. zcanbus_packet_t *m_receipt_frame = (zcanbus_packet_t *)m_receipt_cache;
  68. size_t m_receipt_len = 0;
  69. public:
  70. ZSCanProtocolCom() {}
  71. public:
  72. void initialize(QTSerialChannel *ch);
  73. void regOnRawData(on_raw_data_t cb) { on_rx_raw = cb; }
  74. void updateChannelConfig();
  75. shared_ptr<Receipt> callcmd(int32_t to, int32_t cmdid, uint8_t *param, int32_t paramLen, int32_t overtime = 100);
  76. shared_ptr<Receipt> callcmd0(int32_t to, int32_t cmdid, int32_t overtime = 100);
  77. shared_ptr<Receipt> callcmd1(int32_t to, int32_t cmdid, int32_t param0, int32_t overtime = 100);
  78. shared_ptr<Receipt> callcmd2(int32_t to, int32_t cmdid, int32_t param0, int32_t param1, int32_t overtime = 100);
  79. shared_ptr<Receipt> callcmd3(int32_t to, int32_t cmdid, int32_t param0, int32_t param1, int32_t param2, int32_t overtime = 100);
  80. shared_ptr<Receipt> callcmd4(int32_t to, int32_t cmdid, int32_t param0, int32_t param1, int32_t param2, int32_t param3, int32_t overtime = 100);
  81. shared_ptr<Receipt> callcmd5(int32_t to, int32_t cmdid, int32_t param0, int32_t param1, int32_t param2, int32_t param3, int32_t param4, int32_t overtime = 100);
  82. void sendraw(int32_t from, int32_t to, uint8_t *data, size_t len);
  83. private:
  84. void sendframe(int32_t from, int32_t to, uint8_t *frame, size_t len);
  85. void sendsubframe(int32_t from, int32_t to, int npacket, int packetIndex, uint8_t *packet, size_t len);
  86. void processRxPacket(int32_t from, int32_t to, uint8_t *data, size_t len);
  87. private:
  88. CanPacketRxBuffer *findRxBuff(int deviceId);
  89. string ecode2str(int ecode);
  90. };
  91. } // namespace zscanprotocol
  92. } // namespace iflytop