improvements and fixes.

DFS event emulation.
This commit is contained in:
Jean-François DEL NERO 2022-12-04 01:02:16 +01:00
parent e2cf1239b1
commit 94926167c2

View File

@ -50,7 +50,6 @@
} while (0) } while (0)
#define IQ_SAMPLE_RATE (10000000UL) #define IQ_SAMPLE_RATE (10000000UL)
#define SUBCARRIERS_SAMPLE_RATE (10000000UL)
#define IF_FREQ 0 #define IF_FREQ 0
@ -121,51 +120,64 @@ void printhelp(char* argv[])
{ {
printf("Options:\n"); printf("Options:\n");
printf(" -stdout\t\t\t: IQ stream send to stdout\n"); printf(" -stdout\t\t\t: IQ stream send to stdout\n");
printf(" -freq_bw:[Hz] \t\t: bandwith (Hz)\n"); printf(" -iq_rate\t\t\t: IQ rate (default : 10000000)\n");
printf(" -freq_bw:[Hz]\t\t\t: bandwith (Hz)\n");
printf(" -ping_pong_freq:[centi Hz]\t: ping pong freq (per step of 0.01 Hz)\n"); printf(" -ping_pong_freq:[centi Hz]\t: ping pong freq (per step of 0.01 Hz)\n");
printf(" -jam_mode:[Mode id]\t\t: Mode. 0=ping pong, 1=random, 3=full iq random\n"); printf(" -jam_mode:[Mode id]\t\t: Mode. 0=ping pong, 1=random, 2=fixed frequency, 3=full iq random\n");
printf(" -jam_interval:[ms]\t\t: Interval (ms)\n"); printf(" -jam_interval:[uS]\t\t: Interval (uS)\n");
printf(" -jam_duration:[ms]\t\t: pulses duration (ms)\n"); printf(" -jam_duration:[uS]\t\t: pulses duration (uS)\n");
printf(" -jam_long_interval:[uS]\t\t: pulses long interval (uS)\n");
printf(" -jam_group_pulses:[cnt]\t\t: pulses group count\n");
printf(" -rand_interval\t\t: random interval mode\n"); printf(" -rand_interval\t\t: random interval mode\n");
printf(" -rand_duration\t\t: random duration mode\n"); printf(" -rand_duration\t\t: random duration mode\n");
printf(" -generate\t\t\t: Generate the IQ stream\n"); printf(" -generate\t\t\t: Generate the IQ stream\n");
printf(" -help\t\t\t\t: This help\n"); printf(" -help\t\t\t\t: This help\n");
printf("\n Examples :\n");
printf(" ./rf_jammer -generate -stdout -iq_rate:10000000 -freq_bw:50000 -jam_mode:1 -jam_interval:625 -jam_duration:10 | hackrf_transfer -f 5500000000 -t - -x 47 -a 1 -s 10000000\n");
printf(" ./rf_jammer -generate -stdout -iq_rate:2000000 -jam_mode:2 -jam_interval:613 -jam_duration:12 -jam_long_interval:250000 -jam_group_pulses:15 | hackrf_transfer -f 5500000000 -t - -x 47 -a 1 -s 2000000\n");
printf("\n"); printf("\n");
} }
#define BUFFER_SAMPLES_SIZE (2048*8) #define BUFFER_SAMPLES_SIZE (2048*8)
int us2ticks(int samplerate, int us)
{
return (us * (samplerate / 1000000));
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
char temp_str[512]; char temp_str[512];
wave_io * wave1,*wave2; wave_io * wave1,*wave2;
uint16_t iq_wavebuf[ BUFFER_SAMPLES_SIZE * (IQ_SAMPLE_RATE/SUBCARRIERS_SAMPLE_RATE)]; uint16_t iq_wavebuf[ BUFFER_SAMPLES_SIZE ];
int16_t wavebuf_dbg[BUFFER_SAMPLES_SIZE]; int16_t wavebuf_dbg[BUFFER_SAMPLES_SIZE];
double wavebuf_dbg2[BUFFER_SAMPLES_SIZE]; double wavebuf_dbg2[BUFFER_SAMPLES_SIZE];
unsigned int i,j,k; unsigned int i,j;
int freq_bw; int freq_bw;
int pulses_cnt;
int ping_pong_freq; int ping_pong_freq;
int jam_mode; int jam_mode;
int pulses_interval, pulses_interval_cnt; int pulses_interval, pulses_interval_cnt;
int pulses_long_interval,pulses_short_interval;
int pulses_duration, pulses_duration_cnt; int pulses_duration, pulses_duration_cnt;
int pulses_group_nb;
int rand_interval,rand_duration; int rand_interval,rand_duration;
double audio_sample_final; double ping_pong_sample;
double fm_mod; double fm_mod;
double old_freq,interpolation_step;
uint32_t randdword; uint32_t randdword;
rand_gen_state randgen; rand_gen_state randgen;
iq_wave_gen iqgen; iq_wave_gen iqgen;
wave_gen audio_l_gen; wave_gen ping_pong_gen;
verbose=0; verbose=0;
stdoutmode = 0; stdoutmode = 0;
@ -178,6 +190,7 @@ int main(int argc, char* argv[])
pulses_interval_cnt = 0; pulses_interval_cnt = 0;
rand_interval = 0; rand_interval = 0;
rand_duration = 0; rand_duration = 0;
pulses_cnt = 0;
if(isOption(argc,argv,"stdout",NULL)>0) if(isOption(argc,argv,"stdout",NULL)>0)
{ {
@ -186,7 +199,7 @@ int main(int argc, char* argv[])
if(!stdoutmode) if(!stdoutmode)
{ {
printf("rf_jammer v0.0.0.1\n"); printf("rf_jammer v0.0.0.2\n");
printf("Copyright (C) 2022 Jean-Francois DEL NERO\n"); printf("Copyright (C) 2022 Jean-Francois DEL NERO\n");
printf("This program comes with ABSOLUTELY NO WARRANTY\n"); printf("This program comes with ABSOLUTELY NO WARRANTY\n");
printf("This is free software, and you are welcome to redistribute it\n"); printf("This is free software, and you are welcome to redistribute it\n");
@ -207,6 +220,8 @@ int main(int argc, char* argv[])
} }
pulses_interval = 0; pulses_interval = 0;
pulses_short_interval = 0;
pulses_long_interval = 0;
pulses_duration = 2000000; pulses_duration = 2000000;
if(isOption(argc,argv,"freq_bw",(char*)&temp_str)>0) if(isOption(argc,argv,"freq_bw",(char*)&temp_str)>0)
@ -224,14 +239,32 @@ int main(int argc, char* argv[])
jam_mode = atoi(temp_str) ; jam_mode = atoi(temp_str) ;
} }
iqgen.sample_rate = IQ_SAMPLE_RATE;
if(isOption(argc,argv,"iq_rate",(char*)&temp_str)>0)
{
iqgen.sample_rate = atoi(temp_str);
}
if(isOption(argc,argv,"jam_interval",(char*)&temp_str)>0) if(isOption(argc,argv,"jam_interval",(char*)&temp_str)>0)
{ {
pulses_interval = atoi(temp_str) * (IQ_SAMPLE_RATE / 1000); pulses_interval = us2ticks(iqgen.sample_rate, atoi(temp_str));
pulses_short_interval = pulses_interval;
} }
if(isOption(argc,argv,"jam_duration",(char*)&temp_str)>0) if(isOption(argc,argv,"jam_duration",(char*)&temp_str)>0)
{ {
pulses_duration = atoi(temp_str) * (IQ_SAMPLE_RATE / 1000); pulses_duration = us2ticks(iqgen.sample_rate, atoi(temp_str));
}
if(isOption(argc,argv,"jam_long_interval",(char*)&temp_str)>0)
{
pulses_long_interval = us2ticks(iqgen.sample_rate, atoi(temp_str));
}
pulses_group_nb = 0;
if(isOption(argc,argv,"jam_group_pulses",(char*)&temp_str)>0)
{
pulses_group_nb = atoi(temp_str);
} }
if(isOption(argc,argv,"rand_interval",NULL)>0) if(isOption(argc,argv,"rand_interval",NULL)>0)
@ -246,21 +279,22 @@ int main(int argc, char* argv[])
if(isOption(argc,argv,"generate",0)>0) if(isOption(argc,argv,"generate",0)>0)
{ {
memset(iq_wavebuf, 0, BUFFER_SAMPLES_SIZE * sizeof(uint16_t) );
rand_gen_init(&randgen, 0xC279DCEF ); rand_gen_init(&randgen, 0xC279DCEF );
// Init oscillators // Init oscillators
// Left and Right audio freq (used if no .mod music file) // ping pong freq
audio_l_gen.phase = 0; ping_pong_gen.phase = 0;
audio_l_gen.Frequency = ((double)ping_pong_freq) / 100.0; ping_pong_gen.Frequency = ((double)ping_pong_freq) / 100.0;
audio_l_gen.Amplitude = 100; ping_pong_gen.Amplitude = 100;
audio_l_gen.sample_rate = SUBCARRIERS_SAMPLE_RATE; ping_pong_gen.sample_rate = iqgen.sample_rate;
// IQ Modulator // IQ Modulator
iqgen.phase = 0; iqgen.phase = 0;
iqgen.Frequency = IF_FREQ; iqgen.Frequency = IF_FREQ;
iqgen.Amplitude = 127; iqgen.Amplitude = 127;
iqgen.sample_rate = IQ_SAMPLE_RATE;
if(stdoutmode) if(stdoutmode)
{ {
@ -272,31 +306,29 @@ int main(int argc, char* argv[])
{ {
// file mode : create iq + wav files // file mode : create iq + wav files
wave1 = create_wave("test.iq",iqgen.sample_rate,WAVE_FILE_FORMAT_RAW_8BITS_IQ); wave1 = create_wave("test.iq",iqgen.sample_rate,WAVE_FILE_FORMAT_RAW_8BITS_IQ);
wave2 = create_wave("test.wav",SUBCARRIERS_SAMPLE_RATE,WAVE_FILE_FORMAT_WAV_16BITS_MONO); wave2 = create_wave("test.wav",iqgen.sample_rate,WAVE_FILE_FORMAT_WAV_16BITS_MONO);
} }
if(wave1) if(wave1)
{ {
old_freq = IF_FREQ;
// Main loop... // Main loop...
for(i=0;(i<8) || stdoutmode ;i++) for(i=0;(i<8) || stdoutmode ;i++)
{
if(jam_mode != 3)
{
for(j=0;j<BUFFER_SAMPLES_SIZE;j++)
{ {
switch( jam_mode ) switch( jam_mode )
{ {
case 0: // freq ping pong case 0: // freq ping pong
for(j=0;j<BUFFER_SAMPLES_SIZE;j++) ping_pong_sample = f_get_next_sample(&ping_pong_gen);
{
audio_sample_final = f_get_next_sample(&audio_l_gen); fm_mod = ((ping_pong_sample / (double)(100.0)) * (double)(freq_bw));
fm_mod = ((audio_sample_final / (double)(100.0)) * (double)(freq_bw));
wavebuf_dbg[j] = fm_mod; wavebuf_dbg[j] = fm_mod;
wavebuf_dbg2[j] = fm_mod; wavebuf_dbg2[j] = fm_mod;
}
break; break;
case 1: // rand frequency case 1: // rand frequency
for(j=0;j<BUFFER_SAMPLES_SIZE;j++)
{
randdword = rand_gen_get_next_word(&randgen); randdword = rand_gen_get_next_word(&randgen);
fm_mod = (((double)(randdword & 0x7FFFFFFF) / (double)(0x7FFFFFFF)) * (double)(freq_bw)); fm_mod = (((double)(randdword & 0x7FFFFFFF) / (double)(0x7FFFFFFF)) * (double)(freq_bw));
@ -306,35 +338,27 @@ int main(int argc, char* argv[])
wavebuf_dbg[j] = fm_mod; wavebuf_dbg[j] = fm_mod;
wavebuf_dbg2[j] = fm_mod; wavebuf_dbg2[j] = fm_mod;
} break;
case 2: // pulse mode / fixed frequency
fm_mod = 0;
wavebuf_dbg[j] = fm_mod;
wavebuf_dbg2[j] = fm_mod;
break; break;
default: default:
break; break;
} }
if(jam_mode != 3) iqgen.Frequency = ((double)IF_FREQ + wavebuf_dbg2[j]);
{
// Sub carriers sample rate to carrier IQ rate modulation + resampling
for(j=0;j<BUFFER_SAMPLES_SIZE;j++)
{
// linear interpolation TODO ?: Cubic interpolation
interpolation_step = (wavebuf_dbg2[j] - old_freq) / (double)(IQ_SAMPLE_RATE/SUBCARRIERS_SAMPLE_RATE);
for(k=0;k<(IQ_SAMPLE_RATE/SUBCARRIERS_SAMPLE_RATE);k++)
{
old_freq += interpolation_step;
iqgen.Frequency = ((double)IF_FREQ + old_freq);
if( pulses_interval_cnt < pulses_interval ) if( pulses_interval_cnt < pulses_interval )
{ {
pulses_interval_cnt++; pulses_interval_cnt++;
iq_wavebuf[(j*(IQ_SAMPLE_RATE/SUBCARRIERS_SAMPLE_RATE))+k] = 0x0000; iq_wavebuf[j] = 0x0000;
} }
else else
{ {
iq_wavebuf[(j*(IQ_SAMPLE_RATE/SUBCARRIERS_SAMPLE_RATE))+k] = get_next_iq(&iqgen);//iq_sample; iq_wavebuf[j] = get_next_iq(&iqgen);//iq_sample;
if(pulses_duration_cnt < pulses_duration) if(pulses_duration_cnt < pulses_duration)
{ {
@ -344,24 +368,37 @@ int main(int argc, char* argv[])
{ {
pulses_duration_cnt = 0; pulses_duration_cnt = 0;
pulses_interval_cnt = 0; pulses_interval_cnt = 0;
pulses_cnt++;
if(rand_interval) if(pulses_group_nb)
pulses_interval = (rand_gen_get_next_word(&randgen) & 0x7F) * (IQ_SAMPLE_RATE / 1000); {
if(pulses_cnt >= pulses_group_nb)
if(rand_duration) {
pulses_duration = (rand_gen_get_next_word(&randgen) & 0x7F) * (IQ_SAMPLE_RATE / 1000); pulses_interval = pulses_long_interval;
pulses_cnt = 0;
} }
} else
{
iq_wavebuf[(j*(IQ_SAMPLE_RATE/SUBCARRIERS_SAMPLE_RATE))+k] = get_next_iq(&iqgen);//iq_sample; pulses_interval = pulses_short_interval;
}
old_freq = wavebuf_dbg2[j];
} }
} }
else else
{ {
for(j=0;j<BUFFER_SAMPLES_SIZE * (IQ_SAMPLE_RATE/SUBCARRIERS_SAMPLE_RATE);j++) pulses_interval = pulses_short_interval;
}
if(rand_interval)
pulses_interval = (rand_gen_get_next_word(&randgen) & 0x7F) * (iqgen.sample_rate / 1000000);
if(rand_duration)
pulses_duration = (rand_gen_get_next_word(&randgen) & 0x7F) * (iqgen.sample_rate / 1000000);
}
}
}
}
else
{
for(j=0;j<BUFFER_SAMPLES_SIZE;j++)
{ {
randdword = rand_gen_get_next_word(&randgen); randdword = rand_gen_get_next_word(&randgen);
@ -382,19 +419,37 @@ int main(int argc, char* argv[])
{ {
pulses_duration_cnt = 0; pulses_duration_cnt = 0;
pulses_interval_cnt = 0; pulses_interval_cnt = 0;
pulses_cnt++;
if(pulses_group_nb)
{
if(pulses_cnt >= pulses_group_nb)
{
pulses_interval = pulses_long_interval;
pulses_cnt = 0;
}
else
{
pulses_interval = pulses_short_interval;
}
}
else
{
pulses_interval = pulses_short_interval;
}
if(rand_interval) if(rand_interval)
pulses_interval = (rand_gen_get_next_word(&randgen) & 0x7F) * (IQ_SAMPLE_RATE / 1000); pulses_interval = (rand_gen_get_next_word(&randgen) & 0x7F) * (iqgen.sample_rate / 1000000);
if(rand_duration) if(rand_duration)
pulses_duration = (rand_gen_get_next_word(&randgen) & 0x7F) * (IQ_SAMPLE_RATE / 1000); pulses_duration = (rand_gen_get_next_word(&randgen) & 0x7F) * (iqgen.sample_rate / 1000000);
} }
} }
} }
} }
write_wave(wave1, &iq_wavebuf,BUFFER_SAMPLES_SIZE*(IQ_SAMPLE_RATE/SUBCARRIERS_SAMPLE_RATE)); write_wave(wave1, &iq_wavebuf,BUFFER_SAMPLES_SIZE);
write_wave(wave2, &wavebuf_dbg,BUFFER_SAMPLES_SIZE); write_wave(wave2, &wavebuf_dbg,BUFFER_SAMPLES_SIZE);
} }
close_wave(wave1); close_wave(wave1);