Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.5 KiB

  1. #ifndef __ARDUINO_OTA_H
  2. #define __ARDUINO_OTA_H
  3. #include <ESP8266WiFi.h>
  4. #include <WiFiUdp.h>
  5. class UdpContext;
  6. #define OTA_CALLBACK(callback) void (*callback)()
  7. #define OTA_CALLBACK_PROGRESS(callback) void (*callback)(unsigned int, unsigned int)
  8. #define OTA_CALLBACK_ERROR(callback) void (*callback)(ota_error_t)
  9. typedef enum {
  10. OTA_IDLE,
  11. OTA_WAITAUTH,
  12. OTA_RUNUPDATE
  13. } ota_state_t;
  14. typedef enum {
  15. OTA_AUTH_ERROR,
  16. OTA_BEGIN_ERROR,
  17. OTA_CONNECT_ERROR,
  18. OTA_RECEIVE_ERROR,
  19. OTA_END_ERROR
  20. } ota_error_t;
  21. class ArduinoOTAClass
  22. {
  23. public:
  24. ArduinoOTAClass();
  25. ~ArduinoOTAClass();
  26. void setPort(uint16_t port);
  27. void setHostname(const char *hostname);
  28. void setPassword(const char *password);
  29. void onStart(OTA_CALLBACK(fn));
  30. void onEnd(OTA_CALLBACK(fn));
  31. void onProgress(OTA_CALLBACK_PROGRESS(fn));
  32. void onError(OTA_CALLBACK_ERROR (fn));
  33. void begin();
  34. void handle();
  35. private:
  36. int _port;
  37. String _password;
  38. String _hostname;
  39. String _nonce;
  40. UdpContext *_udp_ota;
  41. bool _initialized;
  42. ota_state_t _state;
  43. int _size;
  44. int _cmd;
  45. int _ota_port;
  46. IPAddress _ota_ip;
  47. String _md5;
  48. OTA_CALLBACK(_start_callback);
  49. OTA_CALLBACK(_end_callback);
  50. OTA_CALLBACK_ERROR(_error_callback);
  51. OTA_CALLBACK_PROGRESS(_progress_callback);
  52. void _runUpdate(void);
  53. void _onRx(void);
  54. int parseInt(void);
  55. String readStringUntil(char end);
  56. };
  57. extern ArduinoOTAClass ArduinoOTA;
  58. #endif /* __ARDUINO_OTA_H */