Host2Hub File Sync Protocol
Packet Format
A packet always starts with a colon (:
). This is to filter out unrelated data by e.g. micropython itself.
This is followed by a one byte long identifier specifying the packet type.
Without an argument
Without an argument, the packet is now closed with a closing bracket (]
).
With an argmument
Four digits will specify the length of the following base64 encoded argument.
Packet Types
The Host can send these Packets, the Hub must handle these packets:
Identifier | Type | Meaning | Argmuent |
Y | Actively Sent | Start SYNC Mode | |
D | Actively Sent | Ensure Directory exists | /path/to/dir |
F | Actively Sent | Ensure File has same Hash | /path/to/file SHA256 |
C | Response | CHUNK | 192 Bytes of file |
E | Response | End Of File | |
N | Actively Sent | End SYNC Mode | |
R | Actively Sent | Remove File or Directory | /path/to/file/or/diretory |
P | Actively Sent | Start Program (loop() from /__init__.py) | |
X | Actively Sent | Stop Program | |
& | Actively Sent | Reboot | |
= | Actively Sent | Sync Communication | timestamp |
> | Actively Sent | Enter REPL | |
$ | Actively Sent | Reset State |
The Hub can send these Packets, the Host must handle these packets:
Identifier | Type | Meaning | Argmuent |
K | Response | OK | |
= | Response | Sync Communication | given timestamp |
U | Response | Request File Contents | |
! | Actively Sent | Internal Error | traceback |
E | Actively Sent | User Error | traceback |
$ | Actively Sent | Please Reconnect | |
P | Actively Sent | json-encoded args & kwargs | |
B | Actively Sent | Blocking call, will be inresponsive | |
D | Actively Sent | No more blocking call, will be responsive again |
Packets written in italics are not yet documented below.
Examples
Sync all files
If a full tree needs to be synced to the hub, this is the right choice.
- Host sends
Y
to start SYNC Mode.
Hub responds withK
. - Here, Files and Directories are updated.
Refer to Update an individual file and Update an individual directory. - Host sends
N
to stop SYNC Mode.
Hub responds withK
.
Effect
Upon receiving N
, the Hub deletes all files and directories that were not updated since Y
was sent.
Update an individual file
- Host sends
F
. The argument is the file's path and its SHA256 Hash, seperated by a space. The file path needs to start with/
.
Hub responds withK
, or withU
. - If
U
was sent, Host repeatedly sendsC
with 192 bytes of the file as argmuent, orE
if EOF is reached.
Hub responds to each of these packets withK
. Hub can respond withU
toE
if the hash is still not matching.
Effect
The Hub stores the file under the given file path.
Update an individual directory
- Host sends
D
with the file path as argument. The file path needs to start with/
.
Hub responds withK
.
Effect
The Hub ensures the directory exists.
Delete an individual file or directory
- Host sends
R
with the file path as argument. The file path needs to start with/
.
Hub responds withK
.
Effect
The Hub deletes the file or directory found under the given file path.
Start the program
- Host sends
P
.
Hub responds withK
.
Effect
The Hub stops the task it created when it received P
the last time, if it exists.
The Hub starts the loop()
function it found in the /__init__.py
file by creating a new task using asyncio
.
Stop the program
- Host sends
X
.
Hub responds withK
.
Effect
The Hub stops the task it created when it received P
the last time, if it exists.
Sync connection
- Host sends
=
with some uniwue identifier, eg. the current timestamp, as argument.
Hub echos the packet.
Effect
The Host skips all packets until it receives the echo in order to ignore the hubs responses made before the connection was established. It is recommended to use this at the start of every connection.
Open REPL
- Host sends
>
.
Effect
The Hub stops all running tasks and returns control to the Micropython REPL.
This is to be preferred in comparison to sending Ctrl-C (which also works), as it makes sure the print()
function works as expected afterwards.
Reboot Hub
- Host sends
&
.
Effect
The Hub reboots.
Reset Connection
- Host sends
$
.
Hub responds withK
.
Effect
The Hub resets its state. It is recommended to send this when a new connection is made.
Keine Kommentare