Skip to content

Commit

Permalink
Things are broken.
Browse files Browse the repository at this point in the history
  • Loading branch information
grebneerg committed Dec 11, 2018
1 parent 464415b commit 355a7e2
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 75 deletions.
10 changes: 8 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::thread;
use std::time;

use libds::{
states::{Alliance, RobotMode},
messages::ds::tcp::MatchInfo,
states::{Alliance, MatchType, RobotMode},
DriverStation,
};

Expand All @@ -14,12 +15,17 @@ fn main() {

ds.set_mode(RobotMode::Auto);
ds.set_alliance(Alliance::Blue(2));
ds.set_game_data("rrr".to_string());
ds.set_game_data("lll".to_string());
ds.set_match_info(MatchInfo {
competition: "jack's super fun and definitely real frc competition".to_owned(),
match_type: MatchType::Elimination,
});

println!("we connected");
thread::sleep(time::Duration::from_millis(2000));
ds.set_enabled(true);
println!("we enabled");
ds.set_game_data("rrr".to_string());
thread::sleep(time::Duration::from_millis(2000));
ds.set_enabled(false);
println!("we disabled");
Expand Down
24 changes: 17 additions & 7 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,24 @@ impl DSConnection {
let mut tcp = TcpStream::connect(SocketAddr::new(addr.clone(), 1740)).unwrap();
tcp.set_nonblocking(true).unwrap();

println!(
"{:?}",
GameData::new(state.lock().unwrap().game_data.clone()).to_packet()
);
tcp.write(
GameData::new(state.lock().unwrap().game_data.clone())
.to_packet()
.as_slice(),
);
)
.unwrap();
tcp.write(state.lock().unwrap().match_info.clone().to_packet().as_slice()).unwrap();

loop {
match receiver_signal.try_recv() {
Ok(Signal::Disconnect) | Err(mpsc::TryRecvError::Disconnected) => break,
Ok(Signal::Tcp(tag)) => {
match tcp.write(tag.to_packet().as_slice()) {
Ok(n) => {}
Ok(n) => println!("wrote"),
Err(e) => {} //TODO
}
}
Expand All @@ -84,11 +90,14 @@ impl DSConnection {
match tcp.read_exact(&mut size_buf) {
Ok(_) => {
let size = NetworkEndian::read_u16(&size_buf);
println!("tcp size: {}", size);
let mut buf = vec![0u8; size as usize];
match tcp.read_exact(&mut buf) {
Ok(_) => if let Some(packet) = RioTcpPacket::from_bytes(buf) {
state.lock().unwrap().update_from_tcp(packet);
},
Ok(_) => {
if let Some(packet) = RioTcpPacket::from_bytes(buf) {
state.lock().unwrap().update_from_tcp(packet);
}
}
Err(e) => {
if e.kind() != io::ErrorKind::WouldBlock {
if let Err(e) = sender_res.send(Err(e)) {
Expand All @@ -109,8 +118,9 @@ impl DSConnection {

if last.elapsed() >= Duration::from_millis(20) {
last = Instant::now();
match udp.send(state.lock().unwrap().udp_packet().as_ref()) {
Ok(s) => {}
let packet = state.lock().unwrap().udp_packet();
match udp.send(packet.as_ref()) {
Ok(s) => println!("udp sent {:?}", packet),
Err(e) => {
if e.kind() != io::ErrorKind::WouldBlock {
if let Err(e) = sender_res.send(Err(e)) {
Expand Down
11 changes: 7 additions & 4 deletions src/ds.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use joystick::Joystick;
use messages::rio::*;
use messages::{ds::tcp::MatchInfo, rio::*};
use packet::PacketWriter;
use states::{Alliance, RobotMode};
use states::{Alliance, MatchType, RobotMode};

use chrono::prelude::*;

Expand All @@ -15,7 +15,7 @@ pub struct DriverStationState {
pub mode: RobotMode,
pub alliance: Alliance,
pub game_data: String,
pub competition: String,
pub match_info: MatchInfo,
sequence_num: u16,
request_time: bool,
}
Expand Down Expand Up @@ -116,7 +116,10 @@ impl Default for DriverStationState {
mode: RobotMode::Teleop,
alliance: Alliance::Red(1),
game_data: String::new(),
competition: String::from("unknown"),
match_info: MatchInfo {
competition: String::from("unknown"),
match_type: MatchType::None,
},
sequence_num: 0,
request_time: false,
}
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::thread::JoinHandle;
mod connection;
mod ds;
mod joystick;
mod messages;
pub mod messages; // change to just re-export
mod packet;
pub mod states;

Expand Down Expand Up @@ -79,4 +79,11 @@ impl DriverStation {
conn.send_tcp(TcpTag::GameData(GameData::new(data)));
}
}

pub fn set_match_info(&self, info: MatchInfo) {
self.state.lock().unwrap().match_info = info.clone();
if let Some(ref conn) = self.connection {
conn.send_tcp(TcpTag::MatchInfo(info));
}
}
}
14 changes: 10 additions & 4 deletions src/messages/ds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod tcp {
use joystick::{AxisType, JoystickType};
use states::MatchType;

use byteorder::{WriteBytesExt, NetworkEndian};

pub trait Tag {
fn id(&self) -> u8;

Expand All @@ -11,10 +13,13 @@ pub mod tcp {
let mut buf = Vec::new();
buf.push(self.id());
buf.extend(self.as_bytes());

let len = buf.len();
buf.insert(0, len as u8);
let mut packet = Vec::new();
packet.write_u16::<NetworkEndian>(len as u16);
packet.extend(buf);

buf
packet
}
}

Expand Down Expand Up @@ -45,9 +50,10 @@ pub mod tcp {
pov_count: u8,
}

#[derive(Clone)]
pub struct MatchInfo {
competition: String,
match_type: MatchType,
pub competition: String,
pub match_type: MatchType,
}

impl Tag for MatchInfo {
Expand Down
124 changes: 67 additions & 57 deletions src/messages/rio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,63 +133,73 @@ impl RioTcpPacket {
let size = packet.len();
packet.extract_string(size).unwrap()
})),
0x04 => if packet.len() != 4 {
None
} else {
Some(DisableFaults {
comms: packet.next_u16().unwrap(),
twelve_v: packet.next_u16().unwrap(),
})
},
0x05 => if packet.len() != 6 {
None
} else {
Some(RailFaults {
six_v: packet.next_u16().unwrap(),
five_v: packet.next_u16().unwrap(),
three_point_three_v: packet.next_u16().unwrap(),
})
},
0x0a => if packet.len() < 5 {
None
} else {
None // TODO
},
0x0b => if packet.len() < 16 {
None
} else {
Some(ErrorMessage {
timestamp: packet.next_f32().unwrap(),
sequence_number: packet.next_u16().unwrap(),
print_msg: packet.next_u8().unwrap() == 0x01,
error_code: packet.next_u16().unwrap(),
is_error: packet.next_u8().unwrap() != 0,
details: {
let size = packet.next_u16().unwrap() as usize;
packet.extract_string(size).unwrap()
},
location: {
let size = packet.next_u16().unwrap() as usize;
packet.extract_string(size).unwrap()
},
call_stack: {
let size = packet.next_u16().unwrap() as usize;
packet.extract_string(size).unwrap()
},
})
},
0x0c => if packet.len() < 6 {
None
} else {
Some(StandardOutput {
timestamp: packet.next_f32().unwrap(),
sequence_number: packet.next_u16().unwrap(),
message: {
let size = packet.len();
packet.extract_string(size).unwrap()
},
})
},
0x04 => {
if packet.len() != 4 {
None
} else {
Some(DisableFaults {
comms: packet.next_u16().unwrap(),
twelve_v: packet.next_u16().unwrap(),
})
}
}
0x05 => {
if packet.len() != 6 {
None
} else {
Some(RailFaults {
six_v: packet.next_u16().unwrap(),
five_v: packet.next_u16().unwrap(),
three_point_three_v: packet.next_u16().unwrap(),
})
}
}
0x0a => {
if packet.len() < 5 {
None
} else {
None // TODO
}
}
0x0b => {
if packet.len() < 16 {
None
} else {
Some(ErrorMessage {
timestamp: packet.next_f32().unwrap(),
sequence_number: packet.next_u16().unwrap(),
print_msg: packet.next_u8().unwrap() == 0x01,
error_code: packet.next_u16().unwrap(),
is_error: packet.next_u8().unwrap() != 0,
details: {
let size = packet.next_u16().unwrap() as usize;
packet.extract_string(size).unwrap()
},
location: {
let size = packet.next_u16().unwrap() as usize;
packet.extract_string(size).unwrap()
},
call_stack: {
let size = packet.next_u16().unwrap() as usize;
packet.extract_string(size).unwrap()
},
})
}
}
0x0c => {
if packet.len() < 6 {
None
} else {
Some(StandardOutput {
timestamp: packet.next_f32().unwrap(),
sequence_number: packet.next_u16().unwrap(),
message: {
let size = packet.len();
packet.extract_string(size).unwrap()
},
})
}
}
_ => None,
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/states.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[repr(u8)]
#[derive(Copy, Clone)]
pub enum RobotMode {
Teleop = 0,
Expand Down Expand Up @@ -41,6 +42,7 @@ impl Alliance {
}
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum MatchType {
None = 0,
Expand Down

0 comments on commit 355a7e2

Please sign in to comment.