Skip to content

Commit

Permalink
clang-format update of USB Analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus10110 committed Oct 29, 2020
1 parent f903109 commit 94abbd1
Show file tree
Hide file tree
Showing 15 changed files with 13,130 additions and 10,123 deletions.
273 changes: 138 additions & 135 deletions src/USBAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,186 +6,189 @@
#include "USBAnalyzer.h"
#include "USBAnalyzerSettings.h"

USBAnalyzer::USBAnalyzer()
: mSimulationInitilized(false)
USBAnalyzer::USBAnalyzer() : mSimulationInitilized( false )
{
SetAnalyzerSettings(&mSettings);
SetAnalyzerSettings( &mSettings );
}

USBAnalyzer::~USBAnalyzer()
{
KillThread();
KillThread();
}

void USBAnalyzer::SetupResults()
{
// reset the results
mResults.reset(new USBAnalyzerResults(this, &mSettings));
SetAnalyzerResults(mResults.get());
// reset the results
mResults.reset( new USBAnalyzerResults( this, &mSettings ) );
SetAnalyzerResults( mResults.get() );

// set which channels will carry bubbles
mResults->AddChannelBubblesWillAppearOn(mSettings.mDPChannel);
mResults->AddChannelBubblesWillAppearOn(mSettings.mDMChannel);
// set which channels will carry bubbles
mResults->AddChannelBubblesWillAppearOn( mSettings.mDPChannel );
mResults->AddChannelBubblesWillAppearOn( mSettings.mDMChannel );
}

bool FrameLessThan(const Frame& lhs, const Frame& rhs)
bool FrameLessThan( const Frame& lhs, const Frame& rhs )
{
return lhs.mStartingSampleInclusive < rhs.mStartingSampleInclusive;
return lhs.mStartingSampleInclusive < rhs.mStartingSampleInclusive;
}

U64 USBAnalyzer::SendPacketToHandler(USBPacket& pckt)
U64 USBAnalyzer::SendPacketToHandler( USBPacket& pckt )
{
if (pckt.IsTokenPacket())
{
mCtrlTransLastPipe.addr = pckt.GetAddress();
mCtrlTransLastPipe.endp = pckt.GetEndpoint();
}

// only control transfers and no SOF or PRE packets
if (mCtrlTransLastPipe.endp == 0 && pckt.mPID != PID_SOF && pckt.mPID != PID_PRE)
{
// do we have this address/enpoint already?
USBPipeHandler::iterator srch(mCtrlTransPacketHandlers.find(mCtrlTransLastPipe));

// is this a new address?
if (srch == mCtrlTransPacketHandlers.end())
{
srch = mCtrlTransPacketHandlers.insert(std::make_pair(mCtrlTransLastPipe, USBControlTransferPacketHandler())).first;
srch->second.Init(mResults.get(), mCtrlTransLastPipe.addr);
}

return srch->second.HandleControlTransfer(pckt);
}

return pckt.AddPacketFrames(mResults.get());
if( pckt.IsTokenPacket() )
{
mCtrlTransLastPipe.addr = pckt.GetAddress();
mCtrlTransLastPipe.endp = pckt.GetEndpoint();
}

// only control transfers and no SOF or PRE packets
if( mCtrlTransLastPipe.endp == 0 && pckt.mPID != PID_SOF && pckt.mPID != PID_PRE )
{
// do we have this address/enpoint already?
USBPipeHandler::iterator srch( mCtrlTransPacketHandlers.find( mCtrlTransLastPipe ) );

// is this a new address?
if( srch == mCtrlTransPacketHandlers.end() )
{
srch = mCtrlTransPacketHandlers.insert( std::make_pair( mCtrlTransLastPipe, USBControlTransferPacketHandler() ) ).first;
srch->second.Init( mResults.get(), mCtrlTransLastPipe.addr );
}

return srch->second.HandleControlTransfer( pckt );
}

return pckt.AddPacketFrames( mResults.get() );
}

