diff --git a/App_Resources/iOS/Info.plist b/App_Resources/iOS/Info.plist
index eb192802..b63a4b01 100644
--- a/App_Resources/iOS/Info.plist
+++ b/App_Resources/iOS/Info.plist
@@ -22,6 +22,8 @@
Faceclaw uses Bluetooth to connect to your glasses and ring, display your apps, and receive gestures.
NSSpeechRecognitionUsageDescription
Faceclaw turns audio from your glasses microphone into text for the app you choose. Dictation uses on-device recognition.
+ NSLocationWhenInUseUsageDescription
+ Faceclaw uses your location to correct the glasses compass from magnetic north to true north.
UIBackgroundModes
bluetooth-central
diff --git a/App_Resources/iOS/build.xcconfig b/App_Resources/iOS/build.xcconfig
index 15e50f3e..fdc60547 100644
--- a/App_Resources/iOS/build.xcconfig
+++ b/App_Resources/iOS/build.xcconfig
@@ -3,7 +3,7 @@ TARGETED_DEVICE_FAMILY = 1;
CODE_SIGN_STYLE = Automatic;
// Select the local signing team in Xcode or pass --teamId to the iOS scripts.
-OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework UniformTypeIdentifiers
+OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework UniformTypeIdentifiers
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
diff --git a/App_Resources/iOS/src/FaceclawLocation.h b/App_Resources/iOS/src/FaceclawLocation.h
new file mode 100644
index 00000000..53aaa101
--- /dev/null
+++ b/App_Resources/iOS/src/FaceclawLocation.h
@@ -0,0 +1,9 @@
+#import
+
+/** Short, foreground Core Location lookups for the glasses' true-north correction. */
+@interface FaceclawLocation : NSObject
++ (instancetype)shared;
+- (BOOL)hasPermission;
+- (void)requestPermission:(void (^)(BOOL granted))completion;
+- (void)requestDeclination:(void (^)(double degrees, double latitude, double longitude, NSString *error))completion;
+@end
diff --git a/App_Resources/iOS/src/FaceclawLocation.m b/App_Resources/iOS/src/FaceclawLocation.m
new file mode 100644
index 00000000..1f8872ef
--- /dev/null
+++ b/App_Resources/iOS/src/FaceclawLocation.m
@@ -0,0 +1,101 @@
+#import "FaceclawLocation.h"
+#import
+#import
+
+@interface FaceclawLocation ()
+@property(nonatomic, strong) CLLocationManager *manager;
+@property(nonatomic, strong) NSMutableArray *permissionCallbacks;
+@property(nonatomic, strong) NSMutableArray *declinationCallbacks;
+@property(nonatomic, strong) NSTimer *timer;
+@property(nonatomic, strong) CLLocation *location;
+@property(nonatomic, strong) CLHeading *heading;
+@end
+
+@implementation FaceclawLocation
++ (instancetype)shared {
+ static FaceclawLocation *instance;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{ instance = [FaceclawLocation new]; });
+ return instance;
+}
+- (instancetype)init {
+ if ((self = [super init])) {
+ _permissionCallbacks = [NSMutableArray new];
+ _declinationCallbacks = [NSMutableArray new];
+ _manager = [CLLocationManager new];
+ _manager.delegate = self;
+ _manager.desiredAccuracy = kCLLocationAccuracyKilometer;
+ }
+ return self;
+}
+- (BOOL)hasPermission {
+ CLAuthorizationStatus status = self.manager.authorizationStatus;
+ return status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse;
+}
+- (void)requestPermission:(void (^)(BOOL))completion {
+ if (self.manager.authorizationStatus != kCLAuthorizationStatusNotDetermined) {
+ completion([self hasPermission]);
+ return;
+ }
+ [self.permissionCallbacks addObject:[completion copy]];
+ [self.manager requestWhenInUseAuthorization];
+}
+- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager {
+ if (manager.authorizationStatus == kCLAuthorizationStatusNotDetermined) return;
+ NSArray *callbacks = [self.permissionCallbacks copy];
+ [self.permissionCallbacks removeAllObjects];
+ for (void (^callback)(BOOL) in callbacks) callback([self hasPermission]);
+ if (![self hasPermission]) [self finishWithError:@"Location permission is required for true north"];
+}
+- (void)requestDeclination:(void (^)(double, double, double, NSString *))completion {
+ if (![self hasPermission] || ![CLLocationManager headingAvailable]) {
+ completion(0, 0, 0, @"Location or heading unavailable for true north");
+ return;
+ }
+ [self.declinationCallbacks addObject:[completion copy]];
+ if (self.timer) return;
+ self.location = nil;
+ self.heading = nil;
+ // trueHeading is valid only while the same manager receives location updates.
+ // Subtracting magneticHeading cancels the phone's orientation: the result is
+ // a local correction to the GLASSES heading, never the phone's heading.
+ [self.manager startUpdatingLocation];
+ [self.manager startUpdatingHeading];
+ self.timer = [NSTimer scheduledTimerWithTimeInterval:20 repeats:NO block:^(NSTimer *timer) {
+ [self finishWithError:@"Timed out obtaining the true-north correction"];
+ }];
+}
+- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
+ CLLocation *location = locations.lastObject;
+ if (!location || location.horizontalAccuracy < 0 || fabs(location.timestamp.timeIntervalSinceNow) > 60) return;
+ self.location = location;
+ [self finishIfReady];
+}
+- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)heading {
+ if (heading.headingAccuracy < 0 || heading.trueHeading < 0 || heading.magneticHeading < 0 ||
+ fabs(heading.timestamp.timeIntervalSinceNow) > 60) return;
+ self.heading = heading;
+ [self finishIfReady];
+}
+- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager { return NO; }
+- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
+ if (error.code != kCLErrorLocationUnknown) [self finishWithError:error.localizedDescription];
+}
+- (void)finishIfReady {
+ if (self.location && self.heading) [self finishWithError:@""];
+}
+- (void)finishWithError:(NSString *)error {
+ double degrees = self.heading.trueHeading - self.heading.magneticHeading;
+ if (degrees > 180) degrees -= 360;
+ if (degrees < -180) degrees += 360;
+ CLLocationCoordinate2D coordinate = self.location.coordinate;
+ NSArray *callbacks = [self.declinationCallbacks copy];
+ [self.declinationCallbacks removeAllObjects];
+ [self.timer invalidate]; self.timer = nil;
+ [self.manager stopUpdatingLocation];
+ [self.manager stopUpdatingHeading];
+ self.location = nil; self.heading = nil;
+ for (void (^callback)(double, double, double, NSString *) in callbacks)
+ callback(degrees, coordinate.latitude, coordinate.longitude, error);
+}
+@end
diff --git a/App_Resources/iOS/src/module.modulemap b/App_Resources/iOS/src/module.modulemap
index 7f9d2b72..08938423 100644
--- a/App_Resources/iOS/src/module.modulemap
+++ b/App_Resources/iOS/src/module.modulemap
@@ -5,6 +5,7 @@ module FaceclawGraphics {
header "FaceclawSocket.h"
header "FaceclawLc3Decoder.h"
header "FaceclawSpeech.h"
+ header "FaceclawLocation.h"
header "FaceclawConfigPort.h"
header "FaceclawEvenHubWebView.h"
export *
diff --git a/app/apps/compass/compass-app.ts b/app/apps/compass/compass-app.ts
index 85e0f287..872e6079 100644
--- a/app/apps/compass/compass-app.ts
+++ b/app/apps/compass/compass-app.ts
@@ -21,7 +21,7 @@ import {
type InProcessWindow,
} from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
-import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
+import { ensureLocationPermission, hasLocationPermission } from "../../native/location-permissions";
import { isCompassCalibrated, normalizeHeading } from "./calibration";
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "./compass-rose";
import { CompassCalibrationLayer } from "./calibration-layer";
@@ -268,6 +268,7 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
+ reconfigureSurface: options.reconfigureSurface,
onClosed: () => {
layer.stop();
options.onClosed();
diff --git a/app/apps/compass/declination.ts b/app/apps/compass/declination.ts
index beb9cfe5..a3d04cca 100644
--- a/app/apps/compass/declination.ts
+++ b/app/apps/compass/declination.ts
@@ -2,16 +2,16 @@
* Local magnetic declination for the compass, so the app can show true north.
*
* Declination only varies over tens of kilometres, so the location it needs is
- * whatever the phone can cheaply produce: the same one-shot coarse lookup the
- * Weather app uses, refreshed no more than daily. The last fix is persisted so
- * a fresh start has a usable declination before location comes back, and the
- * model is re-evaluated at the current time when it does.
+ * whatever the phone can cheaply produce, refreshed no more than daily. The
+ * last fix is persisted so a fresh start has a usable correction immediately.
+ * Android re-evaluates its magnetic model; iOS caches the correction supplied
+ * by Core Location (true heading minus magnetic heading).
*/
-import { hasLocationPermission } from "../../g2/android-permissions";
-import { magneticDeclinationDegrees } from "../../native/geomagnetic";
-import { getCurrentLocation } from "../../native/location";
+import { hasLocationPermission } from "../../native/location-permissions";
+import { getCurrentDeclination, restoreDeclination } from "../../native/declination";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
+const DEGREES_KEY = "compass.declination.degrees";
const LATITUDE_KEY = "compass.declination.latitude";
const LONGITUDE_KEY = "compass.declination.longitude";
const LOCATED_AT_KEY = "compass.declination.locatedAtMs";
@@ -40,7 +40,7 @@ function loadStoredFix(): void {
const locatedAtMs = parseInt(getStringSetting(LOCATED_AT_KEY, ""), 10);
if (Number.isFinite(latitude) && Number.isFinite(longitude) && Number.isFinite(locatedAtMs)) {
fix = { latitude, longitude, locatedAtMs };
- declination = magneticDeclinationDegrees(latitude, longitude);
+ declination = restoreDeclination(latitude, longitude, parseFloat(getStringSetting(DEGREES_KEY, "")));
}
}
@@ -73,16 +73,17 @@ export function refreshDeclination(): void {
loadStoredFix();
if (refreshing || !hasLocationPermission()) return;
const now = Date.now();
- if (fix !== null && now - fix.locatedAtMs < FIX_MAX_AGE_MS) return;
+ if (declination !== null && fix !== null && now - fix.locatedAtMs < FIX_MAX_AGE_MS) return;
if (now - lastAttemptMs < RETRY_INTERVAL_MS) return;
lastAttemptMs = now;
refreshing = true;
- getCurrentLocation()
+ getCurrentDeclination()
.then((location) => {
- const value = magneticDeclinationDegrees(location.latitude, location.longitude);
- if (value === null) return;
+ const value = location.degrees;
+ if (!Number.isFinite(value)) return;
fix = { latitude: location.latitude, longitude: location.longitude, locatedAtMs: now };
declination = value;
+ setStringSetting(DEGREES_KEY, String(value));
setStringSetting(LATITUDE_KEY, String(location.latitude));
setStringSetting(LONGITUDE_KEY, String(location.longitude));
setStringSetting(LOCATED_AT_KEY, String(now));
diff --git a/app/apps/glanceboard/widgets/compass-widget.ts b/app/apps/glanceboard/widgets/compass-widget.ts
index 0a3a6d1d..8e6716f0 100644
--- a/app/apps/glanceboard/widgets/compass-widget.ts
+++ b/app/apps/glanceboard/widgets/compass-widget.ts
@@ -1,6 +1,6 @@
import { type GrayImage } from "../../../graphics/image";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
-import { hasLocationPermission } from "../../../g2/android-permissions";
+import { hasLocationPermission } from "../../../native/location-permissions";
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled } from "../../../native/compass";
import { isCompassCalibrated, normalizeHeading } from "../../compass/calibration";
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "../../compass/compass-rose";
diff --git a/app/apps/ios-availability.ts b/app/apps/ios-availability.ts
index 4a6b794a..bae12f44 100644
--- a/app/apps/ios-availability.ts
+++ b/app/apps/ios-availability.ts
@@ -4,7 +4,6 @@ const unavailable: Record = {
notifications: 'Phone notification integration is not available on iOS yet.',
transcribe: 'Voice capture and transcription are not available on iOS yet.',
microphones: 'Glasses microphone capture is not available on iOS yet.',
- compass: 'Glasses compass streaming is not available on iOS yet.',
navigate: 'Navigation location tracking is not available on iOS yet.',
weather: 'Weather location access is not available on iOS yet.',
calendar: 'Calendar access is not available on iOS yet.',
diff --git a/app/g2/ble-protocol.ios.ts b/app/g2/ble-protocol.ios.ts
index e22945dc..e2cfaa80 100644
--- a/app/g2/ble-protocol.ios.ts
+++ b/app/g2/ble-protocol.ios.ts
@@ -1,3 +1,4 @@
+import { type CompassEvent } from '../native/compass-types'
/** iOS binary bridge to the same Kotlin protocol used by Android. */
import { fromData, toData, nativeArray } from '../native/kotlin-data'
import { OsEventTypeList } from './events'
@@ -64,3 +65,13 @@ export function packGray4(gray: Uint8Array, width: number, height: number): Uint
return fromData(protocol.packDataWidthHeight(toData(gray), width, height))
}
export const rle4 = (packed: Uint8Array): Uint8Array => fromData(protocol.rleData(toData(packed)))
+
+export function decodeCompassInput(message: ProtocolMessage): CompassEvent | null {
+ const event = protocol.compassInputDataSidFlag(toData(message.payload), message.sid, message.flag)
+ return event ? { command: event.command, headingDegrees: event.headingDegrees,
+ ...(event.diagnosticFlags >= 0 ? { diagnostics: {
+ magneticAccuracy: event.magneticAccuracy, magneticAnomalies: event.magneticAnomalies,
+ orientationSource: event.orientationSource, flags: event.diagnosticFlags, sampleTimeMs: Number(event.sampleTimeMs),
+ } } : {}),
+ } : null
+}
diff --git a/app/g2/ble-protocol.ts b/app/g2/ble-protocol.ts
index 9fd16184..e8115773 100644
--- a/app/g2/ble-protocol.ts
+++ b/app/g2/ble-protocol.ts
@@ -1,3 +1,4 @@
+import { type CompassEvent } from '../native/compass-types'
/** G2 wire protocol, ported from g2protocol/BleProtocol.java. No platform APIs. */
import { OsEventTypeList } from './events'
export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401'
@@ -178,3 +179,23 @@ export function rle4(packed: Uint8Array): Uint8Array {
}
return output.slice(0, used)
}
+
+/** Reference decoder for host tests; iOS uses the shared Kotlin implementation. */
+export function decodeCompassInput(message: ProtocolMessage): CompassEvent | null {
+ if (message.sid !== 8 || ![1, 6].includes(message.flag)) return null
+ const command = readInteger(message.payload, 1, -1)
+ if (command === 16 || command === 17) return { command, headingDegrees: -1 }
+ if (command !== 15) return null
+ const compass = readBytes(message.payload, 10)
+ const headingDegrees = compass ? readInteger(compass, 1, -1) : -1
+ if (headingDegrees < 0 || headingDegrees >= 360) return null
+ const event: CompassEvent = { command, headingDegrees }
+ let d: Uint8Array | undefined
+ try { d = readBytes(message.payload, 100) } catch { return event }
+ if (d?.length === 12 && d[0] === 67 && d[1] === 77 && d[2] === 1 && d[7] === 0 &&
+ (d[3] <= 3 || d[3] === 255) && (d[4] <= 2 || d[4] === 255) && d[5] <= 3) {
+ event.diagnostics = { magneticAccuracy: d[3] === 255 ? -1 : d[3], magneticAnomalies: d[4] === 255 ? -1 : d[4],
+ orientationSource: d[5], flags: d[6], sampleTimeMs: new DataView(d.buffer, d.byteOffset + 8, 4).getUint32(0, true) }
+ }
+ return event
+}
diff --git a/app/g2/glasses-session.ts b/app/g2/glasses-session.ts
index 16251ed7..d3aeca16 100644
--- a/app/g2/glasses-session.ts
+++ b/app/g2/glasses-session.ts
@@ -1,3 +1,4 @@
+import { type CompassEvent } from '../native/compass-types'
import * as protocol from './ble-protocol'
import { iosBleTraffic } from './ble-traffic-counters'
import { buildBoundingBoxPayload, buildFullFrameBands } from './ble-image-optimizer'
@@ -74,11 +75,16 @@ export class GlassesSession {
private microphoneToken = 0
private microphoneWork: Promise = Promise.resolve()
private microphoneEnabled = false
+ private compassWanted = false
+ private compassSent: boolean | null = null
+ private compassStopping = false
+ private compassWork: Promise = Promise.resolve()
constructor(private readonly transport: SessionTransport,
private readonly onState: (state: SessionState) => void,
private readonly onInput: (input: protocol.GlassesInput) => void,
private readonly log: (message: string) => void = () => {},
- private readonly onActivity: () => void = () => {}) {
+ private readonly onActivity: () => void = () => {},
+ private readonly onCompass: (event: CompassEvent) => void = () => {}) {
this.off = transport.onEvent(event => this.receive(event))
}
private update(phase: SessionState['phase'], status: string): void {
@@ -88,7 +94,7 @@ export class GlassesSession {
if (['connecting', 'connected', 'disconnecting'].includes(this.state.phase)) return
const invalid = deviceAddressError(addresses)
if (invalid) { this.update('error', invalid); return }
- this.addresses = { ...addresses }; this.wanted = true
+ this.addresses = { ...addresses }; this.wanted = true; this.compassStopping = false
if (!retry) this.retryCount = 0
const generation = ++this.generation
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false }
@@ -131,6 +137,7 @@ export class GlassesSession {
this.lastHeartbeat = this.lastLease = this.lastSettings = Date.now()
this.displayed = null; this.retryCount = 0
this.update('connected', 'Glasses connected')
+ await this.syncCompass(); this.check(generation)
this.schedule()
if (addresses.ring && ids.ring) await this.connectRing(generation)
else if (addresses.ring) this.update('connected', 'Glasses connected; configured ring was not found')
@@ -347,6 +354,10 @@ export class GlassesSession {
}
if (message.sid === protocol.SID.settings) this.applySettings(message)
if (event.identifier === this.ids.right) {
+ if (this.state.phase === 'connected') {
+ const compass = protocol.decodeCompassInput(message)
+ if (compass) this.onCompass(compass)
+ }
const input = protocol.decodeGlassesInput(message)
if (input) {
if (input.kind === 'sys-event' && [5, 6, 7].includes(input.eventType) && this.state.phase === 'connected') {
@@ -406,6 +417,24 @@ export class GlassesSession {
this.microphoneWork = work
return work
}
+ setCompassEnabled(enabled: boolean): void {
+ this.compassWanted = enabled
+ void this.syncCompass()
+ }
+ /** Serialize changes so a release cannot overtake an unacknowledged enable. */
+ private syncCompass(): Promise {
+ const generation = this.generation
+ const work = this.compassWork.then(async () => {
+ if (generation !== this.generation || this.state.phase !== 'connected') return
+ const enabled = !this.compassStopping && this.compassWanted
+ if (this.compassSent === enabled) return
+ // Same CFW mode 10, 100 ms report interval and zero minimum change as Android.
+ await this.requestCfw('left', new Uint8Array(enabled ? [10, 2, 100, 0, 0, 0] : [10, 0]))
+ if (generation === this.generation) this.compassSent = enabled
+ }).catch(error => { if (generation === this.generation) this.fail(error, true) })
+ this.compassWork = work
+ return work
+ }
wake(): void {
if (this.timer !== null) { clearTimeout(this.timer); this.timer = null }
void this.pump()
@@ -480,6 +509,7 @@ export class GlassesSession {
}
private reset(): void {
++this.generation
+ this.compassSent = null; this.compassWork = Promise.resolve()
++this.microphoneToken; this.audioListener = null; this.microphoneEnabled = false
if (this.timer !== null) clearTimeout(this.timer)
if (this.retryTimer !== null) clearTimeout(this.retryTimer)
@@ -516,6 +546,8 @@ export class GlassesSession {
}
private async stopInternal(): Promise {
this.wanted = false
+ this.compassStopping = true
+ await this.syncCompass()
// Stop mic capture before ending the display session. Late enable ACKs
// cannot leave a microphone streaming after an explicit Disconnect.
if (this.audioListener || this.microphoneEnabled) {
diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts
index 566eeed8..1f0b0a51 100644
--- a/app/g2/ios-preview-controller.ts
+++ b/app/g2/ios-preview-controller.ts
@@ -1,3 +1,4 @@
+import { bindCompassSession, receiveCompassEvent } from '../native/compass.ios'
import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core'
import { iosBluetooth } from '../native/ios-bluetooth'
import { iosVoiceInput } from '../native/ios-voice-input'
@@ -378,7 +379,8 @@ export class IosPreviewController {
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT)
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
}).catch(error => this.fail(error))
- }, message => this.logBluetooth(message), () => this.refreshClock())
+ }, message => this.logBluetooth(message), () => this.refreshClock(), receiveCompassEvent)
+ bindCompassSession(this.session)
}
await this.session.start(addresses)
}
diff --git a/app/native/compass-types.ts b/app/native/compass-types.ts
new file mode 100644
index 00000000..14ea05b7
--- /dev/null
+++ b/app/native/compass-types.ts
@@ -0,0 +1,23 @@
+export const COMPASS_CHANGED = 15;
+export const COMPASS_CALIBRATION_STARTED = 16;
+export const COMPASS_CALIBRATION_COMPLETE = 17;
+
+export type CompassDiagnostics = {
+ /** 0 uncalibrated .. 3 well calibrated; -1 unavailable. */
+ magneticAccuracy: number;
+ /** 0 none, 1 small/temperature, 2 large; -1 unavailable. */
+ magneticAnomalies: number;
+ /** 0 unknown, 1 GRV (relative), 2 GMRV, 3 RV. */
+ orientationSource: number;
+ flags: number;
+ sampleTimeMs: number;
+};
+
+export type CompassEvent = {
+ command: number;
+ /** Magnetic heading in degrees, or -1 for calibration-only events. */
+ headingDegrees: number;
+ /** Absent with stock/older CFW or an unrecognized diagnostic extension. */
+ diagnostics?: CompassDiagnostics;
+};
+
diff --git a/app/native/compass.ios.ts b/app/native/compass.ios.ts
new file mode 100644
index 00000000..ab201fce
--- /dev/null
+++ b/app/native/compass.ios.ts
@@ -0,0 +1,29 @@
+import { type CompassEvent } from './compass-types'
+export * from './compass-types'
+
+type CompassSession = { setCompassEnabled(enabled: boolean): void }
+const owners = new Set()
+const listeners = new Set<(event: CompassEvent) => void>()
+let session: CompassSession | null = null
+
+/** Holds and listeners survive disconnect/reconnect and opening before Connect. */
+export function bindCompassSession(next: CompassSession): void {
+ if (session && session !== next) session.setCompassEnabled(false)
+ session = next
+ session.setCompassEnabled(owners.size > 0)
+}
+
+export function setCompassEnabled(enabled: boolean, owner = 'compass'): void {
+ if (enabled) owners.add(owner)
+ else owners.delete(owner)
+ session?.setCompassEnabled(owners.size > 0)
+}
+
+export function addCompassListener(listener: (event: CompassEvent) => void): () => void {
+ listeners.add(listener)
+ return () => { listeners.delete(listener) }
+}
+
+export function receiveCompassEvent(event: CompassEvent): void {
+ for (const listener of [...listeners]) listener(event)
+}
diff --git a/app/native/compass.ts b/app/native/compass.ts
index 8a0d6491..dae5f42f 100644
--- a/app/native/compass.ts
+++ b/app/native/compass.ts
@@ -1,28 +1,8 @@
declare const com: any;
declare const global: any;
-export const COMPASS_CHANGED = 15;
-export const COMPASS_CALIBRATION_STARTED = 16;
-export const COMPASS_CALIBRATION_COMPLETE = 17;
-
-export type CompassDiagnostics = {
- /** 0 uncalibrated .. 3 well calibrated; -1 unavailable. */
- magneticAccuracy: number;
- /** 0 none, 1 small/temperature, 2 large; -1 unavailable. */
- magneticAnomalies: number;
- /** 0 unknown, 1 GRV (relative), 2 GMRV, 3 RV. */
- orientationSource: number;
- flags: number;
- sampleTimeMs: number;
-};
-
-export type CompassEvent = {
- command: number;
- /** Magnetic heading in degrees, or -1 for calibration-only events. */
- headingDegrees: number;
- /** Absent with stock/older CFW or an unrecognized diagnostic extension. */
- diagnostics?: CompassDiagnostics;
-};
+import { type CompassEvent } from "./compass-types";
+export * from "./compass-types";
function activeCommunicator(): any {
if (!global.isAndroid) return null;
diff --git a/app/native/declination.ios.ts b/app/native/declination.ios.ts
new file mode 100644
index 00000000..d8f8678f
--- /dev/null
+++ b/app/native/declination.ios.ts
@@ -0,0 +1,14 @@
+declare const FaceclawLocation: any
+
+export function restoreDeclination(_latitude: number, _longitude: number, degrees: number): number | null {
+ return Number.isFinite(degrees) && Math.abs(degrees) <= 180 ? degrees : null
+}
+
+export function getCurrentDeclination(): Promise<{ latitude: number; longitude: number; degrees: number }> {
+ return new Promise((resolve, reject) => {
+ FaceclawLocation.shared().requestDeclination((degrees: number, latitude: number, longitude: number, error: string) => {
+ if (error) reject(new Error(error))
+ else resolve({ degrees, latitude, longitude })
+ })
+ })
+}
diff --git a/app/native/declination.ts b/app/native/declination.ts
new file mode 100644
index 00000000..751ddae4
--- /dev/null
+++ b/app/native/declination.ts
@@ -0,0 +1,13 @@
+import { magneticDeclinationDegrees } from './geomagnetic'
+import { getCurrentLocation } from './location'
+
+export function restoreDeclination(latitude: number, longitude: number, _degrees: number): number | null {
+ return magneticDeclinationDegrees(latitude, longitude)
+}
+
+export async function getCurrentDeclination(): Promise<{ latitude: number; longitude: number; degrees: number }> {
+ const location = await getCurrentLocation()
+ const degrees = magneticDeclinationDegrees(location.latitude, location.longitude)
+ if (degrees === null) throw new Error('Magnetic declination unavailable')
+ return { ...location, degrees }
+}
diff --git a/app/native/kotlin-protocol-smoke.ios.ts b/app/native/kotlin-protocol-smoke.ios.ts
index 91c3ad21..b7c964f6 100644
--- a/app/native/kotlin-protocol-smoke.ios.ts
+++ b/app/native/kotlin-protocol-smoke.ios.ts
@@ -32,5 +32,9 @@ export function runKotlinProtocolSmokeTest(): void {
const changed = new Uint8Array(16); changed[0] = 255
assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === 3, 'image update')
assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge')
+ const compass = protocol.decodeCompassInput({ sid: 8, flag: 1, command: 15, magic: 0,
+ payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) })
+ if (compass?.headingDegrees !== 359 || compass.diagnostics?.sampleTimeMs !== 0xfedcba98)
+ throw new Error('Kotlin compass notification mismatch')
console.log('FACECLAW_KOTLIN_PROTOCOL_PASS iOS')
}
diff --git a/app/native/location-permissions.ios.ts b/app/native/location-permissions.ios.ts
new file mode 100644
index 00000000..aa668f93
--- /dev/null
+++ b/app/native/location-permissions.ios.ts
@@ -0,0 +1,5 @@
+declare const FaceclawLocation: any
+export const hasLocationPermission = (): boolean => FaceclawLocation.shared().hasPermission()
+export function ensureLocationPermission(): Promise {
+ return new Promise(resolve => FaceclawLocation.shared().requestPermission(resolve))
+}
diff --git a/app/native/location-permissions.ts b/app/native/location-permissions.ts
new file mode 100644
index 00000000..c4776a51
--- /dev/null
+++ b/app/native/location-permissions.ts
@@ -0,0 +1 @@
+export { ensureLocationPermission, hasLocationPermission } from '../g2/android-permissions'
diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt
index 4227cdb5..f8dd8058 100644
--- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt
+++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt
@@ -1027,13 +1027,14 @@ class BleProtocol {
/** Decode the stock firmware's compass navigation notifications (sid 0x08). */
@JvmStatic
fun parseCompassEvent(frame: ParsedFrame?): CompassEvent? {
- if (
- ((((frame == null) || !frame.ok) || (frame.sid != SID_NAVIGATION)) ||
- ((frame.flag != FLAG_NOTIFY) && (frame.flag != FLAG_NOTIFY_ALT)))
- ) {
- return null
- }
- var root: ByteArray = stripTrailingCrc(frame.pb)
+ if (frame == null || !frame.ok) return null
+ return parseCompassPayload(frame.sid, frame.flag, stripTrailingCrc(frame.pb))
+ }
+
+ /** MessageReceiver has already validated and removed the CRC. */
+ @JvmStatic
+ fun parseCompassPayload(sid: Int, flag: Int, root: ByteArray): CompassEvent? {
+ if (sid != SID_NAVIGATION || (flag != FLAG_NOTIFY && flag != FLAG_NOTIFY_ALT)) return null
var command: Int = readVarintFieldValue(root, 1, -1)
if ((command == NAV_CMD_COMPASS_CHANGED)) {
var compass: ByteArray? = readFieldBytes(root, 10)
diff --git a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt
index e461a9b6..2c1b364f 100644
--- a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt
+++ b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt
@@ -132,6 +132,9 @@ class IosProtocol {
G2Event.decodePayload(sid, data.byteArray())
else null
+ fun compassInput(data: NSData, sid: Int, flag: Int): BleProtocol.CompassEvent? =
+ BleProtocol.parseCompassPayload(sid, flag, data.byteArray())
+
fun ringInput(data: NSData): G2Event? = FaceclawRingEventDecoder.decode(data.byteArray())?.event
fun pack(data: NSData, width: Int, height: Int): NSData {
diff --git a/tests/compass-debug.test.cjs b/tests/compass-debug.test.cjs
index a000c57a..d9057409 100644
--- a/tests/compass-debug.test.cjs
+++ b/tests/compass-debug.test.cjs
@@ -59,7 +59,7 @@ function compass(width = 576, height = 260) {
createInProcessWindow: (options) => { window = options; return { requestRender: () => ++renders }; },
},
'../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
- '../../g2/android-permissions': { hasLocationPermission: () => false },
+ '../../native/location-permissions': { hasLocationPermission: () => false },
'./calibration': calibration,
'./calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} },
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) },
diff --git a/tests/compass-style.test.cjs b/tests/compass-style.test.cjs
index c6ef2d21..423e5f87 100644
--- a/tests/compass-style.test.cjs
+++ b/tests/compass-style.test.cjs
@@ -58,7 +58,7 @@ function compass({ style, width = 576, height = 260 } = {}) {
createInProcessWindow: (options) => { window = options; return { requestRender: () => renders++ }; },
},
'../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
- '../../g2/android-permissions': { hasLocationPermission: () => false },
+ '../../native/location-permissions': { hasLocationPermission: () => false },
'./calibration': { isCompassCalibrated: () => true, normalizeHeading: (n) => ((n % 360) + 360) % 360 },
'./calibration-layer': {},
'./compass-rose': load('app/apps/compass/compass-rose.ts', {
diff --git a/tests/compass-texture-cache.test.cjs b/tests/compass-texture-cache.test.cjs
index 2f947829..a544fa79 100644
--- a/tests/compass-texture-cache.test.cjs
+++ b/tests/compass-texture-cache.test.cjs
@@ -43,7 +43,7 @@ const compass = load('app/apps/compass/compass-app.ts', {
'../../ui/shell/geometry': { screenCenterInViewportX: () => viewportWidth - 320 },
'../../ui/shell/in-process-window': {},
'../../ui/shell/shell': {},
- '../../g2/android-permissions': {},
+ '../../native/location-permissions': {},
'./calibration': calibration,
'./calibration-layer': {},
'./compass-rose': rose,
diff --git a/tests/ios-app-host.test.cjs b/tests/ios-app-host.test.cjs
index 2d4b702c..e81d37a2 100644
--- a/tests/ios-app-host.test.cjs
+++ b/tests/ios-app-host.test.cjs
@@ -89,6 +89,7 @@ test('background glasses input still composites frames; phone resume preserves t
}
const settings = { onAnySettingChanged: () => () => {}, previewColorSetting: { get: () => 'white' } };
const modules = {
+ '../native/compass.ios': { bindCompassSession() {}, receiveCompassEvent() {} },
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) }, knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
'../native/ios-voice-input': { iosVoiceInput: { handleSessionEnded() {} } },
'../native/ios-bluetooth': { iosBluetooth: () => ({}) }, './glasses-session': { GlassesSession: Session },
@@ -156,6 +157,7 @@ test('iOS bandwidth footer toggles live, polls only in foreground and resets its
on: (key, fn) => appEvents.set(key, fn), off: key => appEvents.delete(key) },
Button: View, Color: class {}, Dialogs: {}, GridLayout: View, Image: View, Label: View, Page: View, StackLayout: View };
const modules = {
+ '../native/compass.ios': { bindCompassSession() {}, receiveCompassEvent() {} },
'@nativescript/core': core,
'../apps/all-apps': { ALL_APPS: [] },
'../g2/ios-preview-controller': { IosPreviewController: class { resume() {} pause() {} } },
diff --git a/tests/ios-ble.test.cjs b/tests/ios-ble.test.cjs
index 55694b12..5d28078a 100644
--- a/tests/ios-ble.test.cjs
+++ b/tests/ios-ble.test.cjs
@@ -159,10 +159,10 @@ async function until(predicate) {
assert.fail('Timed out waiting for test state');
}
function harness(t, transport = new FakeTransport()) {
- const states = [], input = [];
- const session = new GlassesSession(transport, state => states.push(state), event => input.push(event));
+ const states = [], input = [], compass = [];
+ const session = new GlassesSession(transport, state => states.push(state), event => input.push(event), undefined, undefined, event => compass.push(event));
t.after(async () => { transport.hold = false; await session.stop(); });
- return { transport, session, states, input };
+ return { transport, session, states, input, compass };
}
test('session authenticates both arms, checks firmware and sends acknowledged display data to L', async t => {
const h = harness(t); await h.session.start({ ...addresses, ring: 'AA:BB:CC:DD:EE:03' });
@@ -171,7 +171,7 @@ test('session authenticates both arms, checks firmware and sends acknowledged di
assert.equal(h.session.state.charging, false);
assert.deepEqual(h.transport.sent.filter(s => s.message.sid === 128).map(s => s.id).sort(), ['L', 'R']);
h.session.setFrame(new Uint8Array(640 * 480)); await until(() => h.session.state.frames === 1);
- const image = h.transport.sent.find(s => s.message.sid === p.SID.cfw);
+ const image = h.transport.sent.find(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 6);
assert.equal(image.id, 'L');
assert.equal(image.message.payload[0], 6);
const layout = h.transport.sent.find(s => s.message.sid === p.SID.hub && s.message.command === 0);
@@ -293,7 +293,7 @@ test('release while microphone enable awaits ACK sends disable last and rejects
assert.equal(p.readInteger(p.readBytes(mic.at(-1).message.payload, 18), 1), 0);
});
-const imageMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && s.message.payload[0] !== 11);
+const imageMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && ![10, 11].includes(s.message.payload[0]));
const imageBody = sent => sent.message.payload;
const ack = (transport, sent, overrides) => transport.ack(sent.id, sent.message, overrides);
const tick = () => new Promise(resolve => setImmediate(resolve));
@@ -467,3 +467,47 @@ test('delta ids skip reserved values and advance only on emitted deltas; reconne
await h.session.stop(); await h.session.start(addresses);
assert.equal((await submit(base))[0], 6); // Even the same pixels need a trusted full base.
});
+
+const compassMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 10);
+test('compass desired state survives connecting and reconnecting; explicit stop disables it first', async t => {
+ const h = harness(t);
+ h.session.setCompassEnabled(true);
+ await h.session.start(addresses);
+ assert.equal(compassMessages(h.transport).length, 1);
+ assert.equal(compassMessages(h.transport)[0].id, 'L');
+ assert.deepEqual([...compassMessages(h.transport)[0].message.payload], [10, 2, 100, 0, 0, 0]);
+ await h.session.stop();
+ assert.deepEqual([...compassMessages(h.transport).at(-1).message.payload], [10, 0]);
+ assert.equal(h.transport.sent.at(-1).message.payload[0], 11);
+ await h.session.start(addresses);
+ assert.equal(compassMessages(h.transport).at(-1).message.payload[1], 2);
+ h.transport.emit({ kind: 'disconnected', identifier: 'R' });
+ assert.equal(h.session.state.phase, 'retrying');
+ await h.session.start(addresses, true);
+ assert.equal(compassMessages(h.transport).at(-1).message.payload[1], 2);
+});
+test('release during pending compass enable sends disable after the enable ACK', async t => {
+ const h = harness(t); await h.session.start(addresses);
+ assert.deepEqual([...compassMessages(h.transport)[0].message.payload], [10, 0]);
+ h.transport.hold = true;
+ h.session.setCompassEnabled(true);
+ await until(() => h.transport.held.length === 1);
+ h.session.setCompassEnabled(false);
+ const enabling = h.transport.held.shift();
+ h.transport.hold = false; h.transport.ack(enabling.id, enabling.message);
+ await until(() => compassMessages(h.transport).length === 3);
+ assert.deepEqual(compassMessages(h.transport).map(s => s.message.payload[1]), [0, 2, 0]);
+});
+test('compass notifications only come from the right arm and preserve diagnostics and calibration', async t => {
+ const h = harness(t); await h.session.start(addresses);
+ const payload = new Uint8Array(Buffer.from('080f1000520308e702a2060c434d010302038c0098badcfe', 'hex'));
+ const emit = (id, sid = 8, flag = 1, body = payload) => h.transport.emit({ kind: 'notification', identifier: id,
+ characteristic: p.G2_NOTIFY, data: hex(p.concat(...p.frameMessage(body, sid, flag, 20))) });
+ emit('L'); emit('other'); emit('R', 9); emit('R', 8, 0x20);
+ assert.equal(h.compass.length, 0);
+ emit('R'); emit('R', 8, 6, new Uint8Array([8, 16, 16, 0]));
+ emit('R', 8, 1, new Uint8Array([8, 17, 16, 0]));
+ assert.deepEqual(h.compass, [{ command: 15, headingDegrees: 359, diagnostics: {
+ magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 140, sampleTimeMs: 0xfedcba98,
+ } }, { command: 16, headingDegrees: -1 }, { command: 17, headingDegrees: -1 }]);
+});
diff --git a/tests/ios-compass.test.cjs b/tests/ios-compass.test.cjs
new file mode 100644
index 00000000..a348dba3
--- /dev/null
+++ b/tests/ios-compass.test.cjs
@@ -0,0 +1,86 @@
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const fs = require('node:fs');
+const vm = require('node:vm');
+const ts = require('typescript');
+const path = require('node:path');
+function load(file, context = {}) {
+ const sandbox = { exports: {}, console, ...context };
+ vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
+ compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
+ }).outputText, sandbox);
+ return sandbox.exports;
+}
+
+test('iOS compass owners and listeners work before connection and across session replacement', () => {
+ const api = load('app/native/compass.ios.ts', { require: () => ({}) });
+ const states = [], nextStates = [], events = [];
+ const off = api.addCompassListener(event => events.push(event));
+ api.setCompassEnabled(true, 'compass');
+ api.setCompassEnabled(true, 'glanceboard');
+ api.bindCompassSession({ setCompassEnabled: enabled => states.push(enabled) });
+ api.setCompassEnabled(false, 'compass');
+ assert.deepEqual(states, [true, true]);
+ api.bindCompassSession({ setCompassEnabled: enabled => nextStates.push(enabled) });
+ assert.equal(states.at(-1), false);
+ api.setCompassEnabled(false, 'glanceboard');
+ assert.deepEqual(nextStates, [true, false]);
+ api.receiveCompassEvent({ command: 15, headingDegrees: 271 });
+ off(); api.receiveCompassEvent({ command: 15, headingDegrees: 90 });
+ assert.deepEqual(events, [{ command: 15, headingDegrees: 271 }]);
+});
+
+test('shared declination cache handles iOS fixes, persisted corrections and unavailable sensors', async () => {
+ const store = new Map(), listeners = [];
+ let permission = false, lookups = 0, fail = false;
+ let now = 1_800_000_000_000;
+ const ios = load('app/native/declination.ios.ts');
+ const context = {
+ Date: { now: () => now }, console: { warn() {} },
+ require: id => ({
+ '../../native/location-permissions': { hasLocationPermission: () => permission },
+ '../../native/declination': { restoreDeclination: ios.restoreDeclination, getCurrentDeclination: async () => {
+ lookups++; if (fail) throw new Error('No heading sensor');
+ return { latitude: 37.7, longitude: -122.4, degrees: 12.5 };
+ } },
+ '../../native/settings-store': { getStringSetting: (key, fallback) => store.get(key) ?? fallback,
+ setStringSetting: (key, value) => store.set(key, value) },
+ })[id],
+ };
+ const api = load('app/apps/compass/declination.ts', context);
+ api.onDeclinationChanged(() => listeners.push(api.getDeclinationDegrees()));
+ assert.equal(api.getDeclinationAvailability(), 'no-permission');
+ api.refreshDeclination(); assert.equal(lookups, 0);
+ permission = true;
+ assert.equal(api.getDeclinationAvailability(), 'no-fix');
+ api.refreshDeclination(); api.refreshDeclination();
+ await new Promise(resolve => setImmediate(resolve));
+ assert.equal(lookups, 1); assert.deepEqual(listeners, [12.5]);
+ assert.equal(api.getDeclinationAvailability(), 'available');
+ const restarted = load('app/apps/compass/declination.ts', context);
+ assert.equal(restarted.getDeclinationDegrees(), 12.5);
+ restarted.refreshDeclination(); assert.equal(lookups, 1);
+ now += 25 * 60 * 60 * 1000; fail = true;
+ restarted.refreshDeclination(); await new Promise(resolve => setImmediate(resolve));
+ assert.equal(restarted.getDeclinationDegrees(), 12.5);
+ restarted.refreshDeclination(); assert.equal(lookups, 2);
+ // Existing Android preferences have coordinates but no cached correction.
+ store.delete('compass.declination.degrees'); fail = false;
+ store.set('compass.declination.locatedAtMs', String(now));
+ const migrated = load('app/apps/compass/declination.ts', context);
+ assert.equal(migrated.getDeclinationDegrees(), null);
+ migrated.refreshDeclination(); await new Promise(resolve => setImmediate(resolve));
+ assert.equal(migrated.getDeclinationDegrees(), 12.5);
+});
+
+test('iOS native correction callbacks resolve values or propagate unavailable errors', async () => {
+ let unavailable = false;
+ const api = load('app/native/declination.ios.ts', { FaceclawLocation: { shared: () => ({
+ requestDeclination: callback => callback(-8.25, 51, 0, unavailable ? 'Heading unavailable' : ''),
+ }) } });
+ assert.deepEqual({ ...await api.getCurrentDeclination() }, { latitude: 51, longitude: 0, degrees: -8.25 });
+ unavailable = true;
+ await assert.rejects(api.getCurrentDeclination(), /Heading unavailable/);
+ assert.equal(api.restoreDeclination(0, 0, NaN), null);
+ assert.equal(api.restoreDeclination(0, 0, 360), null);
+});
diff --git a/tests/ios-glanceboard.test.cjs b/tests/ios-glanceboard.test.cjs
index e8cb9e07..eb518f36 100644
--- a/tests/ios-glanceboard.test.cjs
+++ b/tests/ios-glanceboard.test.cjs
@@ -68,6 +68,7 @@ function fixture() {
paint: () => { boardStats.paints++; return new images.GrayImage(100, 80, 200); } }),
};
const modules = {
+ "../native/compass.ios": { bindCompassSession() {}, receiveCompassEvent() {} },
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) },
knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
'../native/ios-bluetooth': { iosBluetooth: () => ({}) },
diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/CompassTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/CompassTest.kt
new file mode 100644
index 00000000..08383b72
--- /dev/null
+++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/CompassTest.kt
@@ -0,0 +1,40 @@
+package com.faceclaw.app
+
+import kotlin.test.*
+
+class CompassTest {
+ private fun hex(text: String) = text.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
+
+ @Test
+ fun receiverPayloadAndAndroidFrameDecodeTheSameCompassSample() {
+ val payload = hex("080f1000520308e702a2060c434d010302038c0098badcfe")
+ for (flag in listOf(1, 6)) {
+ val frame = BleProtocol.framePb(payload, 8, flag, 15).single()
+ val received = MessageReceiver().receive("R", frame, 0).single()
+ val shared = assertNotNull(BleProtocol.parseCompassPayload(received.sid, received.flag, received.payload))
+ val android = assertNotNull(BleProtocol.parseCompassEvent(BleProtocol.parseFrame(frame)))
+ assertEquals(359, shared.headingDegrees)
+ assertEquals(15, shared.command)
+ assertEquals(3, shared.magneticAccuracy)
+ assertEquals(2, shared.magneticAnomalies)
+ assertEquals(3, shared.orientationSource)
+ assertEquals(0x8c, shared.diagnosticFlags)
+ assertEquals(0xfedcba98L, shared.sampleTimeMs)
+ assertEquals(android.sampleTimeMs, shared.sampleTimeMs)
+ }
+ assertNull(BleProtocol.parseCompassPayload(9, 1, payload))
+ assertNull(BleProtocol.parseCompassPayload(8, 0x20, payload))
+ assertNull(BleProtocol.parseCompassPayload(8, 1, hex("080f1000520308e802")))
+ assertNull(BleProtocol.parseCompassPayload(8, 1, byteArrayOf()))
+ for (size in 9 until payload.size) {
+ val legacy = assertNotNull(BleProtocol.parseCompassPayload(8, 1, payload.copyOf(size)))
+ assertEquals(359, legacy.headingDegrees)
+ assertEquals(-1, legacy.diagnosticFlags)
+ }
+ for (command in listOf(16, 17)) {
+ val calibration = assertNotNull(BleProtocol.parseCompassPayload(8, 1, byteArrayOf(8, command.toByte(), 16, 0)))
+ assertEquals(command, calibration.command)
+ assertEquals(-1, calibration.headingDegrees)
+ }
+ }
+}
diff --git a/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt
index 2d0f0191..b81255a5 100644
--- a/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt
+++ b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt
@@ -22,6 +22,14 @@ class IosNativeApiTest {
assertContentEquals(auth.byteArray(), result.payload.byteArray())
}
+ @Test
+ fun compassFacadePreservesTheEndOfTheCrcFreePayload() {
+ val protocol = IosProtocol()
+ val payload = byteArrayOf(8, 15, 16, 0, 82, 3, 8, -25, 2).data()
+ assertEquals(359, protocol.compassInput(payload, 8, 1)?.headingDegrees)
+ assertNull(protocol.compassInput(payload, 8, 0))
+ }
+
@Test
fun nativeCompositorClipsPartialUpdatesBeforeCoalescing() {
val compositor = IosSurfaceCompositor(3, 2)