From 76ff85e5bbcab7a00fd28e795478876b60e5b5bf Mon Sep 17 00:00:00 2001 From: Bryan Biedenkapp Date: Thu, 13 Oct 2022 21:03:04 -0400 Subject: [PATCH] add missing files (my bad); --- host/Console.cpp | 246 +++++++++++++++++++++++++++++++++++++++++++++ host/Console.h | 70 +++++++++++++ nxdn/NXDNUtils.cpp | 64 ++++++++++++ nxdn/NXDNUtils.h | 50 +++++++++ 4 files changed, 430 insertions(+) create mode 100644 host/Console.cpp create mode 100644 host/Console.h create mode 100644 nxdn/NXDNUtils.cpp create mode 100644 nxdn/NXDNUtils.h diff --git a/host/Console.cpp b/host/Console.cpp new file mode 100644 index 00000000..b33badbe --- /dev/null +++ b/host/Console.cpp @@ -0,0 +1,246 @@ +/** +* Digital Voice Modem - Host Software +* GPLv2 Open Source. Use is subject to license terms. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* @package DVM / Host Software +* +*/ +// +// Based on code from the MMDVMHost project. (https://github.com/g4klx/MMDVMHost) +// Licensed under the GPLv2 License (https://opensource.org/licenses/GPL-2.0) +// +/* +* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX +* +* 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 of the License, 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; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ +#include "host/Console.h" + +#include + +#if defined(_WIN32) || defined(_WIN64) +#include +#else +#include +#include +#include +#include +#endif + +// --------------------------------------------------------------------------- +// Public Class Members +// --------------------------------------------------------------------------- + +#if defined(_WIN32) || defined(_WIN64) +/// +/// Initializes a new instance of the Console class. +/// +Console::Console() +{ + /* stub */ +} + +/// +/// Finalizes a instance of the Console class. +/// +Console::~Console() +{ + /* stub */ +} + +/// +/// Opens the terminal console. +/// +bool Console::open() +{ + return true; +} + +/// +/// Retrieves a character input on the keyboard. +/// +/// +int Console::getChar() +{ + if (::_kbhit() == 0) + return -1; + + return ::_getch(); +} + +/// +/// Closes the terminal console. +/// +void Console::close() +{ + /* stub */ +} +#else +/// +/// Initializes a new instance of the Console class. +/// +Console::Console() : + m_termios() +{ + ::memset(&m_termios, 0x00U, sizeof(termios)); +} + +/// +/// Finalizes a instance of the Console class. +/// +Console::~Console() +{ + /* stub */ +} + +/// +/// Opens the terminal console. +/// +bool Console::open() +{ + termios tios; + + int n = ::tcgetattr(STDIN_FILENO, &tios); + if (n != 0) { + ::fprintf(stderr, "tcgetattr: returned %d\r\n", n); + return -1; + } + + m_termios = tios; + + ::cfmakeraw(&tios); + + n = ::tcsetattr(STDIN_FILENO, TCSANOW, &tios); + if (n != 0) { + ::fprintf(stderr, "tcsetattr: returned %d\r\n", n); + return -1; + } + + return true; +} + +/// +/// Retrieves a character input on the keyboard. +/// +/// +int Console::getChar() +{ + fd_set fds; + FD_ZERO(&fds); + FD_SET(STDIN_FILENO, &fds); + + timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 0; + + int n = ::select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv); + if (n <= 0) { + if (n < 0) + ::fprintf(stderr, "select: returned %d\r\n", n); + return -1; + } + + char c; + n = ::read(STDIN_FILENO, &c, 1); + if (n <= 0) { + if (n < 0) + ::fprintf(stderr, "read: returned %d\r\n", n); + return -1; + } + + return c; +} + +/// +/// Closes the terminal console. +/// +void Console::close() +{ + int n = ::tcsetattr(STDIN_FILENO, TCSANOW, &m_termios); + if (n != 0) + ::fprintf(stderr, "tcsetattr: returned %d\r\n", n); +} +#endif + +/// +/// Retrieves an array of characters input on the keyboard. +/// +/// +/// +/// +/// +int Console::getLine(char line[], int max, char mask) +{ + int nch = 0; + int c; + bool skipNext = false; + max = max - 1; /* leave room for '\0' */ + + while ((c = getChar()) != '\n') { + if (c != -1) { + if (c == 10 || c == 13) + break; + + // skip "double-byte" control characters + if (c == 224) { + skipNext = true; + continue; + } + + if (skipNext) { + skipNext = false; + continue; + } + + // has characters and backspace character? + if (nch > 0 && (c == 127 || c == 8)) { + // handle backspace + ::fputc(0x8, stdout); + ::fputc(' ', stdout); + ::fputc(0x8, stdout); + ::fflush(stdout); + + line[--nch] = 0; + } + else { + // skip control characters + if (iscntrl(c)) + continue; + + if (nch < max) { + // valid mask character? + if (' ' - 1 < mask && mask < 127) + ::fputc(mask, stdout); + else + ::fputc(c, stdout); + ::fflush(stdout); + + line[nch++] = c; + } + } + } + } + + if (c == EOF && nch == 0) + return EOF; + + ::fputc('\r', stdout); + ::fputc('\n', stdout); + ::fflush(stdout); + + line[nch] = '\0'; + return nch; +} diff --git a/host/Console.h b/host/Console.h new file mode 100644 index 00000000..16101d3c --- /dev/null +++ b/host/Console.h @@ -0,0 +1,70 @@ +/** +* Digital Voice Modem - Host Software +* GPLv2 Open Source. Use is subject to license terms. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* @package DVM / Host Software +* +*/ +// +// Based on code from the MMDVMCal project. (https://github.com/g4klx/MMDVMCal) +// Licensed under the GPLv2 License (https://opensource.org/licenses/GPL-2.0) +// +/* +* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX +* +* 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 of the License, 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; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ +#if !defined(__CONSOLE_H__) +#define __CONSOLE_H__ + +#include "Defines.h" + +#if !defined(_WIN32) && !defined(_WIN64) +#include +#endif + +// --------------------------------------------------------------------------- +// Class Declaration +// Implements cross-platform handling of the terminal console. This is +// mainly used for the calibration mode. +// --------------------------------------------------------------------------- + +class HOST_SW_API Console { +public: + /// Initializes a new instance of the Console class. + Console(); + /// Finalizes a instance of the Console class. + ~Console(); + + /// Opens the terminal console. + bool open(); + + /// Retrieves a character input on the keyboard. + int getChar(); + + /// Retrieves an array of characters input on the keyboard. + int getLine(char line[], int max, char mask); + + /// Closes the terminal console. + void close(); + +private: +#if !defined(_WIN32) && !defined(_WIN64) + termios m_termios; +#endif +}; + +#endif // __CONSOLE_H__ diff --git a/nxdn/NXDNUtils.cpp b/nxdn/NXDNUtils.cpp new file mode 100644 index 00000000..9c3a473d --- /dev/null +++ b/nxdn/NXDNUtils.cpp @@ -0,0 +1,64 @@ +/** +* Digital Voice Modem - Host Software +* GPLv2 Open Source. Use is subject to license terms. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* @package DVM / Host Software +* +*/ +// +// Based on code from the MMDVMHost project. (https://github.com/g4klx/MMDVMHost) +// Licensed under the GPLv2 License (https://opensource.org/licenses/GPL-2.0) +// +/* +* Copyright (C) 2020 by Jonathan Naylor G4KLX +* Copyright (C) 2022 by Bryan Biedenkapp N2PLL +* +* 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 of the License, 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; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ +#include "Defines.h" +#include "nxdn/NXDNDefines.h" +#include "nxdn/NXDNUtils.h" + +using namespace nxdn; + +#include +#include + +// --------------------------------------------------------------------------- +// Constants +// --------------------------------------------------------------------------- + +const uint8_t SCRAMBLER[] = { + 0x00U, 0x00U, 0x00U, 0x82U, 0xA0U, 0x88U, 0x8AU, 0x00U, 0xA2U, 0xA8U, 0x82U, 0x8AU, 0x82U, 0x02U, + 0x20U, 0x08U, 0x8AU, 0x20U, 0xAAU, 0xA2U, 0x82U, 0x08U, 0x22U, 0x8AU, 0xAAU, 0x08U, 0x28U, 0x88U, + 0x28U, 0x28U, 0x00U, 0x0AU, 0x02U, 0x82U, 0x20U, 0x28U, 0x82U, 0x2AU, 0xAAU, 0x20U, 0x22U, 0x80U, + 0xA8U, 0x8AU, 0x08U, 0xA0U, 0xAAU, 0x02U }; + +// --------------------------------------------------------------------------- +// Static Class Members +// --------------------------------------------------------------------------- + +/// +/// Helper to scramble the NXDN frame data. +/// +/// +void NXDNUtils::scrambler(uint8_t* data) +{ + assert(data != NULL); + + for (uint32_t i = 0U; i < NXDN_FRAME_LENGTH_BYTES; i++) + data[i] ^= SCRAMBLER[i]; +} diff --git a/nxdn/NXDNUtils.h b/nxdn/NXDNUtils.h new file mode 100644 index 00000000..b139910b --- /dev/null +++ b/nxdn/NXDNUtils.h @@ -0,0 +1,50 @@ +/** +* Digital Voice Modem - Host Software +* GPLv2 Open Source. Use is subject to license terms. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* @package DVM / Host Software +* +*/ +// +// Based on code from the MMDVMHost project. (https://github.com/g4klx/MMDVMHost) +// Licensed under the GPLv2 License (https://opensource.org/licenses/GPL-2.0) +// +/* +* Copyright (C) 2020 by Jonathan Naylor G4KLX +* Copyright (C) 2022 by Bryan Biedenkapp N2PLL +* +* 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 of the License, 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; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ +#if !defined(__NXDN_UTILS_H__) +#define __NXDN_UTILS_H__ + +#include "Defines.h" + +namespace nxdn +{ + // --------------------------------------------------------------------------- + // Class Declaration + // This class implements various helper functions for scrambling NXDN data. + // --------------------------------------------------------------------------- + + class HOST_SW_API NXDNUtils { + public: + /// Helper to scramble the NXDN frame data. + static void scrambler(uint8_t* data); + }; +} // namespace nxdn + +#endif // __NXDN_UTILS_H__