-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
Copy pathArduinoOTA.h
129 lines (100 loc) · 3.53 KB
/
ArduinoOTA.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
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ARDUINO_OTA_H
#define __ARDUINO_OTA_H
#include "Network.h"
#include "Update.h"
#include <functional>
#define INT_BUFFER_SIZE 16
typedef enum {
OTA_IDLE,
OTA_WAITAUTH,
OTA_RUNUPDATE
} ota_state_t;
typedef enum {
OTA_AUTH_ERROR,
OTA_BEGIN_ERROR,
OTA_CONNECT_ERROR,
OTA_RECEIVE_ERROR,
OTA_END_ERROR
} ota_error_t;
class ArduinoOTAClass {
public:
typedef std::function<void(void)> THandlerFunction;
typedef std::function<void(ota_error_t)> THandlerFunction_Error;
typedef std::function<void(unsigned int, unsigned int)> THandlerFunction_Progress;
ArduinoOTAClass();
~ArduinoOTAClass();
//Sets the service port. Default 3232
ArduinoOTAClass &setPort(uint16_t port);
//Sets the device hostname. Default esp32-xxxxxx
ArduinoOTAClass &setHostname(const char *hostname);
String getHostname();
//Sets the password that will be required for OTA. Default NULL
ArduinoOTAClass &setPassword(const char *password);
//Sets the password as above but in the form MD5(password). Default NULL
ArduinoOTAClass &setPasswordHash(const char *password);
//Sets the partition label to write to when updating SPIFFS. Default NULL
ArduinoOTAClass &setPartitionLabel(const char *partition_label);
String getPartitionLabel();
//Sets if the device should be rebooted after successful update. Default true
ArduinoOTAClass &setRebootOnSuccess(bool reboot);
//Sets if the device should advertise itself to Arduino IDE. Default true
ArduinoOTAClass &setMdnsEnabled(bool enabled);
//This callback will be called when OTA connection has begun
ArduinoOTAClass &onStart(THandlerFunction fn);
//This callback will be called when OTA has finished
ArduinoOTAClass &onEnd(THandlerFunction fn);
//This callback will be called when OTA encountered Error
ArduinoOTAClass &onError(THandlerFunction_Error fn);
//This callback will be called when OTA is receiving data
ArduinoOTAClass &onProgress(THandlerFunction_Progress fn);
//Starts the ArduinoOTA service
void begin();
//Ends the ArduinoOTA service
void end();
//Call this in loop() to run the service
void handle();
//Gets update command type after OTA has started. Either U_FLASH or U_SPIFFS
int getCommand();
void setTimeout(int timeoutInMillis);
private:
int _port;
String _password;
String _hostname;
String _partition_label;
String _nonce;
NetworkUDP _udp_ota;
bool _initialized;
bool _rebootOnSuccess;
bool _mdnsEnabled;
ota_state_t _state;
int _size;
int _cmd;
int _ota_port;
int _ota_timeout;
IPAddress _ota_ip;
String _md5;
THandlerFunction _start_callback;
THandlerFunction _end_callback;
THandlerFunction_Error _error_callback;
THandlerFunction_Progress _progress_callback;
void _runUpdate(void);
void _onRx(void);
int parseInt(void);
String readStringUntil(char end);
};
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_ARDUINOOTA)
extern ArduinoOTAClass ArduinoOTA;
#endif
#endif /* __ARDUINO_OTA_H */