/
ElegantOTA.h
140 lines (121 loc) · 3.45 KB
/
ElegantOTA.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
/**
_____ _ _ ___ _____ _
| ____| | ___ __ _ __ _ _ __ | |_ / _ \_ _|/ \
| _| | |/ _ \/ _` |/ _` | '_ \| __| | | || | / _ \
| |___| | __/ (_| | (_| | | | | |_| |_| || |/ ___ \
|_____|_|\___|\__, |\__,_|_| |_|\__|\___/ |_/_/ \_\
|___/
*/
/**
*
* @name ElegantOTA
* @author Ayush Sharma (ayush@softt.io)
* @brief
* @version 3.0.0
* @date 2023-08-30
*/
#ifndef ElegantOTA_h
#define ElegantOTA_h
#include "Arduino.h"
#include "stdlib_noniso.h"
#include "elop.h"
#ifndef ELEGANTOTA_USE_ASYNC_WEBSERVER
#define ELEGANTOTA_USE_ASYNC_WEBSERVER 0
#endif
#ifndef ELEGANTOTA_DEBUG
#define ELEGANTOTA_DEBUG 0
#endif
#ifndef UPDATE_DEBUG
#define UPDATE_DEBUG 0
#endif
#if ELEGANTOTA_DEBUG
#define ELEGANTOTA_DEBUG_MSG(x) Serial.printf("%s %s", "[ElegantOTA] ", x)
#else
#define ELEGANTOTA_DEBUG_MSG(x)
#endif
#if defined(ESP8266)
#include <functional>
#include "FS.h"
#include "LittleFS.h"
#include "Updater.h"
#include "StreamString.h"
#if ELEGANTOTA_USE_ASYNC_WEBSERVER == 1
#include "ESPAsyncTCP.h"
#include "ESPAsyncWebServer.h"
#define ELEGANTOTA_WEBSERVER AsyncWebServer
#else
#include "ESP8266WiFi.h"
#include "WiFiClient.h"
#include "ESP8266WebServer.h"
#define ELEGANTOTA_WEBSERVER ESP8266WebServer
#endif
#define HARDWARE "ESP8266"
#elif defined(ESP32)
#include <functional>
#include "FS.h"
#include "Update.h"
#include "StreamString.h"
#if ELEGANTOTA_USE_ASYNC_WEBSERVER == 1
#include "AsyncTCP.h"
#include "ESPAsyncWebServer.h"
#define ELEGANTOTA_WEBSERVER AsyncWebServer
#else
#include "WiFi.h"
#include "WiFiClient.h"
#include "WebServer.h"
#define ELEGANTOTA_WEBSERVER WebServer
#endif
#define HARDWARE "ESP32"
#elif defined(TARGET_RP2040)
#include <functional>
#include "Arduino.h"
#include "FS.h"
#include "LittleFS.h"
#include "StreamString.h"
#include "Updater.h"
#if ELEGANTOTA_USE_ASYNC_WEBSERVER == 1
#include "AsyncTCP_RP2040W.h"
#include "ESPAsyncWebServer.h"
#define ELEGANTOTA_WEBSERVER AsyncWebServer
#else
#include "WiFiClient.h"
#include "WiFiServer.h"
#include "WebServer.h"
#include "WiFiUdp.h"
#define ELEGANTOTA_WEBSERVER WebServer
#endif
#define HARDWARE "RP2040"
extern uint8_t _FS_start;
extern uint8_t _FS_end;
#endif
enum OTA_Mode {
OTA_MODE_FIRMWARE = 0,
OTA_MODE_FILESYSTEM = 1
};
class ElegantOTAClass{
public:
ElegantOTAClass();
void begin(ELEGANTOTA_WEBSERVER *server, const char * username = "", const char * password = "");
void setAuth(const char * username, const char * password);
void clearAuth();
void setAutoReboot(bool enable);
void loop();
void onStart(std::function<void()> callable);
void onProgress(std::function<void(size_t current, size_t final)> callable);
void onEnd(std::function<void(bool success)> callable);
private:
ELEGANTOTA_WEBSERVER *_server;
bool _authenticate;
char _username[64];
char _password[64];
bool _auto_reboot = true;
bool _reboot = false;
unsigned long _reboot_request_millis = 0;
String _update_error_str = "";
unsigned long _current_progress_size;
std::function<void()> preUpdateCallback = NULL;
std::function<void(size_t current, size_t final)> progressUpdateCallback = NULL;
std::function<void(bool success)> postUpdateCallback = NULL;
};
extern ElegantOTAClass ElegantOTA;
#endif