Skip to content

Commit

Permalink
firmware: adopt endianness protocol in artiq-zynq
Browse files Browse the repository at this point in the history
  • Loading branch information
occheung committed Sep 10, 2021
1 parent 9afe63c commit de0f2d4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
20 changes: 10 additions & 10 deletions artiq/firmware/libio/proto.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "alloc")]
use {core::str::Utf8Error, alloc::string::String};
use byteorder::{ByteOrder, NetworkEndian};
use byteorder::{ByteOrder, NativeEndian};

use ::{Read, Write, Error as IoError};

Expand Down Expand Up @@ -29,21 +29,21 @@ pub trait ProtoRead {
fn read_u16(&mut self) -> Result<u16, Self::ReadError> {
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<u32, Self::ReadError> {
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<u64, Self::ReadError> {
let mut bytes = [0; 8];
self.read_exact(&mut bytes)?;
Ok(NetworkEndian::read_u64(&bytes))
Ok(NativeEndian::read_u64(&bytes))
}

#[inline]
Expand Down Expand Up @@ -88,42 +88,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)
}

Expand Down
18 changes: 9 additions & 9 deletions artiq/firmware/libproto_artiq/rpc_proto.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::str;
use core::slice;
use cslice::{CSlice, CMutSlice};
use byteorder::{NetworkEndian, ByteOrder};
use byteorder::{NativeEndian, ByteOrder};
use io::{ProtoRead, Read, Write, ProtoWrite, Error};
use self::tag::{Tag, TagIterator, split_tag};

Expand Down Expand Up @@ -69,13 +69,13 @@ unsafe fn recv_value<R, E>(reader: &mut R, tag: Tag, data: &mut *mut (),
let dest = slice::from_raw_parts_mut(data as *mut u8, length * 4);
reader.read_exact(dest)?;
let dest = slice::from_raw_parts_mut(data as *mut i32, length);
NetworkEndian::from_slice_i32(dest);
NativeEndian::from_slice_i32(dest);
},
Tag::Int64 | Tag::Float64 => {
let dest = slice::from_raw_parts_mut(data as *mut u8, length * 8);
reader.read_exact(dest)?;
let dest = slice::from_raw_parts_mut(data as *mut i64, length);
NetworkEndian::from_slice_i64(dest);
NativeEndian::from_slice_i64(dest);
},
_ => {
for _ in 0..length {
Expand Down Expand Up @@ -109,13 +109,13 @@ unsafe fn recv_value<R, E>(reader: &mut R, tag: Tag, data: &mut *mut (),
let dest = slice::from_raw_parts_mut(data as *mut u8, length * 4);
reader.read_exact(dest)?;
let dest = slice::from_raw_parts_mut(data as *mut i32, length);
NetworkEndian::from_slice_i32(dest);
NativeEndian::from_slice_i32(dest);
},
Tag::Int64 | Tag::Float64 => {
let dest = slice::from_raw_parts_mut(data as *mut u8, length * 8);
reader.read_exact(dest)?;
let dest = slice::from_raw_parts_mut(data as *mut i64, length);
NetworkEndian::from_slice_i64(dest);
NativeEndian::from_slice_i64(dest);
},
_ => {
for _ in 0..length {
Expand Down Expand Up @@ -204,8 +204,8 @@ unsafe fn send_value<W>(writer: &mut W, tag: Tag, data: &mut *const ())
let mut data = (*ptr).elements;
writer.write_u8(tag.as_u8())?;
match tag {
// we cannot use NetworkEndian::from_slice_i32 as the data is not mutable,
// and that is not needed as the data is already in network endian
// we cannot use NativeEndian::from_slice_i32 as the data is not mutable,
// and that is not needed as the data is already in native endian
Tag::Bool => {
let slice = slice::from_raw_parts(data as *const u8, length);
writer.write_all(slice)?;
Expand Down Expand Up @@ -243,8 +243,8 @@ unsafe fn send_value<W>(writer: &mut W, tag: Tag, data: &mut *const ())
let mut data = *buffer;
writer.write_u8(elt_tag.as_u8())?;
match elt_tag {
// we cannot use NetworkEndian::from_slice_i32 as the data is not mutable,
// and that is not needed as the data is already in network endian
// we cannot use NativeEndian::from_slice_i32 as the data is not mutable,
// and that is not needed as the data is already in native endian
Tag::Bool => {
let slice = slice::from_raw_parts(data as *const u8, length);
writer.write_all(slice)?;
Expand Down
2 changes: 1 addition & 1 deletion artiq/firmware/runtime/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn worker(stream: &mut TcpStream) -> Result<(), IoError<SchedError>> {
};
debug!("{:?}", header);

stream.write_all("E".as_bytes())?;
stream.write_all("e".as_bytes())?;
header.write_to(stream)?;
if wraparound {
stream.write_all(&data[pointer..])?;
Expand Down
2 changes: 1 addition & 1 deletion artiq/firmware/runtime/mgmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl From<SchedError> for Error<SchedError> {

fn worker(io: &Io, stream: &mut TcpStream) -> Result<(), Error<SchedError>> {
read_magic(stream)?;
Write::write_all(stream, "E".as_bytes())?;
Write::write_all(stream, "e".as_bytes())?;
info!("new connection from {}", stream.remote_endpoint());

loop {
Expand Down
2 changes: 1 addition & 1 deletion artiq/firmware/runtime/moninj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn connection_worker(io: &Io, _aux_mutex: &Mutex, _routing_table: &drtio_routing
let mut next_check = 0;

read_magic(&mut stream)?;
stream.write_all("E".as_bytes())?;
stream.write_all("e".as_bytes())?;
info!("new connection from {}", stream.remote_endpoint());

loop {
Expand Down
6 changes: 3 additions & 3 deletions artiq/firmware/runtime/session.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::{mem, str, cell::{Cell, RefCell}, fmt::Write as FmtWrite};
use alloc::{vec::Vec, string::String};
use byteorder::{ByteOrder, NetworkEndian};
use byteorder::{ByteOrder, NativeEndian};

use io::{Read, Write, Error as IoError};
use board_misoc::{ident, cache, config};
Expand Down Expand Up @@ -473,7 +473,7 @@ fn process_kern_queued_rpc(stream: &mut TcpStream,
_session: &mut Session) -> Result<(), Error<SchedError>> {
rpc_queue::dequeue(|slice| {
debug!("comm<-kern (async RPC)");
let length = NetworkEndian::read_u32(slice) as usize;
let length = NativeEndian::read_u32(slice) as usize;
host_write(stream, host::Reply::RpcRequest { async: true })?;
debug!("{:?}", &slice[4..][..length]);
stream.write_all(&slice[4..][..length])?;
Expand Down Expand Up @@ -615,7 +615,7 @@ pub fn thread(io: Io, aux_mutex: &Mutex,
continue
}
}
match stream.write_all("E".as_bytes()) {
match stream.write_all("e".as_bytes()) {
Ok(()) => (),
Err(_) => {
warn!("cannot send endian byte");
Expand Down

0 comments on commit de0f2d4

Please sign in to comment.