2021-02-18 11:14:35 +01:00
|
|
|
// Copyright 2015-2021 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.
|
|
|
|
|
2017-03-11 07:15:44 +01:00
|
|
|
#ifndef __ARDUINO_OTA_H
|
|
|
|
#define __ARDUINO_OTA_H
|
|
|
|
|
|
|
|
#include <WiFi.h>
|
|
|
|
#include <functional>
|
|
|
|
#include "Update.h"
|
|
|
|
|
2017-10-30 10:44:15 +01:00
|
|
|
#define INT_BUFFER_SIZE 16
|
|
|
|
|
2017-03-11 07:15:44 +01:00
|
|
|
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:
|
2018-07-24 19:40:18 +02:00
|
|
|
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;
|
2017-03-11 07:15:44 +01:00
|
|
|
|
|
|
|
ArduinoOTAClass();
|
|
|
|
~ArduinoOTAClass();
|
|
|
|
|
2017-10-13 10:46:38 +02:00
|
|
|
//Sets the service port. Default 3232
|
2017-11-06 14:25:11 +01:00
|
|
|
ArduinoOTAClass& setPort(uint16_t port);
|
2017-03-11 07:15:44 +01:00
|
|
|
|
2017-10-13 10:46:38 +02:00
|
|
|
//Sets the device hostname. Default esp32-xxxxxx
|
2017-11-06 14:25:11 +01:00
|
|
|
ArduinoOTAClass& setHostname(const char *hostname);
|
2017-03-11 07:15:44 +01:00
|
|
|
String getHostname();
|
|
|
|
|
|
|
|
//Sets the password that will be required for OTA. Default NULL
|
2017-11-06 14:25:11 +01:00
|
|
|
ArduinoOTAClass& setPassword(const char *password);
|
2017-03-11 07:15:44 +01:00
|
|
|
|
|
|
|
//Sets the password as above but in the form MD5(password). Default NULL
|
2017-11-06 14:25:11 +01:00
|
|
|
ArduinoOTAClass& setPasswordHash(const char *password);
|
2017-03-11 07:15:44 +01:00
|
|
|
|
2020-11-02 17:49:24 +01:00
|
|
|
//Sets the partition label to write to when updating SPIFFS. Default NULL
|
|
|
|
ArduinoOTAClass &setPartitionLabel(const char *partition_label);
|
|
|
|
String getPartitionLabel();
|
|
|
|
|
2017-03-11 07:15:44 +01:00
|
|
|
//Sets if the device should be rebooted after successful update. Default true
|
2017-11-06 14:25:11 +01:00
|
|
|
ArduinoOTAClass& setRebootOnSuccess(bool reboot);
|
2017-03-11 07:15:44 +01:00
|
|
|
|
|
|
|
//Sets if the device should advertise itself to Arduino IDE. Default true
|
2017-11-06 14:25:11 +01:00
|
|
|
ArduinoOTAClass& setMdnsEnabled(bool enabled);
|
2017-03-11 07:15:44 +01:00
|
|
|
|
|
|
|
//This callback will be called when OTA connection has begun
|
2017-11-06 14:25:11 +01:00
|
|
|
ArduinoOTAClass& onStart(THandlerFunction fn);
|
2017-03-11 07:15:44 +01:00
|
|
|
|
|
|
|
//This callback will be called when OTA has finished
|
2017-11-06 14:25:11 +01:00
|
|
|
ArduinoOTAClass& onEnd(THandlerFunction fn);
|
2017-03-11 07:15:44 +01:00
|
|
|
|
|
|
|
//This callback will be called when OTA encountered Error
|
2017-11-06 14:25:11 +01:00
|
|
|
ArduinoOTAClass& onError(THandlerFunction_Error fn);
|
2017-03-11 07:15:44 +01:00
|
|
|
|
|
|
|
//This callback will be called when OTA is receiving data
|
2017-11-06 14:25:11 +01:00
|
|
|
ArduinoOTAClass& onProgress(THandlerFunction_Progress fn);
|
2017-03-11 07:15:44 +01:00
|
|
|
|
|
|
|
//Starts the ArduinoOTA service
|
|
|
|
void begin();
|
|
|
|
|
2017-10-30 10:44:15 +01:00
|
|
|
//Ends the ArduinoOTA service
|
|
|
|
void end();
|
|
|
|
|
2017-03-11 07:15:44 +01:00
|
|
|
//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();
|
|
|
|
|
2018-07-24 19:40:18 +02:00
|
|
|
void setTimeout(int timeoutInMillis);
|
|
|
|
|
2017-03-11 07:15:44 +01:00
|
|
|
private:
|
|
|
|
int _port;
|
|
|
|
String _password;
|
|
|
|
String _hostname;
|
2020-11-02 17:49:24 +01:00
|
|
|
String _partition_label;
|
2017-03-11 07:15:44 +01:00
|
|
|
String _nonce;
|
|
|
|
WiFiUDP _udp_ota;
|
|
|
|
bool _initialized;
|
|
|
|
bool _rebootOnSuccess;
|
|
|
|
bool _mdnsEnabled;
|
|
|
|
ota_state_t _state;
|
|
|
|
int _size;
|
|
|
|
int _cmd;
|
|
|
|
int _ota_port;
|
2018-07-24 19:40:18 +02:00
|
|
|
int _ota_timeout;
|
2017-03-11 07:15:44 +01:00
|
|
|
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
|
|
|
|
|
2018-07-24 19:40:18 +02:00
|
|
|
#endif /* __ARDUINO_OTA_H */
|