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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#![cfg_attr(not(feature="std"), no_std)]
#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]

#![cfg_attr(feature="clippy", deny(almost_swapped))]
#![cfg_attr(feature="clippy", deny(blacklisted_name))]
#![cfg_attr(feature="clippy", deny(bool_comparison))]
#![cfg_attr(feature="clippy", deny(builtin_type_shadow))]
#![cfg_attr(feature="clippy", deny(clone_on_copy))]
#![cfg_attr(feature="clippy", deny(char_lit_as_u8))]
#![cfg_attr(feature="clippy", deny(should_assert_eq))]
#![cfg_attr(feature="clippy", deny(manual_memcpy))]
#![cfg_attr(feature="clippy", deny(unreadable_literal))]
#![cfg_attr(feature="clippy", deny(if_same_then_else))]
#![cfg_attr(feature="clippy", deny(needless_bool))]
#![cfg_attr(feature="clippy", deny(assign_op_pattern))]
#![cfg_attr(feature="clippy", deny(needless_return))]
#![cfg_attr(feature="clippy", deny(doc_markdown))]

#[allow(unused_imports)]
#[macro_use]
extern crate uavcan_derive;

extern crate bit_field;
extern crate embedded_types;
extern crate ux;
extern crate half;

mod lib {
    pub mod core {
        #[cfg(feature="std")]
        pub use std::*;
        #[cfg(not(feature="std"))]
        pub use core::*;
    }
}

mod uavcan {
    #[allow(unused_imports)]
    pub use *;
}

/// This module is only exposed so `Struct` can be derived.
/// It is not intended for use outside the derive macro and
/// must not be considered as a stable part of the API.
#[doc(hidden)]
pub use uavcan_derive::*;

pub mod transfer;
pub mod types;
mod crc;
mod deserializer;
mod frame_assembler;
mod serializer;
mod frame_disassembler;
pub mod node;
pub mod storage;

use bit_field::BitField;

use transfer::TransferFrameID;


pub use node::NodeConfig;
pub use node::NodeID;
pub use node::Node;
pub use node::SimpleNode;


/// These data type is only exposed so `Struct` can be derived.
/// It is not intended for use outside the derive macro and
/// must not be considered as a stable part of the API.
#[doc(hidden)]
pub use serializer::{
    SerializationResult,
    SerializationBuffer,        
};

/// These data type is only exposed so `Struct` can be derived.
/// It is not intended for use outside the derive macro and
/// must not be considered as a stable part of the API.
#[doc(hidden)]
pub use deserializer::{
    DeserializationResult,
    DeserializationBuffer,
};


/// The trait that needs to be implemented for all types that will be sent over Uavcan
///
/// The (de)serialization is based on flattening all structures to primitive fields
/// The serializer will then iterate over all fields and bits
pub trait Serializable {
    
    /// The minimum bit length an uavcan type can have
    ///
    ///
    /// # Examples
    ///
    /// ```
    /// # #[macro_use]
    /// # extern crate uavcan;
    /// # use uavcan::Struct;
    /// # use uavcan::types::*;
    /// # use uavcan::Serializable;
    ///
    /// # fn main() {
    /// // The primitive types have a fixed amount of bits
    /// assert_eq!(u2::BIT_LENGTH_MIN, 2);
    ///
    /// // The static arrays also have a fixed amount of bits
    /// assert_eq!(<[i62; 4] as Serializable>::BIT_LENGTH_MIN, 62*4);
    /// 
    /// // The dynamic arrays have their length coding included even though they can be optimized in some cases
    /// assert_eq!(Dynamic::<[void11; 3]>::BIT_LENGTH_MIN, 2);
    ///
    /// // Structs have the sum of all fields `MIN_BIT_LENGTH` as their `MIN_BIT_LENGTH`.
    /// #[derive(UavcanStruct)]
    /// struct Foo {
    ///     v1: u2,
    ///     v2: [i62; 4],
    ///     v3: Dynamic<[void11; 3]>,
    /// }
    ///
    /// assert_eq!(Foo::BIT_LENGTH_MIN, 2 + 62*4 + 2);
    ///
    /// // Enums have the minimum of all variants `MIN_BIT_LENGTH` as their `MIN_BIT_LENGTH`.
    /// // (But this is not implemented yet)
    ///
    /// # }
    /// ```
    const BIT_LENGTH_MIN: usize;
    
    /// Number of primitive fields after flattening of data type.
    ///
    /// Flattening of a struct consists of replacing all structs with its fields.
    /// Flattening of an enum consists of putting all fields in order
    ///
    /// # Examples
    /// ## Flattening of struct
    /// ```
    /// # #[macro_use]
    /// # extern crate uavcan;
    /// # use uavcan::Struct;
    /// # use uavcan::Serializable;
    /// #[derive(UavcanStruct)]
    /// struct InnerStruct {
    ///     v1: u8,
    ///     v2: u8,
    /// }
    ///
    /// #[derive(UavcanStruct)]
    /// struct OuterStruct {
    ///     v1: InnerStruct,
    ///     v2: InnerStruct,
    /// }
    ///
    /// # fn main() {
    /// assert_eq!(InnerStruct::FLATTENED_FIELDS_NUMBER, 2);
    /// assert_eq!(OuterStruct::FLATTENED_FIELDS_NUMBER, 4);
    /// # }
    /// ```
    /// ## Flattening of enum
    /// ```
    /// # #[macro_use]
    /// # extern crate uavcan;
    /// # use uavcan::Struct;
    /// # use uavcan::Serializable;
    /// #[derive(UavcanStruct)]
    /// enum InnerEnum {
    ///     V1(u8),
    ///     V2(u8),
    /// }
    ///
    /// #[derive(UavcanStruct)]
    /// enum OuterEnum {
    ///     V1(InnerEnum),
    ///     V2(InnerEnum),
    /// }
    ///
    /// # fn main() {
    /// assert_eq!(InnerEnum::FLATTENED_FIELDS_NUMBER, 2);
    /// assert_eq!(OuterEnum::FLATTENED_FIELDS_NUMBER, 4);
    /// # }
    /// ```
    const FLATTENED_FIELDS_NUMBER: usize;

