Skip to content

Commit

Permalink
Initial rework to mpsc channels
Browse files Browse the repository at this point in the history
Start of breaking the connection between channels and the router. This
allows profiles to be a lot more flexible in their execution domain and
order. It also breaks a lot of borrowing limitations being impossed.

Future work will expose traits so any type of mpsc channel can be used.
For initial implemenation thingbuf will be used.
  • Loading branch information
cujomalainey committed May 18, 2024
1 parent 0644b1a commit 8e1e7ce
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 85 deletions.
7 changes: 7 additions & 0 deletions ant/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ pub trait Channel {
/// Assign channel from associated router or manually if not using a router
fn set_channel(&mut self, channel: ChannelAssignment);
}

// TODO add a send and get response
//
// Logically since this is single threaded, if we send and recieve in the same call, all
// messages that may come inbetween send and recieve have no consequence on the code flow. The
// only challenge will be handling ownership since we will likely be holding the sender in a
// mutable state and if they recieve another message it will be a problem
24 changes: 12 additions & 12 deletions ant/src/messages/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ impl ConfigureAdvancedBurstData {
}

/// Represents a Configure Advanced Burst message (0x78)
#[derive(Clone, Debug, Default, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ConfigureAdvancedBurst {
/// Required Fields
pub data: ConfigureAdvancedBurstData,
Expand Down Expand Up @@ -830,7 +830,7 @@ impl ConfigureAdvancedBurst {

/// Represents a Configure Event Filter message (0x79)
#[allow(clippy::too_many_arguments)]
#[derive(PackedStruct, AntTx, new, Clone, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "3")]
pub struct ConfigureEventFilter {
#[new(default)]
Expand Down Expand Up @@ -872,7 +872,7 @@ pub struct ConfigureEventFilter {
}

/// Represents a Configure Selective Data Updates message (0x7A)
#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "2")]
pub struct ConfigureSelectiveDataUpdates {
/// Channel to be configured
Expand All @@ -886,7 +886,7 @@ pub struct ConfigureSelectiveDataUpdates {
// TODO test

/// Represents a Set Selective Data Update Mask message (0x7B)
#[derive(PackedStruct, AntTx, new, Clone, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "9")]
pub struct SetSelectiveDataUpdateMask {
/// Mask to updated
Expand Down Expand Up @@ -914,7 +914,7 @@ pub enum EncryptionMode {
}

/// Represents a Enable Single Channel Encryption message (0x7D)
#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "4")]
pub struct EnableSingleChannelEncryption {
/// Channel to be configured
Expand All @@ -932,7 +932,7 @@ pub struct EnableSingleChannelEncryption {
pub decimation_rate: u8,
}

#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "17")]
pub struct SetEncryptionKey {
/// Per version 5.1 of the spec this field has a range of 0
Expand All @@ -946,7 +946,7 @@ pub struct SetEncryptionKey {
// The spec defines this as a single variable message but variable types are
// basically impossible with the packed_stuct lib so it is easier to just
// implement 3 message types to handle all the cases.
#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "5")]
pub struct SetEncryptionInfoEncryptionId {
// 0 for encryption id
Expand All @@ -957,7 +957,7 @@ pub struct SetEncryptionInfoEncryptionId {
pub encryption_id: EncryptionId,
}

#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "20")]
pub struct SetEncryptionInfoUserInformationString {
// 1 for User Information String
Expand All @@ -971,7 +971,7 @@ pub struct SetEncryptionInfoUserInformationString {
pub user_information_string: UserInformationString,
}

#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "17")]
pub struct SetEncryptionInfoRandomSeed {
// 2 for Random Number Seed
Expand All @@ -988,7 +988,7 @@ pub struct SetEncryptionInfoRandomSeed {
pub random_seed: [u8; 16],
}

#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "2")]
pub struct ChannelSearchSharing {
#[packed_field(bytes = "0")]
Expand All @@ -997,7 +997,7 @@ pub struct ChannelSearchSharing {
pub search_sharing_cycles: u8,
}

#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "3")]
pub struct LoadEncryptionKeyFromNvm {
#[new(default)]
Expand All @@ -1011,7 +1011,7 @@ pub struct LoadEncryptionKeyFromNvm {
volatile_key_index: ReservedZeroes<packed_bits::Bits<8>>,
}

#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "18")]
pub struct StoreEncryptionKeyInNvm {
#[new(default)]
Expand Down
4 changes: 2 additions & 2 deletions ant/src/messages/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use ant_derive::AntTx;
use derive_new::new;
use packed_struct::prelude::*;

#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "1")]
pub struct ResetSystem {
#[new(default)]
#[packed_field(bytes = "0")]
filler: ReservedZeroes<packed_bits::Bits<8>>,
}

#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "1")]
pub struct OpenChannel {
#[packed_field(bytes = "0")]
Expand Down
29 changes: 29 additions & 0 deletions ant/src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub enum RxMessage {
// #define EXTENDED_BURST_DATA 0x5F
}

#[derive(Clone, Debug)]
pub enum TxMessage {
UnAssignChannel(UnAssignChannel),
AssignChannel(AssignChannel),
Expand Down Expand Up @@ -133,6 +134,13 @@ pub enum TxMessage {
CwTest(CwTest),
}

// Hack to allow channels to recycle memory, not for actual use
impl Default for TxMessage {
fn default() -> TxMessage {
TxMessage::UnAssignChannel(UnAssignChannel { channel_number: 0 })
}
}

impl TransmitableMessage for TxMessage {
fn serialize_message(&self, buf: &mut [u8]) -> Result<usize, PackingError> {
match self {
Expand Down Expand Up @@ -378,6 +386,27 @@ pub struct AntMessage {
pub checksum: u8,
}

// Hack to allow memory channels to recycle, not intended for actual use
impl Default for AntMessage {
fn default() -> AntMessage {
AntMessage {
header: RxMessageHeader {
sync: RxSyncByte::Read,
msg_length: 0,
msg_id: RxMessageId::StartUpMessage,
},
message: RxMessage::StartUpMessage(StartUpMessage {
hardware_reset_line: false,
watch_dog_reset: false,
command_reset: false,
synchronous_reset: false,
suspend_reset: false,
}),
checksum: 0,
}
}
}

/// Trait for any TX message type
pub trait TransmitableMessage {
fn serialize_message(&self, buf: &mut [u8]) -> Result<usize, PackingError>;
Expand Down
4 changes: 2 additions & 2 deletions ant/src/messages/test_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use ant_derive::AntTx;
use derive_new::new;
use packed_struct::prelude::*;

#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "1")]
pub struct CwInit {
#[new(default)]
#[packed_field(bytes = "0")]
filler: ReservedZeroes<packed_bits::Bits<8>>,
}

#[derive(PackedStruct, AntTx, new, Debug, Default, PartialEq)]
#[derive(PackedStruct, AntTx, new, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "3")]
pub struct CwTest {
#[new(default)]
Expand Down
Loading

0 comments on commit 8e1e7ce

Please sign in to comment.