Skip to content

Commit

Permalink
try initial bitstream io
Browse files Browse the repository at this point in the history
  • Loading branch information
jr1221 committed May 19, 2024
1 parent b70a61b commit fc37e6a
Showing 1 changed file with 44 additions and 50 deletions.
94 changes: 44 additions & 50 deletions cangen/RustSynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def parse_messages(self, msgs: List[CANMsg]) -> Result:
"""
result = Result("", "")
result.decode_data += RustSnippets.ignore_clippy
result.decode_data += RustSnippets.bitreader_impl
# result.decode_data += RustSnippets.bitreader_impl
result.decode_data += RustSnippets.format_impl
result.decode_data += RustSnippets.decode_data_import
result.decode_data += RustSnippets.decode_mock
Expand Down Expand Up @@ -139,19 +139,10 @@ def parse_decoders(self, field: CANPoint) -> str:
Helper function that parses the decoders for a given CANUnit by applying the
decoders to the data and casting the result to the final type of the CANUnit.
"""
base = f"reader.read_bits({field.size})"

# aware conversion from network to platform endianness
if field.endianness == "little":
base = f"(u{field.get_size_bits()}::from_be({base} as u{field.get_size_bits()}) as i{field.get_size_bits()})"
elif field.endianness == "big":
base = f"u32::from_le({base})"
else:
print("Invalid endianness on point!")
exit(1)

# if field.signed or field.endianness == "little":
# base = f"({base} as i{field.get_size_bits()})"
size = field.size
if field.size < 8:
size = 8
base = f"reader.read::<u{size}>({field.size}).unwrap()"

return f"{base} as {field.final_type}"

Expand All @@ -175,40 +166,40 @@ class RustSnippets:
"#![allow(clippy::all)]\n" # Ignoring clippy for decode_data because it's autogenerated and has some unnecessary type casting to ensure correct types
)

bitreader_impl: str = ("""
/* Struct to decode a byte array bit by bit, sequentially */
struct BitReader<'a> {
data: &'a [u8],
byte_pos: usize,
bit_pos: u8,
}
impl<'a> BitReader<'a> {
fn new(data: &'a [u8]) -> Self {
Self {
data,
byte_pos: 0,
bit_pos: 0,
}
}
fn read_bits(&mut self, num_bits: u8) -> u32 {
let mut value: u32 = 0;
for _ in 0..num_bits {
let bit = (self.data[self.byte_pos] >> (7 - self.bit_pos)) & 1;
value = (value << 1) | bit as u32;
self.bit_pos += 1;
if self.bit_pos == 8 {
self.bit_pos = 0;
self.byte_pos += 1;
}
}
value
}
}
"""
)
# bitreader_impl: str = ("""
# /* Struct to decode a byte array bit by bit, sequentially */
# struct BitReader<'a> {
# data: &'a [u8],
# byte_pos: usize,
# bit_pos: u8,
# }

# impl<'a> BitReader<'a> {
# fn new(data: &'a [u8]) -> Self {
# Self {
# data,
# byte_pos: 0,
# bit_pos: 0,
# }
# }

# fn read_bits(&mut self, num_bits: u8) -> u32 {
# let mut value: u32 = 0;
# for _ in 0..num_bits {
# let bit = (self.data[self.byte_pos] >> (7 - self.bit_pos)) & 1;
# value = (value << 1) | bit as u32;

# self.bit_pos += 1;
# if self.bit_pos == 8 {
# self.bit_pos = 0;
# self.byte_pos += 1;
# }
# }
# value
# }
# }
# """
# )

format_impl = """
/**
Expand All @@ -235,10 +226,13 @@ class RustSnippets:
}
}"""

bitreader_create = " let mut reader = BitReader::new(data);"
bitreader_create = " let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);"

decode_data_import: str = (
"use super::data::Data;\n" # Importing the Data struct and the FormatData and ProcessData traits
"""use super::data::Data;
use std::io::{Read, Cursor};
use bitstream_io::{BigEndian, BitReader, BitRead};\n
""" # Importing the Data struct and the FormatData and ProcessData traits
)

decode_return_type: str = "Vec::<Data>" # The return type of any decode function
Expand Down

0 comments on commit fc37e6a

Please sign in to comment.