Project restructuring: add tactility-headless (#55)
- Created `tactility-headless` to support ESP32 firmwares that don't require graphics - `tactility` subproject now contains both PC and ESP32 code (to avoid having to split up `tactility` and `tactility-headless` into separate projects, which would result in a very complex dependency tree) - `tactility` subproject is now defined as component for ESP32 and as regular module for PC - Improvements for dispatcher - Added `project-structure.puml` to docs - `Gui` service now depends on `Loader` service instead of the reverse - Added `statusbar_updater` service for updating Wi-Fi and SD card icons
This commit is contained in:
committed by
GitHub
parent
b659d5b940
commit
27730260e0
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#define TT_ASSET_FOLDER "A:/assets/"
|
||||
#define TT_ASSET(file) TT_ASSET_FOLDER file
|
||||
|
||||
// App icons
|
||||
#define TT_ASSETS_APP_ICON_FALLBACK TT_ASSET("app_icon_fallback.png")
|
||||
#define TT_ASSETS_APP_ICON_FILES TT_ASSET("app_icon_files.png")
|
||||
#define TT_ASSETS_APP_ICON_DISPLAY_SETTINGS TT_ASSET("app_icon_display_settings.png")
|
||||
#define TT_ASSETS_APP_ICON_SETTINGS TT_ASSET("app_icon_settings.png")
|
||||
#define TT_ASSETS_APP_ICON_SYSTEM_INFO TT_ASSET("app_icon_system_info.png")
|
||||
|
||||
// SD card status
|
||||
#define TT_ASSETS_ICON_SDCARD TT_ASSET("sdcard.png")
|
||||
#define TT_ASSETS_ICON_SDCARD_ALERT TT_ASSET("sdcard_alert.png")
|
||||
|
||||
// Wifi status
|
||||
#define TT_ASSETS_ICON_WIFI_CONNECTION_ISSUE TT_ASSET("wifi_connection_issue.png")
|
||||
#define TT_ASSETS_ICON_WIFI_FIND TT_ASSET("wifi_find.png")
|
||||
#define TT_ASSETS_ICON_WIFI_OFF TT_ASSET("wifi_off.png")
|
||||
#define TT_ASSETS_ICON_WIFI_PERM_SCAN TT_ASSET("wifi_perm_scan.png")
|
||||
#define TT_ASSETS_ICON_WIFI_SIGNAL_0 TT_ASSET("wifi_signal_0.png")
|
||||
#define TT_ASSETS_ICON_WIFI_SIGNAL_0_LOCKED TT_ASSET("wifi_signal_0_locked.png")
|
||||
#define TT_ASSETS_ICON_WIFI_SIGNAL_1 TT_ASSET("wifi_signal_1.png")
|
||||
#define TT_ASSETS_ICON_WIFI_SIGNAL_1_LOCKED TT_ASSET("wifi_signal_1_locked.png")
|
||||
#define TT_ASSETS_ICON_WIFI_SIGNAL_2 TT_ASSET("wifi_signal_2.png")
|
||||
#define TT_ASSETS_ICON_WIFI_SIGNAL_2_LOCKED TT_ASSET("wifi_signal_2_locked.png")
|
||||
#define TT_ASSETS_ICON_WIFI_SIGNAL_3 TT_ASSET("wifi_signal_3.png")
|
||||
#define TT_ASSETS_ICON_WIFI_SIGNAL_3_LOCKED TT_ASSET("wifi_signal_3_locked.png")
|
||||
#define TT_ASSETS_ICON_WIFI_SIGNAL_4 TT_ASSET("wifi_signal_4.png")
|
||||
#define TT_ASSETS_ICON_WIFI_SIGNAL_4_LOCKED TT_ASSET("wifi_signal_4_locked.png")
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "tactility_core.h"
|
||||
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#include "esp_partitions.h"
|
||||
#include "services/wifi/wifi_credentials.h"
|
||||
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#define TAG "tactility"
|
||||
|
||||
// Initialize NVS
|
||||
static void tt_esp_nvs_init() {
|
||||
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");
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
TT_LOG_I(TAG, "nvs initialized");
|
||||
}
|
||||
|
||||
static void tt_esp_network_init() {
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
}
|
||||
|
||||
void tt_esp_init() {
|
||||
tt_esp_nvs_init();
|
||||
tt_esp_partitions_init();
|
||||
|
||||
tt_esp_network_init();
|
||||
|
||||
tt_wifi_credentials_init();
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void tt_esp_init();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,70 @@
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#include "esp_partitions.h"
|
||||
#include "esp_spiffs.h"
|
||||
#include "log.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
static const char* TAG = "filesystem";
|
||||
|
||||
static esp_err_t nvs_flash_init_safely() {
|
||||
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());
|
||||
result = nvs_flash_init();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static esp_err_t spiffs_init(esp_vfs_spiffs_conf_t* conf) {
|
||||
esp_err_t ret = esp_vfs_spiffs_register(conf);
|
||||
if (ret != ESP_OK) {
|
||||
if (ret == ESP_FAIL) {
|
||||
TT_LOG_E(TAG, "Failed to mount or format filesystem %s", conf->base_path);
|
||||
} else if (ret == ESP_ERR_NOT_FOUND) {
|
||||
TT_LOG_E(TAG, "Failed to find SPIFFS partition %s", conf->base_path);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to initialize SPIFFS %s (%s)", conf->base_path, esp_err_to_name(ret));
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
size_t total = -1, used = 0;
|
||||
ret = esp_spiffs_info(NULL, &total, &used);
|
||||
if (ret != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to get SPIFFS partition information for %s (%s)", conf->base_path, esp_err_to_name(ret));
|
||||
} else {
|
||||
TT_LOG_I(TAG, "Partition size for %s: total: %d, used: %d", conf->base_path, total, used);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t tt_esp_partitions_init() {
|
||||
ESP_ERROR_CHECK(nvs_flash_init_safely());
|
||||
|
||||
esp_vfs_spiffs_conf_t assets_spiffs = {
|
||||
.base_path = MOUNT_POINT_ASSETS,
|
||||
.partition_label = NULL,
|
||||
.max_files = 100,
|
||||
.format_if_mount_failed = false
|
||||
};
|
||||
|
||||
if (spiffs_init(&assets_spiffs) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
esp_vfs_spiffs_conf_t config_spiffs = {
|
||||
.base_path = MOUNT_POINT_CONFIG,
|
||||
.partition_label = "config",
|
||||
.max_files = 100,
|
||||
.format_if_mount_failed = false
|
||||
};
|
||||
|
||||
if (spiffs_init(&config_spiffs) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#define MOUNT_POINT_ASSETS "/assets"
|
||||
#define MOUNT_POINT_CONFIG "/config"
|
||||
|
||||
esp_err_t tt_esp_partitions_init();
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,22 @@
|
||||
#include "hardware_i.h"
|
||||
|
||||
#include "preferences.h"
|
||||
#include "sdcard_i.h"
|
||||
|
||||
#define TAG "hardware"
|
||||
|
||||
void tt_hardware_init(const HardwareConfig* config) {
|
||||
if (config->bootstrap != NULL) {
|
||||
TT_LOG_I(TAG, "Bootstrapping");
|
||||
tt_check(config->bootstrap(), "bootstrap failed");
|
||||
}
|
||||
|
||||
tt_sdcard_init();
|
||||
if (config->sdcard != NULL) {
|
||||
TT_LOG_I(TAG, "Mounting sdcard");
|
||||
tt_sdcard_mount(config->sdcard);
|
||||
}
|
||||
|
||||
tt_check(config->init_graphics, "lvlg init not set");
|
||||
tt_check(config->init_graphics(), "lvgl init failed");
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include "tactility_core.h"
|
||||
#include "sdcard.h"
|
||||
|
||||
typedef bool (*Bootstrap)();
|
||||
typedef bool (*InitGraphics)();
|
||||
|
||||
typedef void (*SetBacklightDuty)(uint8_t);
|
||||
typedef struct {
|
||||
/** Set backlight duty */
|
||||
SetBacklightDuty set_backlight_duty;
|
||||
} Display;
|
||||
|
||||
typedef struct {
|
||||
/**
|
||||
* Optional bootstrapping method (e.g. to turn peripherals on)
|
||||
* This is called after Tactility core init and before any other inits in the HardwareConfig.
|
||||
* */
|
||||
const Bootstrap _Nullable bootstrap;
|
||||
|
||||
/**
|
||||
* Initializes LVGL with all relevant hardware.
|
||||
* This includes the display and optional pointer devices (such as touch) or a keyboard.
|
||||
*/
|
||||
const InitGraphics init_graphics;
|
||||
|
||||
/**
|
||||
* An interface for display features such as setting the backlight.
|
||||
* This does nothing when a display isn't present.
|
||||
*/
|
||||
const Display display;
|
||||
|
||||
/**
|
||||
* An optional SD card interface.
|
||||
*/
|
||||
const SdCard* _Nullable sdcard;
|
||||
} HardwareConfig;
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "hardware_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void tt_hardware_init(const HardwareConfig* config);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "preferences.h"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
extern const Preferences preferences_esp;
|
||||
#else
|
||||
extern const Preferences preferences_memory;
|
||||
#endif
|
||||
|
||||
const Preferences* tt_preferences() {
|
||||
#ifdef ESP_PLATFORM
|
||||
return &preferences_esp;
|
||||
#else
|
||||
return &preferences_memory;
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef bool (*PreferencesHasBool)(const char* namespace, const char* key);
|
||||
typedef bool (*PreferencesHasInt32)(const char* namespace, const char* key);
|
||||
typedef bool (*PreferencesHasString)(const char* namespace, const char* key);
|
||||
|
||||
typedef bool (*PreferencesOptBool)(const char* namespace, const char* key, bool* out);
|
||||
typedef bool (*PreferencesOptInt32)(const char* namespace, const char* key, int32_t* out);
|
||||
typedef bool (*PreferencesOptString)(const char* namespace, const char* key, char* out, size_t* size);
|
||||
|
||||
typedef void (*PreferencesPutBool)(const char* namespace, const char* key, bool value);
|
||||
typedef void (*PreferencesPutInt32)(const char* namespace, const char* key, int32_t value);
|
||||
typedef void (*PreferencesPutString)(const char* namespace, const char* key, const char* value);
|
||||
|
||||
typedef struct {
|
||||
PreferencesHasBool has_bool;
|
||||
PreferencesHasInt32 has_int32;
|
||||
PreferencesHasString has_string;
|
||||
|
||||
PreferencesOptBool opt_bool;
|
||||
PreferencesOptInt32 opt_int32;
|
||||
PreferencesOptString opt_string;
|
||||
|
||||
PreferencesPutBool put_bool;
|
||||
PreferencesPutInt32 put_int32;
|
||||
PreferencesPutString put_string;
|
||||
} Preferences;
|
||||
|
||||
/**
|
||||
* @return an instance of Preferences.
|
||||
*/
|
||||
const Preferences* tt_preferences();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,108 @@
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include "preferences.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "tactility_core.h"
|
||||
|
||||
#define TAG "preferences"
|
||||
|
||||
static bool opt_bool(const char* namespace, const char* key, bool* out) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
return false;
|
||||
} else {
|
||||
uint8_t out_number;
|
||||
bool success = nvs_get_u8(handle, key, &out_number) == ESP_OK;
|
||||
nvs_close(handle);
|
||||
if (success) {
|
||||
*out = (bool)out_number;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
static bool opt_int32(const char* namespace, const char* key, int32_t* out) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
return false;
|
||||
} else {
|
||||
bool success = nvs_get_i32(handle, key, out) == ESP_OK;
|
||||
nvs_close(handle);
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
static bool opt_string(const char* namespace, const char* key, char* out, size_t* size) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
return false;
|
||||
} else {
|
||||
bool success = nvs_get_str(handle, key, out, size) == ESP_OK;
|
||||
nvs_close(handle);
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
static bool has_bool(const char* namespace, const char* key) {
|
||||
bool temp;
|
||||
return opt_bool(namespace, key, &temp);
|
||||
}
|
||||
|
||||
static bool has_int32(const char* namespace, const char* key) {
|
||||
int32_t temp;
|
||||
return opt_int32(namespace, key, &temp);
|
||||
}
|
||||
|
||||
static bool has_string(const char* namespace, const char* key) {
|
||||
char temp[128];
|
||||
size_t temp_size = 128;
|
||||
return opt_string(namespace, key, temp, &temp_size);
|
||||
}
|
||||
|
||||
static void put_bool(const char* namespace, const char* key, bool value) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace, NVS_READWRITE, &handle) == ESP_OK) {
|
||||
if (nvs_set_u8(handle, key, (uint8_t)value) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to write %s:%s", namespace, key);
|
||||
}
|
||||
nvs_close(handle);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "failed to open namespace %s for writing", namespace);
|
||||
}
|
||||
}
|
||||
|
||||
static void put_int32(const char* namespace, const char* key, int32_t value) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace, NVS_READWRITE, &handle) == ESP_OK) {
|
||||
if (nvs_set_i32(handle, key, value) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to write %s:%s", namespace, key);
|
||||
}
|
||||
nvs_close(handle);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "failed to open namespace %s for writing", namespace);
|
||||
}
|
||||
}
|
||||
|
||||
static void put_string(const char* namespace, const char* key, const char* text) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace, NVS_READWRITE, &handle) == ESP_OK) {
|
||||
nvs_set_str(handle, key, text);
|
||||
nvs_close(handle);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "failed to open namespace %s for writing", namespace);
|
||||
}
|
||||
}
|
||||
|
||||
const Preferences preferences_esp = {
|
||||
.has_bool = &has_bool,
|
||||
.has_int32 = &has_int32,
|
||||
.has_string = &has_string,
|
||||
.opt_bool = &opt_bool,
|
||||
.opt_int32 = &opt_int32,
|
||||
.opt_string = &opt_string,
|
||||
.put_bool = &put_bool,
|
||||
.put_int32 = &put_int32,
|
||||
.put_string = &put_string
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,109 @@
|
||||
#ifndef ESP_PLATFOM
|
||||
|
||||
#include "bundle.h"
|
||||
#include "preferences.h"
|
||||
#include <string.h>
|
||||
#include <tactility_core.h>
|
||||
|
||||
static Bundle* preferences_bundle;
|
||||
|
||||
static Bundle* get_preferences_bundle() {
|
||||
if (preferences_bundle == NULL) {
|
||||
preferences_bundle = tt_bundle_alloc();
|
||||
}
|
||||
return preferences_bundle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string that is effectively "namespace:key" so we can create a single map (bundle)
|
||||
* to store all the key/value pairs.
|
||||
*
|
||||
* @param[in] namespace
|
||||
* @param[in] key
|
||||
* @param[out] out
|
||||
*/
|
||||
static void get_bundle_key(const char* namespace, const char* key, char* out) {
|
||||
strcpy(out, namespace);
|
||||
size_t namespace_len = strlen(namespace);
|
||||
out[namespace_len] = ':';
|
||||
char* out_with_key_offset = &out[namespace_len + 1];
|
||||
strcpy(out_with_key_offset, key);
|
||||
}
|
||||
|
||||
static bool has_bool(const char* namespace, const char* key) {
|
||||
char bundle_key[128];
|
||||
get_bundle_key(namespace, key, bundle_key);
|
||||
return tt_bundle_has_bool(get_preferences_bundle(), bundle_key);
|
||||
}
|
||||
|
||||
static bool has_int32(const char* namespace, const char* key) {
|
||||
char bundle_key[128];
|
||||
get_bundle_key(namespace, key, bundle_key);
|
||||
return tt_bundle_has_int32(get_preferences_bundle(), bundle_key);
|
||||
}
|
||||
|
||||
static bool has_string(const char* namespace, const char* key) {
|
||||
char bundle_key[128];
|
||||
get_bundle_key(namespace, key, bundle_key);
|
||||
return tt_bundle_has_string(get_preferences_bundle(), bundle_key);
|
||||
}
|
||||
|
||||
static bool opt_bool(const char* namespace, const char* key, bool* out) {
|
||||
char bundle_key[128];
|
||||
get_bundle_key(namespace, key, bundle_key);
|
||||
return tt_bundle_opt_bool(get_preferences_bundle(), bundle_key, out);
|
||||
}
|
||||
|
||||
static bool opt_int32(const char* namespace, const char* key, int32_t* out) {
|
||||
char bundle_key[128];
|
||||
get_bundle_key(namespace, key, bundle_key);
|
||||
return tt_bundle_opt_int32(get_preferences_bundle(), bundle_key, out);
|
||||
}
|
||||
|
||||
static bool opt_string(const char* namespace, const char* key, char* out, size_t* size) {
|
||||
char bundle_key[128];
|
||||
get_bundle_key(namespace, key, bundle_key);
|
||||
char* bundle_out = NULL;
|
||||
if (tt_bundle_opt_string(get_preferences_bundle(), bundle_key, &bundle_out)) {
|
||||
tt_assert(bundle_out != NULL);
|
||||
size_t found_length = strlen(bundle_out);
|
||||
tt_check(found_length <= (*size + 1), "output buffer not large enough");
|
||||
*size = found_length;
|
||||
strcpy(out, bundle_out);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void put_bool(const char* namespace, const char* key, bool value) {
|
||||
char bundle_key[128];
|
||||
get_bundle_key(namespace, key, bundle_key);
|
||||
return tt_bundle_put_bool(get_preferences_bundle(), bundle_key, value);
|
||||
}
|
||||
|
||||
static void put_int32(const char* namespace, const char* key, int32_t value) {
|
||||
char bundle_key[128];
|
||||
get_bundle_key(namespace, key, bundle_key);
|
||||
return tt_bundle_put_int32(get_preferences_bundle(), bundle_key, value);
|
||||
}
|
||||
|
||||
static void put_string(const char* namespace, const char* key, const char* text) {
|
||||
char bundle_key[128];
|
||||
get_bundle_key(namespace, key, bundle_key);
|
||||
return tt_bundle_put_string(get_preferences_bundle(), bundle_key, text);
|
||||
}
|
||||
|
||||
const Preferences preferences_memory = {
|
||||
.has_bool = &has_bool,
|
||||
.has_int32 = &has_int32,
|
||||
.has_string = &has_string,
|
||||
.opt_bool = &opt_bool,
|
||||
.opt_int32 = &opt_int32,
|
||||
.opt_string = &opt_string,
|
||||
.put_bool = &put_bool,
|
||||
.put_int32 = &put_int32,
|
||||
.put_string = &put_string
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,91 @@
|
||||
#include "sdcard.h"
|
||||
|
||||
#include "mutex.h"
|
||||
#include "tactility_core.h"
|
||||
|
||||
#define TAG "sdcard"
|
||||
|
||||
static Mutex* mutex = NULL;
|
||||
|
||||
typedef struct {
|
||||
const SdCard* sdcard;
|
||||
void* context;
|
||||
} MountData;
|
||||
|
||||
static MountData data = {
|
||||
.sdcard = NULL,
|
||||
.context = NULL
|
||||
};
|
||||
|
||||
void tt_sdcard_init() {
|
||||
if (mutex == NULL) {
|
||||
mutex = tt_mutex_alloc(MutexTypeRecursive);
|
||||
}
|
||||
}
|
||||
|
||||
static bool sdcard_lock(uint32_t timeout_ticks) {
|
||||
return tt_mutex_acquire(mutex, timeout_ticks) == TtStatusOk;
|
||||
}
|
||||
|
||||
static void sdcard_unlock() {
|
||||
tt_mutex_release(mutex);
|
||||
}
|
||||
|
||||
bool tt_sdcard_mount(const SdCard* sdcard) {
|
||||
TT_LOG_I(TAG, "Mounting");
|
||||
|
||||
if (data.sdcard != NULL) {
|
||||
TT_LOG_E(TAG, "Failed to mount: already mounted");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sdcard_lock(100)) {
|
||||
void* context = sdcard->mount(TT_SDCARD_MOUNT_POINT);
|
||||
data = (MountData) {
|
||||
.context = context,
|
||||
.sdcard = sdcard
|
||||
};
|
||||
sdcard_unlock();
|
||||
if (data.context != NULL) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to lock");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
SdcardState tt_sdcard_get_state() {
|
||||
if (data.context == NULL) {
|
||||
return SdcardStateUnmounted;
|
||||
} else if (data.sdcard->is_mounted(data.context)) {
|
||||
return SdcardStateMounted;
|
||||
} else {
|
||||
return SdcardStateError;
|
||||
}
|
||||
}
|
||||
|
||||
bool tt_sdcard_unmount(uint32_t timeout_ticks) {
|
||||
TT_LOG_I(TAG, "Unmounting");
|
||||
bool result = false;
|
||||
|
||||
if (sdcard_lock(timeout_ticks)) {
|
||||
if (data.sdcard != NULL) {
|
||||
data.sdcard->unmount(data.context);
|
||||
data = (MountData) {
|
||||
.context = NULL,
|
||||
.sdcard = NULL
|
||||
};
|
||||
result = true;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Can't unmount: nothing mounted");
|
||||
}
|
||||
sdcard_unlock();
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to lock in %lu ticks", timeout_ticks);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include "tactility_core.h"
|
||||
|
||||
#define TT_SDCARD_MOUNT_POINT "/sdcard"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void* (*SdcardMount)(const char* mount_path);
|
||||
typedef void (*SdcardUnmount)(void* context);
|
||||
typedef bool (*SdcardIsMounted)(void* context);
|
||||
|
||||
typedef enum {
|
||||
SdcardStateMounted,
|
||||
SdcardStateUnmounted,
|
||||
SdcardStateError,
|
||||
} SdcardState;
|
||||
|
||||
typedef enum {
|
||||
SdcardMountBehaviourAtBoot, /** Only mount at boot */
|
||||
SdcardMountBehaviourAnytime /** Mount/dismount any time */
|
||||
} SdcardMountBehaviour;
|
||||
|
||||
typedef struct {
|
||||
SdcardMount mount;
|
||||
SdcardUnmount unmount;
|
||||
SdcardIsMounted is_mounted;
|
||||
SdcardMountBehaviour mount_behaviour;
|
||||
} SdCard;
|
||||
|
||||
bool tt_sdcard_mount(const SdCard* sdcard);
|
||||
SdcardState tt_sdcard_get_state();
|
||||
bool tt_sdcard_unmount();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void tt_sdcard_init();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,62 @@
|
||||
#include "log.h"
|
||||
#include "service_i.h"
|
||||
#include "tactility_core.h"
|
||||
|
||||
// region Alloc/free
|
||||
|
||||
ServiceData* tt_service_alloc(const ServiceManifest* manifest) {
|
||||
ServiceData* data = malloc(sizeof(ServiceData));
|
||||
*data = (ServiceData) {
|
||||
.manifest = manifest,
|
||||
.mutex = tt_mutex_alloc(MutexTypeNormal),
|
||||
.data = NULL
|
||||
};
|
||||
return data;
|
||||
}
|
||||
|
||||
void tt_service_free(ServiceData* data) {
|
||||
tt_assert(data);
|
||||
tt_mutex_free(data->mutex);
|
||||
free(data);
|
||||
}
|
||||
|
||||
// endregion Alloc/free
|
||||
|
||||
// region Internal
|
||||
|
||||
static void tt_service_lock(ServiceData * data) {
|
||||
tt_mutex_acquire(data->mutex, TtWaitForever);
|
||||
}
|
||||
|
||||
static void tt_service_unlock(ServiceData* data) {
|
||||
tt_mutex_release(data->mutex);
|
||||
}
|
||||
|
||||
// endregion Internal
|
||||
|
||||
// region Getters & Setters
|
||||
|
||||
const ServiceManifest* tt_service_get_manifest(Service service) {
|
||||
ServiceData* data = (ServiceData*)service;
|
||||
tt_service_lock(data);
|
||||
const ServiceManifest* manifest = data->manifest;
|
||||
tt_service_unlock(data);
|
||||
return manifest;
|
||||
}
|
||||
|
||||
void tt_service_set_data(Service service, void* value) {
|
||||
ServiceData* data = (ServiceData*)service;
|
||||
tt_service_lock(data);
|
||||
data->data = value;
|
||||
tt_service_unlock(data);
|
||||
}
|
||||
|
||||
void* _Nullable tt_service_get_data(Service service) {
|
||||
ServiceData* data = (ServiceData*)service;
|
||||
tt_service_lock(data);
|
||||
void* value = data->data;
|
||||
tt_service_unlock(data);
|
||||
return value;
|
||||
}
|
||||
|
||||
// endregion Getters & Setters
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "service_manifest.h"
|
||||
|
||||
typedef void* Service;
|
||||
|
||||
const ServiceManifest* tt_service_get_manifest(Service service);
|
||||
|
||||
void tt_service_set_data(Service service, void* value);
|
||||
void* _Nullable tt_service_get_data(Service service);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "service.h"
|
||||
|
||||
#include "mutex.h"
|
||||
#include "service_manifest.h"
|
||||
|
||||
typedef struct {
|
||||
Mutex* mutex;
|
||||
const ServiceManifest* manifest;
|
||||
void* data;
|
||||
} ServiceData;
|
||||
|
||||
ServiceData* tt_service_alloc(const ServiceManifest* manifest);
|
||||
void tt_service_free(ServiceData* service);
|
||||
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include "tactility_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void* Service;
|
||||
|
||||
typedef void (*ServiceOnStart)(Service service);
|
||||
typedef void (*ServiceOnStop)(Service service);
|
||||
|
||||
typedef struct {
|
||||
/**
|
||||
* The identifier by which the app is launched by the system and other apps.
|
||||
*/
|
||||
const char* id;
|
||||
|
||||
/**
|
||||
* Non-blocking method to call when service is started.
|
||||
*/
|
||||
const ServiceOnStart _Nullable on_start;
|
||||
|
||||
/**
|
||||
* Non-blocking method to call when service is stopped.
|
||||
*/
|
||||
const ServiceOnStop _Nullable on_stop;
|
||||
|
||||
} ServiceManifest;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,145 @@
|
||||
#include "service_registry.h"
|
||||
|
||||
#include "m-dict.h"
|
||||
#include "m_cstr_dup.h"
|
||||
#include "mutex.h"
|
||||
#include "service_i.h"
|
||||
#include "service_manifest.h"
|
||||
#include "tactility_core.h"
|
||||
|
||||
#define TAG "service_registry"
|
||||
|
||||
DICT_DEF2(ServiceManifestDict, const char*, M_CSTR_DUP_OPLIST, const ServiceManifest*, M_PTR_OPLIST)
|
||||
DICT_DEF2(ServiceInstanceDict, const char*, M_CSTR_DUP_OPLIST, const ServiceData*, M_PTR_OPLIST)
|
||||
|
||||
#define APP_REGISTRY_FOR_EACH(manifest_var_name, code_to_execute) \
|
||||
{ \
|
||||
service_registry_manifest_lock(); \
|
||||
ServiceManifestDict_it_t it; \
|
||||
for (ServiceManifestDict_it(it, service_manifest_dict); !ServiceManifestDict_end_p(it); ServiceManifestDict_next(it)) { \
|
||||
const ServiceManifest*(manifest_var_name) = ServiceManifestDict_cref(it)->value; \
|
||||
code_to_execute; \
|
||||
} \
|
||||
service_registry_manifest_unlock(); \
|
||||
}
|
||||
|
||||
static ServiceManifestDict_t service_manifest_dict;
|
||||
static ServiceInstanceDict_t service_instance_dict;
|
||||
static Mutex* manifest_mutex = NULL;
|
||||
static Mutex* instance_mutex = NULL;
|
||||
|
||||
void tt_service_registry_init() {
|
||||
tt_assert(manifest_mutex == NULL);
|
||||
manifest_mutex = tt_mutex_alloc(MutexTypeNormal);
|
||||
ServiceManifestDict_init(service_manifest_dict);
|
||||
|
||||
tt_assert(instance_mutex == NULL);
|
||||
instance_mutex = tt_mutex_alloc(MutexTypeNormal);
|
||||
ServiceInstanceDict_init(service_instance_dict);
|
||||
}
|
||||
|
||||
void service_registry_instance_lock() {
|
||||
tt_assert(instance_mutex != NULL);
|
||||
tt_mutex_acquire(instance_mutex, TtWaitForever);
|
||||
}
|
||||
|
||||
void service_registry_instance_unlock() {
|
||||
tt_assert(instance_mutex != NULL);
|
||||
tt_mutex_release(instance_mutex);
|
||||
}
|
||||
|
||||
void service_registry_manifest_lock() {
|
||||
tt_assert(manifest_mutex != NULL);
|
||||
tt_mutex_acquire(manifest_mutex, TtWaitForever);
|
||||
}
|
||||
|
||||
void service_registry_manifest_unlock() {
|
||||
tt_assert(manifest_mutex != NULL);
|
||||
tt_mutex_release(manifest_mutex);
|
||||
}
|
||||
void tt_service_registry_add(const ServiceManifest* manifest) {
|
||||
TT_LOG_I(TAG, "adding %s", manifest->id);
|
||||
|
||||
service_registry_manifest_lock();
|
||||
ServiceManifestDict_set_at(service_manifest_dict, manifest->id, manifest);
|
||||
service_registry_manifest_unlock();
|
||||
}
|
||||
|
||||
void tt_service_registry_remove(const ServiceManifest* manifest) {
|
||||
TT_LOG_I(TAG, "removing %s", manifest->id);
|
||||
service_registry_manifest_lock();
|
||||
ServiceManifestDict_erase(service_manifest_dict, manifest->id);
|
||||
service_registry_manifest_unlock();
|
||||
}
|
||||
|
||||
const ServiceManifest* _Nullable tt_service_registry_find_manifest_by_id(const char* id) {
|
||||
service_registry_manifest_lock();
|
||||
const ServiceManifest** _Nullable manifest = ServiceManifestDict_get(service_manifest_dict, id);
|
||||
service_registry_manifest_unlock();
|
||||
return (manifest != NULL) ? *manifest : NULL;
|
||||
}
|
||||
|
||||
ServiceData* _Nullable service_registry_find_instance_by_id(const char* id) {
|
||||
service_registry_instance_lock();
|
||||
const ServiceData** _Nullable service_ptr = ServiceInstanceDict_get(service_instance_dict, id);
|
||||
if (service_ptr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ServiceData* service = (ServiceData*)*service_ptr;
|
||||
service_registry_instance_unlock();
|
||||
return service;
|
||||
}
|
||||
|
||||
void tt_service_registry_for_each_manifest(ServiceManifestCallback callback, void* _Nullable context) {
|
||||
APP_REGISTRY_FOR_EACH(manifest, {
|
||||
callback(manifest, context);
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: return proper error/status instead of BOOL
|
||||
bool tt_service_registry_start(const char* service_id) {
|
||||
TT_LOG_I(TAG, "starting %s", service_id);
|
||||
const ServiceManifest* manifest = tt_service_registry_find_manifest_by_id(service_id);
|
||||
if (manifest == NULL) {
|
||||
TT_LOG_E(TAG, "manifest not found for service %s", service_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
Service service = tt_service_alloc(manifest);
|
||||
manifest->on_start(service);
|
||||
|
||||
service_registry_instance_lock();
|
||||
ServiceInstanceDict_set_at(service_instance_dict, manifest->id, service);
|
||||
service_registry_instance_unlock();
|
||||
TT_LOG_I(TAG, "started %s", service_id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Service _Nullable tt_service_find(const char* service_id) {
|
||||
service_registry_instance_lock();
|
||||
const ServiceData** _Nullable service_ptr = ServiceInstanceDict_get(service_instance_dict, service_id);
|
||||
const ServiceData* service = service_ptr ? *service_ptr : NULL;
|
||||
service_registry_instance_unlock();
|
||||
return (Service)service;
|
||||
}
|
||||
|
||||
bool tt_service_registry_stop(const char* service_id) {
|
||||
TT_LOG_I(TAG, "stopping %s", service_id);
|
||||
ServiceData* service = service_registry_find_instance_by_id(service_id);
|
||||
if (service == NULL) {
|
||||
TT_LOG_W(TAG, "service not running: %s", service_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
service->manifest->on_stop(service);
|
||||
tt_service_free(service);
|
||||
|
||||
service_registry_instance_lock();
|
||||
ServiceInstanceDict_erase(service_instance_dict, service_id);
|
||||
service_registry_instance_unlock();
|
||||
|
||||
TT_LOG_I(TAG, "stopped %s", service_id);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "service_manifest.h"
|
||||
#include "tactility_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef void (*ServiceManifestCallback)(const ServiceManifest*, void* context);
|
||||
|
||||
void tt_service_registry_init();
|
||||
|
||||
void tt_service_registry_add(const ServiceManifest* manifest);
|
||||
void tt_service_registry_remove(const ServiceManifest* manifest);
|
||||
const ServiceManifest _Nullable* tt_service_registry_find_manifest_by_id(const char* id);
|
||||
void tt_service_registry_for_each_manifest(ServiceManifestCallback callback, void* _Nullable context);
|
||||
|
||||
bool tt_service_registry_start(const char* service_id);
|
||||
bool tt_service_registry_stop(const char* service_id);
|
||||
|
||||
Service _Nullable tt_service_find(const char* service_id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,104 @@
|
||||
#include <dirent.h>
|
||||
|
||||
#include "mutex.h"
|
||||
#include "service.h"
|
||||
#include "tactility_core.h"
|
||||
#include "tactility_headless.h"
|
||||
|
||||
#define TAG "sdcard_service"
|
||||
|
||||
static int32_t sdcard_task(void* context);
|
||||
|
||||
typedef struct {
|
||||
Mutex* mutex;
|
||||
Thread* thread;
|
||||
SdcardState last_state;
|
||||
bool interrupted;
|
||||
} ServiceData;
|
||||
|
||||
static ServiceData* service_data_alloc() {
|
||||
ServiceData* data = malloc(sizeof(ServiceData));
|
||||
*data = (ServiceData) {
|
||||
.mutex = tt_mutex_alloc(MutexTypeNormal),
|
||||
.thread = tt_thread_alloc_ex(
|
||||
"sdcard",
|
||||
3000, // Minimum is ~2800 @ ESP-IDF 5.1.2 when ejecting sdcard
|
||||
&sdcard_task,
|
||||
data
|
||||
),
|
||||
.last_state = -1,
|
||||
.interrupted = false
|
||||
};
|
||||
tt_thread_set_priority(data->thread, ThreadPriorityLow);
|
||||
return data;
|
||||
}
|
||||
|
||||
static void service_data_free(ServiceData* data) {
|
||||
tt_mutex_free(data->mutex);
|
||||
tt_thread_free(data->thread);
|
||||
}
|
||||
|
||||
static void service_data_lock(ServiceData* data) {
|
||||
tt_check(tt_mutex_acquire(data->mutex, TtWaitForever) == TtStatusOk);
|
||||
}
|
||||
|
||||
static void service_data_unlock(ServiceData* data) {
|
||||
tt_check(tt_mutex_release(data->mutex) == TtStatusOk);
|
||||
}
|
||||
|
||||
static int32_t sdcard_task(void* context) {
|
||||
ServiceData* data = (ServiceData*)context;
|
||||
|
||||
bool interrupted = false;
|
||||
|
||||
do {
|
||||
service_data_lock(data);
|
||||
|
||||
interrupted = data->interrupted;
|
||||
|
||||
SdcardState new_state = tt_sdcard_get_state();
|
||||
|
||||
if (new_state == SdcardStateError) {
|
||||
TT_LOG_W(TAG, "Sdcard error - unmounting. Did you eject the card in an unsafe manner?");
|
||||
tt_sdcard_unmount();
|
||||
}
|
||||
|
||||
if (new_state != data->last_state) {
|
||||
data->last_state = new_state;
|
||||
}
|
||||
|
||||
service_data_unlock(data);
|
||||
tt_delay_ms(2000);
|
||||
} while (!interrupted);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void on_start(Service service) {
|
||||
if (tt_get_hardware_config()->sdcard != NULL) {
|
||||
ServiceData* data = service_data_alloc();
|
||||
tt_service_set_data(service, data);
|
||||
tt_thread_start(data->thread);
|
||||
} else {
|
||||
TT_LOG_I(TAG, "task not started due to config");
|
||||
}
|
||||
}
|
||||
|
||||
static void on_stop(Service service) {
|
||||
ServiceData* data = tt_service_get_data(service);
|
||||
if (data != NULL) {
|
||||
service_data_lock(data);
|
||||
data->interrupted = true;
|
||||
service_data_unlock(data);
|
||||
|
||||
tt_thread_join(data->thread);
|
||||
|
||||
service_data_free(data);
|
||||
}
|
||||
}
|
||||
|
||||
const ServiceManifest sdcard_service = {
|
||||
.id = "sdcard",
|
||||
.on_start = &on_start,
|
||||
.on_stop = &on_stop
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,134 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "pubsub.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "esp_wifi.h"
|
||||
#else
|
||||
#include <stdint.h>
|
||||
// From esp_wifi_types.h in ESP-IDF 5.2
|
||||
typedef enum {
|
||||
WIFI_AUTH_OPEN = 0, /**< authenticate mode : open */
|
||||
WIFI_AUTH_WEP, /**< authenticate mode : WEP */
|
||||
WIFI_AUTH_WPA_PSK, /**< authenticate mode : WPA_PSK */
|
||||
WIFI_AUTH_WPA2_PSK, /**< authenticate mode : WPA2_PSK */
|
||||
WIFI_AUTH_WPA_WPA2_PSK, /**< authenticate mode : WPA_WPA2_PSK */
|
||||
WIFI_AUTH_ENTERPRISE, /**< authenticate mode : WiFi EAP security */
|
||||
WIFI_AUTH_WPA2_ENTERPRISE = WIFI_AUTH_ENTERPRISE, /**< authenticate mode : WiFi EAP security */
|
||||
WIFI_AUTH_WPA3_PSK, /**< authenticate mode : WPA3_PSK */
|
||||
WIFI_AUTH_WPA2_WPA3_PSK, /**< authenticate mode : WPA2_WPA3_PSK */
|
||||
WIFI_AUTH_WAPI_PSK, /**< authenticate mode : WAPI_PSK */
|
||||
WIFI_AUTH_OWE, /**< authenticate mode : OWE */
|
||||
WIFI_AUTH_WPA3_ENT_192, /**< authenticate mode : WPA3_ENT_SUITE_B_192_BIT */
|
||||
WIFI_AUTH_WPA3_EXT_PSK, /**< authenticate mode : WPA3_PSK_EXT_KEY */
|
||||
WIFI_AUTH_WPA3_EXT_PSK_MIXED_MODE, /**< authenticate mode: WPA3_PSK + WPA3_PSK_EXT_KEY */
|
||||
WIFI_AUTH_MAX
|
||||
} wifi_auth_mode_t;
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
/** Radio was turned on */
|
||||
WifiEventTypeRadioStateOn,
|
||||
/** Radio is turning on. */
|
||||
WifiEventTypeRadioStateOnPending,
|
||||
/** Radio is turned off */
|
||||
WifiEventTypeRadioStateOff,
|
||||
/** Radio is turning off */
|
||||
WifiEventTypeRadioStateOffPending,
|
||||
/** Started scanning for access points */
|
||||
WifiEventTypeScanStarted,
|
||||
/** Finished scanning for access points */ // TODO: 1 second validity
|
||||
WifiEventTypeScanFinished,
|
||||
WifiEventTypeDisconnected,
|
||||
WifiEventTypeConnectionPending,
|
||||
WifiEventTypeConnectionSuccess,
|
||||
WifiEventTypeConnectionFailed
|
||||
} WifiEventType;
|
||||
|
||||
typedef enum {
|
||||
WIFI_RADIO_ON_PENDING,
|
||||
WIFI_RADIO_ON,
|
||||
WIFI_RADIO_CONNECTION_PENDING,
|
||||
WIFI_RADIO_CONNECTION_ACTIVE,
|
||||
WIFI_RADIO_OFF_PENDING,
|
||||
WIFI_RADIO_OFF
|
||||
} WifiRadioState;
|
||||
|
||||
typedef struct {
|
||||
WifiEventType type;
|
||||
} WifiEvent;
|
||||
|
||||
typedef struct {
|
||||
uint8_t ssid[33];
|
||||
int8_t rssi;
|
||||
wifi_auth_mode_t auth_mode;
|
||||
} WifiApRecord;
|
||||
|
||||
/**
|
||||
* @brief Get wifi pubsub
|
||||
* @return PubSub*
|
||||
*/
|
||||
PubSub* wifi_get_pubsub();
|
||||
|
||||
WifiRadioState wifi_get_radio_state();
|
||||
/**
|
||||
* @brief Request scanning update. Returns immediately. Results are through pubsub.
|
||||
*/
|
||||
void wifi_scan();
|
||||
|
||||
/**
|
||||
* @return true if wifi is actively scanning
|
||||
*/
|
||||
bool wifi_is_scanning();
|
||||
|
||||
/**
|
||||
* @brief Returns the access points from the last scan (if any). It only contains public APs.
|
||||
* @param records the allocated buffer to store the records in
|
||||
* @param limit the maximum amount of records to store
|
||||
*/
|
||||
void wifi_get_scan_results(WifiApRecord records[], uint16_t limit, uint16_t* result_count);
|
||||
|
||||
/**
|
||||
* @brief Overrides the default scan result size of 16.
|
||||
* @param records the record limit for the scan result (84 bytes per record!)
|
||||
*/
|
||||
void wifi_set_scan_records(uint16_t records);
|
||||
|
||||
/**
|
||||
* @brief Enable/disable the radio. Ignores input if desired state matches current state.
|
||||
* @param enabled
|
||||
*/
|
||||
void wifi_set_enabled(bool enabled);
|
||||
|
||||
/**
|
||||
* @brief Connect to a network. Disconnects any existing connection.
|
||||
* Returns immediately but runs in the background. Results are through pubsub.
|
||||
* @param ssid
|
||||
* @param password
|
||||
*/
|
||||
void wifi_connect(const char* ssid, const char _Nullable password[64]);
|
||||
|
||||
/**
|
||||
* @brief Disconnect from the access point. Doesn't have any effect when not connected.
|
||||
*/
|
||||
void wifi_disconnect();
|
||||
|
||||
/**
|
||||
* Return true if the connection isn't unencrypted.
|
||||
*/
|
||||
bool wifi_is_connection_secure();
|
||||
|
||||
/**
|
||||
* Returns the RSSI value (negative number) or return 1 when not connected
|
||||
*/
|
||||
int wifi_get_rssi();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TT_WIFI_CREDENTIALS_PASSWORD_LIMIT 64 // Should be equal to wifi_sta_config_t.password
|
||||
// TODO: Move to config file
|
||||
#define TT_WIFI_CREDENTIALS_LIMIT 16
|
||||
|
||||
void tt_wifi_credentials_init();
|
||||
|
||||
bool tt_wifi_credentials_contains(const char* ssid);
|
||||
|
||||
bool tt_wifi_credentials_get(const char* ssid, char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT]);
|
||||
|
||||
bool tt_wifi_credentials_set(const char* ssid, char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT]);
|
||||
|
||||
bool tt_wifi_credentials_remove(const char* ssid);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,235 @@
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#include "wifi_credentials.h"
|
||||
|
||||
#include "nvs_flash.h"
|
||||
#include "log.h"
|
||||
#include "hash.h"
|
||||
#include "check.h"
|
||||
#include "mutex.h"
|
||||
#include "secure.h"
|
||||
|
||||
#define TAG "wifi_credentials"
|
||||
#define TT_NVS_NAMESPACE "tt_wifi_cred" // limited by NVS_KEY_NAME_MAX_SIZE
|
||||
#define TT_NVS_PARTITION "nvs"
|
||||
|
||||
static void hash_reset_all();
|
||||
|
||||
// region Hash
|
||||
|
||||
static Mutex* hash_mutex = NULL;
|
||||
static int8_t hash_index = -1;
|
||||
static uint32_t hashes[TT_WIFI_CREDENTIALS_LIMIT] = { 0 };
|
||||
|
||||
static void hash_init() {
|
||||
tt_assert(hash_mutex == NULL);
|
||||
hash_mutex = tt_mutex_alloc(MutexTypeNormal);
|
||||
hash_reset_all();
|
||||
}
|
||||
|
||||
static void tt_hash_mutex_lock() {
|
||||
tt_assert(tt_mutex_acquire(hash_mutex, 100) == TtStatusOk);
|
||||
}
|
||||
|
||||
static void tt_hash_mutex_unlock() {
|
||||
tt_assert(tt_mutex_release(hash_mutex) == TtStatusOk);
|
||||
}
|
||||
|
||||
static int hash_find_value(uint32_t hash) {
|
||||
tt_hash_mutex_lock();
|
||||
for (int i = 0; i < hash_index; ++i) {
|
||||
if (hashes[i] == hash) {
|
||||
tt_hash_mutex_unlock();
|
||||
return i;
|
||||
}
|
||||
}
|
||||
tt_hash_mutex_unlock();
|
||||
return -1;
|
||||
}
|
||||
|
||||
static bool hash_contains_value(uint32_t value) {
|
||||
return hash_find_value(value) != -1;
|
||||
}
|
||||
|
||||
static void hash_add(const char* ssid) {
|
||||
uint32_t hash = tt_hash_string_djb2(ssid);
|
||||
if (!hash_contains_value(hash)) {
|
||||
tt_hash_mutex_lock();
|
||||
tt_check((hash_index + 1) < TT_WIFI_CREDENTIALS_LIMIT, "exceeding wifi credentials list size");
|
||||
hash_index++;
|
||||
hashes[hash_index] = hash;
|
||||
tt_hash_mutex_unlock();
|
||||
}
|
||||
}
|
||||
|
||||
static void hash_reset_all() {
|
||||
hash_index = -1;
|
||||
}
|
||||
|
||||
// endregion Hash
|
||||
|
||||
// region Wi-Fi Credentials - static
|
||||
|
||||
static esp_err_t tt_wifi_credentials_nvs_open(nvs_handle_t* handle, nvs_open_mode_t mode) {
|
||||
return nvs_open(TT_NVS_NAMESPACE, NVS_READWRITE, handle);
|
||||
}
|
||||
|
||||
static void tt_wifi_credentials_nvs_close(nvs_handle_t handle) {
|
||||
nvs_close(handle);
|
||||
}
|
||||
|
||||
static bool tt_wifi_credentials_contains_in_flash(const char* ssid) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = tt_wifi_credentials_nvs_open(&handle, NVS_READONLY);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open NVS handle: %s", esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
hash_reset_all();
|
||||
|
||||
nvs_iterator_t iterator;
|
||||
result = nvs_entry_find(TT_NVS_PARTITION, TT_NVS_NAMESPACE, NVS_TYPE_BLOB, &iterator);
|
||||
bool contains_ssid = false;
|
||||
while (result == ESP_OK) {
|
||||
nvs_entry_info_t info;
|
||||
nvs_entry_info(iterator, &info); // Can omit error check if parameters are guaranteed to be non-NULL
|
||||
if (strcmp(info.key, ssid) == 0) {
|
||||
contains_ssid = true;
|
||||
break;
|
||||
}
|
||||
result = nvs_entry_next(&iterator);
|
||||
}
|
||||
|
||||
nvs_release_iterator(iterator);
|
||||
tt_wifi_credentials_nvs_close(handle);
|
||||
|
||||
return contains_ssid;
|
||||
}
|
||||
|
||||
// endregion Wi-Fi Credentials - static
|
||||
|
||||
// region Wi-Fi Credentials - public
|
||||
|
||||
bool tt_wifi_credentials_contains(const char* ssid) {
|
||||
uint32_t hash = tt_hash_string_djb2(ssid);
|
||||
if (hash_contains_value(hash)) {
|
||||
return tt_wifi_credentials_contains_in_flash(ssid);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void tt_wifi_credentials_init() {
|
||||
TT_LOG_I(TAG, "init started");
|
||||
hash_init();
|
||||
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = tt_wifi_credentials_nvs_open(&handle, NVS_READWRITE);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open NVS handle for init: %s", esp_err_to_name(result));
|
||||
return;
|
||||
}
|
||||
|
||||
nvs_iterator_t iterator;
|
||||
result = nvs_entry_find(TT_NVS_PARTITION, TT_NVS_NAMESPACE, NVS_TYPE_BLOB, &iterator);
|
||||
while (result == ESP_OK) {
|
||||
nvs_entry_info_t info;
|
||||
nvs_entry_info(iterator, &info); // Can omit error check if parameters are guaranteed to be non-NULL
|
||||
hash_add(info.key);
|
||||
result = nvs_entry_next(&iterator);
|
||||
}
|
||||
nvs_release_iterator(iterator);
|
||||
|
||||
tt_wifi_credentials_nvs_close(handle);
|
||||
TT_LOG_I(TAG, "init finished");
|
||||
}
|
||||
|
||||
bool tt_wifi_credentials_get(const char* ssid, char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT]) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = tt_wifi_credentials_nvs_open(&handle, NVS_READONLY);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open NVS handle: %s", esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
char password_encrypted[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT];
|
||||
size_t length = TT_WIFI_CREDENTIALS_PASSWORD_LIMIT;
|
||||
result = nvs_get_blob(handle, ssid, password_encrypted, &length);
|
||||
|
||||
uint8_t iv[16];
|
||||
tt_secure_get_iv_from_string(ssid, iv);
|
||||
int decrypt_result = tt_secure_decrypt(
|
||||
iv,
|
||||
(uint8_t*)password_encrypted,
|
||||
(uint8_t*)password,
|
||||
TT_WIFI_CREDENTIALS_PASSWORD_LIMIT
|
||||
);
|
||||
|
||||
if (decrypt_result != 0) {
|
||||
result = ESP_FAIL;
|
||||
TT_LOG_E(TAG, "Failed to decrypt credentials for \"%s\": %d", ssid, decrypt_result);
|
||||
}
|
||||
|
||||
if (result != ESP_OK && result != ESP_ERR_NVS_NOT_FOUND) {
|
||||
TT_LOG_E(TAG, "Failed to get credentials for \"%s\": %s", ssid, esp_err_to_name(result));
|
||||
}
|
||||
|
||||
tt_wifi_credentials_nvs_close(handle);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool tt_wifi_credentials_set(const char* ssid, char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT]) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = tt_wifi_credentials_nvs_open(&handle, NVS_READWRITE);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open NVS handle: %s", esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
char password_encrypted[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT];
|
||||
uint8_t iv[16];
|
||||
tt_secure_get_iv_from_string(ssid, iv);
|
||||
int encrypt_result = tt_secure_encrypt(
|
||||
iv,
|
||||
(uint8_t*)password,
|
||||
(uint8_t*)password_encrypted,
|
||||
TT_WIFI_CREDENTIALS_PASSWORD_LIMIT
|
||||
);
|
||||
|
||||
if (encrypt_result != 0) {
|
||||
result = ESP_FAIL;
|
||||
TT_LOG_E(TAG, "Failed to encrypt credentials for \"%s\": %d", ssid, encrypt_result);
|
||||
}
|
||||
|
||||
if (result == ESP_OK) {
|
||||
result = nvs_set_blob(handle, ssid, password_encrypted, TT_WIFI_CREDENTIALS_PASSWORD_LIMIT);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to get credentials for \"%s\": %s", ssid, esp_err_to_name(result));
|
||||
}
|
||||
}
|
||||
|
||||
tt_wifi_credentials_nvs_close(handle);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool tt_wifi_credentials_remove(const char* ssid) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = tt_wifi_credentials_nvs_open(&handle, NVS_READWRITE);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open NVS handle to store \"%s\": %s", ssid, esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
result = nvs_erase_key(handle, ssid);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to erase credentials for \"%s\": %s", ssid, esp_err_to_name(result));
|
||||
}
|
||||
|
||||
tt_wifi_credentials_nvs_close(handle);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
// end region Wi-Fi Credentials - public
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef ESP_TARGET
|
||||
|
||||
#include "wifi_credentials.h"
|
||||
#include "log.h"
|
||||
|
||||
#define TAG "wifi_credentials_mock"
|
||||
|
||||
static void hash_reset_all();
|
||||
|
||||
bool tt_wifi_credentials_contains(const char* ssid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void tt_wifi_credentials_init() {
|
||||
TT_LOG_I(TAG, "init");
|
||||
}
|
||||
|
||||
bool tt_wifi_credentials_get(const char* ssid, char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool tt_wifi_credentials_set(const char* ssid, char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool tt_wifi_credentials_remove(const char* ssid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,613 @@
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#include "wifi.h"
|
||||
|
||||
#include "assets.h"
|
||||
#include "check.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "log.h"
|
||||
#include "message_queue.h"
|
||||
#include "mutex.h"
|
||||
#include "pubsub.h"
|
||||
#include "service.h"
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
#define TAG "wifi"
|
||||
#define WIFI_SCAN_RECORD_LIMIT 16 // default, can be overridden
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
typedef struct {
|
||||
/** @brief Locking mechanism for modifying the Wifi instance */
|
||||
Mutex* mutex;
|
||||
/** @brief The public event bus */
|
||||
PubSub* pubsub;
|
||||
/** @brief The internal message queue */
|
||||
MessageQueue* queue;
|
||||
/** @brief The network interface when wifi is started */
|
||||
esp_netif_t* _Nullable netif;
|
||||
/** @brief Scanning results */
|
||||
wifi_ap_record_t* _Nullable scan_list;
|
||||
/** @brief The current item count in scan_list (-1 when scan_list is NULL) */
|
||||
uint16_t scan_list_count;
|
||||
/** @brief Maximum amount of records to scan (value > 0) */
|
||||
uint16_t scan_list_limit;
|
||||
bool scan_active;
|
||||
bool secure_connection;
|
||||
esp_event_handler_instance_t event_handler_any_id;
|
||||
esp_event_handler_instance_t event_handler_got_ip;
|
||||
EventGroupHandle_t event_group;
|
||||
WifiRadioState radio_state;
|
||||
} Wifi;
|
||||
|
||||
typedef enum {
|
||||
WifiMessageTypeRadioOn,
|
||||
WifiMessageTypeRadioOff,
|
||||
WifiMessageTypeScan,
|
||||
WifiMessageTypeConnect,
|
||||
WifiMessageTypeDisconnect
|
||||
} WifiMessageType;
|
||||
|
||||
typedef struct {
|
||||
uint8_t ssid[32];
|
||||
uint8_t password[64];
|
||||
} WifiConnectMessage;
|
||||
|
||||
typedef struct {
|
||||
WifiMessageType type;
|
||||
union {
|
||||
WifiConnectMessage connect_message;
|
||||
};
|
||||
} WifiMessage;
|
||||
|
||||
static Wifi* wifi_singleton = NULL;
|
||||
|
||||
// Forward declarations
|
||||
static void wifi_scan_list_free_safely(Wifi* wifi);
|
||||
static void wifi_disconnect_internal(Wifi* wifi);
|
||||
static void wifi_lock(Wifi* wifi);
|
||||
static void wifi_unlock(Wifi* wifi);
|
||||
|
||||
// region Alloc
|
||||
|
||||
static Wifi* wifi_alloc() {
|
||||
Wifi* instance = malloc(sizeof(Wifi));
|
||||
instance->mutex = tt_mutex_alloc(MutexTypeRecursive);
|
||||
instance->pubsub = tt_pubsub_alloc();
|
||||
// TODO: Deal with messages that come in while an action is ongoing
|
||||
// for example: when scanning and you turn off the radio, the scan should probably stop or turning off
|
||||
// the radio should disable the on/off button in the app as it is pending.
|
||||
instance->queue = tt_message_queue_alloc(1, sizeof(WifiMessage));
|
||||
instance->netif = NULL;
|
||||
instance->scan_active = false;
|
||||
instance->scan_list = NULL;
|
||||
instance->scan_list_count = 0;
|
||||
instance->scan_list_limit = WIFI_SCAN_RECORD_LIMIT;
|
||||
instance->event_handler_any_id = NULL;
|
||||
instance->event_handler_got_ip = NULL;
|
||||
instance->event_group = xEventGroupCreate();
|
||||
instance->radio_state = WIFI_RADIO_OFF;
|
||||
instance->secure_connection = false;
|
||||
return instance;
|
||||
}
|
||||
|
||||
static void wifi_free(Wifi* instance) {
|
||||
tt_mutex_free(instance->mutex);
|
||||
tt_pubsub_free(instance->pubsub);
|
||||
tt_message_queue_free(instance->queue);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
// endregion Alloc
|
||||
|
||||
// region Public functions
|
||||
|
||||
PubSub* wifi_get_pubsub() {
|
||||
tt_assert(wifi_singleton);
|
||||
return wifi_singleton->pubsub;
|
||||
}
|
||||
|
||||
WifiRadioState wifi_get_radio_state() {
|
||||
return wifi_singleton->radio_state;
|
||||
}
|
||||
|
||||
void wifi_scan() {
|
||||
tt_assert(wifi_singleton);
|
||||
WifiMessage message = {.type = WifiMessageTypeScan};
|
||||
// No need to lock for queue
|
||||
tt_message_queue_put(wifi_singleton->queue, &message, 100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
bool wifi_is_scanning() {
|
||||
tt_assert(wifi_singleton);
|
||||
return wifi_singleton->scan_active;
|
||||
}
|
||||
|
||||
void wifi_connect(const char* ssid, const char _Nullable password[64]) {
|
||||
tt_assert(wifi_singleton);
|
||||
tt_check(strlen(ssid) <= 32);
|
||||
WifiMessage message = {.type = WifiMessageTypeConnect};
|
||||
memcpy(message.connect_message.ssid, ssid, 32);
|
||||
if (password != NULL) {
|
||||
memcpy(message.connect_message.password, password, 64);
|
||||
} else {
|
||||
message.connect_message.password[0] = 0;
|
||||
}
|
||||
tt_message_queue_put(wifi_singleton->queue, &message, 100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
void wifi_disconnect() {
|
||||
tt_assert(wifi_singleton);
|
||||
WifiMessage message = {.type = WifiMessageTypeDisconnect};
|
||||
tt_message_queue_put(wifi_singleton->queue, &message, 100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
void wifi_set_scan_records(uint16_t records) {
|
||||
tt_assert(wifi_singleton);
|
||||
if (records != wifi_singleton->scan_list_limit) {
|
||||
wifi_scan_list_free_safely(wifi_singleton);
|
||||
wifi_singleton->scan_list_limit = records;
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_get_scan_results(WifiApRecord records[], uint16_t limit, uint16_t* result_count) {
|
||||
tt_check(wifi_singleton);
|
||||
tt_check(result_count);
|
||||
|
||||
if (wifi_singleton->scan_list_count == 0) {
|
||||
*result_count = 0;
|
||||
} else {
|
||||
uint16_t i = 0;
|
||||
TT_LOG_I(TAG, "processing up to %d APs", wifi_singleton->scan_list_count);
|
||||
uint16_t last_index = TT_MIN(wifi_singleton->scan_list_count, limit);
|
||||
for (; i < last_index; ++i) {
|
||||
memcpy(records[i].ssid, wifi_singleton->scan_list[i].ssid, 33);
|
||||
records[i].rssi = wifi_singleton->scan_list[i].rssi;
|
||||
records[i].auth_mode = wifi_singleton->scan_list[i].authmode;
|
||||
}
|
||||
// The index already overflowed right before the for-loop was terminated,
|
||||
// so it effectively became the list count:
|
||||
*result_count = i;
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_set_enabled(bool enabled) {
|
||||
if (enabled) {
|
||||
WifiMessage message = {.type = WifiMessageTypeRadioOn};
|
||||
// No need to lock for queue
|
||||
tt_message_queue_put(wifi_singleton->queue, &message, 100 / portTICK_PERIOD_MS);
|
||||
} else {
|
||||
WifiMessage message = {.type = WifiMessageTypeRadioOff};
|
||||
// No need to lock for queue
|
||||
tt_message_queue_put(wifi_singleton->queue, &message, 100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
bool wifi_is_connection_secure() {
|
||||
tt_check(wifi_singleton);
|
||||
return wifi_singleton->secure_connection;
|
||||
}
|
||||
|
||||
int wifi_get_rssi() {
|
||||
static int rssi = 0;
|
||||
if (esp_wifi_sta_get_rssi(&rssi) == ESP_OK) {
|
||||
return rssi;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// endregion Public functions
|
||||
|
||||
static void wifi_lock(Wifi* wifi) {
|
||||
tt_crash("this fails for now"); // TODO: Fix
|
||||
tt_assert(wifi);
|
||||
tt_assert(wifi->mutex);
|
||||
tt_check(xSemaphoreTakeRecursive(wifi->mutex, portMAX_DELAY) == pdPASS);
|
||||
}
|
||||
|
||||
static void wifi_unlock(Wifi* wifi) {
|
||||
tt_assert(wifi);
|
||||
tt_assert(wifi->mutex);
|
||||
tt_check(xSemaphoreGiveRecursive(wifi->mutex) == pdPASS);
|
||||
}
|
||||
|
||||
static void wifi_scan_list_alloc(Wifi* wifi) {
|
||||
tt_check(wifi->scan_list == NULL);
|
||||
wifi->scan_list = malloc(sizeof(wifi_ap_record_t) * wifi->scan_list_limit);
|
||||
wifi->scan_list_count = 0;
|
||||
}
|
||||
|
||||
static void wifi_scan_list_alloc_safely(Wifi* wifi) {
|
||||
if (wifi->scan_list == NULL) {
|
||||
wifi_scan_list_alloc(wifi);
|
||||
}
|
||||
}
|
||||
|
||||
static void wifi_scan_list_free(Wifi* wifi) {
|
||||
tt_check(wifi->scan_list != NULL);
|
||||
free(wifi->scan_list);
|
||||
wifi->scan_list = NULL;
|
||||
wifi->scan_list_count = 0;
|
||||
}
|
||||
|
||||
static void wifi_scan_list_free_safely(Wifi* wifi) {
|
||||
if (wifi->scan_list != NULL) {
|
||||
wifi_scan_list_free(wifi);
|
||||
}
|
||||
}
|
||||
|
||||
static void wifi_publish_event_simple(Wifi* wifi, WifiEventType type) {
|
||||
WifiEvent turning_on_event = {.type = type};
|
||||
tt_pubsub_publish(wifi->pubsub, &turning_on_event);
|
||||
}
|
||||
|
||||
static void event_handler(TT_UNUSED void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
TT_LOG_I(TAG, "event_handler: sta start");
|
||||
if (wifi_singleton->radio_state == WIFI_RADIO_CONNECTION_PENDING) {
|
||||
esp_wifi_connect();
|
||||
}
|
||||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
xEventGroupSetBits(wifi_singleton->event_group, WIFI_FAIL_BIT);
|
||||
TT_LOG_I(TAG, "event_handler: disconnected");
|
||||
wifi_singleton->radio_state = WIFI_RADIO_ON;
|
||||
wifi_publish_event_simple(wifi_singleton, WifiEventTypeDisconnected);
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t* event = (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);
|
||||
}
|
||||
}
|
||||
|
||||
static void wifi_enable(Wifi* wifi) {
|
||||
WifiRadioState state = wifi->radio_state;
|
||||
if (
|
||||
state == WIFI_RADIO_ON ||
|
||||
state == WIFI_RADIO_ON_PENDING ||
|
||||
state == WIFI_RADIO_OFF_PENDING
|
||||
) {
|
||||
TT_LOG_W(TAG, "Can't enable from current state");
|
||||
return;
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "Enabling");
|
||||
wifi->radio_state = WIFI_RADIO_ON_PENDING;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeRadioStateOnPending);
|
||||
|
||||
if (wifi->netif != NULL) {
|
||||
esp_netif_destroy(wifi->netif);
|
||||
}
|
||||
wifi->netif = esp_netif_create_default_wifi_sta();
|
||||
|
||||
// Warning: this is the memory-intensive operation
|
||||
// It uses over 117kB of RAM with default settings for S3 on IDF v5.1.2
|
||||
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_err_t init_result = esp_wifi_init(&config);
|
||||
if (init_result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Wifi init failed");
|
||||
if (init_result == ESP_ERR_NO_MEM) {
|
||||
TT_LOG_E(TAG, "Insufficient memory");
|
||||
}
|
||||
wifi->radio_state = WIFI_RADIO_OFF;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
return;
|
||||
}
|
||||
|
||||
esp_wifi_set_storage(WIFI_STORAGE_RAM);
|
||||
|
||||
// TODO: don't crash on check failure
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
||||
WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&wifi->event_handler_any_id
|
||||
));
|
||||
|
||||
// TODO: don't crash on check failure
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
||||
IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&wifi->event_handler_got_ip
|
||||
));
|
||||
|
||||
if (esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Wifi mode setting failed");
|
||||
wifi->radio_state = WIFI_RADIO_OFF;
|
||||
esp_wifi_deinit();
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
return;
|
||||
}
|
||||
|
||||
esp_err_t start_result = esp_wifi_start();
|
||||
if (start_result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Wifi start failed");
|
||||
if (start_result == ESP_ERR_NO_MEM) {
|
||||
TT_LOG_E(TAG, "Insufficient memory");
|
||||
}
|
||||
wifi->radio_state = WIFI_RADIO_OFF;
|
||||
esp_wifi_set_mode(WIFI_MODE_NULL);
|
||||
esp_wifi_deinit();
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
return;
|
||||
}
|
||||
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeRadioStateOn);
|
||||
TT_LOG_I(TAG, "Enabled");
|
||||
}
|
||||
|
||||
static void wifi_disable(Wifi* wifi) {
|
||||
WifiRadioState state = wifi->radio_state;
|
||||
if (
|
||||
state == WIFI_RADIO_OFF ||
|
||||
state == WIFI_RADIO_OFF_PENDING ||
|
||||
state == WIFI_RADIO_ON_PENDING
|
||||
) {
|
||||
TT_LOG_W(TAG, "Can't disable from current state");
|
||||
return;
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "Disabling");
|
||||
wifi->radio_state = WIFI_RADIO_OFF_PENDING;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeRadioStateOffPending);
|
||||
|
||||
// Free up scan list memory
|
||||
wifi_scan_list_free_safely(wifi_singleton);
|
||||
|
||||
if (esp_wifi_stop() != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to stop radio");
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeRadioStateOn);
|
||||
return;
|
||||
}
|
||||
|
||||
if (esp_wifi_set_mode(WIFI_MODE_NULL) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to unset mode");
|
||||
}
|
||||
|
||||
if (esp_event_handler_instance_unregister(
|
||||
WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
wifi->event_handler_any_id
|
||||
) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to unregister id event handler");
|
||||
}
|
||||
|
||||
if (esp_event_handler_instance_unregister(
|
||||
IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
wifi->event_handler_got_ip
|
||||
) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to unregister ip event handler");
|
||||
}
|
||||
|
||||
if (esp_wifi_deinit() != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to deinit");
|
||||
}
|
||||
|
||||
tt_check(wifi->netif != NULL);
|
||||
esp_netif_destroy(wifi->netif);
|
||||
wifi->netif = NULL;
|
||||
|
||||
wifi->radio_state = WIFI_RADIO_OFF;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
TT_LOG_I(TAG, "Disabled");
|
||||
}
|
||||
|
||||
static void wifi_scan_internal(Wifi* wifi) {
|
||||
WifiRadioState state = wifi->radio_state;
|
||||
if (state != WIFI_RADIO_ON && state != WIFI_RADIO_CONNECTION_ACTIVE) {
|
||||
TT_LOG_W(TAG, "Scan unavailable: wifi not enabled");
|
||||
return;
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "Starting scan");
|
||||
wifi->scan_active = true;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeScanStarted);
|
||||
|
||||
// Create scan list if it does not exist
|
||||
wifi_scan_list_alloc_safely(wifi);
|
||||
wifi->scan_list_count = 0;
|
||||
|
||||
esp_wifi_scan_start(NULL, true);
|
||||
uint16_t record_count = wifi->scan_list_limit;
|
||||
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&record_count, wifi->scan_list));
|
||||
uint16_t safe_record_count = TT_MIN(wifi->scan_list_limit, record_count);
|
||||
wifi->scan_list_count = safe_record_count;
|
||||
TT_LOG_I(TAG, "Scanned %u APs. Showing %u:", record_count, safe_record_count);
|
||||
for (uint16_t i = 0; i < safe_record_count; i++) {
|
||||
wifi_ap_record_t* record = &wifi->scan_list[i];
|
||||
TT_LOG_I(TAG, " - SSID %s (RSSI %d, channel %d)", record->ssid, record->rssi, record->primary);
|
||||
}
|
||||
|
||||
esp_wifi_scan_stop();
|
||||
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeScanFinished);
|
||||
wifi->scan_active = false;
|
||||
TT_LOG_I(TAG, "Finished scan");
|
||||
}
|
||||
|
||||
static void wifi_connect_internal(Wifi* wifi, WifiConnectMessage* connect_message) {
|
||||
// TODO: only when connected!
|
||||
wifi_disconnect_internal(wifi);
|
||||
|
||||
wifi->radio_state = WIFI_RADIO_CONNECTION_PENDING;
|
||||
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeConnectionPending);
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
/* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8).
|
||||
* If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
|
||||
* to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
|
||||
* WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
|
||||
*/
|
||||
.threshold.authmode = WIFI_AUTH_WPA2_WPA3_PSK,
|
||||
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
|
||||
.sae_h2e_identifier = {0},
|
||||
},
|
||||
};
|
||||
memcpy(wifi_config.sta.ssid, connect_message->ssid, 32);
|
||||
memcpy(wifi_config.sta.password, connect_message->password, 64);
|
||||
|
||||
wifi->secure_connection = (wifi_config.sta.password[0] != 0x00);
|
||||
|
||||
esp_err_t set_config_result = esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
|
||||
if (set_config_result != ESP_OK) {
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
TT_LOG_E(TAG, "failed to set wifi config (%s)", esp_err_to_name(set_config_result));
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeConnectionFailed);
|
||||
return;
|
||||
}
|
||||
|
||||
esp_err_t wifi_start_result = esp_wifi_start();
|
||||
if (wifi_start_result != ESP_OK) {
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
TT_LOG_E(TAG, "failed to start wifi to begin connecting (%s)", esp_err_to_name(wifi_start_result));
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeConnectionFailed);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 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
|
||||
);
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
wifi->radio_state = WIFI_RADIO_CONNECTION_ACTIVE;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeConnectionSuccess);
|
||||
TT_LOG_I(TAG, "Connected to %s", connect_message->ssid);
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeConnectionFailed);
|
||||
TT_LOG_I(TAG, "Failed to connect to %s", connect_message->ssid);
|
||||
} else {
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeConnectionFailed);
|
||||
TT_LOG_E(TAG, "UNEXPECTED EVENT");
|
||||
}
|
||||
}
|
||||
|
||||
static void wifi_disconnect_internal(Wifi* wifi) {
|
||||
esp_err_t stop_result = esp_wifi_stop();
|
||||
if (stop_result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to disconnect (%s)", esp_err_to_name(stop_result));
|
||||
} else {
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
wifi->secure_connection = false;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeDisconnected);
|
||||
TT_LOG_I(TAG, "Disconnected");
|
||||
}
|
||||
}
|
||||
|
||||
static void wifi_disconnect_internal_but_keep_active(Wifi* wifi) {
|
||||
esp_err_t stop_result = esp_wifi_stop();
|
||||
if (stop_result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to disconnect (%s)", esp_err_to_name(stop_result));
|
||||
return;
|
||||
}
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = {0},
|
||||
.password = {0},
|
||||
.threshold.authmode = WIFI_AUTH_OPEN,
|
||||
.sae_pwe_h2e = WPA3_SAE_PWE_UNSPECIFIED,
|
||||
.sae_h2e_identifier = {0},
|
||||
},
|
||||
};
|
||||
|
||||
esp_err_t set_config_result = esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
|
||||
if (set_config_result != ESP_OK) {
|
||||
// TODO: disable radio, because radio state is in limbo between off and on
|
||||
wifi->radio_state = WIFI_RADIO_OFF;
|
||||
TT_LOG_E(TAG, "failed to set wifi config (%s)", esp_err_to_name(set_config_result));
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
return;
|
||||
}
|
||||
|
||||
esp_err_t wifi_start_result = esp_wifi_start();
|
||||
if (wifi_start_result != ESP_OK) {
|
||||
// TODO: disable radio, because radio state is in limbo between off and on
|
||||
wifi->radio_state = WIFI_RADIO_OFF;
|
||||
TT_LOG_E(TAG, "failed to start wifi to begin connecting (%s)", esp_err_to_name(wifi_start_result));
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
return;
|
||||
}
|
||||
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
wifi_publish_event_simple(wifi, WifiEventTypeDisconnected);
|
||||
TT_LOG_I(TAG, "Disconnected");
|
||||
}
|
||||
|
||||
// ESP wifi APIs need to run from the main task, so we can't just spawn a thread
|
||||
_Noreturn int32_t wifi_main(TT_UNUSED void* parameter) {
|
||||
TT_LOG_I(TAG, "Started main loop");
|
||||
tt_check(wifi_singleton != NULL);
|
||||
Wifi* wifi = wifi_singleton;
|
||||
MessageQueue* queue = wifi->queue;
|
||||
|
||||
WifiMessage message;
|
||||
while (true) {
|
||||
if (tt_message_queue_get(queue, &message, 1000 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
TT_LOG_I(TAG, "Processing message of type %d", message.type);
|
||||
switch (message.type) {
|
||||
case WifiMessageTypeRadioOn:
|
||||
wifi_enable(wifi);
|
||||
break;
|
||||
case WifiMessageTypeRadioOff:
|
||||
wifi_disable(wifi);
|
||||
break;
|
||||
case WifiMessageTypeScan:
|
||||
wifi_scan_internal(wifi);
|
||||
break;
|
||||
case WifiMessageTypeConnect:
|
||||
wifi_connect_internal(wifi, &message.connect_message);
|
||||
break;
|
||||
case WifiMessageTypeDisconnect:
|
||||
wifi_disconnect_internal_but_keep_active(wifi);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void wifi_service_start(TT_UNUSED Service service) {
|
||||
tt_check(wifi_singleton == NULL);
|
||||
wifi_singleton = wifi_alloc();
|
||||
}
|
||||
|
||||
static void wifi_service_stop(TT_UNUSED Service service) {
|
||||
tt_check(wifi_singleton != NULL);
|
||||
|
||||
WifiRadioState state = wifi_singleton->radio_state;
|
||||
if (state != WIFI_RADIO_OFF) {
|
||||
wifi_disable(wifi_singleton);
|
||||
}
|
||||
|
||||
wifi_free(wifi_singleton);
|
||||
wifi_singleton = NULL;
|
||||
|
||||
// wifi_main() cannot be stopped yet as it runs in the main task.
|
||||
// We could theoretically exit it, but then we wouldn't be able to restart the service.
|
||||
tt_crash("not fully implemented");
|
||||
}
|
||||
|
||||
const ServiceManifest wifi_service = {
|
||||
.id = "wifi",
|
||||
.on_start = &wifi_service_start,
|
||||
.on_stop = &wifi_service_stop
|
||||
};
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,157 @@
|
||||
#include "wifi.h"
|
||||
|
||||
#ifndef ESP_TARGET
|
||||
|
||||
#include "assets.h"
|
||||
#include "check.h"
|
||||
#include "log.h"
|
||||
#include "message_queue.h"
|
||||
#include "mutex.h"
|
||||
#include "pubsub.h"
|
||||
#include "service.h"
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
#define TAG "wifi"
|
||||
#define WIFI_SCAN_RECORD_LIMIT 16 // default, can be overridden
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
typedef struct {
|
||||
/** @brief Locking mechanism for modifying the Wifi instance */
|
||||
Mutex* mutex;
|
||||
/** @brief The public event bus */
|
||||
PubSub* pubsub;
|
||||
/** @brief The internal message queue */
|
||||
MessageQueue* queue;
|
||||
bool scan_active;
|
||||
bool secure_connection;
|
||||
WifiRadioState radio_state;
|
||||
} Wifi;
|
||||
|
||||
|
||||
static Wifi* wifi_singleton = NULL;
|
||||
|
||||
// Forward declarations
|
||||
static void wifi_lock(Wifi* wifi);
|
||||
static void wifi_unlock(Wifi* wifi);
|
||||
|
||||
// region Alloc
|
||||
|
||||
static Wifi* wifi_alloc() {
|
||||
Wifi* instance = malloc(sizeof(Wifi));
|
||||
instance->mutex = tt_mutex_alloc(MutexTypeRecursive);
|
||||
instance->pubsub = tt_pubsub_alloc();
|
||||
instance->scan_active = false;
|
||||
instance->radio_state = WIFI_RADIO_OFF;
|
||||
instance->secure_connection = false;
|
||||
return instance;
|
||||
}
|
||||
|
||||
static void wifi_free(Wifi* instance) {
|
||||
tt_mutex_free(instance->mutex);
|
||||
tt_pubsub_free(instance->pubsub);
|
||||
tt_message_queue_free(instance->queue);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
// endregion Alloc
|
||||
|
||||
// region Public functions
|
||||
|
||||
PubSub* wifi_get_pubsub() {
|
||||
tt_assert(wifi_singleton);
|
||||
return wifi_singleton->pubsub;
|
||||
}
|
||||
|
||||
WifiRadioState wifi_get_radio_state() {
|
||||
return wifi_singleton->radio_state;
|
||||
}
|
||||
|
||||
void wifi_scan() {
|
||||
tt_assert(wifi_singleton);
|
||||
wifi_singleton->scan_active = false; // TODO: enable and then later disable automatically
|
||||
}
|
||||
|
||||
bool wifi_is_scanning() {
|
||||
tt_assert(wifi_singleton);
|
||||
return wifi_singleton->scan_active;
|
||||
}
|
||||
|
||||
void wifi_connect(const char* ssid, const char _Nullable password[64]) {
|
||||
tt_assert(wifi_singleton);
|
||||
tt_check(strlen(ssid) <= 32);
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
void wifi_disconnect() {
|
||||
tt_assert(wifi_singleton);
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
void wifi_set_scan_records(uint16_t records) {
|
||||
tt_assert(wifi_singleton);
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
void wifi_get_scan_results(WifiApRecord records[], uint16_t limit, uint16_t* result_count) {
|
||||
tt_check(wifi_singleton);
|
||||
tt_check(result_count);
|
||||
|
||||
// TODO: implement
|
||||
*result_count = 0;
|
||||
}
|
||||
|
||||
void wifi_set_enabled(bool enabled) {
|
||||
tt_assert(wifi_singleton != NULL);
|
||||
if (enabled) {
|
||||
wifi_singleton->radio_state = WIFI_RADIO_CONNECTION_ACTIVE;
|
||||
wifi_singleton->secure_connection = true;
|
||||
} else {
|
||||
wifi_singleton->radio_state = WIFI_RADIO_OFF;
|
||||
}
|
||||
}
|
||||
|
||||
bool wifi_is_connection_secure() {
|
||||
return wifi_singleton->secure_connection;
|
||||
}
|
||||
|
||||
int wifi_get_rssi() {
|
||||
// TODO: implement
|
||||
return -10;
|
||||
}
|
||||
|
||||
// endregion Public functions
|
||||
|
||||
static void wifi_lock(Wifi* wifi) {
|
||||
tt_crash("this fails for now");
|
||||
tt_assert(wifi);
|
||||
tt_assert(wifi->mutex);
|
||||
tt_mutex_acquire(wifi->mutex, 100);
|
||||
}
|
||||
|
||||
static void wifi_unlock(Wifi* wifi) {
|
||||
tt_assert(wifi);
|
||||
tt_assert(wifi->mutex);
|
||||
tt_mutex_release(wifi->mutex);
|
||||
}
|
||||
|
||||
|
||||
static void wifi_service_start(TT_UNUSED Service service) {
|
||||
tt_check(wifi_singleton == NULL);
|
||||
wifi_singleton = wifi_alloc();
|
||||
}
|
||||
|
||||
static void wifi_service_stop(TT_UNUSED Service service) {
|
||||
tt_check(wifi_singleton != NULL);
|
||||
|
||||
wifi_free(wifi_singleton);
|
||||
wifi_singleton = NULL;
|
||||
}
|
||||
|
||||
const ServiceManifest wifi_service = {
|
||||
.id = "wifi",
|
||||
.on_start = &wifi_service_start,
|
||||
.on_stop = &wifi_service_stop
|
||||
};
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "tactility_headless.h"
|
||||
#include "hardware_config.h"
|
||||
#include "hardware_i.h"
|
||||
#include "service_registry.h"
|
||||
|
||||
static const HardwareConfig* hardwareConfig = NULL;
|
||||
|
||||
void tt_tactility_headless_init(const HardwareConfig* config, const ServiceManifest* const services[32]) {
|
||||
tt_service_registry_init();
|
||||
tt_hardware_init(config);
|
||||
hardwareConfig = config;
|
||||
}
|
||||
|
||||
const HardwareConfig* tt_get_hardware_config() {
|
||||
return hardwareConfig;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "hardware_config.h"
|
||||
#include "service_manifest.h"
|
||||
#include "tactility_headless_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void tt_tactility_headless_init(const HardwareConfig* config, const ServiceManifest* const services[32]);
|
||||
|
||||
const HardwareConfig* tt_get_hardware_config();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define TT_CONFIG_SERVICES_LIMIT 32
|
||||
Reference in New Issue
Block a user