Initial code check in continued

This commit is contained in:
dennis.sheirer
2014-04-26 19:48:24 +00:00
parent 960a6f9952
commit 3dfedcf8d1
474 changed files with 76613 additions and 0 deletions

322
src/bits/BitSetBuffer.java Normal file
View File

@@ -0,0 +1,322 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package bits;
import java.util.BitSet;
public class BitSetBuffer extends BitSet
{
private static final long serialVersionUID = 1L;
/**
* Logical (ie constructed) size of this bitset, despite the actual size of
* the super bitset that this class is based on
*/
private int mSize = 0;
/**
* Pointer to the next fill index location, when adding bits to this bitset
* one at a time.
*/
private int mPointer = 0;
/**
* Bitset that buffers bits added one at a time, up to the size of the this
* bitset.
*
* Note: the super class bitset behind this class may have a size larger
* that the size parameter specified.
* @param size
*/
public BitSetBuffer( int size )
{
super( size );
mSize = size;
}
/**
* Constructs a bitset buffer and preloads it with the bits contained in
* the bitsToPreload parameter. If the bitsToPreload are longer than the
* size of the bitset, only those bits that fit will be preloaded
*
* @param size
* @param bitsToPreload
*/
public BitSetBuffer( int size, boolean[] bitsToPreload )
{
this( size );
int pointer = 0;
while( !this.isFull() && pointer < bitsToPreload.length )
{
try
{
this.add( bitsToPreload[ pointer ] );
}
catch( BitSetFullException e )
{
e.printStackTrace();
}
pointer++;
}
}
/**
* Constructs a new BitSetBuffer from an existing one
*/
private BitSetBuffer( BitSetBuffer toCopyFrom )
{
this( toCopyFrom.size() );
this.or( toCopyFrom );
this.mPointer = toCopyFrom.pointer();
}
/**
* Current pointer index
*/
public int pointer()
{
return mPointer;
}
/**
* Static method to construct a new BitSetBuffer, preloaded with the bits
* from the preload parameter, and then filled with the bits from the
* second bitsetbuffer parameter.
*
* @param preloadBits - boolean array of bits to be prepended to the new
* bitset
* @param bitsetToAppend - full bitset to be appended to the residual bits array
* @return - new Bitset preloaded with residual bits and new bitset
*/
public static BitSetBuffer merge( boolean[] preloadBits, BitSetBuffer bitsetToAppend )
{
BitSetBuffer returnValue = new BitSetBuffer( preloadBits.length + bitsetToAppend.size(), preloadBits );
int pointer = 0;
while( pointer < bitsetToAppend.size() && !returnValue.isFull() )
{
try
{
returnValue.add( bitsetToAppend.get( pointer ) );
}
catch( BitSetFullException e )
{
e.printStackTrace();
}
pointer++;
}
return returnValue;
}
/**
* Returns a (new) copy of this bitsetbuffer
* @return
*/
public BitSetBuffer copy()
{
return new BitSetBuffer( this );
}
public boolean isFull()
{
return mPointer >= mSize;
}
/**
* Overrides the in-build size() method of the bitset and returns the value
* specified at instantiation. The actual bitset size may be larger than
* this value, and that size is managed by the super class.
*/
@Override
public int size()
{
return mSize;
}
/**
* Clears (sets to false or 0) the bits in this bitset and resets the
* pointer to zero.
*/
@Override
public void clear()
{
this.clear( 0, mSize );
mPointer = 0;
}
/**
* Adds a the bit parameters to this bitset, placing it in the index
* specified by mPointer, and incrementing mPointer to prepare for the next
* call to this method
* @param value
* @throws BitSetFullException - if the size specified at construction is
* exceeded. Invoke full() to determine if the bitset is full either before
* adding a new bit, or after adding a bit.
*/
public void add( boolean value ) throws BitSetFullException
{
if( !isFull() )
{
this.set( mPointer++, value );
}
else
{
throw new BitSetFullException( "bitset is full -- contains " + ( mPointer + 1 ) + " bits" );
}
}
public String toString()
{
StringBuilder sb = new StringBuilder();
for( int x = 0; x < mSize; x++ )
{
sb.append( ( this.get( x ) ? "1" : "0" ) );
}
return sb.toString();
}
/**
* Returns a boolean array from startIndex to end of the bitset
*/
public boolean[] getBits( int startIndex )
{
return getBits( startIndex, mSize - 1 );
}
/**
* Returns a boolean array of the right-most bitCount number of bits
*/
public boolean[] right( int bitCount )
{
return getBits( mSize - bitCount - 1 );
}
/**
* Returns a boolean array representing the bits located from startIndex
* through endIndex
*/
public boolean[] getBits( int startIndex, int endIndex )
{
boolean[] returnValue = null;
if( startIndex >= 0 &&
startIndex < endIndex &&
endIndex < mSize )
{
returnValue = new boolean[ endIndex - startIndex + 1 ];
int bitsetPointer = startIndex;
int returnPointer = 0;
while( bitsetPointer <= endIndex )
{
returnValue[ returnPointer ] = this.get( bitsetPointer );
bitsetPointer++;
returnPointer++;
}
}
return returnValue;
}
/**
* Returns the integer value represented by the bit array
* @param bits - an array of bit positions that will be treated as if they
* were contiguous bits, with index 0 being the MSB and index
* length - 1 being the LSB
* @return - integer value of the bit array
*/
public int getInt( int[] bits )
{
int retVal = 0;
for( int x = 0; x < bits.length; x++ )
{
if( get( bits[ x ] ) )
{
retVal += 1<<( bits.length - 1 - x );
}
}
return retVal;
}
public String getHex( int[] bits, int digitDisplayCount )
{
int value = getInt( bits );
return String.format( "%0" + digitDisplayCount + "X", value );
}
/**
* Returns the integer value represented by the bit range
* @param start - MSB of the integer
* @param end - LSB of the integer
* @return - integer value of the bit range
*/
public int getInt( int start, int end )
{
if( end - start > 32 )
{
throw new IllegalArgumentException( "BitSetBuffer - requested bit "
+ "length [" + ( end - start ) + "] is larger than an "
+ "integer (32 bits)" );
}
int retVal = 0;
for( int x = start; x <= end; x++ )
{
if( get( x ) )
{
retVal += 1<<( end - x );
}
}
return retVal;
}
/**
* Returns the long value represented by the bit range
* @param start - MSB of the long
* @param end - LSB of the long
* @return - long value of the bit range
*/
public long getLong( int start, int end )
{
long retVal = 0;
for( int x = start; x <= end; x++ )
{
if( get( x ) )
{
retVal += 1<<( end - x );
}
}
return retVal;
}
}

