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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Test the CRC hardware.

use kernel::debug;
use kernel::hil::crc::{Client, Crc, CrcAlgorithm, CrcOutput};
use kernel::utilities::cells::TakeCell;
use kernel::utilities::leasable_buffer::SubSliceMut;
use kernel::ErrorCode;

pub struct TestCrc<'a, C: 'a> {
    crc: &'a C,
    data: TakeCell<'static, [u8]>,
}

impl<'a, C: Crc<'a>> TestCrc<'a, C> {
    pub fn new(crc: &'a C, data: &'static mut [u8]) -> Self {
        TestCrc {
            crc,
            data: TakeCell::new(data),
        }
    }

    pub fn run_test(&self, algorithm: CrcAlgorithm) {
        let res = self.crc.set_algorithm(algorithm);
        if res.is_err() {
            debug!("CrcTest ERROR: failed to set algorithm to Crc32: {:?}", res);
            return;
        }
        let leasable: SubSliceMut<'static, u8> = SubSliceMut::new(self.data.take().unwrap());

        let res = self.crc.input(leasable);
        if let Err((error, _buffer)) = res {
            debug!(
                "CrcTest ERROR: failed to start input processing: {:?}",
                error
            );
        }
    }

    pub fn run(&self) {
        self.run_test(CrcAlgorithm::Crc32);
    }
}

impl<'a, C: Crc<'a>> Client for TestCrc<'a, C> {
    fn input_done(&self, result: Result<(), ErrorCode>, buffer: SubSliceMut<'static, u8>) {
        if result.is_err() {
            debug!("CrcTest ERROR: failed to process input: {:?}", result);
            return;
        }

        if buffer.len() == 0 {
            self.data.replace(buffer.take());
            let res = self.crc.compute();
            if res.is_err() {
                debug!("CrcTest ERROR: failed to start CRC computation: {:?}", res);
            }
        } else {
            let res = self.crc.input(buffer);
            if let Err((error, _buffer)) = res {
                debug!(
                    "CrcTest ERROR: failed to start input processing: {:?}",
                    error
                );
            }
        }
    }

    /// Called when the CRC computation is finished.
    fn crc_done(&self, result: Result<CrcOutput, ErrorCode>) {
        if let Err(code) = result {
            debug!("CrcTest ERROR: failed to compute CRC: {:?}", code);
        } else {
            if let Ok(output) = result {
                match output {
                    CrcOutput::Crc32(x) => {
                        debug!("CRC32: {:#x}", x);
                        self.run_test(CrcAlgorithm::Crc32C);
                    }
                    CrcOutput::Crc32C(x) => {
                        debug!("CRC32C: {:#x}", x);
                        self.run_test(CrcAlgorithm::Crc16CCITT);
                    }
                    CrcOutput::Crc16CCITT(x) => {
                        debug!("CRC16CCITT: {:#x}", x);
                    }
                }
            }
        }
    }
}