Skip to content

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PeerTalkSwift

A Swift implementation of peertalk - USB communication between iOS and macOS.

Overview

PeerTalkSwift enables direct communication between iOS devices and macOS over USB through the usbmuxd daemon. It provides a modern Swift API with full async/await support and Swift 6 concurrency.

Requirements

  • iOS 15.0+ / macOS 12.0+
  • Swift 6.0+
  • Xcode 16.0+

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/user/PeerTalkSwift.git", from: "1.0.0")
]

Tuist

// Project.swift
dependencies: [
    .external(name: "PeerTalkSwift")
]

Usage

Listening for Devices (macOS)

import PeerTalkSwift

// Listen for device attach/detach events
for try await event in USBHub.shared.startListening() {
    switch event {
    case .deviceAttached(let device):
        print("Device connected: \(device.serialNumber)")
    case .deviceDetached(let deviceID):
        print("Device disconnected: \(deviceID)")
    }
}

Connecting to a Device (macOS)

import PeerTalkSwift

class MyDelegate: ChannelDelegate {
    func channel(_ channel: Channel, didReceiveFrame frame: Frame) {
        print("Received frame type: \(frame.type)")
        if let payload = frame.payload {
            print("Payload: \(payload)")
        }
    }

    func channel(_ channel: Channel, didEndWithError error: Error?) {
        print("Channel closed: \(error?.localizedDescription ?? "no error")")
    }
}

let channel = Channel()
channel.delegate = MyDelegate()

// Connect to device on port 2345
try await channel.connect(toDevice: device, port: 2345)

// Send data
let data = "Hello from Mac!".data(using: .utf8)!
try await channel.send(type: 100, payload: data)

Listening for Connections (iOS)

import PeerTalkSwift

class ServerDelegate: ChannelServerDelegate {
    func server(_ server: ChannelServer, didAcceptChannel channel: Channel) {
        print("New connection accepted")
        channel.delegate = self
    }
}

let server = ChannelServer()
server.delegate = ServerDelegate()
try server.listen(onPort: 2345)

Architecture

PeerTalkSwift/
├── USBMux/           # usbmuxd protocol layer
│   ├── USBMuxPacket      # Packet encoding/decoding (plist)
│   └── USBMuxConnection  # Unix socket communication
├── USBHub/           # Device management
│   ├── USBDevice         # Device model
│   └── USBHub            # Device listening (actor)
├── Protocol/         # Frame protocol
│   ├── Frame             # Frame structure
│   └── FrameProtocol     # Frame encoding/decoding
└── Channel/          # High-level API
    ├── Channel           # Client channel
    └── ChannelServer     # Server listener

Protocol Layers

  1. USBMux Layer: Communicates with macOS usbmuxd daemon via Unix socket at /var/run/usbmuxd. Uses plist-encoded packets.

  2. Frame Layer: Application-level protocol with 16-byte header:

    | version (4) | type (4) | tag (4) | payload_size (4) | payload (N) |
    
  3. Channel Layer: High-level abstraction with delegate pattern for sending/receiving frames.

API Reference

USBHub

public actor USBHub {
    static let shared: USBHub
    var connectedDevices: [USBDevice]

    func startListening() -> AsyncThrowingStream<USBHubEvent, Error>
    func stopListening()
    func connect(to device: USBDevice, port: UInt16) async throws -> Int32
}

Channel

public final class Channel {
    var delegate: ChannelDelegate?
    var isConnected: Bool
    var userInfo: Any?

    func connect(toDevice: USBDevice, port: UInt16) async throws
    func send(type: UInt32, tag: UInt32, payload: Data?) async throws
    func sendWithNewTag(type: UInt32, payload: Data?) async throws -> UInt32
    func close()
    func cancel()
}

Frame

public struct Frame: Sendable {
    static let noTag: UInt32 = 0
    static let endOfStreamType: UInt32 = 0

    let type: UInt32
    let tag: UInt32
    let payload: Data?
}

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages