feat(wifi): introduce BLE-based WiFi provisioning for nymea-compatible devices (#4968)
Some checks are pending
Dependency Submission / dependency-submission (push) Waiting to run
Main CI (Verify & Build) / validate-and-build (push) Waiting to run
Main Push Changelog / Generate main push changelog (push) Waiting to run

This commit is contained in:
James Rich 2026-04-02 12:31:17 -05:00 committed by GitHub
parent 1fee6c4431
commit 7e041c00e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
38 changed files with 3326 additions and 50 deletions

View file

@ -0,0 +1,96 @@
/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.core.common.util
import kotlin.test.Test
import kotlin.test.assertEquals
class FormatStringTest {
@Test
fun positionalStringSubstitution() {
assertEquals("Hello World", formatString("%1\$s %2\$s", "Hello", "World"))
}
@Test
fun positionalIntSubstitution() {
assertEquals("Count: 42", formatString("Count: %1\$d", 42))
}
@Test
fun positionalFloatSubstitution() {
assertEquals("Value: 3.1", formatString("Value: %1\$.1f", 3.14159))
}
@Test
fun positionalFloatTwoDecimals() {
assertEquals("12.35%", formatString("%1\$.2f%%", 12.345))
}
@Test
fun literalPercentEscape() {
assertEquals("100%", formatString("100%%"))
}
@Test
fun mixedPositionalArgs() {
assertEquals("Battery: 85, Voltage: 3.7 V", formatString("Battery: %1\$d, Voltage: %2\$.1f V", 85, 3.7))
}
@Test
fun deviceMetricsPercentTemplate() {
assertEquals("ChUtil: 18.5%", formatString("%1\$s: %2\$.1f%%", "ChUtil", 18.456))
}
@Test
fun deviceMetricsVoltageTemplate() {
assertEquals("Voltage: 3.7 V", formatString("%1\$s: %2\$.1f V", "Voltage", 3.725))
}
@Test
fun deviceMetricsNumericTemplate() {
assertEquals("42.3", formatString("%1\$.1f", 42.345))
}
@Test
fun localStatsUtilizationTemplate() {
assertEquals(
"ChUtil: 12.35% | AirTX: 5.68%",
formatString("ChUtil: %1\$.2f%% | AirTX: %2\$.2f%%", 12.345, 5.678),
)
}
@Test
fun noArgsPlainString() {
assertEquals("Hello", formatString("Hello"))
}
@Test
fun sequentialStringSubstitution() {
assertEquals("a b", formatString("%s %s", "a", "b"))
}
@Test
fun sequentialIntSubstitution() {
assertEquals("1 2", formatString("%d %d", 1, 2))
}
@Test
fun sequentialFloatSubstitution() {
assertEquals("1.2 3.5", formatString("%.1f %.1f", 1.23, 3.45))
}
}

View file

@ -16,7 +16,85 @@
*/
package org.meshtastic.core.common.util
/** Apple (iOS) implementation of string formatting. Stub implementation for compile-only validation. */
actual fun formatString(pattern: String, vararg args: Any?): String = throw UnsupportedOperationException(
"formatString is not supported on iOS at runtime; this target is intended for compile-only validation.",
)
/**
* Apple (iOS) implementation of string formatting.
*
* Implements a subset of Java's `String.format()` patterns used in this codebase:
* - `%s`, `%d` positional or sequential string/integer
* - `%N$s`, `%N$d` explicit positional string/integer
* - `%N$.Nf`, `%.Nf` float with decimal precision
* - `%%` literal percent
*
* This avoids a dependency on `NSString.stringWithFormat` (which uses Obj-C `%@` conventions).
*/
actual fun formatString(pattern: String, vararg args: Any?): String = buildString {
var i = 0
var autoIndex = 0
while (i < pattern.length) {
if (pattern[i] != '%') {
append(pattern[i])
i++
continue
}
i++ // skip '%'
if (i >= pattern.length) break
// Literal %%
if (pattern[i] == '%') {
append('%')
i++
continue
}
// Parse optional positional index (N$)
var explicitIndex: Int? = null
val startPos = i
while (i < pattern.length && pattern[i].isDigit()) i++
if (i < pattern.length && pattern[i] == '$' && i > startPos) {
explicitIndex = pattern.substring(startPos, i).toInt() - 1 // 1-indexed → 0-indexed
i++ // skip '$'
} else {
i = startPos // rewind — digits are part of width/precision, not positional index
}
// Parse optional flags/width (skip for now — not used in this codebase)
// Parse optional precision (.N)
var precision: Int? = null
if (i < pattern.length && pattern[i] == '.') {
i++ // skip '.'
val precStart = i
while (i < pattern.length && pattern[i].isDigit()) i++
if (i > precStart) {
precision = pattern.substring(precStart, i).toInt()
}
}
// Parse conversion character
if (i >= pattern.length) break
val conversion = pattern[i]
i++
val argIndex = explicitIndex ?: autoIndex++
val arg = args.getOrNull(argIndex)
when (conversion) {
's' -> append(arg?.toString() ?: "null")
'd' -> append((arg as? Number)?.toLong()?.toString() ?: arg?.toString() ?: "0")
'f' -> {
val value = (arg as? Number)?.toDouble() ?: 0.0
val places = precision ?: DEFAULT_FLOAT_PRECISION
append(NumberFormatter.format(value, places))
}
else -> {
// Unknown conversion — reproduce original token
append('%')
if (explicitIndex != null) append("${explicitIndex + 1}$")
if (precision != null) append(".$precision")
append(conversion)
}
}
}
}
private const val DEFAULT_FLOAT_PRECISION = 6