From 0fb17545c38f069874eedf584ede8d960ea2af76 Mon Sep 17 00:00:00 2001 From: Andreas Berthoud Date: Sun, 18 Jul 2021 23:30:24 +0200 Subject: [PATCH] ble-dongle: Split commands into shared and non-shared --- nucleo-wb55-dongle-ble/app/Command.hpp | 40 ------------------- nucleo-wb55-dongle-ble/app/commands.hpp | 16 -------- .../{app => commands}/GPCommand.cpp | 1 + .../{app => commands}/GPCommand.hpp | 0 nucleo-wb55-dongle-ble/commands/dispatch.cpp | 32 +++++++++++++++ nucleo-wb55-dongle-ble/commands/dispatch.hpp | 15 +++++++ .../shared_commands/Command.hpp | 25 ++++++++++++ .../HeartbeatCommand.cpp | 0 .../HeartbeatCommand.hpp | 0 .../{app => shared_commands}/LedCommand.cpp | 0 .../{app => shared_commands}/LedCommand.hpp | 0 .../{app => shared_commands}/LogCommand.cpp | 0 .../{app => shared_commands}/LogCommand.hpp | 1 + .../{app => shared_commands}/Notification.cpp | 0 .../{app => shared_commands}/Notification.hpp | 0 .../{app => shared_commands}/Request.cpp | 0 .../{app => shared_commands}/Request.hpp | 0 .../{app => shared_commands}/Response.cpp | 0 .../{app => shared_commands}/Response.hpp | 0 .../command_interpreter.c | 0 .../{app => shared_commands}/commands.h | 6 +-- .../shared_commands/commands.hpp | 24 +++++++++++ .../execute.cpp} | 33 ++++----------- .../shared_commands/execute.hpp | 13 ++++++ 24 files changed, 121 insertions(+), 85 deletions(-) delete mode 100644 nucleo-wb55-dongle-ble/app/Command.hpp delete mode 100644 nucleo-wb55-dongle-ble/app/commands.hpp rename nucleo-wb55-dongle-ble/{app => commands}/GPCommand.cpp (99%) rename nucleo-wb55-dongle-ble/{app => commands}/GPCommand.hpp (100%) create mode 100644 nucleo-wb55-dongle-ble/commands/dispatch.cpp create mode 100644 nucleo-wb55-dongle-ble/commands/dispatch.hpp create mode 100644 nucleo-wb55-dongle-ble/shared_commands/Command.hpp rename nucleo-wb55-dongle-ble/{app => shared_commands}/HeartbeatCommand.cpp (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/HeartbeatCommand.hpp (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/LedCommand.cpp (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/LedCommand.hpp (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/LogCommand.cpp (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/LogCommand.hpp (98%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/Notification.cpp (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/Notification.hpp (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/Request.cpp (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/Request.hpp (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/Response.cpp (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/Response.hpp (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/command_interpreter.c (100%) rename nucleo-wb55-dongle-ble/{app => shared_commands}/commands.h (100%) create mode 100644 nucleo-wb55-dongle-ble/shared_commands/commands.hpp rename nucleo-wb55-dongle-ble/{app/Command.cpp => shared_commands/execute.cpp} (50%) create mode 100644 nucleo-wb55-dongle-ble/shared_commands/execute.hpp diff --git a/nucleo-wb55-dongle-ble/app/Command.hpp b/nucleo-wb55-dongle-ble/app/Command.hpp deleted file mode 100644 index 95d4a0e..0000000 --- a/nucleo-wb55-dongle-ble/app/Command.hpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Command.hpp - * - * Created on: Jul 8, 2021 - * Author: Andreas Berthoud - */ - -#ifndef SRC_COMMAND_H_ -#define SRC_COMMAND_H_ - -#include - -extern "C" void pop_and_execute_commands(); -extern "C" void handle_received_command(uint8_t command_id, uint8_t * payload_ptr, uint16_t length); - -typedef enum : uint8_t { - COMMAND_NONE = 0, - COMMAND_LOG = 0x1, - COMMAND_HEARTBEAT_REQUEST = 0x2, - COMMAND_HEARTBEAT_RESPONSE = 0x3, - COMMAND_LED_REQUEST = 0x4, - COMMAND_LED_RESPONSE = 0x5, - COMMAND_GP_REQUEST = 0x6, - COMMAND_GP_RESPONSE = 0x7, -} CommandId; - - -class Command { -public: - CommandId id; - - virtual ~Command() {}; - - virtual CommandId get_command_id() = 0; - virtual bool execute() = 0; -}; - -void push_command(Command * command); - -#endif /* SRC_COMMAND_H_ */ diff --git a/nucleo-wb55-dongle-ble/app/commands.hpp b/nucleo-wb55-dongle-ble/app/commands.hpp deleted file mode 100644 index 0d6b19b..0000000 --- a/nucleo-wb55-dongle-ble/app/commands.hpp +++ /dev/null @@ -1,16 +0,0 @@ -/* - * commands.hpp - * - * Created on: Jul 13, 2021 - * Author: Andreas Berthoud - */ - -#ifndef COMMANDS_HPP_ -#define COMMANDS_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, ...); - -#endif /* COMMANDS_HPP_ */ diff --git a/nucleo-wb55-dongle-ble/app/GPCommand.cpp b/nucleo-wb55-dongle-ble/commands/GPCommand.cpp similarity index 99% rename from nucleo-wb55-dongle-ble/app/GPCommand.cpp rename to nucleo-wb55-dongle-ble/commands/GPCommand.cpp index a65c98a..9e227f0 100644 --- a/nucleo-wb55-dongle-ble/app/GPCommand.cpp +++ b/nucleo-wb55-dongle-ble/commands/GPCommand.cpp @@ -6,6 +6,7 @@ */ #include "GPCommand.hpp" + #include "app_ble.h" GPResponse::GPResponse(uint16_t response_identifier, bool was_successful) diff --git a/nucleo-wb55-dongle-ble/app/GPCommand.hpp b/nucleo-wb55-dongle-ble/commands/GPCommand.hpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/GPCommand.hpp rename to nucleo-wb55-dongle-ble/commands/GPCommand.hpp diff --git a/nucleo-wb55-dongle-ble/commands/dispatch.cpp b/nucleo-wb55-dongle-ble/commands/dispatch.cpp new file mode 100644 index 0000000..b522040 --- /dev/null +++ b/nucleo-wb55-dongle-ble/commands/dispatch.cpp @@ -0,0 +1,32 @@ +/* + * dispatch.cpp + * + * Created on: 18 Jul 2021 + * Author: Andreas Berthoud + */ + +#include "dispatch.hpp" + +#include "GPCommand.hpp" +#include "HeartbeatCommand.hpp" +#include "LedCommand.hpp" + +void handle_received_command(uint8_t command_id, uint8_t * payload_ptr, uint16_t size) { + + switch (command_id) + { + case COMMAND_HEARTBEAT_REQUEST: + push_command(new HeartbeatRequest(payload_ptr, size)); + break; + case COMMAND_LED_REQUEST: + push_command(new LedRequest(payload_ptr, size)); + break; + case COMMAND_GP_REQUEST: + push_command(new GPRequest(payload_ptr, size)); + break; + + default: + break; + } + +} diff --git a/nucleo-wb55-dongle-ble/commands/dispatch.hpp b/nucleo-wb55-dongle-ble/commands/dispatch.hpp new file mode 100644 index 0000000..4064550 --- /dev/null +++ b/nucleo-wb55-dongle-ble/commands/dispatch.hpp @@ -0,0 +1,15 @@ +/* + * dispatch.hpp + * + * Created on: 18 Jul 2021 + * Author: Andreas Berthoud + */ + +#ifndef DISPATCH_HPP_ +#define DISPATCH_HPP_ + +#include + +extern "C" void handle_received_command(uint8_t command_id, uint8_t * payload_ptr, uint16_t length); + +#endif /* DISPATCH_HPP_ */ diff --git a/nucleo-wb55-dongle-ble/shared_commands/Command.hpp b/nucleo-wb55-dongle-ble/shared_commands/Command.hpp new file mode 100644 index 0000000..3705d9a --- /dev/null +++ b/nucleo-wb55-dongle-ble/shared_commands/Command.hpp @@ -0,0 +1,25 @@ +/* + * Command.hpp + * + * Created on: Jul 8, 2021 + * Author: Andreas Berthoud + */ + +#ifndef SRC_COMMAND_H_ +#define SRC_COMMAND_H_ + +#include "commands.hpp" + +class Command { +public: + CommandId id; + + virtual ~Command() {}; + + virtual CommandId get_command_id() = 0; + virtual bool execute() = 0; +}; + +void push_command(Command * command); + +#endif /* SRC_COMMAND_H_ */ diff --git a/nucleo-wb55-dongle-ble/app/HeartbeatCommand.cpp b/nucleo-wb55-dongle-ble/shared_commands/HeartbeatCommand.cpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/HeartbeatCommand.cpp rename to nucleo-wb55-dongle-ble/shared_commands/HeartbeatCommand.cpp diff --git a/nucleo-wb55-dongle-ble/app/HeartbeatCommand.hpp b/nucleo-wb55-dongle-ble/shared_commands/HeartbeatCommand.hpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/HeartbeatCommand.hpp rename to nucleo-wb55-dongle-ble/shared_commands/HeartbeatCommand.hpp diff --git a/nucleo-wb55-dongle-ble/app/LedCommand.cpp b/nucleo-wb55-dongle-ble/shared_commands/LedCommand.cpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/LedCommand.cpp rename to nucleo-wb55-dongle-ble/shared_commands/LedCommand.cpp diff --git a/nucleo-wb55-dongle-ble/app/LedCommand.hpp b/nucleo-wb55-dongle-ble/shared_commands/LedCommand.hpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/LedCommand.hpp rename to nucleo-wb55-dongle-ble/shared_commands/LedCommand.hpp diff --git a/nucleo-wb55-dongle-ble/app/LogCommand.cpp b/nucleo-wb55-dongle-ble/shared_commands/LogCommand.cpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/LogCommand.cpp rename to nucleo-wb55-dongle-ble/shared_commands/LogCommand.cpp diff --git a/nucleo-wb55-dongle-ble/app/LogCommand.hpp b/nucleo-wb55-dongle-ble/shared_commands/LogCommand.hpp similarity index 98% rename from nucleo-wb55-dongle-ble/app/LogCommand.hpp rename to nucleo-wb55-dongle-ble/shared_commands/LogCommand.hpp index 993d3d8..2ab730e 100644 --- a/nucleo-wb55-dongle-ble/app/LogCommand.hpp +++ b/nucleo-wb55-dongle-ble/shared_commands/LogCommand.hpp @@ -9,6 +9,7 @@ #define LOGCOMMAND_H_ #include "Notification.hpp" +#include extern "C" void log_debug(const char * logger_name, const char * format, int nargs, ...); extern "C" void log_info(const char * logger_name, const char * format, int nargs, ...); diff --git a/nucleo-wb55-dongle-ble/app/Notification.cpp b/nucleo-wb55-dongle-ble/shared_commands/Notification.cpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/Notification.cpp rename to nucleo-wb55-dongle-ble/shared_commands/Notification.cpp diff --git a/nucleo-wb55-dongle-ble/app/Notification.hpp b/nucleo-wb55-dongle-ble/shared_commands/Notification.hpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/Notification.hpp rename to nucleo-wb55-dongle-ble/shared_commands/Notification.hpp diff --git a/nucleo-wb55-dongle-ble/app/Request.cpp b/nucleo-wb55-dongle-ble/shared_commands/Request.cpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/Request.cpp rename to nucleo-wb55-dongle-ble/shared_commands/Request.cpp diff --git a/nucleo-wb55-dongle-ble/app/Request.hpp b/nucleo-wb55-dongle-ble/shared_commands/Request.hpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/Request.hpp rename to nucleo-wb55-dongle-ble/shared_commands/Request.hpp diff --git a/nucleo-wb55-dongle-ble/app/Response.cpp b/nucleo-wb55-dongle-ble/shared_commands/Response.cpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/Response.cpp rename to nucleo-wb55-dongle-ble/shared_commands/Response.cpp diff --git a/nucleo-wb55-dongle-ble/app/Response.hpp b/nucleo-wb55-dongle-ble/shared_commands/Response.hpp similarity index 100% rename from nucleo-wb55-dongle-ble/app/Response.hpp rename to nucleo-wb55-dongle-ble/shared_commands/Response.hpp diff --git a/nucleo-wb55-dongle-ble/app/command_interpreter.c b/nucleo-wb55-dongle-ble/shared_commands/command_interpreter.c similarity index 100% rename from nucleo-wb55-dongle-ble/app/command_interpreter.c rename to nucleo-wb55-dongle-ble/shared_commands/command_interpreter.c diff --git a/nucleo-wb55-dongle-ble/app/commands.h b/nucleo-wb55-dongle-ble/shared_commands/commands.h similarity index 100% rename from nucleo-wb55-dongle-ble/app/commands.h rename to nucleo-wb55-dongle-ble/shared_commands/commands.h index 5a60992..6831733 100644 --- a/nucleo-wb55-dongle-ble/app/commands.h +++ b/nucleo-wb55-dongle-ble/shared_commands/commands.h @@ -8,12 +8,12 @@ #ifndef COMMANDS_H_ #define COMMANDS_H_ -void pop_and_execute_commands(); -void handle_received_command(uint8_t command_id, uint8_t * payload_ptr, uint16_t length); - void log_debug(const char * logger_name, const char * format, int nargs, ...); void log_info(const char * logger_name, const char * format, int nargs, ...); void log_warning(const char * logger_name, const char * format, int nargs, ...); void log_error(const char * logger_name, const char * format, int nargs, ...); +void pop_and_execute_commands(); +void handle_received_command(uint8_t command_id, uint8_t * payload_ptr, uint16_t length); + #endif /* COMMANDS_H_ */ diff --git a/nucleo-wb55-dongle-ble/shared_commands/commands.hpp b/nucleo-wb55-dongle-ble/shared_commands/commands.hpp new file mode 100644 index 0000000..b26fcf3 --- /dev/null +++ b/nucleo-wb55-dongle-ble/shared_commands/commands.hpp @@ -0,0 +1,24 @@ +/* + * commands.hpp + * + * Created on: Jul 13, 2021 + * Author: Andreas Berthoud + */ + +#ifndef COMMANDS_HPP_ +#define COMMANDS_HPP_ + +#include + +typedef enum : uint8_t { + COMMAND_NONE = 0, + COMMAND_LOG = 0x1, + COMMAND_HEARTBEAT_REQUEST = 0x2, + COMMAND_HEARTBEAT_RESPONSE = 0x3, + COMMAND_LED_REQUEST = 0x4, + COMMAND_LED_RESPONSE = 0x5, + COMMAND_GP_REQUEST = 0x6, + COMMAND_GP_RESPONSE = 0x7, +} CommandId; + +#endif /* COMMANDS_HPP_ */ diff --git a/nucleo-wb55-dongle-ble/app/Command.cpp b/nucleo-wb55-dongle-ble/shared_commands/execute.cpp similarity index 50% rename from nucleo-wb55-dongle-ble/app/Command.cpp rename to nucleo-wb55-dongle-ble/shared_commands/execute.cpp index a00d1f8..b42920b 100644 --- a/nucleo-wb55-dongle-ble/app/Command.cpp +++ b/nucleo-wb55-dongle-ble/shared_commands/execute.cpp @@ -1,19 +1,17 @@ /* - * Command.cpp + * execute.cpp * - * Created on: Jul 8, 2021 + * Created on: 18 Jul 2021 * Author: Andreas Berthoud */ + #include #include #include "Command.hpp" -#include "string.h" +#include "execute.hpp" #include "stm32wbxx_hal.h" -#include "commands.hpp" -#include "HeartbeatCommand.hpp" -#include "LedCommand.hpp" -#include "GPCommand.hpp" +#include "LogCommand.hpp" std::queue command_queue; @@ -28,29 +26,12 @@ void pop_and_execute_commands() { bool was_successful = command->execute(); HAL_Delay(5); // this delay is required. Otherwise, the command were not sent somehow... if (!was_successful) { - log_error("Execution of command with ID %n was not successful!", 1, command->id); + log_error("pop_and_execute_commands", "Execution of command with ID %n was not successful!", 1, command->id); } delete command; command_queue.pop(); } } -void handle_received_command(uint8_t command_id, uint8_t * payload_ptr, uint16_t size) { - - switch (command_id) - { - case COMMAND_HEARTBEAT_REQUEST: - push_command(new HeartbeatRequest(payload_ptr, size)); - break; - case COMMAND_LED_REQUEST: - push_command(new LedRequest(payload_ptr, size)); - break; - case COMMAND_GP_REQUEST: - push_command(new GPRequest(payload_ptr, size)); - break; - - default: - break; - } -} + diff --git a/nucleo-wb55-dongle-ble/shared_commands/execute.hpp b/nucleo-wb55-dongle-ble/shared_commands/execute.hpp new file mode 100644 index 0000000..8dfe412 --- /dev/null +++ b/nucleo-wb55-dongle-ble/shared_commands/execute.hpp @@ -0,0 +1,13 @@ +/* + * execute.hpp + * + * Created on: 18 Jul 2021 + * Author: Andreas Berthoud + */ + +#ifndef EXECUTE_HPP_ +#define EXECUTE_HPP_ + +extern "C" void pop_and_execute_commands(); + +#endif /* EXECUTE_HPP_ */