From b8dcc844628357a94ed80567eb7187b9cd7640c2 Mon Sep 17 00:00:00 2001 From: Marcus10110 Date: Mon, 1 Jun 2020 17:53:48 -0700 Subject: [PATCH] applied clang format. --- src/CanAnalyzer.cpp | 942 ++++++++++++++--------------- src/CanAnalyzer.h | 155 ++--- src/CanAnalyzerResults.cpp | 691 ++++++++++----------- src/CanAnalyzerResults.h | 38 +- src/CanAnalyzerSettings.cpp | 129 ++-- src/CanAnalyzerSettings.h | 35 +- src/CanSimulationDataGenerator.cpp | 900 +++++++++++++-------------- src/CanSimulationDataGenerator.h | 70 +-- 8 files changed, 1484 insertions(+), 1476 deletions(-) diff --git a/src/CanAnalyzer.cpp b/src/CanAnalyzer.cpp index 5338936..bfa1241 100644 --- a/src/CanAnalyzer.cpp +++ b/src/CanAnalyzer.cpp @@ -4,553 +4,543 @@ #include -CanAnalyzer::CanAnalyzer() -: Analyzer2(), - mSettings( new CanAnalyzerSettings() ), - mSimulationInitilized( false ) +CanAnalyzer::CanAnalyzer() : Analyzer2(), mSettings( new CanAnalyzerSettings() ), mSimulationInitilized( false ) { - SetAnalyzerSettings( mSettings.get() ); + SetAnalyzerSettings( mSettings.get() ); } CanAnalyzer::~CanAnalyzer() { - KillThread(); + KillThread(); } void CanAnalyzer::SetupResults() { - mResults.reset( new CanAnalyzerResults( this, mSettings.get() ) ); - SetAnalyzerResults( mResults.get() ); - mResults->AddChannelBubblesWillAppearOn( mSettings->mCanChannel ); + mResults.reset( new CanAnalyzerResults( this, mSettings.get() ) ); + SetAnalyzerResults( mResults.get() ); + mResults->AddChannelBubblesWillAppearOn( mSettings->mCanChannel ); } void CanAnalyzer::WorkerThread() { - mSampleRateHz = GetSampleRate(); - mCan = GetAnalyzerChannelData( mSettings->mCanChannel ); - - InitSampleOffsets(); - WaitFor7RecessiveBits(); //first of all, let's get at least 7 recessive bits in a row, to make sure we're in-between frames. - - //now let's pull in the frames, one at a time. - for( ; ; ) - { - if( mCan->GetBitState() == mSettings->Recessive() ) - mCan->AdvanceToNextEdge(); - - //we're at the first DOMINANT edge of the frame - GetRawFrame(); - AnalizeRawFrame(); - - if( mCanError == true ) - { - Frame frame; - frame.mStartingSampleInclusive = mErrorStartingSample; - frame.mEndingSampleInclusive = mErrorEndingSample; - frame.mType = CanError; - mResults->AddFrame( frame ); - mResults->CancelPacketAndStartNewPacket(); - } - - U32 count = mCanMarkers.size(); - for( U32 i=0; iAddMarker( mCanMarkers[i].mSample, AnalyzerResults::Dot, mSettings->mCanChannel ); - else - mResults->AddMarker( mCanMarkers[i].mSample, AnalyzerResults::ErrorX, mSettings->mCanChannel ); - } - - mResults->CommitResults(); - ReportProgress( mCan->GetSampleNumber() ); - CheckIfThreadShouldExit(); - - if( mCanError == true ) - { - WaitFor7RecessiveBits(); - } - } + mSampleRateHz = GetSampleRate(); + mCan = GetAnalyzerChannelData( mSettings->mCanChannel ); + + InitSampleOffsets(); + WaitFor7RecessiveBits(); // first of all, let's get at least 7 recessive bits in a row, to make sure we're in-between frames. + + // now let's pull in the frames, one at a time. + for( ;; ) + { + if( mCan->GetBitState() == mSettings->Recessive() ) + mCan->AdvanceToNextEdge(); + + // we're at the first DOMINANT edge of the frame + GetRawFrame(); + AnalizeRawFrame(); + + if( mCanError == true ) + { + Frame frame; + frame.mStartingSampleInclusive = mErrorStartingSample; + frame.mEndingSampleInclusive = mErrorEndingSample; + frame.mType = CanError; + mResults->AddFrame( frame ); + mResults->CancelPacketAndStartNewPacket(); + } + + U32 count = mCanMarkers.size(); + for( U32 i = 0; i < count; i++ ) + { + if( mCanMarkers[ i ].mType == Standard ) + mResults->AddMarker( mCanMarkers[ i ].mSample, AnalyzerResults::Dot, mSettings->mCanChannel ); + else + mResults->AddMarker( mCanMarkers[ i ].mSample, AnalyzerResults::ErrorX, mSettings->mCanChannel ); + } + + mResults->CommitResults(); + ReportProgress( mCan->GetSampleNumber() ); + CheckIfThreadShouldExit(); + + if( mCanError == true ) + { + WaitFor7RecessiveBits(); + } + } } void CanAnalyzer::InitSampleOffsets() { - mSampleOffsets.resize( 256 ); - - double samples_per_bit = double( mSampleRateHz ) / double( mSettings->mBitRate ); - double samples_behind = 0.0; - - U32 increment = U32( (samples_per_bit * .5 ) + samples_behind ); - samples_behind = (samples_per_bit * .5) + samples_behind - double( increment ); - - mSampleOffsets[0] = increment; - U32 current_offset = increment; - - for( U32 i=1; i<256; i++ ) - { - U32 increment = U32( samples_per_bit + samples_behind ); - samples_behind = samples_per_bit + samples_behind - double( increment ); - current_offset += increment; - mSampleOffsets[i] = current_offset; - } - - mNumSamplesIn7Bits = U32( samples_per_bit * 7.0 ); + mSampleOffsets.resize( 256 ); + + double samples_per_bit = double( mSampleRateHz ) / double( mSettings->mBitRate ); + double samples_behind = 0.0; + + U32 increment = U32( ( samples_per_bit * .5 ) + samples_behind ); + samples_behind = ( samples_per_bit * .5 ) + samples_behind - double( increment ); + + mSampleOffsets[ 0 ] = increment; + U32 current_offset = increment; + + for( U32 i = 1; i < 256; i++ ) + { + U32 increment = U32( samples_per_bit + samples_behind ); + samples_behind = samples_per_bit + samples_behind - double( increment ); + current_offset += increment; + mSampleOffsets[ i ] = current_offset; + } + + mNumSamplesIn7Bits = U32( samples_per_bit * 7.0 ); } void CanAnalyzer::WaitFor7RecessiveBits() { - if( mCan->GetBitState() == mSettings->Dominant() ) - mCan->AdvanceToNextEdge(); + if( mCan->GetBitState() == mSettings->Dominant() ) + mCan->AdvanceToNextEdge(); - for( ; ; ) - { - if( mCan->WouldAdvancingCauseTransition( mNumSamplesIn7Bits ) == false ) - return; + for( ;; ) + { + if( mCan->WouldAdvancingCauseTransition( mNumSamplesIn7Bits ) == false ) + return; - mCan->AdvanceToNextEdge(); - mCan->AdvanceToNextEdge(); - } + mCan->AdvanceToNextEdge(); + mCan->AdvanceToNextEdge(); + } } void CanAnalyzer::GetRawFrame() { - mCanError = false; - mRecessiveCount = 0; - mDominantCount = 0; - mRawBitResults.clear(); - - if( mCan->GetBitState() != mSettings->Dominant() ) - AnalyzerHelpers::Assert( "GetFrameOrError assumes we start DOMINANT" ); - - mStartOfFrame = mCan->GetSampleNumber(); - - U32 i = 0; - //what we're going to do now is capture a sequence up until we get 7 recessive bits in a row. - for( ; ; ) - { - if( i > 255 ) - { - //we are in garbage data most likely, lets get out of here. - return; - } - - mCan->AdvanceToAbsPosition( mStartOfFrame + mSampleOffsets[i] ); - i++; - - if( mCan->GetBitState() == mSettings->Dominant() ) - { - //the bit is DOMINANT - mDominantCount++; - mRecessiveCount = 0; - mRawBitResults.push_back( mSettings->Dominant() ); - - if( mDominantCount == 6 ) - { - //we have detected an error. - - mCanError = true; - mErrorStartingSample = mStartOfFrame + mSampleOffsets[i-5]; - mErrorEndingSample = mStartOfFrame + mSampleOffsets[i]; - - //don't use any of these error bits in analysis. - mRawBitResults.pop_back(); - mRawBitResults.pop_back(); - mRawBitResults.pop_back(); - mRawBitResults.pop_back(); - mRawBitResults.pop_back(); - mRawBitResults.pop_back(); - - mNumRawBits = mRawBitResults.size(); - - //the channel is currently high. addvance it to the next start bit. - //no, don't bother, we want to analyze this packet before we advance. - - break; - } - }else - { - //the bit is RECESSIVE - mRecessiveCount++; - mDominantCount = 0; - mRawBitResults.push_back( mSettings->Recessive() ); - - if( mRecessiveCount == 7 ) - { - //we're done. - break; - } - - } - } - - mNumRawBits = mRawBitResults.size(); + mCanError = false; + mRecessiveCount = 0; + mDominantCount = 0; + mRawBitResults.clear(); + + if( mCan->GetBitState() != mSettings->Dominant() ) + AnalyzerHelpers::Assert( "GetFrameOrError assumes we start DOMINANT" ); + + mStartOfFrame = mCan->GetSampleNumber(); + + U32 i = 0; + // what we're going to do now is capture a sequence up until we get 7 recessive bits in a row. + for( ;; ) + { + if( i > 255 ) + { + // we are in garbage data most likely, lets get out of here. + return; + } + + mCan->AdvanceToAbsPosition( mStartOfFrame + mSampleOffsets[ i ] ); + i++; + + if( mCan->GetBitState() == mSettings->Dominant() ) + { + // the bit is DOMINANT + mDominantCount++; + mRecessiveCount = 0; + mRawBitResults.push_back( mSettings->Dominant() ); + + if( mDominantCount == 6 ) + { + // we have detected an error. + + mCanError = true; + mErrorStartingSample = mStartOfFrame + mSampleOffsets[ i - 5 ]; + mErrorEndingSample = mStartOfFrame + mSampleOffsets[ i ]; + + // don't use any of these error bits in analysis. + mRawBitResults.pop_back(); + mRawBitResults.pop_back(); + mRawBitResults.pop_back(); + mRawBitResults.pop_back(); + mRawBitResults.pop_back(); + mRawBitResults.pop_back(); + + mNumRawBits = mRawBitResults.size(); + + // the channel is currently high. addvance it to the next start bit. + // no, don't bother, we want to analyze this packet before we advance. + + break; + } + } + else + { + // the bit is RECESSIVE + mRecessiveCount++; + mDominantCount = 0; + mRawBitResults.push_back( mSettings->Recessive() ); + + if( mRecessiveCount == 7 ) + { + // we're done. + break; + } + } + } + + mNumRawBits = mRawBitResults.size(); } - void CanAnalyzer::AnalizeRawFrame() { - BitState bit; - U64 last_sample; - - UnstuffRawFrameBit( bit, last_sample, true ); //grab the start bit, and reset everything. - mArbitrationField.clear(); - mControlField.clear(); - mDataField.clear(); - mCrcFieldWithoutDelimiter.clear(); - mAckField.clear(); - - bool done; - - mIdentifier = 0; - for( U32 i=0; i<11; i++ ) - { - mIdentifier <<= 1; - BitState bit; - done = UnstuffRawFrameBit( bit, last_sample ); - if( done == true ) - return; - mArbitrationField.push_back( bit ); - - if( bit == mSettings->Recessive() ) - mIdentifier |= 1; - } - - //ok, the next three bits will let us know if this is 11-bit or 29-bit can. If it's 11-bit, then it'll also tell us if this is a remote frame request or not. - - BitState bit0; - done = UnstuffRawFrameBit( bit0, last_sample ); - if( done == true ) - return; - - BitState bit1; - done = UnstuffRawFrameBit( bit1, last_sample ); - if( done == true ) - return; - - //ok, if bit1 is dominant, then this is 11-bit. - - Frame frame; - - if( bit1 == mSettings->Dominant() ) - { - //11-bit CAN - - BitState bit2; //since this is 11-bit CAN, we know that bit2 is the r0 bit, which we are going to throw away. - done = UnstuffRawFrameBit( bit2, last_sample ); - if( done == true ) - return; - - mStandardCan = true; - - frame.mStartingSampleInclusive = mStartOfFrame + mSampleOffsets[1]; - frame.mEndingSampleInclusive = last_sample; - frame.mType = IdentifierField; - - if( bit0 == mSettings->Recessive() ) //since this is 11-bit CAN, we know that bit0 is the RTR bit - { - mRemoteFrame = true; - frame.mFlags = REMOTE_FRAME; - }else - { - mRemoteFrame = false; - frame.mFlags = 0; - } - - frame.mData1 = mIdentifier; - mResults->AddFrame( frame ); - } - else - { - //29-bit CAN - - mStandardCan = false; - - //get the next 18 address bits. - for( U32 i=0; i<18; i++ ) - { - mIdentifier <<= 1; - - BitState bit; - done = UnstuffRawFrameBit( bit, last_sample ); - if( done == true ) - return; - mArbitrationField.push_back( bit ); - - if( bit == mSettings->Recessive() ) - mIdentifier |= 1; - } - - //get the RTR bit - BitState rtr; - done = UnstuffRawFrameBit( rtr, last_sample ); - if( done == true ) - return; - - //get the r0 and r1 bits (we won't use them) - BitState r0; - done = UnstuffRawFrameBit( r0, last_sample ); - if( done == true ) - return; - - BitState r1; - done = UnstuffRawFrameBit( r1, last_sample ); - if( done == true ) - return; - - Frame frame; - frame.mStartingSampleInclusive = mStartOfFrame + mSampleOffsets[1]; - frame.mEndingSampleInclusive = last_sample; - frame.mType = IdentifierFieldEx; - - if( rtr == mSettings->Recessive() ) - { - mRemoteFrame = true; - frame.mFlags = REMOTE_FRAME; - }else - { - mRemoteFrame = false; - frame.mFlags = 0; - } - - frame.mData1 = mIdentifier; - mResults->AddFrame( frame ); - } - - - U32 mask = 0x8; - mNumDataBytes = 0; - U64 first_sample = 0; - for( U32 i=0; i<4; i++ ) - { - BitState bit; - if( i == 0 ) - done = UnstuffRawFrameBit( bit, first_sample ); - else - done = UnstuffRawFrameBit( bit, last_sample ); - - if( done == true ) - return; - - mControlField.push_back( bit ); - - if( bit == mSettings->Recessive() ) - mNumDataBytes |= mask; - - mask >>= 1; - } - - frame.mStartingSampleInclusive = first_sample; - frame.mEndingSampleInclusive = last_sample; - frame.mType = ControlField ; - frame.mData1 = mNumDataBytes; - mResults->AddFrame( frame ); - - U32 num_bytes = mNumDataBytes; - if( num_bytes > 8 ) - num_bytes = 8; - - if( mRemoteFrame == true ) - num_bytes = 0; //ignore the num_bytes if this is a remote frame. - - for( U32 i=0; iRecessive() ) - data |= mask; - - mask >>= 1; - - mDataField.push_back( bit ); - } - - frame.mStartingSampleInclusive = first_sample; - frame.mEndingSampleInclusive = last_sample; - frame.mType = DataField; - frame.mData1 = data; - mResults->AddFrame( frame ); - } - - mCrcValue = 0; - for( U32 i=0; i<15; i++ ) - { - mCrcValue <<= 1; - BitState bit; - - if( i==0 ) - done = UnstuffRawFrameBit( bit, first_sample ); - else - done = UnstuffRawFrameBit( bit, last_sample ); - - if( done == true ) - return; - - mCrcFieldWithoutDelimiter.push_back( bit ); - - if( bit == mSettings->Recessive() ) - mCrcValue |= 1; - } - - frame.mStartingSampleInclusive = first_sample; - frame.mEndingSampleInclusive = last_sample; - frame.mType = CrcField; - frame.mData1 = mCrcValue; - mResults->AddFrame( frame ); - - done = UnstuffRawFrameBit( mCrcDelimiter, first_sample ); - - if( done == true ) - return; - - BitState ack; - done = GetFixedFormFrameBit( ack, first_sample ); - - mAckField.push_back( ack ); - if( ack == mSettings->Dominant() ) - mAck = true; - else - mAck = false; - - done = GetFixedFormFrameBit( ack, last_sample ); - - if( done == true ) - return; + BitState bit; + U64 last_sample; + + UnstuffRawFrameBit( bit, last_sample, true ); // grab the start bit, and reset everything. + mArbitrationField.clear(); + mControlField.clear(); + mDataField.clear(); + mCrcFieldWithoutDelimiter.clear(); + mAckField.clear(); + + bool done; + + mIdentifier = 0; + for( U32 i = 0; i < 11; i++ ) + { + mIdentifier <<= 1; + BitState bit; + done = UnstuffRawFrameBit( bit, last_sample ); + if( done == true ) + return; + mArbitrationField.push_back( bit ); + + if( bit == mSettings->Recessive() ) + mIdentifier |= 1; + } + + // ok, the next three bits will let us know if this is 11-bit or 29-bit can. If it's 11-bit, then it'll also tell us if this is a + // remote frame request or not. + + BitState bit0; + done = UnstuffRawFrameBit( bit0, last_sample ); + if( done == true ) + return; + + BitState bit1; + done = UnstuffRawFrameBit( bit1, last_sample ); + if( done == true ) + return; + + // ok, if bit1 is dominant, then this is 11-bit. + + Frame frame; + + if( bit1 == mSettings->Dominant() ) + { + // 11-bit CAN + + BitState bit2; // since this is 11-bit CAN, we know that bit2 is the r0 bit, which we are going to throw away. + done = UnstuffRawFrameBit( bit2, last_sample ); + if( done == true ) + return; + + mStandardCan = true; + + frame.mStartingSampleInclusive = mStartOfFrame + mSampleOffsets[ 1 ]; + frame.mEndingSampleInclusive = last_sample; + frame.mType = IdentifierField; + + if( bit0 == mSettings->Recessive() ) // since this is 11-bit CAN, we know that bit0 is the RTR bit + { + mRemoteFrame = true; + frame.mFlags = REMOTE_FRAME; + } + else + { + mRemoteFrame = false; + frame.mFlags = 0; + } + + frame.mData1 = mIdentifier; + mResults->AddFrame( frame ); + } + else + { + // 29-bit CAN + + mStandardCan = false; + + // get the next 18 address bits. + for( U32 i = 0; i < 18; i++ ) + { + mIdentifier <<= 1; + + BitState bit; + done = UnstuffRawFrameBit( bit, last_sample ); + if( done == true ) + return; + mArbitrationField.push_back( bit ); + + if( bit == mSettings->Recessive() ) + mIdentifier |= 1; + } + + // get the RTR bit + BitState rtr; + done = UnstuffRawFrameBit( rtr, last_sample ); + if( done == true ) + return; + + // get the r0 and r1 bits (we won't use them) + BitState r0; + done = UnstuffRawFrameBit( r0, last_sample ); + if( done == true ) + return; + + BitState r1; + done = UnstuffRawFrameBit( r1, last_sample ); + if( done == true ) + return; + + Frame frame; + frame.mStartingSampleInclusive = mStartOfFrame + mSampleOffsets[ 1 ]; + frame.mEndingSampleInclusive = last_sample; + frame.mType = IdentifierFieldEx; + + if( rtr == mSettings->Recessive() ) + { + mRemoteFrame = true; + frame.mFlags = REMOTE_FRAME; + } + else + { + mRemoteFrame = false; + frame.mFlags = 0; + } + + frame.mData1 = mIdentifier; + mResults->AddFrame( frame ); + } + + + U32 mask = 0x8; + mNumDataBytes = 0; + U64 first_sample = 0; + for( U32 i = 0; i < 4; i++ ) + { + BitState bit; + if( i == 0 ) + done = UnstuffRawFrameBit( bit, first_sample ); + else + done = UnstuffRawFrameBit( bit, last_sample ); + + if( done == true ) + return; + + mControlField.push_back( bit ); + + if( bit == mSettings->Recessive() ) + mNumDataBytes |= mask; + + mask >>= 1; + } + + frame.mStartingSampleInclusive = first_sample; + frame.mEndingSampleInclusive = last_sample; + frame.mType = ControlField; + frame.mData1 = mNumDataBytes; + mResults->AddFrame( frame ); + + U32 num_bytes = mNumDataBytes; + if( num_bytes > 8 ) + num_bytes = 8; + + if( mRemoteFrame == true ) + num_bytes = 0; // ignore the num_bytes if this is a remote frame. + + for( U32 i = 0; i < num_bytes; i++ ) + { + U32 data = 0; + U32 mask = 0x80; + for( U32 j = 0; j < 8; j++ ) + { + BitState bit; + + if( j == 0 ) + done = UnstuffRawFrameBit( bit, first_sample ); + else + done = UnstuffRawFrameBit( bit, last_sample ); + + if( done == true ) + return; + + if( bit == mSettings->Recessive() ) + data |= mask; + + mask >>= 1; + + mDataField.push_back( bit ); + } + + frame.mStartingSampleInclusive = first_sample; + frame.mEndingSampleInclusive = last_sample; + frame.mType = DataField; + frame.mData1 = data; + mResults->AddFrame( frame ); + } + + mCrcValue = 0; + for( U32 i = 0; i < 15; i++ ) + { + mCrcValue <<= 1; + BitState bit; + + if( i == 0 ) + done = UnstuffRawFrameBit( bit, first_sample ); + else + done = UnstuffRawFrameBit( bit, last_sample ); + + if( done == true ) + return; + + mCrcFieldWithoutDelimiter.push_back( bit ); + + if( bit == mSettings->Recessive() ) + mCrcValue |= 1; + } + + frame.mStartingSampleInclusive = first_sample; + frame.mEndingSampleInclusive = last_sample; + frame.mType = CrcField; + frame.mData1 = mCrcValue; + mResults->AddFrame( frame ); + + done = UnstuffRawFrameBit( mCrcDelimiter, first_sample ); - mAckField.push_back( ack ); + if( done == true ) + return; - frame.mStartingSampleInclusive = first_sample; - frame.mEndingSampleInclusive = last_sample; - frame.mType = AckField; - frame.mData1 = mAck; - mResults->AddFrame( frame ); - mResults->CommitPacketAndStartNewPacket(); + BitState ack; + done = GetFixedFormFrameBit( ack, first_sample ); + + mAckField.push_back( ack ); + if( ack == mSettings->Dominant() ) + mAck = true; + else + mAck = false; + + done = GetFixedFormFrameBit( ack, last_sample ); + + if( done == true ) + return; + + mAckField.push_back( ack ); + + frame.mStartingSampleInclusive = first_sample; + frame.mEndingSampleInclusive = last_sample; + frame.mType = AckField; + frame.mData1 = mAck; + mResults->AddFrame( frame ); + mResults->CommitPacketAndStartNewPacket(); } bool CanAnalyzer::GetFixedFormFrameBit( BitState& result, U64& sample ) { - if( mNumRawBits == mRawFrameIndex ) - return true; + if( mNumRawBits == mRawFrameIndex ) + return true; - result = mRawBitResults[ mRawFrameIndex ]; - sample = mStartOfFrame + mSampleOffsets[mRawFrameIndex]; - mCanMarkers.push_back( CanMarker( sample, Standard ) ); - mRawFrameIndex++; + result = mRawBitResults[ mRawFrameIndex ]; + sample = mStartOfFrame + mSampleOffsets[ mRawFrameIndex ]; + mCanMarkers.push_back( CanMarker( sample, Standard ) ); + mRawFrameIndex++; - return false; + return false; } bool CanAnalyzer::UnstuffRawFrameBit( BitState& result, U64& sample, bool reset ) { - if( reset == true ) - { - mRecessiveCount = 0; - mDominantCount = 0; - mRawFrameIndex = 0; - mCanMarkers.clear(); - } - - if( mRawFrameIndex == mNumRawBits ) - return true; - - if( mRecessiveCount == 5 ) - { - mRecessiveCount = 0; - mDominantCount = 1; //this bit is DOMINANT, and counts twards the next bit stuff - mCanMarkers.push_back( CanMarker( mStartOfFrame + mSampleOffsets[mRawFrameIndex], BitStuff ) ); - mRawFrameIndex++; - } - - if( mDominantCount == 5 ) - { - mDominantCount = 0; - mRecessiveCount = 1; //this bit is RECESSIVE, and counts twards the next bit stuff - mCanMarkers.push_back( CanMarker( mStartOfFrame + mSampleOffsets[mRawFrameIndex], BitStuff ) ); - mRawFrameIndex++; - } - - if( mRawFrameIndex == mNumRawBits ) - return true; - - result = mRawBitResults[ mRawFrameIndex ]; - - if( result == mSettings->Recessive() ) - { - mRecessiveCount++; - mDominantCount = 0; - }else - { - mDominantCount++; - mRecessiveCount = 0; - } - - sample = mStartOfFrame + mSampleOffsets[mRawFrameIndex]; - mCanMarkers.push_back( CanMarker( sample, Standard ) ); - mRawFrameIndex++; - - return false; + if( reset == true ) + { + mRecessiveCount = 0; + mDominantCount = 0; + mRawFrameIndex = 0; + mCanMarkers.clear(); + } + + if( mRawFrameIndex == mNumRawBits ) + return true; + + if( mRecessiveCount == 5 ) + { + mRecessiveCount = 0; + mDominantCount = 1; // this bit is DOMINANT, and counts twards the next bit stuff + mCanMarkers.push_back( CanMarker( mStartOfFrame + mSampleOffsets[ mRawFrameIndex ], BitStuff ) ); + mRawFrameIndex++; + } + + if( mDominantCount == 5 ) + { + mDominantCount = 0; + mRecessiveCount = 1; // this bit is RECESSIVE, and counts twards the next bit stuff + mCanMarkers.push_back( CanMarker( mStartOfFrame + mSampleOffsets[ mRawFrameIndex ], BitStuff ) ); + mRawFrameIndex++; + } + + if( mRawFrameIndex == mNumRawBits ) + return true; + + result = mRawBitResults[ mRawFrameIndex ]; + + if( result == mSettings->Recessive() ) + { + mRecessiveCount++; + mDominantCount = 0; + } + else + { + mDominantCount++; + mRecessiveCount = 0; + } + + sample = mStartOfFrame + mSampleOffsets[ mRawFrameIndex ]; + mCanMarkers.push_back( CanMarker( sample, Standard ) ); + mRawFrameIndex++; + + return false; } - - - - - - - - - - - bool CanAnalyzer::NeedsRerun() { - return false; + return false; } -U32 CanAnalyzer::GenerateSimulationData( U64 minimum_sample_index, U32 device_sample_rate, SimulationChannelDescriptor** simulation_channels ) +U32 CanAnalyzer::GenerateSimulationData( U64 minimum_sample_index, U32 device_sample_rate, + SimulationChannelDescriptor** simulation_channels ) { - if( mSimulationInitilized == false ) - { - mSimulationDataGenerator.Initialize( GetSimulationSampleRate(), mSettings.get() ); - mSimulationInitilized = true; - } + if( mSimulationInitilized == false ) + { + mSimulationDataGenerator.Initialize( GetSimulationSampleRate(), mSettings.get() ); + 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 CanAnalyzer::GetMinimumSampleRateHz() { - return mSettings->mBitRate * 8; + return mSettings->mBitRate * 8; } const char* CanAnalyzer::GetAnalyzerName() const { - return "CAN"; + return "CAN"; } const char* GetAnalyzerName() { - return "CAN"; + return "CAN"; } Analyzer* CreateAnalyzer() { - return new CanAnalyzer(); + return new CanAnalyzer(); } void DestroyAnalyzer( Analyzer* analyzer ) { - delete analyzer; + delete analyzer; } diff --git a/src/CanAnalyzer.h b/src/CanAnalyzer.h index 42462e1..0806311 100644 --- a/src/CanAnalyzer.h +++ b/src/CanAnalyzer.h @@ -5,100 +5,103 @@ #include "CanAnalyzerResults.h" #include "CanSimulationDataGenerator.h" -enum CanBitType { Standard, BitStuff }; - +enum CanBitType +{ + Standard, + BitStuff +}; class CanMarker { -public: - CanMarker( U64 sample, enum CanBitType type ) - { - mSample = sample; - mType = type; - } - - U64 mSample; - enum CanBitType mType; + public: + CanMarker( U64 sample, enum CanBitType type ) + { + mSample = sample; + mType = type; + } + + U64 mSample; + enum CanBitType mType; }; - class SerialAnalyzerSettings; class CanAnalyzer : public Analyzer2 { -public: - CanAnalyzer(); - virtual ~CanAnalyzer(); - virtual void SetupResults(); - virtual void WorkerThread(); + public: + CanAnalyzer(); + virtual ~CanAnalyzer(); + virtual void SetupResults(); + virtual void WorkerThread(); - virtual U32 GenerateSimulationData( U64 newest_sample_requested, U32 sample_rate, SimulationChannelDescriptor** simulation_channels ); - virtual U32 GetMinimumSampleRateHz(); + virtual U32 GenerateSimulationData( U64 newest_sample_requested, U32 sample_rate, SimulationChannelDescriptor** simulation_channels ); + virtual U32 GetMinimumSampleRateHz(); - virtual const char* GetAnalyzerName() const; - virtual bool NeedsRerun(); + virtual const char* GetAnalyzerName() const; + virtual bool NeedsRerun(); #pragma warning( push ) -#pragma warning( disable : 4251 ) //warning C4251: 'SerialAnalyzer::<...>' : class <...> needs to have dll-interface to be used by clients of class - -protected: //vars - std::auto_ptr< CanAnalyzerSettings > mSettings; - std::auto_ptr< CanAnalyzerResults > mResults; - AnalyzerChannelData* mCan; - U32 mSampleRateHz; - - CanSimulationDataGenerator mSimulationDataGenerator; - bool mSimulationInitilized; - - -protected: //analysis functions - void WaitFor7RecessiveBits(); - void InitSampleOffsets(); - void GetRawFrame(); - void AnalizeRawFrame(); - bool UnstuffRawFrameBit( BitState& result, U64& sample, bool reset = false); - bool GetFixedFormFrameBit( BitState& result, U64& sample ); - -protected: //analysis vars: - //ChunkedArray* mFrameBubbles; - - U32 mNumSamplesIn7Bits; - U32 mRecessiveCount; - U32 mDominantCount; - U32 mRawFrameIndex; - U64 mStartOfFrame; - U32 mIdentifier; - U32 mCrcValue; - bool mAck; - - std::vector mSampleOffsets; - std::vector mRawBitResults; - std::vector mBitResults; - - std::vector mCanMarkers; - - std::vector mArbitrationField; - bool mStandardCan; - bool mRemoteFrame; - U32 mNumDataBytes; - std::vector mControlField; - std::vector mDataField; - std::vector mCrcFieldWithoutDelimiter; - BitState mCrcDelimiter; - std::vector mAckField; - std::vector mEndOfFrame; - - U32 mNumRawBits; - bool mCanError; - U64 mErrorStartingSample; - U64 mErrorEndingSample; +#pragma warning( \ + disable : 4251 ) // warning C4251: 'SerialAnalyzer::<...>' : class <...> needs to have dll-interface to be used by clients of class + + protected: // vars + std::auto_ptr mSettings; + std::auto_ptr mResults; + AnalyzerChannelData* mCan; + U32 mSampleRateHz; + + CanSimulationDataGenerator mSimulationDataGenerator; + bool mSimulationInitilized; + + + protected: // analysis functions + void WaitFor7RecessiveBits(); + void InitSampleOffsets(); + void GetRawFrame(); + void AnalizeRawFrame(); + bool UnstuffRawFrameBit( BitState& result, U64& sample, bool reset = false ); + bool GetFixedFormFrameBit( BitState& result, U64& sample ); + + protected: // analysis vars: + // ChunkedArray* mFrameBubbles; + + U32 mNumSamplesIn7Bits; + U32 mRecessiveCount; + U32 mDominantCount; + U32 mRawFrameIndex; + U64 mStartOfFrame; + U32 mIdentifier; + U32 mCrcValue; + bool mAck; + + std::vector mSampleOffsets; + std::vector mRawBitResults; + std::vector mBitResults; + + std::vector mCanMarkers; + + std::vector mArbitrationField; + bool mStandardCan; + bool mRemoteFrame; + U32 mNumDataBytes; + std::vector mControlField; + std::vector mDataField; + std::vector mCrcFieldWithoutDelimiter; + BitState mCrcDelimiter; + std::vector mAckField; + std::vector mEndOfFrame; + + U32 mNumRawBits; + bool mCanError; + U64 mErrorStartingSample; + U64 mErrorEndingSample; #pragma warning( pop ) }; extern "C" ANALYZER_EXPORT const char* __cdecl GetAnalyzerName(); -extern "C" ANALYZER_EXPORT Analyzer* __cdecl CreateAnalyzer( ); +extern "C" ANALYZER_EXPORT Analyzer* __cdecl CreateAnalyzer(); extern "C" ANALYZER_EXPORT void __cdecl DestroyAnalyzer( Analyzer* analyzer ); -#endif //CAN_ANALYZER_H +#endif // CAN_ANALYZER_H diff --git a/src/CanAnalyzerResults.cpp b/src/CanAnalyzerResults.cpp index e37036e..10478e6 100644 --- a/src/CanAnalyzerResults.cpp +++ b/src/CanAnalyzerResults.cpp @@ -5,12 +5,10 @@ #include #include -#pragma warning(disable: 4800) //warning C4800: 'U64' : forcing value to bool 'true' or 'false' (performance warning) +#pragma warning( disable : 4800 ) // warning C4800: 'U64' : forcing value to bool 'true' or 'false' (performance warning) CanAnalyzerResults::CanAnalyzerResults( CanAnalyzer* analyzer, CanAnalyzerSettings* settings ) -: AnalyzerResults(), - mSettings( settings ), - mAnalyzer( analyzer ) + : AnalyzerResults(), mSettings( settings ), mAnalyzer( analyzer ) { } @@ -18,359 +16,370 @@ CanAnalyzerResults::~CanAnalyzerResults() { } -void CanAnalyzerResults::GenerateBubbleText( U64 frame_index, Channel& /*channel*/, DisplayBase display_base ) //unrefereced vars commented out to remove warnings. +void CanAnalyzerResults::GenerateBubbleText( U64 frame_index, Channel& /*channel*/, + DisplayBase display_base ) // unrefereced vars commented out to remove warnings. { - //we only need to pay attention to 'channel' if we're making bubbles for more than one channel (as set by AddChannelBubblesWillAppearOn) - ClearResultStrings(); - Frame frame = GetFrame( frame_index ); - - switch( frame.mType ) - { - case IdentifierField: - case IdentifierFieldEx: - { - char number_str[128]; - - if( frame.mType == IdentifierField ) - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 12, number_str, 128 ); - else - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 32, number_str, 128 ); - - std::stringstream ss; - - AddResultString( "Id" ); - - ss << "Id: " << number_str; - AddResultString( ss.str().c_str() ); - ss.str(""); - - ss << "Identifier: " << number_str; - AddResultString( ss.str().c_str() ); - ss.str(""); - - if( frame.HasFlag( REMOTE_FRAME ) == false ) - { - if( frame.mType == IdentifierField ) - ss << "Standard CAN Identifier: " << number_str; - else - ss << "Extended CAN Identifier: " << number_str; - }else - { - if( frame.mType == IdentifierField ) - ss << "Standard CAN Identifier: " << number_str << " (RTR)"; - else - ss << "Extended CAN Identifier: " << number_str << " (RTR)"; - } - - AddResultString( ss.str().c_str() ); - } - break; - case ControlField: - { - char number_str[128]; - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 4, number_str, 128 ); - - std::stringstream ss; - AddResultString( "Ctrl" ); - - ss << "Ctrl: " << number_str; - AddResultString( ss.str().c_str() ); - ss.str(""); - - ss << "Control Field: " << number_str; - AddResultString( ss.str().c_str() ); - - ss << " bytes"; - AddResultString( ss.str().c_str() ); - } - break; - case DataField: - { - char number_str[128]; - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 8, number_str, 128 ); - - AddResultString( number_str ); - - std::stringstream ss; - ss << "Data: " << number_str; - AddResultString( ss.str().c_str() ); - ss.str(""); - - ss << "Data Field Byte: " << number_str; - AddResultString( ss.str().c_str() ); - } - break; - case CrcField: - { - char number_str[128]; - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 15, number_str, 128 ); - - AddResultString( "CRC" ); - - std::stringstream ss; - ss << "CRC: " << number_str; - AddResultString( ss.str().c_str() ); - ss.str(""); - - ss << "CRC value: " << number_str; - AddResultString( ss.str().c_str() ); - } - break; - case AckField: - { - if( bool( frame.mData1 ) == true ) - AddResultString( "ACK" ); - else - AddResultString( "NAK" ); - } - break; - case CanError: - { - AddResultString( "E" ); - AddResultString( "Error" ); - } - break; - } + // we only need to pay attention to 'channel' if we're making bubbles for more than one channel (as set by + // AddChannelBubblesWillAppearOn) + ClearResultStrings(); + Frame frame = GetFrame( frame_index ); + + switch( frame.mType ) + { + case IdentifierField: + case IdentifierFieldEx: + { + char number_str[ 128 ]; + + if( frame.mType == IdentifierField ) + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 12, number_str, 128 ); + else + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 32, number_str, 128 ); + + std::stringstream ss; + + AddResultString( "Id" ); + + ss << "Id: " << number_str; + AddResultString( ss.str().c_str() ); + ss.str( "" ); + + ss << "Identifier: " << number_str; + AddResultString( ss.str().c_str() ); + ss.str( "" ); + + if( frame.HasFlag( REMOTE_FRAME ) == false ) + { + if( frame.mType == IdentifierField ) + ss << "Standard CAN Identifier: " << number_str; + else + ss << "Extended CAN Identifier: " << number_str; + } + else + { + if( frame.mType == IdentifierField ) + ss << "Standard CAN Identifier: " << number_str << " (RTR)"; + else + ss << "Extended CAN Identifier: " << number_str << " (RTR)"; + } + + AddResultString( ss.str().c_str() ); + } + break; + case ControlField: + { + char number_str[ 128 ]; + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 4, number_str, 128 ); + + std::stringstream ss; + AddResultString( "Ctrl" ); + + ss << "Ctrl: " << number_str; + AddResultString( ss.str().c_str() ); + ss.str( "" ); + + ss << "Control Field: " << number_str; + AddResultString( ss.str().c_str() ); + + ss << " bytes"; + AddResultString( ss.str().c_str() ); + } + break; + case DataField: + { + char number_str[ 128 ]; + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 8, number_str, 128 ); + + AddResultString( number_str ); + + std::stringstream ss; + ss << "Data: " << number_str; + AddResultString( ss.str().c_str() ); + ss.str( "" ); + + ss << "Data Field Byte: " << number_str; + AddResultString( ss.str().c_str() ); + } + break; + case CrcField: + { + char number_str[ 128 ]; + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 15, number_str, 128 ); + + AddResultString( "CRC" ); + + std::stringstream ss; + ss << "CRC: " << number_str; + AddResultString( ss.str().c_str() ); + ss.str( "" ); + + ss << "CRC value: " << number_str; + AddResultString( ss.str().c_str() ); + } + break; + case AckField: + { + if( bool( frame.mData1 ) == true ) + AddResultString( "ACK" ); + else + AddResultString( "NAK" ); + } + break; + case CanError: + { + AddResultString( "E" ); + AddResultString( "Error" ); + } + break; + } } void CanAnalyzerResults::GenerateExportFile( const char* file, DisplayBase display_base, U32 /*export_type_user_id*/ ) { - //export_type_user_id is only important if we have more than one export type. - std::stringstream ss; - void* f = AnalyzerHelpers::StartFile( file ); - - U64 trigger_sample = mAnalyzer->GetTriggerSample(); - U32 sample_rate = mAnalyzer->GetSampleRate(); - - ss << "Time [s],Packet,Type,Identifier,Control,Data,CRC,ACK" << std::endl; - U64 num_frames = GetNumFrames(); - U64 num_packets = GetNumPackets(); - for( U32 i=0; i < num_packets; i++ ) - { - if( i != 0 ) - { - //below, we "continue" the loop rather than run to the end. So we need to save to the file here. - ss << std::endl; - - AnalyzerHelpers::AppendToFile( (U8*)ss.str().c_str(), ss.str().length(), f ); - ss.str( std::string() ); - - - if( UpdateExportProgressAndCheckForCancel( i, num_packets ) == true ) - { - AnalyzerHelpers::EndFile( f ); - return; - } - } - - - U64 first_frame_id; - U64 last_frame_id; - GetFramesContainedInPacket( i, &first_frame_id, &last_frame_id ); - Frame frame = GetFrame( first_frame_id ); - - //static void GetTimeString( U64 sample, U64 trigger_sample, U32 sample_rate_hz, char* result_string, U32 result_string_max_length ); - char time_str[128]; - AnalyzerHelpers::GetTimeString( frame.mStartingSampleInclusive, trigger_sample, sample_rate, time_str, 128 ); - - char packet_str[128]; - AnalyzerHelpers::GetNumberString( i, Decimal, 0, packet_str, 128 ); - - if( frame.HasFlag( REMOTE_FRAME ) == false ) - ss << time_str << "," << packet_str << ",DATA"; - else - ss << time_str << "," << packet_str << ",REMOTE"; - - U64 frame_id = first_frame_id; - - frame = GetFrame( frame_id ); - - char number_str[128]; - - if( frame.mType == IdentifierField ) - { - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 12, number_str, 128); - ss << "," << number_str; - ++frame_id; - } - else if( frame.mType == IdentifierFieldEx ) - { - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 32, number_str, 128 ); - ss << "," << number_str; - ++frame_id; - } - else - { - ss << ","; - } - - if( frame_id > last_frame_id ) - continue; - - frame = GetFrame( frame_id ); - if( frame.mType == ControlField ) - { - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 4, number_str, 128); - ss << "," << number_str; - ++frame_id; - } - else - { - ss << ","; - } - ss << ","; - if( frame_id > last_frame_id ) - continue; - - for( ; ; ) - { - frame = GetFrame( frame_id ); - if( frame.mType != DataField ) - break; - - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 8, number_str, 128 ); - ss << number_str; - if( frame_id == last_frame_id ) - break; - - ++frame_id; - if( GetFrame( frame_id ).mType == DataField ) - ss << " "; - } - - if( frame_id > last_frame_id ) - continue; - - frame = GetFrame( frame_id ); - if( frame.mType == CrcField ) - { - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 15, number_str, 128); - ss << "," << number_str; - ++frame_id; - }else - { - ss << ","; - } - if( frame_id > last_frame_id ) - continue; - - frame = GetFrame( frame_id ); - if( frame.mType == AckField ) - { - if( bool( frame.mData1 ) == true ) - ss << "," << "ACK"; - else - ss << "," << "NAK"; - - ++frame_id; - }else - { - ss << ","; - } - } - - UpdateExportProgressAndCheckForCancel( num_frames, num_frames ); - AnalyzerHelpers::EndFile( f ); + // export_type_user_id is only important if we have more than one export type. + std::stringstream ss; + void* f = AnalyzerHelpers::StartFile( file ); + + U64 trigger_sample = mAnalyzer->GetTriggerSample(); + U32 sample_rate = mAnalyzer->GetSampleRate(); + + ss << "Time [s],Packet,Type,Identifier,Control,Data,CRC,ACK" << std::endl; + U64 num_frames = GetNumFrames(); + U64 num_packets = GetNumPackets(); + for( U32 i = 0; i < num_packets; i++ ) + { + if( i != 0 ) + { + // below, we "continue" the loop rather than run to the end. So we need to save to the file here. + ss << std::endl; + + AnalyzerHelpers::AppendToFile( ( U8* )ss.str().c_str(), ss.str().length(), f ); + ss.str( std::string() ); + + + if( UpdateExportProgressAndCheckForCancel( i, num_packets ) == true ) + { + AnalyzerHelpers::EndFile( f ); + return; + } + } + + + U64 first_frame_id; + U64 last_frame_id; + GetFramesContainedInPacket( i, &first_frame_id, &last_frame_id ); + Frame frame = GetFrame( first_frame_id ); + + // static void GetTimeString( U64 sample, U64 trigger_sample, U32 sample_rate_hz, char* result_string, U32 result_string_max_length + // ); + char time_str[ 128 ]; + AnalyzerHelpers::GetTimeString( frame.mStartingSampleInclusive, trigger_sample, sample_rate, time_str, 128 ); + + char packet_str[ 128 ]; + AnalyzerHelpers::GetNumberString( i, Decimal, 0, packet_str, 128 ); + + if( frame.HasFlag( REMOTE_FRAME ) == false ) + ss << time_str << "," << packet_str << ",DATA"; + else + ss << time_str << "," << packet_str << ",REMOTE"; + + U64 frame_id = first_frame_id; + + frame = GetFrame( frame_id ); + + char number_str[ 128 ]; + + if( frame.mType == IdentifierField ) + { + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 12, number_str, 128 ); + ss << "," << number_str; + ++frame_id; + } + else if( frame.mType == IdentifierFieldEx ) + { + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 32, number_str, 128 ); + ss << "," << number_str; + ++frame_id; + } + else + { + ss << ","; + } + + if( frame_id > last_frame_id ) + continue; + + frame = GetFrame( frame_id ); + if( frame.mType == ControlField ) + { + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 4, number_str, 128 ); + ss << "," << number_str; + ++frame_id; + } + else + { + ss << ","; + } + ss << ","; + if( frame_id > last_frame_id ) + continue; + + for( ;; ) + { + frame = GetFrame( frame_id ); + if( frame.mType != DataField ) + break; + + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 8, number_str, 128 ); + ss << number_str; + if( frame_id == last_frame_id ) + break; + + ++frame_id; + if( GetFrame( frame_id ).mType == DataField ) + ss << " "; + } + + if( frame_id > last_frame_id ) + continue; + + frame = GetFrame( frame_id ); + if( frame.mType == CrcField ) + { + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 15, number_str, 128 ); + ss << "," << number_str; + ++frame_id; + } + else + { + ss << ","; + } + if( frame_id > last_frame_id ) + continue; + + frame = GetFrame( frame_id ); + if( frame.mType == AckField ) + { + if( bool( frame.mData1 ) == true ) + ss << "," + << "ACK"; + else + ss << "," + << "NAK"; + + ++frame_id; + } + else + { + ss << ","; + } + } + + UpdateExportProgressAndCheckForCancel( num_frames, num_frames ); + AnalyzerHelpers::EndFile( f ); } void CanAnalyzerResults::GenerateFrameTabularText( U64 frame_index, DisplayBase display_base ) { ClearTabularText(); - Frame frame = GetFrame( frame_index ); - - switch( frame.mType ) - { - case IdentifierField: - case IdentifierFieldEx: - { - char number_str[128]; - - if( frame.mType == IdentifierField ) - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 12, number_str, 128 ); - else - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 32, number_str, 128 ); - - std::stringstream ss; - - - if( frame.HasFlag( REMOTE_FRAME ) == false ) - { - if( frame.mType == IdentifierField ) - ss << "Standard CAN Identifier: " << number_str; - else - ss << "Extended CAN Identifier: " << number_str; - }else - { - if( frame.mType == IdentifierField ) - ss << "Standard CAN Identifier: " << number_str << " (RTR)"; - else - ss << "Extended CAN Identifier: " << number_str << " (RTR)"; - } - - AddTabularText( ss.str().c_str() ); - } - break; - case ControlField: - { - char number_str[128]; - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 4, number_str, 128 ); - - std::stringstream ss; - - ss << "Control Field: " << number_str; - ss << " bytes"; - AddTabularText( ss.str().c_str() ); - - } - break; - case DataField: - { - char number_str[128]; - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 8, number_str, 128 ); - - std::stringstream ss; - - ss << "Data Field Byte: " << number_str; - AddTabularText( ss.str().c_str() ); - } - break; - case CrcField: - { - char number_str[128]; - AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 15, number_str, 128 ); - - std::stringstream ss; - - ss << "CRC value: " << number_str; - AddTabularText( ss.str().c_str() ); - } - break; - case AckField: - { - if( bool( frame.mData1 ) == true ) - AddTabularText( "ACK" ); - else - AddTabularText( "NAK" ); - } - break; - case CanError: - { - AddTabularText( "Error" ); - } - break; - } + Frame frame = GetFrame( frame_index ); + + switch( frame.mType ) + { + case IdentifierField: + case IdentifierFieldEx: + { + char number_str[ 128 ]; + + if( frame.mType == IdentifierField ) + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 12, number_str, 128 ); + else + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 32, number_str, 128 ); + + std::stringstream ss; + + + if( frame.HasFlag( REMOTE_FRAME ) == false ) + { + if( frame.mType == IdentifierField ) + ss << "Standard CAN Identifier: " << number_str; + else + ss << "Extended CAN Identifier: " << number_str; + } + else + { + if( frame.mType == IdentifierField ) + ss << "Standard CAN Identifier: " << number_str << " (RTR)"; + else + ss << "Extended CAN Identifier: " << number_str << " (RTR)"; + } + + AddTabularText( ss.str().c_str() ); + } + break; + case ControlField: + { + char number_str[ 128 ]; + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 4, number_str, 128 ); + + std::stringstream ss; + + ss << "Control Field: " << number_str; + ss << " bytes"; + AddTabularText( ss.str().c_str() ); + } + break; + case DataField: + { + char number_str[ 128 ]; + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 8, number_str, 128 ); + + std::stringstream ss; + + ss << "Data Field Byte: " << number_str; + AddTabularText( ss.str().c_str() ); + } + break; + case CrcField: + { + char number_str[ 128 ]; + AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 15, number_str, 128 ); + + std::stringstream ss; + + ss << "CRC value: " << number_str; + AddTabularText( ss.str().c_str() ); + } + break; + case AckField: + { + if( bool( frame.mData1 ) == true ) + AddTabularText( "ACK" ); + else + AddTabularText( "NAK" ); + } + break; + case CanError: + { + AddTabularText( "Error" ); + } + break; + } } -void CanAnalyzerResults::GeneratePacketTabularText( U64 /*packet_id*/, DisplayBase /*display_base*/ ) //unrefereced vars commented out to remove warnings. +void CanAnalyzerResults::GeneratePacketTabularText( U64 /*packet_id*/, + DisplayBase /*display_base*/ ) // unrefereced vars commented out to remove warnings. { - ClearResultStrings(); - AddResultString( "not supported" ); + ClearResultStrings(); + AddResultString( "not supported" ); } -void CanAnalyzerResults::GenerateTransactionTabularText( U64 /*transaction_id*/, DisplayBase /*display_base*/ ) //unrefereced vars commented out to remove warnings. +void + CanAnalyzerResults::GenerateTransactionTabularText( U64 /*transaction_id*/, + DisplayBase /*display_base*/ ) // unrefereced vars commented out to remove warnings. { - ClearResultStrings(); - AddResultString( "not supported" ); + ClearResultStrings(); + AddResultString( "not supported" ); } diff --git a/src/CanAnalyzerResults.h b/src/CanAnalyzerResults.h index 95234ce..3aeb178 100644 --- a/src/CanAnalyzerResults.h +++ b/src/CanAnalyzerResults.h @@ -3,7 +3,16 @@ #include -enum CanFrameType { IdentifierField, IdentifierFieldEx, ControlField, DataField, CrcField, AckField, CanError }; +enum CanFrameType +{ + IdentifierField, + IdentifierFieldEx, + ControlField, + DataField, + CrcField, + AckField, + CanError +}; #define REMOTE_FRAME ( 1 << 0 ) //#define FRAMING_ERROR_FLAG ( 1 << 0 ) @@ -14,22 +23,21 @@ class CanAnalyzerSettings; class CanAnalyzerResults : public AnalyzerResults { -public: - CanAnalyzerResults( CanAnalyzer* analyzer, CanAnalyzerSettings* settings ); - virtual ~CanAnalyzerResults(); - - virtual void GenerateBubbleText( U64 frame_index, Channel& channel, DisplayBase display_base ); - virtual void GenerateExportFile( const char* file, DisplayBase display_base, U32 export_type_user_id ); + public: + CanAnalyzerResults( CanAnalyzer* analyzer, CanAnalyzerSettings* settings ); + virtual ~CanAnalyzerResults(); - virtual void GenerateFrameTabularText(U64 frame_index, DisplayBase display_base ); - virtual void GeneratePacketTabularText( U64 packet_id, DisplayBase display_base ); - virtual void GenerateTransactionTabularText( U64 transaction_id, DisplayBase display_base ); + virtual void GenerateBubbleText( U64 frame_index, Channel& channel, DisplayBase display_base ); + virtual void GenerateExportFile( const char* file, DisplayBase display_base, U32 export_type_user_id ); -protected: //functions + virtual void GenerateFrameTabularText( U64 frame_index, DisplayBase display_base ); + virtual void GeneratePacketTabularText( U64 packet_id, DisplayBase display_base ); + virtual void GenerateTransactionTabularText( U64 transaction_id, DisplayBase display_base ); -protected: //vars - CanAnalyzerSettings* mSettings; - CanAnalyzer* mAnalyzer; + protected: // functions + protected: // vars + CanAnalyzerSettings* mSettings; + CanAnalyzer* mAnalyzer; }; -#endif //CAN_ANALYZER_RESULTS +#endif // CAN_ANALYZER_RESULTS diff --git a/src/CanAnalyzerSettings.cpp b/src/CanAnalyzerSettings.cpp index e8448e1..5c12f82 100644 --- a/src/CanAnalyzerSettings.cpp +++ b/src/CanAnalyzerSettings.cpp @@ -4,38 +4,35 @@ #include #include -CanAnalyzerSettings::CanAnalyzerSettings() -: mCanChannel ( UNDEFINED_CHANNEL ), - mBitRate ( 1000000 ), - mInverted( false ) +CanAnalyzerSettings::CanAnalyzerSettings() : mCanChannel( UNDEFINED_CHANNEL ), mBitRate( 1000000 ), mInverted( false ) { - mCanChannelInterface.reset( new AnalyzerSettingInterfaceChannel() ); - mCanChannelInterface->SetTitleAndTooltip( "CAN", "Controller Area Network - Input" ); - mCanChannelInterface->SetChannel( mCanChannel ); + mCanChannelInterface.reset( new AnalyzerSettingInterfaceChannel() ); + mCanChannelInterface->SetTitleAndTooltip( "CAN", "Controller Area Network - Input" ); + mCanChannelInterface->SetChannel( mCanChannel ); - mBitRateInterface.reset( new AnalyzerSettingInterfaceInteger() ); - mBitRateInterface->SetTitleAndTooltip( "Bit Rate (Bits/s)", "Specify the bit rate in bits per second." ); + mBitRateInterface.reset( new AnalyzerSettingInterfaceInteger() ); + mBitRateInterface->SetTitleAndTooltip( "Bit Rate (Bits/s)", "Specify the bit rate in bits per second." ); mBitRateInterface->SetMax( 25000000 ); - mBitRateInterface->SetMin( 10000 ); - mBitRateInterface->SetInteger( mBitRate ); + mBitRateInterface->SetMin( 10000 ); + mBitRateInterface->SetInteger( mBitRate ); - mCanChannelInvertedInterface.reset( new AnalyzerSettingInterfaceBool( ) ); - mCanChannelInvertedInterface->SetTitleAndTooltip( "", "Use this option when recording CAN High directly" ); - mCanChannelInvertedInterface->SetCheckBoxText( "Inverted (CAN High)" ); - mCanChannelInvertedInterface->SetValue( mInverted ); + mCanChannelInvertedInterface.reset( new AnalyzerSettingInterfaceBool() ); + mCanChannelInvertedInterface->SetTitleAndTooltip( "", "Use this option when recording CAN High directly" ); + mCanChannelInvertedInterface->SetCheckBoxText( "Inverted (CAN High)" ); + mCanChannelInvertedInterface->SetValue( mInverted ); - AddInterface( mCanChannelInterface.get() ); - AddInterface( mBitRateInterface.get() ); - AddInterface( mCanChannelInvertedInterface.get() ); + AddInterface( mCanChannelInterface.get() ); + AddInterface( mBitRateInterface.get() ); + AddInterface( mCanChannelInvertedInterface.get() ); - //AddExportOption( 0, "Export as text/csv file", "text (*.txt);;csv (*.csv)" ); - AddExportOption( 0, "Export as text/csv file" ); - AddExportExtension( 0, "text", "txt" ); - AddExportExtension( 0, "csv", "csv" ); + // AddExportOption( 0, "Export as text/csv file", "text (*.txt);;csv (*.csv)" ); + AddExportOption( 0, "Export as text/csv file" ); + AddExportExtension( 0, "text", "txt" ); + AddExportExtension( 0, "csv", "csv" ); - ClearChannels(); - AddChannel( mCanChannel, "CAN", false ); + ClearChannels(); + AddChannel( mCanChannel, "CAN", false ); } CanAnalyzerSettings::~CanAnalyzerSettings() @@ -44,72 +41,72 @@ CanAnalyzerSettings::~CanAnalyzerSettings() bool CanAnalyzerSettings::SetSettingsFromInterfaces() { - Channel can_channel = mCanChannelInterface->GetChannel(); - - if( can_channel == UNDEFINED_CHANNEL ) - { - SetErrorText( "Please select a channel for the CAN interface" ); - return false; - } - mCanChannel = can_channel; - mBitRate = mBitRateInterface->GetInteger(); - mInverted = mCanChannelInvertedInterface->GetValue(); - - ClearChannels(); - AddChannel( mCanChannel, "CAN", true ); - - return true; + Channel can_channel = mCanChannelInterface->GetChannel(); + + if( can_channel == UNDEFINED_CHANNEL ) + { + SetErrorText( "Please select a channel for the CAN interface" ); + return false; + } + mCanChannel = can_channel; + mBitRate = mBitRateInterface->GetInteger(); + mInverted = mCanChannelInvertedInterface->GetValue(); + + ClearChannels(); + AddChannel( mCanChannel, "CAN", true ); + + return true; } void CanAnalyzerSettings::LoadSettings( const char* settings ) { - SimpleArchive text_archive; - text_archive.SetString( settings ); + SimpleArchive text_archive; + text_archive.SetString( settings ); - const char* name_string; //the first thing in the archive is the name of the protocol analyzer that the data belongs to. - text_archive >> &name_string; - if( strcmp( name_string, "SaleaeCANAnalyzer" ) != 0 ) - AnalyzerHelpers::Assert( "SaleaeCanAnalyzer: Provided with a settings string that doesn't belong to us;" ); + const char* name_string; // the first thing in the archive is the name of the protocol analyzer that the data belongs to. + text_archive >> &name_string; + if( strcmp( name_string, "SaleaeCANAnalyzer" ) != 0 ) + AnalyzerHelpers::Assert( "SaleaeCanAnalyzer: Provided with a settings string that doesn't belong to us;" ); - text_archive >> mCanChannel; - text_archive >> mBitRate; - text_archive >> mInverted; //SimpleArchive catches exception and returns false if it fails. + text_archive >> mCanChannel; + text_archive >> mBitRate; + text_archive >> mInverted; // SimpleArchive catches exception and returns false if it fails. - ClearChannels(); - AddChannel( mCanChannel, "CAN", true ); + ClearChannels(); + AddChannel( mCanChannel, "CAN", true ); - UpdateInterfacesFromSettings(); + UpdateInterfacesFromSettings(); } const char* CanAnalyzerSettings::SaveSettings() { - SimpleArchive text_archive; + SimpleArchive text_archive; - text_archive << "SaleaeCANAnalyzer"; - text_archive << mCanChannel; - text_archive << mBitRate; - text_archive << mInverted; + text_archive << "SaleaeCANAnalyzer"; + text_archive << mCanChannel; + text_archive << mBitRate; + text_archive << mInverted; - return SetReturnString( text_archive.GetString() ); + return SetReturnString( text_archive.GetString() ); } void CanAnalyzerSettings::UpdateInterfacesFromSettings() { - mCanChannelInterface->SetChannel( mCanChannel ); - mBitRateInterface->SetInteger( mBitRate ); - mCanChannelInvertedInterface->SetValue( mInverted ); + mCanChannelInterface->SetChannel( mCanChannel ); + mBitRateInterface->SetInteger( mBitRate ); + mCanChannelInvertedInterface->SetValue( mInverted ); } BitState CanAnalyzerSettings::Recessive() { - if( mInverted ) - return BIT_LOW; - return BIT_HIGH; + if( mInverted ) + return BIT_LOW; + return BIT_HIGH; } BitState CanAnalyzerSettings::Dominant() { - if( mInverted ) - return BIT_HIGH; - return BIT_LOW; + if( mInverted ) + return BIT_HIGH; + return BIT_LOW; } diff --git a/src/CanAnalyzerSettings.h b/src/CanAnalyzerSettings.h index 9bd86a4..3de99b8 100644 --- a/src/CanAnalyzerSettings.h +++ b/src/CanAnalyzerSettings.h @@ -9,27 +9,26 @@ class CanAnalyzerSettings : public AnalyzerSettings { -public: - CanAnalyzerSettings(); - virtual ~CanAnalyzerSettings(); + public: + CanAnalyzerSettings(); + virtual ~CanAnalyzerSettings(); - virtual bool SetSettingsFromInterfaces(); - virtual void LoadSettings( const char* settings ); - virtual const char* SaveSettings(); + virtual bool SetSettingsFromInterfaces(); + virtual void LoadSettings( const char* settings ); + virtual const char* SaveSettings(); - void UpdateInterfacesFromSettings(); + void UpdateInterfacesFromSettings(); - Channel mCanChannel; - U32 mBitRate; - bool mInverted; + Channel mCanChannel; + U32 mBitRate; + bool mInverted; - BitState Recessive(); - BitState Dominant(); - -protected: - std::auto_ptr< AnalyzerSettingInterfaceChannel > mCanChannelInterface; - std::auto_ptr< AnalyzerSettingInterfaceInteger > mBitRateInterface; - std::auto_ptr< AnalyzerSettingInterfaceBool > mCanChannelInvertedInterface; + BitState Recessive(); + BitState Dominant(); + protected: + std::auto_ptr mCanChannelInterface; + std::auto_ptr mBitRateInterface; + std::auto_ptr mCanChannelInvertedInterface; }; -#endif //CAN_ANALYZER_SETTINGS +#endif // CAN_ANALYZER_SETTINGS diff --git a/src/CanSimulationDataGenerator.cpp b/src/CanSimulationDataGenerator.cpp index 72eaecf..6610b85 100644 --- a/src/CanSimulationDataGenerator.cpp +++ b/src/CanSimulationDataGenerator.cpp @@ -11,486 +11,488 @@ CanSimulationDataGenerator::~CanSimulationDataGenerator() void CanSimulationDataGenerator::Initialize( U32 simulation_sample_rate, CanAnalyzerSettings* settings ) { - mSimulationSampleRateHz = simulation_sample_rate; - mSettings = settings; + mSimulationSampleRateHz = simulation_sample_rate; + mSettings = settings; - mClockGenerator.Init( mSettings->mBitRate, simulation_sample_rate ); - mCanSimulationData.SetChannel( mSettings->mCanChannel ); - mCanSimulationData.SetSampleRate( simulation_sample_rate ); - mCanSimulationData.SetInitialBitState( mSettings->Recessive() ); + mClockGenerator.Init( mSettings->mBitRate, simulation_sample_rate ); + mCanSimulationData.SetChannel( mSettings->mCanChannel ); + mCanSimulationData.SetSampleRate( simulation_sample_rate ); + mCanSimulationData.SetInitialBitState( mSettings->Recessive() ); - mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 10.0 ) ); //insert 10 bit-periods of idle + mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 10.0 ) ); // insert 10 bit-periods of idle - mValue = 0; + mValue = 0; } -U32 CanSimulationDataGenerator::GenerateSimulationData( U64 largest_sample_requested, U32 sample_rate, SimulationChannelDescriptor** simulation_channels ) +U32 CanSimulationDataGenerator::GenerateSimulationData( U64 largest_sample_requested, U32 sample_rate, + SimulationChannelDescriptor** simulation_channels ) { - U64 adjusted_largest_sample_requested = AnalyzerHelpers::AdjustSimulationTargetSample( largest_sample_requested, sample_rate, mSimulationSampleRateHz ); + U64 adjusted_largest_sample_requested = + AnalyzerHelpers::AdjustSimulationTargetSample( largest_sample_requested, sample_rate, mSimulationSampleRateHz ); - std::vector data; - std::vector empty_data; + std::vector data; + std::vector empty_data; - while( mCanSimulationData.GetCurrentSampleNumber() < adjusted_largest_sample_requested ) - { - data.clear(); - data.push_back( mValue + 0 ); - data.push_back( mValue + 1 ); - data.push_back( mValue + 2 ); - data.push_back( mValue + 3 ); + while( mCanSimulationData.GetCurrentSampleNumber() < adjusted_largest_sample_requested ) + { + data.clear(); + data.push_back( mValue + 0 ); + data.push_back( mValue + 1 ); + data.push_back( mValue + 2 ); + data.push_back( mValue + 3 ); - data.push_back( mValue + 4 ); - data.push_back( mValue + 5 ); - data.push_back( mValue + 6 ); - data.push_back( mValue + 7 ); + data.push_back( mValue + 4 ); + data.push_back( mValue + 5 ); + data.push_back( mValue + 6 ); + data.push_back( mValue + 7 ); - mValue++; + mValue++; - CreateDataOrRemoteFrame( 123, false, false, data, true ); - WriteFrame(); + CreateDataOrRemoteFrame( 123, false, false, data, true ); + WriteFrame(); - CreateDataOrRemoteFrame( 321, true, false, data, true ); - WriteFrame(); + CreateDataOrRemoteFrame( 321, true, false, data, true ); + WriteFrame(); - CreateDataOrRemoteFrame( 456, true, false, data, true ); - WriteFrame(true); + CreateDataOrRemoteFrame( 456, true, false, data, true ); + WriteFrame( true ); - mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 40 ) ); + mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 40 ) ); - CreateDataOrRemoteFrame( 123, false, true, empty_data, true ); - WriteFrame(); + CreateDataOrRemoteFrame( 123, false, true, empty_data, true ); + WriteFrame(); - CreateDataOrRemoteFrame( 321, true, true, empty_data, true ); - WriteFrame(); + CreateDataOrRemoteFrame( 321, true, true, empty_data, true ); + WriteFrame(); - mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 100 ) ); - } + mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 100 ) ); + } - *simulation_channels = &mCanSimulationData; - return 1; // we are retuning the size of the SimulationChannelDescriptor array. In our case, the "array" is length 1. + *simulation_channels = &mCanSimulationData; + return 1; // we are retuning the size of the SimulationChannelDescriptor array. In our case, the "array" is length 1. } -void CanSimulationDataGenerator::CreateDataOrRemoteFrame( U32 identifier, bool use_extended_frame_format, bool remote_frame, std::vector& data, bool get_ack_in_response ) +void CanSimulationDataGenerator::CreateDataOrRemoteFrame( U32 identifier, bool use_extended_frame_format, bool remote_frame, + std::vector& data, bool get_ack_in_response ) { - //A DATA FRAME is composed of seven different bit fields: - //START OF FRAME, ARBITRATION FIELD, CONTROL FIELD, DATA FIELD, CRC - //FIELD, ACK FIELD, END OF FRAME. The DATA FIELD can be of length zero. - - mFakeStuffedBits.clear(); - mFakeFixedFormBits.clear(); - mFakeStartOfFrameField.clear(); - mFakeArbitrationField.clear(); - mFakeControlField.clear(); - mFakeDataField.clear(); - mFakeCrcFieldWithoutDelimiter.clear(); - mFakeAckField.clear(); - mFakeEndOfFrame.clear(); - - //START OF FRAME (Standard Format as well as Extended Format) - //The START OF FRAME (SOF) marks the beginning of DATA FRAMES and REMOTE - //FRAMEs. It consists of a single dominant bit. - //A station is only allowed to start transmission when the bus is idle (see INTERFRAME - //Spacing). All stations have to synchronize to the leading edge caused by START OF - //FRAME (see HARD SYNCHRONIZATION) of the station starting transmission first. - - mFakeStartOfFrameField.push_back( mSettings->Dominant() ); - - //ARBITRATION FIELD - //The format of the ARBITRATION FIELD is different for Standard Format and - //Extended Format Frames. - - //In Standard Format the ARBITRATION FIELD consists of the 11 bit IDENTIFIER - //and the RTR-BIT. The IDENTIFIER bits are denoted ID-28 ... ID-18. - - //In Extended Format the ARBITRATION FIELD consists of the 29 bit IDENTIFIER, - //the SRR-Bit, the IDE-Bit, and the RTR-BIT. The IDENTIFIER bits are denoted ID-28 - //... ID-0. - - if( use_extended_frame_format == true ) - { - U32 mask = 1 << 28; //28 bits in exdended format's identifier. - - //IDENTIFIER - Extended Format - //In contrast to the Standard Format the Extended Format consists of 29 bits. The - //format comprises two sections: - - //Base ID - //The Base ID consists of 11 bits. It is transmitted in the order from ID-28 to ID-18. It - //is equivalent to format of the Standard Identifier. The Base ID defines the Extended - //Frames base priority. - - //Extended ID - //The Extended ID consists of 18 bits. It is transmitted in the order of ID-17 to ID-0. - - //11 bits of identifier: - for( U32 i=0; i<11; i++ ) - { - if( ( mask & identifier ) == 0 ) - mFakeArbitrationField.push_back( mSettings->Dominant() ); - else - mFakeArbitrationField.push_back( mSettings->Recessive() ); - - mask >>= 1; - } - - //the next bit is SRR - //SRR BIT (Extended Format) - //Substitute Remote Request BIT - //The SRR is a recessive bit. It is transmitted in Extended Frames at the position of the - //RTR bit in Standard Frames and so substitutes the RTR-Bit in the Standard Frame. - //Therefore, collisions of a Standard Frame and an Extended Frame, the Base ID (see - //Extended IDENTIFIER below) of which is the same as the Standard Frames Identifier, - //are resolved in such a way that the Standard Frame prevails the Extended Frame. - - mFakeArbitrationField.push_back( mSettings->Recessive() ); //SSR bit - - //the next bit is IDE - //IDE BIT (Extended Format) - //Identifier Extension Bit - //The IDE Bit belongs to - //- the ARBITRATION FIELD for the Extended Format - //- the Control Field for the Standard Format - //The IDE bit in the Standard Format is transmitted dominant, whereas in the Extended - //Format the IDE bit is recessive. - - mFakeArbitrationField.push_back( mSettings->Recessive() ); //IDE bit - - //18 bits of identifier: - for( U32 i=0; i<18; i++ ) - { - if( ( mask & identifier ) == 0 ) - mFakeArbitrationField.push_back( mSettings->Dominant() ); - else - mFakeArbitrationField.push_back( mSettings->Recessive() ); - - mask >>= 1; - } - - //the next bit is RTR - //RTR BIT (Standard Format as well as Extended Format) - //Remote Transmission Request BIT - //In DATA FRAMEs the RTR BIT has to be dominant. Within a REMOTE FRAME the - //RTR BIT has to be recessive. - - if( remote_frame == true ) - mFakeArbitrationField.push_back( mSettings->Recessive() ); //RTR bit - else - mFakeArbitrationField.push_back( mSettings->Dominant() ); //RTR bit - - - //next is the control field. r1, r0, DLC3, DLC2, DLC1, DLC0 - - //CONTROL FIELD (Standard Format as well as Extended Format) - //The CONTROL FIELD consists of six bits. The format of the CONTROL FIELD is - //different for Standard Format and Extended Format. Frames in Standard Format - //include the DATA LENGTH CODE, the IDE bit, which is transmitted dominant (see - //above), and the reserved bit r0. Frames in the Extended Format include the DATA - //LENGTH CODE and two reserved bits r1 and r0. The reserved bits have to be sent - //dominant, but receivers accept dominant and recessive bits in all combinations. - - mFakeControlField.push_back( mSettings->Recessive() ); //r1 bit - mFakeControlField.push_back( mSettings->Recessive() ); //r0 bit - }else - { - //IDENTIFIER - //IDENTIFIER - Standard Format - //The IDENTIFIERs length is 11 bits and corresponds to the Base ID in Extended - //Format. These bits are transmitted in the order from ID-28 to ID-18. The least - //significant bit is ID-18. The 7 most significant bits (ID-28 - ID-22) must not be all - //recessive. - - //In a Standard Frame the IDENTIFIER is followed by the RTR bit. - //RTR BIT (Standard Format as well as Extended Format) - //Remote Transmission Request BIT - //In DATA FRAMEs the RTR BIT has to be dominant. Within a REMOTE FRAME the - //RTR BIT has to be recessive. - - - U32 mask = 1 << 10; - for( U32 i=0; i<11; i++ ) - { - if( ( mask & identifier ) == 0 ) - mFakeArbitrationField.push_back( mSettings->Dominant() ); - else - mFakeArbitrationField.push_back( mSettings->Recessive() ); - - mask >>= 1; - } - - //the next bit is RTR - //RTR BIT (Standard Format as well as Extended Format) - //Remote Transmission Request BIT - //In DATA FRAMEs the RTR BIT has to be dominant. Within a REMOTE FRAME the - //RTR BIT has to be recessive. - - if( remote_frame == true ) - mFakeArbitrationField.push_back( mSettings->Recessive() ); //RTR bit - else - mFakeArbitrationField.push_back( mSettings->Dominant() ); //RTR bit - - //next is the control field. r1, r0, DLC3, DLC2, DLC1, DLC0 - - //CONTROL FIELD (Standard Format as well as Extended Format) - //The CONTROL FIELD consists of six bits. The format of the CONTROL FIELD is - //different for Standard Format and Extended Format. Frames in Standard Format - //include the DATA LENGTH CODE, the IDE bit, which is transmitted dominant (see - //above), and the reserved bit r0. Frames in the Extended Format include the DATA - //LENGTH CODE and two reserved bits r1 and r0. The reserved bits have to be sent - //dominant, but receivers accept dominant and recessive bits in all combinations. - - mFakeControlField.push_back( mSettings->Dominant() ); //IDE bit - mFakeControlField.push_back( mSettings->Dominant() ); //r0 bit - } - - - //send 4 bits for the length of the attached data. - U32 data_size = data.size(); - if( data_size > 9 ) - AnalyzerHelpers::Assert( "can't sent more than 8 bytes" ); - - if( remote_frame == true ) - if( data_size != 0 ) - AnalyzerHelpers::Assert( "remote frames can't send data" ); - - U32 mask = 1 << 3; - for( U32 i=0; i<4; i++ ) - { - if( ( mask & data_size ) == 0 ) - mFakeControlField.push_back( mSettings->Dominant() ); - else - mFakeControlField.push_back( mSettings->Recessive() ); - - mask >>= 1; - } - - //next is the DATA FIELD - //DATA FIELD (Standard Format as well as Extended Format) - //The DATA FIELD consists of the data to be transferred within a DATA FRAME. It can - //contain from 0 to 8 bytes, which each contain 8 bits which are transferred MSB first. - - if( remote_frame == false ) - { - for( U32 i=0; iDominant() ); - else - mFakeDataField.push_back( mSettings->Recessive() ); - - mask >>= 1; - } - } - } - - //CRC FIELD (Standard Format as well as Extended Format) - //contains the CRC SEQUENCE followed by a CRC DELIMITER. - - AddCrc(); - - - //ACK FIELD (Standard Format as well as Extended Format) - //The ACK FIELD is two bits long and contains the ACK SLOT and the ACK DELIMITER. - //In the ACK FIELD the transmitting station sends two recessive bits. - //A RECEIVER which has received a valid message correctly, reports this to the - //TRANSMITTER by sending a dominant bit during the ACK SLOT (it sends ACK). - - //ACK SLOT - //All stations having received the matching CRC SEQUENCE report this within the ACK - //SLOT by superscribing the recessive bit of the TRANSMITTER by a dominant bit. - - if( get_ack_in_response == true ) - mFakeAckField.push_back( mSettings->Dominant() ); - else - mFakeAckField.push_back( mSettings->Recessive() ); - - //ACK DELIMITER - //The ACK DELIMITER is the second bit of the ACK FIELD and has to be a recessive - //bit. As a consequence, the ACK SLOT is surrounded by two recessive bits (CRC - //DELIMITER, ACK DELIMITER). - - mFakeAckField.push_back( mSettings->Recessive() ); - - //END OF FRAME (Standard Format as well as Extended Format) - //Each DATA FRAME and REMOTE FRAME is delimited by a flag sequence consisting - //of seven recessive bits. - - for( U32 i=0; i<7; i++ ) - mFakeEndOfFrame.push_back( mSettings->Recessive() ); - - - mFakeFixedFormBits.insert( mFakeFixedFormBits.end(), mFakeAckField.begin(), mFakeAckField.end() ); - mFakeFixedFormBits.insert( mFakeFixedFormBits.end(), mFakeEndOfFrame.begin(), mFakeEndOfFrame.end() ); + // A DATA FRAME is composed of seven different bit fields: + // START OF FRAME, ARBITRATION FIELD, CONTROL FIELD, DATA FIELD, CRC + // FIELD, ACK FIELD, END OF FRAME. The DATA FIELD can be of length zero. + + mFakeStuffedBits.clear(); + mFakeFixedFormBits.clear(); + mFakeStartOfFrameField.clear(); + mFakeArbitrationField.clear(); + mFakeControlField.clear(); + mFakeDataField.clear(); + mFakeCrcFieldWithoutDelimiter.clear(); + mFakeAckField.clear(); + mFakeEndOfFrame.clear(); + + // START OF FRAME (Standard Format as well as Extended Format) + // The START OF FRAME (SOF) marks the beginning of DATA FRAMES and REMOTE + // FRAMEs. It consists of a single dominant bit. + // A station is only allowed to start transmission when the bus is idle (see INTERFRAME + // Spacing). All stations have to synchronize to the leading edge caused by START OF + // FRAME (see HARD SYNCHRONIZATION) of the station starting transmission first. + + mFakeStartOfFrameField.push_back( mSettings->Dominant() ); + + // ARBITRATION FIELD + // The format of the ARBITRATION FIELD is different for Standard Format and + // Extended Format Frames. + + // In Standard Format the ARBITRATION FIELD consists of the 11 bit IDENTIFIER + // and the RTR-BIT. The IDENTIFIER bits are denoted ID-28 ... ID-18. + + // In Extended Format the ARBITRATION FIELD consists of the 29 bit IDENTIFIER, + // the SRR-Bit, the IDE-Bit, and the RTR-BIT. The IDENTIFIER bits are denoted ID-28 + //... ID-0. + + if( use_extended_frame_format == true ) + { + U32 mask = 1 << 28; // 28 bits in exdended format's identifier. + + // IDENTIFIER - Extended Format + // In contrast to the Standard Format the Extended Format consists of 29 bits. The + // format comprises two sections: + + // Base ID + // The Base ID consists of 11 bits. It is transmitted in the order from ID-28 to ID-18. It + // is equivalent to format of the Standard Identifier. The Base ID defines the Extended + // Frames base priority. + + // Extended ID + // The Extended ID consists of 18 bits. It is transmitted in the order of ID-17 to ID-0. + + // 11 bits of identifier: + for( U32 i = 0; i < 11; i++ ) + { + if( ( mask & identifier ) == 0 ) + mFakeArbitrationField.push_back( mSettings->Dominant() ); + else + mFakeArbitrationField.push_back( mSettings->Recessive() ); + + mask >>= 1; + } + + // the next bit is SRR + // SRR BIT (Extended Format) + // Substitute Remote Request BIT + // The SRR is a recessive bit. It is transmitted in Extended Frames at the position of the + // RTR bit in Standard Frames and so substitutes the RTR-Bit in the Standard Frame. + // Therefore, collisions of a Standard Frame and an Extended Frame, the Base ID (see + // Extended IDENTIFIER below) of which is the same as the Standard Frames Identifier, + // are resolved in such a way that the Standard Frame prevails the Extended Frame. + + mFakeArbitrationField.push_back( mSettings->Recessive() ); // SSR bit + + // the next bit is IDE + // IDE BIT (Extended Format) + // Identifier Extension Bit + // The IDE Bit belongs to + //- the ARBITRATION FIELD for the Extended Format + //- the Control Field for the Standard Format + // The IDE bit in the Standard Format is transmitted dominant, whereas in the Extended + // Format the IDE bit is recessive. + + mFakeArbitrationField.push_back( mSettings->Recessive() ); // IDE bit + + // 18 bits of identifier: + for( U32 i = 0; i < 18; i++ ) + { + if( ( mask & identifier ) == 0 ) + mFakeArbitrationField.push_back( mSettings->Dominant() ); + else + mFakeArbitrationField.push_back( mSettings->Recessive() ); + + mask >>= 1; + } + + // the next bit is RTR + // RTR BIT (Standard Format as well as Extended Format) + // Remote Transmission Request BIT + // In DATA FRAMEs the RTR BIT has to be dominant. Within a REMOTE FRAME the + // RTR BIT has to be recessive. + + if( remote_frame == true ) + mFakeArbitrationField.push_back( mSettings->Recessive() ); // RTR bit + else + mFakeArbitrationField.push_back( mSettings->Dominant() ); // RTR bit + + + // next is the control field. r1, r0, DLC3, DLC2, DLC1, DLC0 + + // CONTROL FIELD (Standard Format as well as Extended Format) + // The CONTROL FIELD consists of six bits. The format of the CONTROL FIELD is + // different for Standard Format and Extended Format. Frames in Standard Format + // include the DATA LENGTH CODE, the IDE bit, which is transmitted dominant (see + // above), and the reserved bit r0. Frames in the Extended Format include the DATA + // LENGTH CODE and two reserved bits r1 and r0. The reserved bits have to be sent + // dominant, but receivers accept dominant and recessive bits in all combinations. + + mFakeControlField.push_back( mSettings->Recessive() ); // r1 bit + mFakeControlField.push_back( mSettings->Recessive() ); // r0 bit + } + else + { + // IDENTIFIER + // IDENTIFIER - Standard Format + // The IDENTIFIERs length is 11 bits and corresponds to the Base ID in Extended + // Format. These bits are transmitted in the order from ID-28 to ID-18. The least + // significant bit is ID-18. The 7 most significant bits (ID-28 - ID-22) must not be all + // recessive. + + // In a Standard Frame the IDENTIFIER is followed by the RTR bit. + // RTR BIT (Standard Format as well as Extended Format) + // Remote Transmission Request BIT + // In DATA FRAMEs the RTR BIT has to be dominant. Within a REMOTE FRAME the + // RTR BIT has to be recessive. + + + U32 mask = 1 << 10; + for( U32 i = 0; i < 11; i++ ) + { + if( ( mask & identifier ) == 0 ) + mFakeArbitrationField.push_back( mSettings->Dominant() ); + else + mFakeArbitrationField.push_back( mSettings->Recessive() ); + + mask >>= 1; + } + + // the next bit is RTR + // RTR BIT (Standard Format as well as Extended Format) + // Remote Transmission Request BIT + // In DATA FRAMEs the RTR BIT has to be dominant. Within a REMOTE FRAME the + // RTR BIT has to be recessive. + + if( remote_frame == true ) + mFakeArbitrationField.push_back( mSettings->Recessive() ); // RTR bit + else + mFakeArbitrationField.push_back( mSettings->Dominant() ); // RTR bit + + // next is the control field. r1, r0, DLC3, DLC2, DLC1, DLC0 + + // CONTROL FIELD (Standard Format as well as Extended Format) + // The CONTROL FIELD consists of six bits. The format of the CONTROL FIELD is + // different for Standard Format and Extended Format. Frames in Standard Format + // include the DATA LENGTH CODE, the IDE bit, which is transmitted dominant (see + // above), and the reserved bit r0. Frames in the Extended Format include the DATA + // LENGTH CODE and two reserved bits r1 and r0. The reserved bits have to be sent + // dominant, but receivers accept dominant and recessive bits in all combinations. + + mFakeControlField.push_back( mSettings->Dominant() ); // IDE bit + mFakeControlField.push_back( mSettings->Dominant() ); // r0 bit + } + + + // send 4 bits for the length of the attached data. + U32 data_size = data.size(); + if( data_size > 9 ) + AnalyzerHelpers::Assert( "can't sent more than 8 bytes" ); + + if( remote_frame == true ) + if( data_size != 0 ) + AnalyzerHelpers::Assert( "remote frames can't send data" ); + + U32 mask = 1 << 3; + for( U32 i = 0; i < 4; i++ ) + { + if( ( mask & data_size ) == 0 ) + mFakeControlField.push_back( mSettings->Dominant() ); + else + mFakeControlField.push_back( mSettings->Recessive() ); + + mask >>= 1; + } + + // next is the DATA FIELD + // DATA FIELD (Standard Format as well as Extended Format) + // The DATA FIELD consists of the data to be transferred within a DATA FRAME. It can + // contain from 0 to 8 bytes, which each contain 8 bits which are transferred MSB first. + + if( remote_frame == false ) + { + for( U32 i = 0; i < data_size; i++ ) + { + U32 dat = data[ i ]; + U32 mask = 0x80; + + for( U32 j = 0; j < 8; j++ ) + { + if( ( mask & dat ) == 0 ) + mFakeDataField.push_back( mSettings->Dominant() ); + else + mFakeDataField.push_back( mSettings->Recessive() ); + + mask >>= 1; + } + } + } + + // CRC FIELD (Standard Format as well as Extended Format) + // contains the CRC SEQUENCE followed by a CRC DELIMITER. + + AddCrc(); + + + // ACK FIELD (Standard Format as well as Extended Format) + // The ACK FIELD is two bits long and contains the ACK SLOT and the ACK DELIMITER. + // In the ACK FIELD the transmitting station sends two recessive bits. + // A RECEIVER which has received a valid message correctly, reports this to the + // TRANSMITTER by sending a dominant bit during the ACK SLOT (it sends ACK). + + // ACK SLOT + // All stations having received the matching CRC SEQUENCE report this within the ACK + // SLOT by superscribing the recessive bit of the TRANSMITTER by a dominant bit. + + if( get_ack_in_response == true ) + mFakeAckField.push_back( mSettings->Dominant() ); + else + mFakeAckField.push_back( mSettings->Recessive() ); + + // ACK DELIMITER + // The ACK DELIMITER is the second bit of the ACK FIELD and has to be a recessive + // bit. As a consequence, the ACK SLOT is surrounded by two recessive bits (CRC + // DELIMITER, ACK DELIMITER). + + mFakeAckField.push_back( mSettings->Recessive() ); + + // END OF FRAME (Standard Format as well as Extended Format) + // Each DATA FRAME and REMOTE FRAME is delimited by a flag sequence consisting + // of seven recessive bits. + + for( U32 i = 0; i < 7; i++ ) + mFakeEndOfFrame.push_back( mSettings->Recessive() ); + + + mFakeFixedFormBits.insert( mFakeFixedFormBits.end(), mFakeAckField.begin(), mFakeAckField.end() ); + mFakeFixedFormBits.insert( mFakeFixedFormBits.end(), mFakeEndOfFrame.begin(), mFakeEndOfFrame.end() ); } void CanSimulationDataGenerator::AddCrc() -{ - mFakeStuffedBits.insert( mFakeStuffedBits.end(), mFakeStartOfFrameField.begin(), mFakeStartOfFrameField.end() ); - mFakeStuffedBits.insert( mFakeStuffedBits.end(), mFakeArbitrationField.begin(), mFakeArbitrationField.end() ); - mFakeStuffedBits.insert( mFakeStuffedBits.end(), mFakeControlField.begin(), mFakeControlField.end() ); - mFakeStuffedBits.insert( mFakeStuffedBits.end(), mFakeDataField.begin(), mFakeDataField.end() ); - - U32 bits_for_crc = mFakeStuffedBits.size(); - U16 crc = ComputeCrc( mFakeStuffedBits, bits_for_crc ); - U32 mask = 0x4000; - for( U32 i=0; i<15; i++ ) - { - if( ( mask & crc ) == 0 ) - mFakeCrcFieldWithoutDelimiter.push_back( mSettings->Dominant() ); - else - mFakeCrcFieldWithoutDelimiter.push_back( mSettings->Recessive() ); - - mask >>= 1; - } - - mFakeStuffedBits.insert( mFakeStuffedBits.end(), mFakeCrcFieldWithoutDelimiter.begin(), mFakeCrcFieldWithoutDelimiter.end() ); - - //CRC DELIMITER (Standard Format as well as Extended Format) - //The CRC SEQUENCE is followed by the CRC DELIMITER which consists of a single - //recessive bit. - - mFakeFixedFormBits.push_back( mSettings->Recessive() ); +{ + mFakeStuffedBits.insert( mFakeStuffedBits.end(), mFakeStartOfFrameField.begin(), mFakeStartOfFrameField.end() ); + mFakeStuffedBits.insert( mFakeStuffedBits.end(), mFakeArbitrationField.begin(), mFakeArbitrationField.end() ); + mFakeStuffedBits.insert( mFakeStuffedBits.end(), mFakeControlField.begin(), mFakeControlField.end() ); + mFakeStuffedBits.insert( mFakeStuffedBits.end(), mFakeDataField.begin(), mFakeDataField.end() ); + + U32 bits_for_crc = mFakeStuffedBits.size(); + U16 crc = ComputeCrc( mFakeStuffedBits, bits_for_crc ); + U32 mask = 0x4000; + for( U32 i = 0; i < 15; i++ ) + { + if( ( mask & crc ) == 0 ) + mFakeCrcFieldWithoutDelimiter.push_back( mSettings->Dominant() ); + else + mFakeCrcFieldWithoutDelimiter.push_back( mSettings->Recessive() ); + + mask >>= 1; + } + + mFakeStuffedBits.insert( mFakeStuffedBits.end(), mFakeCrcFieldWithoutDelimiter.begin(), mFakeCrcFieldWithoutDelimiter.end() ); + + // CRC DELIMITER (Standard Format as well as Extended Format) + // The CRC SEQUENCE is followed by the CRC DELIMITER which consists of a single + // recessive bit. + + mFakeFixedFormBits.push_back( mSettings->Recessive() ); } U16 CanSimulationDataGenerator::ComputeCrc( std::vector& bits, U32 num_bits ) { - //note that this is a 15 bit CRC (not 16-bit) - - //CRC FIELD (Standard Format as well as Extended Format) - //contains the CRC SEQUENCE followed by a CRC DELIMITER. - //CRC SEQUENCE (Standard Format as well as Extended Format) - //The frame check sequence is derived from a cyclic redundancy code best suited for - ///frames with bit counts less than 127 bits (BCH Code). - //In order to carry out the CRC calculation the polynomial to be divided is defined as the - //polynomial, the coefficients of which are given by the destuffed bit stream consisting of - //START OF FRAME, ARBITRATION FIELD, CONTROL FIELD, DATA FIELD (if - ///present) and, for the 15 lowest coefficients, by 0. This polynomial is divided (the - //coefficients are calculated modulo-2) by the generator-polynomial: - //X15 + X14 + X10 + X8 + X7 + X4 + X3 + 1. - //The remainder of this polynomial division is the CRC SEQUENCE transmitted over the - //bus. In order to implement this function, a 15 bit shift register CRC_RG(14:0) can be - //used. If NXTBIT denotes the next bit of the bit stream, given by the destuffed bit - //sequence from START OF FRAME until the end of the DATA FIELD, the CRC - //SEQUENCE is calculated as follows: - - //CRC_RG = 0; // initialize shift register - //REPEAT - //CRCNXT = NXTBIT EXOR CRC_RG(14); - //CRC_RG(14:1) = CRC_RG(13:0); // shift left by - //CRC_RG(0) = 0; // 1 position - //IF CRCNXT THEN - //CRC_RG(14:0) = CRC_RG(14:0) EXOR (4599hex); - //ENDIF - //UNTIL (CRC SEQUENCE starts or there is an ERROR condition) - - //After the transmission / reception of the last bit of the DATA FIELD, CRC_RG contains - //the CRC sequence. - - U16 crc_result = 0; - for( U32 i=0; iRecessive() ) //normally bit high. - crc_result ^= 0x4599; - } - - return crc_result & 0x7FFF; + // note that this is a 15 bit CRC (not 16-bit) + + // CRC FIELD (Standard Format as well as Extended Format) + // contains the CRC SEQUENCE followed by a CRC DELIMITER. + // CRC SEQUENCE (Standard Format as well as Extended Format) + // The frame check sequence is derived from a cyclic redundancy code best suited for + /// frames with bit counts less than 127 bits (BCH Code). + // In order to carry out the CRC calculation the polynomial to be divided is defined as the + // polynomial, the coefficients of which are given by the destuffed bit stream consisting of + // START OF FRAME, ARBITRATION FIELD, CONTROL FIELD, DATA FIELD (if + /// present) and, for the 15 lowest coefficients, by 0. This polynomial is divided (the + // coefficients are calculated modulo-2) by the generator-polynomial: + // X15 + X14 + X10 + X8 + X7 + X4 + X3 + 1. + // The remainder of this polynomial division is the CRC SEQUENCE transmitted over the + // bus. In order to implement this function, a 15 bit shift register CRC_RG(14:0) can be + // used. If NXTBIT denotes the next bit of the bit stream, given by the destuffed bit + // sequence from START OF FRAME until the end of the DATA FIELD, the CRC + // SEQUENCE is calculated as follows: + + // CRC_RG = 0; // initialize shift register + // REPEAT + // CRCNXT = NXTBIT EXOR CRC_RG(14); + // CRC_RG(14:1) = CRC_RG(13:0); // shift left by + // CRC_RG(0) = 0; // 1 position + // IF CRCNXT THEN + // CRC_RG(14:0) = CRC_RG(14:0) EXOR (4599hex); + // ENDIF + // UNTIL (CRC SEQUENCE starts or there is an ERROR condition) + + // After the transmission / reception of the last bit of the DATA FIELD, CRC_RG contains + // the CRC sequence. + + U16 crc_result = 0; + for( U32 i = 0; i < num_bits; i++ ) + { + BitState next_bit = bits[ i ]; + + // Exclusive or + if( ( crc_result & 0x4000 ) != 0 ) + { + next_bit = Invert( next_bit ); // if the msb of crc_result is zero, then next_bit is not inverted; otherwise, it is. + } + + crc_result <<= 1; + + if( next_bit == mSettings->Recessive() ) // normally bit high. + crc_result ^= 0x4599; + } + + return crc_result & 0x7FFF; } void CanSimulationDataGenerator::WriteFrame( bool error ) { - U32 recessive_count = 0; - U32 dominant_count = 0; - - //The frame segments START OF FRAME, ARBITRATION FIELD, CONTROL FIELD, - //DATA FIELD and CRC SEQUENCE are coded by the method of bit stuffing. Whenever - //a transmitter detects five consecutive bits of identical value in the bit stream to be - //transmitted it automatically inserts a complementary bit in the actual transmitted bit - //stream. - - //The remaining bit fields of the DATA FRAME or REMOTE FRAME (CRC DELIMITER, - //ACK FIELD, and END OF FRAME) are of fixed form and not stuffed. The ERROR - //FRAME and the OVERLOAD FRAME are of fixed form as well and not coded by the - //method of bit stuffing. - - U32 count = mFakeStuffedBits.size(); - - if( error == true ) - count -= 9; - - for( U32 i=0; iRecessive() ) - { - recessive_count++; - dominant_count = 0; - }else - { - dominant_count++; - recessive_count = 0; - } - - mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 1.0 ) ); - mCanSimulationData.TransitionIfNeeded( bit ); - } - - if( error == true ) - { - if( mCanSimulationData.GetCurrentBitState() != mSettings->Dominant() ) - { - mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 1.0 ) ); - mCanSimulationData.Transition(); //to DOMINANT - } - - mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 8.0 ) ); - - mCanSimulationData.Transition(); //to DOMINANT - - return; - } - - count = mFakeFixedFormBits.size(); - - for( U32 i=0; iRecessive() ) + { + recessive_count++; + dominant_count = 0; + } + else + { + dominant_count++; + recessive_count = 0; + } + + mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 1.0 ) ); + mCanSimulationData.TransitionIfNeeded( bit ); + } + + if( error == true ) + { + if( mCanSimulationData.GetCurrentBitState() != mSettings->Dominant() ) + { + mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 1.0 ) ); + mCanSimulationData.Transition(); // to DOMINANT + } + + mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 8.0 ) ); + + mCanSimulationData.Transition(); // to DOMINANT + + return; + } + + count = mFakeFixedFormBits.size(); + + for( U32 i = 0; i < count; i++ ) + { + mCanSimulationData.Advance( mClockGenerator.AdvanceByHalfPeriod( 1.0 ) ); + mCanSimulationData.TransitionIfNeeded( mFakeFixedFormBits[ i ] ); + } } - - diff --git a/src/CanSimulationDataGenerator.h b/src/CanSimulationDataGenerator.h index 4f02a92..2ad8ecb 100644 --- a/src/CanSimulationDataGenerator.h +++ b/src/CanSimulationDataGenerator.h @@ -7,40 +7,40 @@ class CanAnalyzerSettings; class CanSimulationDataGenerator { -public: - CanSimulationDataGenerator(); - ~CanSimulationDataGenerator(); - - void Initialize( U32 simulation_sample_rate, CanAnalyzerSettings* settings ); - U32 GenerateSimulationData( U64 newest_sample_requested, U32 sample_rate, SimulationChannelDescriptor** simulation_channels ); - -protected: - CanAnalyzerSettings* mSettings; - U32 mSimulationSampleRateHz; - - - -protected: //functions - U16 ComputeCrc( std::vector& bits, U32 num_bits ); - void AddCrc(); - void CreateDataOrRemoteFrame( U32 identifier, bool use_extended_frame_format, bool remote_frame, std::vector& data, bool get_ack_in_response ); - void WriteFrame( bool error = false ); - -protected: //vars - ClockGenerator mClockGenerator; - SimulationChannelDescriptor mCanSimulationData; //if we had more than one channel to simulate, they would need to be in an array - - U8 mValue; - - std::vector mFakeStartOfFrameField; - std::vector mFakeArbitrationField; - std::vector mFakeControlField; - std::vector mFakeDataField; - std::vector mFakeCrcFieldWithoutDelimiter; - std::vector mFakeAckField; - std::vector mFakeEndOfFrame; - std::vector mFakeStuffedBits; - std::vector mFakeFixedFormBits; + public: + CanSimulationDataGenerator(); + ~CanSimulationDataGenerator(); + + void Initialize( U32 simulation_sample_rate, CanAnalyzerSettings* settings ); + U32 GenerateSimulationData( U64 newest_sample_requested, U32 sample_rate, SimulationChannelDescriptor** simulation_channels ); + + protected: + CanAnalyzerSettings* mSettings; + U32 mSimulationSampleRateHz; + + + protected: // functions + U16 ComputeCrc( std::vector& bits, U32 num_bits ); + void AddCrc(); + void CreateDataOrRemoteFrame( U32 identifier, bool use_extended_frame_format, bool remote_frame, std::vector& data, + bool get_ack_in_response ); + void WriteFrame( bool error = false ); + + protected: // vars + ClockGenerator mClockGenerator; + SimulationChannelDescriptor mCanSimulationData; // if we had more than one channel to simulate, they would need to be in an array + + U8 mValue; + + std::vector mFakeStartOfFrameField; + std::vector mFakeArbitrationField; + std::vector mFakeControlField; + std::vector mFakeDataField; + std::vector mFakeCrcFieldWithoutDelimiter; + std::vector mFakeAckField; + std::vector mFakeEndOfFrame; + std::vector mFakeStuffedBits; + std::vector mFakeFixedFormBits; }; -#endif //CAN_SIMULATION_DATA_GENERATOR +#endif // CAN_SIMULATION_DATA_GENERATOR