drewtech

package
v2.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package drewtech is a native (no Windows DLL) J2534 client for the DrewTech Mongoose Pro GM II. It speaks the Mongoose wire protocol directly over the device's virtual COM port at 115200 baud.

The wire protocol is decoded from the Windows DLL in devel/drewtech (re/*.md; PROTOCOL.md there predates it and is wrong about 0x10). The libj2534_mongoose C driver implements the same protocol in full and is the reference when the two disagree. The notable facts:

  • framing check word is length^0x51E6 (LE), not a constant magic byte
  • replies carry the command with 0x8000 set and echo the request's sequence, a 1..255 byte counter; status is a u32 at body[12:16]
  • RX arrives as unsolicited 0x0009 pushes; there is no "read messages" command
  • 0x08/0x0A are Tx confirmations carrying the Tx sequence, not RX
  • a pass filter (0x0D) per CAN id is required or you receive nothing; 0x0E removes one by the handle the 0x0D reply gave, 0x10 all of a type
  • 0x11 is a multiplexed control op (subtype at payload[8:12]): GetStatus, clear-TX=2, clear-RX=3, become-master=4

This file is the wire layer (framing + device transport + CAN data path). The J2534 API facade lives in j2534.go.

Index

Constants

View Source
const (
	MagicByte    uint8  = 0x51
	ChecksumXOR  uint8  = 0xE6
	ChecksumWord uint16 = 0x51E6 // check = length ^ 0x51E6 (LE)

	DirectionOut uint8 = 0x01
	DirectionIn  uint8 = 0x00

	FlagRequest       uint8 = 0x01
	FlagRequestFinal  uint8 = 0x00
	FlagResponse      uint8 = 0x81
	FlagResponseFinal uint8 = 0x80

	SubCmdInit           uint8 = 0x00
	SubCmdGetVersion     uint8 = 0x03
	SubCmdClose          uint8 = 0x05
	SubCmdConnect        uint8 = 0x06
	SubCmdDisconnect     uint8 = 0x07
	SubCmdCANTx          uint8 = 0x08
	SubCmdCANRx          uint8 = 0x09
	SubCmdTxStatus       uint8 = 0x0A // device->host Tx completion status
	SubCmdDeleteObject   uint8 = 0x0E // remove one filter or periodic: type, device handle
	SubCmdClearFilters   uint8 = 0x10 // remove every object of one type
	SubCmdGetConfig      uint8 = 0x0C // get config (there is no runtime set)
	SubCmdStartMsgFilter uint8 = 0x0D
	SubCmdControl        uint8 = 0x11 // multiplexed: status / clear buffers / master
	SubCmdSetProtocol    uint8 = 0x12
	SubCmdGetSerial      uint8 = 0x13

	ProtocolCAN uint32 = 6

	ParamDataRate       uint32 = 0x04
	ParamBitSamplePoint uint32 = 0x14
	ParamSyncJumpWidth  uint32 = 0x15
)
View Source
const (
	GET_CONFIG                    uint32 = 0x01
	SET_CONFIG                    uint32 = 0x02
	READ_VBATT                    uint32 = 0x03
	FIVE_BAUD_INIT                uint32 = 0x04
	FAST_INIT                     uint32 = 0x05
	CLEAR_TX_BUFFER               uint32 = 0x07
	CLEAR_RX_BUFFER               uint32 = 0x08
	CLEAR_PERIODIC_MSGS           uint32 = 0x09
	CLEAR_MSG_FILTERS             uint32 = 0x0A
	CLEAR_FUNCT_MSG_LOOKUP_TABLE  uint32 = 0x0B
	ADD_TO_FUNCT_MSG_LOOKUP_TABLE uint32 = 0x0C
	DELETE_FROM_FUNCT_MSG_LOOKUP  uint32 = 0x0D
	READ_PROG_VOLTAGE             uint32 = 0x0E
)

J2534 IOCTL ids (SAE J2534-1).

View Source
const (
	PASS_FILTER         uint32 = 0x01
	BLOCK_FILTER        uint32 = 0x02
	FLOW_CONTROL_FILTER uint32 = 0x03
)

J2534 filter types (SAE J2534-1) for PassThruStartMsgFilter.

Variables

View Source
var ErrDeviceGone = errors.New("drewtech: device gone")

ErrDeviceGone is reported to the error handler when reading the port fails, which ends the read loop: the device was unplugged or the port broke.

View Source
var ErrNotSupported = errors.New("drewtech: operation not supported")

ErrNotSupported is returned by J2534 operations the Mongoose firmware or this native client does not implement (see PROTOCOL.md for why each is out).

Functions

func PassThruGetNextCarDAQ

func PassThruGetNextCarDAQ() ([]string, error)

PassThruGetNextCarDAQ enumerates candidate device serial ports. The J2534 DLL discovers Mongoose units over USB/SetupDi; here we return the OS serial-port list for the caller to pick from.

Types

type CANFrame

type CANFrame struct {
	ID        uint32
	Data      []byte
	DLC       int
	Timestamp uint32
}

CANFrame represents a CAN bus frame.

func (*CANFrame) String

func (f *CANFrame) String() string

type Device

type Device struct {
	// contains filtered or unexported fields
}

Device represents a DrewTech J2534 device.

func New

func New(opts ...Option) *Device

New creates a new Device.

func (*Device) BecomeMaster

func (d *Device) BecomeMaster(pollID byte) error

BecomeMaster issues the "become master" control command (0x11 subtype 4).

func (*Device) ClearMsgFilters

func (d *Device) ClearMsgFilters() error

ClearMsgFilters removes all pass and block filters, one wire op 0x10 per object type.

func (*Device) Close

func (d *Device) Close() error

Close stops periodic messages, the read loop and closes the port.

func (*Device) GetChannelConfig

func (d *Device) GetChannelConfig(param uint32) (uint32, error)

GetChannelConfig reads a config param from the connected channel. The reply echoes the param at [16:20] and returns the value at [20:24]. Only DATA_RATE (0x04), BIT_SAMPLE_POINT (0x14) and SYNC_JUMP_WIDTH (0x15) are supported; there is no runtime set (0x0C is get-only). See PROTOCOL.md.

func (*Device) Open

func (d *Device) Open(portName string) error

Open opens the serial port and starts the read loop.

func (*Device) PassThruClose

func (d *Device) PassThruClose() error

PassThruClose sends the wire close command and tears down the transport.

func (*Device) PassThruConnect

func (d *Device) PassThruConnect(baudRate uint32) error

PassThruConnect connects the CAN channel at the given bit rate and selects the CAN protocol. The device accepts off-table rates (e.g. 615384); the rate whitelist lived only in the Windows DLL, which this client bypasses.

func (*Device) PassThruDisconnect

func (d *Device) PassThruDisconnect() error

PassThruDisconnect disconnects the channel from the bus.

func (*Device) PassThruFirmwareUpdate

func (d *Device) PassThruFirmwareUpdate(image []byte) error

PassThruFirmwareUpdate is intentionally not implemented. The reflash images live encrypted in the DLL's RCDATA resources and the streaming protocol is untested here; a wrong write can brick the dongle. See PROTOCOL.md.

func (*Device) PassThruGetLastError

func (d *Device) PassThruGetLastError() string

PassThruGetLastError returns the most recent error observed by the device (read loop, tx faults, rx overflow), or "" if none.

func (*Device) PassThruIoctl

func (d *Device) PassThruIoctl(ioctlID, param uint32) (uint32, error)

PassThruIoctl dispatches the standard J2534 ioctls. For GET_CONFIG, param is the config id and the returned value is the config value; other ioctls ignore param and return 0.

func (*Device) PassThruOpen

func (d *Device) PassThruOpen() error

PassThruOpen runs the DLL's device handshake and caches firmware/serial info for PassThruReadVersion. Like the DLL it only requires the board info, the jump to firmware and the open; the other answers go unused.

func (*Device) PassThruReadDetails

func (d *Device) PassThruReadDetails() VersionInfo

PassThruReadDetails returns the cached version/serial details of the open device (there is no separate wire query; PassThruOpen already fetched them).

func (*Device) PassThruReadMsgs

func (d *Device) PassThruReadMsgs(max int, timeout time.Duration) ([]*CANFrame, error)

PassThruReadMsgs drains up to max frames from the host-side RX FIFO, waiting up to timeout for the first one. There is no wire "read" command: the device pushes 0x09 frames unsolicited and the read loop queues them.

func (*Device) PassThruReadVersion

func (d *Device) PassThruReadVersion() VersionInfo

PassThruReadVersion returns cached firmware/bootloader/serial (from PassThruOpen) plus the reference DLL/API version strings.

func (*Device) PassThruSetIncomingMsgCallback

func (d *Device) PassThruSetIncomingMsgCallback(cb func(*CANFrame))

PassThruSetIncomingMsgCallback registers a callback invoked for every RX frame (in addition to the FIFO and any WithCANChannel). Safe to call while the read loop is running.

func (*Device) PassThruSetProgrammingVoltage

func (d *Device) PassThruSetProgrammingVoltage(pin, millivolts uint32) error

PassThruSetProgrammingVoltage is not implemented: the wire encoding is buried in the DLL's per-pin C++ dispatch and untestable without hardware, and wrong bytes on a voltage pin are a hardware risk. See PROTOCOL.md.

func (*Device) PassThruStartMsgFilter

func (d *Device) PassThruStartMsgFilter(filterType, pattern, mask uint32) (uint32, error)

PassThruStartMsgFilter installs a message filter and returns its id. A PASS filter per CAN id is required: with none installed you receive nothing. Pass mask 0xFFFFFFFF for an exact match on pattern. Only PASS_FILTER is implemented: BLOCK is wire kind 2 in the same message (PROTOCOL.md §8) but untested on hardware, and FLOW_CONTROL needs the ISO15765 3-message variant.

func (*Device) PassThruStartPeriodicMsg

func (d *Device) PassThruStartPeriodicMsg(canID uint32, data []byte, interval time.Duration) (uint32, error)

PassThruStartPeriodicMsg sends canID/data every interval and returns a message id for PassThruStopPeriodicMsg. Implemented host-side (a ticker calling WriteMsgs); the firmware has no periodic-message wire command.

func (*Device) PassThruStopMsgFilter

func (d *Device) PassThruStopMsgFilter(filterID uint32) error

PassThruStopMsgFilter removes a pass filter by the id PassThruStartMsgFilter returned.

func (*Device) PassThruStopPeriodicMsg

func (d *Device) PassThruStopPeriodicMsg(id uint32) error

PassThruStopPeriodicMsg stops a periodic message by id.

func (*Device) PassThruWriteMsgs

func (d *Device) PassThruWriteMsgs(canID uint32, data []byte) error

PassThruWriteMsgs queues a CAN frame on the device, returning once the device has queued it, as the DLL does with a zero timeout. A full device queue is retried for up to a second. The wire frame is the fixed-size 0x24 layout the DLL sends: payload padded to 32 bytes, actual data length carried at [16:18].

type FirmwareInfo

type FirmwareInfo struct {
	FW Version
	BL Version
}

func ExtractVersions

func ExtractVersions(data []byte) FirmwareInfo

type Option

type Option func(*Device)

Option configures the device.

func WithCANChannel

func WithCANChannel(ch chan *CANFrame) Option

WithCANChannel sets a channel for receiving CAN frames.

func WithCANFrameHandler

func WithCANFrameHandler(handler func(*CANFrame)) Option

WithCANFrameHandler sets the CAN frame callback. Frames pushed to a handler or channel are not also queued for PassThruReadMsgs.

func WithErrorHandler

func WithErrorHandler(handler func(error)) Option

WithErrorHandler sets the error callback.

type Packet

type Packet struct {
	Length    uint16
	Direction uint8
	Channel   uint8
	Payload   []byte
}

Packet represents a protocol packet. Payload starts at the command opcode (wire offset 8): Payload[0]=opcode, Payload[1]=flag, Payload[2:4]=sequence.

func (*Packet) IsCANRx

func (p *Packet) IsCANRx() bool

IsCANRx reports whether this is an unsolicited RX push, command 0x0009. Board-info replies share the opcode but carry the reply flag (0x8109).

func (*Packet) ParseCANFrame

func (p *Packet) ParseCANFrame() (*CANFrame, error)

func (*Packet) Sequence

func (p *Packet) Sequence() uint16

func (*Packet) SubCommand

func (p *Packet) SubCommand() uint8

func (*Packet) ToBytes

func (p *Packet) ToBytes() []byte

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser handles streaming packet parsing.

func NewParser

func NewParser() *Parser

func (*Parser) Feed

func (p *Parser) Feed(data []byte) ([]*Packet, error)

Feed appends data and returns the complete packets now buffered, each with its own copy of the payload.

type Version

type Version struct {
	Major, Minor, Patch, Build byte
}

func (Version) String

func (v Version) String() string

type VersionInfo

type VersionInfo struct {
	Firmware   string // device firmware, e.g. "1.2.4.0"
	Bootloader string // device bootloader
	Serial     string // device serial number
	DLL        string // reference DLL version string
	API        string // J2534 API version the reference DLL implements
}

VersionInfo is what PassThruReadVersion reports.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL