Skip to content

Commit 6b3b409

Browse files
update 160/320 center calculations
1 parent 44630d2 commit 6b3b409

File tree

15 files changed

+417
-545
lines changed

15 files changed

+417
-545
lines changed

app/build.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Build Properties
2-
#Tue Mar 25 22:38:45 EDT 2025
3-
version_build=11
2+
#Thu Mar 27 17:45:38 EDT 2025
3+
version_build=12
44
version_major=3
55
version_minor=1
66
version_patch=4

app/jacoco.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ task jacocoTestCoverageVerification(type: JacocoCoverageVerification, dependsOn:
7070
}
7171
limit {
7272
counter = 'BRANCH'
73-
minimum = 0.87
73+
minimum = 0.88
7474
}
7575
limit {
7676
counter = 'COMPLEXITY'

app/src/main/kotlin/com/vrem/wifianalyzer/wifi/band/WiFiChannel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package com.vrem.wifianalyzer.wifi.band
1919

20+
private const val ALLOWED_RANGE = FREQUENCY_SPREAD / 2
21+
2022
data class WiFiChannel(val channel: Int = 0, val frequency: Int = 0) : Comparable<WiFiChannel> {
2123
fun inRange(value: Int): Boolean =
2224
value in frequency - ALLOWED_RANGE..frequency + ALLOWED_RANGE
@@ -26,7 +28,6 @@ data class WiFiChannel(val channel: Int = 0, val frequency: Int = 0) : Comparabl
2628

2729
companion object {
2830
val UNKNOWN = WiFiChannel()
29-
private const val ALLOWED_RANGE = WiFiChannels.FREQUENCY_SPREAD / 2
3031
}
3132

3233
}

app/src/main/kotlin/com/vrem/wifianalyzer/wifi/band/WiFiChannels.kt

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,59 @@ package com.vrem.wifianalyzer.wifi.band
1919

2020
import com.vrem.util.EMPTY
2121

22+
internal const val FREQUENCY_SPREAD = 5
23+
2224
typealias WiFiRange = Pair<Int, Int>
2325
typealias WiFiChannelPair = Pair<WiFiChannel, WiFiChannel>
2426

25-
abstract class WiFiChannels(val channelRange: WiFiChannelPair, val graphChannels: Set<Int>) {
27+
private val channelRangeGHZ2 = WiFiChannelPair(WiFiChannel(-1, 2402), WiFiChannel(15, 2482))
28+
private val graphChannelsGHZ2 = (1..13).associate { it to "$it" }
29+
30+
class WiFiChannelsGHZ2 : WiFiChannels(channelRangeGHZ2, graphChannelsGHZ2) {
31+
override fun availableChannels(countryCode: String): List<WiFiChannel> =
32+
availableChannels(WiFiChannelCountry.find(countryCode).channelsGHZ2())
33+
}
34+
35+
private val channelRangeGHZ5 = WiFiChannelPair(WiFiChannel(30, 5150), WiFiChannel(179, 5895))
36+
private val graphChannelsGHZ5 = listOf(42, 58, 74, 90, 106, 122, 138, 156, 171).associate {
37+
it to when (it) {
38+
156 -> "155"
39+
else -> "$it"
40+
}
41+
}
42+
43+
class WiFiChannelsGHZ5 : WiFiChannels(channelRangeGHZ5, graphChannelsGHZ5) {
44+
override fun availableChannels(countryCode: String): List<WiFiChannel> =
45+
availableChannels(WiFiChannelCountry.find(countryCode).channelsGHZ5())
46+
47+
override fun graphChannelCount(): Int = super.graphChannelCount() / 2
48+
}
49+
50+
private val channelRangeGHZ6 = WiFiChannelPair(WiFiChannel(-5, 5925), WiFiChannel(235, 7125))
51+
private val graphChannelsGHZ6 = listOf(15, 47, 79, 110, 142, 174, 208).associate {
52+
it to when (it) {
53+
110 -> "111"
54+
142 -> "143"
55+
174 -> "175"
56+
208 -> "207"
57+
else -> "$it"
58+
}
59+
}
60+
61+
class WiFiChannelsGHZ6 : WiFiChannels(channelRangeGHZ6, graphChannelsGHZ6) {
62+
override fun availableChannels(countryCode: String): List<WiFiChannel> =
63+
availableChannels(WiFiChannelCountry.find(countryCode).channelsGHZ6())
64+
65+
override fun graphChannelCount(): Int = super.graphChannelCount() / 2
66+
}
67+
68+
abstract class WiFiChannels(val channelRange: WiFiChannelPair, val graphChannels: Map<Int, String>) {
2669
abstract fun availableChannels(countryCode: String): List<WiFiChannel>
27-
abstract fun channelAvailable(countryCode: String, channel: Int): Boolean
2870

2971
fun inRange(frequency: Int): Boolean = frequency in channelRange.first.frequency..channelRange.second.frequency
3072

3173
fun wiFiChannelByFrequency(frequency: Int): WiFiChannel =
32-
if (inRange(frequency)) {
33-
wiFiChannel(frequency, channelRange)
34-
} else {
35-
WiFiChannel.UNKNOWN
36-
}
74+
if (inRange(frequency)) wiFiChannel(frequency) else WiFiChannel.UNKNOWN
3775

3876
fun wiFiChannelByChannel(channel: Int): WiFiChannel =
3977
if (channel in channelRange.first.channel..channelRange.second.channel) {
@@ -42,31 +80,18 @@ abstract class WiFiChannels(val channelRange: WiFiChannelPair, val graphChannels
4280
WiFiChannel.UNKNOWN
4381
}
4482

45-
open fun graphChannelCount(): Int {
46-
return channelRange.second.channel - channelRange.first.channel + 1
47-
}
83+
open fun graphChannelCount(): Int = channelRange.second.channel - channelRange.first.channel + 1
4884

49-
fun graphChannelByFrequency(frequency: Int): String {
50-
val wiFiChannel: WiFiChannel = wiFiChannelByFrequency(frequency)
51-
return if (WiFiChannel.UNKNOWN != wiFiChannel && graphChannels.contains(wiFiChannel.channel)) {
52-
"${wiFiChannel.channel}"
53-
} else {
54-
String.EMPTY
55-
}
56-
}
85+
fun graphChannelByFrequency(frequency: Int): String =
86+
graphChannels[wiFiChannelByFrequency(frequency).channel] ?: String.EMPTY
5787

58-
fun availableChannels(channels: Set<Int>): List<WiFiChannel> = channels.map { this.wiFiChannelByChannel(it) }
88+
fun availableChannels(channels: Set<Int>): List<WiFiChannel> = channels.map { wiFiChannelByChannel(it) }
5989

6090
fun wiFiChannels(): List<WiFiChannel> = (channelRange.first.channel..channelRange.second.channel).map { wiFiChannelByChannel(it) }
6191

62-
private fun wiFiChannel(frequency: Int, wiFiChannelPair: WiFiChannelPair): WiFiChannel {
63-
val firstChannel: Int = (wiFiChannelPair.first.channel + if (wiFiChannelPair.first.channel < 0) -0.5 else 0.5).toInt()
64-
val channel: Int = ((frequency - wiFiChannelPair.first.frequency).toDouble() / FREQUENCY_SPREAD + firstChannel).toInt()
92+
private fun wiFiChannel(frequency: Int): WiFiChannel {
93+
val firstChannel = (channelRange.first.channel + if (channelRange.first.channel < 0) -0.5 else 0.5).toInt()
94+
val channel = ((frequency - channelRange.first.frequency) / FREQUENCY_SPREAD + firstChannel).toInt()
6595
return WiFiChannel(channel, frequency)
6696
}
67-
68-
companion object {
69-
internal const val FREQUENCY_SPREAD = 5
70-
}
71-
7297
}

app/src/main/kotlin/com/vrem/wifianalyzer/wifi/band/WiFiChannelsGHZ2.kt

Lines changed: 0 additions & 31 deletions
This file was deleted.

app/src/main/kotlin/com/vrem/wifianalyzer/wifi/band/WiFiChannelsGHZ5.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.

app/src/main/kotlin/com/vrem/wifianalyzer/wifi/band/WiFiChannelsGHZ6.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.

app/src/main/kotlin/com/vrem/wifianalyzer/wifi/model/WiFiWidth.kt

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,75 @@
1818
package com.vrem.wifianalyzer.wifi.model
1919

2020
import android.net.wifi.ScanResult
21+
import com.vrem.util.buildMinVersionT
2122
import kotlin.math.abs
2223

23-
typealias ChannelWidth = Int
24+
private val CHANNEL_WIDTH_320MHZ = if (buildMinVersionT()) ScanResult.CHANNEL_WIDTH_320MHZ else 5
25+
26+
// 5GHz: 50, 82, 114, 163 | 6GHz: 15, 47, 79, 111, 143, 175, 207
27+
private val frequency160Range = listOf(
28+
5250 to (5170 to 5329),
29+
5410 to (5330 to 5489),
30+
5650 to (5490 to 5730),
31+
5815 to (5735 to 5895),
32+
6025 to (5945 to 6104),
33+
6185 to (6105 to 6264),
34+
6345 to (6265 to 6424),
35+
6505 to (6425 to 6584),
36+
6665 to (6585 to 6744),
37+
6825 to (6745 to 6904),
38+
6985 to (6905 to 7065)
39+
)
40+
private val frequency160center = frequency160Range.map { it.first }
41+
42+
// 6GHz: 31, 95, 159, 191
43+
private val frequency320Range = listOf(
44+
6100 to (5945 to 6264),
45+
6430 to (6265 to 6584),
46+
6750 to (6585 to 6904),
47+
6910 to (6905 to 7065)
48+
)
49+
50+
// 6GHz: 31, 63, 95, 127, 159, 191
51+
private val frequency320Center = listOf(6100, 6270, 6430, 6590, 6750, 6910)
2452

53+
typealias ChannelWidth = Int
2554
typealias CalculateCenter = (primary: Int, center0: Int, center1: Int) -> Int
2655

2756
internal val calculateCenter20: CalculateCenter = { primary, _, _ -> primary }
28-
2957
internal val calculateCenter40: CalculateCenter = { primary, center0, _ ->
30-
if (abs(primary - center0) >= WiFiWidth.MHZ_40.frequencyWidthHalf) {
31-
(primary + center0) / 2
32-
} else {
33-
center0
34-
}
58+
if (abs(primary - center0) >= WiFiWidth.MHZ_40.frequencyWidthHalf) (primary + center0) / 2 else center0
3559
}
36-
3760
internal val calculateCenter80: CalculateCenter = { _, center0, _ -> center0 }
61+
internal val calculateCenter160: CalculateCenter = { primary, center0, center1 ->
62+
when {
63+
center1 in frequency160center -> center1
64+
center0 in frequency160center -> center0
65+
primary in frequency160center -> primary
66+
else -> frequency160Range.firstOrNull { primary in it.second.first..it.second.second }?.first ?: center1
67+
}
68+
}
69+
internal val calculateCenter320: CalculateCenter = { primary, center0, center1 ->
70+
when {
71+
center1 in frequency320Center -> center1
72+
center0 in frequency320Center -> center0
73+
primary in frequency320Center -> primary
74+
else -> frequency320Range.firstOrNull { primary in it.second.first..it.second.second }?.first ?: center1
75+
}
76+
}
3877

39-
internal val calculateCenter160: CalculateCenter = { _, _, center1 -> center1 }
40-
41-
internal val calculateCenter320: CalculateCenter = { _, _, center1 -> center1 }
42-
43-
enum class WiFiWidth(
44-
val channelWidth: ChannelWidth,
45-
val frequencyWidth: Int,
46-
val guardBand: Int,
47-
val calculateCenter: CalculateCenter
48-
) {
78+
enum class WiFiWidth(val channelWidth: ChannelWidth, val frequencyWidth: Int, val guardBand: Int, val calculateCenter: CalculateCenter) {
4979
MHZ_20(ScanResult.CHANNEL_WIDTH_20MHZ, 20, 2, calculateCenter20),
5080
MHZ_40(ScanResult.CHANNEL_WIDTH_40MHZ, 40, 3, calculateCenter40),
5181
MHZ_80(ScanResult.CHANNEL_WIDTH_80MHZ, 80, 3, calculateCenter80),
5282
MHZ_160(ScanResult.CHANNEL_WIDTH_160MHZ, 160, 3, calculateCenter160),
5383
MHZ_80_PLUS(ScanResult.CHANNEL_WIDTH_80MHZ_PLUS_MHZ, 80, 3, calculateCenter80),
54-
MHZ_320(ScanResult.CHANNEL_WIDTH_320MHZ, 320, 3, calculateCenter320);
84+
MHZ_320(CHANNEL_WIDTH_320MHZ, 320, 3, calculateCenter320);
5585

5686
val frequencyWidthHalf: Int = frequencyWidth / 2
5787

5888
companion object {
5989
fun findOne(channelWidth: ChannelWidth): WiFiWidth =
6090
WiFiWidth.entries.firstOrNull { it.channelWidth == channelWidth } ?: MHZ_20
6191
}
62-
}
92+
}

app/src/test/kotlin/com/vrem/wifianalyzer/wifi/band/WiFiChannelCountryGHZ2Test.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import org.junit.Test
2222
import java.util.SortedSet
2323

2424
class WiFiChannelCountryGHZ2Test {
25-
private val channelsSet1: SortedSet<Int> = sortedSetOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
26-
private val channelsSet2: SortedSet<Int> = sortedSetOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
25+
private val channelsSet1 = (1..11).toList()
26+
private val channelsSet2 = channelsSet1.union(12..13).toList()
2727
private val fixture = WiFiChannelCountryGHZ2()
2828

2929
@Test
@@ -38,7 +38,7 @@ class WiFiChannelCountryGHZ2Test {
3838
.forEach { validateChannels(channelsSet2, fixture.findChannels(it)) }
3939
}
4040

41-
private fun validateChannels(expected: SortedSet<Int>, actual: SortedSet<Int>) {
41+
private fun validateChannels(expected: List<Int>, actual: SortedSet<Int>) {
4242
assertThat(actual).hasSize(expected.size)
4343
assertThat(actual).containsAll(expected)
4444
}

0 commit comments

Comments
 (0)