HttpUpgrader.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * HttpUpgrader.h
8  *
9  * Created on: 2015/09/03.
10  * Author: Richard A Burton & Anakod
11  *
12  * Modified: 2017, 2021 - Slavey Karadzhov <slav@attachix.com>
13  *
14  ****/
15 
16 #pragma once
17 
18 #include <Network/HttpClient.h>
20 
21 namespace Ota
22 {
23 namespace Network
24 {
28 constexpr uint8_t NO_ROM_SWITCH{0xff};
29 
30 class HttpUpgrader : protected HttpClient
31 {
32 public:
33  using CompletedDelegate = Delegate<void(HttpUpgrader& client, bool result)>;
35 
36  struct Item {
38  Partition partition; // << partition to write the data to
39  size_t size{0}; // << actual size of written bytes
40  std::unique_ptr<ReadWriteStream> stream; // (optional) output stream to use.
41 
43  {
44  }
45 
47  {
48  if(!stream) {
49  stream = std::make_unique<Ota::UpgradeOutputStream>(partition);
50  }
51  return stream.get();
52  }
53  };
54 
55  class ItemList : public Vector<Item>
56  {
57  public:
58  bool addNew(Item* it)
59  {
60  if(addElement(it)) {
61  return true;
62  }
63  delete it;
64  return false;
65  }
66  };
67 
76  bool addItem(const String& firmwareFileUrl, Partition partition, ReadWriteStream* stream = nullptr)
77  {
78  return items.addNew(new Item{firmwareFileUrl, partition, stream});
79  }
80 
81  void start();
82 
87  void switchToRom(uint8_t romSlot)
88  {
89  this->romSlot = romSlot;
90  }
91 
92  void setCallback(CompletedDelegate reqUpdateDelegate)
93  {
94  setDelegate(reqUpdateDelegate);
95  }
96 
97  void setDelegate(CompletedDelegate reqUpdateDelegate)
98  {
99  this->updateDelegate = reqUpdateDelegate;
100  }
101 
113  {
114  baseRequest = request;
115  }
116 
120  const ItemList& getItems() const
121  {
122  return items;
123  }
124 
125 protected:
126  void applyUpdate();
130 
131  int itemComplete(HttpConnection& client, bool success);
132 
133 protected:
138  uint8_t currentItem{0};
139 };
140 
141 } // namespace Network
142 
143 } // namespace Ota
Definition: HttpClient.h:29
Provides http base used for client and server connections.
Definition: HttpConnection.h:28
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:37
Definition: HttpUpgrader.h:56
bool addNew(Item *it)
Definition: HttpUpgrader.h:58
Definition: HttpUpgrader.h:31
int itemComplete(HttpConnection &client, bool success)
void setBaseRequest(HttpRequest *request)
Sets the base request that can be used to pass.
Definition: HttpUpgrader.h:112
HttpRequest * baseRequest
Definition: HttpUpgrader.h:136
const ItemList & getItems() const
Allow read access to item list.
Definition: HttpUpgrader.h:120
uint8_t romSlot
Definition: HttpUpgrader.h:137
void setDelegate(CompletedDelegate reqUpdateDelegate)
Definition: HttpUpgrader.h:97
ItemList items
Definition: HttpUpgrader.h:134
void setCallback(CompletedDelegate reqUpdateDelegate)
Definition: HttpUpgrader.h:92
bool addItem(const String &firmwareFileUrl, Partition partition, ReadWriteStream *stream=nullptr)
Add an item to update.
Definition: HttpUpgrader.h:76
CompletedDelegate updateDelegate
Definition: HttpUpgrader.h:135
uint8_t currentItem
Definition: HttpUpgrader.h:138
void switchToRom(uint8_t romSlot)
On completion, switch to the given ROM slot.
Definition: HttpUpgrader.h:87
Base class for read/write stream.
Definition: ReadWriteStream.h:20
Represents a flash partition.
Definition: Partition.h:86
The String class.
Definition: WString.h:137
Vector class template.
Definition: WVector.h:32
bool addElement(const Item &obj)
Definition: WVector.h:349
constexpr uint8_t NO_ROM_SWITCH
Magic value for ROM slot indicating slot won't change after successful OTA.
Definition: HttpUpgrader.h:28
Definition: IdfUpgrader.h:18
Definition: HttpUpgrader.h:36
ReadWriteStream * getStream()
Definition: HttpUpgrader.h:46
Item(String url, Partition partition, ReadWriteStream *stream)
Definition: HttpUpgrader.h:42
Partition partition
Definition: HttpUpgrader.h:38
size_t size
Definition: HttpUpgrader.h:39
std::unique_ptr< ReadWriteStream > stream
Definition: HttpUpgrader.h:40
String url
Definition: HttpUpgrader.h:37