From 38dc2667abd921cfb529c06af5f492d2422e766a Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 15 Sep 2018 14:47:13 +0200 Subject: [PATCH] ota-test first push --- platformio.ini | 17 +++++++---- publish_firmware.py | 71 +++++++++++++++++++++++++++++++++++++++++++++ src/SecureOTA.cpp | 26 ++++++++++------- 3 files changed, 97 insertions(+), 17 deletions(-) create mode 100644 publish_firmware.py diff --git a/platformio.ini b/platformio.ini index ddf0adc7..65eb1107 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,6 +9,7 @@ ; http://docs.platformio.org/page/projectconf.html + ; ---> SELECT TARGET PLATFORM HERE! <--- [platformio] ;env_default = generic @@ -29,9 +30,9 @@ description = Paxcounter is a proof-of-concept ESP32 device for metering passeng [bintray] user = cyberman54 -repository = paxcounter -package = esp32-paxcounter -api_token = 9f02e2a2374c278fd79d5bcf4b4442fca9752012 +repository = paxcounter-firmware +package = ttgov21_old +api_token = *** [wifi] ssid = *** @@ -42,7 +43,8 @@ platform = https://github.com/platformio/platform-espressif32.git ; firmware version, please modify it between releases ; positive integer value -release_version = 1 +;release_version = 1.4.30 +release_version = 4 ; build configuration based on Bintray and Wi-Fi settings build_flags = @@ -51,7 +53,7 @@ build_flags = '-DBINTRAY_USER="${bintray.user}"' '-DBINTRAY_REPO="${bintray.repository}"' '-DBINTRAY_PACKAGE="${bintray.package}"' - '-DVERSION=0' + -DVERSION=${common.release_version} ; ; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <--- ; otherwise device may leak RAM @@ -70,7 +72,7 @@ build_flags = ; -DCORE_DEBUG_LEVEL=5 [common_env_data] -platform_espressif32 = espressif32@1.2.0 +platform_espressif32 = espressif32@1.3.0 ;platform_espressif32 = https://github.com/platformio/platform-espressif32.git#feature/stage ;board_build.partitions = no_ota.csv board_build.partitions = min_spiffs.csv @@ -142,6 +144,7 @@ build_flags = ${common_env_data.build_flags} [env:ttgov21] +bintraypackage = ttgov21_old platform = ${common_env_data.platform_espressif32} framework = arduino board = esp32dev @@ -154,6 +157,8 @@ lib_deps = build_flags = ${common.build_flags} ${common_env_data.build_flags} +upload_protocol = custom +extra_scripts = pre:publish_firmware.py [env:ttgobeam] platform = ${common_env_data.platform_espressif32} diff --git a/publish_firmware.py b/publish_firmware.py new file mode 100644 index 00000000..7086d013 --- /dev/null +++ b/publish_firmware.py @@ -0,0 +1,71 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import requests +from os.path import basename +from platformio import util + +Import('env') + +project_config = util.load_project_config() +bintray_config = {k: v for k, v in project_config.items("bintray")} +version = project_config.get("common", "release_version") + +# +# Push new firmware to the Bintray storage using API +# + + +def publish_firmware(source, target, env): + firmware_path = str(source[0]) + firmware_name = basename(firmware_path) + + print("Uploading {0} to Bintray. Version: {1}".format( + firmware_name, version)) + + print(firmware_path, firmware_name) + + url = "/".join([ + "https://api.bintray.com", "content", + bintray_config.get("user"), + bintray_config.get("repository"), + bintray_config.get("package"), version, firmware_name + ]) + + print(url) + + headers = { + "Content-type": "application/octet-stream", + "X-Bintray-Publish": "1", + "X-Bintray-Override": "1" + } + + r = requests.put( + url, + data=open(firmware_path, "rb"), + headers=headers, + auth=(bintray_config.get("user"), bintray_config['api_token'])) + + if r.status_code != 201: + print("Failed to submit package: {0}\n{1}".format( + r.status_code, r.text)) + else: + print("The firmware has been successfuly published at Bintray.com!") + + +# Custom upload command and program name +env.Replace( + PROGNAME="firmware_v_%s" % version, + UPLOADCMD=publish_firmware +) \ No newline at end of file diff --git a/src/SecureOTA.cpp b/src/SecureOTA.cpp index 2ce69eca..8b35c93b 100644 --- a/src/SecureOTA.cpp +++ b/src/SecureOTA.cpp @@ -31,9 +31,13 @@ const uint32_t RESPONSE_TIMEOUT_MS = 5000; volatile int contentLength = 0; volatile bool isValidContentType = false; +// Local logging tag +static const char TAG[] = "main"; + void checkFirmwareUpdates() { // Fetch the latest firmware version + ESP_LOGI(TAG, "Checking latest firmware version..."); const String latest = bintray.getLatestVersion(); if (latest.length() == 0) { @@ -42,11 +46,11 @@ void checkFirmwareUpdates() } else if (atoi(latest.c_str()) <= VERSION) { - //ESP_LOGI(TAG, "The current firmware is up to date. Continue ..."); + ESP_LOGI(TAG, "The current firmware is up to date. Continue ..."); return; } - ESP_LOGI(TAG, "There is a new version of firmware available: v.%s", latest); + ESP_LOGI(TAG, "There is a new version of firmware available: v.%s", latest.c_str()); processOTAUpdate(latest); } @@ -76,7 +80,7 @@ void processOTAUpdate(const String &version) if (!client.connect(currentHost.c_str(), port)) { - ESP_LOGI(TAG, "Cannot connect to %s", currentHost); + ESP_LOGI(TAG, "Cannot connect to %s", currentHost.c_str()); return; } @@ -89,7 +93,7 @@ void processOTAUpdate(const String &version) client.setCACert(bintray.getCertificate(currentHost)); if (!client.connect(currentHost.c_str(), port)) { - ESP_LOGI(TAG, "Redirect detected! Cannot connect to %s for some reason!", currentHost); + ESP_LOGI(TAG, "Redirect detected! Cannot connect to %s for some reason!", currentHost.c_str()); return; } } @@ -148,12 +152,12 @@ void processOTAUpdate(const String &version) if (line.startsWith("Location: ")) { String newUrl = getHeaderValue(line, "Location: "); - ESP_LOGI(TAG, "Got new url: %s", newUrl); + ESP_LOGI(TAG, "Got new url: %s", newUrl.c_str()); newUrl.remove(0, newUrl.indexOf("//") + 2); currentHost = newUrl.substring(0, newUrl.indexOf('/')); newUrl.remove(newUrl.indexOf(currentHost), currentHost.length()); firmwarePath = newUrl; - ESP_LOGI(TAG, "firmwarePath: %s", firmwarePath); + ESP_LOGI(TAG, "firmwarePath: %s", firmwarePath.c_str()); continue; } @@ -161,13 +165,13 @@ void processOTAUpdate(const String &version) if (line.startsWith("Content-Length: ")) { contentLength = atoi((getHeaderValue(line, "Content-Length: ")).c_str()); - ESP_LOGI(TAG, "Got %s bytes from server", String(contentLength)); + ESP_LOGI(TAG, "Got %d bytes from server", contentLength); } if (line.startsWith("Content-Type: ")) { String contentType = getHeaderValue(line, "Content-Type: "); - ESP_LOGI(TAG, "Got %s payload", contentType); + ESP_LOGI(TAG, "Got %s payload", contentType.c_str()); if (contentType == "application/octet-stream") { isValidContentType = true; @@ -186,11 +190,11 @@ void processOTAUpdate(const String &version) if (written == contentLength) { - ESP_LOGI(TAG, "Written %s successfully", String(written)); + ESP_LOGI(TAG, "Written %d successfully", written); } else { - ESP_LOGI(TAG, "Written only %s / %s Retry?", String(written), String(contentLength)); + ESP_LOGI(TAG, "Written only %d / %d Retry?", written, contentLength); // Retry?? } @@ -208,7 +212,7 @@ void processOTAUpdate(const String &version) } else { - ESP_LOGI(TAG, "An error occurred. Error #: %s", String(Update.getError())); + ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError()); } } else