-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path0003-v0.0.18-adding-spoofing-detection.patch
560 lines (555 loc) · 24.1 KB
/
0003-v0.0.18-adding-spoofing-detection.patch
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
From 76a56d549a5f9729715f09e9dd6a5b865f242b37 Mon Sep 17 00:00:00 2001
From: Gwenhael Goavec-Merou <[email protected]>
Date: Fri, 28 Jul 2023 16:09:56 +0200
Subject: [PATCH 3/3] adding spoofing detection
Signed-off-by: Gwenhael Goavec-Merou <[email protected]>
---
.../adapters/uhd_signal_source.cc | 22 +-
.../adapters/uhd_signal_source.h | 4 +
.../signal_source/libs/CMakeLists.txt | 2 +
.../signal_source/libs/spoofing_detection.cc | 317 ++++++++++++++++++
.../signal_source/libs/spoofing_detection.h | 103 ++++++
5 files changed, 447 insertions(+), 1 deletion(-)
create mode 100644 src/algorithms/signal_source/libs/spoofing_detection.cc
create mode 100644 src/algorithms/signal_source/libs/spoofing_detection.h
diff --git a/src/algorithms/signal_source/adapters/uhd_signal_source.cc b/src/algorithms/signal_source/adapters/uhd_signal_source.cc
index 8deea3a47..72cc743c1 100644
--- a/src/algorithms/signal_source/adapters/uhd_signal_source.cc
+++ b/src/algorithms/signal_source/adapters/uhd_signal_source.cc
@@ -21,6 +21,7 @@
#include "gnss_sdr_filesystem.h"
#include "gnss_sdr_string_literals.h"
#include "gnss_sdr_valve.h"
+#include "spoofing_detection.h"
#include "jamming_detection.h"
#include "sgd.h"
#include <glog/logging.h>
@@ -63,8 +64,18 @@ UhdSignalSource::UhdSignalSource(const ConfigurationInterface* configuration,
clock_source_ = configuration->property(role + ".clock_source", std::string("internal"));
otw_format_ = configuration->property(role + ".otw_format", std::string("sc16"));
RF_channels_ = configuration->property(role + ".RF_channels", 1);
+ spoofing_protection_ = configuration->property(role + ".spoofing_protection", 0);
sgd_ = configuration->property(role + ".sgd", 0);
jamming_ = configuration->property(role + ".jamming_protection", 0);
+ if (spoofing_protection_ !=0)
+ {
+ printf("Spoofing protection %d\n",spoofing_protection_);
+ RF_channels_=spoofing_protection_; // spoofing protection: override RF_channels in this block only => N inputs and RF_chan=1 output
+ subdevice_ = configuration->property(role + ".subdevice", std::string("A:A A:B"));
+ spoofing_averages=configuration->property(role + ".spoofing_averages", Navg);
+ spoofing_threshold=configuration->property(role + ".spoofing_threshold", STD_THRESHOLD);
+ spoofing_detect_=gnss_sdr_make_spoof(spoofing_threshold, spoofing_averages); // threshold on phase standard deviation (rad), number of averages
+ }
if (sgd_ != 0)
{
printf("SGD jamming protection %d\n",sgd_); fflush(stdout);
@@ -85,7 +96,7 @@ UhdSignalSource::UhdSignalSource(const ConfigurationInterface* configuration,
jamming_threshold=configuration->property(role + ".jamming_threshold", NORM_THRESHOLD);
jamming_xcorr_=gnss_sdr_make_jamm(jamming_threshold, jamming_averages);
}
- if ((sgd_ == 0) && (jamming_==0))
+ if ((spoofing_protection_==0) && (sgd_ == 0) && (jamming_==0))
{
subdevice_ = configuration->property(role + ".subdevice", empty);
}
@@ -342,6 +353,11 @@ void UhdSignalSource::connect(gr::top_block_sptr top_block)
DLOG(INFO) << "connected usrp source to file sink RF Channel " << i;
}
}
+ if (spoofing_protection_ != 0)
+ {
+ top_block->connect(uhd_source_, i, spoofing_detect_, i);
+ printf("UHD -> Spoofing connect: %d\n",i);fflush(stdout);
+ }
if (sgd_ != 0)
{
top_block->connect(uhd_source_, i, jamming_sgd_, i);
@@ -401,6 +417,10 @@ gr::basic_block_sptr UhdSignalSource::get_right_block(int RF_channel)
{
return valve_.at(RF_channel);
}
+ if ( spoofing_protection_ != 0ULL)
+ {
+ return spoofing_detect_;
+ }
if ( jamming_sgd_ != 0ULL)
{
return jamming_sgd_;
diff --git a/src/algorithms/signal_source/adapters/uhd_signal_source.h b/src/algorithms/signal_source/adapters/uhd_signal_source.h
index eb1d685c7..b97cd45ab 100644
--- a/src/algorithms/signal_source/adapters/uhd_signal_source.h
+++ b/src/algorithms/signal_source/adapters/uhd_signal_source.h
@@ -60,6 +60,7 @@ public:
private:
gr::uhd::usrp_source::sptr uhd_source_;
+ gnss_shared_ptr<gr::block> spoofing_detect_;
gnss_shared_ptr<gr::block> jamming_sgd_;
gnss_shared_ptr<gr::block> jamming_xcorr_;
@@ -89,10 +90,13 @@ private:
double sample_rate_;
size_t item_size_;
int RF_channels_;
+ int spoofing_protection_;
int sgd_;
int jamming_;
int jamming_averages;
+ int spoofing_averages;
double jamming_threshold;
+ double spoofing_threshold;
unsigned int in_stream_;
unsigned int out_stream_;
};
diff --git a/src/algorithms/signal_source/libs/CMakeLists.txt b/src/algorithms/signal_source/libs/CMakeLists.txt
index b19c63ba8..681f827eb 100644
--- a/src/algorithms/signal_source/libs/CMakeLists.txt
+++ b/src/algorithms/signal_source/libs/CMakeLists.txt
@@ -33,6 +33,7 @@ set(SIGNAL_SOURCE_LIB_SOURCES
rtl_tcp_dongle_info.cc
gnss_sdr_valve.cc
sgd_impl.cc
+ spoofing_detection.cc
jamming_detection.cc
gnss_sdr_timestamp.cc
${OPT_SIGNAL_SOURCE_LIB_SOURCES}
@@ -42,6 +43,7 @@ set(SIGNAL_SOURCE_LIB_HEADERS
rtl_tcp_commands.h
rtl_tcp_dongle_info.h
gnss_sdr_valve.h
+ spoofing_detection.h
jamming_detection
sgd.h
${OPT_SIGNAL_SOURCE_LIB_HEADERS}
diff --git a/src/algorithms/signal_source/libs/spoofing_detection.cc b/src/algorithms/signal_source/libs/spoofing_detection.cc
new file mode 100644
index 000000000..d3bc50e2d
--- /dev/null
+++ b/src/algorithms/signal_source/libs/spoofing_detection.cc
@@ -0,0 +1,317 @@
+/*!
+ *
+ * -------------------------------------------------------------------------
+ *
+ * Copyright (C) 2010-2019 (see AUTHORS file for a list of contributors)
+ *
+ * GNSS-SDR is a software defined Global Navigation
+ * Satellite Systems receiver
+ *
+ * This file is part of GNSS-SDR.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ * -------------------------------------------------------------------------
+ */
+
+#include "spoofing_detection.h"
+#include <glog/logging.h> // for LOG
+#include <gnuradio/io_signature.h> // for io_signature
+#include <algorithm> // for min
+#include <cstring> // for memcpy
+#include <unistd.h> // for usleep
+#include <utility>
+#include <volk/volk.h>
+//#define CHUNK_SIZE (2048*8*2) // ~1023 MS/s/16384=30~Hz/bin moved to .h
+#define KEEPSIZE 600 // 30 Hz/bin * 600 = ~+/-20 kHz
+#define MAXSAT 20 // too many satellites will start detecting genuine constellation ?
+#define MAXKEEP 7 // too many satellites will start detecting genuine constellation ?
+#define MEMORY_LEN 5 // remember spoofing even std rises suddendly
+#define moycpl // average complex (if active) or average mag/phase (if inactive)
+
+#pragma message("Spoofing detection compile")
+
+
+Gnss_Spoofing_Protect::Gnss_Spoofing_Protect(float threshold, int averages) : gr::sync_block("spoofing_detection",
+ gr::io_signature::make(1, 20, sizeof(gr_complex)),
+ gr::io_signature::make(1, 1, sizeof(gr_complex))),
+ d_threshold(threshold),
+ d_averages(averages)
+{
+ printf("Gnss_Spoofing_Protect\n");
+/*
+Ron Economos (April 5, 2020 10:58 AM)
+To: [email protected]
+I would use set_output_multiple() instead. See my previous e-mail for an
+example.
+https://lists.gnu.org/archive/html/discuss-gnuradio/2019-08/msg00188.html
+*/
+ set_output_multiple(CHUNK_SIZE); // only trigger processing if that amount of samples was accumulated
+ first_time_=0;
+ memset(spoofing_average_mul,0,sizeof(gr_complex)*KEEP_SIZE*2);
+ memset(spoofing_average_div,0,sizeof(gr_complex)*KEEP_SIZE*2);
+ avg_index_=0;
+ num_file_=0;
+ weight_=1.;
+ stdargres_=10.;
+ spoofing_memory_=0;
+}
+
+#if GNURADIO_USES_STD_POINTERS
+std::shared_ptr<Gnss_Spoofing_Protect> gnss_sdr_make_spoof(float threshold, int averages)
+{
+// unsigned int alignment = volk_get_alignment();
+ std::shared_ptr<Gnss_Spoofing_Protect> spoofing_detect_(new Gnss_Spoofing_Protect(threshold, averages));
+ return spoofing_detect_;
+}
+#else
+boost::shared_ptr<Gnss_Spoofing_Protect> gnss_sdr_make_spoof(float threshold, int averages)
+{
+// unsigned int alignment = volk_get_alignment();
+ boost::shared_ptr<Gnss_Spoofing_Protect> spoofing_detection(new Gnss_Spoofing_Protect(threshold, averages));
+ printf("Spoofing detection: variable created\n");
+/*
+ spoofing_average=(gr_complex*)volk_malloc(sizeof(gr_complex)*KEEP_SIZE*2,alignement); // 2* since sta followed by sto -> must fftshift to put 0 at center
+ bufout0_sta=(gr_complex*)volk_malloc(sizeof(gr_complex)*KEEP_SIZE,alignement);
+ bufout0_sto=(gr_complex*)volk_malloc(sizeof(gr_complex)*KEEP_SIZE,alignement);
+ bufout_sta=(gr_complex*)volk_malloc(sizeof(gr_complex)*KEEP_SIZE,alignement);
+ bufout_sto=(gr_complex*)volk_malloc(sizeof(gr_complex)*KEEP_SIZE,alignement);
+*/
+ return spoofing_detection;
+}
+#endif
+
+int Gnss_Spoofing_Protect::work(int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items)
+{ long unsigned int ch = 0;
+ int i,c,cnt;
+ uint16_t maxpos;
+ unsigned int alignment = volk_get_alignment();
+ float maxval,maxvallim;
+ gr_complex weightcpl={0.,0.};
+ gr_complex* bufin;
+ int opposite=0;
+#ifdef moycpl
+ gr_complex stddiv[MAXSAT];
+ gr_complex integral;
+#else
+ float meanarg,meanabs,stdarg[MAXSAT],stdabs[MAXSAT],weightabs=0.,weightarg=0.;
+#endif
+ gr_complex meandiv;
+/*
+ FILE *fo;
+ char filenam[256];
+*/
+ int count;
+ const gr_complex* in;
+ gr_complex* carre=(gr_complex*)volk_malloc(sizeof(gr_complex)*CHUNK_SIZE, alignment);
+// see https://github.com/gnss-sdr/gnss-sdr/blob/master/src/algorithms/acquisition/gnuradio_blocks/pcps_acquisition_fine_doppler_cc.h for declaration of gr::fft
+// gr::fft::fft_complex* plan = new gr::fft::fft_complex(CHUNK_SIZE, true);
+ bufin=plan->get_inbuf();
+// printf("Spoofing block: %d items, %ld out, %ld in\n",noutput_items,output_items.size(),input_items.size());
+// Spoofing block: 32768 items, 1 out, 2 in
+ if (input_items.size()!=2) first_time_=1; // don't save if other than 2 input channels
+/////////// CHECK FILES RUN AT THE SAME RATE
+// select same file with different filenames
+/*
+ volk_32fc_s32fc_multiply_32fc(carre, (const gr_complex*)input_items[0],-1, CHUNK_SIZE);
+ volk_32fc_x2_add_32fc (carre, (const gr_complex*)input_items[1],carre,CHUNK_SIZE);
+ maxpos=0;
+ for (i=0;i<CHUNK_SIZE;i++)
+ {if (carre[i].real()!=0) {printf("%d: real !=0\n",i);maxpos=i;}
+ if (carre[i].imag()!=0) {printf("%d: imag !=0\n",i);maxpos=i;}
+ }
+if (maxpos!=0) {printf("Spoofing: sync error\n");fflush(stdout);}
+*/
+/////////// END CHECK FILES RUN AT THE SAME RATE
+
+ for (ch = 0; ch < input_items.size(); ch++)
+ { // identity: output the same as 1st channel input
+ in= (const gr_complex*)input_items[ch]; // all channels
+ volk_32fc_x2_multiply_32fc(carre, in, in, CHUNK_SIZE);
+ memcpy(bufin, carre, CHUNK_SIZE * sizeof(gr_complex));
+ plan->execute();
+ if (ch==0)
+ {memcpy(bufout0_sta,plan->get_outbuf(),KEEP_SIZE * sizeof(gr_complex)); // save FFT(CH0)
+ // volk_32fc_s32fc_multiply_32fc(bufout0_sta,bufout0_sta,{1000.0,0.0},KEEP_SIZE);
+ memcpy(bufout0_sto,&plan->get_outbuf()[CHUNK_SIZE-KEEP_SIZE-1],KEEP_SIZE * sizeof(gr_complex)); // save FFT(CH0)
+ // volk_32fc_s32fc_multiply_32fc(bufout0_sto,bufout0_sto,{1000.0,0.0},KEEP_SIZE);
+ }
+ if (ch==1)
+ {// volk_32fc_s32fc_multiply_32fc(bufout_tmpsta,plan->get_outbuf(),{1000.0,0.0},KEEP_SIZE);
+ // volk_32fc_s32fc_multiply_32fc(bufout_tmpsto,&plan->get_outbuf()[CHUNK_SIZE-KEEP_SIZE-1],{1000.0,0.0},KEEP_SIZE);
+ volk_32fc_x2_multiply_conjugate_32fc(bufout_sta,plan->get_outbuf(),bufout0_sta,KEEP_SIZE);
+ volk_32fc_x2_multiply_conjugate_32fc(bufout_sto,&plan->get_outbuf()[CHUNK_SIZE-KEEP_SIZE-1],bufout0_sto,KEEP_SIZE);
+ volk_32fc_x2_add_32fc( spoofing_average_mul, spoofing_average_mul, bufout_sta,KEEP_SIZE);
+ volk_32fc_x2_add_32fc(&spoofing_average_mul[KEEP_SIZE],&spoofing_average_mul[KEEP_SIZE],bufout_sto,KEEP_SIZE);
+
+ volk_32fc_x2_divide_32fc(bufout_sta,bufout0_sta,plan->get_outbuf(),KEEP_SIZE); // CH0/CH1
+ volk_32fc_x2_divide_32fc(bufout_sto,bufout0_sto,&plan->get_outbuf()[CHUNK_SIZE-KEEP_SIZE-1],KEEP_SIZE);
+// for (i=0;i<KEEP_SIZE;i++) {bufout_sta[i]=sqrt(bufout_sta[i]); bufout_sto[i]=sqrt(bufout_sto[i]);}
+ volk_32fc_x2_add_32fc( spoofing_average_div, spoofing_average_div, bufout_sta,KEEP_SIZE);
+ volk_32fc_x2_add_32fc(&spoofing_average_div[KEEP_SIZE],&spoofing_average_div[KEEP_SIZE],bufout_sto,KEEP_SIZE);
+ avg_index_++;
+ }
+/*
+ if ((first_time_==0)&&(avg_index_==Navg))
+ {//if (ch==input_items.size()-1) first_time_=1; // save both channels in file
+ sprintf(filenam,"/tmp/output_f0_%d.txt",num_file_);
+ fo=fopen(filenam,"w");
+ for (count=0;count<KEEP_SIZE;count++)
+ fprintf(fo,"%ld %f %f\n",ch,bufout0_sta[count].real(),bufout0_sta[count].imag());
+ for (count=0;count<KEEP_SIZE;count++)
+ fprintf(fo,"%ld %f %f\n",ch,bufout0_sto[count].real(),bufout0_sto[count].imag());
+ fclose(fo);
+ sprintf(filenam,"/tmp/output_favg%ld_%d.txt",ch,num_file_);
+ fo=fopen(filenam,"w");
+ for (count=0;count<KEEP_SIZE*2;count++)
+ fprintf(fo,"%ld %f %f\n",ch,spoofing_average[count].real(),spoofing_average[count].imag());
+ fclose(fo);
+ num_file_++;
+ printf("\n***********\n");
+ fo=fopen("/tmp/output_t1.txt","a");
+ for (count=0;count<CHUNK_SIZE;count++) fprintf(fo,"%ld %f %f\n",ch,in[count].real(),in[count].imag());
+ fclose(fo);
+ fo=fopen("/tmp/output_t2.txt","a");
+ for (count=0;count<CHUNK_SIZE;count++) fprintf(fo,"%ld %f %f\n",ch,carre[count].real(),carre[count].imag());
+ fclose(fo);
+ }
+*/
+ }
+ if (avg_index_==d_averages) // restart averaging
+ {//volk_32fc_magnitude_squared_32f(spoofing_mag,spoofing_average,KEEP_SIZE*2);
+// https://www.libvolk.org/doxygen/volk_32fc_index_max_16u.html
+// Finds and returns the index which contains the maximum magnitude for complex points in the given vector
+ volk_32fc_index_max_16u(&maxpos, &spoofing_average_mul[KEEP_SIZE-10],20); // max value where there should be no satellite
+ maxvallim=10.*norm(spoofing_average_mul[KEEP_SIZE-10+maxpos]); // abs(max)^2 TODO adjust x10 ?
+ count=0;
+#ifdef moycpl
+ meandiv={0.,0.};
+ weightcpl={0.,0.};
+#else
+ meanarg=0.;
+ meanabs=0.;
+#endif
+ do
+ {volk_32fc_index_max_16u(&maxpos, spoofing_average_mul, KEEP_SIZE*2);
+// printf("%hd:\tangle=%.3f\t-\t",maxpos,arg(spoofing_average_mul[maxpos])); // atan2(spoofing_average_mul[maxpos].imag(),spoofing_average_mul[maxpos].real()));
+// printf("div: mag=%.3f angle=%.3f\n", abs(spoofing_average_div[maxpos]),arg(spoofing_average_div[maxpos]));
+ maxval=norm(spoofing_average_mul[maxpos]);
+ spoofing_average_mul[maxpos]=gr_complex{0.,0.}; // on met ce bin a 0 et on itere sur ses voisins
+ if (maxpos>0)
+ spoofing_average_mul[maxpos-1]=gr_complex{0.,0.}; // on met ce bin a 0 et on itere sur ses voisins
+ if (maxpos>1)
+ spoofing_average_mul[maxpos-2]=gr_complex{0.,0.}; // on met ce bin a 0 et on itere sur ses voisins
+ if (maxpos<2*KEEP_SIZE-1)
+ spoofing_average_mul[maxpos+1]=gr_complex{0.,0.}; // on met ce bin a 0 et on itere sur ses voisins
+ if (maxpos<2*KEEP_SIZE-2)
+ spoofing_average_mul[maxpos+2]=gr_complex{0.,0.}; // on met ce bin a 0 et on itere sur ses voisins
+#ifdef moycpl
+ meandiv+=sqrt(spoofing_average_div[maxpos]); // racine des coefficient mis au carr'e
+ stddiv[count]=sqrt(spoofing_average_div[maxpos]);
+#else
+ meanarg+=(arg(spoofing_average_div[maxpos])/2);
+ meanabs+=sqrt(abs(spoofing_average_div[maxpos]));
+ stdarg[count]=(arg(spoofing_average_div[maxpos])/2);
+ stdabs[count]=sqrt(abs(spoofing_average_div[maxpos]));
+#endif
+ count++;
+ }
+ while ((maxval>maxvallim)&&(count<MAXSAT));
+ if (count>0)
+ {
+#ifdef moycpl
+ meandiv/=(float)(count);
+#else
+ meanabs/=(float)(count);
+ meanarg/=(float)(count);
+#endif
+ stdargres_=0.;
+#ifdef moycpl
+ for (i=0;i<count;i++) {stdargres_+=(arg(stddiv[i])-arg(meandiv))*(arg(stddiv[i])-arg(meandiv));} // ecart type sur les phases
+// for (i=0;i<count;i++) {printf("%d: %f\n",i,abs(stddiv[i]));} // affiche le module de tous les poids
+#else
+ for (i=0;i<count;i++) {stdargres_+=(stdarg[i]-meanarg)*(stdarg[i]-meanarg);}
+#endif
+ stdargres_/=(float)(count);
+ printf("%d:\tstdargs=%.5f\t",count,stdargres_);fflush(stdout);
+ if (stdargres_<=d_threshold) // spoofing
+ {
+// initial phase estimate, from S.Daneshmand, A.Jafarnia-Jahromi, A.Broumandan, and G.Lachapelle,
+// A low-complexity GPS anti-spoofing method using a multi-antenna array,” vol. 2, pp. 2, 2012.
+ volk_32fc_x2_multiply_conjugate_32fc(carre,(const gr_complex*)input_items[0],(const gr_complex*)input_items[1],CHUNK_SIZE);
+ integral={0.,0.};
+ for (ch = 0; ch < CHUNK_SIZE; ch++) integral+=carre[ch];
+ // integrale/=CHUNK_SIZE;
+ printf("1st estimate: %.5f\t",arg(integral));
+
+#ifndef moycpl
+ weightabs=0.;
+ weightarg=0.;
+#endif
+ c=0;
+ cnt=0;
+ do {
+#ifdef moycpl
+ if ((arg(stddiv[c])-arg(meandiv))<0.1)
+ {weightcpl+=stddiv[c];
+ cnt++;
+ }
+#else
+ if (abs(stdarg[c]-meanarg)<0.1)
+ {printf("%d ",c);
+ weightarg+=stdarg[c];
+ weightabs+=stdabs[c];
+ cnt++;
+ } // only keep spoofing SV
+#endif
+ c++;
+ } while ((c<count)&&(cnt<MAXKEEP));
+#ifdef moycpl
+ weightcpl/=(float)cnt;
+#else
+ weightarg/=(float)cnt;
+ weightabs/=(float)cnt;
+#endif
+#ifdef moycpl // should be -sqrt, -sqrt to subtract, but with \pi phase rotation remove the '-'
+ weight_=weightcpl; // .=(A1/A2)^2 WHY +pi ?
+#else
+ weight_={(weightabs)*cosf(weightarg),(weightabs)*sinf(weightarg)}; // .=(A1/A2)^2 WHY +pi ?
+#endif
+ if (abs(arg(weight_)-arg(integral))<0.7)
+ {printf("*");
+ weight_=-weight_; // add pi to phase => subtract weight
+ opposite=1;
+ }
+ else opposite=0;
+ }
+ printf("weightabs=%.2f,weightarg=%.2f",abs(weight_),arg(weight_));
+ }
+ memset(spoofing_average_mul,0,sizeof(gr_complex)*KEEP_SIZE*2); // start + stop
+ memset(spoofing_average_div,0,sizeof(gr_complex)*KEEP_SIZE*2); // start + stop
+ avg_index_=0;
+ }
+ if ((stdargres_>d_threshold)&&(spoofing_memory_==0)) // no spoofing
+ {
+ memcpy(output_items[0], input_items[0], noutput_items * sizeof(gr_complex));
+ printf("\n");
+ }
+ else
+ {if (stdargres_<=d_threshold)
+ {printf(" /!\\\n");
+ spoofing_memory_=MEMORY_LEN; // reinit memory
+ }
+ else
+ {printf(" \\!/\n");
+ spoofing_memory_--; // stdargres_ was > STD_THRESHOLD so spoofing_memory_>0 and --
+ }
+ volk_32fc_s32fc_multiply_32fc(carre,(const gr_complex*)input_items[1],weight_,CHUNK_SIZE); // -alpha*ant0
+ volk_32fc_x2_add_32fc((gr_complex*)output_items[0], (const gr_complex*)input_items[0], carre, CHUNK_SIZE);
+ if (opposite==1)
+ volk_32fc_s32fc_multiply_32fc((gr_complex*)output_items[0],(gr_complex*)output_items[0],-1,CHUNK_SIZE); // opposite
+ }
+// delete plan;
+ volk_free(carre);
+ return noutput_items;
+}
diff --git a/src/algorithms/signal_source/libs/spoofing_detection.h b/src/algorithms/signal_source/libs/spoofing_detection.h
new file mode 100644
index 000000000..6232fbf5d
--- /dev/null
+++ b/src/algorithms/signal_source/libs/spoofing_detection.h
@@ -0,0 +1,103 @@
+/*!
+ * \file
+ * \brief Interface of a GNU Radio block that sends a STOP message to the
+ * control queue right after a specific number of samples have passed through it.
+ * \author Javier Arribas, 2018. jarribas(at)cttc.es
+ * \author Carlos Aviles, 2010. carlos.avilesr(at)googlemail.com
+ *
+ * -------------------------------------------------------------------------
+ *
+ * Copyright (C) 2010-2019 (see AUTHORS file for a list of contributors)
+ *
+ * GNSS-SDR is a software defined Global Navigation
+ * Satellite Systems receiver
+ *
+ * This file is part of GNSS-SDR.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ * -------------------------------------------------------------------------
+ */
+
+
+#ifndef GNSS_SDR_GNSS_SDR_SPOOF_H
+#define GNSS_SDR_GNSS_SDR_SPOOF_H
+
+#include "concurrent_queue.h"
+#if GNURADIO_USES_STD_POINTERS
+#include <memory>
+#else
+#include <boost/shared_ptr.hpp>
+#endif
+#include <gnuradio/sync_block.h> // for sync_block
+#include <gnuradio/types.h> // for gr_vector_const_void_star
+#include <pmt/pmt.h>
+#include <cstddef> // for size_t
+#include <cstdint>
+#include <memory>
+
+#include <gnuradio/fft/fft.h>
+#include <gnuradio/fft/fft_shift.h>
+
+// #define CHUNK_SIZE (2048*8*2) // ~1023 MS/s/32768=30~Hz/bin
+#define CHUNK_SIZE (8192*64) // cf Matlab
+#define KEEP_SIZE (25000) // +/-50 kHz
+#define STD_THRESHOLD 0.05 // rad
+#define Navg 1 // FFT averages
+
+class Gnss_Spoofing_Protect;
+#if GNURADIO_USES_STD_POINTERS
+std::shared_ptr<Gnss_Spoofing_Protect> gnss_sdr_make_spoof(
+ float threshold,
+ int averages);
+
+#else
+boost::shared_ptr<Gnss_Spoofing_Protect> gnss_sdr_make_spoof(
+ float threshold,
+ int averages);
+
+#endif
+
+/*!
+ * \brief Implementation of a GNU Radio block that sends a STOP message to the
+ * control queue right after a specific number of samples have passed through it.
+ */
+class Gnss_Spoofing_Protect : public gr::sync_block
+{
+public:
+ int work(int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items);
+
+private:
+#if GNURADIO_USES_STD_POINTERS
+ friend std::shared_ptr<Gnss_Spoofing_Protect> gnss_sdr_make_spoof(
+ float threshold, int averages);
+#else
+ friend boost::shared_ptr<Gnss_Spoofing_Protect> gnss_sdr_make_spoof(
+ float threshold, int averages);
+#endif
+
+ Gnss_Spoofing_Protect(float threshold, int averages);
+
+ gr::fft::fft_complex* plan = new gr::fft::fft_complex(CHUNK_SIZE, true);
+ gr_complex spoofing_average_mul[KEEP_SIZE*2]; // 2* since sta followed by sto -> must fftshift to put 0 at center
+ gr_complex spoofing_average_div[KEEP_SIZE*2]; // 2* since sta followed by sto -> must fftshift to put 0 at center
+ gr_complex bufout0_sta[KEEP_SIZE];
+ gr_complex bufout0_sto[KEEP_SIZE];
+ gr_complex bufout_sta[KEEP_SIZE];
+ gr_complex bufout_sto[KEEP_SIZE];
+// gr_complex bufout_tmpsta[KEEP_SIZE];
+// gr_complex bufout_tmpsto[KEEP_SIZE];
+ gr_complex processed_output[CHUNK_SIZE];
+ gr_complex weight_;
+ float stdargres_;
+ int avg_index_;
+ int num_file_;
+ int spoofing_memory_;
+ float d_threshold;
+ int d_averages;
+ int first_time_;
+};
+
+#endif // GNSS_SDR_GNSS_SDR_SPOOF_H
--
2.41.0