Skip to content

Commit

Permalink
Allow overriding advertisement for debugging purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Jun 9, 2022
1 parent ac62477 commit e38787c
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ set(
"Source/Gui/SettingsWindow.cpp"
"Source/Gui/Widget/Battery.cpp"

"Source/Core/Debug.cpp"
"Source/Core/Update.cpp"
"Source/Core/AirPods.cpp"
"Source/Core/AppleCP.cpp"
Expand Down
11 changes: 11 additions & 0 deletions Source/Core/Bluetooth_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Bluetooth_win.h"

#include "../Logger.h"
#include "Debug.h"
#include "OS/Windows.h"

namespace Core::Bluetooth {
Expand All @@ -30,6 +31,8 @@ using namespace WinrtBluetooth;
using namespace WinrtBluetoothAdv;
using namespace WinrtDevicesEnumeration;

using namespace Core::Debug;

//////////////////////////////////////////////////
// Device
//
Expand Down Expand Up @@ -340,6 +343,14 @@ void AdvertisementWatcher::OnReceived(const BluetoothLEAdvertisementReceivedEven

std::vector<uint8_t> stdData(data.data(), data.data() + data.Length());

#if defined APD_DEBUG
auto overrideAdv = DebugConfig::GetInstance().GetOverrideAdv();
if (overrideAdv.has_value()) {
stdData = std::move(overrideAdv.value());
LOG(Trace, "Adv override: {}", Helper::ToString(stdData));
}
#endif

receivedData.manufacturerDataMap.try_emplace(companyId, std::move(stdData));
}

Expand Down
59 changes: 59 additions & 0 deletions Source/Core/Debug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// AirPodsDesktop - AirPods Desktop User Experience Enhancement Program.
// Copyright (C) 2021-2022 SpriteOvO
//
// 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 3 of the License, 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. If not, see <https://www.gnu.org/licenses/>.
//

#include "Debug.h"

#include <random>

namespace Core::Debug {

DebugConfig &DebugConfig::GetInstance()
{
static DebugConfig i;
return i;
}

void DebugConfig::UpdateAdvOverride(bool enabled, std::vector<std::vector<uint8_t>> advs)
{
std::lock_guard<std::mutex> lock{_mutex};

_fields.enabled = enabled;
_fields.advs = std::move(advs);
}

std::optional<std::vector<uint8_t>> DebugConfig::GetOverrideAdv() const
{
std::optional<std::vector<uint8_t>> result;

std::lock_guard<std::mutex> lock{_mutex};

do {
if (!_fields.enabled || _fields.advs.empty()) {
break;
}

std::default_random_engine re{std::random_device{}()};
std::uniform_int_distribution<size_t> dist{0, _fields.advs.size() - 1};

result = _fields.advs.at(dist(re));
} while (false);

return result;
}

} // namespace Core::Debug
51 changes: 51 additions & 0 deletions Source/Core/Debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// AirPodsDesktop - AirPods Desktop User Experience Enhancement Program.
// Copyright (C) 2021-2022 SpriteOvO
//
// 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 3 of the License, 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. If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <mutex>
#include <vector>
#include <optional>

namespace Core::Debug {

struct AdvOverrideFields {
bool enabled{false};
std::vector<std::vector<uint8_t>> advs;
};

struct DebugConfigFields {
AdvOverrideFields advOverride;
};

class DebugConfig
{
public:
static DebugConfig &GetInstance();

DebugConfig() = default;

void UpdateAdvOverride(bool enabled, std::vector<std::vector<uint8_t>> advs);
std::optional<std::vector<uint8_t>> GetOverrideAdv() const;

private:
mutable std::mutex _mutex;
AdvOverrideFields _fields;
};

} // namespace Core::Debug
50 changes: 50 additions & 0 deletions Source/Gui/SettingsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <Config.h>

#include "../Application.h"
#include "../Core/Debug.h"

using namespace std::chrono_literals;

Expand Down Expand Up @@ -68,6 +69,19 @@ SettingsWindow::SettingsWindow(QWidget *parent) : QDialog{parent}

_ui.setupUi(this);

auto debugTabIndex = _ui.tabWidget->count() - 1;
APD_ASSERT(_ui.tabWidget->tabText(debugTabIndex) == "Debug");
#if !defined APD_DEBUG
_ui.tabWidget->setTabVisible(debugTabIndex, false);
#else
connect(
_ui.cbAdvOverride, &QCheckBox::toggled, this, &SettingsWindow::On_cbAdvOverride_toggled);

connect(
_ui.teAdvOverride, &QTextEdit::textChanged, this,
&SettingsWindow::On_teAdvOverride_textChanged);
#endif

InitCreditsText();

auto versionText =
Expand Down Expand Up @@ -300,6 +314,32 @@ void SettingsWindow::Update(const Fields &fields, bool trigger)
_trigger = true;
}

void SettingsWindow::UpdateAdvOverride()
{
auto advsStr = _ui.teAdvOverride->toPlainText();
auto vAdvsStr = advsStr.split('\n', QString::SkipEmptyParts);

std::vector<std::vector<uint8_t>> advs;

for (const auto &advStr : vAdvsStr) {

auto advBytesStr = advStr.split(' ', QString::SkipEmptyParts);

std::vector<uint8_t> bytes;
for (const auto advByteStr : advBytesStr) {
bool success{false};
auto byte = advByteStr.toUInt(&success, 16);
APD_ASSERT(success);
bytes.emplace_back(byte);
}

advs.emplace_back(std::move(bytes));
}

Core::Debug::DebugConfig::GetInstance().UpdateAdvOverride(
_ui.cbAdvOverride->isChecked(), std::move(advs));
}

void SettingsWindow::showEvent(QShowEvent *event)
{
Update(GetCurrent(), false);
Expand Down Expand Up @@ -366,6 +406,16 @@ void SettingsWindow::On_pbOpenLogsDirectory_clicked()
Utils::File::OpenFileLocation(Logger::GetLogFilePath());
}

void SettingsWindow::On_cbAdvOverride_toggled(bool checked)
{
UpdateAdvOverride();
}

void SettingsWindow::On_teAdvOverride_textChanged()
{
UpdateAdvOverride();
}

} // namespace Gui

#include "SettingsWindow.moc"
5 changes: 5 additions & 0 deletions Source/Gui/SettingsWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class SettingsWindow : public QDialog
void InitCreditsText();
void RestoreDefaults();
void Update(const Fields &fields, bool trigger);
void UpdateAdvOverride();

void showEvent(QShowEvent *event) override;

Expand All @@ -71,6 +72,10 @@ class SettingsWindow : public QDialog
// About
void On_pbOpenLogsDirectory_clicked();

// Debug
void On_cbAdvOverride_toggled(bool checked);
void On_teAdvOverride_textChanged();

UTILS_QT_DISABLE_ESC_QUIT(QDialog);
UTILS_QT_REGISTER_LANGUAGECHANGE(QDialog, [this] {
_ui.retranslateUi(this);
Expand Down
26 changes: 26 additions & 0 deletions Source/Gui/SettingsWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,32 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="debug">
<attribute name="title">
<string notr="true">Debug</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_6">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string notr="true">Bluetooth Advertisement Override</string>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="1" column="0">
<widget class="QTextEdit" name="teAdvOverride"/>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="cbAdvOverride">
<property name="text">
<string notr="true">Enabled</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item>
Expand Down

0 comments on commit e38787c

Please sign in to comment.