From 38dc2667abd921cfb529c06af5f492d2422e766a Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 15 Sep 2018 14:47:13 +0200 Subject: [PATCH 1/7] 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 From 460606f6a4354e80910f1ebdc6fe2b640236688c Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 15 Sep 2018 14:52:46 +0200 Subject: [PATCH 2/7] protect platformio.ini against github upload --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 030b6f3d..f1f6d754 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ .clang_complete .gcc-flags.json src/loraconf.h +platformio.ini \ No newline at end of file From ffd7260223eddb30cfdc73368c6ee9e1abd60113 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 15 Sep 2018 14:53:34 +0200 Subject: [PATCH 3/7] add keys to platformio.ini wifi section --- platformio.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platformio.ini b/platformio.ini index d6e092c0..764dd17c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -35,8 +35,8 @@ package = ttgov21_old api_token = *** [wifi] -ssid = *** -password = *** +ssid = testnet +password = test0815 [common] platform = https://github.com/platformio/platform-espressif32.git From 11e3ee8a8819a8d220f0eeaf7873e53ea955b4b8 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 15 Sep 2018 14:55:53 +0200 Subject: [PATCH 4/7] testing --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 764dd17c..975578c6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -32,7 +32,7 @@ description = Paxcounter is a proof-of-concept ESP32 device for metering passeng user = cyberman54 repository = paxcounter-firmware package = ttgov21_old -api_token = *** +api_token = **** [wifi] ssid = testnet From 7bd0d4d2c3c0ae30c026f50c2a324076b09323c7 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 15 Sep 2018 15:02:03 +0200 Subject: [PATCH 5/7] remove platformio.ini from git working tree --- platformio.ini | 271 ------------------------------------------------- 1 file changed, 271 deletions(-) delete mode 100644 platformio.ini diff --git a/platformio.ini b/platformio.ini deleted file mode 100644 index 975578c6..00000000 --- a/platformio.ini +++ /dev/null @@ -1,271 +0,0 @@ -; PlatformIO Project Configuration File -; -; Build options: build flags, source filter -; Upload options: custom upload port, speed and extra flags -; Library options: dependencies, extra library storages -; Advanced options: extra scripting -; -; Please visit documentation for the other options and examples -; http://docs.platformio.org/page/projectconf.html - - - -; ---> SELECT TARGET PLATFORM HERE! <--- -[platformio] -;env_default = generic -;env_default = ebox -;env_default = heltec -;env_default = ttgov1 -;env_default = ttgov2 -env_default = ttgov21 -;env_default = ttgobeam -;env_default = lopy -;env_default = lopy4 -;env_default = fipy -;env_default = lolin32litelora -;env_default = lolin32lora -;env_default = lolin32lite -; -description = Paxcounter is a proof-of-concept ESP32 device for metering passenger flows in realtime. It counts how many mobile devices are around. - -[bintray] -user = cyberman54 -repository = paxcounter-firmware -package = ttgov21_old -api_token = **** - -[wifi] -ssid = testnet -password = test0815 - -[common] -platform = https://github.com/platformio/platform-espressif32.git - -; firmware version, please modify it between releases -; positive integer value -;release_version = 1.4.30 -release_version = 4 - -; build configuration based on Bintray and Wi-Fi settings -build_flags = - '-DWIFI_SSID="${wifi.ssid}"' - '-DWIFI_PASS="${wifi.password}"' - '-DBINTRAY_USER="${bintray.user}"' - '-DBINTRAY_REPO="${bintray.repository}"' - '-DBINTRAY_PACKAGE="${bintray.package}"' - -DVERSION=${common.release_version} -; -; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <--- -; otherwise device may leak RAM -; -; None -; -DCORE_DEBUG_LEVEL=0 -; Error -; -DCORE_DEBUG_LEVEL=1 -; Warn -; -DCORE_DEBUG_LEVEL=2 -; Info - -DCORE_DEBUG_LEVEL=3 -; Debug -; -DCORE_DEBUG_LEVEL=4 -; Verbose -; -DCORE_DEBUG_LEVEL=5 - -[common_env_data] -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 -lib_deps_all = - ArduinoJson -lib_deps_display = - U8g2@>=2.23.16 -lib_deps_rgbled = - SmartLeds@>=1.1.3 -lib_deps_gps = - TinyGPSPlus@>=1.0.2 - Time@>=1.5 -build_flags = -; override lora settings from LMiC library in lmic/config.h and use main.h instead - -D_lmic_config_h_ - -include "src/paxcounter.conf" - -include "src/hal/${PIOENV}.h" - -w - -[env:ebox] -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = esp32dev -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 115200 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} -build_flags = - ${common_env_data.build_flags} - -[env:heltec] -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = heltec_wifi_lora_32 -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 115200 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_display} -build_flags = - ${common_env_data.build_flags} - -[env:ttgov1] -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = esp32dev -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 115200 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_display} -build_flags = - ${common_env_data.build_flags} - -[env:ttgov2] -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = esp32dev -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 921600 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_display} -build_flags = - ${common_env_data.build_flags} - -[env:ttgov21] -bintraypackage = ttgov21_old -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = esp32dev -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 921600 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_display} -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} -framework = arduino -board = esp32dev -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 921600 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_gps} -build_flags = - ${common_env_data.build_flags} - -mfix-esp32-psram-cache-issue - -[env:fipy] -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = esp32dev -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 921600 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_rgbled} -build_flags = - ${common_env_data.build_flags} - -[env:lopy] -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = esp32dev -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 921600 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_rgbled} - ${common_env_data.lib_deps_gps} -build_flags = - ${common_env_data.build_flags} - -[env:lopy4] -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = esp32dev -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 921600 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_rgbled} - ${common_env_data.lib_deps_gps} -build_flags = - ${common_env_data.build_flags} - -mfix-esp32-psram-cache-issue - -[env:lolin32litelora] -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = lolin32 -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 921600 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_rgbled} -build_flags = - ${common_env_data.build_flags} - -[env:lolin32lora] -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = lolin32 -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 921600 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_rgbled} -build_flags = - ${common_env_data.build_flags} - -[env:lolin32lite] -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = lolin32 -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 921600 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_rgbled} -build_flags = - ${common_env_data.build_flags} - -[env:generic] -platform = ${common_env_data.platform_espressif32} -framework = arduino -board = esp32dev -board_build.partitions = ${common_env_data.board_build.partitions} -upload_speed = 921600 -monitor_speed = 115200 -lib_deps = - ${common_env_data.lib_deps_all} - ${common_env_data.lib_deps_rgbled} - ${common_env_data.lib_deps_gps} - ${common_env_data.lib_deps_display} -build_flags = - ${common_env_data.build_flags} From 5139f7f8c76fc79be7dd866c4c23a25c92f4c169 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Sat, 15 Sep 2018 15:20:41 +0200 Subject: [PATCH 6/7] clean platformio.ini uploaded --- platformio.ini | 270 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 platformio.ini diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 00000000..17ab99e0 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,270 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/page/projectconf.html + + + +; ---> SELECT TARGET PLATFORM HERE! <--- +[platformio] +;env_default = generic +;env_default = ebox +;env_default = heltec +;env_default = ttgov1 +;env_default = ttgov2 +env_default = ttgov21 +;env_default = ttgobeam +;env_default = lopy +;env_default = lopy4 +;env_default = fipy +;env_default = lolin32litelora +;env_default = lolin32lora +;env_default = lolin32lite +; +description = Paxcounter is a proof-of-concept ESP32 device for metering passenger flows in realtime. It counts how many mobile devices are around. + +[bintray] +user = cyberman54 +repository = paxcounter-firmware +package = ttgov21_old +api_token = *** + +[wifi] +ssid = *** +password = *** + +[common] +platform = https://github.com/platformio/platform-espressif32.git + +; firmware version, please modify it between releases +; positive integer value +;release_version = 1.4.30 +release_version = 3 + +; build configuration based on Bintray and Wi-Fi settings +build_flags = + '-DWIFI_SSID="${wifi.ssid}"' + '-DWIFI_PASS="${wifi.password}"' + '-DBINTRAY_USER="${bintray.user}"' + '-DBINTRAY_REPO="${bintray.repository}"' + '-DBINTRAY_PACKAGE="${bintray.package}"' + -DVERSION=${common.release_version} +; +; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <--- +; otherwise device may leak RAM +; +; None +; -DCORE_DEBUG_LEVEL=0 +; Error +; -DCORE_DEBUG_LEVEL=1 +; Warn +; -DCORE_DEBUG_LEVEL=2 +; Info + -DCORE_DEBUG_LEVEL=3 +; Debug +; -DCORE_DEBUG_LEVEL=4 +; Verbose +; -DCORE_DEBUG_LEVEL=5 + +[common_env_data] +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 +lib_deps_all = + ArduinoJson +lib_deps_display = + U8g2@>=2.23.16 +lib_deps_rgbled = + SmartLeds@>=1.1.3 +lib_deps_gps = + TinyGPSPlus@>=1.0.2 + Time@>=1.5 +build_flags = +; override lora settings from LMiC library in lmic/config.h and use main.h instead + -D_lmic_config_h_ + -include "src/paxcounter.conf" + -include "src/hal/${PIOENV}.h" + -w + +[env:ebox] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = esp32dev +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 115200 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} +build_flags = + ${common_env_data.build_flags} + +[env:heltec] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = heltec_wifi_lora_32 +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 115200 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_display} +build_flags = + ${common_env_data.build_flags} + +[env:ttgov1] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = esp32dev +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 115200 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_display} +build_flags = + ${common_env_data.build_flags} + +[env:ttgov2] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = esp32dev +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 921600 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_display} +build_flags = + ${common_env_data.build_flags} + +[env:ttgov21] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = esp32dev +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 921600 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_display} +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} +framework = arduino +board = esp32dev +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 921600 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_gps} +build_flags = + ${common_env_data.build_flags} + -mfix-esp32-psram-cache-issue + +[env:fipy] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = esp32dev +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 921600 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_rgbled} +build_flags = + ${common_env_data.build_flags} + +[env:lopy] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = esp32dev +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 921600 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_rgbled} + ${common_env_data.lib_deps_gps} +build_flags = + ${common_env_data.build_flags} + +[env:lopy4] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = esp32dev +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 921600 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_rgbled} + ${common_env_data.lib_deps_gps} +build_flags = + ${common_env_data.build_flags} + -mfix-esp32-psram-cache-issue + +[env:lolin32litelora] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = lolin32 +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 921600 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_rgbled} +build_flags = + ${common_env_data.build_flags} + +[env:lolin32lora] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = lolin32 +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 921600 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_rgbled} +build_flags = + ${common_env_data.build_flags} + +[env:lolin32lite] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = lolin32 +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 921600 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_rgbled} +build_flags = + ${common_env_data.build_flags} + +[env:generic] +platform = ${common_env_data.platform_espressif32} +framework = arduino +board = esp32dev +board_build.partitions = ${common_env_data.board_build.partitions} +upload_speed = 921600 +monitor_speed = 115200 +lib_deps = + ${common_env_data.lib_deps_all} + ${common_env_data.lib_deps_rgbled} + ${common_env_data.lib_deps_gps} + ${common_env_data.lib_deps_display} +build_flags = + ${common_env_data.build_flags} From 2c1dd22555bf52fabc2d600077399149cb63d592 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 15 Sep 2018 16:29:52 +0200 Subject: [PATCH 7/7] ota first test --- platformio.ini | 6 +- src/OTA.cpp | 261 ++++++++++++++++++++++++++++++++++++---------- src/OTA.h | 8 +- src/SecureOTA.cpp | 229 ---------------------------------------- src/SecureOTA.h | 25 ----- 5 files changed, 211 insertions(+), 318 deletions(-) delete mode 100644 src/SecureOTA.cpp delete mode 100644 src/SecureOTA.h diff --git a/platformio.ini b/platformio.ini index 17ab99e0..121cca95 100644 --- a/platformio.ini +++ b/platformio.ini @@ -32,11 +32,11 @@ description = Paxcounter is a proof-of-concept ESP32 device for metering passeng user = cyberman54 repository = paxcounter-firmware package = ttgov21_old -api_token = *** +api_token = 2e10f923df5d47b9c7e25752510322a1d65ee997 [wifi] -ssid = *** -password = *** +ssid = testnet +password = test0815 [common] platform = https://github.com/platformio/platform-espressif32.git diff --git a/src/OTA.cpp b/src/OTA.cpp index 1ec3be8e..888c28cd 100644 --- a/src/OTA.cpp +++ b/src/OTA.cpp @@ -1,79 +1,226 @@ -#include "OTA.h" +/* + Parts of this code: + 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. +*/ + +#include +#include +#include +#include "ota.h" const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE); -bool Wifi_Connected = false; +// Connection port (HTTPS) +const int port = 443; -esp_err_t event_handler(void *ctx, system_event_t *event) { - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - ESP_LOGI(TAG, "Event STA_START"); - break; - case SYSTEM_EVENT_STA_GOT_IP: - Wifi_Connected = true; - ESP_LOGI(TAG, "Event STA_GOT_IP"); - // print the local IP address - tcpip_adapter_ip_info_t ip_info; - ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info)); - ESP_LOGI(TAG, "IP %s", ip4addr_ntoa(&ip_info.ip)); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - Wifi_Connected = false; - ESP_LOGI(TAG, "Event STA_DISCONNECTED"); - break; - default: - break; - } -} +// Connection timeout +const uint32_t RESPONSE_TIMEOUT_MS = 5000; -void ota_wifi_init(void) { +// Variables to validate firmware content +volatile int contentLength = 0; +volatile bool isValidContentType = false; - tcpip_adapter_if_t tcpip_if = TCPIP_ADAPTER_IF_STA; +// Local logging tag +static const char TAG[] = "main"; - // initialize the tcp stack - // nvs_flash_init(); - tcpip_adapter_init(); - tcpip_adapter_set_hostname(tcpip_if, PROGNAME); - tcpip_adapter_dhcpc_start(tcpip_if); +void start_ota_update() { + ota_update = false; // clear ota trigger switch - // initialize the wifi event handler - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + ESP_LOGI(TAG, "Stopping Wifi scanner"); + vTaskDelete(WifiLoopTask); + ESP_LOGI(TAG, "Starting Wifi OTA update"); // switch off monitor more ESP_ERROR_CHECK( esp_wifi_set_promiscuous(false)); // now switch on monitor mode ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(NULL)); - wifi_sta_config_t cfg; - strcpy((char *)cfg.ssid, WIFI_SSID); - strcpy((char *)cfg.password, WIFI_PASS); - cfg.bssid_set = false; + WiFi.begin(WIFI_SSID, WIFI_PASS); - wifi_config_t sta_cfg; - sta_cfg.sta = cfg; + while (WiFi.status() != WL_CONNECTED) { + delay(2000); + ESP_LOGI(TAG, "trying to connect to %s", WIFI_SSID); + } - wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_LOGI(TAG, "connected to %s", WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_init(&wifi_cfg)); - ESP_ERROR_CHECK( - esp_wifi_set_storage(WIFI_STORAGE_RAM)); // we don't need NVRAM - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_cfg)); - ESP_ERROR_CHECK(esp_wifi_start()); + checkFirmwareUpdates(); + ESP.restart(); // reached only if update was not successful + +} // start_ota_update + +void checkFirmwareUpdates() { + // Fetch the latest firmware version + ESP_LOGI(TAG, "Checking latest firmware version..."); + const String latest = bintray.getLatestVersion(); + if (latest.length() == 0) { + ESP_LOGI(TAG, "Could not load info about the latest firmware, so nothing " + "to update. Continue ..."); + return; + } else if (atoi(latest.c_str()) <= VERSION) { + 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.c_str()); + processOTAUpdate(latest); } -void start_ota_update() { - ESP_LOGI(TAG, "Stopping Wifi task on core 0"); - vTaskDelete(WifiLoopTask); +// A helper function to extract header value from header +inline String getHeaderValue(String header, String headerName) { + return header.substring(strlen(headerName.c_str())); +} - ESP_LOGI(TAG, "Stopping LORA task on core 1"); - vTaskDelete(LoraTask); +/** + * OTA update processing + */ +void processOTAUpdate(const String &version) { + String firmwarePath = bintray.getBinaryPath(version); + if (!firmwarePath.endsWith(".bin")) { + ESP_LOGI(TAG, "Unsupported binary format. OTA update cannot be performed!"); + return; + } - ESP_LOGI(TAG, "Connecting to %s", WIFI_SSID); - ota_wifi_init(); - delay(2000); - delay(2000); - checkFirmwareUpdates(); - ESP.restart(); // reached if update was not successful + String currentHost = bintray.getStorageHost(); + String prevHost = currentHost; + + WiFiClientSecure client; + client.setCACert(bintray.getCertificate(currentHost)); + + if (!client.connect(currentHost.c_str(), port)) { + ESP_LOGI(TAG, "Cannot connect to %s", currentHost.c_str()); + return; + } + + bool redirect = true; + while (redirect) { + if (currentHost != prevHost) { + client.stop(); + 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.c_str()); + return; + } + } + + // ESP_LOGI(TAG, "Requesting: " + firmwarePath); + + client.print(String("GET ") + firmwarePath + " HTTP/1.1\r\n"); + client.print(String("Host: ") + currentHost + "\r\n"); + client.print("Cache-Control: no-cache\r\n"); + client.print("Connection: close\r\n\r\n"); + + unsigned long timeout = millis(); + while (client.available() == 0) { + if (millis() - timeout > RESPONSE_TIMEOUT_MS) { + ESP_LOGI(TAG, "Client Timeout !"); + client.stop(); + return; + } + } + + while (client.available()) { + String line = client.readStringUntil('\n'); + // Check if the line is end of headers by removing space symbol + line.trim(); + // if the the line is empty, this is the end of the headers + if (!line.length()) { + break; // proceed to OTA update + } + + // Check allowed HTTP responses + if (line.startsWith("HTTP/1.1")) { + if (line.indexOf("200") > 0) { + ESP_LOGI(TAG, "Got 200 status code from server. Proceeding to " + "firmware flashing"); + redirect = false; + } else if (line.indexOf("302") > 0) { + ESP_LOGI(TAG, "Got 302 status code from server. Redirecting to the " + "new address"); + redirect = true; + } else { + ESP_LOGI(TAG, "Could not get a valid firmware url"); + // Unexptected HTTP response. Retry or skip update? + redirect = false; + } + } + + // Extracting new redirect location + if (line.startsWith("Location: ")) { + String newUrl = getHeaderValue(line, "Location: "); + 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.c_str()); + continue; + } + + // Checking headers + if (line.startsWith("Content-Length: ")) { + contentLength = + atoi((getHeaderValue(line, "Content-Length: ")).c_str()); + 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.c_str()); + if (contentType == "application/octet-stream") { + isValidContentType = true; + } + } + } + } + + // check whether we have everything for OTA update + if (contentLength && isValidContentType) { + if (Update.begin(contentLength)) { + ESP_LOGI(TAG, "Starting Over-The-Air update. This may take some time to " + "complete ..."); + size_t written = Update.writeStream(client); + + if (written == contentLength) { + ESP_LOGI(TAG, "Written %d successfully", written); + } else { + ESP_LOGI(TAG, "Written only %d / %d Retry?", written, contentLength); + // Retry?? + } + + if (Update.end()) { + if (Update.isFinished()) { + ESP_LOGI(TAG, "OTA update has successfully completed. Rebooting ..."); + ESP.restart(); + } else { + ESP_LOGI(TAG, "Something went wrong! OTA update hasn't been finished " + "properly."); + } + } else { + ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError()); + } + } else { + ESP_LOGI(TAG, "There isn't enough space to start OTA update"); + client.flush(); + } + } else { + ESP_LOGI(TAG, + "There was no valid content in the response from the OTA server!"); + client.flush(); + } } \ No newline at end of file diff --git a/src/OTA.h b/src/OTA.h index 8b6a27df..a20e54df 100644 --- a/src/OTA.h +++ b/src/OTA.h @@ -2,12 +2,12 @@ #define OTA_H #include -#include "globals.h" -#include #include -#include "ota.h" -#include "SecureOTA.h" +#include "globals.h" +#include "wifiscan.h" +void checkFirmwareUpdates(); +void processOTAUpdate(const String &version); void start_ota_update(); #endif // OTA_H \ No newline at end of file diff --git a/src/SecureOTA.cpp b/src/SecureOTA.cpp deleted file mode 100644 index 8b35c93b..00000000 --- a/src/SecureOTA.cpp +++ /dev/null @@ -1,229 +0,0 @@ -/* - 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. -**/ - -#include -#include -#include -#include "SecureOTA.h" - -const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE); - -// Connection port (HTTPS) -const int port = 443; - -// Connection timeout -const uint32_t RESPONSE_TIMEOUT_MS = 5000; - -// Variables to validate firmware content -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) - { - ESP_LOGI(TAG, "Could not load info about the latest firmware, so nothing to update. Continue ..."); - return; - } - else if (atoi(latest.c_str()) <= VERSION) - { - 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.c_str()); - processOTAUpdate(latest); -} - -// A helper function to extract header value from header -inline String getHeaderValue(String header, String headerName) -{ - return header.substring(strlen(headerName.c_str())); -} - -/** - * OTA update processing - */ -void processOTAUpdate(const String &version) -{ - String firmwarePath = bintray.getBinaryPath(version); - if (!firmwarePath.endsWith(".bin")) - { - ESP_LOGI(TAG, "Unsupported binary format. OTA update cannot be performed!"); - return; - } - - String currentHost = bintray.getStorageHost(); - String prevHost = currentHost; - - WiFiClientSecure client; - client.setCACert(bintray.getCertificate(currentHost)); - - if (!client.connect(currentHost.c_str(), port)) - { - ESP_LOGI(TAG, "Cannot connect to %s", currentHost.c_str()); - return; - } - - bool redirect = true; - while (redirect) - { - if (currentHost != prevHost) - { - client.stop(); - 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.c_str()); - return; - } - } - - //ESP_LOGI(TAG, "Requesting: " + firmwarePath); - - client.print(String("GET ") + firmwarePath + " HTTP/1.1\r\n"); - client.print(String("Host: ") + currentHost + "\r\n"); - client.print("Cache-Control: no-cache\r\n"); - client.print("Connection: close\r\n\r\n"); - - unsigned long timeout = millis(); - while (client.available() == 0) - { - if (millis() - timeout > RESPONSE_TIMEOUT_MS) - { - ESP_LOGI(TAG, "Client Timeout !"); - client.stop(); - return; - } - } - - while (client.available()) - { - String line = client.readStringUntil('\n'); - // Check if the line is end of headers by removing space symbol - line.trim(); - // if the the line is empty, this is the end of the headers - if (!line.length()) - { - break; // proceed to OTA update - } - - // Check allowed HTTP responses - if (line.startsWith("HTTP/1.1")) - { - if (line.indexOf("200") > 0) - { - ESP_LOGI(TAG, "Got 200 status code from server. Proceeding to firmware flashing"); - redirect = false; - } - else if (line.indexOf("302") > 0) - { - ESP_LOGI(TAG, "Got 302 status code from server. Redirecting to the new address"); - redirect = true; - } - else - { - ESP_LOGI(TAG, "Could not get a valid firmware url"); - //Unexptected HTTP response. Retry or skip update? - redirect = false; - } - } - - // Extracting new redirect location - if (line.startsWith("Location: ")) - { - String newUrl = getHeaderValue(line, "Location: "); - 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.c_str()); - continue; - } - - // Checking headers - if (line.startsWith("Content-Length: ")) - { - contentLength = atoi((getHeaderValue(line, "Content-Length: ")).c_str()); - 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.c_str()); - if (contentType == "application/octet-stream") - { - isValidContentType = true; - } - } - } - } - - // check whether we have everything for OTA update - if (contentLength && isValidContentType) - { - if (Update.begin(contentLength)) - { - ESP_LOGI(TAG, "Starting Over-The-Air update. This may take some time to complete ..."); - size_t written = Update.writeStream(client); - - if (written == contentLength) - { - ESP_LOGI(TAG, "Written %d successfully", written); - } - else - { - ESP_LOGI(TAG, "Written only %d / %d Retry?", written, contentLength); - // Retry?? - } - - if (Update.end()) - { - if (Update.isFinished()) - { - ESP_LOGI(TAG, "OTA update has successfully completed. Rebooting ..."); - ESP.restart(); - } - else - { - ESP_LOGI(TAG, "Something went wrong! OTA update hasn't been finished properly."); - } - } - else - { - ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError()); - } - } - else - { - ESP_LOGI(TAG, "There isn't enough space to start OTA update"); - client.flush(); - } - } - else - { - ESP_LOGI(TAG, "There was no valid content in the response from the OTA server!"); - client.flush(); - } -} \ No newline at end of file diff --git a/src/SecureOTA.h b/src/SecureOTA.h deleted file mode 100644 index d8b17c3f..00000000 --- a/src/SecureOTA.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - 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. -**/ - -#ifndef SECURE_OTA_H -#define SECURE_OTA_H - -#include - -void checkFirmwareUpdates(); -void processOTAUpdate(const String &version); - -#endif // SECURE_OTA_H \ No newline at end of file