File: FSK441App.cpp

package info (click to toggle)
wstools 0.4.8d-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,440 kB
  • ctags: 1,991
  • sloc: cpp: 13,880; makefile: 469
file content (603 lines) | stat: -rw-r--r-- 14,711 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
 *   Copyright (C) 2002-2004 by Jonathan Naylor G4KLX
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "FSK441App.h"

#include <wx/config.h>
#include <wx/filename.h>
#include <wx/ffile.h>

IMPLEMENT_APP(CFSK441App)

#include "common/SoundCard.h"
#include "common/Exception.h"
#include "common/Log.h"

#include "FSK441Defs.h"

const wxString SECTION_GENERAL  = wxT("General");
const wxString SECTION_TRANSMIT = wxT("Transmit");
const wxString SECTION_RECEIVE  = wxT("Receive");
const wxString SECTION_SOUND    = wxT("Sound");
const wxString SECTION_PTT      = wxT("PTT");
const wxString SECTION_PATHS    = wxT("Paths");
const wxString SECTION_MESSAGES = wxT("Messages");

const wxString KEY_CALLSIGN     = wxT("callsign");
const wxString KEY_LOCATOR      = wxT("locator");
const wxString KEY_FIRST        = wxT("first");
const wxString KEY_MIN_LENGTH   = wxT("min_length");
const wxString KEY_MIN_STRENGTH = wxT("min_strength");
const wxString KEY_MAX_OFFSET   = wxT("max_offset");
const wxString KEY_TYPE         = wxT("type");
const wxString KEY_FILENAME     = wxT("filename");
const wxString KEY_DEVICE       = wxT("device");
const wxString KEY_AUDIO        = wxT("audio");
const wxString KEY_TEXT         = wxT("text");
const wxString KEY_MESSAGE      = wxT("message_");

const wxString DEFAULT_CALLSIGN = wxT("G4KLX");
const wxString DEFAULT_LOCATOR  = wxT("IO93FB");

const bool DEFAULT_TX_ENABLED = false;
const bool DEFAULT_TX_FIRST   = true;

const long DEFAULT_MIN_LENGTH     = 30L;
const long DEFAULT_MIN_STRENGTH   = 6L;
const long DEFAULT_MAX_OFFSET     = 400L;

const wxString DEFAULT_PTT_TYPE   = wxT("none");
const wxString DEFAULT_PTT_DEVICE = wxEmptyString;

#ifdef __WINDOWS__
const wxString DEFAULT_SOUND_FILENAME = wxT("Wave Mapper");
#else
const wxString DEFAULT_SOUND_FILENAME = wxT("/dev/dsp");
#endif

const wxString DEFAULT_MESSAGE_0 = wxT("%T %M ");
const wxString DEFAULT_MESSAGE_1 = wxT("%T %M %R %R ");
const wxString DEFAULT_MESSAGE_2 = wxT("%T %M R%R R%R ");
const wxString DEFAULT_MESSAGE_3 = wxT("%M RRRRRRRR ");
const wxString DEFAULT_MESSAGE_4 = wxT("73 %M ");
const wxString DEFAULT_MESSAGE_5 = wxT("CQ %M ");


CFSK441App::CFSK441App() :
wxApp(),
m_frame(NULL),
m_controller(NULL),
m_messages(NULL),
m_file(NULL),
m_saving(false),
m_recording(false),
m_txEnable(DEFAULT_TX_ENABLED)
{
}

CFSK441App::~CFSK441App()
{
}

bool CFSK441App::OnInit()
{
	SetVendorName(FSK441_VENDOR_NAME);
	SetAppName(FSK441_APPL_NAME);

	wxConfigBase* config = wxConfigBase::Get();
	config->SetRecordDefaults();

	wxString pathAudio, pathText;
	getPaths(pathAudio, pathText);

	wxFileName fileName(pathText, FSK441_LOGFILE_NAME, FSK441_LOGFILE_EXT);

	wxLog* log = new CLog(fileName.GetFullPath());
	wxLog::SetActiveTarget(log);

	createController();
	::wxSleep(1);

	createFileReader();

	readRegistry();

	m_frame = new CFSK441Frame();
	m_frame->Show();

	SetTopWindow(m_frame);

	return true;
}

int CFSK441App::OnExit()
{
	wxASSERT(m_controller != NULL);
	wxASSERT(m_file != NULL);

	m_controller->Delete();
	m_file->kill();

	delete wxConfigBase::Set(NULL);

	return 0;
}

