Files
mayhem-firmware/firmware/application/protocols/ax25.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

161 lines
4.1 KiB
C++

/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2017 Furrtek
*
* 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 "ax25.hpp"
#include "portapack_shared_memory.hpp"
namespace ax25 {
void AX25Frame::make_extended_field(char* const data, size_t length, bool is_last) {
size_t i = 0;
for (i = 0; i < length - 1; i++)
add_data(data[i] << 1);
if (is_last)
add_data((data[i] << 1) | 1);
else
add_data((data[i] << 1));
}
void AX25Frame::NRZI_add_bit(const uint32_t bit) {
if (!bit)
current_bit ^= 1; // Zero: flip
current_byte <<= 1;
current_byte |= current_bit;
bit_counter++;
if (bit_counter == 8) {
bit_counter = 0;
*bb_data_ptr = current_byte;
bb_data_ptr++;
}
}
void AX25Frame::add_byte(uint8_t byte, bool is_flag, bool is_data) {
bool bit;
if (is_data)
crc_ccitt.process_byte(byte);
for (uint32_t i = 0; i < 8; i++) {
bit = (byte >> i) & 1;
NRZI_add_bit(bit);
if (bit) {
ones_counter++;
if ((ones_counter == 5) && (!is_flag)) {
NRZI_add_bit(0);
ones_counter = 0;
}
} else
ones_counter = 0;
}
}
void AX25Frame::flush() {
if (bit_counter)
*bb_data_ptr = current_byte << (8 - bit_counter); // euquiq: This was 7 but there are 8 bits
};
void AX25Frame::add_flag() {
add_byte(AX25_FLAG, true, false);
};
void AX25Frame::add_data(uint8_t byte) {
add_byte(byte, false, true);
};
void AX25Frame::add_checksum() {
auto checksum = crc_ccitt.checksum();
add_byte(checksum, false, false);
add_byte(checksum >> 8, false, false);
}
void AX25Frame::make_ui_frame(char* const address, const uint8_t control, const uint8_t protocol, const std::string& info, const std::string& path) {
size_t i;
bb_data_ptr = (uint16_t*)shared_memory.bb_data.data;
memset(bb_data_ptr, 0, sizeof(shared_memory.bb_data.data));
bit_counter = 0;
current_bit = 0;
current_byte = 0;
ones_counter = 0;
crc_ccitt.reset();
add_flag(); // 0x73
add_flag(); // 0x73
add_flag(); // 0x73
add_flag(); // 0x73
// path must not contain the - character! just the callsign and SSID next to each other
bool has_path = (path.size() > 0 && path.size() <= 63 && path.size() % 7 == 0);
make_extended_field(address, 14, !has_path);
if (has_path) {
make_extended_field(const_cast<char*>(path.data()), path.size(), true);
}
add_data(control); // 0x03
add_data(protocol); // 0xf0
for (i = 0; i < info.size(); i++)
add_data(info[i]);
add_checksum(); // fcs
add_flag(); // 0x73
add_flag(); // 0x73
flush();
}
// __attribute__((used)) prevents dead-stripping; called only from KISS TNC external app.
__attribute__((used)) void AX25Frame::make_frame_from_raw(const uint8_t* data, size_t len) {
bb_data_ptr = (uint16_t*)shared_memory.bb_data.data;
memset(bb_data_ptr, 0, sizeof(shared_memory.bb_data.data));
bit_counter = 0;
current_bit = 0;
current_byte = 0;
ones_counter = 0;
crc_ccitt.reset();
add_flag();
add_flag();
add_flag();
add_flag();
for (size_t i = 0; i < len; i++)
add_data(data[i]);
add_checksum();
add_flag();
add_flag();
flush();
}
} /* namespace ax25 */