Android के लिए बॉर्डर राऊटर डिवाइस गाइड

Android ऐप्लिकेशन के डेवलपर, Thread Border Router (TBR) को मैनेज करने के लिए, Home APIs का इस्तेमाल कर सकते हैं.

GoogleBorderRouterDevice को डिवाइस की दो मुख्य विशेषताओं का इस्तेमाल करके लागू किया जाता है: ThreadNetworkCapabilities, इससे border router की सुविधाओं की जांच करने के लिए, सिर्फ़ पढ़ने वाले एट्रिब्यूट मिलते हैं. दूसरी विशेषता है ThreadNetworkManagement, यह नेटवर्क लाइफ़साइकल के कमांड और कमिश्नर के लिए, Ephemeral Pre-Shared Key (ePSKc) का इस्तेमाल करके क्रेडेंशियल शेयर करने की सुविधा को मैनेज करती है. स्ट्रक्चर-लेवल पर इंटरनेट ऐक्सेस की नीतियों को ThreadNetworkSettings विशेषता का इस्तेमाल करके मैनेज किया जाता है.

किसी भी सुविधा का इस्तेमाल करने या एट्रिब्यूट अपडेट करने की कोशिश करने से पहले, हमेशा यह देखें कि डिवाइस पर एट्रिब्यूट और कमांड काम करते हैं या नहीं. ज़्यादा जानकारी के लिए, पर जाकर डिवाइसों को कंट्रोल करना Android लेख पढ़ें.

Home APIs डिवाइस का टाइप विशेषताएं Kotlin सैंपल ऐप्लिकेशन इस्तेमाल का उदाहरण

बॉर्डर राऊटर

GoogleBorderRouterDevice

home.matter.6006.types.0161

ज़रूरी विशेषताएं
     google ThreadNetworkCapabilities
     google ThreadNetworkManagement

बॉर्डर राऊटर

किसी डिवाइस के बारे में बुनियादी जानकारी पाना

   Android के लिए सैंपल ऐप्लिकेशन में लागू की गई सुविधा   

The BasicInformation विशेषता में, किसी डिवाइस के लिए वेंडर का नाम, वेंडर आईडी, प्रॉडक्ट आईडी, प्रॉडक्ट का नाम (इसमें मॉडल की जानकारी शामिल होती है) और सॉफ़्टवेयर वर्शन जैसी जानकारी शामिल होती है:

// Get device basic information. All general information traits are on the RootNodeDevice type.
    device.type(RootNodeDevice).first().standardTraits.basicInformation?.let { basicInformation ->
        println("vendorName ${basicInformation.vendorName}")
        println("vendorId ${basicInformation.vendorId}")
        println("productId ${basicInformation.productId}")
        println("productName ${basicInformation.productName}")
        println("softwareVersion ${basicInformation.softwareVersion}")
    }

बॉर्डर राऊटर की क्षमताओं की जांच करना

border router की सिर्फ़ पढ़ने वाली क्षमताओं की जांच की जा सकती है. जैसे, ePSKc के साथ काम करने की सुविधा और इंटरनेट ऐक्सेस की सेटिंग का कॉन्फ़िगरेशन. ThreadNetworkCapabilities का इस्तेमाल करके.

suspend fun checkBorderRouterCapabilities(device: HomeDevice) {
    // Filter for GoogleBorderRouterDevice device type
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    if (gtbrDevice == null) {
        println("Device is not a Google border router.")
        return
    }

    // Retrieve the ThreadNetworkCapabilities trait
    val capabilitiesTrait = gtbrDevice.trait(ThreadNetworkCapabilities)
    if (capabilitiesTrait == null) {
        println("ThreadNetworkCapabilities trait not found on device.")
        return
    }

    val isEpskcSupported = capabilitiesTrait.epskcSupported ?: false
    val internetAccessOption = capabilitiesTrait.internetAccessOption?.name ?: "None"
    val isIasSupported = !internetAccessOption.equals("None", ignoreCase = true)

    println("ePSKc Supported: $isEpskcSupported")
    println("Internet Access Setting Supported: $isIasSupported")
}

Thread के क्रेडेंशियल शेयर करने की सुविधा (ePSKc) को मैनेज करना

