Skip to content

Commit 5004a7b

Browse files
author
Dennis Sheirer
committed
#1955 WIP - final changes to SDRPlay api native library loading.
1 parent 11cbba2 commit 5004a7b

File tree

6 files changed

+74
-17
lines changed

6 files changed

+74
-17
lines changed

src/main/java/io/github/dsheirer/gui/SDRTrunk.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import io.github.dsheirer.source.tuner.TunerEvent;
5656
import io.github.dsheirer.source.tuner.manager.DiscoveredTuner;
5757
import io.github.dsheirer.source.tuner.manager.TunerManager;
58+
import io.github.dsheirer.source.tuner.sdrplay.api.SDRPlayLibraryHelper;
5859
import io.github.dsheirer.source.tuner.ui.TunerSpectralDisplayManager;
5960
import io.github.dsheirer.spectrum.DisableSpectrumWaterfallMenuItem;
6061
import io.github.dsheirer.spectrum.ShowTunerMenuItem;
@@ -152,6 +153,14 @@ public SDRTrunk()
152153
mApplicationLog = new ApplicationLog(mUserPreferences);
153154
mApplicationLog.start();
154155

156+
//Note: invoke this early in the application lifecycle, before the TunerManager causes the sdrplay classes
157+
//to be loaded since the jextract auto-generated code attempts to load the library by name and that can fail
158+
//when the library was not installed into a normal/default location, particularly on windows OS systems.
159+
if(SDRPlayLibraryHelper.LOADED)
160+
{
161+
mLog.info("SDRPlay API native library preemptively loaded");
162+
}
163+
155164
mResourceMonitor = new ResourceMonitor(mUserPreferences);
156165

157166
String operatingSystem = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);

src/main/java/io/github/dsheirer/source/tuner/sdrplay/api/README

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,24 @@ Version 3.15 - Adds support for RSP dx2
4040

4141
As a final step, delete the original api version branches and rename each of the new api version branches to remove the _1 suffix.
4242

43+
Code Modifications: for each version, modify the sdrplay_api_h.java class to wrap the SYMBOL_LOOKUP variable using the
44+
following changes, so that the code doesn't throw an error on systems that don't have the API installed:
45+
//jextract auto-generated code modified to wrap with exception handler for systems that don't have the library
46+
static SymbolLookup SYMBOL_LOOKUP;
47+
static
48+
{
49+
try
50+
{
51+
SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("sdrplay_api"), LIBRARY_ARENA)
52+
.or(SymbolLookup.loaderLookup())
53+
.or(Linker.nativeLinker().defaultLookup());
54+
}
55+
catch(Exception e)
56+
{
57+
SYMBOL_LOOKUP = null;
58+
}
59+
}
60+
4361
IntelliJ setup
4462
1. Run configuration
4563
2. JVM Option: --enable-native-access=ALL-UNNAMED
46-
3. Add library location to the java.library.path:
47-
Linux: /usr/local/lib
48-
Windows: ??
49-
MacOS: ??

src/main/java/io/github/dsheirer/source/tuner/sdrplay/api/SDRPlayLibraryHelper.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public static boolean load()
6161
catch(Throwable t)
6262
{
6363
String libraryPath = getSDRplayLibraryPath();
64-
mLog.info("Checking: " + libraryPath);
6564
Path path = Path.of(libraryPath);
6665
boolean exists = Files.exists(path);
6766

@@ -74,13 +73,12 @@ public static boolean load()
7473
}
7574
catch(Throwable t2)
7675
{
77-
String name = System.mapLibraryName(SDRPLAY_API_LIBRARY_NAME);
78-
mLog.warn("SDRPlay - unable to load API library: " + libraryPath);
76+
mLog.warn("Unable to load SDRPlay API native library: " + libraryPath);
7977
}
8078
}
8179
else
8280
{
83-
mLog.info("SDRPlay API library not found at: " + libraryPath);
81+
mLog.info("SDRPlay API native library not found at: " + libraryPath);
8482
}
8583
}
8684

src/main/java/io/github/dsheirer/source/tuner/sdrplay/api/v3_07/sdrplay_api_h.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,21 @@ static MemoryLayout align(MemoryLayout layout, long align) {
8383
};
8484
}
8585

86-
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("sdrplay_api"), LIBRARY_ARENA)
87-
.or(SymbolLookup.loaderLookup())
88-
.or(Linker.nativeLinker().defaultLookup());
86+
//jextract auto-generated code modified to wrap with exception handler for systems that don't have the library
87+
static SymbolLookup SYMBOL_LOOKUP;
88+
static
89+
{
90+
try
91+
{
92+
SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("sdrplay_api"), LIBRARY_ARENA)
93+
.or(SymbolLookup.loaderLookup())
94+
.or(Linker.nativeLinker().defaultLookup());
95+
}
96+
catch(Exception e)
97+
{
98+
SYMBOL_LOOKUP = null;
99+
}
100+
}
89101

90102
public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
91103
public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;

src/main/java/io/github/dsheirer/source/tuner/sdrplay/api/v3_08/sdrplay_api_h.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,21 @@ static MemoryLayout align(MemoryLayout layout, long align) {
8383
};
8484
}
8585

86-
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("sdrplay_api"), LIBRARY_ARENA)
87-
.or(SymbolLookup.loaderLookup())
88-
.or(Linker.nativeLinker().defaultLookup());
86+
//jextract auto-generated code modified to wrap with exception handler for systems that don't have the library
87+
static SymbolLookup SYMBOL_LOOKUP;
88+
static
89+
{
90+
try
91+
{
92+
SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("sdrplay_api"), LIBRARY_ARENA)
93+
.or(SymbolLookup.loaderLookup())
94+
.or(Linker.nativeLinker().defaultLookup());
95+
}
96+
catch(Exception e)
97+
{
98+
SYMBOL_LOOKUP = null;
99+
}
100+
}
89101

90102
public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
91103
public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;

src/main/java/io/github/dsheirer/source/tuner/sdrplay/api/v3_15/sdrplay_api_h.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,21 @@ static MemoryLayout align(MemoryLayout layout, long align) {
8383
};
8484
}
8585

86-
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("sdrplay_api"), LIBRARY_ARENA)
87-
.or(SymbolLookup.loaderLookup())
88-
.or(Linker.nativeLinker().defaultLookup());
86+
//jextract auto-generated code modified to wrap with exception handler for systems that don't have the library
87+
static SymbolLookup SYMBOL_LOOKUP;
88+
static
89+
{
90+
try
91+
{
92+
SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("sdrplay_api"), LIBRARY_ARENA)
93+
.or(SymbolLookup.loaderLookup())
94+
.or(Linker.nativeLinker().defaultLookup());
95+
}
96+
catch(Exception e)
97+
{
98+
SYMBOL_LOOKUP = null;
99+
}
100+
}
89101

90102
public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
91103
public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;

0 commit comments

Comments
 (0)