Class: PWN::SDR::Decoder::P25::PacketIQ

Inherits:
Object
  • Object
show all
Defined in:
lib/pwn/sdr/decoder/p25.rb

Overview

TIA-102 one-to-three-block TSDU receiver. Protocol tables cross-checked against https://github.com/boatbod/op25/blob/master/op25/gr-op25_repeater/lib/p25p1_fdma.cc C4FM only; no CQPSK, voice, Phase 2 or confirmed 3/4-rate PDUs.

Constant Summary collapse

WORDS =
[[2, 12, 1, 15], [14, 0, 13, 3], [9, 7, 10, 4], [5, 11, 6, 8]].freeze
INTERLEAVE =
((0...12).flat_map { |row| [0, 52, 100, 148].flat_map { |base| Array.new(4) { |bit| base + (row * 4) + bit } } } + [48, 49, 50, 51]).freeze

Instance Method Summary collapse

Constructor Details

#initialize(rate:, mode: :tsbk) ⇒ PacketIQ



112
113
114
115
116
# File 'lib/pwn/sdr/decoder/p25.rb', line 112

def initialize(rate:, mode: :tsbk)
  @mode = mode.to_sym
  @rate = rate.to_f
  @dibits = []
end

Instance Method Details

#feed_dibits(dibits) ⇒ Object

Raises:

  • (ArgumentError)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/pwn/sdr/decoder/p25.rb', line 154

def feed_dibits(dibits, &)
  raise ArgumentError, 'dibits must be integers 0..3' unless dibits.all? { |d| (0..3).cover?(d) }

  @dibits.concat(dibits)
  while @dibits.length >= 57
    unless @dibits.first(24) == FS_SYMS
      @dibits.shift
      next
    end
    # Status symbol after every 35 data dibits, including sync/NID.
    data = @dibits.each_with_index.reject { |_, i| (i % 36) == 35 }.map(&:first)
    nid = data[24, 32].reduce(0) { |v, d| (v << 2) | d }
    info = checked_nid(nid)
    if info && (info & 15) == 12 && i[pdu all].include?(@mode)
      result = PacketData.parse(data: data, info: info, decoder: method(:decode_block))
      break if result == :incomplete

      if result
        packet, count = result
        [packet].each(&)
        @dibits.shift(count + (count / 35))
      else
        @dibits.shift
      end
      next
    end
    unless info && (info & 15) == 7 && i[tsbk all].include?(@mode)
      @dibits.shift
      next
    end
    packets = []
    incomplete = false
    3.times do |index|
      if data.length < 56 + ((index + 1) * 98)
        incomplete = true
        break
      end
      decoded_block = decode_block(data[56 + (index * 98), 98])
      break unless decoded_block

      bytes, errors = decoded_block
      break unless DSP.crc16(bytes: bytes.first(10), init: 0, xorout: 0xFFFF) == ((bytes[10] << 8) | bytes[11])

      protected = bytes[0].anybits?(0x40)
      payload = bytes[2, 8].pack('C*').unpack1('H*').upcase
      fields = {}
      if !protected && bytes[1].zero? && bytes[0].nobits?(63)
        channel = (bytes[3] << 8) | bytes[4]
        fields = { service_options: bytes[2], channel_id: channel >> 12,
                   channel_number: channel & 0xFFF, talkgroup: (bytes[5] << 8) | bytes[6],
                   source_id: (bytes[7] << 16) | (bytes[8] << 8) | bytes[9] }
      end
      packets << { protocol: 'P25', event: 'tsbk', capability: 'tsdu-tsbk',
                   decoded: true, checksum_verified: true, crc_ok: true,
                   nac: format('%03X', info >> 4), duid: 7, duid_name: 'TSBK',
                   opcode: bytes[0] & 63, manufacturer_id: bytes[1], last_block: bytes[0].anybits?(0x80), block_index: index,
                   protected: protected, encrypted: protected, fec_corrected_bits: errors,
                   payload_hex: protected ? nil : payload,
                   ciphertext_hex: protected ? payload : nil,
                   raw_hex: bytes.pack('C*').unpack1('H*').upcase }.merge(fields)
      break if packets.last[:last_block]
    end
    break if incomplete

    if packets.last && packets.last[:last_block]
      packets.each(&)
      count = 56 + (packets.length * 98)
      @dibits.shift(count + (count / 35))
    else
      @dibits.shift
    end
  end
end

#feed_iq(samples, rate: nil) ⇒ Object

Continuous discriminator and transition-aided clock for centered C4FM. No frequency offset acquisition or CQPSK recovery is claimed.

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/pwn/sdr/decoder/p25.rb', line 120

def feed_iq(samples, rate: nil, &)
  @rate = rate.to_f if rate
  raise ArgumentError, 'C4FM requires an integer samples/symbol rate >= 48000' unless @rate >= 48_000 && (@rate % 4800).zero?

  spb = @rate / 4800
  @clock ||= spb / 2
  dibits = []
  samples.each_slice(2) do |re, im|
    next unless im

    if @previous_iq
      pr, pi = @previous_iq
      hz = Math.atan2((im * pr) - (re * pi), (re * pr) + (im * pi)) * @rate / (2 * Math::PI)
      @clock = spb / 2 if @previous_hz && (hz - @previous_hz).abs > 650
      @clock -= 1
      if @clock <= 0
        dibits << (if hz >= 1200
                     1
                   else
                     (if hz >= 0
                        0
                      else
                        (hz > -1200 ? 2 : 3)
                      end)
                   end)
        @clock += spb
      end
      @previous_hz = hz
    end
    @previous_iq = [re, im]
  end
  feed_dibits(dibits, &)
end