2023-10-23 01:58:14 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2017 Furrtek
|
|
|
|
* Copyright (C) 2023 TJ Baginski
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2023-11-03 03:08:49 -04:00
|
|
|
#include "ble_rx_app.hpp"
|
2023-12-09 12:02:00 -05:00
|
|
|
#include "ble_rx_app.hpp"
|
2023-10-23 01:58:14 -04:00
|
|
|
#include "ui_modemsetup.hpp"
|
|
|
|
|
|
|
|
#include "modems.hpp"
|
|
|
|
#include "audio.hpp"
|
2023-12-09 12:02:00 -05:00
|
|
|
#include "io_file.hpp"
|
2023-10-23 01:58:14 -04:00
|
|
|
#include "rtc_time.hpp"
|
|
|
|
#include "baseband_api.hpp"
|
|
|
|
#include "string_format.hpp"
|
|
|
|
#include "portapack_persistent_memory.hpp"
|
2023-12-09 12:02:00 -05:00
|
|
|
#include "ui_fileman.hpp"
|
2023-11-01 06:46:41 -04:00
|
|
|
#include "ui_textentry.hpp"
|
2023-10-23 01:58:14 -04:00
|
|
|
|
|
|
|
using namespace portapack;
|
|
|
|
using namespace modems;
|
2023-12-09 12:02:00 -05:00
|
|
|
namespace fs = std::filesystem;
|
2023-10-23 01:58:14 -04:00
|
|
|
|
|
|
|
void BLELogger::log_raw_data(const std::string& data) {
|
|
|
|
log_file.write_entry(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string pad_string_with_spaces(int snakes) {
|
|
|
|
std::string paddedStr(snakes, ' ');
|
|
|
|
return paddedStr;
|
|
|
|
}
|
|
|
|
|
2023-10-24 18:08:45 -04:00
|
|
|
uint64_t copy_mac_address_to_uint64(const uint8_t* macAddress) {
|
|
|
|
uint64_t result = 0;
|
|
|
|
|
|
|
|
// Copy each byte of the MAC address to the corresponding byte in the uint64_t.
|
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
|
|
result |= static_cast<uint64_t>(macAddress[i]) << ((5 - i) * 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-11-16 08:46:45 -05:00
|
|
|
void reverse_byte_array(uint8_t* arr, int length) {
|
|
|
|
int start = 0;
|
|
|
|
int end = length - 1;
|
|
|
|
|
|
|
|
while (start < end) {
|
|
|
|
// Swap elements at start and end
|
|
|
|
uint8_t temp = arr[start];
|
|
|
|
arr[start] = arr[end];
|
|
|
|
arr[end] = temp;
|
|
|
|
|
|
|
|
// Move the indices towards the center
|
|
|
|
start++;
|
|
|
|
end--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 01:58:14 -04:00
|
|
|
namespace ui {
|
2023-11-12 02:54:04 -05:00
|
|
|
|
|
|
|
std::string pdu_type_to_string(ADV_PDU_TYPE type) {
|
|
|
|
std::string pduTypeStr = "";
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case ADV_IND:
|
|
|
|
pduTypeStr += "ADV_IND";
|
|
|
|
break;
|
|
|
|
case ADV_DIRECT_IND:
|
|
|
|
pduTypeStr += "ADV_DIRECT_IND";
|
|
|
|
break;
|
|
|
|
case ADV_NONCONN_IND:
|
|
|
|
pduTypeStr += "ADV_NONCONN_IND";
|
|
|
|
break;
|
|
|
|
case SCAN_REQ:
|
|
|
|
pduTypeStr += "SCAN_REQ";
|
|
|
|
break;
|
|
|
|
case SCAN_RSP:
|
|
|
|
pduTypeStr += "SCAN_RSP";
|
|
|
|
break;
|
|
|
|
case CONNECT_REQ:
|
|
|
|
pduTypeStr += "CONNECT_REQ";
|
|
|
|
break;
|
|
|
|
case ADV_SCAN_IND:
|
|
|
|
pduTypeStr += "ADV_SCAN_IND";
|
|
|
|
break;
|
|
|
|
case RESERVED0:
|
|
|
|
case RESERVED1:
|
|
|
|
case RESERVED2:
|
|
|
|
case RESERVED3:
|
|
|
|
case RESERVED4:
|
|
|
|
case RESERVED5:
|
|
|
|
case RESERVED6:
|
|
|
|
case RESERVED7:
|
|
|
|
case RESERVED8:
|
|
|
|
pduTypeStr += "RESERVED";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pduTypeStr += "UNKNOWN";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pduTypeStr;
|
|
|
|
}
|
|
|
|
|
2023-10-23 01:58:14 -04:00
|
|
|
template <>
|
|
|
|
void RecentEntriesTable<BleRecentEntries>::draw(
|
|
|
|
const Entry& entry,
|
|
|
|
const Rect& target_rect,
|
|
|
|
Painter& painter,
|
|
|
|
const Style& style) {
|
2023-11-04 03:29:48 -04:00
|
|
|
std::string line{};
|
2023-11-11 14:46:51 -05:00
|
|
|
line.reserve(30);
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
if (!entry.nameString.empty() && entry.include_name) {
|
2023-11-04 03:29:48 -04:00
|
|
|
line = entry.nameString;
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-11-04 03:29:48 -04:00
|
|
|
if (line.length() < 17) {
|
|
|
|
line += pad_string_with_spaces(17 - line.length());
|
|
|
|
} else {
|
|
|
|
line = truncate(line, 17);
|
|
|
|
}
|
|
|
|
} else {
|
2023-11-06 05:16:01 -05:00
|
|
|
line = to_string_mac_address(entry.packetData.macAddress, 6, false);
|
2023-10-23 01:58:14 -04:00
|
|
|
}
|
|
|
|
|
2023-11-04 03:29:48 -04:00
|
|
|
// 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;
|
2023-10-23 01:58:14 -04:00
|
|
|
|
|
|
|
line.resize(target_rect.width() / 8, ' ');
|
|
|
|
painter.draw_string(target_rect.location(), style, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
BleRecentEntryDetailView::BleRecentEntryDetailView(NavigationView& nav, const BleRecentEntry& entry)
|
|
|
|
: nav_{nav},
|
|
|
|
entry_{entry} {
|
|
|
|
add_children({&button_done,
|
2023-11-06 05:16:01 -05:00
|
|
|
&button_send,
|
2023-11-16 14:00:52 -05:00
|
|
|
&button_save,
|
2023-11-04 03:29:48 -04:00
|
|
|
&label_mac_address,
|
|
|
|
&text_mac_address,
|
2023-11-12 02:54:04 -05:00
|
|
|
&label_pdu_type,
|
|
|
|
&text_pdu_type,
|
2023-10-23 01:58:14 -04:00
|
|
|
&labels});
|
|
|
|
|
2023-11-06 05:16:01 -05:00
|
|
|
text_mac_address.set(to_string_mac_address(entry.packetData.macAddress, 6, false));
|
2023-11-12 02:54:04 -05:00
|
|
|
text_pdu_type.set(pdu_type_to_string(entry.pduType));
|
2023-11-04 03:29:48 -04:00
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
button_done.on_select = [&nav](const ui::Button&) {
|
|
|
|
nav.pop();
|
2023-10-23 01:58:14 -04:00
|
|
|
};
|
2023-11-06 05:16:01 -05:00
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
button_send.on_select = [this, &nav](const ui::Button&) {
|
|
|
|
auto packetToSend = build_packet();
|
|
|
|
nav.set_on_pop([packetToSend, &nav]() {
|
|
|
|
nav.replace<BLETxView>(packetToSend);
|
|
|
|
});
|
|
|
|
nav.pop();
|
2023-11-06 05:16:01 -05:00
|
|
|
};
|
2023-11-16 14:00:52 -05:00
|
|
|
|
|
|
|
button_save.on_select = [this, &nav](const ui::Button&) {
|
|
|
|
auto packetToSave = build_packet();
|
|
|
|
|
|
|
|
packetFileBuffer = "";
|
|
|
|
text_prompt(
|
|
|
|
nav,
|
|
|
|
packetFileBuffer,
|
|
|
|
64,
|
|
|
|
[this, packetToSave](std::string& buffer) {
|
|
|
|
on_save_file(buffer, packetToSave);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BleRecentEntryDetailView::saveFile(const std::filesystem::path& path, BLETxPacket packetToSave) {
|
|
|
|
File f;
|
|
|
|
auto error = f.create(path);
|
|
|
|
if (error)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string macAddressStr = packetToSave.macAddress;
|
|
|
|
std::string advertisementDataStr = packetToSave.advertisementData;
|
|
|
|
std::string packetCountStr = packetToSave.packetCount;
|
|
|
|
|
|
|
|
std::string packetString = macAddressStr + ' ' + advertisementDataStr + ' ' + packetCountStr;
|
|
|
|
|
|
|
|
f.write(packetString.c_str(), packetString.length());
|
|
|
|
|
|
|
|
return true;
|
2023-10-23 01:58:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BleRecentEntryDetailView::update_data() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void BleRecentEntryDetailView::focus() {
|
|
|
|
button_done.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect BleRecentEntryDetailView::draw_field(
|
|
|
|
Painter& painter,
|
|
|
|
const Rect& draw_rect,
|
|
|
|
const Style& style,
|
|
|
|
const std::string& label,
|
|
|
|
const std::string& value) {
|
|
|
|
const int label_length_max = 4;
|
|
|
|
|
|
|
|
painter.draw_string(Point{draw_rect.left(), draw_rect.top()}, style, label);
|
|
|
|
painter.draw_string(Point{draw_rect.left() + (label_length_max + 1) * 8, draw_rect.top()}, style, value);
|
|
|
|
|
|
|
|
return {draw_rect.left(), draw_rect.top() + draw_rect.height(), draw_rect.width(), draw_rect.height()};
|
|
|
|
}
|
|
|
|
|
|
|
|
void BleRecentEntryDetailView::paint(Painter& painter) {
|
|
|
|
View::paint(painter);
|
|
|
|
|
|
|
|
const auto s = style();
|
|
|
|
const auto rect = screen_rect();
|
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
auto field_rect = Rect{rect.left(), rect.top() + 64, rect.width(), 16};
|
2023-10-23 01:58:14 -04:00
|
|
|
|
|
|
|
uint8_t type[total_data_lines];
|
|
|
|
uint8_t length[total_data_lines];
|
|
|
|
uint8_t data[total_data_lines][40];
|
|
|
|
|
|
|
|
int currentByte = 0;
|
|
|
|
int currentPacket = 0;
|
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
|
|
|
int k = 0;
|
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
switch (entry_.pduType) {
|
|
|
|
case ADV_IND:
|
|
|
|
case ADV_NONCONN_IND:
|
|
|
|
case SCAN_RSP:
|
|
|
|
case ADV_SCAN_IND: {
|
2023-11-12 13:10:53 -05:00
|
|
|
ADV_PDU_PAYLOAD_TYPE_0_2_4_6* advertiseData = (ADV_PDU_PAYLOAD_TYPE_0_2_4_6*)entry_.packetData.data;
|
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
for (currentByte = 0; (currentByte < entry_.packetData.dataLen) && (currentPacket < total_data_lines);) {
|
2023-11-12 13:10:53 -05:00
|
|
|
length[currentPacket] = advertiseData->Data[currentByte++];
|
|
|
|
type[currentPacket] = advertiseData->Data[currentByte++];
|
2023-11-12 02:54:04 -05:00
|
|
|
|
|
|
|
// Subtract 1 because type is part of the length.
|
|
|
|
for (i = 0; i < length[currentPacket] - 1; i++) {
|
2023-11-12 13:10:53 -05:00
|
|
|
data[currentPacket][i] = advertiseData->Data[currentByte++];
|
2023-11-12 02:54:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
currentPacket++;
|
|
|
|
}
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
for (i = 0; i < currentPacket; i++) {
|
|
|
|
uint8_t number_data_lines = ceil((float)(length[i] - 1) / 10.0);
|
|
|
|
uint8_t current_line = 0;
|
|
|
|
std::array<std::string, total_data_lines> data_strings{};
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
for (j = 0; (j < (number_data_lines * 10)) && (j < length[i] - 1); j++) {
|
|
|
|
if ((j / 10) != current_line) {
|
|
|
|
current_line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_strings[current_line] += to_string_hex(data[i][j], 2);
|
|
|
|
}
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
// Read the type back to the total length.
|
|
|
|
field_rect = draw_field(painter, field_rect, s, to_string_hex(length[i]), to_string_hex(type[i]) + pad_string_with_spaces(3) + data_strings[0]);
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
if (number_data_lines > 1) {
|
|
|
|
for (k = 1; k < number_data_lines; k++) {
|
|
|
|
if (!data_strings[k].empty()) {
|
|
|
|
field_rect = draw_field(painter, field_rect, s, "", pad_string_with_spaces(5) + data_strings[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-23 01:58:14 -04:00
|
|
|
}
|
2023-11-12 02:54:04 -05:00
|
|
|
} break;
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
case ADV_DIRECT_IND:
|
2023-11-12 13:10:53 -05:00
|
|
|
case SCAN_REQ: {
|
|
|
|
ADV_PDU_PAYLOAD_TYPE_1_3* directed_mac_data = (ADV_PDU_PAYLOAD_TYPE_1_3*)entry_.packetData.data;
|
|
|
|
uint8_t type = 0xFF;
|
|
|
|
field_rect = draw_field(painter, field_rect, s, to_string_hex(entry_.packetData.dataLen), to_string_hex(type) + pad_string_with_spaces(3) + to_string_mac_address(directed_mac_data->A1, 6, false));
|
|
|
|
} break;
|
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
case CONNECT_REQ:
|
|
|
|
default: {
|
|
|
|
uint8_t type = 0xFF;
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-11-12 13:10:53 -05:00
|
|
|
// TODO: Display Connect Request Information. For right now just printing full hex data.
|
|
|
|
// This struct will eventually be used to break apart containing data of Connect Request.
|
|
|
|
// ADV_PDU_PAYLOAD_TYPE_5 * connect_req = (ADV_PDU_PAYLOAD_TYPE_5 *)entry_.packetData.data;
|
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
for (currentByte = 0; (currentByte < entry_.packetData.dataLen); currentByte++) {
|
|
|
|
data[0][currentByte] = entry_.packetData.data[currentByte];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t number_data_lines = ceil((float)entry_.packetData.dataLen / 10.0);
|
|
|
|
uint8_t current_line = 0;
|
|
|
|
std::array<std::string, total_data_lines> data_strings{};
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
for (j = 0; (j < (number_data_lines * 10)) && (j < entry_.packetData.dataLen); j++) {
|
|
|
|
if ((j / 10) != current_line) {
|
|
|
|
current_line++;
|
2023-10-23 01:58:14 -04:00
|
|
|
}
|
2023-11-12 02:54:04 -05:00
|
|
|
|
|
|
|
data_strings[current_line] += to_string_hex(data[0][j], 2);
|
2023-10-23 01:58:14 -04:00
|
|
|
}
|
2023-11-12 02:54:04 -05:00
|
|
|
|
|
|
|
field_rect = draw_field(painter, field_rect, s, to_string_hex(entry_.packetData.dataLen), to_string_hex(type) + pad_string_with_spaces(3) + data_strings[0]);
|
|
|
|
|
|
|
|
if (number_data_lines > 1) {
|
|
|
|
for (k = 1; k < number_data_lines; k++) {
|
|
|
|
if (!data_strings[k].empty()) {
|
|
|
|
field_rect = draw_field(painter, field_rect, s, "", pad_string_with_spaces(5) + data_strings[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
2023-10-23 01:58:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BleRecentEntryDetailView::set_entry(const BleRecentEntry& entry) {
|
|
|
|
entry_ = entry;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
BLETxPacket BleRecentEntryDetailView::build_packet() {
|
2023-11-06 05:16:01 -05:00
|
|
|
BLETxPacket bleTxPacket;
|
2023-11-07 12:10:18 -05:00
|
|
|
memset(&bleTxPacket, 0, sizeof(BLETxPacket));
|
2023-11-06 05:16:01 -05:00
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
std::string macAddressStr = to_string_mac_address(entry_.packetData.macAddress, 6, true);
|
2023-11-06 05:16:01 -05:00
|
|
|
|
2023-11-07 12:10:18 -05:00
|
|
|
strncpy(bleTxPacket.macAddress, macAddressStr.c_str(), 12);
|
2023-11-11 14:46:51 -05:00
|
|
|
strncpy(bleTxPacket.advertisementData, entry_.dataString.c_str(), entry_.packetData.dataLen * 2);
|
2023-11-06 13:09:26 -05:00
|
|
|
strncpy(bleTxPacket.packetCount, "50", 3);
|
2023-11-06 05:16:01 -05:00
|
|
|
bleTxPacket.packet_count = 50;
|
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
return bleTxPacket;
|
2023-11-06 05:16:01 -05:00
|
|
|
}
|
|
|
|
|
2023-10-23 01:58:14 -04:00
|
|
|
static std::uint64_t get_freq_by_channel_number(uint8_t channel_number) {
|
|
|
|
uint64_t freq_hz;
|
|
|
|
|
|
|
|
switch (channel_number) {
|
|
|
|
case 37:
|
|
|
|
freq_hz = 2'402'000'000ull;
|
|
|
|
break;
|
|
|
|
case 38:
|
|
|
|
freq_hz = 2'426'000'000ull;
|
|
|
|
break;
|
|
|
|
case 39:
|
|
|
|
freq_hz = 2'480'000'000ull;
|
|
|
|
break;
|
|
|
|
case 0 ... 10:
|
|
|
|
freq_hz = 2'404'000'000ull + channel_number * 2'000'000ull;
|
|
|
|
break;
|
|
|
|
case 11 ... 36:
|
|
|
|
freq_hz = 2'428'000'000ull + (channel_number - 11) * 2'000'000ull;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
freq_hz = UINT64_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
return freq_hz;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BLERxView::focus() {
|
2023-11-03 03:08:49 -04:00
|
|
|
options_channel.focus();
|
2023-10-23 01:58:14 -04:00
|
|
|
}
|
|
|
|
|
2023-12-09 12:02:00 -05:00
|
|
|
void BLERxView::file_error() {
|
|
|
|
nav_.display_modal("Error", "File read error.");
|
|
|
|
}
|
|
|
|
|
2023-10-23 01:58:14 -04:00
|
|
|
BLERxView::BLERxView(NavigationView& nav)
|
|
|
|
: nav_{nav} {
|
|
|
|
baseband::run_image(portapack::spi_flash::image_tag_btle_rx);
|
|
|
|
|
|
|
|
add_children({&rssi,
|
|
|
|
&channel,
|
|
|
|
&field_rf_amp,
|
|
|
|
&field_lna,
|
|
|
|
&field_vga,
|
2023-10-24 18:08:45 -04:00
|
|
|
&options_channel,
|
2023-10-23 01:58:14 -04:00
|
|
|
&field_frequency,
|
|
|
|
&check_log,
|
2023-12-09 12:02:00 -05:00
|
|
|
&button_find,
|
2023-11-11 14:46:51 -05:00
|
|
|
&check_name,
|
2023-10-24 18:08:45 -04:00
|
|
|
&label_sort,
|
|
|
|
&options_sort,
|
2023-12-09 12:02:00 -05:00
|
|
|
&label_found,
|
|
|
|
&text_found_count,
|
2023-12-22 05:00:27 -05:00
|
|
|
&check_serial_log,
|
2023-11-04 03:29:48 -04:00
|
|
|
&button_filter,
|
2023-11-16 14:00:52 -05:00
|
|
|
&button_save_list,
|
2023-11-18 16:32:52 -05:00
|
|
|
&button_clear_list,
|
2023-11-03 03:08:49 -04:00
|
|
|
&button_switch,
|
2023-11-11 14:46:51 -05:00
|
|
|
&recent_entries_view});
|
2023-10-23 01:58:14 -04:00
|
|
|
|
|
|
|
recent_entries_view.on_select = [this](const BleRecentEntry& entry) {
|
|
|
|
nav_.push<BleRecentEntryDetailView>(entry);
|
|
|
|
};
|
|
|
|
|
2023-12-22 05:00:27 -05:00
|
|
|
check_serial_log.on_select = [this](Checkbox&, bool v) {
|
|
|
|
serial_logging = v;
|
|
|
|
if (v) {
|
|
|
|
usb_serial_thread = std::make_unique<UsbSerialThread>();
|
|
|
|
} else {
|
|
|
|
usb_serial_thread.reset();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
check_serial_log.set_value(serial_logging);
|
2023-12-19 16:20:13 -05:00
|
|
|
|
2023-12-09 12:02:00 -05:00
|
|
|
ensure_directory(find_packet_path);
|
|
|
|
ensure_directory(log_packets_path);
|
|
|
|
ensure_directory(packet_save_path);
|
|
|
|
|
2023-12-04 12:06:07 -05:00
|
|
|
filterBuffer = filter;
|
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
button_filter.on_select = [this](Button&) {
|
2023-11-01 06:46:41 -04:00
|
|
|
text_prompt(
|
2023-11-11 14:46:51 -05:00
|
|
|
nav_,
|
2023-11-01 06:46:41 -04:00
|
|
|
filterBuffer,
|
|
|
|
64,
|
|
|
|
[this](std::string& buffer) {
|
2023-11-12 02:54:04 -05:00
|
|
|
on_filter_change(buffer);
|
2023-11-01 06:46:41 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-12-09 12:02:00 -05:00
|
|
|
logger = std::make_unique<BLELogger>();
|
|
|
|
|
|
|
|
check_log.on_select = [this](Checkbox&, bool v) {
|
|
|
|
str_log = "";
|
|
|
|
logging = v;
|
|
|
|
|
|
|
|
if (logger && logging)
|
2024-03-25 03:44:49 -04:00
|
|
|
logger->append(blerx_dir.string() + "/Logs/BLELOG_" + to_string_timestamp(rtc_time::now()) + ".TXT");
|
2023-12-09 12:02:00 -05:00
|
|
|
};
|
2023-12-22 05:00:27 -05:00
|
|
|
check_log.set_value(logging);
|
2023-12-09 12:02:00 -05:00
|
|
|
|
2023-11-16 14:00:52 -05:00
|
|
|
button_save_list.on_select = [this, &nav](const ui::Button&) {
|
|
|
|
listFileBuffer = "";
|
|
|
|
text_prompt(
|
|
|
|
nav,
|
|
|
|
listFileBuffer,
|
|
|
|
64,
|
|
|
|
[this](std::string& buffer) {
|
|
|
|
on_save_file(buffer);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-11-18 16:32:52 -05:00
|
|
|
button_clear_list.on_select = [this](Button&) {
|
|
|
|
recent.clear();
|
|
|
|
};
|
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
button_switch.on_select = [&nav](Button&) {
|
|
|
|
nav.replace<BLETxView>();
|
2023-11-03 03:08:49 -04:00
|
|
|
};
|
|
|
|
|
2023-10-23 14:54:04 -04:00
|
|
|
field_frequency.set_step(0);
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-12-04 12:06:07 -05:00
|
|
|
check_name.set_value(name_enable);
|
2023-11-11 14:46:51 -05:00
|
|
|
|
|
|
|
check_name.on_select = [this](Checkbox&, bool v) {
|
2023-12-04 12:06:07 -05:00
|
|
|
name_enable = v;
|
2023-11-11 14:46:51 -05:00
|
|
|
setAllMembersToValue(recent, &BleRecentEntry::include_name, v);
|
|
|
|
recent_entries_view.set_dirty();
|
|
|
|
};
|
|
|
|
|
2023-12-04 12:06:07 -05:00
|
|
|
options_channel.on_change = [this](size_t index, int32_t v) {
|
|
|
|
channel_index = (uint8_t)index;
|
|
|
|
|
2023-11-16 08:46:45 -05:00
|
|
|
// If we selected Auto don't do anything and Auto will handle changing.
|
2023-12-04 12:06:07 -05:00
|
|
|
if (v == 40) {
|
2023-11-16 08:46:45 -05:00
|
|
|
auto_channel = true;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
auto_channel = false;
|
|
|
|
}
|
|
|
|
|
2023-12-04 12:06:07 -05:00
|
|
|
field_frequency.set_value(get_freq_by_channel_number(v));
|
|
|
|
channel_number = v;
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-11-03 03:08:49 -04:00
|
|
|
baseband::set_btlerx(channel_number);
|
2023-10-23 01:58:14 -04:00
|
|
|
};
|
|
|
|
|
2023-12-04 12:06:07 -05:00
|
|
|
options_sort.on_change = [this](size_t index, int32_t v) {
|
|
|
|
sort_index = (uint8_t)index;
|
|
|
|
handle_entries_sort(v);
|
2023-10-24 18:08:45 -04:00
|
|
|
};
|
|
|
|
|
2023-12-04 12:06:07 -05:00
|
|
|
options_channel.set_selected_index(channel_index, true);
|
|
|
|
options_sort.set_selected_index(sort_index, true);
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-12-09 12:02:00 -05:00
|
|
|
button_find.on_select = [this](Button&) {
|
|
|
|
auto open_view = nav_.push<FileLoadView>(".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(); });
|
|
|
|
};
|
|
|
|
};
|
2023-10-23 01:58:14 -04:00
|
|
|
|
|
|
|
// Auto-configure modem for LCR RX (will be removed later)
|
2023-11-03 03:08:49 -04:00
|
|
|
baseband::set_btlerx(channel_number);
|
2023-10-23 01:58:14 -04:00
|
|
|
|
|
|
|
receiver_model.enable();
|
|
|
|
}
|
|
|
|
|
2023-11-19 20:53:53 -05:00
|
|
|
std::string BLERxView::build_line_str(BleRecentEntry entry) {
|
|
|
|
std::string macAddressStr = to_string_mac_address(entry.packetData.macAddress, 6, false) + ",";
|
|
|
|
std::string timestameStr = entry.timestamp + ",";
|
|
|
|
std::string nameStr = entry.nameString + ",";
|
|
|
|
std::string pduStr = pdu_type_to_string(entry.pduType) + ",";
|
|
|
|
std::string dataStr = "0x" + entry.dataString + ",";
|
|
|
|
std::string hitsStr = to_string_dec_int(entry.numHits) + ",";
|
|
|
|
std::string dbStr = to_string_dec_int(entry.dbValue) + ",";
|
|
|
|
std::string channelStr = to_string_dec_int(entry.channelNumber) + ",";
|
|
|
|
|
|
|
|
std::string lineStr = timestameStr + macAddressStr + nameStr + pduStr + dataStr + hitsStr + dbStr + channelStr;
|
|
|
|
lineStr += pad_string_with_spaces(maxLineLength - lineStr.length());
|
|
|
|
|
|
|
|
return lineStr;
|
|
|
|
}
|
|
|
|
|
2023-11-16 14:00:52 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BLERxView::saveFile(const std::filesystem::path& path) {
|
2023-11-19 20:53:53 -05:00
|
|
|
// Check to see if file was previously saved.
|
|
|
|
bool file_existed = file_exists(path);
|
|
|
|
|
|
|
|
// Attempt to open, if it can't be opened. Create new.
|
2023-11-20 02:37:56 -05:00
|
|
|
auto src = std::make_unique<File>();
|
|
|
|
auto error = src->open(path, false, true);
|
2023-11-19 20:53:53 -05:00
|
|
|
|
|
|
|
if (error) {
|
2023-11-16 14:00:52 -05:00
|
|
|
return false;
|
2023-11-19 20:53:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& entry : recent) {
|
|
|
|
tempList.emplace_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_existed) {
|
2023-11-20 02:37:56 -05:00
|
|
|
src->write_line(headerStr.c_str());
|
2023-11-19 20:53:53 -05:00
|
|
|
|
|
|
|
auto it = tempList.begin();
|
|
|
|
|
|
|
|
while (it != tempList.end()) {
|
|
|
|
BleRecentEntry entry = (BleRecentEntry)*it;
|
2023-11-20 02:37:56 -05:00
|
|
|
src->write_line(build_line_str(entry).c_str());
|
2023-11-19 20:53:53 -05:00
|
|
|
it++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Check file for macAddressStr before adding.
|
|
|
|
char currentLine[maxLineLength];
|
|
|
|
uint64_t startPos = headerStr.length();
|
|
|
|
uint64_t bytesRead = 0;
|
|
|
|
uint64_t bytePos = 0;
|
|
|
|
|
2023-11-20 02:37:56 -05:00
|
|
|
File::Size currentSize = src->size();
|
2023-11-19 20:53:53 -05:00
|
|
|
|
2023-11-20 02:37:56 -05:00
|
|
|
auto dst = std::make_unique<File>();
|
2023-11-19 20:53:53 -05:00
|
|
|
const std::filesystem::path tempFilePath = path + "~";
|
2023-11-20 02:37:56 -05:00
|
|
|
auto error = dst->open(tempFilePath, false, true);
|
2023-11-19 20:53:53 -05:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-20 02:37:56 -05:00
|
|
|
dst->write_line(headerStr.c_str());
|
2023-11-19 20:53:53 -05:00
|
|
|
|
2023-11-20 02:37:56 -05:00
|
|
|
src->seek(startPos);
|
2023-11-19 20:53:53 -05:00
|
|
|
|
|
|
|
// Look for ones found and rewrite.
|
|
|
|
do {
|
|
|
|
memset(currentLine, 0, maxLineLength);
|
|
|
|
|
2023-11-20 02:37:56 -05:00
|
|
|
bytesRead = readUntil(*src, currentLine, currentSize, '\n');
|
2023-11-19 20:53:53 -05:00
|
|
|
|
|
|
|
if (!bytesRead) {
|
|
|
|
break;
|
|
|
|
}
|
2023-11-16 14:00:52 -05:00
|
|
|
|
2023-11-19 20:53:53 -05:00
|
|
|
bytePos += bytesRead;
|
|
|
|
|
|
|
|
std::string lineStr = "";
|
|
|
|
std::string macAddressStr = "";
|
|
|
|
BleRecentEntry foundEntry;
|
|
|
|
|
|
|
|
char* token;
|
|
|
|
token = strtok(currentLine, ",");
|
|
|
|
|
|
|
|
while (token != NULL) {
|
|
|
|
auto it = tempList.begin();
|
|
|
|
|
|
|
|
while (it != tempList.end()) {
|
|
|
|
BleRecentEntry& entry = reinterpret_cast<BleRecentEntry&>(*it);
|
|
|
|
|
|
|
|
macAddressStr = to_string_mac_address(entry.packetData.macAddress, 6, false);
|
|
|
|
|
|
|
|
if (strstr(token, macAddressStr.c_str()) != NULL) {
|
|
|
|
entry.entryFound = true;
|
|
|
|
foundEntry = entry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundEntry.entryFound) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
token = strtok(NULL, ",");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundEntry.entryFound) {
|
2023-11-20 02:37:56 -05:00
|
|
|
dst->write_line(build_line_str(foundEntry).c_str());
|
2023-11-19 20:53:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} while (bytePos <= currentSize);
|
|
|
|
|
|
|
|
// Write the ones not found.
|
|
|
|
auto it = tempList.begin();
|
|
|
|
|
|
|
|
while (it != tempList.end()) {
|
|
|
|
BleRecentEntry entry = (BleRecentEntry)*it;
|
|
|
|
|
|
|
|
if (!entry.entryFound) {
|
2023-11-20 02:37:56 -05:00
|
|
|
dst->write_line(build_line_str(entry).c_str());
|
2023-11-19 20:53:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
2023-11-20 02:37:56 -05:00
|
|
|
// Close files before renaming/deleting.
|
|
|
|
src.reset();
|
|
|
|
dst.reset();
|
|
|
|
|
|
|
|
// Delete original and overwrite with temp file.
|
|
|
|
delete_file(path);
|
|
|
|
rename_file(tempFilePath, path);
|
2023-11-16 14:00:52 -05:00
|
|
|
}
|
|
|
|
|
2023-11-19 20:53:53 -05:00
|
|
|
tempList.clear();
|
|
|
|
|
2023-11-16 14:00:52 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-23 01:58:14 -04:00
|
|
|
void BLERxView::on_data(BlePacketData* packet) {
|
|
|
|
if (!logging) {
|
|
|
|
str_log = "";
|
|
|
|
}
|
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
str_console += pdu_type_to_string((ADV_PDU_TYPE)packet->type);
|
2023-10-24 18:08:45 -04:00
|
|
|
str_console += " Len:";
|
2023-10-23 01:58:14 -04:00
|
|
|
str_console += to_string_dec_uint(packet->size);
|
2023-12-19 16:20:13 -05:00
|
|
|
str_console += " Mac:";
|
2023-11-06 05:16:01 -05:00
|
|
|
str_console += to_string_mac_address(packet->macAddress, 6, false);
|
2023-12-19 16:20:13 -05:00
|
|
|
str_console += " Data:";
|
2023-10-23 01:58:14 -04:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < packet->dataLen; i++) {
|
2023-11-01 06:46:41 -04:00
|
|
|
str_console += to_string_hex(packet->data[i], 2);
|
2023-10-23 01:58:14 -04:00
|
|
|
}
|
|
|
|
|
2023-10-24 18:08:45 -04:00
|
|
|
uint64_t macAddressEncoded = copy_mac_address_to_uint64(packet->macAddress);
|
2023-10-23 01:58:14 -04:00
|
|
|
|
2023-10-24 18:08:45 -04:00
|
|
|
// Start of Packet stuffing.
|
2023-10-23 01:58:14 -04:00
|
|
|
// Masking off the top 2 bytes to avoid invalid keys.
|
|
|
|
auto& entry = ::on_packet(recent, macAddressEncoded & 0xFFFFFFFFFFFF);
|
2023-11-12 02:54:04 -05:00
|
|
|
updateEntry(packet, entry, (ADV_PDU_TYPE)packet->type);
|
2023-11-01 06:46:41 -04:00
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
// Add entries if they meet the criteria.
|
|
|
|
auto value = filter;
|
|
|
|
resetFilteredEntries(recent, [&value](const BleRecentEntry& entry) {
|
|
|
|
return (entry.dataString.find(value) == std::string::npos) && (entry.nameString.find(value) == std::string::npos);
|
|
|
|
});
|
|
|
|
|
|
|
|
handle_entries_sort(options_sort.selected_index());
|
|
|
|
|
2023-11-01 06:46:41 -04:00
|
|
|
// Log at End of Packet.
|
|
|
|
if (logger && logging) {
|
2023-12-19 16:20:13 -05:00
|
|
|
logger->log_raw_data(str_console + "\r\n");
|
2023-11-01 06:46:41 -04:00
|
|
|
}
|
2023-12-09 12:02:00 -05:00
|
|
|
|
2023-12-22 05:00:27 -05:00
|
|
|
if (serial_logging) {
|
|
|
|
usb_serial_thread->serial_str = str_console + "\r\n";
|
|
|
|
usb_serial_thread->str_ready = true;
|
|
|
|
}
|
2023-12-19 16:20:13 -05:00
|
|
|
str_console = "";
|
|
|
|
|
2023-12-09 12:02:00 -05:00
|
|
|
if (!searchList.empty()) {
|
|
|
|
auto it = searchList.begin();
|
|
|
|
|
|
|
|
while (it != searchList.end()) {
|
|
|
|
std::string searchStr = (std::string)*it;
|
|
|
|
|
|
|
|
if (entry.dataString.find(searchStr) != std::string::npos) {
|
|
|
|
searchList.erase(it);
|
|
|
|
found_count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
text_found_count.set(to_string_dec_uint(found_count) + "/" + to_string_dec_uint(total_count));
|
|
|
|
}
|
2023-11-01 06:46:41 -04:00
|
|
|
}
|
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
void BLERxView::on_filter_change(std::string value) {
|
2023-11-11 14:46:51 -05:00
|
|
|
// New filter? Reset list from recent entries.
|
|
|
|
if (filter != value) {
|
|
|
|
resetFilteredEntries(recent, [&value](const BleRecentEntry& entry) {
|
|
|
|
return (entry.dataString.find(value) == std::string::npos) && (entry.nameString.find(value) == std::string::npos);
|
2023-11-01 06:46:41 -04:00
|
|
|
});
|
2023-11-11 14:46:51 -05:00
|
|
|
}
|
2023-11-01 06:46:41 -04:00
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
filter = value;
|
|
|
|
}
|
2023-11-01 06:46:41 -04:00
|
|
|
|
2023-12-09 12:02:00 -05:00
|
|
|
void BLERxView::on_file_changed(const std::filesystem::path& new_file_path) {
|
2024-03-26 12:32:24 -04:00
|
|
|
file_path = new_file_path;
|
2023-12-09 12:02:00 -05:00
|
|
|
found_count = 0;
|
|
|
|
total_count = 0;
|
|
|
|
searchList.clear();
|
|
|
|
|
|
|
|
{ // Get the size of the data file.
|
|
|
|
File data_file;
|
|
|
|
auto error = data_file.open(file_path, true, false);
|
|
|
|
if (error) {
|
|
|
|
file_error();
|
|
|
|
file_path = "";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t bytesRead = 0;
|
|
|
|
uint64_t bytePos = 0;
|
|
|
|
char currentLine[maxLineLength];
|
|
|
|
|
|
|
|
do {
|
|
|
|
memset(currentLine, 0, maxLineLength);
|
|
|
|
|
|
|
|
bytesRead = readUntil(data_file, currentLine, maxLineLength, '\n');
|
|
|
|
|
|
|
|
// Remove return if found.
|
|
|
|
if (currentLine[strlen(currentLine)] == '\r') {
|
|
|
|
currentLine[strlen(currentLine)] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bytesRead) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
searchList.push_back(currentLine);
|
|
|
|
total_count++;
|
|
|
|
|
|
|
|
bytePos += bytesRead;
|
|
|
|
|
|
|
|
} while (bytePos <= data_file.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 12:18:15 -05:00
|
|
|
// called each 1/60th of second, so 6 = 100ms
|
|
|
|
void BLERxView::on_timer() {
|
|
|
|
if (++timer_count == timer_period) {
|
|
|
|
timer_count = 0;
|
|
|
|
|
|
|
|
if (auto_channel) {
|
|
|
|
int min = 37;
|
|
|
|
int max = 39;
|
|
|
|
|
|
|
|
int randomChannel = min + std::rand() % (max - min + 1);
|
|
|
|
|
|
|
|
field_frequency.set_value(get_freq_by_channel_number(randomChannel));
|
|
|
|
baseband::set_btlerx(randomChannel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
void BLERxView::handle_entries_sort(uint8_t index) {
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
sortEntriesBy(
|
|
|
|
recent, [](const BleRecentEntry& entry) { return entry.macAddress; }, true);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
sortEntriesBy(
|
|
|
|
recent, [](const BleRecentEntry& entry) { return entry.numHits; }, false);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
sortEntriesBy(
|
|
|
|
recent, [](const BleRecentEntry& entry) { return entry.dbValue; }, false);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
sortEntriesBy(
|
|
|
|
recent, [](const BleRecentEntry& entry) { return entry.timestamp; }, false);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
sortEntriesBy(
|
|
|
|
recent, [](const BleRecentEntry& entry) { return entry.nameString; }, true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2023-11-01 06:46:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
recent_entries_view.set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BLERxView::set_parent_rect(const Rect new_parent_rect) {
|
|
|
|
View::set_parent_rect(new_parent_rect);
|
2023-11-03 03:08:49 -04:00
|
|
|
const Rect content_rect{0, header_height, new_parent_rect.width(), new_parent_rect.height() - header_height - switch_button_height};
|
2023-11-01 06:46:41 -04:00
|
|
|
recent_entries_view.set_parent_rect(content_rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
BLERxView::~BLERxView() {
|
|
|
|
receiver_model.disable();
|
|
|
|
baseband::shutdown();
|
|
|
|
}
|
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
void BLERxView::updateEntry(const BlePacketData* packet, BleRecentEntry& entry, ADV_PDU_TYPE pdu_type) {
|
2023-11-01 06:46:41 -04:00
|
|
|
std::string data_string;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < packet->dataLen; i++) {
|
|
|
|
data_string += to_string_hex(packet->data[i], 2);
|
|
|
|
}
|
2023-10-23 01:58:14 -04:00
|
|
|
|
|
|
|
entry.dbValue = packet->max_dB;
|
2023-10-24 18:08:45 -04:00
|
|
|
entry.timestamp = to_string_timestamp(rtc_time::now());
|
2023-11-01 06:46:41 -04:00
|
|
|
entry.dataString = data_string;
|
2023-10-24 18:08:45 -04:00
|
|
|
|
2023-10-23 01:58:14 -04:00
|
|
|
entry.packetData.type = packet->type;
|
|
|
|
entry.packetData.size = packet->size;
|
|
|
|
entry.packetData.dataLen = packet->dataLen;
|
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
// Mac Address of sender.
|
2023-10-23 01:58:14 -04:00
|
|
|
entry.packetData.macAddress[0] = packet->macAddress[0];
|
|
|
|
entry.packetData.macAddress[1] = packet->macAddress[1];
|
|
|
|
entry.packetData.macAddress[2] = packet->macAddress[2];
|
|
|
|
entry.packetData.macAddress[3] = packet->macAddress[3];
|
|
|
|
entry.packetData.macAddress[4] = packet->macAddress[4];
|
|
|
|
entry.packetData.macAddress[5] = packet->macAddress[5];
|
|
|
|
|
2023-11-04 03:29:48 -04:00
|
|
|
entry.numHits++;
|
2023-11-12 02:54:04 -05:00
|
|
|
entry.pduType = pdu_type;
|
2023-11-16 14:00:52 -05:00
|
|
|
entry.channelNumber = channel_number;
|
2023-11-04 03:29:48 -04:00
|
|
|
|
2023-11-12 13:10:53 -05:00
|
|
|
// Parse Data Section into buffer to be interpretted later.
|
2023-10-23 01:58:14 -04:00
|
|
|
for (int i = 0; i < packet->dataLen; i++) {
|
|
|
|
entry.packetData.data[i] = packet->data[i];
|
|
|
|
}
|
|
|
|
|
2023-11-11 14:46:51 -05:00
|
|
|
entry.include_name = check_name.value();
|
2023-11-04 03:29:48 -04:00
|
|
|
|
2023-11-12 09:59:36 -05:00
|
|
|
// Only parse name for advertisment packets and empty name entries
|
|
|
|
if ((pdu_type == ADV_IND || pdu_type == ADV_NONCONN_IND || pdu_type == SCAN_RSP || pdu_type == ADV_SCAN_IND) && entry.nameString.empty()) {
|
2023-11-12 13:10:53 -05:00
|
|
|
ADV_PDU_PAYLOAD_TYPE_0_2_4_6* advertiseData = (ADV_PDU_PAYLOAD_TYPE_0_2_4_6*)entry.packetData.data;
|
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
uint8_t currentByte = 0;
|
|
|
|
uint8_t length = 0;
|
|
|
|
uint8_t type = 0;
|
2023-11-04 03:29:48 -04:00
|
|
|
|
2023-11-12 09:59:36 -05:00
|
|
|
std::string decoded_data;
|
2023-11-12 02:54:04 -05:00
|
|
|
for (currentByte = 0; (currentByte < entry.packetData.dataLen);) {
|
2023-11-12 13:10:53 -05:00
|
|
|
length = advertiseData->Data[currentByte++];
|
|
|
|
type = advertiseData->Data[currentByte++];
|
2023-11-04 03:29:48 -04:00
|
|
|
|
2023-11-12 02:54:04 -05:00
|
|
|
// Subtract 1 because type is part of the length.
|
|
|
|
for (int i = 0; i < length - 1; i++) {
|
2023-11-12 09:59:36 -05:00
|
|
|
if (type == 0x08 || type == 0x09) {
|
2023-11-12 13:10:53 -05:00
|
|
|
decoded_data += (char)advertiseData->Data[currentByte];
|
2023-11-12 02:54:04 -05:00
|
|
|
}
|
|
|
|
currentByte++;
|
|
|
|
}
|
2023-11-12 09:59:36 -05:00
|
|
|
if (!decoded_data.empty()) {
|
|
|
|
entry.nameString = std::move(decoded_data);
|
|
|
|
break;
|
2023-11-12 02:54:04 -05:00
|
|
|
}
|
2023-11-04 03:29:48 -04:00
|
|
|
}
|
2023-11-16 08:46:45 -05:00
|
|
|
} else if (pdu_type == ADV_DIRECT_IND || pdu_type == SCAN_REQ) {
|
|
|
|
ADV_PDU_PAYLOAD_TYPE_1_3* directed_mac_data = (ADV_PDU_PAYLOAD_TYPE_1_3*)entry.packetData.data;
|
|
|
|
reverse_byte_array(directed_mac_data->A1, 6);
|
2023-11-04 03:29:48 -04:00
|
|
|
}
|
2023-10-23 01:58:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace ui */
|