Skip to content

Commit ced64de

Browse files
committed
feat: add hex input to latch-dump
1 parent bc4d7a1 commit ced64de

4 files changed

Lines changed: 176 additions & 3 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ endif()
259259
if(LS_BUILD_TOOLS)
260260
add_executable(latch-dump tools/latch_dump.c)
261261
target_link_libraries(latch-dump PRIVATE latch)
262+
if(LS_BUILD_TESTS)
263+
find_package(Python3 COMPONENTS Interpreter REQUIRED)
264+
add_test(NAME latch-dump-hex-tests
265+
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_latch_dump.py
266+
$<TARGET_FILE:latch-dump> ${CMAKE_CURRENT_SOURCE_DIR}/tests/vectors/lep-v1-basic.hex)
267+
endif()
262268
endif()
263269
if(LS_BUILD_EXAMPLES)
264270
if(CMAKE_CROSSCOMPILING)

‎README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,31 @@ ctest --preset host-debug
152152
cargo check --manifest-path rust/latch/Cargo.toml
153153
```
154154

155+
On Windows with Visual Studio 2022, use the checked-in `host-msvc` preset:
156+
157+
```sh
158+
cmake --preset host-msvc
159+
cmake --build --preset host-msvc
160+
ctest --preset host-msvc
161+
```
162+
163+
CMake generates a 128-bit printable Build ID from the project version, Git revision and source hashes. Applications may override it with `LS_BUILD_ID` or `identity.firmware_build_id`; invalid IDs are rejected by `ls_init`.
164+
165+
### Inspecting LEP vectors
166+
167+
Enable the host tools to build `latch-dump`. Binary envelopes keep the original
168+
syntax; hexadecimal vectors can be decoded with `--hex` and may contain ASCII
169+
whitespace between byte pairs:
170+
171+
```sh
172+
cmake -S . -B build -DLS_BUILD_TOOLS=ON
173+
cmake --build build --target latch-dump
174+
build/latch-dump --hex tests/vectors/lep-v1-basic.hex
175+
```
176+
177+
Hex input is bounded by `LS_MAX_EVENT_SIZE` and rejects odd-length or
178+
non-hexadecimal input before attempting envelope validation.
179+
155180
For a separate compact source distribution, while keeping the reviewable checkout untouched:
156181

157182
```sh

‎tests/test_latch_dump.py‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
"""Exercise the host decoder's bounded hexadecimal input mode."""
3+
4+
from __future__ import annotations
5+
6+
import subprocess
7+
import sys
8+
import tempfile
9+
from pathlib import Path
10+
11+
12+
def run(decoder: str, *arguments: str) -> subprocess.CompletedProcess[str]:
13+
return subprocess.run(
14+
[decoder, *arguments],
15+
check=False,
16+
capture_output=True,
17+
text=True,
18+
)
19+
20+
21+
def main() -> int:
22+
decoder, vector = sys.argv[1:]
23+
with tempfile.TemporaryDirectory() as directory:
24+
root = Path(directory)
25+
26+
valid = root / "valid.hex"
27+
vector_bytes = "".join(Path(vector).read_text().split())
28+
valid.write_text(" ".join(vector_bytes[index : index + 2] for index in range(0, len(vector_bytes), 2)))
29+
result = run(decoder, "--hex", str(valid))
30+
assert result.returncode == 0, result.stderr
31+
assert "LEP v1" in result.stdout
32+
33+
binary = root / "valid.lst"
34+
binary.write_bytes(bytes.fromhex(vector_bytes))
35+
result = run(decoder, str(binary))
36+
assert result.returncode == 0, result.stderr
37+
38+
odd = root / "odd.hex"
39+
odd.write_text("abc")
40+
result = run(decoder, "--hex", str(odd))
41+
assert result.returncode != 0
42+
assert "odd number of digits" in result.stderr
43+
44+
invalid = root / "invalid.hex"
45+
invalid.write_text("00xz")
46+
result = run(decoder, "--hex", str(invalid))
47+
assert result.returncode != 0
48+
assert "non-hex character" in result.stderr
49+
50+
oversized = root / "oversized.hex"
51+
oversized.write_text("00" * 4097)
52+
result = run(decoder, "--hex", str(oversized))
53+
assert result.returncode != 0
54+
assert "exceeds LS_MAX_EVENT_SIZE" in result.stderr
55+
56+
return 0
57+
58+
59+
if __name__ == "__main__":
60+
raise SystemExit(main())

