Direkt zum Hauptinhalt

Host2Hub File Sync Protocol

Packet Format

A packet always starts with a colon (:). This is to filter out 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 exist /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 Print 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 written in italics are not yet documented below.

Routines

Sync all files

If a full tree needs to be synced to the hub, this is the right choice.

  1. Host sends Y to start SYNC Mode.
    Hub responds with with K.
  2. Here, Files and Directories are updated.
    Refer to Update an individual file and Update an individual directory.
  3. Host sends sends N to stop SYNC Mode.
    Hub Hub responds with K.
Effect

Upon receiving N, the Hub deletes all files and directories that were not updated since Y was sent.

Update an individual file

  1. 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 with /.
    Hub responds with with K,  or with with U.
  2. If U was sent, Host repeatedly sends C with 192 bytes of the file as argmuent, or E if EOF is reached.
    Hub responds to each of these packets with K. Hub can respond with with U to E if the hash is still not matching.
Effect

The Hub stores the file under the given file path.

Update an individual directory

  1. Host sends D with the file path as argument. The file path needs to start with with /.
    Hub responds with with K.
Effect

The Hub ensures the directory exists.

Delete an individual file or directory

  1. Host sends R with the file path as argument. The file path needs to start with with /.
    Hub responds with with K.
Effect

The Hub deletes the file or directory found under the given file path.

Start the program

  1. Host sends P.
    Hub responds with with K.
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

  1. Host sends X.
    Hub responds with with K.
Effect

The Hub stops the task it created when it received P the last time, if it exists.

Sync connection

  1. 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

  1. Host sends 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 the print() function works as expected afterwards.

Reboot Hub

  1. Host sends sends &.
Effect

The Hub reboots.

Reset Connection

  1. Host sends $.
    Hub responds with with K.
Effect

The Hub resets its state. It is recommended to send this when a new connection is made.