Skip to content

Commit 4c11f23

Browse files
committed
Stubs, plugin loading and error handling
1 parent f4e1e47 commit 4c11f23

24 files changed

Lines changed: 447 additions & 51 deletions

JavaGUI/jni/TSDRLibraryNDK.c

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,87 @@
22
#include <stdio.h>
33
#include "TSDRLibraryNDK.h"
44
#include "include\TSDRLibrary.h"
5+
#include "include\TSDRCodes.h"
6+
#include <string.h>
57

6-
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_test(JNIEnv *env, jobject thisObj)
8+
tsdr_lib_t tsdr;
9+
10+
#define THROW(x) error(env, x, "")
11+
12+
void error_translate (int exception_code, char * exceptionclass) {
13+
switch (exception_code) {
14+
case TSDR_OK:
15+
return;
16+
case TSDR_ERR_PLUGIN:
17+
strcpy(exceptionclass, "martin/tempest/core/exceptions/TSDRLoadPluginException");
18+
return;
19+
case TSDR_NOT_IMPLEMENTED:
20+
strcpy(exceptionclass, "martin/tempest/core/exceptions/TSDRFunctionNotImplemented");
21+
return;
22+
default:
23+
strcpy(exceptionclass, "java/lang/Exception");
24+
return;
25+
}
26+
}
27+
28+
void error(JNIEnv * env, int exception_code, const char *inmsg, ...)
729
{
8-
test();
30+
if (exception_code == TSDR_OK) return;
31+
32+
char exceptionclass[256];
33+
error_translate(exception_code, exceptionclass);
34+
35+
JavaVM* jvm;
36+
char msg[256];
37+
38+
va_list argptr;
39+
va_start(argptr, inmsg);
40+
vsprintf(msg, inmsg, argptr);
41+
va_end(argptr);
42+
43+
jclass cls = (*env)->FindClass(env, exceptionclass);
44+
/* if cls is NULL, an exception has already been thrown */
45+
if (cls != NULL) {
46+
(*env)->ThrowNew(env, cls, msg);
47+
}
48+
/* free the local ref */
49+
(*env)->DeleteLocalRef(env, cls);
50+
}
51+
52+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeLoadPlugin (JNIEnv * env, jobject obj, jstring path) {
53+
const char *npath = (*env)->GetStringUTFChars(env, path, 0);
54+
THROW(tsdr_loadplugin(&tsdr, npath));
55+
(*env)->ReleaseStringUTFChars(env, path, npath);
56+
}
57+
58+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_pluginParams (JNIEnv * env, jobject obj, jstring params) {
59+
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "pluginParams");
60+
}
61+
62+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setSampleRate (JNIEnv * env, jobject obj, jlong rate) {
63+
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "setSampleRate");
64+
}
65+
66+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setBaseFreq (JNIEnv * env, jobject obj, jlong freq) {
67+
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "setBaseFreq");
68+
}
69+
70+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_start (JNIEnv * env, jobject obj) {
71+
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "start");
72+
}
73+
74+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_stop (JNIEnv * env, jobject obj) {
75+
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "stop");
76+
}
77+
78+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setGain (JNIEnv * env, jobject obj, jfloat gain) {
79+
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "setGain");
80+
}
81+
82+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_readAsync (JNIEnv * env, jobject obj) {
83+
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "readAsync");
84+
}
85+
86+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_unloadPlugin (JNIEnv * env, jobject obj) {
87+
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "unloadPlugin");
988
}

JavaGUI/jni/TSDRLibraryNDK.h

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaGUI/jni/include/TSDRCodes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef _TSDRCodes
2+
#define _TSDRCodes
3+
4+
#define TSDR_OK (0)
5+
#define TSDR_ERR_PLUGIN (1)
6+
#define TSDR_NOT_IMPLEMENTED (404)
7+
8+
#endif