‎tools/latch_dump.c‎

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,104 @@
1+
#include <ctype.h>
12
#include <stdio.h>
23
#include <stdlib.h>
4+
#include <string.h>
35
#include "laststate/latch.h"
6+
7+
static int hex_digit(int character) {
8+
if (character >= '0' && character <= '9') {
9+
return character - '0';
10+
}
11+
if (character >= 'a' && character <= 'f') {
12+
return character - 'a' + 10;
13+
}
14+
if (character >= 'A' && character <= 'F') {
15+
return character - 'A' + 10;
16+
}
17+
return -1;
18+
}
19+
20+
static int read_hex(FILE *file, uint8_t *data, size_t capacity, size_t *length) {
21+
int high = -1;
22+
int character;
23+
while ((character = fgetc(file)) != EOF) {
24+
if (isspace((unsigned char)character)) {
25+
continue;
26+
}
27+
int digit = hex_digit(character);
28+
if (digit < 0) {
29+
fprintf(stderr, "invalid hex input: non-hex character 0x%02x\n",
30+
(unsigned)(unsigned char)character);
31+
return 1;
32+
}
33+
if (high < 0) {
34+
high = digit;
35+
continue;
36+
}
37+
if (*length >= capacity) {
38+
fprintf(stderr, "hex input exceeds LS_MAX_EVENT_SIZE (%u bytes)\n",
39+
(unsigned)LS_MAX_EVENT_SIZE);
40+
return 1;
41+
}
42+
data[*length] = (uint8_t)((high << 4) | digit);
43+
++*length;
44+
high = -1;
45+
}
46+
if (ferror(file)) {
47+
perror("read");
48+
return 1;
49+
}
50+
if (high >= 0) {
51+
fprintf(stderr, "invalid hex input: odd number of digits\n");
52+
return 1;
53+
}
54+
return 0;
55+
}
56+
457
static ls_result_t print_tlv(void *context, uint16_t type, const uint8_t *value, uint16_t length) {
558
(void)context;
659
(void)value;
760
printf(" tlv type=%u length=%u\n", (unsigned)type, (unsigned)length);
861
return LS_OK;
962
}
1063
int main(int argc, char **argv) {
11-
if (argc != 2) {
12-
fprintf(stderr, "usage: latch-dump EVENT.lst\n");
64+
int hex_input = argc == 3 && strcmp(argv[1], "--hex") == 0;
65+
if ((!hex_input && argc != 2) || (hex_input && argc != 3)) {
66+
fprintf(stderr, "usage: latch-dump [--hex] EVENT.lst\n");
1367
return 2;
1468
}
15-
FILE *file = fopen(argv[1], "rb");
69+
const char *path = hex_input ? argv[2] : argv[1];
70+
FILE *file = fopen(path, "rb");
1671
if (!file) {
1772
perror("open");
1873
return 2;
1974
}
75+
if (hex_input) {
76+
uint8_t *data = (uint8_t *)malloc(LS_MAX_EVENT_SIZE);
77+
if (!data) {
78+
fclose(file);
79+
return 2;
80+
}
81+
size_t length = 0;
82+
int result = read_hex(file, data, LS_MAX_EVENT_SIZE, &length);
83+
fclose(file);
84+
if (result) {
85+
free(data);
86+
return 1;
87+
}
88+
ls_envelope_info_t info;
89+
ls_result_t validation = ls_envelope_validate(data, length, &info);
90+
if (validation != LS_OK) {
91+
fprintf(stderr, "invalid LEP envelope: %d\n", validation);
92+
free(data);
93+
return 1;
94+
}
95+
printf("LEP v%u type=%u arch=%u flags=0x%02x sequence=%u event=%08x payload=%u\n", info.version,
96+
info.type, info.architecture, info.flags, (unsigned)info.sequence,
97+
(unsigned)info.event_id, (unsigned)info.payload_length);
98+
validation = ls_envelope_visit(data, length, print_tlv, 0);
99+
free(data);
100+
return validation == LS_OK ? 0 : 1;
101+
}
20102
if (fseek(file, 0, SEEK_END) || ftell(file) < 0) {
21103
fclose(file);
22104
return 2;

0 commit comments

Comments
 (0)