Добавил подмену байт

This commit is contained in:
ru.sadekov 2025-10-27 11:31:40 +03:00
parent b32be8eaa4
commit c719f259ed
9 changed files with 80 additions and 30 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
.cache/
build/ build/
*.zip *.zip

View File

@ -9,6 +9,8 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# set(CMAKE_BUILD_TYPE Release)
add_executable(${PROJECT_NAME} ${SOURCE_DIR}/main.c) add_executable(${PROJECT_NAME} ${SOURCE_DIR}/main.c)
set(HEADERS set(HEADERS
@ -34,4 +36,5 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${HEADERS})
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
target_compile_options(${PROJECT_NAME} PRIVATE -O0 -Wall -Wextra)
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "bin") set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "bin")

View File

@ -14,9 +14,12 @@
#define HDLC_INFO_LENGTH 16 #define HDLC_INFO_LENGTH 16
#define HDLC_PACKET_LENGTH 2+2+HDLC_INFO_LENGTH+2 #define HDLC_PACKET_LENGTH 2+2+HDLC_INFO_LENGTH+2
#define HDLC_PACKET_FLAG 0x7e
#define HDLC_PACKET_ESCAPE 0xab
// Структура пакета (фрейма) HDLC // Структура пакета (фрейма) HDLC
typedef struct { typedef struct {
const uint8_t flag; // const uint8_t flag;
uint16_t address; uint16_t address;
uint16_t control; uint16_t control;
uint8_t info[HDLC_INFO_LENGTH]; uint8_t info[HDLC_INFO_LENGTH];

View File

@ -21,7 +21,7 @@ const char* bigLoopOsStateNames[OS_last] = {
// BIG LOOP OS // BIG LOOP OS
int main(void) { int main(void) {
bigLoopOsState = OS_first; bigLoopOsState = OS_first;
while(1) { while(true) {
if (bigLoopOsState == OS_last) { if (bigLoopOsState == OS_last) {
bigLoopOsState = OS_first; bigLoopOsState = OS_first;
} }

View File

@ -11,10 +11,22 @@ int WORK_preparePackage(workerHost_s *worker) {
printf("WORKX: .. Preparing package\n"); printf("WORKX: .. Preparing package\n");
worker->packet->control = 0b0100110011110000; worker->packet->control = 0b0100110011110000;
WORK_fillBuffer8b(worker->packet->info, HDLC_INFO_LENGTH); WORK_fillBuffer8b(worker->packet->info, HDLC_INFO_LENGTH);
// memcpy(packet->info, testInfo, HDLC_INFO_LENGTH);
worker->packet->fcs = CRC16_compute(worker->packet->info, HDLC_INFO_LENGTH); worker->packet->fcs = CRC16_compute(worker->packet->info, HDLC_INFO_LENGTH);
return 0;
// Перемещение структуры в массив на передачу
size_t structSize = sizeof(hdlcPacket_s);
uint8_t tmpPtr = 0;
uint8_t *packPtr = (uint8_t*)&worker->packet;
for (size_t s = 0; s < structSize; s++, tmpPtr++) {
// Нашли флаг в теле сообщения
if (packPtr[s] == HDLC_PACKET_FLAG) {
worker->byteBuffer[tmpPtr] = HDLC_PACKET_ESCAPE;
tmpPtr++;
}
worker->byteBuffer[tmpPtr] = packPtr[s];
}
return 0;
} }
@ -39,35 +51,46 @@ int WORK_waitPackage(workerHost_s *worker) {
int WORK_receivePackage(workerHost_s *worker) { int WORK_receivePackage(workerHost_s *worker) {
printf("WORKX: .. Receiving package\n"); printf("WORKX: .. Receiving package\n");
// Receiving in buffer // Receiving in buffer
if (DO_UNIT_TESTS){WORK_testFill(worker->byteBuffer, WORK_BYTE_BUFFER_SIZE); return 0;}
uint8_t buf; uint8_t buf;
if (FIFO_get(worker->fifo, &buf) == 0) { if (FIFO_get(worker->fifo, &buf) == 0) {
// return 0; // return 0;
} }
return -1; return -1;
if (DO_UNIT_TESTS){WORK_testFill(worker->byteBuffer, WORK_BYTE_BUFFER_SIZE);}
return 0;
} }
int WORK_parsePackage(workerHost_s *worker) { int WORK_parsePackage(workerHost_s *worker) {
printf("WORKX: .. Parsing package\n"); printf("WORKX: .. Parsing package\n");
uint8_t *first = NULL, *last = NULL;
// Поиск пакета в массиве // Поиск пакета в массиве
for (size_t i = 0; i < WORK_BYTE_BUFFER_SIZE; i++) { uint8_t *first = NULL, *last = NULL;
if (worker->byteBuffer[i] == worker->packet->flag) { uint8_t tmpBuf[WORK_BYTE_BUFFER_SIZE] = {0};
uint8_t bufPtr=0;
for (size_t s = 0; s < WORK_BYTE_BUFFER_SIZE; s++, bufPtr++) {
// Найден флаг
if (worker->byteBuffer[bufPtr] == HDLC_PACKET_FLAG) {
if (!first) { if (!first) {
first = &worker->byteBuffer[i]; first = &tmpBuf[s];
first++; // Скипаем байт флага first++; // Скипаем байт флага
} else if (!last) { } else if (!last) {
last = &worker->byteBuffer[i]; last = &tmpBuf[s];
last--; last--;
} }
} }
// Найден escape-флаг
if (worker->byteBuffer[bufPtr] == HDLC_PACKET_ESCAPE) {
if (worker->byteBuffer[bufPtr+1] == HDLC_PACKET_FLAG) {
bufPtr++;
} }
}
tmpBuf[s] = worker->byteBuffer[bufPtr];
}
// Разбор пакета в структуру // Разбор пакета в структуру
if (first && last) { if (first && last) {
@ -106,11 +129,16 @@ int WORK_parsePackage(workerHost_s *worker) {
} }
} }
} }
} } // (last > first)
} } // (first && last)
// HDLC_parseControlField (worker->packet->control); memset(worker->byteBuffer, 0, WORK_BYTE_BUFFER_SIZE);
// printf("WORKX: .. Info value: %d\n", field.value); memcpy(worker->byteBuffer, tmpBuf, WORK_BYTE_BUFFER_SIZE);
for (uint8_t i = 0; i < WORK_BYTE_BUFFER_SIZE/2; i++) {
printf("0x%d, ", worker->byteBuffer[i]);
}
printf("\n");
return 0; return 0;
} }

