1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#![no_std]

#[macro_use]
extern crate uavcan_derive;

extern crate bit_field;

pub mod can_frame;
pub mod types;
pub mod headers;
mod crc;
mod parser;
pub mod message_builder;
mod serializer;
pub mod frame_generator;

use core::convert::{From};

use bit_field::BitArray;

/// The TransportFrame is uavcan cores main interface to the outside world
///
/// This will in >99% of situations be a CAN2.0B frame
/// But in theory both CAN-FD and other protocols which gives
/// similar guarantees as CAN can also be used
pub trait TransportFrame {
    fn tail_byte(&self) -> TailByte {
        TailByte::from(*self.data().last().unwrap())
    }
    fn is_start_frame(&self) -> bool {
        self.tail_byte().start_of_transfer
    }
    fn is_end_frame(&self) -> bool {
        self.tail_byte().end_of_transfer
    }
    fn is_single_frame(&self) -> bool {
        self.is_end_frame() && self.is_start_frame()
    }

    /// with_data(id: u32, data: &[u]) -> TransportFrame creates a TransportFrame
    /// with an 28 bits ID and data between 0 and the return value ofget_max_data_length()
    fn with_data(id: u32,  data: &[u8]) -> Self;
    fn with_length(id: u32, length: usize) -> Self;
    fn set_data_length(&mut self, length: usize);
    fn max_data_length() -> usize;
    fn data(&self) -> &[u8];
    fn data_as_mut(&mut self) -> &mut[u8];
    fn id(&self) -> u32;
}

pub struct TailByte {
    start_of_transfer: bool,
    end_of_transfer: bool,
    toggle: bool,
    transfer_id: u8,
}

impl From<TailByte> for u8 {
    fn from(tb: TailByte) -> u8 {
        ((tb.start_of_transfer as u8) << 7) | ((tb.end_of_transfer as u8) << 6) | ((tb.toggle as u8) << 5) | (tb.transfer_id&0x1f)
    }
}

impl From<u8> for TailByte {
    fn from(u: u8) -> TailByte {
        TailByte{start_of_transfer: (u&(1<<7)) != 0, end_of_transfer: (u&(1<<6)) != 0, toggle: (u&(1<<5)) != 0, transfer_id: u&0x1f}
    }
}

pub trait UavcanHeader {
    fn to_id(self) -> u32;
    fn from_id(u32) -> Self;
    fn set_priority(&mut self, priority: u8);
    fn get_priority(&self) -> u8;
}

pub trait UavcanIndexable {
    fn number_of_primitive_fields(&self) -> usize;
    fn primitive_field_as_mut(&mut self, field_number: usize) -> &mut UavcanPrimitiveField;
    fn primitive_field(&self, field_number: usize) -> &UavcanPrimitiveField;
}


/// An UavcanPrimitiveField is a field of a flatted out uavcan struct
///
/// It's a superset of Primitive Data Types from the uavcan protocol
/// also containing both constant and variable size arrays.
///
/// All primitive data types have 1 primitive fields,
/// All composite data structures have the same number of primtiive fields
/// as the sum of their members. Except the variable length array.
/// This array has number of primitive fields as their members (elements)+1
pub trait UavcanPrimitiveField{
    fn is_constant_size(&self) -> bool;
    /// get_size(&self) -> usize returns the number of primitive data types in this field
    ///
    /// for primtiive data types (non-array) it will return 1
    fn get_size(&self) -> usize;
    /// get_size_mut(&self) -> Option<&mut usize> returns a mutable reference to the size
    /// if the field is of variable size, or None if the field is constant size 
    fn get_size_mut(&self) -> Option<&mut usize>;
    fn primitive_type_as_mut(&mut self, index: usize) -> &mut UavcanPrimitiveType;
    fn primitive_type(&self, index: usize) -> &UavcanPrimitiveType;
}

pub trait UavcanPrimitiveType : BitArray<u64> {
}


pub trait UavcanFrame<H: UavcanHeader, B: UavcanIndexable> {
    fn from_parts(header: H, body: B) -> Self;
    fn to_parts(self) -> (H, B);
    fn header(&self) -> &H;
    fn body(&self) -> &B;
}




#[cfg(test)]
mod tests {
    use {
        UavcanIndexable,
        UavcanPrimitiveField,
    };
    
    use types::{
        Uint2,
        Uint3,
        Uint16,
        Uint32,
    };
    
    #[test]
    fn uavcan_sized_length_derivation() {

        #[derive(UavcanIndexable)]
        struct NodeStatus {
            uptime_sec: Uint32,
            health: Uint2,
            mode: Uint3,
            sub_mode: Uint3,
            vendor_specific_status_code: Uint16,
        }
        
        impl NodeStatus {
            fn new() -> NodeStatus{
                NodeStatus {
                    uptime_sec: 0.into(),
                    health: 0.into(),
                    mode: 0.into(),
                    sub_mode: 0.into(),
                    vendor_specific_status_code: 0.into(),
                }
            }
        }

        #[derive(UavcanIndexable)]
        struct TestComposite {
            ns1: NodeStatus,
            ns2: NodeStatus,
        }

        impl TestComposite {
            fn new() -> TestComposite {
                TestComposite{
                    ns1: NodeStatus::new(),
                    ns2: NodeStatus::new(),
                }
            }
        }

        #[derive(UavcanIndexable)]
        struct TestComposite2 {
            ns1: NodeStatus,
            tc: TestComposite,
            ns2: NodeStatus,
        }

        impl TestComposite2 {
            fn new() -> TestComposite2 {
                TestComposite2{
                    ns1: NodeStatus::new(),
                    tc: TestComposite::new(),
                    ns2: NodeStatus::new(),
                }
            }
        }

        
        assert_eq!(NodeStatus::new().number_of_primitive_fields(), 5);
        assert_eq!(TestComposite::new().number_of_primitive_fields(), 10);
        assert_eq!(TestComposite2::new().number_of_primitive_fields(), 20);
        
        
    }

    #[test]
    fn uavcan_index_primitive_field() {

        #[derive(UavcanIndexable)]
        struct NodeStatus {
            uptime_sec: Uint32,
            health: Uint2,
            mode: Uint3,
            sub_mode: Uint3,
            vendor_specific_status_code: Uint16,
        }


        impl NodeStatus {
            fn new() -> NodeStatus{
                NodeStatus {
                    uptime_sec: 0.into(),
                    health: 0.into(),
                    mode: 0.into(),
                    sub_mode: 0.into(),
                    vendor_specific_status_code: 0.into(),
                }
            }
        }
        
        
        let mut node_status = NodeStatus::new();

        node_status.primitive_field_as_mut(0).primitive_type_as_mut(0).set_bits(0..32, 1);
        node_status.primitive_field_as_mut(1).primitive_type_as_mut(0).set_bits(0..2, 2);
        node_status.primitive_field_as_mut(2).primitive_type_as_mut(0).set_bits(0..3, 3);
        node_status.primitive_field_as_mut(3).primitive_type_as_mut(0).set_bits(0..3, 4);
        node_status.primitive_field_as_mut(4).primitive_type_as_mut(0).set_bits(0..16, 5);

        assert_eq!(node_status.uptime_sec, 1.into());
        assert_eq!(node_status.health, 2.into());
        assert_eq!(node_status.mode, 3.into());
        assert_eq!(node_status.sub_mode, 4.into());
        assert_eq!(node_status.vendor_specific_status_code, 5.into());

    }



    
    
}