open-merou

Open Mérou, a free iOS app for the Oceanic+ underwater iPhone housing
git clone https://git.schurger.org/open-merou.git
Log | Files | Refs | README | LICENSE

HousingProtocolTests.swift (2328B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 @preconcurrency import CoreBluetooth
      3 import Foundation
      4 import Testing
      5 @testable import OpenMerou
      6 
      7 /// Values taken from the 2026-11-13 capture and from the nRF Connect reads (docs/PROTOCOL.md).
      8 struct HousingProtocolTests {
      9     @Test(arguments: [
     10         (UInt8(0x10), HousingButton.left, false),
     11         (0x20, .shutter, false),
     12         (0x30, .right, false),
     13         (0x40, .up, false),
     14         (0x50, .down, false),
     15         (0x31, .right, true),
     16     ])
     17     func buttons(byte: UInt8, button: HousingButton, isLong: Bool) throws {
     18         let event = try #require(ButtonEvent(Data([byte])))
     19         #expect(event.button == button)
     20         #expect(event.isLong == isLong)
     21     }
     22 
     23     @Test func unknownButtonsAreRejected() {
     24         #expect(ButtonEvent(Data([0x00])) == nil)
     25         #expect(ButtonEvent(Data([0x60])) == nil)
     26         #expect(ButtonEvent(Data([0x32])) == nil)
     27         #expect(ButtonEvent(Data()) == nil)
     28     }
     29 
     30     @Test func sensors() {
     31         var readings = HousingReadings()
     32         readings.apply(HousingUUID.internalPressure, Data([0x1D, 0x86, 0x01, 0x00]))
     33         readings.apply(HousingUUID.temperature, Data([0x3C, 0x09, 0x00, 0x00]))
     34         readings.apply(HousingUUID.externalPressure, Data([0xD4, 0x26, 0x00, 0x00]))
     35         readings.apply(HousingUUID.batteryLevel, Data([0x5C]))
     36         #expect(readings.internalPressurePa == 99_869)
     37         #expect(readings.temperatureC == 23.64)
     38         #expect(readings.externalPressurePa == 99_400)
     39         #expect(readings.batteryPercent == 92)
     40     }
     41 
     42     @Test func vacuumDuringPumping() {
     43         var readings = HousingReadings()
     44         readings.apply(HousingUUID.internalPressure, Data([0xD6, 0x36, 0x01, 0x00]))
     45         #expect(readings.internalPressurePa == 79_574)
     46     }
     47 
     48     @Test func pumpState() {
     49         #expect(PumpState(Data([0x01])) == .running)
     50         #expect(PumpState(Data([0x02])) == .stopped)
     51         #expect(PumpState(Data([0x07])) == .other(7))
     52     }
     53 
     54     @Test func negativeTemperature() {
     55         var readings = HousingReadings()
     56         readings.apply(HousingUUID.temperature, Data([0x38, 0xFF, 0xFF, 0xFF]))  // -2.00 °C
     57         #expect(readings.temperatureC == -2)
     58     }
     59 
     60     @Test func dfuIsNeverDiscovered() {
     61         #expect(!HousingUUID.services.contains(CBUUID(string: "FE59")))
     62     }
     63 }