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

HousingProtocol.swift (6202B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 // BLE protocol of the Oceanic+ dive housing (advertised name "HUISH"), reverse-engineered
      3 // for interoperability: see docs/PROTOCOL.md. Raw values, little-endian, no encryption.
      4 @preconcurrency import CoreBluetooth
      5 import Foundation
      6 
      7 enum HousingUUID {
      8     /// Common base of the custom services: 0000XXXX-1212-EFDE-1523-785FEABCD123.
      9     static func custom(_ short: String) -> CBUUID {
     10         CBUUID(string: "0000\(short)-1212-EFDE-1523-785FEABCD123")
     11     }
     12 
     13     // "LED and Button" service of the Nordic sample.
     14     nonisolated(unsafe) static let buttonService = custom("1523")
     15     nonisolated(unsafe) static let button = custom("1524")
     16 
     17     // The housing's own service.
     18     nonisolated(unsafe) static let housingService = custom("1623")
     19     nonisolated(unsafe) static let pump = custom("1624")              // write: 01 start, 00 stop
     20     nonisolated(unsafe) static let externalPressure = custom("1625")  // assumption: external pressure, tens of Pa
     21     nonisolated(unsafe) static let temperature = custom("1626")       // hundredths of °C
     22     nonisolated(unsafe) static let internalPressure = custom("1627")  // Pa
     23     nonisolated(unsafe) static let alarm = custom("1628")             // unknown, 00 at rest
     24     nonisolated(unsafe) static let pumpState = custom("1629")         // 01 running, 02 stopped
     25 
     26     nonisolated(unsafe) static let batteryService = CBUUID(string: "180F")
     27     nonisolated(unsafe) static let batteryLevel = CBUUID(string: "2A19")
     28 
     29     nonisolated(unsafe) static let deviceInfoService = CBUUID(string: "180A")
     30     nonisolated(unsafe) static let deviceInfoStrings: [CBUUID: DeviceInfoField] = [
     31         CBUUID(string: "2A29"): .manufacturer,
     32         CBUUID(string: "2A24"): .model,
     33         CBUUID(string: "2A25"): .serialNumber,
     34         CBUUID(string: "2A27"): .hardware,
     35         CBUUID(string: "2A26"): .firmware,
     36         CBUUID(string: "2A28"): .software,
     37     ]
     38 
     39     /// Services read by the app. The update service (FE59, DFU) is deliberately not one of them.
     40     nonisolated(unsafe) static let services = [buttonService, housingService, batteryService, deviceInfoService]
     41 
     42     /// Name advertised by the housing.
     43     static let advertisedName = "HUISH"
     44 }
     45 
     46 /// Strings of the Device Information service.
     47 enum DeviceInfoField: Sendable, CaseIterable {
     48     case manufacturer, model, serialNumber, hardware, firmware, software
     49 
     50     var label: String {
     51         switch self {
     52         case .manufacturer: String(localized: "Manufacturer")
     53         case .model: String(localized: "Model")
     54         case .serialNumber: String(localized: "Serial no.")
     55         case .hardware: String(localized: "Hardware")
     56         case .firmware: String(localized: "Firmware")
     57         case .software: String(localized: "Software")
     58         }
     59     }
     60 }
     61 
     62 enum HousingButton: UInt8, Sendable, CaseIterable {
     63     case left = 1, shutter = 2, right = 3, up = 4, down = 5
     64 
     65     var label: String {
     66         switch self {
     67         case .left: String(localized: "Left")
     68         case .shutter: String(localized: "Shutter")
     69         case .right: String(localized: "Right")
     70         case .up: String(localized: "Up")
     71         case .down: String(localized: "Down")
     72         }
     73     }
     74 }
     75 
     76 struct ButtonEvent: Equatable, Sendable {
     77     let button: HousingButton
     78     let isLong: Bool
     79 
     80     /// One byte: high nibble = button, low nibble = 0 (short) or 1 (long).
     81     init?(_ data: Data) {
     82         guard data.count == 1, let byte = data.first,
     83               let button = HousingButton(rawValue: byte >> 4), byte & 0x0F <= 1 else { return nil }
     84         self.button = button
     85         self.isLong = byte & 0x0F == 1
     86     }
     87 }
     88 
     89 enum PumpState: Equatable, Sendable {
     90     case running, stopped, other(UInt8)
     91 
     92     init?(_ data: Data) {
     93         guard let byte = data.first else { return nil }
     94         switch byte {
     95         case 1: self = .running
     96         case 2: self = .stopped
     97         default: self = .other(byte)
     98         }
     99     }
    100 }
    101 
    102 enum LittleEndian {
    103     static func uint32(_ data: Data) -> UInt32? {
    104         guard data.count == 4 else { return nil }
    105         return data.reversed().reduce(0) { $0 << 8 | UInt32($1) }
    106     }
    107 
    108     static func int32(_ data: Data) -> Int32? {
    109         uint32(data).map { Int32(bitPattern: $0) }
    110     }
    111 }
    112 
    113 /// Last known values of the housing.
    114 struct HousingReadings: Equatable, Sendable {
    115     var internalPressurePa: Double?
    116     var externalPressurePa: Double?
    117     var temperatureC: Double?
    118     var batteryPercent: Int?
    119     var pumpState: PumpState?
    120     var alarmRaw: UInt8?
    121     var lastButton: ButtonEvent?
    122 
    123     /// Applies a received value; returns a label for the log, or nil when the characteristic is unknown.
    124     @discardableResult
    125     mutating func apply(_ uuid: CBUUID, _ data: Data) -> String? {
    126         switch uuid {
    127         case HousingUUID.button:
    128             guard let event = ButtonEvent(data) else { return String(localized: "button? \(data.hex)") }
    129             lastButton = event
    130             return String(localized: "button \(event.button.label)") + (event.isLong ? " (long)" : "")
    131         case HousingUUID.internalPressure:
    132             internalPressurePa = LittleEndian.uint32(data).map(Double.init)
    133             return nil  // too frequent for the log
    134         case HousingUUID.externalPressure:
    135             externalPressurePa = LittleEndian.uint32(data).map { Double($0) * 10 }
    136             return nil
    137         case HousingUUID.temperature:
    138             temperatureC = LittleEndian.int32(data).map { Double($0) / 100 }
    139             return nil
    140         case HousingUUID.batteryLevel:
    141             batteryPercent = data.first.map(Int.init)
    142             return String(localized: "battery \(batteryPercent.map(String.init) ?? "?") %")
    143         case HousingUUID.pumpState:
    144             pumpState = PumpState(data)
    145             return String(localized: "pump state \(data.hex)")
    146         case HousingUUID.alarm:
    147             alarmRaw = data.first
    148             return String(localized: "1628 (alarm?) \(data.hex)")
    149         case HousingUUID.pump:
    150             return String(localized: "pump (read) \(data.hex)")
    151         default:
    152             return nil
    153         }
    154     }
    155 }
    156 
    157 extension Data {
    158     var hex: String { map { String(format: "%02X", $0) }.joined(separator: " ") }
    159 }