diff --git a/.build.yml b/.build.yml index 849107e..7ece57b 100644 --- a/.build.yml +++ b/.build.yml @@ -9,17 +9,19 @@ steps: - name: submodules image: alpine/git commands: - - git submodule update --init --recursive + - git submodule update --init --recursive --depth 1 - name: firmware image: docker-repo.service.intern.lab.or.it:5000/fiatlux-build-env depends_on: [ submodules ] commands: - export PATH=$(pwd)/modules/sdk/xtensa-lx106-elf/bin:$PATH + - apt update + - apt install -y minify - make firmware -j$(nproc) - name: pcb - image: setsoft/kicad_auto + image: setsoft/kicad_auto:ki6 commands: - apt update - apt install -y make zip @@ -57,10 +59,11 @@ steps: base_url: https://git.neulandlabor.de/ files: - firmware/firmware/fiatlux.bin + - firmware/otaflash.py - pcb/pcb.zip checksum: - sha512 - md5 - title: buildtest + title: fiatlux when: event: tag diff --git a/.gitmodules b/.gitmodules index 11a7502..5e7dc18 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "modules/rtos"] path = modules/rtos - url = https://github.com/SuperHouse/esp-open-rtos.git + url = https://git.neulandlabor.de/j3d1/esp-open-rtos.git [submodule "modules/sdk"] path = modules/sdk url = https://github.com/pfalcon/esp-open-sdk.git diff --git a/Makefile b/Makefile index 8fafbe1..c8f31c2 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,3 @@ - .PHONY: firmware flash firmware_docker case pcb all: firmware case pcb @@ -19,9 +18,14 @@ clean: +@make -C firmware clean +@make -C pcb clean +flash_docker: + sh -c "docker build -t fiatlux_firmware_env docker/firmware" + sh -c "docker run --volume "$$(pwd)"/firmware:/app/firmware --device=/dev/ttyUSB0 fiatlux_firmware_env make -C firmware flash" + + firmware_docker: sh -c "docker build -t fiatlux_firmware_env docker/firmware" - sh -c "docker run --volume "$$(pwd)"/firmware:/app/firmware fiatlux_firmware_env make -C firmware all" + sh -c "docker run --volume "$$(pwd)"/firmware:/app/firmware fiatlux_firmware_env make -C firmware html all" pcb_docker: sh -c "docker build -t fiatlux_pcb_env docker/pcb" diff --git a/README.md b/README.md index ebf56ee..ecb33c2 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ git submodule update --init --recursive ### Build Requirements - make - - bash gawk perl + - bash gawk - g++ gcc - libc6-dev - flex bison @@ -27,6 +27,7 @@ git submodule update --init --recursive - ncurses-dev libexpat-dev - python3 python3-serial python-dev + - pip install websocket-client (for otaflash.py, optional) ### Build Steps diff --git a/docker/firmware/Dockerfile b/docker/firmware/Dockerfile index 582fe2f..d278775 100644 --- a/docker/firmware/Dockerfile +++ b/docker/firmware/Dockerfile @@ -6,7 +6,7 @@ RUN cd app; git clone --recursive https://github.com/SuperHouse/esp-open-rtos.gi RUN cd app; sed -i 's/GNU bash, version (3\\\.\[1-9\]|4)/GNU bash, version (3.[1-9]|4|5)/g' modules/sdk/crosstool-NG/configure.ac; mkdir -p modules/sdk/crosstool-NG/.build/tarballs; wget https://github.com/libexpat/libexpat/releases/download/R_2_1_0/expat-2.1.0.tar.gz -O modules/sdk/crosstool-NG/.build/tarballs/expat-2.1.0.tar.gz RUN cd app/modules/sdk; export CT_EXPERIMENTAL=y; export CT_ALLOW_BUILD_AS_ROOT=y; export CT_ALLOW_BUILD_AS_ROOT_SURE=y; make standalone=y -j$(nproc); wget -N https://raw.githubusercontent.com/espressif/esptool/master/esptool.py -O xtensa-lx106-elf/bin/esptool.py USER 0 -RUN apt remove --purge -y python2 && apt autoremove --purge -y && apt install -y python3 python3-serial perl +RUN apt remove --purge -y python2 && apt autoremove --purge -y && apt install -y python3 python3-serial RUN apt install -y --reinstall python-is-python3 USER 1000 WORKDIR /app diff --git a/firmware/.gitignore b/firmware/.gitignore index c67e8c9..14041d9 100644 --- a/firmware/.gitignore +++ b/firmware/.gitignore @@ -142,5 +142,5 @@ dkms.conf *.remove firmware/ -fsdata/fsdata.c -compile_commands.json \ No newline at end of file +build/ +compile_commands.json diff --git a/firmware/.idea/firmware.iml b/firmware/.idea/firmware.iml index 58ca04c..190a5aa 100644 --- a/firmware/.idea/firmware.iml +++ b/firmware/.idea/firmware.iml @@ -1,2 +1,9 @@ - \ No newline at end of file + + + + + + + + \ No newline at end of file diff --git a/firmware/.idea/modules.xml b/firmware/.idea/modules.xml deleted file mode 100644 index cb860b6..0000000 --- a/firmware/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/firmware/Makefile b/firmware/Makefile index 1c91e7e..976b533 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -1,20 +1,21 @@ PROGRAM=fiatlux -EXTRA_CFLAGS=-O3 -Ifsdata +EXTRA_CFLAGS=-O3 -Ibuild/gen -DLWIP_NETIF_HOSTNAME=1 -EXTRA_COMPONENTS=extras/i2s_dma extras/ws2812_i2s extras/dhcpserver extras/mbedtls extras/httpd extras/sntp extras/cpp_support +EXTRA_COMPONENTS=extras/i2s_dma extras/ws2812_i2s extras/dhcpserver extras/rboot-ota extras/mbedtls extras/httpd extras/sntp extras/cpp_support extras/paho_mqtt_c LIBS = hal m -FLASH_MODE = dio +FLASH_MODE = qio include ../modules/rtos/common.mk -html: fsdata/fsdata.c +html: build/gen/fsdata.c -fsdata/fsdata.c: fsdata/fs/index.html fsdata/fs/404.html fsdata/fs/css/picnic.min.css fsdata/fs/css/style.css fsdata/fs/js/smoothie_min.js +build/gen/fsdata.c: webdir/index.html webdir/404.html webdir/css/picnic.min.css webdir/css/style.css webdir/js/smoothie_min.js @echo "Generating fsdata.." - cd fsdata && ./makefsdata + @mkdir -p $(dir $@) + @./mkwebfs.py --gzip --minify -o $@ $^ test: unittest systest @@ -24,4 +25,4 @@ unittest: systest: true -.NOTPARALLEL: html all \ No newline at end of file +.NOTPARALLEL: html all diff --git a/firmware/fiatlux.c b/firmware/fiatlux.c index 98d0efb..9e3efeb 100644 --- a/firmware/fiatlux.c +++ b/firmware/fiatlux.c @@ -22,9 +22,9 @@ void user_init(void) wifi_available_semaphore = xSemaphoreCreateBinary(); - xTaskCreate(wifi_task, "wifi_task", 512, NULL, 2, NULL); + xTaskCreate(wifi_task, "wifi_task", 1024, NULL, 1, NULL); - xTaskCreate(&httpd_task, "httpd_task", 512, NULL, 2, NULL); + xTaskCreate(&httpd_task, "httpd_task", 1024, NULL, 2, NULL); xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL); } diff --git a/firmware/fsdata/fs/404.html b/firmware/fsdata/fs/404.html deleted file mode 100644 index da81a20..0000000 --- a/firmware/fsdata/fs/404.html +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - HTTP Server - - - - -
-

