Skip to content

Commit

Permalink
Merge pull request #1043 from deXol/developBLEAddSetBleNameCommand
Browse files Browse the repository at this point in the history
[BLE] Add Device Bluetooth Name device setting support
  • Loading branch information
limpkin authored May 22, 2022
2 parents 2283133 + 6d0fac3 commit 6d4dce7
Show file tree
Hide file tree
Showing 15 changed files with 253 additions and 3 deletions.
56 changes: 56 additions & 0 deletions src/MPDeviceBleImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1859,9 +1859,65 @@ bool MPDeviceBleImpl::resetDefaultSettings()
}

bleSettings->resetDefaultSettings();
if (get_bundleVersion() >= SET_BLE_NAME_BUNDLE_VERSION)
{
setBleName(DEFAULT_BLE_NAME, [this](bool success)
{
if (success)
{
emit changeBleName(DEFAULT_BLE_NAME);
}
else
{
qCritical() << "Setting default Device Bluetooth Name failed";
}
});
}
return true;
}

void MPDeviceBleImpl::setBleName(QString name, std::function<void (bool)> cb)
{
QByteArray nameArray = name.toUtf8();
nameArray.append(ZERO_BYTE);
auto *jobs = new AsyncJobs(QString("Set BLE name"), this);
jobs->append(new MPCommandJob(mpDev, MPCmd::SET_BLE_NAME,
nameArray,
[this, name, cb](const QByteArray &data, bool &) -> bool
{
if (bleProt->getFirstPayloadByte(data) != MSG_SUCCESS)
{
qWarning() << "Set ble name to: " << name << " failed";
cb(false);
return false;
}
cb(true);
return true;
}));
mpDev->enqueueAndRunJob(jobs);
}

void MPDeviceBleImpl::getBleName(const MessageHandlerCbData &cb)
{
auto *jobs = new AsyncJobs("Get Ble Name", mpDev);

jobs->append(new MPCommandJob(mpDev, MPCmd::GET_BLE_NAME, bleProt->getDefaultSizeCheckFuncDone()));

connect(jobs, &AsyncJobs::finished, [this, cb](const QByteArray &data)
{
Q_UNUSED(data);
/* Callback */
cb(true, "", bleProt->getFullPayload(data));
});

mpDev->enqueueAndRunJob(jobs);
}

QString MPDeviceBleImpl::getBleNameFromArray(const QByteArray &arr) const
{
return bleProt->toQString(arr);
}

Common::SubdomainSelection MPDeviceBleImpl::getForceSubdomainSelection() const
{
QSettings s;
Expand Down
7 changes: 7 additions & 0 deletions src/MPDeviceBleImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ class MPDeviceBleImpl: public QObject

bool resetDefaultSettings();

void setBleName(QString name, std::function<void(bool)> cb);
void getBleName(const MessageHandlerCbData &cb);
QString getBleNameFromArray(const QByteArray& arr) const;

Common::SubdomainSelection getForceSubdomainSelection() const;

signals:
Expand All @@ -181,6 +185,7 @@ class MPDeviceBleImpl: public QObject
void userCategoriesFetched(QJsonObject categories);
void notesFetched();
void nimhReconditionFinished(bool success, QString response);
void changeBleName(const QString& name);

private slots:
void handleLongMessageTimeout();
Expand Down Expand Up @@ -271,7 +276,9 @@ public slots:
static constexpr int MINI_FILE_FULL_SIZE_LENGTH = 4;
static constexpr int MINI_FILE_BLOCK_SIZE = 128;
static constexpr int FORCE_SUBDOMAIN_BUNDLE_VERSION = 8;
static constexpr int SET_BLE_NAME_BUNDLE_VERSION = 9;
const QByteArray DEFAULT_BUNDLE_PASSWORD = "\x63\x44\x31\x91\x3a\xfd\x23\xff\xb3\xac\x93\x69\x22\x5b\xf3\xc0";
const QString DEFAULT_BLE_NAME = "Mooltipass Mini BLE";
};

