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
// Copyright 2015, Paul Osborne <osbpau@gmail.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option.  This file may not be copied, modified, or distributed
// except according to those terms.
use core::{I2CDevice, I2CMessage, I2CTransfer};
use std::io;

pub type I2CResult<T> = io::Result<T>;

pub struct I2CRegisterMap {
    registers: [u8; 0xFF],
    offset: usize,
}

impl Default for I2CRegisterMap {
    fn default() -> Self {
        Self::new()
    }
}

impl I2CRegisterMap {
    pub fn new() -> I2CRegisterMap {
        I2CRegisterMap {
            registers: [0x00; 0xFF],
            offset: 0,
        }
    }

    pub fn write_regs(&mut self, offset: usize, data: &[u8]) {
        println!("WRITE | 0x{:X} : {:?}", offset, data);
        self.registers[offset..(data.len() + offset)].clone_from_slice(&data);
    }
}

impl I2CRegisterMap {
    /// Read data from the device to fill the provided slice
    fn read(&mut self, data: &mut [u8]) -> I2CResult<()> {
        let len = data.len();
        data.clone_from_slice(&self.registers[self.offset..(self.offset + len)]);
        println!("READ  | 0x{:X} : {:?}", self.offset - data.len(), data);
        Ok(())
    }

    /// Write the provided buffer to the device
    fn write(&mut self, data: &[u8]) -> I2CResult<()> {
        // ASSUMPTION: first byte sets the offset
        // ASSUMPTION: write has length of at least one (will panic)
        let offset = data[0] as usize;
        let remdata = &data[1..];
        self.write_regs(offset, remdata);
        self.offset = offset + remdata.len();
        Ok(())
    }
}

#[derive(Default)]
pub struct MockI2CDevice {
    pub regmap: I2CRegisterMap,
}

impl MockI2CDevice {
    pub fn new() -> MockI2CDevice {
        MockI2CDevice {
            regmap: I2CRegisterMap::new(),
        }
    }
}

impl I2CDevice for MockI2CDevice {
    type Error = io::Error;

    fn read(&mut self, data: &mut [u8]) -> I2CResult<()> {
        self.regmap.read(data)
    }

    fn write(&mut self, data: &[u8]) -> I2CResult<()> {
        self.regmap.write(data)
    }

    fn smbus_write_quick(&mut self, _bit: bool) -> I2CResult<()> {
        unimplemented!()
    }

    fn smbus_read_block_data(&mut self, _register: u8) -> I2CResult<Vec<u8>> {
        unimplemented!()
    }

    fn smbus_write_block_data(&mut self, _register: u8, _values: &[u8]) -> I2CResult<()> {
        unimplemented!()
    }

    fn smbus_process_block(&mut self, _register: u8, _values: &[u8]) -> I2CResult<Vec<u8>> {
        unimplemented!()
    }

    fn smbus_read_i2c_block_data(&mut self, _register: u8, _len: u8) -> I2CResult<Vec<u8>> {
        unimplemented!()
    }

    fn smbus_write_i2c_block_data(&mut self, _register: u8, _values: &[u8]) -> I2CResult<()> {
        unimplemented!()
    }
}

#[derive(Debug)]
enum MessageType<'a> {
    Write(&'a [u8]),
    Read(&'a mut [u8]),
}

pub struct MockI2CMessage<'a> {
    msg_type: MessageType<'a>,
}

impl<'a> I2CMessage<'a> for MockI2CMessage<'a> {
    fn read(data: &'a mut [u8]) -> Self {
        Self {
            msg_type: MessageType::Read(data),
        }
    }

    /// Write data to device
    fn write(data: &'a [u8]) -> Self {
        Self {
            msg_type: MessageType::Write(data),
        }
    }
}

impl<'a> I2CTransfer<'a> for MockI2CDevice
where
    MockI2CDevice: I2CDevice,
{
    type Error = io::Error;
    type Message = MockI2CMessage<'a>;

    /// Issue the provided sequence of I2C transactions
    fn transfer(&mut self, messages: &'a mut [Self::Message]) -> Result<u32, Self::Error> {
        for msg in messages.iter_mut() {
            match &mut msg.msg_type {
                MessageType::Read(data) => self.read(data)?,
                MessageType::Write(data) => self.write(data)?,
            }
        }
        Ok(messages.len() as u32)
    }
}