Module: PWN::SDR::GQRX

Defined in:
lib/pwn/sdr/gqrx.rb

Overview

This plugin interacts with the remote control interface of GQRX.

Class Method Summary collapse

Class Method Details

.analyze_log(opts = {}) ⇒ Object

Supported Method Parameters

PWN::SDR::GQRX.analyze_log(

scan_log: 'required - Path to signals log file',
target: 'optional - GQRX target IP address (defaults to 127.0.0.1)',
port: 'optional - GQRX target port (defaults to 7356)'

)



1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
# File 'lib/pwn/sdr/gqrx.rb', line 1117

public_class_method def self.analyze_log(opts = {})
  scan_log = opts[:scan_log]
  raise 'ERROR: scan_log path is required.' unless File.exist?(scan_log)

  scan_resp = JSON.parse(File.read(scan_log), symbolize_names: true)
  raise 'ERROR: No signals found in log.' if scan_resp[:signals].nil? || scan_resp[:signals].empty?

  target = opts[:target]
  port = opts[:port]

  analyze_scan(
    scan_resp: scan_resp,
    target: target,
    port: port
  )
rescue StandardError => e
  raise e
end

.analyze_scan(opts = {}) ⇒ Object

Supported Method Parameters

PWN::SDR::GQRX.analyze_scan(

scan_resp: 'required - Scan response hash returned from #scan_range method',
target: 'optional - GQRX target IP address (defaults to 127.0.0.1)',
port: 'optional - GQRX target port (defaults to 7356)'

)



1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
# File 'lib/pwn/sdr/gqrx.rb', line 1046

public_class_method def self.analyze_scan(opts = {})
  scan_resp = opts[:scan_resp]
  raise 'ERROR: scan_resp is required.' if scan_resp.nil? || scan_resp[:signals].nil? || scan_resp[:signals].empty?

  target = opts[:target]
  port = opts[:port]
  gqrx_sock = connect(
    target: target,
    port: port
  )

  scan_resp[:signals].each do |signal|
    # puts JSON.pretty_generate(signal)
    signal[:gqrx_sock] = gqrx_sock

    # This is required to keep connection alive during analysis
    signal[:keep_alive] = true

    # We do this because we need keep_alive true for init_freq calls below
    squelch = signal[:squelch]
    squelch = cmd(gqrx_sock: gqrx_sock, cmd: 'l SQL').to_f if squelch.nil?
    change_squelch_resp = cmd(
      gqrx_sock: gqrx_sock,
      cmd: "L SQL #{squelch}",
      resp_ok: 'RPRT 0'
    )

    audio_gain_db = signal[:audio_gain_db] ||= 0.0
    audio_gain_db = audio_gain_db.to_f
    audio_gain_db_resp = cmd(
      gqrx_sock: gqrx_sock,
      cmd: "L AF #{audio_gain_db}",
      resp_ok: 'RPRT 0'
    )

    demodulator_mode = signal[:demodulator_mode] || :WFM
    mode_str = demodulator_mode.to_s.upcase

    bandwidth = signal[:bandwidth] ||= '200.000'
    passband_hz = PWN::SDR.hz_to_i(bandwidth)
    cmd(
      gqrx_sock: gqrx_sock,
      cmd: "M #{mode_str} #{passband_hz}",
      resp_ok: 'RPRT 0'
    )

    freq_obj = init_freq(signal)
    freq_obj = signal.merge(freq_obj)
    # Redact gqrx_sock from output
    freq_obj.delete(:gqrx_sock)
    unless freq_obj[:decoder]
      puts JSON.pretty_generate(freq_obj)
      print 'Press [ENTER] to continue...'
      gets
    end
    puts "\n" * 3
  end
rescue Interrupt
  puts "\nCTRL+C detected - goodbye."
rescue StandardError => e
  raise e
ensure
  disconnect(gqrx_sock: gqrx_sock)
end

.authorsObject

Author(s)

0day Inc. <support@0dayinc.com>



1240
1241
1242
1243
1244
# File 'lib/pwn/sdr/gqrx.rb', line 1240

public_class_method def self.authors
  "AUTHOR(S):
    0day Inc. <support@0dayinc.com>
  "
end

.cmd(opts = {}) ⇒ Object

Supported Method Parameters

gqrx_resp = PWN::SDR::GQRX.cmd(

gqrx_sock: 'required - GQRX socket object returned from #connect method',
cmd: 'required - GQRX command to execute',
resp_ok: 'optional - Expected response from GQRX to indicate success'

)



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
# File 'lib/pwn/sdr/gqrx.rb', line 18