#endif // MPDEVICEBLEIMPL_H
24 changes: 23 additions & 1 deletion src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ MainWindow::MainWindow(WSClient *client, DbMasterController *mc, QWidget *parent

ui->comboBoxDelayBefUnlockLogin->addItem("100", 10);
ui->comboBoxDelayBefUnlockLogin->addItem("250", 25);
ui->comboBoxDelayBefUnlockLogin->addItem("500", 50);
ui->comboBoxDelayBefUnlockLogin->addItem("600", 60);
ui->comboBoxDelayBefUnlockLogin->addItem("1000", 100);
ui->comboBoxDelayBefUnlockLogin->addItem("1500", 150);
ui->comboBoxDelayBefUnlockLogin->addItem("2000", 200);
Expand Down Expand Up @@ -625,6 +625,8 @@ MainWindow::MainWindow(WSClient *client, DbMasterController *mc, QWidget *parent
m_keyboardUsbLayoutActualValue = m_keyboardUsbLayoutOrigValue;
ui->checkBoxEnforceUSBLayout->setChecked(m_keyboardUsbLayoutOrigValue);

connect(wsClient, &WSClient::bleNameChanged, this, &MainWindow::onBleNameChanged);

wsClient->settingsHelper()->setMainWindow(this);
#ifdef Q_OS_WIN
const auto keyboardLayoutWidth = 150;
Expand Down Expand Up @@ -820,6 +822,12 @@ void MainWindow::updateBackupControlsVisibility(bool visible)
ui->toolButton_setBackupFilePath->setVisible(visible);
}

void MainWindow::displayBLENameChangedDialog()
{
QMessageBox::information(this, tr("Device Bluetooth Name Changed"),
tr("Please disable and re-enable bluetooth for your changes to take effect"));
}

void MainWindow::updatePage()
{
const auto status = wsClient->get_status();
Expand Down Expand Up @@ -2149,6 +2157,7 @@ void MainWindow::onDeviceConnected()
}
wsClient->sendUserSettingsRequest();
wsClient->sendBatteryRequest();
wsClient->sendBleNameRequest();
}
displayBundleVersion();
updateDeviceDependentUI();
Expand Down Expand Up @@ -2374,3 +2383,16 @@ void MainWindow::setCurrentCategoryOptions(const QString &cat1, const QString &c
ui->comboBoxBleCurrentCategory->setCurrentIndex(s.value("settings/enforced_category", 0).toInt());
ui->comboBoxBleCurrentCategory->blockSignals(false);
}

void MainWindow::on_lineEditBleName_textEdited(const QString &arg1)
{
m_bleNameActual = arg1;
}

void MainWindow::onBleNameChanged(const QString &name)
{
m_bleNameActual = name;
m_bleNameOriginal = name;
ui->lineEditBleName->setText(name);
checkSettingsChanged();
}
10 changes: 10 additions & 0 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ class MainWindow : public QMainWindow
bool getActualUsbKeyboardLayout() const { return m_keyboardUsbLayoutActualValue; }
void setOriginalBTKeyboardLayout(bool val) { m_keyboardBTLayoutOrigValue = val; }
void setOriginalUsbKeyboardLayout(bool val) { m_keyboardUsbLayoutOrigValue = val; }
QString getOriginalBleName() const { return m_bleNameOriginal; }
void setOriginalBleName(const QString& name) { m_bleNameOriginal = name; }

void updateBackupControlsVisibility(bool visible);

void displayBLENameChangedDialog();


const static QString NONE_STRING;
const static QString TAB_STRING;
Expand Down Expand Up @@ -203,6 +207,10 @@ private slots:

void setCurrentCategoryOptions(const QString& cat1, const QString& cat2, const QString& cat3, const QString& cat4);

void on_lineEditBleName_textEdited(const QString &arg1);

void onBleNameChanged(const QString& name);

protected:
virtual void keyPressEvent(QKeyEvent *event) override;
virtual void keyReleaseEvent(QKeyEvent *event) override;
Expand Down Expand Up @@ -281,6 +289,8 @@ private slots:
bool m_keyboardBTLayoutOrigValue = false;
bool m_keyboardUsbLayoutActualValue = false;
bool m_keyboardBTLayoutActualValue = false;
QString m_bleNameOriginal = "";
QString m_bleNameActual = "";

bool m_notesFetched = false;

