Skip to content

Commit

Permalink
can cooked
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Tsatsanis committed Apr 23, 2024
1 parent 2bb007f commit da9c35e
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 173 deletions.
34 changes: 20 additions & 14 deletions app/build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
mod commands;
mod datatypes;
mod events;
mod can;

extern crate regex;
extern crate serde;

use std::collections::HashSet;
use std::env;
use std::fs;
use std::path::Path;
use std::sync::Mutex;
use serde::Deserialize;

/*
Expand Down Expand Up @@ -58,6 +62,7 @@ pub const CONFIG_PATH: &str = "../config/config.toml";

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let mut id_list = Mutex::new(Vec::new());
let dest_path = Path::new(&out_dir).join("config.rs");

let ip_file = fs::read_to_string(CONFIG_PATH).unwrap();
Expand All @@ -68,9 +73,10 @@ fn main() {
content.push_str(&*configure_ip(&config));
content.push_str(&*configure_pod(&config));
content.push_str(&*configure_internal(&config));
content.push_str(&*commands::main());
content.push_str(&*datatypes::main());
content.push_str(&*events::main());
content.push_str(&*commands::main(&id_list));
content.push_str(&*datatypes::main(&id_list));
content.push_str(&*events::main(&id_list));
// content.push_str(&*can::main(&id_list));

fs::write(dest_path.clone(), content).expect(&*format!("Couldn't write to {}! Build failed.", dest_path.to_str().unwrap()));
println!("cargo:rerun-if-changed={}", CONFIG_PATH);
Expand All @@ -83,21 +89,21 @@ fn main() {


fn configure_ip(config: &Config) -> String {
format!("pub static GS_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});", config.gs.ip[0], config.gs.ip[1], config.gs.ip[2], config.gs.ip[3], config.gs.port)
+ &*format!("pub static GS_UPD_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});", config.gs.ip[0], config.gs.ip[1], config.gs.ip[2], config.gs.ip[3], config.gs.udp_port)
+ &*format!("pub const NETWORK_BUFFER_SIZE: usize = {};", config.gs.buffer_size)
+ &*format!("pub const IP_TIMEOUT: u64 = {};", config.gs.timeout)
format!("pub static GS_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});\n", config.gs.ip[0], config.gs.ip[1], config.gs.ip[2], config.gs.ip[3], config.gs.port)
+ &*format!("pub static GS_UPD_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});\n", config.gs.ip[0], config.gs.ip[1], config.gs.ip[2], config.gs.ip[3], config.gs.udp_port)
+ &*format!("pub const NETWORK_BUFFER_SIZE: usize = {};\n", config.gs.buffer_size)
+ &*format!("pub const IP_TIMEOUT: u64 = {};\n", config.gs.timeout)
}

fn configure_pod(config: &Config) -> String {
format!("pub static POD_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});", config.pod.net.ip[0], config.pod.net.ip[1], config.pod.net.ip[2], config.pod.net.ip[3], config.pod.net.port)
+ &*format!("pub static POD_UDP_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});", config.pod.net.ip[0], config.pod.net.ip[1], config.pod.net.ip[2], config.pod.net.ip[3], config.pod.net.udp_port)
+ &*format!("pub static POD_MAC_ADDRESS: [u8;6] = [{},{},{},{},{},{}];", config.pod.net.mac_addr[0], config.pod.net.mac_addr[1], config.pod.net.mac_addr[2], config.pod.net.mac_addr[3], config.pod.net.mac_addr[4], config.pod.net.mac_addr[5])
+ &*format!("pub const KEEP_ALIVE: u64 = {};", config.pod.net.keep_alive)
format!("pub static POD_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});\n", config.pod.net.ip[0], config.pod.net.ip[1], config.pod.net.ip[2], config.pod.net.ip[3], config.pod.net.port)
+ &*format!("pub static POD_UDP_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});\n", config.pod.net.ip[0], config.pod.net.ip[1], config.pod.net.ip[2], config.pod.net.ip[3], config.pod.net.udp_port)
+ &*format!("pub static POD_MAC_ADDRESS: [u8;6] = [{},{},{},{},{},{}];\n", config.pod.net.mac_addr[0], config.pod.net.mac_addr[1], config.pod.net.mac_addr[2], config.pod.net.mac_addr[3], config.pod.net.mac_addr[4], config.pod.net.mac_addr[5])
+ &*format!("pub const KEEP_ALIVE: u64 = {};\n", config.pod.net.keep_alive)
}

fn configure_internal(config: &Config) -> String {
format!("pub const EVENT_QUEUE_SIZE: usize = {};", config.pod.internal.event_queue_size)
+ &*format!("pub const DATA_QUEUE_SIZE: usize = {};", config.pod.internal.data_queue_size)
+ &*format!("pub const CAN_QUEUE_SIZE: usize = {};", config.pod.internal.can_queue_size)
format!("pub const EVENT_QUEUE_SIZE: usize = {};\n", config.pod.internal.event_queue_size)
+ &*format!("pub const DATA_QUEUE_SIZE: usize = {};\n", config.pod.internal.data_queue_size)
+ &*format!("pub const CAN_QUEUE_SIZE: usize = {};\n", config.pod.internal.can_queue_size)
}
45 changes: 35 additions & 10 deletions app/build/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,65 @@

extern crate regex;
extern crate serde;

use std::collections::HashSet;
use std::env;
use std::fs;
use std::path::Path;
use std::sync::Mutex;
use serde::Deserialize;
use toml::map::Map;

#[derive(Debug, Deserialize)]
struct Config {
pub struct Config {
Command: Vec<Command>,
}

#[derive(Debug, Deserialize)]
struct Command {
pub struct Command {
name: String,
id: u16,
}


pub const COMMANDS_PATH: &str = "../config/commands.toml";

pub fn main() -> String {
pub fn main(id_list: &Mutex<Vec<u16>>) -> String {
let config_str = fs::read_to_string(COMMANDS_PATH).unwrap();
let config: Config = toml::from_str(&config_str).unwrap();
// println!("{:?}", config);

let mut enum_definitions = String::new();
let mut match_to_id = String::from("match *self {\n");
let mut match_from_id = String::from("match id {\n");

let mut match_to_id = String::from("\t\tmatch *self {\n");
let mut match_from_id = String::from("\t\tmatch id {\n");
let mut id_list = id_list.lock().unwrap();
for command in config.Command {
enum_definitions.push_str(&format!("{}(u64),\n", command.name));
match_to_id.push_str(&format!("Command::{}(_) => {},\n", command.name, command.id));
match_from_id.push_str(&format!("{} => Command::{}(0u64),\n", command.id, command.name));
if command.id & 0b1111_1000_0000_0000 != 0 {
panic!("IDs need to be u11. Found {} > {}", command.id, 2^11);
} else {
if id_list.contains(&command.id) {
panic!("ID {} already taken!! {}:{} : pick a different one.", command.id, command.name, command.id);
}
id_list.push(command.id);
}
enum_definitions.push_str(&format!("\t{}(u64),\n", command.name));
match_to_id.push_str(&format!("\t\t\tCommand::{}(_) => {},\n", command.name, command.id));
match_from_id.push_str(&format!("\t\t\t{} => Command::{}(0u64),\n", command.id, command.name));
}

format!("\n\npub enum Command {{ {} }}\nimpl Command {{\n pub fn to_id(&self)->u16 {{ {} }} }}\n pub fn from_id(id:u16) -> Self {{ {} _ => Command::DefaultCommand(0), }} }}\n}}", enum_definitions, match_to_id, match_from_id)
format!("\n
pub enum Command {{
{}
}}
impl Command {{
pub fn to_id(&self)->u16 {{
{}
}}
}}
pub fn from_id(id:u16) -> Self {{
{}
_ => Command::DefaultCommand(0)
}}
}}
}}", enum_definitions, match_to_id, match_from_id)
}
54 changes: 41 additions & 13 deletions app/build/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,69 @@

extern crate regex;
extern crate serde;
use std::env;

use std::fs;
use std::path::Path;
use std::sync::Mutex;
use serde::Deserialize;

#[derive(Deserialize)]
struct Config {
pub struct Config {
Datatype: Vec<Datatype>,
}

#[derive(Deserialize)]
struct Datatype {
pub struct Datatype {
name: String,
id: u16,
}


pub const COMMANDS_PATH: &str = "../config/datatypes.toml";

pub fn main() -> String {
pub fn get_data_config() -> Config {
let config_str = fs::read_to_string(COMMANDS_PATH).unwrap();
let config: Config = toml::from_str(&config_str).unwrap();
toml::from_str(&config_str).unwrap()
}

let mut enum_definitions = String::new();
let mut match_to_id = String::from("match *self {\n");
let mut match_from_id = String::from("match id {\n");
pub fn main(id_list: &Mutex<Vec<u16>>) -> String {
let config: Config = get_data_config();

let mut enum_definitions = String::new();
let mut match_to_id = String::new();
let mut match_from_id = String::new();
let mut id_list = id_list.lock().unwrap();
let mut data_ids = vec![];
for dtype in config.Datatype {
if dtype.id & 0b1111_1000_0000_0000 != 0 {
panic!("IDs need to be u11. Found {} > {}", dtype.id, 2^11);
} else {
if id_list.contains(&dtype.id) {
panic!("ID {} already taken!! {}:{} : pick a different one.", dtype.id, dtype.name, dtype.id);
}
id_list.push(dtype.id);
data_ids.push(dtype.id);
}
enum_definitions.push_str(&format!("{},\n", dtype.name));
match_to_id.push_str(&format!("Datatype::{} => {},\n", dtype.name, dtype.id));
match_from_id.push_str(&format!("{} => Datatype::{},\n", dtype.id, dtype.name));
enum_definitions.push_str(&format!("\t{},\n", dtype.name));
match_to_id.push_str(&format!("\t\t\tDatatype::{} => {},\n", dtype.name, dtype.id));
match_from_id.push_str(&format!("\t\t\t{} => Datatype::{},\n", dtype.id, dtype.name));
}

format!("\n\npub enum Datatype {{ {} }}\nimpl Datatype {{\n pub fn to_id(&self)->u16 {{ {} }} }}\n pub fn from_id(id:u16) -> Self {{ {} _ => Datatype::DefaultDatatype, }} }}\n}}", enum_definitions, match_to_id, match_from_id)
format!("\n
pub enum Datatype {{
{}
}}\n
impl Datatype {{
pub fn to_id(&self)->u16 {{
match *self {{
{}
}}
}}
pub fn from_id(id:u16) -> Self {{
match id {{
{}
_ => Datatype::DefaultDatatype,
}}
}}
}}
pub static DATA_IDS : [u16;{}] = [{}];\n", enum_definitions, match_to_id, match_from_id, data_ids.len(), data_ids.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(", "))
}
Loading

0 comments on commit da9c35e

Please sign in to comment.