public_class_method def self.cmd(opts = {})
  gqrx_sock = opts[:gqrx_sock]
  cmd = opts[:cmd]
  resp_ok = opts[:resp_ok]

  # Most Recent GQRX Command Set:
  # https://raw.githubusercontent.com/gqrx-sdr/gqrx/master/resources/remote-control.txt
  # Remote control protocol.
  #
  # Supported commands:
  #  f
  #     Get frequency [Hz]
  #  F <frequency>
  #     Set frequency [Hz]
  #  m
  #     Get demodulator mode and passband
  #  M <mode> [passband]
  #     Set demodulator mode and passband [Hz]
  #     Passing a '?' as the first argument instead of 'mode' will return
  #     a space separated list of radio backend supported modes.
  #  l|L ?
  #     Get a space separated list of settings available for reading (l) or writing (L).
  #  l STRENGTH
  #     Get signal strength [dBFS]
  #  l SQL
  #     Get squelch threshold [dBFS]
  #  L SQL <sql>
  #     Set squelch threshold to <sql> [dBFS]
  #  l AF
  #     Get audio gain [dB]
  #  L AF <gain>
  #     Set audio gain to <gain> [dB]
  #  l <gain_name>_GAIN
  #     Get the value of the gain setting with the name <gain_name>
  #  L <gain_name>_GAIN <value>
  #     Set the value of the gain setting with the name <gain_name> to <value>
  #  p RDS_PI
  #     Get the RDS PI code (in hexadecimal). Returns 0000 if not applicable.
  #  p RDS_PS_NAME
  #     Get the RDS Program Service (PS) name
  #  p RDS_RADIOTEXT
  #     Get the RDS RadioText message
  #  u RECORD
  #     Get status of audio recorder
  #  U RECORD <status>
  #     Set status of audio recorder to <status>
  #  u IQRECORD
  #     Get status of IQ recorder
  #  U IQRECORD <status>
  #     Set status of IQ recorder to <status>
  #  u DSP
  #     Get DSP (SDR receiver) status
  #  U DSP <status>
  #     Set DSP (SDR receiver) status to <status>
  #  u RDS
  #     Get RDS decoder status.  Only functions in WFM mode.
  #  U RDS <status>
  #     Set RDS decoder to <status>.  Only functions in WFM mode.
  #  u MUTE
  #     Get audio mute status
  #  U MUTE <status>
  #     Set audio mute to <status>
  #  q|Q
  #     Close connection
  #  AOS
  #     Acquisition of signal (AOS) event, start audio recording
  #  LOS
  #     Loss of signal (LOS) event, stop audio recording
  #  LNB_LO [frequency]
  #     If frequency [Hz] is specified set the LNB LO frequency used for
  #     display. Otherwise print the current LNB LO frequency [Hz].
  #  \chk_vfo
  #     Get VFO option status (only usable for hamlib compatibility)
  #  \dump_state
  #     Dump state (only usable for hamlib compatibility)
  #  \get_powerstat
  #     Get power status (only usable for hamlib compatibility)
  #  v
  #     Get 'VFO' (only usable for hamlib compatibility)
  #  V
  #     Set 'VFO' (only usable for hamlib compatibility)
  #  s
  #     Get 'Split' mode (only usable for hamlib compatibility)
  #  S
  #     Set 'Split' mode (only usable for hamlib compatibility)
  #  _
  #     Get version
  #
  #
  # Reply:
  #  RPRT 0
  #     Command successful
  #  RPRT 1
  #     Command failed

  gqrx_sock.write("#{cmd}\n")
  response = []
  start_time = Time.now

  # Wait up to 2 seconds for initial response
  if gqrx_sock.wait_readable(2.0)
    response.push(gqrx_sock.readline.chomp)
    # Drain any additional lines quickly
    loop do
      break if gqrx_sock.wait_readable(0.0001).nil?

      response.push(gqrx_sock.readline.chomp)
    end
  end

  raise "No response for command: #{cmd}" if response.empty?

  response_str = response.length == 1 ? response.first : response.join(' ')

  raise "ERROR!!! Command: #{cmd} Expected Resp: #{resp_ok}, Got: #{response_str}" if resp_ok && response_str != resp_ok

  # Reformat positive integer frequency responses (e.g., from 'f')
  response_str = PWN::SDR.hz_to_s(response_str) if response_str.match?(/^\d+$/) && response_str.to_i.positive?

  response_str
rescue RuntimeError => e
  response_str = 'Function not supported by this radio backend.' if e.message.include?('RF_GAIN') || e.message.include?('IF_GAIN') || e.message.include?('BB_GAIN')

  raise e unless e.message.include?('RF_GAIN') ||
                 e.message.include?('IF_GAIN') ||
                 e.message.include?('BB_GAIN')
