Skip to content

Commit 5d040ae

Browse files
committed
Improved exception handling. Verbose exception. Fixed mirics plugin tuning bug.
1 parent 3320d2d commit 5d040ae

12 files changed

Lines changed: 180 additions & 61 deletions

File tree

JavaGUI/jni/TSDRLibraryNDK.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
tsdr_lib_t tsdr_instance;
1111

12-
#define THROW(x) error(env, x, "")
12+
#define THROW(x) error(env, x)
1313

1414
struct java_context {
1515
jobject obj;
@@ -58,19 +58,14 @@ void error_translate (int exception_code, char * exceptionclass) {
5858
}
5959
}
6060

61-
void error(JNIEnv * env, int exception_code, const char *inmsg, ...)
61+
void error(JNIEnv * env, int exception_code)
6262
{
6363
if (exception_code == TSDR_OK) return;
6464

6565
char exceptionclass[256];
6666
error_translate(exception_code, exceptionclass);
6767

68-
char msg[256];
69-
70-
va_list argptr;
71-
va_start(argptr, inmsg);
72-
vsprintf(msg, inmsg, argptr);
73-
va_end(argptr);
68+
char * msg = tsdr_getlasterrortext(&tsdr_instance);
7469

7570
jclass cls = (*env)->FindClass(env, exceptionclass);
7671
/* if cls is NULL, an exception has already been thrown */

JavaGUI/jni/include/TSDRLibrary.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
float gain;
2727
float motionblur;
2828
volatile int syncoffset;
29+
char * errormsg;
30+
int errormsg_size;
31+
int errormsg_code;
2932
} typedef tsdr_lib_t;
3033

3134
typedef void(*tsdr_readasync_function)(float *buf, int width, int height, void *ctx);
@@ -41,5 +44,6 @@
4144
int tsdr_isrunning(tsdr_lib_t * tsdr);
4245
int tsdr_sync(tsdr_lib_t * tsdr, int pixels, int direction);
4346
int tsdr_motionblur(tsdr_lib_t * tsdr, float coeff);
47+
char * tsdr_getlasterrortext(tsdr_lib_t * tsdr);
4448

4549
#endif

JavaGUI/src/martin/tempest/core/exceptions/TSDRLoadPluginException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ public class TSDRLoadPluginException extends TSDRException {
66
public TSDRLoadPluginException(final String msg) {
77
super(msg);
88
}
9+
10+
public TSDRLoadPluginException() {
11+
super();
12+
}
913
}

TSDRPlugin_Mirics/src/TSDRPlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
int tsdrplugin_setbasefreq(uint32_t freq);
1313
int tsdrplugin_stop(void);
1414
int tsdrplugin_setgain(float gain);
15+
char * tsdrplugin_getlasterrortext(void);
1516
int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx);
1617

1718
#endif

TSDRPlugin_Mirics/src/TSDRPlugin_Mirics.c

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@ volatile int desiredgainred = 40;
1818

1919
#define SAMPLES_TO_PROCESS_AT_ONCE (20)
2020

