Files
mayhem-firmware/firmware/application/usb_serial_host_to_device.cpp
T
Sarah Rose bf5ea0a004 Add KISS TNC external app for APRS RX/TX over USB serial (#3078)
* Add KISS TNC external app for APRS RX/TX over USB serial

Adds a KISS TNC (Terminal Node Controller) app that bridges APRS
packets between the HackRF radio and a connected PC over USB CDC
serial using the KISS protocol.

Features:
- APRS receive at 144.390 MHz (configurable), decoding packets via
  the existing APRS RX baseband (PAPR.bin)
- Received AX.25 frames forwarded to host as KISS-framed data over
  USB serial
- KISS frames received from host transmitted as AFSK audio using the
  existing AFSK TX baseband (PAFT.bin)
- Clean RX->TX->RX baseband switching with proper timing delays
- USB connection status indicator with periodic refresh
- Compatible with standard KISS TNC clients (Xastir, APRSISCE/32,
  Dire Wolf, etc.)

Firmware changes required to support the external app:
- baseband_api.cpp: mark set_aprs() __attribute__((used)) so it is
  retained by the linker for external app use
- protocols/ax25: add make_frame_from_raw() to build NRZI bitstream
  from raw AX.25 bytes, marked __attribute__((used))
- usb_serial_host_to_device: add kiss_raw_handler hook to route
  incoming USB bytes directly to a registered callback, bypassing
  the shell; marked __attribute__((used))
- baseband/proc_aprsrx: add constructor that auto-configures at
  1200 baud so external apps do not need to call set_aprs() at init

* Address PR review: generic RAII USB handler, fix TX cutoff and buffer overflow

- Replace KISS-specific set_kiss_raw_handler() in shared USB code with a
  generic UsbSerialInputHandler RAII class; any app can now register a
  handler via the ctor and it auto-clears in the dtor — no app-specific
  'if' checks in shared code, no __attribute__((used)) needed
- Remove __attribute__((used)) from set_aprs() — it is referenced directly
  by ui_aprs_rx.cpp so it will never be dead-stripped
- Fix send_kiss_frame() buffer overflow: per-byte bounds check prevents
  2-byte escape sequences from writing past the end of the output buffer
- Replace fillOBuffer(TIME_INFINITE) with chOQWriteTimeout(TIME_IMMEDIATE)
  to drop bytes instead of blocking the event loop
- Remove chThdSleepMilliseconds() calls from start_tx() and finish_tx()
- Fix TX cutoff bug: call start_tx() directly from process_kiss_frame()
  instead of deferring via tx_pending_ flag (deferral caused kiss_idx_
  to be 0 by the time start_tx() fired, producing an empty frame)

* Move USB write into UsbSerialInputHandler class

Add a write() method to UsbSerialInputHandler so all USB I/O
(both input callback and output) is encapsulated in the class.
KISS TNC now uses usb_input_handler_->write() instead of calling
chOQWriteTimeout(&SUSBD1.oqueue, ...) directly, keeping raw USB
details out of app code.

* Address PR review: remove auto-configure from APRSRxProcessor, add timeout param to write()

- Remove default_config from APRSRxProcessor() constructor; baseband should
  start unconfigured and wait for APRSRxConfigureMessage from the app side
  (already sent via baseband::set_aprs() in KISS TNC and APRS RX app)
- Remove stray #include "stdio.h" from proc_aprsrx.cpp
- Add systime_t timeout parameter with default TIME_IMMEDIATE to
  UsbSerialInputHandler::write() so callers can choose blocking behaviour
2026-03-10 10:40:27 +01:00

139 lines
4.1 KiB
C++

/*
* Copyright (C) 2023 Bernd Herzog
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "usb_serial_host_to_device.hpp"
#include "event_m0.hpp"
#include "usb_serial_device_to_host.h"
extern "C" {
#include <common/usb.h>
#include <hackrf_usb/usb_device.h>
#include <hackrf_usb/usb_endpoint.h>
}
#include <queue>
#include <vector>
static Thread* thread_usb_event = NULL;
usb_serial_input_handler_t usb_serial_active_input_handler = nullptr;
struct usb_bulk_buffer_t {
uint8_t* data;
size_t length;
bool completed;
};
std::queue<usb_bulk_buffer_t*> usb_bulk_buffer_queue;
std::queue<usb_bulk_buffer_t*> usb_bulk_buffer_spare;
void serial_bulk_transfer_complete(void* user_data, unsigned int bytes_transferred) {
usb_bulk_buffer_t* transfer_data = (usb_bulk_buffer_t*)user_data;
transfer_data->length = bytes_transferred;
transfer_data->completed = true;
}
void init_host_to_device() {
thread_usb_event = chThdSelf();
}
void reset_transfer_queues() {
while (usb_bulk_buffer_queue.empty() == false)
usb_bulk_buffer_queue.pop();
while (usb_bulk_buffer_spare.empty() == false)
usb_bulk_buffer_spare.pop();
}
void schedule_host_to_device_transfer() {
if (usb_bulk_buffer_queue.size() >= 8)
return;
static usb_bulk_buffer_t* transfer_data = nullptr;
int ret;
do {
if (transfer_data == nullptr) {
if (usb_bulk_buffer_spare.empty() == false) {
transfer_data = usb_bulk_buffer_spare.front();
transfer_data->length = 0;
transfer_data->completed = false;
usb_bulk_buffer_spare.pop();
} else {
transfer_data = new usb_bulk_buffer_t{
.data = new uint8_t[USB_BULK_BUFFER_SIZE],
.length = 0,
.completed = false};
}
}
ret = usb_transfer_schedule(
&usb_endpoint_bulk_out,
transfer_data->data,
USB_BULK_BUFFER_SIZE,
serial_bulk_transfer_complete,
transfer_data);
if (ret != -1) {
usb_bulk_buffer_queue.push(transfer_data);
transfer_data = nullptr;
if (usb_bulk_buffer_queue.size() >= 8)
return;
}
} while (ret != -1);
}
void complete_host_to_device_transfer() {
for (; !usb_bulk_buffer_queue.empty(); usb_bulk_buffer_queue.pop()) {
usb_bulk_buffer_t* transfer_data = usb_bulk_buffer_queue.front();
while (transfer_data->completed == false)
return;
chSysLock();
if (usb_serial_active_input_handler) {
// An input handler is active: route raw bytes directly to it
chSysUnlock();
usb_serial_active_input_handler(transfer_data->data, transfer_data->length);
} else {
// Normal operation: feed bytes into the shell iqueue
for (unsigned int i = 0; i < transfer_data->length; i++) {
msg_t ret;
do {
ret = chIQPutI(&SUSBD1.iqueue, transfer_data->data[i]);
if (ret == Q_FULL) {
chSysUnlock();
chThdSleepMilliseconds(1); // wait for shell thread when buffer is full
chSysLock();
}
} while (ret == Q_FULL);
}
chSysUnlock();
}
usb_bulk_buffer_spare.push(transfer_data);
}
}