rescue StandardError => e
  raise e
end

.connect(opts = {}) ⇒ Object

Supported Method Parameters

gqrx_sock = PWN::SDR::GQRX.connect(

target: 'optional - GQRX target IP address (defaults to 127.0.0.1)',
port: 'optional - GQRX target port (defaults to 7356)'

)



552
553
554
555
556
557
558
559
# File 'lib/pwn/sdr/gqrx.rb', line 552

public_class_method def self.connect(opts = {})
  target = opts[:target] ||= '127.0.0.1'
  port = opts[:port] ||= 7356

  PWN::Plugins::Sock.connect(target: target, port: port)
rescue StandardError => e
  raise e
end

.disconnect(opts = {}) ⇒ Object

Supported Method Parameters

PWN::SDR::GQRX.disconnect(

gqrx_sock: 'required - GQRX socket object returned from #connect method'

)



1230
1231
1232
1233
1234
1235
1236
# File 'lib/pwn/sdr/gqrx.rb', line 1230

public_class_method def self.disconnect(opts = {})
  gqrx_sock = opts[:gqrx_sock]

  PWN::Plugins::Sock.disconnect(sock_obj: gqrx_sock) unless gqrx_sock.closed?
rescue StandardError => e
  raise e
end

.disconnect_udp(opts = {}) ⇒ Object

Supported Method Parameters

PWN::SDR::GQRX.disconnect_udp(

udp_listener: 'required - UDP socket object returned from #listen_udp method'

)



1161
1162
1163
1164
1165
1166
1167
1168
# File 'lib/pwn/sdr/gqrx.rb', line 1161

public_class_method def self.disconnect_udp(opts = {})
  udp_listener = opts[:udp_listener]
  raise 'ERROR: udp_sock is required!' if udp_listener.nil?

  PWN::Plugins::Sock.disconnect(sock_obj: udp_listener) unless udp_listener.closed?
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
# File 'lib/pwn/sdr/gqrx.rb', line 1248

public_class_method def self.help
  puts "USAGE:
    gqrx_sock = #{self}.connect(
      target: 'optional - GQRX target IP address (defaults to 127.0.0.1)',
      port: 'optional - GQRX target port (defaults to 7356)'
    )

    gqrx_resp = #{self}.cmd(
      gqrx_sock: 'required - GQRX socket object returned from #connect method',
      cmd: 'required - GQRX command to send',
      resp_ok: 'optional - Expected OK response (defaults to nil / no check)'
    )

    freq_obj = #{self}.init_freq(
      gqrx_sock: 'required - GQRX socket object returned from #connect method',
      freq: 'required - Frequency to set',
      precision: 'optional - Frequency step precision (number of digits; defaults to 6)',
      demodulator_mode: 'optional - Demodulator mode (defaults to WFM)',
      bandwidth: 'optional - Bandwidth (defaults to \"200.000\")',
      decoder: 'optional - Decoder key (e.g., :gsm) to start live decoding (starts recording if provided)',
      suppress_details: 'optional - Boolean to include extra frequency details in return hash (defaults to false)',
      keep_alive: 'optional - Boolean to keep GQRX connection alive after method completion (defaults to false)'
    )

    scan_resp = #{self}.scan_range(
      gqrx_sock: 'required - GQRX socket object returned from #connect method',
      ranges: 'required - Array of Hash objects with :start_freq and :target_freq keys defining scan ranges',
      demodulator_mode: 'optional - Demodulator mode (e.g. WFM, AM, FM, USB, LSB, RAW, CW, RTTY / defaults to WFM)',
      bandwidth: 'optional - Bandwidth in Hz (Defaults to \"200.000\")',
      precision: 'optional - Precision (Defaults to 1)',
      strength_lock: 'optional - Strength lock (defaults to -70.0)',
      squelch: 'optional - Squelch level (defaults to strength_lock - 3.0)',
      audio_gain_db: 'optional - Audio gain in dB (defaults to 0.0)',
      rf_gain: 'optional - RF gain (defaults to 0.0)',
      intermediate_gain: 'optional - Intermediate gain (defaults to 32.0)',
      baseband_gain: 'optional - Baseband gain (defaults to 10.0)',
      keep_looping: 'optional - Boolean to keep scanning indefinitely (defaults to false)',
      scan_log: 'optional - Path to save detected signals log (defaults to /tmp/pwn_sdr_gqrx_scan_<start_freq>-<target_freq>_<timestamp>.json)',
      location: 'optional - Location string to include in AI analysis (e.g., \"New York, NY\", 90210, GPS coords, etc.)'
    )

    #{self}.analyze_scan(
      scan_resp: 'required - Scan response object from #scan_range method',
      target: 'optional - GQRX target IP address (defaults to 127.0.0.1)',
      port: 'optional - GQRX target port (defaults to 7356)'
    )

    #{self}.analyze_log(
      scan_log: 'required - Path to signals log file',
      target: 'optional - GQRX target IP address (defaults to 127.0.0.1)',
      port: 'optional - GQRX target port (defaults to 7356)'
    )

    udp_listener = #{self}.listen_udp(
      udp_ip: 'optional - IP address to bind UDP listener (defaults to 127.0.0.1)',
      udp_port: 'optional - Port to bind UDP listener (defaults to 7355)'
    )

    #{self}.disconnect_udp(
      udp_listener: 'required - UDP socket object returned from #listen_udp method'
    )

    iq_raw_file = #{self}.record(
      gqrx_sock: 'required - GQRX socket object returned from #connect method'
    )

    #{self}.stop_recording(
      gqrx_sock: 'required - GQRX socket object returned from #connect method',
      iq_raw_file: 'required - iq_raw_file returned from #record method'
    )

    #{self}.disconnect(
      gqrx_sock: 'required - GQRX socket object returned from #connect method'
    )

    #{self}.authors
  "
