diff --git a/firmware/application/CMakeLists.txt b/firmware/application/CMakeLists.txt index 856d867f..522d98ab 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 00000000..c910f714 --- /dev/null +++ b/firmware/application/apps/ble_comm_app.cpp @@ -0,0 +1,331 @@ +/* + * 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) { + // 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(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) { + 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::in_tx_mode() const { + return (bool)is_running_tx; +} + +void BLECommView::toggle() { + if (in_tx_mode()) { + sendAdvertisement(false); + } else { + sendAdvertisement(true); + } +} + +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()); + + // Duty cycle of 40% per 100ms advertisment periods. + strncpy(bleTxPacket.packetCount, "80", 3); + bleTxPacket.packet_count = 80; + + bleTxPacket.packetType = PKT_TYPE_DISCOVERY; + + return bleTxPacket; +} + +void BLECommView::sendAdvertisement(bool enable) { + if (enable) { + startTx(advertisePacket); + is_adv = true; + } else { + is_adv = false; + 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); + + currentPacket = packetToSend; + packet_counter = currentPacket.packet_count; + + 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; + } else { + 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--; +} + +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) { + parse_received_packet(packet, (ADV_PDU_TYPE)packet->type); +} + +// called each 1/60th of second, so 6 = 100ms +void BLECommView::on_timer() { + // Send advertise burst only once every 100ms + if (++timer_counter == timer_period) { + timer_counter = 0; + + if (!is_adv) { + sendAdvertisement(true); + } + } +} + +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); + } + } + } +} + +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); + } + + receivedPacket.dbValue = packet->max_dB; + receivedPacket.timestamp = to_string_timestamp(rtc_time::now()); + receivedPacket.dataString = data_string; + + receivedPacket.packetData.type = packet->type; + receivedPacket.packetData.size = packet->size; + receivedPacket.packetData.dataLen = packet->dataLen; + + 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]; + + receivedPacket.numHits++; + + for (int i = 0; i < packet->dataLen; i++) { + receivedPacket.packetData.data[i] = packet->data[i]; + } + + 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); + } +} + +} /* 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 00000000..d94d959b --- /dev/null +++ b/firmware/application/apps/ble_comm_app.hpp @@ -0,0 +1,203 @@ +/* + * 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); + void stopTx(); + void toggle(); + 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_; + + 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; + bool auto_channel = false; + + char randomMac[13] = "010203040506"; + + bool is_running_tx = false; + bool is_sending = false; + bool is_adv = false; + + 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) + + uint32_t packet_counter{0}; + BLETxPacket advertisePacket{}; + BLETxPacket currentPacket{}; + BleRecentEntry receivedPacket{}; + + static constexpr auto header_height = 5 * 16; + + OptionsField options_channel{ + {0 * 8, 0 * 8}, + 5, + {{"Ch.37 ", 37}, + {"Ch.38", 38}, + {"Ch.39", 39}, + {"Auto", 40}}}; + + 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}; + + 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); + }}; + + MessageHandlerRegistration message_handler_frame_sync{ + Message::ID::DisplayFrameSync, + [this](const Message* const) { + this->on_timer(); + }}; +}; + +} /* namespace ui */ + +#endif /*__UI_AFSK_RX_H__*/ diff --git a/firmware/application/apps/ble_rx_app.cpp b/firmware/application/apps/ble_rx_app.cpp index f4bf7ea2..105b4222 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) { @@ -402,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; @@ -424,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) { @@ -583,6 +617,9 @@ void BLERxView::updateEntry(const BlePacketData* packet, BleRecentEntry& entry, break; } } + } 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); } } diff --git a/firmware/application/apps/ble_rx_app.hpp b/firmware/application/apps/ble_rx_app.hpp index 3b387fd6..6d6f9ec3 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 199cffb0..0263679f 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; @@ -205,11 +194,22 @@ 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); + // 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(channel_number, 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(channel_number, 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,26 @@ 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; } } } @@ -316,12 +322,21 @@ 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; }; 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 26eb33fb..d22c5782 100644 --- a/firmware/application/apps/ble_tx_app.hpp +++ b/firmware/application/apps/ble_tx_app.hpp @@ -55,11 +55,42 @@ class BLELoggerTx { namespace ui { +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 +}; + struct BLETxPacket { char macAddress[13]; char advertisementData[63]; char packetCount[11]; uint32_t packet_count; + PKT_TYPE packetType; }; class BLETxView : public View { @@ -84,6 +115,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); @@ -105,12 +137,14 @@ 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"; bool is_running = false; + bool is_sending = false; uint64_t timer_count{0}; - uint64_t timer_period{256}; + int16_t timer_period{1}; bool repeatLoop = false; uint32_t packet_counter{0}; uint32_t num_packets{0}; @@ -118,45 +152,16 @@ 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}; 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]; - 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; @@ -204,30 +209,31 @@ 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}, 5, {{"Ch.37 ", 37}, {"Ch.38", 38}, - {"Ch.39", 39}}}; + {"Ch.39", 39}, + {"Auto", 40}}}; 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()}}; @@ -284,6 +290,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/application/string_format.cpp b/firmware/application/string_format.cpp index d64c6fa1..ca2d73fb 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 ad0c105f..5f1b2bdc 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 b5c68608..60648296 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(); }}, diff --git a/firmware/baseband/proc_ble_tx.cpp b/firmware/baseband/proc_ble_tx.cpp index 71c77d6e..57d6af9e 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) {