404 - Page not found

-
Sorry, the page you are requesting was not found on this server.
-
- - - diff --git a/firmware/fsdata/makefsdata b/firmware/fsdata/makefsdata deleted file mode 100755 index 5361370..0000000 --- a/firmware/fsdata/makefsdata +++ /dev/null @@ -1,114 +0,0 @@ -#!/usr/bin/perl - -$incHttpHeader = 1; - -open(OUTPUT, "> fsdata.c"); -print(OUTPUT "#include \"httpd/fsdata.h\"\n\n"); - -chdir("fs"); -open(FILES, "find . -type f |"); - -while($file = ) { - - # Do not include files in CVS directories nor backup files. - if($file =~ /(CVS|~)/) { - next; - } - - chop($file); - - if($incHttpHeader == 1) { - open(HEADER, "> /tmp/header") || die $!; - if($file =~ /404/) { - print(HEADER "HTTP/1.0 404 File not found\r\n"); - } else { - print(HEADER "HTTP/1.0 200 OK\r\n"); - } - print(HEADER "lwIP/1.4.1 (http://savannah.nongnu.org/projects/lwip)\r\n"); - if($file =~ /\.html$/ || $file =~ /\.htm$/ || $file =~ /\.shtml$/ || $file =~ /\.shtm$/ || $file =~ /\.ssi$/) { - print(HEADER "Content-type: text/html\r\n"); - } elsif($file =~ /\.js$/) { - print(HEADER "Content-type: application/x-javascript\r\n\r\n"); - } elsif($file =~ /\.css$/) { - print(HEADER "Content-type: text/css\r\n\r\n"); - } elsif($file =~ /\.ico$/) { - print(HEADER "Content-type: image/x-icon\r\n\r\n"); - } elsif($file =~ /\.gif$/) { - print(HEADER "Content-type: image/gif\r\n"); - } elsif($file =~ /\.png$/) { - print(HEADER "Content-type: image/png\r\n"); - } elsif($file =~ /\.jpg$/) { - print(HEADER "Content-type: image/jpeg\r\n"); - } elsif($file =~ /\.bmp$/) { - print(HEADER "Content-type: image/bmp\r\n\r\n"); - } elsif($file =~ /\.class$/) { - print(HEADER "Content-type: application/octet-stream\r\n"); - } elsif($file =~ /\.ram$/) { - print(HEADER "Content-type: audio/x-pn-realaudio\r\n"); - } else { - print(HEADER "Content-type: text/plain\r\n"); - } - print(HEADER "\r\n"); - close(HEADER); - - unless($file =~ /\.plain$/ || $file =~ /cgi/) { - system("cat /tmp/header $file > /tmp/file"); - } else { - system("cp $file /tmp/file"); - } - } else { - system("cp $file /tmp/file"); - } - - open(FILE, "/tmp/file"); - unlink("/tmp/file"); - unlink("/tmp/header"); - - $file =~ s/\.//; - $fvar = $file; - $fvar =~ s-/-_-g; - $fvar =~ s-\.-_-g; - - print(OUTPUT "static const unsigned char data".$fvar."[] = {\n"); - print(OUTPUT "\t/* $file */\n\t"); - for($j = 0; $j < length($file); $j++) { - printf(OUTPUT "0x%02X, ", unpack("C", substr($file, $j, 1))); - } - printf(OUTPUT "0,\n"); - - - $i = 0; - while(read(FILE, $data, 1)) { - if($i == 0) { - print(OUTPUT "\t"); - } - printf(OUTPUT "0x%02X, ", unpack("C", $data)); - $i++; - if($i == 10) { - print(OUTPUT "\n"); - $i = 0; - } - } - print(OUTPUT "};\n\n"); - close(FILE); - push(@fvars, $fvar); - push(@files, $file); -} - -for($i = 0; $i < @fvars; $i++) { - $file = $files[$i]; - $fvar = $fvars[$i]; - - if($i == 0) { - $prevfile = "NULL"; - } else { - $prevfile = "file" . $fvars[$i - 1]; - } - print(OUTPUT "const struct fsdata_file file".$fvar."[] = {{\n$prevfile,\ndata$fvar, "); - print(OUTPUT "data$fvar + ". (length($file) + 1) .",\n"); - print(OUTPUT "sizeof(data$fvar) - ". (length($file) + 1) .",\n"); - print(OUTPUT $incHttpHeader."\n}};\n\n"); -} - -print(OUTPUT "#define FS_ROOT file$fvars[$i - 1]\n\n"); -print(OUTPUT "#define FS_NUMFILES $i\n"); diff --git a/firmware/log.cpp b/firmware/log.cpp new file mode 100644 index 0000000..8b268d9 --- /dev/null +++ b/firmware/log.cpp @@ -0,0 +1,49 @@ +// +// Created by jedi on 18.11.21. +// + +#include "log.h" + +#include + +constexpr unsigned syslog_buffer_size = 1024; +char syslog_buf[syslog_buffer_size]; +volatile unsigned head = 0; +volatile unsigned streams = 0; + +extern "C" void syslog(const char *msg) { + printf("syslog> %s", msg); + while (char c = *msg++) { + syslog_buf[head++ % syslog_buffer_size] = c; + } + syslog_buf[head] = 0; +} + +unsigned syslog_current_tail() { + if(head < syslog_buffer_size) + return 0; + return head + 1 - syslog_buffer_size; +} + +unsigned syslog_data_after(unsigned local_tail) { + if(local_tail > head) + return 0; + return (head % syslog_buffer_size) - (local_tail % syslog_buffer_size); +} + +extern "C" int syslog_copy_out(char *out, int len, unsigned local_tail) { + unsigned cnt = 0; + while (cnt < syslog_data_after(local_tail) && cnt < len) { + out[cnt] = syslog_buf[local_tail % syslog_buffer_size + cnt]; + cnt++; + } + return cnt; +} + +extern "C" void syslog_attach() { + streams++; +} + +extern "C" void syslog_detach() { + streams--; +} \ No newline at end of file diff --git a/firmware/log.h b/firmware/log.h new file mode 100644 index 0000000..0cde491 --- /dev/null +++ b/firmware/log.h @@ -0,0 +1,28 @@ +// +// Created by jedi on 18.11.21. +// + +#ifndef FIRMWARE_LOG_H +#define FIRMWARE_LOG_H + +#ifdef __cplusplus +extern "C" { +#endif + +void syslog(const char *); + +unsigned syslog_current_tail(); + +unsigned syslog_data_after(unsigned); + +int syslog_copy_out(char *, int, unsigned); + +void syslog_attach(); + +void syslog_detach(); + +#ifdef __cplusplus +} +#endif + +#endif //FIRMWARE_LOG_H diff --git a/firmware/mkwebfs.py b/firmware/mkwebfs.py new file mode 100755 index 0000000..e48548c --- /dev/null +++ b/firmware/mkwebfs.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python3 +import os +import gzip +import argparse +import subprocess + +parser = argparse.ArgumentParser() +parser.add_argument('-o', '--output', help='Output file name', default='stdout') +parser.add_argument('-W', '--webroot', help='Output file name', default='webdir/') +parser.add_argument('--gzip', dest='gzip', action='store_true') +parser.add_argument('--no-gzip', dest='gzip', action='store_false') +parser.set_defaults(gzip=False) +parser.add_argument('--minify', dest='minify', action='store_true') +parser.add_argument('--no-minify', dest='minify', action='store_false') +parser.set_defaults(minify=False) +parser.add_argument('--header', dest='header', action='store_true') +parser.add_argument('--no-header', dest='header', action='store_false') +parser.set_defaults(header=True) +parser.add_argument('input', nargs='+', default=os.getcwd()) +args = parser.parse_args() + + +def mimeFromName(name): + if name.endswith(".html") or name.endswith(".htm") or name.endswith(".shtml") or name.endswith( + ".shtm") or name.endswith(".ssi"): + return "text/html" + if name.endswith(".js"): + return "application/x-javascript" + if name.endswith(".css"): + return "text/css" + if name.endswith(".ico"): + return "image/x-icon" + if name.endswith(".gif"): + return "image/gif" + if name.endswith(".png"): + return "image/png" + if name.endswith(".jpg"): + return "image/jpeg" + if name.endswith(".bmp"): + return "image/bmp" + if name.endswith(".class"): + return "application/octet-stream" + if name.endswith(".ram"): + return "audio/x-pn-realaudio" + return "text/plain" + + +def dumpBin2CHex(f, b): + oStr = "\t" + n = 0 + for val in b: + oStr += hex(val) + ", " + n += 1 + if n % 8 == 0: + oStr += "\n\t" + oStr += "\n" + f.write(oStr) + + +f_fsdata_c = open(args.output, 'w') +f_fsdata_c.write('#include "httpd/fsdata.h"\n\n') + +httpFiles = [file for file in args.input if (args.webroot in file)] + +lastFileStruct = "NULL" + +for file in httpFiles: + response = b'' + + webPath = ("/" + file.removeprefix(args.webroot)).replace("//", "/") + print("{} > {}".format(file, webPath)) + + mimeType = mimeFromName(file) + + if args.header: + if ("404" in file): + response = b'HTTP/1.0 404 File not found\r\n' + else: + response = b'HTTP/1.0 200 OK\r\n' + response += b"lwIP/1.4.1 (http://savannah.nongnu.org/projects/lwip)\r\n" + response += b'Content-type: ' + mimeType.encode() + b'\r\n' + + binFile = open(file, 'rb') + binData = binFile.read() + compEff = False + if args.minify: + p = subprocess.Popen(["minify", "--html-keep-document-tags", "--mime", mimeType], stdin=subprocess.PIPE, + stdout=subprocess.PIPE) + minData = p.communicate(binData)[0] + if len(minData) < len(binData): + print("- Minify: {} -> {}".format(len(binData), len(minData))) + compEff = True + binData = minData + + if args.gzip: + compData = gzip.compress(binData, 9) + if len(compData) < len(binData): + compEff = True + print("- Compressed from {} to {}".format(len(binData), len(compData))) + binData = compData + else: + print("- Compression skipped Orig: {} Comp: {}".format(len(binData), len(compData))) + binFile.close() + + if compEff: + response += b'Content-Encoding: gzip\r\n' + response += b"\r\n" + response += binData + binFile.close() + escFile = file.replace("/", "_").replace(".", "_") + escFileData = "data_" + escFile + escFileFile = "file_" + escFile + + f_fsdata_c.write('static const unsigned char {}[] = {{\n'.format(escFileData)) + f_fsdata_c.write('\t/* LOCAL:{} */\n'.format(file)) + f_fsdata_c.write('\t/* WEB: {} */\n'.format(webPath)) + fnameBin = webPath.encode("ascii") + b'\0' + dumpBin2CHex(f_fsdata_c, fnameBin) + dumpBin2CHex(f_fsdata_c, response) + f_fsdata_c.write("};\n\n") + + f_fsdata_c.write("const struct fsdata_file {}[] = {{{{\n {},\n {}, {} + {}, sizeof({}) - {}, 1 }}}};\n\n" + .format(escFileFile, lastFileStruct, escFileData, escFileData, len(fnameBin), escFileData, + len(fnameBin))) + # TODO: The last value is 1 if args.header == True + lastFileStruct = escFileFile + +f_fsdata_c.write("\n") +f_fsdata_c.write("#define FS_ROOT {}\n\n".format(lastFileStruct)) +f_fsdata_c.write("#define FS_NUMFILES {}\n\n".format(len(httpFiles))) diff --git a/firmware/otaflash.py b/firmware/otaflash.py new file mode 100755 index 0000000..2c52133 --- /dev/null +++ b/firmware/otaflash.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 + +import time +import websocket +import argparse +import zlib +from websocket import WebSocketTimeoutException + +parser = argparse.ArgumentParser(description='Update fiatlux firmware via websocket.') +parser.add_argument("binfile") +parser.add_argument("address") + +args = parser.parse_args() + +bs = 1024 + + +def parse_reply(bytes): + cmd = bytes[0:1].decode("utf-8") + ret = int.from_bytes(bytes[1:2], "big") + val = int.from_bytes(bytes[2:4], "big") + return {'cmd': cmd, 'ret': ret, 'val': val} + + +with open(args.binfile, "rb") as f: + try: + ws = websocket.WebSocket() + print("send {}".format(args.binfile)) + ws.connect("ws://" + args.address) + i = 0 + bytes = f.read() + rolling = zlib.crc32(bytes) + total = len(bytes) + while True: + chunk = bytes[bs * i:bs * i + bs] + msg = b'F\x00\x00\x00' + msg += i.to_bytes(2, 'big') + msg += len(chunk).to_bytes(2, 'big') + msg += (zlib.crc32(chunk) & 0xffffffff).to_bytes(4, 'big') + msg += chunk + ws.send(msg) + print("\r{:6.2f}%".format(100 * i * bs / total), end='') + reply = parse_reply(ws.recv()) + if reply['cmd'] == 'F' and reply['ret'] == 0: + i += 1 + elif reply['cmd'] == 'F' and reply['ret'] == 1: + print("Error: SEQUENCE_OUT_OF_ORDER") + i = reply['val'] + elif reply['cmd'] == 'F' and reply['ret'] == 2: + print("Error: CHECKSUM_MISMATCH") + i = reply['val'] + else: + print(reply) + + time.sleep(0.05) + if len(chunk) != bs: + break + + print("\rdone ") + msg = b'C\x00\x00\x00' + msg += total.to_bytes(4, 'big') + msg += (rolling).to_bytes(4, 'big') + ws.settimeout(5) + ws.send(msg) + reply = parse_reply(ws.recv()) + print(reply) + ws.close() + except WebSocketTimeoutException: + pass + except ConnectionResetError: + pass + except KeyboardInterrupt: + pass diff --git a/firmware/system.c b/firmware/system.c index 350f86e..d190606 100644 --- a/firmware/system.c +++ b/firmware/system.c @@ -3,35 +3,120 @@ // #include "system.h" +#include "crc32.h" +#include "log.h" #include -#include #include #include -#include #include +#include +#include +#include +#include -void system_clear_config(){ +#define min(a, b) \ + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a < _b ? _a : _b; }) + +void system_clear_config() { vPortEnterCritical(); - uint32_t num_sectors = 5 + DEFAULT_SYSPARAM_SECTORS; - uint32_t start = sdk_flashchip.chip_size - num_sectors * sdk_flashchip.sector_size; + uint32_t num_sectors = 0x2000 / sdk_flashchip.sector_size; + uint32_t start = 0x00100000; for (uint32_t i = 0; i < num_sectors; i++) { spiflash_erase_sector(start + i * sdk_flashchip.sector_size); } + if(sysparam_create_area(start, num_sectors, true) == SYSPARAM_OK) { + sysparam_init(start, 0); + } + sysparam_init(start, start + 0x2000); sdk_system_restart(); } -void system_init_config(){ - uint32_t base_addr; +void system_init_config() { + uint32_t base_addr = 0x00100000; uint32_t num_sectors; + sysparam_init(base_addr, 0); if(sysparam_get_info(&base_addr, &num_sectors) != SYSPARAM_OK) { - printf("Warning: WiFi config, sysparam not initialized\n"); - num_sectors = DEFAULT_SYSPARAM_SECTORS; - base_addr = sdk_flashchip.chip_size - (5 + num_sectors) * sdk_flashchip.sector_size; + syslog("Warning: WiFi config, sysparam not initialized\n"); + num_sectors = 0x2000 / sdk_flashchip.sector_size; if(sysparam_create_area(base_addr, num_sectors, true) == SYSPARAM_OK) { sysparam_init(base_addr, 0); } sdk_system_restart(); } +} + +#define MAX_IMAGE_SIZE 0x100000 + +struct { + rboot_write_status status; + uint32_t head; + uint32_t base; + uint16_t seq; + uint8_t slot; +} otaflash_context; + +void system_otaflash_init() { + rboot_config conf; + conf = rboot_get_config(); + otaflash_context.slot = (conf.current_rom + 1) % conf.count; + otaflash_context.base = rboot_get_slot_offset(otaflash_context.slot); + otaflash_context.status = rboot_write_init(otaflash_context.base); + otaflash_context.head = otaflash_context.base; + otaflash_context.seq = 0; +} + +enum return_code system_otaflash_chunk(uint8_t *data, uint16_t len, uint16_t seq, uint32_t hash, uint16_t *ack) { + uint32_t local_hash = crc32(data, len); + if(hash == local_hash && otaflash_context.seq == seq) { + if(otaflash_context.head % SECTOR_SIZE == 0) { + sdk_spi_flash_erase_sector(otaflash_context.head / SECTOR_SIZE); + } + if(((uint32_t) data) % 4) { + uint32 buf[len / 4]; + memcpy(buf, data, len); + sdk_spi_flash_write(otaflash_context.head, buf, len); + } else { + sdk_spi_flash_write(otaflash_context.head, (uint32_t *) data, len); + } + otaflash_context.head += len; + otaflash_context.seq++; + return OK; + } else if(hash != local_hash) { + return CHECKSUM_MISMATCH; + } else { + if(ack) + *ack = otaflash_context.seq; + return SEQUENCE_OUT_OF_ORDER; + } + +} + +void system_otaflash_verify_chunk(void *ctx, void *data, size_t len) { + uint32_t digest = *(uint32_t *) ctx; + digest = crc32_partial(digest, data, len); + *(uint32_t *) ctx = digest; +} + +enum return_code system_otaflash_verify_and_switch(uint32_t len, uint32_t hash) { + + uint32_t digest = 0; + rboot_digest_image(otaflash_context.base, min(len, MAX_IMAGE_SIZE), system_otaflash_verify_chunk, &digest); + + if(hash != digest) { + syslog("OTA failed to verify firmware\r\n"); + return CHECKSUM_MISMATCH; + } + + vPortEnterCritical(); + if(!rboot_set_current_rom(otaflash_context.slot)) { + syslog("OTA Update failed to set new rboot slot\r\n"); + vPortExitCritical(); + return RBOOT_SWITCH_FAILED; + } + vPortExitCritical(); + return OK; } \ No newline at end of file diff --git a/firmware/system.h b/firmware/system.h index 14f5188..075bd90 100644 --- a/firmware/system.h +++ b/firmware/system.h @@ -5,13 +5,23 @@ #ifndef FIRMWARE_SYSTEM_H #define FIRMWARE_SYSTEM_H +#include +#include "types.h" + #ifdef __cplusplus extern "C" { #endif void system_clear_config(); + void system_init_config(); +void system_otaflash_init(); + +enum return_code system_otaflash_chunk(uint8_t *data, uint16_t len, uint16_t seq, uint32_t hash, uint16_t *ack); + +enum return_code system_otaflash_verify_and_switch(uint32_t len, uint32_t hash); + #ifdef __cplusplus } #endif diff --git a/firmware/types.h b/firmware/types.h new file mode 100644 index 0000000..726a621 --- /dev/null +++ b/firmware/types.h @@ -0,0 +1,20 @@ +// +// Created by jedi on 09.09.21. +// + +#ifndef FIRMWARE_TYPES_H +#define FIRMWARE_TYPES_H + +#ifdef __cplusplus +extern "C" { +#endif + +enum return_code { + OK = 0, SEQUENCE_OUT_OF_ORDER, CHECKSUM_MISMATCH, RBOOT_SWITCH_FAILED, ERROR = 0xFF +}; + +#ifdef __cplusplus +} +#endif + +#endif //FIRMWARE_TYPES_H diff --git a/firmware/web.cpp b/firmware/web.cpp index c900018..ed55e2d 100644 --- a/firmware/web.cpp +++ b/firmware/web.cpp @@ -6,6 +6,7 @@ #include "system.h" #include "lux.h" #include "wifi.h" +#include "log.h" #include #include @@ -36,28 +37,46 @@ void websocket_task(void *pvParameter) { size_t connstarttime = xTaskGetTickCount(); has_changed = {true, true, true}; + syslog_attach(); + unsigned local_log_tail = syslog_current_tail(); for (;;) { - if(pcb == NULL || pcb->state != ESTABLISHED) { - printf("Connection closed, deleting task\n"); + if(pcb == nullptr || pcb->state != ESTABLISHED) { + syslog("Connection closed, deleting task\n"); break; } + //Syslog + if(syslog_data_after(local_log_tail) != 0) { + char response[128]; + response[0] = 'L'; + size_t len = syslog_copy_out(&response[4], 124, local_log_tail); + response[1] = len; + ((uint16_t &) response[2]) = local_log_tail & 0xFFFF; + if(len < sizeof(response)) { + LOCK_TCPIP_CORE(); + websocket_write(pcb, (unsigned char *) response, len + 4, WS_BIN_MODE); + local_log_tail += len; + UNLOCK_TCPIP_CORE(); + } else + syslog("buffer too small -1\n"); + vTaskDelayMs(1000); + } + //Global Info if(has_changed.global) { - has_changed.global = false; - timeval tv; - gettimeofday(&tv, NULL); - int uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000; + timeval tv{}; + gettimeofday(&tv, nullptr); + size_t uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000; int heap = (int) xPortGetFreeHeapSize(); uint32_t chip_id = sdk_system_get_chip_id(); uint32_t flash_id = sdk_spi_flash_get_id(); uint32_t flash_size = sdk_flashchip.chip_size >> 10; - char *hostname = NULL; + char *hostname = nullptr; sysparam_get_string("hostname", &hostname); /* Generate response in JSON format */ - char response[160]; + char response[192]; size_t len = snprintf(response, sizeof(response), "{\"walltime\" : \"%d\"," "\"uptime\" : \"%d\"," @@ -71,38 +90,34 @@ void websocket_task(void *pvParameter) { if(len < sizeof(response)) { LOCK_TCPIP_CORE(); websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE); + has_changed.global = false; UNLOCK_TCPIP_CORE(); } else - printf("buffer too small 1"); - vTaskDelayMs(2000); + syslog("buffer too small 0\n"); + vTaskDelayMs(1000); } //Connection Info if(has_changed.connection) { - has_changed.connection = false; - timeval tv; - gettimeofday(&tv, NULL); - int connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000; + timeval tv{}; + gettimeofday(&tv, nullptr); + size_t connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000; - printf("conn %d: " - IPSTR - " <-> " - IPSTR - " \n", pcb->netif_idx, IP2STR(&pcb->local_ip), IP2STR(&pcb->remote_ip)); - char response[160]; + printf("conn %d: " IPSTR " <-> " IPSTR " \n", pcb->netif_idx, IP2STR(&pcb->local_ip), + IP2STR(&pcb->remote_ip)); + char response[192]; size_t len = snprintf(response, sizeof(response), "{\"connage\" : \"%d\"," - "\"clientip\" : \"" - IPSTR - "\"" + "\"clientip\" : \"" IPSTR "\"" "}", connuptime, IP2STR(&pcb->remote_ip)); if(len < sizeof(response)) { LOCK_TCPIP_CORE(); websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE); + has_changed.connection = false; UNLOCK_TCPIP_CORE(); } else - printf("buffer too small 1"); - vTaskDelayMs(2000); + syslog("buffer too small 1\n"); + vTaskDelayMs(1000); } if(has_changed.wifi) { @@ -137,10 +152,10 @@ void websocket_task(void *pvParameter) { if(opmode == SOFTAP_MODE || opmode == STATIONAP_MODE) { uint8_t hwaddr[6]; sdk_wifi_get_macaddr(SOFTAP_IF, hwaddr); - ip_info info; + ip_info info{}; sdk_wifi_get_ip_info(SOFTAP_IF, &info); - char *apssid = NULL; + char *apssid = nullptr; sysparam_get_string("wifi_ap_ssid", &apssid); /* Generate response in JSON format */ @@ -148,12 +163,8 @@ void websocket_task(void *pvParameter) { size_t len = snprintf(response, sizeof(response), "{\"opmode\" : \"%s\"," " \"apssid\" : \"%s\"," - " \"apip\" : \"" - IPSTR - "\"," - " \"apmac\" : \"" - MACSTR - "\"" + " \"apip\" : \"" IPSTR "\"," + " \"apmac\" : \"" MACSTR "\"" "}", opmode_str, apssid, IP2STR(&info.ip), MAC2STR(hwaddr)); free(apssid); if(len < sizeof(response)) { @@ -161,15 +172,15 @@ void websocket_task(void *pvParameter) { websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE); UNLOCK_TCPIP_CORE(); } else - printf("buffer too small 2"); + syslog("buffer too small 2\n"); } - vTaskDelayMs(2000); + vTaskDelayMs(1000); if(opmode == STATION_MODE || opmode == STATIONAP_MODE) { uint8_t hwaddr[6]; sdk_wifi_get_macaddr(STATION_IF, hwaddr); - ip_info info; + ip_info info{}; sdk_wifi_get_ip_info(STATION_IF, &info); char *stassid = nullptr; sysparam_get_string("wifi_sta_ssid", &stassid); @@ -179,12 +190,8 @@ void websocket_task(void *pvParameter) { size_t len = snprintf(response, sizeof(response), "{\"opmode\" : \"%s\"," " \"stassid\" : \"%s\"," - " \"staip\" : \"" - IPSTR - "\"," - " \"stamac\" : \"" - MACSTR - "\"" + " \"staip\" : \"" IPSTR "\"," + " \"stamac\" : \"" MACSTR "\"" "}", opmode_str, stassid, IP2STR(&info.ip), MAC2STR(hwaddr)); free(stassid); if(len < sizeof(response)) { @@ -192,62 +199,121 @@ void websocket_task(void *pvParameter) { websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE); UNLOCK_TCPIP_CORE(); } else - printf("buffer too small 3"); + syslog("buffer too small 3\n"); } } vTaskDelayMs(500); - { - uint8_t response[3]; - uint16_t val = 0; - val = sdk_system_adc_read(); - response[2] = (uint8_t) val; - response[1] = val >> 8; - response[0] = 'V'; - websocket_write(pcb, response, 3, WS_BIN_MODE); - } - vTaskDelayMs(500); } - vTaskDelete(NULL); + syslog_detach(); + + vTaskDelete(nullptr); } +struct fw_frame { + char t; + uint8_t reserved[3]; + uint16_t seq; + uint16_t len; + uint32_t hash; + uint8_t data[]; +} __attribute__((packed)); + +struct fw_check { + char t; + uint8_t reserved[3]; + uint32_t len; + uint32_t hash; +} __attribute__((packed)); + /** * This function is called when websocket frame is received. * * Note: this function is executed on TCP thread and should return as soon * as possible. */ -void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, uint8_t mode) { +void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, + uint8_t /*mode*/) { //mode should be WS_BIN_MODE or WS_TEXT_MODE - uint8_t response[3]; - uint16_t val = 0; - char cmd = '0'; + uint8_t response[4]; + auto &cmd = (char &) response[0]; + auto &ret = response[1]; + auto &val = (uint16_t &) response[2]; + cmd = '0'; + ret = ERROR; + val = 0; + + bool togl = false; switch (data[0]) { + case 'R': // Restart + cmd = 'R'; + ret = OK; + break; + case 'X': // Clear Config + cmd = 'X'; + ret = OK; + break; case 'D': // Disable LED + syslog("G\n"); signal_led(false); - val = 1; cmd = 'G'; + ret = OK; + val = 1; break; case 'E': // Enable LED + syslog("E\n"); signal_led(true); - val = 0; cmd = 'G'; + ret = OK; + val = 0; + break; + case 'F': + togl = !togl; + signal_led(togl); + { + auto *f = (fw_frame *) data; + if(f->seq == 0) { + system_otaflash_init(); + } + uint16_t ack = 0; + ret = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash), &ack); + val = htons(ack); + } + cmd = 'F'; + break; + case 'C': + signal_led(false); + { + auto *f = (fw_check *) data; + ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash)); + } + cmd = 'C'; break; default: printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data); - printf("Unknown command\n"); - val = 0; + printf("Unknown command %c\n", data[0]); + ret = ERROR; break; } - response[2] = (uint8_t) val; - response[1] = val >> 8; - response[0] = cmd; + LOCK_TCPIP_CORE(); + websocket_write(pcb, response, 4, WS_BIN_MODE); + UNLOCK_TCPIP_CORE(); - websocket_write(pcb, response, 3, WS_BIN_MODE); + if(ret == OK) { + if(cmd == 'R' || cmd == 'C') { // Restart + printf("rebooting now"); + vTaskDelay(1000 / portTICK_PERIOD_MS); + vPortEnterCritical(); + sdk_system_restart(); + } else if(cmd == 'X') { // Clear Config + vTaskDelay(1000 / portTICK_PERIOD_MS); + system_clear_config(); + } + } } /** @@ -262,10 +328,10 @@ void websocket_open_cb(struct tcp_pcb *pcb, const char *uri) { } extern "C" void httpd_task(void *pvParameters) { + (void) pvParameters; while (!uxSemaphoreGetCount(wifi_available_semaphore)) vTaskDelay(500 / portTICK_PERIOD_MS); - websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb); httpd_init(); diff --git a/firmware/webdir/404.html b/firmware/webdir/404.html new file mode 100644 index 0000000..ebea9c6 --- /dev/null +++ b/firmware/webdir/404.html @@ -0,0 +1,22 @@ + + + + + + + HTTP Server + + + + +
+

