/
HermesIntf.cpp
876 lines (666 loc) · 21.4 KB
/
HermesIntf.cpp
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
// HermesIntf.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "HermesIntf.h"
#include <stdexcept>
#include <string>
#include <iostream>
#include <sstream>
#include "Hermes.h"
#include <assert.h>
#include <time.h>
#include <math.h>
// String literal constants intialized in Hermes.cpp
extern "C" const char UNKNOWN_HPSDR[];
extern "C" const char IDLE[];
extern "C" const char SENDING_DATA[];
extern "C" const char UNKNOWN_STATUS[];
extern "C" const char METIS[];
extern "C" const char HERMES[];
extern "C" const char GRIFFIN[];
extern "C" const char ANGELIA[];
extern "C" const char HERMESLT[];
extern "C" const char UNKNOWN_BRD_ID[];
extern "C" const char RTLDNGL[];
extern "C" const char REDPITAYA[];
extern "C" const char AFEDRI[];
extern "C" const char ORION[];
extern "C" const char ORION2[];
extern "C" const char SATURN[];
extern "C" const char ANAN10E[];
// String buffer for device name
char display_name[50];
#ifdef __MINGW32__
// patch for std::to_string to work around a known compile error bug in minGW:
// error: 'to_string' is not a member of 'std'
// See https://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-g-mingw
namespace patch
{
template < typename T > std::string to_string( const T& n )
{
std::ostringstream stm ;
stm << n ;
return stm.str() ;
}
}
#endif
namespace HermesIntf
{
///////////////////////////////////////////////////////////////////////////////
// Global variables
// Settings from Skimmer server
SdrSettings gSet;
volatile int ADC_overflow_count = 0;
// Sample rate of Skimmer server
int gSampleRate = 0;
// Length of block for one call of IQProc
int gBlockInSamples = 0;
// Buffers for calling IQProc
CmplxA gData1[MAX_RX_COUNT];
CmplxA gData2[MAX_RX_COUNT];
// Current length of data in Buffers for calling IQProc (in samples)
int gDataSamples[MAX_RX_COUNT] = { 0 };
// Instance of hermes
Hermes myHermes;
//Handle & ID of worker thread
DWORD gidWrk = 0;
HANDLE ghWrk = NULL;
//Handle & ID of Agc thread
DWORD gidAgc = 0;
HANDLE ghAgc = NULL;
// Stop flag
volatile bool gStopFlag = false;
//////////////////////////////////////////////////////////////////////////////
// Allocate working buffers & others
BOOL Alloc(void)
{
int i;
// free buffers for calling IQProc
for (i = 0; i < MAX_RX_COUNT; i++)
{// allocated ?
if (gData1[i] != NULL) free(gData1[i]);
gData1[i] = NULL;
if (gData2[i] != NULL) free(gData2[i]);
gData2[i] = NULL;
}
// decode sample rate
if (gSet.RateID == RATE_48KHZ) {
gSampleRate = 48000;
} else if (gSet.RateID == RATE_96KHZ) {
gSampleRate = 96000;
} else if (gSet.RateID == RATE_192KHZ) {
gSampleRate = 192000;
} else {
// unknown sample rate
rt_exception("Unknown sample rate");
return(FALSE);
}
// compute length of block in samples
gBlockInSamples = (int)((float)gSampleRate / (float)BLOCKS_PER_SEC);
// allocate buffers for calling IQProc
for (i = 0; i < MAX_RX_COUNT; i++)
{
// allocated ?
if (gData1[i] != NULL) free(gData1[i]);
if (gData2[i] != NULL) free(gData2[i]);
// allocate buffers
gData1[i] = (CmplxA)_aligned_malloc(gBlockInSamples * sizeof(Cmplx), 16);
gData2[i] = (CmplxA)_aligned_malloc(gBlockInSamples * sizeof(Cmplx), 16);
// have we memory ?
if (gData1[i] == NULL || gData2[i] == NULL)
{// low memory
rt_exception("Low memory");
return(FALSE);
}
// clear it
memset(gData1[i], 0, gBlockInSamples * sizeof(Cmplx));
memset(gData2[i], 0, gBlockInSamples * sizeof(Cmplx));
}
gDataSamples[0] = 0;
// success
write_text_to_log_file("Alloc success");
return(TRUE);
}
DWORD WINAPI Agc(LPVOID lpParameter)
{
//reset attenuator
(myHermes.prot_ver == 1) ? myHermes.SetAtt(0) : myHermes.SetAtt2(0);
int lastAttChange = GetTickCount();
while(!gStopFlag)
{
if (ADC_overflow_count > 0)
{
//write_text_to_log_file("ADC Overload");
myHermes.IncrAtt();
Sleep(10);
ADC_overflow_count = 0;
lastAttChange = GetTickCount();
}
//gradually decrease attenuation
if (GetTickCount() - lastAttChange > 10000) {
myHermes.DecrAtt();
lastAttChange = GetTickCount();
}
Sleep(100);
}
(myHermes.prot_ver == 1) ? myHermes.SetAtt(0) : myHermes.SetAtt2(0);
return(0);
}
DWORD WINAPI Worker(LPVOID lpParameter)
{
Cmplx *optr[MAX_RX_COUNT];
int gNChan = myHermes.rxCount;
// metis samples are in two frames at recvbuff[16..519] and [528..1031]
int samplesPerFrame = 504 / (gNChan*6+2);
const int framesPerPacket = 2;
int i,j,k,bytes_received;
int smplI, smplQ;
// wait for a while ...
Sleep(100);
// prepare pointers to IQ buffer
gDataSamples[0] = 0;
for (i = 0; i < gNChan; i++) optr[i] = gData1[i];
char recvbuff[1500] = {0};
int recvbufflen = 1500;
int len = sizeof(struct sockaddr_in);
//packet sequence numbers
unsigned last_seq=0;
unsigned long rx_seq = 0;
unsigned int lost_pkts = 0;
unsigned long elapsed = 0;
DWORD last_error = GetTickCount();
DWORD start_time = GetTickCount();
// main loop
while (!gStopFlag)
{
// read data block
//bytes_received = recv(myHermes.sock,recvbuff,recvbufflen,0);
bytes_received = recvfrom(myHermes.sock,recvbuff,recvbufflen,0,(sockaddr *)&myHermes.Hermes_addr,&len);
if (bytes_received <= 0 && WSAGetLastError() == WSAETIMEDOUT)
{
rt_exception("Timed out waiting for UDP data");
break;
}
if (myHermes.Hermes_addr.sin_port == htons(12345)) {
rt_exception("Got a magic packet");
}
//check if this is a EP6 frame from metis
if (bytes_received > 0 && recvbuff[0] == (char) 0xEF && recvbuff[1] == (char) 0xFE && recvbuff[2] == (char) 0x01 && recvbuff[3] == (char) 0x06)
{
/* Calculate avg sample rate every 10 sec. UDB buffer size tuning.
//rx_seq = (recvbuff[4] & 0xFF << 24) + (recvbuff[5] & 0xFF << 16) + (recvbuff[6] & 0xFF << 8) + recvbuff[7] & 0xFF;
rx_seq++;
elapsed = GetTickCount()-start_time;
if (GetTickCount() - last_error >= 10000)
{
//rt_exception("Loosing packets!!!");
//write_text_to_log_file(std::to_string(rx_seq));
write_text_to_log_file(std::to_string(rx_seq*samplesPerFrame*2*1000/elapsed ));
last_error = GetTickCount();
rx_seq=0;
start_time = last_error;
}
//last_seq = rx_seq;
//continue;
bench */
//process UDP packet
int indx = 16; //start of samples in recvbuff
/* Debug code for checking sequence numbers
rx_seq = recvbuff[4] << 24 | recvbuff[5] << 16 | recvbuff[6] << 8 | recvbuff[7];
if ( rx_seq != last_seq+1)
{
lost_pkts++;
}
last_seq = rx_seq;
if (GetTickCount() - last_error >= 20000)
{
//rt_exception("Loosing packets!!!");
write_text_to_log_file(std::to_string(lost_pkts));
last_error = GetTickCount();
}
*/
//check for PTT bit
if ( (recvbuff[11] & 0xFF) & (1<<0) == 1 || (recvbuff[523] & 0xFF) & (1<<0) == 1 ) {
//write_text_to_log_file("PTT!");
continue;
}
//check for ADC overload
if ( (((recvbuff[11] & 0xFF) >> 3) == 0 && (recvbuff[12] & 0xFF) & (1<<0) == 1) ||
(((recvbuff[523] & 0xFF) >> 3) == 0 && (recvbuff[524] & 0xFF) & (1<<0) == 1) )
{
ADC_overflow_count++;
}
//check for sync bytes
if ( recvbuff[8] != SYNC || recvbuff[9] != SYNC || recvbuff[10] != SYNC ||
recvbuff[520] != SYNC || recvbuff[521] != SYNC || recvbuff[522] != SYNC )
{
rt_exception("Loss of Sync on HPSDR frames");
return(FALSE);
//continue;
}
//process two HPSDR UDP frames
for (k = 0; k < framesPerPacket; k++)
{
for (i = 0; i < samplesPerFrame; i++)
{
for (j = 0; j < gNChan; j++)
{
//smplI = ((recvbuff[indx++] & 0xFF) << 16) | ((recvbuff[indx++] & 0xFF) << 8) | ((recvbuff[indx++] & 0xFF) << 0);
//smplQ = ((recvbuff[indx++] & 0xFF) << 16) | ((recvbuff[indx++] & 0xFF) << 8) | ((recvbuff[indx++] & 0xFF) << 0);
//smplI = (recvbuff[indx++] << 16) | (recvbuff[indx++] << 8) | (recvbuff[indx++] << 0);
//smplQ = -((recvbuff[indx++] << 16) | (recvbuff[indx++] << 8 ) | (recvbuff[indx++] << 0));
smplI = (recvbuff[indx] & 0xFF) << 24 | (recvbuff[indx+1] & 0xFF) << 16 | (recvbuff[indx+2] & 0xFF) << 8;
smplQ = (recvbuff[indx+3] & 0xFF) << 24 | (recvbuff[indx+4] & 0xFF) << 16 | (recvbuff[indx+5] & 0xFF) << 8;
/* memory aligned access testing
union {
int dummy;
char recvbuff[5000];
} recv;
memset(recv.recvbuff,0,sizeof(recv.recvbuff));
smplI = * (int*) &recv.recvbuff[indx];
smplQ = * (int*) &recv.recvbuff[indx+4];
//smplI = (recv.recvbuff[indx] & 0xFF) << 24 | (recv.recvbuff[indx+1] & 0xFF) << 16 | (recv.recvbuff[indx+2] & 0xFF) << 8;
//smplQ = (recv.recvbuff[indx+3] & 0xFF) << 24 | (recv.recvbuff[indx+4] & 0xFF) << 16 | (recv.recvbuff[indx+5] & 0xFF) << 8;
*/
optr[j]->Re = (float)smplI;
optr[j]->Im = (float)-smplQ;
/* debug code
char s_smplI[25]={0};
char s_smplQ[25]={0};
sprintf(s_smplI, "I: %02X:%02X:%02X, %i",
recvbuff[indx-6] & 0xFF, recvbuff[indx-5] & 0xFF, recvbuff[indx-4] & 0xFF, smplI);
sprintf(s_smplQ, "Q: %02X:%02X:%02X, %i",
recvbuff[indx-3] & 0xFF, recvbuff[indx-2] & 0xFF, recvbuff[indx-1] & 0xFF, smplQ);
write_text_to_log_file(s_smplI);
//write_text_to_log_file(std::to_string(smplI));
write_text_to_log_file(s_smplQ);
//write_text_to_log_file(std::to_string(smplQ));
*/
//advance to the next sample
indx+=6;
(optr[j])++;
}
gDataSamples[0]++;
indx += 2; //skip two mic bytes
// do we have enough data ?
if (gDataSamples[0] >= gBlockInSamples)
{
// yes -> report it
gDataSamples[0] = 0;
if (gSet.pIQProc != NULL) (*gSet.pIQProc)(gSet.THandle, gData1);
// start filling of new data
for (int kount = 0; kount < gNChan; kount++) optr[kount] = gData1[kount];
}
}
//start of the second UDP frame
indx = 528;
}
}
//verify sequence #
}
Sleep(10);
return(0);
}
DWORD WINAPI Worker2(LPVOID lpParameter)
{
Cmplx *inPtr[MAX_RX_COUNT], *outPtr[MAX_RX_COUNT];
int bucket = 0;
int gNChan = myHermes.rxCount;
int samplesperframe, sync = 0;
int indx, i, rx, rx_mask, rx_filled, bytes_received, seq[MAX_RX_COUNT];
int smplI, smplQ;
rx_mask = rx_filled = 0;
for (i = 0; i < gNChan; i++)
{
rx_mask |= 1 << i;
inPtr[i] = gData1[i];
outPtr[i] = gData1[i];
gDataSamples[i] = 0;
}
char recvbuff[2048] = { 0 };
int recvbufflen = 2048;
int len = sizeof(struct sockaddr_in);
// wait for a while ...
Sleep(300);
// main loop
while (!gStopFlag)
{
// read data block
bytes_received = recvfrom(myHermes.sock, recvbuff, recvbufflen, 0, (sockaddr *)&myHermes.Hermes_addr, &len);
if (bytes_received <= 0 && WSAGetLastError() == WSAETIMEDOUT)
{
rt_exception("Timed out waiting for UDP data 2");
break;
}
int sourceport = ntohs(myHermes.Hermes_addr.sin_port);
switch (sourceport) {
case MICROPHONE_DATA_TO_HOST: // use to sync to start of DDC data
sync = 1;
continue;
// case HIGH_PRIORITY_TO_HOST_PORT:
// if (recvbuff[5] & 1) ADC_overflow_count++;
// continue;
case RX_IQ_TO_HOST_PORT_0:
case RX_IQ_TO_HOST_PORT_1:
case RX_IQ_TO_HOST_PORT_2:
case RX_IQ_TO_HOST_PORT_3:
case RX_IQ_TO_HOST_PORT_4:
case RX_IQ_TO_HOST_PORT_5:
case RX_IQ_TO_HOST_PORT_6:
case RX_IQ_TO_HOST_PORT_7:
if (!sync) continue;
rx = sourceport - RX_IQ_TO_HOST_PORT_0;
samplesperframe = ((recvbuff[14] & 0xFF) << 8) + (recvbuff[15] & 0xFF);
//DEBUG
if (rx > gNChan || rx < 0)
{
write_text_to_log_file("rx");
write_text_to_log_file(std::to_string(rx));
continue;
}
if (samplesperframe != 238)
{
write_text_to_log_file("samplesperframe");
write_text_to_log_file(std::to_string(samplesperframe));
continue;
}
#if 0
i = (recvbuff[0] & 0xFF) << 24 | (recvbuff[1] & 0xFF) << 16 | (recvbuff[2] & 0xFF) << 8 | recvbuff[3] & 0xFF;
if (seq[rx] + 1 != i)
{
seq[rx] = i;
continue;
}
seq[rx] = i;
#endif
for (i = 0, indx = 16; i < samplesperframe; i++, indx += 6)
{
smplI = (recvbuff[indx] & 0xFF) << 24 | (recvbuff[indx + 1] & 0xFF) << 16 | (recvbuff[indx + 2] & 0xFF) << 8;
smplQ = (recvbuff[indx + 3] & 0xFF) << 24 | (recvbuff[indx + 4] & 0xFF) << 16 | (recvbuff[indx + 5] & 0xFF) << 8;
inPtr[rx]->Re = (float)smplI;
inPtr[rx]->Im = (float)-smplQ;
// The protocol2 sends each rx's data in a separate packet so we need to store each rx's data
// until we have all of them. The double buffer was an attempt to not loose any data when packets
// got out of sequence.
(inPtr[rx])++;
gDataSamples[rx]++;
// wait and set when each rx reaches bucket full
if (gDataSamples[rx] == gBlockInSamples)
{
// if not already filled, fill and switch the bucket
if (!(rx_filled & (1 << rx)))
{
rx_filled |= 1 << rx;
bucket ^= 1 << rx;
}
// double buffer and keep track of which one is full
if (bucket & (1 << rx))
{
inPtr[rx] = gData2[rx];
outPtr[rx] = gData1[rx];
}
else
{
inPtr[rx] = gData1[rx];
outPtr[rx] = gData2[rx];
}
gDataSamples[rx] = 0;
}
// wait until we have all active rx bucket's filled
if (rx_filled == rx_mask)
{
// send it off
if (gSet.pIQProc != NULL) (*gSet.pIQProc)(gSet.THandle, outPtr);
rx_filled = 0;
//sync = 0;
}
}
break;
default:
break;
}
}
//Sleep(10);
return(0);
}
// DllMain function - Do we need this?
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
for (int i = 0; i < MAX_RX_COUNT; i++)
{
gData1[i] = NULL;
gData2[i] = NULL;
}
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C"
{
HERMESINTF_API void __stdcall GetSdrInfo(PSdrInfo pInfo)
{
// did we get info ?
if (pInfo == NULL) return;
std::string dbg = "GetSdrInfo: ";
if (myHermes.Discover() == 0) {
//sprintf(display_name, "%s v%d %s", myHermes.devname, myHermes.ver, myHermes.mac);
sprintf(display_name, "%s-%s v%d", myHermes.devname, myHermes.mac, myHermes.ver);
//workaround for hermes GUI bug. Force two min receivers
if (myHermes.max_recvrs == 1 && myHermes.prot_ver == 1)
{
pInfo->MaxRecvCount = 2;
}
else
{
pInfo->MaxRecvCount = myHermes.max_recvrs;
}
if (strcmp(myHermes.devname, AFEDRI) == 0) {
pInfo->ExactRates[RATE_48KHZ] = calculate_afedri_sr(48e3);
pInfo->ExactRates[RATE_96KHZ] = calculate_afedri_sr(96e3);
pInfo->ExactRates[RATE_192KHZ] = calculate_afedri_sr(192e3);
std::stringstream sstm;
sstm << "Afedri clock: " << myHermes.clock;
sstm << " Exact Sample rates: " << pInfo->ExactRates[RATE_48KHZ] << " "
<< pInfo->ExactRates[RATE_96KHZ] << " "
<< pInfo->ExactRates[RATE_192KHZ];
write_text_to_log_file(sstm.str());
}
else if (strcmp(myHermes.devname, RTLDNGL) == 0) {
pInfo->ExactRates[RATE_48KHZ] = myHermes.sample_rates[0];
pInfo->ExactRates[RATE_96KHZ] = myHermes.sample_rates[1];
pInfo->ExactRates[RATE_192KHZ] = myHermes.sample_rates[2];
std::stringstream sstm;
sstm << "RTL Dongle Exact Sample Rates "
<< pInfo->ExactRates[RATE_48KHZ] << " "
<< pInfo->ExactRates[RATE_96KHZ] << " "
<< pInfo->ExactRates[RATE_192KHZ];
write_text_to_log_file(sstm.str());
} else {
pInfo->ExactRates[RATE_48KHZ] = 48e3;
pInfo->ExactRates[RATE_96KHZ] = 96e3;
pInfo->ExactRates[RATE_192KHZ] = 192e3;
}
dbg+=(myHermes.devname); dbg+=" ";
dbg+=(myHermes.ip_addr); dbg+=" ";
dbg+=(myHermes.mac); dbg+=" ";
dbg+=(myHermes.status); dbg+=" protocol";
dbg+=(myHermes.prot_ver==1?"1":"2"); dbg += " v";
#ifdef __MINGW32__
dbg+=(patch::to_string(myHermes.ver));
#else
dbg+=(std::to_string(myHermes.ver));
#endif
write_text_to_log_file(dbg);
pInfo->DeviceName = display_name;
} else {
pInfo->DeviceName = (char *) UNKNOWN_HPSDR;
dbg += "No response from HPSDR";
}
write_text_to_log_file(dbg);
}
// Start receivers
HERMESINTF_API void __stdcall StartRx(PSdrSettings pSettings)
{
// have we settings ?
if (pSettings == NULL) return;
// make a copy of SDR settings
memcpy(&gSet, pSettings, sizeof(gSet));
// from skimmer server version 1.1 in high bytes is something strange
gSet.RateID &= 0xFF;
if (myHermes.status == NULL) {
rt_exception("Can't locate HPSDR device");
return;
} else if (myHermes.status != IDLE) {
rt_exception("HPSDR is busy sending data");
return;
} else if ((myHermes.devname == HERMES && (myHermes.ver != 18 && myHermes.ver < 24))
|| (myHermes.devname == METIS && myHermes.ver < 26)
|| (myHermes.devname == ANGELIA && myHermes.ver < 19))
{
rt_exception("Check FPGA firmware version");
return;
}
if (gSet.RecvCount > myHermes.max_recvrs)
{
rt_exception("Too many receivers selected");
return;
}
(myHermes.prot_ver == 1) ? myHermes.StartCapture(gSet.RecvCount,gSet.RateID) : myHermes.StartCapture2(gSet.RecvCount,gSet.RateID);
write_text_to_log_file("StartRx");
// allocate buffers & others
if (!Alloc())
{
// something wrong ...
return;
}
// start worker thread
gStopFlag = false;
ghWrk = CreateThread(NULL, 0, (myHermes.prot_ver == 1) ? Worker : Worker2, NULL, 0, &gidWrk);
if (ghWrk == NULL)
{
// can't start
rt_exception("Can't start worker thread");
return;
}
write_text_to_log_file("Worker thread running");
//for Hermes/Angelia start the AGC loop
if (myHermes.devname == HERMES || myHermes.devname == ANGELIA || myHermes.devname == ORION ||
myHermes.devname == ORION2 || myHermes.devname == SATURN || myHermes.devname == ANAN10E) {
ghAgc = CreateThread(NULL, 0, Agc, NULL, 0, &gidAgc);
if (ghAgc == NULL)
{
rt_exception("Can't start AGC thread");
return;
}
write_text_to_log_file("AGC thread running");
}
}
HERMESINTF_API void __stdcall StopRx(void)
{
// was started worker thread ?
if (ghWrk != NULL)
{// set stop flag
gStopFlag = true;
// wait for threads
WaitForSingleObject(ghWrk, 100);
// close thread handle
CloseHandle(ghWrk);
ghWrk = NULL;
write_text_to_log_file("Worker thread done");
}
//was agc thread started?
if (ghAgc != NULL)
{// set stop flag
gStopFlag = true;
// wait for thread
WaitForSingleObject(ghAgc, 200);
// close thread handle
CloseHandle(ghAgc);
ghAgc = NULL;
write_text_to_log_file("AGC thread done");
}
(myHermes.prot_ver == 1) ? myHermes.StopCapture() : myHermes.StopCapture2();
write_text_to_log_file("StopRx");
}
// Set Rx frequency
HERMESINTF_API void __stdcall SetRxFrequency(int Frequency, int Receiver)
{
// check slave mode
//if (myHermes.SlaveMode == TRUE) {
// rt_exception("Slave mode...");
// return;
//}
//if ((myHermes.devname == NULL) || (Receiver < 0) || (Receiver >= myHermes.max_recvrs))
if ((Receiver < 0) || (Receiver >= myHermes.max_recvrs))
{
rt_exception("Too many receivers selected");
return;
}
(myHermes.prot_ver == 1) ? myHermes.SetLO(Receiver,Frequency) : myHermes.SetLO2(Receiver,Frequency);
std::string dbg = "SetRxFrequency Rx# ";
#ifdef __MINGW32__
dbg += patch::to_string(Receiver);
#else
dbg += std::to_string(Receiver);
#endif
dbg += " Frequency: ";
#ifdef __MINGW32__
dbg += patch::to_string(Frequency);
#else
dbg += std::to_string(Frequency);
#endif
write_text_to_log_file(dbg);
}
HERMESINTF_API int __stdcall ReadPort(int PortNumber)
{
return(0);
}
void __stdcall SetCtrlBits(unsigned char Bits)
{
}
}
void write_text_to_log_file( const std::string &text )
{
SYSTEMTIME st;
GetLocalTime(&st);
char buffer[30];
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d.%03d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
std::ofstream log_file(
"HermesIntf_log_file.txt", std::ios_base::out | std::ios_base::app );
//log_file << GetTickCount() << ": " << text << std::endl;
log_file << buffer << ": " << text << std::endl;
}
void rt_exception(const std::string &text)
{
const char *error = text.c_str();
if (gSet.pErrorProc != NULL) (*gSet.pErrorProc)(gSet.THandle, (char *) error);
write_text_to_log_file(text);
//kill thread if running
//gStopFlag = true;
//StopRx();
//send error to the server
//if (gSet.pErrorProc != NULL) (*gSet.pErrorProc)(gSet.THandle, (char *) error);
return;
}
float calculate_afedri_sr(float sr)
{
float dSR = sr;
float tmp_div = 0;
float floor_div = 0;
float floor_SR = 0;
tmp_div = myHermes.clock / (4 * dSR);
floor_div = floor(tmp_div);
if ((tmp_div - floor_div) >= 0.5) floor_div += 1;
dSR = myHermes.clock / (4 * floor_div);
floor_SR = floor(dSR);
if (dSR - floor_SR >= 0.5) floor_SR += 1;
return floor_SR;
}
}