C++ conversions (#111)
* Remove version from artifact name * Target C++ 20 and higher * Use cpp string * Better crash implementation * String utils in cpp style * Replace parameter methods with start() method * MutexType to Mutex::Type * Kernel c to cpp style * Cleanup event flag * More cpp conversions * Test fixes * Updated ideas docs
This commit is contained in:
committed by
GitHub
parent
d52fe52d96
commit
42e843b463
@@ -23,7 +23,7 @@ private:
|
||||
} Type;
|
||||
|
||||
typedef struct {
|
||||
const char* key;
|
||||
std::string key;
|
||||
Type type;
|
||||
union {
|
||||
bool value_bool;
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include "Check.h"
|
||||
|
||||
#include "CoreDefines.h"
|
||||
#include "Log.h"
|
||||
#include "RtosCompatTask.h"
|
||||
|
||||
#define TAG "kernel"
|
||||
|
||||
static void tt_print_memory_info() {
|
||||
static void logMemoryInfo() {
|
||||
#ifdef ESP_PLATFORM
|
||||
TT_LOG_E(TAG, "default caps:");
|
||||
TT_LOG_E(TAG, " total: %u", heap_caps_get_total_size(MALLOC_CAP_DEFAULT));
|
||||
@@ -19,19 +18,23 @@ static void tt_print_memory_info() {
|
||||
#endif
|
||||
}
|
||||
|
||||
static void tt_print_task_info() {
|
||||
static void logTaskInfo() {
|
||||
const char* name = pcTaskGetName(nullptr);
|
||||
const char* safe_name = name ? name : "main";
|
||||
TT_LOG_E(TAG, "Task: %s", safe_name);
|
||||
TT_LOG_E(TAG, "Stack watermark: %u", uxTaskGetStackHighWaterMark(NULL) * 4);
|
||||
}
|
||||
|
||||
TT_NORETURN void tt_crash_implementation() {
|
||||
tt_print_task_info();
|
||||
tt_print_memory_info();
|
||||
namespace tt {
|
||||
|
||||
TT_NORETURN void _crash() {
|
||||
logTaskInfo();
|
||||
logMemoryInfo();
|
||||
// TODO: Add breakpoint when debugger is attached.
|
||||
#ifdef ESP_TARGET
|
||||
esp_system_abort("System halted. Connect debugger for more info.");
|
||||
#endif
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,17 +15,29 @@
|
||||
|
||||
#include "Log.h"
|
||||
#include <cassert>
|
||||
#include "CoreDefines.h"
|
||||
|
||||
#define TT_NORETURN [[noreturn]]
|
||||
|
||||
/** Crash system */
|
||||
TT_NORETURN void tt_crash_implementation();
|
||||
namespace tt {
|
||||
/**
|
||||
* Don't call this directly. Use tt_crash() as it will trace info.
|
||||
*/
|
||||
TT_NORETURN void _crash();
|
||||
}
|
||||
|
||||
/** Crash system with message. */
|
||||
#define tt_crash(message) \
|
||||
do { \
|
||||
TT_LOG_E("crash", "%s\n\tat %s:%d", ((message) ? (message) : ""), __FILE__, __LINE__); \
|
||||
tt_crash_implementation(); \
|
||||
#define tt_crash(...) TT_ARG_CAT(_tt_crash,TT_ARGCOUNT(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
#define _tt_crash0() do { \
|
||||
TT_LOG_E("crash", "at %s:%d", __FILE__, __LINE__); \
|
||||
tt::_crash(); \
|
||||
} while (0)
|
||||
|
||||
#define _tt_crash1(message) do { \
|
||||
TT_LOG_E("crash", "%s\n\tat %s:%d", message, __FILE__, __LINE__); \
|
||||
tt::_crash(); \
|
||||
} while (0)
|
||||
|
||||
/** Halt system
|
||||
@@ -53,7 +65,7 @@ TT_NORETURN void tt_crash_implementation();
|
||||
* @param optional message (const char*)
|
||||
*/
|
||||
|
||||
#define tt_check(x, ...) if (!(x)) { TT_LOG_E("check", "Failed: %s", #x); tt_crash_implementation(); }
|
||||
#define tt_check(x, ...) if (!(x)) { TT_LOG_E("check", "Failed: %s", #x); tt::_crash(); }
|
||||
|
||||
/** Only in debug build: Assert condition and crash if assert failed */
|
||||
#ifdef TT_DEBUG
|
||||
|
||||
@@ -31,3 +31,19 @@
|
||||
#define TT_IS_ISR() (TT_IS_IRQ_MODE())
|
||||
|
||||
#define TT_CHECK_RETURN __attribute__((__warn_unused_result__))
|
||||
|
||||
// region Variable arguments support
|
||||
|
||||
// Adapted from https://stackoverflow.com/a/78848701/3848666
|
||||
#define TT_ARG_IGNORE(X)
|
||||
#define TT_ARG_CAT(X,Y) _TT_ARG_CAT(X,Y)
|
||||
#define _TT_ARG_CAT(X,Y) X ## Y
|
||||
#define TT_ARGCOUNT(...) _TT_ARGCOUNT ## __VA_OPT__(1(__VA_ARGS__) TT_ARG_IGNORE) (0)
|
||||
#define _TT_ARGCOUNT1(X,...) _TT_ARGCOUNT ## __VA_OPT__(2(__VA_ARGS__) TT_ARG_IGNORE) (1)
|
||||
#define _TT_ARGCOUNT2(X,...) _TT_ARGCOUNT ## __VA_OPT__(3(__VA_ARGS__) TT_ARG_IGNORE) (2)
|
||||
#define _TT_ARGCOUNT3(X,...) _TT_ARGCOUNT ## __VA_OPT__(4(__VA_ARGS__) TT_ARG_IGNORE) (3)
|
||||
#define _TT_ARGCOUNT4(X,...) _TT_ARGCOUNT ## __VA_OPT__(5(__VA_ARGS__) TT_ARG_IGNORE) (4)
|
||||
#define _TT_ARGCOUNT5(X,...) 5
|
||||
#define _TT_ARGCOUNT(X) X
|
||||
|
||||
// endregion
|
||||
@@ -7,7 +7,7 @@ namespace tt {
|
||||
#define BACKPRESSURE_WARNING_COUNT 100
|
||||
|
||||
Dispatcher::Dispatcher() :
|
||||
mutex(MutexTypeNormal)
|
||||
mutex(Mutex::TypeNormal)
|
||||
{}
|
||||
|
||||
Dispatcher::~Dispatcher() {
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
|
||||
namespace tt {
|
||||
|
||||
#define TT_API_LOCK_EVENT (1U << 0)
|
||||
|
||||
/**
|
||||
* Wrapper for FreeRTOS xEventGroup.
|
||||
*/
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
#include "MessageQueue.h"
|
||||
#include "Check.h"
|
||||
#include "Kernel.h"
|
||||
#include "kernel/Kernel.h"
|
||||
|
||||
namespace tt {
|
||||
|
||||
MessageQueue::MessageQueue(uint32_t msg_count, uint32_t msg_size) {
|
||||
tt_assert((kernel_is_irq() == 0U) && (msg_count > 0U) && (msg_size > 0U));
|
||||
tt_assert((kernel::isIrq() == 0U) && (msg_count > 0U) && (msg_size > 0U));
|
||||
queue_handle = xQueueCreate(msg_count, msg_size);
|
||||
tt_check(queue_handle);
|
||||
}
|
||||
|
||||
MessageQueue::~MessageQueue() {
|
||||
tt_assert(kernel_is_irq() == 0U);
|
||||
tt_assert(kernel::isIrq() == 0U);
|
||||
vQueueDelete(queue_handle);
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ TtStatus MessageQueue::put(const void* msg_ptr, uint32_t timeout) {
|
||||
|
||||
stat = TtStatusOk;
|
||||
|
||||
if (kernel_is_irq() != 0U) {
|
||||
if (kernel::isIrq() != 0U) {
|
||||
if ((queue_handle == nullptr) || (msg_ptr == nullptr) || (timeout != 0U)) {
|
||||
stat = TtStatusErrorParameter;
|
||||
} else {
|
||||
@@ -57,7 +57,7 @@ TtStatus MessageQueue::get(void* msg_ptr, uint32_t timeout_ticks) {
|
||||
|
||||
stat = TtStatusOk;
|
||||
|
||||
if (kernel_is_irq() != 0U) {
|
||||
if (kernel::isIrq() != 0U) {
|
||||
if ((queue_handle == nullptr) || (msg_ptr == nullptr) || (timeout_ticks != 0U)) {
|
||||
stat = TtStatusErrorParameter;
|
||||
} else {
|
||||
@@ -122,7 +122,7 @@ uint32_t MessageQueue::getCount() const {
|
||||
|
||||
if (queue_handle == nullptr) {
|
||||
count = 0U;
|
||||
} else if (kernel_is_irq() != 0U) {
|
||||
} else if (kernel::isIrq() != 0U) {
|
||||
count = uxQueueMessagesWaitingFromISR(queue_handle);
|
||||
} else {
|
||||
count = uxQueueMessagesWaiting(queue_handle);
|
||||
@@ -139,7 +139,7 @@ uint32_t MessageQueue::getSpace() const {
|
||||
|
||||
if (mq == nullptr) {
|
||||
space = 0U;
|
||||
} else if (kernel_is_irq() != 0U) {
|
||||
} else if (kernel::isIrq() != 0U) {
|
||||
isrm = taskENTER_CRITICAL_FROM_ISR();
|
||||
|
||||
/* space = pxQueue->uxLength - pxQueue->uxMessagesWaiting; */
|
||||
@@ -157,7 +157,7 @@ uint32_t MessageQueue::getSpace() const {
|
||||
TtStatus MessageQueue::reset() {
|
||||
TtStatus stat;
|
||||
|
||||
if (kernel_is_irq() != 0U) {
|
||||
if (kernel::isIrq() != 0U) {
|
||||
stat = TtStatusErrorISR;
|
||||
} else if (queue_handle == nullptr) {
|
||||
stat = TtStatusErrorParameter;
|
||||
|
||||
@@ -22,13 +22,13 @@ namespace tt {
|
||||
#define tt_mutex_info(mutex, text)
|
||||
#endif
|
||||
|
||||
Mutex::Mutex(MutexType type) : type(type) {
|
||||
Mutex::Mutex(Type type) : type(type) {
|
||||
tt_mutex_info(data, "alloc");
|
||||
switch (type) {
|
||||
case MutexTypeNormal:
|
||||
case TypeNormal:
|
||||
semaphore = xSemaphoreCreateMutex();
|
||||
break;
|
||||
case MutexTypeRecursive:
|
||||
case TypeRecursive:
|
||||
semaphore = xSemaphoreCreateRecursiveMutex();
|
||||
break;
|
||||
default:
|
||||
@@ -51,7 +51,7 @@ TtStatus Mutex::acquire(uint32_t timeout) const {
|
||||
tt_mutex_info(mutex, "acquire");
|
||||
|
||||
switch (type) {
|
||||
case MutexTypeNormal:
|
||||
case TypeNormal:
|
||||
if (xSemaphoreTake(semaphore, timeout) != pdPASS) {
|
||||
if (timeout != 0U) {
|
||||
return TtStatusErrorTimeout;
|
||||
@@ -62,7 +62,7 @@ TtStatus Mutex::acquire(uint32_t timeout) const {
|
||||
return TtStatusOk;
|
||||
}
|
||||
break;
|
||||
case MutexTypeRecursive:
|
||||
case TypeRecursive:
|
||||
if (xSemaphoreTakeRecursive(semaphore, timeout) != pdPASS) {
|
||||
if (timeout != 0U) {
|
||||
return TtStatusErrorTimeout;
|
||||
@@ -84,7 +84,7 @@ TtStatus Mutex::release() const {
|
||||
tt_mutex_info(mutex, "release");
|
||||
|
||||
switch (type) {
|
||||
case MutexTypeNormal: {
|
||||
case TypeNormal: {
|
||||
if (xSemaphoreGive(semaphore) != pdPASS) {
|
||||
return TtStatusErrorResource;
|
||||
} else {
|
||||
@@ -92,7 +92,7 @@ TtStatus Mutex::release() const {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case MutexTypeRecursive:
|
||||
case TypeRecursive:
|
||||
if (xSemaphoreGiveRecursive(semaphore) != pdPASS) {
|
||||
return TtStatusErrorResource;
|
||||
} else {
|
||||
@@ -115,7 +115,7 @@ std::unique_ptr<ScopedMutexUsage> Mutex::scoped() const {
|
||||
return std::move(std::make_unique<ScopedMutexUsage>(*this));
|
||||
}
|
||||
|
||||
Mutex* tt_mutex_alloc(MutexType type) {
|
||||
Mutex* tt_mutex_alloc(Mutex::Type type) {
|
||||
return new Mutex(type);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,21 +14,27 @@ namespace tt {
|
||||
|
||||
class ScopedMutexUsage;
|
||||
|
||||
typedef enum {
|
||||
MutexTypeNormal,
|
||||
MutexTypeRecursive,
|
||||
} MutexType;
|
||||
|
||||
/**
|
||||
* Wrapper for FreeRTOS xSemaphoreCreateMutex and xSemaphoreCreateRecursiveMutex
|
||||
* Can be used in IRQ mode (within ISR context)
|
||||
*/
|
||||
class Mutex {
|
||||
private:
|
||||
SemaphoreHandle_t semaphore;
|
||||
MutexType type;
|
||||
|
||||
public:
|
||||
explicit Mutex(MutexType type = MutexTypeNormal);
|
||||
|
||||
enum Type {
|
||||
TypeNormal,
|
||||
TypeRecursive,
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
SemaphoreHandle_t semaphore;
|
||||
Type type;
|
||||
|
||||
public:
|
||||
|
||||
explicit Mutex(Type type = TypeNormal);
|
||||
~Mutex();
|
||||
|
||||
TtStatus acquire(uint32_t timeout) const;
|
||||
@@ -68,7 +74,7 @@ public:
|
||||
*/
|
||||
|
||||
[[deprecated("use class")]]
|
||||
Mutex* tt_mutex_alloc(MutexType type);
|
||||
Mutex* tt_mutex_alloc(Mutex::Type type);
|
||||
|
||||
/** Free Mutex
|
||||
*
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace tt {
|
||||
namespace tt::string {
|
||||
|
||||
int string_find_last_index(const char* text, size_t from_index, char find) {
|
||||
int findLastIndex(const char* text, size_t from_index, char find) {
|
||||
for (size_t i = from_index; i >= 0; i--) {
|
||||
if (text[i] == find) {
|
||||
return (int)i;
|
||||
@@ -14,8 +14,8 @@ int string_find_last_index(const char* text, size_t from_index, char find) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool string_get_path_parent(const char* path, char* output) {
|
||||
int index = string_find_last_index(path, strlen(path) - 1, '/');
|
||||
bool getPathParent(const char* path, char* output) {
|
||||
int index = findLastIndex(path, strlen(path) - 1, '/');
|
||||
if (index == -1) {
|
||||
return false;
|
||||
} else if (index == 0) {
|
||||
@@ -29,7 +29,7 @@ bool string_get_path_parent(const char* path, char* output) {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> string_split(const std::string&input, const std::string&delimiter) {
|
||||
std::vector<std::string> split(const std::string&input, const std::string&delimiter) {
|
||||
size_t token_index = 0;
|
||||
size_t delimiter_index;
|
||||
const size_t delimiter_length = delimiter.length();
|
||||
@@ -50,7 +50,7 @@ std::vector<std::string> string_split(const std::string&input, const std::string
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string string_join(const std::vector<std::string>& input, const std::string& delimiter) {
|
||||
std::string join(const std::vector<std::string>& input, const std::string& delimiter) {
|
||||
std::stringstream stream;
|
||||
size_t size = input.size();
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace tt {
|
||||
namespace tt::string {
|
||||
|
||||
/**
|
||||
* Find the last occurrence of a character.
|
||||
@@ -13,7 +13,7 @@ namespace tt {
|
||||
* @param[in] find the character to search for
|
||||
* @return the index of the found character, or -1 if none found
|
||||
*/
|
||||
int string_find_last_index(const char* text, size_t from_index, char find);
|
||||
int findLastIndex(const char* text, size_t from_index, char find);
|
||||
|
||||
/**
|
||||
* Given a filesystem path as input, try and get the parent path.
|
||||
@@ -21,7 +21,7 @@ int string_find_last_index(const char* text, size_t from_index, char find);
|
||||
* @param[out] output an output buffer that is allocated to at least the size of "current"
|
||||
* @return true when successful
|
||||
*/
|
||||
bool string_get_path_parent(const char* path, char* output);
|
||||
bool getPathParent(const char* path, char* output);
|
||||
|
||||
/**
|
||||
* Splits the provided input into separate pieces with delimiter as separator text.
|
||||
@@ -30,7 +30,7 @@ bool string_get_path_parent(const char* path, char* output);
|
||||
* @param input the input to split up
|
||||
* @param delimiter a non-empty string to recognize as separator
|
||||
*/
|
||||
std::vector<std::string> string_split(const std::string& input, const std::string& delimiter);
|
||||
std::vector<std::string> split(const std::string& input, const std::string& delimiter);
|
||||
|
||||
/**
|
||||
* Join a set of tokens into a single string, given a delimiter (separator).
|
||||
@@ -40,6 +40,6 @@ std::vector<std::string> string_split(const std::string& input, const std::strin
|
||||
* @param input the tokens to join together
|
||||
* @param delimiter the separator to join with
|
||||
*/
|
||||
std::string string_join(const std::vector<std::string>& input, const std::string& delimiter);
|
||||
std::string join(const std::vector<std::string>& input, const std::string& delimiter);
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -6,7 +6,10 @@
|
||||
#include "CoreDefines.h"
|
||||
#include "CoreExtraDefines.h"
|
||||
#include "CoreTypes.h"
|
||||
#include "critical/Critical.h"
|
||||
#include "EventFlag.h"
|
||||
#include "Kernel.h"
|
||||
#include "kernel/Kernel.h"
|
||||
#include "kernel/critical/Critical.h"
|
||||
#include "kernel/Kernel.h"
|
||||
#include "Log.h"
|
||||
#include "Mutex.h"
|
||||
#include "Thread.h"
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "Check.h"
|
||||
#include "CoreDefines.h"
|
||||
#include "Kernel.h"
|
||||
#include "kernel/Kernel.h"
|
||||
#include "Log.h"
|
||||
|
||||
namespace tt {
|
||||
@@ -197,7 +197,7 @@ bool Thread::join() {
|
||||
// If your thread exited, but your app stuck here: some other thread uses
|
||||
// all cpu time, which delays kernel from releasing task handle
|
||||
while (data.taskHandle) {
|
||||
delay_ms(10);
|
||||
kernel::delayMillis(10);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <utility>
|
||||
#include "Check.h"
|
||||
#include "Kernel.h"
|
||||
#include "kernel/Kernel.h"
|
||||
#include "RtosCompat.h"
|
||||
|
||||
namespace tt {
|
||||
@@ -16,7 +16,7 @@ static void timer_callback(TimerHandle_t hTimer) {
|
||||
}
|
||||
|
||||
Timer::Timer(Type type, Callback callback, std::shared_ptr<void> callbackContext) {
|
||||
tt_assert((kernel_is_irq() == 0U) && (callback != nullptr));
|
||||
tt_assert((kernel::isIrq() == 0U) && (callback != nullptr));
|
||||
|
||||
this->callback = callback;
|
||||
this->callbackContext = std::move(callbackContext);
|
||||
@@ -33,12 +33,12 @@ Timer::Timer(Type type, Callback callback, std::shared_ptr<void> callbackContext
|
||||
}
|
||||
|
||||
Timer::~Timer() {
|
||||
tt_assert(!kernel_is_irq());
|
||||
tt_assert(!kernel::isIrq());
|
||||
tt_check(xTimerDelete(timerHandle, portMAX_DELAY) == pdPASS);
|
||||
}
|
||||
|
||||
TtStatus Timer::start(uint32_t ticks) {
|
||||
tt_assert(!kernel_is_irq());
|
||||
tt_assert(!kernel::isIrq());
|
||||
tt_assert(ticks < portMAX_DELAY);
|
||||
|
||||
if (xTimerChangePeriod(timerHandle, ticks, portMAX_DELAY) == pdPASS) {
|
||||
@@ -49,7 +49,7 @@ TtStatus Timer::start(uint32_t ticks) {
|
||||
}
|
||||
|
||||
TtStatus Timer::restart(uint32_t ticks) {
|
||||
tt_assert(!kernel_is_irq());
|
||||
tt_assert(!kernel::isIrq());
|
||||
tt_assert(ticks < portMAX_DELAY);
|
||||
|
||||
if (xTimerChangePeriod(timerHandle, ticks, portMAX_DELAY) == pdPASS &&
|
||||
@@ -61,24 +61,24 @@ TtStatus Timer::restart(uint32_t ticks) {
|
||||
}
|
||||
|
||||
TtStatus Timer::stop() {
|
||||
tt_assert(!kernel_is_irq());
|
||||
tt_assert(!kernel::isIrq());
|
||||
tt_check(xTimerStop(timerHandle, portMAX_DELAY) == pdPASS);
|
||||
return TtStatusOk;
|
||||
}
|
||||
|
||||
bool Timer::isRunning() {
|
||||
tt_assert(!kernel_is_irq());
|
||||
tt_assert(!kernel::isIrq());
|
||||
return xTimerIsTimerActive(timerHandle) == pdTRUE;
|
||||
}
|
||||
|
||||
uint32_t Timer::getExpireTime() {
|
||||
tt_assert(!kernel_is_irq());
|
||||
tt_assert(!kernel::isIrq());
|
||||
return (uint32_t)xTimerGetExpiryTime(timerHandle);
|
||||
}
|
||||
|
||||
void Timer::pendingCallback(PendingCallback callback, void* callbackContext, uint32_t arg) {
|
||||
BaseType_t ret = pdFAIL;
|
||||
if (kernel_is_irq()) {
|
||||
if (kernel::isIrq()) {
|
||||
ret = xTimerPendFunctionCallFromISR(callback, callbackContext, arg, nullptr);
|
||||
} else {
|
||||
ret = xTimerPendFunctionCall(callback, callbackContext, arg, TtWaitForever);
|
||||
@@ -87,7 +87,7 @@ void Timer::pendingCallback(PendingCallback callback, void* callbackContext, uin
|
||||
}
|
||||
|
||||
void Timer::setThreadPriority(TimerThreadPriority priority) {
|
||||
tt_assert(!kernel_is_irq());
|
||||
tt_assert(!kernel::isIrq());
|
||||
|
||||
TaskHandle_t task_handle = xTimerGetTimerDaemonTaskHandle();
|
||||
tt_assert(task_handle); // Don't call this method before timer task start
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "Log.h"
|
||||
#include "mbedtls/aes.h"
|
||||
#include <cstring>
|
||||
#include <cstdint>
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "esp_cpu.h"
|
||||
@@ -95,7 +96,7 @@ static void get_nvs_key(uint8_t key[32]) {
|
||||
* @param[out] out output buffer for result of XOR
|
||||
* @param[in] length data length (all buffers must be at least this size)
|
||||
*/
|
||||
static void xor_key(const uint8_t* in_left, const uint8_t* in_right, uint8_t* out, size_t length) {
|
||||
static void xorKey(const uint8_t* in_left, const uint8_t* in_right, uint8_t* out, size_t length) {
|
||||
for (int i = 0; i < length; ++i) {
|
||||
out[i] = in_left[i] ^ in_right[i];
|
||||
}
|
||||
@@ -105,7 +106,7 @@ static void xor_key(const uint8_t* in_left, const uint8_t* in_right, uint8_t* ou
|
||||
* Combines a stored key and a hardware key into a single reliable key value.
|
||||
* @param[out] key the key output
|
||||
*/
|
||||
static void get_key(uint8_t key[32]) {
|
||||
static void getKey(uint8_t key[32]) {
|
||||
#if !defined(CONFIG_SECURE_BOOT) || !defined(CONFIG_SECURE_FLASH_ENC_ENABLED)
|
||||
TT_LOG_W(TAG, "Using tt_secure_* code with secure boot and/or flash encryption disabled.");
|
||||
TT_LOG_W(TAG, "An attacker with physical access to your ESP32 can decrypt your secure data.");
|
||||
@@ -117,14 +118,14 @@ static void get_key(uint8_t key[32]) {
|
||||
#ifdef ESP_PLATFORM
|
||||
get_hardware_key(hardware_key);
|
||||
get_nvs_key(nvs_key);
|
||||
xor_key(hardware_key, nvs_key, key, 32);
|
||||
xorKey(hardware_key, nvs_key, key, 32);
|
||||
#else
|
||||
TT_LOG_W(TAG, "Using unsafe key for debugging purposes.");
|
||||
memset(key, 0, 32);
|
||||
#endif
|
||||
}
|
||||
|
||||
void get_iv_from_data(const void* data, size_t data_length, uint8_t iv[16]) {
|
||||
void getIv(const void* data, size_t data_length, uint8_t iv[16]) {
|
||||
memset((void*)iv, 0, 16);
|
||||
uint8_t* data_bytes = (uint8_t*)data;
|
||||
for (int i = 0; i < data_length; ++i) {
|
||||
@@ -133,11 +134,7 @@ void get_iv_from_data(const void* data, size_t data_length, uint8_t iv[16]) {
|
||||
}
|
||||
}
|
||||
|
||||
void get_iv_from_string(const char* input, uint8_t iv[16]) {
|
||||
get_iv_from_data((const void*)input, strlen(input), iv);
|
||||
}
|
||||
|
||||
static int aes256_crypt_cbc(
|
||||
static int aes256CryptCbc(
|
||||
const uint8_t key[32],
|
||||
int mode,
|
||||
size_t length,
|
||||
@@ -166,25 +163,25 @@ static int aes256_crypt_cbc(
|
||||
int encrypt(const uint8_t iv[16], uint8_t* in_data, uint8_t* out_data, size_t length) {
|
||||
tt_check(length % 16 == 0, "Length is not a multiple of 16 bytes (for AES 256");
|
||||
uint8_t key[32];
|
||||
get_key(key);
|
||||
getKey(key);
|
||||
|
||||
// TODO: Is this still needed after switching to regular AES functions?
|
||||
uint8_t iv_copy[16];
|
||||
memcpy(iv_copy, iv, sizeof(iv_copy));
|
||||
|
||||
return aes256_crypt_cbc(key, MBEDTLS_AES_ENCRYPT, length, iv_copy, in_data, out_data);
|
||||
return aes256CryptCbc(key, MBEDTLS_AES_ENCRYPT, length, iv_copy, in_data, out_data);
|
||||
}
|
||||
|
||||
int decrypt(const uint8_t iv[16], uint8_t* in_data, uint8_t* out_data, size_t length) {
|
||||
tt_check(length % 16 == 0, "Length is not a multiple of 16 bytes (for AES 256");
|
||||
uint8_t key[32];
|
||||
get_key(key);
|
||||
getKey(key);
|
||||
|
||||
// TODO: Is this still needed after switching to regular AES functions?
|
||||
uint8_t iv_copy[16];
|
||||
memcpy(iv_copy, iv, sizeof(iv_copy));
|
||||
|
||||
return aes256_crypt_cbc(key, MBEDTLS_AES_DECRYPT, length, iv_copy, in_data, out_data);
|
||||
return aes256CryptCbc(key, MBEDTLS_AES_DECRYPT, length, iv_copy, in_data, out_data);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace tt::crypt {
|
||||
|
||||
@@ -29,14 +30,7 @@ namespace tt::crypt {
|
||||
* @param data_length input data length
|
||||
* @param iv output IV
|
||||
*/
|
||||
void get_iv_from_data(const void* data, size_t data_length, uint8_t iv[16]);
|
||||
|
||||
/**
|
||||
* @brief Fills the IV with zeros and then creates an IV based on the input data.
|
||||
* @param input input text
|
||||
* @param iv output IV
|
||||
*/
|
||||
void get_iv_from_string(const char* input, uint8_t iv[16]);
|
||||
void getIv(const void* data, size_t data_length, uint8_t iv[16]);
|
||||
|
||||
/**
|
||||
* @brief Encrypt data.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "Kernel.h"
|
||||
#include "kernel/Kernel.h"
|
||||
#include "Check.h"
|
||||
#include "CoreDefines.h"
|
||||
#include "CoreTypes.h"
|
||||
@@ -10,18 +10,18 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace tt {
|
||||
namespace tt::kernel {
|
||||
|
||||
bool kernel_is_irq() {
|
||||
bool isIrq() {
|
||||
return TT_IS_IRQ_MODE();
|
||||
}
|
||||
|
||||
bool kernel_is_running() {
|
||||
bool isRunning() {
|
||||
return xTaskGetSchedulerState() != taskSCHEDULER_RUNNING;
|
||||
}
|
||||
|
||||
int32_t kernel_lock() {
|
||||
tt_assert(!kernel_is_irq());
|
||||
int32_t lock() {
|
||||
tt_assert(!isIrq());
|
||||
|
||||
int32_t lock;
|
||||
|
||||
@@ -45,8 +45,8 @@ int32_t kernel_lock() {
|
||||
return (lock);
|
||||
}
|
||||
|
||||
int32_t kernel_unlock() {
|
||||
tt_assert(!kernel_is_irq());
|
||||
int32_t unlock() {
|
||||
tt_assert(!isIrq());
|
||||
|
||||
int32_t lock;
|
||||
|
||||
@@ -75,8 +75,8 @@ int32_t kernel_unlock() {
|
||||
return (lock);
|
||||
}
|
||||
|
||||
int32_t kernel_restore_lock(int32_t lock) {
|
||||
tt_assert(!kernel_is_irq());
|
||||
int32_t restoreLock(int32_t lock) {
|
||||
tt_assert(!isIrq());
|
||||
|
||||
switch (xTaskGetSchedulerState()) {
|
||||
case taskSCHEDULER_SUSPENDED:
|
||||
@@ -106,13 +106,13 @@ int32_t kernel_restore_lock(int32_t lock) {
|
||||
return (lock);
|
||||
}
|
||||
|
||||
uint32_t kernel_get_tick_frequency() {
|
||||
uint32_t getTickFrequency() {
|
||||
/* Return frequency in hertz */
|
||||
return (configTICK_RATE_HZ);
|
||||
}
|
||||
|
||||
void delay_ticks(TickType_t ticks) {
|
||||
tt_assert(!kernel_is_irq());
|
||||
void delayTicks(TickType_t ticks) {
|
||||
tt_assert(!isIrq());
|
||||
if (ticks == 0U) {
|
||||
taskYIELD();
|
||||
} else {
|
||||
@@ -120,8 +120,8 @@ void delay_ticks(TickType_t ticks) {
|
||||
}
|
||||
}
|
||||
|
||||
TtStatus delay_until_tick(TickType_t tick) {
|
||||
tt_assert(!kernel_is_irq());
|
||||
TtStatus delayUntilTick(TickType_t tick) {
|
||||
tt_assert(!isIrq());
|
||||
|
||||
TickType_t tcnt, delay;
|
||||
TtStatus stat;
|
||||
@@ -147,10 +147,10 @@ TtStatus delay_until_tick(TickType_t tick) {
|
||||
return (stat);
|
||||
}
|
||||
|
||||
TickType_t get_ticks() {
|
||||
TickType_t getTicks() {
|
||||
TickType_t ticks;
|
||||
|
||||
if (kernel_is_irq() != 0U) {
|
||||
if (isIrq() != 0U) {
|
||||
ticks = xTaskGetTickCountFromISR();
|
||||
} else {
|
||||
ticks = xTaskGetTickCount();
|
||||
@@ -159,7 +159,7 @@ TickType_t get_ticks() {
|
||||
return ticks;
|
||||
}
|
||||
|
||||
TickType_t ms_to_ticks(uint32_t milliseconds) {
|
||||
TickType_t millisToTicks(uint32_t milliseconds) {
|
||||
#if configTICK_RATE_HZ == 1000
|
||||
return (TickType_t)milliseconds;
|
||||
#else
|
||||
@@ -167,7 +167,7 @@ TickType_t ms_to_ticks(uint32_t milliseconds) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void delay_ms(uint32_t milliseconds) {
|
||||
void delayMillis(uint32_t milliseconds) {
|
||||
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
|
||||
if (milliseconds > 0 && milliseconds < portMAX_DELAY - 1) {
|
||||
milliseconds += 1;
|
||||
@@ -175,14 +175,14 @@ void delay_ms(uint32_t milliseconds) {
|
||||
#if configTICK_RATE_HZ_RAW == 1000
|
||||
tt_delay_tick(milliseconds);
|
||||
#else
|
||||
delay_ticks(ms_to_ticks(milliseconds));
|
||||
delayTicks(kernel::millisToTicks(milliseconds));
|
||||
#endif
|
||||
} else if (milliseconds > 0) {
|
||||
delay_us(milliseconds * 1000);
|
||||
kernel::delayMicros(milliseconds * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
void delay_us(uint32_t microseconds) {
|
||||
void delayMicros(uint32_t microseconds) {
|
||||
#ifdef ESP_PLATFORM
|
||||
ets_delay_us(microseconds);
|
||||
#else
|
||||
@@ -190,7 +190,7 @@ void delay_us(uint32_t microseconds) {
|
||||
#endif
|
||||
}
|
||||
|
||||
Platform get_platform() {
|
||||
Platform getPlatform() {
|
||||
#ifdef ESP_PLATFORM
|
||||
return PlatformEsp;
|
||||
#else
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "FreeRTOS.h"
|
||||
#endif
|
||||
|
||||
namespace tt {
|
||||
namespace tt::kernel {
|
||||
|
||||
typedef enum {
|
||||
PlatformEsp,
|
||||
@@ -30,13 +30,13 @@ typedef enum {
|
||||
*
|
||||
* @return true if CPU is in IRQ or kernel running and IRQ is masked
|
||||
*/
|
||||
bool kernel_is_irq();
|
||||
bool isIrq();
|
||||
|
||||
/** Check if kernel is running
|
||||
*
|
||||
* @return true if running, false otherwise
|
||||
*/
|
||||
bool kernel_is_running();
|
||||
bool isRunning();
|
||||
|
||||
/** Lock kernel, pause process scheduling
|
||||
*
|
||||
@@ -44,7 +44,7 @@ bool kernel_is_running();
|
||||
*
|
||||
* @return previous lock state(0 - unlocked, 1 - locked)
|
||||
*/
|
||||
int32_t kernel_lock();
|
||||
int32_t lock();
|
||||
|
||||
/** Unlock kernel, resume process scheduling
|
||||
*
|
||||
@@ -52,7 +52,7 @@ int32_t kernel_lock();
|
||||
*
|
||||
* @return previous lock state(0 - unlocked, 1 - locked)
|
||||
*/
|
||||
int32_t kernel_unlock();
|
||||
int32_t unlock();
|
||||
|
||||
/** Restore kernel lock state
|
||||
*
|
||||
@@ -62,15 +62,15 @@ int32_t kernel_unlock();
|
||||
*
|
||||
* @return new lock state or error
|
||||
*/
|
||||
int32_t kernel_restore_lock(int32_t lock);
|
||||
int32_t restoreLock(int32_t lock);
|
||||
|
||||
/** Get kernel systick frequency
|
||||
*
|
||||
* @return systick counts per second
|
||||
*/
|
||||
uint32_t kernel_get_tick_frequency();
|
||||
uint32_t getTickFrequency();
|
||||
|
||||
TickType_t get_ticks();
|
||||
TickType_t getTicks();
|
||||
|
||||
/** Delay execution
|
||||
*
|
||||
@@ -80,7 +80,7 @@ TickType_t get_ticks();
|
||||
*
|
||||
* @param[in] ticks The ticks count to pause
|
||||
*/
|
||||
void delay_ticks(TickType_t ticks);
|
||||
void delayTicks(TickType_t ticks);
|
||||
|
||||
/** Delay until tick
|
||||
*
|
||||
@@ -90,14 +90,14 @@ void delay_ticks(TickType_t ticks);
|
||||
*
|
||||
* @return The status.
|
||||
*/
|
||||
TtStatus delay_until_tick(uint32_t tick);
|
||||
TtStatus delayUntilTick(uint32_t tick);
|
||||
|
||||
/** Convert milliseconds to ticks
|
||||
*
|
||||
* @param[in] milliseconds time in milliseconds
|
||||
* @param[in] milliSeconds time in milliseconds
|
||||
* @return time in ticks
|
||||
*/
|
||||
TickType_t ms_to_ticks(uint32_t milliseconds);
|
||||
TickType_t millisToTicks(uint32_t milliSeconds);
|
||||
|
||||
/** Delay in milliseconds
|
||||
*
|
||||
@@ -108,18 +108,18 @@ TickType_t ms_to_ticks(uint32_t milliseconds);
|
||||
*
|
||||
* @warning Cannot be used from ISR
|
||||
*
|
||||
* @param[in] milliseconds milliseconds to wait
|
||||
* @param[in] milliSeconds milliseconds to wait
|
||||
*/
|
||||
void delay_ms(uint32_t milliseconds);
|
||||
void delayMillis(uint32_t milliSeconds);
|
||||
|
||||
/** Delay in microseconds
|
||||
*
|
||||
* Implemented using Cortex DWT counter. Blocking and non aliased.
|
||||
*
|
||||
* @param[in] microseconds microseconds to wait
|
||||
* @param[in] microSeconds microseconds to wait
|
||||
*/
|
||||
void delay_us(uint32_t microseconds);
|
||||
void delayMicros(uint32_t microSeconds);
|
||||
|
||||
Platform get_platform();
|
||||
Platform getPlatform();
|
||||
|
||||
} // namespace
|
||||
+10
-10
@@ -9,18 +9,18 @@ static portMUX_TYPE critical_mutex;
|
||||
#define TT_ENTER_CRITICAL() taskENTER_CRITICAL()
|
||||
#endif
|
||||
|
||||
namespace tt::critical {
|
||||
namespace tt::kernel::critical {
|
||||
|
||||
TtCriticalInfo enter() {
|
||||
TtCriticalInfo info;
|
||||
TtCriticalInfo info = {
|
||||
.isrm = 0,
|
||||
.fromIsr = TT_IS_ISR(),
|
||||
.kernelRunning = (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING)
|
||||
};
|
||||
|
||||
info.isrm = 0;
|
||||
info.from_isr = TT_IS_ISR();
|
||||
info.kernel_running = (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING);
|
||||
|
||||
if (info.from_isr) {
|
||||
if (info.fromIsr) {
|
||||
info.isrm = taskENTER_CRITICAL_FROM_ISR();
|
||||
} else if (info.kernel_running) {
|
||||
} else if (info.kernelRunning) {
|
||||
TT_ENTER_CRITICAL();
|
||||
} else {
|
||||
portDISABLE_INTERRUPTS();
|
||||
@@ -30,9 +30,9 @@ TtCriticalInfo enter() {
|
||||
}
|
||||
|
||||
void exit(TtCriticalInfo info) {
|
||||
if (info.from_isr) {
|
||||
if (info.fromIsr) {
|
||||
taskEXIT_CRITICAL_FROM_ISR(info.isrm);
|
||||
} else if (info.kernel_running) {
|
||||
} else if (info.kernelRunning) {
|
||||
TT_ENTER_CRITICAL();
|
||||
} else {
|
||||
portENABLE_INTERRUPTS();
|
||||
+3
-3
@@ -10,12 +10,12 @@
|
||||
#define TT_CRITICAL_EXIT() __tt_critical_exit(__tt_critical_info);
|
||||
#endif
|
||||
|
||||
namespace tt::critical {
|
||||
namespace tt::kernel::critical {
|
||||
|
||||
typedef struct {
|
||||
uint32_t isrm;
|
||||
bool from_isr;
|
||||
bool kernel_running;
|
||||
bool fromIsr;
|
||||
bool kernelRunning;
|
||||
} TtCriticalInfo;
|
||||
|
||||
TtCriticalInfo enter();
|
||||
Reference in New Issue
Block a user