-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEOTAUpdate.h
More file actions
80 lines (73 loc) · 2.98 KB
/
Copy pathEOTAUpdate.h
File metadata and controls
80 lines (73 loc) · 2.98 KB
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
#ifndef UPDATE_CHECKER
#define UPDATE_CHECKER
#include <Arduino.h>
/**
* @brief Utility class to automatically fetch and install firmware updates on ESP32
*
* EOTAUpdate can periodically check for updates on a remote server, fetch and
* flash new firmware.
*
* If the URL passed in the constructor begins with https, no non-secure connections will be made
* to avoid misconfigurations or potential attack vectors.
*
* Setup:
* - Create a publicly accessible configuration file containing four lines (no spaces):
* https://myserver/ota/lastBuild.bin <- Full URL of firmware to install
* 3 <- Integer version of the new firmware
* a2fbf5835de079474e820ceda2ca9986 <- [optional] MD5 of the binary
* 1.3 <- [optional] Version string, useful for logging
*
* Let's assume it is publicly accessible at https://myserver/ota/cfg.txt
*
* - Add these lines to your code:
* #include <EOTAUpdate.h>
* const unsigned VERSION_NUMBER = 1;
* const String UPDATE_URL = "https://myserver/ota/cfg.txt";
* EOTAUpdate updater(UPDATE_URL, VERSION_NUMBER);
* Then, in the loop() function add
* updater.CheckAndUpdate();
* EOTAUpdate will search for a firmware update every minute by default.
*
* Limitations:
* - As of now, self-signed certificates will not work over SSL
*
*/
class EOTAUpdate
{
private:
static const unsigned long UPDATE_INTERVAL_MS = 60 * 60 * 1000; // 1 hour
public:
/**
* @brief Create an EOTAUpdate instance
* @param url URL pointing to the OTA configuration text file
* @param currentVersion Current running software version
* @param updateIntervalMs Min amount of time to between update checks in Ms
*/
EOTAUpdate(
const String &url,
const unsigned currentVersion,
const unsigned long updateIntervalMs = UPDATE_INTERVAL_MS);
/**
* @brief Check for an update and, if available, fetch it and flash it.
*
* @param force By default update check frequency is limited by the constructor
* param updateIntervalMs. If set to True, the last update time will
* be ignored.
* @return true An update has been found and flashed. Technically this should never
* be returned as the Microcontroller will be rebooted after a
* successfull update.
* @return false Either no update was available or flashing failed
*/
bool CheckAndUpdate(bool force = false);
private:
bool GetUpdateFWURL(String &binURL, String &binMD5);
bool GetUpdateFWURL(String &binURL, String &binMD5, const String &url, const uint16_t retries = 5);
bool PerformOTA(String &binURL, String &binMD5);
private:
String _url;
const bool _forceSSL;
const unsigned _currentVersion;
const unsigned long _updateIntervalMs;
unsigned long _lastUpdateMs;
};
#endif // UPDATE_CHECKER