iOS app developers can use the Home APIs to manage a Thread Border Router (TBR).

The `GoogleBorderRouterDevice` is implemented using two primary device traits:
[`ThreadNetworkCapabilitiesTrait`](https://developers.home.google.com/reference/swift/GoogleHomeTypes/Enums/Google/ThreadNetworkCapabilitiesTrait.md.txt),
which provides read-only attributes to inspect border router features,
and [`ThreadNetworkManagementTrait`](https://developers.home.google.com/reference/swift/GoogleHomeTypes/Enums/Google/ThreadNetworkManagementTrait.md.txt),
which handles network lifecycle commands and credential sharing
using an Ephemeral Pre-Shared Key for the Commissioner (ePSKc). Structure-level
internet access policies are managed using the
[`ThreadNetworkSettingsTrait`](https://developers.home.google.com/reference/swift/GoogleHomeTypes/Enums/Google/ThreadNetworkSettingsTrait.md.txt)
trait.

Always check for attribute and command support for a device prior to using any
features or attempting to update attributes. See [Control devices on
iOS](https://developers.home.google.com/apis/ios/device/control) for more
information.

<br />


| Home APIs Device Type | Traits | Swift Sample App | Use Case |
|---|---|:---:|:---:|
| Border Router `GoogleBorderRouterDeviceType` `home.matter.6006.types.0161` | Required Traits google [ThreadNetworkCapabilitiesTrait](https://developers.home.google.com/reference/swift/GoogleHomeTypes/Enums/Google/ThreadNetworkCapabilitiesTrait.md.txt) google [ThreadNetworkManagementTrait](https://developers.home.google.com/reference/swift/GoogleHomeTypes/Enums/Google/ThreadNetworkManagementTrait.md.txt) | Yes | [Border Router](https://developers.home.google.com/use-cases?device=border-router) |

<br />

> [!NOTE]
> **Note:** The Automation API doesn't support these traits.

## Get basic information about a device

[Implemented in Sample App for iOS](https://developers.home.google.com/apis/ios/sample-app/build.md.txt)

The [`BasicInformation`](https://developers.home.google.com/reference/swift/GoogleHomeTypes/Enums/Matter/BasicInformationTrait.md.txt)
trait includes information like vendor name, vendor ID, product ID,
product name (includes model information), and software version for a device:

```swift
let vendorName = basicInfoTrait.attributes.vendorName!
let vendorID = basicInfoTrait.attributes.vendorID!
let productID = basicInfoTrait.attributes.productID!
let productName = basicInfoTrait.attributes.productName!
let softwareVersion = basicInfoTrait.attributes.softwareVersion!
```

## Inspect border router capabilities

You can examine a border router's read-only capabilities (such as
ePSKc support and Internet Access setting configuration) using the
`ThreadNetworkCapabilitiesTrait` trait.

```swift
func checkBorderRouterCapabilities(device: HomeDevice) async {
    // Filter for GoogleBorderRouterDevice device type
    guard let gtbrDevice = device.type(Google.GoogleBorderRouterDevice.self) else {
        print("Device is not a Google border router.")
        return
    }

    // Retrieve the ThreadNetworkCapabilitiesTrait
    guard let capabilitiesTrait = gtbrDevice.traits(Google.ThreadNetworkCapabilitiesTrait.self) else {
        print("ThreadNetworkCapabilitiesTrait not found on device.")
        return
    }

    do {
        let isEpskcSupported = try await capabilitiesTrait.epskcSupported.read()
        let internetAccessOption = try await capabilitiesTrait.internetAccessOption.read()
        let isIasSupported = internetAccessOption != .none

        print("ePSKc Supported: \(isEpskcSupported)")
        print("Internet Access Setting Supported: \(isIasSupported)")
    } catch {
        print("Failed to read capabilities: \(error)")
    }
}
```

## Manage Thread credential sharing (ePSKc)

Thread credential sharing is achieved using an Ephemeral Pre-Shared Key for the
Commissioner (ePSKc). ePSKc mode generates a temporary, secure passkey that
external devices or commissioners can use to securely obtain the Thread network
dataset.

### Activate ePSKc mode

```swift
func startEpskcSession(device: HomeDevice, durationSeconds: Int16) async -> Google.ThreadNetworkManagementTrait.ActivateEpskcModeResponse? {
    guard let gtbrDevice = device.type(Google.GoogleBorderRouterDevice.self),
          let mgmtTrait = gtbrDevice.traits(Google.ThreadNetworkManagementTrait.self) else {
        print("ThreadNetworkManagementTrait not found.")
        return nil
    }

    do {
        var request = Google.ThreadNetworkManagementTrait.ActivateEpskcModeRequest()
        request.requestedDurationSeconds = durationSeconds

        let response = try await mgmtTrait.activateEpskcMode(request)

        print("ePSKc Session Activated!")
        print("Status: \(response.status)")
        print("Ephemeral PSKc: \(response.epskc)")
        print("Valid Duration (s): \(response.validDurationSeconds)")

        return response
    } catch {
        print("Failed to activate ePSKc mode: \(error)")
        return nil
    }
}
```

### Deactivate ePSKc mode

```swift
func stopEpskcSession(device: HomeDevice) async {
    guard let gtbrDevice = device.type(Google.GoogleBorderRouterDevice.self),
          let mgmtTrait = gtbrDevice.traits(Google.ThreadNetworkManagementTrait.self) else {
        return
    }

    do {
        try await mgmtTrait.deactivateEpskcMode(Google.ThreadNetworkManagementTrait.DeactivateEpskcModeRequest())
        print("ePSKc mode deactivated.")
    } catch {
        print("Failed to deactivate ePSKc mode: \(error)")
    }
}
```

### Observe ePSKc deactivation events

TBRs emit events when an ePSKc session ends (for example,
because the key was used, the session expired, or it was manually cancelled).

```swift
func observeEpskcEvents(device: HomeDevice) async {
    guard let gtbrDevice = device.type(Google.GoogleBorderRouterDevice.self),
          let mgmtTrait = gtbrDevice.traits(Google.ThreadNetworkManagementTrait.self) else {
        return
    }

    do {
        for try await event in mgmtTrait.epskcModeDeactivatedEvent.stream() {
            print("ePSKc Session Ended. Reason: \(event.reason)")
            switch event.reason {
            case .keyUsed:
                print("Key was successfully used to commission a device.")
            case .expired:
                print("Session timed out before the key was used.")
            case .cancelled:
                print("Session was manually cancelled.")
            @unknown default:
                print("Unknown deactivation reason.")
            }
        }
    } catch {
        print("Error streaming ePSKc events: \(error)")
    }
}
```

## Manage Thread network membership

You can command a TBR to join a new Thread network by
providing the active Operational Dataset TLVs, or command it to leave its
current network.

```swift
func joinNetwork(device: HomeDevice, datasetTlvs: Data) async {
    guard let gtbrDevice = device.type(Google.GoogleBorderRouterDevice.self),
          let mgmtTrait = gtbrDevice.traits(Google.ThreadNetworkManagementTrait.self) else {
        return
    }

    do {
        var request = Google.ThreadNetworkManagementTrait.JoinNetworkRequest()
        request.operationalDatasetTlvs = datasetTlvs

        let response = try await mgmtTrait.joinNetwork(request)
        print("Join network command sent. Status: \(response.status)")
    } catch {
        print("Join network failed: \(error)")
    }
}

func leaveNetwork(device: HomeDevice) async {
    guard let gtbrDevice = device.type(Google.GoogleBorderRouterDevice.self),
          let mgmtTrait = gtbrDevice.traits(Google.ThreadNetworkManagementTrait.self) else {
        return
    }

    do {
        try await mgmtTrait.leaveNetwork(Google.ThreadNetworkManagementTrait.LeaveNetworkRequest())
        print("Leave network command sent successfully.")
    } catch {
        print("Leave network failed: \(error)")
    }
}
```

## Configure structure-level internet access

The `ThreadNetworkSettings` trait is an updatable trait attached to a
`Structure` (representing a home or building). It allows developers to configure
the structure-wide internet access policy for TBRs.

```swift
func updateStructureInternetAccess(structure: Structure, enableInternetAccess: Bool) async {
    guard let settingsTrait = structure.traits(Google.ThreadNetworkSettingsTrait.self) else {
        print("ThreadNetworkSettingsTrait not found on structure.")
        return
    }

    let option: Google.ThreadNetworkSettingsTrait.InternetAccessOption = enableInternetAccess ? .internetAccessOptionAll : .internetAccessOptionNone

    do {
        try await settingsTrait.update { mutator in
            mutator.internetAccessOption = option
        }
        print("Successfully updated Thread internet access policy.")
    } catch {
        print("Failed to update Thread internet access policy: \(error)")
    }
}
```