diff --git a/.gitignore b/.gitignore index d0c4049..eb6cdde 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ +.cache/ build/ *.zip - diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f7bd4f..947b82d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,8 @@ set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +# set(CMAKE_BUILD_TYPE Release) + add_executable(${PROJECT_NAME} ${SOURCE_DIR}/main.c) set(HEADERS @@ -34,4 +36,5 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${HEADERS}) 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") diff --git a/src/hdlc/hdlc_type.h b/src/hdlc/hdlc_type.h index 46bc790..99ab948 100644 --- a/src/hdlc/hdlc_type.h +++ b/src/hdlc/hdlc_type.h @@ -14,9 +14,12 @@ #define HDLC_INFO_LENGTH 16 #define HDLC_PACKET_LENGTH 2+2+HDLC_INFO_LENGTH+2 +#define HDLC_PACKET_FLAG 0x7e +#define HDLC_PACKET_ESCAPE 0xab + // Структура пакета (фрейма) HDLC typedef struct { - const uint8_t flag; + // const uint8_t flag; uint16_t address; uint16_t control; uint8_t info[HDLC_INFO_LENGTH]; diff --git a/src/main.c b/src/main.c index ece7f28..24e24ed 100644 --- a/src/main.c +++ b/src/main.c @@ -21,7 +21,7 @@ const char* bigLoopOsStateNames[OS_last] = { // BIG LOOP OS int main(void) { bigLoopOsState = OS_first; - while(1) { + while(true) { if (bigLoopOsState == OS_last) { bigLoopOsState = OS_first; } diff --git a/src/worker/worker.c b/src/worker/worker.c index 09a36d2..722e08a 100644 --- a/src/worker/worker.c +++ b/src/worker/worker.c @@ -11,10 +11,22 @@ int WORK_preparePackage(workerHost_s *worker) { printf("WORKX: .. Preparing package\n"); worker->packet->control = 0b0100110011110000; 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); - 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) { printf("WORKX: .. Receiving package\n"); // Receiving in buffer + + if (DO_UNIT_TESTS){WORK_testFill(worker->byteBuffer, WORK_BYTE_BUFFER_SIZE); return 0;} + uint8_t buf; if (FIFO_get(worker->fifo, &buf) == 0) { // return 0; } return -1; - - if (DO_UNIT_TESTS){WORK_testFill(worker->byteBuffer, WORK_BYTE_BUFFER_SIZE);} - - return 0; } int WORK_parsePackage(workerHost_s *worker) { printf("WORKX: .. Parsing package\n"); - - uint8_t *first = NULL, *last = NULL; + // Поиск пакета в массиве - for (size_t i = 0; i < WORK_BYTE_BUFFER_SIZE; i++) { - if (worker->byteBuffer[i] == worker->packet->flag) { + uint8_t *first = NULL, *last = NULL; + 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) { - first = &worker->byteBuffer[i]; + first = &tmpBuf[s]; first++; // Скипаем байт флага } else if (!last) { - last = &worker->byteBuffer[i]; + last = &tmpBuf[s]; 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) { @@ -106,11 +129,16 @@ int WORK_parsePackage(workerHost_s *worker) { } } } - } - } + } // (last > first) + } // (first && last) - // HDLC_parseControlField (worker->packet->control); - // printf("WORKX: .. Info value: %d\n", field.value); + memset(worker->byteBuffer, 0, WORK_BYTE_BUFFER_SIZE); + 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; } diff --git a/src/worker/worker.h b/src/worker/worker.h index b714d86..48beb7f 100644 --- a/src/worker/worker.h +++ b/src/worker/worker.h @@ -33,7 +33,7 @@ int WORK_parsePackage(workerHost_s *worker); int WORK_fillBuffer8b(uint8_t *buffer, size_t size); -// toolsNtests +// tools and tests int WORK_printBlocks(workerHost_s *worker, uint8_t mode); int WORK_testFill(uint8_t* buffer, size_t bufferSize); int WORK_testFifo(fifo_s *fifo); diff --git a/src/worker/worker_master.c b/src/worker/worker_master.c index 3bd8ec6..4ed7cd4 100644 --- a/src/worker/worker_master.c +++ b/src/worker/worker_master.c @@ -4,7 +4,7 @@ #include -static hdlcPacket_s hdlcMasterPacket = {hdlcFlag, hdlcMasterAddress}; +static hdlcPacket_s hdlcMasterPacket = {hdlcMasterAddress}; static fifo_s hdlcMasterFifo; static workerHost_s hdlcMaster = {0}; static uint8_t masterBuffer[WORK_BYTE_BUFFER_SIZE]; diff --git a/src/worker/worker_slave.c b/src/worker/worker_slave.c index 1851d5c..82d10b8 100644 --- a/src/worker/worker_slave.c +++ b/src/worker/worker_slave.c @@ -1,7 +1,7 @@ #include "main.h" #include "worker.h" -static hdlcPacket_s hdlcSlavePacket = {hdlcFlag, hdlcSlaveAddress}; +static hdlcPacket_s hdlcSlavePacket = {hdlcSlaveAddress}; static fifo_s hdlcSlaveFifo; static workerHost_s hdlcSlave = {0}; @@ -11,7 +11,7 @@ workerHost_s* WORK_slaveInit () { hdlcSlave.packet = &hdlcSlavePacket; hdlcSlave.fifo = &hdlcSlaveFifo; hdlcSlave.isInitialized = true; - printf("WSLAV Slave initialization done\n"); + printf("WSLAV: Slave initialization done\n"); WORK_printBlocks(&hdlcSlave, 3); } diff --git a/src/worker/worker_tools.c b/src/worker/worker_tools.c index de456da..e574ce2 100644 --- a/src/worker/worker_tools.c +++ b/src/worker/worker_tools.c @@ -1,5 +1,12 @@ #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 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) { printf("TOOL: .. Packet:\n"); 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: .... 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); } @@ -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]); 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++) { 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) { + bufferSize++; static uint8_t testbuffer[50] = { 0xaa,0xbb, - 0x7e, + HDLC_PACKET_FLAG, 0xa1, 0xa2, 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, - 0x7e + HDLC_PACKET_FLAG }; for (int i = 0; i < 50; i++) { buffer[i] = testbuffer[i]; @@ -57,6 +69,7 @@ int WORK_testFill(uint8_t* buffer, size_t bufferSize) { } +// Тестирование фифо int WORK_testFifo(fifo_s *fifo) { printf("TEST: FIFO START:\n"); @@ -64,14 +77,16 @@ int WORK_testFifo(fifo_s *fifo) { FIFO_put(fifo, 3); FIFO_put(fifo, 8); FIFO_print(fifo); + uint8_t arr[10] = {0}; for (int a = 0; a < 10; a++) { FIFO_get(fifo, &arr[a]); printf("%d:", arr[a]); } - printf("\n"); + printf("\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++) { buffer[i] = i; } + buffer[size/2] = HDLC_PACKET_FLAG; return 0; } \ No newline at end of file