Skip to content

Commit f06aceb

Browse files
committed
Handling errors, etc
1 parent 266587c commit f06aceb

1 file changed

Lines changed: 72 additions & 61 deletions

File tree

TSDRPlugin_ExtIO/TSDRPlugin_ExtIO/TSDRPlugin_ExtIO.cpp

Lines changed: 72 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ uint32_t act_freq = -1;
3838
float req_gain = 0.5;
3939
float act_gain = -1;
4040

41+
HANDLE guisyncevent;
42+
4143
extern "C" void closeextio(void) {
4244
if (source == NULL) return;
4345
if (source->HideGUI != NULL) source->HideGUI();
@@ -127,52 +129,7 @@ extern "C" void callback(int cnt, int status, float IQoffs, void *IQdata) {
127129
}
128130

129131
DWORD WINAPI doGuiStuff(LPVOID arg) {
130-
//AFX_MANAGE_STATE(AfxGetAppModuleState());
131-
132-
if (source->OpenHW()) {
133-
printf("Opened device yey"); fflush(stdout);
134-
135-
// list attenuators
136-
if (source->GetAttenuators != NULL) {
137-
max_att_id = 0;
138-
float att;
139-
while (source->GetAttenuators(max_att_id++, &att) == 0) {};
140-
}
141-
142-
if (source->ShowGUI != NULL)
143-
source->ShowGUI();
144-
//RETURN_OK();
145-
}
146-
else {
147-
closeextio();
148-
//RETURN_EXCEPTION("The ExtIO driver failed to open a device. Make sure your device is plugged in and its drivers are installed correctly.", TSDR_CANNOT_OPEN_DEVICE);
149-
}
150-
151-
152-
153-
MSG msg;
154-
BOOL bRet;
155-
156-
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
157-
{
158-
if (bRet == -1)
159-
{
160-
// handle the error and possibly exit
161-
printf("Errorrrr\n"); fflush(stdout);
162-
}
163-
else
164-
{
165-
printf("Sending messages\n"); fflush(stdout);
166-
167-
TranslateMessage(&msg);
168-
DispatchMessage(&msg);
169-
}
170-
}
171-
172-
return 0;
173-
}
174-
175-
extern "C" int TSDRPLUGIN_EXTIO_API __stdcall tsdrplugin_init(const char * params) {
132+
char * params = (char *)arg;
176133

177134
if (outbuf == NULL) {
178135
outbuf = (float *)malloc(sizeof(float));
@@ -185,26 +142,40 @@ extern "C" int TSDRPLUGIN_EXTIO_API __stdcall tsdrplugin_init(const char * param
185142

186143
// inititalize source
187144
source = (extiosource_t *)malloc(sizeof(extiosource_t));
188-
printf("LOADING! PLUGCHOOO \n"); fflush(stdout);
145+
189146
int status;
190147
if ((status = extio_load(source, params)) == TSDR_OK) {
191148
char name[200];
192149
char model[200];
193-
printf("LOADED! PLUGCHOOO \n"); fflush(stdout);
150+
194151
if (source->InitHW(name, model, &hwtype)) {
195152
source->SetCallback(&callback);
196153

197154
if (hwtype != EXTIO_HWTYPE_16B && hwtype != EXTIO_HWTYPE_24B && hwtype != EXTIO_HWTYPE_32B && hwtype != EXTIO_HWTYPE_FLOAT) {
198155
closeextio();
199-
RETURN_EXCEPTION("The sample format of the ExtIO plugin is not supported.", TSDR_CANNOT_OPEN_DEVICE);
200-
}
156+
announceexception("The sample format of the ExtIO plugin is not supported.", TSDR_CANNOT_OPEN_DEVICE);
157+
} else if (source->OpenHW()) {
158+
//printf("Opened %s model %s!\n", name, model); fflush(stdout);
159+
160+
if (source->ShowGUI != NULL) source->ShowGUI();
201161

202-
CreateThread(NULL, 0, doGuiStuff, NULL, 0, NULL);
203-
RETURN_OK();
162+
// list attenuators
163+
if (source->GetAttenuators != NULL) {
164+
max_att_id = 0;
165+
float att;
166+
while (source->GetAttenuators(max_att_id++, &att) == 0) {};
167+
}
168+
169+
errormsg_code = TSDR_OK;
170+
}
171+
else {
172+
closeextio();
173+
announceexception("The ExtIO driver failed to open a device. Make sure your device is plugged in and its drivers are installed correctly.", TSDR_CANNOT_OPEN_DEVICE);
174+
}
204175
}
205176
else {
206177
closeextio();
207-
RETURN_EXCEPTION("The ExtIO driver failed to initialize a device. Make sure your device is plugged in and its drivers are installed correctly.", TSDR_CANNOT_OPEN_DEVICE);
178+
announceexception("The ExtIO driver failed to initialize a device. Make sure your device is plugged in and its drivers are installed correctly.", TSDR_CANNOT_OPEN_DEVICE);
208179
}
209180
}
210181
else {
@@ -214,12 +185,55 @@ extern "C" int TSDRPLUGIN_EXTIO_API __stdcall tsdrplugin_init(const char * param
214185

215186
free(source);
216187
source = NULL;
217-
188+
218189
if (status == TSDR_INCOMPATIBLE_PLUGIN)
219-
RETURN_EXCEPTION("The ExtIO dll is not compatible with the current machine or does not exist. Please check the filename is correct and the file is a valid ExtIO dll file and try again.", TSDR_PLUGIN_PARAMETERS_WRONG)
220-
else
221-
RETURN_EXCEPTION("The provided library is not a valid/compatible ExtIO dll. Please check the filename is correct and the file is a valid ExtIO dll file and try again.", TSDR_PLUGIN_PARAMETERS_WRONG);
190+
announceexception("The ExtIO dll is not compatible with the current machine or does not exist. Please check the filename is correct and the file is a valid ExtIO dll file and try again.", TSDR_PLUGIN_PARAMETERS_WRONG);
191+
else
192+
announceexception("The provided library is not a valid/compatible ExtIO dll. Please check the filename is correct and the file is a valid ExtIO dll file and try again.", TSDR_PLUGIN_PARAMETERS_WRONG);
193+
}
194+
195+
// notify we have finished loading
196+
SetEvent(guisyncevent);
197+
198+
// do GUI handling
199+
MSG msg;
200+
BOOL bRet;
201+
202+
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
203+
{
204+
if (bRet == -1)
205+
{
206+
// error!
207+
return -1;
208+
}
209+
else
210+
{
211+
if (source == NULL) return 0;
212+
TranslateMessage(&msg);
213+
DispatchMessage(&msg);
214+
}
222215
}
216+
217+
printf("THREAD SAYS BYE BYE\n"); fflush(stdout);
218+
return 0;
219+
}
220+
221+
extern "C" int TSDRPLUGIN_EXTIO_API __stdcall tsdrplugin_init(const char * params) {
222+
223+
// create synchronization event
224+
guisyncevent = CreateEvent(0, FALSE, FALSE, 0);
225+
226+
// do the initialization in a GUI friendly thread
227+
CreateThread(NULL, 0, doGuiStuff, (LPVOID)params, 0, NULL);
228+
229+
// wait for initialization to finish
230+
WaitForSingleObject(guisyncevent, INFINITE);
231+
232+
// close synchronozation event
233+
CloseHandle(guisyncevent);
234+
235+
// return whatever error code was generated during thread execution
236+
return errormsg_code;
223237
}
224238

225239
extern "C" void attenuate(float gain) {
@@ -232,7 +246,6 @@ extern "C" void attenuate(float gain) {
232246
}
233247

234248
extern "C" int TSDRPLUGIN_EXTIO_API __stdcall tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx) {
235-
AFX_MANAGE_STATE(AfxGetAppModuleState());
236249

237250
if (source == NULL)
238251
RETURN_EXCEPTION("Please provide a full path to a valid ExtIO dll.", TSDR_PLUGIN_PARAMETERS_WRONG);
@@ -244,8 +257,6 @@ extern "C" int TSDRPLUGIN_EXTIO_API __stdcall tsdrplugin_readasync(tsdrplugin_re
244257

245258
act_freq = req_freq;
246259

247-
if (source->HideGUI != NULL) source->HideGUI();
248-
249260
if (source->StartHW(act_freq) < 0)
250261
RETURN_EXCEPTION("The device has stopped responding.", TSDR_CANNOT_OPEN_DEVICE);
251262

@@ -280,7 +291,7 @@ extern "C" void TSDRPLUGIN_EXTIO_API __stdcall tsdrplugin_cleanup(void) {
280291

281292
if (source == NULL) return;
282293

283-
//if (source->pfnHideGUI != NULL) source->pfnHideGUI();
294+
if (source->HideGUI != NULL) source->HideGUI();
284295

285296
closeextio();
286297
}

0 commit comments

Comments
 (0)