21+
int errormsg_code;
22+
char * errormsg;
23+
int errormsg_size = 0;
24+
#define RETURN_EXCEPTION(message, status) {announceexception(message, status); return status;}
25+
#define RETURN_OK() {errormsg_code = TSDR_OK; return TSDR_OK;}
26+
27+
static inline void announceexception(const char * message, int status) {
28+
errormsg_code = status;
29+
if (status == TSDR_OK) return;
30+
31+
const int length = strlen(message);
32+
if (errormsg_size == 0) {
33+
errormsg_size = length;
34+
errormsg = (char *) malloc(length+1);
35+
} else if (length > errormsg_size) {
36+
errormsg_size = length;
37+
errormsg = (char *) realloc((void*) errormsg, length+1);
38+
}
39+
strcpy(errormsg, message);
40+
}
41+
42+
char * tsdrplugin_getlasterrortext(void) {
43+
if (errormsg_code == TSDR_OK)
44+
return NULL;
45+
else
46+
return errormsg;
47+
}
48+
2149
void tsdrplugin_getName(char * name) {
2250
strcpy(name, "TSDR Mirics SDR Plugin");
2351
}
@@ -32,23 +60,23 @@ uint32_t tsdrplugin_getsamplerate() {
3260

3361
int tsdrplugin_setbasefreq(uint32_t freq) {
3462
desiredfreq = freq;
35-
return TSDR_OK;
63+
RETURN_OK();
3664
}
3765

3866
int tsdrplugin_stop(void) {
3967
working = 0;
40-
return TSDR_OK;
68+
RETURN_OK();
4169
}
4270

4371
int tsdrplugin_setgain(float gain) {
4472
desiredgainred = 102 - (int) (gain * 102);
4573
if (desiredgainred < 0) desiredgainred = 0;
4674
else if (desiredgainred > 102) desiredgainred = 102;
47-
return TSDR_OK;
75+
RETURN_OK();
4876
}
4977

5078
int tsdrplugin_setParams(const char * params) {
51-
return TSDR_OK;
79+
RETURN_OK();
5280
}
5381

5482
int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx) {
@@ -57,14 +85,16 @@ int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx) {
5785
int err, sps, grc, rfc, fsc, i, id;
5886
unsigned int fs;
5987
unsigned int frame = 0;
88+
int inited = 0 ;
6089

6190
double freq = desiredfreq;
6291
double gainred = desiredgainred;
6392
err = mir_sdr_Init(gainred, 8, freq / 1000000.0, mir_sdr_BW_8_000, mir_sdr_IF_Zero, &sps);
6493
if (err != 0) {
6594
mir_sdr_Uninit();
66-
return TSDR_CANNOT_OPEN_DEVICE;
67-
}
95+
RETURN_EXCEPTION("Can't access the Mirics SDR dongle. Make sure it is properly plugged in, drivers are installed and the mir_sdr_api.dll is in the executable's folder and try again. Please, refer to the TSDRPlugin_Miricis readme file for more information.", TSDR_CANNOT_OPEN_DEVICE);
96+
} else
97+
inited = 1;
6898

6999
short * xi = (short *)malloc(sps * SAMPLES_TO_PROCESS_AT_ONCE * sizeof(short));
70100
short * xq = (short *)malloc(sps * SAMPLES_TO_PROCESS_AT_ONCE * sizeof(short));
@@ -89,46 +119,55 @@ int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx) {
89119
}
90120

