Skip to content

Commit

Permalink
initial bidirectional config
Browse files Browse the repository at this point in the history
  • Loading branch information
jr1221 committed Jun 26, 2024
1 parent 3f9dcf7 commit 86857d4
Show file tree
Hide file tree
Showing 14 changed files with 569 additions and 139 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@ __pycache__/
build/
dist/
logs/
master_mapping.rs

mod.rs

# cangen files generaed by calypsogen.py
decode_master_mapping.rs
decode_data.rs
encode_data.rs
encode_master_mapping.rs
format_data.rs
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ paho-mqtt = "0.12.5"
protobuf-codegen = "3.3.0"
protobuf = "3.3.0"
bitstream-io = "2.3.0"


[build-dependencies]
protobuf-codegen = "3.3.0"
10 changes: 10 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ fn main() {
std::process::exit(1);
}
}

protobuf_codegen::Codegen::new()
.pure()
// All inputs and imports from the inputs must reside in `includes` directories.
.includes(&["src/proto"])
// Inputs must reside in some of include paths.
.input("src/proto/command_data.proto")
// Specify output directory relative to Cargo output directory.
.out_dir("src")
.run_from_script();
}
24 changes: 21 additions & 3 deletions calypsogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,42 @@
spec.loader.exec_module(cangen)

decode_data = open("./src/decode_data.rs", "w")
master_mapping = open("./src/master_mapping.rs", "w")
decode_master_mapping = open("./src/decode_master_mapping.rs", "w")

encode_data = open("./src/encode_data.rs", "w")
encode_master_mapping = open("./src/encode_master_mapping.rs", "w")

format_data = open("./src/format_data.rs", "w")


bms_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/bms.yaml", "r"))
mpu_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/mpu.yaml", "r"))
wheel_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/wheel.yaml", "r"))
dti_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/dti.yaml", "r"))
calypso_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/calypso_cmd.yaml", "r"))
charger_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/charger.yaml", "r"))


bms_messages.msgs.extend(mpu_messages.msgs)
bms_messages.msgs.extend(wheel_messages.msgs)
bms_messages.msgs.extend(dti_messages.msgs)
bms_messages.msgs.extend(charger_messages.msgs)
bms_messages.msgs.extend(calypso_messages.msgs)

result = cangen.RustSynth().parse_messages(bms_messages.msgs)

decode_data.write(result.decode_data)
decode_data.close()

master_mapping.write(result.master_mapping)
master_mapping.close()
decode_master_mapping.write(result.decode_master_mapping)
decode_master_mapping.close()

encode_data.write(result.encode_data)
encode_data.close()

encode_master_mapping.write(result.encode_master_mapping)
encode_master_mapping.close()

format_data.write(result.format_data)
format_data.close()

183 changes: 183 additions & 0 deletions src/command_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// This file is generated by rust-protobuf 3.3.0. Do not edit
// .proto file is parsed by pure
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
#![allow(unknown_lints)]
#![allow(clippy::all)]

#![allow(unused_attributes)]
#![cfg_attr(rustfmt, rustfmt::skip)]

#![allow(box_pointers)]
#![allow(dead_code)]
#![allow(missing_docs)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(trivial_casts)]
#![allow(unused_results)]
#![allow(unused_mut)]

//! Generated file from `command_data.proto`
/// Generated files are compatible only with the same version
/// of protobuf runtime.
const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_3_3_0;

// @@protoc_insertion_point(message:command_data.v1.CommandData)
#[derive(PartialEq,Clone,Default,Debug)]
pub struct CommandData {
// message fields
// @@protoc_insertion_point(field:command_data.v1.CommandData.data)
pub data: ::std::vec::Vec<f32>,
// special fields
// @@protoc_insertion_point(special_field:command_data.v1.CommandData.special_fields)
pub special_fields: ::protobuf::SpecialFields,
}

impl<'a> ::std::default::Default for &'a CommandData {
fn default() -> &'a CommandData {
<CommandData as ::protobuf::Message>::default_instance()
}
}