void USBAnalyzer::WorkerThread()
{
// get the channel pointers
mDP = GetAnalyzerChannelData(mSettings.mDPChannel);
mDM = GetAnalyzerChannelData(mSettings.mDMChannel);

USBSignalFilter sf(this, mResults.get(), &mSettings, mDP, mDM, mSettings.mSpeed);

const double BIT_DUR = mSettings.mSpeed == FULL_SPEED ? FS_BIT_DUR : LS_BIT_DUR;
const double SAMPLE_DUR = 1e9 / GetSampleRate(); // 1 sample duration in ns
const double BIT_SAMPLES = BIT_DUR / SAMPLE_DUR;

ResetUSB();

USBSignalState s;
USBPacket pckt;
U64 lastFrameEnd = 0;
while (sf.HasMoreData())
{
s = sf.GetState();

if (mSettings.mDecodeLevel == OUT_SIGNALS)
{
s.AddFrame(mResults.get());
} else {

if (lastFrameEnd == 0)
lastFrameEnd = s.mSampleBegin;

// if this is a data signal
if (sf.IsDataSignal(s))
{
// try reading an entire USB packet by parsing subsequent data signals
if (sf.GetPacket(pckt, s))
{
if (mSettings.mDecodeLevel == OUT_CONTROL_TRANSFERS)
lastFrameEnd = SendPacketToHandler(pckt);
else if (mSettings.mDecodeLevel == OUT_PACKETS)
lastFrameEnd = pckt.AddPacketFrames(mResults.get());
else if (mSettings.mDecodeLevel == OUT_BYTES)
lastFrameEnd = pckt.AddRawByteFrames(mResults.get());

} else {
lastFrameEnd = pckt.AddErrorFrame(mResults.get());
}

} else if (mSettings.mSpeed == LOW_SPEED // is this a LS Keep-alive?
&& s.mState == S_SE0
&& s.GetNumBits(LOW_SPEED) == 2) {

Frame f;
f.mStartingSampleInclusive = lastFrameEnd;
f.mEndingSampleInclusive = s.mSampleEnd;
f.mType = FT_KeepAlive;
f.mFlags = FF_None;
f.mData1 = f.mData2 = 0;

mResults->AddFrame(f);
mResults->CommitResults();

lastFrameEnd = s.mSampleEnd;

} else if (s.mState == S_SE0 && s.mDur > 1e7) { // Reset? dur > 10 ms

Frame f;
f.mStartingSampleInclusive = lastFrameEnd;
f.mEndingSampleInclusive = s.mSampleEnd;
f.mType = FT_Reset;
f.mFlags = FF_None;
f.mData1 = f.mData2 = 0;

mResults->AddFrame(f);
mResults->CommitResults();

lastFrameEnd = s.mSampleEnd;

ResetUSB();

} else if (s.mState == S_J) { // Idle

lastFrameEnd = s.mSampleEnd;
}
}

ReportProgress(s.mSampleEnd);
CheckIfThreadShouldExit();
}
// get the channel pointers
mDP = GetAnalyzerChannelData( mSettings.mDPChannel );
mDM = GetAnalyzerChannelData( mSettings.mDMChannel );

USBSignalFilter sf( this, mResults.get(), &mSettings, mDP, mDM, mSettings.mSpeed );

const double BIT_DUR = mSettings.mSpeed == FULL_SPEED ? FS_BIT_DUR : LS_BIT_DUR;
const double SAMPLE_DUR = 1e9 / GetSampleRate(); // 1 sample duration in ns
const double BIT_SAMPLES = BIT_DUR / SAMPLE_DUR;

ResetUSB();

USBSignalState s;
USBPacket pckt;
U64 lastFrameEnd = 0;
while( sf.HasMoreData() )
{
s = sf.GetState();

if( mSettings.mDecodeLevel == OUT_SIGNALS )
{
s.AddFrame( mResults.get() );
}
else
{
if( lastFrameEnd == 0 )
lastFrameEnd = s.mSampleBegin;

// if this is a data signal
if( sf.IsDataSignal( s ) )
{
// try reading an entire USB packet by parsing subsequent data signals
if( sf.GetPacket( pckt, s ) )
{
if( mSettings.mDecodeLevel == OUT_CONTROL_TRANSFERS )
lastFrameEnd = SendPacketToHandler( pckt );
else if( mSettings.mDecodeLevel == OUT_PACKETS )
lastFrameEnd = pckt.AddPacketFrames( mResults.get() );
else if( mSettings.mDecodeLevel == OUT_BYTES )
lastFrameEnd = pckt.AddRawByteFrames( mResults.get() );
}
else
{
lastFrameEnd = pckt.AddErrorFrame( mResults.get() );
}
}
else if( mSettings.mSpeed == LOW_SPEED // is this a LS Keep-alive?
&& s.mState == S_SE0 && s.GetNumBits( LOW_SPEED ) == 2 )
{
Frame f;
f.mStartingSampleInclusive = lastFrameEnd;
f.mEndingSampleInclusive = s.mSampleEnd;
f.mType = FT_KeepAlive;
f.mFlags = FF_None;
f.mData1 = f.mData2 = 0;

mResults->AddFrame( f );
mResults->CommitResults();

lastFrameEnd = s.mSampleEnd;
}
else if( s.mState == S_SE0 && s.mDur > 1e7 )
{ // Reset? dur > 10 ms

Frame f;
f.mStartingSampleInclusive = lastFrameEnd;
f.mEndingSampleInclusive = s.mSampleEnd;
f.mType = FT_Reset;
f.mFlags = FF_None;
f.mData1 = f.mData2 = 0;

mResults->AddFrame( f );
mResults->CommitResults();

lastFrameEnd = s.mSampleEnd;

ResetUSB();
}
else if( s.mState == S_J )
{ // Idle

lastFrameEnd = s.mSampleEnd;
}
}

ReportProgress( s.mSampleEnd );
CheckIfThreadShouldExit();
}
}

