Merge develop into main (#167)
- WiFi Connect app is now hidden by default, but accessible at the bottom of the WiFi Manage app when WiFi is turned on. - WiFi service now turns on WiFi when calling connect() and WiFi is not on. - Removed `blocking` option for `service::loader::startApp()`. This feature was unused and complex. - Various apps: Moved private headers into Private/ folder. - Various apps: created start() function for easy starting. - Added documentation to all TactilityC APIs - Refactored various `enum` into `class enum` - Refactor M5Stack `initBoot()` (but VBus is still 0V for some reason)
This commit is contained in:
committed by
GitHub
parent
3ca0f8cf97
commit
3ea02d912f
@@ -8,7 +8,7 @@ class ServiceInstance : public ServiceContext {
|
||||
|
||||
private:
|
||||
|
||||
Mutex mutex = Mutex(Mutex::TypeNormal);
|
||||
Mutex mutex = Mutex(Mutex::Type::Normal);
|
||||
const service::ServiceManifest& manifest;
|
||||
std::shared_ptr<void> data = nullptr;
|
||||
|
||||
|
||||
@@ -11,11 +11,11 @@ public:
|
||||
Power() = default;
|
||||
virtual ~Power() = default;
|
||||
|
||||
enum MetricType {
|
||||
IS_CHARGING, // bool
|
||||
CURRENT, // int32_t, mAh - battery current: either during charging (positive value) or discharging (negative value)
|
||||
BATTERY_VOLTAGE, // uint32_t, mV
|
||||
CHARGE_LEVEL, // uint8_t [0, 100]
|
||||
enum class MetricType {
|
||||
IsCharging, // bool
|
||||
Current, // int32_t, mAh - battery current: either during charging (positive value) or discharging (negative value)
|
||||
BatteryVoltage, // uint32_t, mV
|
||||
ChargeLevel, // uint8_t [0, 100]
|
||||
};
|
||||
|
||||
union MetricData {
|
||||
|
||||
@@ -9,16 +9,16 @@ namespace tt::hal {
|
||||
|
||||
class SdCard {
|
||||
public:
|
||||
enum State {
|
||||
StateMounted,
|
||||
StateUnmounted,
|
||||
StateError,
|
||||
StateUnknown
|
||||
enum class State {
|
||||
Mounted,
|
||||
Unmounted,
|
||||
Error,
|
||||
Unknown
|
||||
};
|
||||
|
||||
enum MountBehaviour {
|
||||
MountBehaviourAtBoot, /** Only mount at boot */
|
||||
MountBehaviourAnytime /** Mount/dismount any time */
|
||||
enum class MountBehaviour {
|
||||
AtBoot, /** Only mount at boot */
|
||||
Anytime /** Mount/dismount any time */
|
||||
};
|
||||
|
||||
private:
|
||||
@@ -33,7 +33,7 @@ public:
|
||||
virtual State getState() const = 0;
|
||||
|
||||
virtual MountBehaviour getMountBehaviour() const { return mountBehaviour; }
|
||||
bool isMounted() const { return getState() == StateMounted; }
|
||||
bool isMounted() const { return getState() == State::Mounted; }
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -127,7 +127,7 @@ bool SpiSdCard::unmount() {
|
||||
// TODO: Refactor to "bool getStatus(Status* status)" method so that it can fail when the lvgl lock fails
|
||||
tt::hal::SdCard::State SpiSdCard::getState() const {
|
||||
if (card == nullptr) {
|
||||
return StateUnmounted;
|
||||
return State::Unmounted;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,7 +139,7 @@ tt::hal::SdCard::State SpiSdCard::getState() const {
|
||||
bool locked = config->lockable->lock(50); // TODO: Refactor to a more reliable locking mechanism
|
||||
if (!locked) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "LVGL");
|
||||
return StateUnknown;
|
||||
return State::Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,9 +150,9 @@ tt::hal::SdCard::State SpiSdCard::getState() const {
|
||||
}
|
||||
|
||||
if (result) {
|
||||
return StateMounted;
|
||||
return State::Mounted;
|
||||
} else {
|
||||
return StateError;
|
||||
return State::Error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,11 +21,23 @@ static Data dataArray[I2C_NUM_MAX];
|
||||
|
||||
#define TAG "i2c"
|
||||
|
||||
const char* initModeToString(InitMode mode) {
|
||||
switch (mode) {
|
||||
case InitMode::ByTactility:
|
||||
return TT_STRINGIFY(InitMode::ByTactility);
|
||||
case InitMode::ByExternal:
|
||||
return TT_STRINGIFY(InitMode::ByExternal);
|
||||
case InitMode::Disabled:
|
||||
return TT_STRINGIFY(InitMode::Disabled);
|
||||
}
|
||||
tt_crash("not implemented");
|
||||
}
|
||||
|
||||
void printInfo(const Data& data) {
|
||||
TT_LOG_V(TAG, "I2C info for port %d", data.configuration.port);
|
||||
TT_LOG_V(TAG, " isStarted: %d", data.isStarted);
|
||||
TT_LOG_V(TAG, " isConfigured: %d", data.isConfigured);
|
||||
TT_LOG_V(TAG, " initMode: %d", data.configuration.initMode);
|
||||
TT_LOG_V(TAG, " initMode: %s", initModeToString(data.configuration.initMode));
|
||||
TT_LOG_V(TAG, " canReinit: %d", data.configuration.canReinit);
|
||||
TT_LOG_V(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration);
|
||||
TT_LOG_V(TAG, " SDA pin: %d", data.configuration.config.sda_io_num);
|
||||
@@ -46,11 +58,11 @@ bool init(const std::vector<i2c::Configuration>& configurations) {
|
||||
|
||||
for (const auto& config: configurations) {
|
||||
printInfo(dataArray[config.port]);
|
||||
if (config.initMode == InitByTactility) {
|
||||
if (config.initMode == InitMode::ByTactility) {
|
||||
if (!start(config.port)) {
|
||||
return false;
|
||||
}
|
||||
} else if (config.initMode == InitByExternal) {
|
||||
} else if (config.initMode == InitMode::ByExternal) {
|
||||
dataArray[config.port].isStarted = true;
|
||||
}
|
||||
}
|
||||
@@ -177,6 +189,7 @@ bool isStarted(i2c_port_t port) {
|
||||
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout) {
|
||||
if (lock(port)) {
|
||||
// TODO: We're passing an inaccurate timeout value as we already lost time with locking and previous writes in this loop
|
||||
esp_err_t result = i2c_master_read_from_device(port, address, data, dataSize, timeout);
|
||||
unlock(port);
|
||||
return result == ESP_OK;
|
||||
@@ -186,9 +199,7 @@ bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize
|
||||
}
|
||||
}
|
||||
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout) {
|
||||
tt_check(reg != 0);
|
||||
|
||||
bool masterReadRegister(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout) {
|
||||
if (!lock(port)) {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
|
||||
return false;
|
||||
@@ -207,6 +218,7 @@ bool masterRead(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, si
|
||||
}
|
||||
i2c_master_read_byte(cmd, data + dataSize - 1, I2C_MASTER_NACK);
|
||||
i2c_master_stop(cmd);
|
||||
// TODO: We're passing an inaccurate timeout value as we already lost time with locking
|
||||
esp_err_t result = i2c_master_cmd_begin(port, cmd, timeout);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
|
||||
@@ -218,8 +230,9 @@ bool masterRead(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, si
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool masterWrite(i2c_port_t port, uint16_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
bool masterWrite(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
if (lock(port)) {
|
||||
// TODO: We're passing an inaccurate timeout value as we already lost time with locking
|
||||
esp_err_t result = i2c_master_write_to_device(port, address, data, dataSize, timeout);
|
||||
unlock(port);
|
||||
return result == ESP_OK;
|
||||
@@ -229,7 +242,7 @@ bool masterWrite(i2c_port_t port, uint16_t address, const uint8_t* data, uint16_
|
||||
}
|
||||
}
|
||||
|
||||
bool masterWrite(i2c_port_t port, uint16_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
bool masterWriteRegister(i2c_port_t port, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
tt_check(reg != 0);
|
||||
|
||||
if (!lock(port)) {
|
||||
@@ -243,6 +256,7 @@ bool masterWrite(i2c_port_t port, uint16_t address, uint8_t reg, const uint8_t*
|
||||
i2c_master_write_byte(cmd, reg, ACK_CHECK_EN);
|
||||
i2c_master_write(cmd, (uint8_t*) data, dataSize, ACK_CHECK_EN);
|
||||
i2c_master_stop(cmd);
|
||||
// TODO: We're passing an inaccurate timeout value as we already lost time with locking
|
||||
esp_err_t result = i2c_master_cmd_begin(port, cmd, timeout);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
|
||||
@@ -253,8 +267,21 @@ bool masterWrite(i2c_port_t port, uint16_t address, uint8_t reg, const uint8_t*
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool masterWriteRegisterArray(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
assert(dataSize % 2 == 0);
|
||||
bool result = true;
|
||||
for (int i = 0; i < dataSize; i += 2) {
|
||||
// TODO: We're passing an inaccurate timeout value as we already lost time with locking and previous writes in this loop
|
||||
if (!masterWriteRegister(port, address, data[i], &data[i + 1], 1, timeout)) {
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize, TickType_t timeout) {
|
||||
if (lock(port)) {
|
||||
// TODO: We're passing an inaccurate timeout value as we already lost time with locking
|
||||
esp_err_t result = i2c_master_write_read_device(port, address, writeData, writeDataSize, readData, readDataSize, timeout);
|
||||
unlock(port);
|
||||
return result == ESP_OK;
|
||||
@@ -267,6 +294,7 @@ bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData,
|
||||
bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeout) {
|
||||
if (lock(port)) {
|
||||
uint8_t message[2] = { 0, 0 };
|
||||
// TODO: We're passing an inaccurate timeout value as we already lost time with locking
|
||||
esp_err_t result = i2c_master_write_to_device(port, address, message, 2, timeout);
|
||||
unlock(port);
|
||||
return result == ESP_OK;
|
||||
|
||||
@@ -9,11 +9,11 @@
|
||||
|
||||
namespace tt::hal::i2c {
|
||||
|
||||
typedef enum {
|
||||
InitByTactility, // Tactility will initialize it in the correct bootup phase
|
||||
InitByExternal, // The device is already initialized and Tactility should assume it works
|
||||
InitDisabled // Not initialized by default
|
||||
} InitMode;
|
||||
enum class InitMode {
|
||||
ByTactility, // Tactility will initialize it in the correct bootup phase
|
||||
ByExternal, // The device is already initialized and Tactility should assume it works
|
||||
Disabled // Not initialized by default
|
||||
};
|
||||
|
||||
struct Configuration {
|
||||
std::string name;
|
||||
@@ -35,7 +35,6 @@ enum Status {
|
||||
UNKNOWN
|
||||
};
|
||||
|
||||
|
||||
bool init(const std::vector<i2c::Configuration>& configurations);
|
||||
|
||||
bool start(i2c_port_t port);
|
||||
@@ -43,9 +42,10 @@ bool stop(i2c_port_t port);
|
||||
bool isStarted(i2c_port_t port);
|
||||
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout);
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout);
|
||||
bool masterWrite(i2c_port_t port, uint16_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout);
|
||||
bool masterWrite(i2c_port_t port, uint16_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout);
|
||||
bool masterReadRegister(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout);
|
||||
bool masterWrite(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout);
|
||||
bool masterWriteRegister(i2c_port_t port, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout);
|
||||
bool masterWriteRegisterArray(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout);
|
||||
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize, TickType_t timeout);
|
||||
bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeout);
|
||||
|
||||
|
||||
@@ -28,9 +28,9 @@ bool init(const std::vector<i2c::Configuration>& configurations) {
|
||||
}
|
||||
|
||||
for (const auto& config: configurations) {
|
||||
if (config.initMode == InitByTactility && !start(config.port)) {
|
||||
if (config.initMode == InitMode::ByTactility && !start(config.port)) {
|
||||
return false;
|
||||
} else if (config.initMode == InitByExternal) {
|
||||
} else if (config.initMode == InitMode::ByExternal) {
|
||||
dataArray[config.port].isStarted = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ struct BootMode {
|
||||
uint32_t flag = 0;
|
||||
};
|
||||
|
||||
static Mode currentMode = ModeDefault;
|
||||
static Mode currentMode = Mode::Default;
|
||||
static RTC_NOINIT_ATTR BootMode bootMode;
|
||||
|
||||
sdmmc_card_t* _Nullable getCard() {
|
||||
@@ -47,7 +47,7 @@ sdmmc_card_t* _Nullable getCard() {
|
||||
}
|
||||
|
||||
static bool canStartNewMode() {
|
||||
return isSupported() && (currentMode == ModeDefault || currentMode == ModeNone);
|
||||
return isSupported() && (currentMode == Mode::Default || currentMode == Mode::None);
|
||||
}
|
||||
|
||||
bool isSupported() {
|
||||
@@ -65,7 +65,7 @@ bool startMassStorageWithSdmmc() {
|
||||
TT_LOG_E(TAG, "Failed to init mass storage: %s", esp_err_to_name(result));
|
||||
return false;
|
||||
} else {
|
||||
currentMode = ModeMassStorageSdmmc;
|
||||
currentMode = Mode::MassStorageSdmmc;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -77,7 +77,7 @@ void stop() {
|
||||
|
||||
tusbStop();
|
||||
|
||||
currentMode = ModeNone;
|
||||
currentMode = Mode::None;
|
||||
}
|
||||
|
||||
Mode getMode() {
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
namespace tt::hal::usb {
|
||||
|
||||
enum Mode {
|
||||
ModeDefault, // Default state of USB stack
|
||||
ModeNone, // State after TinyUSB was used and (partially) deinitialized
|
||||
ModeMassStorageSdmmc
|
||||
enum class Mode {
|
||||
Default, // Default state of USB stack
|
||||
None, // State after TinyUSB was used and (partially) deinitialized
|
||||
MassStorageSdmmc
|
||||
};
|
||||
|
||||
bool startMassStorageWithSdmmc();
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace tt::hal::usb {
|
||||
|
||||
bool startMassStorageWithSdmmc() { return false; }
|
||||
void stop() {}
|
||||
Mode getMode() { return ModeDefault; }
|
||||
Mode getMode() { return Mode::Default; }
|
||||
bool isSupported() { return false; }
|
||||
|
||||
bool canRebootIntoMassStorageSdmmc() { return false; }
|
||||
|
||||
@@ -17,8 +17,8 @@ typedef std::unordered_map<std::string, ServiceInstance*> ServiceInstanceMap;
|
||||
static ManifestMap service_manifest_map;
|
||||
static ServiceInstanceMap service_instance_map;
|
||||
|
||||
static Mutex manifest_mutex(Mutex::TypeNormal);
|
||||
static Mutex instance_mutex(Mutex::TypeNormal);
|
||||
static Mutex manifest_mutex(Mutex::Type::Normal);
|
||||
static Mutex instance_mutex(Mutex::Type::Normal);
|
||||
|
||||
void addService(const ServiceManifest* manifest) {
|
||||
TT_LOG_I(TAG, "Adding %s", manifest->id.c_str());
|
||||
|
||||
@@ -17,7 +17,7 @@ extern const ServiceManifest manifest;
|
||||
struct ServiceData {
|
||||
Mutex mutex;
|
||||
std::unique_ptr<Timer> updateTimer;
|
||||
hal::SdCard::State lastState = hal::SdCard::StateUnmounted;
|
||||
hal::SdCard::State lastState = hal::SdCard::State::Unmounted;
|
||||
|
||||
bool lock(TickType_t timeout) const {
|
||||
return mutex.acquire(timeout) == TtStatusOk;
|
||||
@@ -44,7 +44,7 @@ static void onUpdate(std::shared_ptr<void> context) {
|
||||
|
||||
auto new_state = sdcard->getState();
|
||||
|
||||
if (new_state == hal::SdCard::StateError) {
|
||||
if (new_state == hal::SdCard::State::Error) {
|
||||
TT_LOG_W(TAG, "Sdcard error - unmounting. Did you eject the card in an unsafe manner?");
|
||||
sdcard->unmount();
|
||||
}
|
||||
@@ -61,7 +61,7 @@ static void onStart(ServiceContext& service) {
|
||||
auto data = std::make_shared<ServiceData>();
|
||||
service.setData(data);
|
||||
|
||||
data->updateTimer = std::make_unique<Timer>(Timer::TypePeriodic, onUpdate, data);
|
||||
data->updateTimer = std::make_unique<Timer>(Timer::Type::Periodic, onUpdate, data);
|
||||
// We want to try and scan more often in case of startup or scan lock failure
|
||||
data->updateTimer->start(1000);
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "./Wifi.h"
|
||||
|
||||
namespace tt::service::wifi {
|
||||
|
||||
const char* radioStateToString(RadioState state) {
|
||||
switch (state) {
|
||||
case RadioState::OnPending:
|
||||
return TT_STRINGIFY(RadioState::OnPending);
|
||||
case RadioState::On:
|
||||
return TT_STRINGIFY(RadioState::On);
|
||||
case RadioState::ConnectionPending:
|
||||
return TT_STRINGIFY(RadioState::ConnectionPending);
|
||||
case RadioState::ConnectionActive:
|
||||
return TT_STRINGIFY(RadioState::ConnectionActive);
|
||||
case RadioState::OffPending:
|
||||
return TT_STRINGIFY(RadioState::OnPending);
|
||||
case RadioState::Off:
|
||||
return TT_STRINGIFY(RadioState::Off);
|
||||
}
|
||||
tt_crash("not implemented");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -34,39 +34,39 @@ typedef enum {
|
||||
|
||||
namespace tt::service::wifi {
|
||||
|
||||
enum WifiEventType {
|
||||
enum class EventType {
|
||||
/** Radio was turned on */
|
||||
WifiEventTypeRadioStateOn,
|
||||
RadioStateOn,
|
||||
/** Radio is turning on. */
|
||||
WifiEventTypeRadioStateOnPending,
|
||||
RadioStateOnPending,
|
||||
/** Radio is turned off */
|
||||
WifiEventTypeRadioStateOff,
|
||||
RadioStateOff,
|
||||
/** Radio is turning off */
|
||||
WifiEventTypeRadioStateOffPending,
|
||||
RadioStateOffPending,
|
||||
/** Started scanning for access points */
|
||||
WifiEventTypeScanStarted,
|
||||
ScanStarted,
|
||||
/** Finished scanning for access points */ // TODO: 1 second validity
|
||||
WifiEventTypeScanFinished,
|
||||
WifiEventTypeDisconnected,
|
||||
WifiEventTypeConnectionPending,
|
||||
WifiEventTypeConnectionSuccess,
|
||||
WifiEventTypeConnectionFailed
|
||||
ScanFinished,
|
||||
Disconnected,
|
||||
ConnectionPending,
|
||||
ConnectionSuccess,
|
||||
ConnectionFailed
|
||||
};
|
||||
|
||||
enum WifiRadioState {
|
||||
WIFI_RADIO_ON_PENDING,
|
||||
WIFI_RADIO_ON,
|
||||
WIFI_RADIO_CONNECTION_PENDING,
|
||||
WIFI_RADIO_CONNECTION_ACTIVE,
|
||||
WIFI_RADIO_OFF_PENDING,
|
||||
WIFI_RADIO_OFF,
|
||||
enum class RadioState {
|
||||
OnPending,
|
||||
On,
|
||||
ConnectionPending,
|
||||
ConnectionActive,
|
||||
OffPending,
|
||||
Off,
|
||||
};
|
||||
|
||||
struct WifiEvent {
|
||||
WifiEventType type;
|
||||
struct Event {
|
||||
EventType type;
|
||||
};
|
||||
|
||||
struct WifiApRecord {
|
||||
struct ApRecord {
|
||||
std::string ssid;
|
||||
int8_t rssi;
|
||||
wifi_auth_mode_t auth_mode;
|
||||
@@ -78,7 +78,12 @@ struct WifiApRecord {
|
||||
*/
|
||||
std::shared_ptr<PubSub> getPubsub();
|
||||
|
||||
WifiRadioState getRadioState();
|
||||
/** @return Get the current radio state */
|
||||
RadioState getRadioState();
|
||||
|
||||
/** For logging purposes */
|
||||
const char* radioStateToString(RadioState state);
|
||||
|
||||
/**
|
||||
* @brief Request scanning update. Returns immediately. Results are through pubsub.
|
||||
*/
|
||||
@@ -91,7 +96,7 @@ bool isScanning();
|
||||
std::string getConnectionTarget();
|
||||
|
||||
/** @return the access points from the last scan (if any). It only contains public APs. */
|
||||
std::vector<WifiApRecord> getScanResults();
|
||||
std::vector<ApRecord> getScanResults();
|
||||
|
||||
/**
|
||||
* @brief Overrides the default scan result size of 16.
|
||||
|
||||
@@ -35,15 +35,15 @@ class Wifi {
|
||||
|
||||
private:
|
||||
|
||||
std::atomic<WifiRadioState> radio_state = WIFI_RADIO_OFF;
|
||||
std::atomic<RadioState> radio_state = RadioState::Off;
|
||||
bool scan_active = false;
|
||||
bool secure_connection = false;
|
||||
|
||||
public:
|
||||
|
||||
/** @brief Locking mechanism for modifying the Wifi instance */
|
||||
Mutex radioMutex = Mutex(Mutex::TypeRecursive);
|
||||
Mutex dataMutex = Mutex(Mutex::TypeRecursive);
|
||||
Mutex radioMutex = Mutex(Mutex::Type::Recursive);
|
||||
Mutex dataMutex = Mutex(Mutex::Type::Recursive);
|
||||
std::unique_ptr<Timer> autoConnectTimer;
|
||||
/** @brief The public event bus */
|
||||
std::shared_ptr<PubSub> pubsub = std::make_shared<PubSub>();
|
||||
@@ -71,14 +71,14 @@ public:
|
||||
bool pause_auto_connect = false; // Pause when manually disconnecting until manually connecting again
|
||||
bool connection_target_remember = false; // Whether to store the connection_target on successful connection or not
|
||||
|
||||
WifiRadioState getRadioState() const {
|
||||
RadioState getRadioState() const {
|
||||
auto lockable = dataMutex.scoped();
|
||||
lockable->lock(TtWaitForever);
|
||||
// TODO: Handle lock failure
|
||||
return radio_state;
|
||||
}
|
||||
|
||||
void setRadioState(WifiRadioState newState) {
|
||||
void setRadioState(RadioState newState) {
|
||||
auto lockable = dataMutex.scoped();
|
||||
lockable->lock(TtWaitForever);
|
||||
// TODO: Handle lock failure
|
||||
@@ -89,7 +89,7 @@ public:
|
||||
auto lockable = dataMutex.scoped();
|
||||
lockable->lock(TtWaitForever);
|
||||
// TODO: Handle lock failure
|
||||
return radio_state;
|
||||
return scan_active;
|
||||
}
|
||||
|
||||
void setScanning(bool newState) {
|
||||
@@ -138,12 +138,12 @@ std::shared_ptr<PubSub> getPubsub() {
|
||||
return wifi->pubsub;
|
||||
}
|
||||
|
||||
WifiRadioState getRadioState() {
|
||||
RadioState getRadioState() {
|
||||
auto wifi = wifi_singleton;
|
||||
if (wifi != nullptr) {
|
||||
return wifi->getRadioState();
|
||||
} else {
|
||||
return WIFI_RADIO_OFF;
|
||||
return RadioState::Off;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,10 +153,10 @@ std::string getConnectionTarget() {
|
||||
return "";
|
||||
}
|
||||
|
||||
WifiRadioState state = wifi->getRadioState();
|
||||
RadioState state = wifi->getRadioState();
|
||||
if (
|
||||
state != WIFI_RADIO_CONNECTION_PENDING &&
|
||||
state != WIFI_RADIO_CONNECTION_ACTIVE
|
||||
state != RadioState::ConnectionPending &&
|
||||
state != RadioState::ConnectionActive
|
||||
) {
|
||||
return "";
|
||||
}
|
||||
@@ -199,6 +199,11 @@ void connect(const settings::WifiApSettings* ap, bool remember) {
|
||||
wifi->pause_auto_connect = true;
|
||||
memcpy(&wifi->connection_target, ap, sizeof(settings::WifiApSettings));
|
||||
wifi->connection_target_remember = remember;
|
||||
|
||||
if (wifi->getRadioState() == RadioState::Off) {
|
||||
getMainDispatcher().dispatch(dispatchEnable, wifi);
|
||||
}
|
||||
|
||||
getMainDispatcher().dispatch(dispatchConnect, wifi);
|
||||
}
|
||||
|
||||
@@ -242,11 +247,11 @@ void setScanRecords(uint16_t records) {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<WifiApRecord> getScanResults() {
|
||||
std::vector<ApRecord> getScanResults() {
|
||||
TT_LOG_I(TAG, "getScanResults()");
|
||||
auto wifi = wifi_singleton;
|
||||
|
||||
std::vector<WifiApRecord> records;
|
||||
std::vector<ApRecord> records;
|
||||
|
||||
if (wifi == nullptr) {
|
||||
return records;
|
||||
@@ -260,7 +265,7 @@ std::vector<WifiApRecord> getScanResults() {
|
||||
if (wifi->scan_list_count > 0) {
|
||||
uint16_t i = 0;
|
||||
for (; i < wifi->scan_list_count; ++i) {
|
||||
records.push_back((WifiApRecord) {
|
||||
records.push_back((ApRecord) {
|
||||
.ssid = (const char*)wifi->scan_list[i].ssid,
|
||||
.rssi = wifi->scan_list[i].rssi,
|
||||
.auth_mode = wifi->scan_list[i].authmode
|
||||
@@ -355,18 +360,18 @@ static void scan_list_free_safely(std::shared_ptr<Wifi> wifi) {
|
||||
}
|
||||
}
|
||||
|
||||
static void publish_event_simple(std::shared_ptr<Wifi> wifi, WifiEventType type) {
|
||||
static void publish_event_simple(std::shared_ptr<Wifi> wifi, EventType type) {
|
||||
auto lockable = wifi->dataMutex.scoped();
|
||||
if (lockable->lock(TtWaitForever)) {
|
||||
WifiEvent turning_on_event = {.type = type};
|
||||
Event turning_on_event = {.type = type};
|
||||
tt_pubsub_publish(wifi->pubsub, &turning_on_event);
|
||||
}
|
||||
}
|
||||
|
||||
static bool copy_scan_list(std::shared_ptr<Wifi> wifi) {
|
||||
auto state = wifi->getRadioState();
|
||||
bool can_fetch_results = (state == WIFI_RADIO_ON || state == WIFI_RADIO_CONNECTION_ACTIVE) &&
|
||||
wifi->isScanActive();
|
||||
bool can_fetch_results = (state == RadioState::On || state == RadioState::ConnectionActive) &&
|
||||
wifi->isScanActive();
|
||||
|
||||
if (!can_fetch_results) {
|
||||
TT_LOG_I(TAG, "Skip scan result fetching");
|
||||
@@ -443,20 +448,20 @@ static void eventHandler(TT_UNUSED void* arg, esp_event_base_t event_base, int32
|
||||
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
TT_LOG_I(TAG, "eventHandler: sta start");
|
||||
if (wifi->getRadioState() == WIFI_RADIO_CONNECTION_PENDING) {
|
||||
if (wifi->getRadioState() == RadioState::ConnectionPending) {
|
||||
esp_wifi_connect();
|
||||
}
|
||||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
TT_LOG_I(TAG, "eventHandler: disconnected");
|
||||
if (wifi->getRadioState() == WIFI_RADIO_CONNECTION_PENDING) {
|
||||
if (wifi->getRadioState() == RadioState::ConnectionPending) {
|
||||
wifi->connection_wait_flags.set(WIFI_FAIL_BIT);
|
||||
}
|
||||
wifi->setRadioState(WIFI_RADIO_ON);
|
||||
publish_event_simple(wifi, WifiEventTypeDisconnected);
|
||||
wifi->setRadioState(RadioState::On);
|
||||
publish_event_simple(wifi, EventType::Disconnected);
|
||||
} 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, "eventHandler: got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||
if (wifi->getRadioState() == WIFI_RADIO_CONNECTION_PENDING) {
|
||||
if (wifi->getRadioState() == RadioState::ConnectionPending) {
|
||||
wifi->connection_wait_flags.set(WIFI_CONNECTED_BIT);
|
||||
// We resume auto-connecting only when there was an explicit request by the user for the connection
|
||||
// TODO: Make thread-safe
|
||||
@@ -469,17 +474,17 @@ static void eventHandler(TT_UNUSED void* arg, esp_event_base_t event_base, int32
|
||||
|
||||
auto state = wifi->getRadioState();
|
||||
if (
|
||||
state != WIFI_RADIO_OFF &&
|
||||
state != WIFI_RADIO_OFF_PENDING
|
||||
state != RadioState::Off &&
|
||||
state != RadioState::OffPending
|
||||
) {
|
||||
wifi->setScanActive(false);
|
||||
esp_wifi_scan_stop();
|
||||
}
|
||||
|
||||
publish_event_simple(wifi_singleton, WifiEventTypeScanFinished);
|
||||
publish_event_simple(wifi_singleton, EventType::ScanFinished);
|
||||
TT_LOG_I(TAG, "eventHandler: Finished scan");
|
||||
|
||||
if (copied_list && wifi_singleton->getRadioState() == WIFI_RADIO_ON && !wifi->pause_auto_connect) {
|
||||
if (copied_list && wifi_singleton->getRadioState() == RadioState::On && !wifi->pause_auto_connect) {
|
||||
getMainDispatcher().dispatch(dispatchAutoConnect, wifi);
|
||||
}
|
||||
}
|
||||
@@ -489,12 +494,12 @@ static void dispatchEnable(std::shared_ptr<void> context) {
|
||||
TT_LOG_I(TAG, "dispatchEnable()");
|
||||
auto wifi = std::static_pointer_cast<Wifi>(context);
|
||||
|
||||
WifiRadioState state = wifi->getRadioState();
|
||||
RadioState state = wifi->getRadioState();
|
||||
if (
|
||||
state == WIFI_RADIO_ON ||
|
||||
state == WIFI_RADIO_ON_PENDING ||
|
||||
state == WIFI_RADIO_OFF_PENDING
|
||||
) {
|
||||
state == RadioState::On ||
|
||||
state == RadioState::OnPending ||
|
||||
state == RadioState::OffPending
|
||||
) {
|
||||
TT_LOG_W(TAG, "Can't enable from current state");
|
||||
return;
|
||||
}
|
||||
@@ -503,8 +508,8 @@ static void dispatchEnable(std::shared_ptr<void> context) {
|
||||
|
||||
if (lockable->lock(50 / portTICK_PERIOD_MS)) {
|
||||
TT_LOG_I(TAG, "Enabling");
|
||||
wifi->setRadioState(WIFI_RADIO_ON_PENDING);
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOnPending);
|
||||
wifi->setRadioState(RadioState::OnPending);
|
||||
publish_event_simple(wifi, EventType::RadioStateOnPending);
|
||||
|
||||
if (wifi->netif != nullptr) {
|
||||
esp_netif_destroy(wifi->netif);
|
||||
@@ -520,8 +525,8 @@ static void dispatchEnable(std::shared_ptr<void> context) {
|
||||
if (init_result == ESP_ERR_NO_MEM) {
|
||||
TT_LOG_E(TAG, "Insufficient memory");
|
||||
}
|
||||
wifi->setRadioState(WIFI_RADIO_OFF);
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
wifi->setRadioState(RadioState::Off);
|
||||
publish_event_simple(wifi, EventType::RadioStateOff);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -547,9 +552,9 @@ static void dispatchEnable(std::shared_ptr<void> context) {
|
||||
|
||||
if (esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Wifi mode setting failed");
|
||||
wifi->setRadioState(WIFI_RADIO_OFF);
|
||||
wifi->setRadioState(RadioState::Off);
|
||||
esp_wifi_deinit();
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
publish_event_simple(wifi, EventType::RadioStateOff);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -559,15 +564,15 @@ static void dispatchEnable(std::shared_ptr<void> context) {
|
||||
if (start_result == ESP_ERR_NO_MEM) {
|
||||
TT_LOG_E(TAG, "Insufficient memory");
|
||||
}
|
||||
wifi->setRadioState(WIFI_RADIO_OFF);
|
||||
wifi->setRadioState(RadioState::Off);
|
||||
esp_wifi_set_mode(WIFI_MODE_NULL);
|
||||
esp_wifi_deinit();
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
publish_event_simple(wifi, EventType::RadioStateOff);
|
||||
return;
|
||||
}
|
||||
|
||||
wifi->setRadioState(WIFI_RADIO_ON);
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOn);
|
||||
wifi->setRadioState(RadioState::On);
|
||||
publish_event_simple(wifi, EventType::RadioStateOn);
|
||||
TT_LOG_I(TAG, "Enabled");
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
|
||||
@@ -584,27 +589,27 @@ static void dispatchDisable(std::shared_ptr<void> context) {
|
||||
return;
|
||||
}
|
||||
|
||||
WifiRadioState state = wifi->getRadioState();
|
||||
RadioState state = wifi->getRadioState();
|
||||
if (
|
||||
state == WIFI_RADIO_OFF ||
|
||||
state == WIFI_RADIO_OFF_PENDING ||
|
||||
state == WIFI_RADIO_ON_PENDING
|
||||
) {
|
||||
state == RadioState::Off ||
|
||||
state == RadioState::OffPending ||
|
||||
state == RadioState::OnPending
|
||||
) {
|
||||
TT_LOG_W(TAG, "Can't disable from current state");
|
||||
return;
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "Disabling");
|
||||
wifi->setRadioState(WIFI_RADIO_OFF_PENDING);
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOffPending);
|
||||
wifi->setRadioState(RadioState::OffPending);
|
||||
publish_event_simple(wifi, EventType::RadioStateOffPending);
|
||||
|
||||
// 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->setRadioState(WIFI_RADIO_ON);
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOn);
|
||||
wifi->setRadioState(RadioState::On);
|
||||
publish_event_simple(wifi, EventType::RadioStateOn);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -636,8 +641,8 @@ static void dispatchDisable(std::shared_ptr<void> context) {
|
||||
esp_netif_destroy(wifi->netif);
|
||||
wifi->netif = nullptr;
|
||||
wifi->setScanActive(false);
|
||||
wifi->setRadioState(WIFI_RADIO_OFF);
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
wifi->setRadioState(RadioState::Off);
|
||||
publish_event_simple(wifi, EventType::RadioStateOff);
|
||||
TT_LOG_I(TAG, "Disabled");
|
||||
}
|
||||
|
||||
@@ -651,8 +656,8 @@ static void dispatchScan(std::shared_ptr<void> context) {
|
||||
return;
|
||||
}
|
||||
|
||||
WifiRadioState state = wifi->getRadioState();
|
||||
if (state != WIFI_RADIO_ON && state != WIFI_RADIO_CONNECTION_ACTIVE && state != WIFI_RADIO_CONNECTION_PENDING) {
|
||||
RadioState state = wifi->getRadioState();
|
||||
if (state != RadioState::On && state != RadioState::ConnectionActive && state != RadioState::ConnectionPending) {
|
||||
TT_LOG_W(TAG, "Scan unavailable: wifi not enabled");
|
||||
return;
|
||||
}
|
||||
@@ -672,7 +677,7 @@ static void dispatchScan(std::shared_ptr<void> context) {
|
||||
|
||||
TT_LOG_I(TAG, "Starting scan");
|
||||
wifi->setScanActive(true);
|
||||
publish_event_simple(wifi, WifiEventTypeScanStarted);
|
||||
publish_event_simple(wifi, EventType::ScanStarted);
|
||||
}
|
||||
|
||||
static void dispatchConnect(std::shared_ptr<void> context) {
|
||||
@@ -688,12 +693,12 @@ static void dispatchConnect(std::shared_ptr<void> context) {
|
||||
TT_LOG_I(TAG, "Connecting to %s", wifi->connection_target.ssid);
|
||||
|
||||
// Stop radio first, if needed
|
||||
WifiRadioState radio_state = wifi->getRadioState();
|
||||
RadioState radio_state = wifi->getRadioState();
|
||||
if (
|
||||
radio_state == WIFI_RADIO_ON ||
|
||||
radio_state == WIFI_RADIO_CONNECTION_ACTIVE ||
|
||||
radio_state == WIFI_RADIO_CONNECTION_PENDING
|
||||
) {
|
||||
radio_state == RadioState::On ||
|
||||
radio_state == RadioState::ConnectionActive ||
|
||||
radio_state == RadioState::ConnectionPending
|
||||
) {
|
||||
TT_LOG_I(TAG, "Connecting: Stopping radio first");
|
||||
esp_err_t stop_result = esp_wifi_stop();
|
||||
wifi->setScanActive(false);
|
||||
@@ -703,9 +708,9 @@ static void dispatchConnect(std::shared_ptr<void> context) {
|
||||
}
|
||||
}
|
||||
|
||||
wifi->setRadioState(WIFI_RADIO_CONNECTION_PENDING);
|
||||
wifi->setRadioState(RadioState::ConnectionPending);
|
||||
|
||||
publish_event_simple(wifi, WifiEventTypeConnectionPending);
|
||||
publish_event_simple(wifi, EventType::ConnectionPending);
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
@@ -759,17 +764,17 @@ static void dispatchConnect(std::shared_ptr<void> context) {
|
||||
|
||||
esp_err_t set_config_result = esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
|
||||
if (set_config_result != ESP_OK) {
|
||||
wifi->setRadioState(WIFI_RADIO_ON);
|
||||
wifi->setRadioState(RadioState::On);
|
||||
TT_LOG_E(TAG, "Failed to set wifi config (%s)", esp_err_to_name(set_config_result));
|
||||
publish_event_simple(wifi, WifiEventTypeConnectionFailed);
|
||||
publish_event_simple(wifi, EventType::ConnectionFailed);
|
||||
return;
|
||||
}
|
||||
|
||||
esp_err_t wifi_start_result = esp_wifi_start();
|
||||
if (wifi_start_result != ESP_OK) {
|
||||
wifi->setRadioState(WIFI_RADIO_ON);
|
||||
wifi->setRadioState(RadioState::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);
|
||||
publish_event_simple(wifi, EventType::ConnectionFailed);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -781,8 +786,8 @@ static void dispatchConnect(std::shared_ptr<void> context) {
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
wifi->setSecureConnection(wifi_config.sta.password[0] != 0x00U);
|
||||
wifi->setRadioState(WIFI_RADIO_CONNECTION_ACTIVE);
|
||||
publish_event_simple(wifi, WifiEventTypeConnectionSuccess);
|
||||
wifi->setRadioState(RadioState::ConnectionActive);
|
||||
publish_event_simple(wifi, EventType::ConnectionSuccess);
|
||||
TT_LOG_I(TAG, "Connected to %s", wifi->connection_target.ssid);
|
||||
if (wifi->connection_target_remember) {
|
||||
if (!settings::save(&wifi->connection_target)) {
|
||||
@@ -792,12 +797,12 @@ static void dispatchConnect(std::shared_ptr<void> context) {
|
||||
}
|
||||
}
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
wifi->setRadioState(WIFI_RADIO_ON);
|
||||
publish_event_simple(wifi, WifiEventTypeConnectionFailed);
|
||||
wifi->setRadioState(RadioState::On);
|
||||
publish_event_simple(wifi, EventType::ConnectionFailed);
|
||||
TT_LOG_I(TAG, "Failed to connect to %s", wifi->connection_target.ssid);
|
||||
} else {
|
||||
wifi->setRadioState(WIFI_RADIO_ON);
|
||||
publish_event_simple(wifi, WifiEventTypeConnectionFailed);
|
||||
wifi->setRadioState(RadioState::On);
|
||||
publish_event_simple(wifi, EventType::ConnectionFailed);
|
||||
TT_LOG_E(TAG, "UNEXPECTED EVENT");
|
||||
}
|
||||
|
||||
@@ -864,23 +869,23 @@ static void dispatchDisconnectButKeepActive(std::shared_ptr<void> context) {
|
||||
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->setRadioState(WIFI_RADIO_OFF);
|
||||
wifi->setRadioState(RadioState::Off);
|
||||
TT_LOG_E(TAG, "failed to set wifi config (%s)", esp_err_to_name(set_config_result));
|
||||
publish_event_simple(wifi, WifiEventTypeRadioStateOff);
|
||||
publish_event_simple(wifi, EventType::RadioStateOff);
|
||||
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->setRadioState(WIFI_RADIO_OFF);
|
||||
wifi->setRadioState(RadioState::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);
|
||||
publish_event_simple(wifi, EventType::RadioStateOff);
|
||||
return;
|
||||
}
|
||||
|
||||
wifi->setRadioState(WIFI_RADIO_ON);
|
||||
publish_event_simple(wifi, WifiEventTypeDisconnected);
|
||||
wifi->setRadioState(RadioState::On);
|
||||
publish_event_simple(wifi, EventType::Disconnected);
|
||||
TT_LOG_I(TAG, "Disconnected");
|
||||
}
|
||||
|
||||
@@ -891,7 +896,7 @@ static bool shouldScanForAutoConnect(std::shared_ptr<Wifi> wifi) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_radio_in_scannable_state = wifi->getRadioState() == WIFI_RADIO_ON &&
|
||||
bool is_radio_in_scannable_state = wifi->getRadioState() == RadioState::On &&
|
||||
!wifi->isScanActive() &&
|
||||
!wifi->pause_auto_connect;
|
||||
|
||||
@@ -921,7 +926,7 @@ static void onStart(ServiceContext& service) {
|
||||
|
||||
service.setData(wifi_singleton);
|
||||
|
||||
wifi_singleton->autoConnectTimer = std::make_unique<Timer>(Timer::TypePeriodic, onAutoConnectTimer, wifi_singleton);
|
||||
wifi_singleton->autoConnectTimer = std::make_unique<Timer>(Timer::Type::Periodic, onAutoConnectTimer, wifi_singleton);
|
||||
// We want to try and scan more often in case of startup or scan lock failure
|
||||
wifi_singleton->autoConnectTimer->start(TT_MIN(2000, AUTO_SCAN_INTERVAL));
|
||||
|
||||
@@ -935,8 +940,8 @@ static void onStop(ServiceContext& service) {
|
||||
auto wifi = wifi_singleton;
|
||||
tt_assert(wifi != nullptr);
|
||||
|
||||
WifiRadioState state = wifi->getRadioState();
|
||||
if (state != WIFI_RADIO_OFF) {
|
||||
RadioState state = wifi->getRadioState();
|
||||
if (state != RadioState::Off) {
|
||||
dispatchDisable(wifi);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,13 +20,13 @@ struct Wifi {
|
||||
~Wifi() = default;
|
||||
|
||||
/** @brief Locking mechanism for modifying the Wifi instance */
|
||||
Mutex mutex = Mutex(Mutex::TypeRecursive);
|
||||
Mutex mutex = Mutex(Mutex::Type::Recursive);
|
||||
/** @brief The public event bus */
|
||||
std::shared_ptr<PubSub> pubsub = std::make_shared<PubSub>();
|
||||
/** @brief The internal message queue */
|
||||
bool scan_active = false;
|
||||
bool secure_connection = false;
|
||||
WifiRadioState radio_state = WIFI_RADIO_CONNECTION_ACTIVE;
|
||||
RadioState radio_state = RadioState::ConnectionActive;
|
||||
};
|
||||
|
||||
|
||||
@@ -34,8 +34,8 @@ static Wifi* wifi = nullptr;
|
||||
|
||||
// region Static
|
||||
|
||||
static void publish_event_simple(Wifi* wifi, WifiEventType type) {
|
||||
WifiEvent turning_on_event = {.type = type};
|
||||
static void publish_event_simple(Wifi* wifi, EventType type) {
|
||||
Event turning_on_event = { .type = type };
|
||||
tt_pubsub_publish(wifi->pubsub, &turning_on_event);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ std::shared_ptr<PubSub> getPubsub() {
|
||||
return wifi->pubsub;
|
||||
}
|
||||
|
||||
WifiRadioState getRadioState() {
|
||||
RadioState getRadioState() {
|
||||
return wifi->radio_state;
|
||||
}
|
||||
|
||||
@@ -80,31 +80,31 @@ void setScanRecords(uint16_t records) {
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
std::vector<WifiApRecord> getScanResults() {
|
||||
std::vector<ApRecord> getScanResults() {
|
||||
tt_check(wifi);
|
||||
|
||||
std::vector<WifiApRecord> records;
|
||||
records.push_back((WifiApRecord) {
|
||||
std::vector<ApRecord> records;
|
||||
records.push_back((ApRecord) {
|
||||
.ssid = "Home Wifi",
|
||||
.rssi = -30,
|
||||
.auth_mode = WIFI_AUTH_WPA2_PSK
|
||||
});
|
||||
records.push_back((WifiApRecord) {
|
||||
records.push_back((ApRecord) {
|
||||
.ssid = "No place like 127.0.0.1",
|
||||
.rssi = -67,
|
||||
.auth_mode = WIFI_AUTH_WPA2_PSK
|
||||
});
|
||||
records.push_back((WifiApRecord) {
|
||||
records.push_back((ApRecord) {
|
||||
.ssid = "Pretty fly for a Wi-Fi",
|
||||
.rssi = -70,
|
||||
.auth_mode = WIFI_AUTH_WPA2_PSK
|
||||
});
|
||||
records.push_back((WifiApRecord) {
|
||||
records.push_back((ApRecord) {
|
||||
.ssid = "An AP with a really, really long name",
|
||||
.rssi = -80,
|
||||
.auth_mode = WIFI_AUTH_WPA2_PSK
|
||||
});
|
||||
records.push_back((WifiApRecord) {
|
||||
records.push_back((ApRecord) {
|
||||
.ssid = "Bad Reception",
|
||||
.rssi = -90,
|
||||
.auth_mode = WIFI_AUTH_OPEN
|
||||
@@ -116,10 +116,10 @@ std::vector<WifiApRecord> getScanResults() {
|
||||
void setEnabled(bool enabled) {
|
||||
tt_assert(wifi != nullptr);
|
||||
if (enabled) {
|
||||
wifi->radio_state = WIFI_RADIO_ON;
|
||||
wifi->radio_state = RadioState::On;
|
||||
wifi->secure_connection = true;
|
||||
} else {
|
||||
wifi->radio_state = WIFI_RADIO_OFF;
|
||||
wifi->radio_state = RadioState::Off;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ bool isConnectionSecure() {
|
||||
}
|
||||
|
||||
int getRssi() {
|
||||
if (wifi->radio_state == WIFI_RADIO_CONNECTION_ACTIVE) {
|
||||
if (wifi->radio_state == RadioState::ConnectionActive) {
|
||||
return -30;
|
||||
} else {
|
||||
return 0;
|
||||
|
||||
@@ -11,9 +11,9 @@ namespace tt::service::wifi::settings {
|
||||
* This makes it easier to use the char array as a string in various places.
|
||||
*/
|
||||
struct WifiApSettings {
|
||||
char ssid[TT_WIFI_SSID_LIMIT + 1];
|
||||
char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT + 1];
|
||||
bool auto_connect;
|
||||
char ssid[TT_WIFI_SSID_LIMIT + 1] = { 0 };
|
||||
char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT + 1] = { 0 };
|
||||
bool auto_connect = true;
|
||||
};
|
||||
|
||||
bool contains(const char* ssid);
|
||||
|
||||
Reference in New Issue
Block a user