234 lines
4.9 KiB
C++
234 lines
4.9 KiB
C++
#include "ota.h"
|
|
|
|
OtaProcess_class ota(100);
|
|
|
|
bool OtaProcess_class::initialize(void)
|
|
{
|
|
if (m_newState)
|
|
{
|
|
log_i("Otastate = initialize");
|
|
m_newState = false;
|
|
m_otaState = otaScan;
|
|
}
|
|
|
|
switch (m_otaState)
|
|
{
|
|
|
|
case otaScan:
|
|
{
|
|
log_i("Otastate = initialize(scan)");
|
|
|
|
int n = WiFi.scanNetworks();
|
|
if (n == 0)
|
|
{
|
|
log_e("no networks found");
|
|
m_otaState = otaError;
|
|
}
|
|
else
|
|
{
|
|
log_i(" %d wifi networks found", n);
|
|
String tmppsk = "";
|
|
for (int i = 0; i < n; ++i)
|
|
{
|
|
tmppsk = GetWifiPassword(WiFi.SSID(i));
|
|
if(tmppsk != "" && m_ssid == "")
|
|
{
|
|
m_ssid = WiFi.SSID(i);
|
|
m_psk = tmppsk;
|
|
}
|
|
else{
|
|
log_w("using fallback SSID %s", SECRET_SSID);
|
|
m_ssid = SECRET_SSID;
|
|
m_psk = SECRET_PASS;
|
|
}
|
|
log_i("[%d] %s %s (%d) [%s]", i, WiFi.SSID(i), (WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*", WiFi.RSSI(i), (tmppsk != "")? "OK": "Not congigured");
|
|
}
|
|
}
|
|
if(m_ssid != "")
|
|
{
|
|
m_otaState = otaInit;
|
|
log_i("Otastate = initialize(scan): done");
|
|
}
|
|
else
|
|
{
|
|
m_otaState = otaError;
|
|
log_e("Otastate = initialize(scan): NOT CONFIGURED");
|
|
}
|
|
|
|
}
|
|
break;
|
|
|
|
case otaInit:
|
|
{
|
|
log_i("Otastate = initialize(init)");
|
|
WiFi.begin(m_ssid.c_str(), m_psk.c_str());
|
|
m_otaState = otaConnect;
|
|
log_i("Otastate = initialize(init):done");
|
|
}
|
|
break;
|
|
|
|
case otaConnect:
|
|
{
|
|
uint32_t timeTemp = millis();
|
|
if (timeTemp - m_lastconnectTime > WIFICONNECTINTERVAL)
|
|
{
|
|
log_i("Otastate = initialize(connect)");
|
|
if (WiFi.status() != WL_CONNECTED)
|
|
{
|
|
log_e("Connection Failed! Retry...");
|
|
}
|
|
else
|
|
{
|
|
m_otaState = otaSetup;
|
|
}
|
|
m_lastconnectTime = timeTemp;
|
|
}
|
|
}
|
|
break;
|
|
|
|
case otaSetup:
|
|
{
|
|
log_i("Otastate = initialize(setup)");
|
|
// Port defaults to 3232
|
|
// ArduinoOTA.setPort(3232);
|
|
// Hostname defaults to esp3232-[MAC]
|
|
ArduinoOTA.setHostname("muziekdoos");
|
|
// No authentication by default
|
|
// ArduinoOTA.setPassword("admin");
|
|
// Password can be set with it's md5 value as well
|
|
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
|
|
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
|
|
ArduinoOTA
|
|
.onStart([]()
|
|
{
|
|
String type;
|
|
ota.m_otaState = otaStart;
|
|
if (ArduinoOTA.getCommand() == U_FLASH)
|
|
{
|
|
type = "sketch";
|
|
}
|
|
else // U_SPIFFS
|
|
{
|
|
type = "filesystem";
|
|
LittleFS.end();
|
|
}
|
|
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
|
Serial.println("Start updating " + type); })
|
|
.onEnd([]()
|
|
{ log_i("End"); ota.m_otaState = otaDone; })
|
|
.onProgress([](unsigned int progress, unsigned int total)
|
|
{ log_i("Progress: %u%%\r", (progress / (total / 100))); ota.m_otaState = otaBusy; PowerKeepAlive();})
|
|
.onError([](ota_error_t error)
|
|
{
|
|
log_e("Error[%u]: ", error);
|
|
ota.m_otaState = otaError;
|
|
if (error == OTA_AUTH_ERROR) log_e("Auth Failed");
|
|
else if (error == OTA_BEGIN_ERROR) log_e("Begin Failed");
|
|
else if (error == OTA_CONNECT_ERROR) log_e("Connect Failed");
|
|
else if (error == OTA_RECEIVE_ERROR) log_e("Receive Failed");
|
|
else if (error == OTA_END_ERROR) log_e("End Failed"); });
|
|
|
|
m_otaState = otaStart;
|
|
}
|
|
break;
|
|
case otaStart:
|
|
{
|
|
log_i("Otastate = initialize(start)");
|
|
ArduinoOTA.begin();
|
|
log_i("Ota ready, IPaddress:%s", WiFi.localIP().toString());
|
|
m_otaState = otaInitDone;
|
|
setLedBlink(true);
|
|
}
|
|
break;
|
|
case otaInitDone:
|
|
{
|
|
setProcessState(processIdle);
|
|
return true;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void OtaProcess_class::idle(void)
|
|
{
|
|
if (m_newState)
|
|
{
|
|
log_i("Otastate = Idle");
|
|
m_newState = false;
|
|
}
|
|
if (m_otaState == otaInitDone)
|
|
{
|
|
ArduinoOTA.handle();
|
|
}
|
|
}
|
|
|
|
void OtaProcess_class::active(void)
|
|
{
|
|
if (m_newState)
|
|
{
|
|
log_i("Otastate = active(=>idle)");
|
|
m_newState = false;
|
|
setProcessState(processIdle);
|
|
}
|
|
idle();
|
|
}
|
|
|
|
void OtaProcess_class::disabled(void)
|
|
{
|
|
if (m_newState)
|
|
{
|
|
log_i("Otastate = disabled");
|
|
m_newState = false;
|
|
}
|
|
}
|
|
|
|
void OtaProcess_class::halted(void)
|
|
{
|
|
if (m_newState)
|
|
{
|
|
log_i("Otastate = halted(=>disabled)");
|
|
m_newState = false;
|
|
}
|
|
disabled();
|
|
}
|
|
|
|
void OtaProcess_class::stopped(void)
|
|
{
|
|
if (m_newState)
|
|
{
|
|
log_i("Otastate = stopped(=>disabled)");
|
|
m_newState = false;
|
|
}
|
|
|
|
if (WiFi.getMode() != WIFI_MODE_NULL)
|
|
{
|
|
WiFi.mode(WIFI_MODE_NULL);
|
|
}
|
|
}
|
|
|
|
void otaEnable(void)
|
|
{
|
|
ota.setProcessState(PROCESS_STATE::processInit);
|
|
}
|
|
|
|
void otaDisable(void)
|
|
{
|
|
ota.setProcessState(PROCESS_STATE::processDisabled);
|
|
}
|
|
|
|
void initOta(void)
|
|
{
|
|
/* noting */
|
|
}
|
|
|
|
OTASTATES getOtaState(void)
|
|
{
|
|
return ota.m_otaState;
|
|
}
|
|
|
|
void handleOta(void)
|
|
{
|
|
ota.run();
|
|
} |