bool USBAnalyzer::NeedsRerun()
{
return false;
return false;
}

U32 USBAnalyzer::GenerateSimulationData(U64 minimum_sample_index, U32 device_sample_rate, SimulationChannelDescriptor** simulation_channels)
U32 USBAnalyzer::GenerateSimulationData( U64 minimum_sample_index, U32 device_sample_rate,
SimulationChannelDescriptor** simulation_channels )
{
if (!mSimulationInitilized)
{
mSimulationDataGenerator.Initialize(GetSimulationSampleRate(), &mSettings);
mSimulationInitilized = true;
}
if( !mSimulationInitilized )
{
mSimulationDataGenerator.Initialize( GetSimulationSampleRate(), &mSettings );
mSimulationInitilized = true;
}

return mSimulationDataGenerator.GenerateSimulationData(minimum_sample_index, device_sample_rate, simulation_channels);
return mSimulationDataGenerator.GenerateSimulationData( minimum_sample_index, device_sample_rate, simulation_channels );
}

U32 USBAnalyzer::GetMinimumSampleRateHz()
{
return 24000000; // full 24MHz
return 24000000; // full 24MHz
}

const char* USBAnalyzer::GetAnalyzerName() const
{
return ::GetAnalyzerName();
return ::GetAnalyzerName();
}

const char* GetAnalyzerName()
{
return "USB LS and FS";
return "USB LS and FS";
}

Analyzer* CreateAnalyzer()
{
return new USBAnalyzer();
return new USBAnalyzer();
}

void DestroyAnalyzer(Analyzer* analyzer)
void DestroyAnalyzer( Analyzer* analyzer )
{
delete analyzer;
delete analyzer;
}
Loading

0 comments on commit 94abbd1

Please sign in to comment.