Skip to content

Commit

Permalink
Allow multiple WiFi configurations for different networks (#343)
Browse files Browse the repository at this point in the history
* Use wifi multi to connect to different networks

- use multi wifi
- enhance config structure
- about page updated
- trusted networks (private) get direct http access
- delay for ntp to get proper time
- UI for config
  • Loading branch information
amandel committed Jan 5, 2024
1 parent d53fea6 commit b7e83f2
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 86 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -158,4 +158,4 @@ You can find the OpenBikeSensor documentation under:

## Supporters

[JetBrains supports the development with one license for their IDEs.](https://jb.gg/OpenSource)
[JetBrains supports the development with one license for their IDEs.](https://jb.gg/OpenSource)
17 changes: 14 additions & 3 deletions docs/software/firmware/obs_cfg.md
Expand Up @@ -72,10 +72,21 @@ relevant put the order of the array content.
"privacyConfig": 10,
// Active preset - always 0 as of today
"selectedPreset": 0,
// Password of your Wi-Fi where the OBS should log into in server mode
// Deprecated: Password of your Wi-Fi where the OBS should log into in server mode
"wifiPassword": "swordfish",
// SSID of your Wi-Fi where the OBS should log into in server mode
"wifiSsid": "obs-cloud"
// Deprecated: SSID of your Wi-Fi where the OBS should log into in server mode
"wifiSsid": "obs-cloud",
// Array with multiple Wi-Fi networks to be used in client mode
"wifiNetworks": [
{
// Password of your Wi-Fi where the OBS should log into in client mode
"wifiPassword": "swordfish",
// SSID of your Wi-Fi where the OBS should log into in client mode
"wifiSsid": "obs-cloud",
// if true obs will allow unprotected access in this network
"wifiPrivate": true
}
]
}
]
}
Expand Down
53 changes: 53 additions & 0 deletions src/config.cpp
Expand Up @@ -41,6 +41,8 @@ const String ObsConfig::PROPERTY_BLUETOOTH = String("bluetooth");
const String ObsConfig::PROPERTY_OFFSET = String("offset");
const String ObsConfig::PROPERTY_WIFI_SSID = String("wifiSsid");
const String ObsConfig::PROPERTY_WIFI_PASSWORD = String("wifiPassword");
const String ObsConfig::PROPERTY_WIFI_NETWORKS = String("wifiNetworks");
const String ObsConfig::PROPERTY_WIFI_PRIVATE = String("wifiPrivate");
const String ObsConfig::PROPERTY_PORTAL_URL = String("portalUrl");
const String ObsConfig::PROPERTY_PORTAL_TOKEN = String("portalToken");
const String ObsConfig::PROPERTY_DISPLAY_CONFIG = String("displayConfig");
Expand Down Expand Up @@ -482,3 +484,54 @@ bool ObsConfig::removeConfig() {
&& !SPIFFS.exists(CONFIG_OLD_FILENAME)
&& !SPIFFS.exists(CONFIG_FILENAME);
}

WifiConfig ObsConfig::getWifiConfig(int wifiId) const {
WifiConfig result;
auto data = getProfileConst(selectedProfile)[PROPERTY_WIFI_NETWORKS][wifiId];

// legacy data single wifi
if (wifiId == 0 && data.isNull()) {
result.ssid = getProperty<String>(PROPERTY_WIFI_SSID);
result.password = getProperty<String>(PROPERTY_WIFI_PASSWORD);
result.trusted = false;
return result;
}
result.ssid = data[PROPERTY_WIFI_SSID].as<String>();
result.password = data[PROPERTY_WIFI_PASSWORD].as<String>();
result.trusted = data[PROPERTY_WIFI_PRIVATE].as<bool>();
return result;
}

int ObsConfig::getNumberOfWifiConfigs() const {
uint result = getProfileConst(selectedProfile)[PROPERTY_WIFI_NETWORKS].size();

log_i("getNumberOfWifiConfigs: %d", result);
log_i("PROPERTY_WIFI_SSID: %s", getProperty<const char*>(PROPERTY_WIFI_SSID));
log_i("getProperty<String>(PROPERTY_WIFI_SSID): %s", getProperty<String>(PROPERTY_WIFI_SSID).c_str());

if (result == 0 && getProperty<String>(PROPERTY_WIFI_SSID).length() > 0) {
result = 1;
}
return (int) result;
}

bool ObsConfig::removeWifiConfig(int wifiId) {
getProfile(selectedProfile)[PROPERTY_WIFI_NETWORKS].remove(wifiId);
return true;
}

bool ObsConfig::addWifiConfig(const WifiConfig &wifiConfig) {
auto data = getProfile(selectedProfile)[PROPERTY_WIFI_NETWORKS].createNestedObject();
data[PROPERTY_WIFI_SSID] = wifiConfig.ssid;
data[PROPERTY_WIFI_PASSWORD] = wifiConfig.password;
data[PROPERTY_WIFI_PRIVATE] = wifiConfig.trusted;
return true;
}

bool ObsConfig::setWifiConfig(int wifiId, const WifiConfig &wifiConfig) {
auto data = getProfile(selectedProfile)[PROPERTY_WIFI_NETWORKS][wifiId];
data[PROPERTY_WIFI_SSID] = wifiConfig.ssid;
data[PROPERTY_WIFI_PASSWORD] = wifiConfig.password;
data[PROPERTY_WIFI_PRIVATE] = wifiConfig.trusted;
return true;
}
14 changes: 14 additions & 0 deletions src/config.h
Expand Up @@ -55,6 +55,12 @@ enum PrivacyOptions {
OverridePrivacy = 0x08 //8
};

struct WifiConfig {
String ssid = "";
String password = "";
bool trusted = false;
};

struct PrivacyArea {
double latitude;
double longitude;
Expand All @@ -75,6 +81,7 @@ struct Config {
int privacyConfig;
int confirmationTimeWindow;
std::vector<PrivacyArea> privacyAreas;
std::vector<WifiConfig> wifiConfigs;
};

enum DevOptions {
Expand Down Expand Up @@ -109,6 +116,11 @@ class ObsConfig {
bool removePrivacyArea(int profile, int paId);
PrivacyArea getPrivacyArea(int profile, int paId) const;
int getNumberOfPrivacyAreas(int profile) const;
WifiConfig getWifiConfig(int wifiId) const;
int getNumberOfWifiConfigs() const;
bool removeWifiConfig(int wifiId);
bool addWifiConfig(WifiConfig const &wifiConfig);
bool setWifiConfig(int wifiId, WifiConfig const &wifiConfig);

int getNumberOfProfiles() const;
int addProfile();
Expand All @@ -130,6 +142,8 @@ class ObsConfig {
static const String PROPERTY_SIM_RA;
static const String PROPERTY_WIFI_SSID;
static const String PROPERTY_WIFI_PASSWORD;
static const String PROPERTY_WIFI_NETWORKS;
static const String PROPERTY_WIFI_PRIVATE;
static const String PROPERTY_PORTAL_TOKEN;
static const String PROPERTY_PORTAL_URL;
static const String PROPERTY_DISPLAY_CONFIG;
Expand Down

0 comments on commit b7e83f2

Please sign in to comment.