SatNOGS-COMMS
4.1.0
A COMMS subsystem for CubeSats
Toggle main menu visibility
Loading...
Searching...
No Matches
ota.hpp
Go to the documentation of this file.
1
/*
2
* SatNOGS-COMMS MCU software
3
*
4
* Copyright (C) 2025, Libre Space Foundation <http://libre.space>
5
*
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
*
19
* SPDX-License-Identifier: GNU General Public License v3.0 or later
20
*/
21
22
#pragma once
23
24
#include <
time.hpp
>
25
#include <zephyr/kernel.h>
26
27
namespace
satnogs::comms
28
{
29
37
class
ota
38
{
39
public
:
45
static
constexpr
size_t
MAX_FILE_LEN
= 64;
46
47
static
constexpr
const
char
*
LITTLEFS_MCU_DIR
=
"/SD/ota-mcu/"
;
48
static
constexpr
const
char
*
LITTLEFS_FPGA_DIR
=
"/SD/ota-fpga/"
;
49
50
static
ota
&
51
get_instance
()
52
{
53
static
ota
instance;
54
return
instance;
55
}
56
57
ota
(
ota
const
&) =
delete
;
58
59
void
60
operator=
(
ota
const
&) =
delete
;
61
66
enum class
subsys
: uint8_t
67
{
68
MCU_SLOT0
,
69
MCU_SLOT1
,
70
FPGA_WIC
,
72
MCU_LITTLEFS
,
74
FPGA_LITTLEFS
76
};
77
78
enum class
status
: uint8_t
79
{
80
OK
,
81
INVALID_SEQ
,
82
INVALID_SESSION
,
83
INVALID_DST
,
84
INVALID_SLOT
,
85
INVALID_HASH
,
86
INVALID_SIZE
,
87
INVALID_STATE
,
88
INVALID_DATA
,
89
NO_SESSIONS_LEFT
,
90
FIN_OPERATION_PENDING
,
91
INTERNAL_ERROR
,
92
CURRENT_IMG_NOT_CONFIRMED
93
};
94
95
class
start_tlc
96
{
97
public
:
98
subsys
dst
;
99
uint32_t
crc
;
100
uint32_t
size
;
101
uint8_t
slot
;
102
uint8_t
sha256
[32];
103
etl::string<MAX_FILE_LEN>
name
;
104
105
start_tlc
() :
dst
(
subsys
::
MCU_LITTLEFS
),
crc
(0),
size
(0),
slot
(0),
name
() {}
106
};
107
108
class
data_tlc
109
{
110
public
:
111
uint8_t
session
;
112
uint32_t
seq
;
113
const
uint8_t *
data
;
114
uint32_t
len
;
115
data_tlc
() :
session
(255),
seq
(0),
data
(nullptr),
len
(0) {}
116
};
117
118
class
fin_tlc
119
{
120
public
:
121
uint8_t
session
;
122
uint8_t
method
;
123
uint32_t
delay_secs
;
124
125
fin_tlc
() :
session
(0),
method
(0),
delay_secs
(0) {}
126
};
127
128
class
response_tlm
129
{
130
public
:
131
uint8_t
session
;
132
status
s
;
133
uint32_t
ack
;
134
135
response_tlm
()
136
:
session
(CONFIG_OTA_MAX_SESSIONS),
s
(
status
::
INVALID_STATE
),
ack
(0)
137
{
138
}
139
};
140
141
class
session
142
{
143
public
:
144
/*
145
* @warning This is a custom hash that is used to discriminate possible
146
* changes of the session class Make sure to change it if you do ANY
147
* modification in the class. With this way, the retention memory will
148
* invalidate its contents avoiding conflicts with a firmware previously
149
* stored session information
150
*/
151
static
constexpr
size_t
HASH
= 0x6713A79;
152
session
()
153
:
destination
(
subsys
::
MCU_LITTLEFS
),
154
slot
(0),
155
active
(false),
156
applied
(false),
157
total_size
(0U),
158
ack
(0U),
159
time_src
(
time
::
time_src
::UPTIME),
160
t
{},
161
name
(),
162
sha256
{}
163
{
164
}
165
166
subsys
destination
;
167
uint8_t
slot
;
168
bool
active
;
169
bool
applied
;
170
uint32_t
total_size
;
171
uint32_t
ack
;
172
time::time_src
time_src
;
173
struct
tm
t
;
174
etl::string<MAX_FILE_LEN>
name
;
175
uint8_t
sha256
[32];
176
};
177
178
void
179
reset_session
(uint32_t
id
);
180
181
void
182
reset_sessions
();
183
184
void
185
start
(
const
start_tlc
&req,
response_tlm
&tlm);
186
187
void
188
recv
(
const
data_tlc
&tlc,
response_tlm
&tlm);
189
190
void
191
finalize
(
const
fin_tlc
&tlc,
response_tlm
&tlm,
int
wdgid);
192
193
void
194
confirm_image
();
195
196
bool
197
get_session_info
(uint8_t
id
,
session
&out)
const
;
198
199
class
inval_session
:
public
lib::exception
200
{
201
public
:
202
inval_session
(
const
char
*file_name,
int
line)
203
:
exception
(
exception
::
severity
::
MINOR
, file_name, line,
204
error_msg
{
"Invalid OTA session"
,
"otainvalsession"
, EINVAL})
205
{
206
}
207
};
208
209
protected
:
216
static
constexpr
size_t
OTA_RETENTION_MEM_SIZE
= 1752;
217
ota
();
218
219
private
:
220
std::array<session, CONFIG_OTA_MAX_SESSIONS> m_sessions;
221
mutable
struct
k_mutex m_mtx;
222
223
void
224
reset_retention();
225
226
void
227
update_retention(uint32_t idx);
228
229
status
230
write_slot(
session
&s,
const
data_tlc
&tlc);
231
232
status
233
write_wic(
session
&s,
const
data_tlc
&tlc);
234
235
status
236
write_littlefs(
bool
mcu,
session
&s,
const
data_tlc
&tlc);
237
238
bool
239
sha256_check(
const
session
&s,
int
wdgid);
240
241
bool
242
sha256_littlefs(
const
session
&s,
const
etl::istring &path,
int
wdgid);
243
244
bool
245
sha256_fpga_wic(
const
session
&s,
int
wdgid);
246
247
bool
248
sha256_mcu(
const
session
&s);
249
250
bool
251
finalize_littlefs_to_mcu(
session
&s,
response_tlm
&tlm);
252
253
bool
254
finalize_mcu(
session
&s,
response_tlm
&tlm);
255
};
256
257
}
// namespace satnogs::comms
satnogs::comms::lib::exception::error_msg
A class representing error messages in the SatNOGS-COMMS system.
Definition
exception.hpp:83
satnogs::comms::lib::exception
Exception base class.
Definition
exception.hpp:63
satnogs::comms::lib::exception::severity
severity
Severity levels of exceptions.
Definition
exception.hpp:71
satnogs::comms::lib::exception::severity::MINOR
@ MINOR
Failure having minimal impact.
Definition
exception.hpp:75
satnogs::comms::lib::exception::exception
exception(severity sev, const char *file, int lineno, const error_msg &err_msg)
Constructor for the exception class.
Definition
exception.hpp:102
satnogs::comms::ota::data_tlc
Definition
ota.hpp:109
satnogs::comms::ota::data_tlc::session
uint8_t session
Definition
ota.hpp:111
satnogs::comms::ota::data_tlc::seq
uint32_t seq
Definition
ota.hpp:112
satnogs::comms::ota::data_tlc::len
uint32_t len
Definition
ota.hpp:114
satnogs::comms::ota::data_tlc::data_tlc
data_tlc()
Definition
ota.hpp:115
satnogs::comms::ota::data_tlc::data
const uint8_t * data
Definition
ota.hpp:113
satnogs::comms::ota::fin_tlc
Definition
ota.hpp:119
satnogs::comms::ota::fin_tlc::delay_secs
uint32_t delay_secs
Definition
ota.hpp:123
satnogs::comms::ota::fin_tlc::method
uint8_t method
Definition
ota.hpp:122
satnogs::comms::ota::fin_tlc::session
uint8_t session
Definition
ota.hpp:121
satnogs::comms::ota::fin_tlc::fin_tlc
fin_tlc()
Definition
ota.hpp:125
satnogs::comms::ota::inval_session::inval_session
inval_session(const char *file_name, int line)
Definition
ota.hpp:202
satnogs::comms::ota::response_tlm
Definition
ota.hpp:129
satnogs::comms::ota::response_tlm::session
uint8_t session
Definition
ota.hpp:131
satnogs::comms::ota::response_tlm::response_tlm
response_tlm()
Definition
ota.hpp:135
satnogs::comms::ota::response_tlm::ack
uint32_t ack
Definition
ota.hpp:133
satnogs::comms::ota::response_tlm::s
status s
Definition
ota.hpp:132
satnogs::comms::ota::session
Definition
ota.hpp:142
satnogs::comms::ota::session::name
etl::string< MAX_FILE_LEN > name
Definition
ota.hpp:174
satnogs::comms::ota::session::applied
bool applied
Definition
ota.hpp:169
satnogs::comms::ota::session::active
bool active
Definition
ota.hpp:168
satnogs::comms::ota::session::sha256
uint8_t sha256[32]
Definition
ota.hpp:175
satnogs::comms::ota::session::t
struct tm t
Definition
ota.hpp:173
satnogs::comms::ota::session::HASH
static constexpr size_t HASH
Definition
ota.hpp:151
satnogs::comms::ota::session::ack
uint32_t ack
Definition
ota.hpp:171
satnogs::comms::ota::session::destination
subsys destination
Definition
ota.hpp:166
satnogs::comms::ota::session::total_size
uint32_t total_size
Definition
ota.hpp:170
satnogs::comms::ota::session::time_src
time::time_src time_src
Definition
ota.hpp:172
satnogs::comms::ota::session::slot
uint8_t slot
Definition
ota.hpp:167
satnogs::comms::ota::session::session
session()
Definition
ota.hpp:152
satnogs::comms::ota::start_tlc
Definition
ota.hpp:96
satnogs::comms::ota::start_tlc::dst
subsys dst
Definition
ota.hpp:98
satnogs::comms::ota::start_tlc::size
uint32_t size
Definition
ota.hpp:100
satnogs::comms::ota::start_tlc::name
etl::string< MAX_FILE_LEN > name
Definition
ota.hpp:103
satnogs::comms::ota::start_tlc::start_tlc
start_tlc()
Definition
ota.hpp:105
satnogs::comms::ota::start_tlc::sha256
uint8_t sha256[32]
Definition
ota.hpp:102
satnogs::comms::ota::start_tlc::slot
uint8_t slot
Definition
ota.hpp:101
satnogs::comms::ota::start_tlc::crc
uint32_t crc
Definition
ota.hpp:99
satnogs::comms::ota::status
status
Definition
ota.hpp:79
satnogs::comms::ota::status::NO_SESSIONS_LEFT
@ NO_SESSIONS_LEFT
Definition
ota.hpp:89
satnogs::comms::ota::status::INVALID_SLOT
@ INVALID_SLOT
Definition
ota.hpp:84
satnogs::comms::ota::status::INVALID_SIZE
@ INVALID_SIZE
Definition
ota.hpp:86
satnogs::comms::ota::status::INVALID_SESSION
@ INVALID_SESSION
Definition
ota.hpp:82
satnogs::comms::ota::status::INVALID_HASH
@ INVALID_HASH
Definition
ota.hpp:85
satnogs::comms::ota::status::INVALID_STATE
@ INVALID_STATE
Definition
ota.hpp:87
satnogs::comms::ota::status::INVALID_DST
@ INVALID_DST
Definition
ota.hpp:83
satnogs::comms::ota::status::CURRENT_IMG_NOT_CONFIRMED
@ CURRENT_IMG_NOT_CONFIRMED
Definition
ota.hpp:92
satnogs::comms::ota::status::INVALID_SEQ
@ INVALID_SEQ
Definition
ota.hpp:81
satnogs::comms::ota::status::FIN_OPERATION_PENDING
@ FIN_OPERATION_PENDING
Definition
ota.hpp:90
satnogs::comms::ota::status::OK
@ OK
Definition
ota.hpp:80
satnogs::comms::ota::status::INTERNAL_ERROR
@ INTERNAL_ERROR
Definition
ota.hpp:91
satnogs::comms::ota::status::INVALID_DATA
@ INVALID_DATA
Definition
ota.hpp:88
satnogs::comms::ota::recv
void recv(const data_tlc &tlc, response_tlm &tlm)
Definition
ota.cpp:214
satnogs::comms::ota::LITTLEFS_FPGA_DIR
static constexpr const char * LITTLEFS_FPGA_DIR
Definition
ota.hpp:48
satnogs::comms::ota::finalize
void finalize(const fin_tlc &tlc, response_tlm &tlm, int wdgid)
Definition
ota.cpp:280
satnogs::comms::ota::reset_session
void reset_session(uint32_t id)
Definition
ota.cpp:54
satnogs::comms::ota::ota
ota(ota const &)=delete
satnogs::comms::ota::LITTLEFS_MCU_DIR
static constexpr const char * LITTLEFS_MCU_DIR
Definition
ota.hpp:47
satnogs::comms::ota::OTA_RETENTION_MEM_SIZE
static constexpr size_t OTA_RETENTION_MEM_SIZE
The available retention memory for the OTA service.
Definition
ota.hpp:216
satnogs::comms::ota::subsys
subsys
Destination target for a firmware file.
Definition
ota.hpp:67
satnogs::comms::ota::subsys::MCU_LITTLEFS
@ MCU_LITTLEFS
Definition
ota.hpp:72
satnogs::comms::ota::subsys::FPGA_LITTLEFS
@ FPGA_LITTLEFS
Definition
ota.hpp:74
satnogs::comms::ota::subsys::MCU_SLOT1
@ MCU_SLOT1
MCU MCUBoot Slot 1.
Definition
ota.hpp:69
satnogs::comms::ota::subsys::MCU_SLOT0
@ MCU_SLOT0
MCU MCUBoot Slot 0.
Definition
ota.hpp:68
satnogs::comms::ota::subsys::FPGA_WIC
@ FPGA_WIC
Definition
ota.hpp:70
satnogs::comms::ota::ota
ota()
Definition
ota.cpp:351
satnogs::comms::ota::get_instance
static ota & get_instance()
Definition
ota.hpp:51
satnogs::comms::ota::start
void start(const start_tlc &req, response_tlm &tlm)
Definition
ota.cpp:84
satnogs::comms::ota::MAX_FILE_LEN
static constexpr size_t MAX_FILE_LEN
The maximum supported filename length.
Definition
ota.hpp:45
satnogs::comms::ota::confirm_image
void confirm_image()
Sets the running firmware as confirmed.
Definition
ota.cpp:346
satnogs::comms::ota::operator=
void operator=(ota const &)=delete
satnogs::comms::ota::get_session_info
bool get_session_info(uint8_t id, session &out) const
Definition
ota.cpp:74
satnogs::comms::ota::reset_sessions
void reset_sessions()
Definition
ota.cpp:66
satnogs::comms::time
Time and position information.
Definition
time.hpp:60
satnogs::comms::time::time_src
time_src
Source of the reported time.
Definition
time.hpp:74
satnogs::comms
Definition
bsp.cpp:28
time.hpp
src
ota.hpp
Generated by
1.17.0