Skip to content

Commit b8baada

Browse files
Alex Vegaundivisible
authored andcommitted
perf(ios): route BLE notifications off the Dart hot path
Route Omi audio batches and Limitless flash data through native handlers while leaving storage and control notifications on Dart. Add deterministic router coverage and DEBUG-only aggregate BLE throughput diagnostics. Physical iPhone testing validated sustained backlog drain, Bluetooth off/on recovery, app relaunch recovery, and automatic reconnection after CV1 OTA. Failure-Class: none
1 parent dfc7fae commit b8baada

6 files changed

Lines changed: 254 additions & 22 deletions

File tree

‎app/ios/Runner.xcodeproj/project.pbxproj‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
42B17C622F69B9CF00BFDA9D /* OmiBleManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42B17C612F69B9CF00BFDA9D /* OmiBleManager.swift */; };
3737
42B17C642F69B9E100BFDA9D /* BleHostApiImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42B17C632F69B9E100BFDA9D /* BleHostApiImpl.swift */; };
3838
C0D3B1E00000000000000002 /* BleGattOperationScheduler.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0D3B1E00000000000000001 /* BleGattOperationScheduler.swift */; };
39+
C0D3B1E10000000000000002 /* BleNotificationRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0D3B1E10000000000000001 /* BleNotificationRouter.swift */; };
3940
42B17C7C2F69BC6600BFDA9D /* PigeonCommunicator.g.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42B17C7B2F69BC6600BFDA9D /* PigeonCommunicator.g.swift */; };
4041
42BABFB82F2A81B100E78A3C /* OmiRecordingAudioDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42BABFB62F2A81B100E78A3C /* OmiRecordingAudioDevice.swift */; };
4142
42BABFB92F2A81B100E78A3C /* OmiPhoneCallsPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42BABFB72F2A81B100E78A3C /* OmiPhoneCallsPlugin.swift */; };
@@ -76,6 +77,7 @@
7677
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
7778
99ADCEC5933A6FA3EE3AAE5D /* OmiBleManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42B17C612F69B9CF00BFDA9D /* OmiBleManager.swift */; };
7879
C0D3B1E00000000000000003 /* BleGattOperationScheduler.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0D3B1E00000000000000001 /* BleGattOperationScheduler.swift */; };
80+
C0D3B1E10000000000000003 /* BleNotificationRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0D3B1E10000000000000001 /* BleNotificationRouter.swift */; };
7981
9A25BD940EC94B21766CB950 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E5BBFB39079A4743502BBEE /* Pods_Runner.framework */; };
8082
9A26EFBA2ABCE6C441ADBE8E /* OmiCallCoordinatorProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42BABFC62F2A81B100E78A3C /* OmiCallCoordinatorProtocol.swift */; };
8183
9A519AEFA12F139B0082101F /* Base.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = CF2F518C2EC08EA60025B331 /* Base.xcconfig */; };
@@ -208,6 +210,7 @@
208210
42B17C612F69B9CF00BFDA9D /* OmiBleManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OmiBleManager.swift; sourceTree = "<group>"; };
209211
42B17C632F69B9E100BFDA9D /* BleHostApiImpl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BleHostApiImpl.swift; sourceTree = "<group>"; };
210212
C0D3B1E00000000000000001 /* BleGattOperationScheduler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BleGattOperationScheduler.swift; sourceTree = "<group>"; };
213+
C0D3B1E10000000000000001 /* BleNotificationRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BleNotificationRouter.swift; sourceTree = "<group>"; };
211214
42B17C7B2F69BC6600BFDA9D /* PigeonCommunicator.g.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PigeonCommunicator.g.swift; sourceTree = "<group>"; };
212215
42BABFB62F2A81B100E78A3C /* OmiRecordingAudioDevice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OmiRecordingAudioDevice.swift; sourceTree = "<group>"; };
213216
42BABFB72F2A81B100E78A3C /* OmiPhoneCallsPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OmiPhoneCallsPlugin.swift; sourceTree = "<group>"; };
@@ -493,6 +496,7 @@
493496
isa = PBXGroup;
494497
children = (
495498
C0D3B1E00000000000000001 /* BleGattOperationScheduler.swift */,
499+
C0D3B1E10000000000000001 /* BleNotificationRouter.swift */,
496500
42B17C612F69B9CF00BFDA9D /* OmiBleManager.swift */,
497501
42B17C632F69B9E100BFDA9D /* BleHostApiImpl.swift */,
498502
);
@@ -928,6 +932,7 @@
928932
files = (
929933
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */,
930934
C0D3B1E00000000000000002 /* BleGattOperationScheduler.swift in Sources */,
935+
C0D3B1E10000000000000002 /* BleNotificationRouter.swift in Sources */,
931936
42B17C622F69B9CF00BFDA9D /* OmiBleManager.swift in Sources */,
932937
BAADF00DF00DF00DF00DF0D2 /* BatchAudioWriter.swift in Sources */,
933938
BAADF00DF00DF00DF00DF0D4 /* BaseBatchAudioWriter.swift in Sources */,
@@ -973,6 +978,7 @@
973978
files = (
974979
873C99F3D85C142369688A1D /* AppDelegate.swift in Sources */,
975980
C0D3B1E00000000000000003 /* BleGattOperationScheduler.swift in Sources */,
981+
C0D3B1E10000000000000003 /* BleNotificationRouter.swift in Sources */,
976982
99ADCEC5933A6FA3EE3AAE5D /* OmiBleManager.swift in Sources */,
977983
0658C1BEFC86B56785C10A69 /* BatchAudioWriter.swift in Sources */,
978984
BB125CDB70C0DB5AAFB15E9C /* BaseBatchAudioWriter.swift in Sources */,
@@ -2986,6 +2992,7 @@
29862992
PRODUCT_BUNDLE_IDENTIFIER = "$(APP_BUNDLE_IDENTIFIER)";
29872993
SDKROOT = iphoneos;
29882994
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
2995+
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
29892996
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
29902997
SWIFT_VERSION = 5.0;
29912998
TARGETED_DEVICE_FAMILY = 1;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Foundation
2+
3+
enum BleNotificationRoute: Equatable {
4+
case limitlessFlash
5+
case batchAudio
6+
case dart
7+
}
8+
9+
/// Keeps unrelated BLE notifications off the native batch-capture hot paths.
10+
///
11+
/// Both native handlers consult UserDefaults (and may parse JSON) before they
12+
/// can reject a packet. Ring-sync traffic is much hotter than audio, so routing
13+
/// by the protocol's fixed service/characteristic identity first prevents that
14+
/// policy work from running for every storage notification.
15+
enum BleNotificationRouter {
16+
private static let omiServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214"
17+
private static let omiAudioCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214"
18+
private static let friendPendantServiceUuid = "1a3fd0e7-b1f3-ac9e-2e49-b647b2c4f8da"
19+
private static let friendPendantAudioCharacteristicUuid = "01000000-1111-1111-1111-111111111111"
20+
private static let limitlessServiceUuid = "632de001-604c-446b-a80f-7963e950f3fb"
21+
private static let limitlessReceiveCharacteristicUuid = "632de003-604c-446b-a80f-7963e950f3fb"
22+
23+
static func route(serviceUuid: String, characteristicUuid: String) -> BleNotificationRoute {
24+
let service = serviceUuid.lowercased()
25+
let characteristic = characteristicUuid.lowercased()
26+
27+
if service == limitlessServiceUuid, characteristic == limitlessReceiveCharacteristicUuid {
28+
return .limitlessFlash
29+
}
30+
if (service == omiServiceUuid && characteristic == omiAudioCharacteristicUuid)
31+
|| (service == friendPendantServiceUuid
32+
&& characteristic == friendPendantAudioCharacteristicUuid)
33+
{
34+
return .batchAudio
35+
}
36+
return .dart
37+
}
38+
}

‎app/ios/Runner/Ble/OmiBleManager.swift‎

Lines changed: 92 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ final class OmiBleManager: NSObject {
9292
/// Queued scan request if Bluetooth wasn't ready when startScan was called.
9393
private var pendingScan: (timeout: Int, serviceUuids: [String])?
9494

95+
#if DEBUG
96+
private struct NotificationWindow {
97+
var count = 0
98+
var bytes = 0
99+
var maxValueLength = 0
100+
}
101+
102+
private var notificationWindowStartedAt = Date()
103+
private var notificationWindows: [String: NotificationWindow] = [:]
104+
private var pigeonNotificationsInFlight = 0
105+
private var peakPigeonNotificationsInFlight = 0
106+
#endif
107+
95108
// MARK: - Initialization
96109

97110
private override init() {
@@ -930,6 +943,48 @@ final class OmiBleManager: NSObject {
930943
}
931944
operationSchedulers[peripheralUuid]?.endSession()
932945
}
946+
947+
#if DEBUG
948+
private func recordNotificationMetric(characteristicUuid: String, valueLength: Int) {
949+
var window = notificationWindows[characteristicUuid] ?? NotificationWindow()
950+
window.count += 1
951+
window.bytes += valueLength
952+
window.maxValueLength = max(window.maxValueLength, valueLength)
953+
notificationWindows[characteristicUuid] = window
954+
955+
let elapsed = Date().timeIntervalSince(notificationWindowStartedAt)
956+
guard elapsed >= 5 else { return }
957+
958+
for (characteristic, values) in notificationWindows.sorted(by: { $0.key < $1.key }) {
959+
NSLog(
960+
"[OmiBleMetrics] window=%.3fs characteristic=%@ notifications=%d bytes=%d max_value=%d "
961+
+ "notifications_per_sec=%.1f bytes_per_sec=%.1f pigeon_in_flight=%d pigeon_peak=%d",
962+
elapsed,
963+
characteristic,
964+
values.count,
965+
values.bytes,
966+
values.maxValueLength,
967+
Double(values.count) / elapsed,
968+
Double(values.bytes) / elapsed,
969+
pigeonNotificationsInFlight,
970+
peakPigeonNotificationsInFlight
971+
)
972+
}
973+
notificationWindows.removeAll(keepingCapacity: true)
974+
notificationWindowStartedAt = Date()
975+
peakPigeonNotificationsInFlight = pigeonNotificationsInFlight
976+
}
977+
978+
private func recordPigeonNotificationSent() {
979+
pigeonNotificationsInFlight += 1
980+
peakPigeonNotificationsInFlight = max(
981+
peakPigeonNotificationsInFlight, pigeonNotificationsInFlight)
982+
}
983+
984+
private func recordPigeonNotificationCompleted() {
985+
pigeonNotificationsInFlight = max(0, pigeonNotificationsInFlight - 1)
986+
}
987+
#endif
933988
}
934989

935990
// MARK: - CBCentralManagerDelegate
@@ -1227,40 +1282,55 @@ extension OmiBleManager: CBPeripheralDelegate {
12271282
// Handle notification
12281283
guard let data = characteristic.value, !data.isEmpty else { return }
12291284

1285+
#if DEBUG
1286+
recordNotificationMetric(characteristicUuid: charUuid, valueLength: data.count)
1287+
#endif
1288+
12301289
if characteristic.uuid == OmiBleManager.batteryLevelCharUuid, let firstByte = data.first {
12311290
persistBatteryReading(uuid: uuid, level: Int(firstByte))
12321291
}
12331292

1234-
// Limitless Transcribe Later: while batch mode targets this pendant's RX
1235-
// characteristic, the flash-drain engine consumes the packet natively.
1236-
if LimitlessFlashDrainEngine.shared.handle(
1237-
peripheralUuid: uuid,
1238-
serviceUuid: serviceUuid,
1239-
characteristicUuid: charUuid,
1240-
value: data
1241-
) {
1242-
return
1243-
}
1244-
1245-
// Batch (offline) mode: store audio natively and skip the Dart forward so the
1246-
// Flutter engine stays idle. Returns true only for the configured audio
1247-
// characteristic while batch mode is on; everything else falls through.
1248-
if OmiBatchAudioWriter.shared.handle(
1249-
peripheralUuid: uuid,
1250-
serviceUuid: serviceUuid,
1251-
characteristicUuid: charUuid,
1252-
value: data
1253-
) {
1254-
return
1293+
// Route by fixed protocol identity before consulting native capture policy.
1294+
// Ring sync can deliver hundreds of notifications per second; asking both
1295+
// batch handlers to read UserDefaults before rejecting every storage packet
1296+
// needlessly occupies CoreBluetooth's main-queue callback.
1297+
switch BleNotificationRouter.route(serviceUuid: serviceUuid, characteristicUuid: charUuid) {
1298+
case .limitlessFlash:
1299+
if LimitlessFlashDrainEngine.shared.handle(
1300+
peripheralUuid: uuid,
1301+
serviceUuid: serviceUuid,
1302+
characteristicUuid: charUuid,
1303+
value: data
1304+
) {
1305+
return
1306+
}
1307+
case .batchAudio:
1308+
if OmiBatchAudioWriter.shared.handle(
1309+
peripheralUuid: uuid,
1310+
serviceUuid: serviceUuid,
1311+
characteristicUuid: charUuid,
1312+
value: data
1313+
) {
1314+
return
1315+
}
1316+
case .dart:
1317+
break
12551318
}
12561319

12571320
let typedData = FlutterStandardTypedData(bytes: data)
1321+
#if DEBUG
1322+
recordPigeonNotificationSent()
1323+
#endif
12581324
flutterApi?.onCharacteristicValueUpdated(
12591325
peripheralUuid: uuid,
12601326
serviceUuid: serviceUuid,
12611327
characteristicUuid: charUuid,
12621328
value: typedData
1263-
) { _ in }
1329+
) { [weak self] _ in
1330+
#if DEBUG
1331+
self?.recordPigeonNotificationCompleted()
1332+
#endif
1333+
}
12641334
}
12651335

12661336
func peripheral(
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import Foundation
2+
3+
private func expect(
4+
_ actual: BleNotificationRoute,
5+
_ expected: BleNotificationRoute,
6+
_ message: String,
7+
file: StaticString = #filePath,
8+
line: UInt = #line
9+
) {
10+
guard actual == expected else {
11+
fatalError("\(file):\(line): \(message): expected \(expected), got \(actual)")
12+
}
13+
}
14+
15+
private func testRoutesOnlyKnownNativeCaptureCharacteristics() {
16+
expect(
17+
BleNotificationRouter.route(
18+
serviceUuid: "632DE001-604C-446B-A80F-7963E950F3FB",
19+
characteristicUuid: "632DE003-604C-446B-A80F-7963E950F3FB"
20+
),
21+
.limitlessFlash,
22+
"Limitless receive notifications must reach the native flash drain"
23+
)
24+
expect(
25+
BleNotificationRouter.route(
26+
serviceUuid: "19B10000-E8F2-537E-4F6C-D104768A1214",
27+
characteristicUuid: "19B10001-E8F2-537E-4F6C-D104768A1214"
28+
),
29+
.batchAudio,
30+
"Omi audio notifications must reach native batch capture"
31+
)
32+
expect(
33+
BleNotificationRouter.route(
34+
serviceUuid: "1A3FD0E7-B1F3-AC9E-2E49-B647B2C4F8DA",
35+
characteristicUuid: "01000000-1111-1111-1111-111111111111"
36+
),
37+
.batchAudio,
38+
"Friend pendant audio notifications must reach native batch capture"
39+
)
40+
}
41+
42+
private func testStorageAndControlNotificationsBypassNativeCapturePolicy() {
43+
for characteristic in [
44+
"30295781-4301-eabd-2904-2849adfeae43",
45+
"30295782-4301-eabd-2904-2849adfeae43",
46+
"00002a19-0000-1000-8000-00805f9b34fb",
47+
] {
48+
expect(
49+
BleNotificationRouter.route(
50+
serviceUuid: "30295780-4301-eabd-2904-2849adfeae43",
51+
characteristicUuid: characteristic
52+
),
53+
.dart,
54+
"non-audio notification must bypass native capture policy"
55+
)
56+
}
57+
}
58+
59+
private func testWrongCharacteristicWithinKnownServiceFallsThrough() {
60+
expect(
61+
BleNotificationRouter.route(
62+
serviceUuid: "632de001-604c-446b-a80f-7963e950f3fb",
63+
characteristicUuid: "632de002-604c-446b-a80f-7963e950f3fb"
64+
),
65+
.dart,
66+
"Limitless TX characteristic is not a native receive hot path"
67+
)
68+
expect(
69+
BleNotificationRouter.route(
70+
serviceUuid: "19b10000-e8f2-537e-4f6c-d104768a1214",
71+
characteristicUuid: "19b10002-e8f2-537e-4f6c-d104768a1214"
72+
),
73+
.dart,
74+
"Omi codec notifications must not consult batch capture policy"
75+
)
76+
}
77+
78+
@main
79+
private enum BleNotificationRouterTests {
80+
static func main() {
81+
testRoutesOnlyKnownNativeCaptureCharacteristics()
82+
testStorageAndControlNotificationsBypassNativeCapturePolicy()
83+
testWrongCharacteristicWithinKnownServiceFallsThrough()
84+
print("BleNotificationRouterTests: PASS")
85+
}
86+
}

‎app/ios/test/ble_gatt_project_graph_test.rb‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ class BleGattProjectGraphTest < Minitest::Test
66
PROJECT = File.expand_path("../Runner.xcodeproj/project.pbxproj", __dir__)
77
MANAGER = File.expand_path("../Runner/Ble/OmiBleManager.swift", __dir__)
88
SCHEDULER = File.expand_path("../Runner/Ble/BleGattOperationScheduler.swift", __dir__)
9+
ROUTER = File.expand_path("../Runner/Ble/BleNotificationRouter.swift", __dir__)
910
FILE_REF = "C0D3B1E00000000000000001"
1011
RUNNER_BUILD_REF = "C0D3B1E00000000000000002"
1112
RAYBAN_BUILD_REF = "C0D3B1E00000000000000003"
13+
ROUTER_FILE_REF = "C0D3B1E10000000000000001"
14+
ROUTER_RUNNER_BUILD_REF = "C0D3B1E10000000000000002"
15+
ROUTER_RAYBAN_BUILD_REF = "C0D3B1E10000000000000003"
1216

1317
def setup
1418
@project = File.read(PROJECT)
1519
@manager = File.read(MANAGER)
1620
@scheduler = File.read(SCHEDULER)
21+
@router = File.read(ROUTER)
1722
end
1823

1924
def test_scheduler_has_one_file_reference_and_two_target_build_references
@@ -33,6 +38,18 @@ def test_scheduler_is_compiled_by_both_phone_targets
3338
refute_includes rayban_sources, RUNNER_BUILD_REF
3439
end
3540

41+
def test_notification_router_is_compiled_by_both_phone_targets
42+
assert_equal 1, @project.scan(/#{ROUTER_FILE_REF} .* = \{isa = PBXFileReference;/).length
43+
assert_equal 2, @project.scan(/fileRef = #{ROUTER_FILE_REF}/).length
44+
45+
runner_sources = source_phase("97C146EA1CF9000F007C117D")
46+
rayban_sources = source_phase("A39438EBCDB909113855E505")
47+
assert_includes runner_sources, ROUTER_RUNNER_BUILD_REF
48+
refute_includes runner_sources, ROUTER_RAYBAN_BUILD_REF
49+
assert_includes rayban_sources, ROUTER_RAYBAN_BUILD_REF
50+
refute_includes rayban_sources, ROUTER_RUNNER_BUILD_REF
51+
end
52+
3653
# Static integration tripwires complement the behavioral scheduler tests.
3754
# They make sure the manager does not bypass the tested ownership boundary.
3855
def test_manager_does_not_restore_characteristic_keyed_completion_ownership
@@ -51,6 +68,12 @@ def test_manager_does_not_restore_rssi_keepalive_or_fixed_reconnect_spin
5168
assert_includes @scheduler, "BleReconnectBackoff.delay"
5269
end
5370

71+
def test_manager_routes_before_native_batch_policy
72+
assert_includes @manager, "BleNotificationRouter.route("
73+
assert_includes @router, "case limitlessFlash"
74+
assert_includes @router, "case batchAudio"
75+
end
76+
5477
private
5578

5679
def source_phase(identifier)

0 commit comments

Comments
 (0)