impl CommandData {
pub fn new() -> CommandData {
::std::default::Default::default()
}

fn generated_message_descriptor_data() -> ::protobuf::reflect::GeneratedMessageDescriptorData {
let mut fields = ::std::vec::Vec::with_capacity(1);
let mut oneofs = ::std::vec::Vec::with_capacity(0);
fields.push(::protobuf::reflect::rt::v2::make_vec_simpler_accessor::<_, _>(
"data",
|m: &CommandData| { &m.data },
|m: &mut CommandData| { &mut m.data },
));
::protobuf::reflect::GeneratedMessageDescriptorData::new_2::<CommandData>(
"CommandData",
fields,
oneofs,
)
}
}

impl ::protobuf::Message for CommandData {
const NAME: &'static str = "CommandData";

fn is_initialized(&self) -> bool {
true
}

fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::Result<()> {
while let Some(tag) = is.read_raw_tag_or_eof()? {
match tag {
10 => {
is.read_repeated_packed_float_into(&mut self.data)?;
},
13 => {
self.data.push(is.read_float()?);
},
tag => {
::protobuf::rt::read_unknown_or_skip_group(tag, is, self.special_fields.mut_unknown_fields())?;
},
};
}
::std::result::Result::Ok(())
}

// Compute sizes of nested messages
#[allow(unused_variables)]
fn compute_size(&self) -> u64 {
let mut my_size = 0;
my_size += 5 * self.data.len() as u64;
my_size += ::protobuf::rt::unknown_fields_size(self.special_fields.unknown_fields());
self.special_fields.cached_size().set(my_size as u32);
my_size
}

fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::Result<()> {
for v in &self.data {
os.write_float(1, *v)?;
};
os.write_unknown_fields(self.special_fields.unknown_fields())?;
::std::result::Result::Ok(())
}

fn special_fields(&self) -> &::protobuf::SpecialFields {
&self.special_fields
}

fn mut_special_fields(&mut self) -> &mut ::protobuf::SpecialFields {
&mut self.special_fields
}

fn new() -> CommandData {
CommandData::new()
}

fn clear(&mut self) {
self.data.clear();
self.special_fields.clear();
}

fn default_instance() -> &'static CommandData {
static instance: CommandData = CommandData {
data: ::std::vec::Vec::new(),
special_fields: ::protobuf::SpecialFields::new(),
};
&instance
}
}

impl ::protobuf::MessageFull for CommandData {
fn descriptor() -> ::protobuf::reflect::MessageDescriptor {
static descriptor: ::protobuf::rt::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::Lazy::new();
descriptor.get(|| file_descriptor().message_by_package_relative_name("CommandData").unwrap()).clone()
}
}

impl ::std::fmt::Display for CommandData {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
::protobuf::text_format::fmt(self, f)
}
}

impl ::protobuf::reflect::ProtobufValue for CommandData {
type RuntimeType = ::protobuf::reflect::rt::RuntimeTypeMessage<Self>;
}

static file_descriptor_proto_data: &'static [u8] = b"\
\n\x12command_data.proto\x12\x0fcommand_data.v1\"!\n\x0bCommandData\x12\
\x12\n\x04data\x18\x01\x20\x03(\x02R\x04datab\x06proto3\
";

/// `FileDescriptorProto` object which was a source for this generated file
fn file_descriptor_proto() -> &'static ::protobuf::descriptor::FileDescriptorProto {
static file_descriptor_proto_lazy: ::protobuf::rt::Lazy<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::Lazy::new();
file_descriptor_proto_lazy.get(|| {
::protobuf::Message::parse_from_bytes(file_descriptor_proto_data).unwrap()
})
}

