Updated matter decoder to show more info, and added a homekit decoder.

This commit is contained in:
Scott Garrett committed 2026-08-31 19:46:47 -04:00
1 parent 548f2e701f
commit 27ebf13f6f
8 files changed
+831 -6

No files matched your search

+157 -2
View File
@@ -14,6 +14,14 @@
# It is derived from project-chip/connectedhomeip's manufacturers.xml and is
# optional: without it, all fields still decode — the vendor name line is simply
# skipped. Regenerate it with: python3 gen-manufacturers.py (see that file).
#
# Two more optional companions, same "missing file = skip the extra" rule:
# products.tsv 3 cols: vendor id \t product id \t name (any vendor),
# regenerated by the gen-*-products.py scripts.
# vendor-equivalents.tsv 2 cols: alias vendor id \t canonical vendor id;
# hand-edited to record mergers/acquisitions (e.g.
# Feit 5155 == LIFX 4870) so a product resolves under
# either id and the vendor name gets annotated.
set -euo pipefail
@@ -94,6 +102,125 @@ get_vendor_name() {
awk -F'\t' -v id="$1" '$1==id { print $2; exit }' "$tsv"
}
# vendor-equivalents.tsv is 2 columns "alias vendor id \t canonical vendor id"
# (see that file's header). It records that two vendor IDs are the same company
# -- a merger or acquisition (e.g. Feit Electric 5155 == LIFX 4870).
#
# equivalent_vendor_ids VENDOR -> prints VENDOR's whole equivalence class: the
# vendor itself plus every alias that shares its canonical id, one per line,
# unique and sorted. It is the single source of truth for "these vendor IDs are
# the same company", used by both the product lookup and the vendor-name
# annotation. A missing vendor-equivalents.tsv yields just the vendor itself.
equivalent_vendor_ids() {
local eq="$SCRIPT_DIR/vendor-equivalents.tsv"
local v=$1 root
[[ -f $eq ]] || { printf '%s\n' "$v"; return 0; }
# canonical id of v: col-2 where v is an alias, else v itself.
root=$(awk -F'\t' -v id="$v" '$1 !~ /^#/ && $1==id { print $2; exit }' "$eq")
[[ -n $root ]] || root=$v
# every alias of that canonical, plus the canonical itself; unique+sorted.
{
awk -F'\t' -v r="$root" '$1 !~ /^#/ && $2==r { print $1 }' "$eq"
printf '%s\n' "$root"
} | sort -un
}
# products.tsv is 3 columns "vendor id \t product id \t name" (regenerated by the
# gen-*-products.py scripts; see the "do not edit" note there).
#
# get_product_name VENDOR PRODUCT -> prints the product name if that pair -- or,
# via equivalent_vendor_ids, any equivalent vendor's -- is in products.tsv. The
# exact vendor is checked before its equivalents. A missing products.tsv, or an
# id not present, yields no name.
get_product_name() {
local products="$SCRIPT_DIR/products.tsv"
[[ -f $products ]] || return 0
local id hit
# exact vendor first
hit=$(awk -F'\t' -v v="$1" -v p="$2" '$1==v && $2==p { print $3; exit }' "$products")
[[ -n $hit ]] && { printf '%s' "$hit"; return 0; }
# then any equivalent vendor id, skipping the exact vendor already checked
for id in $(equivalent_vendor_ids "$1"); do
[[ "$id" == "$1" ]] && continue
hit=$(awk -F'\t' -v v="$id" -v p="$2" '$1==v && $2==p { print $3; exit }' "$products")
[[ -n $hit ]] && { printf '%s' "$hit"; return 0; }
done
return 0
}
# The Matter "manual pairing code" is the PIN a commissioning app (Apple Home,
# Google Home, the Matter app) asks you to type -- it is NOT one of the raw
# payload fields, it is derived from the passcode and discriminator and, for
# non-standard commissioning flows, the vendor/product ids, then guarded by a
# Verhoeff check digit. A port of generateManualPairingCode() from the Matter
# QR tool (chill-uk/matter-qr-app, web/app.js).
# verhoeff_check_digit DIGITS -> prints the single Verhoeff check digit of the
# decimal string DIGITS. The three tables are the fixed standard Verhoeff
# tables; the digit string is processed right-to-left, as the reference does.
verhoeff_check_digit() {
local value=$1
local -a mul=(
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
)
local -a perm=(
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
)
local -a inv=( 0 4 3 2 1 5 6 7 8 9 )
local len=${#value}
local i p col checksum=0 digit
for (( i=0; i<len; i++ )); do
# The reference reverses the digit string; the i-th processed digit is
# therefore the (len-1-i)-th character of the original.
digit=${value:len-1-i:1}
p=$(( (i + 1) % 8 ))
col=${perm[p * 10 + digit]}
checksum=${mul[checksum * 10 + col]}
done
printf '%s' "${inv[checksum]}"
}
# compute_pairing_code VENDOR PRODUCT FLOW DISCRIMINATOR PASSCODE -> prints the
# Matter manual pairing code. For standard flow (0) it is 10 digits + a Verhoeff
# check digit; user-intent (1) and custom (2) flows append 5-digit vendor and
# product groups, so they are 20 digits + check.
compute_pairing_code() {
local vendor=$1 product=$2 flow=$3 discriminator=$4 passcode=$5
local short has chunk1 chunk2 chunk3 code
# 4-bit manual discriminator: the top 4 bits of the 12-bit QR discriminator.
short=$(( (discriminator >> 8) & 0x0f ))
# Only non-standard flows carry the vendor/product ids in the pairing code.
has=$(( flow != 0 ? 1 : 0 ))
chunk1=$(( ((short >> 2) & 0x03) | (has << 2) ))
chunk2=$(( (passcode & 0x3FFF) | ((short & 0x03) << 14) ))
chunk3=$(( (passcode >> 14) & 0x1FFF ))
code=$(printf '%1d%05d%04d' "$chunk1" "$chunk2" "$chunk3")
if (( has )); then
code+=$(printf '%05d%05d' "$vendor" "$product")
fi
code+=$(verhoeff_check_digit "$code")
printf '%s' "$code"
}
main() {
local uri payload
@@ -118,7 +245,7 @@ main() {
base38_decode "$payload" || return 1
local version vendor product customFlow discovery discriminator passcode
local cf caps="" vname
local cf caps="" vname pname="" others="" on id pairing=""
version=$(extract_bits 0 3)
vendor=$(extract_bits 3 16)
@@ -143,16 +270,44 @@ main() {
[[ -z $caps ]] && caps="None"
vname=$(get_vendor_name "$vendor")
# If the decoded vendor has an equivalent (e.g. Feit 5155 <-> LIFX 4870),
# annotate its name so the two read as one company.
for id in $(equivalent_vendor_ids "$vendor"); do
[[ "$id" == "$vendor" ]] && continue
on=$(get_vendor_name "$id")
[[ -n $on ]] && others="${others:+$others, }$on"
done
if [[ -n $others ]]; then
if [[ -n $vname ]]; then
vname="$vname (a.k.a. $others)"
else
vname="a.k.a. $others"
fi
fi
# Resolve the product ID to a human name, for any vendor (and equivalents).
pname=$(get_product_name "$vendor" "$product")
# The pairing code is the PIN a commissioning app asks you to type; it is
# derived from the passcode, discriminator, and (for non-standard flows) the
# vendor/product ids, with a Verhoeff check digit appended.
pairing=$(compute_pairing_code "$vendor" "$product" "$customFlow" "$discriminator" "$passcode")
if [ ${#pairing} -eq 11 ]
then
pairing="${pairing:0:4}-${pairing:4:3}-${pairing:7:4}"
fi
printf 'Version: %s\n' "$version"
printf 'Vendor ID: %s (0x%04X)' "$vendor" "$vendor"
[[ -n $vname ]] && printf ' %s' "$vname"
printf '\n'
printf 'Product ID: %s (0x%04X)\n' "$product" "$product"
printf 'Product ID: %s (0x%04X)' "$product" "$product"
[[ -n $pname ]] && printf ' %s' "$pname"
printf '\n'
printf 'Custom Flow: %s\n' "$cf"
printf 'Discovery: %s\n' "$caps"
printf 'Discriminator: %s\n' "$discriminator"
printf 'Passcode: %s\n' "$passcode"
printf 'Pairing Code: %s\n' "$pairing"
}
main "$@"