NosfetFrameDetector.kt

/*
 *
 *  Copyright (C) 2026 Gauthier Le Bartz Lyan (Tritbool)
 *  euc_ble_library — version 1.2.0 — licence GPL-3.0
 *  https://github.com/Tritbool/euc_ble_library
 * /
 */

package io.github.tritbool.euc.ble.protocols

import co.touchlab.stately.concurrency.Lock
import co.touchlab.stately.concurrency.withLock

import io.github.tritbool.euc.ble.core.InternalEucBleApi
import io.github.tritbool.euc.ble.core.ByteUtils
import io.github.tritbool.euc.ble.core.EucCrc32

@InternalEucBleApi
class NosfetFrameDetector {
    private val lock = Lock()
    private val buffer = ArrayList<Byte>()

    fun reset() = lock.withLock { buffer.clear() }

    fun detect(data: ByteArray): ByteArray? = lock.withLock {
        for ((index, byte) in data.withIndex()) {
            buffer.add(byte)
            if (buffer.size > 259) buffer.removeAt(0)
            // Inspect all possible headers so a corrupt length cannot hide the next frame.
            for (start in 0 until (buffer.size - 3)) {
                if (buffer[start] != 0xDC.toByte() ||
                    buffer[start + 1] != 0x5A.toByte() ||
                    buffer[start + 2] != 0x5C.toByte()
                ) continue
                val len = buffer[start + 3].toInt() and 0xFF
                if (len < 32 || len == 0x5F || buffer.size - start != len + 4) continue
                val frame = buffer.subList(start, buffer.size).toByteArray()
                if (ByteUtils.getUnsignedShortBE(frame, 28) / 1000 !in 42..44) continue
                if (len > 38) {
                    val crc = EucCrc32.calculate(frame, 0, len)
                    val expected = ByteUtils.getUnsignedIntBE(frame, len)
                    if (crc != expected) continue
                }
                if (ByteUtils.getUnsignedShortBE(frame, 4) !in 2000..18000 ||
                    ByteUtils.getSignedShortBE(frame, 6) !in -1200..1200 ||
                    ByteUtils.getSignedShortBE(frame, 16) !in -30000..30000 ||
                    ByteUtils.getSignedShortBE(frame, 18) !in -5000..13000
                ) continue
                buffer.clear()
                return@withLock frame + data.copyOfRange(index + 1, data.size)
            }
        }
        null
    }
}