Browse Source

ble: Add Command class for logging

ble
Andreas Berthoud 4 years ago
parent
commit
eac14ea3ef
  1. 5
      backend/command.py
  2. 3
      backend/defaults/config.yml
  3. 1
      nucleo-wb55-dongle-ble/.project
  4. 118
      nucleo-wb55-dongle-ble/Core/Src/logger.c
  5. 12
      nucleo-wb55-dongle-ble/Core/Src/main.c
  6. 28
      nucleo-wb55-dongle-ble/app/Command.cpp
  7. 39
      nucleo-wb55-dongle-ble/app/Command.hpp
  8. 55
      nucleo-wb55-dongle-ble/app/LogCommand.cpp
  9. 30
      nucleo-wb55-dongle-ble/app/LogCommand.hpp
  10. 10
      nucleo-wb55-dongle-ble/app/commands.h

5
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:

3
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

1
nucleo-wb55-dongle-ble/.project

@ -28,5 +28,6 @@
<nature>com.st.stm32cube.ide.mcu.MCURootProjectNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
</natures>
</projectDescription>

118
nucleo-wb55-dongle-ble/Core/Src/logger.c

@ -1,118 +0,0 @@
/*
* logger.c
*
* Created on: Jul 4, 2021
* Author: Andreas Berthoud
*/
#include <stdarg.h>
#include <string.h>
#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);
}

12
nucleo-wb55-dongle-ble/Core/Src/main.c

@ -26,7 +26,7 @@
#include <stdio.h>
#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 */

28
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;
}

39
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 <stdint.h>
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_ */

55
nucleo-wb55-dongle-ble/app/LogCommand.cpp

@ -0,0 +1,55 @@
/*
* LogCommand.cpp
*
* Created on: Jul 8, 2021
* Author: Andreas Berthoud
*/
#include <stdarg.h>
#include <string.h>
#include <string>
#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
}

30
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_ */

10
nucleo-wb55-dongle-ble/Core/Inc/logger.h → 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_ */
Loading…
Cancel
Save