[AX.25] Use common CRC implementation

This commit is contained in:
jgromes 2023-05-12 20:57:15 +02:00
parent 32a5166ae3
commit efd449875d
2 changed files with 4 additions and 33 deletions

View file

@ -306,8 +306,9 @@ int16_t AX25Client::sendFrame(AX25Frame* frame) {
frameBuff[i] = Module::reflect(frameBuff[i], 8);
}
// calculate FCS
uint16_t fcs = getFrameCheckSequence(frameBuff, frameBuffLen);
// calculate
RadioLibCRC crc(16, RADIOLIB_CRC_CCITT_POLY, RADIOLIB_CRC_CCITT_INIT, RADIOLIB_CRC_CCITT_OUT, false, false);
uint16_t fcs = crc.checksum(frameBuff, frameBuffLen);
*(frameBuffPtr++) = (uint8_t)((fcs >> 8) & 0xFF);
*(frameBuffPtr++) = (uint8_t)(fcs & 0xFF);
@ -436,27 +437,4 @@ uint8_t AX25Client::getSSID() {
return(sourceSSID);
}
/*
CCITT CRC implementation based on https://github.com/kicksat/ax25
Licensed under Creative Commons Attribution-ShareAlike 4.0 International
https://creativecommons.org/licenses/by-sa/4.0/
*/
uint16_t AX25Client::getFrameCheckSequence(uint8_t* buff, size_t len) {
uint8_t outBit;
uint16_t mask;
uint16_t shiftReg = CRC_CCITT_INIT;
for(size_t i = 0; i < len; i++) {
for(uint8_t b = 0x80; b > 0x00; b /= 2) {
outBit = (shiftReg & 0x01) ? 0x01 : 0x00;
shiftReg >>= 1;
mask = XOR((buff[i] & b), outBit) ? CRC_CCITT_POLY_REVERSED : 0x0000;
shiftReg ^= mask;
}
}
return(Module::flipBits16(~shiftReg));
}
#endif

View file

@ -8,6 +8,7 @@
#include "../PhysicalLayer/PhysicalLayer.h"
#include "../AFSK/AFSK.h"
#include "../BellModem/BellModem.h"
#include "../../utils/CRC.h"
// macros to access bits in byte array, from http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/bit-array.html
#define SET_BIT_IN_ARRAY(A, k) ( A[(k/8)] |= (1 << (k%8)) )
@ -15,12 +16,6 @@
#define TEST_BIT_IN_ARRAY(A, k) ( A[(k/8)] & (1 << (k%8)) )
#define GET_BIT_IN_ARRAY(A, k) ( (A[(k/8)] & (1 << (k%8))) ? 1 : 0 )
// CRC-CCITT calculation macros
#define XOR(A, B) ( ((A) || (B)) && !((A) && (B)) )
#define CRC_CCITT_POLY 0x1021 // generator polynomial
#define CRC_CCITT_POLY_REVERSED 0x8408 // CRC_CCITT_POLY in reversed bit order
#define CRC_CCITT_INIT 0xFFFF // initial value
// maximum callsign length in bytes
#define RADIOLIB_AX25_MAX_CALLSIGN_LEN 6
@ -322,8 +317,6 @@ class AX25Client {
uint8_t sourceSSID = 0;
uint16_t preambleLen = 0;
static uint16_t getFrameCheckSequence(uint8_t* buff, size_t len);
void getCallsign(char* buff);
uint8_t getSSID();
};