You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AutoConnect/src/AutoConnectCredential.h

122 lines
3.6 KiB

7 years ago
/**
* Declaration of AutoConnectCredential class.
* @file AutoConnectCredential.h
* @author hieromon@gmail.com
* @version 1.0.0
* @date 2019-08-15
7 years ago
* @copyright MIT license.
*/
#ifndef _AUTOCONNECTCREDENTIAL_H_
#define _AUTOCONNECTCREDENTIAL_H_
7 years ago
#include <Arduino.h>
#include <memory>
7 years ago
#if defined(ARDUINO_ARCH_ESP8266)
7 years ago
extern "C" {
#include <user_interface.h>
}
#define NO_GLOBAL_EEPROM
#include <EEPROM.h>
7 years ago
#elif defined(ARDUINO_ARCH_ESP32)
#include <map>
7 years ago
#include <esp_wifi.h>
#include <Preferences.h>
7 years ago
struct station_config {
uint8_t ssid[32];
uint8_t password[64];
uint8_t bssid_set;
uint8_t bssid[6];
wifi_fast_scan_threshold_t threshold;
7 years ago
};
#endif
7 years ago
/** Credential storage area offset specifier in EEPROM.
* By defining AC_IDENTIFIER_OFFSET macro in the user sketch, the credential
* storage area can be shifted in EEPROM.
*/
#ifndef AC_IDENTIFIER_OFFSET
#define AC_IDENTIFIER_OFFSET 0
#endif
/**
*
*/
#ifndef AC_IDENTIFIER
#define AC_IDENTIFIER "AC_CREDT"
#endif
class AutoConnectCredentialBase {
public:
explicit AutoConnectCredentialBase() {}
virtual ~AutoConnectCredentialBase() {}
virtual uint8_t entries(void) { return _entries; }
virtual bool del(const char* ssid) = 0;
virtual int8_t load(const char* ssid, struct station_config* config) = 0;
virtual bool load(int8_t entry, struct station_config* config) = 0;
virtual bool save(const struct station_config* config) = 0;
protected:
uint8_t _entries; /**< Count of the available entry */
uint16_t _containSize; /**< Container size */
};
#if defined(ARDUINO_ARCH_ESP8266)
/** AutoConnectCredential class using EEPROM for ESP8266 */
class AutoConnectCredential : public AutoConnectCredentialBase {
7 years ago
public:
AutoConnectCredential();
7 years ago
explicit AutoConnectCredential(uint16_t offset);
7 years ago
~AutoConnectCredential();
bool del(const char* ssid) override;
int8_t load(const char* ssid, struct station_config* config) override;
bool load(int8_t entry, struct station_config* config) override;
bool save(const struct station_config* config) override;
7 years ago
private:
void _allocateEntry(void); /**< Initialize storage for credentials. */
7 years ago
void _retrieveEntry(char* ssid, char* password, uint8_t* bssid); /**< Read an available entry. */
7 years ago
int _dp; /**< The current address in EEPROM */
int _ep; /**< The current entry address in EEPROM */
uint16_t _offset; /**< The offset for the saved area of credentials in EEPROM. */
std::unique_ptr<EEPROMClass> _eeprom; /**< shared EEPROM class */
7 years ago
};
#elif defined(ARDUINO_ARCH_ESP32)
#define AC_CREDENTIAL_NVSNAME AC_IDENTIFIER
#define AC_CREDENTIAL_NVSKEY AC_CREDENTIAL_NVSNAME
/** AutoConnectCredential class using Preferences for ESP32 */
class AutoConnectCredential : public AutoConnectCredentialBase {
public:
AutoConnectCredential();
explicit AutoConnectCredential(uint16_t offset);
~AutoConnectCredential();
bool del(const char* ssid) override;
int8_t load(const char* ssid, struct station_config* config) override;
bool load(int8_t entry, struct station_config* config) override;
bool save(const struct station_config* config) override;
private:
typedef station_config station_config_t;
typedef struct {
String password;
uint8_t bssid[6];
} AC_CREDTBODY_t;
bool _add(const station_config_t* config);
size_t _commit(void);
uint8_t _import(void);
void _obtain(std::map<String, AC_CREDTBODY_t>::iterator const& it, station_config_t* config);
std::unique_ptr<Preferences> _pref;
std::map<String, AC_CREDTBODY_t> _credit;
};
#endif
#endif // _AUTOCONNECTCREDENTIAL_H_