Ken Van Hoeylandt bab3eb19bc
Merge develop into main (#343)
- Refactor `AppManifest`: add new fields and rename existing ones
- Parse and validate the manifest from an app that is being installed.
- Remove deprecated `scoped()` from `Lock`
- Create `Tactility/Paths.h`
- App loading at boot now properly parses the manifest files of external apps
- Properly lock both source and destination locations during app install
- Remove LVGL path variants from `AppPaths` and `ServicePaths`
- Removed `xPath` base classes for apps and services. There's now `AppPaths` and `ServicePaths`.
- Renamed app and service paths: "data" and "system" paths are now "user data" and "assets"
2025-09-22 08:03:21 +02:00

162 lines
3.4 KiB
C++

#ifndef ESP_PLATFORM
#include <Tactility/service/wifi/Wifi.h>
#include <Tactility/Check.h>
#include <Tactility/Log.h>
#include <Tactility/Mutex.h>
#include <Tactility/PubSub.h>
#include <Tactility/service/Service.h>
#include <Tactility/service/ServiceManifest.h>
namespace tt::service::wifi {
constexpr auto* TAG = "Wifi";
struct Wifi {
/** @brief Locking mechanism for modifying the Wifi instance */
Mutex mutex = Mutex(Mutex::Type::Recursive);
/** @brief The public event bus */
std::shared_ptr<PubSub<WifiEvent>> pubsub = std::make_shared<PubSub<WifiEvent>>();
/** @brief The internal message queue */
bool scan_active = false;
bool secure_connection = false;
RadioState radio_state = RadioState::ConnectionActive;
};
static Wifi* wifi = nullptr;
// region Static
static void publish_event(WifiEvent event) {
wifi->pubsub->publish(event);
}
// endregion Static
// region Public functions
std::shared_ptr<PubSub<WifiEvent>> getPubsub() {
assert(wifi);
return wifi->pubsub;
}
RadioState getRadioState() {
return wifi->radio_state;
}
std::string getConnectionTarget() {
return "Home Wifi";
}
void scan() {
assert(wifi);
wifi->scan_active = false; // TODO: enable and then later disable automatically
}
bool isScanning() {
assert(wifi);
return wifi->scan_active;
}
void connect(const settings::WifiApSettings& ap, bool remember) {
assert(wifi);
// TODO: implement
}
void disconnect() {
assert(wifi);
}
void setScanRecords(uint16_t records) {
assert(wifi);
// TODO: implement
}
std::vector<ApRecord> getScanResults() {
tt_check(wifi);
std::vector<ApRecord> records;
records.push_back((ApRecord) {
.ssid = "Home Wifi",
.rssi = -30,
.auth_mode = WIFI_AUTH_WPA2_PSK
});
records.push_back((ApRecord) {
.ssid = "No place like 127.0.0.1",
.rssi = -67,
.auth_mode = WIFI_AUTH_WPA2_PSK
});
records.push_back((ApRecord) {
.ssid = "Pretty fly for a Wi-Fi",
.rssi = -70,
.auth_mode = WIFI_AUTH_WPA2_PSK
});
records.push_back((ApRecord) {
.ssid = "An AP with a really, really long name",
.rssi = -80,
.auth_mode = WIFI_AUTH_WPA2_PSK
});
records.push_back((ApRecord) {
.ssid = "Bad Reception",
.rssi = -90,
.auth_mode = WIFI_AUTH_OPEN
});
return records;
}
void setEnabled(bool enabled) {
assert(wifi != nullptr);
if (enabled) {
wifi->radio_state = RadioState::On;
wifi->secure_connection = true;
} else {
wifi->radio_state = RadioState::Off;
}
}
bool isConnectionSecure() {
return wifi->secure_connection;
}
int getRssi() {
if (wifi->radio_state == RadioState::ConnectionActive) {
return -30;
} else {
return 0;
}
}
std::string getIp() {
return "192.168.1.2";
}
// endregion Public functions
class WifiService final : public Service {
public:
bool onStart(TT_UNUSED ServiceContext& service) override {
tt_check(wifi == nullptr);
wifi = new Wifi();
return true;
}
void onStop(TT_UNUSED ServiceContext& service) override {
tt_check(wifi != nullptr);
delete wifi;
wifi = nullptr;
}
};
extern const ServiceManifest manifest = {
.id = "Wifi",
.createService = create<WifiService>
};
} // namespace
#endif // ESP_PLATFORM