parser

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractContent

func ExtractContent(data []byte) ([]byte, error)

ExtractContent extracts the content portion of a packet (excluding header/footer) For 0x7878 packets: StartBit(2) + Length(1) + Protocol(1) + Content + Serial(2) + CRC(2) + StopBit(2) For 0x7979 packets: StartBit(2) + Length(2) + Protocol(1) + Content + Serial(2) + CRC(2) + StopBit(2)

func ExtractSerialNumber

func ExtractSerialNumber(data []byte) (uint16, error)

ExtractSerialNumber extracts the serial number from a packet

func Has

func Has(protocolNum byte) bool

Has returns true if the default registry has a parser for the protocol

func IsLongPacket

func IsLongPacket(data []byte) bool

IsLongPacket returns true if the packet uses 0x7979 format

func IsShortPacket

func IsShortPacket(data []byte) bool

IsShortPacket returns true if the packet uses 0x7878 format

func MustRegister

func MustRegister(p Parser)

MustRegister adds a parser to the default registry and panics on error

func Parse

func Parse(protocolNum byte, data []byte) (packet.Packet, error)

Parse uses the default registry to parse a packet

func Register

func Register(p Parser) error

Register adds a parser to the default registry

Types

type Alarm4GParser

type Alarm4GParser struct {
	BaseParser
}

Alarm4GParser parses 4G alarm packets (Protocol 0xA4)

func NewAlarm4GParser

func NewAlarm4GParser() *Alarm4GParser

NewAlarm4GParser creates a new 4G alarm parser

func (*Alarm4GParser) Parse

func (p *Alarm4GParser) Parse(data []byte, ctx Context) (packet.Packet, error)

type AlarmMultiFenceParser

type AlarmMultiFenceParser struct {
	BaseParser
}

AlarmMultiFenceParser parses alarm packets with geo-fence data (Protocol 0x27)

func NewAlarmMultiFenceParser

func NewAlarmMultiFenceParser() *AlarmMultiFenceParser

NewAlarmMultiFenceParser creates a new multi-fence alarm parser

func (*AlarmMultiFenceParser) Parse

