From faa335461d49405a21060a3fc2b6254929b0eb61 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Fri, 22 Jan 2021 13:36:38 +0800 Subject: [PATCH] runtime: modified protocols to use device endian --- src/runtime/src/analyzer.rs | 1 + src/runtime/src/comms.rs | 1 + src/runtime/src/mgmt.rs | 1 + src/runtime/src/moninj.rs | 1 + src/runtime/src/proto_async.rs | 8 ++--- src/runtime/src/proto_core_io.rs | 20 ++++++------- src/runtime/src/rpc.rs | 50 +++++++++----------------------- 7 files changed, 31 insertions(+), 51 deletions(-) diff --git a/src/runtime/src/analyzer.rs b/src/runtime/src/analyzer.rs index ed082e0c..20fbd9b5 100644 --- a/src/runtime/src/analyzer.rs +++ b/src/runtime/src/analyzer.rs @@ -50,6 +50,7 @@ struct Header { } async fn write_header(stream: &mut TcpStream, header: &Header) -> Result<(), Error> { + stream.send_slice("e".as_bytes()).await?; write_i32(stream, header.sent_bytes as i32).await?; write_i64(stream, header.total_byte_count as i64).await?; write_i8(stream, header.error_occurred as i8).await?; diff --git a/src/runtime/src/comms.rs b/src/runtime/src/comms.rs index d12e5781..bfb4b4aa 100644 --- a/src/runtime/src/comms.rs +++ b/src/runtime/src/comms.rs @@ -317,6 +317,7 @@ async fn handle_connection(stream: &TcpStream, control: Rc Result<()> if !expect(&stream, b"ARTIQ moninj\n").await? { return Err(Error::UnexpectedPattern); } + stream.send_slice("e".as_bytes()).await?; let mut probe_watch_list: BTreeMap<(i32, i8), Option> = BTreeMap::new(); let mut inject_watch_list: BTreeMap<(i32, i8), Option> = BTreeMap::new(); diff --git a/src/runtime/src/proto_async.rs b/src/runtime/src/proto_async.rs index 32cd23c9..f084eec9 100644 --- a/src/runtime/src/proto_async.rs +++ b/src/runtime/src/proto_async.rs @@ -54,13 +54,13 @@ pub async fn read_i8(stream: &TcpStream) -> Result { pub async fn read_i32(stream: &TcpStream) -> Result { let mut buffer: [u8; 4] = [0; 4]; read_chunk(stream, &mut buffer).await?; - Ok(i32::from_be_bytes(buffer)) + Ok(i32::from_le_bytes(buffer)) } pub async fn read_i64(stream: &TcpStream) -> Result { let mut buffer: [u8; 8] = [0; 8]; read_chunk(stream, &mut buffer).await?; - Ok(i64::from_be_bytes(buffer)) + Ok(i64::from_le_bytes(buffer)) } pub async fn read_chunk(stream: &TcpStream, destination: &mut [u8]) -> Result<()> { @@ -90,12 +90,12 @@ pub async fn write_bool(stream: &TcpStream, value: bool) -> Result<()> { } pub async fn write_i32(stream: &TcpStream, value: i32) -> Result<()> { - stream.send_slice(&value.to_be_bytes()).await?; + stream.send_slice(&value.to_le_bytes()).await?; Ok(()) } pub async fn write_i64(stream: &TcpStream, value: i64) -> Result<()> { - stream.send_slice(&value.to_be_bytes()).await?; + stream.send_slice(&value.to_le_bytes()).await?; Ok(()) } diff --git a/src/runtime/src/proto_core_io.rs b/src/runtime/src/proto_core_io.rs index 845d0424..c165606b 100644 --- a/src/runtime/src/proto_core_io.rs +++ b/src/runtime/src/proto_core_io.rs @@ -1,5 +1,5 @@ use core::str::Utf8Error; -use byteorder::{ByteOrder, NetworkEndian}; +use byteorder::{ByteOrder, NativeEndian}; use alloc::vec; use alloc::string::String; @@ -28,21 +28,21 @@ pub trait ProtoRead { fn read_u16(&mut self) -> Result { let mut bytes = [0; 2]; self.read_exact(&mut bytes)?; - Ok(NetworkEndian::read_u16(&bytes)) + Ok(NativeEndian::read_u16(&bytes)) } #[inline] fn read_u32(&mut self) -> Result { let mut bytes = [0; 4]; self.read_exact(&mut bytes)?; - Ok(NetworkEndian::read_u32(&bytes)) + Ok(NativeEndian::read_u32(&bytes)) } #[inline] fn read_u64(&mut self) -> Result { let mut bytes = [0; 8]; self.read_exact(&mut bytes)?; - Ok(NetworkEndian::read_u64(&bytes)) + Ok(NativeEndian::read_u64(&bytes)) } #[inline] @@ -85,42 +85,42 @@ pub trait ProtoWrite { #[inline] fn write_u16(&mut self, value: u16) -> Result<(), Self::WriteError> { let mut bytes = [0; 2]; - NetworkEndian::write_u16(&mut bytes, value); + NativeEndian::write_u16(&mut bytes, value); self.write_all(&bytes) } #[inline] fn write_i16(&mut self, value: i16) -> Result<(), Self::WriteError> { let mut bytes = [0; 2]; - NetworkEndian::write_i16(&mut bytes, value); + NativeEndian::write_i16(&mut bytes, value); self.write_all(&bytes) } #[inline] fn write_u32(&mut self, value: u32) -> Result<(), Self::WriteError> { let mut bytes = [0; 4]; - NetworkEndian::write_u32(&mut bytes, value); + NativeEndian::write_u32(&mut bytes, value); self.write_all(&bytes) } #[inline] fn write_i32(&mut self, value: i32) -> Result<(), Self::WriteError> { let mut bytes = [0; 4]; - NetworkEndian::write_i32(&mut bytes, value); + NativeEndian::write_i32(&mut bytes, value); self.write_all(&bytes) } #[inline] fn write_u64(&mut self, value: u64) -> Result<(), Self::WriteError> { let mut bytes = [0; 8]; - NetworkEndian::write_u64(&mut bytes, value); + NativeEndian::write_u64(&mut bytes, value); self.write_all(&bytes) } #[inline] fn write_i64(&mut self, value: i64) -> Result<(), Self::WriteError> { let mut bytes = [0; 8]; - NetworkEndian::write_i64(&mut bytes, value); + NativeEndian::write_i64(&mut bytes, value); self.write_all(&bytes) } diff --git a/src/runtime/src/rpc.rs b/src/runtime/src/rpc.rs index c6c30426..bb17f507 100644 --- a/src/runtime/src/rpc.rs +++ b/src/runtime/src/rpc.rs @@ -2,7 +2,7 @@ use core::str; use core::future::Future; use cslice::{CSlice, CMutSlice}; use log::trace; -use byteorder::{NetworkEndian, ByteOrder}; +use byteorder::{NativeEndian, ByteOrder}; use core_io::{Write, Error}; use libboard_zynq::smoltcp; @@ -96,7 +96,7 @@ async unsafe fn recv_value(stream: &TcpStream, tag: Tag<'async_recursion>, da proto_async::read_chunk(stream, dest).await?; drop(dest); let dest = core::slice::from_raw_parts_mut(ptr, length); - NetworkEndian::from_slice_u32(dest); + NativeEndian::from_slice_u32(dest); }, Tag::Int64 | Tag::Float64 => { let ptr = align_ptr_mut::(data); @@ -104,7 +104,7 @@ async unsafe fn recv_value(stream: &TcpStream, tag: Tag<'async_recursion>, da proto_async::read_chunk(stream, dest).await?; drop(dest); let dest = core::slice::from_raw_parts_mut(ptr, length); - NetworkEndian::from_slice_u64(dest); + NativeEndian::from_slice_u64(dest); }, _ => { for _ in 0..(*ptr).length as usize { @@ -141,7 +141,7 @@ async unsafe fn recv_value(stream: &TcpStream, tag: Tag<'async_recursion>, da proto_async::read_chunk(stream, dest).await?; drop(dest); let dest = core::slice::from_raw_parts_mut(ptr, length); - NetworkEndian::from_slice_u32(dest); + NativeEndian::from_slice_u32(dest); }, Tag::Int64 | Tag::Float64 => { let ptr = align_ptr_mut::(data); @@ -149,7 +149,7 @@ async unsafe fn recv_value(stream: &TcpStream, tag: Tag<'async_recursion>, da proto_async::read_chunk(stream, dest).await?; drop(dest); let dest = core::slice::from_raw_parts_mut(ptr, length); - NetworkEndian::from_slice_u64(dest); + NativeEndian::from_slice_u64(dest); }, _ => { for _ in 0..length { @@ -244,25 +244,13 @@ unsafe fn send_value(writer: &mut W, tag: Tag, data: &mut *const ()) }, Tag::Int32 => { let ptr1 = align_ptr::(data); - let slice = core::slice::from_raw_parts(ptr1, length as usize); - let mut v: alloc::vec::Vec = slice.to_vec(); - NetworkEndian::from_slice_i32(&mut v); - let slice2 = core::slice::from_raw_parts( - v.as_ptr() as usize as *const u8, - length as usize * 4 - ); - writer.write_all(slice2)?; + let slice = core::slice::from_raw_parts(ptr1 as *const u8, length as usize * 4); + writer.write_all(slice)?; }, Tag::Int64 | Tag::Float64 => { let ptr1 = align_ptr::(data); - let slice = core::slice::from_raw_parts(ptr1, length as usize); - let mut v: alloc::vec::Vec = slice.to_vec(); - NetworkEndian::from_slice_i64(&mut v); - let slice2 = core::slice::from_raw_parts( - v.as_ptr() as usize as *const u8, - length as usize * 8 - ); - writer.write_all(slice2)?; + let slice = core::slice::from_raw_parts(ptr1 as *const u8, length as usize * 8); + writer.write_all(slice)?; }, // non-primitive types, not sure if this would happen but we can handle it... _ => { @@ -297,25 +285,13 @@ unsafe fn send_value(writer: &mut W, tag: Tag, data: &mut *const ()) }, Tag::Int32 => { let ptr1 = align_ptr::(data); - let slice = core::slice::from_raw_parts(ptr1, length as usize); - let mut v: alloc::vec::Vec = slice.to_vec(); - NetworkEndian::from_slice_i32(&mut v); - let slice2 = core::slice::from_raw_parts( - v.as_ptr() as usize as *const u8, - length as usize * 4 - ); - writer.write_all(slice2)?; + let slice = core::slice::from_raw_parts(ptr1 as *const u8, length as usize * 4); + writer.write_all(slice)?; }, Tag::Int64 | Tag::Float64 => { let ptr1 = align_ptr::(data); - let slice = core::slice::from_raw_parts(ptr1, length as usize); - let mut v: alloc::vec::Vec = slice.to_vec(); - NetworkEndian::from_slice_i64(&mut v); - let slice2 = core::slice::from_raw_parts( - v.as_ptr() as usize as *const u8, - length as usize * 8 - ); - writer.write_all(slice2)?; + let slice = core::slice::from_raw_parts(ptr1 as *const u8, length as usize * 8); + writer.write_all(slice)?; }, // non-primitive types, not sure if this would happen but we can handle it... _ => {