Skip to content

Commit c3a21cd

Browse files
Merge sedsprintf_rs upstream main
2 parents b7b46ce + 2d206c8 commit c3a21cd

File tree

7 files changed

+77
-61
lines changed

7 files changed

+77
-61
lines changed

sedsprintf_rs/src/c_api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ pub extern "C" fn seds_router_receive_serialized(
928928
}
929929
let router = unsafe { &(*r).inner }; // shared borrow
930930
let slice = unsafe { slice::from_raw_parts(bytes, len) };
931-
ok_or_status(router.receive_serialized(slice))
931+
ok_or_status(router.rx_serialized(slice))
932932
}
933933

934934
/// Feed a packet view (constructed on the C side) into the router RX path.
@@ -942,7 +942,7 @@ pub extern "C" fn seds_router_receive(r: *mut SedsRouter, view: *const SedsPacke
942942
Ok(p) => p,
943943
Err(_) => return status_from_err(TelemetryError::InvalidType),
944944
};
945-
ok_or_status(router.receive(&pkt))
945+
ok_or_status(router.rx(&pkt))
946946
}
947947

948948
/// Transmit a packet view via the router’s TX queue immediately.
@@ -959,7 +959,7 @@ pub extern "C" fn seds_router_transmit_message_queue(
959959
Ok(p) => p,
960960
Err(_) => return status_from_err(TelemetryError::InvalidType),
961961
};
962-
ok_or_status(router.transmit_message_queue(pkt))
962+
ok_or_status(router.tx_queue(pkt))
963963
}
964964

965965
// ----- Queue processing (no time budget) -----

sedsprintf_rs/src/config.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#[allow(unused_imports)]
1212
use crate::{MessageDataType, MessageElementCount, MessageMeta, MessageType, STRING_VALUE_ELEMENT};
1313
use strum_macros::EnumCount;
14-
14+
use crate::EndpointsBroadcastMode;
1515
// -----------------------------------------------------------------------------
1616
// User-editable configuration
1717
// -----------------------------------------------------------------------------
@@ -72,7 +72,7 @@ impl DataEndpoint {
7272
///
7373
/// This should remain stable over time for compatibility with tests and
7474
/// external tooling.
75-
pub fn as_str(self) -> &'static str {
75+
pub fn as_str(&self) -> &'static str {
7676
match self {
7777
DataEndpoint::SdCard => "SD_CARD",
7878
DataEndpoint::GroundStation => "GROUND_STATION",
@@ -81,6 +81,16 @@ impl DataEndpoint {
8181
DataEndpoint::Abort => "ABORT",
8282
}
8383
}
84+
85+
pub fn get_broadast_mode(&self) -> EndpointsBroadcastMode{
86+
match self {
87+
DataEndpoint::SdCard => EndpointsBroadcastMode::Default,
88+
DataEndpoint::GroundStation => EndpointsBroadcastMode::Default,
89+
DataEndpoint::FlightController => EndpointsBroadcastMode::Default,
90+
DataEndpoint::FuelBoard => EndpointsBroadcastMode::Default,
91+
DataEndpoint::Abort => EndpointsBroadcastMode::Always,
92+
}
93+
}
8494
}
8595

8696
// -----------------------------------------------------------------------------

sedsprintf_rs/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ impl_repr_u32_enum!(DataEndpoint, MAX_VALUE_DATA_ENDPOINT);
150150
// ============================================================================
151151