func (p *AlarmMultiFenceParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface Multi-fence alarm packet has the same structure as alarm but with fence ID

type AlarmParser

type AlarmParser struct {
	BaseParser
}

AlarmParser parses alarm packets (Protocol 0x26)

func NewAlarmParser

func NewAlarmParser() *AlarmParser

NewAlarmParser creates a new alarm parser

func (*AlarmParser) Parse

func (p *AlarmParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface Alarm packet content structure (Protocol 0x26): - DateTime: 6 bytes (YY MM DD HH MM SS) - GPS Info Length: 1 byte - Latitude: 4 bytes - Longitude: 4 bytes - Speed: 1 byte - Course/Status: 2 bytes - LBS Length: 1 byte (total length of LBS info) - MCC: 2 bytes - MNC: 1 byte - LAC: 2 bytes - CellID: 3 bytes - Terminal Info: 1 byte - Voltage Level: 1 byte - GSM Signal: 1 byte - Alarm Type: 1 byte (Alert and Language byte 1) - Language: 1 byte (Alert and Language byte 2) - Mileage: 4 bytes Total content: 33 bytes minimum

type BaseParser

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

BaseParser provides common functionality for parsers

func NewBaseParser

func NewBaseParser(protocolNum byte, name string) BaseParser

NewBaseParser creates a new base parser

func (*BaseParser) Name

func (p *BaseParser) Name() string

Name implements Parser

func (*BaseParser) ProtocolNumber

func (p *BaseParser) ProtocolNumber() byte

ProtocolNumber implements Parser

type ChineseAddressParser

type ChineseAddressParser struct {
	BaseParser
}

ChineseAddressParser parses Chinese address response packets (Protocol 0x17)

func NewChineseAddressParser

func NewChineseAddressParser() *ChineseAddressParser

NewChineseAddressParser creates a new Chinese address parser

func (*ChineseAddressParser) Parse

func (p *ChineseAddressParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface Chinese Address packet content structure (Protocol 0x17): - Content Length: 1 byte (length of data between server flag and serial number) - Server Flag: 4 bytes (server marker) - ALARMSMS: 8 bytes (ASCII, typically "ALARMSMS") - Separator "&&": 2 bytes (ASCII) - Address Content: M bytes (UNICODE - UTF-16 BE) - Separator "&&": 2 bytes (ASCII) - Phone Number: 21 bytes (ASCII, "0" repeated for alarm packets) - Separator "##": 2 bytes (ASCII) Minimum content: 1 + 4 + 8 + 2 + 0 + 2 + 21 + 2 = 40 bytes

type CommandResponseOldParser

type CommandResponseOldParser struct {
	BaseParser
}

CommandResponseOldParser parses old-format command response packets (Protocol 0x15)

func NewCommandResponseOldParser

func NewCommandResponseOldParser() *CommandResponseOldParser

NewCommandResponseOldParser creates a new old-format command response parser

func (*CommandResponseOldParser) Parse

func (p *CommandResponseOldParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface

type CommandResponseParser

type CommandResponseParser struct {
	BaseParser
}

CommandResponseParser parses command response packets (Protocol 0x21 and 0x15)

func NewCommandResponseParser

func NewCommandResponseParser() *CommandResponseParser

NewCommandResponseParser creates a new command response parser

func (*CommandResponseParser) Parse

func (p *CommandResponseParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface Command response packet content structure: - Response Length: 1 byte - Server Flag: 4 bytes (echo of original command) - Response Content: variable length (ASCII)

type Context

type Context struct {
	// StrictMode enables strict validation
	StrictMode bool

	// ValidateIMEI enables IMEI checksum validation
	ValidateIMEI bool

	// TimezoneOffset is the default timezone offset in minutes
	TimezoneOffset int
}

Context provides additional context for parsing

func DefaultContext

func DefaultContext() Context

DefaultContext returns the default parser context

type EnglishAddressParser

type EnglishAddressParser struct {
	BaseParser
}

EnglishAddressParser parses English address response packets (Protocol 0x97)

func NewEnglishAddressParser

func NewEnglishAddressParser() *EnglishAddressParser

NewEnglishAddressParser creates a new English address parser

func (*EnglishAddressParser) Parse

func (p *EnglishAddressParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface English Address packet content structure (Protocol 0x97): Same as Chinese but: - Uses long packet format (0x7979 start bit, 2-byte length) - Address content is ASCII/UTF-8 instead of UNICODE - Content Length: 1 byte - Server Flag: 4 bytes - ALARMSMS: 8 bytes (ASCII) - Separator "&&": 2 bytes (ASCII) - Address Content: M bytes (ASCII/UTF-8) - Separator "&&": 2 bytes (ASCII) - Phone Number: 21 bytes (ASCII) - Separator "##": 2 bytes (ASCII) Minimum content: 40 bytes

type GPSAddressRequestParser

type GPSAddressRequestParser struct {
	BaseParser
}

GPSAddressRequestParser parses GPS address request packets (Protocol 0x2A)

func NewGPSAddressRequestParser

func NewGPSAddressRequestParser() *GPSAddressRequestParser

NewGPSAddressRequestParser creates a new GPS address request parser

func (*GPSAddressRequestParser) Parse

func (p *GPSAddressRequestParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface Content structure (41 bytes according to JM-VL03 spec): - DateTime: 6 bytes (YY MM DD HH MM SS) - GPS Info: 1 byte (satellites in low nibble) - Latitude: 4 bytes (raw / 1,800,000) - Longitude: 4 bytes (raw / 1,800,000) - Speed: 1 byte (km/h) - Course/Status: 2 bytes (heading + status flags) - Phone Number: 21 bytes (ASCII) - Alert/Language: 2 bytes (AlarmType + Language) Total content: 41 bytes

type HeartbeatParser

type HeartbeatParser struct {
	BaseParser
}

HeartbeatParser parses heartbeat packets (Protocol 0x13)

func NewHeartbeatParser

func NewHeartbeatParser() *HeartbeatParser

NewHeartbeatParser creates a new heartbeat parser

func (*HeartbeatParser) Parse

func (p *HeartbeatParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface Heartbeat packet content structure: - Terminal Info: 1 byte - Voltage Level: 1 byte - GSM Signal: 1 byte - Extended Info: 2 bytes (optional, in newer devices) Total content: 3-5 bytes

type InfoTransferParser

type InfoTransferParser struct {
	BaseParser
}

InfoTransferParser parses information transfer packets (Protocol 0x94)

func NewInfoTransferParser

func NewInfoTransferParser() *InfoTransferParser

NewInfoTransferParser creates a new info transfer parser

func (*InfoTransferParser) Parse

func (p *InfoTransferParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface Info transfer packet content structure: - Sub-protocol: 1 byte - Data: variable length

type LBS4GParser

type LBS4GParser struct {
	BaseParser
}

LBS4GParser parses 4G LBS packets (Protocol 0xA1)

func NewLBS4GParser

func NewLBS4GParser() *LBS4GParser

NewLBS4GParser creates a new 4G LBS parser

func (*LBS4GParser) Parse

func (p *LBS4GParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface 4G LBS packet has extended LBS info with multiple cell towers

type LBSParser

type LBSParser struct {
	BaseParser
}

LBSParser parses LBS packets (Protocol 0x28)

func NewLBSParser

func NewLBSParser() *LBSParser

NewLBSParser creates a new LBS parser

func (*LBSParser) Parse

func (p *LBSParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface LBS packet content structure: - DateTime: 6 bytes - MCC: 2 bytes - MNC: 1 byte - LAC: 2 bytes - Cell ID: 3 bytes Optional: - Terminal Info: 1 byte - Voltage Level: 1 byte - GSM Signal: 1 byte - Upload Mode: 1 byte Minimum content: 14 bytes, with status: 18 bytes

type Location4GParser

type Location4GParser struct {
	BaseParser
}

Location4GParser parses 4G GPS location packets (Protocol 0xA0)

func NewLocation4GParser

func NewLocation4GParser() *Location4GParser

NewLocation4GParser creates a new 4G location parser

func (*Location4GParser) Parse

func (p *Location4GParser) Parse(data []byte, ctx Context) (packet.Packet, error)

type LocationParser

type LocationParser struct {
	BaseParser
}

LocationParser parses GPS location packets (Protocol 0x22)

func NewLocationParser

func NewLocationParser() *LocationParser

NewLocationParser creates a new location parser

func (*LocationParser) Parse

func (p *LocationParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface Location packet content structure (Protocol 0x22): - DateTime: 6 bytes (YY MM DD HH MM SS) - GPS Info Length: 1 byte (high nibble: satellites, low nibble: GPS data length / 2) - Latitude: 4 bytes (raw value / 1800000 = decimal degrees) - Longitude: 4 bytes (raw value / 1800000 = decimal degrees) - Speed: 1 byte (km/h) - Course/Status: 2 bytes - MCC: 2 bytes (Mobile Country Code) - MNC: 1 byte (Mobile Network Code) - LAC: 2 bytes (Location Area Code) - CellID: 3 bytes (Cell Tower ID) - ACC: 1 byte (0x00=OFF, 0x01=ON) - Data Upload Mode: 1 byte - GPS Data Re-upload: 1 byte (0x00=Real-time, 0x01=Re-upload) - Mileage Statistics: 4 bytes Total content: 33 bytes minimum (all fields are MANDATORY per protocol spec)

type LoginParser

type LoginParser struct {
	BaseParser
}

LoginParser parses login packets (Protocol 0x01)

func NewLoginParser

func NewLoginParser() *LoginParser

NewLoginParser creates a new login parser

func (*LoginParser) Parse

func (p *LoginParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface Login packet content structure: - IMEI: 8 bytes (BCD encoded, 15 digits + padding) - Model Identification Code: 2 bytes - Timezone/Language: 2 bytes Total content: 12 bytes

type OnlineCommandParser

type OnlineCommandParser struct {
	BaseParser
}

OnlineCommandParser parses online command packets (Protocol 0x80)

func NewOnlineCommandParser

func NewOnlineCommandParser() *OnlineCommandParser

NewOnlineCommandParser creates a new online command parser

func (*OnlineCommandParser) Parse

func (p *OnlineCommandParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface Online command packet content structure: - Command Length: 1 byte - Server Flag: 4 bytes - Command Content: variable length (ASCII)

type Parser

type Parser interface {
	// ProtocolNumber returns the protocol number this parser handles
	ProtocolNumber() byte

	// Parse decodes the raw packet data into a typed Packet
	// The data parameter contains the full packet (including start bit, length, etc.)
	// The ctx parameter provides parsing context and configuration
	// Returns the parsed packet or an error if parsing fails
	Parse(data []byte, ctx Context) (packet.Packet, error)

	// Name returns the human-readable name of this parser
	Name() string
}

Parser is the interface that all protocol parsers must implement

type Registry

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

Registry maintains a mapping of protocol numbers to parsers

func DefaultRegistry

func DefaultRegistry() *Registry

DefaultRegistry returns the default global registry

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new parser registry

func (*Registry) Context

func (r *Registry) Context() Context

Context returns the current parser context

func (*Registry) Count

func (r *Registry) Count() int

Count returns the number of registered parsers

func (*Registry) Get

func (r *Registry) Get(protocolNum byte) (Parser, bool)

Get returns the parser for the given protocol number

func (*Registry) Has

func (r *Registry) Has(protocolNum byte) bool

Has returns true if a parser for the protocol number is registered

func (*Registry) List

func (r *Registry) List() []byte

List returns all registered protocol numbers

func (*Registry) MustRegister

func (r *Registry) MustRegister(p Parser)

MustRegister adds a parser and panics if registration fails

func (*Registry) Parse

func (r *Registry) Parse(protocolNum byte, data []byte) (packet.Packet, error)

Parse uses the appropriate parser to decode the packet

func (*Registry) Register

func (r *Registry) Register(p Parser) error

Register adds a parser to the registry Returns an error if a parser for the protocol is already registered

func (*Registry) SetContext

func (r *Registry) SetContext(ctx Context)

SetContext sets the parser context

func (*Registry) Unregister

func (r *Registry) Unregister(protocolNum byte)

Unregister removes a parser from the registry

type TimeCalibrationParser

type TimeCalibrationParser struct {
	BaseParser
}

TimeCalibrationParser parses time calibration request packets (Protocol 0x8A)

func NewTimeCalibrationParser

func NewTimeCalibrationParser() *TimeCalibrationParser

NewTimeCalibrationParser creates a new time calibration parser

func (*TimeCalibrationParser) Parse

func (p *TimeCalibrationParser) Parse(data []byte, ctx Context) (packet.Packet, error)

Parse implements Parser interface Time calibration packet has no content - it's just a request

Jump to

Keyboard shortcuts

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