View File

@ -33,7 +33,7 @@ int WORK_parsePackage(workerHost_s *worker);
int WORK_fillBuffer8b(uint8_t *buffer, size_t size); int WORK_fillBuffer8b(uint8_t *buffer, size_t size);
// toolsNtests // tools and tests
int WORK_printBlocks(workerHost_s *worker, uint8_t mode); int WORK_printBlocks(workerHost_s *worker, uint8_t mode);
int WORK_testFill(uint8_t* buffer, size_t bufferSize); int WORK_testFill(uint8_t* buffer, size_t bufferSize);
int WORK_testFifo(fifo_s *fifo); int WORK_testFifo(fifo_s *fifo);

View File

@ -4,7 +4,7 @@
#include <string.h> #include <string.h>
static hdlcPacket_s hdlcMasterPacket = {hdlcFlag, hdlcMasterAddress}; static hdlcPacket_s hdlcMasterPacket = {hdlcMasterAddress};
static fifo_s hdlcMasterFifo; static fifo_s hdlcMasterFifo;
static workerHost_s hdlcMaster = {0}; static workerHost_s hdlcMaster = {0};
static uint8_t masterBuffer[WORK_BYTE_BUFFER_SIZE]; static uint8_t masterBuffer[WORK_BYTE_BUFFER_SIZE];

View File

@ -1,7 +1,7 @@
#include "main.h" #include "main.h"
#include "worker.h" #include "worker.h"
static hdlcPacket_s hdlcSlavePacket = {hdlcFlag, hdlcSlaveAddress}; static hdlcPacket_s hdlcSlavePacket = {hdlcSlaveAddress};
static fifo_s hdlcSlaveFifo; static fifo_s hdlcSlaveFifo;
static workerHost_s hdlcSlave = {0}; static workerHost_s hdlcSlave = {0};
@ -11,7 +11,7 @@ workerHost_s* WORK_slaveInit () {
hdlcSlave.packet = &hdlcSlavePacket; hdlcSlave.packet = &hdlcSlavePacket;
hdlcSlave.fifo = &hdlcSlaveFifo; hdlcSlave.fifo = &hdlcSlaveFifo;
hdlcSlave.isInitialized = true; hdlcSlave.isInitialized = true;
printf("WSLAV Slave initialization done\n"); printf("WSLAV: Slave initialization done\n");
WORK_printBlocks(&hdlcSlave, 3); WORK_printBlocks(&hdlcSlave, 3);
} }

View File

