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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

use std::net::{SocketAddr};
use std::sync::{Arc, Mutex};
use std::collections::{HashMap, hash_map::Entry};

use daemon_engine::{TcpServer, JsonCodec};
use tokio::prelude::*;

use embedded_hal::blocking::spi::{Transfer as SpiTransfer, Write as SpiWrite};
use embedded_hal::blocking::i2c::{Read as I2cRead, Write as I2cWrite, WriteRead as I2cWriteRead};
use embedded_hal::digital::{InputPin, OutputPin};


use crate::common::*;
use crate::error::Error;

use crate::local::spi::Spi;
use crate::local::i2c::I2c;
use crate::local::pin::Pin;

/// remote-hal server, this exposes embedded-hal devices over TCP RPC interface
/// 
/// THIS MUST BE RUN IN A TOKIO CONTEXT
#[derive(Clone)]
pub struct Server {
    _server: TcpServer<JsonCodec<Response, Request>>,

    spi: Arc<Mutex<HashMap<String, Spi>>>,
    i2c: Arc<Mutex<HashMap<String, I2c>>>,
    pin: Arc<Mutex<HashMap<String, Pin>>>,
}

impl Server {
    pub fn new(addr: SocketAddr) -> Result<Self, Error> {
        debug!("server binding to: {}", addr);

        let server = TcpServer::<JsonCodec<Response, Request>>::new(&addr, JsonCodec::new()).unwrap();

        let s = Self {
            _server: server.clone(),
            spi: Arc::new(Mutex::new(HashMap::new())),
            i2c: Arc::new(Mutex::new(HashMap::new())),
            pin: Arc::new(Mutex::new(HashMap::new())),    
        };

        let mut s1 = s.clone();

        let server_handle = server.clone()
            .incoming()
            .unwrap()
            .for_each(move |r| {
                let req = r.data();
                info!("Received: {:?} info: {:?}", req, r.info());

                let resp = match s1.handle(&req.device, req.kind) {
                    Ok(resp) => resp,
                    Err(e) => ResponseKind::Error(format!("{:?}", e)),
                };

                info!("Response: {:?}", resp);

                r.send(Response{id: req.id, kind: resp}).map(|_v| trace!("server send complete") ).map_err(|e| error!("server error: {:?}", e))
            }).map(|_v| () ).map_err(|_e| ());

        tokio::spawn(server_handle);

        Ok(s)
    }

