SatNOGS-COMMS  4.1.0
A COMMS subsystem for CubeSats
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
27namespace satnogs::comms
28{
29
37class ota
38{
39public:
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 &
52 {
53 static ota instance;
54 return instance;
55 }
56
57 ota(ota const &) = delete;
58
59 void
60 operator=(ota const &) = delete;
61
77
94
96 {
97 public:
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
106 };
107
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
119 {
120 public:
121 uint8_t session;
122 uint8_t method;
123 uint32_t delay_secs;
124
126 };
127
129 {
130 public:
131 uint8_t session;
133 uint32_t ack;
134
136 : session(CONFIG_OTA_MAX_SESSIONS), s(status::INVALID_STATE), ack(0)
137 {
138 }
139 };
140
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;
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
167 uint8_t slot;
168 bool active;
170 uint32_t total_size;
171 uint32_t ack;
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
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
195
196 bool
197 get_session_info(uint8_t id, session &out) const;
198
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
209protected:
216 static constexpr size_t OTA_RETENTION_MEM_SIZE = 1752;
217 ota();
218
219private:
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
A class representing error messages in the SatNOGS-COMMS system.
Definition exception.hpp:83
Exception base class.
Definition exception.hpp:63
severity
Severity levels of exceptions.
Definition exception.hpp:71
@ MINOR
Failure having minimal impact.
Definition exception.hpp:75
exception(severity sev, const char *file, int lineno, const error_msg &err_msg)
Constructor for the exception class.
const uint8_t * data
Definition ota.hpp:113
inval_session(const char *file_name, int line)
Definition ota.hpp:202
etl::string< MAX_FILE_LEN > name
Definition ota.hpp:174
static constexpr size_t HASH
Definition ota.hpp:151
time::time_src time_src
Definition ota.hpp:172
etl::string< MAX_FILE_LEN > name
Definition ota.hpp:103
void recv(const data_tlc &tlc, response_tlm &tlm)
Definition ota.cpp:214
static constexpr const char * LITTLEFS_FPGA_DIR
Definition ota.hpp:48
void finalize(const fin_tlc &tlc, response_tlm &tlm, int wdgid)
Definition ota.cpp:280
void reset_session(uint32_t id)
Definition ota.cpp:54
ota(ota const &)=delete
static constexpr const char * LITTLEFS_MCU_DIR
Definition ota.hpp:47
static constexpr size_t OTA_RETENTION_MEM_SIZE
The available retention memory for the OTA service.
Definition ota.hpp:216
subsys
Destination target for a firmware file.
Definition ota.hpp:67
@ MCU_SLOT1
MCU MCUBoot Slot 1.
Definition ota.hpp:69
@ MCU_SLOT0
MCU MCUBoot Slot 0.
Definition ota.hpp:68
static ota & get_instance()
Definition ota.hpp:51
void start(const start_tlc &req, response_tlm &tlm)
Definition ota.cpp:84
static constexpr size_t MAX_FILE_LEN
The maximum supported filename length.
Definition ota.hpp:45
void confirm_image()
Sets the running firmware as confirmed.
Definition ota.cpp:346
void operator=(ota const &)=delete
bool get_session_info(uint8_t id, session &out) const
Definition ota.cpp:74
void reset_sessions()
Definition ota.cpp:66
Time and position information.
Definition time.hpp:60
time_src
Source of the reported time.
Definition time.hpp:74