2022-11-04 09:35:17 +01:00
|
|
|
//**************************************************************************************
|
|
|
|
/*! \copyright: 2020-2021 Thales Alenia Space Deutschland GmbH
|
|
|
|
* \project: multiMIND
|
|
|
|
* \file: (name of source file: hdlc.c)
|
|
|
|
* \date: (09.02.2022)
|
|
|
|
* \author: (Stelios Filippopoulos)
|
|
|
|
* \brief: (hdlc functions)
|
|
|
|
* \language: (C)
|
|
|
|
**************************************************************************************
|
|
|
|
*/
|
|
|
|
|
2022-11-04 09:45:32 +01:00
|
|
|
#include "tas/hdlc.h"
|
|
|
|
#include "tas/crc.h"
|
2022-11-04 09:35:17 +01:00
|
|
|
|
2022-11-04 12:38:30 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2022-11-17 11:33:47 +01:00
|
|
|
void hdlc_add_byte(uint8_t ch, uint8_t *buff, size_t *pos)
|
2022-11-04 09:35:17 +01:00
|
|
|
{
|
2022-11-17 11:33:47 +01:00
|
|
|
size_t templen = *pos;
|
2022-11-04 09:35:17 +01:00
|
|
|
|
|
|
|
if ((ch == 0x7E) ||
|
|
|
|
(ch == 0x7D) ||
|
|
|
|
(ch == 0x7C))
|
|
|
|
{
|
|
|
|
buff[templen++] = 0x7D;
|
|
|
|
ch ^= 0x20;
|
|
|
|
}
|
|
|
|
buff[templen++] = ch;
|
|
|
|
|
|
|
|
*pos = templen;
|
|
|
|
}
|
|
|
|
|
2022-11-04 12:38:30 +01:00
|
|
|
void hdlc_add_framing(const uint8_t *src, size_t slen, uint8_t *dst, size_t *dlen)
|
2022-11-04 09:35:17 +01:00
|
|
|
{
|
2022-11-17 11:33:47 +01:00
|
|
|
size_t tlen = 0;
|
2022-11-04 09:35:17 +01:00
|
|
|
uint16_t ii;
|
|
|
|
uint16_t crc16;
|
|
|
|
uint8_t bt;
|
|
|
|
|
|
|
|
// calc crc16
|
|
|
|
crc16 = calc_crc16_buff_reflected( src, slen );
|
|
|
|
|
|
|
|
dst[tlen++] = 0x7E;
|
|
|
|
for (ii = 0; ii < slen; ii++)
|
|
|
|
{
|
|
|
|
bt = *src++;
|
|
|
|
hdlc_add_byte(bt, dst, &tlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
// hdlc crc16 is in little endian format
|
2022-11-17 11:33:47 +01:00
|
|
|
// WARNING: This is not portable code! Bytes need to be swapped on a big
|
|
|
|
// endian system
|
|
|
|
// TODO: Fix
|
2022-11-04 09:35:17 +01:00
|
|
|
hdlc_add_byte((uint8_t) (crc16 & 0xFF), dst, &tlen);
|
|
|
|
hdlc_add_byte((uint8_t) ((crc16 >> 8) & 0xFF), dst, &tlen);
|
|
|
|
|
|
|
|
dst[tlen++] = 0x7C;
|
|
|
|
*dlen = tlen;
|
|
|
|
}
|
|
|
|
|
2022-11-16 14:42:18 +01:00
|
|
|
int hdlc_remove_framing_with_crc_check(const uint8_t *src, size_t slen, uint8_t *dst, size_t *dlen)
|
2022-11-04 09:35:17 +01:00
|
|
|
{
|
|
|
|
uint16_t tlen = 0;
|
|
|
|
uint16_t ii;
|
|
|
|
uint8_t bt;
|
|
|
|
|
|
|
|
*dlen = 0;
|
2022-11-16 14:42:18 +01:00
|
|
|
if (slen < 4) return -1;
|
|
|
|
if ((src[tlen] != 0x7E) && (src[slen-1] != 0x7C)) return -2;
|
2022-11-17 11:45:09 +01:00
|
|
|
src++;
|
2022-11-04 09:35:17 +01:00
|
|
|
for (ii = 1; ii < slen-1; ii++)
|
|
|
|
{
|
|
|
|
bt = *src++;
|
|
|
|
|
|
|
|
if (bt == 0x7D)
|
|
|
|
{
|
|
|
|
bt = *src++ ^ 0x20;
|
2022-11-17 11:45:09 +01:00
|
|
|
ii++;
|
2022-11-04 09:35:17 +01:00
|
|
|
}
|
|
|
|
dst[tlen++] = bt;
|
|
|
|
}
|
2022-11-16 14:42:18 +01:00
|
|
|
// calc crc16
|
2022-11-18 14:18:45 +01:00
|
|
|
if(calc_crc16_buff_reflected( dst, tlen ) != 0x0f47) {
|
2022-11-16 14:42:18 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*dlen = tlen - 2;
|
2022-11-16 13:26:49 +01:00
|
|
|
return 0;
|
2022-11-04 09:35:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|