eive-obsw/thirdparty/tas/hdlc.c
Robin Mueller 1ac9e53b1f
Some checks failed
EIVE/eive-obsw/pipeline/head There was a failure building this commit
add TAS sources
2022-11-04 09:35:17 +01:00

81 lines
1.7 KiB
C

//**************************************************************************************
/*! \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)
**************************************************************************************
*/
#include <stdint.h>
#include "hdlc.h"
#include "spacepacket.h"
#include "crc.h"
static void hdlc_add_byte(uint8_t ch, uint8_t *buff, uint16_t *pos)
{
uint16_t templen = *pos;
if ((ch == 0x7E) ||
(ch == 0x7D) ||
(ch == 0x7C))
{
buff[templen++] = 0x7D;
ch ^= 0x20;
}
buff[templen++] = ch;
*pos = templen;
}
void hdlc_add_framing(uint8_t *src, uint16_t slen, uint8_t *dst, uint16_t *dlen)
{
uint16_t tlen = 0;
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
hdlc_add_byte((uint8_t) (crc16 & 0xFF), dst, &tlen);
hdlc_add_byte((uint8_t) ((crc16 >> 8) & 0xFF), dst, &tlen);
dst[tlen++] = 0x7C;
*dlen = tlen;
}
void hdlc_remove_framing(uint8_t *src, uint16_t slen, uint8_t *dst, uint16_t *dlen)
{
uint16_t tlen = 0;
uint16_t ii;
uint8_t bt;
*dlen = 0;
if (slen == 0) return;
if ((src[tlen] != 0x7E) && (src[slen-1] != 0x7C)) return;
for (ii = 1; ii < slen-1; ii++)
{
bt = *src++;
if (bt == 0x7D)
{
bt = *src++ ^ 0x20;
}
dst[tlen++] = bt;
}
*dlen = tlen;
}