src.nth.io/

summaryrefslogtreecommitdiff
path: root/src/fabrics.ts
blob: 2c4c90e09211f713274d9e0bb25758df0a04075e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { FabricIndex } from "@matter/main";
import { OperationalCredentials } from "@matter/main/clusters/operational-credentials";
import type { CommissioningController } from "@project-chip/matter.js";
import type { PairedNode } from "@project-chip/matter.js/device";
import { Log } from "./logging.js";

const log = new Log("fabrics");

/**
 * Fabric-table management on the device's Operational Credentials cluster.
 *
 * Every commissioning writes a fabric entry into the device's limited fabric
 * table. Entries never expire; identities whose local storage was deleted
 * leave orphans behind. These helpers make the table visible and allow an
 * admin (us) to remove stale entries without a factory reset.
 */

export interface FabricEntry {
  fabricIndex: number;
  fabricId: bigint;
  nodeId: bigint;
  vendorId: number;
  label: string;
  /** True when the entry belongs to this controller's current identity. */
  isOurs: boolean;
}

export interface FabricTable {
  supportedFabrics: number;
  commissionedFabrics: number;
  entries: FabricEntry[];
}

const KNOWN_VENDORS: Record<number, string> = {
  0x1349: "Apple",
  0x6006: "Google",
  0x10e1: "SmartThings",
  0x117c: "IKEA",
};

function vendorName(vendorId: number): string {
  const known = KNOWN_VENDORS[vendorId];
  if (known !== undefined) return known;
  if (vendorId >= 0xfff1 && vendorId <= 0xfff4) return "test vendor";
  return "unknown vendor";
}

export async function readFabricTable(
  controller: CommissioningController,
  node: PairedNode,
): Promise<FabricTable> {
  const client = node.getRootClusterClient(OperationalCredentials.Complete);
  if (client === undefined) {
    throw new Error("Operational Credentials cluster not found on the device's root endpoint");
  }

  // Read from the device with fabric filtering off, so entries from all
  // fabrics are returned, not just our own.
  const fabrics = await client.attributes.fabrics.get(true, false);
  const supportedFabrics = (await client.attributes.supportedFabrics.get(true)) ?? 0;
  const commissionedFabrics = (await client.attributes.commissionedFabrics.get(true)) ?? 0;
  if (fabrics === undefined) {
    throw new Error("Device returned no fabric list");
  }

  const ourFabric = controller.fabric;
  const entries = fabrics.map(fabric => ({
    fabricIndex: Number(fabric.fabricIndex),
    fabricId: BigInt(fabric.fabricId),
    nodeId: BigInt(fabric.nodeId),
    vendorId: Number(fabric.vendorId),
    label: fabric.label,
    isOurs: ourFabric.matchesFabricIdAndRootPublicKey(fabric.fabricId, fabric.rootPublicKey),
  }));

  return { supportedFabrics, commissionedFabrics, entries };
}

export function formatFabricTable(nodeId: bigint, table: FabricTable): string {
  const lines: string[] = [];
  lines.push(
    `Fabric table on node ${nodeId} (${table.commissionedFabrics} of ${table.supportedFabrics} slots used):`,
  );
  for (const entry of table.entries) {
    const vendor = `0x${entry.vendorId.toString(16).padStart(4, "0")} (${vendorName(entry.vendorId)})`;
    const marker = entry.isOurs ? "  [this controller]" : "";
    lines.push(
      `  index ${entry.fabricIndex}: label ${JSON.stringify(entry.label)}  vendor ${vendor}  ` +
        `fabricId ${entry.fabricId}  nodeId ${entry.nodeId}${marker}`,
    );
  }
  const strays = findLikelyStrays(table);
  if (strays.length > 0) {
    lines.push("");
    lines.push(
      `Likely stale entries (our label, but not our current identity): ` +
        `${strays.map(entry => `index ${entry.fabricIndex}`).join(", ")}`,
    );
    lines.push(`Remove one with: mattertimesync fabrics --remove <index>`);
  }
  return lines.join("\n");
}

/**
 * Entries carrying our fabric label but not our current identity: orphans
 * from a commissioning whose local storage was deleted or replaced.
 */
export function findLikelyStrays(table: FabricTable): FabricEntry[] {
  const ourLabel = table.entries.find(entry => entry.isOurs)?.label;
  return table.entries.filter(entry => !entry.isOurs && ourLabel !== undefined && entry.label === ourLabel);
}

/**
 * Removes another fabric's entry from the device. Refuses to remove our own
 * entry; `decommission` is the correct path for that, because it also cleans
 * up local controller state.
 */
export async function removeFabricByIndex(
  controller: CommissioningController,
  node: PairedNode,
  fabricIndex: number,
): Promise<void> {
  const table = await readFabricTable(controller, node);
  const entry = table.entries.find(candidate => candidate.fabricIndex === fabricIndex);
  if (entry === undefined) {
    throw new Error(
      `No fabric with index ${fabricIndex} on the device ` +
        `(present: ${table.entries.map(candidate => candidate.fabricIndex).join(", ")})`,
    );
  }
  if (entry.isOurs) {
    throw new Error(
      `Fabric index ${fabricIndex} is this controller's own entry. ` +
        `Use "decommission" instead, so local state is cleaned up too.`,
    );
  }

  const client = node.getRootClusterClient(OperationalCredentials.Complete);
  if (client === undefined) {
    throw new Error("Operational Credentials cluster not found on the device's root endpoint");
  }

  log.info(`Removing fabric index ${fabricIndex} (label ${JSON.stringify(entry.label)}) from the device`);
  const response = await client.commands.removeFabric({ fabricIndex: FabricIndex(fabricIndex) });
  if (response.statusCode !== OperationalCredentials.NodeOperationalCertStatus.Ok) {
    throw new Error(
      `Device rejected RemoveFabric for index ${fabricIndex}: ` +
        `status ${OperationalCredentials.NodeOperationalCertStatus[response.statusCode] ?? response.statusCode}` +
        `${response.debugText ? ` (${response.debugText})` : ""}`,
    );
  }
  log.info(`Fabric index ${fabricIndex} removed`);
}