Compare commits

...
Sign in to create a new pull request.

7 commits

Author SHA1 Message Date
66cf65f803 fix: trim auth and post-write delays from 600ms down to 100ms total
- Auth trigger settle: 100ms → 50ms
- Auth password settle: 500ms → 50ms
- Post-write reboot settle: 200ms → 50ms

Beacon handles 50ms inter-command just fine, no reason for the
beginning and end to be slower.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 04:18:18 +00:00
38600193b7 fix: auto-write config immediately after connect — no manual tap needed
Removes the two-phase flow where the user had to confirm beacon flashing
and tap "Write Config". Now goes straight from connect → write → register
in one shot. The dxsmartConnectedView UI is removed.
2026-03-23 04:16:04 +00:00
3c41ecb49d fix: add disconnect detection + drop inter-command delay to 50ms
Two changes:
1. DXSmartProvisioner now registers for BLE disconnect callbacks.
   Previously if the beacon dropped the link mid-write, the provisioner
   would sit waiting for ACK timeouts (5s × 2 retries = 10s of dead air).
   Now it fails immediately with a clear error.

2. Inter-command delay reduced from 150ms → 50ms since beacon handles
   fast writes fine.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 04:08:40 +00:00
a08d3db893 fix: explicitly disconnect after writeConfig so beacon can reboot
After SaveConfig is sent, the DXSmartProvisioner.writeConfig() returns
but never disconnects. The beacon MCU needs the BLE link dropped to
finalize its save-and-reboot cycle. Without disconnect, the beacon stays
connected and keeps flashing indefinitely.

Added 200ms delay + explicit provisioner.disconnect() after writeConfig
completes in the success path.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 04:04:45 +00:00
640bc32f92 fix: use .withoutResponse for SaveConfig to prevent silent write drop
The beacon reboots instantly on SaveConfig (0x60). Using .withResponse
meant CoreBluetooth expected a GATT ACK that never arrived, potentially
causing the write to be silently dropped — leaving the config unsaved
and the beacon LED still flashing after provisioning.

Switching to .withoutResponse fires the bytes directly into the BLE
radio buffer without requiring a round-trip ACK. The beacon firmware
processes the save command from its buffer before rebooting.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 03:57:56 +00:00
157ab6d008 fix: send HardwareId to register_beacon_hardware API
The API requires HardwareId as a mandatory field, but the iOS app was
sending MacAddress (wrong key) and always passing nil. This caused
"HardwareId is required" errors after provisioning.

Since CoreBluetooth doesn't expose raw MAC addresses, we use the
CBPeripheral.identifier UUID as the hardware ID — same concept as
Android's device.address.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 03:55:13 +00:00
ce81a1a3d8 Merge branch 'schwifty/faster-provisioning' into main 2026-03-23 03:50:41 +00:00
3 changed files with 79 additions and 108 deletions

View file

