humpback-dds/src/flash.rs

65 lines
1.8 KiB
Rust
Raw Normal View History

2020-10-06 15:03:53 +08:00
use heapless::{String, consts::*};
use smoltcp as net;
use embedded_nal as nal;
use core::str::FromStr;
2020-09-17 11:21:24 +08:00
2020-10-06 15:03:53 +08:00
unsafe fn read_record_length(addr: u32) -> u32 {
core::ptr::read(addr as *const u32)
2020-09-17 11:21:24 +08:00
}
2020-10-06 15:03:53 +08:00
unsafe fn read_byte(addr: u32) -> u8 {
core::ptr::read(addr as *const u8)
}
2020-09-17 11:21:24 +08:00
2020-10-06 15:03:53 +08:00
fn read_flash_str(addr: &mut u32) -> (String<U32>, String<U64>) {
let mut key: String<U32> = String::new();
let mut string: String<U64> = String::new();
let record_length = unsafe {
read_record_length(*addr)
2020-09-17 11:21:24 +08:00
};
2020-10-06 15:03:53 +08:00
if record_length == 0xFFFFFFFF {
return (key, string);
2020-09-25 14:24:33 +08:00
}
2020-09-17 11:21:24 +08:00
2020-10-06 15:03:53 +08:00
let mut key_string_div = false;
for f_addr in (*addr)+4..((*addr)+record_length) {
if key_string_div {
string.push(unsafe {read_byte(f_addr)} as char).unwrap();
} else {
let c = unsafe {read_byte(f_addr)};
if c == 0 {
key_string_div = true;
} else {
key.push(c as char).unwrap();
}
}
}
*addr += record_length;
(key, string)
}
2020-09-17 11:21:24 +08:00
2020-10-06 15:03:53 +08:00
pub fn read_flash(addr: &mut u32)
-> (net::wire::IpCidr, net::wire::EthernetAddress, nal::Ipv4Addr, String<U64>)
{
let mut cidr = net::wire::IpCidr::Ipv6(
net::wire::Ipv6Cidr::SOLICITED_NODE_PREFIX,
);
let mut eth = net::wire::EthernetAddress::BROADCAST;
let mut ip = nal::Ipv4Addr::unspecified();
let mut name = String::new();
loop {
let (key, string) = read_flash_str(addr);
if key.len() == 0 {
return (cidr, eth, ip, name);
}
match key.as_str() {
"CIDR" => cidr = net::wire::IpCidr::from_str(&string).unwrap(),
"MAC" => eth = net::wire::EthernetAddress::from_str(&string).unwrap(),
"BrokerIP" => ip = nal::Ipv4Addr::from_str(&string).unwrap(),
"Name" => name = string,
_ => {},
}
}
}