#ifdef __WXDEBUG__
void CFSK441App::OnAssert(const wxChar* file, int line, const wxChar* cond, const wxChar* msg)
{
	::wxLogFatalError(wxT("Assertion failed on line %d in file %s: %s %s"), line, file, cond, msg);
}
#endif

void CFSK441App::receiveMessage(CFSK441Message* message, EWho who) const
{
	wxASSERT(m_frame != NULL);
	wxASSERT(message != NULL);

	m_frame->showMessage(message, who);
}

void CFSK441App::logMessage(const CFSK441Message& message) const
{
	if (m_saving) {
		wxString   id = message.getId();
		double    tim = message.getTime();
		int       len = message.getLength();
		int  strength = message.getStrength();
		int        DF = message.getDF();
		wxString text = message.getText();

		wxString pathAudio, pathText;
		getPaths(pathAudio, pathText);

		wxFileName fileName(pathText, FSK441_MESSAGES_NAME, FSK441_MESSAGES_EXT);

		wxFFile* file = new wxFFile(fileName.GetFullPath(), "a");

		if (!file->IsOpened()) {
			wxString text;
			text.Printf(wxT("Cannot open save text file: %s"), fileName.GetFullPath().c_str());
			error(text);

			delete file;
			return;
		}

		wxString line;
		line.Printf(wxT("%s  %.1f  %d  %d  %d  %s\n"), id.c_str(), tim, len, strength, DF, text.c_str());

		file->Write(line);

		file->Close();

		delete file;
	}
}

void CFSK441App::showLevels(CFSK441Levels* levels, EWho who) const
{
	wxASSERT(levels != NULL);
	wxASSERT(m_frame != NULL);

	m_frame->showLevels(levels, who);
}

void CFSK441App::showAudio(double audio, EWho who) const
{
	wxASSERT(m_frame != NULL);

	m_frame->showAudio(audio, who);
}

void CFSK441App::error(const wxString& text) const
{
	wxASSERT(m_frame != NULL);

	m_frame->error(text);
}

void CFSK441App::getPersonal(wxString& callsign, wxString& locator) const
{
	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString callsignKey = wxT("/") + SECTION_GENERAL + wxT("/") + KEY_CALLSIGN;
	wxString locatorKey  = wxT("/") + SECTION_GENERAL + wxT("/") + KEY_LOCATOR;

	profile->Read(callsignKey, &callsign, DEFAULT_CALLSIGN);
	profile->Read(locatorKey,  &locator,  DEFAULT_LOCATOR);
}

void CFSK441App::setPersonal(const wxString& callsign, const wxString& locator) const
{
	CText::setLocalCallsign(callsign);
	CText::setLocalLocator(locator);

	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString callsignKey = wxT("/") + SECTION_GENERAL + wxT("/") + KEY_CALLSIGN;
	wxString locatorKey  = wxT("/") + SECTION_GENERAL + wxT("/") + KEY_LOCATOR;

	profile->Write(callsignKey, callsign);
	profile->Write(locatorKey,  locator);
	profile->Flush();
}

void CFSK441App::setRemote(const wxString& callsign, const wxString& report)
{
	if (callsign.IsEmpty())
		CText::setRemoteCallsign(wxEmptyString);
	else
		CText::setRemoteCallsign(callsign);

	if (report.IsEmpty())
		CText::setLocalReport(wxEmptyString);
	else
		CText::setLocalReport(report);
}

bool CFSK441App::getTXFirst() const
{
	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString key = wxT("/") + SECTION_TRANSMIT + wxT("/") + KEY_FIRST;

	bool txFirst;
	profile->Read(key, &txFirst, DEFAULT_TX_FIRST);

	return txFirst;
}

void CFSK441App::setTXFirst(bool txFirst) const
{
	wxASSERT(m_controller != NULL);

	m_controller->setTXFirst(txFirst);

	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString key = wxT("/") + SECTION_TRANSMIT + wxT("/") + KEY_FIRST;

	profile->Write(key, txFirst);
	profile->Flush();
}

bool CFSK441App::getTXEnable() const
{
	return m_txEnable;
}

void CFSK441App::setTXEnable(bool txEnable)
{
	wxASSERT(m_controller != NULL);

	m_controller->setTXEnable(txEnable);

	m_txEnable = txEnable;
}

