-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathota.h
182 lines (148 loc) · 4.9 KB
/
ota.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// ota.h
#pragma once
// #if defined(__ESP__)
#include <functional>
#include "ustd_platform.h"
#include "ustd_array.h"
#include "ustd_map.h"
#include "scheduler.h"
#include "filesystem.h"
#include <ArduinoOTA.h>
#include <Arduino_JSON.h>
namespace ustd {
/*! \brief munet OTA Class
The OTA object listens for network connections and automatically establishes OTA update funcionality
on successful connection to WiFi. In case of software update, all other muwerk tasks are
automatically halted, and software update is granted priority.
Network failures are handled automatically.
## Sample OTA Integration
\code{cpp}
#define __ESP__ 1 // Platform defines required, see doc, mainpage.
#include "scheduler.h"
#include "net.h"
#include "ota.h"
ustd::Scheduler sched;
ustd::Net net();
ustd::Ota ota;
void setup() {
net.begin(&sched);
ota.begin(&sched);
}
\endcode
Security note: currently the API doesn't support setting an OTA password. Please use:
\code{cpp}
ArduinoOTA.setPassword("secret");
// or:
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
\endcode
to set an OTA password. This will be supported within the API in a future version.
*/
class Ota {
private:
// muwerk task management
Scheduler *pSched;
int tID;
// runtime control - state management
bool bNetUp = false;
bool bCheckOTA = false;
bool bOTAUpdateActive = false;
public:
Ota() {
//! Instantiate a over-the-air (OTA) software update object.
}
~Ota() {
}
void begin(Scheduler *_pSched) {
/*! Setup OTA over-the-air software update.
*
* This activates the OTA interface. As soon as a network connection is available, listening
* for OTA requests are started. Handling of network connections and disconnnects is done
* automatically and does not require further interaction.
*
* @param _pSched Pointer to the muwerk scheduler.
*/
// init scheduler
pSched = _pSched;
tID = pSched->add([this]() { this->loop(); }, "ota", 25000L); // check for ota every 25ms
// subscribe to all messages
pSched->subscribe(tID, "#", [this](String topic, String msg, String originator) {
this->subsMsg(topic, msg, originator);
});
pSched->publish("net/network/get");
}
private:
void loop() {
if (bCheckOTA) {
ArduinoOTA.handle();
}
}
void subsMsg(String topic, String msg, String originator) {
JSONVar mqttJsonMsg = JSON.parse(msg);
if (JSON.typeof(mqttJsonMsg) == "undefined") {
return;
}
if (topic == "net/network") {
String state = (const char *)mqttJsonMsg["state"]; // root["state"];
if (state == "connected") {
if (!bNetUp) {
bNetUp = true;
OTAsetup();
bCheckOTA = true;
}
} else {
bNetUp = false;
bCheckOTA = false;
}
}
}
void OTAsetup() {
#if defined(__ESP32__) || defined(__ESP32_RISC__)
ArduinoOTA.setHostname(WiFi.getHostname());
#else
ArduinoOTA.setHostname(WiFi.hostname().c_str());
#endif
// TODO: No authentication by default
// ArduinoOTA.setPassword("secret");
// Password can be set with it's md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
ArduinoOTA.onStart([&]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
DBG("Start updating " + type);
bOTAUpdateActive = true;
pSched->singleTaskMode(tID);
fsEnd();
});
ArduinoOTA.onEnd([&]() {
DBG("\nEnd of update");
pSched->singleTaskMode(-1);
bOTAUpdateActive = false;
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
DBGF("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([&](ota_error_t error) {
#ifdef USE_SERIAL_DBG
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR)
Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR)
Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR)
Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR)
Serial.println("Receive Failed");
else if (error == OTA_END_ERROR)
Serial.println("End Failed");
#endif
});
ArduinoOTA.begin();
}
};
} // namespace ustd
// #endif // defined(__ESP__)