Documentation
¶
Overview ¶
Package osmand is the OsmAnd HTTP location protocol: reading requests into track.Message and answering them. It is spoken by phone tracking apps such as Traccar Client (Android, iOS), OsmAnd's online tracking and the many driver and field-staff apps that use the same simple protocol, and by Traccar servers forwarding their positions.
The codec reads HTTP/1.x requests from a TCP (or TLS, so HTTPS) stream (keep-alive supported) and answers each one itself. Every request names its device, so each one carries Message.IMEI, and the first on a connection is a Login. Three request forms are read, told apart by their content, so one listener takes them all:
- Query or form parameters (GET with a query string, or a POST with an application/x-www-form-urlencoded body, which is what Traccar Client 9 sends): id or deviceid, lat and lon (or location=lat,lon), timestamp (Unix seconds or milliseconds, ISO 8601, or "yyyy-MM-dd HH:mm:ss" in UTC), valid, speed (knots by default, speedUnit), bearing or heading, altitude, accuracy (or hacc), hdop, sat, batt (percent), charge, alarm, driverUniqueId, ignition, cell (mcc,mnc,lac,cid[,rssi], repeatable) and wifi (mac,rssi, repeatable; both become the record's track.Network). Any other parameter is kept in status under "attributes".
- The JSON body of the older Traccar Client releases: {"device_id": …, "location": {"timestamp", "coords": {latitude, longitude, speed in m/s, heading, accuracy, altitude}, "battery": {level 0..1, is_charging}, "event", "is_moving", "odometer", "activity", "mock", "extras": {"alarm": …}}}; "location" may also be a list (batch).
- A Traccar server's position forwarding (forward.type json): a POST of {"position": {fixTime, latitude, longitude, altitude, speed in knots, course, accuracy, valid, outdated, attributes: {alarm, batteryLevel, ignition, motion, odometer, sat, hdop, …}}, "device": {uniqueId, name, …}}, and its event forwarding ({"event": {type, attributes}, "position", "device"}): an alarm event or an ignition, geofence or overspeed event becomes an alert. A forwarding server carries many devices, so each such request is answered with Connection: close and every connection carries one device.
Answers: 200 with an empty body for a request that is read; 400 for a request without a device id; 401 for one without the profile's token; 403 (Message.Reject) for a device the server refuses; 413 for a body over MaxBody (then the connection closes). Connection: close and HTTP/1.0 requests are honoured: the answer carries Connection: close and the connection ends after it.
Alarms: alarm=sos (Traccar Client's SOS button) is "emergency"; Traccar's other alarm names map to the shared alert types (overspeed over-speed, lowBattery low-battery, powerCut main-power-removed, hardBraking harsh-braking, geofenceEnter geofence-enter, tow towing, accident impact, jamming gnss-jamming, tampering tamper, …); an alarm without a shared type is the alert "device-alarm" with the app's word as its code.
Commands: the protocol has no way back to the phone or to the forwarding server, so every command, named or text, fails with track.ErrUnsupported and the reason.
A fix older than historyAfter when it arrives (the app's queue of positions recorded while offline) is marked Record.History, so a late backlog never becomes the device's latest position.
Public sources (own words here; Traccar's Java decoder was read as a reference for behaviour only):
https://www.traccar.org/osmand/ (parameters, time forms, knots)
https://github.com/traccar/traccar-client-sdk (HttpUploader.kt: the form POST of Traccar Client 9, speed converted to knots, batt in percent, alarm)
https://github.com/traccar/traccar-client (main_screen.dart: the SOS button sends alarm=sos)
https://github.com/traccar/traccar/blob/master/src/test/java/org/traccar/protocol/OsmAndProtocolDecoderTest.java (sample requests and JSON bodies, used as test vectors)
https://www.traccar.org/configuration-file/ and https://www.traccar.org/forward/ (forward.url, forward.type json, forward.header); the JSON field names are Traccar's Position and Device models (src/main/java/org/traccar/model).
The profile (NewCodec; Profile):
token: "" # shared token; when set, a request must carry it as ?token= or Authorization: Bearer (default none) speedUnit: kn # unit of the speed parameter: kn (default, Traccar Client), kmh, mps or mph historyAfter: 10m # a fix older than this on arrival is history (default 10m; 0 turns it off)
Index ¶
Constants ¶
const MaxBody = 64 << 10
MaxBody is the largest request body read (64 KiB).
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Profile ¶
type Profile struct {
// Token, when set, is a shared secret every request must carry, as the
// query or form parameter token or as Authorization: Bearer <token>;
// a request without it is answered 401 and not read. Put it in the
// app's server URL (https://host:5055/?token=…). Default none.
Token string `json:"token,omitempty"`
// SpeedUnit is the unit of the speed parameter: kn (default; Traccar
// Client sends knots), kmh, mps or mph. Apps where the operator writes
// the URL template (OsmAnd's online tracking) often send m/s. The JSON
// body form is always m/s and Traccar forwarding always knots.
SpeedUnit string `json:"speedUnit,omitempty"`
// HistoryAfter: a fix older than this when it arrives (the app's queue
// of positions recorded offline) is marked history (Record.History)
// instead of being the latest position. Default 10m; 0 turns it off.
HistoryAfter *track.Duration `json:"historyAfter,omitempty"`
}
Profile is the protocol settings.