2020-04-29 01:26:43 +08:00
|
|
|
use heapless::{
|
|
|
|
consts::*,
|
|
|
|
String,
|
|
|
|
Vec
|
|
|
|
};
|
|
|
|
|
|
|
|
use core::fmt::Write;
|
|
|
|
|
|
|
|
|
|
|
|
use serde::{
|
|
|
|
Deserialize,
|
|
|
|
Serialize
|
|
|
|
};
|
|
|
|
|
|
|
|
use serde_json_core::{
|
|
|
|
de::from_slice,
|
|
|
|
ser::to_string
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::net;
|
2020-06-03 23:15:57 +08:00
|
|
|
use super::iir;
|
2020-04-29 01:26:43 +08:00
|
|
|
|
2020-06-03 21:46:18 +08:00
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
pub enum Request<'a, 'b> {
|
|
|
|
Read{attribute: &'a str},
|
|
|
|
Write{attribute: &'a str, value: &'b str},
|
2020-04-29 01:26:43 +08:00
|
|
|
}
|
|
|
|
|
2020-06-03 23:15:57 +08:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct IirRequest {
|
|
|
|
pub channel: u8,
|
|
|
|
pub iir: iir::IIR,
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:26:43 +08:00
|
|
|
#[derive(Serialize)]
|
2020-06-03 22:53:25 +08:00
|
|
|
pub struct Response {
|
2020-04-29 01:26:43 +08:00
|
|
|
code: i32,
|
2020-06-03 22:53:25 +08:00
|
|
|
attribute: String<U128>,
|
|
|
|
value: String<U128>,
|
2020-06-03 21:46:18 +08:00
|
|
|
}
|
|
|
|
|
2020-06-03 22:53:25 +08:00
|
|
|
impl Response {
|
|
|
|
pub fn success<'a, 'b>(attribute: &'a str, value: &'b str) -> Self
|
2020-06-03 21:46:18 +08:00
|
|
|
{
|
2020-06-03 22:53:25 +08:00
|
|
|
Self { code: 200, attribute: String::from(attribute), value: String::from(value)}
|
2020-06-03 21:46:18 +08:00
|
|
|
}
|
|
|
|
|
2020-06-03 22:53:25 +08:00
|
|
|
pub fn error<'a, 'b>(attribute: &'a str, message: &'b str) -> Self
|
2020-06-03 21:46:18 +08:00
|
|
|
{
|
2020-06-03 22:53:25 +08:00
|
|
|
Self { code: 400, attribute: String::from(attribute), value: String::from(message)}
|
2020-06-03 21:46:18 +08:00
|
|
|
}
|
|
|
|
|
2020-06-03 22:53:25 +08:00
|
|
|
pub fn custom<'a>(code: i32, message : &'a str) -> Self
|
2020-06-03 21:46:18 +08:00
|
|
|
{
|
2020-06-03 22:53:25 +08:00
|
|
|
Self { code: code, attribute: String::from(""), value: String::from(message)}
|
2020-06-03 21:46:18 +08:00
|
|
|
}
|
2020-04-29 01:26:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct Status {
|
|
|
|
pub t: u32,
|
|
|
|
pub x0: f32,
|
|
|
|
pub y0: f32,
|
|
|
|
pub x1: f32,
|
|
|
|
pub y1: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn json_reply<T: Serialize>(socket: &mut net::socket::TcpSocket, msg: &T) {
|
|
|
|
let mut u: String<U128> = to_string(msg).unwrap();
|
|
|
|
u.push('\n').unwrap();
|
|
|
|
socket.write_str(&u).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Server {
|
|
|
|
data: Vec<u8, U256>,
|
|
|
|
discard: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
data: Vec::new(),
|
|
|
|
discard: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 22:53:25 +08:00
|
|
|
pub fn poll<F>(
|
2020-04-29 01:26:43 +08:00
|
|
|
&mut self,
|
|
|
|
socket: &mut net::socket::TcpSocket,
|
2020-06-03 22:53:25 +08:00
|
|
|
mut f: F,
|
2020-06-03 21:46:18 +08:00
|
|
|
)
|
2020-04-29 01:26:43 +08:00
|
|
|
where
|
2020-06-03 22:53:25 +08:00
|
|
|
F: FnMut(&Request) -> Response
|
2020-04-29 01:26:43 +08:00
|
|
|
{
|
|
|
|
while socket.can_recv() {
|
2020-06-03 21:46:18 +08:00
|
|
|
let found = socket.recv(|buf| {
|
|
|
|
let (len, found) =
|
|
|
|
match buf.iter().position(|&c| c as char == '\n') {
|
|
|
|
Some(end) => (end + 1, true),
|
|
|
|
None => (buf.len(), false),
|
|
|
|
};
|
|
|
|
if self.data.len() + len >= self.data.capacity() {
|
|
|
|
self.discard = true;
|
|
|
|
self.data.clear();
|
|
|
|
} else if !self.discard && len > 0 {
|
|
|
|
self.data.extend_from_slice(&buf[..len]).unwrap();
|
|
|
|
}
|
|
|
|
(len, found)
|
|
|
|
}).unwrap();
|
|
|
|
|
2020-04-29 01:26:43 +08:00
|
|
|
if found {
|
|
|
|
if self.discard {
|
|
|
|
self.discard = false;
|
2020-06-03 22:53:25 +08:00
|
|
|
json_reply(socket, &Response::custom(520, "command buffer overflow"));
|
2020-04-29 01:26:43 +08:00
|
|
|
} else {
|
2020-06-03 21:46:18 +08:00
|
|
|
let r = from_slice::<Request>(&self.data[..self.data.len() - 1]);
|
2020-04-29 01:26:43 +08:00
|
|
|
match r {
|
|
|
|
Ok(res) => {
|
2020-06-03 21:46:18 +08:00
|
|
|
let response = f(&res);
|
|
|
|
json_reply(socket, &response);
|
|
|
|
},
|
2020-04-29 01:26:43 +08:00
|
|
|
Err(err) => {
|
|
|
|
warn!("parse error {:?}", err);
|
2020-06-03 22:53:25 +08:00
|
|
|
json_reply(socket, &Response::custom(550, "parse error"));
|
2020-04-29 01:26:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 22:53:25 +08:00
|
|
|
self.data.clear();
|
2020-04-29 01:26:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|