152152
/// Describes how many elements are present for a given message type.
153-
#[allow(dead_code)]
154153
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
155154
pub enum MessageElementCount {
156155
/// Fixed number of elements.
@@ -159,6 +158,12 @@ pub enum MessageElementCount {
159158
Dynamic,
160159
}
161160

161+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
162+
pub enum EndpointsBroadcastMode {
163+
Always,
164+
Never,
165+
Default,
166+
}
162167
/// Static metadata for a message type: element count and valid endpoints.
163168
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
164169
pub struct MessageMeta {
@@ -182,7 +187,8 @@ pub fn message_meta(ty: DataType) -> MessageMeta {
182187

183188
impl Mul<MessageElementCount> for usize {
184189
type Output = usize;
185-
190+
191+
#[inline]
186192
fn mul(self, rhs: MessageElementCount) -> usize {
187193
self * rhs.into()
188194
}
@@ -191,6 +197,7 @@ impl Mul<MessageElementCount> for usize {
191197
impl Mul<usize> for MessageElementCount {
192198
type Output = usize;
193199

200+
#[inline]
194201
fn mul(self, rhs: usize) -> usize {
195202
self.into() * rhs
196203
}
@@ -201,6 +208,7 @@ impl MessageElementCount {
201208
///
202209
/// - `Static(n)` → `n`
203210
/// - `Dynamic` → `0` (caller must handle dynamic sizing separately)
211+
#[inline]
204212
fn into(self) -> usize {
205213
match self {
206214
MessageElementCount::Static(a) => a,

sedsprintf_rs/src/router.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
use crate::{
2-
config::{DataEndpoint, DataType, DEVICE_IDENTIFIER, MAX_HANDLER_RETRIES}, get_needed_message_size, impl_letype_num,
3-
lock::RouterMutex,
4-
message_meta, serialize,
5-
telemetry_packet::TelemetryPacket,
6-
MessageElementCount, TelemetryError,
7-
TelemetryResult,
8-
};
1+
use crate::{config::{DataEndpoint, DataType, DEVICE_IDENTIFIER, MAX_HANDLER_RETRIES}, get_needed_message_size, impl_letype_num, lock::RouterMutex, message_meta, serialize, telemetry_packet::TelemetryPacket, EndpointsBroadcastMode, MessageElementCount, TelemetryError, TelemetryResult};
92
use alloc::{boxed::Box, collections::VecDeque, format, sync::Arc, vec, vec::Vec};
103

114
#[cfg(all(not(feature = "std"), target_os = "none"))]
@@ -357,7 +350,7 @@ impl Router {
357350
payload,
358351
)?;
359352

360-
self.transmit_message(&error_pkt)
353+
self.tx(&error_pkt)
361354
}
362355

363356
// ---------- PUBLIC API: now all &self (thread-safe via internal locking) ----------
@@ -374,7 +367,7 @@ impl Router {
374367
st.transmit_queue.pop_front()
375368
};
376369
let Some(pkt) = pkt_opt else { break };
377-
self.transmit_message(&pkt)?; // No lock held while calling user code
370+
self.tx(&pkt)?; // No lock held while calling user code
378371
}
379372
Ok(())
380373
}
@@ -430,7 +423,7 @@ impl Router {
430423
st.transmit_queue.pop_front()
431424
};
432425
let Some(pkt) = pkt_opt else { break };
433-
self.transmit_message(&pkt)?;
426+
self.tx(&pkt)?;
434427
if self.clock.now_ms().wrapping_sub(start) >= timeout_ms as u64 {
435428
break;
436429
}
@@ -444,7 +437,7 @@ impl Router {
444437
/// # Returns
445438
/// A TelemetryResult indicating success or failure.
446439
fn process_rx_queue_item(&self, item: RxItem) -> TelemetryResult<()> {
447-
self.receive_item(&item)
440+
self.rx_item(&item)
448441
}
449442

450443
/// Process packets in the receive queue for up to `timeout_ms` milliseconds.
@@ -491,7 +484,7 @@ impl Router {
491484
let mut st = self.state.lock();
492485
st.transmit_queue.pop_front()
493486
} {
494-
self.transmit_message(&pkt)?;
487+
self.tx(&pkt)?;
495488
did_any = true;
496489
}
497490
if !drain_fully && self.clock.now_ms().wrapping_sub(start) >= timeout_ms as u64 {
@@ -525,7 +518,7 @@ impl Router {
525518
/// * `pkt` - The TelemetryPacket to enqueue.
526519
/// # Returns
527520
/// A TelemetryResult indicating success or failure.
528-
pub fn transmit_message_queue(&self, pkt: TelemetryPacket) -> TelemetryResult<()> {
521+
pub fn tx_queue(&self, pkt: TelemetryPacket) -> TelemetryResult<()> {
529522
pkt.validate()?;
530523
let mut st = self.state.lock();
531524
st.transmit_queue.push_back(pkt);
@@ -736,7 +729,7 @@ impl Router {
736729
env.timestamp_ms,
737730
payload,
738731
)?;
739-
self.transmit_message(&error_pkt)
732+
self.tx(&error_pkt)
740733
}
741734

742735
/// Core receive function handling both Packet and Serialized RxItems.
@@ -745,7 +738,7 @@ impl Router {
745738
/// * `item` - The RxItem to process (either Packet or Serialized).
746739
/// # Returns
747740
/// A TelemetryResult indicating success or failure.
748-
pub fn receive_item(&self, item: &RxItem) -> TelemetryResult<()> {
741+
pub fn rx_item(&self, item: &RxItem) -> TelemetryResult<()> {
749742
match item {
750743
RxItem::Packet(pkt) => {
751744
pkt.validate()?;
@@ -832,7 +825,7 @@ impl Router {
832825
/// * `pkt` - The TelemetryPacket to transmit.
833826
/// # Returns
834827
/// A TelemetryResult indicating success or failure.
835-
pub fn transmit_message(&self, pkt: &TelemetryPacket) -> TelemetryResult<()> {
828+
pub fn tx(&self, pkt: &TelemetryPacket) -> TelemetryResult<()> {
836829
pkt.validate()?;
837830

838831
let has_serialized_local = pkt
@@ -843,7 +836,7 @@ impl Router {
843836
let send_remote = pkt
844837
.endpoints()
845838
.iter()
846-
.any(|e| !self.cfg.is_local_endpoint(*e));
839+
.any(|e| (!self.cfg.is_local_endpoint(*e) && e.get_broadast_mode() != EndpointsBroadcastMode::Never) || e.get_broadast_mode() == EndpointsBroadcastMode::Always);
847840

848841
// Only serialize if needed.
849842
let bytes_opt = if has_serialized_local || send_remote {
@@ -892,9 +885,9 @@ impl Router {
892885
/// * `bytes` - The serialized telemetry packet as a byte slice.
893886
/// # Returns
894887
/// A TelemetryResult indicating success or failure.
895-
pub fn receive_serialized(&self, bytes: &[u8]) -> TelemetryResult<()> {
888+
pub fn rx_serialized(&self, bytes: &[u8]) -> TelemetryResult<()> {
896889
let item = RxItem::Serialized(Arc::from(bytes));
897-
self.receive_item(&item)
890+
self.rx_item(&item)
898891
}
899892

900893
/// Enqueue and process a telemetry packet.
@@ -903,9 +896,9 @@ impl Router {
903896
/// * `pkt` - The TelemetryPacket to process.
904897
/// # Returns
905898
/// A TelemetryResult indicating success or failure.
906-
pub fn receive(&self, pkt: &TelemetryPacket) -> TelemetryResult<()> {
899+
pub fn rx(&self, pkt: &TelemetryPacket) -> TelemetryResult<()> {
907900
let item = RxItem::Packet(pkt.clone());
908-
self.receive_item(&item)
901+
self.rx_item(&item)
909902
}
910903

911904
/// Build a packet then send immediately.
@@ -916,7 +909,7 @@ impl Router {
916909
/// A TelemetryResult indicating success or failure.
917910
pub fn log<T: LeBytes>(&self, ty: DataType, data: &[T]) -> TelemetryResult<()> {
918911
log_raw(self.sender, ty, data, self.clock.now_ms(), |pkt| {
919-
self.transmit_message(&pkt)
912+
self.tx(&pkt)
920913
})
921914
}
922915

@@ -928,7 +921,7 @@ impl Router {
928921
/// A TelemetryResult indicating success or failure.
929922
pub fn log_queue<T: LeBytes>(&self, ty: DataType, data: &[T]) -> TelemetryResult<()> {
930923
log_raw(self.sender, ty, data, self.clock.now_ms(), |pkt| {
931-
self.transmit_message_queue(pkt)
924+
self.tx_queue(pkt)
932925
})
933926
}
934927

@@ -946,7 +939,7 @@ impl Router {
946939
data: &[T],
947940
) -> TelemetryResult<()> {
948941
log_raw(self.sender, ty, data, timestamp, |pkt| {
949-
self.transmit_message(&pkt)
942+
self.tx(&pkt)
950943
})
951944
}
952945

@@ -964,7 +957,7 @@ impl Router {
964957
data: &[T],
965958
) -> TelemetryResult<()> {
966959
log_raw(self.sender, ty, data, timestamp, |pkt| {
967-
self.transmit_message_queue(pkt)
960+
self.tx_queue(pkt)
968961
})
969962
}
970963
}

sedsprintf_rs/src/tests.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ mod handler_failure_tests {
525525
)
526526
.unwrap();
527527

528-
handle_errors(router.transmit_message(&pkt));
528+
handle_errors(router.tx(&pkt));
529529

530530
// The capturing handler should have seen the original packet and then the error packet.
531531
assert!(
@@ -588,7 +588,7 @@ mod handler_failure_tests {
588588
)
589589
.unwrap();
590590

591-
handle_errors(router.transmit_message(&pkt));
591+
handle_errors(router.tx(&pkt));
592592

593593
assert!(
594594
saw_error.load(Ordering::SeqCst) >= 1,
@@ -1281,7 +1281,7 @@ mod tests_extra {
12811281
)
12821282
.unwrap();
12831283

1284-
r.transmit_message_queue(pkt_tx).unwrap();
1284+
r.tx_queue(pkt_tx).unwrap();
12851285
r.rx_packet_to_queue(pkt_rx).unwrap();
12861286

12871287
// Clearing should drop both queues before any processing.
@@ -1339,7 +1339,7 @@ mod tests_extra {
13391339
.unwrap();
13401340

13411341
// Sending should surface a HandlerError after all retries.
1342-
let res = r.transmit_message(&pkt);
1342+
let res = r.tx(&pkt);
13431343
match res {
13441344
Err(TelemetryError::HandlerError(_)) => {}
13451345
other => panic!("expected HandlerError after retries, got {other:?}"),
@@ -1459,7 +1459,7 @@ mod tests_extra {
14591459
)
14601460
.unwrap();
14611461

1462-
let res = r.transmit_message(&pkt);
1462+
let res = r.tx(&pkt);
14631463
match res {
14641464
Err(TelemetryError::HandlerError(_)) => {} // TX path wraps as HandlerError
14651465
other => panic!("expected HandlerError from TX failure, got {other:?}"),
@@ -1631,7 +1631,7 @@ mod tests_more {
16311631
BoardConfig::new(vec![handler]),
16321632
zero_clock(),
16331633
);
1634-
r.receive_serialized(&wire).unwrap();
1634+
r.rx_serialized(&wire).unwrap();
16351635
assert_eq!(called.load(Ordering::SeqCst), 1);
16361636
}
16371637

@@ -1669,7 +1669,7 @@ mod tests_more {
16691669
zero_clock(),
16701670
);
16711671

1672-
r.receive_serialized(&wire).unwrap();
1672+
r.rx_serialized(&wire).unwrap();
16731673
assert_eq!(packet_called.load(Ordering::SeqCst), 1);
16741674
assert_eq!(serialized_called.load(Ordering::SeqCst), 1);
16751675
}
@@ -1704,7 +1704,7 @@ mod tests_more {
17041704
0,
17051705
)
17061706
.unwrap();
1707-
r.transmit_message(&pkt).unwrap();
1707+
r.tx(&pkt).unwrap();
17081708

17091709
assert_eq!(tx_called.load(Ordering::SeqCst), 0);
17101710
assert_eq!(hits.load(Ordering::SeqCst), 1);
@@ -1733,7 +1733,7 @@ mod tests_more {
17331733
0,
17341734
)
17351735
.unwrap();
1736-
r.receive(&pkt).unwrap();
1736+
r.rx(&pkt).unwrap();
17371737

17381738
assert_eq!(called.load(Ordering::SeqCst), 1);
17391739
}
@@ -1769,7 +1769,7 @@ mod tests_more {
17691769
1,
17701770
)
17711771
.unwrap();
1772-
let _ = r.transmit_message(&pkt);
1772+
let _ = r.tx(&pkt);
17731773

17741774
let s = captured.lock().unwrap().clone();
17751775
assert!(!s.is_empty());
@@ -2085,7 +2085,7 @@ mod concurrency_tests {
20852085
threads_vec.push(thread::spawn(move || {
20862086
for _ in 0..ITERS_PER_THREAD {
20872087
r_cloned
2088-
.receive_serialized(&w_cloned)
2088+
.rx_serialized(&w_cloned)
20892089
.expect("receive_serialized failed");
20902090
}
20912091
}));

0 commit comments

Comments
 (0)