h02

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 13 Imported by: 1 module, 1 package

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 h02 is the H02 tracker protocol of the Sinotrack ST-901, ST-902 and ST-906 and the many budget two-wheeler and car trackers built on the same firmware: frame reading, decoding into track.Message, and command framing. Our own implementation from the published protocol documents:

Traccar's H02 decoder and encoder (Apache-2.0) were read as a reference for behaviour seen in the field (NBR, LINK, the binary validity nibble, the S71 interval form); no code is taken from them.

Frames

Text frames are "*HQ,<id>,<type>,…#" (any two-letter maker in place of HQ, either case); <id> is the IMEI or a 10-digit device number, and every frame carries it, so the first frame identifies the tracker (there is no login exchange). Handled types:

V1 V2 V5 V6 V8 V19 VI1   position: hhmmss, A|V|B, ddmm.mmmm, N|S, dddmm.mmmm, E|W, knots, course, ddmmyy, status word, …
VP1                      short position (no time), or cells separated by Y when the second field is V
BC                       positions stored while out of coverage (history), separated by ';'
ALRM                     alarm: 1 SOS, 2 low battery, 3 geofence, with a position
V3 NBR                   mobile cells (the record's track.Network)
LINK                     signal, satellites and battery
XT HTBT V0 (empty type)  heartbeat; HTBT and V0 are echoed back, as devices expect
V4                       answer to a command: V4,<command>[,<result>][,<command time>,<time>,<position>…]
SMS                      a text answer, as the device would send by SMS

Any other type is skipped (track.Skip) with the warning unknownMessage. The binary form starts with '$' (0x24): a 32-byte record (45 bytes in a newer form whose last 13 bytes are not read) holding the 10-digit device number, time and date, latitude and longitude as packed BCD, a nibble of validity and hemisphere flags, speed (knots), course and the status word. The Cantrack document's Y-model record starts with 'Y' (0x59) and has the same 32-byte layout with two analog inputs (status analog1, analog2: a fuel level is the tank capacity times analog1/analog2) in place of the device number, and a user alarm flag and record number after the status word; it names no device, so it is read once an earlier frame did. Positions are not answered unless profile.ackPositions is set.

Both documents' uplink types are read; the 'X' binary record some firmware sends, which no document describes, is not.

Status word

Eight hexadecimal characters, 32 bits; both documents define each bit as active when it is 0 (statusActiveHigh flips that for firmware that sets them the other way). Counting bit 0 as the lowest bit of the word: bit 8 door open, bit 9 armed, bit 10 ACC off (so ignition is on while bit 10 is inactive), bit 19 running on the backup battery, bit 27 fuel or power cut by the relay. Alarm bits become alerts once, when they become active (and again on each new connection while they stay active):

0 vibration (theft)   1 emergency (robbery)   2 over-speed   3 tamper (illegal ignition)
4 geofence-enter      7 geofence-exit         11 vibration   12 low-battery
15 over-speed         18 emergency (SOS)      19 main-power-removed   20 main-power-removed

main-power-restored and low-battery-restored follow when those bits clear, and ignition-on / ignition-off when bit 10 changes. profile.alarmBits adds, replaces or turns off ("") the alert of any bit.

Commands

{"text": "<command>"} is sent as "*HQ,<id>,<command>#" (or as written when it starts with '*'). The named commands (track.Named; DefaultNamed) map to the HuaSunTeK V1.0.5 document's commands:

engineStop    *HQ,<id>,S20,<hhmmss>,1,1#   (static cut: relay held)
engineResume  *HQ,<id>,S20,<hhmmss>,1,0#
locate        *HQ,<id>,CR#
reboot        *HQ,<id>,CQ#                 (Cantrack firmware: R1)
interval      *HQ,<id>,XT,<seconds>#       (5 to 3600 s while driving; Cantrack: D1, Traccar: S71,22)

profile.namedCommands replaces the text after the id for firmware that differs; the codec puts the current UTC time after the command word of any numbered command (S20, S71, D1, R1, …), as both documents show, so a template is written without it ("D1,{seconds}" becomes "*HQ,<id>,D1,<hhmmss>,600#").

The device answers with V4 naming the command, and for commands that carry a time also echoing that time. The answer completes the command; its result fails it when it is a hexadecimal byte below 80 (00 not done, 01 not supported, 02 out of range, …) or holds a failure word (ERROR). An answer is matched to its command (the codec is a track.Correlator) by command word and echoed time (answerMatch "commandTime"), so a late answer cannot complete a later command; "command" matches by the word only, for firmware that does not echo the time, and "none" takes any answer (a server should then hold the next command until a timed-out command's answer came or its time passed).

Many Sinotrack units send over UDP as well as TCP; a datagram may hold several frames, which Decode reads one after the other, and commands go back to the tracker's last UDP source.

The profile (NewCodec; Profile):

timezone: UTC                 # zone of the device's clock (default UTC)
statusActiveHigh: false       # default false: status bits are active when 0, as documented
alarmBits: {"26": tamper}     # extra or changed status-word bits → alert type ("" turns a bit off)
ackPositions: false           # default false; true answers positions (V4,V1 and R12), for firmware that waits for it
answerMatch: commandTime      # commandTime (default), command or none
failureWords: [error, fail]   # default; an answer result holding one fails the command
marker: HQ                    # default HQ; the maker field of commands sent
namedCommands: {reboot: "R1", interval: "D1,{seconds}"}   # text after the id per named action ("" turns one off)

Index

Constants

This section is empty.

Variables

View Source
var DefaultNamed = map[string]string{
	track.ActionEngineStop:   "S20,1,1",
	track.ActionEngineResume: "S20,1,0",
	track.ActionLocate:       "CR",
	track.ActionReboot:       "CQ",
	track.ActionInterval:     "XT,{seconds}",
}

DefaultNamed are the named commands of the HuaSunTeK V1.0.5 document, the text after the id (the codec adds the time to numbered commands).

View Source
var Interval = track.IntervalRange{Min: 5, Max: 3600}

Interval bounds the interval command (the V1.0.5 document's XT range).

View Source
var NamedActions = track.Actions

NamedActions are the named commands H02 has: all of them.

Functions

func NewCodec

func NewCodec(raw json.RawMessage) (func() track.Codec, error)

NewCodec validates a profile (empty for the defaults) and returns a factory of codecs, one per connection.

func ReadFrame

func ReadFrame(r *bufio.Reader) (string, error)

ReadFrame reads one text frame ("*…#", returned without '*' and '#') or binary record ('$' and the record) from r, for simulators and tools.

Types

type Profile

type Profile struct {
	// Timezone is the zone of the device's clock (default UTC).
	Timezone string `json:"timezone,omitempty"`
	// StatusActiveHigh (default false) reads the status word's bits as
	// active when 1, for firmware that inverts the documented polarity.
	StatusActiveHigh bool `json:"statusActiveHigh,omitempty"`
	// AlarmBits maps a status-word bit ("0" to "31") to an alert type, or
	// to "" to raise none for it; it is applied over the defaults.
	AlarmBits map[string]string `json:"alarmBits,omitempty"`
	// AckPositions (default false) answers position frames (V4,V1 for text,
	// R12 for binary), for firmware that resends until answered.
	AckPositions bool `json:"ackPositions,omitempty"`
	// AnswerMatch is how a V4 answer is matched to its command:
	// "commandTime" (default) by command word and echoed time, "command"
	// by the word only, "none" not at all.
	AnswerMatch string `json:"answerMatch,omitempty"`
	// FailureWords are the words of a V4 result that fail the command
	// (default error, fail).
	FailureWords []string `json:"failureWords,omitempty"`
	// Marker is the maker field of commands sent (default HQ).
	Marker string `json:"marker,omitempty"`
	// NamedCommands replaces the text after the id of a named command
	// (with {seconds} and {imei}); "" turns the action off.
	NamedCommands track.NamedTemplates `json:"namedCommands,omitempty"`
}

Profile is the manufacturer-variation settings.

Jump to

Keyboard shortcuts

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