Skip to content

Commit

Permalink
Fix ConfigManager Bugs (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored Dec 12, 2019
1 parent c3b1126 commit e6b5761
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.pio
.pioenvs
.pio

Expand Down
25 changes: 8 additions & 17 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
# Continuous Integration (CI) is the practice, in software
# engineering, of merging all developer working copies with a shared mainline
# several times a day < https://docs.platformio.org/page/ci/index.html >
#
# Documentation:
#
# * Travis CI Embedded Builds with PlatformIO
# < https://docs.travis-ci.com/user/integration/platformio/ >
#
# * PlatformIO integration with Travis CI
# < https://docs.platformio.org/page/ci/travis.html >
#
# * User Guide for `platformio ci` command
# < https://docs.platformio.org/page/userguide/cmd_ci.html >
#
sudo: false

language: python
python:
- "3.7.5"
- "2.7"

sudo: false
cache:
directories:
- "~/.platformio"
Expand All @@ -28,3 +14,8 @@ install:

script:
- platformio ci --lib="./src" -c platformio.ini ./examples/simple/

notifications:
email:
on_success: never
on_failure: always
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
![Logo](http://svg.wiersma.co.za/github/project?lang=cpp&title=ConfigManager&tag=wifi%20configuration%20manager)

[![Build Status](https://travis-ci.com/nrwiersma/ConfigManager.svg?branch=master)](https://travis-ci.com/nrwiersma/ConfigManager)
[![arduino-library-badge](http://www.ardu-badge.com/badge/ConfigManager.svg)](http://www.ardu-badge.com/ConfigManager)

Wifi connection and configuration manager for ESP8266 and ESP32.
Expand Down Expand Up @@ -109,7 +110,7 @@ on the most common IDEs:
### Enabling
By default, ConfigManager runs in `DEBUG_MODE` off. This is to allow the serial iterface to communicate as needed.
By default, ConfigManager runs in `DEBUG_MODE` off. This is to allow the serial iterface to communicate as needed.
To turn on debugging, add the following line inside your `setup` routine:
```
Expand Down Expand Up @@ -295,7 +296,7 @@ ssid=access point&password=some password
```json
{
"ssid": "access point name",
"strength": *int*,
"strength": *int*,
"security": *bool*
}
```
Expand Down
24 changes: 12 additions & 12 deletions src/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const byte DNS_PORT = 53;
const char magicBytes[MAGIC_LENGTH] = {'C', 'M'};
const char magicBytesEmpty[MAGIC_LENGTH] = {NULL, NULL};
const char magicBytesEmpty[MAGIC_LENGTH] = {'\0', '\0'};

const char mimeHTML[] PROGMEM = "text/html";
const char mimeJSON[] PROGMEM = "application/json";
Expand Down Expand Up @@ -128,7 +128,7 @@ void ConfigManager::handleAPPost() {
storeWifiSettings(ssid, password, false);

server->send(204, FPSTR(mimePlain), F("Saved. Will attempt to reboot."));

ESP.restart();
}

Expand All @@ -148,7 +148,7 @@ void ConfigManager::handleScanGet() {
for (int i = 0; i < n; ++i) {
String ssid = WiFi.SSID(i);
int rssi = WiFi.RSSI(i);
String security = WiFi.encryptionType(i) == ENC_TYPE_NONE ? "none" : "enabled";
String security = WiFi.encryptionType(i) == WIFI_OPEN ? "none" : "enabled";

DebugPrint("Name: ");
DebugPrint(ssid);
Expand Down Expand Up @@ -215,12 +215,12 @@ void ConfigManager::handleRESTPut() {
void ConfigManager::handleNotFound() {
String URI = toStringIP(server->client().localIP()) + String(":") + String(webPort);
String header = server->hostHeader();

if ( !isIp(header) && header != URI) {
DebugPrint(F("Unknown URL: "));
DebugPrintln(header);
server->sendHeader("Location", String("http://") + URI, true);
server->send(302, FPSTR(mimePlain), "");
server->send(302, FPSTR(mimePlain), "");
// Empty content inhibits Content-length header so we have to close the socket ourselves.
server->client().stop();
return;
Expand Down Expand Up @@ -298,13 +298,13 @@ void ConfigManager::startAP() {

WiFi.mode(WIFI_AP);
WiFi.softAP(apName, apPassword);

delay(500); // Need to wait to get IP

IPAddress ip(192, 168, 1, 1);
IPAddress NMask(255, 255, 255, 0);
WiFi.softAPConfig(ip, ip, NMask);

DebugPrint("AP Name: ");
DebugPrintln(apName);

Expand Down Expand Up @@ -348,7 +348,7 @@ void ConfigManager::createBaseWebServer() {
server.reset(new WebServer(this->webPort));
DebugPrint(F("Webserver enabled on port: "));
DebugPrintln(webPort);

server->collectHeaders(headerKeys, headerKeysSize);

server->on("/", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleAPGet, this));
Expand All @@ -361,8 +361,8 @@ void ConfigManager::createBaseWebServer() {
void ConfigManager::clearWifiSettings(bool reboot) {
char ssid[SSID_LENGTH];
char password[PASSWORD_LENGTH];
memset(ssid, NULL, SSID_LENGTH);
memset(password, NULL, PASSWORD_LENGTH);
memset(ssid, 0, SSID_LENGTH);
memset(password, 0, PASSWORD_LENGTH);

DebugPrintln(F("Clearing WiFi connection."));
storeWifiSettings(ssid, password, true);
Expand Down Expand Up @@ -429,7 +429,7 @@ void ConfigManager::writeConfig() {
}

boolean ConfigManager::isIp(String str) {
for (int i = 0; i < str.length(); i++) {
for (uint i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (c != '.' && (c < '0' || c > '9')) {
return false;
Expand Down
12 changes: 9 additions & 3 deletions src/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
#include <list>
#include "ArduinoJson.h"

#if defined(ARDUINO_ARCH_ESP8266) //ESP8266
#define WIFI_OPEN ENC_TYPE_NONE
#elif defined(ARDUINO_ARCH_ESP32) //ESP32
#define WIFI_OPEN WIFI_AUTH_OPEN
#endif

#define MAGIC_LENGTH 2
#define SSID_LENGTH 32
#define PASSWORD_LENGTH 64
Expand Down Expand Up @@ -111,7 +117,7 @@ class ConfigStringParameter : public BaseParameter {
if (json->containsKey(name) && json->is<char *>(name)) {
const char * value = json->get<const char *>(name);

memset(ptr, NULL, length);
memset(ptr, 0, length);
strncpy(ptr, const_cast<char*>(value), length - 1);
}
}
Expand All @@ -123,9 +129,9 @@ class ConfigStringParameter : public BaseParameter {
void clearData() {
DebugPrint("Clearing: ");
DebugPrintln(name);
memset(ptr, NULL, length);
memset(ptr, 0, length);
}


private:
const char *name;
Expand Down

0 comments on commit e6b5761

Please sign in to comment.