-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathOTALogic.h
128 lines (95 loc) · 3.28 KB
/
OTALogic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
This file is part of ArduinoIoTCloud.
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html
You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/
#ifndef ARDUINO_OTA_LOGIC_H_
#define ARDUINO_OTA_LOGIC_H_
/******************************************************************************
* INCLUDE
******************************************************************************/
#ifndef HOST
#include <ArduinoIoTCloud_Config.h>
#else
#define OTA_ENABLED (1)
#endif
#if OTA_ENABLED
#include "OTAStorage.h"
#include "crc.h"
/******************************************************************************
* CONSTANT
******************************************************************************/
static size_t const MQTT_OTA_BUF_SIZE = 256;
/******************************************************************************
* TYPEDEF
******************************************************************************/
enum class OTAState
{
Init, Idle, StartDownload, WaitForHeader, HeaderReceived, WaitForBinary, BinaryReceived, Verify, Rename, Reset, Error
};
enum class OTAError : int
{
None = 0,
StorageInitFailed = 1,
StorageOpenFailed = 2,
StorageWriteFailed = 3,
ChecksumMismatch = 4,
ReceivedDataOverrun = 5,
RenameOfTempFileFailed = 6
};
/******************************************************************************
* CLASS DECLARATION
******************************************************************************/
class OTALogic
{
public:
OTALogic();
inline void setOTAStorage(OTAStorage & ota_storage) { _ota_storage = &ota_storage; }
OTAError update();
void onOTADataReceived(uint8_t const * const data, size_t const length);
#ifdef HOST
inline OTAState state() const { return _ota_state; }
inline OTAError error() const { return _ota_error; }
#endif
private:
typedef struct
{
size_t num_bytes;
uint8_t buf[MQTT_OTA_BUF_SIZE];
} sMQTTOTABuffer;
typedef struct
{
uint32_t hdr_len;
uint32_t hdr_crc32;
uint32_t bytes_received;
crc_t crc32;
} sOTABinaryData;
OTAStorage * _ota_storage;
OTAState _ota_state;
OTAError _ota_error;
sMQTTOTABuffer _mqtt_ota_buf;
sOTABinaryData _ota_bin_data;
static size_t const OTA_BINARY_HEADER_SIZE = sizeof(_ota_bin_data.hdr_len) + sizeof(_ota_bin_data.hdr_crc32);
OTAState handle_Init();
OTAState handle_Idle();
OTAState handle_StartDownload();
OTAState handle_WaitForHeader();
OTAState handle_HeaderReceived();
OTAState handle_WaitForBinary();
OTAState handle_BinaryReceived();
OTAState handle_Verify();
OTAState handle_Rename();
OTAState handle_Reset();
void init_mqtt_ota_buffer();
void init_ota_binary_data();
};
#endif /* OTA_ENABLED */
#endif /* ARDUINO_OTA_LOGIC_H_ */