-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
194 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
const fourU8ToU32 = (f) => (f[0] << 24) | (f[1] << 16) | (f[2] << 8) | f[3]; | ||
|
||
const u8ArrToU32Arr = (u8a) => { | ||
let res = []; | ||
for (let i = 0; i < u8a.length/4; i++) { | ||
const c = u8a.slice(i*4, (i+1)*4); | ||
res.push(fourU8ToU32(c)); | ||
} | ||
return res; | ||
}; | ||
|
||
const u8ArrToStr = (u8a) => u8a.reduce((a,c,i) => { | ||
if (i === u8a.length -1) { | ||
return a; | ||
} | ||
const n = (c === 0) ? ";" : String.fromCharCode(c); | ||
return `${a}${n}`; | ||
}, ""); | ||
|
||
// some props are simple strings | ||
const getStringProp = (n, pname) => { | ||
const p = n.props.find(p => p[0] === pname); | ||
if (p) { | ||
return u8ArrToStr(p[1]); | ||
} | ||
}; | ||
|
||
// many props are just numbers | ||
const getProp = (n, pname) => { | ||
const p = n.props.find(p => p[0] === pname); | ||
return p ? u8ArrToU32Arr(p[1]) : null; | ||
}; | ||
|
||
// strings representation of lists of numbers for pretty-printing | ||
const getPropStr = (n, pname) => { | ||
const p = getProp(n, pname); | ||
return p ? p.join(", ") : null; | ||
}; | ||
|
||
// transform a node's props into numbers and strings, omitting many | ||
const transformNode = (n) => { | ||
const name = n.name || "root"; | ||
// phandle is an identifier to the node | ||
const phandle = getProp(n, "phandle"); | ||
// phy-handle is a ref to another node | ||
// TODO: make list of props that are refs | ||
const phyHandle = getProp(n, "phy-handle"); | ||
const phySupply = getProp(n, "phy-supply"); | ||
const resets = getProp(n, "resets"); | ||
const dmas = getProp(n, "dmas"); | ||
const clks = getProp(n, "clocks"); | ||
const cnames = getStringProp(n, "clock-names"); | ||
const compat = getStringProp(n, "compatible"); | ||
return { | ||
name, | ||
...(phandle ? { phandle: phandle[0] } : null), | ||
...(phySupply ? { phySupply: phySupply[0] } : null), | ||
...(phyHandle ? { phyHandle: phyHandle[0] } : null), | ||
...(resets ? { resets } : null), | ||
...(dmas ? { dmas } : null), | ||
...(clks ? { clks } : null), | ||
...(cnames ? { cnames } : null), | ||
...(compat ? { compat } : null), | ||
}; | ||
}; | ||
|
||
export const transform = (n, id = "10000") => { | ||
return { | ||
...transformNode(n), | ||
id, | ||
children: n.children.map((c, i) => transform(c, `${id}_${i}`)), | ||
} | ||
}; | ||
|
||
// flatten tree to list of nodes, use IDs to define ReactFlow edges | ||
export const getNodesEdges = (tree) => { | ||
const nodes = []; | ||
const edges = []; | ||
const rec = (n, d=1,b=1) => { | ||
nodes.push({ | ||
id: n.id, | ||
type: "custom", | ||
position: { | ||
x: d*180, | ||
y: b*(10-d)*12 + 50*n.children.length, | ||
}, | ||
data: { | ||
label: n.name, | ||
}, | ||
}); | ||
n.children.forEach((c,i) => { | ||
edges.push({ | ||
id: `${n.id}${c.id}`, | ||
source: n.id, | ||
target: c.id, | ||
}); | ||
rec(c, d+1, b+1+i); | ||
}); | ||
}; | ||
rec(tree); | ||
return { nodes, edges }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[toolchain] | ||
channel = "nightly-2023-10-13" | ||
components = ["rust-src", "llvm-tools", "rustfmt", "clippy"] | ||
targets = [ | ||
"wasm32-unknown-unknown", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
mod utils; | ||
|
||
use device_tree::DeviceTree; | ||
use gloo_utils::format::JsValueSerdeExt; | ||
use serde::{Deserialize, Serialize}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
// TODO: add actual parser :p | ||
mod utils; | ||
|
||
type Bin = Vec<u8>; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct Res { | ||
b: u32, | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn parse_dtb() -> Result<JsValue, JsValue> { | ||
pub fn parse_dtb(data: JsValue) -> js_sys::Promise { | ||
utils::set_panic_hook(); | ||
|
||
let x = Res { b: 123 }; | ||
Ok(JsValue::from_serde(&x).unwrap()) | ||
let bin: Bin = data.into_serde().unwrap(); | ||
|
||
let res = DeviceTree::load(&bin.as_slice()).unwrap(); | ||
let res_val = JsValue::from_serde(&res).unwrap(); | ||
js_sys::Promise::resolve(&res_val) | ||
} |