Skip to content

Commit

Permalink
#52 WIP - unresolved: re-enabling an RSP device after disable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Sheirer committed Dec 31, 2022
1 parent fdca7ba commit 4df376a
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
import com.github.dsheirer.sdrplay.parameter.composite.CompositeParameters;
import com.github.dsheirer.sdrplay.parameter.tuner.IfMode;
import com.github.dsheirer.sdrplay.parameter.tuner.LoMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.foreign.MemoryAddress;
import java.lang.foreign.MemorySegment;
import java.util.Queue;
Expand All @@ -43,6 +40,8 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Abstract device structure (sdrplay_api_DeviceT)
Expand Down Expand Up @@ -99,7 +98,7 @@ public IStreamCallbackListener getStreamCallbackListener()
*/
private void loadDeviceParameters() throws SDRPlayException
{
if(selected() && !hasCompositeParameters())
if(selected())
{
mCompositeParameters = (T)getAPI().getCompositeParameters(Device.this, getDeviceHandle());
}
Expand Down Expand Up @@ -381,25 +380,15 @@ MemoryAddress getDeviceHandle() throws SDRPlayException
/**
* Composite parameters for this device
*/
//TODO: change back to package private
public T getCompositeParameters()
{
return mCompositeParameters;
}

/**
* Sets the composite parameters
* @param compositeParameters to apply
*/
public void setCompositeParameters(T compositeParameters)
{
mCompositeParameters = compositeParameters;
}

/**
* Indicates if this device has composite parameters
*/
boolean hasCompositeParameters()
public boolean hasCompositeParameters()
{
return mCompositeParameters != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public void saveConfigurations()
}
}