404 - Page not found

+
Sorry, the page you are requesting was not found on this server.
+
+ + + diff --git a/firmware/fsdata/fs/css/picnic.min.css b/firmware/webdir/css/picnic.min.css similarity index 100% rename from firmware/fsdata/fs/css/picnic.min.css rename to firmware/webdir/css/picnic.min.css diff --git a/firmware/fsdata/fs/css/style.css b/firmware/webdir/css/style.css similarity index 63% rename from firmware/fsdata/fs/css/style.css rename to firmware/webdir/css/style.css index 0e0e33a..c84c3d9 100644 --- a/firmware/fsdata/fs/css/style.css +++ b/firmware/webdir/css/style.css @@ -6,15 +6,15 @@ main { margin-right: auto; } -canvas{ +canvas { width: 100%; } -main section:target ~ section, main section#io, main section#wifi, main section#ota { +main section:target ~ section, main section#io, main section#wifi, main section#ota { display: none; } -main section:target{ +main section:target { display: block !important; } @@ -22,30 +22,34 @@ main section:target{ width: 100%; display: table; } -.table>.row{ + +.table > .row { display: table-row; } -.table>.row:nth-child(2n) { - background: rgba(17,17,17,0.05); + +.table > .row:nth-child(2n) { + background: rgba(17, 17, 17, 0.05); } -.table>.row>*{ - display: table-cell; - padding: .3em 2.4em .3em .6em; + +.table > .row > * { + display: table-cell; + padding: .3em .6em .3em .6em; } -.table>header.row>*{ + +.table > header.row > * { text-align: left; font-weight: 900; color: #fff; background-color: #0074d9; } -.table>.row>input{ +.table > .row > input { border: none; background: none; font-weight: 900; } -.plain{ +.plain { opacity: initial; width: initial; } \ No newline at end of file diff --git a/firmware/fsdata/fs/index.html b/firmware/webdir/index.html similarity index 56% rename from firmware/fsdata/fs/index.html rename to firmware/webdir/index.html index 2244e40..317dc10 100644 --- a/firmware/fsdata/fs/index.html +++ b/firmware/webdir/index.html @@ -5,6 +5,7 @@ fiatlux v0.2 +
+
+

System

+
+
+

Firmware Update

+
+
+
+ +
+
+
+
+
+
+
+ +
+
+
+
+
+

Restart

+
+
+
+ +
+
+
+
+
+

Reset Config

+
+
+
+ +
+
+
+
+
+

Syslog

+
+
+
+

+                
+
+
+ +

Status

@@ -133,19 +189,23 @@ diff --git a/firmware/fsdata/fs/js/smoothie_min.js b/firmware/webdir/js/smoothie_min.js similarity index 100% rename from firmware/fsdata/fs/js/smoothie_min.js rename to firmware/webdir/js/smoothie_min.js diff --git a/firmware/wifi.cpp b/firmware/wifi.cpp index 65b114b..729e71a 100644 --- a/firmware/wifi.cpp +++ b/firmware/wifi.cpp @@ -3,6 +3,7 @@ // #include "wifi.h" +#include "log.h" #include #include @@ -36,7 +37,7 @@ SemaphoreHandle_t wifi_available_semaphore = nullptr; char *wifi_ap_ip_addr = nullptr; sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr); if(!wifi_ap_ip_addr) { - printf("dns: no ip address\n"); + syslog("dns: no ip address\n"); vTaskDelete(nullptr); } ip4_addr_t server_addr; @@ -194,7 +195,7 @@ extern "C" void wifi_task(void *pvParameters) { /* If the ssid and password are not valid then disable the AP interface. */ if(!wifi_ap_ssid || strlen(wifi_ap_ssid) < 1 || strlen(wifi_ap_ssid) >= 32 || !wifi_ap_password || strlen(wifi_ap_password) < 8 || strlen(wifi_ap_password) >= 64) { - printf("len err\n"); + syslog("len err\n"); wifi_ap_enable = 0; } } diff --git a/modules/rtos b/modules/rtos index 503e66a..7faa16b 160000 --- a/modules/rtos +++ b/modules/rtos @@ -1 +1 @@ -Subproject commit 503e66a500419e8863998b7ea784c5e26a7a5f7c +Subproject commit 7faa16b07ce0d606f9525a316990da5b58e61314 diff --git a/pcb/.gitignore b/pcb/.gitignore index 5d83bb6..5efb808 100644 --- a/pcb/.gitignore +++ b/pcb/.gitignore @@ -31,6 +31,8 @@ fp-info-cache *.wrl *.step -*-bak +*-backups/ gen/ pcb.zip + +report.txt \ No newline at end of file diff --git a/webapp/.gitignore b/webapp/.gitignore new file mode 100644 index 0000000..dd60b59 --- /dev/null +++ b/webapp/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +src/gen/ +package-lock.json \ No newline at end of file