    pub fn handle(&mut self, device: &str, req: RequestKind) -> Result<ResponseKind, Error> {
        let resp = match req {
            RequestKind::Ping => ResponseKind::Ok,
            
            RequestKind::SpiConnect(c) => {
                info!("received SpiConnect (device: {}, baud: {}, mode: {:?})", device, c.baud, c.mode);
                let mut spi_map = self.spi.lock().unwrap();

                match spi_map.entry(device.to_owned()) {
                    Entry::Occupied(_e) => ResponseKind::DeviceAlreadyBound,
                    Entry::Vacant(v) => {
                        v.insert(Spi::new(device, c.baud, c.mode)?);
                        ResponseKind::Ok
                    },
                }
            },


            RequestKind::SpiDisconnect => {
                info!("received SpiDisconnect (device: {})", device);
                let mut spi = self.spi.lock().unwrap();
                match spi.remove(device) {
                    Some(_d) => ResponseKind::Ok,
                    None => ResponseKind::DeviceNotBound,
                }
            },

            RequestKind::SpiTransfer{write_data} => {
                info!("received SpiTransfer");
                let mut spi_map = self.spi.lock().unwrap();
                let spi = match spi_map.get_mut(device) {
                    Some(s) => s,
                    None => return Ok(ResponseKind::DeviceNotBound),
                };

                let mut d = write_data.data.clone();

                match SpiTransfer::transfer(spi, &mut d) {
                    Ok(d) => ResponseKind::SpiTransfer(d.to_vec()),
                    Err(e) => ResponseKind::Error(format!("{:?}", e)),
                }
            },

            RequestKind::SpiWrite{write_data} => {
                info!("received SpiWrite");
                let mut spi_map = self.spi.lock().unwrap();
                let spi = match spi_map.get_mut(device) {
                    Some(s) => s,
                    None => return Ok(ResponseKind::DeviceNotBound),
                };

                let mut d = write_data.data.clone();

                match SpiWrite::write(spi, &mut d) {
                    Ok(_) => ResponseKind::Ok,
                    Err(e) => ResponseKind::Error(format!("{:?}", e)),
                }
            },

            RequestKind::I2cConnect => {
                info!("received I2cConnect (device: {})", device);
                let mut i2c = self.i2c.lock().unwrap();

                match i2c.entry(device.to_owned()) {
                    Entry::Occupied(_e) => ResponseKind::DeviceAlreadyBound,
                    Entry::Vacant(v) => {
                        v.insert(I2c::new(device)?);
                        ResponseKind::Ok
                    },
                }
            },

            RequestKind::I2cDisconnect => {
                info!("received I2cDisconnect (device: {})", device);
                let mut i2c = self.i2c.lock().unwrap();
                match i2c.remove(device) {
                    Some(_d) => ResponseKind::Ok,
                    None => ResponseKind::DeviceNotBound,
                }
            },

            RequestKind::I2cWrite(c) => {
                info!("received I2cWrite (address: {}, data: {:?})", c.addr, c.write_data);
                let mut i2c_map = self.i2c.lock().unwrap();
                let i2c = match i2c_map.get_mut(device) {
                    Some(s) => s,
                    None => return Ok(ResponseKind::DeviceNotBound),
                };

                match I2cWrite::write(i2c, c.addr, &c.write_data.data) {
                    Ok(_) => ResponseKind::Ok,
                    Err(e) => ResponseKind::Error(format!("{:?}", e)),
                }
            },

            RequestKind::I2cRead(c) => {
                info!("received I2cRead (address: {}, len: {})", c.addr, c.read_len);
                let mut i2c_map = self.i2c.lock().unwrap();
                let i2c = match i2c_map.get_mut(device) {
                    Some(s) => s,
                    None => return Ok(ResponseKind::DeviceNotBound),
                };

                let mut buff = vec![0; c.read_len as usize];

                match I2cRead::read(i2c, c.addr, &mut buff) {
                    Ok(_) => ResponseKind::I2cRead(buff),
                    Err(e) => ResponseKind::Error(format!("{:?}", e)),
                }
            },

            RequestKind::I2cWriteRead(c) => {
                info!("received I2cWriteRead (address: {}, write_data: {:?}, read_len: {}", c.addr, c.write_data, c.read_len);
                let mut i2c_map = self.i2c.lock().unwrap();
                let i2c = match i2c_map.get_mut(device) {
                    Some(s) => s,
                    None => return Ok(ResponseKind::DeviceNotBound),
                };

                let mut buff = vec![0; c.read_len as usize];

                match I2cWriteRead::write_read(i2c, c.addr, &c.write_data.data, &mut buff) {
                    Ok(_) => ResponseKind::I2cRead(buff),
                    Err(e) => ResponseKind::Error(format!("{:?}", e)),
                }
            },

            RequestKind::PinConnect(mode) => {
                info!("received PinConnect (device: {})", device);
                let mut pin = self.pin.lock().unwrap();

                match pin.entry(device.to_owned()) {
                    Entry::Occupied(_e) => ResponseKind::DeviceAlreadyBound,
                    Entry::Vacant(v) => {
                        let p = Pin::new(device, mode)?;
                        v.insert(p);
                        ResponseKind::Ok
                    },
                }
            },

            RequestKind::PinDisconnect => {
                info!("received PinDisconnect (device: {})", device);
                let mut pins = self.pin.lock().unwrap();
                match pins.remove(device) {
                    Some(_p) => {
                        ResponseKind::Ok
                    },
                    None => ResponseKind::DeviceNotBound
                }
            },

            RequestKind::PinSet(c) => {
                info!("received PinSet");
                let mut pin_map = self.pin.lock().unwrap();
                let pin = match pin_map.get_mut(device) {
                    Some(s) => s,
                    None => return Ok(ResponseKind::DeviceNotBound),
                };

                let res: Result<_, ()> = Ok(match c.value {
                    true => OutputPin::set_high(pin),
                    false => OutputPin::set_low(pin),
                });

                match res {
                    Ok(_) => ResponseKind::Ok,
                    Err(e) => ResponseKind::Error(format!("{:?}", e)),
                }
            },

            RequestKind::PinGet => {
                info!("received PinGet");
                let mut pin_map = self.pin.lock().unwrap();
                let pin = match pin_map.get_mut(device) {
                    Some(s) => s,
                    None => return Ok(ResponseKind::DeviceNotBound),
                };

                let v: Result<_, ()> = Ok(InputPin::is_high(pin));

                match v {
                    Ok(v) => ResponseKind::PinGet(v),
                    Err(e) => ResponseKind::Error(format!("{:?}", e)),
                }
            },
        };

        Ok(resp)
    }
}