Browse Source

server implementation with GPIO ports working

master
Michael Preisach 7 years ago
parent
commit
a24eb26837
  1. 7
      Cargo.lock
  2. 1
      Cargo.toml
  3. 8
      src/main.rs
  4. 82
      src/protocol.rs

7
Cargo.lock

@ -1,3 +1,8 @@
[[package]]
name = "gpio"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "1.3.0"
@ -12,6 +17,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "noobctf-server"
version = "0.1.0"
dependencies = [
"gpio 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rppal 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -25,6 +31,7 @@ dependencies = [
]
[metadata]
"checksum gpio 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4fe6783270536547ac473c9d2ae5a7e0e715ea43f29004ced47fbd1c1372d2c7"
"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
"checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917"
"checksum rppal 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "47fa3e09d34f2992f47f3d4315f8ee8cfffc1fe1912d4d08258b66cc18dc3b00"

1
Cargo.toml

@ -6,3 +6,4 @@ edition = "2018"
[dependencies]
rppal = "0.11.1"
gpio = "0.4.1"

8
src/main.rs

@ -5,14 +5,14 @@ use std::io::{BufReader, Result};
mod protocol;
fn handle_connection(mut stream: TcpStream) -> Result<()> {
fn handle_connection(stream: TcpStream) -> Result<()> {
let mut outstream = stream.try_clone()?;
let mut reader = BufReader::new(stream);
let mut text = String::new();
let mut prot = protocol::Protocol::new();
prot.start();
write!(outstream,"Hello to 7seg-writer!\n");
write!(outstream,"Hello to 7seg-writer!\n").expect("could not write to TCP Stream!");
while prot.is_running() {
let len = reader.read_line(&mut text).expect("Reading line from client failed!");
if len == 0 {
@ -21,11 +21,11 @@ fn handle_connection(mut stream: TcpStream) -> Result<()> {
println!("read: {}", text);
match prot.parse(&text) {
Ok(response) => {
writeln!(outstream,"{}",response);
writeln!(outstream,"{}",response).expect("could not write to TCP Stream!");
//stream.write_line(response.as_bytes());
},
Err(e) => {
writeln!(outstream,"{}",e);
writeln!(outstream,"{}",e).expect("could not write to TCP Stream!");
//stream.write_line(e.as_bytes());
}
}

82
src/protocol.rs

@ -1,17 +1,54 @@
extern crate gpio;
use gpio::GpioOut;
struct Segment {
nextval: bool,
port: gpio::sysfs::SysFsGpioOutput,
}
impl Segment {
pub fn new(portnum: u16) -> Segment {
Segment {
nextval: false,
port: gpio::sysfs::SysFsGpioOutput::open(portnum)
.expect("Startup failed, could not open all GPIOs"),
}
}
pub fn set_nextval(&mut self, value: bool) {
self.nextval = value;
}
pub fn show(&mut self) -> Result<(), &'static str> {
match self.port.set_value(self.nextval) {
Ok(_) => Ok(()),
Err(_) => Err("Could not write to GPIO port"),
}
}
}
pub struct Protocol {
segments: Vec<bool>,
segments: Vec<Segment>,
running: bool,
}
impl Protocol {
pub fn new() -> Protocol {
Protocol {
segments: [false, false, false, false, false, false, false, false].to_vec(),
segments: Vec::with_capacity(8),
running: false,
}
}
pub fn start(&mut self) {
self.segments[0] = Segment::new(5);
self.segments[1] = Segment::new(6);
self.segments[2] = Segment::new(13);
self.segments[3] = Segment::new(19);
self.segments[4] = Segment::new(26);
self.segments[5] = Segment::new(21);
self.segments[6] = Segment::new(20);
self.segments[7] = Segment::new(16);
self.running = true;
}
@ -19,39 +56,44 @@ impl Protocol {
self.running
}
pub fn get_segment(&self, index: usize) -> Result<bool, &'static str> {
if index < 8 && index >= 0 {
Ok(self.segments[index])
} else {
Err("get_segment: Index out of bounds")
}
}
pub fn parse(&mut self, input : &str) -> Result<&'static str, &'static str> {
let arguments:Vec<&str> = input.split_whitespace().collect();
if arguments.len() < 1 {
self.running = false;
return Err("No argument found!");
return Err("ERR Nmissing arguments");
}
match arguments[0] {
"setsegment" => {
if arguments.len() != 3 {
self.running = false;
return Err("Wrong number of arguments!");
return Err("ERR Wrong number of arguments");
}
let index = match arguments[1].parse::<usize>() {
Ok(value) => {
if value < 8 {
return Err("ERR index out of bounds");
} else {
value
}
let index = arguments[1].parse::<usize>().unwrap();
},
Err(_e) => return Err("ERR index not a number")
};
let value = match arguments[2] {
"on" => true,
"off" => false,
_ => self.segments[index],
_ => return Err("ERR no value for segment")
};
self.segments[index] = value;
self.segments[index].set_nextval(value);
}
"display" => {
//show the saved state on the display
print!("Showing state on display: ");
for seg in self.segments.clone() {
print!("{}, ", seg);
for i in 0 .. 7 {
match self.segments[i].show() {
Ok(_) => (),
Err(e) => println!("{}", e),
}
}
println!();
}
@ -61,9 +103,7 @@ impl Protocol {
self.running = false;
}
_ => {
println!("Err: Command not found, closing connection!");
self.running = false;
return Err("Command not found!");
return Err("ERR Command not found!");
}
}
Ok("OK")

Loading…
Cancel
Save