PayloadParser.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  * PayloadParser.h
8  *
9  * Created: 2021 - Slavey Karadzhov <slav@attachix.com>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include <Platform/System.h>
18 #include <memory>
19 
20 namespace OtaUpgrade
21 {
22 namespace Mqtt
23 {
24 constexpr int8_t VERSION_NOT_READY{-1};
25 
26 constexpr int8_t ERROR_INVALID_MQTT_MESSAGE{-1};
27 constexpr int8_t ERROR_INVALID_PATCH_VERSION{-2};
28 constexpr int8_t ERROR_UNKNOWN_REASON{-10};
29 
31 {
32 public:
33  struct UpdateState {
34  std::unique_ptr<ReadWriteStream> stream;
35  bool started{false};
36  size_t version{0};
37  };
38 
44  PayloadParser(size_t currentPatchVersion, size_t allowedVersionBytes = 24)
45  : currentPatchVersion(currentPatchVersion), allowedVersionBytes(allowedVersionBytes)
46  {
47  }
48 
49  virtual ~PayloadParser()
50  {
51  }
52 
58  virtual bool switchRom(const UpdateState& updateState) = 0;
59 
66  virtual ReadWriteStream* getStorageStream(size_t storageSize) = 0;
67 
76  int parse(MqttPayloadParserState& state, mqtt_message_t* message, const char* buffer, int length);
77 
78 private:
79  int getPatchVersion(const char* buffer, int length, size_t& offset, size_t versionStart = 0);
80 
81  size_t currentPatchVersion;
82  size_t allowedVersionBytes;
83 };
84 
85 } // namespace Mqtt
86 } // namespace OtaUpgrade
Definition: PayloadParser.h:31
virtual ReadWriteStream * getStorageStream(size_t storageSize)=0
Creates new stream to store the firmware update.
virtual bool switchRom(const UpdateState &updateState)=0
This method is responsible for switching the rom. This method is NOT restarting the system....
PayloadParser(size_t currentPatchVersion, size_t allowedVersionBytes=24)
Definition: PayloadParser.h:44
virtual ~PayloadParser()
Definition: PayloadParser.h:49
int parse(MqttPayloadParserState &state, mqtt_message_t *message, const char *buffer, int length)
This method takes care to read the incoming MQTT message and pass it to the stream that is responsobl...
Base class for read/write stream.
Definition: ReadWriteStream.h:20
constexpr int8_t ERROR_INVALID_PATCH_VERSION
Definition: PayloadParser.h:27
constexpr int8_t VERSION_NOT_READY
Definition: PayloadParser.h:24
constexpr int8_t ERROR_UNKNOWN_REASON
Definition: PayloadParser.h:28
constexpr int8_t ERROR_INVALID_MQTT_MESSAGE
Definition: PayloadParser.h:26
Definition: BasicStream.h:24
Definition: MqttPayloadParser.h:29
Definition: PayloadParser.h:33
bool started
Definition: PayloadParser.h:35
std::unique_ptr< ReadWriteStream > stream
Definition: PayloadParser.h:34
size_t version
Definition: PayloadParser.h:36