Deploy script, wifi reconnect

This commit is contained in:
Martin Bauer 2020-08-08 22:18:35 +02:00
parent 314f57b2b7
commit ae341f91b3
3 changed files with 155 additions and 17 deletions

View File

@ -1,7 +1,7 @@
{ {
// See http://go.microsoft.com/fwlink/?LinkId=827846 // See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format // for the documentation about the extensions.json format
"recommendations": [ "recommendations": [
"platformio.platformio-ide" "platformio.platformio-ide"
] ]
} }

121
firmware/deploy.py Normal file
View File

@ -0,0 +1,121 @@
import struct
import locale
from collections import namedtuple
from datetime import datetime
import subprocess
from distutils.version import StrictVersion
locale.setlocale(locale.LC_ALL, 'en_US')
# sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)
APP_DESC_OFFSET = 32
# sizeof(esp_app_desc_t)
APP_DESC_SIZE = 256
APP_DESC_STRUCT = "<II2I32s32s16s16s32s32B20I"
AppDesc = namedtuple('AppDesc',
[
"secure_version",
"version",
"project_name",
"time",
"date",
"idf_ver",
"app_elf_sha256",
]
)
def version_number_from_git(tag_prefix='release/', sha_length=10, version_format="{version}.dev{commits}+{sha}"):
def get_released_versions():
tags = sorted(subprocess.getoutput('git tag').split('\n'))
versions = [t[len(tag_prefix):]
for t in tags if t.startswith(tag_prefix)]
return versions
def tag_from_version(v):
return tag_prefix + v
def increment_version(v):
parsed_version = [int(i) for i in v.split('.')]
parsed_version[-1] += 1
return '.'.join(str(i) for i in parsed_version)
version_strings = get_released_versions()
version_strings.sort(key=StrictVersion)
latest_release = version_strings[-1]
commits_since_tag = subprocess.getoutput(
'git rev-list {}..HEAD --count'.format(tag_from_version(latest_release)))
sha = subprocess.getoutput('git rev-parse HEAD')[:sha_length]
is_dirty = len(subprocess.getoutput(
"git status --untracked-files=no -s")) > 0
if int(commits_since_tag) == 0:
version_string = latest_release
else:
next_version = increment_version(latest_release)
version_string = version_format.format(
version=next_version, commits=commits_since_tag, sha=sha)
if is_dirty:
version_string += ".dirty"
return version_string
def read_app_description(file_name):
def process_bytes(b):
if not isinstance(b, bytes):
return b
s = b.decode()
return s[:s.find("\x00")]
with open(file_name, 'rb') as f:
f.seek(APP_DESC_OFFSET, 0)
raw_app_desc = f.read(APP_DESC_SIZE)
unpacked = struct.unpack(APP_DESC_STRUCT, raw_app_desc)
unpacked = tuple(process_bytes(e) for e in unpacked)
magic_word, secure_version, _, _, version, project_name, time, date, idf_ver, app_elf_sha256, *_ = unpacked
assert magic_word == 0xABCD5432
return AppDesc(secure_version, version, project_name, time, date, idf_ver, app_elf_sha256)
def patch_app_description(file_name, version, project_name, time, date):
assert len(version) < 32
assert len(project_name) < 32
assert len(time) < 16
assert len(date) < 16
def fill_zeros(s, total_length):
s += "\x00" * (total_length - len(s))
s = s.encode()
assert len(s) == total_length
return s
with open(file_name, 'r+b') as f:
f.seek(APP_DESC_OFFSET, 0)
raw_app_desc = f.read(APP_DESC_SIZE)
unpacked = list(struct.unpack(APP_DESC_STRUCT, raw_app_desc))
unpacked[4] = fill_zeros(version, 32)
unpacked[5] = fill_zeros(project_name, 32)
unpacked[6] = fill_zeros(time, 16)
unpacked[7] = fill_zeros(date, 16)
packed = struct.pack(APP_DESC_STRUCT, *unpacked)
f.seek(APP_DESC_OFFSET, 0)
f.write(packed)
def add_info_to_firmware(firmware_file, version):
now = datetime.now()
date = now.strftime("%b %d %Y")
time = now.strftime("%H:%M:%S")
patch_app_description(firmware_file, version=version,
project_name="swimtracker.bauer.tech", time=time, date=date)
if __name__ == "__main__":
fimware_file = ".pio/build/esp32/firmware.bin"
print(read_app_description(fimware_file))
patch_app_description(fimware_file, version="myversion",
project_name="swimtracker.bauer.tech", time="22:16:12", date="Feb 20 2020")
print(read_app_description(fimware_file))

