GotwaySyncProtocol

Gotway/Begode reverse-engineered protocol (updated)

This comment summarizes the observed variants:

  • Raw "A" / "B" frames (controller serial stream, header 0x55 0xAA)

  • Short "legacy" packets (e.g., 0x01 / 0x02) re-emitted by some adapters

  • Type 0xA5 packets (compressed commands/status emitted by firmware/adapters)

General observations:

  • Raw A/B frames observed on the serial port typically use header 0x55 0xAA and Big Endian (BE) fields for 16/32 bit integers.

  • Legacy packets (0x01/0x02) and some 0xA5 packets often use Little Endian (LE) encoding and a more compact format.

  • Current/temperature fields can be signed; must be converted correctly.

  • Many firmware/adapters do not add checksum. The stream can be fragmented, delayed, or lose bytes on the BLE side (no flow control).

  • Some packets include BMS cell voltages at the end, encoded as pairs of 2 bytes (LE) or in mV/centivolts depending on the variant.

Example (observed): A: 55 AA 19 F0 00 00 00 00 00 00 01 2C FD CA 00 01 FF F8 00 18 5A 5A 5A 5A B: 55 AA 00 0A 4A 12 48 00 1C 20 00 2A 00 03 00 07 00 08 04 18 5A 5A 5A 5A

Summary format (adjust according to firmware/model):

  • Frame A (header 0x55 0xAA): Bytes 0-1: 0x55 0xAA Bytes 2-3: BE voltage (fixed point, e.g., 1/100) Bytes 4-5: BE speed (fixed point, e.g., 3.6 * value / 100 -> km/h) Bytes 6-9: BE distance (uint32, meters) Bytes 10-11: BE current (signed, fixed point) Bytes 12-13: BE temperature (signed or raw MPU value) Bytes 14-17: unknown / flags Byte 18: frame type (e.g., 0x00) Byte 19: footer (0x18) Bytes 20-..: footer 0x5A 0x5A 0x5A 0x5A (or variants) + optional BMS trailing

  • Frame B (header 0x55 0xAA): Bytes 2-5: BE total distance (uint32) Byte 6: pedals mode / alarms (nibbles) Bytes 7-12: additional unknown fields Byte 13: LED / mode Bytes 14-17: unknown Byte 18: frame type (e.g., 0x04) Footer same

  • Legacy packets (e.g., 0x01 / 0x02): - Often sent by Serial->BLE adapter or alternative firmware. - LE fields, more compact formats; can represent voltage/speed/etc.

  • 0xA5 packets: - Used for commands (LIGHT_ON/OFF, BEEP, POWER_OFF) and sometimes for compressed states. Different structure (header 0xA5 ...).

Parsing recommendations:

  • Dispatch by first byte/header: 0x55 (A/B raw), 0x01/0x02 (legacy), 0xA5 (command/status), or by type byte in the frame if present.

  • For A/B: process integers as BE. For legacy/0xA5: try LE.

  • Handle fragmentation: tolerate variable sizes, ignore too short frames, attempt re-synchronization on 0x55 0xAA or adapter headers.

  • Dynamically extract cell voltages from the queue if present: read pairs of 2 bytes (LE) and convert to V (mV -> V or /100 -> V depending on ranges).

  • Correctly convert signed values (current, motor temperatures).

  • Stay defensive: validate plausible ranges (voltage, current, temperature).

Why were these variants not in the old comment?

  • The original comment described the raw serial stream observed on a specific controller/firmware. Other firmware/adapters (Serial->BLE) re-emit or transform these bytes (different headers, different endianness) — these variants were not necessarily present during the initial reverse engineering.

Inheritors

Constructors

Link copied to clipboard
constructor(clock: EucClock)

Types

Link copied to clipboard
data class BatteryVoltageRange(val minVoltage: Double, val maxVoltage: Double)
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

Gotway/Begode wheels use an HM-10 style serial-to-BLE bridge whose FFE1 characteristic only advertises write-without-response; legacy WheelLog also writes every Gotway command with WriteType.WITHOUT_RESPONSE.

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 fun canRetryCommand(payload: ByteArray): Boolean

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

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

Rechecked by the BLE write queue immediately before submission.

Link copied to clipboard
open 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 smart-BMS groups. Each entry represents one cell group (up to 4, matching the Begode app's G1..G4 tabs). Group summaries come from Type 1 frames (subpage = group) and cell voltages from the Type 2/3 streams (Type 2 carries groups 1 and 2, Type 3 carries groups 3 and 4).

Link copied to clipboard

Returns the candidate data characteristic UUIDs for this protocol.

Link copied to clipboard

The advertised firmware banner selects which catalogue profile applies. Stock firmware gets the base command set; "BF" (SmirnoV) adds the fourth pedal mode and "CF" (Freestyl3r) adds the fourth alarm preset. Until a banner arrives the base set is used, so unknown wheels are never offered a command their firmware is not known to accept.

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 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.