123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- // // ******************************************************************
- // // /\ /| @File USBDecode.cpp
- // // \ V/ @Brief
- // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
- // // / | @Creation 2024-1-4
- // // / \\ @Modified 2024-1-15
- // // *(__\_\
- // // ******************************************************************
- #include "USBDecode.h"
- namespace Protocol
- {
- double USBDecode::GetBitRateByPs() const
- {
- double result = 1.0;
- switch (signalRate)
- {
- case Enums::SignalRate::LowRate:
- result /= 1.5E6;
- break;
- case Enums::SignalRate::FullRate:
- result /= 12E6;
- break;
- case Enums::SignalRate::HighRate:
- result /= 480E6;
- break;
- default: break;
- }
- return result;
- }
- std::vector<std::string> USBDecode::EventInfoTitles() const
- {
- return {"Index", "Start Time", "Sync", "PID", "Data", "Addr", "FNUM", "CRC5", "CRC16", "EOP", "Error"};
- }
- Enums::SignalRate USBDecode::SignalRate() const
- {
- return signalRate;
- }
- void USBDecode::SetSignalRate(Enums::SignalRate value)
- {
- signalRate = value;
- }
- uint16_t USBDecode::ByteCount() const
- {
- return byteCount;
- }
- void USBDecode::SetByteCount(uint16_t value)
- {
- byteCount = value;
- }
- uint16_t USBDecode::MaxByteCount()
- {
- return 1023;
- }
- uint16_t USBDecode::MinByteCount()
- {
- return 0;
- }
- bool USBDecode::ParseData(std::vector<Protocol::TwoLevelEdgePulse> edgePluses,
- std::promise<void>& cancellationSignal)
- {
- if (edgePluses.empty()) return false;
- // 使用 std::future 来检测取消请求
- std::future<void> cancellationFuture = cancellationSignal.get_future();
- // 检查是否取消
- if (cancellationFuture.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready)
- {
- std::cout << "AsyncFunction canceled." << '\n';
- return false;
- }
- int32_t leftdataSize = static_cast<int32_t>(edgePluses.size());
- if (FindPackets(edgePluses.data(), leftdataSize, usbPackets, cancellationSignal))
- {
- if (usbPackets.empty())
- {
- return false;
- }
- //resultData.ClearData();
- //todo 事件总线和绘图对象 下沉后在这里构建事件总线和绘图序列 这里只做简单逻辑暂时,细节未实现
- for (auto&& element : usbPackets)
- {
- resultData.Add(new UsbDecodeResultCell(element));
- }
- }
- return true;
- }
- bool USBDecode::CheckDifferenceWithinThreshold(double* array, int32_t size, double threshold)
- {
- //threshold = 0.15;
- for (int i = 1; i < size; i++)
- {
- double difference = std::abs(array[i] - array[i - 1]);
- double percentageDifference = difference / array[i - 1];
- if (difference < 50)
- {
- threshold = 0.28;
- }
- if (percentageDifference > threshold)
- {
- return false; // 如果差异大于阈值,则返回 false
- }
- }
- return true; // 所有元素之间的差异都小于阈值
- }
- void USBDecode::ClearEdgeArray(Protocol::Edge* edges, int32_t size)
- {
- for (int i = 0; i < size; ++i)
- {
- edges[i] = Protocol::Edge::None;
- }
- }
- bool USBDecode::CheckSyncEdges(Protocol::Edge startEdge, Protocol::Edge* edges)
- {
- return edges[0] == edges[1] && edges[0] == startEdge && edges[0] == edges[6];
- }
- bool USBDecode::CheckSyncSpans(Protocol::TwoLevelEdgePulse* node, int32_t& leftOverSize
- , const int32_t count, int32_t& avgSpans)
- {
- if (count < 1)
- {
- return false;
- }
- auto startIndexs = new int32_t[count + 1];
- avgSpans = 0;
- if ((node - 1) != nullptr)
- {
- node--;
- }
- for (int i = count; i >= 0; i--)
- {
- if (node == nullptr)
- {
- return false;
- }
- startIndexs[i] = node->StartIndex;
- node--;
- }
- return CheckSyncSpansByArrary(startIndexs, count, avgSpans);
- }
- bool USBDecode::CheckSyncSpansByArrary(int32_t* startIndexs, const int32_t count,
- int32_t& avgSpans)
- {
- if (count < 1)
- {
- return false;
- }
- auto tmpSpans = new double[count - 1];
- for (int i = 1; i < count; i++)
- {
- tmpSpans[i - 1] = (double)(startIndexs[i] - startIndexs[i - 1]);
- }
- double totalSpans = 0;
- for (int i = 0; i < count - 1; i++)
- {
- totalSpans += std::abs(tmpSpans[0]);
- }
- avgSpans = (int)(totalSpans / (count - 1));
- return CheckDifferenceWithinThreshold(tmpSpans, count - 1);
- }
- bool USBDecode::FindAllSyncs(Protocol::TwoLevelEdgePulse* node, int32_t& leftOverSize,
- std::vector<SYNC>& allSYNC, std::promise<void>& cancellationSignal,
- bool polarity)
- {
- // 使用 std::future 来检测取消请求
- std::future<void> cancellationFuture = cancellationSignal.get_future();
- allSYNC.clear();
- int firstAvgSpan = 0;
- if (node == nullptr || node - 1 == nullptr)
- {
- return false;
- }
- const int syncBitLen = 8;
- Protocol::Edge nextEdge = polarity ? Protocol::Edge::Rise : Protocol::Edge::Falling;
- Protocol::Edge startEdge = nextEdge;
- Protocol::TwoLevelEdgePulse* startInfoNode = nullptr;
- Protocol::Edge tmpEdges[syncBitLen];
- ClearEdgeArray(tmpEdges, syncBitLen);
- int tmpEdgeCount = 0;
- Protocol::TwoLevelEdgePulse* tmpNode = node;
- while (tmpNode != nullptr)
- {
- if (cancellationFuture.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready)
- {
- std::cout << "AsyncFunction canceled." << '\n';
- return false;
- }
- if (tmpEdgeCount <= 7)
- {
- if (tmpEdgeCount == 0)
- {
- startInfoNode = tmpNode;
- }
- tmpEdges[tmpEdgeCount] = tmpNode->Edge;
- tmpEdgeCount++;
- }
- else
- {
- if (CheckSyncEdges(startEdge, tmpEdges))
- {
- int avgSpans;
- if (CheckSyncSpans(tmpNode, leftOverSize, syncBitLen - 1, avgSpans))
- {
- if (allSYNC.empty())
- {
- firstAvgSpan = avgSpans;
- allSYNC.emplace_back(startInfoNode->StartIndex, (tmpNode - 1)->StartIndex,
- avgSpans);
- }
- else if (startInfoNode != nullptr && (startInfoNode - 1) != nullptr && (startInfoNode - 1)->
- GetLength() > firstAvgSpan * 8)
- {
- allSYNC.emplace_back(startInfoNode->StartIndex, (tmpNode - 1)->StartIndex,
- avgSpans);
- }
- ClearEdgeArray(tmpEdges, syncBitLen);
- tmpEdgeCount = 0;
- tmpNode++;
- leftOverSize--;
- continue;
- }
- }
- for (int i = 0; i < syncBitLen - 1; i++)
- {
- tmpEdges[i] = tmpEdges[i + 1];
- }
- tmpEdges[syncBitLen - 1] = tmpNode->Edge;
- startInfoNode++;
- }
- tmpNode++;
- leftOverSize--;
- }
- return !allSYNC.empty();
- }
- bool USBDecode::FindPackets(Protocol::TwoLevelEdgePulse* node, int32_t& leftOverSize,
- std::vector<USBPacket>& allPacket, std::promise<void>& cancellationSignal,
- bool polarity)
- {
- // 使用 std::future 来检测取消请求
- const std::future<void> cancellationFuture = cancellationSignal.get_future();
- allPacket.clear();
- std::vector<SYNC> allSYNC;
- if (!FindAllSyncs(node, leftOverSize, allSYNC, cancellationSignal, polarity) || allSYNC.empty())
- {
- return false;
- }
- Protocol::TwoLevelEdgePulse* tmpNode = node;
- for (const auto& sync : allSYNC)
- {
- while (tmpNode != nullptr && tmpNode->StartIndex < sync.StartIndex)
- {
- if (cancellationFuture.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready)
- {
- std::cout << "AsyncFunction canceled." << '\n';
- return false;
- }
- tmpNode--;
- }
- if (tmpNode == nullptr || tmpNode - 1 == nullptr)
- {
- break;
- }
- if (sync.StartIndex != tmpNode->StartIndex)
- {
- continue;
- }
- while (sync.EndIndex > tmpNode->StartIndex && tmpNode != nullptr)
- {
- tmpNode++;
- if (cancellationFuture.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready)
- {
- std::cout << "AsyncFunction canceled." << '\n';
- return false;
- }
- }
- if (tmpNode != nullptr)
- {
- USBPacket packet(tmpNode, leftOverSize, sync, polarity);
- if (packet.IsValid())
- {
- allPacket.push_back(packet);
- }
- }
- }
- return true;
- }
- }
|