Skip to content

Commit 5d6abb9

Browse files
committed
adding wmbus with key to the online ui
1 parent 39f92fd commit 5d6abb9

4 files changed

Lines changed: 127 additions & 5 deletions

File tree

docs/index.html

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<head>
55
<meta charset="utf-8">
6-
<title>Wired M-Bus Parser</title>
6+
<title>M-Bus Parser (Wired & Wireless)</title>
77
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.0/styles/default.min.css">
88
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.0/highlight.min.js"></script>
99
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.0/languages/json.min.js"></script>
@@ -87,6 +87,43 @@
8787
font-family: monospace;
8888
}
8989

90+
input[type="text"] {
91+
width: 100%;
92+
padding: 10px;
93+
border: 1px solid #ddd;
94+
border-radius: 4px;
95+
box-sizing: border-box;
96+
margin-bottom: 10px;
97+
font-family: monospace;
98+
}
99+
100+
.key-input-container {
101+
position: relative;
102+
margin-bottom: 15px;
103+
}
104+
105+
.key-help {
106+
font-size: 0.85em;
107+
color: #666;
108+
margin-top: 4px;
109+
margin-bottom: 8px;
110+
}
111+
112+
.key-toggle {
113+
position: absolute;
114+
right: 10px;
115+
top: 10px;
116+
background: none;
117+
border: none;
118+
cursor: pointer;
119+
padding: 4px;
120+
color: #666;
121+
}
122+
123+
.key-toggle:hover {
124+
color: #333;
125+
}
126+
90127
input[type="button"] {
91128
background-color: #4CAF50;
92129
color: white;
@@ -198,6 +235,24 @@
198235
border: 1px solid #444;
199236
}
200237

238+
body.dark-mode input[type="text"] {
239+
background: #23272a;
240+
color: #e0e0e0;
241+
border: 1px solid #444;
242+
}
243+
244+
body.dark-mode .key-help {
245+
color: #aaa;
246+
}
247+
248+
body.dark-mode .key-toggle {
249+
color: #aaa;
250+
}
251+
252+
body.dark-mode .key-toggle:hover {
253+
color: #e0e0e0;
254+
}
255+
201256
body.dark-mode pre {
202257
background: #23272a;
203258
color: #e0e0e0;
@@ -270,7 +325,7 @@
270325
<div class="header">
271326
<img id="banner" src="meter.png" alt="Meter Banner">
272327
<div class="header-text">
273-
<h1>Online M-Bus parser (wired) <span class="parser-version">v0.0.7</span></h1>
328+
<h1>Online M-Bus Parser <span class="parser-version">v0.0.8</span></h1>
274329
<div class="subtitle">
275330
<a href="https://maebli.github.io/m-bus-parser" target="_blank">maebli.github.io/m-bus-parser</a><a href="https://github.com/maebli" target="_blank">Michael Aebli</a>
276331
</div>
@@ -295,6 +350,19 @@ <h1>Online M-Bus parser (wired) <span class="parser-version">v0.0.7</span></h1>
295350
<label for="inputstring">Input String:</label>
296351
<textarea rows="5" cols="80"
297352
id="inputstring">68 3D 3D 68 08 01 72 00 51 20 02 82 4D 02 04 00 88 00 00 04 07 00 00 00 00 0C 15 03 00 00 00 0B 2E 00 00 00 0B 3B 00 00 00 0A 5A 88 12 0A 5E 16 05 0B 61 23 77 00 02 6C 8C 11 02 27 37 0D 0F 60 00 67 16</textarea>
353+
354+
<label for="aeskey">AES-128 Decryption Key (optional):</label>
355+
<div class="key-input-container">
356+
<input type="password" id="aeskey" placeholder="Enter 32 hex characters (16 bytes)" maxlength="32" pattern="[0-9A-Fa-f]{32}" />
357+
<button type="button" class="key-toggle" id="key-toggle" title="Show/Hide key">
358+
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
359+
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
360+
<circle cx="12" cy="12" r="3"></circle>
361+
</svg>
362+
</button>
363+
</div>
364+
<div class="key-help">Leave empty for unencrypted frames. Format: 32 hexadecimal characters (e.g., 0123456789ABCDEF0123456789ABCDEF)</div>
365+
298366
<input id="parse_json" type="button" value="Parse to JSON" />
299367
<input id="parse_yaml" type="button" value="Parse to YAML" />
300368
<input id="parse_table" type="button" value="Parse to Table" />
@@ -317,7 +385,7 @@ <h1>Online M-Bus parser (wired) <span class="parser-version">v0.0.7</span></h1>
317385
<pre id="output"></pre>
318386
</div>
319387
<script type="module">
320-
import init, { m_bus_parse } from "./m_bus_parser_wasm_pack.js";
388+
import init, { m_bus_parse, m_bus_parse_with_key } from "./m_bus_parser_wasm_pack.js";
321389

322390
let currentFormat = "json"; // Keep track of the last used format
323391

@@ -344,6 +412,20 @@ <h1>Online M-Bus parser (wired) <span class="parser-version">v0.0.7</span></h1>
344412
localStorage.setItem('darkMode', darkMode);
345413
});
346414

