คู่มืออุปกรณ์ Border Router สำหรับ iOS

นักพัฒนาแอป iOS สามารถใช้ Home API เพื่อจัดการ Thread Border Router (TBR) ได้

GoogleBorderRouterDevice จะใช้ลักษณะอุปกรณ์หลัก 2 อย่าง ได้แก่ ThreadNetworkCapabilitiesTrait ซึ่งมีแอตทริบิวต์แบบอ่านอย่างเดียวเพื่อตรวจสอบฟีเจอร์ border router และ ThreadNetworkManagementTrait ซึ่งจะจัดการคำสั่งวงจรเครือข่ายและการแชร์ข้อมูลเข้าสู่ระบบ โดยใช้คีย์ที่แชร์ล่วงหน้าชั่วคราวสำหรับคอมมิชชันเนอร์ (ePSKc) นโยบายการเข้าถึงอินเทอร์เน็ตระดับบ้านจะได้รับการจัดการโดยใช้ลักษณะ ThreadNetworkSettingsTrait

ตรวจสอบการรองรับแอตทริบิวต์และคำสั่งสำหรับอุปกรณ์ก่อนใช้ฟีเจอร์หรือพยายามอัปเดตแอตทริบิวต์เสมอ ดูข้อมูลเพิ่มเติมได้ที่ควบคุมอุปกรณ์ในiOS

ประเภทอุปกรณ์ของ Home API ลักษณะเฉพาะ แอปตัวอย่าง Swift กรณีการใช้งาน

Border Router

GoogleBorderRouterDeviceType

home.matter.6006.types.0161

ลักษณะที่จำเป็น
     google ThreadNetworkCapabilitiesTrait
     google ThreadNetworkManagementTrait

Border Router

ดูข้อมูลพื้นฐานเกี่ยวกับอุปกรณ์

   ติดตั้งใช้งานในแอปตัวอย่างสำหรับ iOS   

ลักษณะ BasicInformation ประกอบด้วยข้อมูลต่างๆ เช่น ชื่อผู้ให้บริการ รหัสผู้ให้บริการ รหัสสินค้า ชื่อสินค้า (รวมถึงข้อมูลรุ่น) และเวอร์ชันของซอฟต์แวร์สำหรับอุปกรณ์ ดังนี้

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!

ตรวจสอบความสามารถของ Border Router

คุณตรวจสอบความสามารถแบบอ่านอย่างเดียวของ border router (เช่น การรองรับ ePSKc และการกำหนดค่าการตั้งค่าการเข้าถึงอินเทอร์เน็ต) ได้โดยใช้ลักษณะ ThreadNetworkCapabilitiesTrait

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)")
    }
}

จัดการการแชร์ข้อมูลเข้าสู่ระบบของ Thread (ePSKc)

การแชร์ข้อมูลเข้าสู่ระบบ Thread ทำได้โดยใช้คีย์ที่แชร์ล่วงหน้าแบบชั่วคราวสำหรับคอมมิชชันเนอร์ (ePSKc) โหมด ePSKc จะสร้างพาสคีย์ชั่วคราวที่ปลอดภัยซึ่งอุปกรณ์ภายนอกหรือคอมมิชชันเนอร์สามารถใช้เพื่อรับชุดข้อมูลเครือข่าย Thread ได้อย่างปลอดภัย

เปิดใช้งานโหมด ePSKc

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
    }
}

ปิดใช้งานโหมด ePSKc

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)")
    }
}

สังเกตเหตุการณ์การปิดใช้งาน ePSKc

TBR จะส่งเหตุการณ์เมื่อเซสชัน ePSKc สิ้นสุดลง (เช่น เนื่องจากมีการใช้คีย์ เซสชันหมดอายุ หรือมีการยกเลิกด้วยตนเอง)

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)")
    }
}

จัดการการเป็นสมาชิกเครือข่าย Thread

คุณสามารถสั่งให้ TBR เข้าร่วมเครือข่าย Thread ใหม่โดยระบุ TLV ของชุดข้อมูลการทำงานที่ใช้งานอยู่ หรือสั่งให้ออกจากเครือข่ายปัจจุบัน

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)")
    }
}

กำหนดค่าการเข้าถึงอินเทอร์เน็ตระดับบ้าน

ลักษณะ ThreadNetworkSettings เป็นลักษณะที่อัปเดตได้ซึ่งแนบมากับ Structure (แสดงถึงบ้านหรืออาคาร) ซึ่งช่วยให้นักพัฒนาซอฟต์แวร์กำหนดค่านโยบายการเข้าถึงอินเทอร์เน็ตทั่วทั้งบ้านสำหรับ TBR ได้

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)")
    }
}