Skip to content

Commit eff8a59

Browse files
Merge sedsprintf_rs upstream main
2 parents c595bf0 + e9cfa52 commit eff8a59

File tree

10 files changed

+151
-69
lines changed

10 files changed

+151
-69
lines changed

sedsprintf_rs/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ debug = 0
6060
incremental = false
6161
overflow-checks = false
6262

63+
[profile.release.package."*"]
64+
opt-level = 3
65+
codegen-units = 1
66+
strip = "symbols"
67+
debug = 0
68+
incremental = false
69+
overflow-checks = false
70+
6371
# Release profile for embedded builds, size optimized
6472
[profile.release-embedded]
6573
inherits = "release"
@@ -70,4 +78,12 @@ panic = "abort"
7078
strip = "symbols"
7179
debug = 0
7280
incremental = false
81+
overflow-checks = false
82+
83+
[profile.release-embedded.package."*"]
84+
opt-level = "z"
85+
debug = 0
86+
strip = "symbols"
87+
codegen-units = 1
88+
incremental = false
7389
overflow-checks = false

sedsprintf_rs/src/c_api.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ fn view_to_packet(view: &SedsPacketView) -> Result<TelemetryPacket, ()> {
183183
}
184184

185185
// Sender as Arc<str>
186-
let sender_owned: Arc<str> = if view.sender.is_null() || view.sender_len == 0 {
187-
Arc::<str>::from("")
186+
let sender_owned: &str = if view.sender.is_null() || view.sender_len == 0 {
187+
""
188188
} else {
189189
let sb = unsafe { slice::from_raw_parts(view.sender as *const u8, view.sender_len) };
190190
let s = from_utf8(sb).map_err(|_| ())?;
191-
Arc::<str>::from(s)
191+
s
192192
};
193193

194194
// Payload bytes
@@ -211,7 +211,7 @@ fn view_to_packet(view: &SedsPacketView) -> Result<TelemetryPacket, ()> {
211211
/// - If the buffer is too small, writes as much as fits (NUL-terminated)
212212
/// and returns the required size.
213213
/// - On success, returns `SEDS_OK` (0).
214-
#[inline]
214+
#[inline]
215215
unsafe fn write_str_to_buf(s: &str, buf: *mut c_char, buf_len: usize) -> i32 {
216216
if buf.is_null() && buf_len != 0 {
217217
return status_from_err(TelemetryError::BadArg);
@@ -796,8 +796,9 @@ pub extern "C" fn seds_router_log_typed_ex(
796796
(SEDS_EK_SIGNED, 2) => finish_with::<i16>(r, ty, ts, queue, &padded, required_elems, 2),
797797
(SEDS_EK_SIGNED, 4) => finish_with::<i32>(r, ty, ts, queue, &padded, required_elems, 4),
798798
(SEDS_EK_SIGNED, 8) => finish_with::<i64>(r, ty, ts, queue, &padded, required_elems, 8),
799-
(SEDS_EK_SIGNED, 16) => finish_with::<i128>(r, ty, ts, queue, &padded, required_elems, 16),
800-
799+
(SEDS_EK_SIGNED, 16) => {
800+
finish_with::<i128>(r, ty, ts, queue, &padded, required_elems, 16)
801+
}
801802

802803
(SEDS_EK_FLOAT, 4) => finish_with::<f32>(r, ty, ts, queue, &padded, required_elems, 4),
803804
(SEDS_EK_FLOAT, 8) => finish_with::<f64>(r, ty, ts, queue, &padded, required_elems, 8),
@@ -819,7 +820,6 @@ pub extern "C" fn seds_router_log_typed_ex(
819820
(SEDS_EK_SIGNED, 8) => do_vec_log_typed!(r, ty, ts, queue, data, count, i64),
820821
(SEDS_EK_SIGNED, 16) => do_vec_log_typed!(r, ty, ts, queue, data, count, i128),
821822

822-
823823
(SEDS_EK_FLOAT, 4) => do_vec_log_typed!(r, ty, ts, queue, data, count, f32),
824824
(SEDS_EK_FLOAT, 8) => do_vec_log_typed!(r, ty, ts, queue, data, count, f64),
825825

@@ -1350,7 +1350,9 @@ pub extern "C" fn seds_pkt_get_typed(
13501350
(SEDS_EK_SIGNED, 2) => extract_typed_into::<i16>(view, elem_size, needed, out as *mut i16),
13511351
(SEDS_EK_SIGNED, 4) => extract_typed_into::<i32>(view, elem_size, needed, out as *mut i32),
13521352
(SEDS_EK_SIGNED, 8) => extract_typed_into::<i64>(view, elem_size, needed, out as *mut i64),
1353-
(SEDS_EK_SIGNED, 16) => extract_typed_into::<i128>(view, elem_size, needed, out as *mut i128),
1353+
(SEDS_EK_SIGNED, 16) => {
1354+
extract_typed_into::<i128>(view, elem_size, needed, out as *mut i128)
1355+
}
13541356

13551357
(SEDS_EK_FLOAT, 4) => extract_typed_into::<f32>(view, elem_size, needed, out as *mut f32),
13561358
(SEDS_EK_FLOAT, 8) => extract_typed_into::<f64>(view, elem_size, needed, out as *mut f64),

sedsprintf_rs/src/config.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub enum DataEndpoint {
6262
SdCard,
6363
GroundStation,
6464
FlightController,
65+
FuelBoard,
6566
}
6667

6768
impl DataEndpoint {
@@ -75,6 +76,7 @@ impl DataEndpoint {
7576
DataEndpoint::SdCard => "SD_CARD",
7677
DataEndpoint::GroundStation => "GROUND_STATION",
7778
DataEndpoint::FlightController => "FLIGHT_CONTROLLER",
79+
DataEndpoint::FuelBoard => "FUEL_BOARD",
7880
}
7981
}
8082
}
@@ -107,6 +109,9 @@ pub enum DataType {
107109
BatteryVoltage,
108110
BatteryCurrent,
109111
BarometerData,
112+
Abort,
113+
FuelFlow,
114+
FuelTankPressure,
110115
/// Generic string message payload.
111116
MessageData,
112117
}
@@ -127,6 +132,9 @@ impl DataType {
127132
DataType::BatteryVoltage => "BATTERY_VOLTAGE",
128133
DataType::BatteryCurrent => "BATTERY_CURRENT",
129134
DataType::BarometerData => "BAROMETER_DATA",
135+
DataType::Abort => "ABORT",
136+
DataType::FuelFlow => "FUEL_FLOW",
137+
DataType::FuelTankPressure => "FUEL_TANK_PRESSURE",
130138
DataType::MessageData => "MESSAGE_DATA",
131139
}
132140
}
@@ -156,6 +164,9 @@ pub const fn get_message_data_type(data_type: DataType) -> MessageDataType {
156164
DataType::GyroData => MessageDataType::Float32,
157165
DataType::AccelData => MessageDataType::Float32,
158166
DataType::BarometerData => MessageDataType::Float32,
167+
DataType::Abort => MessageDataType::Bool,
168+
DataType::FuelFlow => MessageDataType::Float32,
169+
DataType::FuelTankPressure => MessageDataType::Float32,
159170
DataType::MessageData => MessageDataType::String,
160171
}
161172
}
@@ -175,6 +186,9 @@ pub const fn get_message_info_types(message_type: DataType) -> MessageType {
175186
DataType::GyroData => MessageType::Info,
176187
DataType::AccelData => MessageType::Info,
177188
DataType::BarometerData => MessageType::Info,
189+
DataType::Abort => MessageType::Error,
190+
DataType::FuelFlow => MessageType::Info,
191+
DataType::FuelTankPressure => MessageType::Info,
178192
DataType::MessageData => MessageType::Info,
179193
}
180194
}
@@ -208,7 +222,11 @@ pub const fn get_message_meta(data_type: DataType) -> MessageMeta {
208222
MessageMeta {
209223
// GPS Data
210224
element_count: MessageElementCount::Static(2), // GPS Data messages carry 3 float32 elements (latitude, longitude)
211-
endpoints: &[DataEndpoint::GroundStation, DataEndpoint::SdCard, DataEndpoint::FlightController],
225+
endpoints: &[
226+
DataEndpoint::GroundStation,
227+
DataEndpoint::SdCard,
228+
DataEndpoint::FlightController,
229+
],
212230
}
213231
}
214232
DataType::KalmanFilterData => {
@@ -235,7 +253,7 @@ pub const fn get_message_meta(data_type: DataType) -> MessageMeta {
235253
DataType::GyroData => {
236254
MessageMeta {
237255
// System Status
238-
element_count: MessageElementCount::Static(1), // Gyro data messages carry 3 float32 elements (Gyro x, y,z)
256+
element_count: MessageElementCount::Static(3), // Gyro data messages carry 3 float32 elements (Gyro x, y,z)
239257
endpoints: &[DataEndpoint::SdCard, DataEndpoint::GroundStation],
240258
}
241259
}
@@ -253,6 +271,32 @@ pub const fn get_message_meta(data_type: DataType) -> MessageMeta {
253271
endpoints: &[DataEndpoint::GroundStation, DataEndpoint::SdCard],
254272
}
255273
}
274+
DataType::Abort => {
275+
MessageMeta {
276+
// Abort Command
277+
element_count: MessageElementCount::Static(1), // Abort messages carry 1 boolean element
278+
endpoints: &[
279+
DataEndpoint::SdCard,
280+
DataEndpoint::GroundStation,
281+
DataEndpoint::FlightController,
282+
DataEndpoint::FuelBoard,
283+
],
284+
}
285+
}
286+
DataType::FuelFlow => {
287+
MessageMeta {
288+
// Fuel Flow Data
289+
element_count: MessageElementCount::Static(1), // Fuel Flow messages carry 1 float32 element
290+
endpoints: &[DataEndpoint::GroundStation, DataEndpoint::SdCard],
291+
}
292+
}
293+
DataType::FuelTankPressure => {
294+
MessageMeta {
295+
// Fuel Tank Pressure Data
296+
element_count: MessageElementCount::Static(1), // Fuel Tank Pressure messages carry 1 float32 element
297+
endpoints: &[DataEndpoint::GroundStation, DataEndpoint::SdCard],
298+
}
299+
}
256300
DataType::MessageData => {
257301
MessageMeta {
258302
// Message Data

sedsprintf_rs/src/lib.rs

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,20 @@ extern crate alloc;
2929
extern crate core;
3030
#[cfg(feature = "std")]
3131
extern crate std;
32-
33-
34-
use core::mem::size_of;
35-
use core::ops::Mul;
36-
use strum::EnumCount;
32+
#[cfg(feature = "std")]
33+
use std::io::Error;
3734

3835
use crate::config::{
3936
get_message_data_type, get_message_info_types, get_message_meta, DataEndpoint, DataType,
4037
MAX_STATIC_HEX_LENGTH, MAX_STATIC_STRING_LENGTH,
4138
};
4239
use crate::macros::{ReprI32Enum, ReprU32Enum};
40+
use alloc::string::ToString;
41+
use alloc::sync::Arc;
42+
use core::fmt::Formatter;
43+
use core::mem::size_of;
44+
use core::ops::Mul;
45+
use strum::EnumCount;
4346

4447
// ============================================================================
4548
// Test / Python FFI modules (std-only)
@@ -69,7 +72,6 @@ unsafe extern "C" {
6972
mod embedded_alloc {
7073
use core::alloc::{GlobalAlloc, Layout};
7174

72-
7375
unsafe extern "C" {
7476
fn telemetryMalloc(size: usize) -> *mut core::ffi::c_void;
7577
fn telemetryFree(ptr: *mut core::ffi::c_void);
@@ -94,11 +96,9 @@ mod embedded_alloc {
9496
#[global_allocator]
9597
static A: TelemetryAlloc = TelemetryAlloc;
9698

97-
9899
// Panic handler for embedded
99100
use core::panic::PanicInfo;
100101

101-
102102
#[panic_handler]
103103
fn panic(_info: &PanicInfo) -> ! {
104104
// Halt forever after panic
@@ -315,8 +315,11 @@ pub enum MessageType {
315315
///
316316
/// Most public APIs expose a `TelemetryResult<T>` alias for
317317
/// `Result<T, TelemetryError>`.
318-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
318+
#[derive(Debug, Clone, PartialEq, Eq)]
319319
pub enum TelemetryError {
320+
/// Generic / unspecified error.
321+
GenericError(Option<Arc<str>>),
322+
320323
/// Logical type ID is not a valid [`DataType`].
321324
InvalidType,
322325

@@ -359,6 +362,7 @@ impl TelemetryError {
359362
/// used by the FFI layers.
360363
pub const fn to_error_code(&self) -> TelemetryErrorCode {
361364
match self {
365+
TelemetryError::GenericError(_) => TelemetryErrorCode::GenericError,
362366
TelemetryError::InvalidType => TelemetryErrorCode::InvalidType,
363367
TelemetryError::SizeMismatch { .. } => TelemetryErrorCode::SizeMismatch,
364368
TelemetryError::SizeMismatchError => TelemetryErrorCode::SizeMismatchError,
@@ -375,25 +379,60 @@ impl TelemetryError {
375379
}
376380
}
377381

382+
/// Allow conversion of `TelemetryError` to human-readable string.
383+
impl core::fmt::Display for TelemetryError {
384+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
385+
f.write_str(&TelemetryError::to_string(self))
386+
}
387+
}
388+
389+
/// Implement `std::error::Error` for `TelemetryError` when `std` is enabled.
390+
#[cfg(feature = "std")]
391+
impl std::error::Error for TelemetryError {
392+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
393+
None
394+
}
395+
}
396+
397+
/// Allow the conversion from std error to telemetry error
398+
#[cfg(feature = "std")]
399+
impl From<Error> for TelemetryError {
400+
fn from(error: Error) -> Self {
401+
let str = error.to_string();
402+
let astr: Arc<str> = Arc::from(str.as_str());
403+
TelemetryError::GenericError(Some(astr))
404+
}
405+
}
406+
407+
#[cfg(feature = "std")]
408+
impl From<Box<dyn std::error::Error>> for TelemetryError {
409+
fn from(err: Box<dyn std::error::Error>) -> Self {
410+
let str = err.to_string();
411+
let astr: Arc<str> = Arc::from(str.as_str());
412+
TelemetryError::GenericError(Some(astr))
413+
}
414+
}
415+
378416
/// Numeric error codes used on the C/Python FFI boundary.
379417
///
380418
/// Negative values are used to avoid collisions with success codes
381419
/// and other positive return values (e.g. lengths).
382420
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
383421
#[repr(i32)]
384422
pub enum TelemetryErrorCode {
385-
InvalidType = -2,
386-
SizeMismatch = -3,
387-
SizeMismatchError = -4,
388-
EmptyEndpoints = -5,
389-
TimestampInvalid = -6,
390-
MissingPayload = -7,
391-
HandlerError = -8,
392-
BadArg = -9,
393-
Serialize = -10,
394-
Deserialize = -11,
395-
Io = -12,
396-
InvalidUtf8 = -13,
423+
GenericError = -2,
424+
InvalidType = -3,
425+
SizeMismatch = -4,
426+
SizeMismatchError = -5,
427+
EmptyEndpoints = -6,
428+
TimestampInvalid = -7,
429+
MissingPayload = -8,
430+
HandlerError = -9,
431+
BadArg = -10,
432+
Serialize = -11,
433+
Deserialize = -12,
434+
Io = -13,
435+
InvalidUtf8 = -14,
397436
}
398437

399438
// Generate ReprI32Enum helpers for TelemetryErrorCode
@@ -416,6 +455,7 @@ impl TelemetryErrorCode {
416455
#[inline]
417456
pub fn as_str(&self) -> &'static str {
418457
match self {
458+
TelemetryErrorCode::GenericError => "GenericError",
419459
TelemetryErrorCode::InvalidType => "{Invalid Type}",
420460
TelemetryErrorCode::SizeMismatch => "{Size Mismatch}",
421461
TelemetryErrorCode::SizeMismatchError => "{Size Mismatch Error}",

sedsprintf_rs/src/python_api.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -920,14 +920,8 @@ pub fn make_packet(
920920
let payload_arc = AArc::<[u8]>::from(buf);
921921

922922
// Go through TelemetryPacket::new for validation + consistency
923-
let pkt = TelemetryPacket::new(
924-
ty,
925-
&eps,
926-
AArc::<str>::from(sender),
927-
timestamp_ms,
928-
payload_arc,
929-
)
930-
.map_err(py_err_from)?;
923+
let pkt =
924+
TelemetryPacket::new(ty, &eps, sender, timestamp_ms, payload_arc).map_err(py_err_from)?;
931925

932926
Ok(Py::new(py, PyPacket { inner: pkt })?.into_any())
933927
}

sedsprintf_rs/src/router.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use alloc::{boxed::Box, collections::VecDeque, format, sync::Arc, vec, vec::Vec}
1111
#[cfg(all(not(feature = "std"), target_os = "none"))]
1212
use crate::seds_error_msg;
1313

14-
1514
pub enum RxItem {
1615
Packet(TelemetryPacket),
1716
Serialized(Arc<[u8]>),
@@ -733,7 +732,7 @@ impl Router {
733732
let error_pkt = TelemetryPacket::new(
734733
DataType::TelemetryError,
735734
&locals,
736-
env.sender.clone(),
735+
&*env.sender.clone(),
737736
env.timestamp_ms,
738737
payload,
739738
)?;

0 commit comments

Comments
 (0)