View File

@@ -0,0 +1,28 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package bits;
public class BitSetFullException extends Exception
{
private static final long serialVersionUID = 1L;
public BitSetFullException( String text )
{
super( text );
}
}

232
src/bits/MessageFramer.java Normal file
View File

@@ -0,0 +1,232 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package bits;
import java.util.ArrayList;
import java.util.Iterator;
import sample.Broadcaster;
import sample.Listener;
import dsp.SyncDetectListener;
import dsp.SyncDetectProvider;
/**
* MessageFramer - processes bitsets looking for a sync pattern within
* the bits, and then extracts the message, including the sync
* pattern, for a total bit length of messageLength.
*
* Will extract multiple messages simultaneously, for each sync pattern that is
* encountered within the bitset bit stream.
*/
public class MessageFramer implements Listener<Boolean>,
SyncDetectProvider
{
private boolean[] mSyncPattern;
private int mMessageLength;
private SyncDetectListener mSyncDetectListener;
private Broadcaster<BitSetBuffer> mBroadcaster =
new Broadcaster<BitSetBuffer>();
private ArrayList<MessageAssembler> mMessageAssemblers =
new ArrayList<MessageAssembler>();
private ArrayList<MessageAssembler> mCompletedMessageAssemblers =
new ArrayList<MessageAssembler>();
private BitSetBuffer mPreviousBuffer = null;
private SyncPatternMatcher mMatcher;
public MessageFramer( boolean[] syncPattern, int messageLength )
{
mSyncPattern = syncPattern;
mMatcher = new SyncPatternMatcher( syncPattern );
mMessageLength = messageLength;
}
public void dispose()
{
mBroadcaster.dispose();
mCompletedMessageAssemblers.clear();
mMessageAssemblers.clear();
}
@Override
public void receive( Boolean bit )
{
mMatcher.receive( bit );
Iterator<MessageAssembler> it = mMessageAssemblers.iterator();
MessageAssembler assembler;
while( it.hasNext() )
{
assembler = it.next();
/* Dispose and remove any completed assemblers */
if( assembler.complete() )
{
assembler.dispose();
it.remove();
}
/* Otherwise, send them the bit */
else
{
assembler.receive( bit );
}
}
/* Check for sync match and add new message assembler */
if( mMatcher.matches() )
{
addMessageAssembler( new MessageAssembler( mMessageLength, mSyncPattern ) );
/* Notify any sync detect listener(s) */
if( mSyncDetectListener != null )
{
mSyncDetectListener.syncDetected();
}
}
}
/**
* Causes all messages currently under assembly to be forcibly
* sent (ie flushed) to all registered message listeners, and
* subsequently, all assemblers to be deleted
*/
public void flush()
{
for( MessageAssembler assembler: mMessageAssemblers )
{
assembler.flush();
}
}
@Override
public void setSyncDetectListener( SyncDetectListener listener )
{
mSyncDetectListener = listener;
}
/**
* Allow a message listener to register with this framer to receive
* all framed messages
*/
public void addMessageListener( Listener<BitSetBuffer> listener )
{
mBroadcaster.addListener( listener );
}
public void removeMessageListener( Listener<BitSetBuffer> listener )
{
mBroadcaster.removeListener( listener );
}
/*
* Adds a message assembler to receive bits from the bit stream
*/
private void addMessageAssembler( MessageAssembler assembler )
{
mMessageAssemblers.add( assembler );
}
private void removeMessageAssembler( MessageAssembler assembler )
{
mMessageAssemblers.remove( assembler );
}
/**
* Assembles a binary message, starting with the initial fill of the
* identified sync pattern, and every bit thereafter. Once the accumulated
* bits equal the message length, the message is sent and the assembler
* flags itself as complete.
*
* By design, multiple message assemblers can exist at the same time, each
* assembling different, overlapping potential messages
*/
private class MessageAssembler implements Listener<Boolean>
{
BitSetBuffer mMessage;
boolean mComplete = false;
MessageAssembler( int messageLength )
{
mMessage = new BitSetBuffer( messageLength );
}
MessageAssembler( int messageLength, boolean[] initialFill )
{
this( messageLength );
/* Pre-load the message with the sync pattern */
for( int x = 0; x < initialFill.length; x++ )
{
receive( initialFill[ x ] );
}
}
public void dispose()
{
mMessage = null;
}
@Override
/**
* Receives one bit at a time, and assembles them into a message
*/
public void receive( Boolean bit )
{
try
{
mMessage.add( bit );
}
catch( BitSetFullException e )
{
e.printStackTrace();
}
/* Once our message is complete (ie full), send it to all registered
* message listeners, and set complete flag so for auto-removal */
if( mMessage.isFull() )
{
mComplete = true;
flush();
}
}
/**
* Flushes/Sends the current message, or partial message, and sets
* complete flag to true, so that we can be auto-removed
*/
public void flush()
{
mBroadcaster.receive( mMessage );
mComplete = true;
}
/**
* Flag to indicate when this assembler has received all of the bits it
* is looking for (ie message length), and should then be removed from
* receiving any more bits
*/
public boolean complete()
{
return mComplete;
}
}
}

