Communicating between a DriverKit extension and a client app
At a glance
| Item | Summary |
|---|---|
| Purpose | Send and receive different kinds of data securely by validating inputs and asynchronously by storing and using a callback. |
| App architecture | A C, C++, C/Objective-C header, Swift sample with the source-visible chain DriverKitSampleApp → DriverKitSampleView → DriverCommunicationViewModel → DriverKit APIs. |
| Main patterns | Model-View-ViewModel, Delegate or data-source callbacks, Binding-based state propagation, Publisher-backed observable state |
| Project style | 12 scanned source file(s) across C, C++, C/Objective-C header, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: DispatchQueue.main.async; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, ObservableObject, @Published. |
| Key frameworks/packages | DriverKit, SwiftUI, os, Foundation, IOKit; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── DriverKitUserClientSample/
├── DriverKitSampleApp/
│ ├── DriverKitSampleApp.swift
│ ├── DriverCommunicationViewModel.swift
│ ├── DriverCommunicationView.swift
│ ├── DriverKitSampleView.swift
│ ├── DriverLoadingViewModel+macOS.swift
│ ├── DriverLoadingView+iOS.swift
│ ├── DriverLoadingView+macOS.swift
│ ├── DriverKitSampleApp-Bridging-Header.h
│ ├── UserClientCommunication.c
│ └── UserClientCommunication.h
└── NullDriver/
├── NullDriverUserClient.cpp
└── NullDriver.cpp
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: C, C++, C/Objective-C header, Swift.
- The verified tree contains 7 project/configuration file(s) and 16 source declaration(s).
Overall architecture
flowchart LR
N1["DriverKitSampleApp"]
N2["DriverKitSampleView"]
N3["DriverCommunicationViewModel"]
N4["DriverKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleApp.swift:10 — architecture anchor
@main
struct DriverKitSampleApp: App {
var body: some Scene {
WindowGroup {
DriverKitSampleView()
}
}
}Interpretation
The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into DriverKit.
Ownership and state
classDiagram
DriverCommunicationViewModel *-- Bool : isConnected
DriverCommunicationViewModel o-- io_connect_t : connection
DriverCommunicationViewModel *-- UnsafeMutableRawPointer : opaqueSelf
DriverCommunicationViewModel o-- State : state
Ownership evidence
DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift:66 — stored dependency or nearest verified ownership anchor
class DriverCommunicationViewModel: NSObject, ObservableObject {
// ...
@Published public var isConnected: Bool = false
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
DriverCommunicationViewModel |
Bool (isConnected) |
owns value state | App/module collaborators |
DriverCommunicationViewModel |
io_connect_t (connection) |
stores or receives | App/module collaborators |
DriverCommunicationViewModel |
UnsafeMutableRawPointer (opaqueSelf) |
creates and retains | App/module collaborators |
DriverCommunicationViewModel |
State (state) |
stores or receives | Owning lexical scope |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Queue scheduling | DispatchQueue.main.async |
The source addresses the main dispatch queue. | DriverKitUserClientSample/DriverKitSampleApp/DriverLoadingViewModel+macOS.swift:226 |
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
Reference code
DriverKitUserClientSample/DriverKitSampleApp/DriverLoadingViewModel+macOS.swift:226 — representative execution boundary
#if swift(>=5.5)
DispatchQueue.main.async {
self.state = DriverLoadingStateMachine.process(self.state, self.installedDextProperties != nil ? .discoveredLoaded : .discoveredUnloaded)
}
#endifState propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:12 |
| State propagation | ObservableObject |
ObservableObject supplies an observation contract. | DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift:64 |
| State propagation | @Published |
A published property can emit owner-controlled changes. | DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift:66 |
| Source import | DriverKit |
The cited file imports this module; runtime use and architectural role are not inferred. | DriverKitUserClientSample/NullDriver/NullDriver.cpp:10 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:8 |
| Source import | os |
The cited file imports this module; runtime use and architectural role are not inferred. | DriverKitUserClientSample/DriverKitSampleApp/DriverLoadingViewModel+macOS.swift:10 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift:8 |
| Source import | IOKit |
The cited file imports this module; runtime use and architectural role are not inferred. | DriverKitUserClientSample/DriverKitSampleApp/UserClientCommunication.c:12 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleApp.swift:11 — representative type boundary
@main
struct DriverKitSampleApp: App {
// ...
DriverKitSampleView()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
DriverKitSampleApp |
Application entry and top-level composition | App |
DriverCommunicationViewModel |
UI-facing state and feature coordination | NSObject, ObservableObject |
DriverCommunicationView |
User-interface presentation and input forwarding | View |
DriverKitSampleView |
User-interface presentation and input forwarding | View |
DisplayedView |
User-interface presentation and input forwarding | Concrete collaborators/imported frameworks |
DriverLoadingViewModel |
UI-facing state and feature coordination | NSObject |
DriverLoadingView |
User-interface presentation and input forwarding | View |
DriverLoadingView |
User-interface presentation and input forwarding | View |
DriverCommunicationStateMachine |
Owns feature behavior and collaborator lifecycle | Concrete collaborators/imported frameworks |
State |
Represents mutable feature state | Concrete collaborators/imported frameworks |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
viewModel (DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:13) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
headerView (DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:35) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
uncheckedView (DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:55) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
checkedView (DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:87) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
Reference code
DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:13 — representative boundary
@Binding var displayedView: DriverKitSampleView.DisplayedView
@ObservedObject public var viewModel: DriverCommunicationViewModel = .init()
var body: some View {
// ...
}Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application entry and top-level composition | DriverKitSampleApp |
The source’s App suffix makes this role explicit. |
| User-interface presentation and input forwarding | DisplayedView, DriverCommunicationView, DriverKitSampleView, DriverLoadingView |
The source’s View suffix makes this role explicit. |
| UI-facing state and feature coordination | DriverCommunicationViewModel, DriverLoadingViewModel |
The source’s ViewModel suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Model-View-ViewModel | DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift:64 |
Role-named view models keep UI-facing state or coordination outside view declarations. |
| Delegate or data-source callbacks | DriverKitUserClientSample/DriverKitSampleApp/DriverLoadingViewModel+macOS.swift:158 |
Callback protocols invert event delivery back into the sample’s owner. |
| Binding-based state propagation | DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:12 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
| Publisher-backed observable state | DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift:66 |
Published properties notify observers while mutation remains with the state object. |
Naming conventions
- Types: App: DriverKitSampleApp; View: DisplayedView, DriverCommunicationView, DriverKitSampleView, DriverLoadingView; ViewModel: DriverCommunicationViewModel, DriverLoadingViewModel.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
SwiftAsyncCallback,SwiftDeviceAdded,SwiftDeviceRemoved,process,LocalAsyncCallback,ProcessResult,SwiftUncheckedScalar,SwiftUncheckedStruct. - Files:
DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleApp.swift,DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift,DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift,DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleView.swift.
Architecture takeaways
DriverKitSampleAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches DriverKit, SwiftUI, IOKit, CoreFoundation through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleApp.swift |
Cited implementation, DriverKitSampleApp |
DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift |
Cited implementation, DriverCommunicationViewModel, ObservableObject, @Published, Foundation, DriverCommunicationStateMachine, State, Event |
DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift |
Cited implementation, SwiftUI state property wrapper, SwiftUI, DriverCommunicationView, DriverCommunicationView_Previews |
DriverKitUserClientSample/DriverKitSampleApp/DriverLoadingViewModel+macOS.swift |
Cited implementation, DispatchQueue.main.async, os, DriverLoadingStateMachine, State, Event, DriverLoadingViewModel |
DriverKitUserClientSample/NullDriver/NullDriver.cpp |
DriverKit, NullDriver_IVars |
DriverKitUserClientSample/DriverKitSampleApp/UserClientCommunication.c |
IOKit, Feature implementation |
DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleView.swift |
DriverKitSampleView, DisplayedView |
DriverKitUserClientSample/NullDriver/NullDriverUserClient.cpp |
NullDriverUserClient_IVars |
DriverKitUserClientSample/DriverKitSampleApp/DriverLoadingView+iOS.swift |
DriverLoadingView, DriverLoadingView_Previews |
DriverKitUserClientSample/DriverKitSampleApp/DriverLoadingView+macOS.swift |
DriverLoadingView, DriverLoadingView_Previews |
DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleApp-Bridging-Header.h |
Feature implementation |
DriverKitUserClientSample/DriverKitSampleApp/UserClientCommunication.h |
Feature implementation |