C++ refactoring (#91)

This commit is contained in:
Ken Van Hoeylandt
2024-11-26 17:51:05 +01:00
committed by GitHub
parent d06137ba76
commit ca5d4b8226
159 changed files with 904 additions and 1086 deletions
+6 -6
View File
@@ -13,7 +13,7 @@ namespace tt {
#define TAG "tactility"
// Initialize NVS
static void esp_nvs_init() {
static void initEspNvs() {
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
TT_LOG_I(TAG, "nvs erasing");
@@ -24,15 +24,15 @@ static void esp_nvs_init() {
TT_LOG_I(TAG, "nvs initialized");
}
static void esp_network_init() {
static void initEspNetwork() {
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
}
void esp_init() {
esp_nvs_init();
esp_partitions_init();
esp_network_init();
void initEsp() {
initEspNvs();
initEspPartitions();
initEspNetwork();
}
} // namespace
+6 -6
View File
@@ -11,7 +11,7 @@ namespace tt {
static const char* TAG = "filesystem";
static esp_err_t nvs_flash_init_safely() {
static esp_err_t initNvsFlashSafely() {
esp_err_t result = nvs_flash_init();
if (result == ESP_ERR_NVS_NO_FREE_PAGES || result == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
@@ -20,7 +20,7 @@ static esp_err_t nvs_flash_init_safely() {
return result;
}
static esp_err_t spiffs_init(esp_vfs_spiffs_conf_t* conf) {
static esp_err_t initSpiffs(esp_vfs_spiffs_conf_t* conf) {
esp_err_t ret = esp_vfs_spiffs_register(conf);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
@@ -43,8 +43,8 @@ static esp_err_t spiffs_init(esp_vfs_spiffs_conf_t* conf) {
return ESP_OK;
}
esp_err_t esp_partitions_init() {
ESP_ERROR_CHECK(nvs_flash_init_safely());
esp_err_t initEspPartitions() {
ESP_ERROR_CHECK(initNvsFlashSafely());
esp_vfs_spiffs_conf_t assets_spiffs = {
.base_path = MOUNT_POINT_ASSETS,
@@ -53,7 +53,7 @@ esp_err_t esp_partitions_init() {
.format_if_mount_failed = false
};
if (spiffs_init(&assets_spiffs) != ESP_OK) {
if (initSpiffs(&assets_spiffs) != ESP_OK) {
return ESP_FAIL;
}
@@ -64,7 +64,7 @@ esp_err_t esp_partitions_init() {
.format_if_mount_failed = false
};
if (spiffs_init(&config_spiffs) != ESP_OK) {
if (initSpiffs(&config_spiffs) != ESP_OK) {
return ESP_FAIL;
}
-1
View File
@@ -1,7 +1,6 @@
#pragma once
#include <cstdint>
#include <cstddef>
#include <string>
namespace tt {
@@ -2,7 +2,6 @@
#include "Bundle.h"
#include "Preferences.h"
#include "TactilityCore.h"
namespace tt {
@@ -1,22 +0,0 @@
#pragma once
#include "ServiceManifest.h"
#include "TactilityCore.h"
namespace tt {
typedef void (*ServiceManifestCallback)(const ServiceManifest*, void* context);
void service_registry_init();
void service_registry_add(const ServiceManifest* manifest);
void service_registry_remove(const ServiceManifest* manifest);
_Nullable const ServiceManifest* service_registry_find_manifest_by_id(const std::string& id);
void service_registry_for_each_manifest(ServiceManifestCallback callback, void* _Nullable context);
bool service_registry_start(const std::string& id);
bool service_registry_stop(const std::string& id);
Service* _Nullable service_find(const std::string& id);
} // namespace
+11 -11
View File
@@ -1,8 +1,8 @@
#include "TactilityHeadless.h"
#include "Hal/Configuration.h"
#include "Hal/Hal_i.h"
#include "ServiceManifest.h"
#include "ServiceRegistry.h"
#include "hal/Configuration.h"
#include "hal/Hal_i.h"
#include "service/Manifest.h"
#include "service/ServiceRegistry.h"
#ifdef ESP_PLATFORM
#include "EspInit.h"
@@ -12,10 +12,10 @@ namespace tt {
#define TAG "tactility"
namespace service::wifi { extern const ServiceManifest manifest; }
namespace service::sdcard { extern const ServiceManifest manifest; }
namespace service::wifi { extern const Manifest manifest; }
namespace service::sdcard { extern const Manifest manifest; }
static const ServiceManifest* const system_services[] = {
static const service::Manifest* const system_services[] = {
&service::sdcard::manifest,
&service::wifi::manifest
};
@@ -24,16 +24,16 @@ static const hal::Configuration* hardwareConfig = nullptr;
static void register_and_start_system_services() {
TT_LOG_I(TAG, "Registering and starting system services");
int app_count = sizeof(system_services) / sizeof(ServiceManifest*);
int app_count = sizeof(system_services) / sizeof(service::Manifest*);
for (int i = 0; i < app_count; ++i) {
service_registry_add(system_services[i]);
tt_check(service_registry_start(system_services[i]->id));
addService(system_services[i]);
tt_check(service::startService(system_services[i]->id));
}
}
void initHeadless(const hal::Configuration& config) {
#ifdef ESP_PLATFORM
esp_init();
initEsp();
#endif
hardwareConfig = &config;
hal::init(config);
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once
#include "Hal/Configuration.h"
#include "hal/Configuration.h"
#include "TactilityHeadlessConfig.h"
namespace tt {
@@ -1,9 +1,8 @@
#pragma once
#include "Power.h"
#include "Sdcard.h"
#include "I2c/I2c.h"
#include "TactilityCore.h"
#include "hal/sdcard/Sdcard.h"
#include "hal/i2c/I2c.h"
namespace tt::hal {
@@ -1,5 +1,5 @@
#include "Hal/Hal_i.h"
#include "Hal/I2c/I2c.h"
#include "hal/Hal_i.h"
#include "hal/i2c/I2c.h"
#define TAG "hal"
@@ -11,11 +11,11 @@ typedef uint8_t (*PowerGetBatteryCharge)(); // Power value [0, 255] which maps t
typedef int32_t (*PowerGetCurrent)(); // Consumption or charge current in mAh
typedef struct {
PowerIsCharging is_charging;
PowerIsChargingEnabled is_charging_enabled;
PowerSetChargingEnabled set_charging_enabled;
PowerGetBatteryCharge get_charge_level;
PowerGetCurrent get_current;
PowerIsCharging isCharging;
PowerIsChargingEnabled isChargingEnabled;
PowerSetChargingEnabled setChargingEnabled;
PowerGetBatteryCharge getChargeLevel;
PowerGetCurrent getCurrent;
} Power;
} // namespace tt
@@ -49,7 +49,7 @@ bool mount(const SdCard* sdcard) {
}
}
State get_state() {
State getState() {
if (data.context == nullptr) {
return StateUnmounted;
} else if (data.sdcard->is_mounted(data.context)) {
@@ -29,7 +29,7 @@ typedef struct {
} SdCard;
bool mount(const SdCard* sdcard);
State get_state();
State getState();
bool unmount(uint32_t timeout_ticks);
} // namespace
@@ -1,16 +1,15 @@
#pragma once
#include "TactilityCore.h"
#include <string>
namespace tt {
namespace tt::service {
class Service;
typedef void (*ServiceOnStart)(Service& service);
typedef void (*ServiceOnStop)(Service& service);
typedef struct ServiceManifest {
typedef struct Manifest {
/**
* The identifier by which the app is launched by the system and other apps.
*/
@@ -19,13 +18,13 @@ typedef struct ServiceManifest {
/**
* Non-blocking method to call when service is started.
*/
const ServiceOnStart on_start = nullptr;
const ServiceOnStart onStart = nullptr;
/**
* Non-blocking method to call when service is stopped.
*/
const ServiceOnStop on_stop = nullptr;
const ServiceOnStop onStop = nullptr;
} ServiceManifest;
} Manifest;
} // namespace
@@ -1,11 +1,11 @@
#include "Service.h"
#include "ServiceManifest.h"
#include "Manifest.h"
namespace tt {
namespace tt::service {
Service::Service(const ServiceManifest& manifest) : manifest(manifest) {}
Service::Service(const service::Manifest& manifest) : manifest(manifest) {}
const ServiceManifest& Service::getManifest() const { return manifest; }
const service::Manifest& Service::getManifest() const { return manifest; }
void* Service::getData() const {
mutex.acquire(TtWaitForever);
@@ -1,20 +1,20 @@
#pragma once
#include "Mutex.h"
#include "ServiceManifest.h"
#include "Manifest.h"
namespace tt {
namespace tt::service {
class Service {
private:
Mutex mutex = Mutex(MutexTypeNormal);
const ServiceManifest& manifest;
const service::Manifest& manifest;
void* data = nullptr;
public:
Service(const ServiceManifest& manifest);
Service(const service::Manifest& manifest);
[[nodiscard]] const ServiceManifest& getManifest() const;
[[nodiscard]] const service::Manifest& getManifest() const;
[[nodiscard]] void* getData() const;
void setData(void* newData);
};
@@ -2,25 +2,25 @@
#include "Mutex.h"
#include "Service.h"
#include "ServiceManifest.h"
#include "Manifest.h"
#include "TactilityCore.h"
#include <string>
#include <unordered_map>
namespace tt {
namespace tt::service {
#define TAG "service_registry"
typedef std::unordered_map<std::string, const ServiceManifest*> ServiceManifestMap;
typedef std::unordered_map<std::string, const Manifest*> ManifestMap;
typedef std::unordered_map<std::string, Service*> ServiceInstanceMap;
static ServiceManifestMap service_manifest_map;
static ManifestMap service_manifest_map;
static ServiceInstanceMap service_instance_map;
static Mutex manifest_mutex(MutexTypeNormal);
static Mutex instance_mutex(MutexTypeNormal);
void service_registry_add(const ServiceManifest* manifest) {
void addService(const Manifest* manifest) {
TT_LOG_I(TAG, "adding %s", manifest->id.c_str());
manifest_mutex.acquire(TtWaitForever);
@@ -28,10 +28,10 @@ void service_registry_add(const ServiceManifest* manifest) {
manifest_mutex.release();
}
const ServiceManifest* _Nullable service_registry_find_manifest_by_id(const std::string& id) {
const Manifest* _Nullable findManifestId(const std::string& id) {
manifest_mutex.acquire(TtWaitForever);
auto iterator = service_manifest_map.find(id);
_Nullable const ServiceManifest * manifest = iterator != service_manifest_map.end() ? iterator->second : nullptr;
_Nullable const Manifest * manifest = iterator != service_manifest_map.end() ? iterator->second : nullptr;
manifest_mutex.release();
return manifest;
}
@@ -44,25 +44,17 @@ static Service* _Nullable service_registry_find_instance_by_id(const std::string
return service;
}
void service_registry_for_each_manifest(ServiceManifestCallback callback, void* _Nullable context) {
manifest_mutex.acquire(TtWaitForever);
for (auto& it : service_manifest_map) {
callback(it.second, context);
}
manifest_mutex.release();
}
// TODO: return proper error/status instead of BOOL
bool service_registry_start(const std::string& id) {
bool startService(const std::string& id) {
TT_LOG_I(TAG, "starting %s", id.c_str());
const ServiceManifest* manifest = service_registry_find_manifest_by_id(id);
const Manifest* manifest = findManifestId(id);
if (manifest == nullptr) {
TT_LOG_E(TAG, "manifest not found for service %s", id.c_str());
return false;
}
auto* service = new Service(*manifest);
manifest->on_start(*service);
manifest->onStart(*service);
instance_mutex.acquire(TtWaitForever);
service_instance_map[manifest->id] = service;
@@ -72,11 +64,11 @@ bool service_registry_start(const std::string& id) {
return true;
}
_Nullable Service* service_find(const std::string& service_id) {
_Nullable Service* findServiceById(const std::string& service_id) {
return (Service*)service_registry_find_instance_by_id(service_id);
}
bool service_registry_stop(const std::string& id) {
bool stopService(const std::string& id) {
TT_LOG_I(TAG, "stopping %s", id.c_str());
Service* service = service_registry_find_instance_by_id(id);
if (service == nullptr) {
@@ -84,7 +76,7 @@ bool service_registry_stop(const std::string& id) {
return false;
}
service->getManifest().on_stop(*service);
service->getManifest().onStop(*service);
delete service;
instance_mutex.acquire(TtWaitForever);
@@ -0,0 +1,20 @@
#pragma once
#include "Manifest.h"
namespace tt::service {
typedef void (*ManifestCallback)(const Manifest*, void* context);
void initRegistry();
void addService(const Manifest* manifest);
void removeService(const Manifest* manifest);
bool startService(const std::string& id);
bool stopService(const std::string& id);
const Manifest* _Nullable findManifestId(const std::string& id);
Service* _Nullable findServiceById(const std::string& id);
} // namespace
@@ -1,7 +1,7 @@
#include <cstdlib>
#include "Mutex.h"
#include "Service.h"
#include "service/Service.h"
#include "TactilityCore.h"
#include "TactilityHeadless.h"
@@ -14,7 +14,7 @@ static int32_t sdcard_task(void* context);
typedef struct {
Mutex* mutex;
Thread* thread;
hal::sdcard::State last_state;
hal::sdcard::State lastState;
bool interrupted;
} ServiceData;
@@ -28,7 +28,7 @@ static ServiceData* service_data_alloc() {
&sdcard_task,
data
),
.last_state = hal::sdcard::StateUnmounted,
.lastState = hal::sdcard::StateUnmounted,
.interrupted = false
};
data->thread->setPriority(Thread::PriorityLow);
@@ -57,15 +57,15 @@ static int32_t sdcard_task(void* context) {
interrupted = data->interrupted;
hal::sdcard::State new_state = hal::sdcard::get_state();
hal::sdcard::State new_state = hal::sdcard::getState();
if (new_state == hal::sdcard::StateError) {
TT_LOG_W(TAG, "Sdcard error - unmounting. Did you eject the card in an unsafe manner?");
hal::sdcard::unmount(ms_to_ticks(1000));
}
if (new_state != data->last_state) {
data->last_state = new_state;
if (new_state != data->lastState) {
data->lastState = new_state;
}
service_data_unlock(data);
@@ -98,10 +98,10 @@ static void on_stop(Service& service) {
}
}
extern const ServiceManifest manifest = {
extern const Manifest manifest = {
.id = "sdcard",
.on_start = &on_start,
.on_stop = &on_stop
.onStart = &on_start,
.onStop = &on_stop
};
} // namespace
@@ -9,11 +9,12 @@
#include "freertos/event_groups.h"
#include "Log.h"
#include "Pubsub.h"
#include "Service.h"
#include "service/Service.h"
#include "WifiSettings.h"
#include <atomic>
#include <cstring>
#include <sys/cdefs.h>
#include <TactilityCore.h>
namespace tt::service::wifi {
@@ -67,7 +68,7 @@ public:
bool secure_connection = false;
esp_event_handler_instance_t event_handler_any_id = nullptr;
esp_event_handler_instance_t event_handler_got_ip = nullptr;
EventGroupHandle_t event_group;
EventFlag connection_wait_flags;
settings::WifiApSettings connection_target = {
.ssid = { 0 },
.password = { 0 },
@@ -88,7 +89,6 @@ static void unlock(Wifi* wifi);
Wifi::Wifi() : radio_state(WIFI_RADIO_OFF) {
pubsub = tt_pubsub_alloc();
event_group = xEventGroupCreate();
}
Wifi::~Wifi() {
@@ -309,7 +309,7 @@ static void event_handler(TT_UNUSED void* arg, esp_event_base_t event_base, int3
}
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (wifi_singleton->radio_state != WIFI_RADIO_OFF_PENDING) {
xEventGroupSetBits(wifi_singleton->event_group, WIFI_FAIL_BIT);
wifi_singleton->connection_wait_flags.set(WIFI_FAIL_BIT);
TT_LOG_I(TAG, "event_handler: disconnected");
wifi_singleton->radio_state = WIFI_RADIO_ON;
publish_event_simple(wifi_singleton, WifiEventTypeDisconnected);
@@ -317,7 +317,7 @@ static void event_handler(TT_UNUSED void* arg, esp_event_base_t event_base, int3
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
auto* event = static_cast<ip_event_got_ip_t*>(event_data);
TT_LOG_I(TAG, "event_handler: got ip:" IPSTR, IP2STR(&event->ip_info.ip));
xEventGroupSetBits(wifi_singleton->event_group, WIFI_CONNECTED_BIT);
wifi_singleton->connection_wait_flags.set(WIFI_CONNECTED_BIT);
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
auto* event = static_cast<wifi_event_sta_scan_done_t*>(event_data);
TT_LOG_I(TAG, "event_handler: wifi scanning done (scan id %u)", event->scan_id);
@@ -595,13 +595,7 @@ static void connect_internal(Wifi* wifi) {
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT)
* or connection failed for the maximum number of re-tries (WIFI_FAIL_BIT).
* The bits are set by wifi_event_handler() */
EventBits_t bits = xEventGroupWaitBits(
wifi->event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY
);
uint32_t bits = wifi_singleton->connection_wait_flags.wait(WIFI_FAIL_BIT | WIFI_CONNECTED_BIT);
if (bits & WIFI_CONNECTED_BIT) {
wifi->radio_state = WIFI_RADIO_CONNECTION_ACTIVE;
@@ -624,7 +618,7 @@ static void connect_internal(Wifi* wifi) {
TT_LOG_E(TAG, "UNEXPECTED EVENT");
}
xEventGroupClearBits(wifi_singleton->event_group, WIFI_FAIL_BIT | WIFI_CONNECTED_BIT);
wifi_singleton->connection_wait_flags.clear(WIFI_FAIL_BIT | WIFI_CONNECTED_BIT);
}
static void disconnect_internal_but_keep_active(Wifi* wifi) {
@@ -752,10 +746,10 @@ static void service_stop(Service& service) {
tt_crash("not fully implemented");
}
extern const ServiceManifest manifest = {
extern const Manifest manifest = {
.id = "Wifi",
.on_start = &service_start,
.on_stop = &service_stop
.onStart = &service_start,
.onStop = &service_stop
};
} // namespace
@@ -7,7 +7,7 @@
#include "MessageQueue.h"
#include "Mutex.h"
#include "Pubsub.h"
#include "Service.h"
#include "service/Service.h"
#include <cstdlib>
#include <cstring>
@@ -176,10 +176,10 @@ static void service_stop(TT_UNUSED Service& service) {
wifi = nullptr;
}
extern const ServiceManifest manifest = {
extern const Manifest manifest = {
.id = "Wifi",
.on_start = &service_start,
.on_stop = &service_stop
.onStart = &service_start,
.onStop = &service_stop
};
} // namespace
@@ -6,9 +6,8 @@
#include "nvs_flash.h"
#include "Log.h"
#include "Hash.h"
#include "Check.h"
#include "Crypt.h"
#include "crypt/Hash.h"
#include "crypt/Crypt.h"
#define TAG "wifi_settings"
#define TT_NVS_NAMESPACE "wifi_settings" // limited by NVS_KEY_NAME_MAX_SIZE