JavaGUI/jni/include/TSDRLibrary.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef _TSDRLibrary
2+
#define _TSDRLibrary
3+
4+
#include <stdint.h>
5+
6+
struct tsdr_lib {
7+
void * plugin;
8+
} typedef tsdr_lib_t;
9+
10+
typedef void(*tsdr_readasync_function)(unsigned char *buf, uint32_t len, void *ctx);
11+
12+
int tsdr_loadplugin(tsdr_lib_t * tsdr, char * filepath);
13+
int tsdr_pluginparams(tsdr_lib_t * tsdr, char * params);
14+
int tsdr_setsamplerate(tsdr_lib_t * tsdr, uint32_t rate);
15+
int tsdr_setbasefreq(tsdr_lib_t * tsdr, uint32_t freq);
16+
int tsdr_start(tsdr_lib_t * tsdr);
17+
int tsdr_stop(tsdr_lib_t * tsdr);
18+
int tsdr_setgain(tsdr_lib_t * tsdr, float gain);
19+
int tsdr_readasync(tsdr_lib_t * tsdr, tsdr_readasync_function cb, void *ctx, uint32_t buf_num, uint32_t buf_len);
20+
int tsdr_unloadplugin(tsdr_lib_t * tsdr);
21+
22+
#endif

JavaGUI/jni/makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ rebuildtempestsdr : $(OUTPUTFOLDER)
5959
@cp -f ../../TempestSDR/bin/$(OSNAME)/$(ARCHNAME)/TSDRLibrary$(EXT) $(OUTPUTFOLDER)/TSDRLibrary$(EXT)
6060

6161
# Compile the sources to obj files
62-
TSDRLibraryNDK.o : TSDRLibraryNDK.c TSDRLibraryNDK.h include/TSDRLibrary.h
62+
TSDRLibraryNDK.o : TSDRLibraryNDK.c TSDRLibraryNDK.h include/TSDRLibrary.h include/TSDRCodes.h
6363
gcc $(foreach d, $(INC), -I$d) -c $< -o $@
6464

6565
# Copy over a fresh set of library headers
66-
include/TSDRLibrary.h : ../../TempestSDR/src/include/TSDRLibrary.h
66+
include/%.h : ../../TempestSDR/src/include/%.h
6767
mkdir -p include
6868
@cp -f $< $@
6969

JavaGUI/makefile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,9 @@ jnilib : plugins
8181
@$(MAKE) -C jni/ all JAVA_HOME=$(JAVA_HOME)
8282

