forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoOTA.cpp
113 lines (95 loc) · 2.75 KB
/
ArduinoOTA.cpp
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
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include "ArduinoOTA.h"
ArduinoOTA::ArduinoOTA(const char *mdns_host_prefix, int port, bool serial_debug)
{
_port = port;
_mdns_host = new String(mdns_host_prefix);
*_mdns_host += String(ESP.getChipId(), HEX);
_udp_ota = new WiFiUDP();
_serial_debug = serial_debug;
_start_callback = NULL;
_end_callback = NULL;
_progress_callback = NULL;
_error_callback = NULL;
}
void ArduinoOTA::onStart(OTA_CALLBACK(fn)){
_start_callback = fn;
}
void ArduinoOTA::onEnd(OTA_CALLBACK(fn)){
_end_callback = fn;
}
void ArduinoOTA::onProgress(OTA_CALLBACK_PROGRESS(fn)){
_progress_callback = fn;
}
void ArduinoOTA::onError(OTA_CALLBACK(fn)){
_error_callback = fn;
}
ArduinoOTA::~ArduinoOTA(){
delete _udp_ota;
delete _mdns_host;
}
void ArduinoOTA::setup() {
_udp_ota->begin(_port);
if (_mdns_host) {
if (_serial_debug)
Serial.printf("OTA server at: %s:%u\n",
_mdns_host->c_str(),
_port);
MDNS.begin(_mdns_host->c_str());
MDNS.addService("arduino", "tcp", _port);
}
}
void ArduinoOTA::handle() {
if (!_udp_ota->parsePacket()) return;
IPAddress remote = _udp_ota->remoteIP();
int cmd = _udp_ota->parseInt();
int port = _udp_ota->parseInt();
int size = _udp_ota->parseInt();
if (_serial_debug){
Serial.print("Update Start: ip:");
Serial.print(remote);
Serial.printf(", port:%d, size:%d\n", port, size);
}
WiFiUDP::stopAll();
if(!Update.begin(size)){
if (_serial_debug)
Serial.println("Update Begin Error");
if (_error_callback) _error_callback();
_udp_ota->begin(_port);
return;
}
if (_start_callback) _start_callback();
if (_progress_callback) _progress_callback(0, size);
WiFiClient client;
if (!client.connect(remote, port)) {
if (_serial_debug)
Serial.printf("Connect Failed\n");
_udp_ota->begin(_port);
if (_error_callback) _error_callback();
}
uint32_t written;
while(!Update.isFinished() && client.connected()){
// TODO(mangelajo): enhance the Update.write(client) to
// accept a progress callback
written = Update.write(client);
if(written > 0) client.print(written, DEC);
if(_progress_callback) _progress_callback(written, size);
}
Serial.setDebugOutput(false);
if(Update.end()){
client.println("OK");
if (_serial_debug)
Serial.printf("Update Success\nRebooting...\n");
if(_end_callback) _end_callback();
ESP.restart();
} else {
// Update failed: listen UDP again, callback and print
_udp_ota->begin(_port);
if (_error_callback) _error_callback();
Update.printError(client);
if (_serial_debug)
Update.printError(Serial);
}
}