feat: parse and validate FIT containers
This commit is contained in:
32
tests/fit/builders.py
Normal file
32
tests/fit/builders.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import struct
|
||||
|
||||
from app.fit.crc import fit_crc
|
||||
|
||||
|
||||
def make_fit(data_records: bytes, *, header_size: int = 14) -> bytes:
|
||||
if header_size not in {12, 14}:
|
||||
raise ValueError(header_size)
|
||||
header = bytearray(header_size)
|
||||
header[0] = header_size
|
||||
header[1] = 0x20
|
||||
struct.pack_into("<H", header, 2, 0x0100)
|
||||
struct.pack_into("<I", header, 4, len(data_records))
|
||||
header[8:12] = b".FIT"
|
||||
if header_size == 14:
|
||||
struct.pack_into("<H", header, 12, fit_crc(header[:12]))
|
||||
body = header + data_records
|
||||
return bytes(body + struct.pack("<H", fit_crc(body)))
|
||||
|
||||
|
||||
def definition(local: int, global_num: int, fields: list[tuple[int, int, int]], *, endian: str = "<") -> bytes:
|
||||
architecture = 1 if endian == ">" else 0
|
||||
payload = bytearray([0, architecture])
|
||||
payload.extend(struct.pack(f"{endian}H", global_num))
|
||||
payload.append(len(fields))
|
||||
for num, size, base_type in fields:
|
||||
payload.extend(bytes([num, size, base_type]))
|
||||
return bytes([0x40 | local]) + bytes(payload)
|
||||
|
||||
|
||||
def data(local: int, payload: bytes) -> bytes:
|
||||
return bytes([local]) + payload
|
||||
Reference in New Issue
Block a user