|
| 1 | +#include <ESP8266WiFi.h> |
| 2 | +#include <ESP8266mDNS.h> |
| 3 | +#include <WiFiUdp.h> |
| 4 | +#include "ArduinoOTA.h" |
| 5 | + |
| 6 | +ArduinoOTA::ArduinoOTA(const char *mdns_host_prefix, int port, bool serial_debug) |
| 7 | +{ |
| 8 | + _port = port; |
| 9 | + _mdns_host = new String(mdns_host_prefix); |
| 10 | + *_mdns_host += String(ESP.getChipId(), HEX); |
| 11 | + _udp_ota = new WiFiUDP(); |
| 12 | + _serial_debug = serial_debug; |
| 13 | + |
| 14 | + _start_callback = NULL; |
| 15 | + _end_callback = NULL; |
| 16 | + _progress_callback = NULL; |
| 17 | + _error_callback = NULL; |
| 18 | +} |
| 19 | + |
| 20 | +void ArduinoOTA::onStart(OTA_CALLBACK(fn)){ |
| 21 | + _start_callback = fn; |
| 22 | +} |
| 23 | + |
| 24 | +void ArduinoOTA::onEnd(OTA_CALLBACK(fn)){ |
| 25 | + _end_callback = fn; |
| 26 | +} |
| 27 | + |
| 28 | +void ArduinoOTA::onProgress(OTA_CALLBACK_PROGRESS(fn)){ |
| 29 | + _progress_callback = fn; |
| 30 | +} |
| 31 | + |
| 32 | +void ArduinoOTA::onError(OTA_CALLBACK(fn)){ |
| 33 | + _error_callback = fn; |
| 34 | +} |
| 35 | + |
| 36 | +ArduinoOTA::~ArduinoOTA(){ |
| 37 | + delete _udp_ota; |
| 38 | + delete _mdns_host; |
| 39 | +} |
| 40 | + |
| 41 | +void ArduinoOTA::setup() { |
| 42 | + _udp_ota->begin(_port); |
| 43 | + if (_mdns_host) { |
| 44 | + if (_serial_debug) |
| 45 | + Serial.printf("OTA server at: %s:%u\n", |
| 46 | + _mdns_host->c_str(), |
| 47 | + _port); |
| 48 | + MDNS.begin(_mdns_host->c_str()); |
| 49 | + MDNS.addService("arduino", "tcp", _port); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +void ArduinoOTA::handle() { |
| 54 | + |
| 55 | + if (!_udp_ota->parsePacket()) return; |
| 56 | + |
| 57 | + IPAddress remote = _udp_ota->remoteIP(); |
| 58 | + int cmd = _udp_ota->parseInt(); |
| 59 | + int port = _udp_ota->parseInt(); |
| 60 | + int size = _udp_ota->parseInt(); |
| 61 | + |
| 62 | + if (_serial_debug){ |
| 63 | + Serial.print("Update Start: ip:"); |
| 64 | + Serial.print(remote); |
| 65 | + Serial.printf(", port:%d, size:%d\n", port, size); |
| 66 | + } |
| 67 | + |
| 68 | + WiFiUDP::stopAll(); |
| 69 | + |
| 70 | + if(!Update.begin(size)){ |
| 71 | + if (_serial_debug) |
| 72 | + Serial.println("Update Begin Error"); |
| 73 | + if (_error_callback) _error_callback(); |
| 74 | + _udp_ota->begin(_port); |
| 75 | + return; |
| 76 | + } |
| 77 | + if (_start_callback) _start_callback(); |
| 78 | + if (_progress_callback) _progress_callback(0, size); |
| 79 | + |
| 80 | + WiFiClient client; |
| 81 | + if (!client.connect(remote, port)) { |
| 82 | + if (_serial_debug) |
| 83 | + Serial.printf("Connect Failed\n"); |
| 84 | + _udp_ota->begin(_port); |
| 85 | + if (_error_callback) _error_callback(); |
| 86 | + } |
| 87 | + |
| 88 | + uint32_t written; |
| 89 | + while(!Update.isFinished() && client.connected()){ |
| 90 | + // TODO(mangelajo): enhance the Update.write(client) to |
| 91 | + // accept a progress callback |
| 92 | + written = Update.write(client); |
| 93 | + if(written > 0) client.print(written, DEC); |
| 94 | + if(_progress_callback) _progress_callback(written, size); |
| 95 | + } |
| 96 | + |
| 97 | + Serial.setDebugOutput(false); |
| 98 | + |
| 99 | + if(Update.end()){ |
| 100 | + client.println("OK"); |
| 101 | + if (_serial_debug) |
| 102 | + Serial.printf("Update Success\nRebooting...\n"); |
| 103 | + if(_end_callback) _end_callback(); |
| 104 | + ESP.restart(); |
| 105 | + } else { |
| 106 | + // Update failed: listen UDP again, callback and print |
| 107 | + _udp_ota->begin(_port); |
| 108 | + if (_error_callback) _error_callback(); |
| 109 | + Update.printError(client); |
| 110 | + if (_serial_debug) |
| 111 | + Update.printError(Serial); |
| 112 | + } |
| 113 | +} |
0 commit comments