use crate::error::{Error, ErrorCode, Result, bail};
use crate::msg::VendorId;
const ALPHABET: &[u8; 38] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-.";
pub const QR_PREFIX: &str = "MT:";
pub const PACKED_LEN: usize = 11;
pub const QR_LEN_NO_TLV: usize = 3 + 19;
pub const MANUAL_CODE_MAX_DIGITS: usize = 21;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Passcode(u32);
impl Passcode {
pub const MIN: u32 = 0x0000_0001;
pub const MAX: u32 = 0x05F5_E0FE;
pub const INVALID: [u32; 12] = [
0, 11_111_111, 22_222_222, 33_333_333, 44_444_444, 55_555_555, 66_666_666, 77_777_777,
88_888_888, 99_999_999, 12_345_678, 87_654_321,
];
pub const fn new(value: u32) -> Result<Self> {
if value < Self::MIN || value > Self::MAX {
return Err(Error::new(ErrorCode::InvalidArgument));
}
let mut rest: &[u32] = &Self::INVALID;
while let [first, tail @ ..] = rest {
if *first == value {
return Err(Error::new(ErrorCode::InvalidArgument));
}
rest = tail;
}
Ok(Self(value))
}
#[must_use]
pub const fn value(self) -> u32 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u8)]
pub enum CustomFlow {
#[default]
Standard = 0,
UserIntent = 1,
Custom = 2,
}
impl CustomFlow {
const fn from_bits(bits: u8) -> Result<Self> {
Ok(match bits {
0 => Self::Standard,
1 => Self::UserIntent,
2 => Self::Custom,
_ => return Err(Error::new(ErrorCode::MessageReserved)),
})
}
}
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct DiscoveryCapabilities: u8 {
const BLE = 1 << 1;
const ON_IP_NETWORK = 1 << 2;
const WIFI_PAF = 1 << 3;
const NFC = 1 << 4;
const THREAD_COMMISSIONING = 1 << 5;
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OnboardingPayload {
pub vendor_id: VendorId,
pub product_id: u16,
pub custom_flow: CustomFlow,
pub discovery: DiscoveryCapabilities,
pub discriminator: u16,
pub passcode: Passcode,
}
const DISCRIMINATOR_MASK: u16 = 0x0FFF;
impl OnboardingPayload {
pub fn new(
vendor_id: VendorId,
product_id: u16,
discriminator: u16,
passcode: Passcode,
discovery: DiscoveryCapabilities,
custom_flow: CustomFlow,
) -> Result<Self> {
if discriminator > DISCRIMINATOR_MASK {
bail!(InvalidArgument)
}
Ok(Self {
vendor_id,
product_id,
custom_flow,
discovery,
discriminator,
passcode,
})
}
#[must_use]
pub const fn short_discriminator(&self) -> u8 {
(self.discriminator >> 8) as u8
}
pub fn pack(&self) -> [u8; PACKED_LEN] {
let mut bits = BitWriter::new();
bits.push(0, 3);
bits.push(u64::from(self.vendor_id.0), 16);
bits.push(u64::from(self.product_id), 16);
bits.push(self.custom_flow as u64, 2);
bits.push(u64::from(self.discovery.bits()), 8);
bits.push(u64::from(self.discriminator & DISCRIMINATOR_MASK), 12);
bits.push(u64::from(self.passcode.value()), 27);
bits.finish()
}
#[expect(
clippy::cast_possible_truncation,
reason = "every `take` here is width-exact: §5.1.3.1.3's fields are 27, 16, 12, 8 and 2 bits"
)]
pub fn unpack(bytes: &[u8]) -> Result<Self> {
let Some(packed) = bytes.get(..PACKED_LEN) else {
bail!(MessageTruncated)
};
let mut bits = BitReader::new(packed);
let version = bits.take(3)?;
if version != 0 {
bail!(UnsupportedVersion)
}
let vendor_id = VendorId(bits.take(16)? as u16);
let product_id = bits.take(16)? as u16;
let custom_flow = CustomFlow::from_bits(bits.take(2)? as u8)?;
let discovery = DiscoveryCapabilities::from_bits_retain(bits.take(8)? as u8);
let discriminator = bits.take(12)? as u16;
let passcode = Passcode::new(bits.take(27)? as u32)?;
Ok(Self {
vendor_id,
product_id,
custom_flow,
discovery,
discriminator,
passcode,
})
}
pub fn to_qr(&self) -> Result<heapless::String<QR_LEN_NO_TLV>> {
let mut out = heapless::String::new();
out.push_str(QR_PREFIX)
.map_err(|_| Error::new(ErrorCode::NoSpace))?;
base38_encode(&self.pack(), &mut out)?;
Ok(out)
}
pub fn from_qr(text: &str) -> Result<Self> {
let body = text.strip_prefix(QR_PREFIX).unwrap_or(text);
let mut packed = [0u8; PACKED_LEN];
let n = base38_decode(body, &mut packed)?;
if n < PACKED_LEN {
bail!(MessageTruncated)
}
Self::unpack(&packed)
}
pub fn to_manual_code(
&self,
include_vid_pid: bool,
) -> Result<heapless::String<MANUAL_CODE_MAX_DIGITS>> {
if !include_vid_pid && self.custom_flow != CustomFlow::Standard {
bail!(InvalidArgument)
}
let passcode = self.passcode.value();
let discriminator = u32::from(self.discriminator & DISCRIMINATOR_MASK);
let first = (u32::from(include_vid_pid) << 2) | (discriminator >> 10);
let group2 = ((discriminator & 0x300) << 6) | (passcode & 0x3FFF);
let group3 = passcode >> 14;
let mut out = heapless::String::<MANUAL_CODE_MAX_DIGITS>::new();
push_digits(&mut out, first, 1)?;
push_digits(&mut out, group2, 5)?;
push_digits(&mut out, group3, 4)?;
if include_vid_pid {
push_digits(&mut out, u32::from(self.vendor_id.0), 5)?;
push_digits(&mut out, u32::from(self.product_id), 5)?;
}
let check = verhoeff_check_digit(out.as_bytes())?;
push_digits(&mut out, u32::from(check), 1)?;
Ok(out)
}
#[expect(
clippy::cast_possible_truncation,
reason = "`bounded` holds each group to the width §5.1.4.1.4's tables give it, before any cast"
)]
pub fn from_manual_code(text: &str) -> Result<Self> {
let mut digits = heapless::Vec::<u8, MANUAL_CODE_MAX_DIGITS>::new();
for c in text.chars() {
match c {
'0'..='9' => digits
.push((c as u8).wrapping_sub(b'0'))
.map_err(|_| Error::new(ErrorCode::InvalidArgument))?,
'-' | ' ' => {}
_ => bail!(InvalidArgument),
}
}
let expect_vid_pid = match digits.len() {
11 => false,
21 => true,
_ => bail!(InvalidArgument),
};
if !verhoeff_is_valid(&digits) {
bail!(IntegrityCheckFailed)
}
let first = u32::from(digits.first().copied().unwrap_or(0));
if first > 7 {
bail!(UnsupportedVersion)
}
let vid_pid_present = (first & 0b100) != 0;
if vid_pid_present != expect_vid_pid {
bail!(InvalidArgument)
}
let group2 = bounded(digits_value(&digits, 1, 5)?, 0xFFFF)?;
let group3 = bounded(digits_value(&digits, 6, 4)?, 0x1FFF)?;
let discriminator = (((first & 0b011) << 10) | ((group2 & 0xC000) >> 6)) as u16;
let passcode = Passcode::new((group2 & 0x3FFF) | (group3 << 14))?;
let (vendor_id, product_id) = if vid_pid_present {
(
VendorId(bounded(digits_value(&digits, 10, 5)?, 0xFFFF)? as u16),
bounded(digits_value(&digits, 15, 5)?, 0xFFFF)? as u16,
)
} else {
(VendorId(0), 0)
};
Ok(Self {
vendor_id,
product_id,
custom_flow: if vid_pid_present {
CustomFlow::UserIntent
} else {
CustomFlow::Standard
},
discovery: DiscoveryCapabilities::empty(),
discriminator,
passcode,
})
}
}
fn push_digits<const N: usize>(
out: &mut heapless::String<N>,
value: u32,
width: usize,
) -> Result<()> {
let mut scratch = [0u8; 10];
let mut v = value;
for i in (0..width).rev() {
let Some(slot) = scratch.get_mut(i) else {
bail!(InvalidArgument)
};
*slot = b'0'.saturating_add((v % 10) as u8);
v /= 10;
}
if v != 0 {
bail!(InvalidArgument)
}
let Some(text) = scratch
.get(..width)
.and_then(|b| core::str::from_utf8(b).ok())
else {
bail!(InvalidArgument)
};
out.push_str(text)
.map_err(|_| Error::new(ErrorCode::NoSpace))
}
const fn bounded(value: u32, max: u32) -> Result<u32> {
if value > max {
return Err(Error::new(ErrorCode::InvalidArgument));
}
Ok(value)
}
fn digits_value(digits: &[u8], start: usize, width: usize) -> Result<u32> {
let end = start
.checked_add(width)
.ok_or(Error::new(ErrorCode::InvalidArgument))?;
let Some(slice) = digits.get(start..end) else {
bail!(InvalidArgument)
};
let mut value = 0u32;
for d in slice {
value = value
.checked_mul(10)
.and_then(|v| v.checked_add(u32::from(*d)))
.ok_or(Error::new(ErrorCode::InvalidArgument))?;
}
Ok(value)
}
fn base38_encode<const N: usize>(data: &[u8], out: &mut heapless::String<N>) -> Result<()> {
for chunk in data.chunks(3) {
let (value, width) = match chunk {
[a, b, c] => (
u32::from(*a) | (u32::from(*b) << 8) | (u32::from(*c) << 16),
5,
),
[a, b] => (u32::from(*a) | (u32::from(*b) << 8), 4),
[a] => (u32::from(*a), 2),
_ => bail!(InvalidArgument),
};
let mut v = value;
for _ in 0..width {
let Some(&c) = ALPHABET.get((v % 38) as usize) else {
bail!(InvalidArgument)
};
out.push(char::from(c))
.map_err(|_| Error::new(ErrorCode::NoSpace))?;
v /= 38;
}
}
Ok(())
}
#[expect(
clippy::cast_possible_truncation,
reason = "a base-38 group is checked against its own byte width before its octets are taken"
)]
fn base38_decode(text: &str, out: &mut [u8]) -> Result<usize> {
let bytes = text.as_bytes();
let mut written = 0usize;
for chunk in bytes.chunks(5) {
let width = match chunk.len() {
5 => 3usize,
4 => 2,
2 => 1,
_ => bail!(InvalidArgument),
};
let mut value = 0u32;
for c in chunk.iter().rev() {
let Some(digit) = ALPHABET.iter().position(|a| a == c) else {
bail!(InvalidArgument)
};
value = value
.checked_mul(38)
.and_then(|v| v.checked_add(digit as u32))
.ok_or(Error::new(ErrorCode::InvalidArgument))?;
}
let limit = 1u32
.checked_shl((width as u32).saturating_mul(8))
.unwrap_or(u32::MAX);
if width < 4 && value >= limit {
bail!(InvalidArgument)
}
for i in 0..width {
let Some(slot) = out.get_mut(written) else {
bail!(BufferTooSmall)
};
*slot = (value >> (i.saturating_mul(8))) as u8;
written = written.saturating_add(1);
}
}
Ok(written)
}
const VERHOEFF_D: [[u8; 10]; 10] = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
];
const VERHOEFF_P: [[u8; 10]; 8] = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
];
const VERHOEFF_INV: [u8; 10] = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9];
fn verhoeff_check_digit(ascii_digits: &[u8]) -> Result<u8> {
let mut c = 0u8;
for (i, ascii) in ascii_digits.iter().rev().enumerate() {
let digit = ascii.checked_sub(b'0').filter(|d| *d < 10);
let Some(digit) = digit else {
bail!(InvalidArgument)
};
let Some(row) = VERHOEFF_P.get(i.saturating_add(1) % 8) else {
bail!(InvalidArgument)
};
let Some(&permuted) = row.get(usize::from(digit)) else {
bail!(InvalidArgument)
};
let Some(&next) = VERHOEFF_D
.get(usize::from(c))
.and_then(|r| r.get(usize::from(permuted)))
else {
bail!(InvalidArgument)
};
c = next;
}
VERHOEFF_INV
.get(usize::from(c))
.copied()
.ok_or(Error::new(ErrorCode::InvalidArgument))
}
fn verhoeff_is_valid(digits: &[u8]) -> bool {
let mut c = 0u8;
for (i, digit) in digits.iter().rev().enumerate() {
if *digit > 9 {
return false;
}
let (Some(row), Some(table)) = (VERHOEFF_P.get(i % 8), VERHOEFF_D.get(usize::from(c)))
else {
return false;
};
let Some(&permuted) = row.get(usize::from(*digit)) else {
return false;
};
let Some(&next) = table.get(usize::from(permuted)) else {
return false;
};
c = next;
}
c == 0
}
struct BitWriter {
bytes: [u8; PACKED_LEN],
bit: usize,
}
impl BitWriter {
const fn new() -> Self {
Self {
bytes: [0; PACKED_LEN],
bit: 0,
}
}
fn push(&mut self, value: u64, width: usize) {
for i in 0..width {
if (value >> i) & 1 == 1 {
let index = self.bit.saturating_add(i);
if let Some(slot) = self.bytes.get_mut(index / 8) {
*slot |= 1u8 << (index % 8);
}
}
}
self.bit = self.bit.saturating_add(width);
}
const fn finish(self) -> [u8; PACKED_LEN] {
self.bytes
}
}
struct BitReader<'a> {
bytes: &'a [u8],
bit: usize,
}
impl<'a> BitReader<'a> {
const fn new(bytes: &'a [u8]) -> Self {
Self { bytes, bit: 0 }
}
fn take(&mut self, width: usize) -> Result<u64> {
let mut value = 0u64;
for i in 0..width {
let index = self.bit.saturating_add(i);
let Some(&byte) = self.bytes.get(index / 8) else {
bail!(MessageTruncated)
};
if (byte >> (index % 8)) & 1 == 1 {
value |= 1u64 << i;
}
}
self.bit = self.bit.saturating_add(width);
Ok(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test_payload() -> OnboardingPayload {
OnboardingPayload::new(
VendorId(0xFFF1),
0x8001,
3840,
Passcode::new(20_202_021).expect("passcode"),
DiscoveryCapabilities::ON_IP_NETWORK,
CustomFlow::Standard,
)
.expect("payload")
}
#[test]
fn the_passcode_rules_of_5_1_7_1_are_enforced() {
assert!(Passcode::new(20_202_021).is_ok());
assert!(Passcode::new(1).is_ok());
assert!(Passcode::new(99_999_998).is_ok());
assert!(Passcode::new(0).is_err());
assert!(Passcode::new(99_999_999).is_err());
assert!(Passcode::new(u32::MAX).is_err());
for invalid in Passcode::INVALID {
assert!(Passcode::new(invalid).is_err(), "{invalid} must be refused");
}
}
#[test]
fn the_packed_structure_is_88_bits() {
assert_eq!(test_payload().pack().len(), PACKED_LEN);
}
#[test]
fn the_packed_structure_round_trips() {
let payload = test_payload();
assert_eq!(
OnboardingPayload::unpack(&payload.pack()).expect("unpack"),
payload
);
}
#[test]
fn version_zero_is_the_only_one_readable() {
let mut packed = test_payload().pack();
packed[0] |= 0b001;
assert_eq!(
OnboardingPayload::unpack(&packed).unwrap_err().code(),
ErrorCode::UnsupportedVersion
);
}
#[test]
fn the_qr_payload_round_trips() {
let payload = test_payload();
let qr = payload.to_qr().expect("qr");
assert!(qr.starts_with("MT:"), "{qr}");
assert_eq!(qr.len(), 22, "{qr}");
assert_eq!(OnboardingPayload::from_qr(&qr).expect("parse"), payload);
}
#[test]
fn a_qr_payload_parses_with_or_without_its_prefix() {
let qr = test_payload().to_qr().expect("qr");
let bare = qr.strip_prefix("MT:").expect("prefix");
assert_eq!(
OnboardingPayload::from_qr(bare).expect("bare"),
OnboardingPayload::from_qr(&qr).expect("prefixed")
);
}
#[test]
fn base38_uses_the_alphabet_of_table_61() {
const QR_ALPHANUMERIC: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
for c in ALPHABET {
assert!(QR_ALPHANUMERIC.contains(c), "{}", char::from(*c));
}
assert_eq!(ALPHABET.len(), 38);
for removed in b"$%*+/ :" {
assert!(!ALPHABET.contains(removed), "{}", char::from(*removed));
}
}
#[test]
fn base38_group_sizes_follow_the_specification() {
for (octets, chars) in [(3usize, 5usize), (2, 4), (1, 2), (6, 10), (11, 19)] {
let mut out = heapless::String::<64>::new();
base38_encode(&vec_of(octets), &mut out).expect("encode");
assert_eq!(out.len(), chars, "{octets} octets");
}
}
fn vec_of(n: usize) -> heapless::Vec<u8, 16> {
let mut v = heapless::Vec::new();
for i in 0..n {
let _ = v.push(i as u8);
}
v
}
#[test]
fn base38_round_trips_every_length() {
for len in 1..=11usize {
let data = vec_of(len);
let mut text = heapless::String::<64>::new();
base38_encode(&data, &mut text).expect("encode");
let mut back = [0u8; 16];
let n = base38_decode(&text, &mut back).expect("decode");
assert_eq!(n, len);
assert_eq!(&back[..n], &data[..], "length {len}");
}
}
#[test]
fn base38_refuses_characters_outside_the_alphabet() {
let mut out = [0u8; 16];
assert!(base38_decode("ABC$E", &mut out).is_err());
assert!(base38_decode("abcde", &mut out).is_err(), "lower case");
assert!(base38_decode("A", &mut out).is_err());
assert!(base38_decode("ABC", &mut out).is_err());
}
#[test]
fn the_manual_code_round_trips() {
let payload = test_payload();
let code = payload.to_manual_code(false).expect("code");
assert_eq!(code.len(), 11, "{code}");
let parsed = OnboardingPayload::from_manual_code(&code).expect("parse");
assert_eq!(parsed.passcode, payload.passcode);
assert_eq!(
parsed.discriminator >> 8,
payload.discriminator >> 8,
"{code}"
);
assert_eq!(parsed.discriminator & 0xFF, 0, "the rest are unknown");
}
#[test]
fn the_long_manual_code_carries_vendor_and_product() {
let mut payload = test_payload();
payload.custom_flow = CustomFlow::UserIntent;
let code = payload.to_manual_code(true).expect("code");
assert_eq!(code.len(), 21, "{code}");
let parsed = OnboardingPayload::from_manual_code(&code).expect("parse");
assert_eq!(parsed.vendor_id, payload.vendor_id);
assert_eq!(parsed.product_id, payload.product_id);
assert_eq!(parsed.passcode, payload.passcode);
}
#[test]
fn a_non_standard_flow_may_not_use_the_short_code() {
let mut payload = test_payload();
payload.custom_flow = CustomFlow::UserIntent;
assert!(payload.to_manual_code(false).is_err());
assert!(payload.to_manual_code(true).is_ok());
}
#[test]
fn the_check_digit_catches_a_single_wrong_digit() {
let code = test_payload().to_manual_code(false).expect("code");
let bytes = code.as_bytes();
for position in 0..bytes.len() {
for replacement in b'0'..=b'9' {
if bytes[position] == replacement {
continue;
}
let mut broken = heapless::String::<32>::new();
for (i, b) in bytes.iter().enumerate() {
let c = if i == position { replacement } else { *b };
let _ = broken.push(char::from(c));
}
assert!(
OnboardingPayload::from_manual_code(&broken).is_err(),
"a wrong digit at {position} was accepted: {broken}"
);
}
}
}
#[test]
fn the_check_digit_catches_a_transposition() {
let code = test_payload().to_manual_code(false).expect("code");
let bytes = code.as_bytes();
for i in 0..bytes.len().saturating_sub(1) {
if bytes[i] == bytes[i + 1] {
continue;
}
let mut swapped = heapless::String::<32>::new();
for (j, b) in bytes.iter().enumerate() {
let c = if j == i {
bytes[i + 1]
} else if j == i + 1 {
bytes[i]
} else {
*b
};
let _ = swapped.push(char::from(c));
}
assert!(
OnboardingPayload::from_manual_code(&swapped).is_err(),
"a transposition at {i} was accepted: {swapped}"
);
}
}
#[test]
fn dashes_and_spaces_are_tolerated() {
let code = test_payload().to_manual_code(false).expect("code");
let mut spaced = heapless::String::<32>::new();
for (i, c) in code.chars().enumerate() {
if i == 4 || i == 7 {
let _ = spaced.push('-');
}
let _ = spaced.push(c);
}
assert_eq!(
OnboardingPayload::from_manual_code(&spaced).expect("spaced"),
OnboardingPayload::from_manual_code(&code).expect("plain")
);
}
#[test]
fn a_manual_code_of_the_wrong_length_is_refused() {
for text in [
"",
"1",
"1234567890",
"123456789012",
"1".repeat(22).as_str(),
] {
assert!(
OnboardingPayload::from_manual_code(text).is_err(),
"{text:?} has {} digits",
text.len()
);
}
}
#[test]
fn a_first_digit_of_eight_or_nine_is_a_future_version() {
for first in ['8', '9'] {
let mut code = heapless::String::<32>::new();
let _ = code.push(first);
for _ in 0..10 {
let _ = code.push('0');
}
let digits: heapless::Vec<u8, 21> = code.bytes().take(10).map(|b| b - b'0').collect();
let mut ascii = heapless::String::<32>::new();
for d in &digits {
let _ = ascii.push(char::from(b'0' + d));
}
let check = verhoeff_check_digit(ascii.as_bytes()).expect("check");
let _ = ascii.push(char::from(b'0' + check));
assert_eq!(
OnboardingPayload::from_manual_code(&ascii)
.unwrap_err()
.code(),
ErrorCode::UnsupportedVersion,
"{ascii}"
);
}
}
#[test]
fn the_discriminator_must_fit_twelve_bits() {
assert!(
OnboardingPayload::new(
VendorId(1),
1,
0x1000,
Passcode::new(1).expect("p"),
DiscoveryCapabilities::empty(),
CustomFlow::Standard,
)
.is_err()
);
assert!(
OnboardingPayload::new(
VendorId(1),
1,
0x0FFF,
Passcode::new(1).expect("p"),
DiscoveryCapabilities::empty(),
CustomFlow::Standard,
)
.is_ok()
);
}
#[test]
fn a_reserved_custom_flow_is_refused() {
assert_eq!(
CustomFlow::from_bits(3).unwrap_err().code(),
ErrorCode::MessageReserved
);
let mut packed = test_payload().pack();
packed[4] |= 0b0001_1000;
assert_eq!(
OnboardingPayload::unpack(&packed).unwrap_err().code(),
ErrorCode::MessageReserved,
"a payload with the reserved flow must not decode"
);
}
#[test]
fn every_field_survives_the_round_trip_independently() {
for discriminator in [0u16, 1, 0x0FF, 0x800, 0x0FFF] {
for passcode in [1u32, 20_202_021, 99_999_998] {
for flow in [
CustomFlow::Standard,
CustomFlow::UserIntent,
CustomFlow::Custom,
] {
let payload = OnboardingPayload::new(
VendorId(0xFFF1),
0x8001,
discriminator,
Passcode::new(passcode).expect("passcode"),
DiscoveryCapabilities::BLE | DiscoveryCapabilities::ON_IP_NETWORK,
flow,
)
.expect("payload");
let qr = payload.to_qr().expect("qr");
assert_eq!(
OnboardingPayload::from_qr(&qr).expect("parse"),
payload,
"{discriminator:#x} {passcode} {flow:?}"
);
}
}
}
}
}