Module: PWN::FFI::HackRF
- Extended by:
- Library
- Defined in:
- lib/pwn/ffi/hack_rf.rb
Overview
Thin libhackrf binding for inventory / RX of raw I/Q.
Intentionally control-plane first: init/open/tune/rate/gains + one-shot
sync-style helpers used by Extrospection probe_rf and by wideband
PWN::SDR::Decoder::* modules that need real I/Q (not GQRX audio).
Callbacks copy into a bounded nonblocking queue. Ruby callbacks still
acquire the GVL and allocate: this is not a hard-realtime guarantee.
Defined Under Namespace
Classes: Transfer
Constant Summary collapse
- HACKRF_SUCCESS =
0
Class Attribute Summary collapse
-
.load_error ⇒ Object
readonly
Returns the value of attribute load_error.
Class Method Summary collapse
-
.authors ⇒ Object
- Author(s)
0day Inc.
-
.available? ⇒ Boolean
- Supported Method Parameters
PWN::FFI::HackRF.available?.
-
.capture(opts = {}) ⇒ Object
- Supported Method Parameters
data = PWN::FFI::HackRF.capture( freq_hz: 'required - center frequency Hz', rate_hz: 'optional - sample rate (default 10e6)', samples: 'optional - I/Q sample pairs to capture (default 262_144)', lna_gain: 'optional', vga_gain: 'optional', amp: 'optional', serial: 'optional' ) One-shot open/configure/start_rx/read/stop_rx/close.
-
.close(opts = {}) ⇒ Object
- Supported Method Parameters
PWN::FFI::HackRF.close(device: pointer).
-
.configure(opts = {}) ⇒ Object
rubocop:disable Naming/PredicateMethod.
-
.device_info(opts = {}) ⇒ Object
- Supported Method Parameters
meta = PWN::FFI::HackRF.device_info(device: pointer) Returns { board_id:, board_name:, version: }.
-
.help ⇒ Object
Display Usage for this Module.
-
.info ⇒ Object
- Supported Method Parameters
info = PWN::FFI::HackRF.info Returns Hash with library_version / library_release / available.
-
.open(opts = {}) ⇒ Object
- Supported Method Parameters
dev = PWN::FFI::HackRF.open(serial: nil) Returns FFI::Pointer (opaque hackrf_device*) or raises.
-
.read_sync(opts = {}) ⇒ Object
- Supported Method Parameters
data = PWN::FFI::HackRF.read_sync( handle: 'required - handle from .start_rx', timeout: 'optional - seconds to wait for a chunk (default 1.0)' ) Returns String of interleaved cs8 I/Q, or nil on timeout/stopped RX.
-
.start_rx(opts = {}) ⇒ Object
- Supported Method Parameters
rx = PWN::FFI::HackRF.start_rx( device: 'required - pointer from .open', max_queue: 'optional - max buffered chunks (default 64)' ) Returns { device:, queue:, callback:, overruns:, dropped_bytes: }.
-
.stop_rx(opts = {}) ⇒ Object
- Supported Method Parameters
PWN::FFI::HackRF.stop_rx(handle: rx).
Class Attribute Details
.load_error ⇒ Object (readonly)
Returns the value of attribute load_error.
29 30 31 |
# File 'lib/pwn/ffi/hack_rf.rb', line 29 def load_error @load_error end |
Class Method Details
.authors ⇒ Object
- Author(s)
0day Inc. [email protected]
301 302 303 |
# File 'lib/pwn/ffi/hack_rf.rb', line 301 public_class_method def self. "AUTHOR(S):\n 0day Inc. <[email protected]>\n" end |
.available? ⇒ Boolean
- Supported Method Parameters
PWN::FFI::HackRF.available?
62 63 64 65 66 |
# File 'lib/pwn/ffi/hack_rf.rb', line 62 public_class_method def self.available? !@load_error && respond_to?(:hackrf_init, true) rescue StandardError false end |
.capture(opts = {}) ⇒ Object
- Supported Method Parameters
data = PWN::FFI::HackRF.capture( freq_hz: 'required - center frequency Hz', rate_hz: 'optional - sample rate (default 10e6)', samples: 'optional - I/Q sample pairs to capture (default 262_144)', lna_gain: 'optional', vga_gain: 'optional', amp: 'optional', serial: 'optional' ) One-shot open/configure/start_rx/read/stop_rx/close. Returns String of interleaved cs8 I/Q.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/pwn/ffi/hack_rf.rb', line 276 public_class_method def self.capture(opts = {}) want = (opts[:samples] || 262_144).to_i * 2 dev = self.open(serial: opts[:serial]) configure( device: dev, freq_hz: opts[:freq_hz], rate_hz: opts[:rate_hz] || 10_000_000, lna_gain: opts[:lna_gain], vga_gain: opts[:vga_gain], amp: opts[:amp] ) rx = start_rx(device: dev) buf = +'' while buf.bytesize < want chunk = read_sync(handle: rx, timeout: 2.0) break unless chunk buf << chunk end buf[0, want] ensure stop_rx(handle: rx) if defined?(rx) && rx close(device: dev) if defined?(dev) && dev end |
.close(opts = {}) ⇒ Object
- Supported Method Parameters
PWN::FFI::HackRF.close(device: pointer)
104 105 106 107 108 109 110 111 |
# File 'lib/pwn/ffi/hack_rf.rb', line 104 public_class_method def self.close(opts = {}) dev = opts[:device] return unless dev && !dev.null? check!(hackrf_close(dev)) hackrf_exit nil end |
.configure(opts = {}) ⇒ Object
rubocop:disable Naming/PredicateMethod
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/pwn/ffi/hack_rf.rb', line 124 public_class_method def self.configure(opts = {}) # rubocop:disable Naming/PredicateMethod dev = opts[:device] raise 'ERROR: :device required' if dev.nil? || dev.null? freq = opts[:freq_hz].to_i rate = (opts[:rate_hz] || 10_000_000).to_f lna = (opts[:lna_gain] || 16).to_i vga = (opts[:vga_gain] || 20).to_i amp = opts[:amp] ? 1 : 0 check!(hackrf_set_freq(dev, freq)) check!(hackrf_set_sample_rate(dev, rate)) check!(hackrf_set_lna_gain(dev, lna)) check!(hackrf_set_vga_gain(dev, vga)) check!(hackrf_set_amp_enable(dev, amp)) check!(hackrf_set_baseband_filter_bandwidth(dev, opts[:bb_bw_hz].to_i)) if opts[:bb_bw_hz] true end |
.device_info(opts = {}) ⇒ Object
- Supported Method Parameters
meta = PWN::FFI::HackRF.device_info(device: pointer) Returns { board_id:, board_name:, version: }
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/pwn/ffi/hack_rf.rb', line 147 public_class_method def self.device_info(opts = {}) dev = opts[:device] raise 'ERROR: :device required' if dev.nil? || dev.null? bid_ptr = PubFFI::MemoryPointer.new(:uint8) check!(hackrf_board_id_read(dev, bid_ptr)) bid = bid_ptr.read_uint8 ver_buf = PubFFI::MemoryPointer.new(:char, 255) check!(hackrf_version_string_read(dev, ver_buf, 255)) { board_id: bid, board_name: hackrf_board_id_name(bid).to_s, version: ver_buf.read_string } end |
.help ⇒ Object
Display Usage for this Module
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/pwn/ffi/hack_rf.rb', line 307 public_class_method def self.help puts "USAGE: # Run available and return its result #{self}.available? # Run info and return its result #{self}.info # Open a session or connection and return a handle. #{self}.open( serial: 'optional - serial value consumed by #open' ) # Close a session previously returned by #open. #{self}.close( device: 'optional - device value consumed by #close' ) # Run configure and return its result #{self}.configure( device: 'required - pointer from .open', freq_hz: 'required - center frequency Hz', rate_hz: 'optional - sample rate (default 10e6)', lna_gain: 'optional - 0..40 step 8 (default 16)', vga_gain: 'optional - 0..62 step 2 (default 20)', amp: 'optional - true/false RF amp (default false)', bb_bw_hz: 'optional - baseband filter BW Hz' ) # Run device info and return its result #{self}.device_info( device: 'required - device value consumed by #device_info' ) # Run start rx and return its result #{self}.start_rx( device: 'required - pointer from .open', max_queue: 'optional - max buffered chunks (default 64)' ) # Run read sync and return its result #{self}.read_sync( handle: 'required - handle from .start_rx', timeout: 'optional - seconds to wait for a chunk (default 1.0)' ) # Run stop rx and return its result #{self}.stop_rx( handle: 'optional - handle value consumed by #stop_rx' ) # Run capture and return its result #{self}.capture( freq_hz: 'required - center frequency Hz', rate_hz: 'optional - sample rate (default 10e6)', samples: 'optional - I/Q sample pairs to capture (default 262_144)', lna_gain: 'optional - optional, vga_gain: optional, amp: optional', serial: 'optional - serial value consumed by #capture', vga_gain: 'optional - vga gain value consumed by #capture', amp: 'optional - amp value consumed by #capture' ) # Print the AUTHOR(S) string for this module. #{self}.authors " constants.sort end |
.info ⇒ Object
- Supported Method Parameters
info = PWN::FFI::HackRF.info Returns Hash with library_version / library_release / available.
72 73 74 75 76 77 78 79 80 |
# File 'lib/pwn/ffi/hack_rf.rb', line 72 public_class_method def self.info return { available: false, error: @load_error&. } unless available? { available: true, library_version: hackrf_library_version.to_s, library_release: hackrf_library_release.to_s } end |
.open(opts = {}) ⇒ Object
- Supported Method Parameters
dev = PWN::FFI::HackRF.open(serial: nil) Returns FFI::Pointer (opaque hackrf_device*) or raises.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/pwn/ffi/hack_rf.rb', line 86 public_class_method def self.open(opts = {}) raise 'ERROR: libhackrf not available' unless available? check!(hackrf_init) dev_ptr = PubFFI::MemoryPointer.new(:pointer) serial = opts[:serial] rc = if serial hackrf_open_by_serial(serial.to_s, dev_ptr) else hackrf_open(dev_ptr) end check!(rc) dev_ptr.read_pointer end |
.read_sync(opts = {}) ⇒ Object
- Supported Method Parameters
data = PWN::FFI::HackRF.read_sync( handle: 'required - handle from .start_rx', timeout: 'optional - seconds to wait for a chunk (default 1.0)' ) Returns String of interleaved cs8 I/Q, or nil on timeout/stopped RX. Raises on callback errors or queue overrun; never bridges lost IQ.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/pwn/ffi/hack_rf.rb', line 227 public_class_method def self.read_sync(opts = {}) h = opts[:handle] raise 'ERROR: :handle required (from start_rx)' unless h.is_a?(Hash) && h[:queue] raise h[:error] if h[:error] q = h[:queue] to = (opts[:timeout] || 1.0).to_f deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + to loop do raise h[:error] if h[:error] return nil if h[:stopped] begin return q.pop(true) rescue ThreadError return nil if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline end sleep 0.005 end end |
.start_rx(opts = {}) ⇒ Object
- Supported Method Parameters
rx = PWN::FFI::HackRF.start_rx( device: 'required - pointer from .open', max_queue: 'optional - max buffered chunks (default 64)' ) Returns { device:, queue:, callback:, overruns:, dropped_bytes: }. Counters cover application queue drops only, not unreported USB/RF loss. An overrun/error latches :error; read_sync raises until RX is restarted. libhackrf runs the callback on its own libusb thread; FFI acquires the GVL for us, so keep the callback body to a bare byte copy.
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 |
# File 'lib/pwn/ffi/hack_rf.rb', line 183 public_class_method def self.start_rx(opts = {}) raise 'ERROR: libhackrf not available' unless available? dev = opts[:device] raise 'ERROR: :device required' if dev.nil? || dev.null? max_q = (opts[:max_queue] || 64).to_i raise ArgumentError, 'max_queue must be positive' unless max_q.positive? queue = SizedQueue.new(max_q) h = { device: dev, queue: queue, dropped_bytes: 0, overruns: 0 } cb = PubFFI::Function.new(:int, [:pointer]) do |xfer_ptr| begin xfer = Transfer.new(xfer_ptr) len = xfer[:valid_length].to_i raise 'ERROR: invalid HackRF transfer length' if len.negative? || len > xfer[:buffer_length] || len.odd? if len.positive? begin queue.push(xfer[:buffer].read_bytes(len), true) rescue ThreadError h[:dropped_bytes] += len h[:overruns] += 1 h[:error] = RuntimeError.new('ERROR: HackRF RX overrun: IQ continuity lost') end end rescue StandardError => e h[:error] = e end 0 end h[:callback] = cb check!(hackrf_start_rx(dev, cb, nil)) h end |
.stop_rx(opts = {}) ⇒ Object
- Supported Method Parameters
PWN::FFI::HackRF.stop_rx(handle: rx)
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/pwn/ffi/hack_rf.rb', line 252 public_class_method def self.stop_rx(opts = {}) h = opts[:handle] return unless h.is_a?(Hash) && h[:device] return if h[:stopped] check!(hackrf_stop_rx(h[:device])) h[:stopped] = true h[:queue]&.clear h[:callback] = nil nil end |