Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DO NOT MERGE [AUR-101] Enforce custom APID values #19

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions src/Broker/Broker.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Broker_deliver_request(const enum RemoteInterface interface, const uint8_t* cons
(uint16_t)interface,
packetizer_buffer,
SPACE_PACKET_PRIMARY_HEADER_SIZE,
length);
length + SPACE_PACKET_ERROR_CONTROL_SIZE);

const enum SystemBus bus_id = port_to_bus_map[interface];
void* driver_private_data = bus_to_driver_private_data[bus_id];
Expand All @@ -85,7 +85,7 @@ Broker_receive_packet(uint8_t* const data, const size_t length)

#if defined BROKER_EXPECT_TELECOMMAND
const Packetizer_PacketType packet_type = Packetizer_PacketType_Telecommand;
#elif defined BROKER_EXPECT_TELEMETRT
#elif defined BROKER_EXPECT_TELEMETRY
const Packetizer_PacketType packet_type = Packetizer_PacketType_Telemetry;
#else
const Packetizer_PacketType packet_type = Packetizer_PacketType_Telemetry;
Expand Down
57 changes: 35 additions & 22 deletions src/Escaper/Escaper.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@

#include <assert.h>

static void
decode_escape_byte(Escaper* const self, const uint8_t byte)
{
if(self->m_decoded_packet_buffer_index >= self->m_decoded_packet_max_size) {
// buffer overflow prevented
// the current packet cannot be delivered
// reset escaper to search for new START_BYTE
self->m_decoded_packet_buffer_index = 0;
self->m_parse_state = Escaper_State_Wait;
} else {
if(byte == ESC_START_BYTE) {
self->m_decoded_packet_buffer[self->m_decoded_packet_buffer_index] = START_BYTE;
} else if(byte == ESC_ESCAPE_BYTE) {
self->m_decoded_packet_buffer[self->m_decoded_packet_buffer_index] = ESCAPE_BYTE;
} else if(byte == ESC_STOP_BYTE) {
self->m_decoded_packet_buffer[self->m_decoded_packet_buffer_index] = STOP_BYTE;
} else {
self->m_decoded_packet_buffer[self->m_decoded_packet_buffer_index] = byte;
}
++self->m_decoded_packet_buffer_index;
self->m_parse_state = Escaper_State_Data_Byte;
}
}

