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:
35 
36  struct Item {
38  Partition partition; // << partition to write the data to
39  size_t size{0}; // << actual size of written bytes
40  ReadWriteStream* stream{nullptr}; // (optional) output stream to use.
41 
42  Item(String url, Partition partition, ReadWriteStream* stream) : url(url), partition(partition), stream(stream)
43  {
44  }
45 
47  {
48  delete stream;
49  }
50 
52  {
53  if(stream == nullptr) {
54  stream = new Ota::UpgradeOutputStream(partition);
55  }
56  return stream;
57  }
58  };
59 
60  class ItemList : public Vector<Item>
61  {
62  public:
63  bool addNew(Item* it)
64  {
65  if(addElement(it)) {
66  return true;
67  }
68  delete it;
69  return false;
70  }
71  };
72 
81  bool addItem(const String& firmwareFileUrl, Partition partition, ReadWriteStream* stream = nullptr)
82  {
83  return items.addNew(new Item{firmwareFileUrl, partition, stream});
84  }
85 
86  void start();
87 
93  {
94  this->romSlot = romSlot;
95  }
96 
97  void setCallback(CompletedDelegate reqUpdateDelegate)
98  {
99  setDelegate(reqUpdateDelegate);
100  }
101 
102  void setDelegate(CompletedDelegate reqUpdateDelegate)
103  {
104  this->updateDelegate = reqUpdateDelegate;
105  }
106 
118  {
119  baseRequest = request;
120  }
121 
125  const ItemList& getItems() const
126  {
127  return items;
128  }
129 
130 protected:
131  void applyUpdate();
132  void updateFailed();
133 
134  virtual int itemComplete(HttpConnection& client, bool success);
135  virtual int updateComplete(HttpConnection& client, bool success);
136 
137 protected:
143 };
144 
145 } // namespace Network
146 
147 } // namespace Ota
Write-only stream type used during firmware upgrade.
Definition: UpgradeOutputStream.h:23
void switchToRom(uint8_t romSlot)
On completion, switch to the given ROM slot.
Definition: HttpUpgrader.h:92
Vector class template.
Definition: WVector.h:31
bool addNew(Item *it)
Definition: HttpUpgrader.h:63
uint8_t currentItem
Definition: HttpUpgrader.h:142
Item(String url, Partition partition, ReadWriteStream *stream)
Definition: HttpUpgrader.h:42
CompletedDelegate updateDelegate
Definition: HttpUpgrader.h:139
Definition: HttpClient.h:28
The String class.
Definition: WString.h:136
Definition: HttpUpgrader.h:21
size_t size
Definition: HttpUpgrader.h:39
Definition: HttpUpgrader.h:60
virtual int updateComplete(HttpConnection &client, bool success)
void setDelegate(CompletedDelegate reqUpdateDelegate)
Definition: HttpUpgrader.h:102
~Item()
Definition: HttpUpgrader.h:46
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:36
Provides http base used for client and server connections.
Definition: HttpConnection.h:27
Represents a flash partition.
Definition: Partition.h:85
void setBaseRequest(HttpRequest *request)
Sets the base request that can be used to pass.
Definition: HttpUpgrader.h:117
String url
Definition: HttpUpgrader.h:37
Partition partition
Definition: HttpUpgrader.h:38
Definition: HttpUpgrader.h:30
ReadWriteStream * stream
Definition: HttpUpgrader.h:40
constexpr uint8_t NO_ROM_SWITCH
Magic value for ROM slot indicating slot won&#39;t change after successful OTA.
Definition: HttpUpgrader.h:28
Definition: HttpUpgrader.h:36
bool addItem(const String &firmwareFileUrl, Partition partition, ReadWriteStream *stream=nullptr)
Add an item to update.
Definition: HttpUpgrader.h:81
void setCallback(CompletedDelegate reqUpdateDelegate)
Definition: HttpUpgrader.h:97
ItemList items
Definition: HttpUpgrader.h:138
HttpRequest * baseRequest
Definition: HttpUpgrader.h:140
ReadWriteStream * getStream()
Definition: HttpUpgrader.h:51
Base class for read/write stream.
Definition: ReadWriteStream.h:19
virtual int itemComplete(HttpConnection &client, bool success)
uint8_t romSlot
Definition: HttpUpgrader.h:141
const ItemList & getItems() const
Allow read access to item list.
Definition: HttpUpgrader.h:125