XfcTransportProtocol/Src/XfcProtDecoder.c

66 lines
2.0 KiB
C

//
// Created by xemon on 05.05.2022.
//
#include "XfcProtDecoder.h"
#include "XfcProtCrc.h"
#include "XfcProtCommon.h"
uint16_t XfcTransPackDecoderNextMagicFrom(tXfcProtDecoder *env, uint16_t begin) {
for (uint8_t *end = env->buffer->data + env->buffer->end, *byte = env->buffer->data + begin; end > byte; ++byte) {
if (*byte == env->magic) {
return byte - env->buffer->data;
}
}
return env->buffer->end;
}
bool XfcTransPackDecoderIsStartsWithMagick(tXfcProtDecoder *env) {
return env->buffer->data[0] == env->magic;
}
bool XfcTransPackDecoderHasHeader(tXfcProtDecoder *env) {
return XfcArrayGetDataSize(env->buffer) >= XFC_TRANSPORT_HEADER_LEN;
}
uint16_t XfcTransPackDecoderGetId(tXfcProtDecoder *env) {
return *((env->buffer->data + XFC_TRANSPORT_COMMAND_ID_OFFSET ));
}
uint16_t XfcTransPackDecoderGetDataSegmentLength(tXfcProtDecoder *env) {
return *((uint16_t *) (env->buffer->data + XFC_TRANSPORT_DATA_LEN_OFFSET));
}
uint16_t XfcTransPackDecoderGetCrc(tXfcProtDecoder *env) {
return *((uint16_t *) (env->buffer->data + XFC_TRANSPORT_HEADER_LEN + XfcTransPackDecoderGetDataSegmentLength(env)));
}
uint16_t XfcTransPackDecoderGetRequiredFullPackLength(tXfcProtDecoder *env) {
return XfcTransPackDecoderGetDataSegmentLength(env) + XFC_TRANSPORT_HEADER_LEN + XFC_TRANSPORT_CRC_LEN;
}
bool XfcTransPackDecoderHasRequiredLength(tXfcProtDecoder *env) {
return XfcTransPackDecoderGetRequiredFullPackLength(env) <= XfcArrayGetDataSize(env->buffer);
}
bool XfcTransPackDecoderIsCrcCorrect(tXfcProtDecoder *env) {
return XfcProtCrc(env->buffer->data, XfcTransPackDecoderGetDataSegmentLength(env) + XFC_TRANSPORT_HEADER_LEN) ==
XfcTransPackDecoderGetCrc(env);
}
tXfcArray XfcTransPackDecoderGetDataSegmentAsArray(tXfcProtDecoder *env) {
uint16_t dataLen = XfcTransPackDecoderGetDataSegmentLength(env);
return (tXfcArray) {
.data = env->buffer->data + XFC_TRANSPORT_HEADER_LEN,
.begin = 0,
.end = dataLen,
.limit = dataLen,
};
}