From b178462e479cc4dd7ce9965081a6efab845aa898 Mon Sep 17 00:00:00 2001 From: Netro <146584182+iNetro@users.noreply.github.com> Date: Sat, 4 Nov 2023 03:29:48 -0400 Subject: [PATCH] BLE Rx improvements. (#1550) * Added parsing for ble name added focus for ble tx play button. * Added sort by name * Fixed issue where we were pushing too early before pop was finished. * Fix ordering * Fix field location. * truncating entries to stop memory overflow * Added Number of Hits. * Limiting number of hits to uint16_t, as any higher would overflow screen * Limiting number of hits to uint16_t due to possible screen overflow. * Stop creating a log every time app starts. Cluttering log folder. --- firmware/application/apps/ble_rx_app.cpp | 107 +++++++++++++++++++---- firmware/application/apps/ble_rx_app.hpp | 61 +++++++------ firmware/application/apps/ble_tx_app.cpp | 6 +- 3 files changed, 128 insertions(+), 46 deletions(-) diff --git a/firmware/application/apps/ble_rx_app.cpp b/firmware/application/apps/ble_rx_app.cpp index 8fccb720..3d64ce05 100644 --- a/firmware/application/apps/ble_rx_app.cpp +++ b/firmware/application/apps/ble_rx_app.cpp @@ -64,17 +64,33 @@ void RecentEntriesTable::draw( const Rect& target_rect, Painter& painter, const Style& style) { - std::string line = to_string_mac_address(entry.packetData.macAddress, 6); + std::string line{}; - // Handle spacing for negative sign. - uint8_t db_spacing = entry.dbValue > 0 ? 7 : 6; + if (!entry.nameString.empty()) { + line = entry.nameString; - // Pushing single digit values down right justified. - if (entry.dbValue > 9 || entry.dbValue < -9) { - db_spacing--; + if (line.length() < 17) { + line += pad_string_with_spaces(17 - line.length()); + } else { + line = truncate(line, 17); + } + } else { + line = to_string_mac_address(entry.packetData.macAddress, 6); } - line += pad_string_with_spaces(db_spacing) + to_string_dec_int(entry.dbValue); + // Pushing single digit values down right justified. + std::string hitsStr = to_string_dec_int(entry.numHits); + int hitsDigits = hitsStr.length(); + uint8_t hits_spacing = 8 - hitsDigits; + + // Pushing single digit values down right justified. + std::string dbStr = to_string_dec_int(entry.dbValue); + int dbDigits = dbStr.length(); + uint8_t db_spacing = 5 - dbDigits; + + line += pad_string_with_spaces(hits_spacing) + hitsStr; + + line += pad_string_with_spaces(db_spacing) + dbStr; line.resize(target_rect.width() / 8, ' '); painter.draw_string(target_rect.location(), style, line); @@ -84,8 +100,12 @@ BleRecentEntryDetailView::BleRecentEntryDetailView(NavigationView& nav, const Bl : nav_{nav}, entry_{entry} { add_children({&button_done, + &label_mac_address, + &text_mac_address, &labels}); + text_mac_address.set(to_string_mac_address(entry.packetData.macAddress, 6)); + button_done.on_select = [this](const ui::Button&) { nav_.pop(); }; @@ -118,7 +138,7 @@ void BleRecentEntryDetailView::paint(Painter& painter) { const auto s = style(); const auto rect = screen_rect(); - auto field_rect = Rect{rect.left(), rect.top() + 16, rect.width(), 16}; + auto field_rect = Rect{rect.left(), rect.top() + 48, rect.width(), 16}; uint8_t type[total_data_lines]; uint8_t length[total_data_lines]; @@ -217,7 +237,7 @@ BLERxView::BLERxView(NavigationView& nav) &check_log, &label_sort, &options_sort, - &button_message, + &button_filter, &button_switch, &recent_entries_view, &recent_entries_filter_view, @@ -234,7 +254,7 @@ BLERxView::BLERxView(NavigationView& nav) nav_.push(entry); }; - button_message.on_select = [this, &nav](Button&) { + button_filter.on_select = [this, &nav](Button&) { text_prompt( nav, filterBuffer, @@ -245,8 +265,8 @@ BLERxView::BLERxView(NavigationView& nav) }; button_switch.on_select = [this, &nav](Button&) { - nav.pop(); - nav.push(); + nav_.set_on_pop([this]() { nav_.push(); }); + nav_.pop(); }; field_frequency.set_step(0); @@ -256,6 +276,9 @@ BLERxView::BLERxView(NavigationView& nav) check_log.on_select = [this](Checkbox&, bool v) { str_log = ""; logging = v; + + if (logger && logging) + logger->append(LOG_ROOT_DIR "/BLELOG_" + to_string_timestamp(rtc_time::now()) + ".TXT"); }; options_channel.on_change = [this](size_t, int32_t i) { @@ -274,17 +297,29 @@ BLERxView::BLERxView(NavigationView& nav) filterEntries, [](const BleRecentEntry& entry) { return entry.macAddress; }, true); break; case 1: + sortEntriesBy( + recent, [](const BleRecentEntry& entry) { return entry.numHits; }, false); + sortEntriesBy( + filterEntries, [](const BleRecentEntry& entry) { return entry.numHits; }, false); + break; + case 2: sortEntriesBy( recent, [](const BleRecentEntry& entry) { return entry.dbValue; }, true); sortEntriesBy( filterEntries, [](const BleRecentEntry& entry) { return entry.dbValue; }, true); break; - case 2: + case 3: sortEntriesBy( recent, [](const BleRecentEntry& entry) { return entry.timestamp; }, false); sortEntriesBy( filterEntries, [](const BleRecentEntry& entry) { return entry.timestamp; }, false); break; + case 4: + sortEntriesBy( + recent, [](const BleRecentEntry& entry) { return entry.nameString; }, true); + sortEntriesBy( + filterEntries, [](const BleRecentEntry& entry) { return entry.nameString; }, true); + break; default: break; } @@ -298,9 +333,6 @@ BLERxView::BLERxView(NavigationView& nav) logger = std::make_unique(); - if (logger) - logger->append(LOG_ROOT_DIR "/BLELOG_" + to_string_timestamp(rtc_time::now()) + ".TXT"); - // Auto-configure modem for LCR RX (will be removed later) baseband::set_btlerx(channel_number); @@ -376,6 +408,7 @@ void BLERxView::on_data(BlePacketData* packet) { // Start of Packet stuffing. // Masking off the top 2 bytes to avoid invalid keys. auto& entry = ::on_packet(recent, macAddressEncoded & 0xFFFFFFFFFFFF); + truncate_entries(recent, 32); updateEntry(packet, entry); // Log at End of Packet. @@ -442,11 +475,37 @@ void BLERxView::updateEntry(const BlePacketData* packet, BleRecentEntry& entry) entry.packetData.macAddress[4] = packet->macAddress[4]; entry.packetData.macAddress[5] = packet->macAddress[5]; + entry.numHits++; + for (int i = 0; i < packet->dataLen; i++) { entry.packetData.data[i] = packet->data[i]; } - // entry.update(packet); + entry.nameString = ""; + + uint8_t currentByte = 0; + uint8_t length = 0; + uint8_t type = 0; + + bool stringFound = false; + + for (currentByte = 0; (currentByte < entry.packetData.dataLen);) { + length = entry.packetData.data[currentByte++]; + type = entry.packetData.data[currentByte++]; + + // Subtract 1 because type is part of the length. + for (int i = 0; i < length - 1; i++) { + if (((type == 0x08) || (type == 0x09)) && !stringFound) { + entry.nameString += (char)entry.packetData.data[currentByte]; + } + + currentByte++; + } + + if (!entry.nameString.empty()) { + stringFound = true; + } + } switch (options_sort.selected_index()) { case 0: @@ -456,17 +515,29 @@ void BLERxView::updateEntry(const BlePacketData* packet, BleRecentEntry& entry) filterEntries, [](const BleRecentEntry& entry) { return entry.macAddress; }, true); break; case 1: + sortEntriesBy( + recent, [](const BleRecentEntry& entry) { return entry.numHits; }, false); + sortEntriesBy( + filterEntries, [](const BleRecentEntry& entry) { return entry.numHits; }, false); + break; + case 2: sortEntriesBy( recent, [](const BleRecentEntry& entry) { return entry.dbValue; }, true); sortEntriesBy( filterEntries, [](const BleRecentEntry& entry) { return entry.dbValue; }, true); break; - case 2: + case 3: sortEntriesBy( recent, [](const BleRecentEntry& entry) { return entry.timestamp; }, false); sortEntriesBy( filterEntries, [](const BleRecentEntry& entry) { return entry.timestamp; }, false); break; + case 4: + sortEntriesBy( + recent, [](const BleRecentEntry& entry) { return entry.nameString; }, true); + sortEntriesBy( + filterEntries, [](const BleRecentEntry& entry) { return entry.nameString; }, true); + break; default: break; } diff --git a/firmware/application/apps/ble_rx_app.hpp b/firmware/application/apps/ble_rx_app.hpp index 8aac62c9..da5c0670 100644 --- a/firmware/application/apps/ble_rx_app.hpp +++ b/firmware/application/apps/ble_rx_app.hpp @@ -78,6 +78,8 @@ struct BleRecentEntry { BlePacketData packetData; std::string timestamp; std::string dataString; + std::string nameString; + uint16_t numHits; BleRecentEntry() : BleRecentEntry{0} { @@ -89,7 +91,9 @@ struct BleRecentEntry { dbValue{}, packetData{}, timestamp{}, - dataString{} { + dataString{}, + nameString{}, + numHits{} { } Key key() const { @@ -117,10 +121,17 @@ class BleRecentEntryDetailView : public View { static constexpr uint8_t total_data_lines{5}; + Labels label_mac_address{ + {{0 * 8, 0 * 16}, "Mac Address:", Color::light_grey()}}; + + Text text_mac_address{ + {12 * 8, 0 * 16, 17 * 8, 16}, + "-"}; + Labels labels{ - {{0 * 8, 0 * 16}, "Len", Color::light_grey()}, - {{5 * 8, 0 * 16}, "Type", Color::light_grey()}, - {{10 * 8, 0 * 16}, "Value", Color::light_grey()}, + {{0 * 8, 2 * 16}, "Len", Color::light_grey()}, + {{5 * 8, 2 * 16}, "Type", Color::light_grey()}, + {{10 * 8, 2 * 16}, "Value", Color::light_grey()}, }; Button button_done{ @@ -199,26 +210,28 @@ class BLERxView : public View { Channel channel{ {24 * 8, 5, 6 * 8, 4}}; + Labels label_sort{ + {{0 * 8, 3 * 8}, "Sort:", Color::light_grey()}}; + + OptionsField options_sort{ + {5 * 8, 3 * 8}, + 4, + {{"MAC", 0}, + {"Hits", 1}, + {"dB", 2}, + {"Time", 3}, + {"Name", 4}}}; + + Button button_filter{ + {12 * 8, 3 * 8, 4 * 8, 16}, + "Filter"}; + Checkbox check_log{ - {0 * 8, 3 * 8}, + {20 * 8, 3 * 8}, 3, "Log", true}; - Labels label_sort{ - {{6 * 8, 3 * 8}, "Sort:", Color::light_grey()}}; - - OptionsField options_sort{ - {12 * 8, 3 * 8}, - 6, - {{"MAC", 0}, - {"dB", 1}, - {"Recent", 2}}}; - - Button button_message{ - {22 * 8, 3 * 8, 4 * 8, 16}, - "Filter"}; - Console console{ {0, 4 * 16, 240, 240}}; @@ -231,17 +244,13 @@ class BLERxView : public View { std::unique_ptr logger{}; - // const RecentEntriesColumns columns{{{"Source", 9}, - // {"Loc", 6}, - // {"Hits", 4}, - // {"Time", 8}}}; - BleRecentEntries recent{}; BleRecentEntries filterEntries{}; const RecentEntriesColumns columns{{ - {"Mac Address", 20}, - {"dB", 20}, + {"Mac Address", 17}, + {"Hits", 7}, + {"dB", 4}, }}; BleRecentEntry entry_{}; diff --git a/firmware/application/apps/ble_tx_app.cpp b/firmware/application/apps/ble_tx_app.cpp index 1f7dfb80..19286213 100644 --- a/firmware/application/apps/ble_tx_app.cpp +++ b/firmware/application/apps/ble_tx_app.cpp @@ -319,12 +319,14 @@ BLETxView::BLETxView(NavigationView& nav) auto open_view = nav.push(".TXT"); open_view->on_changed = [this](std::filesystem::path new_file_path) { on_file_changed(new_file_path); + + nav_.set_on_pop([this]() { button_play.focus(); }); }; }; button_switch.on_select = [this, &nav](Button&) { - nav.pop(); - nav.push(); + nav_.set_on_pop([this]() { nav_.push(); }); + nav_.pop(); }; }