void
Escaper_init(Escaper* const self,
uint8_t* m_encoded_packet_buffer,
Expand All @@ -34,7 +58,6 @@ Escaper_init(Escaper* const self,
{
self->m_parse_state = Escaper_State_Wait;
self->m_encode_started = false;
self->m_escape = false;
self->m_encode_finished = false;
self->m_encoded_packet_buffer = m_encoded_packet_buffer;
self->m_encoded_packet_max_size = m_encoded_packet_buffer_size;
Expand Down Expand Up @@ -83,17 +106,7 @@ Escaper_decode_packet(Escaper* const self, uint8_t* buffer, const size_t length,
}
break;
case Escaper_State_Escape_Byte:
if(self->m_decoded_packet_buffer_index >= self->m_decoded_packet_max_size) {
// buffer overflow prevented
// the current packet cannot be delivered
// reset escaper to search for new START_BYTE
self->m_decoded_packet_buffer_index = 0;
self->m_parse_state = Escaper_State_Wait;
} else {
self->m_decoded_packet_buffer[self->m_decoded_packet_buffer_index] = buffer[i];
++self->m_decoded_packet_buffer_index;
self->m_parse_state = Escaper_State_Data_Byte;
}
decode_escape_byte(self, buffer[i]);
break;
default:
assert(false && "Unknown parser state");
Expand All @@ -106,7 +119,6 @@ void
Escaper_start_encoder(Escaper* const self)
{
self->m_encode_finished = false;
self->m_escape = false;
self->m_encode_started = false;
}

Expand All @@ -122,23 +134,24 @@ Escaper_encode_packet(Escaper* const self, const uint8_t* const data, const size
}

while(*index < length) {
if(self->m_escape) {
self->m_encoded_packet_buffer[encoded_packet_buffer_index] = data[*index];
++encoded_packet_buffer_index;
++*index;
self->m_escape = false;
} else if(data[*index] == START_BYTE) {
if(data[*index] == START_BYTE) {
self->m_encoded_packet_buffer[encoded_packet_buffer_index] = ESCAPE_BYTE;
++encoded_packet_buffer_index;
self->m_escape = true;
self->m_encoded_packet_buffer[encoded_packet_buffer_index] = ESC_START_BYTE;
++encoded_packet_buffer_index;
++*index;
} else if(data[*index] == ESCAPE_BYTE) {
self->m_encoded_packet_buffer[encoded_packet_buffer_index] = ESCAPE_BYTE;
++encoded_packet_buffer_index;
self->m_escape = true;
self->m_encoded_packet_buffer[encoded_packet_buffer_index] = ESC_ESCAPE_BYTE;
++encoded_packet_buffer_index;
++*index;
} else if(data[*index] == STOP_BYTE) {
self->m_encoded_packet_buffer[encoded_packet_buffer_index] = ESCAPE_BYTE;
++encoded_packet_buffer_index;
self->m_escape = true;
self->m_encoded_packet_buffer[encoded_packet_buffer_index] = ESC_STOP_BYTE;
++encoded_packet_buffer_index;
++*index;
} else {
self->m_encoded_packet_buffer[encoded_packet_buffer_index] = data[*index];
++encoded_packet_buffer_index;
Expand Down
1 change: 0 additions & 1 deletion src/Escaper/Escaper.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ typedef struct
{
Escaper_PacketParseState m_parse_state;
bool m_encode_started;
bool m_escape;
bool m_encode_finished;
uint8_t* m_encoded_packet_buffer;
size_t m_encoded_packet_max_size;
Expand Down
10 changes: 7 additions & 3 deletions src/Escaper/EscaperInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
#ifndef ESCAPER_INTERNAL_H
#define ESCAPER_INTERNAL_H

#define START_BYTE (uint8_t)0x00
#define STOP_BYTE (uint8_t)0xFF
#define ESCAPE_BYTE (uint8_t)0xFE
#define START_BYTE (uint8_t)0xB0
#define STOP_BYTE (uint8_t)0xC0
#define ESCAPE_BYTE (uint8_t)0xDA

#define ESC_START_BYTE (uint8_t)0xDB
#define ESC_STOP_BYTE (uint8_t)0xDC
#define ESC_ESCAPE_BYTE (uint8_t)0xDD

#endif
2 changes: 2 additions & 0 deletions src/Packetizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ project(Packetizer VERSION 1.0.0 LANGUAGES C)
add_library(Packetizer STATIC)
target_sources(Packetizer
PRIVATE Crc16.c
IsoChecksum.c
Packetizer.c
Crc16.h
IsoChecksum.h
PacketizerInternal.h
SpacePacketInternal.h
PUBLIC Packetizer.h
Expand Down
63 changes: 63 additions & 0 deletions src/Packetizer/IsoChecksum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**@file
* This file is part of the TASTE Runtime Common.
*
* @copyright 2022 N7 Space Sp. z o.o.
*
* TASTE Runtime Common was developed under a programme of,
* and funded by, the European Space Agency (the "ESA").
*
* Licensed under the ESA Public License (ESA-PL) Permissive,
* Version 2.3 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://essr.esa.int/license/list
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "IsoChecksum.h"

#define MAKE_UINT16(hi, lo) (uint16_t)(((hi) << 8u) | (lo))
#define REDUCE(x) (((x)&0xFFu) + ((x) >> 8u))
#define CHUNK_SIZE 20u

static uint16_t
fletcher16(const uint8_t* const data, const size_t length)
{
uint32_t sum1 = 0xffu;
uint32_t sum2 = 0xffu;
size_t dataLength = length;
const uint8_t* it = data;
Lurkerpas marked this conversation as resolved.
Show resolved Hide resolved

while(dataLength > 0u) {
size_t chunkLength = (dataLength > CHUNK_SIZE) ? CHUNK_SIZE : dataLength;
dataLength -= chunkLength;
do {
sum1 += *it++;
sum2 += sum1;
} while(--chunkLength > 0);
sum1 = REDUCE(sum1);
sum2 = REDUCE(sum2);
}

return MAKE_UINT16(REDUCE(sum2), REDUCE(sum1));
}

uint16_t
IsoChecksum_calculate(const uint8_t* const data, const size_t length)
{
const uint16_t sum = fletcher16(data, length);

const uint32_t f0 = sum & 0xFFu;
const uint32_t f1 = (sum >> 8u) & 0xFFu;

const uint32_t c0 = 0xFFu - ((f0 + f1) % 0xFFu);
const uint32_t c1 = 0xFFu - ((f0 + c0) % 0xFFu);

return MAKE_UINT16(c0, c1);
}
40 changes: 40 additions & 0 deletions src/Packetizer/IsoChecksum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**@file
* This file is part of the TASTE Runtime Common.
*
* @copyright 2022 N7 Space Sp. z o.o.
*
* TASTE Runtime Common was developed under a programme of,
* and funded by, the European Space Agency (the "ESA").
*
* Licensed under the ESA Public License (ESA-PL) Permissive,
* Version 2.3 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://essr.esa.int/license/list
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef PACKETIZER_ISOCHECKSUM_H
#define PACKETIZER_ISOCHECKSUM_H

#include <stddef.h>
#include <stdint.h>

/**
* @brief Calculates ISO checksum for given data block.
* Checksum is calculated according to as ECSS-E-70-41A Annex A.
*
* @param[in] data Pointer to start of data block. Can't be NULL.
* @param[in] length Data block length in bytes.
*
* @return Calculated checksum.
*/
uint16_t IsoChecksum_calculate(const uint8_t* const data, const size_t length);

#endif // PACKETIZER_ISOCHECKSUM_H
Loading