Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload Firmware Files from Github Actions to Flashing Branch #27

Open
keenanjohnson opened this issue Jan 7, 2023 · 0 comments
Open
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@keenanjohnson
Copy link
Member

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)
@keenanjohnson keenanjohnson added the enhancement New feature or request label Jan 7, 2023
@keenanjohnson keenanjohnson added the good first issue Good for newcomers label Jan 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
No open projects
Status: No status
Development

No branches or pull requests

1 participant