🚀 Builds with new ESP32-Sveltekit template
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
# ESP32 SvelteKit --
|
||||
#
|
||||
# A simple, secure and extensible framework for IoT projects for ESP32 platforms
|
||||
# with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
|
||||
# https://github.com/theelims/ESP32-sveltekit
|
||||
#
|
||||
# Copyright (C) 2018 - 2023 rjwats
|
||||
# Copyright (C) 2023 theelims
|
||||
# Copyright (C) 2023 Maxtrium B.V. [ code available under dual license ]
|
||||
#
|
||||
# All Rights Reserved. This software may be modified and distributed under
|
||||
# the terms of the LGPL v3 license. See the LICENSE file for details.
|
||||
|
||||
from pathlib import Path
|
||||
from shutil import copyfileobj
|
||||
import os
|
||||
import gzip
|
||||
import mimetypes
|
||||
import glob
|
||||
from datetime import datetime
|
||||
|
||||
Import("env")
|
||||
|
||||
output_file = env["PROJECT_DIR"] + "/lib/ESP32-sveltekit/WWWData.h"
|
||||
source_www_dir = env["PROJECT_DIR"] + "/../app2/src"
|
||||
build_www_dir = env["PROJECT_DIR"] + "/../app2/build"
|
||||
www_dir = "www"
|
||||
|
||||
def find_latest_timestamp_for_app():
|
||||
list_of_files = glob.glob(source_www_dir+'/**/*', recursive=True)
|
||||
latest_file = max(list_of_files, key=os.path.getmtime)
|
||||
return os.path.getmtime(latest_file)
|
||||
|
||||
def should_regenerate_output_file():
|
||||
if not flag_exists("EMBED_WWW") or not os.path.exists(output_file):
|
||||
return True
|
||||
last_source_change = find_latest_timestamp_for_app()
|
||||
last_build = os.path.getmtime(output_file)
|
||||
|
||||
print(f'Newest interface file: {datetime.fromtimestamp(last_source_change):%Y-%m-%d %H:%M:%S}, WWW Outputfile: {datetime.fromtimestamp(last_build):%Y-%m-%d %H:%M:%S}')
|
||||
|
||||
if (last_build < last_source_change):
|
||||
print("Svelte source files are updated. Need to regenerate.")
|
||||
return True
|
||||
print("Current outputfile is O.K. No need to regenerate.")
|
||||
return False
|
||||
|
||||
|
||||
def gzip_file(file):
|
||||
with open(file, 'rb') as f_in:
|
||||
with gzip.open(file + '.gz', 'wb') as f_out:
|
||||
copyfileobj(f_in, f_out)
|
||||
os.remove(file)
|
||||
|
||||
def is_compressed_filetype(filetype):
|
||||
compressed_types = ['zip', 'gz', 'rar', '7z', 'jpg', 'jpeg', 'png', 'mp4', 'mp3']
|
||||
return filetype.lower() in compressed_types
|
||||
|
||||
def flag_exists(flag):
|
||||
buildFlags = env.ParseFlags(env["BUILD_FLAGS"])
|
||||
for define in buildFlags.get("CPPDEFINES"):
|
||||
if (define == flag or (isinstance(define, list) and define[0] == flag)):
|
||||
return True
|
||||
|
||||
def build_progmem():
|
||||
mimetypes.init()
|
||||
with open(output_file, "w") as progmem:
|
||||
progmem.write("#include <functional>\n")
|
||||
progmem.write("#include <Arduino.h>\n\n")
|
||||
|
||||
assetMap = {}
|
||||
|
||||
# Iterate over all files in the build directory
|
||||
for idx, path in enumerate(Path(www_dir).rglob("*.*")):
|
||||
asset_path = path.relative_to(www_dir).as_posix()
|
||||
print(f"Converting {asset_path}")
|
||||
|
||||
asset_var = f'WEB_ASSET_DATA_{idx}'
|
||||
filetype = asset_path.split('.')[-1]
|
||||
asset_mime, _ = mimetypes.guess_type(asset_path)
|
||||
asset_mime = asset_mime if asset_mime else f'application/octet-stream'
|
||||
|
||||
progmem.write('// ' + str(asset_path) + '\n')
|
||||
progmem.write('const uint8_t ' + asset_var + '[] = {\n ')
|
||||
|
||||
# Open path as binary file, compress and read into byte array
|
||||
size = 0
|
||||
with open(path, "rb") as f:
|
||||
file_data = f.read()
|
||||
if not is_compressed_filetype(filetype):
|
||||
file_data = gzip.compress(file_data)
|
||||
for byte in file_data:
|
||||
if not (size % 16):
|
||||
progmem.write('\n\t')
|
||||
|
||||
progmem.write(f"0x{byte:02X},")
|
||||
size += 1
|
||||
|
||||
progmem.write('\n};\n\n')
|
||||
assetMap[asset_path] = { "name": asset_var, "mime": asset_mime, "size": size }
|
||||
|
||||
progmem.write('typedef std::function<void(const String& uri, const String& contentType, const uint8_t * content, size_t len)> RouteRegistrationHandler;\n\n')
|
||||
progmem.write('class WWWData {\n')
|
||||
progmem.write('\tpublic:\n')
|
||||
progmem.write('\t\tstatic void registerRoutes(RouteRegistrationHandler handler) {\n')
|
||||
|
||||
for asset_path, asset in assetMap.items():
|
||||
progmem.write(f'\t\t\thandler("/{asset_path}", "{asset["mime"]}", {asset["name"]}, {asset["size"]});\n')
|
||||
|
||||
progmem.write('\t\t}\n')
|
||||
progmem.write('};\n\n')
|
||||
|
||||
def build_webapp():
|
||||
os.chdir("../app2")
|
||||
print("Building app with pnpm")
|
||||
env.Execute("pnpm install")
|
||||
env.Execute("pnpm run build")
|
||||
os.chdir("../esp32")
|
||||
|
||||
def embed_webapp():
|
||||
if flag_exists("EMBED_WWW"):
|
||||
print("Converting interface to PROGMEM")
|
||||
build_progmem()
|
||||
return
|
||||
|
||||
print("running: build_interface.py")
|
||||
if (should_regenerate_output_file()):
|
||||
build_webapp()
|
||||
embed_webapp()
|
||||
@@ -0,0 +1,209 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# modified ESP32 x509 certificate bundle generation utility to run with platformio
|
||||
#
|
||||
# Converts PEM and DER certificates to a custom bundle format which stores just the
|
||||
# subject name and public key to reduce space
|
||||
#
|
||||
# The bundle will have the format: number of certificates; crt 1 subject name length; crt 1 public key length;
|
||||
# crt 1 subject name; crt 1 public key; crt 2...
|
||||
#
|
||||
# SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from __future__ import with_statement
|
||||
|
||||
from pathlib import Path
|
||||
import os
|
||||
import struct
|
||||
import sys
|
||||
import requests
|
||||
from io import open
|
||||
|
||||
Import("env")
|
||||
|
||||
try:
|
||||
from cryptography import x509
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
except ImportError:
|
||||
env.Execute("$PYTHONEXE -m pip install cryptography")
|
||||
|
||||
|
||||
ca_bundle_bin_file = 'x509_crt_bundle.bin'
|
||||
mozilla_cacert_url = 'https://curl.se/ca/cacert.pem'
|
||||
adafruit_cacert_url = 'https://raw.githubusercontent.com/adafruit/certificates/main/data/roots.pem'
|
||||
certs_dir = Path("./ssl_certs")
|
||||
binary_dir = Path("./src/certs")
|
||||
|
||||
quiet = False
|
||||
|
||||
def download_cacert_file(source):
|
||||
if source == "mozilla":
|
||||
response = requests.get(mozilla_cacert_url)
|
||||
elif source == "adafruit":
|
||||
response = requests.get(adafruit_cacert_url)
|
||||
else:
|
||||
raise InputError('Invalid certificate source')
|
||||
|
||||
if response.status_code == 200:
|
||||
|
||||
# Ensure the directory exists, create it if necessary
|
||||
os.makedirs(certs_dir, exist_ok=True)
|
||||
|
||||
# Generate the full path to the output file
|
||||
output_file = os.path.join(certs_dir, "cacert.pem")
|
||||
|
||||
# Write the certificate bundle to the output file with utf-8 encoding
|
||||
with open(output_file, "w", encoding="utf-8") as f:
|
||||
f.write(response.text)
|
||||
|
||||
status('Certificate bundle downloaded to: %s' % output_file)
|
||||
else:
|
||||
status('Failed to fetch the certificate bundle.')
|
||||
|
||||
def status(msg):
|
||||
""" Print status message to stderr """
|
||||
if not quiet:
|
||||
critical(msg)
|
||||
|
||||
|
||||
def critical(msg):
|
||||
""" Print critical message to stderr """
|
||||
sys.stderr.write('SSL Cert Store: ')
|
||||
sys.stderr.write(msg)
|
||||
sys.stderr.write('\n')
|
||||
|
||||
|
||||
class CertificateBundle:
|
||||
def __init__(self):
|
||||
self.certificates = []
|
||||
self.compressed_crts = []
|
||||
|
||||
if os.path.isfile(ca_bundle_bin_file):
|
||||
os.remove(ca_bundle_bin_file)
|
||||
|
||||
def add_from_path(self, crts_path):
|
||||
|
||||
found = False
|
||||
for file_path in os.listdir(crts_path):
|
||||
found |= self.add_from_file(os.path.join(crts_path, file_path))
|
||||
|
||||
if found is False:
|
||||
raise InputError('No valid x509 certificates found in %s' % crts_path)
|
||||
|
||||
def add_from_file(self, file_path):
|
||||
try:
|
||||
if file_path.endswith('.pem'):
|
||||
status('Parsing certificates from %s' % file_path)
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
crt_str = f.read()
|
||||
self.add_from_pem(crt_str)
|
||||
return True
|
||||
|
||||
elif file_path.endswith('.der'):
|
||||
status('Parsing certificates from %s' % file_path)
|
||||
with open(file_path, 'rb') as f:
|
||||
crt_str = f.read()
|
||||
self.add_from_der(crt_str)
|
||||
return True
|
||||
|
||||
except ValueError:
|
||||
critical('Invalid certificate in %s' % file_path)
|
||||
raise InputError('Invalid certificate')
|
||||
|
||||
return False
|
||||
|
||||
def add_from_pem(self, crt_str):
|
||||
""" A single PEM file may have multiple certificates """
|
||||
|
||||
crt = ''
|
||||
count = 0
|
||||
start = False
|
||||
|
||||
for strg in crt_str.splitlines(True):
|
||||
if strg == '-----BEGIN CERTIFICATE-----\n' and start is False:
|
||||
crt = ''
|
||||
start = True
|
||||
elif strg == '-----END CERTIFICATE-----\n' and start is True:
|
||||
crt += strg + '\n'
|
||||
start = False
|
||||
self.certificates.append(x509.load_pem_x509_certificate(crt.encode(), default_backend()))
|
||||
count += 1
|
||||
if start is True:
|
||||
crt += strg
|
||||
|
||||
if count == 0:
|
||||
raise InputError('No certificate found')
|
||||
|
||||
status('Successfully added %d certificates' % count)
|
||||
|
||||
def add_from_der(self, crt_str):
|
||||
self.certificates.append(x509.load_der_x509_certificate(crt_str, default_backend()))
|
||||
status('Successfully added 1 certificate')
|
||||
|
||||
def create_bundle(self):
|
||||
# Sort certificates in order to do binary search when looking up certificates
|
||||
self.certificates = sorted(self.certificates, key=lambda cert: cert.subject.public_bytes(default_backend()))
|
||||
|
||||
bundle = struct.pack('>H', len(self.certificates))
|
||||
|
||||
for crt in self.certificates:
|
||||
""" Read the public key as DER format """
|
||||
pub_key = crt.public_key()
|
||||
pub_key_der = pub_key.public_bytes(serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo)
|
||||
|
||||
""" Read the subject name as DER format """
|
||||
sub_name_der = crt.subject.public_bytes(default_backend())
|
||||
|
||||
name_len = len(sub_name_der)
|
||||
key_len = len(pub_key_der)
|
||||
len_data = struct.pack('>HH', name_len, key_len)
|
||||
|
||||
bundle += len_data
|
||||
bundle += sub_name_der
|
||||
bundle += pub_key_der
|
||||
|
||||
return bundle
|
||||
|
||||
class InputError(RuntimeError):
|
||||
def __init__(self, e):
|
||||
super(InputError, self).__init__(e)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
bundle = CertificateBundle()
|
||||
|
||||
try:
|
||||
cert_source = env.GetProjectOption("board_ssl_cert_source")
|
||||
|
||||
if (cert_source == "mozilla" or cert_source == "adafruit"):
|
||||
download_cacert_file(cert_source)
|
||||
bundle.add_from_file(os.path.join(certs_dir, "cacert.pem"))
|
||||
elif (cert_source == "folder"):
|
||||
bundle.add_from_path(certs_dir)
|
||||
except ValueError:
|
||||
critical('Invalid configuration option: use \'board_ssl_cert_source\' parameter in platformio.ini' )
|
||||
raise InputError('Invalid certificate')
|
||||
|
||||
status('Successfully added %d certificates in total' % len(bundle.certificates))
|
||||
|
||||
crt_bundle = bundle.create_bundle()
|
||||
|
||||
# Ensure the directory exists, create it if necessary
|
||||
os.makedirs(binary_dir, exist_ok=True)
|
||||
|
||||
output_file = os.path.join(binary_dir, ca_bundle_bin_file)
|
||||
|
||||
with open(output_file, 'wb') as f:
|
||||
f.write(crt_bundle)
|
||||
|
||||
status('Successfully created %s' % output_file)
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except InputError as e:
|
||||
print(e)
|
||||
sys.exit(2)
|
||||
@@ -0,0 +1,89 @@
|
||||
"""
|
||||
EMS-ESP - https://github.com/emsesp/EMS-ESP
|
||||
Copyright 2020-2023 Paul Derbyshire
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
|
||||
import shutil
|
||||
import re
|
||||
import os
|
||||
Import("env")
|
||||
import hashlib
|
||||
|
||||
|
||||
OUTPUT_DIR = "build{}".format(os.path.sep)
|
||||
|
||||
def readFlag(flag):
|
||||
buildFlags = env.ParseFlags(env["BUILD_FLAGS"])
|
||||
# print(buildFlags.get("CPPDEFINES"))
|
||||
for define in buildFlags.get("CPPDEFINES"):
|
||||
if (define == flag or (isinstance(define, list) and define[0] == flag)):
|
||||
# print("Found "+flag+" = "+define[1])
|
||||
# strip quotes ("") from define[1]
|
||||
cleanedFlag = re.sub(r'^"|"$', '', define[1])
|
||||
return cleanedFlag
|
||||
return None
|
||||
|
||||
|
||||
def bin_copy(source, target, env):
|
||||
|
||||
# get the build info
|
||||
app_version = readFlag("APP_VERSION")
|
||||
app_name = readFlag("APP_NAME")
|
||||
build_target = env.get('PIOENV')
|
||||
|
||||
# print information's
|
||||
print("App Version: " + app_version)
|
||||
print("App Name: " + app_name)
|
||||
print("Build Target: " + build_target)
|
||||
|
||||
# convert . to - so Windows doesn't complain
|
||||
variant = app_name + "_" + build_target + "_" + app_version.replace(".", "-")
|
||||
|
||||
# check if output directories exist and create if necessary
|
||||
if not os.path.isdir(OUTPUT_DIR):
|
||||
os.mkdir(OUTPUT_DIR)
|
||||
|
||||
for d in ['firmware']:
|
||||
if not os.path.isdir("{}{}".format(OUTPUT_DIR, d)):
|
||||
os.mkdir("{}{}".format(OUTPUT_DIR, d))
|
||||
|
||||
# create string with location and file names based on variant
|
||||
bin_file = "{}firmware{}{}.bin".format(OUTPUT_DIR, os.path.sep, variant)
|
||||
md5_file = "{}firmware{}{}.md5".format(OUTPUT_DIR, os.path.sep, variant)
|
||||
|
||||
# check if new target files exist and remove if necessary
|
||||
for f in [bin_file]:
|
||||
if os.path.isfile(f):
|
||||
os.remove(f)
|
||||
|
||||
# check if new target files exist and remove if necessary
|
||||
for f in [md5_file]:
|
||||
if os.path.isfile(f):
|
||||
os.remove(f)
|
||||
|
||||
print("Renaming file to "+bin_file)
|
||||
|
||||
# copy firmware.bin to firmware/<variant>.bin
|
||||
shutil.copy(str(target[0]), bin_file)
|
||||
|
||||
with open(bin_file,"rb") as f:
|
||||
result = hashlib.md5(f.read())
|
||||
print("Calculating MD5: "+result.hexdigest())
|
||||
file1 = open(md5_file, 'w')
|
||||
file1.write(result.hexdigest())
|
||||
file1.close()
|
||||
|
||||
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", [bin_copy])
|
||||
env.AddPostAction("$BUILD_DIR/${PROGNAME}.md5", [bin_copy])
|
||||
Reference in New Issue
Block a user