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

use rand::random;
use structopt::StructOpt;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Request {
    pub id: u64,
    pub device: String,
    pub kind: RequestKind,
}

impl Request {
    pub fn new(device: String, kind: RequestKind) -> Self {
        Self{id: random(), device, kind}
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, StructOpt)]
pub enum RequestKind {
    #[structopt(name = "ping")]
    Ping,

    #[structopt(name = "spi-connect")]
    SpiConnect(SpiConnect),
    #[structopt(name = "spi-transfer")]
    SpiTransfer(Data),
    #[structopt(name = "spi-write")]
    SpiWrite(Data),
    #[structopt(name = "spi-disconnect")]
    SpiDisconnect,

    #[structopt(name = "pin-connect")]
    PinConnect,
    #[structopt(name = "pin-set")]
    PinSet(Value),
    #[structopt(name = "pin-get")]
    PinGet,
    #[structopt(name = "pin-disconnect")]
    PinDisconnect,

    #[structopt(name = "i2c-connect")]
    I2cConnect,
    #[structopt(name = "i2c-write")]
    I2cWrite(I2cWrite),
    #[structopt(name = "i2c-read")]
    I2cRead(I2cRead),
    #[structopt(name = "i2c-writeRead")]
    I2cWriteRead(I2cWriteRead),
    #[structopt(name = "i2c-disconnect")]
    I2cDisconnect,
}


#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Response {
    pub id: u64,
    pub kind: ResponseKind,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ResponseKind {
    Ok,
    Error(String),
    Unhandled,
    DeviceAlreadyBound,
    DeviceNotBound,

    SpiTransfer(Vec<u8>),
    PinGet(bool),
    I2cRead(Vec<u8>),
}

#[derive(Debug, Clone, Serialize, Deserialize, StructOpt)]
pub struct Data {
    pub data: Vec<u8>,
}

impl std::str::FromStr for Data {
    type Err = ();

    fn from_str(s: &str) -> std::result::Result<Self, ()> {
        Err(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, StructOpt)]
pub struct Value {
    pub value: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, StructOpt)]
pub struct SpiConnect {
    /// SPI baud rate in bps
    pub baud: u32,
    
    /// SPI mode
    pub mode: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize, StructOpt)]
pub struct I2cWrite {
    /// I2C device address
    pub addr: u8,
    #[structopt(flatten)]
    pub write_data: Data,
}

#[derive(Debug, Clone, Serialize, Deserialize, StructOpt)]
pub struct I2cRead {
    /// I2C device address
    pub addr: u8,
    /// I2C read length
    pub read_len: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize, StructOpt)]
pub struct I2cWriteRead {
    /// I2C device address
    pub addr: u8,
    #[structopt(flatten)]
    pub write_data: Data,
    /// I2C read length
    pub read_len: u16,
}