How a pc talks to the Smart Beacon gateway over USB.
Introduction
The gateway scans for tags and reports a change of their state to a pc over a USB CDC serial port. Everything on the link is a frame: a small binary message with an opcode, a sequence number, a payload and a checksum.
Requests
The pc sends a request, the gateway answers with a response or with an error response. A response carries the seq of its request.
Events
The gateway sends tag events on its own, whenever the state of a tag changes. Events have no response.
Settings
A set request stores the setting in the gateway and applies it at once. The response carries the values in effect.
Open the port at any baud rate; the setting has no effect on a USB CDC port. Assert DTR, as most serial libraries do by default.
All multi-byte values are little-endian; signed values are two's complement.
The gateway has no clock. The pc stamps an event when it arrives.
A seq gap in the events shows that events were dropped; the statistics count them.
The Tags and Settings tabs of this tool use exactly these messages.
USB framing
Defines how frames are delimited, encoded and protected on the USB CDC link between the gateway and the pc.
A set request carries the same payload layout as the matching response.
The response to a set request carries the values in effect after the change.
A response carries the seq of its request.
Multi-byte values are little-endian; signed values are two's complement.
Enum values not listed in a table are invalid.
Request handling
The pc keeps at most one request outstanding: it sends the next request after the response or the error response of the previous one has arrived. The gateway is specified for that case only.
A request has failed when no frame with its seq arrives within 1 s; for the update requests 0x40 to 0x45 the limit is 5 s, because they write flash. The reference tools use these limits.
After a timeout the pc may send the same get or set request again with a new seq. A late response to the earlier try carries the old seq and is ignored.
Tag events can arrive at any time, also between a request and its response. The pc recognises a response by its seq and its opcode, and handles every other frame as an event.
Gateway behaviour
Answers every request with its response opcode, or with the error response.
Validates the full payload of a set request before applying any part of it.
Stores accepted settings in nvm and applies them immediately; when a setting needs the scanner to stop, the gateway stops and starts the scanner itself without restarting the mcu.
Emits tag events without being asked.
Error response (0x0F)
field
type
description
error code
uint8
reason the request failed; see error codes
error code
description
0x01
unknown opcode
0x02
payload length does not match the opcode
0x03
a value is out of range or not a valid enum value
0x04
storing the setting in nvm failed
0x05
gateway cannot process the request now
0x06
image rejected: transfer hash, header, product, size or firmware hash is invalid
0x07
block offset does not match the next expected offset
Tag event
Message sent by the gateway when the state of a tag differs from its last reported state.
Summary
opcode
0x10
direction
gateway to pc
kind
event; no response
payload size
7 bytes
Payload
tag idtag id4 bytes
sensorsensor1 byte
rssirssi1 byte
batterybattery1 byte
offset
size
field
type
description
0
4
tag id
uint32
identity of the tag
4
1
sensor
enum
see sensor
5
1
rssi
int8
signal strength in dBm at the gateway
6
1
battery
uint8
battery level of the tag in percent, 0 to 100; 0xFF means unknown
Sensor
value
description
0x00
closed
0x01
open
Gateway behaviour
Sends an event the first time a tag is seen after boot, whatever its sensor: the gateway has no earlier sensor to compare with.
Counts a tag as seen once an advertisement of it has passed the scan filter and its sensor is decoded.
Sends a further event only when the sensor differs from the last reported sensor of that tag.
Reports the rssi of the advertisement that carried the change.
Reports battery 0xFF when the tag table holds no battery percentage.
Increments the event seq once per event.
Device info
Messages that report the identity and versions of the gateway.
Summary
request opcode
0x02, pc to gateway, empty payload
response opcode
0x03, gateway to pc
response payload size
23 bytes
Response payload
proto majprotocol major1 byte
proto minprotocol minor1 byte
fw majfirmware major1 byte
fw minfirmware minor1 byte
fw patfirmware patch1 byte
boot majbootloader major1 byte
boot minbootloader minor1 byte
boot patbootloader patch1 byte
reservedreserved8 bytes
ble addrble address6 bytes
addr typeble address type1 byte
offset
size
field
type
description
0
1
protocol major
uint8
major version of this protocol
1
1
protocol minor
uint8
minor version of this protocol
2
1
firmware major
uint8
major version of the firmware
3
1
firmware minor
uint8
minor version of the firmware
4
1
firmware patch
uint8
patch version of the firmware
5
1
bootloader major
uint8
major version of the bootloader; 0 when the bootloader did not report one
6
1
bootloader minor
uint8
minor version of the bootloader
7
1
bootloader patch
uint8
patch version of the bootloader
8
8
reserved
bytes
zero
16
6
ble address
bytes
bluetooth address of the gateway radio, most significant octet first
22
1
ble address type
enum
see ble address type
Ble address type
value
description
0x00
public
0x01
random static
Gateway behaviour
Increments the protocol minor version for additions that keep existing messages valid, and the major version for changes that break them.
Reports the version that the bootloader published before it started the firmware, or 0 for all three parts after a power-on reset without a bootloader, see Firmware Update.
Reports the bluetooth address and its type from the device address registers of the mcu.
Sets the two most significant bits of a random static ble address, as the Bluetooth specification requires for that type.
Statistics
Messages that report the counters of the gateway.
Summary
request opcode
0x04, pc to gateway, empty payload
response opcode
0x05, gateway to pc
response payload size
18 bytes
Response payload
uptimeuptime4 bytes
advertisements seenadvertisements seen4 bytes
events sentevents sent4 bytes
events droppedevents dropped4 bytes
known tagsknown tags2 bytes
offset
size
field
type
description
0
4
uptime
uint32
seconds since boot
4
4
advertisements seen
uint32
advertisements that passed the scan filter
8
4
events sent
uint32
tag events written to usb
12
4
events dropped
uint32
tag events discarded because the usb output was full
16
2
known tags
uint16
tags currently held in the tag table
Gateway behaviour
Keeps all counters in ram only; every counter starts at 0 after boot.
Counters wrap at their maximum value.
Increments events dropped for every tag event that is not written, so each drop shows as a seq gap at the pc.
Scan filter
Messages that read and write which advertisements the gateway accepts.
Summary
get opcode
0x20, pc to gateway, empty payload
response opcode
0x21, gateway to pc
set opcode
0x22, pc to gateway; answered by 0x21 with the filter in effect after the change
payload size
13 bytes
Payload
mac prefix lengthmac prefix length1 byte
mac prefixmac prefix3 bytes
name prefix lengthname prefix length1 byte
name prefixname prefix8 bytes
offset
size
field
type
description
0
1
mac prefix length
uint8
number of mac prefix bytes that must match; 0 to 3, 0 accepts every address
1
3
mac prefix
bytes
most significant octets of the address, first octet first; unused bytes are zero
4
1
name prefix length
uint8
number of name prefix characters that must match; 0 to 8, 0 accepts every name
5
8
name prefix
ascii
start of the tag name; unused bytes are zero
Defaults
field
value
mac prefix length
1
mac prefix
BC 00 00
name prefix length
4
name prefix
TW11
Gateway behaviour
Accepts an advertisement only when its address matches the mac prefix and its decoded name matches the name prefix.
Replies with error code 0x03 when a length exceeds its maximum.
Keeps the known tag table when the filter changes.
Scan parameters
Messages that read and write how the gateway scans for advertisements.
Summary
get opcode
0x23, pc to gateway, empty payload
response opcode
0x24, gateway to pc
set opcode
0x25, pc to gateway; answered by 0x24 with the parameters in effect after the change
payload size
3 bytes
Payload
channelchannel1 byte
dwell timedwell time2 bytes
offset
size
field
type
description
0
1
channel
enum
see channel
1
2
dwell time
uint16
milliseconds on a channel before the next one when the channel is all; 2 to 1000
Channel
value
description
0x00
all advertising channels
0x01
channel 37
0x02
channel 38
0x03
channel 39
Defaults
field
value
channel
0x00
dwell time
10 ms
Gateway behaviour
Scans continuously on the selected channel or channels.
Changes channel every dwell time when the channel is all, and ignores the dwell time otherwise.
Replies with error code 0x03 when the channel is not listed or the dwell time is outside 2 to 1000 ms, and with 0x02 when the payload is not 3 bytes.
Reads settings stored by a gateway of protocol 1.3, which held the channel only, with the default dwell time.
Stops and restarts the scanner to apply a change, without restarting the mcu.
Reporting
Messages that read and write which tag events the gateway reports.
Summary
get opcode
0x26, pc to gateway, empty payload
response opcode
0x27, gateway to pc
set opcode
0x28, pc to gateway; answered by 0x27 with the settings in effect after the change
payload size
1 byte
Payload
rssi thresholdrssi threshold1 byte
offset
size
field
type
description
0
1
rssi threshold
int8
advertisements weaker than this value in dBm are ignored
Defaults
field
value
rssi threshold
-127
Gateway behaviour
Ignores advertisements whose rssi is below the threshold before they reach the tag table.
Accepts every advertisement at the default value.
Firmware update
Replaces the firmware of the gateway over the USB connection: the pc sends the new image in blocks, the gateway checks it and restarts into it.
Messages
Update begin (0x40, pc to device)
image sizeimage size4 bytes
hashhash32 bytes
offset
size
field
type
description
0
4
image size
uint32
size of the image file in bytes, header included
4
32
hash
bytes
sha-256 of the image file
Update block (0x42, pc to device)
offsetoffset4 bytes
datadatan bytes
offset
size
field
type
description
0
4
offset
uint32
offset of the first data byte in the image file
4
n
data
bytes
image data; n is 1 to the maximum block size of the response, 56
Update end (0x43), update activate (0x44) and update abort (0x45)
image rejected: transfer hash, header, product, size or firmware hash is invalid
0x07
block offset does not match the next expected offset
Connection loss
A lost connection, a closed port or a removed cable before the activate request returns the device to idle. The data received so far is discarded, the running firmware keeps scanning and nothing restarts. The pc starts again with update begin.
A request that gets no response within 5 s is not repeated. The pc sends update abort and starts again with update begin, because it cannot know whether the device processed the lost request.
After the activate response the device restarts by itself, so the connection is lost on purpose. The pc reconnects and reads Device Info; the reference tool waits up to 40 s for the board to reappear.
A power loss while the bootloader replaces the firmware is repaired at the next start: the bootloader repeats the interrupted replacement and the board always starts a complete image.
A new firmware image that does not confirm itself within 10 s is replaced by the previous image at the next restart, and update begin is refused until the trial has ended.
Procedure
The pc sends update begin and reads the maximum block size.
The pc sends update blocks until the whole image is sent.
The pc sends update end; the device answers with state verified or error 0x06.
The pc sends update activate; the device answers and restarts.
The pc reconnects and reads Device Info to confirm the new version. A firmware image confirms itself after 10 s.