8383
# Recompile each plugin and copy its shared library over to the java directory
84-
plugins : $(OUTPUTFOLDER)
85-
$(foreach pdir, $(PLUGINS_DIRS), $(MAKE) -C $(pdir) all; cp -f $(pdir)/bin/$(OSNAME)/$(ARCHNAME)/*$(EXT) $(OUTPUTFOLDER)/;)
86-
87-
# Create output dir
88-
$(OUTPUTFOLDER) :
84+
plugins :
8985
mkdir -p $(OUTPUTFOLDER)
86+
$(foreach pdir, $(PLUGINS_DIRS), $(MAKE) -C $(pdir) all; cp -f $(pdir)/bin/$(OSNAME)/$(ARCHNAME)/*$(EXT) $(OUTPUTFOLDER)/;)
9087

9188
# Clean the jni stuff
9289
cleanjni: cleanplugins

JavaGUI/src/martin/tempest/Main.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ public class Main {
77
public static void main(String[] args) {
88
try {
99
TSDRLibrary sdrlib = new TSDRLibrary();
10-
sdrlib.test();
10+
sdrlib.loadSource(TSDRLibrary.getAllSources()[0]);
11+
sdrlib.setBaseFreq(0);
1112
} catch(Exception e) {
12-
System.out.println("Cannot load! Reason: "+e.getLocalizedMessage());
13+
System.err.println("Cannot load! Reason: "+e.getLocalizedMessage());
14+
e.printStackTrace();
1315
}
1416
}
1517

JavaGUI/src/martin/tempest/core/TSDRLibrary.java

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import java.io.IOException;
88
import java.io.InputStream;
99

10+
import martin.tempest.core.exceptions.TSDRException;
11+
import martin.tempest.core.exceptions.TSDRLibraryNotCompatible;
12+
1013
/**
1114
* This is a Java wrapper library for TSDRLibrary
1215
*
@@ -15,16 +18,18 @@
1518
*/
1619
public class TSDRLibrary {
1720

21+
private static TSDRSource[] PLUGINS = new TSDRSource[] {new TSDRSource("TSDRPlugin_RawFile")};
22+
1823
// If the binaries weren't loaded, this will go off
19-
private static Exception m_e = null;
24+
private static TSDRLibraryNotCompatible m_e = null;
2025

2126
/**
2227
* Extracts a library to a temporary path and prays for the OS to delete it after the app closes.
2328
* @param name
2429
* @return
2530
* @throws IOException
2631
*/
27-
static final File extractLibrary(final String name) throws IOException {
32+
static final File extractLibrary(final String name) throws TSDRLibraryNotCompatible {
2833
final String rawOSNAME = System.getProperty("os.name").toLowerCase();
2934
final String rawARCHNAME = System.getProperty("os.arch").toLowerCase();
3035

@@ -49,7 +54,7 @@ else if (rawARCHNAME.contains("64"))
4954
ARCHNAME = "X86";
5055

5156
if (OSNAME == null || EXT == null || ARCHNAME == null)
52-
throw new RuntimeException("Your OS or CPU is not yet supported, sorry.");
57+
throw new TSDRLibraryNotCompatible("Your OS or CPU is not yet supported, sorry.");
5358

5459
final String relative_path = "lib/"+OSNAME+"/"+ARCHNAME+"/"+name+EXT;
5560

@@ -60,21 +65,25 @@ else if (rawARCHNAME.contains("64"))
6065
in = new FileInputStream(relative_path);
6166
} catch (FileNotFoundException e) {}
6267

63-
if (in == null) throw new RuntimeException("The library has not been compiled for your OS/Architecture yet ("+OSNAME+"/"+ARCHNAME+").");
64-
65-
66-
byte[] buffer = new byte[in.available()];
68+
if (in == null) throw new TSDRLibraryNotCompatible("The library has not been compiled for your OS/Architecture yet ("+OSNAME+"/"+ARCHNAME+").");
6769

68-
int read = -1;
69-
final File temp = new File(System.getProperty("java.io.tmpdir"), name+EXT);
70-
temp.deleteOnExit();
71-
final FileOutputStream fos = new FileOutputStream(temp);
72-
73-
while((read = in.read(buffer)) != -1) {
74-
fos.write(buffer, 0, read);
70+
File temp;
71+
try {
72+
byte[] buffer = new byte[in.available()];
73+
74+
int read = -1;
75+
temp = new File(System.getProperty("java.io.tmpdir"), name+EXT);
76+
temp.deleteOnExit();
77+
final FileOutputStream fos = new FileOutputStream(temp);
78+
79+
while((read = in.read(buffer)) != -1) {
80+
fos.write(buffer, 0, read);
81+
}
82+
fos.close();
83+
in.close();
84+
} catch (IOException e) {
85+
throw new TSDRLibraryNotCompatible(e);
7586
}
76-
fos.close();
77-
in.close();
7887

7988
return temp;
8089
}
@@ -84,7 +93,7 @@ else if (rawARCHNAME.contains("64"))
8493
* @param name
8594
* @throws IOException
8695
*/
87-
static final void loadLibrary(final String name) throws IOException {
96+
static final void loadLibrary(final String name) throws TSDRLibraryNotCompatible {
8897
try {
8998
// try traditional method
9099
System.loadLibrary(name);
@@ -102,15 +111,32 @@ static final void loadLibrary(final String name) throws IOException {
102111
try {
103112
loadLibrary("TSDRLibrary");
104113
loadLibrary("TSDRLibraryNDK");
105-
extractLibrary("TSDRPlugin_RawFile");
106-
} catch (IOException e) {
114+
115+
} catch (TSDRLibraryNotCompatible e) {
107116
m_e = e;
108117
}
109118
}
110119

111-
public TSDRLibrary() throws Exception {
120+
public TSDRLibrary() throws TSDRLibraryNotCompatible {
112121
if (m_e != null) throw m_e;
113122
}
114123

115-
public native void test();
124+
private native void nativeLoadPlugin(String filepath) throws TSDRException;
125+
public native void pluginParams(String params) throws TSDRException;
126+
public native void setSampleRate(long rate) throws TSDRException;
127+
public native void setBaseFreq(long freq) throws TSDRException;
128+
public native void start() throws TSDRException;
129+
public native void stop() throws TSDRException;
130+
public native void setGain(float gain) throws TSDRException;
131+
public native void readAsync() throws TSDRException;
132+
public native void unloadPlugin() throws TSDRException;
133+
134+
public void loadSource(final TSDRSource plugin) throws TSDRException {
135+
nativeLoadPlugin(extractLibrary(plugin.libname).getAbsolutePath());
136+
}
137+
138+
public static TSDRSource[] getAllSources() {
139+
return PLUGINS;
140+
}
141+
116142
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package martin.tempest.core;
2+
3+
public class TSDRSource {
4+
final String libname;
5+
6+
public TSDRSource(final String libname) {
7+
this.libname = libname;
8+
}
9+
}

0 commit comments

Comments
 (0)