91121
// if desired frequency is different and valid
92-
if (freq != desiredfreq && !(desiredfreq < 60e6 || (desiredfreq > 245e6 && desiredfreq < 420e6) || desiredfreq > 1000e6)) {
122+
if (err == 0 && freq != desiredfreq && !(desiredfreq < 60e6 || (desiredfreq > 245e6 && desiredfreq < 420e6) || desiredfreq > 1000e6)) {
93123

94124
err = mir_sdr_SetRf(desiredfreq, 1, 0);
95125
if (err == 0)
96126
freq = desiredfreq; // if OK, frequency has been changed
97127
else if (err == mir_sdr_RfUpdateError) {
98128
// if an error occurs, try to reset the device
99-
mir_sdr_Uninit();
129+
if (mir_sdr_Uninit() == 0) inited = 0;
100130
gainred = desiredgainred;
101-
err = mir_sdr_Init(gainred, 8, desiredfreq / 1000000.0, mir_sdr_BW_8_000, mir_sdr_IF_Zero, &sps);
102-
if (err == 3) {
131+
if ((err = mir_sdr_Init(gainred, 8, desiredfreq / 1000000.0, mir_sdr_BW_8_000, mir_sdr_IF_Zero, &sps))==0) inited = 1;
132+
if (err == mir_sdr_OutOfRange) {
103133
// if the desired frequency is outside operational range, go back to the working frequency
104-
mir_sdr_Uninit();
105-
mir_sdr_Init(gainred, 8, freq / 1000000.0, mir_sdr_BW_8_000, mir_sdr_IF_Zero, &sps);
134+
if (mir_sdr_Uninit() == 0) inited = 0;
135+
if (mir_sdr_Init(gainred, 8, freq / 1000000.0, mir_sdr_BW_8_000, mir_sdr_IF_Zero, &sps) == 0) inited = 1;
106136
desiredfreq = freq;
107137
err = 0;
108-
}
109-
else
138+
} else if (err == 0)
110139
freq = desiredfreq;
140+
} else if (err == mir_sdr_OutOfRange) {
141+
mir_sdr_SetRf(freq, 1, 0);
142+
desiredfreq = freq;
143+
err = 0;
111144
}
112145
}
113146

114-
if (gainred != desiredgainred) {
147+
if (err == 0 && gainred != desiredgainred) {
115148
gainred = desiredgainred;
116149
mir_sdr_SetGr(gainred, 1, 0);
117150
}
118151

152+
if (err == 0)
119153
for (i = 0; i < outbufsize; i++) {
120154
const short val = (i & 1) ? (xq[i >> 1]) : (xi[i >> 1]);
121155
outbuf[i] = val / 32767.0;
122156
}
123157

124-
cb(outbuf, outbufsize, ctx, dropped);
158+
if (err != 0)
159+
{ working = 0; break; }
160+
else
161+
cb(outbuf, outbufsize, ctx, dropped);
125162
}
126163

164+
165+
127166
free(outbuf);
128167
free(xi);
129168
free(xq);
130169

131-
if (mir_sdr_Uninit() != 0) return TSDR_ERR_PLUGIN;
170+
if (inited && mir_sdr_Uninit() != 0) RETURN_EXCEPTION("Cannot properly close the Mirics USB dongle. If you experience any further issues, please disconnect it and reconnect it again.", TSDR_CANNOT_OPEN_DEVICE);
132171

133-
return (err == 0) ? TSDR_OK : TSDR_ERR_PLUGIN;
172+
RETURN_EXCEPTION("The Mirics SDR dongle stopped responding.", (err == 0) ? TSDR_OK : TSDR_ERR_PLUGIN);
134173
}

TSDRPlugin_RawFile/src/TSDRPlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
int tsdrplugin_setbasefreq(uint32_t freq);
1313
int tsdrplugin_stop(void);
1414
int tsdrplugin_setgain(float gain);
15+
char * tsdrplugin_getlasterrortext(void);
1516
int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx);
1617

1718
#endif

TSDRPlugin_RawFile/src/TSDRPlugin_RawFile.c

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ void thread_sleep(uint32_t milliseconds) {
4343
#endif
4444
}
4545

46+
int errormsg_code;
47+
char * errormsg;
48+
int errormsg_size = 0;
49+
#define RETURN_EXCEPTION(message, status) {announceexception(message, status); return status;}
50+
#define RETURN_OK() {errormsg_code = TSDR_OK; return TSDR_OK;}
51+
52+
static inline void announceexception(const char * message, int status) {
53+
errormsg_code = status;
54+
if (status == TSDR_OK) return;
55+
56+
const int length = strlen(message);
57+
if (errormsg_size == 0) {
58+
errormsg_size = length;
59+
errormsg = (char *) malloc(length+1);
60+
} else if (length > errormsg_size) {
61+
errormsg_size = length;
62+
errormsg = (char *) realloc((void*) errormsg, length+1);
63+
}
64+
strcpy(errormsg, message);
65+
}
66+
67+
char * tsdrplugin_getlasterrortext(void) {
68+
if (errormsg_code == TSDR_OK)
69+
return NULL;
70+
else
71+
return errormsg;
72+
}
73+
4674
void tsdrplugin_getName(char * name) {
4775
strcpy(name, "TSDR Raw File Source Plugin");
4876
}
@@ -56,16 +84,16 @@ uint32_t tsdrplugin_getsamplerate() {
5684
}
5785

5886
int tsdrplugin_setbasefreq(uint32_t freq) {
59-
return TSDR_OK;
87+
RETURN_OK();
6088
}
6189

6290
int tsdrplugin_stop(void) {
6391
working = 0;
64-
return TSDR_OK;
92+
RETURN_OK();
6593
}
6694

