Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / apple / FindFamily / FindFamilyiOS / UWB / UwbEnvelope.swift

UwbEnvelope.swift74 lines · 3.0 KiBRaw
1import Foundation
2
3/// Wire format for UWB session-setup messages sent through the existing
4/// E2E-encrypted `/api/uwb/*` channel.
5///
6/// Mirrors `UwbEnvelope.kt` on Modern-Apps Android. JSON-encoded inside an
7/// encrypted blob; only the small handshake (request / ack / config / cancel)
8/// flows over the server. All `NINearbyObject` updates flow peer-to-peer over
9/// the UWB radio after the session is established.
10struct UwbEnvelope: Codable {
11 /// Random UUID identifying this ranging session end-to-end.
12 var sessionId: String
13 /// The userid (signed Int64, encoded as UInt64 on the wire) of the sender.
14 var sender: UInt64
15 /// Sender's platform tag: `"android"` or `"ios"`.
16 var senderPlatform: String
17 /// Envelope kind: `"request"`, `"ack"`, `"config"`, `"cancel"`.
18 var kind: String
19 /// Optional payload. Contents depend on the cross-platform pairing — see [UwbHandshake].
20 var payload: UwbHandshake?
21}
22
23/// Cross-platform UWB handshake payload. Fields are populated based on the
24/// pairing — iOS↔iOS uses [discoveryTokenB64], Android↔Android uses the
25/// FiRa fields, Android↔iOS uses the accessory-protocol fields.
26struct UwbHandshake: Codable {
27 // --- Android↔Android FiRa fields ---
28 /// Local UWB MAC address of the sender (2 bytes), base64-encoded.
29 var addressB64: String?
30 /// Complex channel (channel + preamble index) chosen by the controller.
31 var channelNumber: Int?
32 var preambleIndex: Int?
33 /// Random session key (8 bytes), base64-encoded. Generated by the controller.
34 var sessionKeyB64: String?
35 /// FiRa session id (Int). Generated by the controller.
36 var sessionId: Int?
37
38 // --- iOS↔iOS NearbyInteraction fields ---
39 /// `NIDiscoveryToken` archived via `NSKeyedArchiver(requiringSecureCoding: true)`, base64.
40 var discoveryTokenB64: String?
41
42 // --- Cross-platform (Phase 5) Apple accessory protocol fields ---
43 /// `accessoryData` returned by Apple's accessory protocol. Sent Android → iOS at session start.
44 var accessoryConfigDataB64: String?
45 /// `shareableConfigurationData` returned by iOS after NI session run. Sent iOS → Android.
46 var shareableConfigDataB64: String?
47
48 init(
49 addressB64: String? = nil,
50 channelNumber: Int? = nil,
51 preambleIndex: Int? = nil,
52 sessionKeyB64: String? = nil,
53 sessionId: Int? = nil,
54 discoveryTokenB64: String? = nil,
55 accessoryConfigDataB64: String? = nil,
56 shareableConfigDataB64: String? = nil
57 ) {
58 self.addressB64 = addressB64
59 self.channelNumber = channelNumber
60 self.preambleIndex = preambleIndex
61 self.sessionKeyB64 = sessionKeyB64
62 self.sessionId = sessionId
63 self.discoveryTokenB64 = discoveryTokenB64
64 self.accessoryConfigDataB64 = accessoryConfigDataB64
65 self.shareableConfigDataB64 = shareableConfigDataB64
66 }
67}
68
69enum UwbEnvelopeKind {
70 static let request = "request"
71 static let ack = "ack"
72 static let config = "config"
73 static let cancel = "cancel"
74}