Commit fe339a93 authored by Jeremy Soller's avatar Jeremy Soller Committed by GitHub

Redo networking (#22)

* Rewriting network functions

* Add buffer to dup
Fix non-blocking handling by triggering once on enabling events to read to EOF

* Modifications for UDP API

* Implement TCP client side

* Add active close

* Add DMAR parser

* Implement basic TCP listening. Need to improve the state machine

* Reduce debugging

* Fixes for close procedure

* Updates to fix path processing in libstd
parent 891a8a85
......@@ -52,10 +52,14 @@ fn main() {
let mut event = Event::default();
event_file.read(&mut event).expect("ahcid: failed to read event file");
if event.id == socket_fd {
let mut packet = Packet::default();
socket.read(&mut packet).expect("ahcid: failed to read disk scheme");
scheme.handle(&mut packet);
socket.write(&mut packet).expect("ahcid: failed to write disk scheme");
loop {
let mut packet = Packet::default();
if socket.read(&mut packet).expect("ahcid: failed to read disk scheme") == 0 {
break;
}
scheme.handle(&mut packet);
socket.write(&mut packet).expect("ahcid: failed to write disk scheme");
}
} else if event.id == irq_fd {
let mut irq = [0; 8];
if irq_file.read(&mut irq).expect("ahcid: failed to read irq file") >= irq.len() {
......
......@@ -47,7 +47,7 @@ impl Scheme for DiskScheme {
}
}
fn dup(&self, id: usize) -> Result<usize> {
fn dup(&self, id: usize, _buf: &[u8]) -> Result<usize> {
let mut handles = self.handles.lock();
let new_handle = {
let handle = handles.get(&id).ok_or(Error::new(EBADF))?;
......
......@@ -109,7 +109,7 @@ impl Scheme for Intel8254x {
}
}
fn dup(&self, id: usize) -> Result<usize> {
fn dup(&self, id: usize, _buf: &[u8]) -> Result<usize> {
Ok(id)
}
......
......@@ -80,16 +80,20 @@ fn main() {
let socket_packet = socket.clone();
event_queue.add(socket_fd, move |_count: usize| -> Result<Option<usize>> {
let mut packet = Packet::default();
socket_packet.borrow_mut().read(&mut packet)?;
let a = packet.a;
device.handle(&mut packet);
if packet.a == (-EWOULDBLOCK) as usize {
packet.a = a;
todo.borrow_mut().push(packet);
} else {
socket_packet.borrow_mut().write(&mut packet)?;
loop {
let mut packet = Packet::default();
if socket_packet.borrow_mut().read(&mut packet)? == 0 {
break;
}
let a = packet.a;
device.handle(&mut packet);
if packet.a == (-EWOULDBLOCK) as usize {
packet.a = a;
todo.borrow_mut().push(packet);
} else {
socket_packet.borrow_mut().write(&mut packet)?;
}
}
let next_read = device.next_read();
......@@ -100,10 +104,23 @@ fn main() {
Ok(None)
}).expect("e1000d: failed to catch events on IRQ file");
for event_count in event_queue.trigger_all(0).expect("e1000d: failed to trigger events") {
socket.borrow_mut().write(&Packet {
id: 0,
pid: 0,
uid: 0,
gid: 0,
a: syscall::number::SYS_FEVENT,
b: 0,
c: syscall::flag::EVENT_READ,
d: event_count
}).expect("e1000d: failed to write event");
}
loop {
let event_count = event_queue.run().expect("e1000d: failed to handle events");
let event_packet = Packet {
socket.borrow_mut().write(&Packet {
id: 0,
pid: 0,
uid: 0,
......@@ -112,9 +129,7 @@ fn main() {
b: 0,
c: syscall::flag::EVENT_READ,
d: event_count
};
socket.borrow_mut().write(&event_packet).expect("vesad: failed to write display event");
}).expect("e1000d: failed to write event");
}
}
unsafe { let _ = syscall::physunmap(address); }
......
......@@ -32,6 +32,92 @@ bitflags! {
}
}
struct Ps2d {
input: File,
lshift: bool,
rshift: bool,
packets: [u8; 4],
packet_i: usize,
extra_packet: bool
}
impl Ps2d {
fn new(input: File, extra_packet: bool) -> Self {
Ps2d {
input: input,
lshift: false,
rshift: false,
packets: [0; 4],
packet_i: 0,
extra_packet: extra_packet
}
}
fn handle(&mut self, keyboard: bool, data: u8) {
if keyboard {
let (scancode, pressed) = if data >= 0x80 {
(data - 0x80, false)
} else {
(data, true)
};
if scancode == 0x2A {
self.lshift = pressed;
} else if scancode == 0x36 {
self.rshift = pressed;
}
self.input.write(&KeyEvent {
character: keymap::get_char(scancode, self.lshift || self.rshift),
scancode: scancode,
pressed: pressed
}.to_event()).expect("ps2d: failed to write key event");
} else {
self.packets[self.packet_i] = data;
self.packet_i += 1;
let flags = MousePacketFlags::from_bits_truncate(self.packets[0]);
if ! flags.contains(ALWAYS_ON) {
println!("MOUSE MISALIGN {:X}", self.packets[0]);
self.packets = [0; 4];
self.packet_i = 0;
} else if self.packet_i >= self.packets.len() || (!self.extra_packet && self.packet_i >= 3) {
if ! flags.contains(X_OVERFLOW) && ! flags.contains(Y_OVERFLOW) {
let mut dx = self.packets[1] as i32;
if flags.contains(X_SIGN) {
dx -= 0x100;
}
let mut dy = -(self.packets[2] as i32);
if flags.contains(Y_SIGN) {
dy += 0x100;
}
let _extra = if self.extra_packet {
self.packets[3]
} else {
0
};
self.input.write(&MouseEvent {
x: dx,
y: dy,
left_button: flags.contains(LEFT_BUTTON),
middle_button: flags.contains(MIDDLE_BUTTON),
right_button: flags.contains(RIGHT_BUTTON)
}.to_event()).expect("ps2d: failed to write mouse event");
} else {
println!("ps2d: overflow {:X} {:X} {:X} {:X}", self.packets[0], self.packets[1], self.packets[2], self.packets[3]);
}
self.packets = [0; 4];
self.packet_i = 0;
}
}
}
}
fn main() {
thread::spawn(|| {
unsafe {
......@@ -39,9 +125,11 @@ fn main() {
asm!("cli" :::: "intel", "volatile");
}
let input = File::open("display:input").expect("ps2d: failed to open display:input");
let extra_packet = controller::Ps2::new().init();
let mut input = File::open("display:input").expect("ps2d: failed to open display:input");
let mut ps2d = Ps2d::new(input, extra_packet);
let mut event_queue = EventQueue::<(bool, u8)>::new().expect("ps2d: failed to create event queue");
......@@ -81,75 +169,13 @@ fn main() {
}
}).expect("ps2d: failed to poll irq:12");
let mut lshift = false;
let mut rshift = false;
let mut packets = [0; 4];
let mut packet_i = 0;
for (keyboard, data) in event_queue.trigger_all(0).expect("ps2d: failed to trigger events") {
ps2d.handle(keyboard, data);
}
loop {
let (keyboard, data) = event_queue.run().expect("ps2d: failed to handle events");
if keyboard {
let (scancode, pressed) = if data >= 0x80 {
(data - 0x80, false)
} else {
(data, true)
};
if scancode == 0x2A {
lshift = pressed;
} else if scancode == 0x36 {
rshift = pressed;
}
input.write(&KeyEvent {
character: keymap::get_char(scancode, lshift || rshift),
scancode: scancode,
pressed: pressed
}.to_event()).expect("ps2d: failed to write key event");
} else {
packets[packet_i] = data;
packet_i += 1;
let flags = MousePacketFlags::from_bits_truncate(packets[0]);
if ! flags.contains(ALWAYS_ON) {
println!("MOUSE MISALIGN {:X}", packets[0]);
packets = [0; 4];
packet_i = 0;
} else if packet_i >= packets.len() || (!extra_packet && packet_i >= 3) {
if ! flags.contains(X_OVERFLOW) && ! flags.contains(Y_OVERFLOW) {
let mut dx = packets[1] as i32;
if flags.contains(X_SIGN) {
dx -= 0x100;
}
let mut dy = -(packets[2] as i32);
if flags.contains(Y_SIGN) {
dy += 0x100;
}
let _extra = if extra_packet {
packets[3]
} else {
0
};
input.write(&MouseEvent {
x: dx,
y: dy,
left_button: flags.contains(LEFT_BUTTON),
middle_button: flags.contains(MIDDLE_BUTTON),
right_button: flags.contains(RIGHT_BUTTON)
}.to_event()).expect("ps2d: failed to write mouse event");
} else {
println!("ps2d: overflow {:X} {:X} {:X} {:X}", packets[0], packets[1], packets[2], packets[3]);
}
packets = [0; 4];
packet_i = 0;
}
}
ps2d.handle(keyboard, data);
}
});
}
......@@ -84,7 +84,7 @@ impl SchemeMut for Rtl8168 {
}
}
fn dup(&mut self, id: usize) -> Result<usize> {
fn dup(&mut self, id: usize, _buf: &[u8]) -> Result<usize> {
Ok(id)
}
......
......@@ -85,16 +85,20 @@ fn main() {
let socket_fd = socket.borrow().as_raw_fd();
let socket_packet = socket.clone();
event_queue.add(socket_fd, move |_count: usize| -> Result<Option<usize>> {
let mut packet = Packet::default();
socket_packet.borrow_mut().read(&mut packet)?;
let a = packet.a;
device.borrow_mut().handle(&mut packet);
if packet.a == (-EWOULDBLOCK) as usize {
packet.a = a;
todo.borrow_mut().push(packet);
} else {
socket_packet.borrow_mut().write(&mut packet)?;
loop {
let mut packet = Packet::default();
if socket_packet.borrow_mut().read(&mut packet)? == 0 {
break;
}
let a = packet.a;
device.borrow_mut().handle(&mut packet);
if packet.a == (-EWOULDBLOCK) as usize {
packet.a = a;
todo.borrow_mut().push(packet);
} else {
socket_packet.borrow_mut().write(&mut packet)?;
}
}
let next_read = device.borrow().next_read();
......@@ -105,10 +109,23 @@ fn main() {
Ok(None)
}).expect("rtl8168d: failed to catch events on IRQ file");
for event_count in event_queue.trigger_all(0).expect("rtl8168d: failed to trigger events") {
socket.borrow_mut().write(&Packet {
id: 0,
pid: 0,
uid: 0,
gid: 0,
a: syscall::number::SYS_FEVENT,
b: 0,
c: syscall::flag::EVENT_READ,
d: event_count
}).expect("rtl8168d: failed to write event");
}
loop {
let event_count = event_queue.run().expect("rtl8168d: failed to handle events");
let event_packet = Packet {
socket.borrow_mut().write(&Packet {
id: 0,
pid: 0,
uid: 0,
......@@ -117,9 +134,7 @@ fn main() {
b: 0,
c: syscall::flag::EVENT_READ,
d: event_count
};
socket.borrow_mut().write(&event_packet).expect("vesad: failed to write display event");
}).expect("rtl8168d: failed to write event");
}
}
unsafe { let _ = syscall::physunmap(address); }
......
......@@ -63,7 +63,6 @@ fn main() {
loop {
let mut packet = Packet::default();
socket.read(&mut packet).expect("vesad: failed to read display scheme");
//println!("vesad: {:?}", packet);
// If it is a read packet, and there is no data, block it. Otherwise, handle packet
if packet.a == syscall::number::SYS_READ && packet.d > 0 && scheme.will_block(packet.b) {
......
......@@ -60,7 +60,7 @@ impl SchemeMut for DisplayScheme {
}
}
fn dup(&mut self, id: usize) -> Result<usize> {
fn dup(&mut self, id: usize, _buf: &[u8]) -> Result<usize> {
Ok(id)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment