diff options
| -rw-r--r-- | src/crc.rs | 32 | ||||
| -rw-r--r-- | src/device.rs | 10 | ||||
| -rw-r--r-- | src/main.rs | 4 | 
3 files changed, 28 insertions, 18 deletions
| @@ -1,23 +1,25 @@  // Copyright 2019 Robin Krahl <robin.krahl@ireas.org>  // SPDX-License-Identifier: GPL-3.0-or-later +use core::iter::Iterator; +  use hal::stm32::CRC; -pub struct Crc { +pub trait Crc { +    fn get(&self, data: &[u8]) -> u32 { +        self.get_u32(data.chunks(4).map(slice_to_u32)) +    } + +    fn get_u32<I: Iterator<Item = u32>>(&self, iter: I) -> u32; +} + +pub struct Stm32Crc {      crc: CRC,  } -impl Crc { +impl Stm32Crc {      pub fn new(crc: CRC) -> Self { -        Crc { crc } -    } - -    pub fn get(&self, data: &[u8]) -> u32 { -        self.reset(); -        data.chunks(4) -            .map(slice_to_u32) -            .for_each(|val| self.write(val)); -        self.read() +        Self { crc }      }      fn write(&self, val: u32) { @@ -33,6 +35,14 @@ impl Crc {      }  } +impl Crc for Stm32Crc { +    fn get_u32<I: Iterator<Item = u32>>(&self, iter: I) -> u32 { +        self.reset(); +        iter.for_each(|val| self.write(val)); +        self.read() +    } +} +  fn slice_to_u32(data: &[u8]) -> u32 {      data.iter()          .enumerate() diff --git a/src/device.rs b/src/device.rs index 9f3d1a7..830fa93 100644 --- a/src/device.rs +++ b/src/device.rs @@ -94,15 +94,15 @@ impl Response {      }  } -pub struct Nitrokey { -    crc: Crc, +pub struct Nitrokey<C: Crc> { +    crc: C,      request: Option<Request>,      request_crc: u32,      buf: [u8; REPORT_LEN],  } -impl Nitrokey { -    pub fn new(crc: Crc) -> Self { +impl<C: Crc> Nitrokey<C> { +    pub fn new(crc: C) -> Self {          Nitrokey {              crc,              request: None, @@ -112,7 +112,7 @@ impl Nitrokey {      }  } -impl HidDevice for Nitrokey { +impl<C: Crc> HidDevice for Nitrokey<C> {      fn subclass(&self) -> Subclass {          Subclass::BootInterface      } diff --git a/src/main.rs b/src/main.rs index 7e364ab..eb01dd6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use hal::prelude::*;  use stm32f103xx_usb::UsbBus;  use usb_device::class::UsbClass; -use crate::crc::Crc; +use crate::crc::Stm32Crc;  use crate::device::Nitrokey;  use crate::hid::HidClass; @@ -28,7 +28,7 @@ fn main() -> ! {      let p = hal::stm32::Peripherals::take().unwrap();      p.RCC.ahbenr.modify(|_, w| w.crcen().set_bit()); -    let crc = Crc::new(p.CRC); +    let crc = Stm32Crc::new(p.CRC);      let mut flash = p.FLASH.constrain();      let mut rcc = p.RCC.constrain(); | 