end

.init_freq(opts = {}) ⇒ Object

Supported Method Parameters

freq_obj = PWN::SDR::GQRX.init_freq(

gqrx_sock: 'required - GQRX socket object returned from #connect method',
freq: 'required - Frequency to set',
demodulator_mode: 'optional - Demodulator mode (defaults to WFM)',
bandwidth: 'optional - Bandwidth (defaults to "200.000")',
squelch: 'optional - Squelch level to set (Defaults to current value)',
decoder: 'optional - Decoder key (e.g., :gsm) to start live decoding (starts recording if provided)',
udp_ip: 'optional - UDP IP address for decoder module (defaults to 127.0.0.1)',
udp_port: 'optional - UDP port for decoder module (defaults to 7355)',
suppress_details: 'optional - Boolean to include extra frequency details in return hash (defaults to false)',
keep_alive: 'optional - Boolean to keep GQRX connection alive after method completion (defaults to false)'

)



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
# File 'lib/pwn/sdr/gqrx.rb', line 574

public_class_method def self.init_freq(opts = {})
  gqrx_sock = opts[:gqrx_sock]
  freq = opts[:freq]
  precision = opts[:precision] ||= 6
  valid_demodulator_modes = %i[
    AM
    AM_SYNC
    CW
    CWL
    CWU
    FM
    OFF
    LSB
    RAW
    USB
    WFM
    WFM_ST
    WFM_ST_OIRT
  ]
  demodulator_mode = opts[:demodulator_mode] ||= :WFM
  raise "ERROR: Invalid demodulator_mode '#{demodulator_mode}'. Valid modes: #{valid_demodulator_modes.join(', ')}" unless valid_demodulator_modes.include?(demodulator_mode.to_sym)

  bandwidth = opts[:bandwidth] ||= '200.000'
  squelch = opts[:squelch]
  decoder = opts[:decoder]
  udp_ip = opts[:udp_ip]
  udp_port = opts[:udp_port]
  suppress_details = opts[:suppress_details] || false
  keep_alive = opts[:keep_alive] || false

  unless keep_alive
    squelch = cmd(gqrx_sock: gqrx_sock, cmd: 'l SQL').to_f if squelch.nil?
    change_squelch_resp = cmd(
      gqrx_sock: gqrx_sock,
      cmd: "L SQL #{squelch}",
      resp_ok: 'RPRT 0'
    )

    mode_str = demodulator_mode.to_s.upcase
    passband_hz = PWN::SDR.hz_to_i(bandwidth)
    cmd(
      gqrx_sock: gqrx_sock,
      cmd: "M #{mode_str} #{passband_hz}",
      resp_ok: 'RPRT 0'
    )
  end

  tune_to(gqrx_sock: gqrx_sock, hz: freq)
  strength_db = measure_signal_strength(
    gqrx_sock: gqrx_sock,
    freq: freq,
    precision: precision,
    phase: :init_freq
  )

  freq_obj = {
    freq: freq,
    demodulator_mode: demodulator_mode,
    bandwidth: bandwidth,
    strength_db: strength_db,
    decoder: decoder,
    squelch: squelch
  }

  unless suppress_details
    demod_n_passband = cmd(
      gqrx_sock: gqrx_sock,
      cmd: 'm'
    )

    audio_gain_db = cmd(
      gqrx_sock: gqrx_sock,
      cmd: 'l AF'
    ).to_f

    squelch = cmd(
      gqrx_sock: gqrx_sock,
      cmd: 'l SQL'
    ).to_f

    rf_gain = cmd(
      gqrx_sock: gqrx_sock,
      cmd: 'l RF_GAIN'
    )

    if_gain = cmd(
      gqrx_sock: gqrx_sock,
      cmd: 'l IF_GAIN'
    )

    bb_gain = cmd(
      gqrx_sock: gqrx_sock,
      cmd: 'l BB_GAIN'
    )

    freq_obj[:audio_gain_db] = audio_gain_db
    freq_obj[:demod_mode_n_passband] = demod_n_passband
    freq_obj[:bb_gain] = bb_gain
    freq_obj[:if_gain] = if_gain
    freq_obj[:rf_gain] = rf_gain
    freq_obj[:squelch] = squelch

    # Start recording and decoding if decoder provided
    if decoder
      decoder = decoder.to_s.to_sym
      decoder_module = nil

      # Resolve decoder module via case statement for extensibility
      case decoder
      when :flex
        decoder_module = PWN::SDR::Decoder::Flex
      when :gsm
        decoder_module = PWN::SDR::Decoder::GSM
      when :pocsag
        decoder_module = PWN::SDR::Decoder::POCSAG
      when :rds
        decoder_module = PWN::SDR::Decoder::RDS
      else
        raise "ERROR: Unknown decoder key: #{decoder}. Supported: :gsm"
      end

      # Initialize and start decoder (module style: .start returns thread)
      freq_obj[:gqrx_sock] = gqrx_sock
      freq_obj[:udp_ip] = udp_ip
      freq_obj[:udp_port] = udp_port
      freq_obj[:decoder_module] = decoder_module
      decoder_module.decode(freq_obj: freq_obj)
    end
  end

  freq_obj
