import Foundation /// Wire format for UWB session-setup messages sent through the existing /// E2E-encrypted `/api/uwb/*` channel. /// /// Mirrors `UwbEnvelope.kt` on Modern-Apps Android. JSON-encoded inside an /// encrypted blob; only the small handshake (request / ack / config / cancel) /// flows over the server. All `NINearbyObject` updates flow peer-to-peer over /// the UWB radio after the session is established. struct UwbEnvelope: Codable { /// Random UUID identifying this ranging session end-to-end. var sessionId: String /// The userid (signed Int64, encoded as UInt64 on the wire) of the sender. var sender: UInt64 /// Sender's platform tag: `"android"` or `"ios"`. var senderPlatform: String /// Envelope kind: `"request"`, `"ack"`, `"config"`, `"cancel"`. var kind: String /// Optional payload. Contents depend on the cross-platform pairing — see [UwbHandshake]. var payload: UwbHandshake? } /// Cross-platform UWB handshake payload. Fields are populated based on the /// pairing — iOS↔iOS uses [discoveryTokenB64], Android↔Android uses the /// FiRa fields, Android↔iOS uses the accessory-protocol fields. struct UwbHandshake: Codable { // --- Android↔Android FiRa fields --- /// Local UWB MAC address of the sender (2 bytes), base64-encoded. var addressB64: String? /// Complex channel (channel + preamble index) chosen by the controller. var channelNumber: Int? var preambleIndex: Int? /// Random session key (8 bytes), base64-encoded. Generated by the controller. var sessionKeyB64: String? /// FiRa session id (Int). Generated by the controller. var sessionId: Int? // --- iOS↔iOS NearbyInteraction fields --- /// `NIDiscoveryToken` archived via `NSKeyedArchiver(requiringSecureCoding: true)`, base64. var discoveryTokenB64: String? // --- Cross-platform (Phase 5) Apple accessory protocol fields --- /// `accessoryData` returned by Apple's accessory protocol. Sent Android → iOS at session start. var accessoryConfigDataB64: String? /// `shareableConfigurationData` returned by iOS after NI session run. Sent iOS → Android. var shareableConfigDataB64: String? init( addressB64: String? = nil, channelNumber: Int? = nil, preambleIndex: Int? = nil, sessionKeyB64: String? = nil, sessionId: Int? = nil, discoveryTokenB64: String? = nil, accessoryConfigDataB64: String? = nil, shareableConfigDataB64: String? = nil ) { self.addressB64 = addressB64 self.channelNumber = channelNumber self.preambleIndex = preambleIndex self.sessionKeyB64 = sessionKeyB64 self.sessionId = sessionId self.discoveryTokenB64 = discoveryTokenB64 self.accessoryConfigDataB64 = accessoryConfigDataB64 self.shareableConfigDataB64 = shareableConfigDataB64 } } enum UwbEnvelopeKind { static let request = "request" static let ack = "ack" static let config = "config" static let cancel = "cancel" }