Expand Down
51 changes: 51 additions & 0 deletions src/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,57 @@ Hint: keep your mouse positioned over an option to get more details.</string>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="settings_ble_name" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_71">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_BleName">
<property name="text">
<string>Device Bluetooth Name</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditBleName">
<property name="maximumSize">
<size>
<width>180</width>
<height>16777215</height>
</size>
</property>
<property name="maxLength">
<number>22</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_61">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QCheckBox" name="hashDisplayFeatureCheckBox">
<property name="toolTip">
Expand Down
3 changes: 3 additions & 0 deletions src/MessageProtocol/MessageProtocolBLE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ void MessageProtocolBLE::fillCommandMapping()
{MPCmd::DELETE_NOTE_FILE , 0x003C},
{MPCmd::GET_TOTP_CODE , 0x0041},
{MPCmd::SET_CUR_CATEGORY , 0x003E},
{MPCmd::WAKE_UP_DEVICE , 0x003F},
{MPCmd::SET_BLE_NAME , 0x0040},
{MPCmd::GET_BLE_NAME , 0x0042},
{MPCmd::CMD_DBG_OPEN_DISP_BUFFER , 0x8001},
{MPCmd::CMD_DBG_SEND_TO_DISP_BUFFER , 0x8002},
{MPCmd::CMD_DBG_CLOSE_DISP_BUFFER , 0x8003},
Expand Down
3 changes: 3 additions & 0 deletions src/MooltipassCmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ class MPCmd: public QObject
DELETE_NOTE_FILE ,
GET_TOTP_CODE ,
SET_CUR_CATEGORY ,
WAKE_UP_DEVICE ,
SET_BLE_NAME ,
GET_BLE_NAME ,
CMD_DBG_MESSAGE ,
CMD_DBG_OPEN_DISP_BUFFER ,
CMD_DBG_SEND_TO_DISP_BUFFER ,
Expand Down
2 changes: 2 additions & 0 deletions src/Settings/SettingsGuiBLE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void SettingsGuiBLE::checkDeviceSettingsForBundle9(int bundleVersion)
ui->settings_screen_brightness_bat->show();
ui->settings_screen_brightness_usb->show();
ui->settings_subdomain_specification->show();
ui->settings_ble_name->show();
}
else
{
Expand All @@ -120,6 +121,7 @@ void SettingsGuiBLE::checkDeviceSettingsForBundle9(int bundleVersion)
ui->settings_screen_brightness_bat->hide();
ui->settings_screen_brightness_usb->hide();
ui->settings_subdomain_specification->hide();
ui->settings_ble_name->hide();
}
}

Expand Down
30 changes: 29 additions & 1 deletion src/Settings/SettingsGuiHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void SettingsGuiHelper::setMainWindow(MainWindow *mw)
}
connect(widget, signal.c_str(), m_mw, SLOT(checkSettingsChanged()));
}
connect(ui->lineEditBleName, SIGNAL(textEdited(const QString&)), m_mw, SLOT(checkSettingsChanged()));
}

void SettingsGuiHelper::createSettingUIMapping()
Expand Down Expand Up @@ -129,7 +130,7 @@ bool SettingsGuiHelper::checkSettingsChanged()
auto* metaObj = m_settings->getMetaObject();
if (m_wsClient->isMPBLE())
{
if (checkEnforceLayoutChanged())
if (checkEnforceLayoutChanged() || checkBleNameChanged())
{
return true;
}
Expand Down Expand Up @@ -178,6 +179,7 @@ void SettingsGuiHelper::resetSettings()
if (m_wsClient->isMPBLE())
{
resetEnforceLayout();
resetBleName();
}
auto* metaObj = m_settings->getMetaObject();
while (nullptr != metaObj && QString{metaObj->className()} != "QObject")
Expand Down Expand Up @@ -210,6 +212,7 @@ void SettingsGuiHelper::getChangedSettings(QJsonObject &o)
if (m_wsClient->isMPBLE())
{
saveEnforceLayout();
saveBleName();
}
while (nullptr != metaObj && QString{metaObj->className()} != "QObject")
{
Expand Down Expand Up @@ -313,6 +316,31 @@ bool SettingsGuiHelper::checkEnforceLayoutChanged()
return btLayoutEnforceChanged || usbLayoutEnforceChanged;
}

bool SettingsGuiHelper::checkBleNameChanged()
{
return m_mw->ui->lineEditBleName->text() != m_mw->getOriginalBleName();
}

void SettingsGuiHelper::resetBleName()
{
QString bleName = m_mw->getOriginalBleName();
if (bleName != m_mw->ui->lineEditBleName->text())
{
m_mw->ui->lineEditBleName->setText(bleName);
}
}

void SettingsGuiHelper::saveBleName()
{
QString bleName = m_mw->ui->lineEditBleName->text();
if (bleName != m_mw->getOriginalBleName())
{
m_wsClient->sendSetBleName(bleName);
m_mw->setOriginalBleName(bleName);
m_mw->displayBLENameChangedDialog();
}
}

void SettingsGuiHelper::resetEnforceLayout()
{
QSettings s;
Expand Down
3 changes: 3 additions & 0 deletions src/Settings/SettingsGuiHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ private slots:
bool checkEnforceLayoutChanged();
void resetEnforceLayout();
void saveEnforceLayout();
bool checkBleNameChanged();
void resetBleName();
void saveBleName();

WSClient* m_wsClient = nullptr;
DeviceSettings* m_settings = nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/Settings/SettingsGuiMini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void SettingsGuiMini::updateUI()
ui->settings_screen_brightness_bat->hide();
ui->settings_screen_brightness_usb->hide();
ui->settings_subdomain_specification->hide();
ui->settings_ble_name->hide();

ui->groupBox_BLESettings->hide();

Expand Down
Loading

0 comments on commit 6d4dce7

Please sign in to comment.