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 
19 namespace OtaUpgrade
20 {
21 namespace Mqtt
22 {
23 constexpr int8_t VERSION_NOT_READY{-1};
24 
25 constexpr int8_t ERROR_INVALID_MQTT_MESSAGE{-1};
26 constexpr int8_t ERROR_INVALID_PATCH_VERSION{-2};
27 constexpr int8_t ERROR_UNKNOWN_REASON{-10};
28 
30 {
31 public:
32  struct UpdateState {
34  bool started{false};
35  size_t version{0};
36 
38  {
39  delete stream;
40  }
41  };
42 
48  PayloadParser(size_t currentPatchVersion, size_t allowedVersionBytes = 24)
49  : currentPatchVersion(currentPatchVersion), allowedVersionBytes(allowedVersionBytes)
50  {
51  }
52 
53  virtual ~PayloadParser()
54  {
55  }
56 
62  virtual bool switchRom(const UpdateState& updateState) = 0;
63 
70  virtual ReadWriteStream* getStorageStream(size_t storageSize) = 0;
71 
80  int parse(MqttPayloadParserState& state, mqtt_message_t* message, const char* buffer, int length);
81 
82 private:
83  int getPatchVersion(const char* buffer, int length, size_t& offset, size_t versionStart = 0);
84 
85  size_t currentPatchVersion;
86  size_t allowedVersionBytes;
87 };
88 
89 } // namespace Mqtt
90 } // namespace OtaUpgrade
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...
virtual ReadWriteStream * getStorageStream(size_t storageSize)=0
Creates new stream to store the firmware update.
virtual ~PayloadParser()
Definition: PayloadParser.h:53
Definition: AdvancedPayloadParser.h:18
virtual bool switchRom(const UpdateState &updateState)=0
This method is responsible for switching the rom. This method is NOT restarting the system...
Definition: MqttPayloadParser.h:29
constexpr int8_t ERROR_INVALID_PATCH_VERSION
Definition: PayloadParser.h:26
constexpr int8_t VERSION_NOT_READY
Definition: PayloadParser.h:23
ReadWriteStream * stream
Definition: PayloadParser.h:33
PayloadParser(size_t currentPatchVersion, size_t allowedVersionBytes=24)
Definition: PayloadParser.h:48
Definition: PayloadParser.h:32
Definition: PayloadParser.h:29
constexpr int8_t ERROR_INVALID_MQTT_MESSAGE
Definition: PayloadParser.h:25
~UpdateState()
Definition: PayloadParser.h:37
Base class for read/write stream.
Definition: ReadWriteStream.h:19
bool started
Definition: PayloadParser.h:34
constexpr int8_t ERROR_UNKNOWN_REASON
Definition: PayloadParser.h:27
size_t version
Definition: PayloadParser.h:35