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 
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
ESP32 OTA Upgrader implementation.
Definition: IdfUpgrader.h:23
Partition getBootPartition() override
Gets information about the partition that is set as the default one to boot.
Definition: IdfUpgrader.h:47
static const esp_partition_t * convertToIdfPartition(Partition partition)
Definition: IdfUpgrader.h:63
Partition getRunningPartition() override
Gets information about the partition from which the current application is running.
Definition: IdfUpgrader.h:52
bool abort() override
Aborts a partition upgrade.
Definition: IdfUpgrader.h:33
static Partition convertFromIdfPartition(const esp_partition_t *partition)
Definition: IdfUpgrader.h:69
Partition getNextBootPartition(Partition startFrom={}) override
Gets the next bootable partition that can be used after successful OTA upgrade.
Definition: IdfUpgrader.h:57
bool end() override
Finalizes the partition upgrade.
Definition: IdfUpgrader.h:28
size_t write(const uint8_t *buffer, size_t size) override
Writes chunk of data to the partition set in begin().
bool begin(Partition partition, size_t size=0) override
Prepares a partition for an upgrade. The preparation is bootloader and architecture dependent.
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:38
Definition: UpgraderBase.h:20
Represents a flash partition.
Definition: Partition.h:86
Partition::Type type() const
Obtain partition type.
Definition: Partition.h:319
uint8_t subType() const
Obtain partition sub-type.
Definition: Partition.h:327
String name() const
Get partition name.
Definition: Partition.h:370
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:617
Definition: IdfUpgrader.h:18
Partition findPartition(const String &name)
Find the first partition matching the given name.