Skip to content

Commit 6d4dce7

Browse files
authored
Merge pull request #1043 from deXol/developBLEAddSetBleNameCommand
[BLE] Add Device Bluetooth Name device setting support
2 parents 2283133 + 6d0fac3 commit 6d4dce7

15 files changed

Lines changed: 253 additions & 3 deletions

src/MPDeviceBleImpl.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,9 +1859,65 @@ bool MPDeviceBleImpl::resetDefaultSettings()
18591859
}
18601860

18611861
bleSettings->resetDefaultSettings();
1862+
if (get_bundleVersion() >= SET_BLE_NAME_BUNDLE_VERSION)
1863+
{
1864+
setBleName(DEFAULT_BLE_NAME, [this](bool success)
1865+
{
1866+
if (success)
1867+
{
1868+
emit changeBleName(DEFAULT_BLE_NAME);
1869+
}
1870+
else
1871+
{
1872+
qCritical() << "Setting default Device Bluetooth Name failed";
1873+
}
1874+
});
1875+
}
18621876
return true;
18631877
}
18641878

1879+
void MPDeviceBleImpl::setBleName(QString name, std::function<void (bool)> cb)
1880+
{
1881+
QByteArray nameArray = name.toUtf8();
1882+
nameArray.append(ZERO_BYTE);
1883+
auto *jobs = new AsyncJobs(QString("Set BLE name"), this);
1884+
jobs->append(new MPCommandJob(mpDev, MPCmd::SET_BLE_NAME,
1885+
nameArray,
1886+
[this, name, cb](const QByteArray &data, bool &) -> bool
1887+
{
1888+
if (bleProt->getFirstPayloadByte(data) != MSG_SUCCESS)
1889+
{
1890+
qWarning() << "Set ble name to: " << name << " failed";
1891+
cb(false);
1892+
return false;
1893+
}
1894+
cb(true);
1895+
return true;
1896+
}));
1897+
mpDev->enqueueAndRunJob(jobs);
1898+
}
1899+
1900+
void MPDeviceBleImpl::getBleName(const MessageHandlerCbData &cb)
1901+
{
1902+
auto *jobs = new AsyncJobs("Get Ble Name", mpDev);
1903+
1904+
jobs->append(new MPCommandJob(mpDev, MPCmd::GET_BLE_NAME, bleProt->getDefaultSizeCheckFuncDone()));
1905+
1906+
connect(jobs, &AsyncJobs::finished, [this, cb](const QByteArray &data)
1907+
{
1908+
Q_UNUSED(data);
1909+
/* Callback */
1910+
cb(true, "", bleProt->getFullPayload(data));
1911+
});
1912+
1913+
mpDev->enqueueAndRunJob(jobs);
1914+
}
1915+
1916+
QString MPDeviceBleImpl::getBleNameFromArray(const QByteArray &arr) const
1917+
{
1918+
return bleProt->toQString(arr);
1919+
}
1920+
18651921
Common::SubdomainSelection MPDeviceBleImpl::getForceSubdomainSelection() const
18661922
{
18671923
QSettings s;

src/MPDeviceBleImpl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ class MPDeviceBleImpl: public QObject
171171

172172
bool resetDefaultSettings();
173173

174+
void setBleName(QString name, std::function<void(bool)> cb);
175+
void getBleName(const MessageHandlerCbData &cb);
176+
QString getBleNameFromArray(const QByteArray& arr) const;
177+
174178
Common::SubdomainSelection getForceSubdomainSelection() const;
175179

176180
signals:
@@ -181,6 +185,7 @@ class MPDeviceBleImpl: public QObject
181185
void userCategoriesFetched(QJsonObject categories);
182186
void notesFetched();
183187
void nimhReconditionFinished(bool success, QString response);
188+
void changeBleName(const QString& name);
184189

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

277284
#endif // MPDEVICEBLEIMPL_H

src/MainWindow.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ MainWindow::MainWindow(WSClient *client, DbMasterController *mc, QWidget *parent
387387

388388
ui->comboBoxDelayBefUnlockLogin->addItem("100", 10);
389389
ui->comboBoxDelayBefUnlockLogin->addItem("250", 25);
390-
ui->comboBoxDelayBefUnlockLogin->addItem("500", 50);
390+
ui->comboBoxDelayBefUnlockLogin->addItem("600", 60);
391391
ui->comboBoxDelayBefUnlockLogin->addItem("1000", 100);
392392
ui->comboBoxDelayBefUnlockLogin->addItem("1500", 150);
393393
ui->comboBoxDelayBefUnlockLogin->addItem("2000", 200);
@@ -625,6 +625,8 @@ MainWindow::MainWindow(WSClient *client, DbMasterController *mc, QWidget *parent
625625
m_keyboardUsbLayoutActualValue = m_keyboardUsbLayoutOrigValue;
626626
ui->checkBoxEnforceUSBLayout->setChecked(m_keyboardUsbLayoutOrigValue);
627627

628+
connect(wsClient, &WSClient::bleNameChanged, this, &MainWindow::onBleNameChanged);
629+
628630
wsClient->settingsHelper()->setMainWindow(this);
629631
#ifdef Q_OS_WIN
630632
const auto keyboardLayoutWidth = 150;
@@ -820,6 +822,12 @@ void MainWindow::updateBackupControlsVisibility(bool visible)
820822
ui->toolButton_setBackupFilePath->setVisible(visible);
821823
}
822824

825+
void MainWindow::displayBLENameChangedDialog()
826+
{
827+
QMessageBox::information(this, tr("Device Bluetooth Name Changed"),
828+
tr("Please disable and re-enable bluetooth for your changes to take effect"));
829+
}
830+
823831
void MainWindow::updatePage()
824832
{
825833
const auto status = wsClient->get_status();
@@ -2149,6 +2157,7 @@ void MainWindow::onDeviceConnected()
21492157
}
21502158
wsClient->sendUserSettingsRequest();
21512159
wsClient->sendBatteryRequest();
2160+
wsClient->sendBleNameRequest();
21522161
}
21532162
displayBundleVersion();
21542163
updateDeviceDependentUI();
@@ -2374,3 +2383,16 @@ void MainWindow::setCurrentCategoryOptions(const QString &cat1, const QString &c
23742383
ui->comboBoxBleCurrentCategory->setCurrentIndex(s.value("settings/enforced_category", 0).toInt());
23752384
ui->comboBoxBleCurrentCategory->blockSignals(false);
23762385
}
2386+
2387+
void MainWindow::on_lineEditBleName_textEdited(const QString &arg1)
2388+
{
2389+
m_bleNameActual = arg1;
2390+
}
2391+
2392+
void MainWindow::onBleNameChanged(const QString &name)
2393+
{
2394+
m_bleNameActual = name;
2395+
m_bleNameOriginal = name;
2396+
ui->lineEditBleName->setText(name);
2397+
checkSettingsChanged();
2398+
}

src/MainWindow.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ class MainWindow : public QMainWindow
6464
bool getActualUsbKeyboardLayout() const { return m_keyboardUsbLayoutActualValue; }
6565
void setOriginalBTKeyboardLayout(bool val) { m_keyboardBTLayoutOrigValue = val; }
6666
void setOriginalUsbKeyboardLayout(bool val) { m_keyboardUsbLayoutOrigValue = val; }
67+
QString getOriginalBleName() const { return m_bleNameOriginal; }
68+
void setOriginalBleName(const QString& name) { m_bleNameOriginal = name; }
6769

6870
void updateBackupControlsVisibility(bool visible);
6971

72+
void displayBLENameChangedDialog();
73+
7074

7175
const static QString NONE_STRING;
7276
const static QString TAB_STRING;
@@ -203,6 +207,10 @@ private slots:
203207

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

210+
void on_lineEditBleName_textEdited(const QString &arg1);
211+
212+
void onBleNameChanged(const QString& name);
213+
206214
protected:
207215
virtual void keyPressEvent(QKeyEvent *event) override;
208216
virtual void keyReleaseEvent(QKeyEvent *event) override;
@@ -281,6 +289,8 @@ private slots:
281289
bool m_keyboardBTLayoutOrigValue = false;
282290
bool m_keyboardUsbLayoutActualValue = false;
283291
bool m_keyboardBTLayoutActualValue = false;
292+
QString m_bleNameOriginal = "";
293+
QString m_bleNameActual = "";
284294

285295
bool m_notesFetched = false;
286296

src/MainWindow.ui

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,57 @@ Hint: keep your mouse positioned over an option to get more details.</string>
17561756
</layout>
17571757
</widget>
17581758
</item>
1759+
<item>
1760+
<widget class="QWidget" name="settings_ble_name" native="true">
1761+
<layout class="QHBoxLayout" name="horizontalLayout_71">
1762+
<property name="leftMargin">
1763+
<number>0</number>
1764+
</property>
1765+
<property name="topMargin">
1766+
<number>0</number>
1767+
</property>
1768+
<property name="rightMargin">
1769+
<number>0</number>
1770+
</property>
1771+
<property name="bottomMargin">
1772+
<number>0</number>
1773+
</property>
1774+
<item>
1775+
<widget class="QLabel" name="label_BleName">
1776+
<property name="text">
1777+
<string>Device Bluetooth Name</string>
1778+
</property>
1779+
</widget>
1780+
</item>
1781+
<item>
1782+
<widget class="QLineEdit" name="lineEditBleName">
1783+
<property name="maximumSize">
1784+
<size>
1785+
<width>180</width>
1786+
<height>16777215</height>
1787+
</size>
1788+
</property>
1789+
<property name="maxLength">
1790+
<number>22</number>
1791+
</property>
1792+
</widget>
1793+
</item>
1794+
<item>
1795+
<spacer name="horizontalSpacer_61">
1796+
<property name="orientation">
1797+
<enum>Qt::Horizontal</enum>
1798+
</property>
1799+
<property name="sizeHint" stdset="0">
1800+
<size>
1801+
<width>40</width>
1802+
<height>20</height>
1803+
</size>
1804+
</property>
1805+
</spacer>
1806+
</item>
1807+
</layout>
1808+
</widget>
1809+
</item>
17591810
<item>
17601811
<widget class="QCheckBox" name="hashDisplayFeatureCheckBox">
17611812
<property name="toolTip">

src/MessageProtocol/MessageProtocolBLE.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ void MessageProtocolBLE::fillCommandMapping()
374374
{MPCmd::DELETE_NOTE_FILE , 0x003C},
375375
{MPCmd::GET_TOTP_CODE , 0x0041},
376376
{MPCmd::SET_CUR_CATEGORY , 0x003E},
377+
{MPCmd::WAKE_UP_DEVICE , 0x003F},
378+
{MPCmd::SET_BLE_NAME , 0x0040},
379+
{MPCmd::GET_BLE_NAME , 0x0042},
377380
{MPCmd::CMD_DBG_OPEN_DISP_BUFFER , 0x8001},
378381
{MPCmd::CMD_DBG_SEND_TO_DISP_BUFFER , 0x8002},
379382
{MPCmd::CMD_DBG_CLOSE_DISP_BUFFER , 0x8003},

src/MooltipassCmds.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ class MPCmd: public QObject
170170
DELETE_NOTE_FILE ,
171171
GET_TOTP_CODE ,
172172
SET_CUR_CATEGORY ,
173+
WAKE_UP_DEVICE ,
174+
SET_BLE_NAME ,
175+
GET_BLE_NAME ,
173176
CMD_DBG_MESSAGE ,
174177
CMD_DBG_OPEN_DISP_BUFFER ,
175178
CMD_DBG_SEND_TO_DISP_BUFFER ,

src/Settings/SettingsGuiBLE.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ void SettingsGuiBLE::checkDeviceSettingsForBundle9(int bundleVersion)
109109
ui->settings_screen_brightness_bat->show();
110110
ui->settings_screen_brightness_usb->show();
111111
ui->settings_subdomain_specification->show();
112+
ui->settings_ble_name->show();
112113
}
113114
else
114115
{
@@ -120,6 +121,7 @@ void SettingsGuiBLE::checkDeviceSettingsForBundle9(int bundleVersion)
120121
ui->settings_screen_brightness_bat->hide();
121122
ui->settings_screen_brightness_usb->hide();
122123
ui->settings_subdomain_specification->hide();
124+
ui->settings_ble_name->hide();
123125
}
124126
}
125127

