-
Notifications
You must be signed in to change notification settings - Fork 15
/
ogn-rf.cc
1198 lines (1028 loc) · 58.1 KB
/
ogn-rf.cc
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
OGN - Open Glider Network - http://glidernet.org/
Copyright (c) 2015 The OGN Project
A detailed list of copyright holders can be found in the file "AUTHORS".
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 3 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 software. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <limits.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libconfig.h>
#include <algorithm>
#include "thread.h" // multi-thread stuff
#include "fft.h" // Fast Fourier Transform
#include "rtlsdr.h" // SDR radio
#define QUOTE(name) #name
#define STR(macro) QUOTE(macro)
#ifndef VERSION
#define VERSION 0.0.0
#endif
#include "freqplan.h"
#include "jpeg.h"
#include "socket.h"
#include "sysmon.h"
#include "pulsefilter.h"
#include "tonefilter.h"
#include "dataserver.h"
// ==================================================================================================
template <class Float> // scale floating-point data to 8-bit gray scale image
void LogImage(SampleBuffer<uint8_t> &Image, SampleBuffer<Float> &Data, Float LogRef=0, Float Scale=1, Float Bias=0)
{ Image.Allocate(Data);
int Pixels=Data.Full;
for(int Idx=0; Idx<Pixels; Idx++)
{ Float Pixel=Data.Data[Idx];
if(LogRef)
{ if(Pixel) { Pixel=logf((Float)Pixel/LogRef); Pixel = Pixel*Scale + Bias; }
else { Pixel=0; } }
else
{ Pixel = Pixel*Scale + Bias; }
if(Pixel<0x00) Pixel=0x00;
else if(Pixel>0xFF) Pixel=0xFF;
Image.Data[Idx]=(uint8_t)Pixel;
}
Image.Full=Pixels;
}
// ==================================================================================================
class RF_Acq // acquire wideband (1MHz) RF data thus both OGN frequencies at same time
{ public:
int SampleRate; // [Hz] sampling rate
int OGN_GainMode; // 0=Auto, 1=Manual, 2=Linearity, 3=Sensitivity
int OGN_Gain; // [0.1dB] Rx gain for OGN reception
double OGN_StartTime; // [sec] when to start acquisition on the center frequency
int OGN_SamplesPerRead; // [samples] should correspond to about 800 ms of data and be a multiple of 256
// the goal is to listen on center frequency from 0.4 to 1.2 sec
FreqPlan HoppingPlan; // frequency hopping plan (depends on the world region)
int DeviceIndex; // rtl-sdr device index
char DeviceSerial[64]; // serial number of the rtl-sdr device to be selected
int OffsetTuning; // [bool] this option might be good for E4000 tuner
int BiasTee; // [bool] T-bias for external LNA power
int FreqCorr; // [ppm] frequency correction applied to the Rx chip
int FreqRaster; // [Hz] use only center frequencies on this raster to avoid tuning inaccuracies
RTLSDR SDR; // SDR receiver (DVB-T stick)
ReuseObjectQueue< SampleBuffer<uint8_t> > OutQueue; // OGN sample batches are sent there
Thread Thr; // acquisition thread
volatile int StopReq; // request to stop the acquisition thread
PulseFilter PulseFilt;
static const int GSM_GainMode = 1; // Manual gain mode for GSM
int GSM_Gain; // [0.1dB] Rx gain for GSM frequency calibration
int GSM_CenterFreq; // [Hz] should be selected to cover at lease one broadcast channel in the area
int GSM_Scan; // [bool] scan around the whole GSM band
int GSM_SamplesPerRead; // [samples] should cover one or more frequency correction bursts (100 ms should be enough ?)
volatile float GSM_FreqCorr; // [ppm] frequency correction measured by the GSM frequency calibration
static const int GSM_LowEdge = 925100000; // [Hz] E-GSM-900 band, excluding the guards of 100kHz
static const int GSM_UppEdge = 959900000; // [Hz]
static const int GSM_ScanStep = 800000; // [Hz]
ReuseObjectQueue< SampleBuffer<uint8_t> > GSM_OutQueue; // GSM sample batches are sent there
const static uint32_t OGN_RawDataSync = 0x254F7D01;
char FilePrefix[16];
int OGN_SaveRawData;
MessageQueue<Socket *> RawDataQueue; // sockets send to this queue should be written with a most recent raw data
MessageQueue<Socket *> SpectrogramQueue; // sockets send to this queue should be written with a most recent spectrogram
DFT1d<float> SpectrogramFFT; // FFT to create spectrograms
int SpectrogramFFTsize;
float *SpectrogramWindow;
SampleBuffer< std::complex<float> > SpectraBuffer;
SampleBuffer<float> SpectraPwr;
SampleBuffer<uint8_t> Image;
JPEG JpegImage;
time_t StartTime;
uint32_t CountAllTimeSlots;
uint32_t CountLifeTimeSlots;
public:
RF_Acq() { Config_Defaults();
GSM_FreqCorr=0;
// PulseBox.Preset(PulseBoxSize);
SpectrogramWindow=0;
StartTime=0; CountAllTimeSlots=0; CountLifeTimeSlots=0;
StopReq=0; Thr.setExec(ThreadExec); }
~RF_Acq() { if(SpectrogramWindow) free(SpectrogramWindow); }
double getLifeTime(void)
{ time_t Now; time(&Now); if(Now<=StartTime) return 0;
return 0.5*CountLifeTimeSlots/(Now-StartTime); }
void Config_Defaults(void)
{ SampleRate=1000000;
OGN_StartTime=0.375; OGN_SamplesPerRead=(850*SampleRate)/1000;
OGN_GainMode=1; OGN_Gain=600;
HoppingPlan.setPlan(0);
PulseFilt.Threshold=0;
DeviceIndex=0; DeviceSerial[0]=0;
OffsetTuning=0; FreqCorr=0; FreqRaster=28125; BiasTee=(-1);
// GSM_CenterFreq=GSM_LowEdge+GSM_ScanStep/2; GSM_Scan=1; GSM_SamplesPerRead=(250*SampleRate)/1000; GSM_Gain=200;
GSM_CenterFreq=0; GSM_Scan=0; GSM_Gain=200;
SpectrogramFFTsize=0;
OGN_SaveRawData=0;
FilePrefix[0]=0; }
int config_lookup_float_or_int(config_t *Config, const char *Path, double *Value)
{ int Ret = config_lookup_float(Config, Path, Value); if(Ret==CONFIG_TRUE) return Ret;
int IntValue; Ret = config_lookup_int(Config, Path, &IntValue); if(Ret==CONFIG_TRUE) { (*Value) = IntValue; return Ret; }
return Ret; }
int Config(config_t *Config)
{ const char *Call=0;
config_lookup_string(Config,"APRS.Call", &Call);
if(Call) strcpy(FilePrefix, Call);
double Corr=0.0;
config_lookup_float_or_int(Config, "RF.FreqCorr", &Corr); FreqCorr = (int)floor(Corr+0.5);
config_lookup_int(Config, "RF.FreqRaster", &FreqRaster);
config_lookup_int(Config, "RF.Device", &DeviceIndex);
const char *Serial = 0;
config_lookup_string(Config,"RF.DeviceSerial", &Serial);
if(Serial) { strncpy(DeviceSerial, Serial, 64); DeviceSerial[63]=0; }
config_lookup_int(Config, "RF.OfsTune", &OffsetTuning);
config_lookup_int(Config, "RF.BiasTee", &BiasTee);
config_lookup_int(Config, "RF.OGN.GainMode", &OGN_GainMode);
config_lookup_int(Config, "RF.OGN.SaveRawData", &OGN_SaveRawData);
SampleRate=1000000;
if(config_lookup_int(Config, "RF.OGN.SampleRate", &SampleRate)!=CONFIG_TRUE)
{ double Rate;
if(config_lookup_float(Config, "RF.OGN.SampleRate", &Rate)==CONFIG_TRUE) SampleRate=(int)floor(1e6*Rate+0.5);
else if(config_lookup_int(Config, "RF.SampleRate", &SampleRate)!=CONFIG_TRUE)
{ if(config_lookup_float(Config, "RF.SampleRate", &Rate)==CONFIG_TRUE) SampleRate=(int)floor(1e6*Rate+0.5); }
}
double InpGain= 60.0; config_lookup_float_or_int(Config, "RF.OGN.Gain", &InpGain); OGN_Gain=(int)floor(InpGain*10+0.5);
InpGain= 20.0; config_lookup_float_or_int(Config, "RF.GSM.Gain", &InpGain); GSM_Gain=(int)floor(InpGain*10+0.5);
double Freq = 0; config_lookup_float_or_int(Config, "RF.GSM.CenterFreq", &Freq); GSM_CenterFreq=(int)floor(Freq*1e6+0.5);
GSM_Scan = 0; config_lookup_int(Config, "RF.GSM.Scan", &GSM_Scan);
int PosOK=0;
int Latitude, Longitude;
if(config_lookup_int(Config, "Position.Latitude", &Latitude)!=CONFIG_TRUE)
{ double Lat;
if(config_lookup_float(Config, "Position.Latitude", &Lat)==CONFIG_TRUE)
{ Latitude=(int)floor(Lat*1e7+0.5); }
else PosOK=(-1); }
if(config_lookup_int(Config, "Position.Longitude", &Longitude)!=CONFIG_TRUE)
{ double Lon;
if(config_lookup_float(Config, "Position.Longitude", &Lon)==CONFIG_TRUE)
{ Longitude=(int)floor(Lon*1e7+0.5); }
else PosOK=(-1); }
int Plan=0;
config_lookup_int(Config, "RF.FreqPlan", &Plan);
if( (Plan==0) && (PosOK>=0) )
{ Plan=HoppingPlan.calcPlan(Latitude/50*3, Longitude/50*3); } // decide hopping plan from position
HoppingPlan.setPlan(Plan);
PulseFilt.Threshold=0;
config_lookup_int(Config, "RF.PulseFilter.Threshold", &PulseFilt.Threshold);
config_lookup_float(Config, "RF.OGN.StartTime", &OGN_StartTime);
double SensTime=0.850;
config_lookup_float(Config, "RF.OGN.SensTime", &SensTime);
OGN_SamplesPerRead=(int)floor(SensTime*SampleRate+0.5);
SensTime=0.250;
config_lookup_float(Config, "RF.GSM.SensTime", &SensTime);
GSM_SamplesPerRead=(int)floor(SensTime*SampleRate+0.5);
SpectrogramFFTsize=(8*SampleRate)/15625;
SpectrogramFFT.PresetForward(SpectrogramFFTsize);
SpectrogramWindow=(float *)realloc(SpectrogramWindow, SpectrogramFFTsize*sizeof(float));
SpectrogramFFT.SetSineWindow(SpectrogramWindow, SpectrogramFFTsize, (float)(1.0/sqrt(SpectrogramFFTsize)) );
return 0; }
int QueueSize(void) { return OutQueue.Size(); }
int Start(void) { StopReq=0; return Thr.Create(this); }
int Stop(void) { StopReq=1; return Thr.Join(); }
static void *ThreadExec(void *Context)
{ RF_Acq *This = (RF_Acq *)Context; return This->Exec(); }
void *Exec(void)
{ // printf("RF_Acq.Exec() ... Start\n");
time(&StartTime); CountAllTimeSlots=0; CountLifeTimeSlots=0;
char Header[256];
int Priority = Thr.getMaxPriority(); Thr.setPriority(Priority);
int CurrCenterFreq = calcCenterFreq(0);
while(!StopReq)
{ if(SDR.isOpen()) // if device is already open
{ double Now = SDR.getTime();
int IntTimeNow = (int)floor(Now);
int ReadGSM = (GSM_CenterFreq>0) && ((IntTimeNow%30) == 0); // do the GSM calibration every 30 seconds
int NextCenterFreq = calcCenterFreq(IntTimeNow+1); // next center frequency for OGN
double FracTimeNow = Now-IntTimeNow;
double WaitTime = OGN_StartTime-FracTimeNow; if(WaitTime<0) WaitTime+=1.0;
int SamplesToRead=OGN_SamplesPerRead;
int LifeSlots=2;
if( ReadGSM || (QueueSize()>1) ) { SamplesToRead/=2; LifeSlots=1; } // when GSM calibration or data is not being processed fast enough we only read half-time
if(WaitTime<0.200)
{ usleep((int)floor(1e6*WaitTime+0.5)); // wait right before the time slot starts
SampleBuffer<uint8_t> *Buffer = OutQueue.New(); // get the next buffer to fill with raw I/Q data
SDR.ResetBuffer(); // needed before every Read()
int Read=SDR.Read(*Buffer, SamplesToRead); // read the time slot raw RF data
if(Read>0) // RF data Read() successful
{ Buffer->Freq += Buffer->Freq * (1e-6*GSM_FreqCorr); // correct the frequency (sign ?)
if(OGN_SaveRawData>0)
{ time_t Time=(time_t)floor(Buffer->Time);
struct tm *TM = gmtime(&Time);
char FileName[32]; sprintf(FileName, "%s_%04d.%02d.%02d.u8", FilePrefix, 1900+TM->tm_year, TM->tm_mon+1, TM->tm_mday);
FILE *File=fopen(FileName, "ab");
if(File)
{ Serialize_WriteSync(File, OGN_RawDataSync);
Buffer->Serialize(File);
fclose(File);
OGN_SaveRawData--; }
}
PulseFilt.Process(*Buffer);
if(QueueSize()>1) printf("RF_Acq.Exec() ... Half time slot\n");
// printf("RF_Acq.Exec() ... SDR.Read() => %d, Time=%16.3f, Freq=%6.1fMHz\n", Read, Buffer->Time, 1e-6*Buffer->Freq);
while(RawDataQueue.Size()) // when a raw data for this slot was requested
{ Socket *Client; RawDataQueue.Pop(Client);
sprintf(Header, "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nContent-Type: audio/basic\r\n\
Content-Disposition: attachment; filename=\"%s_%07.3fMHz_%03.1fMsps_%14.3fsec.u8\"\r\n\r\n", FilePrefix, 1e-6*Buffer->Freq, 1e-6*Buffer->Rate, Buffer->Time);
Client->Send(Header);
Client->Send(Buffer->Data, Buffer->Full);
Client->SendShutdown(); Client->Close(); delete Client; }
if(SpectrogramQueue.Size())
{ SlidingFFT(SpectraBuffer, *Buffer, SpectrogramFFT, SpectrogramWindow);
SpectraPower(SpectraPwr, SpectraBuffer); // calc. spectra power
float BkgNoise=0.33;
LogImage(Image, SpectraPwr, (float)BkgNoise, (float)32.0, (float)32.0); // make the image
JpegImage.Compress_MONO8(Image.Data, Image.Len, Image.Samples() ); } // and into JPEG
while(SpectrogramQueue.Size())
{ Socket *Client; SpectrogramQueue.Pop(Client);
sprintf(Header, "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nContent-Type: image/jpeg\r\nRefresh: 5\r\n\
Content-Disposition: attachment; filename=\"%s_%07.3fMHz_%03.1fMsps_%10dsec.jpg\"\r\n\r\n",
FilePrefix, 1e-6*SpectraBuffer.Freq, 1e-6*SpectraBuffer.Rate*SpectraBuffer.Len/2, (uint32_t)floor(SpectraBuffer.Date+SpectraBuffer.Time));
Client->Send(Header);
Client->Send(JpegImage.Data, JpegImage.Size);
Client->SendShutdown(); Client->Close(); delete Client; }
if(OutQueue.Size()<4) { OutQueue.Push(Buffer); CountLifeTimeSlots+=LifeSlots; }
else { OutQueue.Recycle(Buffer); printf("RF_Acq.Exec() ... Dropped a slot\n"); }
} else // RF data Read() failed
{ SDR.Close(); printf("RF_Acq.Exec() ... SDR.Read() failed => SDR.Close()\n"); continue; }
if(ReadGSM) // if we are to read GSM in the second half-slot
{ SDR.setCenterFreq(GSM_CenterFreq); // setup for the GSM reception
SDR.setTunerGainMode(GSM_GainMode);
SDR.setTunerGain(GSM_Gain);
GSM_FreqCorr-=(FreqCorr-SDR.getFreqCorrection()); // this is just in case someone changed the frequency correction live
SDR.setFreqCorrection(FreqCorr);
SampleBuffer<uint8_t> *Buffer = GSM_OutQueue.New();
SDR.ResetBuffer();
int Read=SDR.Read(*Buffer, GSM_SamplesPerRead);
// printf("RF_Acq.Exec() ...(GSM) SDR.Read() => %d, Time=%16.3f, Freq=%6.1fMHz\n", Read, Buffer->Time, 1e-6*Buffer->Freq);
if(Read>0)
{ if(GSM_OutQueue.Size()<3) GSM_OutQueue.Push(Buffer);
else { GSM_OutQueue.Recycle(Buffer); printf("RF_Acq.Exec() ... Dropped a GSM batch\n"); }
}
SDR.setTunerGainMode(OGN_GainMode);
SDR.setTunerGain(OGN_Gain); // back to OGN reception setup
if(GSM_Scan)
{ GSM_CenterFreq+=GSM_ScanStep;
if(GSM_CenterFreq>=GSM_UppEdge) GSM_CenterFreq=GSM_LowEdge+GSM_ScanStep/2;
}
}
// if(ReadGSM | OGN_FreqHopChannels)
{ SDR.setCenterFreq(NextCenterFreq); CurrCenterFreq=NextCenterFreq; }
}
else usleep(100000);
}
else // if not open yet or was closed due to an error
{ int Index=(-1);
if(DeviceSerial[0]) Index=SDR.getDeviceIndexBySerial(DeviceSerial);
if(Index<0) Index=DeviceIndex;
SDR.FreqRaster = FreqRaster;
if(SDR.Open(Index, CurrCenterFreq, SampleRate)<0) // try to open it
{ printf("RF_Acq.Exec() ... SDR.Open(%d, , ) fails, retry after 1 sec\n", Index); usleep(1000000); }
else
{ SDR.setOffsetTuning(OffsetTuning);
if(BiasTee>=0) SDR.setBiasTee(BiasTee);
SDR.setTunerGainMode(OGN_GainMode);
SDR.setTunerGain(OGN_Gain);
SDR.setFreqCorrection(FreqCorr); }
}
}
SDR.Close();
// printf("RF_Acq.Exec() ... Stop\n");
return 0; }
int calcCenterFreq(uint32_t Time)
{ int HopFreq[4];
HopFreq[0] = HoppingPlan.getFrequency(Time, 0, 0);
HopFreq[1] = HoppingPlan.getFrequency(Time, 0, 1);
HopFreq[2] = HoppingPlan.getFrequency(Time, 1, 0);
HopFreq[3] = HoppingPlan.getFrequency(Time, 1, 1);
int MidFreq0 = (HopFreq[0]+HopFreq[1]+1)>>1;
// int MidFreq1 = (HopFreq[2]+HopFreq[3]+1)>>1;
// int MidFreq = (MidFreq0+MidFreq1+1)>>1;
// int FreqDiff = MidFreq1-MidFreq0;
int CenterFreq = MidFreq0;
int Band = SampleRate-150000;
std::sort(HopFreq, HopFreq+4);
// if(abs(FreqDiff)<HalfBand) CenterFreq = MidFreq;
if((HopFreq[3]-HopFreq[0])<Band) CenterFreq=(HopFreq[0]+HopFreq[3]+1)>>1;
else if((HopFreq[2]-HopFreq[0])<Band) CenterFreq=(HopFreq[0]+HopFreq[2]+1)>>1;
else if((HopFreq[3]-HopFreq[1])<Band) CenterFreq=(HopFreq[1]+HopFreq[3]+1)>>1;
// printf("calcCenterFreq(%d): %5.1f-%5.1f-%5.1f-%5.1f [%5.1f] => %5.1f [MHz] %c\n",
// Time, 1e-6*HopFreq[0], 1e-6*HopFreq[1], 1e-6*HopFreq[2], 1e-6*HopFreq[3], 1e-6*CenterFreq,
// 1e-6*(HopFreq[3]-HopFreq[0]), CenterFreq!=MidFreq0?'*':' ');
return CenterFreq; }
} ;
// ==================================================================================================
template <class Float>
class Inp_Filter
{ public:
Thread Thr; // processing thread
volatile int StopReq;
RF_Acq *RF;
int Enable;
ToneFilter<Float> ToneFilt;
ReuseObjectQueue< SampleBuffer< std::complex<Float> > > OutQueue;
public:
Inp_Filter(RF_Acq *RF)
{ this->RF=RF; Config_Defaults(); Preset(); }
void Config_Defaults(void)
{ Enable = 0; ToneFilt.FFTsize = 32768; ToneFilt.Threshold=32; }
int Config(config_t *Config)
{ config_lookup_int(Config, "RF.ToneFilter.Enable", &Enable);
config_lookup_int(Config, "RF.ToneFilter.FFTsize", &ToneFilt.FFTsize);
config_lookup_float(Config, "RF.ToneFilter.Threshold", &ToneFilt.Threshold);
return 0; }
int Preset(void) { return ToneFilt.Preset(); }
int QueueSize(void) { return OutQueue.Size(); }
void Start(void)
{ StopReq=0; Thr.setExec(ThreadExec); Thr.Create(this); }
~Inp_Filter()
{ Thr.Cancel(); }
double getCPU(void) // get CPU time for this thread
{
#if !defined(__MACH__)
struct timespec now; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now); return now.tv_sec + 1e-9*now.tv_nsec;
#else
return 0;
#endif
}
static void *ThreadExec(void *Context)
{ Inp_Filter<Float> *This = (Inp_Filter<Float> *)Context; return This->Exec(); }
void *Exec(void)
{ // printf("Inp_Filter.Exec() ... Start\n");
while(!StopReq)
{ if(!Enable) { sleep(1); continue; }
double ExecTime=getCPU();
SampleBuffer<uint8_t> *InpBuffer = RF->OutQueue.Pop(); // here we wait for a new data batch
// printf("Inp_Filter.Exec() ... Input(%5.3fMHz, %5.3fsec, %dsamples)\n", 1e-6*InpBuffer->Freq, InpBuffer->Time, InpBuffer->Full/2);
SampleBuffer< std::complex<Float> > *OutBuffer = OutQueue.New();
ToneFilt.Process(OutBuffer, InpBuffer);
RF->OutQueue.Recycle(InpBuffer); // let the input buffer go free
// printf("Inp_Filter.Exec() ... Output(%5.3fMHz, %5.3fsec, %dsamples)\n", 1e-6*OutBuffer->Freq, OutBuffer->Time, OutBuffer->Full/2);
if(OutQueue.Size()<4) { OutQueue.Push(OutBuffer); }
else { OutQueue.Recycle(OutBuffer); printf("Inp_Filter.Exec() ... Dropped a slot\n"); }
ExecTime=getCPU()-ExecTime; // printf("Inp_FFT.Exec() ... %5.3fsec\n", ExecTime);
}
// printf("Inp_FFT.Exec() ... Stop\n");
return 0; }
// classical sliding box filter - calc. the sum within box of 2*Radius+1
static void BoxSum(Float *Output, Float *Input, int Size, int Radius)
{ int BoxSize=2*Radius+1;
Float Sum=0; int InpIdx=0; int OutIdx=0;
for( ; InpIdx<Radius; InpIdx++)
{ Sum+=Input[InpIdx]; }
for( ; InpIdx<BoxSize; InpIdx++)
{ Sum+=Input[InpIdx]; Output[OutIdx++]=Sum; }
for( ; InpIdx<Size; InpIdx++)
{ Sum+=Input[InpIdx]-Input[InpIdx-BoxSize]; Output[OutIdx++]=Sum; }
for( ; OutIdx<Size; )
{ Sum-=Input[InpIdx-BoxSize]; Output[OutIdx++]=Sum; }
}
} ;
// ==================================================================================================
template <class Float>
class Inp_FFT // FFT of the RF data
{ public:
Thread Thr; // processing thread
volatile int StopReq;
RF_Acq *RF;
Inp_Filter<Float> *Filter;
int FFTsize;
#ifdef USE_RPI_GPU_FFT
RPI_GPU_FFT FFT;
#else
DFT1d<Float> FFT;
#endif
Float *Window;
SampleBuffer< std::complex<Float> > OutBuffer;
char OutPipeName[32]; // name of the pipe to send the RF data (as FFT) to the demodulator and decoder.
int OutPipe;
TCP_DataServer DataServer;
const static uint32_t OutPipeSync = 0x254F7D00 + sizeof(Float);
public:
Inp_FFT(RF_Acq *RF, Inp_Filter<Float> *Filter=0)
{ Window=0; this->RF=RF; this->Filter=Filter; Preset(); OutPipe=(-1); Config_Defaults(); }
void Config_Defaults(void)
{ strcpy(OutPipeName, "ogn-rf.fifo"); }
int Config(config_t *Config)
{ const char *PipeName = "ogn-rf.fifo";
config_lookup_string(Config, "RF.PipeName", &PipeName);
strcpy(OutPipeName, PipeName);
return 0; }
int Preset(void) { return Preset(RF->SampleRate); }
int Preset(int SampleRate)
{ FFTsize=(8*8*SampleRate)/15625;
FFT.PresetForward(FFTsize);
Window=(Float *)realloc(Window, FFTsize*sizeof(Float));
FFT.SetSineWindow(Window, FFTsize, (Float)(1.0/sqrt(FFTsize)) );
return 1; }
int SerializeSpectra(int OutPipe)
{ int Len=Serialize_WriteSync(OutPipe, OutPipeSync);
if(Len>=0) { Len=Serialize_WriteName(OutPipe, "FreqCorr"); }
if(Len>=0) { Len=Serialize_WriteData(OutPipe, (void *)&(RF->FreqCorr), sizeof(int) ); }
if(Len>=0) { Len=Serialize_WriteData(OutPipe, (void *)&(RF->GSM_FreqCorr), sizeof(float) ); }
if(Len>=0) { Len=Serialize_WriteSync(OutPipe, OutPipeSync); }
if(Len>=0) { Len=Serialize_WriteName(OutPipe, "Spectra"); }
if(Len>=0) { Len=OutBuffer.Serialize(OutPipe); }
return Len; }
int WriteToPipe(void) // write OutBuffer to the output pipe
{ if( (OutPipe<0) && (!DataServer.isListenning()) )
{ const char *Colon=strchr(OutPipeName, ':');
if(Colon)
{ int Port=atoi(Colon+1);
if(DataServer.Listen(Port)<0)
printf("Inp_FFT.Exec() ... cannot open data server on port %d\n", Port);
else
printf("Inp_FFT.Exec() ... data server listenning on port %d\n", Port);
}
else
{ OutPipe=open(OutPipeName, O_WRONLY);
if(OutPipe<0)
{ printf("Inp_FFT.Exec() ... Cannot open %s\n", OutPipeName);
// here we could try to create the missing pipe
if(mkfifo(OutPipeName, 0666)<0)
printf("Inp_FFT.Exec() ... Cannot create %s\n", OutPipeName);
else
{ printf("Inp_FFT.Exec() ... %s has been created\n", OutPipeName);
OutPipe=open(OutPipeName, O_WRONLY); }
}
}
if( (OutPipe<0) && (!DataServer.isListenning()) ) return -1;
}
if(DataServer.isListenning())
{ for(int Idx=0; Idx<DataServer.Clients(); Idx++) // loop over clients
{ int Len=SerializeSpectra(DataServer.Client[Idx]); // serialize same data to every client
if(Len<0)
{ printf("Inp_FFT.Exec() ... Dropped a client\n");
DataServer.Close(Idx); } // if anything goes wrong: close this client
}
int Ret=DataServer.RemoveClosed(); // remove closed clients from the list
Ret=DataServer.Accept(); // check for more clients who might be waiting to connect
if(Ret>0) printf("Inp_FFT.Exec() ... Accepted new client (%d clients now)\n", DataServer.Clients() );
}
if(OutPipe>=0)
{ int Len=SerializeSpectra(OutPipe);
if(Len<0) { printf("Inp_FFT.Exec() ... Error while writing to %s\n", OutPipeName); close(OutPipe); OutPipe=(-1); return -1; }
}
return 0; }
void Start(void)
{ StopReq=0; Thr.setExec(ThreadExec); Thr.Create(this); }
~Inp_FFT()
{ Thr.Cancel();
if(Window) free(Window); }
double getCPU(void) // get CPU time for this thread
{
#if !defined(__MACH__)
struct timespec now; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now); return now.tv_sec + 1e-9*now.tv_nsec;
#else
return 0;
#endif
}
static void *ThreadExec(void *Context)
{ Inp_FFT *This = (Inp_FFT *)Context; return This->Exec(); }
void *Exec(void)
{ // printf("Inp_FFT.Exec() ... Start\n");
while(!StopReq)
{ double ExecTime=getCPU();
#ifndef USE_RPI_GPU_FFT
if(Filter && Filter->Enable)
{ SampleBuffer< std::complex<Float> > *InpBuffer = Filter->OutQueue.Pop();
// printf("Inp_FFT.Exec() ... (%5.3fMHz, %5.3fsec, %dsamples)\n", 1e-6*InpBuffer->Freq, InpBuffer->Time, InpBuffer->Full/2);
SlidingFFT(OutBuffer, *InpBuffer, FFT, Window); // Process input samples, produce FFT spectra
Filter->OutQueue.Recycle(InpBuffer);
}
else
#endif
{ SampleBuffer<uint8_t> *InpBuffer = RF->OutQueue.Pop(); // here we wait for a new data batch
// printf("Inp_FFT.Exec() ... (%5.3fMHz, %5.3fsec, %dsamples)\n", 1e-6*InpBuffer->Freq, InpBuffer->Time, InpBuffer->Full/2);
SlidingFFT(OutBuffer, *InpBuffer, FFT, Window); // Process input samples, produce FFT spectra
RF->OutQueue.Recycle(InpBuffer);
}
WriteToPipe(); // here we send the FFT spectra in OutBuffer to the demodulator
ExecTime=getCPU()-ExecTime; // printf("Inp_FFT.Exec() ... %5.3fsec\n", ExecTime);
}
// printf("Inp_FFT.Exec() ... Stop\n");
if(OutPipe>=0) { close(OutPipe); OutPipe=(-1); }
return 0; }
} ;
// ==================================================================================================
template <class Float>
class GSM_FFT // FFT of the GSM RF data
{ public:
Thread Thr; // processing thread
volatile int StopReq;
RF_Acq *RF; // pointer to the RF acquisition
int FFTsize;
DFT1d<Float> FFT;
Float *Window;
SampleBuffer< std::complex<Float> > Spectra; // (complex) spectra
SampleBuffer< Float > Power; // spectra power (energy)
MessageQueue<Socket *> SpectrogramQueue; // sockets send to this queue should be written with a most recent spectrogram
SampleBuffer<uint8_t> Image;
JPEG JpegImage;
std::vector<Float> PPM_Values; // [ppm] measured frequency correction values (a vector of)
Float PPM_Aver; // [ppm] average frequency correction
Float PPM_RMS; // [ppm] RMS of the frequency correction
int PPM_Points; // number of measurements taken into the average
time_t PPM_Time; // time when correction measured
Float getPPM(void) const { Float Value=PPM_Aver; return Value; }
public:
GSM_FFT(RF_Acq *RF)
{ Window=0; this->RF=RF; Preset(); }
int Preset(void) { return Preset(RF->SampleRate); }
int Preset(int SampleRate)
{ FFTsize=(8*SampleRate)/15625;
FFT.PresetForward(FFTsize);
Window=(Float *)realloc(Window, FFTsize*sizeof(Float));
FFT.SetSineWindow(Window, FFTsize, (Float)(1.0/sqrt(FFTsize)) );
PPM_Values.clear(); PPM_Aver=0; PPM_RMS=0; PPM_Points=0; PPM_Time=0;
return 1; }
void Start(void)
{ StopReq=0; Thr.setExec(ThreadExec); Thr.Create(this); }
~GSM_FFT()
{ Thr.Cancel();
if(Window) free(Window); }
double getCPU(void) // get CPU time for this thread
{
#if !defined(__MACH__)
struct timespec now; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now); return now.tv_sec + 1e-9*now.tv_nsec;
#else
return 0.0;
#endif
}
static void *ThreadExec(void *Context)
{ GSM_FFT *This = (GSM_FFT *)Context; return This->Exec(); }
void *Exec(void)
{ // printf("GSM_FFT.Exec() ... Start\n");
while(!StopReq)
{ double ExecTime=getCPU();
SampleBuffer<uint8_t> *InpBuffer = RF->GSM_OutQueue.Pop(); // get data sample on a GSM frequency
// printf("GSM_FFT.Exec() ... (%5.3fMHz, %5.3fsec, %dsamples)\n", 1e-6*InpBuffer->Freq, InpBuffer->Time, InpBuffer->Full/2);
SlidingFFT(Spectra, *InpBuffer, FFT, Window); // perform sliding-FFT on the data
SpectraPower(Power, Spectra); // calculate power of the spectra
RF->GSM_OutQueue.Recycle(InpBuffer); // return the buffer to the queue for reuse
if(SpectrogramQueue.Size()) // of spectrogram was requested
{ LogImage(Image, Power, (Float)0.33, (Float)32.0, (Float)32.0); // create spectrogram image
JpegImage.Compress_MONO8(Image.Data, Image.Len, Image.Samples() ); }
while(SpectrogramQueue.Size()) // send the image to all requesters
{ Socket *Client; SpectrogramQueue.Pop(Client);
Client->Send("HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nContent-Type: image/jpeg\r\nRefresh: 10\r\n\r\n");
// printf("GSM_FFT.Exec() ... Request for (GSM)spectrogram\n");
Client->Send(JpegImage.Data, JpegImage.Size);
Client->SendShutdown(); Client->Close(); delete Client; }
Process(); // process the data to find frequency calibration markers
ExecTime=getCPU()-ExecTime; // printf("GSM_FFT.Exec() ... %5.3fsec\n", ExecTime);
}
// printf("GSM_FFT.Exec() ... Stop\n");
return 0; }
static const int ChanWidth = 200000; // [Hz] GSM channel width
static const int DataRate = 270833; // [Hz] GSM data rate
SampleBuffer<Float> Aver, Peak, PeakPos, Bkg;
void Process(void)
{ Float BinWidth=Power.Rate/2; // [Hz] FFT bin spectral width
int Bins = Power.Len; // [int] number of FFT bins
Float FirstBinFreq = Power.Freq-BinWidth*Bins/2; // [Hz] center frequency of the first FFT bin
Float LastBinFreq = Power.Freq+BinWidth*Bins/2; // [Hz] center frequency of the one-after-the-last FFT bin
int Chan = (int)ceil(FirstBinFreq/ChanWidth); // integer channel number corr. to the first FFT bin (GSM channels are on multiples of 200kHz)
for( ; ; Chan++) // loop over (possible) channels in this scan
{ Float CenterFreq=Chan*ChanWidth; if(CenterFreq>=LastBinFreq) break; // center frequency of the channel
Float LowFreq = CenterFreq-0.45*ChanWidth; // [Hz] lower frequency to measure the channel
Float UppFreq = CenterFreq+0.45*ChanWidth; // [Hz] upper frequency to measure the channel
int LowBin=(int)floor((LowFreq-FirstBinFreq)/BinWidth+0.5); // FFT bins corresponding to the channel frequency range
int UppBin=(int)floor((UppFreq-FirstBinFreq)/BinWidth+0.5);
if( (LowBin<0) || (LowBin>=Bins) ) continue; // skip this channel if range to measure
if( (UppBin<0) || (UppBin>=Bins) ) continue; // not contained completely in this scan
Float AverPower;
int Marks=ProcessChan(AverPower, LowBin, UppBin, (CenterFreq-FirstBinFreq)/BinWidth, BinWidth, CenterFreq);
if(Marks==1) PPM_Values.pop_back(); // if only one mark found, drop it - likely a false signal
printf("GSM_FFT::Process: Chan=%d, Freq=%8.3fMHz [%4d-%4d] %+6.1fdB %d marks\n", Chan, 1e-6*CenterFreq, LowBin, UppBin, 10*log10(AverPower), Marks);
// { char FileName[32]; sprintf(FileName, "GSM_%5.1fMHz.dat", 1e-6*CenterFreq);
// FILE *File=fopen(FileName, "wt");
// for(int Idx=0; Idx<Aver.Full; Idx++)
// { fprintf(File, "%5d %12.6f %12.6f %+10.6f %10.6f\n",
// Idx, Aver[Idx], Peak[Idx], PeakPos[Idx], Bkg[Idx]); }
// fclose(File); }
}
std::sort(PPM_Values.begin(), PPM_Values.end());
if(PPM_Values.size()>=16) // if at least 16 measured points
{ Float Aver, RMS; int Margin=PPM_Values.size()/4;
AverRMS(Aver, RMS, PPM_Values.data()+Margin, PPM_Values.size()-2*Margin);
// printf("PPM = %+7.3f (%5.3f) [%d]\n", Aver, RMS, PPM_Values.size()-2*Margin);
if(RMS<0.5)
{ PPM_Aver=Aver; PPM_RMS=RMS; PPM_Points=PPM_Values.size()-2*Margin; PPM_Time=(time_t)floor(Power.Time+0.5); PPM_Values.clear();
printf("GSM freq. calib. = %+7.3f +/- %5.3f ppm, %d points\n", PPM_Aver, PPM_RMS, PPM_Points);
Float Corr=RF->GSM_FreqCorr; Corr+=0.25*(PPM_Aver-Corr); RF->GSM_FreqCorr=Corr; }
PPM_Values.clear();
}
if(PPM_Values.size()>=8) // if at least 8 measured points
{ Float Aver, RMS;
AverRMS(Aver, RMS, PPM_Values.data()+1, PPM_Values.size()-2); // calc. the average excluding two extreme points
// printf("PPM = %+7.3f (%5.3f) [%d]\n", Aver, RMS, PPM_Values.size()-2);
if(RMS<0.5)
{ PPM_Aver=Aver; PPM_RMS=RMS; PPM_Points=PPM_Values.size()-2; PPM_Time=(time_t)floor(Power.Time+0.5); PPM_Values.clear();
printf("GSM freq. calib. = %+7.3f +/- %5.3f ppm, %d points\n", PPM_Aver, PPM_RMS, PPM_Points);
Float Corr=RF->GSM_FreqCorr; Corr+=0.25*(PPM_Aver-Corr); RF->GSM_FreqCorr=Corr; }
}
}
// Average, Peak (with Position) and Background = Average - values around the Peak
static void AverPeakBkg(Float &Aver, Float &Peak, Float &PeakPos, Float &Bkg, Float *Data, int Size)
{ Aver=0; Peak=0; PeakPos=0; int PeakIdx=0;
for(int Idx=0; Idx<Size; Idx++)
{ Float Dat=Data[Idx];
if(Dat>Peak) { Peak=Dat; PeakIdx=Idx; }
Aver+=Dat; }
if(PeakIdx==0) { Peak+=Data[ 1]; PeakPos=PeakIdx+Data[ 1]/Peak; Bkg=(Aver-Peak)/(Size-2); }
else if(PeakPos==(Size-1)) { Peak+=Data[Size-2]; PeakPos=PeakIdx-Data[Size-2]/Peak; Bkg=(Aver-Peak)/(Size-2); }
else { Peak+=Data[PeakIdx+1]+Data[PeakIdx-1]; PeakPos=PeakIdx+(Data[PeakIdx+1]-Data[PeakIdx-1])/Peak; Bkg=(Aver-Peak)/(Size-3); }
Aver/=Size; }
// average and RMS of a data series
static void AverRMS(Float &Aver, Float &RMS, Float *Data, int Size)
{ Aver=0; RMS=0;
for(int Idx=0; Idx<Size; Idx++)
{ Aver+=Data[Idx]; }
Aver/=Size;
for(int Idx=0; Idx<Size; Idx++)
{ Float Diff=Data[Idx]-Aver; RMS+=Diff*Diff; }
RMS=sqrt(RMS/Size); }
int ProcessChan(Float &AverPower, int LowBin, int UppBin, Float CenterBin, Float BinWidth, Float CenterFreq) // process a single GSM channel
{ int Slides = Power.Samples(); // [FFT slides] in the data
int Bins = Power.Len; // number of FFT bins
Aver.Allocate(1, Slides); Peak.Allocate(1, Slides); PeakPos.Allocate(1, Slides); Bkg.Allocate(1, Slides);
Float *Data = Power.Data;
for(int Idx=0; Idx<Slides; Idx++, Data+=Bins)
{ AverPeakBkg(Aver[Idx], Peak[Idx], PeakPos[Idx], Bkg[Idx], Data+LowBin, UppBin-LowBin+1);
PeakPos[Idx]+=LowBin-CenterBin; }
Aver.Full=Slides; Peak.Full=Slides; PeakPos.Full=Slides; Bkg.Full=Slides;
Float PowerRMS; AverRMS(AverPower, PowerRMS, Aver.Data, Slides);
// printf("AverPower=%3.1f, PowerRMS=%3.1f\n", AverPower, PowerRMS);
if(PowerRMS>(0.5*AverPower)) return 0; // skip pulsing channels
Float AverPeak, PeakRMS; AverRMS(AverPeak, PeakRMS, Peak.Data, Slides);
Float AverBkg, BkgRMS; AverRMS(AverBkg, BkgRMS, Bkg.Data, Slides);
// printf("AverPeak=%3.1f, PeakRMS=%3.1f, AverBkg=%5.3f, BkgRMS=%5.3f\n", AverPeak, PeakRMS, AverBkg, BkgRMS);
int Marks=0;
Float PeakThres = 4*PeakRMS;
Float BkgThres = 4*BkgRMS;
for(int Idx=1; Idx<(Slides-1); Idx++)
{ Float PeakL=Peak.Data[Idx-1]-AverPeak;
Float PeakM=Peak.Data[Idx ]-AverPeak;
Float PeakR=Peak.Data[Idx+1]-AverPeak;
Float PeakSum = PeakL+PeakM+PeakR;
if(PeakSum<=PeakThres) continue;
if(PeakM<PeakL) continue;
if(PeakM<=PeakR) continue;
if(PeakM<=((PeakL+PeakR)/2)) continue;
Float BkgSum = Bkg.Data[Idx-1]+Bkg.Data[Idx]+Bkg.Data[Idx+1];
if((3*AverBkg-BkgSum)<BkgThres) continue;
if(Peak.Data[Idx]<(40*Bkg.Data[Idx])) continue;
Float PPM = -1e6*(PeakPos.Data[Idx]*BinWidth-(Float)DataRate/4)/CenterFreq;
// printf("Mark: PeakSum[%5d]=%8.1f/%6.1f Bkg=%8.3f/%6.3f Peak/Bkg=%8.1f PeakPos=%+7.3f %+7.3fppm\n",
// Idx, PeakSum, PeakThres, 3*AverBkg-BkgSum, BkgThres, Peak.Data[Idx]/Bkg.Data[Idx], PeakPos.Data[Idx], PPM);
PPM_Values.push_back(PPM);
Marks++; }
return Marks; }
} ;
// ==================================================================================================
template <class Float>
class HTTP_Server
{ public:
int Port; // listenning port
Thread Thr; // processing thread
RF_Acq *RF; // pointer to RF acquisition
GSM_FFT<Float> *GSM;
char Host[32]; // Host name
char ConfigFileName[PATH_MAX];
public:
HTTP_Server(RF_Acq *RF, GSM_FFT<Float> *GSM)
{ this->RF=RF; this->GSM=GSM;
Host[0]=0; SocketAddress::getHostName(Host, 32);
Config_Defaults(); }
void Config_Defaults(void)
{ ConfigFileName[0]=0;
Port=8080; }
int Config(config_t *Config)
{ config_lookup_int(Config, "HTTP.Port", &Port); return 0; }
void Start(void)
{ if(Port<=0) return;
Thr.setExec(ThreadExec); Thr.Create(this); }
~HTTP_Server()
{ if(Port) Thr.Cancel(); }
static void *ThreadExec(void *Context)
{ HTTP_Server *This = (HTTP_Server *)Context; return This->Exec(); }
void *Exec(void)
{ printf("HTTP_Server.Exec() ... Start\n");
while(1)
{ Socket Listen;
// if(Listen.Create_STREAM()<0) { printf("HTTP_Server.Exec() ... Cannot Create_STREAM()\n"); sleep(1); continue; }
// if(Listen.setReuseAddress()<0) { printf("HTTP_Server.Exec() ... Cannot setReuseAddress()\n"); sleep(1); continue; }
if(Listen.Listen(Port)<0) { printf("HTTP_Server.Exec() ... Cannot listen() on port %d\n", Port); sleep(1); continue; }
printf("HTTP_Server.Exec() ... Listening on port %d\n", Port);
while(1)
{ Socket *Client = new Socket; SocketAddress ClientAddress;
if(Listen.Accept(*Client, ClientAddress)<0) { printf("HTTP_Server.Exec() ... Cannot accept()\n"); sleep(1); break; }
printf("HTTP_Server.Exec() ... Client from %s\n", ClientAddress.getIPColonPort());
Client->setReceiveTimeout(2.0); Client->setSendTimeout(20.0); Client->setLinger(1, 5);
SocketBuffer Request; time_t ConnectTime; time(&ConnectTime);
while(1)
{ if(Client->Receive(Request)<0) { printf("HTTP_Server.Exec() ... Cannot receive()\n"); Client->SendShutdown(); Client->Close(); delete Client; Client=0; break; }
if( Request.Len && strstr(Request.Data, "\r\n\r\n") ) break;
time_t Now; time(&Now);
if((Now-ConnectTime)>2) { printf("HTTP_Server.Exec() ... Request timeout\n"); Client->SendShutdown(); Client->Close(); delete Client; Client=0; break; }
}
if(Client)
{ // printf("HTTP_Server.Exec() ... Request[%d]:\n", Request.Len); Request.WriteToFile(stdout); fflush(stdout);
ProcessRequest(Client, Request); }
}
Listen.Close();
}
printf("HTTP_Server.Exec() ... Stop\n");
return 0; }
int CopyWord(char *Dst, char *Src, int MaxLen)
{ int Count=0; MaxLen-=1;
for( ; ; )
{ char ch = (*Src++); if(ch<=' ') break;
if(Count>=MaxLen) return -1;
(*Dst++) = ch; Count++; }
(*Dst++)=0;
return Count; }
void ProcessRequest(Socket *Client, SocketBuffer &Request)
{ if(memcmp(Request.Data, "GET ", 4)!=0) goto BadRequest;
char File[64]; if(CopyWord(File, Request.Data+4, 64)<0) goto BadRequest;
printf("HTTP_Server.Exec() ... Request for %s\n", File);
if(strcmp(File, "/")==0)
{ Status(Client); return; }
else if( (strcmp(File, "/status.html")==0) || (strcmp(File, "status.html")==0) )
{ Status(Client); return; }
else if( (strcmp(File, "/gsm-spectrogram.jpg")==0) || (strcmp(File, "gsm-spectrogram.jpg")==0) )
{ GSM->SpectrogramQueue.Push(Client); return; }
else if( (strcmp(File, "/spectrogram.jpg")==0) || (strcmp(File, "spectrogram.jpg")==0) )
{ RF->SpectrogramQueue.Push(Client); return; }
else if( (strcmp(File, "/time-slot-rf.u8")==0) || (strcmp(File, "time-slot-rf.u8")==0) )
{ RF->RawDataQueue.Push(Client); return; }
// NotFound:
Client->Send("HTTP/1.0 404 Not Found\r\n\r\n"); Client->SendShutdown(); Client->Close(); delete Client; return;
BadRequest:
Client->Send("HTTP/1.0 400 Bad Request\r\n\r\n"); Client->SendShutdown(); Client->Close(); delete Client; return;
}
void Status(Socket *Client)
{ Client->Send("\
HTTP/1.1 200 OK\r\n\
Cache-Control: no-cache\r\n\
Content-Type: text/html\r\n\
Refresh: 5\r\n\
\r\n\
<!DOCTYPE html>\r\n\
<html>\r\n\
");
// time_t Now; time(&Now);
dprintf(Client->SocketFile, "\
<title>%s RTLSDR-OGN RF processor " STR(VERSION) " status</title>\n\
<b>RTLSDR OGN RF processor " STR(VERSION) "/"__DATE__"</b><br /><br />\n\n", Host);
dprintf(Client->SocketFile, "<table>\n<tr><th>System</th><th></th></tr>\n");
dprintf(Client->SocketFile, "<tr><td>Host name</td><td align=right><b>%s</b></td></tr>\n", Host);
dprintf(Client->SocketFile, "<tr><td>Configuration file path+name</td><td align=right><b>%s</b></td></tr>\n", ConfigFileName);
time_t Now; time(&Now);
struct tm TM; localtime_r(&Now, &TM);
dprintf(Client->SocketFile, "<tr><td>Local time</td><td align=right><b>%02d:%02d:%02d</b></td></tr>\n", TM.tm_hour, TM.tm_min, TM.tm_sec);
dprintf(Client->SocketFile, "<tr><td>Software</td><td align=right><b>" STR(VERSION) "</b></td></tr>\n");
#ifndef __MACH__
struct sysinfo SysInfo;
if(sysinfo(&SysInfo)>=0)
{ dprintf(Client->SocketFile, "<tr><td>CPU load</td><td align=right><b>%3.1f/%3.1f/%3.1f</b></td></tr>\n",
SysInfo.loads[0]/65536.0, SysInfo.loads[1]/65536.0, SysInfo.loads[2]/65536.0);
dprintf(Client->SocketFile, "<tr><td>RAM [free/total]</td><td align=right><b>%3.1f/%3.1f MB</b></td></tr>\n",
1e-6*SysInfo.freeram*SysInfo.mem_unit, 1e-6*SysInfo.totalram*SysInfo.mem_unit);
}
#endif
float CPU_Temperature;
if(getCpuTemperature(CPU_Temperature)>=0)
dprintf(Client->SocketFile, "<tr><td>CPU temperature</td><td align=right><b>%+5.1f ℃</b></td></tr>\n", CPU_Temperature);
float SupplyVoltage=0;
if(getSupplyVoltage(SupplyVoltage)>=0)
dprintf(Client->SocketFile, "<tr><td>Supply voltage</td><td align=right><b>%5.3f V</b></td></tr>\n", SupplyVoltage);
float SupplyCurrent=0;
if(getSupplyCurrent(SupplyCurrent)>=0)
dprintf(Client->SocketFile, "<tr><td>Supply current</td><td align=right><b>%5.3f A</b></td></tr>\n", SupplyCurrent);
double NtpTime, EstError, RefFreqCorr;
if(getNTP(NtpTime, EstError, RefFreqCorr)>=0)
{ time_t Time = floor(NtpTime);
struct tm TM; gmtime_r(&Time, &TM);
dprintf(Client->SocketFile, "<tr><td>NTP UTC time</td><td align=right><b>%02d:%02d:%02d</b></td></tr>\n", TM.tm_hour, TM.tm_min, TM.tm_sec);
dprintf(Client->SocketFile, "<tr><td>NTP est. error</td><td align=right><b>%3.1f ms</b></td></tr>\n", 1e3*EstError);
dprintf(Client->SocketFile, "<tr><td>NTP freq. corr.</td><td align=right><b>%+5.2f ppm</b></td></tr>\n", RefFreqCorr);
}
if(RF->SDR.isOpen())
{ dprintf(Client->SocketFile, "<tr><th>RTL-SDR device #%d</th><th></th></tr>\n", RF->SDR.DeviceIndex);
dprintf(Client->SocketFile, "<tr><td>Name</td><td align=right><b>%s</b></td></tr>\n", RF->SDR.getDeviceName());
dprintf(Client->SocketFile, "<tr><td>Tuner type</td><td align=right><b>%s</b></td></tr>\n", RF->SDR.getTunerTypeName());
char Manuf[256], Product[256], Serial[256];
RF->SDR.getUsbStrings(Manuf, Product, Serial);
dprintf(Client->SocketFile, "<tr><td>Manufacturer</td><td align=right><b>%s</b></td></tr>\n", Manuf);
dprintf(Client->SocketFile, "<tr><td>Product</td><td align=right><b>%s</b></td></tr>\n", Product);
dprintf(Client->SocketFile, "<tr><td>Serial</td><td align=right><b>%s</b></td></tr>\n", Serial);
#ifdef NEW_RTLSDR_LIB
for(int Stage=0; Stage<8; Stage++)
{ char Descr[256]; int Gains=RF->SDR.getTunerStageGains(Stage, 0, Descr); if(Gains<=0) break;
dprintf(Client->SocketFile, "<tr><td>Tuner stage #%d</td><td align=right><b>%s [%2d]</b></td></tr>\n", Stage, Descr, Gains);
}
dprintf(Client->SocketFile, "<tr><td>Tuner bandwidths</td><td align=right><b>[%d]</b></td></tr>\n", RF->SDR.getTunerBandwidths());
dprintf(Client->SocketFile, "<tr><td>Tuner gains</td><td align=right><b>[%d]</b></td></tr>\n", RF->SDR.getTunerGains());
#endif
dprintf(Client->SocketFile, "<tr><td>Center frequency</td><td align=right><b>%7.3f MHz</b></td></tr>\n", 1e-6*RF->SDR.getCenterFreq());
dprintf(Client->SocketFile, "<tr><td>Sample rate</td><td align=right><b>%5.3f MHz</b></td></tr>\n", 1e-6*RF->SDR.getSampleRate());
dprintf(Client->SocketFile, "<tr><td>Frequency correction</td><td align=right><b>%+5.1f ppm</b></td></tr>\n", RF->FreqCorr + RF->GSM_FreqCorr);
dprintf(Client->SocketFile, "<tr><td>Live Time</td><td align=right><b>%5.1f%%</b></td></tr>\n", 100*RF->getLifeTime());
uint32_t RtlFreq, TunerFreq; RF->SDR.getXtalFreq(RtlFreq, TunerFreq);
dprintf(Client->SocketFile, "<tr><td>RTL Xtal</td><td align=right><b>%8.6f MHz</b></td></tr>\n", 1e-6*RtlFreq);
dprintf(Client->SocketFile, "<tr><td>Tuner Xtal</td><td align=right><b>%8.6f MHz</b></td></tr>\n", 1e-6*TunerFreq);
/*
dprintf(Client->SocketFile, "<tr><td>Gain[%d]</td><td align=right><b>", RF->SDR.Gains);
for(int Idx=0; Idx<RF->SDR.Gains; Idx++)
{ dprintf(Client->SocketFile, "%c%3.1f", Idx?',':' ', 0.1*RF->SDR.Gain[Idx]);
}
dprintf(Client->SocketFile, " [dB]</b></td></tr>\n");
*/
}
dprintf(Client->SocketFile, "<tr><th>RF</th><th></th></tr>\n");
dprintf(Client->SocketFile, "<tr><td>RF.FreqPlan</td><td align=right><b>%d: %s</b></td></tr>\n", RF->HoppingPlan.Plan, RF->HoppingPlan.getPlanName() );
dprintf(Client->SocketFile, "<tr><td>RF.Device</td><td align=right><b>%d</b></td></tr>\n", RF->DeviceIndex);
if(RF->DeviceSerial[0])
dprintf(Client->SocketFile, "<tr><td>RF.DeviceSerial</td><td align=right><b>%s</b></td></tr>\n", RF->DeviceSerial);
dprintf(Client->SocketFile, "<tr><td>RF.SampleRate</td><td align=right><b>%3.1f MHz</b></td></tr>\n", 1e-6*RF->SampleRate);
// dprintf(Client->SocketFile, "<tr><td>RF.PipeName</td><td align=right><b>%s</b></td></tr>\n", ??->OutPipeName );
dprintf(Client->SocketFile, "<tr><td>RF.FreqCorr</td><td align=right><b>%+3d ppm</b></td></tr>\n", RF->FreqCorr);
if(RF->FreqRaster)
dprintf(Client->SocketFile, "<tr><td>RF.FreqRaster</td><td align=right><b>%3d Hz</b></td></tr>\n", RF->FreqRaster);
if(RF->BiasTee>=0)