Skip to content

Commit

Permalink
Merge pull request #329 from 4ms/wifi_remaining_features
Browse files Browse the repository at this point in the history
Wifi remaining features
  • Loading branch information
danngreen authored Sep 26, 2024
2 parents bcb671a + 79729f7 commit 6136dfe
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 42 deletions.
6 changes: 3 additions & 3 deletions firmware/flashing/manifest_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ def parse_file_version(version):
if args.assets_file:
j["files"].append(process_file(destination_dir, args.assets_file, "app", name="Assets", address=0xa00000))

if args.wifi_fs_file:
j["files"].append(process_file(destination_dir, args.wifi_fs_file, "wifi", name="Wifi Filesystem", address=0x200000, compressed=True))

if args.wifi_bl_file:
j["files"].append(process_file(destination_dir, args.wifi_bl_file, "wifi", name="Wifi Bootloader", address=0x0))

if args.wifi_app_file:
j["files"].append(process_file(destination_dir, args.wifi_app_file, "wifi", name="Wifi Application", address=0x10000))

if args.wifi_fs_file:
j["files"].append(process_file(destination_dir, args.wifi_fs_file, "wifi", name="Wifi Filesystem", address=0x200000, compressed=True))

with open(args.out_file, "w+") as out_file:
data_json = json.dumps(j, indent=4)
out_file.write(data_json)
Expand Down
2 changes: 1 addition & 1 deletion firmware/lib/esp-serial-flasher
4 changes: 3 additions & 1 deletion firmware/src/core_intercom/intercore_message.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ struct IntercoreStorageMessage {
ChecksumMatch,
ChecksumMismatch,
ChecksumFailed,
ReadFlashFailed,
WifiExpanderNotConnected,
WifiExpanderCommError,

StartFlashing, // write to Flash or WifiExpander
FlashingOk,
FlashingFailed,

RequestReadFlash,
ReadFlashOk,
ReadFlashFailed,

RequestWriteFile,
WriteFileFail,
Expand Down
11 changes: 6 additions & 5 deletions firmware/src/fw_update/firmware_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ IntercoreStorageMessage FirmwareWriter::compareChecksumWifi(uint32_t address, ui
returnValue = {.message_type = ChecksumMismatch};

} else if (result == ESP_LOADER_SUCCESS) {
pr_dbg("-> Checksum matches\n");
pr_trace("-> Checksum matches\n");
returnValue = {.message_type = ChecksumMatch};

} else {
pr_err("-> Cannot get checksum\n");
returnValue = {.message_type = ChecksumFailed};
pr_trace("-> Cannot get checksum\n");
returnValue = {.message_type = WifiExpanderCommError};
}

} else {
pr_err("Cannot connect to wifi bootloader\n");
returnValue = {.message_type = ChecksumFailed};
returnValue = {.message_type = WifiExpanderNotConnected};
}

