10 changed files with 170 additions and 131 deletions
@ -1,3 +1,4 @@ |
|||||
device_id: /dev/tty.usbmodem2067368F32521 |
# device_id: /dev/tty.usbmodem2067368F32521 |
||||
|
device_id: /dev/tty.usbmodem207E3283544E1 |
||||
baudrate: 115200 |
baudrate: 115200 |
||||
header_size: 4 |
header_size: 4 |
||||
|
|||||
@ -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); |
|
||||
} |
|
||||
@ -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; |
||||
|
} |
||||
@ -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_ */ |
||||
@ -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
|
||||
|
} |
||||
@ -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_ */ |
||||
@ -1,16 +1,16 @@ |
|||||
/*
|
/*
|
||||
* logger.h |
* commands.h |
||||
* |
* |
||||
* Created on: Jul 4, 2021 |
* Created on: Jul 8, 2021 |
||||
* Author: Andreas Berthoud |
* Author: Andreas Berthoud |
||||
*/ |
*/ |
||||
|
|
||||
#ifndef INC_LOGGER_H_ |
#ifndef COMMANDS_H_ |
||||
#define INC_LOGGER_H_ |
#define COMMANDS_H_ |
||||
|
|
||||
void log_debug(const char * format, int nargs, ...); |
void log_debug(const char * format, int nargs, ...); |
||||
void log_info(const char * format, int nargs, ...); |
void log_info(const char * format, int nargs, ...); |
||||
void log_warning(const char * format, int nargs, ...); |
void log_warning(const char * format, int nargs, ...); |
||||
void log_error(const char * format, int nargs, ...); |
void log_error(const char * format, int nargs, ...); |
||||
|
|
||||
#endif /* INC_LOGGER_H_ */ |
#endif /* COMMANDS_H_ */ |
||||
Loading…
Reference in new issue