Ribbit-Network / ribbit-network-frog-software

The software for the Ribbit Network Frog Sensor
https://www.ribbitnetwork.org/
MIT License
9 stars 8 forks source link

Upload Firmware Files from Github Actions to Flashing Branch #27

Open keenanjohnson opened 1 year ago

keenanjohnson commented 1 year ago

The current easiest way to flash a Frog Sensor Esp32 with the software uses the webpage stored in the gh-pages branch and esp-web-tools to flash a committed version of the firmware.

The firmware needs a few files:

What We do today

Currently, the Ribbit Network core team is building the firmware locally, running a python script to generate the manifest file, and then manually committing and git pushing the firmware to the gh-pages branch.

What We Should Do

The Github actions CI system is already building the main branch firmware on every commit.

We should modify the Github actions CI to automatically generate the manifest after a successful build and commit the new files the GH pages branch.

Python Script to generate the manifest

import base64
import hashlib
import json
import os
import shutil
import subprocess
import tempfile

def load_version():
    with open("__version__.py", encoding="utf-8") as f:
        data = f.read()

    ret = {}
    exec(data, None, ret)
    return ret

def hash_file(filepath):
    h = hashlib.sha256()
    with open(filepath, "rb") as f:
        while True:
            data = f.read(128 << 10)
            if data == b"":
                break
            h.update(data)

        hh = h.hexdigest()

    prefix, ext = filepath.rsplit(".", 1)
    new_filepath = "%s-%s.%s" % (prefix, hh[:8], ext)

    os.rename(filepath, new_filepath)
    return new_filepath

os.environ["SOURCE_DATE_EPOCH"] = (
    subprocess.check_output(["git", "log", "-1", "--format=%ct"])
    .decode("utf-8")
    .strip()
)

print(os.environ["SOURCE_DATE_EPOCH"])
build_path = os.getcwd()
print(build_path)

version = load_version()
with open("firmware/micropython.bin", "rb") as f:
    firmware_base64 = base64.b64encode(f.read()).decode("ascii")

firmware_path = os.path.join(build_path, "firmware")

os.chdir(build_path)

os.rename(
    os.path.join(firmware_path, "micropython.bin"),
    os.path.join(firmware_path, "ribbit-frog.bin"),
)

manifest = {
    "name": "Ribbit Frog Sensor",
    "version": version["version"],
    "builds": [
        {
            "chipFamily": "ESP32-S3",
            "parts": [
                {"path": hash_file("firmware/bootloader.bin"), "offset": 0},
                {
                    "path": hash_file("firmware/partition-table.bin"),
                    "offset": 32768,
                },
                {
                    "path": hash_file("firmware/ota_data_initial.bin"),
                    "offset": 53248,
                },
                {"path": hash_file("firmware/ribbit-frog.bin"), "offset": 65536},
            ],
        }
    ],
}

print(build_path)
with open(os.path.join(build_path, "manifest.json"), "w", encoding="utf-8") as f:
    json.dump(manifest, f)