Thread के क्रेडेंशियल शेयर करने की सुविधा, कमिश्नर के लिए Ephemeral Pre-Shared Key (ePSKc) का इस्तेमाल करके हासिल की जाती है. ePSKc मोड, एक अस्थायी और सुरक्षित पासकी जनरेट करता है. इसका इस्तेमाल, बाहरी डिवाइस या कमिश्नर, Thread नेटवर्क का डेटासेट सुरक्षित तरीके से पाने के लिए कर सकते हैं.

ePSKc मोड चालू करना

suspend fun startEpskcSession(device: HomeDevice, durationSeconds: Short): ActivateEpskcModeCommand.Response? {
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)

    if (mgmtTrait == null) {
        println("ThreadNetworkManagement trait not found.")
        return null
    }

    return try {
        val response = mgmtTrait.activateEpskcMode(
            optionalArgs = { this.requestedDurationSeconds = durationSeconds }
        )

        println("ePSKc Session Activated!")
        println("Status: ${response.status}")
        println("Ephemeral PSKc: ${response.epskc}")
        println("Valid Duration (s): ${response.validDurationSeconds}")

        response
    } catch (e: Exception) {
        println("Failed to activate ePSKc mode: ${e.message}")
        null
    }
}

ePSKc मोड बंद करना

suspend fun stopEpskcSession(device: HomeDevice) {
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)

    mgmtTrait?.deactivateEpskcMode()
    println("ePSKc mode deactivated.")
}

ePSKc को बंद करने वाले इवेंट देखना

TBRs, ePSKc सेशन खत्म होने पर इवेंट एमिट करते हैं. जैसे, कुंजी का इस्तेमाल किया गया, सेशन की समयसीमा खत्म हो गई या इसे मैन्युअल तरीके से रद्द कर दिया गया.

suspend fun observeEpskcEvents(device: HomeDevice) {
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)

    mgmtTrait?.epskcModeDeactivatedEventFlow()?.collect { event ->
        println("ePSKc Session Ended. Reason: ${event.reason}")
        when (event.reason?.name) {
            "KeyUsed" -> println("Key was successfully used to commission a device.")
            "Expired" -> println("Session timed out before the key was used.")
            "Cancelled" -> println("Session was manually cancelled.")
        }
    }
}

Thread नेटवर्क की सदस्यता को मैनेज करना

किसी TBR को, चालू Operational Dataset TLV उपलब्ध कराकर, नए Thread नेटवर्क में शामिल होने का निर्देश दिया जा सकता है. इसके अलावा, उसे अपने मौजूदा नेटवर्क से बाहर निकलने का निर्देश भी दिया जा सकता है.

suspend fun joinNetwork(device: HomeDevice, datasetTlvs: ByteArray) {
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)

    try {
        val response = mgmtTrait?.joinNetwork(operationalDatasetTlvs = datasetTlvs)
        println("Join network command sent. Status: ${response?.status}")
    } catch (e: Exception) {
        println("Join network failed: ${e.message}")
    }
}

suspend fun leaveNetwork(device: HomeDevice) {
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)

    try {
        mgmtTrait?.leaveNetwork()
        println("Leave network command sent successfully.")
    } catch (e: Exception) {
        println("Leave network failed: ${e.message}")
    }
}

स्ट्रक्चर-लेवल पर इंटरनेट ऐक्सेस को कॉन्फ़िगर करना

ThreadNetworkSettings विशेषता, अपडेट की जा सकने वाली एक विशेषता है. इसे Structure (घर या बिल्डिंग के लिए) से जोड़ा जाता है. इससे डेवलपर, TBR के लिए स्ट्रक्चर-वाइड इंटरनेट ऐक्सेस की नीति को कॉन्फ़िगर कर सकते हैं.

suspend fun updateStructureInternetAccess(structure: Structure, enableInternetAccess: Boolean) {
    // Retrieve the ThreadNetworkSettings trait for the structure
    val settingsTrait = structure.trait(ThreadNetworkSettings).firstOrNull()
    if (settingsTrait == null) {
        println("ThreadNetworkSettings trait not found on structure.")
        return
    }

    val option = if (enableInternetAccess) {
        InternetAccessOption.InternetAccessOptionAll
    } else {
        InternetAccessOption.InternetAccessOptionNone
    }

    try {
        settingsTrait.update {
            setInternetAccessOption(option)
        }
        println("Successfully updated Thread internet access policy to: ${option.name}")
    } catch (e: Exception) {
        println("Failed to update Thread internet access policy: ${e.message}")
    }
}