vp-digi/Core/Src/common.c

255 wiersze
5.4 KiB
C
Czysty Zwykły widok Historia

2021-09-10 09:30:51 +00:00
/*
Copyright 2020-2023 Piotr Wilkon
2021-09-10 09:30:51 +00:00
This file is part of VP-Digi.
VP-Digi 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 3 of the License, or
(at your option) any later version.
VP-Digi 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 VP-Digi. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <stdlib.h>
2023-09-08 08:51:54 +00:00
#include "common.h"
#include "ax25.h"
2022-08-29 11:00:28 +00:00
#include "usbd_cdc_if.h"
2021-09-10 09:30:51 +00:00
2023-08-17 12:57:29 +00:00
struct _GeneralConfig GeneralConfig =
{
.call = {'N' << 1, '0' << 1, 'C' << 1, 'A' << 1, 'L' << 1, 'L' << 1},
.callSsid = 0,
.dest = {130, 160, 156, 172, 96, 98, 96}, //destination address: APNV01-0 by default. SSID MUST remain 0.
.kissMonitor = 0,
};
2021-09-10 09:30:51 +00:00
2023-08-30 14:25:37 +00:00
const char versionString[] = "VP-Digi v. 2.0.0\r\nThe open-source standalone APRS digipeater controller and KISS TNC\r\n"
#ifdef ENABLE_FX25
2023-08-30 14:52:27 +00:00
"With FX.25 support compiled-in\r\n"
2023-08-30 14:25:37 +00:00
#endif
;
2021-09-10 09:30:51 +00:00
2023-08-17 12:57:29 +00:00
static uint64_t pow10i(uint16_t exp)
{
if(exp == 0)
return 1;
uint64_t n = 1;
while(exp--)
n *= 10;
return n;
}
2021-09-10 09:30:51 +00:00
2023-08-17 12:57:29 +00:00
int64_t StrToInt(const char *str, uint16_t len)
2021-09-10 09:30:51 +00:00
{
if(len == 0)
2023-08-17 12:57:29 +00:00
len = strlen(str);
int64_t tmp = 0;
for(int32_t i = (len - 1); i >= 0; i--)
2021-09-10 09:30:51 +00:00
{
2023-08-17 12:57:29 +00:00
if((i == 0) && (str[0] == '-'))
2021-09-10 09:30:51 +00:00
{
2023-08-17 12:57:29 +00:00
return -tmp;
2021-09-10 09:30:51 +00:00
}
2023-08-17 12:57:29 +00:00
else if(IS_NUMBER(str[i]))
tmp += ((str[i] - '0') * pow10i(len - 1 - i));
2021-09-10 09:30:51 +00:00
else
2023-08-17 12:57:29 +00:00
return 0;
2021-09-10 09:30:51 +00:00
}
return tmp;
}
2023-08-17 12:57:29 +00:00
int16_t Random(int16_t min, int16_t max)
2021-09-10 09:30:51 +00:00
{
int16_t tmp;
2023-08-17 12:57:29 +00:00
if (max >= min)
max -= min;
2021-09-10 09:30:51 +00:00
else
{
2023-08-17 12:57:29 +00:00
tmp = min - max;
min = max;
max = tmp;
2021-09-10 09:30:51 +00:00
}
return max ? (rand() % max + min) : min;
}
2023-08-17 12:57:29 +00:00
static void sendTNC2ToUart(Uart *uart, uint8_t *from, uint16_t len)
2021-09-10 09:30:51 +00:00
{
for(uint8_t i = 0; i < 6; i++) //source call
{
2023-08-17 12:57:29 +00:00
if((from[7 + i] >> 1) != ' ') //skip spaces
2021-09-10 09:30:51 +00:00
{
2023-08-17 12:57:29 +00:00
UartSendByte(uart, from[7 + i] >> 1);
2021-09-10 09:30:51 +00:00
}
}
2023-08-17 12:57:29 +00:00
uint8_t ssid = ((from[13] >> 1) & 0b00001111); //store ssid
2021-09-10 09:30:51 +00:00
if(ssid > 0) //SSID >0
{
2023-08-17 12:57:29 +00:00
UartSendByte(uart, '-'); //add -
UartSendNumber(uart, ssid);
2021-09-10 09:30:51 +00:00
}
2023-08-17 12:57:29 +00:00
UartSendByte(uart, '>'); //first separator
2021-09-10 09:30:51 +00:00
for(uint8_t i = 0; i < 6; i++) //destination call
{
2023-08-17 12:57:29 +00:00
if((from[i] >> 1) != ' ') //skip spaces
2021-09-10 09:30:51 +00:00
{
2023-08-17 12:57:29 +00:00
UartSendByte(uart, from[i] >> 1);
2021-09-10 09:30:51 +00:00
}
}
2023-08-17 12:57:29 +00:00
ssid = ((from[6] >> 1) & 0b00001111); //store ssid
2021-09-10 09:30:51 +00:00
if(ssid > 0) //SSID >0
{
2023-08-17 12:57:29 +00:00
UartSendByte(uart, '-'); //add -
UartSendNumber(uart, ssid);
2021-09-10 09:30:51 +00:00
}
uint16_t nextPathEl = 14; //next path element index
2023-08-17 12:57:29 +00:00
if(!(from[13] & 1)) //no c-bit in source address, there is a digi path
2021-09-10 09:30:51 +00:00
{
2023-08-17 12:57:29 +00:00
do //analyze all path elements
2021-09-10 09:30:51 +00:00
{
2023-08-17 12:57:29 +00:00
UartSendByte(uart, ','); //path separator
2021-09-10 09:30:51 +00:00
for(uint8_t i = 0; i < 6; i++) //copy element
{
2023-08-17 12:57:29 +00:00
if((from[nextPathEl + i] >> 1) != ' ') //skip spaces
2021-09-10 09:30:51 +00:00
{
2023-08-17 12:57:29 +00:00
UartSendByte(uart, from[nextPathEl + i] >> 1);
2021-09-10 09:30:51 +00:00
}
}
2023-08-17 12:57:29 +00:00
ssid = ((from[nextPathEl + 6] >> 1) & 0b00001111); //store ssid
2021-09-10 09:30:51 +00:00
if(ssid > 0) //SSID >0
{
2023-08-17 12:57:29 +00:00
UartSendByte(uart, '-'); //add -
UartSendNumber(uart, ssid);
2021-09-10 09:30:51 +00:00
}
2023-08-17 12:57:29 +00:00
if((from[nextPathEl + 6] & 0x80)) //h-bit in ssid
UartSendByte(uart, '*'); //add *
2021-09-10 09:30:51 +00:00
nextPathEl += 7; //next path element
if(nextPathEl > 56) //too many path elements
break;
}
2023-08-17 12:57:29 +00:00
while((from[nextPathEl - 1] & 1) == 0); //loop until the c-bit is found
2021-09-10 09:30:51 +00:00
}
2023-09-05 05:38:01 +00:00
UartSendByte(uart, ':'); //separator
2021-09-10 09:30:51 +00:00
2023-09-05 05:38:01 +00:00
if((from[nextPathEl] & 0b11101111) == 0b00000011) //check if UI packet
{
nextPathEl += 2; //skip Control and PID
2021-09-10 09:30:51 +00:00
2023-09-05 05:38:01 +00:00
UartSendString(uart, &(from[nextPathEl]), len - nextPathEl); //send information field
}
else
UartSendString(uart, "<not UI packet>", 0);
2023-08-17 12:57:29 +00:00
}
2021-09-10 09:30:51 +00:00
2023-08-17 12:57:29 +00:00
void SendTNC2(uint8_t *from, uint16_t len)
{
if(UartUsb.mode == MODE_MONITOR)
sendTNC2ToUart(&UartUsb, from, len);
if(Uart1.mode == MODE_MONITOR)
sendTNC2ToUart(&Uart1, from, len);
if(Uart2.mode == MODE_MONITOR)
sendTNC2ToUart(&Uart2, from, len);
2021-09-10 09:30:51 +00:00
}
2023-08-17 12:57:29 +00:00
uint32_t Crc32(uint32_t crc0, uint8_t *s, uint64_t n)
2021-09-10 09:30:51 +00:00
{
uint32_t crc = ~crc0;
for(uint64_t i = 0; i < n; i++)
{
uint8_t ch = s[i];
for(uint8_t j = 0; j < 8; j++) {
uint32_t b = (ch ^ crc) & 1;
crc >>= 1;
if(b) crc ^= 0xEDB88320;
ch >>= 1;
}
}
return ~crc;
}
2022-08-29 11:00:28 +00:00
2023-08-17 12:57:29 +00:00
bool ParseCallsign(const char *in, uint16_t size, uint8_t *out)
2022-08-29 11:00:28 +00:00
{
2023-08-17 12:57:29 +00:00
if(size > 6)
return false;
2022-08-29 11:00:28 +00:00
2023-08-17 12:57:29 +00:00
uint8_t tmp[6];
uint8_t i = 0;
for(; i < size; i++)
{
if(!IS_UPPERCASE_ALPHANUMERIC(in[i]))
return false;
tmp[i] = in[i] << 1;
}
for(uint8_t k = 0; k < i; k++)
out[k] = tmp[k];
for(; i < 6; i++)
out[i] = ' ' << 1;
return true;
}
bool ParseCallsignWithSsid(const char *in, uint16_t size, uint8_t *out, uint8_t *ssid)
{
uint16_t ssidPosition = size;
for(uint16_t i = 0; i < size; i++)
2022-08-29 11:00:28 +00:00
{
2023-08-17 12:57:29 +00:00
if(in[i] == '-')
2022-08-29 11:00:28 +00:00
{
2023-08-17 12:57:29 +00:00
ssidPosition = i;
break;
2022-08-29 11:00:28 +00:00
}
}
2023-08-17 12:57:29 +00:00
ssidPosition++;
if(!ParseCallsign(in, ssidPosition - 1, out))
return false;
2022-08-29 11:00:28 +00:00
2023-08-17 12:57:29 +00:00
if(ssidPosition == size)
2022-08-29 11:00:28 +00:00
{
2023-08-17 12:57:29 +00:00
*ssid = 0;
return true;
}
2022-08-29 11:00:28 +00:00
2023-08-17 12:57:29 +00:00
if(!ParseSsid(&in[ssidPosition], size - ssidPosition, ssid))
return false;
return true;
}
2022-08-29 11:00:28 +00:00
2023-08-17 12:57:29 +00:00
bool ParseSsid(const char *in, uint16_t size, uint8_t *out)
{
int64_t ssid = StrToInt(in, size);
if((ssid >= 0) && (ssid <= 15))
{
*out = (uint8_t)ssid;
return true;
2022-08-29 11:00:28 +00:00
}
2023-08-17 12:57:29 +00:00
return false;
2022-08-29 11:00:28 +00:00
}
2023-08-17 12:57:29 +00:00