void CFSK441App::getProtoSettings(int& minLength, int& minStrength, int& maxOffset) const
{
	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString lengthKey   = wxT("/") + SECTION_RECEIVE + wxT("/") + KEY_MIN_LENGTH;
	wxString strengthKey = wxT("/") + SECTION_RECEIVE + wxT("/") + KEY_MIN_STRENGTH;
	wxString offsetKey   = wxT("/") + SECTION_RECEIVE + wxT("/") + KEY_MAX_OFFSET;

	long length, strength, offset;
	profile->Read(lengthKey,   &length,   DEFAULT_MIN_LENGTH);
	profile->Read(strengthKey, &strength, DEFAULT_MIN_STRENGTH);
	profile->Read(offsetKey,   &offset,   DEFAULT_MAX_OFFSET);

	minLength   = length;
	minStrength = strength;
	maxOffset   = offset;
}

void CFSK441App::setProtoSettings(int minLength, int minStrength, int maxOffset) const
{
	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString lengthKey   = wxT("/") + SECTION_RECEIVE + wxT("/") + KEY_MIN_LENGTH;
	wxString strengthKey = wxT("/") + SECTION_RECEIVE + wxT("/") + KEY_MIN_STRENGTH;
	wxString offsetKey   = wxT("/") + SECTION_RECEIVE + wxT("/") + KEY_MAX_OFFSET;

	profile->Write(lengthKey,   minLength);
	profile->Write(strengthKey, minStrength);
	profile->Write(offsetKey,   maxOffset);
	profile->Flush();
}

wxString CFSK441App::getReplacedMessage(int n) const
{
	wxASSERT(n >= 0 && n < FSK441_MAX_MESSAGES);
	wxASSERT(m_messages[n] != NULL);

	return m_messages[n]->getReplacedText();
}

wxString CFSK441App::getOriginalMessage(int n) const
{
	wxASSERT(n >= 0 && n < FSK441_MAX_MESSAGES);
	wxASSERT(m_messages[n] != NULL);

	return m_messages[n]->getOriginalText();
}

void CFSK441App::getMessage(int n, wxString& message) const
{
	wxASSERT(n >= 0 && n < FSK441_MAX_MESSAGES);

	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString number;
	number.Printf(wxT("%d"), n);

	wxString key = wxT("/") + SECTION_MESSAGES + wxT("/") + KEY_MESSAGE + number;
	wxString defMessage;

	switch (n) {
		case 0:
			defMessage = DEFAULT_MESSAGE_0;
			break;
		case 1:
			defMessage = DEFAULT_MESSAGE_1;
			break;
		case 2:
			defMessage = DEFAULT_MESSAGE_2;
			break;
		case 3:
			defMessage = DEFAULT_MESSAGE_3;
			break;
		case 4:
			defMessage = DEFAULT_MESSAGE_4;
			break;
		case 5:
			defMessage = DEFAULT_MESSAGE_5;
			break;
		default:
			defMessage = wxEmptyString;
			break;
	}

	profile->Read(key, &message, defMessage);
}

void CFSK441App::setMessage(int n, const wxString& message)
{
	wxASSERT(n >= 0 && n < FSK441_MAX_MESSAGES);

	if (m_messages[n] != NULL)
		delete m_messages[n];

	m_messages[n] = new CText(message);

	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString number;
	number.Printf(wxT("%d"), n);

	wxString key = wxT("/") + SECTION_MESSAGES + wxT("/") + KEY_MESSAGE + number;

	profile->Write(key, message);
	profile->Flush();
}

void CFSK441App::getPTT(wxString& type, wxString& device) const
{
	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString typeKey   = wxT("/") + SECTION_PTT + wxT("/") + KEY_TYPE;
	wxString deviceKey = wxT("/") + SECTION_PTT + wxT("/") + KEY_DEVICE;

	profile->Read(typeKey,   &type,   DEFAULT_PTT_TYPE);
	profile->Read(deviceKey, &device, DEFAULT_PTT_DEVICE);
}

void CFSK441App::setPTT(const wxString& type, const wxString& device) const
{
	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString typeKey   = wxT("/") + SECTION_PTT + wxT("/") + KEY_TYPE;
	wxString deviceKey = wxT("/") + SECTION_PTT + wxT("/") + KEY_DEVICE;

	profile->Write(typeKey,   type);
	profile->Write(deviceKey, device);
	profile->Flush();
}

