USBDecode.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // // ******************************************************************
  2. // // /\ /| @File USBDecode.cpp
  3. // // \ V/ @Brief
  4. // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
  5. // // / | @Creation 2024-1-4
  6. // // / \\ @Modified 2024-1-15
  7. // // *(__\_\
  8. // // ******************************************************************
  9. #include "USBDecode.h"
  10. namespace Protocol
  11. {
  12. double USBDecode::GetBitRateByPs() const
  13. {
  14. double result = 1.0;
  15. switch (signalRate)
  16. {
  17. case Enums::SignalRate::LowRate:
  18. result /= 1.5E6;
  19. break;
  20. case Enums::SignalRate::FullRate:
  21. result /= 12E6;
  22. break;
  23. case Enums::SignalRate::HighRate:
  24. result /= 480E6;
  25. break;
  26. default: break;
  27. }
  28. return result;
  29. }
  30. std::vector<std::string> USBDecode::EventInfoTitles() const
  31. {
  32. return {"Index", "Start Time", "Sync", "PID", "Data", "Addr", "FNUM", "CRC5", "CRC16", "EOP", "Error"};
  33. }
  34. Enums::SignalRate USBDecode::SignalRate() const
  35. {
  36. return signalRate;
  37. }
  38. void USBDecode::SetSignalRate(Enums::SignalRate value)
  39. {
  40. signalRate = value;
  41. }
  42. uint16_t USBDecode::ByteCount() const
  43. {
  44. return byteCount;
  45. }
  46. void USBDecode::SetByteCount(uint16_t value)
  47. {
  48. byteCount = value;
  49. }
  50. uint16_t USBDecode::MaxByteCount()
  51. {
  52. return 1023;
  53. }
  54. uint16_t USBDecode::MinByteCount()
  55. {
  56. return 0;
  57. }
  58. bool USBDecode::ParseData(std::vector<Protocol::TwoLevelEdgePulse> edgePluses,
  59. std::promise<void>& cancellationSignal)
  60. {
  61. if (edgePluses.empty()) return false;
  62. // 使用 std::future 来检测取消请求
  63. std::future<void> cancellationFuture = cancellationSignal.get_future();
  64. // 检查是否取消
  65. if (cancellationFuture.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready)
  66. {
  67. std::cout << "AsyncFunction canceled." << '\n';
  68. return false;
  69. }
  70. int32_t leftdataSize = static_cast<int32_t>(edgePluses.size());
  71. if (FindPackets(edgePluses.data(), leftdataSize, usbPackets, cancellationSignal))
  72. {
  73. if (usbPackets.empty())
  74. {
  75. return false;
  76. }
  77. //resultData.ClearData();
  78. //todo 事件总线和绘图对象 下沉后在这里构建事件总线和绘图序列 这里只做简单逻辑暂时,细节未实现
  79. for (auto&& element : usbPackets)
  80. {
  81. resultData.Add(new UsbDecodeResultCell(element));
  82. }
  83. }
  84. return true;
  85. }
  86. bool USBDecode::CheckDifferenceWithinThreshold(double* array, int32_t size, double threshold)
  87. {
  88. //threshold = 0.15;
  89. for (int i = 1; i < size; i++)
  90. {
  91. double difference = std::abs(array[i] - array[i - 1]);
  92. double percentageDifference = difference / array[i - 1];
  93. if (difference < 50)
  94. {
  95. threshold = 0.28;
  96. }
  97. if (percentageDifference > threshold)
  98. {
  99. return false; // 如果差异大于阈值,则返回 false
  100. }
  101. }
  102. return true; // 所有元素之间的差异都小于阈值
  103. }
  104. void USBDecode::ClearEdgeArray(Protocol::Edge* edges, int32_t size)
  105. {
  106. for (int i = 0; i < size; ++i)
  107. {
  108. edges[i] = Protocol::Edge::None;
  109. }
  110. }
  111. bool USBDecode::CheckSyncEdges(Protocol::Edge startEdge, Protocol::Edge* edges)
  112. {
  113. return edges[0] == edges[1] && edges[0] == startEdge && edges[0] == edges[6];
  114. }
  115. bool USBDecode::CheckSyncSpans(Protocol::TwoLevelEdgePulse* node, int32_t& leftOverSize
  116. , const int32_t count, int32_t& avgSpans)
  117. {
  118. if (count < 1)
  119. {
  120. return false;
  121. }
  122. auto startIndexs = new int32_t[count + 1];
  123. avgSpans = 0;
  124. if ((node - 1) != nullptr)
  125. {
  126. node--;
  127. }
  128. for (int i = count; i >= 0; i--)
  129. {
  130. if (node == nullptr)
  131. {
  132. return false;
  133. }
  134. startIndexs[i] = node->StartIndex;
  135. node--;
  136. }
  137. return CheckSyncSpansByArrary(startIndexs, count, avgSpans);
  138. }
  139. bool USBDecode::CheckSyncSpansByArrary(int32_t* startIndexs, const int32_t count,
  140. int32_t& avgSpans)
  141. {
  142. if (count < 1)
  143. {
  144. return false;
  145. }
  146. auto tmpSpans = new double[count - 1];
  147. for (int i = 1; i < count; i++)
  148. {
  149. tmpSpans[i - 1] = (double)(startIndexs[i] - startIndexs[i - 1]);
  150. }
  151. double totalSpans = 0;
  152. for (int i = 0; i < count - 1; i++)
  153. {
  154. totalSpans += std::abs(tmpSpans[0]);
  155. }
  156. avgSpans = (int)(totalSpans / (count - 1));
  157. return CheckDifferenceWithinThreshold(tmpSpans, count - 1);
  158. }
  159. bool USBDecode::FindAllSyncs(Protocol::TwoLevelEdgePulse* node, int32_t& leftOverSize,
  160. std::vector<SYNC>& allSYNC, std::promise<void>& cancellationSignal,
  161. bool polarity)
  162. {
  163. // 使用 std::future 来检测取消请求
  164. std::future<void> cancellationFuture = cancellationSignal.get_future();
  165. allSYNC.clear();
  166. int firstAvgSpan = 0;
  167. if (node == nullptr || node - 1 == nullptr)
  168. {
  169. return false;
  170. }
  171. const int syncBitLen = 8;
  172. Protocol::Edge nextEdge = polarity ? Protocol::Edge::Rise : Protocol::Edge::Falling;
  173. Protocol::Edge startEdge = nextEdge;
  174. Protocol::TwoLevelEdgePulse* startInfoNode = nullptr;
  175. Protocol::Edge tmpEdges[syncBitLen];
  176. ClearEdgeArray(tmpEdges, syncBitLen);
  177. int tmpEdgeCount = 0;
  178. Protocol::TwoLevelEdgePulse* tmpNode = node;
  179. while (tmpNode != nullptr)
  180. {
  181. if (cancellationFuture.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready)
  182. {
  183. std::cout << "AsyncFunction canceled." << '\n';
  184. return false;
  185. }
  186. if (tmpEdgeCount <= 7)
  187. {
  188. if (tmpEdgeCount == 0)
  189. {
  190. startInfoNode = tmpNode;
  191. }
  192. tmpEdges[tmpEdgeCount] = tmpNode->Edge;
  193. tmpEdgeCount++;
  194. }
  195. else
  196. {
  197. if (CheckSyncEdges(startEdge, tmpEdges))
  198. {
  199. int avgSpans;
  200. if (CheckSyncSpans(tmpNode, leftOverSize, syncBitLen - 1, avgSpans))
  201. {
  202. if (allSYNC.empty())
  203. {
  204. firstAvgSpan = avgSpans;
  205. allSYNC.emplace_back(startInfoNode->StartIndex, (tmpNode - 1)->StartIndex,
  206. avgSpans);
  207. }
  208. else if (startInfoNode != nullptr && (startInfoNode - 1) != nullptr && (startInfoNode - 1)->
  209. GetLength() > firstAvgSpan * 8)
  210. {
  211. allSYNC.emplace_back(startInfoNode->StartIndex, (tmpNode - 1)->StartIndex,
  212. avgSpans);
  213. }
  214. ClearEdgeArray(tmpEdges, syncBitLen);
  215. tmpEdgeCount = 0;
  216. tmpNode++;
  217. leftOverSize--;
  218. continue;
  219. }
  220. }
  221. for (int i = 0; i < syncBitLen - 1; i++)
  222. {
  223. tmpEdges[i] = tmpEdges[i + 1];
  224. }
  225. tmpEdges[syncBitLen - 1] = tmpNode->Edge;
  226. startInfoNode++;
  227. }
  228. tmpNode++;
  229. leftOverSize--;
  230. }
  231. return !allSYNC.empty();
  232. }
  233. bool USBDecode::FindPackets(Protocol::TwoLevelEdgePulse* node, int32_t& leftOverSize,
  234. std::vector<USBPacket>& allPacket, std::promise<void>& cancellationSignal,
  235. bool polarity)
  236. {
  237. // 使用 std::future 来检测取消请求
  238. const std::future<void> cancellationFuture = cancellationSignal.get_future();
  239. allPacket.clear();
  240. std::vector<SYNC> allSYNC;
  241. if (!FindAllSyncs(node, leftOverSize, allSYNC, cancellationSignal, polarity) || allSYNC.empty())
  242. {
  243. return false;
  244. }
  245. Protocol::TwoLevelEdgePulse* tmpNode = node;
  246. for (const auto& sync : allSYNC)
  247. {
  248. while (tmpNode != nullptr && tmpNode->StartIndex < sync.StartIndex)
  249. {
  250. if (cancellationFuture.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready)
  251. {
  252. std::cout << "AsyncFunction canceled." << '\n';
  253. return false;
  254. }
  255. tmpNode--;
  256. }
  257. if (tmpNode == nullptr || tmpNode - 1 == nullptr)
  258. {
  259. break;
  260. }
  261. if (sync.StartIndex != tmpNode->StartIndex)
  262. {
  263. continue;
  264. }
  265. while (sync.EndIndex > tmpNode->StartIndex && tmpNode != nullptr)
  266. {
  267. tmpNode++;
  268. if (cancellationFuture.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready)
  269. {
  270. std::cout << "AsyncFunction canceled." << '\n';
  271. return false;
  272. }
  273. }
  274. if (tmpNode != nullptr)
  275. {
  276. USBPacket packet(tmpNode, leftOverSize, sync, polarity);
  277. if (packet.IsValid())
  278. {
  279. allPacket.push_back(packet);
  280. }
  281. }
  282. }
  283. return true;
  284. }
  285. }