KingsongSyncProtocol

Improved KingSong protocol: tolerant parsing, header resync, header-aware frame reassembly using ByteByByteFrameParser, safe bounds checks, optional cell voltages parsing, and clamped command generation.

Supports frame types: 0xA9 (live telemetry + pedal hardness), 0xB9 (distance/fan/motorTemperature), 0xBB (name/model/version), 0xB3 (serial number), 0xF5 (CPU load/PWM), 0xF6 (dynamic speed limit), 0xA4/0xB5 (speed settings), 0xF1/0xF2 (Smart BMS data).

Constructors

Link copied to clipboard
constructor(clock: EucClock, unlockRandom: EucRandom)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
open override val manufacturer: String

Manufacturer name (used for display and logging).

Link copied to clipboard

More specific dialect/family implementations may refine this protocol identity.

Link copied to clipboard

Preferred GATT write type for commands sent by this protocol.

Link copied to clipboard
open override val protocolId: String

Stable fingerprint key; independent of runtime class names and inheritance.

Link copied to clipboard

Explicit command support matrix for this protocol. Commands outside this set are considered unsupported by design.

Functions

Link copied to clipboard
open override fun canRetryCommand(payload: ByteArray): Boolean

Whether a BLE-stack rejection may be retried. Accepted writes are never replayed.

Link copied to clipboard
open override fun canSendCommand(payload: ByteArray): Boolean

Rechecked by the BLE write queue immediately before submission.

Link copied to clipboard
open override fun commandStateError(commandId: String): String?

Additional live prerequisites; explanations must never include input values or credentials.

Link copied to clipboard
open fun createCommand(commandId: String, value: CommandValue = CommandValue.None): ByteArray

Open-ID encoder extension point; existing protocols retain their enum encoder.

open override fun createCommand(commandType: CommandType, value: CommandValue = CommandValue.None): ByteArray

Encode a command. Returns an owned byte array, or an empty array when unsupported, invalid or blocked by protocol state. The session must surface an empty result as failure.

Link copied to clipboard

Drains every pending output of this protocol in FIFO order.

Link copied to clipboard
open fun encodeCommand(commandId: String, value: CommandValue = CommandValue.None): CommandEncodingResult

Checked contract API. Rejects invalid values before encoding (and its possible side effects), instead of relying on legacy clamping, masking or ignored fields.

Link copied to clipboard
open override fun getBMSData(): List<BMSData>

Returns the current BMS data snapshots for all detected battery packs. Each entry represents one BMS unit (typically 1 or 2 for dual-battery wheels). Data is accumulated from frame types 0xF1/0xF2 pages 0x00 (summary), 0x01 (temperatures), and 0x02-0x06 (cell voltages).

Link copied to clipboard

Returns the candidate data characteristic UUIDs for this protocol.

Link copied to clipboard

Latest model/dialect snapshot. Conditional encoding still requires protocol state checks.

Link copied to clipboard

API-level command support check (used by framework and clients).

Link copied to clipboard
open override fun getDataCharacteristicUUID(): EucUuid

Get the UUID for the data characteristic

Link copied to clipboard

Latest diagnostic-log download snapshot, or null if none was started (or unsupported). Data is only exposed once the whole file is received and CRC-verified.

Link copied to clipboard
open override fun getPollingPlan(): ProtocolPollingPlan

Optional polling/query plan consumed by the session layer.

Link copied to clipboard
Link copied to clipboard
open override fun getServiceUUID(): EucUuid

Get the UUID for the service

Link copied to clipboard

Get the UUID for the write characteristic (if different from data characteristic)

Link copied to clipboard
open override fun hasSpeedMeasurement(data: EUCData): Boolean

True when this telemetry contains a new speed measurement rather than a carried-over value.

Link copied to clipboard
open override fun isDeviceReady(data: EUCData): Boolean

Check if the device is ready for operation

Link copied to clipboard
open fun matchesDeviceName(deviceName: String): Boolean

Returns true if the given BLE device advertisement name suggests this protocol.

Link copied to clipboard
open override fun matchesQueryResponse(query: ProtocolQuerySpec, data: ByteArray): Boolean

Optional query/response matcher used by session observability and retry policy.

Link copied to clipboard
open override fun onCommandSubmitted(payload: ByteArray)

Called only after the BLE stack accepts a queued write, not when it is encoded.

Link copied to clipboard
open override fun onNotification(data: ByteArray)

Feeds one raw BLE notification (any fragmentation) into the state machine.

Link copied to clipboard
open override fun onTimer(id: String)

Called when a timer armed with ProtocolOutput.StartTimer expires.

Link copied to clipboard
open override fun pollOutput(): ProtocolOutput?

Returns and removes the next pending output, or null when there is none.

Link copied to clipboard
open override fun resetSession()

Clears connection-specific state without closing this reusable protocol.