/** * @license * Copyright 2022-2026 Matter.js Authors * SPDX-License-Identifier: Apache-2.0 */ import { MAX_UDP_MESSAGE_SIZE, Millis, Seconds } from "@matter/general"; /** @see {@link MatterSpecification.v161.Core} § 4.19 */ export namespace MatterBle { /** Short 16-bit form of the Matter BLE service UUID. */ export const SERVICE_UUID_SHORT = "fff6"; /** Full 128-bit (Bluetooth SIG base UUID) form of the Matter BLE service UUID. */ export const SERVICE_UUID = "0000fff6-0000-1000-8000-00805f9b34fb"; /** * Returns true if the given UUID matches the Matter BLE service UUID. * Accepts both the short 16-bit form ("fff6") and the full 128-bit form * ("0000fff6-0000-1000-8000-00805f9b34fb") as some BLE adapters and bindings * report the full Bluetooth SIG base UUID instead of the abbreviated form. */ export function isServiceUuid(uuid: string): boolean { const lower = uuid.toLowerCase(); return lower === SERVICE_UUID_SHORT || lower === SERVICE_UUID; } export const C1_CHARACTERISTIC_UUID = "18EE2EF5-263D-4559-959F-4F9C429F9D11"; export const C2_CHARACTERISTIC_UUID = "18EE2EF5-263D-4559-959F-4F9C429F9D12"; export const C3_CHARACTERISTIC_UUID = "64630238-8772-45F2-B87D-748A83218F04"; export const MINIMUM_ATT_MTU = 20; // 23-byte minimum ATT_MTU - 3 bytes for ATT operation header export const MAXIMUM_BTP_MTU = 244; // Maximum size of BTP segment export const MAXIMUM_ATT_MTU = MAXIMUM_BTP_MTU + 3; // 247: BTP segment plus the 3-byte GATT header (§4.19.3.1.4) /** §4.19.3.1.4: BTP segment size is the ATT_MTU minus the 3-byte GATT header, clamped to the supported range. */ export function btpSegmentSizeFromAttMtu(attMtu: number): number { const segmentSize = attMtu - 3; if (segmentSize < MINIMUM_ATT_MTU) return MINIMUM_ATT_MTU; if (segmentSize > MAXIMUM_BTP_MTU) return MAXIMUM_BTP_MTU; return segmentSize; } export const BTP_MAXIMUM_WINDOW_SIZE = 255; // Server maximum window size /** §4.19.4.7: reserve the remote window's last slot for an ack so a full window on both sides can't deadlock. */ export const BTP_WINDOW_NO_ACK_SEND_THRESHOLD = 1; /** §4.19.4.8: at this many free local-window slots or fewer, send any pending ack immediately. */ export const BTP_IMMEDIATE_ACK_WINDOW_THRESHOLD = 2; /** * The maximum amount of time after sending a BTP Session Handshake request to wait for a BTP Session Handshake * response before closing the connection. */ export const BTP_CONN_RSP_TIMEOUT = Seconds(15); // timer starts when receives handshake request & waits for a subscription request on c2 /** * The maximum amount of time after receipt of a segment before a stand-alone ACK must be sent. Also the duration of * the acknowledgement-received timer (§4.19.4.8). */ export const BTP_ACK_TIMEOUT = Seconds(15); // timer in ms before ack should be sent for a segment export const BTP_SEND_ACK_TIMEOUT = Millis(BTP_ACK_TIMEOUT / 3); // timer starts when we receive a packet and stops when we sends its ack /** * The maximum amount of time no unique data has been sent over a BTP session before the Central Device must close * the BTP session. */ export const BTP_CONN_IDLE_TIMEOUT = Seconds(60); /** BTP protocol versions supported, sorted in descending order. */ export const BTP_SUPPORTED_VERSIONS = [4]; /** * The maximum message size that can be transported in a Matter message via BTP. * Seems the chip code in BTPEngine limits the size currently to "one pbuf" which should mean one UDP message. */ export const MAX_MATTER_PAYLOAD_SIZE = MAX_UDP_MESSAGE_SIZE; }