swimtracker-firmware/firmware/lib/wifimanager/WifiManager.h

54 lines
1.4 KiB
C
Raw Permalink Normal View History

#pragma once
2023-08-28 15:06:29 +02:00
#include "PreferencesAbstraction.h"
#include <WiFi.h>
/**
* Manages connection and provisioning of WiFI
*
* Starts in state AP_PROVISIONING, where device is an access point without password.
* Device should not work normally in this mode, only API is served to set password.
* Two options:
* - connect to existing WiFi (station mode), via setStaCredentials
* - create secured access point, via setApCredentials
*
* When operating in access point mode, the device IP is 192.168.42.1
*
2021-06-06 15:56:30 +02:00
* call iteration() regularly, it has a wifiWatchdog to reconnect in station mode if connection was lost
*/
class WifiManager
{
public:
enum State
{
INVALID,
STA,
AP_PROVISIONING,
AP_SECURE,
};
WifiManager() : state_(INVALID) {}
2021-07-22 18:38:28 +02:00
void begin(const String &hostname, const String & wifiStaModeName);
void setStaCredentials(const char *wifiName, const char *password);
void setApCredentials(const char *password);
void resetToApProvisioning();
2021-06-06 15:56:30 +02:00
void iteration();
bool inProvisioningMode() const { return state_ == INVALID || state_ == AP_PROVISIONING; }
State state() const { return state_; }
const char *stateStr() const { return stateToString(state_); }
2021-07-26 14:35:25 +02:00
const String & hostname() const { return hostname_; }
static const char *stateToString(State state);
private:
void startWifi();
Preferences prefs_;
State state_;
String hostname_;
2021-07-22 18:38:28 +02:00
String wifiStaModeName_;
};