void CFSK441App::getPaths(wxString& pathAudio, wxString& pathText) const
{
	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString audioKey = wxT("/") + SECTION_PATHS + wxT("/") + KEY_AUDIO;
	wxString textKey  = wxT("/") + SECTION_PATHS + wxT("/") + KEY_TEXT;

	wxString homeDir = wxFileName::GetHomeDir();

	profile->Read(audioKey, &pathAudio, homeDir);
	profile->Read(textKey,  &pathText,  homeDir);
}

void CFSK441App::setPaths(const wxString& pathAudio, const wxString& pathText) const
{
	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString audioKey = wxT("/") + SECTION_PATHS + wxT("/") + KEY_AUDIO;
	wxString textKey  = wxT("/") + SECTION_PATHS + wxT("/") + KEY_TEXT;

	profile->Write(audioKey, pathAudio);
	profile->Write(textKey,  pathText);
	profile->Flush();
}

void CFSK441App::getSound(wxString& fileName) const
{
	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString fileNameKey = wxT("/") + SECTION_SOUND + wxT("/") + KEY_FILENAME;

	profile->Read(fileNameKey, &fileName, DEFAULT_SOUND_FILENAME);
}

void CFSK441App::setSound(const wxString& fileName) const
{
	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	wxString fileNameKey = wxT("/") + SECTION_SOUND + wxT("/") + KEY_FILENAME;

	profile->Write(fileNameKey, fileName);
	profile->Flush();
}

void CFSK441App::setSaving(bool saving)
{
	m_saving = saving;
}

void CFSK441App::deleteMessages() const
{
	wxString pathAudio, pathText;
	getPaths(pathAudio, pathText);

	wxFileName fileName(pathText, FSK441_MESSAGES_NAME, FSK441_MESSAGES_EXT);

	::wxRemoveFile(fileName.GetFullPath());
}

void CFSK441App::setRecording(bool recording)
{
	wxASSERT(m_controller != NULL);

	m_recording = recording;

	m_controller->setRXRecording(m_recording);
}

void CFSK441App::sendMessage(const wxString& text)
{
	wxASSERT(m_controller != NULL);

	m_controller->sendMessage(text);
}

void CFSK441App::readFile(const wxString& fileName)
{
	wxASSERT(m_file != NULL);

	if (m_file->isRunning()) {
		error(wxT("Another WAV file is currently being processed"));
		return;
	}

	try {
		m_file->setFileName(fileName);
		m_file->start();
	}
	catch (CException& ex) {
		error(ex.getMessage());
		return;
	}
}

void CFSK441App::createController()
{
	try {
		wxString type, fileName;

		getSound(fileName);
		CSoundCard* soundDev = new CSoundCard(fileName, FSK441_SAMPLE_RATE, FSK441_SOUNDBUF_LENGTH, FSK441_SYMBOL_LENGTH);

		getPTT(type, fileName);
		CPTTPort* pttPort = CPTTPort::createPTTPort(type, fileName);

		m_controller = new CFSK441Controller(soundDev, pttPort);
		m_controller->Create();
		m_controller->Run();
	}
	catch (CException& ex) {
		::fprintf(stderr, "FSK441: an error has occured when creating the sound device or the PTT port: %s\n", ex.getMessage().c_str());
		throw;
	}
}

void CFSK441App::createFileReader()
{
	m_file = new CFSK441FileReceive(wxT("File Receive"), RX_FILE);

	m_file->Create();
	m_file->Run();
}

void CFSK441App::readRegistry()
{
	wxASSERT(m_controller != NULL);

	wxString callsign, locator;
	getPersonal(callsign, locator);
	CText::setLocalCallsign(callsign);
	CText::setLocalLocator(locator);

	bool txEnable = getTXEnable();
	m_controller->setTXEnable(txEnable);

	bool txFirst = getTXFirst();
	m_controller->setTXFirst(txFirst);

	m_messages = new CText*[FSK441_MAX_MESSAGES];

	for (int i = 0; i < FSK441_MAX_MESSAGES; i++) {
		wxString message;
		getMessage(i, message);
		m_messages[i] = new CText(message);
	}

	wxConfigBase* profile = wxConfigBase::Get();

	wxASSERT(profile != NULL);

	profile->Flush();
}