diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..eda337c --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + // "defines": ["DO_UNIT_TESTS=1"], + "compilerPath": "/usr/bin/clang", + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "linux-clang-x64", + "configurationProvider": "ms-vscode.cmake-tools", + "compileCommands": "${workspaceFolder}/build/compile_commands.json" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 660b946..a5aeafb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,11 @@ "fifo_type.h": "c", "chrono": "c", "span": "c", - "worker.h": "c" + "worker.h": "c", + "fifo.h": "c", + "string.h": "c", + "*.ipp": "c", + "cstring": "c", + "stddef.h": "c" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b98859..3f7bd4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ project(${PROJECT_NAME} LANGUAGES C) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_EXTENSIONS OFF) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) add_executable(${PROJECT_NAME} ${SOURCE_DIR}/main.c) @@ -29,8 +30,7 @@ set(SOURCES ) target_include_directories(${PROJECT_NAME} PRIVATE ${HEADERS}) -# target_compile_definitions(${PROJECT_NAME} PRIVATE __USE_POSIX199309 __USE_ISOC11) -target_compile_definitions(${PROJECT_NAME} PRIVATE _POSIX_C_SOURCE=199309L _ISOC11_SOURCE) +# target_compile_definitions(${PROJECT_NAME} PUBLIC ) target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) diff --git a/readme.md b/readme.md index 6f6dd6c..cc59c5a 100644 --- a/readme.md +++ b/readme.md @@ -68,6 +68,19 @@ FIFO пуст или FIFO полон. Вся логика работы прог ## Функции и их параметры +### worker +- *int WORK_execute(int (*callback)(workerHost_s*), workerHost_s *worker);* +- int WORK_preparePackage(workerHost_s *worker); +- int WORK_sendPackage(workerHost_s *worker); +- int WORK_waitPackage(workerHost_s *worker); +- int WORK_receivePackage(workerHost_s *worker); +- int WORK_checkPackage(workerHost_s *worker); +- int WORK_parsePackage(workerHost_s *worker); +- int WORK_fillBuffer8b(uint8_t *buffer, size_t size); + +// tools +int WORK_printBlocks(workerHost_s *worker, uint8_t mode); +int WORK_testFill(uint8_t* buffer, size_t bufferSize); ### tools - TOOL_workerPrintBlocks - hdlcHost_s *worker - указатель на машину diff --git a/src/fifo/fifo.c b/src/fifo/fifo.c index 8eb42d8..255b712 100644 --- a/src/fifo/fifo.c +++ b/src/fifo/fifo.c @@ -1,2 +1,90 @@ #include "fifo.h" +#include "stdio.h" +// if (FIFO_get(fifo, byte) == 0) { +// OK +// } else { +// FIFO ERROR +// } + +int FIFO_isUsable(fifo_s* fifo); + +// Инициализация fifo +int FIFO_init(fifo_s* fifo) { + if (fifo != NULL) { + for (size_t i = 0; i < FIFO_DATA_SIZE; ++i) { + fifo->data[i] = 0; + } + fifo->head = 0; + fifo->state = FIFOS_empty; + fifo->isInitialized = 1; + if (FIFO_isUsable(fifo) == 0) { + return 0; + } + } + return -1; +} + + +// Заполнение fifo +int FIFO_put(fifo_s* fifo, uint8_t data) { + if (FIFO_isUsable(fifo) == 0) { // Не заполнено + if ((fifo->state == FIFOS_empty) || (fifo->state == FIFOS_ready)) { + fifo->data[fifo->head] = data; + + if (fifo->head < FIFO_DATA_SIZE-1) { + fifo->head++; + fifo->state = FIFOS_ready; + } else { + fifo->head = FIFO_DATA_SIZE-1; + fifo->state = FIFOS_full; + } + return 0; + } + } + return -1; +} + + +// Получение из fifo +int FIFO_get(fifo_s* fifo, uint8_t *data) { + if (FIFO_isUsable(fifo) == 0) { + if ((fifo->state == FIFOS_full) || (fifo->state == FIFOS_ready)) { + *data = fifo->data[fifo->head-1]; + fifo->data[fifo->head-1] = 0; + if (fifo->head > 1) { + fifo->head--; + fifo->state = FIFOS_ready; + } else { + fifo->head = 0; + fifo->state = FIFOS_empty; + } + return 0; + } + } + return -1; +} + + +int FIFO_print(fifo_s* fifo) { + if (FIFO_isUsable(fifo) == 0) { + printf("FIFO: "); + for (size_t i = 0; i < FIFO_DATA_SIZE; i++) { + printf("%d", fifo->data[i]); + } + printf("\n"); + return 0; + } + return -1; +} + + +// Проверка доступности fifo +int FIFO_isUsable(fifo_s* fifo) { + if (fifo != NULL) { + if (fifo->isInitialized == 1) { + return 0; + } + } + return -1; +} \ No newline at end of file diff --git a/src/fifo/fifo.h b/src/fifo/fifo.h index 2684fa9..d3a038d 100644 --- a/src/fifo/fifo.h +++ b/src/fifo/fifo.h @@ -3,6 +3,25 @@ #define FIFO_H #include "fifo_type.h" +#include +// Размер FIFO +#define FIFO_DATA_SIZE 8 + +// Структура FIFO +typedef struct { + uint8_t data[FIFO_DATA_SIZE]; + uint8_t head; + fifoState_e state; + uint8_t isInitialized; +} fifo_s; + + +// Функции FIFO +int FIFO_init(fifo_s* fifo); +int FIFO_put(fifo_s* fifo, uint8_t data); +int FIFO_get(fifo_s* fifo, uint8_t *data); +int FIFO_print(fifo_s* fifo); +int FIFO_isUsable(fifo_s* fifo); #endif // FIFO_H \ No newline at end of file diff --git a/src/fifo/fifo_type.h b/src/fifo/fifo_type.h index fa14505..b2b826b 100644 --- a/src/fifo/fifo_type.h +++ b/src/fifo/fifo_type.h @@ -10,13 +10,7 @@ typedef enum { FIFOS_ready, FIFOS_full, FIFOS_error -} fifoState; +} fifoState_e; -// Структура FIFO -typedef struct { - uint8_t data[8]; - uint8_t pointer; - fifoState state; -} fifo_s; #endif // FIFO_TYPE_H \ No newline at end of file diff --git a/src/main.c b/src/main.c index abe03a4..ece7f28 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,5 @@ #include "main.h" -#if __cplusplus -#pragma message ("plus") -#else -#pragma message ("pure") -#endif - - // Перечисление статусов машины состояния enum bigLoopOsState_e { OS_first = 0, diff --git a/src/main.h b/src/main.h index 45cd698..a0e4fa5 100644 --- a/src/main.h +++ b/src/main.h @@ -2,6 +2,8 @@ #ifndef MAIN_H #define MAIN_H +#define DO_UNIT_TESTS 1 + #include #include #include diff --git a/src/tools.c b/src/tools.c index 44d4eab..e3d8d17 100644 --- a/src/tools.c +++ b/src/tools.c @@ -1,8 +1,18 @@ // Файл с общими функциями и конфигурацией -#include "main.h" +#define _POSIX_C_SOURCE 199309L #include +#if __cplusplus +#pragma message ("plus") +#else +#pragma message ("pure") +#endif + +#include "main.h" + + + // Вычисление контрольной суммы CRC16 uint16_t CRC16_compute(uint8_t *data, size_t length) { @@ -53,12 +63,12 @@ uint16_t TOOL_reverseBitsN(uint16_t value, uint8_t n) { void TOOL_msleep(long milliseconds) { - struct timespec ts; - ts.tv_sec = milliseconds / 1000; // Seconds part - ts.tv_nsec = (milliseconds % 1000) * 1000000; // Nanoseconds part + struct timespec time; + time.tv_sec = milliseconds / 1000; + time.tv_nsec = (milliseconds % 1000) * 1000000; // nanosleep can be interrupted by signals, so we loop if interrupted - while (nanosleep(&ts, &ts) == -1) { + while (nanosleep(&time, &time) == -1) { // If interrupted, remaining time is in ts, so we continue sleeping // unless an actual error occurred (other than EINTR) // For simplicity, this example assumes EINTR is the only -1 cause. diff --git a/src/worker/worker.c b/src/worker/worker.c index 3cb5ec1..09a36d2 100644 --- a/src/worker/worker.c +++ b/src/worker/worker.c @@ -29,15 +29,24 @@ int WORK_sendPackage(workerHost_s *worker) { // Ожидание пакета int WORK_waitPackage(workerHost_s *worker) { printf("WORKX: .. Waiting for a package\n"); - return 0; + if (FIFO_isUsable(worker->fifo) == 0) { + return 0; + } + return -1; } int WORK_receivePackage(workerHost_s *worker) { printf("WORKX: .. Receiving package\n"); // Receiving in buffer + uint8_t buf; + if (FIFO_get(worker->fifo, &buf) == 0) { + // return 0; + } + + return -1; - WORK_testFill(worker->byteBuffer, WORK_BYTE_BUFFER_SIZE); + if (DO_UNIT_TESTS){WORK_testFill(worker->byteBuffer, WORK_BYTE_BUFFER_SIZE);} return 0; } diff --git a/src/worker/worker.h b/src/worker/worker.h index 263ff6e..b714d86 100644 --- a/src/worker/worker.h +++ b/src/worker/worker.h @@ -10,7 +10,7 @@ #include "hdlc_type.h" -#include "fifo_type.h" +#include "fifo.h" // Структура устройства typedef struct { @@ -33,9 +33,11 @@ int WORK_parsePackage(workerHost_s *worker); int WORK_fillBuffer8b(uint8_t *buffer, size_t size); -// tools +// toolsNtests int WORK_printBlocks(workerHost_s *worker, uint8_t mode); int WORK_testFill(uint8_t* buffer, size_t bufferSize); +int WORK_testFifo(fifo_s *fifo); + #endif //WORKER_H diff --git a/src/worker/worker_master.c b/src/worker/worker_master.c index abc888f..3bd8ec6 100644 --- a/src/worker/worker_master.c +++ b/src/worker/worker_master.c @@ -16,6 +16,10 @@ workerHost_s* WORK_masterInit () { hdlcMaster.fifo = &hdlcMasterFifo; hdlcMaster.isInitialized = true; hdlcMaster.byteBuffer = masterBuffer; + FIFO_init(&hdlcMasterFifo); + + if (DO_UNIT_TESTS){WORK_testFifo(hdlcMaster.fifo);} + printf("WMAST: Master initialization done\n"); WORK_printBlocks(&hdlcMaster, 3); diff --git a/src/worker/worker_tools.c b/src/worker/worker_tools.c index 435751c..de456da 100644 --- a/src/worker/worker_tools.c +++ b/src/worker/worker_tools.c @@ -32,7 +32,7 @@ int WORK_printBlocks(workerHost_s *worker, uint8_t mode) { } printf("\n"); - printf("TOOL: .. Pointer: %d ", worker->fifo->pointer); + printf("TOOL: .. Pointer: %d ", worker->fifo->head); } return 0; @@ -57,6 +57,24 @@ int WORK_testFill(uint8_t* buffer, size_t bufferSize) { } +int WORK_testFifo(fifo_s *fifo) { + printf("TEST: FIFO START:\n"); + + FIFO_put(fifo, 4); + 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("TEST: FIFO END:\n"); +} + + // Заполнение массива int WORK_fillBuffer8b(uint8_t *buffer, size_t size) { memset(buffer, 0, size);