Flasher::deinit();
Expand All @@ -102,6 +102,7 @@ IntercoreStorageMessage FirmwareWriter::flashWifi(std::span<uint8_t> buffer,
if (result == ESP_LOADER_SUCCESS) {
const std::size_t BatchSize = 1024;

HAL_Delay(20);
result = Flasher::flash_start(address, buffer.size(), BatchSize, uncompressed_size);

if (result == ESP_LOADER_SUCCESS) {
Expand All @@ -127,7 +128,7 @@ IntercoreStorageMessage FirmwareWriter::flashWifi(std::span<uint8_t> buffer,
}

if (not error_during_writes) {
pr_dbg("-> Flashing completed\n");
pr_trace("-> Flashing completed\n");
returnValue = {.message_type = FlashingOk};
} else {
pr_trace("-> Flashing failed\n");
Expand Down
24 changes: 20 additions & 4 deletions firmware/src/fw_update/updater_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ bool FirmwareUpdaterProxy::start(std::string_view manifest_filename,
}

FirmwareUpdaterProxy::Status FirmwareUpdaterProxy::process() {
error_message = "";

switch (state) {
case Idle:
case Success:
Expand Down Expand Up @@ -187,10 +189,25 @@ FirmwareUpdaterProxy::Status FirmwareUpdaterProxy::process() {
moveToState(Writing);
break;

case FileStorageProxy::WifiExpanderCommError:
case FileStorageProxy::ReadFlashFailed:
case FileStorageProxy::ChecksumFailed:
abortWithMessage("Error when comparing checksums");
break;

case FileStorageProxy::WifiExpanderNotConnected: {
error_message = "No Wifi Expander: skipping ";

auto full_path = manifest.files[current_file_idx].filename;
auto slash_pos = full_path.find_first_of("/");
if (slash_pos != std::string::npos)
error_message += full_path.substr(slash_pos + 1);
else
error_message += full_path;

proceedWithNextFile();
} break;

default:
break;
}
Expand All @@ -217,7 +234,8 @@ FirmwareUpdaterProxy::Status FirmwareUpdaterProxy::process() {
&sharedMem->bytes_processed);

if (not result) {
abortWithMessage("Cannot trigger flashing");
// this is not an error, just need to retry
justEnteredState = true;
}
} else {
abortWithMessage("Invalid update file type");
Expand All @@ -244,9 +262,7 @@ FirmwareUpdaterProxy::Status FirmwareUpdaterProxy::process() {
abortWithMessage("Internal Error");
}

return state == State::Error ?
Status{state, current_file_name, current_file_size, sharedMem->bytes_processed, error_message} :
Status{state, current_file_name, current_file_size, sharedMem->bytes_processed};
return Status{state, current_file_name, current_file_size, sharedMem->bytes_processed, error_message};
}

void FirmwareUpdaterProxy::abortWithMessage(const char *message) {
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/fw_update/updater_proxy.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public:
std::string name;
std::size_t file_size{0};
std::size_t bytes_completed{0};
std::string error_message{};
std::string message{};
};

FirmwareUpdaterProxy(FileStorageProxy &file_storage);
Expand Down
14 changes: 13 additions & 1 deletion firmware/src/gui/pages/firmware_update_tab.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct FirmwareUpdateTab : SystemMenuTab {
display_file_not_found();

confirm_popup.init(ui_SystemMenu, group);
lv_label_set_text(ui_SystemMenuUpdateLog, "");
lv_hide(ui_SystemMenUpdateProgressBar);
}

// Returns true if this pages uses the back event
Expand Down Expand Up @@ -84,8 +86,11 @@ struct FirmwareUpdateTab : SystemMenuTab {
case State::Updating: {
auto status = updater.process();

if (status.state != FirmwareUpdaterProxy::Error && status.message.length())
append_log_message(status.message);

if (status.state == FirmwareUpdaterProxy::Error) {
display_update_failed(status.error_message);
display_update_failed(status.message);
state = State::Failed;

} else if (status.state == FirmwareUpdaterProxy::LoadingUpdateFiles) {
Expand Down Expand Up @@ -204,6 +209,13 @@ private:
lv_hide(ui_SystemMenUpdateProgressBar);
}

void append_log_message(std::string_view message) {
std::string log = lv_label_get_text(ui_SystemMenuUpdateLog);
lv_label_set_text_fmt(ui_SystemMenuUpdateLog, "%s\n%.*s", log.c_str(), (int)message.length(), message.data());
lv_hide(ui_FWUpdateSpinner);
lv_hide(ui_SystemMenUpdateProgressBar);
}

void display_success() {
lv_obj_set_style_text_color(ui_SystemMenuUpdateMessage, lv_palette_lighten(LV_PALETTE_GREEN, 1), LV_PART_MAIN);
lv_label_set_text(ui_SystemMenuUpdateMessage, "Success! Firmware has been updated. Power off and back on now");
Expand Down
9 changes: 9 additions & 0 deletions firmware/src/gui/slsexport/meta5/screens/ui_SystemMenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1299,4 +1299,13 @@ lv_obj_set_style_text_color(ui_SystemMenuUpdateLog, lv_color_hex(0xFFFFFF), LV_P
lv_obj_set_style_text_opa(ui_SystemMenuUpdateLog, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
lv_obj_set_style_text_font(ui_SystemMenuUpdateLog, &ui_font_MuseoSansRounded50012, LV_PART_MAIN| LV_STATE_DEFAULT);

ui_SystemMenuUpdateLog = lv_label_create(ui_SystemMenuUpdateTab);
lv_obj_set_width( ui_SystemMenuUpdateLog, lv_pct(100));
lv_obj_set_height( ui_SystemMenuUpdateLog, LV_SIZE_CONTENT); /// 1
lv_obj_set_align( ui_SystemMenuUpdateLog, LV_ALIGN_CENTER );
lv_label_set_text(ui_SystemMenuUpdateLog,"");
lv_obj_set_style_text_color(ui_SystemMenuUpdateLog, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
lv_obj_set_style_text_opa(ui_SystemMenuUpdateLog, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
lv_obj_set_style_text_font(ui_SystemMenuUpdateLog, &ui_font_MuseoSansRounded50012, LV_PART_MAIN| LV_STATE_DEFAULT);

}
2 changes: 1 addition & 1 deletion firmware/src/wifi/comm/flat
Submodule flat updated 1 files
+10 −5 all.fbs
94 changes: 69 additions & 25 deletions firmware/src/wifi/comm/wifi_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,59 @@ namespace MetaModule::WifiInterface
PatchStorage *patchStorage;

flatbuffers::Offset<Message> constructPatchesMessage(flatbuffers::FlatBufferBuilder &fbb) {
auto CreateVector = [&fbb](auto fileList) {
std::vector<flatbuffers::Offset<PatchInfo>> elems(fileList.files.size());

auto ExtractFileInfo = [&fbb](auto& thisFile)
{
auto thisName = fbb.CreateString(std::string_view(thisFile.patchname));
auto thisFilename = fbb.CreateString(std::string_view(thisFile.filename));

return CreatePatchInfo(fbb, thisName, thisFilename, thisFile.filesize, thisFile.timestamp);
};

auto ExtractFileFromDir = [&fbb, &ExtractFileInfo](const auto& fileList)
{
std::vector<flatbuffers::Offset<PatchInfo>> fileInfos(fileList.files.size());
for (std::size_t i = 0; i < fileList.files.size(); i++) {
auto thisName = fbb.CreateString(std::string_view(fileList.files[i].patchname));
fileInfos[i] = ExtractFileInfo(fileList.files[i]);
};
auto files = fbb.CreateVector(fileInfos);

auto thisFilename = fbb.CreateString(std::string_view(fileList.files[i].filename));
auto thisInfo = CreatePatchInfo(fbb, thisName, thisFilename);
elems[i] = thisInfo;
return files;
};

auto ExtractDirFull = [&fbb, &ExtractFileFromDir](const auto& fileList, std::optional<std::string_view> overrideName)
{
auto FixDirName = [](std::string_view in)
{
// remove extra leading slash
return in.substr(1);
};
//TODO: add directories, and files inside directories
return fbb.CreateVector(elems);

std::vector<flatbuffers::Offset<DirInfo>> dirInfos(fileList.dirs.size());
for (std::size_t i=0; i<fileList.dirs.size(); i++)
{
auto files = ExtractFileFromDir(fileList.dirs[i]);
auto name = fbb.CreateString(FixDirName(std::string_view(fileList.dirs[i].name)));

dirInfos[i] = CreateDirInfo(fbb, name, 0, files);
}
auto dirs = fbb.CreateVector(dirInfos);

auto files = ExtractFileFromDir(fileList);

auto name = overrideName.has_value()
? fbb.CreateString(std::string_view(*overrideName))
: fbb.CreateString(FixDirName(std::string_view(fileList.name)));

return CreateDirInfo(fbb, name, dirs, files);
};


auto patchFileList = patchStorage->getPatchList();

auto usbList = CreateVector(patchFileList.volume_root(Volume::USB));
auto flashList = CreateVector(patchFileList.volume_root(Volume::NorFlash));
auto sdcardList = CreateVector(patchFileList.volume_root(Volume::SDCard));
auto usbList = ExtractDirFull(patchFileList.volume_root(Volume::USB), patchFileList.get_vol_name(Volume::USB));
auto flashList = ExtractDirFull(patchFileList.volume_root(Volume::NorFlash), patchFileList.get_vol_name(Volume::NorFlash));
auto sdcardList = ExtractDirFull(patchFileList.volume_root(Volume::SDCard), patchFileList.get_vol_name(Volume::SDCard));

auto patches = CreatePatches(fbb, usbList, flashList, sdcardList);
auto message = CreateMessage(fbb, AnyMessage_Patches, patches.Union());
Expand Down Expand Up @@ -248,32 +283,40 @@ void handle_client_channel(uint8_t destination, std::span<uint8_t> payload) {

sendResponse(fbb.GetBufferSpan());
} else if (auto uploadPatchMessage = message->content_as_UploadPatch(); uploadPatchMessage) {
auto destination = uploadPatchMessage->destination();

assert(uploadPatchMessage->content()->is_span_observable);
auto receivedPatchData =
std::span(uploadPatchMessage->content()->data(), uploadPatchMessage->content()->size());

auto filename = flatbuffers::GetStringView(uploadPatchMessage->filename());

pr_info("Received Patch of %u bytes for location %u\n", receivedPatchData.size(), destination);

auto LocationToVolume = [](auto location) -> std::optional<Volume> {
switch (location) {
case StorageLocation::StorageLocation_USB:
return Volume::USB;
case StorageLocation::StorageLocation_FLASH:
return Volume::NorFlash;
case StorageLocation::StorageLocation_SDCARD:
return Volume::SDCard;
default:
return std::nullopt;
pr_info("Received Patch %.*s of %u bytes\n", filename.size(), filename.data(), receivedPatchData.size());

auto ParseStorageString = [](std::string_view locationName) -> std::optional<Volume> {

if (locationName.compare(PatchDirList::get_vol_name(Volume::USB)) == 0)
{
return Volume::USB;
}
else if (locationName.compare(PatchDirList::get_vol_name(Volume::NorFlash)) == 0)
{
return Volume::NorFlash;
}
else if (locationName.compare(PatchDirList::get_vol_name(Volume::SDCard)) == 0)
{
return Volume::SDCard;
}
else
{
return std::nullopt;
}
};

flatbuffers::FlatBufferBuilder fbb;

if (auto thisVolume = LocationToVolume(destination); thisVolume) {
auto volumeString = flatbuffers::GetStringView(uploadPatchMessage->volume());

if (auto thisVolume = ParseStorageString(volumeString); thisVolume) {
auto success = patchStorage->write_file(*thisVolume, filename, receivedPatchData);

if (success) {
Expand Down Expand Up @@ -307,6 +350,7 @@ void handle_client_channel(uint8_t destination, std::span<uint8_t> payload) {

sendResponse(fbb.GetBufferSpan());


} else {
pr_trace("Other option\n");
}
Expand Down
Binary file modified firmware/src/wifi/flasher/images/application.bin
Binary file not shown.
Binary file modified firmware/src/wifi/flasher/images/filesystem.img
Binary file not shown.

0 comments on commit 6136dfe

Please sign in to comment.