From 2ab50b2105d00430162d27eabc841a52539e52db Mon Sep 17 00:00:00 2001 From: TJ Baginski Date: Sun, 12 Nov 2023 16:00:05 -0500 Subject: [PATCH 01/15] Initial BLE Comm commit. --- firmware/application/CMakeLists.txt | 1 + firmware/application/apps/ble_comm_app.cpp | 368 +++++++++++++++++++++ firmware/application/apps/ble_comm_app.hpp | 185 +++++++++++ firmware/application/apps/ble_tx_app.cpp | 11 - firmware/application/apps/ble_tx_app.hpp | 78 ++--- firmware/application/string_format.cpp | 11 + firmware/application/string_format.hpp | 1 + firmware/application/ui_navigation.cpp | 2 + 8 files changed, 607 insertions(+), 50 deletions(-) create mode 100644 firmware/application/apps/ble_comm_app.cpp create mode 100644 firmware/application/apps/ble_comm_app.hpp diff --git a/firmware/application/CMakeLists.txt b/firmware/application/CMakeLists.txt index 856d867f6..522d98aba 100644 --- a/firmware/application/CMakeLists.txt +++ b/firmware/application/CMakeLists.txt @@ -244,6 +244,7 @@ set(CPPSRC apps/ais_app.cpp apps/analog_audio_app.cpp apps/analog_tv_app.cpp + apps/ble_comm_app.cpp apps/ble_rx_app.cpp apps/ble_tx_app.cpp apps/capture_app.cpp diff --git a/firmware/application/apps/ble_comm_app.cpp b/firmware/application/apps/ble_comm_app.cpp new file mode 100644 index 000000000..90c627a74 --- /dev/null +++ b/firmware/application/apps/ble_comm_app.cpp @@ -0,0 +1,368 @@ +/* + * 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. + */ + +#include "ble_comm_app.hpp" + +#include "ui_modemsetup.hpp" + +#include "modems.hpp" +#include "audio.hpp" +#include "rtc_time.hpp" +#include "baseband_api.hpp" +#include "string_format.hpp" +#include "portapack_persistent_memory.hpp" +#include "ui_text.hpp" + +using namespace portapack; +using namespace modems; + +void BLECommLogger::log_raw_data(const std::string& data) { + log_file.write_entry(data); +} + +namespace ui { +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 BLECommView::focus() { + options_channel.focus(); +} + +BLECommView::BLECommView(NavigationView& nav) + : nav_{nav} { + add_children({&rssi, + &channel, + &field_rf_amp, + &field_lna, + &field_vga, + &options_channel, + &field_frequency, + &label_send_adv, + &button_send_adv, + &check_log, + &label_packets_sent, + &text_packets_sent, + &console}); + + field_frequency.set_step(0); + + button_send_adv.on_select = [this](ImageButton&) { + this->toggle(); + }; + + check_log.set_value(logging); + + 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) { + field_frequency.set_value(get_freq_by_channel_number(i)); + channel_number_rx = i; + }; + + options_channel.set_selected_index(0, true); + + logger = std::make_unique(); + + // Generate new random Mac Address upon each new startup. + generateRandomMacAddress(randomMac); +} + +void BLECommView::set_parent_rect(const Rect new_parent_rect) { + View::set_parent_rect(new_parent_rect); + const Rect content_rect{0, header_height, new_parent_rect.width(), new_parent_rect.height() - header_height}; + console.set_parent_rect(content_rect); +} + +BLECommView::~BLECommView() { + receiver_model.disable(); + transmitter_model.disable(); + baseband::shutdown(); +} + +bool BLECommView::is_sending_tx() const { + return (bool)is_running_tx; +} + +void BLECommView::toggle() { + if (is_sending_tx()) { + stopTx(); + } else { + startTx(build_adv_packet(), PKT_TYPE_DISCOVERY); + } +} + +BLETxPacket BLECommView::build_adv_packet() { + BLETxPacket bleTxPacket; + memset(&bleTxPacket, 0, sizeof(BLETxPacket)); + + std::string dataString = "11094861636b524620506f7274617061636b"; + + strncpy(bleTxPacket.macAddress, randomMac, 12); + strncpy(bleTxPacket.advertisementData, dataString.c_str(), dataString.length()); + + //Little note that, at 64 timer, 40 packets is around 1 second. So this should advertise for 5 seconds for 200 packets. + strncpy(bleTxPacket.packetCount, "200", 4); + bleTxPacket.packet_count = 200; + + return bleTxPacket; +} + +void BLECommView::startTx(BLETxPacket packetToSend, PKT_TYPE pduType) { + + if (!is_sending_tx()) + { + switch_rx_tx(false); + + packet_counter = packetToSend.packet_count; + + button_send_adv.set_bitmap(&bitmap_stop); + baseband::set_btletx(channel_number_tx, packetToSend.macAddress, packetToSend.advertisementData, pduType); + transmitter_model.enable(); + + is_running_tx = true; + } + else { + baseband::set_btletx(channel_number_tx, packetToSend.macAddress, packetToSend.advertisementData, pduType); + } + + if ((packet_counter % 10) == 0) { + text_packets_sent.set(to_string_dec_uint(packet_counter)); + } + + packet_counter--; +} + +void BLECommView::stopTx() { + button_send_adv.set_bitmap(&bitmap_play); + text_packets_sent.set(to_string_dec_uint(packet_counter)); + + switch_rx_tx(true); + + baseband::set_btlerx(channel_number_rx); + receiver_model.enable(); + + is_running_tx = false; +} + +void BLECommView::switch_rx_tx(bool inRxMode) +{ + if (inRxMode) + { + //Start Rx + transmitter_model.disable(); + baseband::shutdown(); + baseband::run_image(portapack::spi_flash::image_tag_btle_rx); + } + else + { + //Start Tx + receiver_model.disable(); + baseband::shutdown(); + baseband::run_image(portapack::spi_flash::image_tag_btle_tx); + } +} + +void BLECommView::on_data(BlePacketData* packet) { + std::string str_console = ""; + + if (!logging) { + str_log = ""; + } + + switch ((ADV_PDU_TYPE)packet->type) { + case ADV_IND: + str_console += "ADV_IND"; + break; + case ADV_DIRECT_IND: + str_console += "ADV_DIRECT_IND"; + break; + case ADV_NONCONN_IND: + str_console += "ADV_NONCONN_IND"; + break; + case SCAN_REQ: + str_console += "SCAN_REQ"; + break; + case SCAN_RSP: + str_console += "SCAN_RSP"; + break; + case CONNECT_REQ: + str_console += "CONNECT_REQ"; + break; + case ADV_SCAN_IND: + str_console += "ADV_SCAN_IND"; + break; + case RESERVED0: + case RESERVED1: + case RESERVED2: + case RESERVED3: + case RESERVED4: + case RESERVED5: + case RESERVED6: + case RESERVED7: + case RESERVED8: + str_console += "RESERVED"; + break; + default: + str_console += "UNKNOWN"; + break; + } + + str_console += " Len:"; + str_console += to_string_dec_uint(packet->size); + + str_console += "\n"; + + str_console += "Mac:"; + str_console += to_string_mac_address(packet->macAddress, 6, false); + + str_console += "\n"; + str_console += "Data:"; + + int i; + + for (i = 0; i < packet->dataLen; i++) { + str_console += to_string_hex(packet->data[i], 2); + } + + str_console += "\n"; + + console.write(str_console); + + //uint64_t macAddressEncoded = copy_mac_address_to_uint64(packet->macAddress); + + parse_received_packet(packet, (ADV_PDU_TYPE)packet->type); + + // Log at End of Packet. + if (logger && logging) { + logger->log_raw_data(str_console); + } +} + +void BLECommView::on_tx_progress(const bool done) { + if (done) { + if (is_sending_tx()) { + // Reached end of current packet repeats. + if (packet_counter == 0) { + stopTx(); + } + else { + if ((timer_count % timer_period) == 0) { + startTx(build_adv_packet(), PKT_TYPE_DISCOVERY); + } + } + + timer_count++; + } + } +} + +void BLECommView::parse_received_packet(const BlePacketData* packet, ADV_PDU_TYPE pdu_type) { + std::string data_string; + + int i; + + for (i = 0; i < packet->dataLen; i++) { + data_string += to_string_hex(packet->data[i], 2); + } + + currentPacket.dbValue = packet->max_dB; + currentPacket.timestamp = to_string_timestamp(rtc_time::now()); + currentPacket.dataString = data_string; + + currentPacket.packetData.type = packet->type; + currentPacket.packetData.size = packet->size; + currentPacket.packetData.dataLen = packet->dataLen; + + currentPacket.packetData.macAddress[0] = packet->macAddress[0]; + currentPacket.packetData.macAddress[1] = packet->macAddress[1]; + currentPacket.packetData.macAddress[2] = packet->macAddress[2]; + currentPacket.packetData.macAddress[3] = packet->macAddress[3]; + currentPacket.packetData.macAddress[4] = packet->macAddress[4]; + currentPacket.packetData.macAddress[5] = packet->macAddress[5]; + + currentPacket.numHits++; + + for (int i = 0; i < packet->dataLen; i++) { + currentPacket.packetData.data[i] = packet->data[i]; + } + + std::string nameString; + + // 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) && nameString.empty()) { + ADV_PDU_PAYLOAD_TYPE_0_2_4_6* advertiseData = (ADV_PDU_PAYLOAD_TYPE_0_2_4_6*)packet->data; + + uint8_t currentByte = 0; + uint8_t length = 0; + uint8_t type = 0; + + std::string decoded_data; + for (currentByte = 0; (currentByte < packet->dataLen);) { + length = advertiseData->Data[currentByte++]; + type = advertiseData->Data[currentByte++]; + + // Subtract 1 because type is part of the length. + for (int i = 0; i < length - 1; i++) { + if (type == 0x08 || type == 0x09) { + decoded_data += (char)advertiseData->Data[currentByte]; + } + currentByte++; + } + if (!decoded_data.empty()) { + nameString = std::move(decoded_data); + break; + } + } + } +} + +} /* namespace ui */ diff --git a/firmware/application/apps/ble_comm_app.hpp b/firmware/application/apps/ble_comm_app.hpp new file mode 100644 index 000000000..a19930d4d --- /dev/null +++ b/firmware/application/apps/ble_comm_app.hpp @@ -0,0 +1,185 @@ +/* + * 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. + */ + +#ifndef __BLE_COMM_APP_H__ +#define __BLE_COMM_APP_H__ + +#include "ble_rx_app.hpp" +#include "ble_tx_app.hpp" + +#include "ui.hpp" +#include "ui_navigation.hpp" +#include "ui_receiver.hpp" +#include "ui_transmitter.hpp" +#include "ui_freq_field.hpp" +#include "ui_record_view.hpp" +#include "app_settings.hpp" +#include "radio_state.hpp" +#include "log_file.hpp" +#include "utility.hpp" + +#include "recent_entries.hpp" + +class BLECommLogger { + public: + Optional append(const std::string& filename) { + return log_file.append(filename); + } + + void log_raw_data(const std::string& data); + + private: + LogFile log_file{}; +}; + +namespace ui { +class BLECommView : public View { + public: + BLECommView(NavigationView& nav); + ~BLECommView(); + + void set_parent_rect(const Rect new_parent_rect) override; + void paint(Painter&) override{}; + + void focus() override; + + std::string title() const override { return "BLE Comm"; }; + + private: + BLETxPacket build_adv_packet(); + void switch_rx_tx(bool inRxMode); + void startTx(BLETxPacket packetToSend, PKT_TYPE pduType); + void stopTx(); + void toggle(); + bool is_sending_tx() const; + void on_data(BlePacketData* packetData); + void on_tx_progress(const bool done); + void parse_received_packet(const BlePacketData* packet, ADV_PDU_TYPE pdu_type); + + NavigationView& nav_; + + RxRadioState radio_state_rx_{ + 2'402'000'000 /* frequency */, + 4'000'000 /* bandwidth */, + 4'000'000 /* sampling rate */, + ReceiverModel::Mode::WidebandFMAudio}; + + TxRadioState radio_state_tx_{ + 2'402'000'000 /* frequency */, + 4'000'000 /* bandwidth */, + 4'000'000 /* sampling rate */ + }; + + app_settings::SettingsManager settings_{ + "BLE Comm Tx", app_settings::Mode::RX_TX}; + + uint8_t console_color{0}; + uint32_t prev_value{0}; + + uint8_t channel_number_tx = 37; + uint8_t channel_number_rx = 37; + + char randomMac[13] = "010203040506"; + bool is_running_tx = false; + uint64_t timer_count{0}; + uint64_t timer_period{64}; + uint32_t packet_counter{0}; + + static constexpr auto header_height = 5 * 16; + + OptionsField options_channel{ + {0 * 8, 0 * 8}, + 5, + {{"Ch.37 ", 37}, + {"Ch.38", 38}, + {"Ch.39", 39}}}; + + RxFrequencyField field_frequency{ + {6 * 8, 0 * 16}, + nav_}; + + RFAmpField field_rf_amp{ + {16 * 8, 0 * 16}}; + + LNAGainField field_lna{ + {18 * 8, 0 * 16}}; + + VGAGainField field_vga{ + {21 * 8, 0 * 16}}; + + RSSI rssi{ + {24 * 8, 0, 6 * 8, 4}}; + + Channel channel{ + {24 * 8, 5, 6 * 8, 4}}; + + Labels label_send_adv{ + {{0 * 8, 2 * 8}, "Send Advertisement:", Color::light_grey()}}; + + ImageButton button_send_adv{ + {21 * 8, 1 * 16, 10 * 8, 2 * 16}, + &bitmap_play, + Color::green(), + Color::black()}; + + Checkbox check_log{ + {24 * 8, 2 * 8}, + 3, + "Log", + true}; + + Labels label_packets_sent{ + {{0 * 8, 4 * 8}, "Packets Left:", Color::light_grey()}}; + + Text text_packets_sent{ + {13 * 8, 2 * 16, 12 * 8, 16}, + "-"}; + + Console console{ + {0, 4 * 16, 240, 240}}; + + std::string str_log{""}; + bool logging{false}; + + BleRecentEntry currentPacket = {}; + + std::unique_ptr logger{}; + + MessageHandlerRegistration message_handler_packet{ + Message::ID::BlePacket, + [this](Message* const p) { + const auto message = static_cast(p); + this->on_data(message->packet); + }}; + + MessageHandlerRegistration message_handler_tx_progress{ + Message::ID::TXProgress, + [this](const Message* const p) { + const auto message = *reinterpret_cast(p); + this->on_tx_progress(message.done); + }}; +}; + +} /* namespace ui */ + +#endif /*__UI_AFSK_RX_H__*/ diff --git a/firmware/application/apps/ble_tx_app.cpp b/firmware/application/apps/ble_tx_app.cpp index 199cffb0c..55fda0512 100644 --- a/firmware/application/apps/ble_tx_app.cpp +++ b/firmware/application/apps/ble_tx_app.cpp @@ -121,17 +121,6 @@ void readUntil(File& file, char* result, std::size_t maxBufferSize, char delimit result[bytesRead] = '\0'; } -void generateRandomMacAddress(char* macAddress) { - const char hexDigits[] = "0123456789ABCDEF"; - - // Generate 12 random hexadecimal characters - for (int i = 0; i < 12; i++) { - int randomIndex = rand() % 16; - macAddress[i] = hexDigits[randomIndex]; - } - macAddress[12] = '\0'; // Null-terminate the string -} - static std::uint64_t get_freq_by_channel_number(uint8_t channel_number) { uint64_t freq_hz; diff --git a/firmware/application/apps/ble_tx_app.hpp b/firmware/application/apps/ble_tx_app.hpp index 26eb33fbd..001645fac 100644 --- a/firmware/application/apps/ble_tx_app.hpp +++ b/firmware/application/apps/ble_tx_app.hpp @@ -62,6 +62,36 @@ struct BLETxPacket { uint32_t packet_count; }; +enum PKT_TYPE { + PKT_TYPE_INVALID_TYPE, + PKT_TYPE_RAW, + PKT_TYPE_DISCOVERY, + PKT_TYPE_IBEACON, + PKT_TYPE_ADV_IND, + PKT_TYPE_ADV_DIRECT_IND, + PKT_TYPE_ADV_NONCONN_IND, + PKT_TYPE_ADV_SCAN_IND, + PKT_TYPE_SCAN_REQ, + PKT_TYPE_SCAN_RSP, + PKT_TYPE_CONNECT_REQ, + PKT_TYPE_LL_DATA, + PKT_TYPE_LL_CONNECTION_UPDATE_REQ, + PKT_TYPE_LL_CHANNEL_MAP_REQ, + PKT_TYPE_LL_TERMINATE_IND, + PKT_TYPE_LL_ENC_REQ, + PKT_TYPE_LL_ENC_RSP, + PKT_TYPE_LL_START_ENC_REQ, + PKT_TYPE_LL_START_ENC_RSP, + PKT_TYPE_LL_UNKNOWN_RSP, + PKT_TYPE_LL_FEATURE_REQ, + PKT_TYPE_LL_FEATURE_RSP, + PKT_TYPE_LL_PAUSE_ENC_REQ, + PKT_TYPE_LL_PAUSE_ENC_RSP, + PKT_TYPE_LL_VERSION_IND, + PKT_TYPE_LL_REJECT_IND, + PKT_TYPE_NUM_PKT_TYPE +}; + class BLETxView : public View { public: BLETxView(NavigationView& nav); @@ -118,36 +148,6 @@ class BLETxView : public View { bool random_mac = false; bool file_override = false; - enum PKT_TYPE { - INVALID_TYPE, - RAW, - DISCOVERY, - IBEACON, - ADV_IND, - ADV_DIRECT_IND, - ADV_NONCONN_IND, - ADV_SCAN_IND, - SCAN_REQ, - SCAN_RSP, - CONNECT_REQ, - LL_DATA, - LL_CONNECTION_UPDATE_REQ, - LL_CHANNEL_MAP_REQ, - LL_TERMINATE_IND, - LL_ENC_REQ, - LL_ENC_RSP, - LL_START_ENC_REQ, - LL_START_ENC_RSP, - LL_UNKNOWN_RSP, - LL_FEATURE_REQ, - LL_FEATURE_RSP, - LL_PAUSE_ENC_REQ, - LL_PAUSE_ENC_RSP, - LL_VERSION_IND, - LL_REJECT_IND, - NUM_PKT_TYPE - }; - static constexpr uint8_t mac_address_size_str{12}; static constexpr uint8_t max_packet_size_str{62}; static constexpr uint8_t max_packet_repeat_str{10}; @@ -156,7 +156,7 @@ class BLETxView : public View { BLETxPacket packets[max_num_packets]; - PKT_TYPE pduType = {DISCOVERY}; + PKT_TYPE pduType = {PKT_TYPE_DISCOVERY}; static constexpr auto header_height = 9 * 16; static constexpr auto switch_button_height = 3 * 16; @@ -220,14 +220,14 @@ class BLETxView : public View { OptionsField options_adv_type{ {17 * 8, 6 * 8}, 14, - {{"DISCOVERY ", DISCOVERY}, - {"ADV_IND", ADV_IND}, - {"ADV_DIRECT", ADV_DIRECT_IND}, - {"ADV_NONCONN", ADV_NONCONN_IND}, - {"ADV_SCAN_IND", ADV_SCAN_IND}, - {"SCAN_REQ", SCAN_REQ}, - {"SCAN_RSP", SCAN_RSP}, - {"CONNECT_REQ", CONNECT_REQ}}}; + {{"DISCOVERY ", PKT_TYPE_DISCOVERY}, + {"ADV_IND", PKT_TYPE_ADV_IND}, + {"ADV_DIRECT", PKT_TYPE_ADV_DIRECT_IND}, + {"ADV_NONCONN", PKT_TYPE_ADV_NONCONN_IND}, + {"ADV_SCAN_IND", PKT_TYPE_ADV_SCAN_IND}, + {"SCAN_REQ", PKT_TYPE_SCAN_REQ}, + {"SCAN_RSP", PKT_TYPE_SCAN_RSP}, + {"CONNECT_REQ", PKT_TYPE_CONNECT_REQ}}}; Labels label_packet_index{ {{0 * 8, 10 * 8}, "Packet Index:", Color::light_grey()}}; diff --git a/firmware/application/string_format.cpp b/firmware/application/string_format.cpp index d64c6fa17..ca2d73fb9 100644 --- a/firmware/application/string_format.cpp +++ b/firmware/application/string_format.cpp @@ -337,6 +337,17 @@ std::string to_string_formatted_mac_address(const char* macAddress) { return formattedAddress; } +void generateRandomMacAddress(char* macAddress) { + const char hexDigits[] = "0123456789ABCDEF"; + + // Generate 12 random hexadecimal characters + for (int i = 0; i < 12; i++) { + int randomIndex = rand() % 16; + macAddress[i] = hexDigits[randomIndex]; + } + macAddress[12] = '\0'; // Null-terminate the string +} + std::string unit_auto_scale(double n, const uint32_t base_unit, uint32_t precision) { const uint32_t powers_of_ten[5] = {1, 10, 100, 1000, 10000}; std::string string{""}; diff --git a/firmware/application/string_format.hpp b/firmware/application/string_format.hpp index ad0c105f0..5f1b2bdc4 100644 --- a/firmware/application/string_format.hpp +++ b/firmware/application/string_format.hpp @@ -79,6 +79,7 @@ std::string to_string_file_size(uint32_t file_size); // Converts Mac Address to string. std::string to_string_mac_address(const uint8_t* macAddress, uint8_t length, bool noColon); std::string to_string_formatted_mac_address(const char* macAddress); +void generateRandomMacAddress(char* macAddress); /* Scales 'n' to be a value less than 1000. 'base_unit' is the index of the unit from * 'unit_prefix' that 'n' is in initially. 3 is the index of the '1s' unit. */ diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index b5c68608d..913b4eb9a 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -86,6 +86,7 @@ #include "ais_app.hpp" #include "analog_audio_app.hpp" #include "analog_tv_app.hpp" +#include "ble_comm_app.hpp" #include "ble_rx_app.hpp" #include "ble_tx_app.hpp" #include "capture_app.hpp" @@ -551,6 +552,7 @@ ReceiversMenuView::ReceiversMenuView(NavigationView& nav) { {"APRS", Color::green(), &bitmap_icon_aprs, [&nav]() { nav.push(); }}, {"Audio", Color::green(), &bitmap_icon_speaker, [&nav]() { nav.push(); }}, //{"BTLE", Color::yellow(), &bitmap_icon_btle, [&nav]() { nav.push(); }}, + {"BLE Comm", ui::Color::orange(), &bitmap_icon_btle, [&nav]() { nav.push(); }}, {"BLE Rx", Color::green(), &bitmap_icon_btle, [&nav]() { nav.push(); }}, {"ERT Meter", Color::green(), &bitmap_icon_ert, [&nav]() { nav.push(); }}, {"Level", Color::green(), &bitmap_icon_options_radio, [&nav]() { nav.push(); }}, From b4f96563795f311b7f02bd486de82882030aa0b4 Mon Sep 17 00:00:00 2001 From: TJ Baginski Date: Sun, 12 Nov 2023 16:34:05 -0500 Subject: [PATCH 02/15] SCAN_RSP MAC was reversed. --- firmware/application/apps/ble_rx_app.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/firmware/application/apps/ble_rx_app.cpp b/firmware/application/apps/ble_rx_app.cpp index f4bf7ea22..58af41819 100644 --- a/firmware/application/apps/ble_rx_app.cpp +++ b/firmware/application/apps/ble_rx_app.cpp @@ -56,6 +56,22 @@ uint64_t copy_mac_address_to_uint64(const uint8_t* macAddress) { return result; } +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--; + } +} + namespace ui { std::string pdu_type_to_string(ADV_PDU_TYPE type) { @@ -584,6 +600,11 @@ void BLERxView::updateEntry(const BlePacketData* packet, BleRecentEntry& entry, } } } + 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); + } } } /* namespace ui */ From 39e3b39c35c399aa8e5c0cdf6cf389d5fa3f9504 Mon Sep 17 00:00:00 2001 From: Netro Date: Mon, 13 Nov 2023 14:03:21 -0500 Subject: [PATCH 03/15] Code formatting. --- firmware/application/apps/ble_comm_app.cpp | 28 ++++++++-------------- firmware/application/apps/ble_comm_app.hpp | 2 +- firmware/application/apps/ble_rx_app.cpp | 4 +--- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/firmware/application/apps/ble_comm_app.cpp b/firmware/application/apps/ble_comm_app.cpp index 90c627a74..a4d22fe2c 100644 --- a/firmware/application/apps/ble_comm_app.cpp +++ b/firmware/application/apps/ble_comm_app.cpp @@ -149,7 +149,7 @@ BLETxPacket BLECommView::build_adv_packet() { strncpy(bleTxPacket.macAddress, randomMac, 12); strncpy(bleTxPacket.advertisementData, dataString.c_str(), dataString.length()); - //Little note that, at 64 timer, 40 packets is around 1 second. So this should advertise for 5 seconds for 200 packets. + // Little note that, at 64 timer, 40 packets is around 1 second. So this should advertise for 5 seconds for 200 packets. strncpy(bleTxPacket.packetCount, "200", 4); bleTxPacket.packet_count = 200; @@ -157,9 +157,7 @@ BLETxPacket BLECommView::build_adv_packet() { } void BLECommView::startTx(BLETxPacket packetToSend, PKT_TYPE pduType) { - - if (!is_sending_tx()) - { + if (!is_sending_tx()) { switch_rx_tx(false); packet_counter = packetToSend.packet_count; @@ -169,8 +167,7 @@ void BLECommView::startTx(BLETxPacket packetToSend, PKT_TYPE pduType) { transmitter_model.enable(); is_running_tx = true; - } - else { + } else { baseband::set_btletx(channel_number_tx, packetToSend.macAddress, packetToSend.advertisementData, pduType); } @@ -193,18 +190,14 @@ void BLECommView::stopTx() { is_running_tx = false; } -void BLECommView::switch_rx_tx(bool inRxMode) -{ - if (inRxMode) - { - //Start Rx +void BLECommView::switch_rx_tx(bool inRxMode) { + if (inRxMode) { + // Start Rx transmitter_model.disable(); baseband::shutdown(); baseband::run_image(portapack::spi_flash::image_tag_btle_rx); - } - else - { - //Start Tx + } else { + // Start Tx receiver_model.disable(); baseband::shutdown(); baseband::run_image(portapack::spi_flash::image_tag_btle_tx); @@ -277,7 +270,7 @@ void BLECommView::on_data(BlePacketData* packet) { console.write(str_console); - //uint64_t macAddressEncoded = copy_mac_address_to_uint64(packet->macAddress); + // uint64_t macAddressEncoded = copy_mac_address_to_uint64(packet->macAddress); parse_received_packet(packet, (ADV_PDU_TYPE)packet->type); @@ -293,8 +286,7 @@ void BLECommView::on_tx_progress(const bool done) { // Reached end of current packet repeats. if (packet_counter == 0) { stopTx(); - } - else { + } else { if ((timer_count % timer_period) == 0) { startTx(build_adv_packet(), PKT_TYPE_DISCOVERY); } diff --git a/firmware/application/apps/ble_comm_app.hpp b/firmware/application/apps/ble_comm_app.hpp index a19930d4d..bf4852a55 100644 --- a/firmware/application/apps/ble_comm_app.hpp +++ b/firmware/application/apps/ble_comm_app.hpp @@ -134,7 +134,7 @@ class BLECommView : public View { {24 * 8, 5, 6 * 8, 4}}; Labels label_send_adv{ - {{0 * 8, 2 * 8}, "Send Advertisement:", Color::light_grey()}}; + {{0 * 8, 2 * 8}, "Send Advertisement:", Color::light_grey()}}; ImageButton button_send_adv{ {21 * 8, 1 * 16, 10 * 8, 2 * 16}, diff --git a/firmware/application/apps/ble_rx_app.cpp b/firmware/application/apps/ble_rx_app.cpp index 58af41819..bf90de0b0 100644 --- a/firmware/application/apps/ble_rx_app.cpp +++ b/firmware/application/apps/ble_rx_app.cpp @@ -599,9 +599,7 @@ void BLERxView::updateEntry(const BlePacketData* packet, BleRecentEntry& entry, break; } } - } - else if (pdu_type == ADV_DIRECT_IND || pdu_type == SCAN_REQ) - { + } 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); } From 6705da1ac919f98d32e79f98ff952b6b99eaca81 Mon Sep 17 00:00:00 2001 From: Netro Date: Mon, 13 Nov 2023 14:14:05 -0500 Subject: [PATCH 04/15] Commenting out BLE Comm for commit --- firmware/application/ui_navigation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index 913b4eb9a..606482961 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -552,7 +552,7 @@ ReceiversMenuView::ReceiversMenuView(NavigationView& nav) { {"APRS", Color::green(), &bitmap_icon_aprs, [&nav]() { nav.push(); }}, {"Audio", Color::green(), &bitmap_icon_speaker, [&nav]() { nav.push(); }}, //{"BTLE", Color::yellow(), &bitmap_icon_btle, [&nav]() { nav.push(); }}, - {"BLE Comm", ui::Color::orange(), &bitmap_icon_btle, [&nav]() { nav.push(); }}, + //{"BLE Comm", ui::Color::orange(), &bitmap_icon_btle, [&nav]() { nav.push(); }}, {"BLE Rx", Color::green(), &bitmap_icon_btle, [&nav]() { nav.push(); }}, {"ERT Meter", Color::green(), &bitmap_icon_ert, [&nav]() { nav.push(); }}, {"Level", Color::green(), &bitmap_icon_options_radio, [&nav]() { nav.push(); }}, From d2ad9f16217fa7b3e61baf2e36ae0590f396002c Mon Sep 17 00:00:00 2001 From: Netro Date: Mon, 13 Nov 2023 15:14:25 -0500 Subject: [PATCH 05/15] Added Auto Channel Hop. --- firmware/application/apps/ble_rx_app.cpp | 18 ++++++++++++++++++ firmware/application/apps/ble_rx_app.hpp | 4 +++- firmware/application/apps/ble_tx_app.cpp | 23 +++++++++++++++++++++-- firmware/application/apps/ble_tx_app.hpp | 4 +++- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/firmware/application/apps/ble_rx_app.cpp b/firmware/application/apps/ble_rx_app.cpp index bf90de0b0..105b4222b 100644 --- a/firmware/application/apps/ble_rx_app.cpp +++ b/firmware/application/apps/ble_rx_app.cpp @@ -418,6 +418,14 @@ BLERxView::BLERxView(NavigationView& nav) }; options_channel.on_change = [this](size_t, int32_t i) { + // If we selected Auto don't do anything and Auto will handle changing. + if (i == 40) { + auto_channel = true; + return; + } else { + auto_channel = false; + } + field_frequency.set_value(get_freq_by_channel_number(i)); channel_number = i; @@ -440,6 +448,16 @@ BLERxView::BLERxView(NavigationView& nav) } void BLERxView::on_data(BlePacketData* packet) { + 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); + } + std::string str_console = ""; if (!logging) { diff --git a/firmware/application/apps/ble_rx_app.hpp b/firmware/application/apps/ble_rx_app.hpp index 3b387fd69..6d6f9ec33 100644 --- a/firmware/application/apps/ble_rx_app.hpp +++ b/firmware/application/apps/ble_rx_app.hpp @@ -196,6 +196,7 @@ class BLERxView : public View { uint8_t console_color{0}; uint32_t prev_value{0}; uint8_t channel_number = 37; + bool auto_channel = false; std::string filterBuffer{}; std::string filter{}; @@ -208,7 +209,8 @@ class BLERxView : public View { 5, {{"Ch.37 ", 37}, {"Ch.38", 38}, - {"Ch.39", 39}}}; + {"Ch.39", 39}, + {"Auto", 40}}}; RxFrequencyField field_frequency{ {6 * 8, 0 * 16}, diff --git a/firmware/application/apps/ble_tx_app.cpp b/firmware/application/apps/ble_tx_app.cpp index 55fda0512..edfe850e1 100644 --- a/firmware/application/apps/ble_tx_app.cpp +++ b/firmware/application/apps/ble_tx_app.cpp @@ -194,6 +194,17 @@ void BLETxView::toggle() { } void BLETxView::start() { + int randomChannel = channel_number; + + if (auto_channel) { + int min = 37; + int max = 39; + + randomChannel = min + std::rand() % (max - min + 1); + + field_frequency.set_value(get_freq_by_channel_number(randomChannel)); + } + // Generate new random Mac Address. generateRandomMacAddress(randomMac); @@ -211,7 +222,7 @@ void BLETxView::start() { progressbar.set_max(packets[0].packet_count); button_play.set_bitmap(&bitmap_stop); - baseband::set_btletx(channel_number, random_mac ? randomMac : packets[0].macAddress, packets[0].advertisementData, pduType); + baseband::set_btletx(randomChannel, random_mac ? randomMac : packets[0].macAddress, packets[0].advertisementData, pduType); transmitter_model.enable(); is_running = true; @@ -219,7 +230,7 @@ void BLETxView::start() { } else { // Send next packet. progressbar.set_max(packets[current_packet].packet_count); - baseband::set_btletx(channel_number, random_mac ? randomMac : packets[current_packet].macAddress, packets[current_packet].advertisementData, pduType); + baseband::set_btletx(randomChannel, random_mac ? randomMac : packets[current_packet].macAddress, packets[current_packet].advertisementData, pduType); } if ((packet_counter % 10) == 0) { @@ -305,6 +316,14 @@ BLETxView::BLETxView(NavigationView& nav) }; options_channel.on_change = [this](size_t, int32_t i) { + // If we selected Auto don't do anything and Auto will handle changing. + if (i == 40) { + auto_channel = true; + return; + } else { + auto_channel = false; + } + field_frequency.set_value(get_freq_by_channel_number(i)); channel_number = i; }; diff --git a/firmware/application/apps/ble_tx_app.hpp b/firmware/application/apps/ble_tx_app.hpp index 001645fac..d79cac6d3 100644 --- a/firmware/application/apps/ble_tx_app.hpp +++ b/firmware/application/apps/ble_tx_app.hpp @@ -135,6 +135,7 @@ class BLETxView : public View { std::filesystem::path file_path{}; std::filesystem::path packet_save_path{u"BLETX/BLETX_????.TXT"}; uint8_t channel_number = 37; + bool auto_channel = false; char randomMac[13] = "010203040506"; @@ -215,7 +216,8 @@ class BLETxView : public View { 5, {{"Ch.37 ", 37}, {"Ch.38", 38}, - {"Ch.39", 39}}}; + {"Ch.39", 39}, + {"Auto", 40}}}; OptionsField options_adv_type{ {17 * 8, 6 * 8}, From c46d135abcca1fb9d04e8867d81f449846e9b992 Mon Sep 17 00:00:00 2001 From: Netro Date: Mon, 13 Nov 2023 16:54:28 -0500 Subject: [PATCH 06/15] Improvements to Tx to better handle timers. Rather than relying on some artificial counter. --- firmware/application/apps/ble_tx_app.cpp | 51 ++++++++++++++---------- firmware/application/apps/ble_tx_app.hpp | 21 +++++++--- firmware/baseband/proc_ble_tx.cpp | 6 +-- 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/firmware/application/apps/ble_tx_app.cpp b/firmware/application/apps/ble_tx_app.cpp index edfe850e1..3058ef6fc 100644 --- a/firmware/application/apps/ble_tx_app.cpp +++ b/firmware/application/apps/ble_tx_app.cpp @@ -208,8 +208,8 @@ void BLETxView::start() { // Generate new random Mac Address. generateRandomMacAddress(randomMac); + // If this is our first run, check file. if (!is_active()) { - // Check if file is present before continuing. File data_file; auto error = data_file.open(file_path); @@ -217,22 +217,19 @@ void BLETxView::start() { file_error(); check_loop.set_value(false); return; - } else { - // Send first or single packet. - progressbar.set_max(packets[0].packet_count); - button_play.set_bitmap(&bitmap_stop); - - baseband::set_btletx(randomChannel, random_mac ? randomMac : packets[0].macAddress, packets[0].advertisementData, pduType); - transmitter_model.enable(); - - is_running = true; } - } else { - // Send next packet. - progressbar.set_max(packets[current_packet].packet_count); - baseband::set_btletx(randomChannel, random_mac ? randomMac : packets[current_packet].macAddress, packets[current_packet].advertisementData, pduType); + + transmitter_model.enable(); + button_play.set_bitmap(&bitmap_stop); + is_running = true; } + // Send next packet. + progressbar.set_max(packets[current_packet].packet_count); + baseband::set_btletx(randomChannel, random_mac ? randomMac : packets[current_packet].macAddress, packets[current_packet].advertisementData, pduType); + + is_sending = true; + if ((packet_counter % 10) == 0) { text_packets_sent.set(to_string_dec_uint(packet_counter)); } @@ -252,9 +249,12 @@ void BLETxView::stop() { is_running = false; } -void BLETxView::on_tx_progress(const bool done) { - if (done) { - if (is_active()) { +// called each 1/60th of second, so 6 = 100ms +void BLETxView::on_timer() { + if (++mscounter == timer_period) { + mscounter = 0; + + if (is_active() && !is_sending) { // Reached end of current packet repeats. if (packet_counter == 0) { // Done sending all packets. @@ -262,20 +262,28 @@ void BLETxView::on_tx_progress(const bool done) { // If looping, restart from beginning. if (check_loop.value()) { update_current_packet(packets[current_packet], 0); + start(); } else { stop(); } } else { current_packet++; update_current_packet(packets[current_packet], current_packet); - } - } else { - if ((timer_count % timer_period) == 0) { start(); } } + else + { + start(); + } + } + } +} - timer_count++; +void BLETxView::on_tx_progress(const bool done) { + if (done) { + if (is_active()) { + is_sending = false; } } } @@ -330,6 +338,7 @@ BLETxView::BLETxView(NavigationView& nav) options_speed.on_change = [this](size_t, int32_t i) { timer_period = i; + mscounter = 0; }; options_adv_type.on_change = [this](size_t, int32_t i) { diff --git a/firmware/application/apps/ble_tx_app.hpp b/firmware/application/apps/ble_tx_app.hpp index d79cac6d3..f871d6f7c 100644 --- a/firmware/application/apps/ble_tx_app.hpp +++ b/firmware/application/apps/ble_tx_app.hpp @@ -114,6 +114,7 @@ class BLETxView : public View { std::string title() const override { return "BLE TX"; }; private: + void on_timer(); void on_data(uint32_t value, bool is_data); void on_file_changed(const std::filesystem::path& new_file_path); void on_save_file(const std::string value); @@ -140,8 +141,9 @@ class BLETxView : public View { char randomMac[13] = "010203040506"; bool is_running = false; + bool is_sending = false; uint64_t timer_count{0}; - uint64_t timer_period{256}; + int16_t timer_period{30}; bool repeatLoop = false; uint32_t packet_counter{0}; uint32_t num_packets{0}; @@ -154,6 +156,7 @@ class BLETxView : public View { static constexpr uint8_t max_packet_repeat_str{10}; static constexpr uint32_t max_packet_repeat_count{UINT32_MAX}; static constexpr uint32_t max_num_packets{32}; + int16_t mscounter = 0; BLETxPacket packets[max_num_packets]; @@ -205,11 +208,11 @@ class BLETxView : public View { OptionsField options_speed{ {7 * 8, 6 * 8}, 3, - {{"1 ", 256}, - {"2 ", 128}, - {"3 ", 64}, - {"4 ", 32}, - {"5 ", 16}}}; + {{"1 ", 1}, //16ms + {"2 ", 2}, //32ms + {"3 ", 3}, //48ms + {"4 ", 6}, //100ms + {"5 ", 12}}}; //200ms OptionsField options_channel{ {11 * 8, 6 * 8}, @@ -286,6 +289,12 @@ class BLETxView : public View { const auto message = *reinterpret_cast(p); this->on_tx_progress(message.done); }}; + + MessageHandlerRegistration message_handler_frame_sync{ + Message::ID::DisplayFrameSync, + [this](const Message* const) { + this->on_timer(); + }}; }; } /* namespace ui */ diff --git a/firmware/baseband/proc_ble_tx.cpp b/firmware/baseband/proc_ble_tx.cpp index 71c77d6ea..bb8c86195 100644 --- a/firmware/baseband/proc_ble_tx.cpp +++ b/firmware/baseband/proc_ble_tx.cpp @@ -339,6 +339,9 @@ void BTLETxProcessor::execute(const buffer_c8_t& buffer) { if (sample_count > length) { configured = false; sample_count = 0; + + txprogress_message.done = true; + shared_memory.application_queue.push(txprogress_message); } else { // Real and imaginary was already calculated in gen_sample_from_phy_bit. // It was processed from each data bit, run through a Gaussian Filter, and then ran through sin and cos table to get each IQ bit. @@ -363,9 +366,6 @@ void BTLETxProcessor::execute(const buffer_c8_t& buffer) { buffer.p[i] = {re, im}; } } - - txprogress_message.done = true; - shared_memory.application_queue.push(txprogress_message); } void BTLETxProcessor::on_message(const Message* const message) { From 0d710236c51d1bb873cf7c0581b6d4c0c4c818f8 Mon Sep 17 00:00:00 2001 From: Netro Date: Mon, 13 Nov 2023 16:54:45 -0500 Subject: [PATCH 07/15] formatting. --- firmware/application/apps/ble_tx_app.cpp | 6 ++---- firmware/application/apps/ble_tx_app.hpp | 10 +++++----- firmware/baseband/proc_ble_tx.cpp | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/firmware/application/apps/ble_tx_app.cpp b/firmware/application/apps/ble_tx_app.cpp index 3058ef6fc..0263679ff 100644 --- a/firmware/application/apps/ble_tx_app.cpp +++ b/firmware/application/apps/ble_tx_app.cpp @@ -271,11 +271,9 @@ void BLETxView::on_timer() { update_current_packet(packets[current_packet], current_packet); start(); } - } - else - { + } else { start(); - } + } } } } diff --git a/firmware/application/apps/ble_tx_app.hpp b/firmware/application/apps/ble_tx_app.hpp index f871d6f7c..d735def6f 100644 --- a/firmware/application/apps/ble_tx_app.hpp +++ b/firmware/application/apps/ble_tx_app.hpp @@ -208,11 +208,11 @@ class BLETxView : public View { OptionsField options_speed{ {7 * 8, 6 * 8}, 3, - {{"1 ", 1}, //16ms - {"2 ", 2}, //32ms - {"3 ", 3}, //48ms - {"4 ", 6}, //100ms - {"5 ", 12}}}; //200ms + {{"1 ", 1}, // 16ms + {"2 ", 2}, // 32ms + {"3 ", 3}, // 48ms + {"4 ", 6}, // 100ms + {"5 ", 12}}}; // 200ms OptionsField options_channel{ {11 * 8, 6 * 8}, diff --git a/firmware/baseband/proc_ble_tx.cpp b/firmware/baseband/proc_ble_tx.cpp index bb8c86195..57d6af9e9 100644 --- a/firmware/baseband/proc_ble_tx.cpp +++ b/firmware/baseband/proc_ble_tx.cpp @@ -339,7 +339,7 @@ void BTLETxProcessor::execute(const buffer_c8_t& buffer) { if (sample_count > length) { configured = false; sample_count = 0; - + txprogress_message.done = true; shared_memory.application_queue.push(txprogress_message); } else { From 7a014e8ac34ba97adb2a236f8ec66c6bae34644c Mon Sep 17 00:00:00 2001 From: Netro Date: Mon, 13 Nov 2023 16:57:03 -0500 Subject: [PATCH 08/15] forgot to change timer default --- firmware/application/apps/ble_tx_app.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/application/apps/ble_tx_app.hpp b/firmware/application/apps/ble_tx_app.hpp index d735def6f..cc9dea30e 100644 --- a/firmware/application/apps/ble_tx_app.hpp +++ b/firmware/application/apps/ble_tx_app.hpp @@ -143,7 +143,7 @@ class BLETxView : public View { bool is_running = false; bool is_sending = false; uint64_t timer_count{0}; - int16_t timer_period{30}; + int16_t timer_period{1}; bool repeatLoop = false; uint32_t packet_counter{0}; uint32_t num_packets{0}; From b1d9168cbd620919672e74b1f622c0c56fdfb1c3 Mon Sep 17 00:00:00 2001 From: Netro Date: Tue, 14 Nov 2023 14:35:52 -0500 Subject: [PATCH 09/15] Auto channel and more work on timers. --- firmware/application/apps/ble_comm_app.cpp | 230 +++++++++------------ firmware/application/apps/ble_comm_app.hpp | 32 ++- firmware/application/apps/ble_tx_app.hpp | 15 +- 3 files changed, 134 insertions(+), 143 deletions(-) diff --git a/firmware/application/apps/ble_comm_app.cpp b/firmware/application/apps/ble_comm_app.cpp index a4d22fe2c..fac01644e 100644 --- a/firmware/application/apps/ble_comm_app.cpp +++ b/firmware/application/apps/ble_comm_app.cpp @@ -104,16 +104,28 @@ BLECommView::BLECommView(NavigationView& nav) }; options_channel.on_change = [this](size_t, int32_t i) { + // If we selected Auto don't do anything and Auto will handle changing. + if (i == 40) { + auto_channel = true; + return; + } else { + auto_channel = false; + } + field_frequency.set_value(get_freq_by_channel_number(i)); channel_number_rx = i; + channel_number_tx = i; }; - options_channel.set_selected_index(0, true); + options_channel.set_selected_index(3, true); logger = std::make_unique(); // Generate new random Mac Address upon each new startup. generateRandomMacAddress(randomMac); + + // Setup Initial Advertise Packet. + advertisePacket = build_adv_packet(); } void BLECommView::set_parent_rect(const Rect new_parent_rect) { @@ -128,15 +140,15 @@ BLECommView::~BLECommView() { baseband::shutdown(); } -bool BLECommView::is_sending_tx() const { +bool BLECommView::in_tx_mode() const { return (bool)is_running_tx; } void BLECommView::toggle() { - if (is_sending_tx()) { - stopTx(); + if (in_tx_mode()) { + sendAdvertisement(false); } else { - startTx(build_adv_packet(), PKT_TYPE_DISCOVERY); + sendAdvertisement(true); } } @@ -149,32 +161,61 @@ BLETxPacket BLECommView::build_adv_packet() { strncpy(bleTxPacket.macAddress, randomMac, 12); strncpy(bleTxPacket.advertisementData, dataString.c_str(), dataString.length()); - // Little note that, at 64 timer, 40 packets is around 1 second. So this should advertise for 5 seconds for 200 packets. - strncpy(bleTxPacket.packetCount, "200", 4); - bleTxPacket.packet_count = 200; + // Little note that 120 packets is around 2 seconds at timer rate of 16ms per tick. + strncpy(bleTxPacket.packetCount, "120", 4); + bleTxPacket.packet_count = 120; + + bleTxPacket.packetType = PKT_TYPE_DISCOVERY; return bleTxPacket; } -void BLECommView::startTx(BLETxPacket packetToSend, PKT_TYPE pduType) { - if (!is_sending_tx()) { +void BLECommView::sendAdvertisement(bool enable) +{ + if (enable) + { + startTx(advertisePacket); + } + else + { + stopTx(); + } +} + +void BLECommView::startTx(BLETxPacket packetToSend) { + + int randomChannel = channel_number_tx; + + if (auto_channel) { + int min = 37; + int max = 39; + + randomChannel = min + std::rand() % (max - min + 1); + + field_frequency.set_value(get_freq_by_channel_number(randomChannel)); + } + + if (!in_tx_mode()) { switch_rx_tx(false); - packet_counter = packetToSend.packet_count; + currentPacket = packetToSend; + packet_counter = currentPacket.packet_count; button_send_adv.set_bitmap(&bitmap_stop); - baseband::set_btletx(channel_number_tx, packetToSend.macAddress, packetToSend.advertisementData, pduType); + baseband::set_btletx(randomChannel, currentPacket.macAddress, currentPacket.advertisementData, currentPacket.packetType); transmitter_model.enable(); is_running_tx = true; } else { - baseband::set_btletx(channel_number_tx, packetToSend.macAddress, packetToSend.advertisementData, pduType); + baseband::set_btletx(randomChannel, currentPacket.macAddress, currentPacket.advertisementData, currentPacket.packetType); } if ((packet_counter % 10) == 0) { text_packets_sent.set(to_string_dec_uint(packet_counter)); } + is_sending = true; + packet_counter--; } @@ -205,94 +246,39 @@ void BLECommView::switch_rx_tx(bool inRxMode) { } void BLECommView::on_data(BlePacketData* packet) { - std::string str_console = ""; - - if (!logging) { - str_log = ""; - } - - switch ((ADV_PDU_TYPE)packet->type) { - case ADV_IND: - str_console += "ADV_IND"; - break; - case ADV_DIRECT_IND: - str_console += "ADV_DIRECT_IND"; - break; - case ADV_NONCONN_IND: - str_console += "ADV_NONCONN_IND"; - break; - case SCAN_REQ: - str_console += "SCAN_REQ"; - break; - case SCAN_RSP: - str_console += "SCAN_RSP"; - break; - case CONNECT_REQ: - str_console += "CONNECT_REQ"; - break; - case ADV_SCAN_IND: - str_console += "ADV_SCAN_IND"; - break; - case RESERVED0: - case RESERVED1: - case RESERVED2: - case RESERVED3: - case RESERVED4: - case RESERVED5: - case RESERVED6: - case RESERVED7: - case RESERVED8: - str_console += "RESERVED"; - break; - default: - str_console += "UNKNOWN"; - break; - } - - str_console += " Len:"; - str_console += to_string_dec_uint(packet->size); - - str_console += "\n"; - - str_console += "Mac:"; - str_console += to_string_mac_address(packet->macAddress, 6, false); - - str_console += "\n"; - str_console += "Data:"; - - int i; - - for (i = 0; i < packet->dataLen; i++) { - str_console += to_string_hex(packet->data[i], 2); - } - - str_console += "\n"; - - console.write(str_console); - - // uint64_t macAddressEncoded = copy_mac_address_to_uint64(packet->macAddress); - parse_received_packet(packet, (ADV_PDU_TYPE)packet->type); - - // Log at End of Packet. - if (logger && logging) { - logger->log_raw_data(str_console); - } } -void BLECommView::on_tx_progress(const bool done) { - if (done) { - if (is_sending_tx()) { +// called each 1/60th of second, so 6 = 100ms +void BLECommView::on_timer() { + if (in_tx_mode()) { + if(!is_sending) + { // Reached end of current packet repeats. if (packet_counter == 0) { stopTx(); } else { - if ((timer_count % timer_period) == 0) { - startTx(build_adv_packet(), PKT_TYPE_DISCOVERY); - } + startTx(currentPacket); } + } + } + else + { + // If timer expired and we need to send something. + if (is_looping && (++timer_rx_counter == timer_rx_period)) + { + timer_rx_counter = 0; + + //Handle what we need to send. + startTx(currentPacket); + } + } +} - timer_count++; +void BLECommView::on_tx_progress(const bool done) { + if (done) { + if (in_tx_mode()) { + is_sending = false; } } } @@ -306,54 +292,40 @@ void BLECommView::parse_received_packet(const BlePacketData* packet, ADV_PDU_TYP data_string += to_string_hex(packet->data[i], 2); } - currentPacket.dbValue = packet->max_dB; - currentPacket.timestamp = to_string_timestamp(rtc_time::now()); - currentPacket.dataString = data_string; + receivedPacket.dbValue = packet->max_dB; + receivedPacket.timestamp = to_string_timestamp(rtc_time::now()); + receivedPacket.dataString = data_string; - currentPacket.packetData.type = packet->type; - currentPacket.packetData.size = packet->size; - currentPacket.packetData.dataLen = packet->dataLen; + receivedPacket.packetData.type = packet->type; + receivedPacket.packetData.size = packet->size; + receivedPacket.packetData.dataLen = packet->dataLen; - currentPacket.packetData.macAddress[0] = packet->macAddress[0]; - currentPacket.packetData.macAddress[1] = packet->macAddress[1]; - currentPacket.packetData.macAddress[2] = packet->macAddress[2]; - currentPacket.packetData.macAddress[3] = packet->macAddress[3]; - currentPacket.packetData.macAddress[4] = packet->macAddress[4]; - currentPacket.packetData.macAddress[5] = packet->macAddress[5]; + receivedPacket.packetData.macAddress[0] = packet->macAddress[0]; + receivedPacket.packetData.macAddress[1] = packet->macAddress[1]; + receivedPacket.packetData.macAddress[2] = packet->macAddress[2]; + receivedPacket.packetData.macAddress[3] = packet->macAddress[3]; + receivedPacket.packetData.macAddress[4] = packet->macAddress[4]; + receivedPacket.packetData.macAddress[5] = packet->macAddress[5]; - currentPacket.numHits++; + receivedPacket.numHits++; for (int i = 0; i < packet->dataLen; i++) { - currentPacket.packetData.data[i] = packet->data[i]; + receivedPacket.packetData.data[i] = packet->data[i]; } std::string nameString; // 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) && nameString.empty()) { - ADV_PDU_PAYLOAD_TYPE_0_2_4_6* advertiseData = (ADV_PDU_PAYLOAD_TYPE_0_2_4_6*)packet->data; - - uint8_t currentByte = 0; - uint8_t length = 0; - uint8_t type = 0; - - std::string decoded_data; - for (currentByte = 0; (currentByte < packet->dataLen);) { - length = advertiseData->Data[currentByte++]; - type = advertiseData->Data[currentByte++]; - - // Subtract 1 because type is part of the length. - for (int i = 0; i < length - 1; i++) { - if (type == 0x08 || type == 0x09) { - decoded_data += (char)advertiseData->Data[currentByte]; - } - currentByte++; - } - if (!decoded_data.empty()) { - nameString = std::move(decoded_data); - break; - } - } + ADV_PDU_PAYLOAD_TYPE_1_3 * directed_mac_data = (ADV_PDU_PAYLOAD_TYPE_1_3*)packet->data; + + std::reverse(directed_mac_data->A1, directed_mac_data->A1 + 6); + console.clear(true); + std::string str_console = ""; + + str_console += "MY MAC:" + to_string_formatted_mac_address(randomMac) + "\n"; + str_console += "SCAN MAC:" + to_string_mac_address(directed_mac_data->A1, 6, false) + "\n"; + console.write(str_console); } } diff --git a/firmware/application/apps/ble_comm_app.hpp b/firmware/application/apps/ble_comm_app.hpp index bf4852a55..ed42ed241 100644 --- a/firmware/application/apps/ble_comm_app.hpp +++ b/firmware/application/apps/ble_comm_app.hpp @@ -68,13 +68,15 @@ class BLECommView : public View { private: BLETxPacket build_adv_packet(); void switch_rx_tx(bool inRxMode); - void startTx(BLETxPacket packetToSend, PKT_TYPE pduType); + void startTx(BLETxPacket packetToSend); void stopTx(); void toggle(); - bool is_sending_tx() const; + bool in_tx_mode() const; + void on_timer(); void on_data(BlePacketData* packetData); void on_tx_progress(const bool done); void parse_received_packet(const BlePacketData* packet, ADV_PDU_TYPE pdu_type); + void sendAdvertisement(bool enable); NavigationView& nav_; @@ -98,12 +100,23 @@ class BLECommView : public View { uint8_t channel_number_tx = 37; uint8_t channel_number_rx = 37; + bool auto_channel = false; char randomMac[13] = "010203040506"; + bool is_running_tx = false; - uint64_t timer_count{0}; - uint64_t timer_period{64}; + bool is_sending = false; + bool is_looping = false; + + int16_t timer_period{1}; //Delay each packet by 16ms. + int16_t timer_counter = 0; + int16_t timer_rx_counter = 0; + int16_t timer_rx_period{12}; //Poll Rx for at least 200ms. (TBD) + uint32_t packet_counter{0}; + BLETxPacket advertisePacket{}; + BLETxPacket currentPacket{}; + BleRecentEntry receivedPacket{}; static constexpr auto header_height = 5 * 16; @@ -112,7 +125,8 @@ class BLECommView : public View { 5, {{"Ch.37 ", 37}, {"Ch.38", 38}, - {"Ch.39", 39}}}; + {"Ch.39", 39}, + {"Auto", 40}}}; RxFrequencyField field_frequency{ {6 * 8, 0 * 16}, @@ -161,8 +175,6 @@ class BLECommView : public View { std::string str_log{""}; bool logging{false}; - BleRecentEntry currentPacket = {}; - std::unique_ptr logger{}; MessageHandlerRegistration message_handler_packet{ @@ -178,6 +190,12 @@ class BLECommView : public View { const auto message = *reinterpret_cast(p); this->on_tx_progress(message.done); }}; + + MessageHandlerRegistration message_handler_frame_sync{ + Message::ID::DisplayFrameSync, + [this](const Message* const) { + this->on_timer(); + }}; }; } /* namespace ui */ diff --git a/firmware/application/apps/ble_tx_app.hpp b/firmware/application/apps/ble_tx_app.hpp index cc9dea30e..d22c5782f 100644 --- a/firmware/application/apps/ble_tx_app.hpp +++ b/firmware/application/apps/ble_tx_app.hpp @@ -55,13 +55,6 @@ class BLELoggerTx { namespace ui { -struct BLETxPacket { - char macAddress[13]; - char advertisementData[63]; - char packetCount[11]; - uint32_t packet_count; -}; - enum PKT_TYPE { PKT_TYPE_INVALID_TYPE, PKT_TYPE_RAW, @@ -92,6 +85,14 @@ enum PKT_TYPE { PKT_TYPE_NUM_PKT_TYPE }; +struct BLETxPacket { + char macAddress[13]; + char advertisementData[63]; + char packetCount[11]; + uint32_t packet_count; + PKT_TYPE packetType; +}; + class BLETxView : public View { public: BLETxView(NavigationView& nav); From b3a321ee27da54db3403047038d3aa1b68f808e9 Mon Sep 17 00:00:00 2001 From: Netro Date: Tue, 14 Nov 2023 14:36:26 -0500 Subject: [PATCH 10/15] formatting --- firmware/application/apps/ble_comm_app.cpp | 25 +++++++--------------- firmware/application/apps/ble_comm_app.hpp | 6 +++--- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/firmware/application/apps/ble_comm_app.cpp b/firmware/application/apps/ble_comm_app.cpp index fac01644e..154dfae17 100644 --- a/firmware/application/apps/ble_comm_app.cpp +++ b/firmware/application/apps/ble_comm_app.cpp @@ -170,20 +170,15 @@ BLETxPacket BLECommView::build_adv_packet() { return bleTxPacket; } -void BLECommView::sendAdvertisement(bool enable) -{ - if (enable) - { +void BLECommView::sendAdvertisement(bool enable) { + if (enable) { startTx(advertisePacket); - } - else - { + } else { stopTx(); } } void BLECommView::startTx(BLETxPacket packetToSend) { - int randomChannel = channel_number_tx; if (auto_channel) { @@ -252,8 +247,7 @@ void BLECommView::on_data(BlePacketData* packet) { // called each 1/60th of second, so 6 = 100ms void BLECommView::on_timer() { if (in_tx_mode()) { - if(!is_sending) - { + if (!is_sending) { // Reached end of current packet repeats. if (packet_counter == 0) { stopTx(); @@ -261,15 +255,12 @@ void BLECommView::on_timer() { startTx(currentPacket); } } - } - else - { + } else { // If timer expired and we need to send something. - if (is_looping && (++timer_rx_counter == timer_rx_period)) - { + if (is_looping && (++timer_rx_counter == timer_rx_period)) { timer_rx_counter = 0; - //Handle what we need to send. + // Handle what we need to send. startTx(currentPacket); } } @@ -317,7 +308,7 @@ void BLECommView::parse_received_packet(const BlePacketData* packet, ADV_PDU_TYP // 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) && nameString.empty()) { - ADV_PDU_PAYLOAD_TYPE_1_3 * directed_mac_data = (ADV_PDU_PAYLOAD_TYPE_1_3*)packet->data; + ADV_PDU_PAYLOAD_TYPE_1_3* directed_mac_data = (ADV_PDU_PAYLOAD_TYPE_1_3*)packet->data; std::reverse(directed_mac_data->A1, directed_mac_data->A1 + 6); console.clear(true); diff --git a/firmware/application/apps/ble_comm_app.hpp b/firmware/application/apps/ble_comm_app.hpp index ed42ed241..c58836dc4 100644 --- a/firmware/application/apps/ble_comm_app.hpp +++ b/firmware/application/apps/ble_comm_app.hpp @@ -108,10 +108,10 @@ class BLECommView : public View { bool is_sending = false; bool is_looping = false; - int16_t timer_period{1}; //Delay each packet by 16ms. + int16_t timer_period{1}; // Delay each packet by 16ms. int16_t timer_counter = 0; int16_t timer_rx_counter = 0; - int16_t timer_rx_period{12}; //Poll Rx for at least 200ms. (TBD) + int16_t timer_rx_period{12}; // Poll Rx for at least 200ms. (TBD) uint32_t packet_counter{0}; BLETxPacket advertisePacket{}; @@ -195,7 +195,7 @@ class BLECommView : public View { Message::ID::DisplayFrameSync, [this](const Message* const) { this->on_timer(); - }}; + }}; }; } /* namespace ui */ From 9e7f831321070944d844beb48a20bac659d4b033 Mon Sep 17 00:00:00 2001 From: Netro Date: Tue, 14 Nov 2023 16:04:45 -0500 Subject: [PATCH 11/15] Setting loop for experimenting. --- firmware/application/apps/ble_comm_app.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/firmware/application/apps/ble_comm_app.cpp b/firmware/application/apps/ble_comm_app.cpp index 154dfae17..f946eabbf 100644 --- a/firmware/application/apps/ble_comm_app.cpp +++ b/firmware/application/apps/ble_comm_app.cpp @@ -162,8 +162,8 @@ BLETxPacket BLECommView::build_adv_packet() { strncpy(bleTxPacket.advertisementData, dataString.c_str(), dataString.length()); // Little note that 120 packets is around 2 seconds at timer rate of 16ms per tick. - strncpy(bleTxPacket.packetCount, "120", 4); - bleTxPacket.packet_count = 120; + strncpy(bleTxPacket.packetCount, "32", 4); + bleTxPacket.packet_count = 32; bleTxPacket.packetType = PKT_TYPE_DISCOVERY; @@ -173,6 +173,7 @@ BLETxPacket BLECommView::build_adv_packet() { void BLECommView::sendAdvertisement(bool enable) { if (enable) { startTx(advertisePacket); + is_looping = true; } else { stopTx(); } From 2b3105cd4fcc3ac710539933b1564ef4740a9ce4 Mon Sep 17 00:00:00 2001 From: Netro Date: Wed, 15 Nov 2023 11:45:30 -0500 Subject: [PATCH 12/15] Trying things with adv periods. --- firmware/application/apps/ble_comm_app.cpp | 56 ++++++++++++---------- firmware/application/apps/ble_comm_app.hpp | 4 +- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/firmware/application/apps/ble_comm_app.cpp b/firmware/application/apps/ble_comm_app.cpp index f946eabbf..fd07d1984 100644 --- a/firmware/application/apps/ble_comm_app.cpp +++ b/firmware/application/apps/ble_comm_app.cpp @@ -162,8 +162,8 @@ BLETxPacket BLECommView::build_adv_packet() { strncpy(bleTxPacket.advertisementData, dataString.c_str(), dataString.length()); // Little note that 120 packets is around 2 seconds at timer rate of 16ms per tick. - strncpy(bleTxPacket.packetCount, "32", 4); - bleTxPacket.packet_count = 32; + strncpy(bleTxPacket.packetCount, "50", 3); + bleTxPacket.packet_count = 50; bleTxPacket.packetType = PKT_TYPE_DISCOVERY; @@ -173,8 +173,9 @@ BLETxPacket BLECommView::build_adv_packet() { void BLECommView::sendAdvertisement(bool enable) { if (enable) { startTx(advertisePacket); - is_looping = true; + is_adv = true; } else { + is_adv = false; stopTx(); } } @@ -246,23 +247,16 @@ void BLECommView::on_data(BlePacketData* packet) { } // called each 1/60th of second, so 6 = 100ms -void BLECommView::on_timer() { - if (in_tx_mode()) { - if (!is_sending) { - // Reached end of current packet repeats. - if (packet_counter == 0) { - stopTx(); - } else { - startTx(currentPacket); - } - } - } else { - // If timer expired and we need to send something. - if (is_looping && (++timer_rx_counter == timer_rx_period)) { - timer_rx_counter = 0; - - // Handle what we need to send. - startTx(currentPacket); +void BLECommView::on_timer() +{ + // Send advertise burst only once every 100ms + if (++timer_counter == timer_period) + { + timer_counter = 0; + + if (!is_adv) + { + sendAdvertisement(true); } } } @@ -270,7 +264,24 @@ void BLECommView::on_timer() { void BLECommView::on_tx_progress(const bool done) { if (done) { if (in_tx_mode()) { + is_sending = false; + + if (packet_counter == 0) + { + if (is_adv) + { + sendAdvertisement(false); + } + else + { + stopTx(); + } + } + else + { + startTx(currentPacket); + } } } } @@ -305,10 +316,7 @@ void BLECommView::parse_received_packet(const BlePacketData* packet, ADV_PDU_TYP receivedPacket.packetData.data[i] = packet->data[i]; } - std::string nameString; - - // 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) && nameString.empty()) { + if (pdu_type == SCAN_REQ) { ADV_PDU_PAYLOAD_TYPE_1_3* directed_mac_data = (ADV_PDU_PAYLOAD_TYPE_1_3*)packet->data; std::reverse(directed_mac_data->A1, directed_mac_data->A1 + 6); diff --git a/firmware/application/apps/ble_comm_app.hpp b/firmware/application/apps/ble_comm_app.hpp index c58836dc4..d94d959b7 100644 --- a/firmware/application/apps/ble_comm_app.hpp +++ b/firmware/application/apps/ble_comm_app.hpp @@ -106,9 +106,9 @@ class BLECommView : public View { bool is_running_tx = false; bool is_sending = false; - bool is_looping = false; + bool is_adv = false; - int16_t timer_period{1}; // Delay each packet by 16ms. + int16_t timer_period{6}; // Delay each packet by 16ms. int16_t timer_counter = 0; int16_t timer_rx_counter = 0; int16_t timer_rx_period{12}; // Poll Rx for at least 200ms. (TBD) From 64cf6962e47eeba6ad9ebae39fea33f147fd6fde Mon Sep 17 00:00:00 2001 From: Netro Date: Wed, 15 Nov 2023 11:45:58 -0500 Subject: [PATCH 13/15] formatting. --- firmware/application/apps/ble_comm_app.cpp | 24 +++++++--------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/firmware/application/apps/ble_comm_app.cpp b/firmware/application/apps/ble_comm_app.cpp index fd07d1984..6899ba14a 100644 --- a/firmware/application/apps/ble_comm_app.cpp +++ b/firmware/application/apps/ble_comm_app.cpp @@ -247,15 +247,12 @@ void BLECommView::on_data(BlePacketData* packet) { } // called each 1/60th of second, so 6 = 100ms -void BLECommView::on_timer() -{ +void BLECommView::on_timer() { // Send advertise burst only once every 100ms - if (++timer_counter == timer_period) - { + if (++timer_counter == timer_period) { timer_counter = 0; - if (!is_adv) - { + if (!is_adv) { sendAdvertisement(true); } } @@ -264,22 +261,15 @@ void BLECommView::on_timer() void BLECommView::on_tx_progress(const bool done) { if (done) { if (in_tx_mode()) { - is_sending = false; - if (packet_counter == 0) - { - if (is_adv) - { + if (packet_counter == 0) { + if (is_adv) { sendAdvertisement(false); - } - else - { + } else { stopTx(); } - } - else - { + } else { startTx(currentPacket); } } From d696b2c9bd67f8f17ff2a59aa60106b01ef8227d Mon Sep 17 00:00:00 2001 From: Netro Date: Wed, 15 Nov 2023 14:14:10 -0500 Subject: [PATCH 14/15] more advertisement numbers. --- firmware/application/apps/ble_comm_app.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/firmware/application/apps/ble_comm_app.cpp b/firmware/application/apps/ble_comm_app.cpp index 6899ba14a..b9dedd90d 100644 --- a/firmware/application/apps/ble_comm_app.cpp +++ b/firmware/application/apps/ble_comm_app.cpp @@ -161,9 +161,9 @@ BLETxPacket BLECommView::build_adv_packet() { strncpy(bleTxPacket.macAddress, randomMac, 12); strncpy(bleTxPacket.advertisementData, dataString.c_str(), dataString.length()); - // Little note that 120 packets is around 2 seconds at timer rate of 16ms per tick. - strncpy(bleTxPacket.packetCount, "50", 3); - bleTxPacket.packet_count = 50; + // Duty cycle of 40% per 100ms advertisment periods. + strncpy(bleTxPacket.packetCount, "80", 3); + bleTxPacket.packet_count = 80; bleTxPacket.packetType = PKT_TYPE_DISCOVERY; @@ -200,6 +200,7 @@ void BLECommView::startTx(BLETxPacket packetToSend) { button_send_adv.set_bitmap(&bitmap_stop); baseband::set_btletx(randomChannel, currentPacket.macAddress, currentPacket.advertisementData, currentPacket.packetType); + transmitter_model.set_tx_gain(47); transmitter_model.enable(); is_running_tx = true; @@ -306,13 +307,24 @@ void BLECommView::parse_received_packet(const BlePacketData* packet, ADV_PDU_TYP receivedPacket.packetData.data[i] = packet->data[i]; } - if (pdu_type == SCAN_REQ) { + if (pdu_type == SCAN_REQ || pdu_type == CONNECT_REQ) { ADV_PDU_PAYLOAD_TYPE_1_3* directed_mac_data = (ADV_PDU_PAYLOAD_TYPE_1_3*)packet->data; std::reverse(directed_mac_data->A1, directed_mac_data->A1 + 6); console.clear(true); std::string str_console = ""; + std::string pduTypeStr = ""; + if (pdu_type == SCAN_REQ) + { + pduTypeStr += "SCAN_REQ"; + } + else if (pdu_type == CONNECT_REQ) + { + pduTypeStr += "CONNECT_REQ"; + } + + str_console += "PACKET TYPE:" + pduTypeStr + "\n"; str_console += "MY MAC:" + to_string_formatted_mac_address(randomMac) + "\n"; str_console += "SCAN MAC:" + to_string_mac_address(directed_mac_data->A1, 6, false) + "\n"; console.write(str_console); From 8e579a61bf235ace971a99390831ffebcf7a06bb Mon Sep 17 00:00:00 2001 From: Netro Date: Wed, 15 Nov 2023 14:14:44 -0500 Subject: [PATCH 15/15] format --- firmware/application/apps/ble_comm_app.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/firmware/application/apps/ble_comm_app.cpp b/firmware/application/apps/ble_comm_app.cpp index b9dedd90d..c910f7149 100644 --- a/firmware/application/apps/ble_comm_app.cpp +++ b/firmware/application/apps/ble_comm_app.cpp @@ -315,12 +315,9 @@ void BLECommView::parse_received_packet(const BlePacketData* packet, ADV_PDU_TYP std::string str_console = ""; std::string pduTypeStr = ""; - if (pdu_type == SCAN_REQ) - { - pduTypeStr += "SCAN_REQ"; - } - else if (pdu_type == CONNECT_REQ) - { + if (pdu_type == SCAN_REQ) { + pduTypeStr += "SCAN_REQ"; + } else if (pdu_type == CONNECT_REQ) { pduTypeStr += "CONNECT_REQ"; }