Skip to content

Commit 464415b

Browse files
committed
Experimental tcp stuff. Does not currently work.
1 parent 9a65ced commit 464415b

File tree

6 files changed

+129
-7
lines changed

6 files changed

+129
-7
lines changed

examples/simple.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ extern crate libds;
33
use std::thread;
44
use std::time;
55

6-
use libds::{states::{RobotMode, Alliance}, DriverStation};
6+
use libds::{
7+
states::{Alliance, RobotMode},
8+
DriverStation,
9+
};
710

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

1215
ds.set_mode(RobotMode::Auto);
13-
ds.set_alliance(Alliance::Blue(2));
16+
ds.set_alliance(Alliance::Blue(2));
17+
ds.set_game_data("rrr".to_string());
1418

1519
println!("we connected");
1620
thread::sleep(time::Duration::from_millis(2000));

src/connection.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::io;
2-
use std::io::Read;
2+
use std::io::{Read, Write};
33
use std::net::{IpAddr, SocketAddr, TcpStream, UdpSocket};
44
use std::sync::{mpsc, Arc, Mutex};
55
use std::thread;
@@ -9,7 +9,7 @@ use std::time::{Duration, Instant};
99
use byteorder::{ByteOrder, NetworkEndian};
1010

1111
use ds::DriverStationState;
12-
use messages::rio::*;
12+
use messages::{ds::tcp::*, rio::*};
1313

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

49+
tcp.write(
50+
GameData::new(state.lock().unwrap().game_data.clone())
51+
.to_packet()
52+
.as_slice(),
53+
);
54+
4955
loop {
5056
match receiver_signal.try_recv() {
5157
Ok(Signal::Disconnect) | Err(mpsc::TryRecvError::Disconnected) => break,
58+
Ok(Signal::Tcp(tag)) => {
59+
match tcp.write(tag.to_packet().as_slice()) {
60+
Ok(n) => {}
61+
Err(e) => {} //TODO
62+
}
63+
}
5264
_ => {}
5365
}
5466

@@ -129,6 +141,10 @@ impl DSConnection {
129141
Ok(ior) => ior,
130142
}
131143
}
144+
145+
pub fn send_tcp(&self, tag: TcpTag) {
146+
self.sender.send(Signal::Tcp(tag)).unwrap();
147+
}
132148
}
133149

134150
impl Drop for DSConnection {
@@ -138,6 +154,6 @@ impl Drop for DSConnection {
138154
}
139155

140156
pub enum Signal {
141-
Tcp,
157+
Tcp(TcpTag),
142158
Disconnect,
143159
}

src/joystick.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
use packet::PacketWriter;
22

3+
pub enum AxisType {
4+
X = 0,
5+
Y = 1,
6+
Z = 2,
7+
Twist = 3,
8+
Throttle = 4,
9+
}
10+
311
#[derive(Clone)]
4-
enum JoystickType {
12+
pub enum JoystickType {
513
Unknown = -1,
614
}
715

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use messages::*;
2828
use packet::PacketWriter;
2929
use states::{Alliance, RobotMode};
3030

31+
use messages::ds::tcp::*;
32+
3133
pub struct DriverStation {
3234
state: Arc<Mutex<DriverStationState>>,
3335
connection: Option<DSConnection>,
@@ -72,6 +74,9 @@ impl DriverStation {
7274
}
7375

7476
pub fn set_game_data(&self, data: String) {
75-
self.state.lock().unwrap().game_data = data;
77+
self.state.lock().unwrap().game_data = data.clone();
78+
if let Some(ref conn) = self.connection {
79+
conn.send_tcp(TcpTag::GameData(GameData::new(data)));
80+
}
7681
}
7782
}

src/messages/ds.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,89 @@
1+
pub mod tcp {
2+
use joystick::{AxisType, JoystickType};
3+
use states::MatchType;
14

5+
pub trait Tag {
6+
fn id(&self) -> u8;
7+
8+
fn as_bytes(&self) -> Vec<u8>;
9+
10+
fn to_packet(&self) -> Vec<u8> {
11+
let mut buf = Vec::new();
12+
buf.push(self.id());
13+
buf.extend(self.as_bytes());
14+
let len = buf.len();
15+
buf.insert(0, len as u8);
16+
17+
buf
18+
}
19+
}
20+
21+
pub enum TcpTag {
22+
JoystickDescriptor(JoystickDescriptor),
23+
MatchInfo(MatchInfo),
24+
GameData(GameData),
25+
}
26+
27+
impl TcpTag {
28+
pub fn to_packet(&self) -> Vec<u8> {
29+
match self {
30+
TcpTag::JoystickDescriptor(jd) => Vec::new(), // TODO
31+
TcpTag::MatchInfo(mi) => mi.to_packet(),
32+
TcpTag::GameData(gd) => gd.to_packet(),
33+
}
34+
}
35+
}
36+
37+
pub struct JoystickDescriptor {
38+
index: u8,
39+
is_xbox: bool,
40+
stick_type: JoystickType,
41+
name: String,
42+
axis_count: u8,
43+
axis_types: Vec<AxisType>,
44+
button_count: u8,
45+
pov_count: u8,
46+
}
47+
48+
pub struct MatchInfo {
49+
competition: String,
50+
match_type: MatchType,
51+
}
52+
53+
impl Tag for MatchInfo {
54+
fn id(&self) -> u8 {
55+
0x07
56+
}
57+
58+
fn as_bytes(&self) -> Vec<u8> {
59+
let mut buf = Vec::new();
60+
buf.extend(self.competition.as_bytes());
61+
buf.push(self.match_type as u8);
62+
63+
buf
64+
}
65+
}
66+
67+
pub struct GameData {
68+
data: String,
69+
}
70+
71+
impl GameData {
72+
pub fn new(data: String) -> Self {
73+
Self { data }
74+
}
75+
}
76+
77+
impl Tag for GameData {
78+
fn id(&self) -> u8 {
79+
0x0e
80+
}
81+
82+
fn as_bytes(&self) -> Vec<u8> {
83+
let mut buf = Vec::new();
84+
buf.extend(self.data.as_bytes());
85+
86+
buf
87+
}
88+
}
89+
}

src/states.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl Alliance {
4141
}
4242
}
4343

44+
#[derive(Copy, Clone)]
4445
pub enum MatchType {
4546
None = 0,
4647
Practice = 1,

0 commit comments

Comments
 (0)