118
src/bits/SyncPattern.java Normal file
View File

@@ -0,0 +1,118 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package bits;
public enum SyncPattern
{
/* Revs (0xAA) and Sync (0x7650) = 01010 0111011001010000 */
FLEETSYNC1( new boolean[]
{
false, true, false, true, false, //end of revs
false, true, true, true, //0111 0x7
false, true, true, false, //0110 0x6
false, true, false, true, //0101 0x5
false, false, false, false //0000 0x0
} ),
/* Revs (0x0A) and Sync (0x23EB) = 10101 0010001111101011 */
FLEETSYNC2( new boolean[]
{
false, true, false, true, false, //End of bit revs
false, false, true, false, //0010 0x2
false, false, true, true, //0011 0x3
true, true, true, false, //1110 0xE
true, false, true, true //1011 0xB
} ),
/**
* Note: we use a truncated portion of the NRZ-I encoded sync pattern.
* The truncated version seems to work well enough for detecting the burst,
* while limiting falsing.
*
* Sync: .... .... 0000 0111 0000 1001 0010 1010 0100 0100 0110 1111
* Rev & Sync: ...1 0101 0000 0111 0000 1001 0010 1010 0100 0100 0110 1111
* NRZ-I: .... 1111 1000 0100 1000 1101 1011 1111 0110 0110 0101 1000
* This: .... .... 1000 0100 1000 1101 1011 1111 .... .... .... ....
*/
/* Sync (0x07092A446F) = 0000011100001001001010100100010001101111 */
MDC1200( new boolean[]
{
false, false, false, false,//0000 0x0
false, true, true, true, //0111 0x7
false, false, false, false,//0000 0x0
true, false, false, true, //1001 0x9
false, false, true, false, //0010 0x2
true, false, true, false, //1010 0xA
false, true, false, false, //0100 0x4
false, true, false, false, //0100 0x4
false, true, true, false, //0110 0x6
true, true, true, true //1111 0xF
} ),
/* Revs (0xA) and Sync(0xA4D7) = 1010 1100010011010111 */
MPT1327_CONTROL( new boolean[]
{
true, false, true, false, //Includes 4 of 16 rev bits
// false, true, false, true, //Includes 4 of 16 rev bits
true, true, false, false, //1100 0xA
false, true, false, false, //0100 0x4
true, true, false, true, //1101 0xD
false, true, true, true //0111 0x7
}),
/* Revs (0xA) and Sync(0x3B28) = 1010 0011101100101000 */
MPT1327_TRAFFIC( new boolean[]
{
true, false, true, false, //1010 Includes 4 of 16 rev bits
// false, true, false, true, //1010 Includes 4 of 16 rev bits
false, false, true, true, //0011 0x3
true, false, true, true, //1011 0xB
false, false, true, false, //0010 0x2
true, false, false, false //1000 0x8
}),
/* Sync (0x158) = 1101011000 */
PASSPORT( new boolean[]
{
true, //0001 0x1
false, true, false, true, //0101 0x5
true, false, false, false //1000 0x8
} ),
LTR_STANDARD_OSW( new boolean[]
{
true,false,true,false,true,true,false,false,false
} ),
LTR_STANDARD_ISW( new boolean[]
{
false, true, false, true,false, false, true, true, true
} );
boolean[] mBits;
private SyncPattern( boolean[] bits )
{
mBits = bits;
}
public boolean[] getPattern()
{
return mBits;
}
}