rescue StandardError => e
  raise e
ensure
  disconnect(gqrx_sock: gqrx_sock) if gqrx_sock.is_a?(TCPSocket) && !keep_alive
end

.listen_udp(opts = {}) ⇒ Object

Supported Method Parameters

udp_listener = PWN::SDR::GQRX.listen_udp(

udp_ip: 'optional - IP address to bind UDP listener (defaults to 127.0.0.1)',
upd_port: 'optional - Port to bind UDP listener (defaults to 7355)'

)



1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
# File 'lib/pwn/sdr/gqrx.rb', line 1142

public_class_method def self.listen_udp(opts = {})
  udp_ip = opts[:udp_ip] ||= '127.0.0.1'
  udp_port = opts[:udp_port] ||= 7355

  PWN::Plugins::Sock.listen(
    server_ip: udp_ip,
    port: udp_port,
    protocol: :udp,
    detach: true
  )
rescue StandardError => e
  raise e
end

.record(opts = {}) ⇒ Object

Supported Method Parameters

iq_raw_file = PWN::SDR::GQRX.record(

gqrx_sock: 'required - GQRX socket object returned from #connect method'

)



1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
# File 'lib/pwn/sdr/gqrx.rb', line 1175

public_class_method def self.record(opts = {})
  gqrx_sock = opts[:gqrx_sock]
  raise 'ERROR: gqrx_sock is required!' if gqrx_sock.nil?

  # Toggle I/Q RECORD on in GQRX for brevity
  cmd(
    gqrx_sock: gqrx_sock,
    cmd: 'U IQRECORD 0',
    resp_ok: 'RPRT 0'
  )

  cmd(
    gqrx_sock: gqrx_sock,
    cmd: 'U IQRECORD 1',
    resp_ok: 'RPRT 0'
  )

  record_dir = Dir.home
  iq_raw_file = Dir.glob("#{record_dir}/gqrx_*.raw").max_by { |f| File.mtime(f) }
  raise 'ERROR: No GQRX .raw I/Q data file found!' unless iq_raw_file

  iq_raw_file
rescue StandardError => e
  raise e
end

.scan_range(opts = {}) ⇒ Object

Supported Method Parameters

