Skip to content

Commit 3dfcaba

Browse files
fixup! defining OTAInterface
1 parent 53c404a commit 3dfcaba

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

src/ota/interface/OTAInterface.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ OTACloudProcessInterface::OTACloudProcessInterface(MessageStream *ms)
6666
, policies(None)
6767
, state(Resume)
6868
, previous_state(Resume)
69+
, report_last_timestamp(0)
70+
, report_counter(0)
6971
, context(nullptr) {
7072
}
7173

@@ -76,35 +78,34 @@ OTACloudProcessInterface::~OTACloudProcessInterface() {
7678
void OTACloudProcessInterface::handleMessage(Message* msg) {
7779

7880
if ((state >= OtaAvailable || state < 0) && previous_state != state) {
79-
reportStatus();
81+
reportStatus(static_cast<int32_t>(state<0? state : 0));
8082
}
8183

8284
// this allows to do status report only when the state changes
8385
previous_state = state;
8486

8587
switch(state) {
86-
case Resume: updateState(resume(msg)); break;
87-
case OtaBegin: updateState(otaBegin()); break;
88-
case Idle: updateState(idle(msg)); break;
89-
case OtaAvailable: updateState(otaAvailable()); break;
90-
case StartOTA: updateState(startOTA()); break;
91-
case Fetch: updateState(fetch()); break;
92-
case FlashOTA: updateState(flashOTA()); break;
93-
case Reboot: updateState(reboot()); break;
94-
default: updateState(fail()); // all the states that are not defined are failures
88+
case Resume: updateState(resume(msg)); break;
89+
case OtaBegin: updateState(otaBegin()); break;
90+
case Idle: updateState(idle(msg)); break;
91+
case OtaAvailable: updateState(otaAvailable()); break;
92+
case StartOTA: updateState(startOTA()); break;
93+
case Fetch: updateState(fetch()); break;
94+
case FlashOTA: updateState(flashOTA()); break;
95+
case Reboot: updateState(reboot()); break;
96+
case OTAUnavailable: break;
97+
default: updateState(fail()); // all the states that are not defined are failures
9598
}
9699
}
97100

98101
OTACloudProcessInterface::State OTACloudProcessInterface::otaBegin() {
99102
if(!isOtaCapable()) {
100-
// FIXME What do we have to do in this case?
101-
DEBUG_WARNING("OTA is not available on this board");
102-
return OtaBegin;
103+
DEBUG_VERBOSE("OTA is not available on this board");
104+
return OTAUnavailable;
103105
}
104106

105107
struct OtaBeginUp msg = {
106108
OtaBeginUpId,
107-
{}
108109
};
109110

110111
SHA256 sha256_calc;
@@ -113,12 +114,16 @@ OTACloudProcessInterface::State OTACloudProcessInterface::otaBegin() {
113114
sha256_calc.finalize(sha256);
114115
memcpy(msg.params.sha, sha256, SHA256::HASH_SIZE);
115116

116-
DEBUG_VERBOSE("calculated SHA256: 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
117-
this->sha256[0], this->sha256[1], this->sha256[2], this->sha256[3], this->sha256[4], this->sha256[5], this->sha256[6], this->sha256[7], this->sha256[8], this->sha256[9], this->sha256[10], this->sha256[11], this->sha256[12], this->sha256[13], this->sha256[14], this->sha256[15], this->sha256[16], this->sha256[17], this->sha256[18], this->sha256[19], this->sha256[20], this->sha256[21], this->sha256[22], this->sha256[23], this->sha256[24], this->sha256[25], this->sha256[26], this->sha256[27], this->sha256[28], this->sha256[29], this->sha256[30], this->sha256[31]
117+
DEBUG_VERBOSE("calculated SHA256: "
118+
"0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X"
119+
"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
120+
sha256[0], sha256[1], sha256[2], sha256[3], sha256[4], sha256[5], sha256[6], sha256[7],
121+
sha256[8], sha256[9], sha256[10], sha256[11], sha256[12], sha256[13], sha256[14], sha256[15],
122+
sha256[16], sha256[17], sha256[18], sha256[19], sha256[20], sha256[21], sha256[22], sha256[23],
123+
sha256[24], sha256[25], sha256[26], sha256[27], sha256[28], sha256[29], sha256[30], sha256[31]
118124
);
119125

120126
deliver((Message*)&msg);
121-
// TODO msg object do not exist after this call make sure it gets copied somewhere
122127

123128
return Idle;
124129
}
@@ -148,8 +153,8 @@ OTACloudProcessInterface::State OTACloudProcessInterface::idle(Message* msg) {
148153
struct OtaUpdateCmdDown* ota_msg = (struct OtaUpdateCmdDown*)msg;
149154

150155
context = new OtaContext(
151-
ota_msg->params.id, ota_msg->params.url,
152-
ota_msg->params.initialSha256, ota_msg->params.finalSha256
156+
ota_msg->params.id, ota_msg->params.url,
157+
ota_msg->params.initialSha256, ota_msg->params.finalSha256
153158
);
154159

155160
// TODO verify that initialSha256 is the sha256 on board
@@ -186,35 +191,31 @@ void OTACloudProcessInterface::clean() {
186191
}
187192
}
188193

189-
void OTACloudProcessInterface::reportStatus() {
194+
void OTACloudProcessInterface::reportStatus(int32_t state_data) {
190195
if(context == nullptr) {
191196
// FIXME handle this case: ota not in progress
192197
return;
193198
}
194-
static uint32_t last_timestamp = getTime();
195-
static uint32_t counter = 0;
196199
uint32_t new_timestamp = getTime();
197200

198201
struct OtaProgressCmdUp msg = {
199202
OtaProgressCmdUpId,
200-
{}
201203
};
202204

203205
memcpy(msg.params.id, context->id, ID_SIZE);
204-
msg.params.state = state>=0 ? state : State::Fail;
206+
msg.params.state = state>=0 ? state : State::Fail;
205207

206-
if(new_timestamp == last_timestamp) {
207-
msg.params.time = new_timestamp*1e6 + ++counter;
208+
if(new_timestamp == report_last_timestamp) {
209+
msg.params.time = new_timestamp*1e6 + ++report_counter;
208210
} else {
209211
msg.params.time = new_timestamp*1e6;
210-
counter = 0;
211-
last_timestamp = new_timestamp;
212+
report_counter = 0;
213+
report_last_timestamp = new_timestamp;
212214
}
213215

214-
msg.params.state_data = static_cast<int32_t>(state<0? state : 0);
216+
msg.params.state_data = state_data;
215217

216218
deliver((Message*)&msg);
217-
// TODO msg object do not exist after this call make sure it gets copied somewhere
218219
}
219220

220221
OTACloudProcessInterface::OtaContext::OtaContext(

src/ota/interface/OTAInterface.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class OTACloudProcessInterface: public CloudProcess {
5050
FlashOTA = 6,
5151
Reboot = 7,
5252
Fail = 8,
53+
OTAUnavailable = 9,
5354

5455
// Error states that may generically happen on all board
5556
NoCapableBootloaderFail = static_cast<State>(ota::OTAError::NoCapableBootloader),
@@ -135,11 +136,9 @@ class OTACloudProcessInterface: public CloudProcess {
135136
// then we go back to idle state
136137
virtual State fail();
137138

138-
// TODO virtual =0 method used to reset the status of the failed ota operation
139-
// TODO depending on the state in which we failed we may have different fail context
140139
virtual void reset() = 0;
141140

142-
uint16_t policies; // TODO getter and setters for this
141+
uint16_t policies;
143142

144143
inline void updateState(State s) {
145144
if(state!=s) {
@@ -151,7 +150,7 @@ class OTACloudProcessInterface: public CloudProcess {
151150
}
152151

153152
// This method is called to report the current state of the OtaClass
154-
void reportStatus();
153+
void reportStatus(int32_t state_data);
155154

156155
// in order to calculate the SHA256 we need to get the start and end address of the Application,
157156
// The Implementation of this class have to implement them.
@@ -160,7 +159,7 @@ class OTACloudProcessInterface: public CloudProcess {
160159
virtual uint32_t appSize() = 0;
161160

162161
// some architecture require to explicitly open the flash in order to read it
163-
virtual bool appFlashOpen() = 0;
162+
virtual bool appFlashOpen() = 0;
164163
virtual bool appFlashClose() = 0;
165164

166165
// sha256 is going to be used in the ota process for validation, avoid calculating it twice
@@ -173,6 +172,8 @@ class OTACloudProcessInterface: public CloudProcess {
173172

174173
State state, previous_state;
175174

175+
// status report related attributes
176+
uint32_t report_last_timestamp, report_counter;
176177
protected:
177178
struct OtaContext {
178179
OtaContext(

0 commit comments

Comments
 (0)