@ -41,6 +41,7 @@ final class DXSmartProvisioner: NSObject, BeaconProvisioner {
private(set) var isConnected = false private(set) var isConnected = false
private(set) var isFlashing = false // Beacon LED flashing after trigger private(set) var isFlashing = false // Beacon LED flashing after trigger
private var useNewSDK = true // Prefer new SDK, fallback to old private var useNewSDK = true // Prefer new SDK, fallback to old
private var disconnected = false // Set true when BLE link drops unexpectedly
var diagnosticLog: ProvisionLog? var diagnosticLog: ProvisionLog?
var bleManager: BLEManager? var bleManager: BLEManager?
@ -76,6 +77,14 @@ final class DXSmartProvisioner: NSObject, BeaconProvisioner {
await diagnosticLog?.log("auth", "Authenticating (trigger + password)…") await diagnosticLog?.log("auth", "Authenticating (trigger + password)…")
try await authenticate() try await authenticate()
await diagnosticLog?.log("auth", "Auth complete — SDK: \(useNewSDK ? "new (FFE2)" : "old (FFE1)")") await diagnosticLog?.log("auth", "Auth complete — SDK: \(useNewSDK ? "new (FFE2)" : "old (FFE1)")")
// Register for unexpected disconnects so we fail fast instead of
// waiting for per-command ACK timeouts (5s × 2 = 10s of dead air).
bleManager?.onPeripheralDisconnected = { [weak self] disconnectedPeripheral, error in
guard disconnectedPeripheral.identifier == self?.peripheral.identifier else { return }
self?.handleUnexpectedDisconnect(error: error)
}
isConnected = true isConnected = true
isFlashing = true isFlashing = true
return return
@ -125,6 +134,8 @@ final class DXSmartProvisioner: NSObject, BeaconProvisioner {
} }
func disconnect() { func disconnect() {
// Unregister disconnect handler so intentional disconnect doesn't trigger error path
bleManager?.onPeripheralDisconnected = nil
if peripheral.state == .connected || peripheral.state == .connecting { if peripheral.state == .connected || peripheral.state == .connecting {
centralManager.cancelPeripheralConnection(peripheral) centralManager.cancelPeripheralConnection(peripheral)
} }
@ -171,12 +182,21 @@ final class DXSmartProvisioner: NSObject, BeaconProvisioner {
] ]
for (index, (name, packet)) in commands.enumerated() { for (index, (name, packet)) in commands.enumerated() {
// Bail immediately if BLE link dropped between commands
if disconnected {
await diagnosticLog?.log("write", "Aborting — BLE disconnected", isError: true)
throw ProvisionError.writeFailed("BLE disconnected during write sequence")
}
await diagnosticLog?.log("write", "[\(index + 1)/\(commands.count)] \(name) (\(packet.count) bytes)") await diagnosticLog?.log("write", "[\(index + 1)/\(commands.count)] \(name) (\(packet.count) bytes)")
// SaveConfig (last command) causes beacon MCU to reboot it never sends an ACK. // SaveConfig (last command) causes beacon MCU to reboot it never sends an ACK.
// Fire the BLE write and return immediately; the disconnect is expected. // Use .withoutResponse so CoreBluetooth fires the bytes immediately into the
// BLE radio buffer without waiting for a GATT round-trip. With .withResponse,
// the beacon reboots before the ACK arrives, and CoreBluetooth may silently
// drop the write leaving the config unsaved and the beacon still flashing.
if name == "SaveConfig" { if name == "SaveConfig" {
peripheral.writeValue(packet, for: writeChar, type: .withResponse) peripheral.writeValue(packet, for: writeChar, type: .withoutResponse)
await diagnosticLog?.log("write", "✅ [\(index + 1)/\(commands.count)] SaveConfig sent — beacon will reboot") await diagnosticLog?.log("write", "✅ [\(index + 1)/\(commands.count)] SaveConfig sent — beacon will reboot")
await diagnosticLog?.log("write", "✅ All commands written successfully") await diagnosticLog?.log("write", "✅ All commands written successfully")
return return
@ -202,8 +222,8 @@ final class DXSmartProvisioner: NSObject, BeaconProvisioner {
throw lastError throw lastError
} }
// 150ms between commands aggressive speedup (was 300ms, originally 500ms) // 50ms between commands beacon handles fast writes fine (was 150ms, 300ms, 500ms)
try await Task.sleep(nanoseconds: 150_000_000) try await Task.sleep(nanoseconds: 50_000_000)
} }
} }
@ -279,6 +299,32 @@ final class DXSmartProvisioner: NSObject, BeaconProvisioner {
return packet return packet
} }
// MARK: - Disconnect Detection
/// Called when BLE link drops unexpectedly during provisioning.
/// Immediately resolves any pending continuations so we fail fast
/// instead of waiting for the 5s operationTimeout.
private func handleUnexpectedDisconnect(error: Error?) {
disconnected = true
isConnected = false
let disconnectError = ProvisionError.writeFailed("BLE disconnected unexpectedly: \(error?.localizedDescription ?? "unknown")")
Task { await diagnosticLog?.log("ble", "⚠️ Unexpected disconnect during provisioning", isError: true) }
// Cancel any pending write/response continuation immediately
if let cont = responseContinuation {
responseContinuation = nil
cont.resume(throwing: disconnectError)
}
if let cont = writeContinuation {
writeContinuation = nil
cont.resume(throwing: disconnectError)
}
if let cont = connectionContinuation {
connectionContinuation = nil
cont.resume(throwing: disconnectError)
}
}
// MARK: - Private Helpers // MARK: - Private Helpers
private func connectOnce() async throws { private func connectOnce() async throws {
@ -338,15 +384,13 @@ final class DXSmartProvisioner: NSObject, BeaconProvisioner {
// Step 1: Trigger fire and forget (matches Android's WRITE_TYPE_NO_RESPONSE) // Step 1: Trigger fire and forget (matches Android's WRITE_TYPE_NO_RESPONSE)
if let triggerData = Self.triggerPassword.data(using: .utf8) { if let triggerData = Self.triggerPassword.data(using: .utf8) {
peripheral.writeValue(triggerData, for: ffe3, type: .withoutResponse) peripheral.writeValue(triggerData, for: ffe3, type: .withoutResponse)
try await Task.sleep(nanoseconds: 100_000_000) // 100ms (matches Android SDK timer) try await Task.sleep(nanoseconds: 50_000_000) // 50ms settle
} }
// Step 2: Auth password fire and forget // Step 2: Auth password fire and forget
if let authData = Self.defaultPassword.data(using: .utf8) { if let authData = Self.defaultPassword.data(using: .utf8) {
peripheral.writeValue(authData, for: ffe3, type: .withoutResponse) peripheral.writeValue(authData, for: ffe3, type: .withoutResponse)
// 500ms settle after auth beacon needs time to enter config mode, try await Task.sleep(nanoseconds: 50_000_000) // 50ms settle
// especially if BLE stack was stressed by prior provisioner attempts
try await Task.sleep(nanoseconds: 500_000_000)
} }
} }

View file

@ -220,7 +220,7 @@ actor APIClient {
uuid: String, uuid: String,
major: Int, major: Int,
minor: Int, minor: Int,
macAddress: String?, hardwareId: String,
beaconType: String, beaconType: String,
token: String token: String
) async throws { ) async throws {
@ -230,9 +230,9 @@ actor APIClient {
"UUID": uuid, "UUID": uuid,
"Major": major, "Major": major,
"Minor": minor, "Minor": minor,
"HardwareId": hardwareId,
"BeaconType": beaconType "BeaconType": beaconType
] ]
if let mac = macAddress { body["MacAddress"] = mac }
let data = try await post(path: "/beacon-sharding/register_beacon_hardware.php", body: body, token: token, businessId: businessId) let data = try await post(path: "/beacon-sharding/register_beacon_hardware.php", body: body, token: token, businessId: businessId)
let resp = try JSONDecoder().decode(OKResponse.self, from: data) let resp = try JSONDecoder().decode(OKResponse.self, from: data)
guard resp.OK else { guard resp.OK else {
@ -241,12 +241,13 @@ actor APIClient {
} }
func verifyBeaconBroadcast( func verifyBeaconBroadcast(
hardwareId: String,
uuid: String, uuid: String,
major: Int, major: Int,
minor: Int, minor: Int,
token: String token: String
) async throws { ) async throws {
let body: [String: Any] = ["UUID": uuid, "Major": major, "Minor": minor] let body: [String: Any] = ["HardwareId": hardwareId, "UUID": uuid, "Major": major, "Minor": minor]
let data = try await post(path: "/beacon-sharding/verify_beacon_broadcast.php", body: body, token: token) let data = try await post(path: "/beacon-sharding/verify_beacon_broadcast.php", body: body, token: token)
let resp = try JSONDecoder().decode(OKResponse.self, from: data) let resp = try JSONDecoder().decode(OKResponse.self, from: data)
guard resp.OK else { guard resp.OK else {

View file

@ -209,8 +209,8 @@ struct ScanView: View {
progressView(title: "Connecting…", message: statusMessage) progressView(title: "Connecting…", message: statusMessage)
case .connected: case .connected:
// DXSmart: beacon is flashing, show write button // Legacy auto-write skips this state now
dxsmartConnectedView progressView(title: "Connected…", message: statusMessage)
case .writing: case .writing:
progressView(title: "Writing Config…", message: statusMessage) progressView(title: "Writing Config…", message: statusMessage)
@ -319,51 +319,7 @@ struct ScanView: View {
// MARK: - DXSmart Connected View // MARK: - DXSmart Connected View
private var dxsmartConnectedView: some View { // dxsmartConnectedView removed auto-write skips the manual confirmation step
VStack(spacing: 24) {
Spacer()
Image(systemName: "light.beacon.max")
.font(.system(size: 64))
.foregroundStyle(Color.payfritGreen)
.modifier(PulseEffectModifier())
Text("Connected — Beacon is Flashing")
.font(.title2.bold())
Text("Confirm the beacon LED is flashing, then tap Write Config to program it.\n\nThe beacon will timeout if you wait too long.")
.font(.subheadline)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
.padding(.horizontal, 32)
Button {
Task { await writeConfigToConnectedBeacon() }
} label: {
HStack {
Image(systemName: "arrow.down.doc")
Text("Write Config")
}
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.tint(Color.payfritGreen)
.controlSize(.large)
.padding(.horizontal, 32)
Button("Cancel") {
cancelProvisioning()
}
.foregroundStyle(.secondary)
// Show diagnostic log
if !provisionLog.entries.isEmpty {
diagnosticLogView
}
Spacer()
}
}
// MARK: - Progress / Success / Failed Views // MARK: - Progress / Success / Failed Views
@ -614,6 +570,8 @@ struct ScanView: View {
// Create appropriate provisioner // Create appropriate provisioner
let provisioner = makeProvisioner(for: beacon) let provisioner = makeProvisioner(for: beacon)
pendingProvisioner = provisioner
pendingConfig = config
// Wire up real-time status updates from provisioner // Wire up real-time status updates from provisioner
if let dxProvisioner = provisioner as? DXSmartProvisioner { if let dxProvisioner = provisioner as? DXSmartProvisioner {
@ -640,14 +598,8 @@ struct ScanView: View {
provisionLog?.log("disconnect", "Unexpected disconnect: \(reason)", isError: true) provisionLog?.log("disconnect", "Unexpected disconnect: \(reason)", isError: true)
// CP-28: disconnect during .connected is expected beacon keeps // For all active states, treat disconnect as failure
// flashing after BLE drops. We'll reconnect when user taps Write Config. if self.provisioningState == .connecting ||
if self.provisioningState == .connected {
provisionLog?.log("disconnect", "CP-28 idle disconnect — beacon still flashing, ignoring")
return
}
// For all other active states, treat disconnect as failure
if self.provisioningState == .connecting || self.provisioningState == .connected ||
self.provisioningState == .writing || self.provisioningState == .verifying { self.provisioningState == .writing || self.provisioningState == .verifying {
self.provisioningState = .failed self.provisioningState = .failed
self.errorMessage = "Beacon disconnected: \(reason)" self.errorMessage = "Beacon disconnected: \(reason)"
@ -659,46 +611,18 @@ struct ScanView: View {
try await provisioner.connect() try await provisioner.connect()
provisionLog.log("connect", "Connected and authenticated successfully") provisionLog.log("connect", "Connected and authenticated successfully")
// CP-28: stop at connected state, wait for user to confirm flashing // Auto-fire write immediately no pause needed
provisioningState = .connected provisioningState = .writing
pendingConfig = config writesCompleted = false
pendingProvisioner = provisioner statusMessage = "Writing config to DX-Smart…"
provisionLog.log("write", "Auto-writing config (no user tap needed)…")
} catch {
provisionLog.log("error", "Provisioning failed after \(provisionLog.elapsed): \(error.localizedDescription)", isError: true)
provisioningState = .failed
errorMessage = error.localizedDescription
}
}
// Store for DXSmart two-phase flow
@State private var pendingConfig: BeaconConfig?
@State private var pendingProvisioner: (any BeaconProvisioner)?
private func writeConfigToConnectedBeacon() async {
guard let config = pendingConfig,
let provisioner = pendingProvisioner,
let sp = selectedServicePoint,
let ns = namespace,
let token = appState.token else { return }
provisioningState = .writing
writesCompleted = false
statusMessage = "Writing config to DX-Smart…"
do {
// Reconnect if the beacon dropped BLE during the "confirm flashing" wait
if !provisioner.isConnected {
provisionLog.log("write", "Beacon disconnected while waiting — reconnecting…")
statusMessage = "Reconnecting to beacon…"
try await provisioner.connect()
provisionLog.log("write", "Reconnected — writing config…")
statusMessage = "Writing config to DX-Smart…"
}
try await provisioner.writeConfig(config) try await provisioner.writeConfig(config)
writesCompleted = true writesCompleted = true
// No explicit disconnect needed succeed() already disconnects
// Brief settle after SaveConfig before dropping the BLE link.
try? await Task.sleep(nanoseconds: 50_000_000)
provisioner.disconnect()
try await APIClient.shared.registerBeaconHardware( try await APIClient.shared.registerBeaconHardware(
businessId: business.id, businessId: business.id,
@ -706,7 +630,7 @@ struct ScanView: View {
uuid: ns.uuid, uuid: ns.uuid,
major: ns.major, major: ns.major,
minor: Int(config.minor), minor: Int(config.minor),
macAddress: nil, hardwareId: selectedBeacon?.id.uuidString ?? "unknown-ios",
beaconType: BeaconType.dxsmart.rawValue, beaconType: BeaconType.dxsmart.rawValue,
token: token token: token
) )
@ -715,14 +639,16 @@ struct ScanView: View {
statusMessage = "\(sp.name) — DX-Smart\nUUID: \(config.formattedUUID.prefix(13))\nMajor: \(config.major) Minor: \(config.minor)" statusMessage = "\(sp.name) — DX-Smart\nUUID: \(config.formattedUUID.prefix(13))\nMajor: \(config.major) Minor: \(config.minor)"
} catch { } catch {
provisionLog.log("error", "Provisioning failed after \(provisionLog.elapsed): \(error.localizedDescription)", isError: true)
provisioningState = .failed provisioningState = .failed
errorMessage = error.localizedDescription errorMessage = error.localizedDescription
} }
pendingConfig = nil
pendingProvisioner = nil
} }
// Kept for cancel/reset and registerAnywayAfterFailure fallback
@State private var pendingConfig: BeaconConfig?
@State private var pendingProvisioner: (any BeaconProvisioner)?
private func registerAnywayAfterFailure() async { private func registerAnywayAfterFailure() async {
guard let sp = selectedServicePoint, guard let sp = selectedServicePoint,
let ns = namespace, let ns = namespace,
@ -739,7 +665,7 @@ struct ScanView: View {
uuid: ns.uuid, uuid: ns.uuid,
major: ns.major, major: ns.major,
minor: Int(config.minor), minor: Int(config.minor),
macAddress: nil, hardwareId: selectedBeacon?.id.uuidString ?? "unknown-ios",
beaconType: selectedBeacon?.type.rawValue ?? "Unknown", beaconType: selectedBeacon?.type.rawValue ?? "Unknown",
token: token token: token
) )