From 4da348b93e3c74cfc54607c41c8e9acd2e8ddbb6 Mon Sep 17 00:00:00 2001 From: Andreas Berthoud Date: Sat, 10 Jul 2021 11:09:18 +0200 Subject: [PATCH] ble: Add Command class for logging --- backend/command.py | 5 +- backend/defaults/config.yml | 3 +- nucleo-wb55-dongle-ble/.project | 1 + nucleo-wb55-dongle-ble/Core/Src/logger.c | 118 ------------------ nucleo-wb55-dongle-ble/Core/Src/main.c | 12 +- nucleo-wb55-dongle-ble/app/Command.cpp | 28 +++++ nucleo-wb55-dongle-ble/app/Command.hpp | 39 ++++++ nucleo-wb55-dongle-ble/app/LogCommand.cpp | 55 ++++++++ nucleo-wb55-dongle-ble/app/LogCommand.hpp | 30 +++++ .../{Core/Inc/logger.h => app/commands.h} | 10 +- 10 files changed, 170 insertions(+), 131 deletions(-) delete mode 100644 nucleo-wb55-dongle-ble/Core/Src/logger.c create mode 100644 nucleo-wb55-dongle-ble/app/Command.cpp create mode 100644 nucleo-wb55-dongle-ble/app/Command.hpp create mode 100644 nucleo-wb55-dongle-ble/app/LogCommand.cpp create mode 100644 nucleo-wb55-dongle-ble/app/LogCommand.hpp rename nucleo-wb55-dongle-ble/{Core/Inc/logger.h => app/commands.h} (70%) diff --git a/backend/command.py b/backend/command.py index 7c68082..630aaf9 100644 --- a/backend/command.py +++ b/backend/command.py @@ -53,6 +53,7 @@ def worker_process( format="[%(asctime)s] [%(name)-20s] [%(levelname)-8s] --- %(message)s", ) logger = logging.getLogger("worker_process") + logger.setLevel(logging.INFO) initialize_container(config) container = get_container() @@ -62,7 +63,7 @@ def worker_process( while True: logger.debug(f"Ping {counter} process_id={os.getpid()}") counter += 1 - time.sleep(0.1) + time.sleep(0.01) receive_and_log( serial=serial, header_size=container.config.header_size(), @@ -150,7 +151,7 @@ def receive_and_log( if command_id == CommandId.command_log: command = LogCommand( - data=bytes_read[header_size : header_size + data_length], + data=payload, ) command.execute() else: diff --git a/backend/defaults/config.yml b/backend/defaults/config.yml index 8158c3d..7ac4701 100644 --- a/backend/defaults/config.yml +++ b/backend/defaults/config.yml @@ -1,3 +1,4 @@ -device_id: /dev/tty.usbmodem2067368F32521 +# device_id: /dev/tty.usbmodem2067368F32521 +device_id: /dev/tty.usbmodem207E3283544E1 baudrate: 115200 header_size: 4 diff --git a/nucleo-wb55-dongle-ble/.project b/nucleo-wb55-dongle-ble/.project index 37e80db..8c776c7 100644 --- a/nucleo-wb55-dongle-ble/.project +++ b/nucleo-wb55-dongle-ble/.project @@ -28,5 +28,6 @@ com.st.stm32cube.ide.mcu.MCURootProjectNature org.eclipse.cdt.managedbuilder.core.managedBuildNature org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + org.eclipse.cdt.core.ccnature diff --git a/nucleo-wb55-dongle-ble/Core/Src/logger.c b/nucleo-wb55-dongle-ble/Core/Src/logger.c deleted file mode 100644 index 49e9c17..0000000 --- a/nucleo-wb55-dongle-ble/Core/Src/logger.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * logger.c - * - * Created on: Jul 4, 2021 - * Author: Andreas Berthoud - */ - -#include -#include - -#include "usbd_cdc_if.h" -#include "logger.h" - -#define BUFFER_MAX 512 -#define COMMAND_HEADER_SIZE 4 -#define LOG_COMMAND_HEADER_SIZE 1 -#define HEADER_SIZE (COMMAND_HEADER_SIZE + LOG_COMMAND_HEADER_SIZE) -#define MESSAGE_BUFFER_START (COMMAND_HEADER_SIZE + LOG_COMMAND_HEADER_SIZE) -#define MAX_LOG_MESSAGE_SIZE (BUFFER_MAX - COMMAND_HEADER_SIZE - LOG_COMMAND_HEADER_SIZE - 1) - -#define min(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a < _b ? _a : _b; }) - - -typedef struct -{ - uint8_t LOG_LEVEL_DEBUG; - uint8_t LOG_LEVEL_INFO; - uint8_t LOG_LEVEL_WARNING; - uint8_t LOG_LEVEL_ERROR; -} LOGGER_SEVERTITY_T; - -LOGGER_SEVERTITY_T LOGGER_SEVERTITY = { - .LOG_LEVEL_DEBUG = 10, - .LOG_LEVEL_INFO = 20, - .LOG_LEVEL_WARNING = 30, - .LOG_LEVEL_ERROR = 40, -}; - -/* - * A command is 4 bytes long - * - * | 1 byte | 2 bytes | 1 byte | N bytes | 1 byte | - * | command ID | length N | reserved | data | stop byte | - * */ - -/* - * command length = N - * | 1 byte | N-1 bytes | - * | severtity | message | - * */ -char buffer[BUFFER_MAX]; - -void log_message(uint8_t severtiy) { - int message_length = min(strlen(buffer + MESSAGE_BUFFER_START), MAX_LOG_MESSAGE_SIZE); - int command_length = message_length + 1; - buffer[0] = 0xFF; // log command - buffer[1] = (command_length & 0x0000FF00) >> 8; - buffer[2] = command_length & 0x000000FF; - buffer[3] = 0x00; // reserved - buffer[4] = severtiy; - buffer[5 + message_length] = 0xFF; // stop byte - CDC_Transmit_FS((uint8_t*)buffer, (uint16_t)(HEADER_SIZE + message_length + 1)); -} - -void log_debug(const char * format, int nargs, ...) { - va_list args; - va_start(args, nargs); - vsnprintf( - buffer + MESSAGE_BUFFER_START, - MAX_LOG_MESSAGE_SIZE, - format, - args - ); - va_end(args); - log_message(LOGGER_SEVERTITY.LOG_LEVEL_DEBUG); -} - -void log_info(const char * format, int nargs, ...) { - va_list args; - va_start(args, nargs); - vsnprintf( - buffer + MESSAGE_BUFFER_START, - MAX_LOG_MESSAGE_SIZE, - format, - args - ); - va_end(args); - log_message(LOGGER_SEVERTITY.LOG_LEVEL_INFO); -} - -void log_warning(const char * format, int nargs, ...) { - va_list args; - va_start(args, nargs); - vsnprintf( - buffer + MESSAGE_BUFFER_START, - MAX_LOG_MESSAGE_SIZE, - format, - args - ); - va_end(args); - log_message(LOGGER_SEVERTITY.LOG_LEVEL_WARNING); -} - -void log_error(const char * format, int nargs, ...) { - va_list args; - va_start(args, nargs); - vsnprintf( - buffer + MESSAGE_BUFFER_START, - MAX_LOG_MESSAGE_SIZE, - format, - args - ); - va_end(args); - log_message(LOGGER_SEVERTITY.LOG_LEVEL_ERROR); -} diff --git a/nucleo-wb55-dongle-ble/Core/Src/main.c b/nucleo-wb55-dongle-ble/Core/Src/main.c index 5815246..a9a8dea 100644 --- a/nucleo-wb55-dongle-ble/Core/Src/main.c +++ b/nucleo-wb55-dongle-ble/Core/Src/main.c @@ -26,7 +26,7 @@ #include #include "usbd_cdc_if.h" -#include "logger.h" +#include "commands.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -105,10 +105,12 @@ int main(void) HAL_Delay(50); HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET); HAL_Delay(100); - log_debug("Counter value is %d", 1, counter_value++); - // log_info("Counter value is %d", 1, counter_value++); - // log_warning("Counter value is %d", 1, counter_value++); - // log_error("Counter value is %d", 1, counter_value++); + counter_value++; + + log_debug("Counter value is %d", 1, counter_value); + // log_info("Counter value is %d", 1, counter_value); + // log_warning("Counter value is %d", 1, counter_value); + // log_error("Counter value is %d", 1, counter_value); /* USER CODE END WHILE */ diff --git a/nucleo-wb55-dongle-ble/app/Command.cpp b/nucleo-wb55-dongle-ble/app/Command.cpp new file mode 100644 index 0000000..2257545 --- /dev/null +++ b/nucleo-wb55-dongle-ble/app/Command.cpp @@ -0,0 +1,28 @@ +/* + * Command.cpp + * + * Created on: Jul 8, 2021 + * Author: Andreas Berthoud + */ + +#include "Command.hpp" +#include "string.h" +#include "usbd_cdc_if.h" + +Command::Command(CommandId id) : id(id) { + this->data[0] = id; + this->data[3] = 0x00; // reserved + this->payload_ptr = this->data + 4; + } + +void Command::send() { + uint16_t size = this->payload_length + 5; + this->data[size-1] = 0xff; + CDC_Transmit_FS(this->data, size); +} + +void Command::set_payload_length(uint16_t payload_length) { + this->payload_length = payload_length; + data[1] = (payload_length & 0xFF00) >> 8; + data[2] = payload_length & 0x00FF; +} diff --git a/nucleo-wb55-dongle-ble/app/Command.hpp b/nucleo-wb55-dongle-ble/app/Command.hpp new file mode 100644 index 0000000..df2da4e --- /dev/null +++ b/nucleo-wb55-dongle-ble/app/Command.hpp @@ -0,0 +1,39 @@ +/* + * Command.hpp + * + * Created on: Jul 8, 2021 + * Author: Andreas Berthoud + */ + +#ifndef SRC_COMMAND_H_ +#define SRC_COMMAND_H_ + +#include + +typedef enum : uint8_t { + COMMAND_NONE = 0, + COMMAND_LOG = 0xff, +} CommandId; + + +class Command { +public: + CommandId id; + + Command(CommandId command_id); + + virtual void send(); + +protected: + uint8_t data[512]; + uint8_t * payload_ptr; + const int max_payload_length = 507; + + void set_payload_length(uint16_t length); + +private: + uint16_t payload_length; + +}; + +#endif /* SRC_COMMAND_H_ */ diff --git a/nucleo-wb55-dongle-ble/app/LogCommand.cpp b/nucleo-wb55-dongle-ble/app/LogCommand.cpp new file mode 100644 index 0000000..a4d2c22 --- /dev/null +++ b/nucleo-wb55-dongle-ble/app/LogCommand.cpp @@ -0,0 +1,55 @@ +/* + * LogCommand.cpp + * + * Created on: Jul 8, 2021 + * Author: Andreas Berthoud + */ + +#include +#include +#include + +#include "LogCommand.hpp" + + +void log_debug(const char * format, int nargs, ...) { + va_list args; + va_start(args, nargs); + LogCommand command = LogCommand(format, args, LOG_LEVEL_DEBUG); + va_end(args); + + command.send(); +} + +void log_info(const char * format, int nargs, ...) { + va_list args; + va_start(args, nargs); + LogCommand command = LogCommand(format, args, LOG_LEVEL_INFO); + va_end(args); + + command.send(); +} + +void log_warning(const char * format, int nargs, ...) { + va_list args; + va_start(args, nargs); + LogCommand command = LogCommand(format, args, LOG_LEVEL_WARNING); + va_end(args); + + command.send(); +} + +void log_error(const char * format, int nargs, ...) { + va_list args; + va_start(args, nargs); + LogCommand command = LogCommand(format, args, LOG_LEVEL_ERROR); + va_end(args); + + command.send(); +} + +LogCommand::LogCommand(const char * format, va_list args, LoggingLevel logging_level) : Command(COMMAND_LOG) { + *this->payload_ptr = logging_level; + vsnprintf((char *)this->payload_ptr + 1, this->max_payload_length - 1, format, args); + this->set_payload_length(strlen((char *)this->payload_ptr) + 1); // strlen + log level +} diff --git a/nucleo-wb55-dongle-ble/app/LogCommand.hpp b/nucleo-wb55-dongle-ble/app/LogCommand.hpp new file mode 100644 index 0000000..20c37c8 --- /dev/null +++ b/nucleo-wb55-dongle-ble/app/LogCommand.hpp @@ -0,0 +1,30 @@ +/* + * LogCommand.h + * + * Created on: Jul 8, 2021 + * Author: Andreas Berthoud + */ + +#ifndef LOGCOMMAND_H_ +#define LOGCOMMAND_H_ + +#include "Command.hpp" + +extern "C" void log_debug(const char * format, int nargs, ...); +extern "C" void log_info(const char * format, int nargs, ...); +extern "C" void log_warning(const char * format, int nargs, ...); +extern "C" void log_error(const char * format, int nargs, ...); + +typedef enum : uint8_t{ + LOG_LEVEL_DEBUG = 10, + LOG_LEVEL_INFO = 20, + LOG_LEVEL_WARNING = 30, + LOG_LEVEL_ERROR = 40, +} LoggingLevel; + +class LogCommand : public Command { +public: + LogCommand(const char * format, va_list args, LoggingLevel logging_level); +}; + +#endif /* LOGCOMMAND_H_ */ diff --git a/nucleo-wb55-dongle-ble/Core/Inc/logger.h b/nucleo-wb55-dongle-ble/app/commands.h similarity index 70% rename from nucleo-wb55-dongle-ble/Core/Inc/logger.h rename to nucleo-wb55-dongle-ble/app/commands.h index 1c58785..45f83a5 100644 --- a/nucleo-wb55-dongle-ble/Core/Inc/logger.h +++ b/nucleo-wb55-dongle-ble/app/commands.h @@ -1,16 +1,16 @@ /* - * logger.h + * commands.h * - * Created on: Jul 4, 2021 + * Created on: Jul 8, 2021 * Author: Andreas Berthoud */ -#ifndef INC_LOGGER_H_ -#define INC_LOGGER_H_ +#ifndef COMMANDS_H_ +#define COMMANDS_H_ void log_debug(const char * format, int nargs, ...); void log_info(const char * format, int nargs, ...); void log_warning(const char * format, int nargs, ...); void log_error(const char * format, int nargs, ...); -#endif /* INC_LOGGER_H_ */ +#endif /* COMMANDS_H_ */