scan_resp = PWN::SDR::GQRX.scan_range(

gqrx_sock: 'required - GQRX socket object returned from #connect method',
ranges: 'required - Array of Hash objects with :start_freq and :target_freq keys defining scan ranges',
demodulator_mode: 'optional - Demodulator mode (e.g. WFM, AM, FM, USB, LSB, RAW, CW, RTTY / defaults to WFM)',
bandwidth: 'optional - Bandwidth in Hz (Defaults to "200.000")',
precision: 'optional - Frequency step precision (number of digits; defaults to 1)',
strength_lock: 'optional - Strength lock in dBFS (defaults to -70.0)',
squelch: 'optional - Squelch level in dBFS (defaults to strength_lock - 3.0)',
audio_gain_db: 'optional - Audio gain in dB (defaults to 0.0)',
rf_gain: 'optional - RF gain (defaults to 0.0)',
intermediate_gain: 'optional - Intermediate gain (defaults to 32.0)',
baseband_gain: 'optional - Baseband gain (defaults to 10.0)',
keep_looping: 'optional - Boolean to keep scanning indefinitely (defaults to false)',
scan_log: 'optional - Path to save detected signals log (defaults to /tmp/pwn_sdr_gqrx_scan_<start_freq>-<target_freq>_<timestamp>_lN.json)',
location: 'optional - Location string to include in AI analysis (e.g., "New York, NY", 90210, GPS coords, etc.)'

)



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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
# File 'lib/pwn/sdr/gqrx.rb', line 729

