Skip to content

Commit bbeb625

Browse files
committed
transmit fix
1 parent d9faf5d commit bbeb625

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎hackrfpy/src/hackrfpy/_commands/transmit.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
# Author(s): <you>
1515
##--------------------------------------------------------------------\
1616

17+
import os
18+
1719
from .. import constants as C
20+
from ..exceptions import HackRFEnvironmentError
1821

1922

2023
class TransmitMixin:
@@ -28,6 +31,12 @@ def transmit(self, freq, sample_rate, source, *, txvga=20, amp=False,
2831
# regulatory + hardware risk if the controlling script dies; this
2932
# gives every transmit an optional dead-man bound.
3033
self.require_mode(C.MODE_TX) # the gate
34+
# Fail fast on a bad path BEFORE spawning hackrf_transfer, so a typo'd
35+
# source raises a clean, catchable error instead of a generic non-zero
36+
# exit from the tool. Skipped for print_cmd (a dry run is a pure command
37+
# preview and may reference a file you haven't generated yet).
38+
if not print_cmd and not os.path.isfile(source):
39+
raise HackRFEnvironmentError(f"transmit source not found: {source!r}")
3140
freq, sample_rate, txvga, amp = self.validate_tx(
3241
freq, sample_rate, txvga, amp)
3342
bw = self._auto_baseband(sample_rate, baseband_bw)

‎hackrfpy/src/hackrfpy/core.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ def _stop_all_live():
9393
from ._commands.device import DeviceMixin
9494

9595

96+
# Upper bound on retained drained output per stream (stdout / stderr). The
97+
# handle-mode drain and the streaming-mode stderr tail both cap here so a
98+
# long-lived RX/TX handle can't grow memory without bound (hackrf_transfer
99+
# prints a stats line every second for the life of the process). We keep the
100+
# most-recent bytes -- enough for an error tail -- and drop older ones.
101+
_DRAIN_CAP = 64 * 1024
102+
103+
96104
class _Process:
97105
# Thin controller returned by _run(mode="handle") for "run until I stop it"
98106
# workflows (the wait-for-user-stop consumption mode). Wraps a Popen and
@@ -124,6 +132,11 @@ def _drain(stream, sink):
124132
try:
125133
for chunk in iter(lambda: stream.read(65536), b""):
126134
sink.append(chunk)
135+
# Bound retained output: drop oldest chunks once over the cap,
136+
# always keeping at least the latest one. Prevents unbounded
137+
# growth on open-ended handles (see _DRAIN_CAP).
138+
while sum(len(c) for c in sink) > _DRAIN_CAP and len(sink) > 1:
139+
sink.pop(0)
127140
except (OSError, ValueError):
128141
pass # pipe closed under us during shutdown
129142

@@ -539,7 +552,7 @@ def _stream(self, resolved, text=False, read_samples=65536):
539552
stderr=subprocess.PIPE, text=text,
540553
creationflags=_CREATION_FLAGS)
541554
err_tail = []
542-
err_cap = 64 * 1024
555+
err_cap = _DRAIN_CAP
543556

544557
def _eat(stream, sink):
545558
try:

‎hackrfpy/src/hackrfpy/sigmf.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ def write_sigmf_meta(data_path, freq, sample_rate, *, lna=None, vga=None,
5252
],
5353
"annotations": [],
5454
}
55+
# SigMF requires any non-core namespace used in the file to be declared in
56+
# core:extensions, or strict validators reject it. We only emit hackrf:*
57+
# keys when gains are supplied, so declare the extension exactly then.
58+
# optional=True: a reader can decode the IQ without understanding hackrf:*.
59+
if annotations_gains:
60+
meta["global"]["core:extensions"] = [
61+
{"name": "hackrf", "version": "1.0.0", "optional": True}
62+
]
5563
if extra:
5664
meta["global"].update(extra)
5765

0 commit comments

Comments
 (0)