@ -1,5 +1,12 @@
#include "worker.h" #include "worker.h"
#define ARRAY_SIZE(array) sizeof(array)/sizeof(array[0])
static inline void WORK_printArray (uint8_t* array, size_t size) {
for (size_t s = 0; s < size; s++) {
printf("%d", array[s]);
}
}
// Параметрический вывод пакета worker // Параметрический вывод пакета worker
int WORK_printBlocks(workerHost_s *worker, uint8_t mode) { int WORK_printBlocks(workerHost_s *worker, uint8_t mode) {
@ -15,9 +22,11 @@ int WORK_printBlocks(workerHost_s *worker, uint8_t mode) {
if (mode & 1) { if (mode & 1) {
printf("TOOL: .. Packet:\n"); printf("TOOL: .. Packet:\n");
printf("TOOL: .... Address: %d \n", worker->packet->address); printf("TOOL: .... Address: %d \n", worker->packet->address);
printf("TOOL: .... Flag: %d \n", worker->packet->flag); // printf("TOOL: .... Flag: %d \n", worker->packet->flag);
printf("TOOL: .... Conrol Field: %d \n", worker->packet->control); printf("TOOL: .... Conrol Field: %d \n", worker->packet->control);
printf("TOOL: .... Info Field: %d \n", worker->packet->info); printf("TOOL: .... Info Field: ");
WORK_printArray(worker->packet->info, ARRAY_SIZE(worker->packet->info));
printf("\n");
printf("TOOL: .... FCS: %d \n", worker->packet->fcs); printf("TOOL: .... FCS: %d \n", worker->packet->fcs);
} }
@ -26,7 +35,7 @@ int WORK_printBlocks(workerHost_s *worker, uint8_t mode) {
size_t size = sizeof(worker->fifo->data) / sizeof(worker->fifo->data[0]); size_t size = sizeof(worker->fifo->data) / sizeof(worker->fifo->data[0]);
printf("TOOL: .. FIFO:\n"); printf("TOOL: .. FIFO:\n");
printf("TOOL: .... Data (%d items): ", size); printf("TOOL: .... Data (%d items): ", (int)size);
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
printf("%02X ", worker->fifo->data[i]); printf("%02X ", worker->fifo->data[i]);
} }
@ -41,14 +50,17 @@ int WORK_printBlocks(workerHost_s *worker, uint8_t mode) {
// Тестовое заполнение буфера // Тестовое заполнение буфера
int WORK_testFill(uint8_t* buffer, size_t bufferSize) { int WORK_testFill(uint8_t* buffer, size_t bufferSize) {
bufferSize++;
static uint8_t testbuffer[50] = { static uint8_t testbuffer[50] = {
0xaa,0xbb, 0xaa,0xbb,
0x7e, HDLC_PACKET_FLAG,
0xa1, 0xa2, 0xa1, 0xa2,
0xc1, 0xc2, 0xc1, 0xc2,
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xde, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
HDLC_PACKET_ESCAPE, HDLC_PACKET_FLAG,
0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xde,
0xcc, 0xcd, 0xcc, 0xcd,
0x7e HDLC_PACKET_FLAG
}; };
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
buffer[i] = testbuffer[i]; buffer[i] = testbuffer[i];
@ -57,6 +69,7 @@ int WORK_testFill(uint8_t* buffer, size_t bufferSize) {
} }
// Тестирование фифо
int WORK_testFifo(fifo_s *fifo) { int WORK_testFifo(fifo_s *fifo) {
printf("TEST: FIFO START:\n"); printf("TEST: FIFO START:\n");
@ -64,14 +77,16 @@ int WORK_testFifo(fifo_s *fifo) {
FIFO_put(fifo, 3); FIFO_put(fifo, 3);
FIFO_put(fifo, 8); FIFO_put(fifo, 8);
FIFO_print(fifo); FIFO_print(fifo);
uint8_t arr[10] = {0}; uint8_t arr[10] = {0};
for (int a = 0; a < 10; a++) { for (int a = 0; a < 10; a++) {
FIFO_get(fifo, &arr[a]); FIFO_get(fifo, &arr[a]);
printf("%d:", arr[a]); printf("%d:", arr[a]);
} }
printf("\n");
printf("\n");
printf("TEST: FIFO END:\n"); printf("TEST: FIFO END:\n");
return 0;
} }
@ -81,5 +96,6 @@ int WORK_fillBuffer8b(uint8_t *buffer, size_t size) {
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
buffer[i] = i; buffer[i] = i;
} }
buffer[size/2] = HDLC_PACKET_FLAG;
return 0; return 0;
} }