/**
* Monitors discovered tuner enabled status and applies configurations or updates disable state of tuners.
* @param discoveredTuner that has a status change.
* @param previous tuner status
* @param current tuner status
*/
@Override
public void tunerStatusUpdated(DiscoveredTuner discoveredTuner, TunerStatus previous, TunerStatus current)
{
Expand All @@ -157,6 +163,23 @@ public void tunerStatusUpdated(DiscoveredTuner discoveredTuner, TunerStatus prev
else if(current == TunerStatus.ENABLED)
{
removeDisabledTuner(discoveredTuner);

if(discoveredTuner.hasTuner())
{
TunerType tunerType = discoveredTuner.getTuner().getTunerType();

if(tunerType != TunerType.RECORDING)
{
TunerConfiguration tunerConfiguration = getTunerConfiguration(tunerType, discoveredTuner.getId());

if(tunerConfiguration != null)
{
discoveredTuner.setTunerConfiguration(tunerConfiguration);
saveConfigurations();
}
}
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ public boolean hasTunerConfiguration()
*/
public void addTunerStatusListener(IDiscoveredTunerStatusListener listener)
{
mListeners.add(listener);
if(!mListeners.contains(listener))
{
mListeners.add(listener);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,20 @@
import io.github.dsheirer.source.tuner.configuration.TunerConfigurationManager;
import io.github.dsheirer.source.tuner.recording.RecordingTunerConfiguration;
import io.github.dsheirer.source.tuner.sdrplay.DiscoveredRspTuner;
import io.github.dsheirer.source.tuner.sdrplay.rspDuo.ControlRspDuoTuner1Master;
import io.github.dsheirer.source.tuner.sdrplay.rspDuo.DiscoveredRspDuoTuner1;
import io.github.dsheirer.source.tuner.sdrplay.rspDuo.MasterSlaveBridge;
import io.github.dsheirer.source.tuner.sdrplay.rspDuo.RspDuoTuner1Controller;
import io.github.dsheirer.source.tuner.sdrplay.rspDuo.RspDuoTuner2Controller;
import io.github.dsheirer.source.tuner.ui.DiscoveredTunerModel;
import io.github.dsheirer.util.ThreadPool;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.usb4java.Context;
Expand All @@ -57,14 +66,6 @@
import org.usb4java.HotplugCallbackHandle;
import org.usb4java.LibUsb;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
* Tuner manager provides access to tuners using USB, recording, sound-card and system-daemon accessible devices. This
* manager also supports hot-plug detection and black-listing of discovered tuners so that they can be used with other
Expand Down Expand Up @@ -295,21 +296,7 @@ private void startAndConfigureTuner(DiscoveredTuner discoveredTuner)
{
mLog.info("Tuner: " + discoveredTuner + " - Added / Starting ...");
//Attempt to start the discovered tuner and determine the tuner type
discoveredTuner.start();

if(discoveredTuner.hasTuner())
{
TunerType tunerType = discoveredTuner.getTuner().getTunerType();

TunerConfiguration tunerConfiguration = mTunerConfigurationManager
.getTunerConfiguration(tunerType, discoveredTuner.getId());

if(tunerConfiguration != null)
{
discoveredTuner.setTunerConfiguration(tunerConfiguration);
mTunerConfigurationManager.saveConfigurations();
}
}
tunerStatusUpdated(discoveredTuner, TunerStatus.DISABLED, TunerStatus.ENABLED);
}

mDiscoveredTunerModel.addDiscoveredTuner(discoveredTuner);
Expand Down Expand Up @@ -495,30 +482,45 @@ private void discoverRecordingTuners()
@Override
public void tunerStatusUpdated(DiscoveredTuner discoveredTuner, TunerStatus previous, TunerStatus current)
{
mTunerConfigurationManager.tunerStatusUpdated(discoveredTuner, previous, current);

if(previous != TunerStatus.ENABLED && current == TunerStatus.ENABLED)
mLog.info("Tuner status updated - previous [" + previous + "] current [" + current + "] tuner: " + discoveredTuner.getId());
if(current == TunerStatus.ENABLED)
{
discoveredTuner.start();
}

if(discoveredTuner.hasTuner())
{
TunerType tunerType = discoveredTuner.getTuner().getTunerType();
//Special handling for RSPduo to auto-update enabled state for slave device when configured for master/slave operation
if(discoveredTuner instanceof DiscoveredRspDuoTuner1 rspDuoTuner1 &&
rspDuoTuner1.getControlRsp() instanceof ControlRspDuoTuner1Master)
{
String id = rspDuoTuner1.getId();
id = id.replace(DiscoveredRspDuoTuner1.RSP_DUO_ID_PREFIX + "1", DiscoveredRspDuoTuner1.RSP_DUO_ID_PREFIX + "2");
DiscoveredTuner rspDuoTuner2 = getDiscoveredTunerModel().getDiscoveredTuner(id);

//Don't fetch or create a configuration for recording tuners
if(tunerType != TunerType.RECORDING)
if(rspDuoTuner2 != null)
{
if(previous == TunerStatus.ENABLED && current == TunerStatus.DISABLED)
{
TunerConfiguration tunerConfiguration = mTunerConfigurationManager
.getTunerConfiguration(tunerType, discoveredTuner.getId());

if(tunerConfiguration != null)
{
mLog.info("Re-Applying tuner configuration ...");
discoveredTuner.setTunerConfiguration(tunerConfiguration);
mTunerConfigurationManager.saveConfigurations();
}
rspDuoTuner2.setEnabled(false);
}
else if(previous == TunerStatus.DISABLED && current == TunerStatus.ENABLED)
{
rspDuoTuner2.setEnabled(true);
rspDuoTuner2.start();
}
}

//Notify tuner configuration manager to apply tuner configurations & update disabled tuner states
mTunerConfigurationManager.tunerStatusUpdated(rspDuoTuner1, previous, current);

if(rspDuoTuner2 != null)
{
mTunerConfigurationManager.tunerStatusUpdated(rspDuoTuner2, previous, current);
}
}
else
{
//Notify tuner configuration manager to apply tuner configuration
mTunerConfigurationManager.tunerStatusUpdated(discoveredTuner, previous, current);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ public DeviceType getDeviceType()
@Override
public String toString()
{
return getDeviceType() + " ID:" + getId();
return getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public long getTunedFrequency() throws SourceException
@Override
public void setTunedFrequency(long frequency) throws SourceException
{
mLog.info("Setting tuned frequency to [" + frequency + "] tuner: " + getClass());
try
{
getControlRsp().setTunedFrequency(frequency);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void start() throws SDRPlayException

if(device instanceof RspDuoDevice rspDuoDevice)
{
mLog.info("*** Starting ControlRspDuo ....");
mDevice = rspDuoDevice;
getDevice().select();

Expand Down Expand Up @@ -201,19 +202,25 @@ public void stopStream()
@Override
public void setSampleRate(RspSampleRate sampleRate) throws SDRPlayException
{
mLog.info("Setting sample rate to: " + sampleRate.name() + " device: " + this.getClass());
if(hasDevice())
{
mSampleRate = sampleRate;

mLog.info("Setting bandwidth: " + sampleRate.getBandwidth().name());
getDevice().getTuner().setBandwidth(sampleRate.getBandwidth());
mLog.info("Setting sample rate: " + sampleRate.getSampleRate());
getDevice().getCompositeParameters().getDeviceParameters().getSamplingFrequency()
.setSampleRate(sampleRate.getSampleRate());
//Only send an update if we're in single-tuner mode ... not sure why we don't have to for dual-tuner mode.
if(getDeviceSelectionMode() != DeviceSelectionMode.MASTER_TUNER_1)
{
mLog.info("Sending update for tuner select [" + getTunerSelect() + "]");
getDevice().update(getTunerSelect(), UpdateReason.DEVICE_SAMPLE_RATE);
}
mLog.info("Setting wideband signal to true");
getControlParameters().getDecimation().setWideBandSignal(true);
mLog.info("Setting decimation to: " + sampleRate.getDecimation());
getDevice().setDecimation(sampleRate.getDecimation());
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ public void start()
}
}

@Override
public String toString()
{
return super.toString() + " Tuner 1";
}

@Override
public String getId()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ public void start()
}
}

@Override
public String toString()
{
return super.toString() + " Tuner 2";
}

@Override
public String getId()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void notifySampleRate(RspSampleRate sampleRate)
}
catch(SDRPlayException se)
{
mLog.error("Error setting sample rate on RSPduo tuner 2 slave");
mLog.error("Error setting sample rate on RSPduo tuner 2 slave", se);
}
}
}
Expand Down
Loading

0 comments on commit 4df376a

Please sign in to comment.