src/Settings/SettingsGuiHelper.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void SettingsGuiHelper::setMainWindow(MainWindow *mw)
8585
}
8686
connect(widget, signal.c_str(), m_mw, SLOT(checkSettingsChanged()));
8787
}
88+
connect(ui->lineEditBleName, SIGNAL(textEdited(const QString&)), m_mw, SLOT(checkSettingsChanged()));
8889
}
8990

9091
void SettingsGuiHelper::createSettingUIMapping()
@@ -129,7 +130,7 @@ bool SettingsGuiHelper::checkSettingsChanged()
129130
auto* metaObj = m_settings->getMetaObject();
130131
if (m_wsClient->isMPBLE())
131132
{
132-
if (checkEnforceLayoutChanged())
133+
if (checkEnforceLayoutChanged() || checkBleNameChanged())
133134
{
134135
return true;
135136
}
@@ -178,6 +179,7 @@ void SettingsGuiHelper::resetSettings()
178179
if (m_wsClient->isMPBLE())
179180
{
180181
resetEnforceLayout();
182+
resetBleName();
181183
}
182184
auto* metaObj = m_settings->getMetaObject();
183185
while (nullptr != metaObj && QString{metaObj->className()} != "QObject")
@@ -210,6 +212,7 @@ void SettingsGuiHelper::getChangedSettings(QJsonObject &o)
210212
if (m_wsClient->isMPBLE())
211213
{
212214
saveEnforceLayout();
215+
saveBleName();
213216
}
214217
while (nullptr != metaObj && QString{metaObj->className()} != "QObject")
215218
{
@@ -313,6 +316,31 @@ bool SettingsGuiHelper::checkEnforceLayoutChanged()
313316
return btLayoutEnforceChanged || usbLayoutEnforceChanged;
314317
}
315318

319+
bool SettingsGuiHelper::checkBleNameChanged()
320+
{
321+
return m_mw->ui->lineEditBleName->text() != m_mw->getOriginalBleName();
322+
}
323+
324+
void SettingsGuiHelper::resetBleName()
325+
{
326+
QString bleName = m_mw->getOriginalBleName();
327+
if (bleName != m_mw->ui->lineEditBleName->text())
328+
{
329+
m_mw->ui->lineEditBleName->setText(bleName);
330+
}
331+
}
332+
333+
void SettingsGuiHelper::saveBleName()
334+
{
335+
QString bleName = m_mw->ui->lineEditBleName->text();
336+
if (bleName != m_mw->getOriginalBleName())
337+
{
338+
m_wsClient->sendSetBleName(bleName);
339+
m_mw->setOriginalBleName(bleName);
340+
m_mw->displayBLENameChangedDialog();
341+
}
342+
}
343+
316344
void SettingsGuiHelper::resetEnforceLayout()
317345
{
318346
QSettings s;

src/Settings/SettingsGuiHelper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ private slots:
3636
bool checkEnforceLayoutChanged();
3737
void resetEnforceLayout();
3838
void saveEnforceLayout();
39+
bool checkBleNameChanged();
40+
void resetBleName();
41+
void saveBleName();
3942

4043
WSClient* m_wsClient = nullptr;
4144
DeviceSettings* m_settings = nullptr;

0 commit comments

Comments
 (0)