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)' )



1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
# File 'lib/pwn/sdr/gqrx.rb', line 1105

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)' )



1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
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
# File 'lib/pwn/sdr/gqrx.rb', line 1034

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(freq: 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



1680
1681
1682
1683
1684
# File 'lib/pwn/sdr/gqrx.rb', line 1680

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(freq: 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' )



1218
1219
1220
1221
1222
1223
1224
# File 'lib/pwn/sdr/gqrx.rb', line 1218

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' )



1149
1150
1151
1152
1153
1154
1155
1156
# File 'lib/pwn/sdr/gqrx.rb', line 1149

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

.fast_scan_range(opts = {}) ⇒ Object

Supported Method Parameters

fast_resp = PWN::SDR::GQRX.fast_scan_range( gqrx_sock: 'required - GQRX socket object', ranges: 'required - Array of {start_freq:, target_freq: }', sample_rate: 'optional - Set this to GQRX visible input sample rate (the span width)', nfft: 'optional - FFT size', avg: 'optional', capture_secs: 'optional', strength_lock: 'optional', min_snr_db: 'optional - Minimum SNR in dB above per-chunk noise floor to report (defaults to 12.0)', precision: 'optional - Band-plan channel raster; detections snapped to 10**(precision-1) Hz grid (defaults to 5)', min_bw_ratio: 'optional - Reject FFT peaks narrower than min_bw_ratio * plan bandwidth as spurs (defaults to 0.30)', demodulator_mode: 'optional - Demodulator mode to attribute to detections (defaults to WFM)', bandwidth: 'optional - Passband bandwidth to attribute to detections (defaults to "200.000")', squelch: 'optional - Squelch level (defaults to strength_lock - 3.0)', decoder: 'optional - Decoder key (e.g. :gsm) to attribute to detections', location: 'optional - Location string for AI analysis', keep_spectrum: 'optional - if true return raw spectrum arrays too (large)' )

Uses chunk-wise retuning where chunk = sample_rate so that the entire visible band (waterfall width) is captured via a single FFT each time rather than point-by-point hops. This yields near real-time panoramic coverage. Update rate is roughly (retune + capture + fft) per chunk.

Per-signal output shape is INTENTIONALLY IDENTICAL to #scan_range / #init_freq (:freq, :demodulator_mode, :bandwidth, :strength_db, :decoder, :squelch, :strength_lock, :iteration, :ai_analysis) so that #analyze_scan / #analyze_log and downstream decoders behave the same regardless of which scan mode produced the log. FFT-specific extras (:hz, :bw_hz, :snr_db, :prominence_db, :noise_floor_db, :chunk_center, :method) are appended for provenance.



1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
# File 'lib/pwn/sdr/gqrx.rb', line 1496

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

  ranges = opts[:ranges]
  raise 'ranges required as array of hashes' unless ranges.is_a?(Array) && !ranges.empty?

  sr = (opts[:sample_rate] || 1_000_000).to_i
  nfft = (opts[:nfft] || 2048).to_i
  avgs = (opts[:avg] || 8).to_i
  cap = (opts[:capture_secs] || 0.10).to_f
  strength_lock = (opts[:strength_lock] || -70.0).to_f
  min_snr_db = (opts[:min_snr_db] || 12.0).to_f
  keep_spec = opts[:keep_spectrum] ? true : false
  res_hz = sr / nfft.to_f
  demodulator_mode = opts[:demodulator_mode] ||= :WFM
  bandwidth = opts[:bandwidth] ||= '200.000'
  squelch = (opts[:squelch] || (strength_lock - 3.0)).to_f
  decoder = opts[:decoder]
  location = opts[:location] ||= 'United States'
  log_timestamp = Time.now.strftime('%Y-%m-%d')

  # ---- Band-plan-aware candidate validation -------------------------
  # `:precision` and `:bandwidth` come straight from
  # PWN::SDR::FrequencyAllocation.band_plans. They encode two facts the
  # raw FFT peak detector cannot know:
  #   1. The channel *raster* (step_hz = 10**(precision-1)) that real
  #      emitters are aligned to. Detections are snapped to this grid so
  #      the same station seen in overlapping chunks lands on ONE hz.
  #   2. The expected *occupied bandwidth* of a single legitimate
  #      emitter. Any FFT peak narrower than min_bw_ratio * plan_bw_hz
  #      is a spur / pilot / IMD product, not a channel; any two peaks
  #      closer than ~half a plan_bw_hz are sub-components of the SAME
  #      emitter (e.g. WFM stereo pilot @19k, RDS @57k) and are merged.
  precision = (opts[:precision] || 5).to_i
  precision = precision.clamp(1, 12)
  step_hz = 10**(precision - 1)
  plan_bw_hz = PWN::SDR.hz_to_i(freq: bandwidth)
  plan_bw_hz = step_hz if plan_bw_hz.zero?
  min_bw_ratio = (opts[:min_bw_ratio] || 0.30).to_f
  min_bw_hz = [(plan_bw_hz * min_bw_ratio).to_i, res_hz.ceil].max

  range_str = ranges.map do |rr|
    a = PWN::SDR.hz_to_s(freq: PWN::SDR.hz_to_i(freq: rr[:start_freq]))
    b = PWN::SDR.hz_to_s(freq: PWN::SDR.hz_to_i(freq: rr[:target_freq]))
    "#{a}-#{b}"
  end.join('_')
  scan_log = opts[:scan_log] ||= "/tmp/pwn_sdr_gqrx_scan_#{range_str}_#{log_timestamp}.json"

  ts_start = Time.now.strftime('%Y-%m-%d %H:%M:%S%z')
  detected = []
  all_specs = [] if keep_spec

  puts "[FAST-SCAN] band-plan gate: raster=#{PWN::SDR.hz_to_s(freq: step_hz)} Hz (precision #{precision}), " \
       "plan_bw=#{PWN::SDR.hz_to_s(freq: plan_bw_hz)} Hz, min_occupied_bw>=#{PWN::SDR.hz_to_s(freq: min_bw_hz)} Hz, " \
       "min_snr>=#{min_snr_db} dB"

  ranges.each do |r|
    s_hz = PWN::SDR.hz_to_i(freq: r[:start_freq])
    t_hz = PWN::SDR.hz_to_i(freq: r[:target_freq])
    dir = t_hz >= s_hz ? 1 : -1
    step = (sr * 0.85).to_i # chunk step - use 85% to have overlap / avoid edge artifacts
    step = sr if step < 100_000

    puts "[FAST-SCAN] Panoramic covering #{PWN::SDR.hz_to_s(freq: s_hz)}..#{PWN::SDR.hz_to_s(freq: t_hz)} using #{sr} SPS chunks (step #{PWN::SDR.hz_to_s(freq: step)})"

    h = s_hz
    while dir.positive? ? (h <= t_hz) : (h >= t_hz)
      # retune to put this chunk in the visible IF
      tune_to(gqrx_sock: gqrx_sock, hz: h)
      sleep 0.15 # allow GQRX / SDR to settle the IF filter & AGC etc.

      snap = get_spectrum_snapshot(
        gqrx_sock: gqrx_sock,
        center_freq: h,
        sample_rate: sr,
        nfft: nfft,
        avg: avgs,
        capture_secs: cap,
        strength_offset_db: opts[:strength_offset_db]
      )

      all_specs << snap if keep_spec

      sigs = snap[:signals] || []
      # Gate on SNR (scale-independent) rather than absolute power_db,
      # because 10*log10(|FFT|^2) is uncalibrated and cannot be compared
      # against the -70 dBFS S-meter strength_lock without a
      # user-supplied strength_offset_db. NO fallback: a quiet chunk
      # correctly contributes zero detections.
      sigs.each do |sig|
        next if sig[:snr_db] && sig[:snr_db] < min_snr_db
        next if sig[:power_db] && sig[:power_db] < strength_lock
        # Band-plan width gate: reject spurs / pilots / IMD narrower
        # than a plausible fraction of the expected channel bandwidth.
        next if sig[:bw_hz] && sig[:bw_hz] < min_bw_hz

        raw_hz = sig[:hz] || sig[:freq_hz]
        # Snap to the band-plan channel raster so the same emitter seen
        # in multiple overlapping chunks / at multiple sub-peaks lands
        # on ONE canonical frequency before dedup.
        hz = ((raw_hz.to_f / step_hz).round * step_hz).to_i
        # Shape MUST match #scan_range / #init_freq freq_obj so that
        # analyze_scan / analyze_log and downstream decoders work
        # identically regardless of which scan mode produced the log.
        detected << {
          freq: PWN::SDR.hz_to_s(freq: hz),
          demodulator_mode: demodulator_mode,
          bandwidth: bandwidth,
          strength_db: sig[:power_db],
          decoder: decoder,
          squelch: squelch,
          strength_lock: strength_lock,
          iteration: 1,
          hz: hz,
          raw_peak_hz: raw_hz,
          bw_hz: sig[:bw_hz],
          snr_db: sig[:snr_db],
          prominence_db: sig[:prominence_db],
          noise_floor_db: sig[:noise_floor_db],
          chunk_center: PWN::SDR.hz_to_s(freq: h),
          method: :fast_spectrum_sdrangel_like
        }
      end
      h += step * dir
    end
  end

  # Cross-chunk / intra-emitter merge. A single legitimate emitter
  # occupies ~plan_bw_hz, so ANY peaks within half that width (or one
  # raster step, or half the *measured* width, whichever is largest) are
  # the same channel. Keep the highest-SNR representative.
  detected.sort_by! { |d| d[:hz] }
  merged = []
  detected.each do |d|
    prev = merged.last
    tol = [
      (plan_bw_hz / 2),
      step_hz,
      (d[:bw_hz].to_i / 2),
      (res_hz * 2).ceil
    ].max
    if prev && (d[:hz] - prev[:hz]).abs <= tol
      merged[-1] = d if (d[:snr_db] || -999) > (prev[:snr_db] || -999)
    else
      merged << d
    end
  end
  detected = merged

  # Attach AI analysis per detection AFTER merge so we only spend
  # inference on the deduplicated set (matches #scan_range behaviour).
  detected.each do |freq_obj|
    puts "\n**** Detected Signal ****"
    ai_analysis = PWN::AI::Agent::GQRX.analyze(
      request: freq_obj.to_json,
      location: location
    )
    freq_obj[:ai_analysis] = ai_analysis unless ai_analysis.nil?
    puts JSON.pretty_generate(freq_obj)
    puts '-' * 86
  rescue StandardError
    # AI analysis is best-effort; never let it kill the scan.
    puts JSON.pretty_generate(freq_obj)
  end

  resp = log_signals(
    signals_detected: detected,
    timestamp_start: ts_start,
    scan_log: scan_log
  )
  resp[:spectrums] = all_specs if keep_spec
  resp[:sample_rate_used] = sr
  resp[:nfft] = nfft
  resp[:precision] = precision
  resp[:plan_bw_hz] = plan_bw_hz
  resp[:method] = :fast_scan_range
  File.write(scan_log, JSON.pretty_generate(resp))
  File.write(scan_log, "\n", mode: 'a')
  resp
rescue StandardError => e
  raise e
end

.get_spectrum_snapshot(opts = {}) ⇒ Object

Author(s)

0day Inc. support@0dayinc.com



1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
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
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
# File 'lib/pwn/sdr/gqrx.rb', line 1228

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

  center_freq = opts[:center_freq]
  center_freq ||= cmd(gqrx_sock: gqrx_sock, cmd: 'f')
  center_hz = PWN::SDR.hz_to_i(freq: center_freq)

  sample_rate = (opts[:sample_rate] || 1_000_000).to_i
  nfft = (opts[:nfft] || 2048).to_i
  avg = (opts[:avg] || 8).to_i
  capture_secs = (opts[:capture_secs] || 0.10).to_f
  strength_offset_db = (opts[:strength_offset_db] || 0.0).to_f

  num_samples = (sample_rate * capture_secs).to_i
  num_samples = [num_samples, nfft].max
  num_samples = ((num_samples.to_f / nfft).ceil * nfft).to_i

  puts "[*] Capturing ~#{format('%.3f', capture_secs)}s I/Q (#{num_samples} samples @ #{sample_rate} SPS) => #{sample_rate / 1_000_000.0} MHz instantaneous span"

  # Start a fresh short IQ recording for snapshot (entire visible band at once)
  begin
    cmd(gqrx_sock: gqrx_sock, cmd: 'U IQRECORD 0', resp_ok: 'RPRT 0')
  rescue StandardError
    nil
  end
  sleep 0.02
  cmd(gqrx_sock: gqrx_sock, cmd: 'U IQRECORD 1', resp_ok: 'RPRT 0')
  sleep(capture_secs + 0.12)
  cmd(gqrx_sock: gqrx_sock, cmd: 'U IQRECORD 0', resp_ok: 'RPRT 0')

  # Newest raw I/Q produced by GQRX (usually ~/gqrx_*.raw , float32 I/Q interleaved @ sample_rate)
  home = Dir.home
  iq_raw_file = Dir.glob("#{home}/gqrx_*.raw").max_by { |f| File.mtime(f) }
  raise "ERROR: No GQRX .raw file found after capture (looked in #{home})" unless iq_raw_file && File.exist?(iq_raw_file)

  # Read tail of most recent bytes
  total_bytes = num_samples * 8 # float32 * 2 channels
  fsize = File.size(iq_raw_file)
  start_pos = [0, fsize - total_bytes].max
  raw_bytes = File.binread(iq_raw_file, total_bytes, start_pos)

  # ---- Pure-Ruby FFT + SDRangel-style peak detection (no external interpreters) ----
  raise 'ERROR: I/Q read empty or short' if raw_bytes.nil? || raw_bytes.bytesize < 8

  # GQRX raw I/Q: little-endian float32 interleaved I,Q,I,Q,...
  floats = raw_bytes.unpack('e*')
  n_iq = [floats.length / 2, num_samples].min
  iq = Array.new(n_iq) { |i| Complex(floats[2 * i], floats[(2 * i) + 1]) }

  # Hann window
  two_pi = 2.0 * Math::PI
  hann = Array.new(nfft) { |k| 0.5 * (1.0 - Math.cos(two_pi * k / (nfft - 1))) }

  # Iterative in-place radix-2 Cooley-Tukey FFT (nfft must be a power of two)
  raise "ERROR: nfft (#{nfft}) must be a power of two" unless nfft.nobits?(nfft - 1)

  log2n = Math.log2(nfft).to_i
  fft_proc = lambda do |x|
    n = x.length
    # bit-reversal permutation
    j = 0
    (0...(n - 1)).each do |i|
      x[i], x[j] = x[j], x[i] if i < j
      k = n >> 1
      while k <= j
        j -= k
        k >>= 1
      end
      j += k
    end
    # butterflies
    (1..log2n).each do |stage|
      m = 1 << stage
      half = m >> 1
      wm = Complex.polar(1.0, -two_pi / m)
      (0...n).step(m) do |kk|
        w = Complex(1.0, 0.0)
        (0...half).each do |jj|
          t = w * x[kk + jj + half]
          u = x[kk + jj]
          x[kk + jj] = u + t
          x[kk + jj + half] = u - t
          w *= wm
        end
      end
    end
    x
  end

  # Overlapping (50%) windowed FFTs, power-averaged (Welch-style)
  hop = [nfft / 2, 1].max
  specs = []
  pos = 0
  while pos + nfft <= iq.length && specs.length < avg
    blk = Array.new(nfft) { |k| iq[pos + k] * hann[k] }
    sp = fft_proc.call(blk)
    half = nfft / 2
    shifted = sp[half, nfft - half] + sp[0, half]
    specs << shifted.map(&:abs2)
    pos += hop
  end
  if specs.empty?
    blk = Array.new(nfft) do |k|
      (k < iq.length ? iq[k] : Complex(0.0, 0.0)) * hann[k]
    end
    sp = fft_proc.call(blk)
    half = nfft / 2
    shifted = sp[half, nfft - half] + sp[0, half]
    specs << shifted.map(&:abs2)
  end

  avg_pwr = Array.new(nfft, 0.0)
  specs.each { |ps| ps.each_with_index { |v, i| avg_pwr[i] += v } }
  cnt = specs.length.to_f
  avg_pwr.map! { |v| v / cnt }

  db = avg_pwr.map { |v| (10.0 * Math.log10(v + 1e-12)) + strength_offset_db }

  # fftshift(fftfreq(nfft, 1/sr)) => bins from -sr/2 .. +sr/2 (exclusive), step sr/nfft
  res_hz = sample_rate / nfft.to_f
  freq_off = Array.new(nfft) { |i| (i - (nfft / 2)) * res_hz }

  bins_out = Array.new(nfft) do |ii|
    fh = (center_hz + freq_off[ii]).to_i
    {
      bin: ii,
      freq_hz: fh,
      freq: PWN::SDR.hz_to_s(freq: fh),
      power_db: db[ii].round(2)
    }
  end

  # Null DC / LO-leakage bin and band-edge guard bins BEFORE detection so
  # they neither skew the noise-floor estimate nor register as phantom
  # signals at the centre of every retune step.
  guard = [(nfft * 0.02).to_i, 2].max
  dc = nfft / 2
  sorted_db = db.sort
  median_nf = sorted_db[sorted_db.length / 2].to_f
  db[dc] = median_nf
  (0...guard).each do |gi|
    db[gi] = median_nf
    db[nfft - 1 - gi] = median_nf
  end

  # Noise floor: median of dB (robust; 12th-percentile put nf+6dB ~= mean
  # noise so ~half of noise bins already cleared the height threshold).
  noise_floor = median_nf

  # SDRangel-style peak detection: local maxima above noise_floor+10dB with
  # min bin separation and >= 6 dB prominence. Thresholds are relative to
  # the MEDIAN noise floor so avg>=8 yields ~0 false positives on pure noise.
  height_thr = noise_floor + 10.0
  min_dist = [3, (6000.0 / res_hz).to_i].max
  prom_thr = 6.0

  candidates = []
  (1...(nfft - 1)).each do |i|
    next unless db[i] >= height_thr
    next unless db[i] > db[i - 1] && db[i] >= db[i + 1]

    # prominence: peak - highest of the two side-valley minima toward the
    # nearest higher-or-equal neighbour (scipy.signal.peak_prominences algorithm)
    left_min = db[i]
    li = i - 1
    while li >= 0 && db[li] <= db[i]
      left_min = db[li] if db[li] < left_min
      li -= 1
    end
    right_min = db[i]
    ri = i + 1
    while ri < nfft && db[ri] <= db[i]
      right_min = db[ri] if db[ri] < right_min
      ri += 1
    end
    prom = db[i] - [left_min, right_min].max
    next if prom < prom_thr

    candidates << { idx: i, pwr: db[i], prom: prom }
  end

  # Enforce minimum distance between peaks (keep strongest first)
  candidates.sort_by! { |c| -c[:pwr] }
  selected = []
  candidates.each do |c|
    selected << c unless selected.any? { |s2| (s2[:idx] - c[:idx]).abs < min_dist }
  end
  selected.sort_by! { |c| c[:idx] }

  edge_thr = noise_floor + 3.5
  signals = selected.map do |c|
    p = c[:idx]
    l = p
    l -= 1 while l.positive? && db[l] >= edge_thr
    r = p
    r += 1 while r < (nfft - 1) && db[r] >= edge_thr
    bw_hz = ([r - l, 1].max * res_hz).to_i
    center = (center_hz + freq_off[p]).to_i
    {
      hz: center,
      freq: PWN::SDR.hz_to_s(freq: center),
      power_db: c[:pwr].round(2),
      noise_floor_db: noise_floor.round(2),
      bw_hz: bw_hz,
      snr_db: (c[:pwr] - noise_floor).round(2),
      peak_bin: p,
      prominence_db: c[:prom].round(2)
    }
  end

  # NOTE: no fallback. A quiet chunk correctly returns signals: [].

  {
    center_freq_hz: center_hz,
    center_freq: PWN::SDR.hz_to_s(freq: center_hz),
    sample_rate: sample_rate,
    visible_span_hz: sample_rate,
    nfft: nfft,
    avg: avg,
    resolution_hz: res_hz.round(2),
    samples: iq.length,
    capture_secs: capture_secs,
    spectrum: bins_out,
    signals: signals,
    noise_floor_db: noise_floor.round(2),
    timestamp: Time.now.strftime('%Y-%m-%d %H:%M:%S%z')
  }
rescue StandardError => e
  begin
    cmd(gqrx_sock: gqrx_sock, cmd: 'U IQRECORD 0', resp_ok: 'RPRT 0')
  rescue StandardError
    nil
  end
  raise e
end

.helpObject

Display Usage for this Module



1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
# File 'lib/pwn/sdr/gqrx.rb', line 1688

public_class_method def self.help
  puts <<~USAGE
    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.)'
      )

      snapshot = #{self}.get_spectrum_snapshot(
        gqrx_sock: 'required - GQRX socket object returned from #connect method',
        center_freq: 'optional - Center frequency (Hz) for snapshot (defaults to current tuned freq)',
        sample_rate: 'optional - Instantaneous bandwidth / sample rate in Hz (defaults to 1_000_000)',
        nfft: 'optional - FFT bin size (defaults to 2048)',
        avg: 'optional - Number of FFT averages (defaults to 8)',
        capture_secs: 'optional - Duration of I/Q capture in seconds (defaults to 0.10)',
        strength_offset_db: 'optional - Add this many dB to all power levels (defaults to 0.0)'
      )

      fast_scan_resp = #{self}.fast_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',
        sample_rate: 'optional - Chunk size / visible span in Hz (defaults to 1_000_000)',
        nfft: 'optional - FFT size (defaults to 2048)',
        avg: 'optional - Number of averages (defaults to 8)',
        capture_secs: 'optional - Seconds of capture per chunk (defaults to 0.10)',
        strength_lock: 'optional - Minimum signal strength in dBFS to report (defaults to -70.0; only meaningful with strength_offset_db calibration)',
        min_snr_db: 'optional - Minimum SNR in dB above per-chunk noise floor to report (defaults to 12.0)',
        precision: 'optional - Band-plan channel raster; detections snapped to 10**(precision-1) Hz grid (defaults to 5)',
        min_bw_ratio: 'optional - Reject FFT peaks narrower than min_bw_ratio * plan bandwidth as spurs (defaults to 0.30)',
        demodulator_mode: 'optional - Demodulator mode to attribute to detections (defaults to WFM)',
        bandwidth: 'optional - Passband bandwidth (defaults to "200.000")',
        squelch: 'optional - Squelch level in dBFS (defaults to strength_lock - 3.0)',
        decoder: 'optional - Decoder key (e.g. :gsm) to attribute to each detection',
        location: 'optional - Location string to include in AI analysis',
        keep_spectrum: 'optional - If true, include full spectrum data in result (can be large, defaults to false)',
        strength_offset_db: 'optional - Add this many dB to all power levels (defaults to 0.0)',
        scan_log: 'optional - Path to save detected signals log (defaults to /tmp/pwn_sdr_gqrx_scan_<start_freq>-<target_freq>_<timestamp>.json)'
      )

      #{self}.analyze_scan(
        scan_resp: 'required - Scan response object from #scan_range or #fast_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
  USAGE
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
# 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(freq: 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
      # Resolve decoder module via central registry (see
      # PWN::SDR::Decoder::REGISTRY) so new protocols only need an
      # autoload + REGISTRY entry — no edit here.
      decoder_module = PWN::SDR::Decoder.resolve(decoder: decoder)

      # Initialize and start decoder (uniform .decode(freq_obj:) API).
      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)' )



1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
# File 'lib/pwn/sdr/gqrx.rb', line 1130

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' )



1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
# File 'lib/pwn/sdr/gqrx.rb', line 1163

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>__lN.json)', location: 'optional - Location string to include in AI analysis (e.g., "New York, NY", 90210, GPS coords, etc.)' )



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

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(freq: start_freq)
    hz_start_str = PWN::SDR.hz_to_s(freq: hz_start)

    target_freq = range[:target_freq]
    hz_target = PWN::SDR.hz_to_i(freq: target_freq)
    hz_target_str = PWN::SDR.hz_to_s(freq: 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(freq: 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(freq: target_freq)
      hz_target_str = PWN::SDR.hz_to_s(freq: 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(freq: 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(freq: 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(freq: start_freq)
      hz = hz_start

      target_freq = range[:target_freq]
      hz_target = PWN::SDR.hz_to_i(freq: 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(freq: 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(freq: 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' )



1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
# File 'lib/pwn/sdr/gqrx.rb', line 1195

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