supla-device
Loading...
Searching...
No Matches
sw_update.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_DEVICE_SW_UPDATE_H_
5#define SRC_SUPLA_DEVICE_SW_UPDATE_H_
6
7#define SUPLA_MAX_URL_LENGTH 202
8
9#include <SuplaDevice.h>
10
11namespace Supla {
12
13namespace Device {
14enum class SwUpdateResult : uint8_t {
15 UPDATED,
16 UP_TO_DATE,
17 FAILED,
18};
19
21 public:
22 virtual ~SwUpdateObserver() = default;
23 virtual void onSwUpdateStarted() = 0;
24 virtual void onSwUpdateProgress(uint32_t downloadedBytes,
25 uint32_t totalBytes) = 0;
26 virtual void onSwUpdateFinished(bool success, const char *reason) = 0;
27 virtual void onSwUpdateResult(SwUpdateResult result, const char *reason) {
28 onSwUpdateFinished(result != SwUpdateResult::FAILED, reason);
29 }
30};
31
32class SwUpdate {
33 public:
34 static SwUpdate *Create(SuplaDeviceClass *sdc,
35 const char *newUrl,
36 Supla::SwUpdateMode mode);
37 virtual ~SwUpdate();
38
39 void start() {
40 started = true;
41 if (observer) {
42 observer->onSwUpdateStarted();
43 }
44 }
45 void setObserver(SwUpdateObserver *newObserver) { observer = newObserver; }
46 virtual void iterate() = 0;
47
48 void setUrl(const char *newUrl);
49 bool isStarted() {
50 return started;
51 }
52 bool isFinished() {
53 return finished;
54 }
55 bool isAborted() {
56 return abort;
57 }
58 void useBeta() {
59 beta = true;
60 }
61 void setSkipCert() {
62 // One-time recovery fallback for expired OTA certificates.
63 // The OTA flow clears this mode after use.
64 skipCert = true;
65 }
66 bool isRetryAllowed() {
67 return retryAllowed;
68 }
69
70 const char *getUrl() const {
71 return updateUrl;
72 }
73 const char *getNewVersion() const {
74 return newVersion;
75 }
76 const char *getChangelogUrl() const {
77 return changelogUrl;
78 }
79
80 bool isSecurityOnly() const {
81 return securityOnly;
82 }
83 void setSecurityOnly() {
84 securityOnly = true;
85 }
86
87 protected:
88 explicit SwUpdate(SuplaDeviceClass *sdc,
89 const char *newUrl,
90 Supla::SwUpdateMode mode);
91
92 void notifyProgress(uint32_t downloadedBytes, uint32_t totalBytes) {
93 if (observer) {
94 observer->onSwUpdateProgress(downloadedBytes, totalBytes);
95 }
96 }
97 void notifyFinished(bool success, const char *reason = nullptr) {
98 if (observer) {
99 observer->onSwUpdateFinished(success, reason);
100 }
101 }
102 void notifyFinished(SwUpdateResult result, const char *reason = nullptr) {
103 if (observer) {
104 observer->onSwUpdateResult(result, reason);
105 }
106 }
107
108 bool beta = false;
109 bool skipCert = false;
110 bool securityOnly = false;
111 bool started = false;
112 bool finished = false;
113 bool abort = false;
114 SuplaDeviceClass *sdc = nullptr;
115 SwUpdateObserver *observer = nullptr;
116 char *updateUrl = nullptr;
117 char *newVersion = nullptr;
118 char *changelogUrl = nullptr;
119 bool retryAllowed = false;
120 Supla::SwUpdateMode mode = Supla::SwUpdateMode::NotSet;
121
122 char url[SUPLA_MAX_URL_LENGTH] = {};
123};
124}; // namespace Device
125}; // namespace Supla
126
127#endif // SRC_SUPLA_DEVICE_SW_UPDATE_H_
Definition SuplaDevice.h:167
Definition sw_update.h:20
Definition sw_update.h:32