415+
// Key visibility toggle
416+
const keyInput = document.getElementById('aeskey');
417+
const keyToggle = document.getElementById('key-toggle');
418+
keyToggle.addEventListener('click', () => {
419+
const type = keyInput.getAttribute('type');
420+
if (type === 'password') {
421+
keyInput.setAttribute('type', 'text');
422+
keyToggle.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24"></path><line x1="1" y1="1" x2="23" y2="23"></line></svg>';
423+
} else {
424+
keyInput.setAttribute('type', 'password');
425+
keyToggle.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path><circle cx="12" cy="12" r="3"></circle></svg>';
426+
}
427+
});
428+
347429
await init(); // Ensure the WASM module is initialized
348430

349431
document.getElementById('parse_json').addEventListener('click', () => {
@@ -380,7 +462,21 @@ <h1>Online M-Bus parser (wired) <span class="parser-version">v0.0.7</span></h1>
380462
function parseInput(format) {
381463
currentFormat = format; // Update the current format
382464
const inputString = document.getElementById('inputstring').value;
383-
const formattedResult = m_bus_parse(inputString, format);
465+
const aesKey = document.getElementById('aeskey').value.trim();
466+
467+
// Use the appropriate parsing function based on whether a key is provided
468+
let formattedResult;
469+
if (aesKey && aesKey.length > 0) {
470+
// Validate key format (32 hex characters)
471+
if (!/^[0-9A-Fa-f]{32}$/.test(aesKey)) {
472+
alert('Invalid AES key format. Please enter exactly 32 hexadecimal characters (0-9, A-F).');
473+
return;
474+
}
475+
formattedResult = m_bus_parse_with_key(inputString, format, aesKey);
476+
} else {
477+
formattedResult = m_bus_parse(inputString, format);
478+
}
479+
384480
const outputContainer = document.getElementById('output');
385481
const outputCode = document.getElementById('output_code');
386482
let lang = '', extension = 'txt', mimeType = 'text/plain';

docs/m_bus_parser_wasm_pack.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,31 @@ export function m_bus_parse(data, format) {
9595
}
9696
}
9797

98+
/**
99+
* @param {string} data
100+
* @param {string} format
101+
* @param {string} key_hex
102+
* @returns {string}
103+
*/
104+
export function m_bus_parse_with_key(data, format, key_hex) {
105+
let deferred4_0;
106+
let deferred4_1;
107+
try {
108+
const ptr0 = passStringToWasm0(data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
109+
const len0 = WASM_VECTOR_LEN;
110+
const ptr1 = passStringToWasm0(format, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
111+
const len1 = WASM_VECTOR_LEN;
112+
const ptr2 = passStringToWasm0(key_hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
113+
const len2 = WASM_VECTOR_LEN;
114+
const ret = wasm.m_bus_parse_with_key(ptr0, len0, ptr1, len1, ptr2, len2);
115+
deferred4_0 = ret[0];
116+
deferred4_1 = ret[1];
117+
return getStringFromWasm0(ret[0], ret[1]);
118+
} finally {
119+
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
120+
}
121+
}
122+
98123
async function __wbg_load(module, imports) {
99124
if (typeof Response === 'function' && module instanceof Response) {
100125
if (typeof WebAssembly.instantiateStreaming === 'function') {
43 KB
Binary file not shown.

wasm/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ tag-name = "wasm-v{{version}}"
1717
crate-type = ["cdylib", "rlib"]
1818

1919
[features]
20-
default = ["console_error_panic_hook"]
20+
default = ["console_error_panic_hook", "decryption"]
21+
decryption = ["m-bus-parser/decryption"]
2122

2223
[dependencies]
2324
wasm-bindgen = "0.2.84"

0 commit comments

Comments
 (0)