View File

@ -72,7 +72,7 @@ void httpSetup(SessionManager<SessionT> *sessionManager)
Serial.println("Tare"); Serial.println("Tare");
sessionManager->tare(); sessionManager->tare();
}; };
auto cbFirmwareUpdate = [](httpd_req_t * req) { auto cbFirmwareUpdate = [](httpd_req_t *req) {
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, "OK", -1); httpd_resp_send(req, "OK", -1);
firmwareUpdate(); firmwareUpdate();
@ -116,7 +116,22 @@ void httpSetup(SessionManager<SessionT> *sessionManager)
{ {
const String freeBytes(ESP.getFreePsram()); const String freeBytes(ESP.getFreePsram());
const String usedBytes(ESP.getPsramSize() - ESP.getFreePsram()); const String usedBytes(ESP.getPsramSize() - ESP.getFreePsram());
result += "\"psram\": { \"used\": " + usedBytes + ", \"free\":" + freeBytes + "}\n"; result += "\"psram\": { \"used\": " + usedBytes + ", \"free\":" + freeBytes + "},\n";
}
// firmware
{
auto descr = esp_ota_get_app_description();
const String projectName(descr->project_name);
const String versionStr(descr->version);
const String idfVersion(descr->idf_ver);
const String compileDate(descr->date);
const String compileTime(descr->time);
result += "\"firmware\": { \"name\" : \"" +
projectName + "\", \"version\": \"" +
versionStr + "\", \"idfVersion\": \"" +
idfVersion + "\", \"compile_date\": \"" +
compileDate + +"\", \"compile_time\": \"" +
compileTime + "\" }\n";
} }
result += "}"; result += "}";
httpd_resp_send(req, result.c_str(), result.length()); httpd_resp_send(req, result.c_str(), result.length());
@ -178,11 +193,8 @@ void checkWifi()
while (WiFi.status() != WL_CONNECTED) while (WiFi.status() != WL_CONNECTED)
{ {
Serial.println("WiFi disconnected. Try to reconnect"); Serial.println("WiFi disconnected. Try to reconnect");
WiFi.disconnect(); WiFi.reconnect();
WiFi.mode(WIFI_OFF); delay(2000);
WiFi.mode(WIFI_STA);
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
delay(200);
} }
} }
@ -202,7 +214,7 @@ void setup()
while (!Serial) while (!Serial)
{ {
} }
Serial.println("Starting SwimTracker Firmware"); Serial.printf("Starting SwimTracker Firmware - connecting to %s\n", CONFIG_WIFI_SSID);
// File system // File system
bool spiffsResult = SPIFFS.begin(true); bool spiffsResult = SPIFFS.begin(true);
@ -212,6 +224,7 @@ void setup()
ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_loop_create_default());
// WiFi // WiFi
WiFi.disconnect();
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD); WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
String fullHostname = String(CONFIG_HOSTNAME) + getIdSuffix(); String fullHostname = String(CONFIG_HOSTNAME) + getIdSuffix();
@ -230,14 +243,18 @@ void setup()
} }
Serial.print("Connected to WiFi. IP:"); Serial.print("Connected to WiFi. IP:");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
Serial.println("Running version 1"); // todo Serial.println("Running version new24!"); // todo
// Session // Session
sessionManager.begin(); sessionManager.begin();
// HTTP & Websocket server // HTTP & Websocket server
httpSetup(&sessionManager); httpSetup(&sessionManager);
Serial.printf("Offset %d\n", sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t));
Serial.printf("Size %d\n", sizeof(esp_app_desc_t));
webSocketServer.begin(); webSocketServer.begin();
} }