View File

@@ -0,0 +1,74 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package bits;
/**
* Sync pattern matcher.
*
* Note: works for sync patterns up to 63 bits (integer size - 1 ) long.
* If a 64 bit or larger sync pattern is applied, then the wrapping feature
* of the Integer.rotate methods will wrap the MSB around to the LSB and
* corrupt the matching value.
*
*/
public class SyncPatternMatcher
{
private long mBits = 0;
private long mMask = 0;
private long mSync = 0;
public SyncPatternMatcher( boolean[] syncPattern )
{
//Setup a bit mask of all ones the length of the sync pattern
mMask = (long)( ( Math.pow( 2, syncPattern.length ) ) - 1 );
//Convert the sync bits into a long value for comparison
for( int x = 0; x < syncPattern.length; x++ )
{
if( syncPattern[ x ] )
{
mSync += 1l << ( syncPattern.length - 1 - x );
}
}
}
public void receive( boolean bit )
{
//Left shift the previous value
mBits = Long.rotateLeft( mBits, 1 );
//Apply the mask to erase the previous MSB
mBits &= mMask;
//Add in the new bit
if( bit )
{
mBits += 1;
}
}
/**
* Indicates if the most recently received bit sequence matches the
* sync pattern
*/
public boolean matches()
{
return ( mBits == mSync );
}
}