Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE RX: fix for empty text prompt crash #2468

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions firmware/application/apps/ble_rx_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ using namespace portapack;
using namespace modems;
namespace fs = std::filesystem;

#define BLE_RX_NO_ERROR 0
#define BLE_RX_LIST_FILENAME_EMPTY_ERROR 1
#define BLE_RX_ENTRY_FILENAME_EMPTY_ERROR 2
#define BLE_RX_LIST_SAVE_ERROR 3
#define BLE_RX_ENTRY_SAVE_ERROR 4

static uint8_t ble_rx_error = BLE_RX_NO_ERROR;

void BLELogger::log_raw_data(const std::string& data) {
log_file.write_entry(data);
}
Expand Down Expand Up @@ -202,19 +210,23 @@ BleRecentEntryDetailView::BleRecentEntryDetailView(NavigationView& nav, const Bl
}

void BleRecentEntryDetailView::on_save_file(const std::string value, BLETxPacket packetToSave) {
ensure_directory(packet_save_path);
auto folder = packet_save_path.parent_path();
auto ext = packet_save_path.extension();
auto new_path = folder / value + ext;

saveFile(new_path, packetToSave);
if (value.length() > 0) {
ensure_directory(packet_save_path);
auto folder = packet_save_path.parent_path();
auto ext = packet_save_path.extension();
auto new_path = folder / value + ext;
ble_rx_error = saveFile(new_path, packetToSave);
} else {
nav_.pop();
ble_rx_error = BLE_RX_ENTRY_FILENAME_EMPTY_ERROR;
}
}

bool BleRecentEntryDetailView::saveFile(const std::filesystem::path& path, BLETxPacket packetToSave) {
File f;
auto error = f.create(path);
if (error)
return false;
return BLE_RX_ENTRY_SAVE_ERROR;

std::string macAddressStr = packetToSave.macAddress;
std::string advertisementDataStr = packetToSave.advertisementData;
Expand All @@ -224,7 +236,7 @@ bool BleRecentEntryDetailView::saveFile(const std::filesystem::path& path, BLETx

f.write(packetString.c_str(), packetString.length());

return true;
return BLE_RX_NO_ERROR;
}

void BleRecentEntryDetailView::update_data() {
Expand Down Expand Up @@ -568,11 +580,14 @@ std::string BLERxView::build_line_str(BleRecentEntry entry) {
}

void BLERxView::on_save_file(const std::string value) {
auto folder = packet_save_path.parent_path();
auto ext = packet_save_path.extension();
auto new_path = folder / value + ext;

saveFile(new_path);
if (value.length() > 0) {
auto folder = packet_save_path.parent_path();
auto ext = packet_save_path.extension();
auto new_path = folder / value + ext;
ble_rx_error = saveFile(new_path);
} else {
ble_rx_error = BLE_RX_LIST_FILENAME_EMPTY_ERROR;
}
}

bool BLERxView::saveFile(const std::filesystem::path& path) {
Expand All @@ -584,7 +599,7 @@ bool BLERxView::saveFile(const std::filesystem::path& path) {
auto error = src->open(path, false, true);

if (error) {
return false;
return BLE_RX_LIST_SAVE_ERROR;
}

for (const auto& entry : recent) {
Expand Down Expand Up @@ -615,7 +630,7 @@ bool BLERxView::saveFile(const std::filesystem::path& path) {
auto error = dst->open(tempFilePath, false, true);

if (error) {
return false;
return BLE_RX_LIST_SAVE_ERROR;
}

dst->write_line(headerStr.c_str());
Expand Down Expand Up @@ -695,7 +710,7 @@ bool BLERxView::saveFile(const std::filesystem::path& path) {

tempList.clear();

return true;
return BLE_RX_NO_ERROR;
}

void BLERxView::on_data(BlePacketData* packet) {
Expand Down Expand Up @@ -831,6 +846,18 @@ void BLERxView::on_timer() {
baseband::set_btlerx(randomChannel);
}
}
if (ble_rx_error != BLE_RX_NO_ERROR) {
if (ble_rx_error == BLE_RX_LIST_FILENAME_EMPTY_ERROR) {
nav_.display_modal("Error", "List filename is empty !");
} else if (ble_rx_error == BLE_RX_ENTRY_FILENAME_EMPTY_ERROR) {
nav_.display_modal("Error", "Entry filename is empty !");
} else if (ble_rx_error == BLE_RX_LIST_SAVE_ERROR) {
nav_.display_modal("Error", "Couldn't save list !");
} else if (ble_rx_error == BLE_RX_ENTRY_SAVE_ERROR) {
nav_.display_modal("Error", "Couldn't save entry !");
}
ble_rx_error = BLE_RX_NO_ERROR;
}
}

void BLERxView::handle_entries_sort(uint8_t index) {
Expand Down
Loading