-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:d4rken-org/capod into main
- Loading branch information
Showing
6 changed files
with
201 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
app-common/src/main/java/eu/darken/capod/pods/core/apple/misc/FakeAirPodsGen2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package eu.darken.capod.pods.core.apple.misc | ||
|
||
import eu.darken.capod.common.bluetooth.BleScanResult | ||
import eu.darken.capod.common.debug.logging.log | ||
import eu.darken.capod.common.debug.logging.logTag | ||
import eu.darken.capod.common.isBitSet | ||
import eu.darken.capod.common.lowerNibble | ||
import eu.darken.capod.common.upperNibble | ||
import eu.darken.capod.pods.core.* | ||
import eu.darken.capod.pods.core.apple.ApplePods | ||
import eu.darken.capod.pods.core.apple.ApplePodsFactory | ||
import eu.darken.capod.pods.core.apple.protocol.ProximityPairing | ||
import java.time.Instant | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Similar data structure but a lot of placeholder values or hardcoded values | ||
*/ | ||
data class FakeAirPodsGen2 constructor( | ||
override val identifier: PodDevice.Id = PodDevice.Id(), | ||
override val seenLastAt: Instant = Instant.now(), | ||
override val seenFirstAt: Instant = Instant.now(), | ||
override val seenCounter: Int = 1, | ||
override val scanResult: BleScanResult, | ||
override val proximityMessage: ProximityPairing.Message, | ||
override val confidence: Float = PodDevice.BASE_CONFIDENCE, | ||
private val rssiAverage: Int? = null, | ||
private val cachedBatteryPercentage: Float? = null, | ||
) : ApplePods, DualPodDevice, HasEarDetectionDual, HasChargeDetectionDual, HasCase { | ||
|
||
override val model: PodDevice.Model = PodDevice.Model.FAKE_AIRPODS_GEN2 | ||
|
||
override val rssi: Int | ||
get() = rssiAverage ?: super<ApplePods>.rssi | ||
|
||
/** | ||
* Normally values for the left pod are in the lower nibbles, if the left pod is primary (microphone) | ||
* If the right pod is the primary, the values are flipped. | ||
*/ | ||
val areValuesFlipped: Boolean | ||
get() = !rawStatus.isBitSet(5) | ||
|
||
override val batteryLeftPodPercent: Float? | ||
get() { | ||
val value = when (areValuesFlipped) { | ||
true -> rawPodsBattery.upperNibble.toInt() | ||
false -> rawPodsBattery.lowerNibble.toInt() | ||
} | ||
return when (value) { | ||
15 -> null | ||
else -> if (value > 10) { | ||
log { "Left pod: Above 100% battery: $value" } | ||
1.0f | ||
} else { | ||
(value / 10f) | ||
} | ||
} | ||
} | ||
|
||
override val batteryRightPodPercent: Float? | ||
get() { | ||
val value = when (areValuesFlipped) { | ||
true -> rawPodsBattery.lowerNibble.toInt() | ||
false -> rawPodsBattery.upperNibble.toInt() | ||
} | ||
return when (value) { | ||
15 -> null | ||
else -> if (value > 10) { | ||
log { "Right pod: Above 100% battery: $value" } | ||
1.0f | ||
} else { | ||
value / 10f | ||
} | ||
} | ||
} | ||
|
||
private val isThisPodInThecase: Boolean | ||
get() = rawStatus.isBitSet(6) | ||
|
||
override val isLeftPodInEar: Boolean | ||
get() = when (areValuesFlipped xor isThisPodInThecase) { | ||
true -> rawStatus.isBitSet(3) | ||
false -> rawStatus.isBitSet(1) | ||
} | ||
|
||
override val isRightPodInEar: Boolean | ||
get() = when (areValuesFlipped xor isThisPodInThecase) { | ||
true -> rawStatus.isBitSet(1) | ||
false -> rawStatus.isBitSet(3) | ||
} | ||
|
||
override val isLeftPodCharging: Boolean | ||
get() = when (areValuesFlipped) { | ||
false -> rawFlags.isBitSet(0) | ||
true -> rawFlags.isBitSet(1) | ||
} | ||
|
||
override val isRightPodCharging: Boolean | ||
get() = when (areValuesFlipped) { | ||
false -> rawFlags.isBitSet(1) | ||
true -> rawFlags.isBitSet(0) | ||
} | ||
|
||
override val batteryCasePercent: Float? | ||
get() = when (val value = rawCaseBattery.toInt()) { | ||
15 -> cachedBatteryPercentage | ||
else -> if (value > 10) { | ||
log { "Case: Above 100% battery: $value" } | ||
1.0f | ||
} else { | ||
value / 10f | ||
} | ||
} | ||
|
||
override val isCaseCharging: Boolean | ||
get() = rawFlags.isBitSet(2) | ||
|
||
class Factory @Inject constructor() : ApplePodsFactory<FakeAirPodsGen2>(TAG) { | ||
|
||
override fun isResponsible(message: ProximityPairing.Message): Boolean = message.run { | ||
// Official message length is 19HEX, i.e. binary 25, did they copy this wrong? | ||
getModelInfo().full == DEVICE_CODE && length == 19 | ||
} | ||
|
||
override fun create(scanResult: BleScanResult, message: ProximityPairing.Message): ApplePods { | ||
var basic = FakeAirPodsGen2(scanResult = scanResult, proximityMessage = message) | ||
val result = searchHistory(basic) | ||
|
||
if (result != null) basic = basic.copy(identifier = result.id) | ||
updateHistory(basic) | ||
|
||
if (result == null) return basic | ||
|
||
return basic.copy( | ||
identifier = result.id, | ||
seenFirstAt = result.seenFirstAt, | ||
seenLastAt = scanResult.receivedAt, | ||
seenCounter = result.seenCounter, | ||
confidence = result.confidence, | ||
cachedBatteryPercentage = result.getLatestCaseBattery(), | ||
rssiAverage = result.averageRssi(basic.rssi), | ||
) | ||
} | ||
} | ||
|
||
companion object { | ||
private val DEVICE_CODE = 0x0F20.toUShort() | ||
private val TAG = logTag("PodDevice", "Apple", "Fake", "AirPods", "Gen2") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app-common/src/test/java/eu/darken/capod/pods/core/apple/misc/FakeAirPodsGen2Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package eu.darken.capod.pods.core.apple.misc | ||
|
||
import eu.darken.capod.pods.core.PodDevice | ||
import eu.darken.capod.pods.core.apple.BaseAirPodsTest | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Test | ||
|
||
class FakeAirPodsGen2Test : BaseAirPodsTest() { | ||
|
||
@Test | ||
fun `charging in box`() = runTest { | ||
create<FakeAirPodsGen1>("07 13 01 02 20 71 AA 37 32 00 10 00 64 64 FF 00 00 00 00 00 00") { | ||
rawPrefix shouldBe 0x01.toUByte() | ||
rawDeviceModel shouldBe 0x0220.toUShort() | ||
rawStatus shouldBe 0x71.toUByte() | ||
rawPodsBattery shouldBe 0xAA.toUByte() | ||
rawFlags shouldBe 0x3.toUShort() | ||
rawCaseBattery shouldBe 0x7.toUShort() | ||
rawCaseLidState shouldBe 0x32.toUByte() | ||
rawDeviceColor shouldBe 0x00.toUByte() | ||
rawSuffix shouldBe 0x10.toUByte() | ||
|
||
batteryLeftPodPercent shouldBe 1.0f | ||
batteryRightPodPercent shouldBe 1.0f | ||
|
||
isLeftPodInEar shouldBe false | ||
isRightPodInEar shouldBe false | ||
|
||
isLeftPodCharging shouldBe true | ||
isRightPodCharging shouldBe true | ||
|
||
isCaseCharging shouldBe false | ||
|
||
batteryCasePercent shouldBe 0.7f | ||
|
||
model shouldBe PodDevice.Model.FAKE_AIRPODS_GEN1 | ||
} | ||
} | ||
} |