IdfUpgrader.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  * IdfUpgrader.h
8  *
9  * This header includes all unified Over-The-Air functions.
10  *
11 */
12 
13 #pragma once
14 #include <Ota/UpgraderBase.h>
15 #include <esp_ota_ops.h>
16 
17 namespace Ota
18 {
22 class IdfUpgrader : public UpgraderBase
23 {
24 public:
25  bool begin(Partition partition, size_t size = 0) override;
26  size_t write(const uint8_t* buffer, size_t size) override;
27 
28  bool end() override
29  {
30  return esp_ota_end(handle) == ESP_OK;
31  }
32 
33  bool abort() override
34  {
35  return true;
36  }
37 
38  bool setBootPartition(Partition partition, bool save = true) override
39  {
40  if(!save) {
41  return false;
42  }
43 
44  return esp_ota_set_boot_partition(convertToIdfPartition(partition)) == ESP_OK;
45  }
46 
47  Partition getBootPartition() override
48  {
49  return convertFromIdfPartition(esp_ota_get_boot_partition());
50  }
51 
53  {
54  return convertFromIdfPartition(esp_ota_get_running_partition());
55  }
56 
57  Partition getNextBootPartition(Partition startFrom = {}) override
58  {
59  const esp_partition_t* idfFrom = startFrom ? convertToIdfPartition(startFrom) : nullptr;
60  return convertFromIdfPartition(esp_ota_get_next_update_partition(idfFrom));
61  }
62 
63  static const esp_partition_t* convertToIdfPartition(Partition partition)
64  {
65  return esp_partition_find_first(esp_partition_type_t(partition.type()),
66  esp_partition_subtype_t(partition.subType()), partition.name().c_str());
67  }
68 
69  static Partition convertFromIdfPartition(const esp_partition_t* partition)
70  {
71  return partition ? Storage::findPartition(String(partition->label)) : Partition{};
72  }
73 
74 private:
75  size_t maxSize{0};
76  size_t writtenSoFar{0};
77  esp_ota_handle_t handle{};
78 };
79 
80 } // namespace Ota
Partition getRunningPartition() override
Gets information about the partition from which the current application is running.
Definition: IdfUpgrader.h:72
Storage::Partition Partition
Definition: UpgraderBase.h:44
static Partition convertFromIdfPartition(const esp_partition_t *partition)
Definition: IdfUpgrader.h:89
size_t write(const uint8_t *buffer, size_t size) override
Writes chunk of data to the partition set in begin().
Partition getBootPartition() override
Gets information about the partition that is set as the default one to boot.
Definition: IdfUpgrader.h:67
The String class.
Definition: WString.h:136
Partition getNextBootPartition(Partition startFrom={}) override
Gets the next bootable partition that can be used after successful OTA upgrade.
Definition: IdfUpgrader.h:77
bool end() override
Finalizes the partition upgrade.
Definition: IdfUpgrader.h:48
Definition: HttpUpgrader.h:21
bool abort() override
Aborts a partition upgrade.
Definition: IdfUpgrader.h:53
bool begin(Partition partition, size_t size=0) override
Prepares a partition for an upgrade. The preparation is bootloader and architecture dependent.
Partition findPartition(const String &name)
Find the first partition matching the given name.
static const esp_partition_t * convertToIdfPartition(Partition partition)
Definition: IdfUpgrader.h:83
bool setBootPartition(Partition partition, bool save=true) override
Sets the default partition from where the application will be booted on next restart.
Definition: IdfUpgrader.h:58