commit 9bd1f7016c9a56f15cf67b2caa9887dffe5ca842 Author: Christian Staudte Date: Wed May 6 21:16:19 2020 +0200 add base files diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eeeb307 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.pio/ +.vscode/ \ No newline at end of file diff --git a/include/elapsedMillis.h b/include/elapsedMillis.h new file mode 100644 index 0000000..d8ebe39 --- /dev/null +++ b/include/elapsedMillis.h @@ -0,0 +1,104 @@ +/* Elapsed time types - for easy-to-use measurements of elapsed time + * http://www.pjrc.com/teensy/ + * Copyright (c) 2011 PJRC.COM, LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef elapsedMillis_h +#define elapsedMillis_h +#ifdef __cplusplus + +#if ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +class elapsedMicros +{ +private: + unsigned long us; +public: + elapsedMicros(void) { us = micros(); } + elapsedMicros(unsigned long val) { us = micros() - val; } + elapsedMicros(const elapsedMicros &orig) { us = orig.us; } + operator unsigned long () const { return micros() - us; } + elapsedMicros & operator = (const elapsedMicros &rhs) { us = rhs.us; return *this; } + elapsedMicros & operator = (unsigned long val) { us = micros() - val; return *this; } + elapsedMicros & operator -= (unsigned long val) { us += val ; return *this; } + elapsedMicros & operator += (unsigned long val) { us -= val ; return *this; } + elapsedMicros operator - (int val) const { elapsedMicros r(*this); r.us += val; return r; } + elapsedMicros operator - (unsigned int val) const { elapsedMicros r(*this); r.us += val; return r; } + elapsedMicros operator - (long val) const { elapsedMicros r(*this); r.us += val; return r; } + elapsedMicros operator - (unsigned long val) const { elapsedMicros r(*this); r.us += val; return r; } + elapsedMicros operator + (int val) const { elapsedMicros r(*this); r.us -= val; return r; } + elapsedMicros operator + (unsigned int val) const { elapsedMicros r(*this); r.us -= val; return r; } + elapsedMicros operator + (long val) const { elapsedMicros r(*this); r.us -= val; return r; } + elapsedMicros operator + (unsigned long val) const { elapsedMicros r(*this); r.us -= val; return r; } +}; + +class elapsedMillis +{ +private: + unsigned long ms; +public: + elapsedMillis(void) { ms = millis(); } + elapsedMillis(unsigned long val) { ms = millis() - val; } + elapsedMillis(const elapsedMillis &orig) { ms = orig.ms; } + operator unsigned long () const { return millis() - ms; } + elapsedMillis & operator = (const elapsedMillis &rhs) { ms = rhs.ms; return *this; } + elapsedMillis & operator = (unsigned long val) { ms = millis() - val; return *this; } + elapsedMillis & operator -= (unsigned long val) { ms += val ; return *this; } + elapsedMillis & operator += (unsigned long val) { ms -= val ; return *this; } + elapsedMillis operator - (int val) const { elapsedMillis r(*this); r.ms += val; return r; } + elapsedMillis operator - (unsigned int val) const { elapsedMillis r(*this); r.ms += val; return r; } + elapsedMillis operator - (long val) const { elapsedMillis r(*this); r.ms += val; return r; } + elapsedMillis operator - (unsigned long val) const { elapsedMillis r(*this); r.ms += val; return r; } + elapsedMillis operator + (int val) const { elapsedMillis r(*this); r.ms -= val; return r; } + elapsedMillis operator + (unsigned int val) const { elapsedMillis r(*this); r.ms -= val; return r; } + elapsedMillis operator + (long val) const { elapsedMillis r(*this); r.ms -= val; return r; } + elapsedMillis operator + (unsigned long val) const { elapsedMillis r(*this); r.ms -= val; return r; } +}; + +class elapsedSeconds +{ +private: + unsigned long s; +public: + elapsedSeconds(void) { s = millis()/1000; } + elapsedSeconds(unsigned long val) { s = millis()/1000 - val; } + elapsedSeconds(const elapsedSeconds &orig) { s = orig.s; } + operator unsigned long () const { return millis()/1000 - s; } + elapsedSeconds & operator = (const elapsedSeconds &rhs) { s = rhs.s; return *this; } + elapsedSeconds & operator = (unsigned long val) { s = millis()/1000 - val; return *this; } + elapsedSeconds & operator -= (unsigned long val) { s += val ; return *this; } + elapsedSeconds & operator += (unsigned long val) { s -= val ; return *this; } + elapsedSeconds operator - (int val) const { elapsedSeconds r(*this); r.s += val; return r; } + elapsedSeconds operator - (unsigned int val) const { elapsedSeconds r(*this); r.s += val; return r; } + elapsedSeconds operator - (long val) const { elapsedSeconds r(*this); r.s += val; return r; } + elapsedSeconds operator - (unsigned long val) const { elapsedSeconds r(*this); r.s += val; return r; } + elapsedSeconds operator + (int val) const { elapsedSeconds r(*this); r.s -= val; return r; } + elapsedSeconds operator + (unsigned int val) const { elapsedSeconds r(*this); r.s -= val; return r; } + elapsedSeconds operator + (long val) const { elapsedSeconds r(*this); r.s -= val; return r; } + elapsedSeconds operator + (unsigned long val) const { elapsedSeconds r(*this); r.s -= val; return r; } +}; + +#endif // __cplusplus +#endif // elapsedMillis_h diff --git a/include/signedUpdatesHelper.h b/include/signedUpdatesHelper.h new file mode 100644 index 0000000..98416a8 --- /dev/null +++ b/include/signedUpdatesHelper.h @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#ifdef PUBLIC_SIGN_KEY + +const char pubkey[] PROGMEM = PUBLIC_SIGN_KEY; + +BearSSL::PublicKey *signPubKey = nullptr; +BearSSL::HashSHA256 *hash; +BearSSL::SigningVerifier *sign; + +void signedUpdatesHelperInit() { + signPubKey = new BearSSL::PublicKey(pubkey); + hash = new BearSSL::HashSHA256(); + sign = new BearSSL::SigningVerifier(signPubKey); + Update.installSignature(hash, sign); +} + +String signedUpdatesHelperRun(WiFiClient& wifi, const String& url, const String& currentVersion = "") { + t_httpUpdate_return ret = ESPhttpUpdate.update(wifi, url, currentVersion); + if (ret == HTTP_UPDATE_FAILED) + return ESPhttpUpdate.getLastErrorString(); + return String(); +} + +#endif diff --git a/public_key.py b/public_key.py new file mode 100644 index 0000000..b1c810a --- /dev/null +++ b/public_key.py @@ -0,0 +1,19 @@ +import configparser +import sys +Import('env') + +config = configparser.ConfigParser() +config.read("platformio.ini") + +sign_cert = config.get('uploadconfig', 'sign_cert') + +cert = '' + +try: + with open(sign_cert, 'r') as f: + for line in f: + cert += line.replace("\n", " ") + env.Append(BUILD_FLAGS='\'-DPUBLIC_SIGN_KEY="' + cert + '"\'') +except FileNotFoundError: + sys.stderr.write("No public key for signing found! Continuing without update support!\n") + pass diff --git a/sign_and_upload.py b/sign_and_upload.py new file mode 100644 index 0000000..d5b5205 --- /dev/null +++ b/sign_and_upload.py @@ -0,0 +1,39 @@ +import configparser +import requests +Import('env') + +config = configparser.ConfigParser() +config.read("platformio.ini") + +room = config.get('common', 'mqtt_room') +device = config.get('common', 'mqtt_device') +version = config.get('common', 'firmware_version') + +sign_prog = config.get('uploadconfig', 'sign_prog') +sign_key = config.get('uploadconfig', 'sign_key') +upl_url = config.get('uploadconfig', 'upl_url') +upl_token = config.get('uploadconfig', 'upl_token') + +def fw_publish(source, target, env): + fw_path = str(source[0]) + + env.Execute(sign_prog + ' --mode sign --privatekey ' + sign_key + ' --bin ' + fw_path + ' --out ' + fw_path + '.signed') + + headers = { + "Content-type": "application/octet-stream", + "x-Room": room, + "x-Device": device, + "x-Version": version, + "x-Token": upl_token + } + + try: + res = requests.put(upl_url, data=open(fw_path + '.signed', 'rb'), headers=headers) + res.raise_for_status() + except requests.exceptions.RequestException as e: + print("Upload failed: %s\n" % ("%s %s" % (res.status_code, res.text) if res else str(e))) + env.Exit(1) + + print('Upload successful') + +env.Replace(UPLOADCMD=fw_publish)