public_class_method def self.scan_range(opts = {})
  timestamp_start = Time.now.strftime('%Y-%m-%d %H:%M:%S%z')
  range_timestamp_start = ''

  gqrx_sock = opts[:gqrx_sock]

  ranges = opts[:ranges]
  raise 'ERROR: ranges must be an Array of Hash objects with :start_freq and :target_freq keys.' unless ranges.is_a?(Array) && ranges.all? { |r| r.is_a?(Hash) && r.key?(:start_freq) && r.key?(:target_freq) }

  demodulator_mode = opts[:demodulator_mode]

  bandwidth = opts[:bandwidth] ||= '200.000'

  precision = opts[:precision] ||= 1
  raise 'ERROR: precision must be an Integer between 1 and 12.' unless precision.is_a?(Integer) && precision.between?(1, 12)

  step_hz = 10**(precision - 1)

  strength_lock = opts[:strength_lock] ||= -70.0
  squelch = opts[:squelch] ||= (strength_lock - 3.0)
  raise 'ERROR: squelch must always be less than strength_lock.' if squelch >= strength_lock

  decoder = opts[:decoder]
  keep_looping = opts[:keep_looping] || false
  log_timestamp = Time.now.strftime('%Y-%m-%d')

  location = opts[:location] ||= 'United States'

  # This is for looping through ranges indefinitely if keep_looping is true
  # Generate ranges strings for log filename
  range_str = ''
  ranges.each do |range|
    start_freq = range[:start_freq]
    hz_start = PWN::SDR.hz_to_i(start_freq)
    hz_start_str = PWN::SDR.hz_to_s(hz_start)

    target_freq = range[:target_freq]
    hz_target = PWN::SDR.hz_to_i(target_freq)
    hz_target_str = PWN::SDR.hz_to_s(hz_target)

    range_str = "#{range_str}_#{hz_start_str}-#{hz_target_str}"
  end
  scan_log = opts[:scan_log] ||= "/tmp/pwn_sdr_gqrx_scan#{range_str}_#{log_timestamp}.json"

  iteration_metrics = []
  candidate_signals = []
  signals_detected = []
  iteration_total = 1
  signals_detected_total = 0
  loop do
    signals_detected_delta = 0
    iter_metrics_hash = {}
    ranges.each do |range|
      range_timestamp_start = Time.now.strftime('%Y-%m-%d %H:%M:%S%z')
      iter_metrics_hash[:iteration] = iteration_total
      iter_metrics_hash[:range] = range
      iter_metrics_hash[:timestamp_start] = range_timestamp_start

      # Verify all frequencies are valid
      start_freq = range[:start_freq]
      hz_start = PWN::SDR.hz_to_i(start_freq)
      raise "ERROR: Invalid start_freq '#{start_freq}' provided." if hz_start.zero?

      target_freq = range[:target_freq]
      hz_target = PWN::SDR.hz_to_i(target_freq)
      hz_target_str = PWN::SDR.hz_to_s(hz_target)
      raise "ERROR: Invalid target_freq '#{target_freq}' provided." if hz_target.zero?

      step_hz_direction = hz_start > hz_target ? -step_hz : step_hz
      noise_floor = measure_noise_floor(
        gqrx_sock: gqrx_sock,
        freq: start_freq,
        precision: precision,
        step_hz_direction: step_hz_direction
      )
      if squelch < noise_floor
        squelch = noise_floor.round + 7
        strength_lock = squelch + 3.0
        puts "Adjusted strength_lock to #{strength_lock} dBFS and squelch to #{squelch} dBFS based on measured noise floor.  This ensures proper signal detection..."
      end

      # Begin scanning range
      puts "\n"
      puts '-' * 86
      puts 'SESSION PARAMS >> Scan Range(s):'
      puts ranges
      puts "SESSION PARAMS >> Step Increment: #{PWN::SDR.hz_to_s(step_hz_direction.abs)} Hz."
      puts "SESSION PARAMS >> Continuously Loop through Scan Range(s): #{keep_looping}"
      puts "\nIf scans are slow and/or you're experiencing false positives/negatives,"
      puts 'consider adjusting the following:'
      puts "1. The SDR's sample rate in GQRX"
      puts "\s\s- Click on `Configure I/O devices`."
      puts "\s\s- A lower `Input rate` value seems counter-intuitive but works well (e.g. ADALM PLUTO ~ 1000000)."
      puts '2. Adjust the :strength_lock parameter.'
      puts '3. Adjust the :precision parameter.'
      puts '4. Disable AI introspection in PWN::Env'
      puts 'Happy scanning!'
      puts '-' * 86
      # print 'Pressing ENTER to begin scan...'
      # gets
      puts "\n\n\n"

      # Set squelch once for each range
      change_squelch_resp = cmd(
        gqrx_sock: gqrx_sock,
        cmd: "L SQL #{squelch}",
        resp_ok: 'RPRT 0'
      )

      # We always disable RDS decoding during the scan
      # to prevent unnecessary processing overhead.
      # We return the rds boolean in the scan_resp object
      # so it will be picked up and used appropriately
      # when calling analyze_scan or analyze_log methods.
      rds_resp = cmd(
        gqrx_sock: gqrx_sock,
        cmd: 'U RDS 0',
        resp_ok: 'RPRT 0'
      )

      # Set demodulator mode & passband once for the scan
      mode_str = demodulator_mode.to_s.upcase
      passband_hz = PWN::SDR.hz_to_i(bandwidth)
      cmd(
        gqrx_sock: gqrx_sock,
        cmd: "M #{mode_str} #{passband_hz}",
        resp_ok: 'RPRT 0'
      )

      audio_gain_db = opts[:audio_gain_db] ||= 0.0
      audio_gain_db = audio_gain_db.to_f
      audio_gain_db_resp = cmd(
        gqrx_sock: gqrx_sock,
        cmd: "L AF #{audio_gain_db}",
        resp_ok: 'RPRT 0'
      )

      rf_gain = opts[:rf_gain] ||= 0.0
      rf_gain = rf_gain.to_f
      rf_gain_resp = cmd(
        gqrx_sock: gqrx_sock,
        cmd: "L RF_GAIN #{rf_gain}",
        resp_ok: 'RPRT 0'
      )

      intermediate_gain = opts[:intermediate_gain] ||= 32.0
      intermediate_gain = intermediate_gain.to_f
      intermediate_resp = cmd(
        gqrx_sock: gqrx_sock,
        cmd: "L IF_GAIN #{intermediate_gain}",
        resp_ok: 'RPRT 0'
      )

      baseband_gain = opts[:baseband_gain] ||= 10.0
      baseband_gain = baseband_gain.to_f
      baseband_resp = cmd(
        gqrx_sock: gqrx_sock,
        cmd: "L BB_GAIN #{baseband_gain}",
        resp_ok: 'RPRT 0'
      )

      prev_freq_obj = init_freq(
        gqrx_sock: gqrx_sock,
        freq: hz_start,
        precision: precision,
        demodulator_mode: demodulator_mode,
        bandwidth: bandwidth,
        squelch: squelch,
        decoder: decoder,
        suppress_details: true,
        keep_alive: true
      )

      start_freq = range[:start_freq]
      hz_start = PWN::SDR.hz_to_i(start_freq)
      hz = hz_start

      target_freq = range[:target_freq]
      hz_target = PWN::SDR.hz_to_i(target_freq)

      # puts "#{range} #{start_freq} (#{hz_start})to #{target_freq} (#{hz_target})"
      # gets
      # while step_hz_direction.positive? ? hz <= hz_target : hz >= hz_target
      while (step_hz_direction.positive? && hz <= hz_target) || (step_hz_direction.negative? && hz >= hz_target)
        tune_to(gqrx_sock: gqrx_sock, hz: hz)
        strength_db = measure_signal_strength(
          gqrx_sock: gqrx_sock,
          freq: hz,
          precision: precision,
          strength_lock: strength_lock,
          phase: :find_candidates
        )

        if strength_db >= strength_lock
          puts '-' * 86
          # Find left and right edges of the signal
          candidate_signals = edge_detection(
            gqrx_sock: gqrx_sock,
            hz: hz,
            step_hz: step_hz,
            precision: precision,
            strength_lock: strength_lock
          )
        elsif candidate_signals.length.positive?
          best_peak = find_best_peak(
            gqrx_sock: gqrx_sock,
            candidate_signals: candidate_signals,
            precision: precision,
            step_hz: step_hz,
            strength_lock: strength_lock
          )

          if best_peak[:hz] && best_peak[:strength_db] > strength_lock
            puts "\n**** Detected Signal ****"
            best_freq = PWN::SDR.hz_to_s(best_peak[:hz])
            best_strength_db = best_peak[:strength_db]
            prev_freq_obj = init_freq(
              gqrx_sock: gqrx_sock,
              freq: best_freq,
              precision: precision,
              demodulator_mode: demodulator_mode,
              bandwidth: bandwidth,
              squelch: squelch,
              decoder: decoder,
              suppress_details: true,
              keep_alive: true
            )
            prev_freq_obj[:strength_lock] = strength_lock
            prev_freq_obj[:strength_db] = best_strength_db.round(1)
            prev_freq_obj[:iteration] = iteration_total

            ai_analysis = PWN::AI::Agent::GQRX.analyze(
              request: prev_freq_obj.to_json,
              location: location
            )

            prev_freq_obj[:ai_analysis] = ai_analysis unless ai_analysis.nil?
            puts JSON.pretty_generate(prev_freq_obj)
            puts '-' * 86
            puts "\n\n\n"
            signals_detected.push(prev_freq_obj)
            log_signals(
              signals_detected: signals_detected,
              timestamp_start: timestamp_start,
              scan_log: scan_log
            )
            hz = candidate_signals.last[:hz]
            # gets
          end
          candidate_signals.clear
        end
        hz += step_hz_direction
      end

      log_signals(
        signals_detected: signals_detected,
        timestamp_start: timestamp_start,
        scan_log: scan_log
      )
    end
    break unless keep_looping

    # Determine how many new signals were detected this iteration
    # Reduces signals_detected to an array of unique frequencies only
    signals_detected.uniq! { |s| PWN::SDR.hz_to_i(s[:freq]) }
    signals_detected_total = signals_detected.select { |s| s[:iteration] == iteration_total }.length
    signals_detected_delta = signals_detected_total - signals_detected_delta
    start_next_iteration = case signals_detected_delta
                           when 0
                             30
                           when 1..5
                             10
                           else
                             5
                           end

    range_timestamp_end = Time.now.strftime('%Y-%m-%d %H:%M:%S%z')
    iter_metrics_hash[:timestamp_end] = range_timestamp_end

    duration = duration_between(timestamp_start: range_timestamp_start, timestamp_end: range_timestamp_end)
    iter_metrics_hash[:duration] = duration
    iter_metrics_hash[:signals_detected] = signals_detected_delta

    iteration_metrics.push(iter_metrics_hash)
    puts "\nScan iteration(s) ##{iteration_total} complete."
    puts JSON.pretty_generate(iter_metrics_hash)

    puts "Resuming next scan iteration in #{start_next_iteration} seconds.  Press CTRL+C to exit"
    start_next_iteration.times do
      print '.'
      sleep 1
    end
    puts "\n"

    # Log current signals one last time just to capture scan iterations accurately
    iteration_total += 1
    log_signals(
      signals_detected: signals_detected,
      timestamp_start: timestamp_start,
      scan_log: scan_log,
      iteration_metrics: iteration_metrics
    )
  end
rescue Interrupt
  puts "\nCTRL+C detected - goodbye."
rescue StandardError => e
  raise e
ensure
  disconnect(gqrx_sock: gqrx_sock) if defined?(gqrx_sock) && gqrx_sock.is_a?(TCPSocket)
end

.stop_recording(opts = {}) ⇒ Object

Supported Method Parameters

PWN::SDR::GQRX.stop_recording(

gqrx_sock: 'required - GQRX socket object returned from #connect method',
iq_raw_file: 'required - iq_raw_file returned from #connect method'

)



1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
# File 'lib/pwn/sdr/gqrx.rb', line 1207

public_class_method def self.stop_recording(opts = {})
  gqrx_sock = opts[:gqrx_sock]
  raise 'ERROR: gqrx_sock is required!' if gqrx_sock.nil?

  iq_raw_file = opts[:iq_raw_file]
  raise 'ERROR: iq_raw_file is required!' if iq_raw_file.nil?

  # Toggle IQRECORD off
  cmd(
    gqrx_sock: gqrx_sock,
    cmd: 'U IQRECORD 0',
    resp_ok: 'RPRT 0'
  )

  FileUtils.rm_f(iq_raw_file)
rescue StandardError => e
  raise e
end