diff --git a/app/src/main/java/eu/darken/capod/pods/core/PodDevice.kt b/app/src/main/java/eu/darken/capod/pods/core/PodDevice.kt index b3b55a5b..0996192a 100644 --- a/app/src/main/java/eu/darken/capod/pods/core/PodDevice.kt +++ b/app/src/main/java/eu/darken/capod/pods/core/PodDevice.kt @@ -103,6 +103,9 @@ interface PodDevice { @Json(name = "fakes.tws.i99999") TWS_I99999( "TWS i99999" ), + @Json(name = "fakes.varunr.airpodspro") VARUNR_AIRPODS_PRO( + "Varunr AirPods Pro" + ), @Json(name = "unknown") UNKNOWN( "Unknown" ); diff --git a/app/src/main/java/eu/darken/capod/pods/core/apple/AppleFactoryModule.kt b/app/src/main/java/eu/darken/capod/pods/core/apple/AppleFactoryModule.kt index 2769c2df..30d38373 100644 --- a/app/src/main/java/eu/darken/capod/pods/core/apple/AppleFactoryModule.kt +++ b/app/src/main/java/eu/darken/capod/pods/core/apple/AppleFactoryModule.kt @@ -8,6 +8,7 @@ import dagger.multibindings.IntoSet import eu.darken.capod.pods.core.apple.airpods.* import eu.darken.capod.pods.core.apple.beats.* import eu.darken.capod.pods.core.apple.misc.Twsi99999 +import eu.darken.capod.pods.core.apple.misc.VarunrAirPodsPro @InstallIn(SingletonComponent::class) @Module @@ -28,4 +29,6 @@ abstract class AppleFactoryModule { @Binds @IntoSet abstract fun powerBeatsPro(factory: PowerBeatsPro.Factory): ApplePodsFactory @Binds @IntoSet abstract fun fakesTwsi999999(factory: Twsi99999.Factory): ApplePodsFactory + @Binds @IntoSet + abstract fun fakesVarunrAirPodsPro(factory: VarunrAirPodsPro.Factory): ApplePodsFactory } \ No newline at end of file diff --git a/app/src/main/java/eu/darken/capod/pods/core/apple/misc/VarunrAirPodsPro.kt b/app/src/main/java/eu/darken/capod/pods/core/apple/misc/VarunrAirPodsPro.kt new file mode 100644 index 00000000..0d243fcd --- /dev/null +++ b/app/src/main/java/eu/darken/capod/pods/core/apple/misc/VarunrAirPodsPro.kt @@ -0,0 +1,143 @@ +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.DualPodDevice +import eu.darken.capod.pods.core.HasCase +import eu.darken.capod.pods.core.HasDualMicrophone +import eu.darken.capod.pods.core.PodDevice +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 + +/** + * AirPods Pro clone similar to Twsi999999. + * Shorter data structure. + */ +data class VarunrAirPodsPro 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, HasDualMicrophone, HasCase { + + override val model: PodDevice.Model = PodDevice.Model.VARUNR_AIRPODS_PRO + + override val rssi: Int + get() = rssiAverage ?: super.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 + } + } + } + + val isThisPodInThecase: Boolean + get() = rawStatus.isBitSet(6) + + /** + * The data flip bit is set if the left pod is primary. + * For the pod that is in the case, this is flipped again though. + */ + override val isLeftPodMicrophone: Boolean + get() = rawStatus.isBitSet(5) xor isThisPodInThecase + + /** + * The data flip bit is UNset if the right pod is primary. + * For the pod that is in the case, this is flipped again though. + */ + override val isRightPodMicrophone: Boolean + get() = !rawStatus.isBitSet(5) xor isThisPodInThecase + + 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(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 = VarunrAirPodsPro(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, + seenCounter = result.seenCounter, + confidence = result.confidence, + cachedBatteryPercentage = result.getLatestCaseBattery(), + rssiAverage = result.averageRssi(basic.rssi), + ) + } + } + + companion object { + private val DEVICE_CODE = 0x0E20.toUShort() + private val TAG = logTag("PodDevice", "Apple", "Varunr", "AirPodsPro") + } +} \ No newline at end of file diff --git a/app/src/test/java/eu/darken/capod/pods/core/apple/misc/VarunrAirPodsProTest.kt b/app/src/test/java/eu/darken/capod/pods/core/apple/misc/VarunrAirPodsProTest.kt new file mode 100644 index 00000000..f4402ec5 --- /dev/null +++ b/app/src/test/java/eu/darken/capod/pods/core/apple/misc/VarunrAirPodsProTest.kt @@ -0,0 +1,31 @@ +package eu.darken.capod.pods.core.apple.misc + +import eu.darken.capod.pods.core.apple.BaseAirPodsTest +import io.kotest.matchers.shouldBe +import kotlinx.coroutines.test.runBlockingTest +import org.junit.jupiter.api.Test + +class VarunrAirPodsProTest : BaseAirPodsTest() { + + @Test + fun `guessed data`() = runBlockingTest { + create("07 13 01 0E 20 71 AA 37 36 00 10 00 FF 64 FF 00 00 00 00 00 00") { + rawPrefix shouldBe 0x01.toUByte() + rawDeviceModel shouldBe 0x0E20.toUShort() + rawStatus shouldBe 0x71.toUByte() + rawPodsBattery shouldBe 0xAA.toUByte() + rawFlags shouldBe 0x3.toUShort() + rawCaseBattery shouldBe 0x7.toUShort() + rawCaseLidState shouldBe 0x36.toUByte() + rawDeviceColor shouldBe 0x00.toUByte() + rawSuffix shouldBe 0x10.toUByte() + + batteryLeftPodPercent shouldBe 1.0f + batteryRightPodPercent shouldBe 1.0f + + isCaseCharging shouldBe false + + batteryCasePercent shouldBe 0.7f + } + } +} \ No newline at end of file