    fn serialize(&self, flattened_field: &mut usize, bit: &mut usize, last_field: bool, buffer: &mut SerializationBuffer) -> SerializationResult;
    fn deserialize(&mut self, flattened_field: &mut usize, bit: &mut usize, last_field: bool, buffer: &mut DeserializationBuffer) -> DeserializationResult;
}

pub trait Struct: Sized + Serializable {
    const DSDL_SIGNATURE: u64;
    const DATA_TYPE_SIGNATURE: u64;
}

pub trait Message: Struct {
    const TYPE_ID: Option<u16>;
}

pub trait Request: Struct {
    type RESPONSE: Response;
    const TYPE_ID: Option<u8>;
}

pub trait Response: Struct {
    type REQUEST: Request;
    const TYPE_ID: Option<u8>;
}

#[derive(Debug, PartialEq)]
pub(crate) struct Frame<T: Struct> {
    id: TransferFrameID,
    body: T,
}

impl<T: Struct> Frame<T> {

    
    pub fn from_message(message: T, priority: u8, source_node: NodeID) -> Self where T: Message {
        if let Some(type_id) = T::TYPE_ID {
            let mut id = 0;
            id.set_bits(0..7, u32::from(source_node));
            id.set_bit(7, false);
            id.set_bits(8..24, u32::from(type_id));
            id.set_bits(24..29, u32::from(priority));
            
            Frame::from_parts(
                TransferFrameID::new(id),
                message,
            )
        } else {
            unimplemented!("Resolvation of type id is not supported yet")
        }
    }

    /*
    pub fn from_anonymous_message(message: T, priority: u8, discriminator: u16) -> Self where T: Message {
        if let Some(type_id) = T::TYPE_ID {
            let mut id = 0;
            id.set_bits(0..7, 0);
            id.set_bit(7, false);
            id.set_bits(8..10, u32::from(type_id));
            id.set_bits(10..24, u32::from(discriminator));
            id.set_bits(24..29, u32::from(priority));
            
            Frame::from_parts(
                TransferFrameID::new(id),
                message,
            )
        } else {
            unimplemented!("Resolvation of type id is not supported yet")
        }

    }

    pub fn from_request(request: T, priority: u8, source_node: NodeID, destination_node: NodeID) -> Self where T: Request{
        if let Some(type_id) = T::TYPE_ID {
            let mut id = 0;
            id.set_bits(0..7, u32::from(source_node));
            id.set_bit(7, false);
            id.set_bits(8..15, u32::from(destination_node));
            id.set_bit(15, true);
            id.set_bits(16..24, u32::from(type_id));
            id.set_bits(24..29, u32::from(priority));
            
            Frame::from_parts(
                TransferFrameID::new(id),
                request,
            )
        } else {
            unimplemented!("Resolvation of type id is not supported yet")
        }

    }

    pub fn from_response(response: T, priority: u8, source_node: NodeID, destination_node: NodeID) -> Self where T: Response {
        if let Some(type_id) = T::TYPE_ID {
            let mut id = 0;
            id.set_bits(0..7, u32::from(source_node));
            id.set_bit(7, false);
            id.set_bits(8..15, u32::from(destination_node));
            id.set_bit(15, true);
            id.set_bits(16..24, u32::from(type_id));
            id.set_bits(24..29, u32::from(priority));

            Frame::from_parts(
                TransferFrameID::new(id),
                response,
            )
        } else {
            unimplemented!("Resolvation of type id is not supported yet")
        }

    }
    */
    
    fn from_parts(id: TransferFrameID, body: T) -> Self {
        Frame{id: id, body: body}
    }
    
    fn into_parts(self) -> (TransferFrameID, T) {
        (self.id, self.body)
    }
}






#[cfg(test)]
mod tests {

    use *;

    // Implementing some types common for several tests
    
    #[derive(Debug, PartialEq)]
    pub struct CanFrame {
        pub id: TransferFrameID,
        pub dlc: usize,
        pub data: [u8; 8],
    }

    impl transfer::TransferFrame for CanFrame {
        const MAX_DATA_LENGTH: usize = 8;
        
        fn new(id: TransferFrameID) -> CanFrame {
            CanFrame{id: id, dlc: 0, data: [0; 8]}
        }
        
        fn set_data_length(&mut self, length: usize) {
            assert!(length <= 8);
            self.dlc = length;
        }

        fn data(&self) -> &[u8] {
            &self.data[0..self.dlc]
        }

        fn data_as_mut(&mut self) -> &mut[u8] {
            &mut self.data[0..self.dlc]
        }
        
        fn id(&self) -> TransferFrameID {
            self.id 
        }
    }

    
    
}