Skip to content

Software flow

Our software is written in Zig, and code can be found in the src folder in the repository.

It has the following structure:

src
└── packages
├── printer
├── logger
└── startup

This is our main binary that runs on start. It is immutable, which means it can’t be changed. If you make any modifications to it, you have to rebuild the image for your update to be applicable (which is explained Run time explannation).

The following are the main functions of this binary:

  • wait for internet connection
  • wait for Raspberry PI to sync its time
  • check if Raspberry PI is authorized to make calls to server
  • if Raspberry PI is not registered to server then register it if server allows it
  • start a tmate session and send the link to server
  • check the latest release on disk and look for update
  • if update found then download it and apply it.
  • if fails then fallback to last release
  • if updated successfully then start the new release
  • clean up any old release
  • start the logger & printer binary in the background in a loop

We have a systemd service file that auto-starts the startup binary and redirects every log done by startup and its inner processes to the /opt/rpi-printer/rpi-printer.log file.

This binary is really simple. All it does is run forever and every 5 minutes it reads the log file, sends the logs to server, and then truncates the log file.

You can always see the logs by running

Terminal window
tail -n 20 -f /opt/rpi-printer/rpi-printer.log

This binary is responsible for all the printing functionality. It’s heavily dependent on the CUPS library to print PDFs. By default, we have disabled the CUPS and cups-browsed service, which means that it will not start CUPS on startup.

The following are the main functions of this binary:

  • clear the old CUPS queue and start the CUPS service
  • wait until we do not have a working rpi-printer connected
  • fetch print API endpoint from server and parse the JSON and start printing
  • for each item in array of JSON it does the following:
  • download the PDF from the link
  • get number of pages in the PDF
  • send print requests to CUPS for the PDF
  • wait for the print to finish
  • clear the queue

Packages contains all the code that is shared between the binaries

We have a master folder in the working directory that contains the initial binaries that comes with the image. it will have

  • startup
  • logger
  • printer

we also have a systemd service which will always start the startup binary from the master folder, even if we have new release or new updates for the binaries from Github but still it will always start startup from master.

the startup binary then starts the logger and printer from the latest release or if not release found then from the master folder.

the startup binary will never crash and if it crashes then it will restart by systemd automatically after a few seconds.