Skip to content

Commit

Permalink
Experimental tcp stuff. Does not currently work.
Browse files Browse the repository at this point in the history
  • Loading branch information
grebneerg committed Dec 2, 2018
1 parent 9a65ced commit 464415b
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 7 deletions.
8 changes: 6 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ extern crate libds;
use std::thread;
use std::time;

use libds::{states::{RobotMode, Alliance}, DriverStation};
use libds::{
states::{Alliance, RobotMode},
DriverStation,
};

fn main() {
let mut ds = DriverStation::new();
ds.connect([169, 254, 204, 207].into()).unwrap();

ds.set_mode(RobotMode::Auto);
ds.set_alliance(Alliance::Blue(2));
ds.set_alliance(Alliance::Blue(2));
ds.set_game_data("rrr".to_string());

println!("we connected");
thread::sleep(time::Duration::from_millis(2000));
Expand Down
22 changes: 19 additions & 3 deletions src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io;
use std::io::Read;
use std::io::{Read, Write};
use std::net::{IpAddr, SocketAddr, TcpStream, UdpSocket};
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
Expand All @@ -9,7 +9,7 @@ use std::time::{Duration, Instant};
use byteorder::{ByteOrder, NetworkEndian};

use ds::DriverStationState;
use messages::rio::*;
use messages::{ds::tcp::*, rio::*};

pub struct DSConnection {
thread: JoinHandle<()>,
Expand Down Expand Up @@ -46,9 +46,21 @@ impl DSConnection {
let mut tcp = TcpStream::connect(SocketAddr::new(addr.clone(), 1740)).unwrap();
tcp.set_nonblocking(true).unwrap();

tcp.write(
GameData::new(state.lock().unwrap().game_data.clone())
.to_packet()
.as_slice(),
);

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) => {}
Err(e) => {} //TODO
}
}
_ => {}
}

Expand Down Expand Up @@ -129,6 +141,10 @@ impl DSConnection {
Ok(ior) => ior,
}
}

pub fn send_tcp(&self, tag: TcpTag) {
self.sender.send(Signal::Tcp(tag)).unwrap();
}
}

impl Drop for DSConnection {
Expand All @@ -138,6 +154,6 @@ impl Drop for DSConnection {
}

pub enum Signal {
Tcp,
Tcp(TcpTag),
Disconnect,
}
10 changes: 9 additions & 1 deletion src/joystick.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use packet::PacketWriter;

pub enum AxisType {
X = 0,
Y = 1,
Z = 2,
Twist = 3,
Throttle = 4,
}

#[derive(Clone)]
enum JoystickType {
pub enum JoystickType {
Unknown = -1,
}

Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use messages::*;
use packet::PacketWriter;
use states::{Alliance, RobotMode};

use messages::ds::tcp::*;

pub struct DriverStation {
state: Arc<Mutex<DriverStationState>>,
connection: Option<DSConnection>,
Expand Down Expand Up @@ -72,6 +74,9 @@ impl DriverStation {
}

pub fn set_game_data(&self, data: String) {
self.state.lock().unwrap().game_data = data;
self.state.lock().unwrap().game_data = data.clone();
if let Some(ref conn) = self.connection {
conn.send_tcp(TcpTag::GameData(GameData::new(data)));
}
}
}
88 changes: 88 additions & 0 deletions src/messages/ds.rs
Original file line number Diff line number Diff line change
@@ -1 +1,89 @@
pub mod tcp {
use joystick::{AxisType, JoystickType};
use states::MatchType;

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

fn as_bytes(&self) -> Vec<u8>;

fn to_packet(&self) -> Vec<u8> {
let mut buf = Vec::new();
buf.push(self.id());
buf.extend(self.as_bytes());
let len = buf.len();
buf.insert(0, len as u8);

buf
}
}

pub enum TcpTag {
JoystickDescriptor(JoystickDescriptor),
MatchInfo(MatchInfo),
GameData(GameData),
}

impl TcpTag {
pub fn to_packet(&self) -> Vec<u8> {
match self {
TcpTag::JoystickDescriptor(jd) => Vec::new(), // TODO
TcpTag::MatchInfo(mi) => mi.to_packet(),
TcpTag::GameData(gd) => gd.to_packet(),
}
}
}

pub struct JoystickDescriptor {
index: u8,
is_xbox: bool,
stick_type: JoystickType,
name: String,
axis_count: u8,
axis_types: Vec<AxisType>,
button_count: u8,
pov_count: u8,
}

pub struct MatchInfo {
competition: String,
match_type: MatchType,
}

impl Tag for MatchInfo {
fn id(&self) -> u8 {
0x07
}

fn as_bytes(&self) -> Vec<u8> {
let mut buf = Vec::new();
buf.extend(self.competition.as_bytes());
buf.push(self.match_type as u8);

buf
}
}

pub struct GameData {
data: String,
}

impl GameData {
pub fn new(data: String) -> Self {
Self { data }
}
}

impl Tag for GameData {
fn id(&self) -> u8 {
0x0e
}

fn as_bytes(&self) -> Vec<u8> {
let mut buf = Vec::new();
buf.extend(self.data.as_bytes());

buf
}
}
}
1 change: 1 addition & 0 deletions src/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl Alliance {
}
}

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

0 comments on commit 464415b

Please sign in to comment.