committed by
GitHub
parent
6d80144e12
commit
85e26636a3
@@ -0,0 +1,39 @@
|
||||
#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_POWER_SETTINGS TT_ASSET("app_icon_power_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")
|
||||
|
||||
// Power status
|
||||
#define TT_ASSETS_ICON_POWER_020 TT_ASSET("power_020.png")
|
||||
#define TT_ASSETS_ICON_POWER_040 TT_ASSET("power_040.png")
|
||||
#define TT_ASSETS_ICON_POWER_060 TT_ASSET("power_060.png")
|
||||
#define TT_ASSETS_ICON_POWER_080 TT_ASSET("power_080.png")
|
||||
#define TT_ASSETS_ICON_POWER_100 TT_ASSET("power_100.png")
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "TactilityCore.h"
|
||||
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#include "EspPartitions_i.h"
|
||||
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
namespace tt {
|
||||
|
||||
#define TAG "tactility"
|
||||
|
||||
// Initialize NVS
|
||||
static void 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 esp_network_init() {
|
||||
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();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,76 @@
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#include "EspPartitions.h"
|
||||
#include "EspPartitions_i.h"
|
||||
#include "Log.h"
|
||||
|
||||
#include "esp_spiffs.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
namespace tt {
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
namespace tt {
|
||||
|
||||
#define MOUNT_POINT_ASSETS "/assets"
|
||||
#define MOUNT_POINT_CONFIG "/config"
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "TactilityCore.h"
|
||||
#include "Sdcard.h"
|
||||
#include "Power.h"
|
||||
|
||||
namespace tt::hal {
|
||||
|
||||
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::SdCard* _Nullable sdcard;
|
||||
|
||||
/**
|
||||
* An optional power interface for battery or other power delivery.
|
||||
*/
|
||||
const Power* _Nullable power;
|
||||
} Configuration;
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,22 @@
|
||||
#include "Hal/Hal_i.h"
|
||||
|
||||
#define TAG "hardware"
|
||||
|
||||
namespace tt::hal {
|
||||
|
||||
void init(const Configuration* configuration) {
|
||||
if (configuration->bootstrap != nullptr) {
|
||||
TT_LOG_I(TAG, "Bootstrapping");
|
||||
tt_check(configuration->bootstrap(), "bootstrap failed");
|
||||
}
|
||||
|
||||
if (configuration->sdcard != nullptr) {
|
||||
TT_LOG_I(TAG, "Mounting sdcard");
|
||||
sdcard::mount(configuration->sdcard);
|
||||
}
|
||||
|
||||
tt_check(configuration->init_graphics, "Graphics init not set");
|
||||
tt_check(configuration->init_graphics(), "Graphics init failed");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace tt::hal {
|
||||
|
||||
typedef bool (*PowerIsCharging)();
|
||||
typedef bool (*PowerIsChargingEnabled)();
|
||||
typedef void (*PowerSetChargingEnabled)(bool enabled);
|
||||
typedef uint8_t (*PowerGetBatteryCharge)(); // Power value [0, 255] which maps to 0-100% charge
|
||||
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;
|
||||
} Power;
|
||||
|
||||
} // namespace tt
|
||||
@@ -0,0 +1,85 @@
|
||||
#include "Sdcard.h"
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "TactilityCore.h"
|
||||
|
||||
namespace tt::hal::sdcard {
|
||||
|
||||
#define TAG "sdcard"
|
||||
|
||||
static Mutex mutex(MutexTypeRecursive);
|
||||
|
||||
typedef struct {
|
||||
const SdCard* sdcard;
|
||||
void* context;
|
||||
} MountData;
|
||||
|
||||
static MountData data = {
|
||||
.sdcard = nullptr,
|
||||
.context = nullptr
|
||||
};
|
||||
|
||||
static bool lock(uint32_t timeout_ticks) {
|
||||
return mutex.acquire(timeout_ticks) == TtStatusOk;
|
||||
}
|
||||
|
||||
static void unlock() {
|
||||
mutex.release();
|
||||
}
|
||||
|
||||
bool mount(const SdCard* sdcard) {
|
||||
TT_LOG_I(TAG, "Mounting");
|
||||
|
||||
if (data.sdcard != nullptr) {
|
||||
TT_LOG_E(TAG, "Failed to mount: already mounted");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (lock(100)) {
|
||||
void* context = sdcard->mount(TT_SDCARD_MOUNT_POINT);
|
||||
data = (MountData) {
|
||||
.sdcard = sdcard,
|
||||
.context = context
|
||||
};
|
||||
unlock();
|
||||
return (data.context != nullptr);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to lock");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
State get_state() {
|
||||
if (data.context == nullptr) {
|
||||
return StateUnmounted;
|
||||
} else if (data.sdcard->is_mounted(data.context)) {
|
||||
return StateMounted;
|
||||
} else {
|
||||
return StateError;
|
||||
}
|
||||
}
|
||||
|
||||
bool unmount(uint32_t timeout_ticks) {
|
||||
TT_LOG_I(TAG, "Unmounting");
|
||||
bool result = false;
|
||||
|
||||
if (lock(timeout_ticks)) {
|
||||
if (data.sdcard != nullptr) {
|
||||
data.sdcard->unmount(data.context);
|
||||
data = (MountData) {
|
||||
.sdcard = nullptr,
|
||||
.context = nullptr
|
||||
};
|
||||
result = true;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Can't unmount: nothing mounted");
|
||||
}
|
||||
unlock();
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to lock in %lu ticks", timeout_ticks);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "TactilityCore.h"
|
||||
|
||||
namespace tt::hal::sdcard {
|
||||
|
||||
#define TT_SDCARD_MOUNT_POINT "/sdcard"
|
||||
|
||||
typedef void* (*Mount)(const char* mount_path);
|
||||
typedef void (*Unmount)(void* context);
|
||||
typedef bool (*IsMounted)(void* context);
|
||||
|
||||
typedef enum {
|
||||
StateMounted,
|
||||
StateUnmounted,
|
||||
StateError,
|
||||
} State;
|
||||
|
||||
typedef enum {
|
||||
MountBehaviourAtBoot, /** Only mount at boot */
|
||||
MountBehaviourAnytime /** Mount/dismount any time */
|
||||
} MountBehaviour;
|
||||
|
||||
typedef struct {
|
||||
Mount mount;
|
||||
Unmount unmount;
|
||||
IsMounted is_mounted;
|
||||
MountBehaviour mount_behaviour;
|
||||
} SdCard;
|
||||
|
||||
bool mount(const SdCard* sdcard);
|
||||
State get_state();
|
||||
bool unmount(uint32_t timeout_ticks);
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
namespace tt {
|
||||
|
||||
class Preferences {
|
||||
private:
|
||||
const char* namespace_;
|
||||
|
||||
public:
|
||||
explicit Preferences(const char* namespace_) {
|
||||
this->namespace_ = namespace_;
|
||||
}
|
||||
|
||||
bool hasBool(const std::string& key) const;
|
||||
bool hasInt32(const std::string& key) const;
|
||||
bool hasString(const std::string& key) const;
|
||||
|
||||
bool optBool(const std::string& key, bool& out) const;
|
||||
bool optInt32(const std::string& key, int32_t& out) const;
|
||||
bool optString(const std::string& key, std::string& out) const;
|
||||
|
||||
void putBool(const std::string& key, bool value);
|
||||
void putInt32(const std::string& key, int32_t value);
|
||||
void putString(const std::string& key, const std::string& value);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,103 @@
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include "nvs_flash.h"
|
||||
#include "Preferences.h"
|
||||
#include "TactilityCore.h"
|
||||
|
||||
#define TAG "preferences"
|
||||
|
||||
namespace tt {
|
||||
|
||||
bool Preferences::optBool(const std::string& key, bool& out) const {
|
||||
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.c_str(), &out_number) == ESP_OK;
|
||||
nvs_close(handle);
|
||||
if (success) {
|
||||
out = (bool)out_number;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
bool Preferences::optInt32(const std::string& key, int32_t& out) const {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace_, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
return false;
|
||||
} else {
|
||||
bool success = nvs_get_i32(handle, key.c_str(), &out) == ESP_OK;
|
||||
nvs_close(handle);
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
bool Preferences::optString(const std::string& key, std::string& out) const {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace_, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
return false;
|
||||
} else {
|
||||
size_t out_size = 256;
|
||||
char* out_data = static_cast<char*>(malloc(out_size));
|
||||
bool success = nvs_get_str(handle, key.c_str(), out_data, &out_size) == ESP_OK;
|
||||
nvs_close(handle);
|
||||
out = out_data;
|
||||
free(out_data);
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
bool Preferences::hasBool(const std::string& key) const {
|
||||
bool temp;
|
||||
return optBool(key, temp);
|
||||
}
|
||||
|
||||
bool Preferences::hasInt32(const std::string& key) const {
|
||||
int32_t temp;
|
||||
return optInt32(key, temp);
|
||||
}
|
||||
|
||||
bool Preferences::hasString(const std::string& key) const {
|
||||
std::string temp;
|
||||
return optString(key, temp);
|
||||
}
|
||||
|
||||
void Preferences::putBool(const std::string& key, bool value) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace_, NVS_READWRITE, &handle) == ESP_OK) {
|
||||
if (nvs_set_u8(handle, key.c_str(), (uint8_t)value) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to write %s:%s", namespace_, key.c_str());
|
||||
}
|
||||
nvs_close(handle);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "failed to open namespace %s for writing", namespace_);
|
||||
}
|
||||
}
|
||||
|
||||
void Preferences::putInt32(const std::string& key, int32_t value) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace_, NVS_READWRITE, &handle) == ESP_OK) {
|
||||
if (nvs_set_i32(handle, key.c_str(), value) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to write %s:%s", namespace_, key.c_str());
|
||||
}
|
||||
nvs_close(handle);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "failed to open namespace %s for writing", namespace_);
|
||||
}
|
||||
}
|
||||
|
||||
void Preferences::putString(const std::string& key, const std::string& text) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace_, NVS_READWRITE, &handle) == ESP_OK) {
|
||||
nvs_set_str(handle, key.c_str(), text.c_str());
|
||||
nvs_close(handle);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "failed to open namespace %s for writing", namespace_);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,70 @@
|
||||
#ifndef ESP_PLATFOM
|
||||
|
||||
#include "Bundle.h"
|
||||
#include "Preferences.h"
|
||||
#include "TactilityCore.h"
|
||||
|
||||
namespace tt {
|
||||
|
||||
static Bundle preferences;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
std::string get_bundle_key(const std::string& namespace_, const std::string& key) {
|
||||
return namespace_ + ':' + key;
|
||||
}
|
||||
|
||||
bool Preferences::hasBool(const std::string& key) const {
|
||||
std::string bundle_key = get_bundle_key(namespace_, key);
|
||||
return preferences.hasBool(bundle_key);
|
||||
}
|
||||
|
||||
bool Preferences::hasInt32(const std::string& key) const {
|
||||
std::string bundle_key = get_bundle_key(namespace_, key);
|
||||
return preferences.hasInt32(bundle_key);
|
||||
}
|
||||
|
||||
bool Preferences::hasString(const std::string& key) const {
|
||||
std::string bundle_key = get_bundle_key(namespace_, key);
|
||||
return preferences.hasString(bundle_key);
|
||||
}
|
||||
|
||||
bool Preferences::optBool(const std::string& key, bool& out) const {
|
||||
std::string bundle_key = get_bundle_key(namespace_, key);
|
||||
return preferences.optBool(bundle_key, out);
|
||||
}
|
||||
|
||||
bool Preferences::optInt32(const std::string& key, int32_t& out) const {
|
||||
std::string bundle_key = get_bundle_key(namespace_, key);
|
||||
return preferences.optInt32(bundle_key, out);
|
||||
}
|
||||
|
||||
bool Preferences::optString(const std::string& key, std::string& out) const {
|
||||
std::string bundle_key = get_bundle_key(namespace_, key);
|
||||
return preferences.optString(bundle_key, out);
|
||||
}
|
||||
|
||||
void Preferences::putBool(const std::string& key, bool value) {
|
||||
std::string bundle_key = get_bundle_key(namespace_, key);
|
||||
return preferences.putBool(bundle_key, value);
|
||||
}
|
||||
|
||||
void Preferences::putInt32(const std::string& key, int32_t value) {
|
||||
std::string bundle_key = get_bundle_key(namespace_, key);
|
||||
return preferences.putInt32(bundle_key, value);
|
||||
}
|
||||
|
||||
void Preferences::putString(const std::string& key, const std::string& value) {
|
||||
std::string bundle_key = get_bundle_key(namespace_, key);
|
||||
return preferences.putString(bundle_key, value);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "Service.h"
|
||||
#include "ServiceManifest.h"
|
||||
|
||||
namespace tt {
|
||||
|
||||
Service::Service(const ServiceManifest& manifest) : manifest(manifest) {}
|
||||
|
||||
const ServiceManifest& Service::getManifest() const { return manifest; }
|
||||
|
||||
void* Service::getData() const {
|
||||
mutex.acquire(TtWaitForever);
|
||||
void* data_copy = data;
|
||||
mutex.release();
|
||||
return data_copy;
|
||||
}
|
||||
|
||||
void Service::setData(void* newData) {
|
||||
mutex.acquire(TtWaitForever);
|
||||
data = newData;
|
||||
mutex.release();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "ServiceManifest.h"
|
||||
|
||||
namespace tt {
|
||||
|
||||
class Service {
|
||||
private:
|
||||
Mutex mutex = Mutex(MutexTypeNormal);
|
||||
const ServiceManifest& manifest;
|
||||
void* data = nullptr;
|
||||
|
||||
public:
|
||||
Service(const ServiceManifest& manifest);
|
||||
|
||||
[[nodiscard]] const ServiceManifest& getManifest() const;
|
||||
[[nodiscard]] void* getData() const;
|
||||
void setData(void* newData);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "TactilityCore.h"
|
||||
#include <string>
|
||||
|
||||
namespace tt {
|
||||
|
||||
class Service;
|
||||
|
||||
typedef void (*ServiceOnStart)(Service& service);
|
||||
typedef void (*ServiceOnStop)(Service& service);
|
||||
|
||||
typedef struct ServiceManifest {
|
||||
/**
|
||||
* The identifier by which the app is launched by the system and other apps.
|
||||
*/
|
||||
std::string id {};
|
||||
|
||||
/**
|
||||
* Non-blocking method to call when service is started.
|
||||
*/
|
||||
const ServiceOnStart on_start = nullptr;
|
||||
|
||||
/**
|
||||
* Non-blocking method to call when service is stopped.
|
||||
*/
|
||||
const ServiceOnStop on_stop = nullptr;
|
||||
|
||||
} ServiceManifest;
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,99 @@
|
||||
#include "ServiceRegistry.h"
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "Service.h"
|
||||
#include "ServiceManifest.h"
|
||||
#include "TactilityCore.h"
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace tt {
|
||||
|
||||
#define TAG "service_registry"
|
||||
|
||||
typedef std::unordered_map<std::string, const ServiceManifest*> ServiceManifestMap;
|
||||
typedef std::unordered_map<std::string, Service*> ServiceInstanceMap;
|
||||
|
||||
static ServiceManifestMap 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) {
|
||||
TT_LOG_I(TAG, "adding %s", manifest->id.c_str());
|
||||
|
||||
manifest_mutex.acquire(TtWaitForever);
|
||||
service_manifest_map[manifest->id] = manifest;
|
||||
manifest_mutex.release();
|
||||
}
|
||||
|
||||
const ServiceManifest* _Nullable service_registry_find_manifest_by_id(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;
|
||||
manifest_mutex.release();
|
||||
return manifest;
|
||||
}
|
||||
|
||||
static Service* _Nullable service_registry_find_instance_by_id(const std::string& id) {
|
||||
manifest_mutex.acquire(TtWaitForever);
|
||||
auto iterator = service_instance_map.find(id);
|
||||
_Nullable Service* service = iterator != service_instance_map.end() ? iterator->second : nullptr;
|
||||
manifest_mutex.release();
|
||||
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) {
|
||||
TT_LOG_I(TAG, "starting %s", id.c_str());
|
||||
const ServiceManifest* manifest = service_registry_find_manifest_by_id(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);
|
||||
|
||||
instance_mutex.acquire(TtWaitForever);
|
||||
service_instance_map[manifest->id] = service;
|
||||
instance_mutex.release();
|
||||
TT_LOG_I(TAG, "started %s", id.c_str());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
_Nullable Service* service_find(const std::string& service_id) {
|
||||
return (Service*)service_registry_find_instance_by_id(service_id);
|
||||
}
|
||||
|
||||
bool service_registry_stop(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) {
|
||||
TT_LOG_W(TAG, "service not running: %s", id.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
service->getManifest().on_stop(*service);
|
||||
delete service;
|
||||
|
||||
instance_mutex.acquire(TtWaitForever);
|
||||
service_instance_map.erase(id);
|
||||
instance_mutex.release();
|
||||
|
||||
TT_LOG_I(TAG, "stopped %s", id.c_str());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,22 @@
|
||||
#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
|
||||
@@ -0,0 +1,107 @@
|
||||
#include <cstdlib>
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "Service.h"
|
||||
#include "TactilityCore.h"
|
||||
#include "TactilityHeadless.h"
|
||||
|
||||
#define TAG "sdcard_service"
|
||||
|
||||
namespace tt::service::sdcard {
|
||||
|
||||
static int32_t sdcard_task(void* context);
|
||||
|
||||
typedef struct {
|
||||
Mutex* mutex;
|
||||
Thread* thread;
|
||||
hal::sdcard::State last_state;
|
||||
bool interrupted;
|
||||
} ServiceData;
|
||||
|
||||
static ServiceData* service_data_alloc() {
|
||||
auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData)));
|
||||
*data = (ServiceData) {
|
||||
.mutex = tt_mutex_alloc(MutexTypeNormal),
|
||||
.thread = thread_alloc_ex(
|
||||
"sdcard",
|
||||
3000, // Minimum is ~2800 @ ESP-IDF 5.1.2 when ejecting sdcard
|
||||
&sdcard_task,
|
||||
data
|
||||
),
|
||||
.last_state = hal::sdcard::StateUnmounted,
|
||||
.interrupted = false
|
||||
};
|
||||
thread_set_priority(data->thread, ThreadPriorityLow);
|
||||
return data;
|
||||
}
|
||||
|
||||
static void service_data_free(ServiceData* data) {
|
||||
tt_mutex_free(data->mutex);
|
||||
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) {
|
||||
auto* data = (ServiceData*)context;
|
||||
bool interrupted = false;
|
||||
|
||||
do {
|
||||
service_data_lock(data);
|
||||
|
||||
interrupted = data->interrupted;
|
||||
|
||||
hal::sdcard::State new_state = hal::sdcard::get_state();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
service_data_unlock(data);
|
||||
delay_ms(2000);
|
||||
} while (!interrupted);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void on_start(Service& service) {
|
||||
if (get_hardware_config()->sdcard != nullptr) {
|
||||
ServiceData* data = service_data_alloc();
|
||||
service.setData(data);
|
||||
thread_start(data->thread);
|
||||
} else {
|
||||
TT_LOG_I(TAG, "task not started due to config");
|
||||
}
|
||||
}
|
||||
|
||||
static void on_stop(Service& service) {
|
||||
auto* data = static_cast<ServiceData*>(service.getData());
|
||||
if (data != nullptr) {
|
||||
service_data_lock(data);
|
||||
data->interrupted = true;
|
||||
service_data_unlock(data);
|
||||
|
||||
thread_join(data->thread);
|
||||
|
||||
service_data_free(data);
|
||||
}
|
||||
}
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "sdcard",
|
||||
.on_start = &on_start,
|
||||
.on_stop = &on_stop
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,131 @@
|
||||
#pragma once
|
||||
|
||||
#include "Pubsub.h"
|
||||
#include "WifiGlobals.h"
|
||||
#include "WifiSettings.h"
|
||||
#include <cstdio>
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "esp_wifi.h"
|
||||
#include "WifiSettings.h"
|
||||
#else
|
||||
#include <cstdint>
|
||||
// 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
|
||||
|
||||
namespace tt::service::wifi {
|
||||
|
||||
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[TT_WIFI_SSID_LIMIT + 1];
|
||||
int8_t rssi;
|
||||
wifi_auth_mode_t auth_mode;
|
||||
} WifiApRecord;
|
||||
|
||||
/**
|
||||
* @brief Get wifi pubsub
|
||||
* @return PubSub*
|
||||
*/
|
||||
PubSub* get_pubsub();
|
||||
|
||||
WifiRadioState get_radio_state();
|
||||
/**
|
||||
* @brief Request scanning update. Returns immediately. Results are through pubsub.
|
||||
*/
|
||||
void scan();
|
||||
|
||||
/**
|
||||
* @return true if wifi is actively scanning
|
||||
*/
|
||||
bool 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 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 set_scan_records(uint16_t records);
|
||||
|
||||
/**
|
||||
* @brief Enable/disable the radio. Ignores input if desired state matches current state.
|
||||
* @param enabled
|
||||
*/
|
||||
void 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 ap
|
||||
*/
|
||||
void connect(const settings::WifiApSettings* ap, bool remember);
|
||||
|
||||
/**
|
||||
* @brief Disconnect from the access point. Doesn't have any effect when not connected.
|
||||
*/
|
||||
void disconnect();
|
||||
|
||||
/**
|
||||
* Return true if the connection isn't unencrypted.
|
||||
*/
|
||||
bool is_connection_secure();
|
||||
|
||||
/**
|
||||
* Returns the RSSI value (negative number) or return 1 when not connected
|
||||
*/
|
||||
int get_rssi();
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,755 @@
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#include "Wifi.h"
|
||||
|
||||
#include "MessageQueue.h"
|
||||
#include "Mutex.h"
|
||||
#include "Check.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "Log.h"
|
||||
#include "Pubsub.h"
|
||||
#include "Service.h"
|
||||
#include "WifiSettings.h"
|
||||
#include <atomic>
|
||||
#include <cstring>
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
namespace tt::service::wifi {
|
||||
|
||||
#define TAG "wifi_service"
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
typedef enum {
|
||||
WifiMessageTypeRadioOn,
|
||||
WifiMessageTypeRadioOff,
|
||||
WifiMessageTypeScan,
|
||||
WifiMessageTypeConnect,
|
||||
WifiMessageTypeDisconnect
|
||||
} WifiMessageType;
|
||||
|
||||
typedef struct {
|
||||
} WifiConnectMessage;
|
||||
|
||||
typedef struct {
|
||||
WifiMessageType type;
|
||||
union {
|
||||
WifiConnectMessage connect_message;
|
||||
};
|
||||
} WifiMessage;
|
||||
|
||||
class Wifi {
|
||||
public:
|
||||
Wifi();
|
||||
~Wifi();
|
||||
|
||||
std::atomic<WifiRadioState> radio_state;
|
||||
/** @brief Locking mechanism for modifying the Wifi instance */
|
||||
Mutex mutex = Mutex(MutexTypeRecursive);
|
||||
/** @brief The public event bus */
|
||||
PubSub* pubsub = nullptr;
|
||||
/** @brief The internal message queue */
|
||||
MessageQueue queue = MessageQueue(1, sizeof(WifiMessage));
|
||||
// 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.
|
||||
/** @brief The network interface when wifi is started */
|
||||
esp_netif_t* _Nullable netif = nullptr;
|
||||
/** @brief Scanning results */
|
||||
wifi_ap_record_t* _Nullable scan_list = nullptr;
|
||||
/** @brief The current item count in scan_list (-1 when scan_list is NULL) */
|
||||
uint16_t scan_list_count = 0;
|
||||
/** @brief Maximum amount of records to scan (value > 0) */
|
||||
uint16_t scan_list_limit = TT_WIFI_SCAN_RECORD_LIMIT;
|
||||
bool scan_active = false;
|
||||
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;
|
||||
settings::WifiApSettings connection_target = {
|
||||
.ssid = { 0 },
|
||||
.password = { 0 },
|
||||
.auto_connect = false
|
||||
};
|
||||
bool connection_target_remember = false; // Whether to store the connection_target on successful connection or not
|
||||
};
|
||||
|
||||
static Wifi* wifi_singleton = nullptr;
|
||||
|
||||
// Forward declarations
|
||||
static void scan_list_free_safely(Wifi* wifi);
|
||||
static void disconnect_internal_but_keep_active(Wifi* wifi);
|
||||
static void lock(Wifi* wifi);
|
||||
static void unlock(Wifi* wifi);
|
||||
|
||||
// region Alloc
|
||||
|
||||
Wifi::Wifi() : radio_state(WIFI_RADIO_OFF) {
|
||||
pubsub = tt_pubsub_alloc();
|
||||
event_group = xEventGroupCreate();
|
||||
}
|
||||
|
||||
Wifi::~Wifi() {
|
||||
tt_pubsub_free(pubsub);
|
||||
}
|
||||
|
||||
// endregion Alloc
|
||||
|
||||
// region Public functions
|
||||
|
||||
PubSub* get_pubsub() {
|
||||
tt_assert(wifi_singleton);
|
||||
return wifi_singleton->pubsub;
|
||||
}
|
||||
|
||||
WifiRadioState get_radio_state() {
|
||||
tt_assert(wifi_singleton);
|
||||
lock(wifi_singleton);
|
||||
WifiRadioState state = wifi_singleton->radio_state;
|
||||
unlock(wifi_singleton);
|
||||
return state;
|
||||
}
|
||||
|
||||
void scan() {
|
||||
tt_assert(wifi_singleton);
|
||||
lock(wifi_singleton);
|
||||
WifiMessage message = {.type = WifiMessageTypeScan};
|
||||
// No need to lock for queue
|
||||
wifi_singleton->queue.put(&message, 100 / portTICK_PERIOD_MS);
|
||||
unlock(wifi_singleton);
|
||||
}
|
||||
|
||||
bool is_scanning() {
|
||||
tt_assert(wifi_singleton);
|
||||
lock(wifi_singleton);
|
||||
bool is_scanning = wifi_singleton->scan_active;
|
||||
unlock(wifi_singleton);
|
||||
return is_scanning;
|
||||
}
|
||||
|
||||
void connect(const settings::WifiApSettings* ap, bool remember) {
|
||||
tt_assert(wifi_singleton);
|
||||
lock(wifi_singleton);
|
||||
memcpy(&wifi_singleton->connection_target, ap, sizeof(settings::WifiApSettings));
|
||||
wifi_singleton->connection_target_remember = remember;
|
||||
WifiMessage message = {.type = WifiMessageTypeConnect};
|
||||
wifi_singleton->queue.put(&message, 100 / portTICK_PERIOD_MS);
|
||||
unlock(wifi_singleton);
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
tt_assert(wifi_singleton);
|
||||
lock(wifi_singleton);
|
||||
wifi_singleton->connection_target = (settings::WifiApSettings) {
|
||||
.ssid = { 0 },
|
||||
.password = { 0 },
|
||||
.auto_connect = false
|
||||
};
|
||||
WifiMessage message = {.type = WifiMessageTypeDisconnect};
|
||||
wifi_singleton->queue.put(&message, 100 / portTICK_PERIOD_MS);
|
||||
unlock(wifi_singleton);
|
||||
}
|
||||
|
||||
void set_scan_records(uint16_t records) {
|
||||
tt_assert(wifi_singleton);
|
||||
lock(wifi_singleton);
|
||||
if (records != wifi_singleton->scan_list_limit) {
|
||||
scan_list_free_safely(wifi_singleton);
|
||||
wifi_singleton->scan_list_limit = records;
|
||||
}
|
||||
unlock(wifi_singleton);
|
||||
}
|
||||
|
||||
void get_scan_results(WifiApRecord records[], uint16_t limit, uint16_t* result_count) {
|
||||
tt_assert(wifi_singleton);
|
||||
tt_assert(result_count);
|
||||
|
||||
lock(wifi_singleton);
|
||||
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;
|
||||
}
|
||||
unlock(wifi_singleton);
|
||||
}
|
||||
|
||||
void set_enabled(bool enabled) {
|
||||
tt_assert(wifi_singleton);
|
||||
lock(wifi_singleton);
|
||||
if (enabled) {
|
||||
WifiMessage message = {.type = WifiMessageTypeRadioOn};
|
||||
// No need to lock for queue
|
||||
wifi_singleton->queue.put(&message, 100 / portTICK_PERIOD_MS);
|
||||
} else {
|
||||
WifiMessage message = {.type = WifiMessageTypeRadioOff};
|
||||
// No need to lock for queue
|
||||
wifi_singleton->queue.put(&message, 100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
unlock(wifi_singleton);
|
||||
}
|
||||
|
||||
bool is_connection_secure() {
|
||||
tt_assert(wifi_singleton);
|
||||
lock(wifi_singleton);
|
||||
bool is_secure = wifi_singleton->secure_connection;
|
||||
unlock(wifi_singleton);
|
||||
return is_secure;
|
||||
}
|
||||
|
||||
int get_rssi() {
|
||||
tt_assert(wifi_singleton);
|
||||
static int rssi = 0;
|
||||
if (esp_wifi_sta_get_rssi(&rssi) == ESP_OK) {
|
||||
return rssi;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// endregion Public functions
|
||||
|
||||
static void lock(Wifi* wifi) {
|
||||
tt_assert(wifi);
|
||||
wifi->mutex.acquire(ms_to_ticks(100));
|
||||
}
|
||||
|
||||
static void unlock(Wifi* wifi) {
|
||||
tt_assert(wifi);
|
||||
wifi->mutex.release();
|
||||
}
|
||||
|
||||
static void scan_list_alloc(Wifi* wifi) {
|
||||
tt_assert(wifi->scan_list == nullptr);
|
||||
wifi->scan_list = static_cast<wifi_ap_record_t*>(malloc(sizeof(wifi_ap_record_t) * wifi->scan_list_limit));
|
||||
wifi->scan_list_count = 0;
|
||||
}
|
||||
|
||||
static void scan_list_alloc_safely(Wifi* wifi) {
|
||||
if (wifi->scan_list == nullptr) {
|
||||
scan_list_alloc(wifi);
|
||||
}
|
||||
}
|
||||
|
||||
static void scan_list_free(Wifi* wifi) {
|
||||
tt_assert(wifi->scan_list != nullptr);
|
||||
free(wifi->scan_list);
|
||||
wifi->scan_list = nullptr;
|
||||
wifi->scan_list_count = 0;
|
||||
}
|
||||
|
||||
static void scan_list_free_safely(Wifi* wifi) {
|
||||
if (wifi->scan_list != nullptr) {
|
||||
scan_list_free(wifi);
|
||||
}
|
||||
}
|
||||
|
||||
static void publish_event_simple(Wifi* wifi, WifiEventType type) {
|
||||
WifiEvent turning_on_event = {.type = type};
|
||||
tt_pubsub_publish(wifi->pubsub, &turning_on_event);
|
||||
}
|
||||
|
||||
static bool copy_scan_list(Wifi* wifi) {
|
||||
if ((wifi->radio_state == WIFI_RADIO_ON || wifi->radio_state == WIFI_RADIO_CONNECTION_ACTIVE) && wifi->scan_active) {
|
||||
// Create scan list if it does not exist
|
||||
scan_list_alloc_safely(wifi);
|
||||
wifi->scan_list_count = 0;
|
||||
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);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void auto_connect(Wifi* wifi) {
|
||||
for (int i = 0; i < wifi->scan_list_count; ++i) {
|
||||
const char* ssid = (const char*)wifi->scan_list[i].ssid;
|
||||
if (settings::contains(ssid)) {
|
||||
static_assert(sizeof(wifi->scan_list[i].ssid) == (TT_WIFI_SSID_LIMIT + 1), "SSID size mismatch");
|
||||
settings::WifiApSettings ap_settings;
|
||||
if (settings::load(ssid, &ap_settings)) {
|
||||
if (ap_settings.auto_connect) {
|
||||
TT_LOG_I(TAG, "Auto-connecting to %s", ap_settings.ssid);
|
||||
connect(&ap_settings, false);
|
||||
}
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to load credentials for ssid %s", ssid);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void event_handler(TT_UNUSED void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
|
||||
lock(wifi_singleton);
|
||||
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) {
|
||||
if (wifi_singleton->radio_state != WIFI_RADIO_OFF_PENDING) {
|
||||
xEventGroupSetBits(wifi_singleton->event_group, WIFI_FAIL_BIT);
|
||||
TT_LOG_I(TAG, "event_handler: disconnected");
|
||||
wifi_singleton->radio_state = WIFI_RADIO_ON;
|
||||
publish_event_simple(wifi_singleton, WifiEventTypeDisconnected);
|
||||
}
|
||||
} 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);
|
||||
} 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);
|
||||
bool copied_list = copy_scan_list(wifi_singleton);
|
||||
|
||||
if (
|
||||
wifi_singleton->radio_state != WIFI_RADIO_OFF &&
|
||||
wifi_singleton->radio_state != WIFI_RADIO_OFF_PENDING
|
||||
) {
|
||||
wifi_singleton->scan_active = false;
|
||||
esp_wifi_scan_stop();
|
||||
}
|
||||
|
||||
publish_event_simple(wifi_singleton, WifiEventTypeScanFinished);
|
||||
TT_LOG_I(TAG, "Finished scan");
|
||||
|
||||
if (copied_list && wifi_singleton->radio_state == WIFI_RADIO_ON) {
|
||||
auto_connect(wifi_singleton);
|
||||
}
|
||||
}
|
||||
unlock(wifi_singleton);
|
||||
}
|
||||
|
||||
static void 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;
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOnPending);
|
||||
|
||||
if (wifi->netif != nullptr) {
|
||||
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;
|
||||
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,
|
||||
nullptr,
|
||||
&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,
|
||||
nullptr,
|
||||
&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();
|
||||
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();
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
return;
|
||||
}
|
||||
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOn);
|
||||
TT_LOG_I(TAG, "Enabled");
|
||||
}
|
||||
|
||||
static void 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;
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOffPending);
|
||||
|
||||
// Free up scan list memory
|
||||
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;
|
||||
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_assert(wifi->netif != nullptr);
|
||||
esp_netif_destroy(wifi->netif);
|
||||
wifi->netif = nullptr;
|
||||
wifi->scan_active = false;
|
||||
wifi->radio_state = WIFI_RADIO_OFF;
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
TT_LOG_I(TAG, "Disabled");
|
||||
}
|
||||
|
||||
static void scan_internal(Wifi* wifi) {
|
||||
WifiRadioState state = wifi->radio_state;
|
||||
if (state != WIFI_RADIO_ON && state != WIFI_RADIO_CONNECTION_ACTIVE && state != WIFI_RADIO_CONNECTION_PENDING) {
|
||||
TT_LOG_W(TAG, "Scan unavailable: wifi not enabled");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!wifi->scan_active) {
|
||||
if (esp_wifi_scan_start(nullptr, false) == ESP_OK) {
|
||||
TT_LOG_I(TAG, "Starting scan");
|
||||
wifi->scan_active = true;
|
||||
publish_event_simple(wifi, WifiEventTypeScanStarted);
|
||||
} else {
|
||||
TT_LOG_I(TAG, "Can't start scan");
|
||||
}
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Scan already pending");
|
||||
}
|
||||
}
|
||||
|
||||
static void connect_internal(Wifi* wifi) {
|
||||
TT_LOG_I(TAG, "Connecting to %s", wifi->connection_target.ssid);
|
||||
|
||||
// Stop radio first, if needed
|
||||
WifiRadioState radio_state = wifi->radio_state;
|
||||
if (
|
||||
radio_state == WIFI_RADIO_ON ||
|
||||
radio_state == WIFI_RADIO_CONNECTION_ACTIVE ||
|
||||
radio_state == WIFI_RADIO_CONNECTION_PENDING
|
||||
) {
|
||||
TT_LOG_I(TAG, "Connecting: Stopping radio first");
|
||||
esp_err_t stop_result = esp_wifi_stop();
|
||||
wifi->scan_active = false;
|
||||
if (stop_result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Connecting: Failed to disconnect (%s)", esp_err_to_name(stop_result));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
wifi->radio_state = WIFI_RADIO_CONNECTION_PENDING;
|
||||
|
||||
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.
|
||||
*/
|
||||
.ssid = {0},
|
||||
.password = {0},
|
||||
.scan_method = WIFI_ALL_CHANNEL_SCAN,
|
||||
.bssid_set = false,
|
||||
.bssid = { 0 },
|
||||
.channel = 0,
|
||||
.listen_interval = 0,
|
||||
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
|
||||
.threshold = {
|
||||
.rssi = 0,
|
||||
.authmode = WIFI_AUTH_WPA2_WPA3_PSK,
|
||||
},
|
||||
.pmf_cfg = {
|
||||
.capable = false,
|
||||
.required = false
|
||||
},
|
||||
.rm_enabled = 0,
|
||||
.btm_enabled = 0,
|
||||
.mbo_enabled = 0,
|
||||
.ft_enabled = 0,
|
||||
.owe_enabled = 0,
|
||||
.transition_disable = 0,
|
||||
.reserved = 0,
|
||||
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
|
||||
.sae_pk_mode = WPA3_SAE_PK_MODE_AUTOMATIC,
|
||||
.failure_retry_cnt = 1,
|
||||
.he_dcm_set = 0,
|
||||
.he_dcm_max_constellation_tx = 0,
|
||||
.he_dcm_max_constellation_rx = 0,
|
||||
.he_mcs9_enabled = 0,
|
||||
.he_su_beamformee_disabled = 0,
|
||||
.he_trig_su_bmforming_feedback_disabled = 0,
|
||||
.he_trig_mu_bmforming_partial_feedback_disabled = 0,
|
||||
.he_trig_cqi_feedback_disabled = 0,
|
||||
.he_reserved = 0,
|
||||
.sae_h2e_identifier = {0},
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(sizeof(wifi_config.sta.ssid) == (sizeof(wifi_singleton->connection_target.ssid)-1), "SSID size mismatch");
|
||||
memcpy(wifi_config.sta.ssid, wifi_singleton->connection_target.ssid, sizeof(wifi_config.sta.ssid));
|
||||
memcpy(wifi_config.sta.password, wifi_singleton->connection_target.password, sizeof(wifi_config.sta.password));
|
||||
|
||||
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));
|
||||
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));
|
||||
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;
|
||||
publish_event_simple(wifi, WifiEventTypeConnectionSuccess);
|
||||
TT_LOG_I(TAG, "Connected to %s", wifi->connection_target.ssid);
|
||||
if (wifi->connection_target_remember) {
|
||||
if (!settings::save(&wifi->connection_target)) {
|
||||
TT_LOG_E(TAG, "Failed to store credentials");
|
||||
} else {
|
||||
TT_LOG_I(TAG, "Stored credentials");
|
||||
}
|
||||
}
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
publish_event_simple(wifi, WifiEventTypeConnectionFailed);
|
||||
TT_LOG_I(TAG, "Failed to connect to %s", wifi->connection_target.ssid);
|
||||
} else {
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
publish_event_simple(wifi, WifiEventTypeConnectionFailed);
|
||||
TT_LOG_E(TAG, "UNEXPECTED EVENT");
|
||||
}
|
||||
|
||||
xEventGroupClearBits(wifi_singleton->event_group, WIFI_FAIL_BIT | WIFI_CONNECTED_BIT);
|
||||
}
|
||||
|
||||
static void 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 = {
|
||||
.rssi = 0,
|
||||
.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));
|
||||
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));
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
return;
|
||||
}
|
||||
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
publish_event_simple(wifi, WifiEventTypeDisconnected);
|
||||
TT_LOG_I(TAG, "Disconnected");
|
||||
}
|
||||
|
||||
// ESP Wi-Fi 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_assert(wifi_singleton != nullptr);
|
||||
Wifi* wifi = wifi_singleton;
|
||||
MessageQueue& queue = wifi->queue;
|
||||
|
||||
if (TT_WIFI_AUTO_ENABLE) {
|
||||
enable(wifi);
|
||||
scan_internal(wifi);
|
||||
}
|
||||
|
||||
WifiMessage message;
|
||||
while (true) {
|
||||
if (queue.get(&message, 10000 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
TT_LOG_I(TAG, "Processing message of type %d", message.type);
|
||||
switch (message.type) {
|
||||
case WifiMessageTypeRadioOn:
|
||||
lock(wifi);
|
||||
enable(wifi);
|
||||
unlock(wifi);
|
||||
break;
|
||||
case WifiMessageTypeRadioOff:
|
||||
lock(wifi);
|
||||
disable(wifi);
|
||||
unlock(wifi);
|
||||
break;
|
||||
case WifiMessageTypeScan:
|
||||
lock(wifi);
|
||||
scan_internal(wifi);
|
||||
unlock(wifi);
|
||||
break;
|
||||
case WifiMessageTypeConnect:
|
||||
lock(wifi);
|
||||
connect_internal(wifi);
|
||||
unlock(wifi);
|
||||
break;
|
||||
case WifiMessageTypeDisconnect:
|
||||
lock(wifi);
|
||||
disconnect_internal_but_keep_active(wifi);
|
||||
unlock(wifi);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Automatic scanning is done so we can automatically connect to access points
|
||||
lock(wifi);
|
||||
bool should_start_scan = wifi->radio_state == WIFI_RADIO_ON && !wifi->scan_active;
|
||||
unlock(wifi);
|
||||
if (should_start_scan) {
|
||||
scan_internal(wifi);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void service_start(Service& service) {
|
||||
tt_assert(wifi_singleton == nullptr);
|
||||
wifi_singleton = new Wifi();
|
||||
service.setData(wifi_singleton);
|
||||
}
|
||||
|
||||
static void service_stop(Service& service) {
|
||||
tt_assert(wifi_singleton != nullptr);
|
||||
|
||||
WifiRadioState state = wifi_singleton->radio_state;
|
||||
if (state != WIFI_RADIO_OFF) {
|
||||
disable(wifi_singleton);
|
||||
}
|
||||
|
||||
delete wifi_singleton;
|
||||
wifi_singleton = nullptr;
|
||||
|
||||
// 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");
|
||||
}
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "wifi",
|
||||
.on_start = &service_start,
|
||||
.on_stop = &service_stop
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#define TT_WIFI_AUTO_CONNECT true // Default setting for new Wi-Fi entries
|
||||
#define TT_WIFI_AUTO_ENABLE false
|
||||
|
||||
#define TT_WIFI_SCAN_RECORD_LIMIT 16 // default, can be overridden
|
||||
|
||||
#define TT_WIFI_SSID_LIMIT 32 // 32 characters/octets, according to IEEE 802.11-2020 spec
|
||||
#define TT_WIFI_CREDENTIALS_PASSWORD_LIMIT 64 // 64 characters/octets, according to IEEE 802.11-2020 spec
|
||||
@@ -0,0 +1,187 @@
|
||||
#include "Wifi.h"
|
||||
|
||||
#ifndef ESP_TARGET
|
||||
|
||||
#include "Check.h"
|
||||
#include "Log.h"
|
||||
#include "MessageQueue.h"
|
||||
#include "Mutex.h"
|
||||
#include "Pubsub.h"
|
||||
#include "Service.h"
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
namespace tt::service::wifi {
|
||||
|
||||
#define TAG "wifi"
|
||||
#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 = NULL;
|
||||
|
||||
// Forward declarations
|
||||
static void wifi_lock(Wifi* wifi);
|
||||
static void wifi_unlock(Wifi* wifi);
|
||||
|
||||
// region Static
|
||||
|
||||
static void publish_event_simple(Wifi* wifi, WifiEventType type) {
|
||||
WifiEvent turning_on_event = {.type = type};
|
||||
tt_pubsub_publish(wifi->pubsub, &turning_on_event);
|
||||
}
|
||||
|
||||
// endregion Static
|
||||
|
||||
// region Alloc
|
||||
|
||||
static Wifi* wifi_alloc() {
|
||||
auto* instance = static_cast<Wifi*>(malloc(sizeof(Wifi)));
|
||||
instance->mutex = tt_mutex_alloc(MutexTypeRecursive);
|
||||
instance->pubsub = tt_pubsub_alloc();
|
||||
instance->scan_active = false;
|
||||
instance->radio_state = WIFI_RADIO_CONNECTION_ACTIVE;
|
||||
instance->secure_connection = false;
|
||||
return instance;
|
||||
}
|
||||
|
||||
static void wifi_free(Wifi* instance) {
|
||||
tt_mutex_free(instance->mutex);
|
||||
tt_pubsub_free(instance->pubsub);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
// endregion Alloc
|
||||
|
||||
// region Public functions
|
||||
|
||||
PubSub* get_pubsub() {
|
||||
tt_assert(wifi);
|
||||
return wifi->pubsub;
|
||||
}
|
||||
|
||||
WifiRadioState get_radio_state() {
|
||||
return wifi->radio_state;
|
||||
}
|
||||
|
||||
void scan() {
|
||||
tt_assert(wifi);
|
||||
wifi->scan_active = false; // TODO: enable and then later disable automatically
|
||||
}
|
||||
|
||||
bool is_scanning() {
|
||||
tt_assert(wifi);
|
||||
return wifi->scan_active;
|
||||
}
|
||||
|
||||
void connect(const settings::WifiApSettings* ap, bool remember) {
|
||||
tt_assert(wifi);
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
tt_assert(wifi);
|
||||
}
|
||||
|
||||
void set_scan_records(uint16_t records) {
|
||||
tt_assert(wifi);
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
void get_scan_results(WifiApRecord records[], uint16_t limit, uint16_t* result_count) {
|
||||
tt_check(wifi);
|
||||
tt_check(result_count);
|
||||
|
||||
if (limit >= 5) {
|
||||
strcpy((char*)records[0].ssid, "Home WiFi");
|
||||
records[0].auth_mode = WIFI_AUTH_WPA2_PSK;
|
||||
records[0].rssi = -30;
|
||||
strcpy((char*)records[1].ssid, "Living Room");
|
||||
records[1].auth_mode = WIFI_AUTH_WPA2_PSK;
|
||||
records[1].rssi = -67;
|
||||
strcpy((char*)records[2].ssid, "No place like 127.0.0.1");
|
||||
records[2].auth_mode = WIFI_AUTH_WPA2_PSK;
|
||||
records[2].rssi = -70;
|
||||
strcpy((char*)records[3].ssid, "Public Wi-Fi");
|
||||
records[3].auth_mode = WIFI_AUTH_WPA2_PSK;
|
||||
records[3].rssi = -80;
|
||||
strcpy((char*)records[4].ssid, "Bad Reception");
|
||||
records[4].auth_mode = WIFI_AUTH_WPA2_PSK;
|
||||
records[4].rssi = -90;
|
||||
*result_count = 5;
|
||||
} else {
|
||||
*result_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void set_enabled(bool enabled) {
|
||||
tt_assert(wifi != NULL);
|
||||
if (enabled) {
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
wifi->secure_connection = true;
|
||||
} else {
|
||||
wifi->radio_state = WIFI_RADIO_OFF;
|
||||
}
|
||||
}
|
||||
|
||||
bool is_connection_secure() {
|
||||
return wifi->secure_connection;
|
||||
}
|
||||
|
||||
int get_rssi() {
|
||||
if (wifi->radio_state == WIFI_RADIO_CONNECTION_ACTIVE) {
|
||||
return -30;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// endregion Public functions
|
||||
|
||||
static void lock(Wifi* wifi) {
|
||||
tt_crash("this fails for now");
|
||||
tt_assert(wifi);
|
||||
tt_assert(wifi->mutex);
|
||||
tt_mutex_acquire(wifi->mutex, 100);
|
||||
}
|
||||
|
||||
static void unlock(Wifi* wifi) {
|
||||
tt_assert(wifi);
|
||||
tt_assert(wifi->mutex);
|
||||
tt_mutex_release(wifi->mutex);
|
||||
}
|
||||
|
||||
|
||||
static void service_start(TT_UNUSED Service& service) {
|
||||
tt_check(wifi == nullptr);
|
||||
wifi = wifi_alloc();
|
||||
}
|
||||
|
||||
static void service_stop(TT_UNUSED Service& service) {
|
||||
tt_check(wifi != nullptr);
|
||||
|
||||
wifi_free(wifi);
|
||||
wifi = nullptr;
|
||||
}
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "wifi",
|
||||
.on_start = &service_start,
|
||||
.on_stop = &service_stop
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "WifiGlobals.h"
|
||||
|
||||
namespace tt::service::wifi::settings {
|
||||
|
||||
/**
|
||||
* This struct is stored as-is into NVS flash.
|
||||
*
|
||||
* The SSID and secret are increased by 1 byte to facilitate string null termination.
|
||||
* This makes it easier to use the char array as a string in various places.
|
||||
*/
|
||||
typedef struct {
|
||||
char ssid[TT_WIFI_SSID_LIMIT + 1];
|
||||
char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT + 1];
|
||||
bool auto_connect;
|
||||
} WifiApSettings;
|
||||
|
||||
bool contains(const char* ssid);
|
||||
|
||||
bool load(const char* ssid, WifiApSettings* settings);
|
||||
|
||||
bool save(const WifiApSettings* settings);
|
||||
|
||||
bool remove(const char* ssid);
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,146 @@
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#include "WifiGlobals.h"
|
||||
#include "WifiSettings.h"
|
||||
#include <cstring>
|
||||
|
||||
#include "nvs_flash.h"
|
||||
#include "Log.h"
|
||||
#include "Hash.h"
|
||||
#include "Check.h"
|
||||
#include "Crypt.h"
|
||||
|
||||
#define TAG "wifi_settings"
|
||||
#define TT_NVS_NAMESPACE "wifi_settings" // limited by NVS_KEY_NAME_MAX_SIZE
|
||||
|
||||
// region Wi-Fi Credentials - static
|
||||
|
||||
static esp_err_t credentials_nvs_open(nvs_handle_t* handle, nvs_open_mode_t mode) {
|
||||
return nvs_open(TT_NVS_NAMESPACE, NVS_READWRITE, handle);
|
||||
}
|
||||
|
||||
static void credentials_nvs_close(nvs_handle_t handle) {
|
||||
nvs_close(handle);
|
||||
}
|
||||
|
||||
// endregion Wi-Fi Credentials - static
|
||||
|
||||
// region Wi-Fi Credentials - public
|
||||
namespace tt::service::wifi::settings {
|
||||
|
||||
bool contains(const char* ssid) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = 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;
|
||||
}
|
||||
|
||||
bool key_exists = nvs_find_key(handle, ssid, NULL) == ESP_OK;
|
||||
credentials_nvs_close(handle);
|
||||
|
||||
return key_exists;
|
||||
}
|
||||
|
||||
bool load(const char* ssid, WifiApSettings* settings) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = 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;
|
||||
}
|
||||
|
||||
WifiApSettings encrypted_settings;
|
||||
size_t length = sizeof(WifiApSettings);
|
||||
result = nvs_get_blob(handle, ssid, &encrypted_settings, &length);
|
||||
|
||||
uint8_t iv[16];
|
||||
crypt::get_iv_from_string(ssid, iv);
|
||||
int decrypt_result = crypt::decrypt(
|
||||
iv,
|
||||
(uint8_t*)encrypted_settings.password,
|
||||
(uint8_t*)settings->password,
|
||||
TT_WIFI_CREDENTIALS_PASSWORD_LIMIT
|
||||
);
|
||||
// Manually ensure null termination, because encryption must be a multiple of 16 bytes
|
||||
encrypted_settings.password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT] = 0;
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
credentials_nvs_close(handle);
|
||||
|
||||
settings->auto_connect = encrypted_settings.auto_connect;
|
||||
strcpy((char*)settings->ssid, encrypted_settings.ssid);
|
||||
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool save(const WifiApSettings* settings) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = 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;
|
||||
}
|
||||
|
||||
WifiApSettings encrypted_settings = {
|
||||
.auto_connect = settings->auto_connect,
|
||||
};
|
||||
strcpy((char*)encrypted_settings.ssid, settings->ssid);
|
||||
// We only decrypt multiples of 16, so we have to ensure the last byte is set to 0
|
||||
encrypted_settings.password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT] = 0;
|
||||
|
||||
uint8_t iv[16];
|
||||
crypt::get_iv_from_data(settings->ssid, strlen(settings->ssid), iv);
|
||||
int encrypt_result = crypt::encrypt(
|
||||
iv,
|
||||
(uint8_t*)settings->password,
|
||||
(uint8_t*)encrypted_settings.password,
|
||||
TT_WIFI_CREDENTIALS_PASSWORD_LIMIT
|
||||
);
|
||||
|
||||
if (encrypt_result != 0) {
|
||||
result = ESP_FAIL;
|
||||
TT_LOG_E(TAG, "Failed to encrypt credentials \"%s\": %d", settings->ssid, encrypt_result);
|
||||
}
|
||||
|
||||
if (result == ESP_OK) {
|
||||
result = nvs_set_blob(handle, settings->ssid, &encrypted_settings, sizeof(WifiApSettings));
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to get credentials for \"%s\": %s", settings->ssid, esp_err_to_name(result));
|
||||
}
|
||||
}
|
||||
|
||||
credentials_nvs_close(handle);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool remove(const char* ssid) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = 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));
|
||||
}
|
||||
|
||||
credentials_nvs_close(handle);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
// end region Wi-Fi Credentials - public
|
||||
|
||||
} // nemespace
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef ESP_TARGET
|
||||
|
||||
#include "WifiSettings.h"
|
||||
#include "Log.h"
|
||||
|
||||
namespace tt::service::wifi::settings {
|
||||
|
||||
#define TAG "wifi_settings_mock"
|
||||
|
||||
bool contains(const char* ssid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool load(const char* ssid, WifiApSettings* settings) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool save(const WifiApSettings* settings) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool remove(const char* ssid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // ESP_TARGET
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "TactilityHeadless.h"
|
||||
#include "Hal/Configuration.h"
|
||||
#include "Hal/Hal_i.h"
|
||||
#include "ServiceManifest.h"
|
||||
#include "ServiceRegistry.h"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "EspInit.h"
|
||||
#endif
|
||||
|
||||
namespace tt {
|
||||
|
||||
#define TAG "tactility"
|
||||
|
||||
namespace service::wifi { extern const ServiceManifest manifest; }
|
||||
namespace service::sdcard { extern const ServiceManifest manifest; }
|
||||
|
||||
static const ServiceManifest* const system_services[] = {
|
||||
&service::sdcard::manifest,
|
||||
&service::wifi::manifest
|
||||
};
|
||||
|
||||
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*);
|
||||
for (int i = 0; i < app_count; ++i) {
|
||||
service_registry_add(system_services[i]);
|
||||
tt_check(service_registry_start(system_services[i]->id));
|
||||
}
|
||||
}
|
||||
|
||||
void headless_init(const hal::Configuration* config) {
|
||||
#ifdef ESP_PLATFORM
|
||||
esp_init();
|
||||
#endif
|
||||
hardwareConfig = config;
|
||||
hal::init(config);
|
||||
register_and_start_system_services();
|
||||
}
|
||||
|
||||
const hal::Configuration* get_hardware_config() {
|
||||
return hardwareConfig;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "Hal/Configuration.h"
|
||||
#include "TactilityHeadlessConfig.h"
|
||||
|
||||
namespace tt {
|
||||
|
||||
void headless_init(const hal::Configuration* config);
|
||||
|
||||
const hal::Configuration* get_hardware_config();
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define TT_CONFIG_SERVICES_LIMIT 32
|
||||
Reference in New Issue
Block a user