From 4df376a76360e85db93cc186b00e32544cfa8a12 Mon Sep 17 00:00:00 2001 From: Dennis Sheirer Date: Sat, 31 Dec 2022 05:22:47 -0500 Subject: [PATCH] #52 WIP - unresolved: re-enabling an RSP device after disable. --- .../dsheirer/sdrplay/device/Device.java | 19 +--- .../TunerConfigurationManager.java | 23 +++++ .../source/tuner/manager/DiscoveredTuner.java | 5 +- .../source/tuner/manager/TunerManager.java | 82 ++++++++--------- .../tuner/sdrplay/DiscoveredRspTuner.java | 2 +- .../tuner/sdrplay/RspTunerController.java | 1 + .../tuner/sdrplay/rspDuo/ControlRspDuo.java | 7 ++ .../rspDuo/DiscoveredRspDuoTuner1.java | 6 -- .../rspDuo/DiscoveredRspDuoTuner2.java | 6 -- .../sdrplay/rspDuo/MasterSlaveBridge.java | 2 +- .../source/tuner/ui/DiscoveredTunerModel.java | 87 ++----------------- 11 files changed, 91 insertions(+), 149 deletions(-) diff --git a/jsdrplay/src/main/java/com/github/dsheirer/sdrplay/device/Device.java b/jsdrplay/src/main/java/com/github/dsheirer/sdrplay/device/Device.java index c3ec965c3..e725037d9 100644 --- a/jsdrplay/src/main/java/com/github/dsheirer/sdrplay/device/Device.java +++ b/jsdrplay/src/main/java/com/github/dsheirer/sdrplay/device/Device.java @@ -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; @@ -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) @@ -99,7 +98,7 @@ public IStreamCallbackListener getStreamCallbackListener() */ private void loadDeviceParameters() throws SDRPlayException { - if(selected() && !hasCompositeParameters()) + if(selected()) { mCompositeParameters = (T)getAPI().getCompositeParameters(Device.this, getDeviceHandle()); } @@ -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; } diff --git a/src/main/java/io/github/dsheirer/source/tuner/configuration/TunerConfigurationManager.java b/src/main/java/io/github/dsheirer/source/tuner/configuration/TunerConfigurationManager.java index cd61a9fa9..069d60c0c 100644 --- a/src/main/java/io/github/dsheirer/source/tuner/configuration/TunerConfigurationManager.java +++ b/src/main/java/io/github/dsheirer/source/tuner/configuration/TunerConfigurationManager.java @@ -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) { @@ -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(); + } + } + } + } } diff --git a/src/main/java/io/github/dsheirer/source/tuner/manager/DiscoveredTuner.java b/src/main/java/io/github/dsheirer/source/tuner/manager/DiscoveredTuner.java index dfd577f7a..ff7d63f6b 100644 --- a/src/main/java/io/github/dsheirer/source/tuner/manager/DiscoveredTuner.java +++ b/src/main/java/io/github/dsheirer/source/tuner/manager/DiscoveredTuner.java @@ -198,7 +198,10 @@ public boolean hasTunerConfiguration() */ public void addTunerStatusListener(IDiscoveredTunerStatusListener listener) { - mListeners.add(listener); + if(!mListeners.contains(listener)) + { + mListeners.add(listener); + } } /** diff --git a/src/main/java/io/github/dsheirer/source/tuner/manager/TunerManager.java b/src/main/java/io/github/dsheirer/source/tuner/manager/TunerManager.java index d5163f197..224ea58b9 100644 --- a/src/main/java/io/github/dsheirer/source/tuner/manager/TunerManager.java +++ b/src/main/java/io/github/dsheirer/source/tuner/manager/TunerManager.java @@ -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; @@ -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 @@ -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); @@ -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); } } diff --git a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/DiscoveredRspTuner.java b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/DiscoveredRspTuner.java index 8f04cc452..6c82ca8e0 100644 --- a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/DiscoveredRspTuner.java +++ b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/DiscoveredRspTuner.java @@ -75,6 +75,6 @@ public DeviceType getDeviceType() @Override public String toString() { - return getDeviceType() + " ID:" + getId(); + return getId(); } } diff --git a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/RspTunerController.java b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/RspTunerController.java index f56a63a25..670c04a1d 100644 --- a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/RspTunerController.java +++ b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/RspTunerController.java @@ -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); diff --git a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/ControlRspDuo.java b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/ControlRspDuo.java index 268fdc100..7b73a1a67 100644 --- a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/ControlRspDuo.java +++ b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/ControlRspDuo.java @@ -105,6 +105,7 @@ public void start() throws SDRPlayException if(device instanceof RspDuoDevice rspDuoDevice) { + mLog.info("*** Starting ControlRspDuo ...."); mDevice = rspDuoDevice; getDevice().select(); @@ -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 diff --git a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/DiscoveredRspDuoTuner1.java b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/DiscoveredRspDuoTuner1.java index 5c29ff804..d4e9bdb87 100644 --- a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/DiscoveredRspDuoTuner1.java +++ b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/DiscoveredRspDuoTuner1.java @@ -63,12 +63,6 @@ public void start() } } - @Override - public String toString() - { - return super.toString() + " Tuner 1"; - } - @Override public String getId() { diff --git a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/DiscoveredRspDuoTuner2.java b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/DiscoveredRspDuoTuner2.java index 4e1381282..f9809c2d3 100644 --- a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/DiscoveredRspDuoTuner2.java +++ b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/DiscoveredRspDuoTuner2.java @@ -61,12 +61,6 @@ public void start() } } - @Override - public String toString() - { - return super.toString() + " Tuner 2"; - } - @Override public String getId() { diff --git a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/MasterSlaveBridge.java b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/MasterSlaveBridge.java index 625ae22af..a94523ad8 100644 --- a/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/MasterSlaveBridge.java +++ b/src/main/java/io/github/dsheirer/source/tuner/sdrplay/rspDuo/MasterSlaveBridge.java @@ -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); } } } diff --git a/src/main/java/io/github/dsheirer/source/tuner/ui/DiscoveredTunerModel.java b/src/main/java/io/github/dsheirer/source/tuner/ui/DiscoveredTunerModel.java index 07277ed98..92be04822 100644 --- a/src/main/java/io/github/dsheirer/source/tuner/ui/DiscoveredTunerModel.java +++ b/src/main/java/io/github/dsheirer/source/tuner/ui/DiscoveredTunerModel.java @@ -25,12 +25,11 @@ import io.github.dsheirer.source.tuner.manager.DiscoveredUSBTuner; import io.github.dsheirer.source.tuner.manager.IDiscoveredTunerStatusListener; import io.github.dsheirer.source.tuner.manager.TunerStatus; -import io.github.dsheirer.source.tuner.sdrplay.rspDuo.ControlRspDuoTuner1Master; -import io.github.dsheirer.source.tuner.sdrplay.rspDuo.DiscoveredRspDuoTuner1; import java.awt.EventQueue; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -137,7 +136,7 @@ public DiscoveredTuner getDiscoveredTuner(int index) * @param id of the tuner to search for * @return discovered tuner with matching ID or null. */ - private DiscoveredTuner getDiscoveredTuner(String id) + public DiscoveredTuner getDiscoveredTuner(String id) { DiscoveredTuner discoveredTuner = null; @@ -145,7 +144,12 @@ private DiscoveredTuner getDiscoveredTuner(String id) try { - discoveredTuner = mDiscoveredTuners.stream().filter(tuner -> tuner.getId().equals(id)).findFirst().get(); + Optional result = mDiscoveredTuners.stream().filter(tuner -> tuner.getId().equals(id)).findFirst(); + + if(result.isPresent()) + { + discoveredTuner = result.get(); + } } finally { @@ -338,28 +342,6 @@ public void tunerStatusUpdated(DiscoveredTuner discoveredTuner, TunerStatus prev int row = mDiscoveredTuners.indexOf(discoveredTuner); EventQueue.invokeLater(() -> fireTableRowsUpdated(row, row)); - - //Special handling for RSPduo to auto-enable slave device when configured for master/slave operation - if(discoveredTuner instanceof DiscoveredRspDuoTuner1 drdt1 && - drdt1.getControlRsp() instanceof ControlRspDuoTuner1Master) - { - String id = drdt1.getId(); - id = id.replace(DiscoveredRspDuoTuner1.RSP_DUO_ID_PREFIX + "1", DiscoveredRspDuoTuner1.RSP_DUO_ID_PREFIX + "2"); - DiscoveredTuner tuner2 = getDiscoveredTuner(id); - - if(tuner2 != null) - { - if(previous == TunerStatus.ENABLED && current == TunerStatus.DISABLED) - { - tuner2.setEnabled(false); - } - else if(previous == TunerStatus.DISABLED && current == TunerStatus.ENABLED) - { - tuner2.setEnabled(true); - } - } - } - } @Override @@ -439,25 +421,6 @@ public Object getValueAt(int rowIndex, int columnIndex) { return ""; } -// case COLUMN_TUNER_ID: -// if(discoveredTuner.hasTuner()) -// { -// return discoveredTuner.getTuner().getUniqueID(); -// } -// else -// { -// return discoveredTuner.getId(); -// } -// case COLUMN_SAMPLE_RATE: -// if(discoveredTuner.hasTuner()) -// { -// double sampleRate = discoveredTuner.getTuner().getTunerController().getSampleRate(); -// return mSampleRateFormat.format(sampleRate / 1E6D) + MHZ; -// } -// else -// { -// return ""; -// } case COLUMN_FREQUENCY: if(discoveredTuner.hasTuner()) { @@ -478,40 +441,6 @@ public Object getValueAt(int rowIndex, int columnIndex) { return ""; } -// case COLUMN_FREQUENCY_ERROR: -// if(discoveredTuner.hasTuner()) -// { -// double ppm = discoveredTuner.getTuner().getTunerController().getFrequencyCorrection(); -// return mFrequencyErrorPPMFormat.format(ppm); -// } -// else -// { -// return ""; -// } -// case COLUMN_MEASURED_FREQUENCY_ERROR: -// if(discoveredTuner.hasTuner()) -// { -// if(discoveredTuner.getTuner().getTunerController().hasMeasuredFrequencyError()) -// { -// StringBuilder sb = new StringBuilder(); -// sb.append(discoveredTuner.getTuner().getTunerController().getMeasuredFrequencyError()); -// sb.append("Hz ("); -// sb.append(mFrequencyErrorPPMFormat.format(discoveredTuner.getTuner().getTunerController().getPPMFrequencyError())); -// sb.append("ppm)"); -// return sb.toString(); -// } -// } -// return ""; -// case COLUMN_ERROR_OR_SPECTRAL_DISPLAY_NEW: -// if(discoveredTuner.hasErrorMessage()) -// { -// return discoveredTuner.getErrorMessage(); -// } -// else if(discoveredTuner.hasTuner()) -// { -// return "New"; -// } -// return ""; default: break; }