Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Duo: take WebDriverAgent's hinge angle on the guest

Jacky02569bfc6281
diff --git a/Research/0_binary_patch_comparison.md b/Research/0_binary_patch_comparison.md
index c735f18c..b8cf435b 100644
--- a/Research/0_binary_patch_comparison.md
+++ b/Research/0_binary_patch_comparison.md
@@ -772,6 +772,29 @@
> No patch was added or changed; see `Research/Guest/iphone_duo_bringup.md`,
> "the inner display's type, and XCTest's screenshot of it".
+> **Client-dispatched hinge values (2026-11-02, issue #784):**
+> `system-backboardd-cfw-hinge`'s `libvphonehinge.dylib` now, on iPhone19,4
+> only, detours IOKit's exported `_io_hideventsystem_dispatch_event` in
+> backboardd from its constructor, with libmisfix's detour
+> (`MISFix/MISFixDetour.c`, built into the hinge). An event that is
+> DeviceHub's hinge slider (host-control vendor event `0xFF61/0x5B`, its
+> IOKit dictionary naming provider `com.apple.Virtualization.VirtualMachines`,
+> `hinge-slider-control`, `range`, value 0–180) never reaches the server,
+> which would refuse a client without
+> `com.apple.private.hid.client.event-dispatch`: the detour frees its buffer,
+> answers success, and the hinge dispatches the event unchanged on
+> the hinge's own host-control service, where locationd's relay and vphoned
+> follow it as they follow DeviceHub's. This is what WebDriverAgent's
+> `POST /wda/device/hingeAngle` sends, from a runner without the
+> entitlement. `MISFixDetour` also now refuses to relocate the PAuth_LR
+> prologues (`PACIASPPC`, `PACIBSPPC`, `PACNBIASPPC`, `PACNBIBSPPC`), which
+> sign with their own address; the X1-lowered cache a guest runs on a host
+> without PAuth_LR has the SP-only forms there. Checked in
+> `make test-hinge`; measured on sbdisplay-duo (27.1): WDA POST 120, 0 and
+> 180 move `fold_angle` and WDA's CoreMotion reading to each, and DeviceHub's
+> own buttons still fold the guest. No firmware patch was added or changed;
+> see `Research/Guest/iphone_duo_bringup.md`, "WebDriverAgent's hinge POST".
+
> **SpringBoard fold-guard (2026-10-25):** `system-springboard-cfw-fold_guard`
> (guest system set, on in `standard`, blocked in `tvos`, `tvos-4k` and
> `homepod-mini`, foldable guests only, iOS 27+; installs
diff --git a/Research/Guest/iphone_duo_bringup.md b/Research/Guest/iphone_duo_bringup.md
index 47ffd6b1..7e224217 100644
--- a/Research/Guest/iphone_duo_bringup.md
+++ b/Research/Guest/iphone_duo_bringup.md
@@ -5316,4 +5316,113 @@ page 0xFF61, usage 0x5B, provider `com.apple.Virtualization.VirtualMachines`)
and dispatches it from its runner with `IOHIDEventSystemClientDispatchEvent`;
the guest's hinge relay follows the events DeviceHub's controls arrive in,
from dtuhidd's HID service, and nothing takes a client-dispatched one. Its
-GET reads CMAngleManager and reports the guest's hinge (180 open).
+GET reads CMAngleManager and reports the guest's hinge (180 open). Taken
+since; see "2026-11-02: WebDriverAgent's hinge POST".
+
+## 2026-11-02: WebDriverAgent's hinge POST
+
+WDA's `POST /wda/device/hingeAngle` (appium-ios 16.17.x,
+`XCUIDevice+FBHinge.m`) builds DeviceHub's slider dictionary
+`{provider: com.apple.Virtualization.VirtualMachines, source:
+hinge-slider-control, type: range, value: <angle>}`, IOKit
+binary-serializes it, wraps it in `IOHIDEventCreateVendorDefinedEvent`
+(page `0xFF61`, usage `0x5B`) and sends it from the runner with
+`IOHIDEventSystemClientCreate` + `IOHIDEventSystemClientDispatchEvent`.
+The dispatch returns nothing, so the endpoint answers WDA's success
+(`FBResponseWithOK`, `value: null`) whatever happened; `null` is success
+here, and a refused argument comes back as an error object.
+
+**Where it went.** backboardd's HID server refused it, every time:
+
+ Error: the client with pid 1211 is missing the
+ 'com.apple.private.hid.client.event-dispatch' entitlement.
+
+The MIG stub `_Xio_hideventsystem_dispatch_event` calls
+`_io_hideventsystem_dispatch_event(port, data, length)` (IOKit, exported).
+That function turns the out-of-line buffer into a CFData
+(`_IOHIDUnserializeAndVMDeallocWithTypeID`, freeing the buffer), finds the
+connection, and for any connection type but 0 requires bit 3 of
+`IOHIDEventSystemConnectionGetEntitlements` before
+`IOHIDEventCreateWithDataInternal` and `_IOHIDEventSystemDispatchEvent`.
+WDA's connection is "Rate Controlled" with entitlements `0x0`. Even
+dispatched, the event would have had no service, and locationd's relay
+reads the host controls from services matching `0xFF61/0x5B`. A runner
+signed by a developer team cannot carry a private entitlement.
+
+**The buffer's formats** (iOS 27.1 IOKit, 24A94425), all needed to read it:
+
+- the buffer is a CFData in a **binary property list**
+ (`_IOHIDCreateBinaryData`: `CFPropertyListWrite`, format 200), not an
+ IOKit serialization; `IOCFUnserializeBinary` returns nil on it;
+- the CFData is the event in `IOHIDEventCreateDataInternal`'s form, read by
+ `IOHIDEventCreateWithDataInternal`; the public pair passes format flag 1,
+ the internal one 0;
+- the vendor payload inside is IOKit binary (WDA serializes with option 1,
+ as dtuhidd's DeviceHub events are), read with `IOCFUnserializeBinary`.
+
+**What changed.** `libvphonehinge` (backboardd, iPhone19,4 only) detours
+`_io_hideventsystem_dispatch_event` from its constructor, before backboardd
+serves clients. The detour decodes the buffer; an event that is a
+host-control vendor event whose dictionary names the VM provider,
+`hinge-slider-control`, `range` and a value of 0–180 is the hinge's: the
+detour frees the buffer as the server would, answers `KERN_SUCCESS`, and
+the hinge dispatches the event unchanged on its own `0xFF61/0x5B` virtual
+service from its queue. The server never sees it, so it neither refuses nor
+logs the missing entitlement. Anything else, including the orientation
+picker, goes to the server unchanged. From the hinge's service the event takes DeviceHub's path:
+locationd's relay moves the hinge, and vphoned's unmatched monitor sees the
+provider and follows it as a DeviceHub value (`vp_hinge_note`: stored
+quietly, the screen lit when opening from closed). The hinge status
+(`VPhoneHingeStatus`) gains `client_dispatch` (`installed` or why not) and
+`client_controls` (values taken).
+
+Ways not taken:
+
+- Granting the entitlement bit to unentitled connections would let any app
+ dispatch any event (touches, buttons).
+- Swapping the routine in `_iohideventsystem_subsystem` (found by the stub
+ whose `BL` targets the exported function; `blraaz`, so a plain function
+ pointer fits): the table is in the cache's `__AUTH_CONST`, and
+ `vm_protect(VM_PROT_COPY | READ | WRITE)` there returns
+ `KERN_PROTECTION_FAILURE`. Text pages take the copy, as libmisfix's
+ detour measured.
+- libmisfix's detour (`MISFix/MISFixDetour.c`, now built into the hinge too)
+ relocates the target's first four instructions. In an X1 cache the
+ prologue is `pacibsppc`, which signs LR with its own address; moved, it
+ signs for the trampoline and the function's `retabsppc` fails. The guest
+ runs the X1-lowered cache on hosts without PAuth_LR (this M5 Pro has
+ none), where the prologue is `pacibsp`. The detour now refuses
+ `PACIASPPC`, `PACIBSPPC`, `PACNBIASPPC` and `PACNBIBSPPC` instead of
+ copying them, so on an unlowered cache the hinge reports `HID server
+ dispatch not detoured: a displaced instruction is PC-relative` and WDA's
+ POST stays a no-op.
+
+**Measured** on sbdisplay-duo (iPhone19,4, iOS 27.1 24A94425, product-id
+patch on), the hinge library pushed with `environment.install` and
+backboardd restarted, WDA 16.17.x from appium-ios:
+
+| request | WDA GET | `display.orientation` `fold_angle` | `screen_off` |
+| --- | --- | --- | --- |
+| before the change, POST 120 | 180 | 180 | |
+| POST 120 | 120 | 120 | |
+| POST 0 | 0 | 0 | false |
+| POST 180 | 180 | 180 | false (lit on opening) |
+| POST 120, 12 s later (past the 5 s resend) | 120 | 120 | |
+
+vphoned stored each value quietly (`VPhoneHingeAngle` `quiet: true`, angle
+as posted) and `client_controls` counted one per POST. DeviceHub (restarted
+after the backboardd restart) still folds the guest from its own buttons:
+Closed 0, Open 180, Partially Open 130, with `client_controls` unchanged,
+since its values come through dtuhidd's virtual service, not a client
+dispatch. After DeviceHub's, WDA's POST 0 and 180 turned DeviceHub's model
+to the cover face and to the open inner panel. `display.pose` is the
+test-automation snapshot and is not current on a relay guest; its hinge
+field is not evidence here.
+
+With the server bypassed for these events, a POST leaves only the
+connection's `Connection added` and `Connection removed` lines in
+backboardd's log, with no entitlement error; POST 120, 0 and 180 and
+DeviceHub's Closed and Open were repeated with the same results. Checked
+off-guest in `make test-hinge`: WDA's payload in the internal form inside a
+binary plist is taken, and another usage page, no provider, the orientation
+picker and values of -1, 181, a Boolean or a string are not.
diff --git a/VPhoneGuestComponents/Hinge/VPhoneHingeClientDispatch.h b/VPhoneGuestComponents/Hinge/VPhoneHingeClientDispatch.h
new file mode 100644
index 00000000..ff9ccbf2
--- /dev/null
+++ b/VPhoneGuestComponents/Hinge/VPhoneHingeClientDispatch.h
@@ -0,0 +1,71 @@
+#pragma once
+#import "VPhoneHingeHID.h"
+#import "VPhoneHingeState.h"
+#include <mach/mach.h>
+#include <math.h>
+
+// MARK: - Hinge values a HID client dispatches
+//
+// WebDriverAgent's `POST /wda/device/hingeAngle` builds DeviceHub's slider
+// dictionary itself, wraps it in a host-control vendor event and sends it with
+// IOHIDEventSystemClientDispatchEvent. backboardd's HID server drops it:
+// `_io_hideventsystem_dispatch_event` needs the client's
+// com.apple.private.hid.client.event-dispatch entitlement, and a test runner
+// has none. Even dispatched, no service would carry it to locationd's relay,
+// which reads the host controls from their services. The hinge takes such a
+// value and dispatches it on its own host-control service, where it reaches
+// the relay and vphoned as a DeviceHub value does.
+
+// The server function `io_hideventsystem_dispatch_event` lands in, exported by
+// IOKit: the client's port, then the out-of-line buffer holding the event's
+// data as a CFData in a binary property list (IOKit's _IOHIDCreateBinaryData).
+// It frees the buffer as it reads it, and only then refuses an unentitled
+// client.
+typedef kern_return_t (*VPhoneHingeDispatchEventServer)(mach_port_t, vm_offset_t, mach_msg_type_number_t);
+
+// An event's data is a few hundred bytes; a host-control payload at most 256.
+#define VP_HINGE_CLIENT_EVENT_MAX_DATA 4096
+
+// IOKit's readers, resolved by the caller. The vendor payload is IOKit
+// binary-serialized, as DeviceHub's is. A client sends the event in
+// IOHIDEventCreateDataInternal's form and the server reads it with
+// IOHIDEventCreateWithDataInternal; the public pair passes another format
+// flag.
+typedef struct {
+ CFTypeRef (*unserializeBinary)(const char *, size_t, CFAllocatorRef, CFOptionFlags, CFStringRef *);
+ CFTypeRef (*createEventWithData)(CFAllocatorRef, CFDataRef);
+ uint32_t (*eventType)(CFTypeRef);
+ CFIndex (*eventInteger)(CFTypeRef, uint32_t);
+ uint8_t *(*eventData)(CFTypeRef, uint32_t);
+} VPhoneHingeEventReader;
+
+/// The client's event when it is DeviceHub's hinge slider: a host-control
+/// vendor event whose dictionary names the VM provider, the slider, and a
+/// value of 0–180 degrees. Anything else is nil and left to the server.
+static inline id VPhoneHingeClientSliderEvent(NSData *eventData, const VPhoneHingeEventReader *reader) {
+ id serialized = [NSPropertyListSerialization propertyListWithData:eventData options:0 format:NULL error:NULL];
+ if (![serialized isKindOfClass:NSData.class])
+ return nil;
+ id event = CFBridgingRelease(reader->createEventWithData(kCFAllocatorDefault, (__bridge CFDataRef)serialized));
+ // kIOHIDEventTypeVendorDefined; its fields are the type << 16 plus usage
+ // page 0, usage 1, version 2, data length 3, data 4.
+ static const uint32_t vendorDefined = 1, field = vendorDefined << 16;
+ CFTypeRef raw = (__bridge CFTypeRef)event;
+ if (!event || reader->eventType(raw) != vendorDefined ||
+ reader->eventInteger(raw, field) != VP_HOST_CONTROLS_USAGE_PAGE ||
+ reader->eventInteger(raw, field | 1) != VP_HOST_CONTROLS_USAGE)
+ return nil;
+ CFIndex length = reader->eventInteger(raw, field | 3);
+ uint8_t *data = reader->eventData(raw, field | 4);
+ if (!data || length <= 0 || length > VP_HOST_CONTROLS_MAX_DATA)
+ return nil;
+ NSDictionary *control = CFBridgingRelease(reader->unserializeBinary((const char *)data, (size_t)length,
+ kCFAllocatorDefault, 0, NULL));
+ if (![control isKindOfClass:NSDictionary.class] ||
+ ![control[@"provider"] isEqual:@"com.apple.Virtualization.VirtualMachines"] ||
+ ![control[@"source"] isEqual:@"hinge-slider-control"] || ![control[@"type"] isEqual:@"range"] ||
+ !VPhoneHingeNumber(control[@"value"]))
+ return nil;
+ double degrees = [control[@"value"] doubleValue];
+ return isfinite(degrees) && degrees >= 0 && degrees <= 180 ? event : nil;
+}
diff --git a/VPhoneGuestComponents/Hinge/libvphonehinge.m b/VPhoneGuestComponents/Hinge/libvphonehinge.m
index 0f692c3f..9e9fd485 100644
--- a/VPhoneGuestComponents/Hinge/libvphonehinge.m
+++ b/VPhoneGuestComponents/Hinge/libvphonehinge.m
@@ -1,9 +1,12 @@
+#import "VPhoneHingeClientDispatch.h"
#import "VPhoneHingeHID.h"
#import "VPhoneHingeState.h"
+#include "../MISFix/MISFixDetour.h"
#include <dlfcn.h>
#include <mach/mach_time.h>
#include <notify.h>
#include <objc/message.h>
+#include <sys/sysctl.h>
#include <unistd.h>
// MARK: - HID provider in backboardd
@@ -31,13 +34,72 @@
@property (nonatomic) uint64_t dispatched;
@property (nonatomic) uint64_t failures;
@property (nonatomic) uint64_t sweeps;
+@property (nonatomic) uint64_t clientControls;
+@property (nonatomic, copy) NSString *clientDispatch;
@property (nonatomic) int notificationToken;
@property (nonatomic, copy) NSString *lastError;
- (void)start;
- (void)dispatchSample;
- (void)dispatchAngle:(double)angle velocity:(double)velocity state:(int32_t)state phase:(int32_t)phase;
+- (void)takeClientEvent:(id)event;
@end
+// MARK: - Client-dispatched hinge values
+//
+// backboardd's HID server hands a client's `io_hideventsystem_dispatch_event`
+// to `_io_hideventsystem_dispatch_event`, which IOKit exports. The hinge
+// detours it. A hinge-slider event is the hinge's: it frees the buffer as the
+// server would and answers success, so the server neither refuses an
+// unentitled client nor logs it. Every other event goes to the server
+// unchanged. The table that names the routine is in the cache's
+// __AUTH_CONST, whose pages refuse a copy-on-write (KERN_PROTECTION_FAILURE);
+// the function's page takes one. The event is dispatched on the hinge's
+// queue, since dispatching on its service from the server's receive path
+// would wait on that path.
+
+static VPhoneHingeProvider *VPhoneHingeClientProvider;
+static VPhoneHingeDispatchEventServer VPhoneHingeDispatchEventOriginal;
+static VPhoneHingeEventReader VPhoneHingeReader;
+
+static kern_return_t VPhoneHingeDispatchEvent(mach_port_t server, vm_offset_t data, mach_msg_type_number_t length) {
+ VPhoneHingeProvider *provider = VPhoneHingeClientProvider;
+ if (provider && data && length && length <= VP_HINGE_CLIENT_EVENT_MAX_DATA) {
+ @autoreleasepool {
+ NSData *eventData = [NSData dataWithBytes:(const void *)data length:length];
+ id event = VPhoneHingeClientSliderEvent(eventData, &VPhoneHingeReader);
+ if (event) {
+ vm_deallocate(mach_task_self(), data, length);
+ dispatch_async(provider.queue, ^{ [provider takeClientEvent:event]; });
+ return KERN_SUCCESS;
+ }
+ }
+ }
+ return VPhoneHingeDispatchEventOriginal(server, data, length);
+}
+
+/// Installed from the constructor, before backboardd serves its clients: the
+/// detour rewrites the function's first four instructions, which a thread must
+/// not be running at the time.
+static NSString *VPhoneHingeAcceptClientDispatches(void) {
+ void *iokit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW | RTLD_LOCAL);
+ VPhoneHingeReader = (VPhoneHingeEventReader){
+ dlsym(iokit, "IOCFUnserializeBinary"), dlsym(iokit, "IOHIDEventCreateWithDataInternal"),
+ dlsym(iokit, "IOHIDEventGetType"), dlsym(iokit, "IOHIDEventGetIntegerValue"),
+ dlsym(iokit, "IOHIDEventGetDataValue"),
+ };
+ void *server = dlsym(iokit, "_io_hideventsystem_dispatch_event");
+ if (!VPhoneHingeReader.unserializeBinary || !VPhoneHingeReader.createEventWithData ||
+ !VPhoneHingeReader.eventType || !VPhoneHingeReader.eventInteger || !VPhoneHingeReader.eventData || !server)
+ return @"HID server dispatch function unavailable";
+ void *original = NULL;
+ MISFixDetourResult result = MISFixDetour("HID server client dispatch", server, (void *)VPhoneHingeDispatchEvent,
+ &original);
+ if (result != MISFixDetourOK)
+ return [NSString stringWithFormat:@"HID server dispatch not detoured: %s", MISFixDetourDescribe(result)];
+ VPhoneHingeDispatchEventOriginal = (VPhoneHingeDispatchEventServer)original;
+ return @"installed";
+}
+
@implementation VPhoneHingeProvider
- (void)publishStatus {
@@ -47,6 +109,7 @@
@"report_interval_us": @(self.interval), @"dispatched": @(self.dispatched),
@"dispatch_failures": @(self.failures), @"sweeps": @(self.sweeps),
@"sweeping": @(self.sweepTimer != nil), @"current_angle": @(self.currentAngle),
+ @"client_dispatch": self.clientDispatch ?: @"", @"client_controls": @(self.clientControls),
@"error": self.lastError ?: @"",
@"configuration": VPhoneHingeEncode(self.state),
};
@@ -257,6 +320,21 @@ static double VPhoneHingeProgress(double t, int32_t profile) {
[self dispatchControl:@{@"source": @"orientation-picker-control", @"type": @"enum", @"value": name}];
}
+- (void)takeClientEvent:(id)event {
+ // The client's own event, unchanged: vphoned follows a dictionary that
+ // names the VM provider, as it follows DeviceHub's, and the relay reads it
+ // from this service.
+ self.clientControls++;
+ if (self.enumerated && [self.service dispatchEvent:event]) {
+ self.dispatched++;
+ self.lastError = nil;
+ } else {
+ self.failures++;
+ self.lastError = @"Client hinge value dispatch failed";
+ }
+ [self publishStatus];
+}
+
- (void)createService {
self.enumerated = NO;
self.cancelling = NO;
@@ -399,5 +477,14 @@ __attribute__((constructor)) static void VPhoneHingeStart(void) {
static VPhoneHingeProvider *provider;
provider = [VPhoneHingeProvider new];
provider.queue = dispatch_queue_create("com.vphone.motion.hinge", DISPATCH_QUEUE_SERIAL);
+ // Only a foldable has a hinge to set; vphoned follows DeviceHub's controls
+ // on the same product.
+ char product[64] = {0};
+ size_t size = sizeof(product);
+ if (sysctlbyname("hw.machine", product, &size, NULL, 0) == 0 && strcmp(product, "iPhone19,4") == 0) {
+ VPhoneHingeClientProvider = provider;
+ provider.clientDispatch = VPhoneHingeAcceptClientDispatches();
+ NSLog(@"[vphone-hinge] client hinge values: %@", provider.clientDispatch);
+ }
dispatch_async(provider.queue, ^{ [provider start]; });
}
diff --git a/VPhoneGuestComponents/MISFix/MISFixDetour.c b/VPhoneGuestComponents/MISFix/MISFixDetour.c
index a1adc7e6..cb8937c1 100644
--- a/VPhoneGuestComponents/MISFix/MISFixDetour.c
+++ b/VPhoneGuestComponents/MISFix/MISFixDetour.c
@@ -129,6 +129,18 @@ static unsigned vpRelocate(uint32_t insn, uint64_t pc, uint32_t *out) {
return 0;
}
+ // An X1 prologue signs LR with its own address, and the function's
+ // `retaasppc`/`retabsppc <label>` checks it against the original's: moved,
+ // it signs for the trampoline and the return faults. The X1-lowered cache
+ // a guest without PAuth_LR runs has the SP-only forms here instead.
+ if (insn == 0xDAC1A3FEu // PACIASPPC
+ || insn == 0xDAC183FEu // PACNBIASPPC
+ || insn == 0xDAC1A7FEu // PACIBSPPC
+ || insn == 0xDAC187FEu) // PACNBIBSPPC
+ {
+ return 0;
+ }
+
out[0] = insn;
return 1;
}
diff --git a/VPhoneGuestComponents/Makefile b/VPhoneGuestComponents/Makefile
index cf44d367..9b6581af 100644
--- a/VPhoneGuestComponents/Makefile
+++ b/VPhoneGuestComponents/Makefile
@@ -97,8 +97,11 @@ ACCELEROMETER := $(STAGE)/accelerometer/libvphoneaccelerometer.dylib
ACCELEROMETER_SOURCES := $(call find_sources,Accelerometer)
ACCELEROMETER_INPUTS := $(call find_headers,Accelerometer) $(call find_folders,Accelerometer)
HINGE := $(STAGE)/hinge/libvphonehinge.dylib
-HINGE_SOURCES := $(call find_sources,Hinge)
-HINGE_INPUTS := $(call find_headers,Hinge) $(call find_folders,Hinge)
+# The hinge detours backboardd's HID server to take a hinge value a client
+# dispatches (WebDriverAgent), with libmisfix's detour.
+HINGE_SOURCES := $(call find_sources,Hinge) MISFix/MISFixDetour.c MISFix/MISFixConfig.c
+HINGE_INPUTS := $(call find_headers,Hinge) $(call find_folders,Hinge) MISFix/MISFixDetour.h \
+ MISFix/MISFixConditionalBranch.h MISFix/MISFixConfig.h
# The SpringBoard guard that lets the foldable product identity survive on a
# single-display guest. See FoldGuard/libvphonefoldguard.m.
FOLDGUARD := $(STAGE)/foldguard/libvphonefoldguard.dylib
diff --git a/VPhoneGuestComponents/README.md b/VPhoneGuestComponents/README.md
index 458935e1..44213c94 100644
--- a/VPhoneGuestComponents/README.md
+++ b/VPhoneGuestComponents/README.md
@@ -96,9 +96,14 @@ turns them into the hinge-angle and device-state services CoreMotion reads
0xFF0C/0x22), and SpringBoard and CoreDevice's hingeAngle stream read
CoreMotion. The relay only runs when the device tree carries the foldable
board's own product-id (`preboot-cfw-devicetree_foldable_product_id`), so on
-any other guest the service sits unread. Foldable guests only; `make
-test-hinge` checks the configuration transport and, on a Mac, the
-dictionaries' serialization round trip, size and vendor-defined event.
+any other guest the service sits unread. On an iPhone Duo it also detours
+backboardd's HID server (`_io_hideventsystem_dispatch_event`) to take the
+DeviceHub hinge-slider event a client without the event-dispatch
+entitlement sends (WebDriverAgent's `POST /wda/device/hingeAngle`) and
+dispatches it on that service. Foldable guests only; `make test-hinge`
+checks the configuration transport and, on a Mac, the dictionaries'
+serialization round trip, size and vendor-defined event, and which
+client-dispatched events the hinge takes.
`system-apps-cfw-attitude` installs `libvphoneattitude.dylib`, loaded by
SystemHook into apps. It supplies a stationary Core Motion pose through
diff --git a/VPhoneGuestComponents/Tests/HingeTests.m b/VPhoneGuestComponents/Tests/HingeTests.m
index ed57ab49..b1092371 100644
--- a/VPhoneGuestComponents/Tests/HingeTests.m
+++ b/VPhoneGuestComponents/Tests/HingeTests.m
@@ -4,8 +4,10 @@
// IOHIDEventCreateVendorDefinedEvent as the guest's does, so a host-control
// dictionary is checked to fit locationd's 256-byte limit, to read back the
// way locationd parses it, and to land in a vendor-defined event on the
-// host-control usage. Where a function is absent the event checks are
-// skipped; the transport checks always run.
+// host-control usage. A client's dispatched event is read back the way
+// backboardd's hinge takes it: DeviceHub's slider only. Where a function is
+// absent the event checks are skipped; the transport checks always run.
+#import "../Hinge/VPhoneHingeClientDispatch.h"
#import "../Hinge/VPhoneHingeHID.h"
#import "../Hinge/VPhoneHingeState.h"
#include <assert.h>
@@ -139,6 +141,51 @@ int main(void) {
[NSPredicate predicateWithFormat:@"length > 0"]] componentsJoinedByString:@" "];
assert([flat localizedCaseInsensitiveContainsString:@"VendorDefined"]);
}
+
+ // A client's dispatch, as IOHIDEventSystemClientDispatchEvent sends
+ // it: the event's data in a binary property list, the buffer the HID
+ // server function receives.
+ CFDataRef (*eventData)(CFAllocatorRef, CFTypeRef) = dlsym(iokit, "IOHIDEventCreateDataInternal");
+ VPhoneHingeEventReader reader = {
+ dlsym(iokit, "IOCFUnserializeBinary"), dlsym(iokit, "IOHIDEventCreateWithDataInternal"),
+ dlsym(iokit, "IOHIDEventGetType"), dlsym(iokit, "IOHIDEventGetIntegerValue"),
+ dlsym(iokit, "IOHIDEventGetDataValue"),
+ };
+ if (!eventData || !reader.unserializeBinary || !reader.createEventWithData || !reader.eventType ||
+ !reader.eventInteger || !reader.eventData) {
+ printf("IOKit event data functions unavailable here; client dispatch checks skipped\n");
+ printf("hinge tests passed\n");
+ return 0;
+ }
+ NSData * (^dispatched)(NSDictionary *, uint32_t) = ^NSData *(NSDictionary *control, uint32_t page) {
+ NSData *payload = CFBridgingRelease(serialize((__bridge CFTypeRef)control, 1));
+ id event = CFBridgingRelease(factory(NULL, mach_absolute_time(), page, VP_HOST_CONTROLS_USAGE, 0,
+ payload.bytes, (long)payload.length, 0));
+ NSData *data = CFBridgingRelease(eventData(NULL, (__bridge CFTypeRef)event));
+ NSData *wire = [NSPropertyListSerialization dataWithPropertyList:data
+ format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];
+ assert(wire.length <= VP_HINGE_CLIENT_EVENT_MAX_DATA);
+ return wire;
+ };
+ // WebDriverAgent's payload: DeviceHub's slider, value a double.
+ NSDictionary *slider = @{
+ @"provider": @"com.apple.Virtualization.VirtualMachines", @"source": @"hinge-slider-control",
+ @"type": @"range", @"value": @120.0,
+ };
+ assert(VPhoneHingeClientSliderEvent(dispatched(slider, VP_HOST_CONTROLS_USAGE_PAGE), &reader));
+ assert(!VPhoneHingeClientSliderEvent(dispatched(slider, 0xff00), &reader));
+ NSArray *refused = @[
+ @{@"source": @"hinge-slider-control", @"type": @"range", @"value": @120},
+ @{@"provider": @"com.apple.Virtualization.VirtualMachines", @"source": @"orientation-picker-control",
+ @"type": @"enum", @"value": @"portrait"},
+ ];
+ for (NSDictionary *control in refused)
+ assert(!VPhoneHingeClientSliderEvent(dispatched(control, VP_HOST_CONTROLS_USAGE_PAGE), &reader));
+ for (id value in @[@-1, @181, @YES, @"120"]) {
+ NSMutableDictionary *bad = [slider mutableCopy];
+ bad[@"value"] = value;
+ assert(!VPhoneHingeClientSliderEvent(dispatched(bad, VP_HOST_CONTROLS_USAGE_PAGE), &reader));
+ }
printf("hinge tests passed\n");
return 0;
}