-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathi2c.rs
More file actions
71 lines (60 loc) · 2.08 KB
/
i2c.rs
File metadata and controls
71 lines (60 loc) · 2.08 KB
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright The Asahi Linux Contributors
*/
use crate::transport::Transport;
use crate::{Error, Result};
use std::io;
#[cfg(any(target_os = "linux", target_os = "android"))]
use i2cdev::{core::I2CDevice, linux::LinuxI2CDevice};
#[cfg(any(target_os = "linux", target_os = "android"))]
pub(crate) struct I2cTransport {
dev: LinuxI2CDevice,
}
#[cfg(any(target_os = "linux", target_os = "android"))]
fn open_i2c(bus: &str, addr: u16) -> Result<LinuxI2CDevice> {
if let Ok(dev) = LinuxI2CDevice::new(bus, addr) {
return Ok(dev);
}
log::info!("Safely opening failed ==> Forcefully opening device...");
unsafe { LinuxI2CDevice::force_new(bus, addr) }.map_err(|_| Error::I2C)
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl I2cTransport {
pub(crate) fn new(bus: &str, addr: u16) -> Result<Self> {
Ok(Self { dev: open_i2c(bus, addr)? })
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl Transport for I2cTransport {
fn write(&mut self, data: &[u8]) -> io::Result<()> {
self.dev
.write(data)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}
fn read(&mut self, len: usize) -> io::Result<Vec<u8>> {
let mut buf = vec![0u8; len];
self.dev
.read(&mut buf)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
Ok(buf)
}
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub(crate) struct I2cTransport;
#[cfg(not(any(target_os = "linux", target_os = "android")))]
impl I2cTransport {
pub(crate) fn new(_bus: &str, _addr: u16) -> Result<Self> {
Err(Error::FeatureMissing)
}
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
impl Transport for I2cTransport {
fn write(&mut self, _data: &[u8]) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Unsupported, "i2c transport is linux-only"))
}
fn read(&mut self, _len: usize) -> io::Result<Vec<u8>> {
Err(io::Error::new(io::ErrorKind::Unsupported, "i2c transport is linux-only"))
}
}