6795
int tsdrplugin_setgain(float gain) {
68-
return TSDR_OK;
96+
RETURN_OK();
6997
}
7098

7199
char * strtoken = NULL;
@@ -115,13 +143,13 @@ char * nexttoken(char * input) {
115143

116144
int tsdrplugin_setParams(const char * params) {
117145
char * fname = nexttoken((char *) params);
118-
if (fname == NULL) return TSDR_PLUGIN_PARAMETERS_WRONG;
146+
if (fname == NULL) RETURN_EXCEPTION("File name was not specified. Commands should be: filename samplerate sampleformat. Format could be float, int8, uint8, int16 or uint16.", TSDR_PLUGIN_PARAMETERS_WRONG);
119147
char * samplerate_s = nexttoken(NULL);
120-
if (samplerate_s == NULL) return TSDR_SAMPLE_RATE_WRONG;
148+
if (samplerate_s == NULL) RETURN_EXCEPTION("Sample rate was not specified. Commands should be: filename samplerate sampleformat. Format could be float, int8, uint8, int16 or uint16.", TSDR_PLUGIN_PARAMETERS_WRONG);
121149
long samplerate_l = atol(samplerate_s);
122-
if (samplerate_l > MAX_SAMP_RATE || samplerate_l <= 0) return TSDR_SAMPLE_RATE_WRONG;
150+
if (samplerate_l > MAX_SAMP_RATE || samplerate_l <= 0) RETURN_EXCEPTION("Samplerate is invalid. Please specify the samplerate the original recording was done with.", TSDR_PLUGIN_PARAMETERS_WRONG);
123151
char * type_s = nexttoken(NULL);
124-
if (type_s == NULL) return TSDR_PLUGIN_PARAMETERS_WRONG;
152+
if (type_s == NULL) RETURN_EXCEPTION("Sample type is not specified. Pick one between float, int8, uint8, int16 or uint16.", TSDR_PLUGIN_PARAMETERS_WRONG);
125153

126154
if (str_eq(type_s,"float")) {
127155
type = TYPE_FLOAT;
@@ -139,12 +167,12 @@ int tsdrplugin_setParams(const char * params) {
139167
type = TYPE_USHORT;
140168
sizepersample = 2;
141169
} else
142-
return TSDR_PLUGIN_PARAMETERS_WRONG;
170+
RETURN_EXCEPTION("Sample type is invalid. Pick one between float, int8, uint8, int16 or uint16.", TSDR_PLUGIN_PARAMETERS_WRONG);
143171

144172
strcpy(filename, fname);
145173
samplerate = (uint32_t) samplerate_l;
146174

147-
return TSDR_OK;
175+
RETURN_OK();
148176
}
149177

150178
int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx) {
@@ -154,11 +182,11 @@ int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx) {
154182
int counter;
155183

156184
if (sizepersample == -1)
157-
return TSDR_PLUGIN_PARAMETERS_WRONG;
185+
RETURN_EXCEPTION("Plugin was not initialized properly.", TSDR_PLUGIN_PARAMETERS_WRONG);
158186

159187
FILE * file=fopen(filename,"rb");
160-
if (file == NULL) return TSDR_PLUGIN_PARAMETERS_WRONG;
161-
if (samplerate > MAX_SAMP_RATE || samplerate <= 0) return TSDR_SAMPLE_RATE_WRONG;
188+
if (file == NULL) RETURN_EXCEPTION("Cannot open the required file.", TSDR_PLUGIN_PARAMETERS_WRONG);
189+
if (samplerate > MAX_SAMP_RATE || samplerate <= 0) RETURN_EXCEPTION("The samplerate the plugin was initialized with is invalid.", TSDR_SAMPLE_RATE_WRONG);
162190

163191
const size_t bytestoread = SAMPLES_TO_READ_AT_ONCE * sizepersample;
164192
char * buf = (char *) malloc(bytestoread);
@@ -227,5 +255,5 @@ int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx) {
227255
free(outbuf);
228256
fclose(file);
229257

230-
return TSDR_OK;
258+
RETURN_OK();
231259
}

0 commit comments

Comments
 (0)