/// `FileDescriptor` object which allows dynamic access to files
pub fn file_descriptor() -> &'static ::protobuf::reflect::FileDescriptor {
static generated_file_descriptor_lazy: ::protobuf::rt::Lazy<::protobuf::reflect::GeneratedFileDescriptor> = ::protobuf::rt::Lazy::new();
static file_descriptor: ::protobuf::rt::Lazy<::protobuf::reflect::FileDescriptor> = ::protobuf::rt::Lazy::new();
file_descriptor.get(|| {
let generated_file_descriptor = generated_file_descriptor_lazy.get(|| {
let mut deps = ::std::vec::Vec::with_capacity(0);
let mut messages = ::std::vec::Vec::with_capacity(1);
messages.push(CommandData::generated_message_descriptor_data());
let mut enums = ::std::vec::Vec::with_capacity(0);
::protobuf::reflect::GeneratedFileDescriptor::new_generated(
file_descriptor_proto(),
deps,
messages,
enums,
)
});
::protobuf::reflect::FileDescriptor::new_generated_2(generated_file_descriptor)
})
}
49 changes: 46 additions & 3 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
/**
* Wrapper Class for Data coming off the car
*/
pub struct Data {
pub struct DecodeData {
pub value: Vec<f32>,
pub topic: String,
pub unit: String,
Expand All @@ -12,7 +12,7 @@ pub struct Data {
/**
* Implementation for the format of the data for debugging purposes
*/
impl fmt::Display for Data {
impl fmt::Display for DecodeData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Overrides the string representation of the class.

Expand All @@ -27,7 +27,7 @@ impl fmt::Display for Data {
/**
* Implementation fo the Data Structs' methods
*/
impl Data {
impl DecodeData {
/**
* Constructor
* @param id: the id of the data
Expand All @@ -42,3 +42,46 @@ impl Data {
}
}
}

/**
* Wrapper Class for data going into the car
*/
pub struct EncodeData {
pub value: Vec<u8>,
pub id: u32,
pub is_ext: bool,
}

/**
* Implementation for the format of the data for debugging purposes
*/
impl fmt::Display for EncodeData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Overrides the string representation of the class.

write!(
f,
"{}#{:?} (extended: {})",
self.id, self.value, self.is_ext
)
}
}

/**
* Implementation fo the Data Structs' methods
*/
impl EncodeData {
/**
* Constructor
* @param id: the id of the can message
* @param value: the can message payload
* @param topic: whether the can message is extended format
*/
pub fn new(id: u32, value: Vec<u8>, is_ext: bool) -> Self {
Self {
id: id,
value: value,
is_ext: is_ext,
}
}
}
16 changes: 8 additions & 8 deletions src/message.rs → src/decodable_message.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use super::data::Data;
use super::data::DecodeData;

use super::master_mapping::get_message_info;
use super::decode_master_mapping::get_message_info;
/**
* Wrapper class for an individual message.
*/
pub struct Message {
pub struct DecodableMessage {
id: u32,
data: Vec<u8>,
}

/**
* Implementation of Message.
*/
impl Message {
impl DecodableMessage {
/**
* Creates a new message with the given timestamp, id, and data.
*/
Expand All @@ -23,8 +23,8 @@ impl Message {
/**
* Decodes the message and returns a vector of Data objects.
*/
pub fn decode(&self) -> Vec<Data> {
Message::decode_message(&self.id, &self.data)
pub fn decode(&self) -> Vec<DecodeData> {
DecodableMessage::decode_message(&self.id, &self.data)
}

/**
Expand All @@ -35,9 +35,9 @@ impl Message {
* param data: The data of the message.
* return: A vector of Data objects.
*/
fn decode_message(id: &u32, data: &[u8]) -> Vec<Data> {
fn decode_message(id: &u32, data: &[u8]) -> Vec<DecodeData> {
let decoder = get_message_info(id).decoder;
let mut decoded_data: Vec<Data> = Vec::new();
let mut decoded_data: Vec<DecodeData> = Vec::new();
let result = decoder(data);
for data in result {
decoded_data.push(data);
Expand Down
Loading

0 comments on commit 86857d4

Please sign in to comment.