Skip to content

Commit

Permalink
Merge pull request #1033 from deXol/developBleSetCurrentCategory
Browse files Browse the repository at this point in the history
[BLE] Add Current Category setting
  • Loading branch information
limpkin authored May 2, 2022
2 parents 43ae939 + 20eb9bc commit 1196ffc
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/CredentialsManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,7 @@ void CredentialsManagement::on_pushButtonSaveCategories_clicked()
wsClient->sendSetUserCategories(cat1, cat2, cat3, cat4);
ui->pushButtonSaveCategories->hide();
m_pCredModel->updateCategories(cat1, cat2, cat3, cat4);
emit wsClient->updateCurrentCategories(cat1, cat2, cat3, cat4);
ui->credDisplayCategoryInput->setItemText(1, cat1);
ui->credDisplayCategoryInput->setItemText(2, cat2);
ui->credDisplayCategoryInput->setItemText(3, cat3);
Expand Down
11 changes: 11 additions & 0 deletions src/MPDeviceBleImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ void MPDeviceBleImpl::getPlatInfo()
mpDev->enqueueAndRunJob(jobs);
}

void MPDeviceBleImpl::enforceCategory(int category)
{
auto *jobs = new AsyncJobs("Set current category", mpDev);

QByteArray catArr;
catArr.append(static_cast<char>(category));
jobs->append(new MPCommandJob(mpDev, MPCmd::SET_CUR_CATEGORY, catArr, bleProt->getDefaultSizeCheckFuncDone()));

mpDev->enqueueAndRunJob(jobs);
}

void MPDeviceBleImpl::getDebugPlatInfo(const MessageHandlerCbData &cb)
{
auto *jobs = new AsyncJobs("Get Debug PlatInfo", mpDev);
Expand Down
1 change: 1 addition & 0 deletions src/MPDeviceBleImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class MPDeviceBleImpl: public QObject
bool isLastPacket(const QByteArray &data);

void getPlatInfo();
void enforceCategory(int category);
void getDebugPlatInfo(const MessageHandlerCbData &cb);
QVector<int> calcDebugPlatInfo(const QByteArray &platInfo);

Expand Down
50 changes: 50 additions & 0 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ MainWindow::MainWindow(WSClient *client, DbMasterController *mc, QWidget *parent
m_notesFetched = true;
wsClient->sendFetchNotes();
}
auto currentCategoryIndex = ui->comboBoxBleCurrentCategory->currentIndex();
if (currentCategoryIndex != 0)
{
// When enforce current category is set sending category number to device
wsClient->sendCurrentCategory(currentCategoryIndex);
}
}
else
{
Expand Down Expand Up @@ -408,6 +414,10 @@ MainWindow::MainWindow(WSClient *client, DbMasterController *mc, QWidget *parent
}
ui->comboBoxSystrayIcon->blockSignals(false);

fillInitialCurrentCategories();
connect(wsClient, &WSClient::displayUserCategories, this, &MainWindow::setCurrentCategoryOptions);
connect(wsClient, &WSClient::updateCurrentCategories, this, &MainWindow::setCurrentCategoryOptions);

ui->cbLoginPrompt->setDisabled(true);
ui->cbPinForMMM->setDisabled(true);
ui->cbStoragePrompt->setDisabled(true);
Expand Down Expand Up @@ -1881,6 +1891,20 @@ void MainWindow::fillBLEBrightnessComboBox(QComboBox *cb)
cb->addItem("100%", 144);
}

void MainWindow::fillInitialCurrentCategories()
{
QSettings s;
ui->comboBoxBleCurrentCategory->blockSignals(true);
ui->comboBoxBleCurrentCategory->clear();
ui->comboBoxBleCurrentCategory->addItem(tr("Disabled"), 0);
ui->comboBoxBleCurrentCategory->addItem("1", 1);
ui->comboBoxBleCurrentCategory->addItem("2", 2);
ui->comboBoxBleCurrentCategory->addItem("3", 3);
ui->comboBoxBleCurrentCategory->addItem("4", 4);
ui->comboBoxBleCurrentCategory->setCurrentIndex(s.value("settings/enforced_category", 0).toInt());
ui->comboBoxBleCurrentCategory->blockSignals(false);
}

void MainWindow::on_toolButton_clearBackupFilePath_released()
{
ui->lineEdit_dbBackupFilePath->clear();
Expand Down Expand Up @@ -2145,6 +2169,7 @@ void MainWindow::onDeviceDisconnected()
noPasswordPromptChanged(false);
ui->pushButtonSettingsSetToDefault->setVisible(false);
m_notesFetched = false;
fillInitialCurrentCategories();
}
ui->groupBox_UserSettings->hide();
wsClient->set_cardId("");
Expand Down Expand Up @@ -2324,3 +2349,28 @@ void MainWindow::sendRequestNotes(int bundle)
wsClient->sendFetchNotes();
}
}

void MainWindow::on_comboBoxBleCurrentCategory_currentIndexChanged(int index)
{
QSettings s;
s.setValue("settings/enforced_category", index);
}

void MainWindow::setCurrentCategoryOptions(const QString &cat1, const QString &cat2, const QString &cat3, const QString &cat4)
{
if (cat1.isEmpty() && cat2.isEmpty() && cat3.isEmpty() && cat4.isEmpty())
{
// When BLE device is connected, but not unlocked daemon is sending categories with empty string
return;
}
QSettings s;
ui->comboBoxBleCurrentCategory->blockSignals(true);
ui->comboBoxBleCurrentCategory->clear();
ui->comboBoxBleCurrentCategory->addItem(tr("Disabled"), 0);
ui->comboBoxBleCurrentCategory->addItem(cat1, 1);
ui->comboBoxBleCurrentCategory->addItem(cat2, 2);
ui->comboBoxBleCurrentCategory->addItem(cat3, 3);
ui->comboBoxBleCurrentCategory->addItem(cat4, 4);
ui->comboBoxBleCurrentCategory->setCurrentIndex(s.value("settings/enforced_category", 0).toInt());
ui->comboBoxBleCurrentCategory->blockSignals(false);
}
6 changes: 6 additions & 0 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ private slots:

void sendRequestNotes(int bundle);

void on_comboBoxBleCurrentCategory_currentIndexChanged(int index);

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

protected:
virtual void keyPressEvent(QKeyEvent *event) override;
virtual void keyReleaseEvent(QKeyEvent *event) override;
Expand Down Expand Up @@ -240,6 +244,8 @@ private slots:

void fillBLEBrightnessComboBox(QComboBox * cb);

void fillInitialCurrentCategories();

Ui::MainWindow *ui = nullptr;
QtAwesome* awesome;

Expand Down
40 changes: 40 additions & 0 deletions src/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -4074,6 +4074,46 @@ Hint: keep your mouse positioned over an option to get more details.</string>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_70">
<item>
<widget class="QLabel" name="label_bleCurrentCategory">
<property name="toolTip">
<string>when selected, you can set moolticute to set a given category upon device connection</string>
</property>
<property name="text">
<string>Upon connection, force category:</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_60">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QComboBox" name="comboBoxBleCurrentCategory">
<property name="minimumSize">
<size>
<width>80</width>
<height>0</height>
</size>
</property>
<property name="toolTip">
<string>when selected, you can set moolticute to set a given category upon device connection</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_43">
<item>
Expand Down
1 change: 1 addition & 0 deletions src/MessageProtocol/MessageProtocolBLE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ void MessageProtocolBLE::fillCommandMapping()
{MPCmd::DELETE_DATA_FILE , 0x003B},
{MPCmd::DELETE_NOTE_FILE , 0x003C},
{MPCmd::GET_TOTP_CODE , 0x0041},
{MPCmd::SET_CUR_CATEGORY , 0x003E},
{MPCmd::CMD_DBG_OPEN_DISP_BUFFER , 0x8001},
{MPCmd::CMD_DBG_SEND_TO_DISP_BUFFER , 0x8002},
{MPCmd::CMD_DBG_CLOSE_DISP_BUFFER , 0x8003},
Expand Down
1 change: 1 addition & 0 deletions src/MooltipassCmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class MPCmd: public QObject
DELETE_DATA_FILE ,
DELETE_NOTE_FILE ,
GET_TOTP_CODE ,
SET_CUR_CATEGORY ,
CMD_DBG_MESSAGE ,
CMD_DBG_OPEN_DISP_BUFFER ,
CMD_DBG_SEND_TO_DISP_BUFFER ,
Expand Down
8 changes: 8 additions & 0 deletions src/WSClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,14 @@ void WSClient::sendBatteryRequest()
sendJsonData({{ "msg", "get_battery" }});
}

void WSClient::sendCurrentCategory(int category)
{
QJsonObject o;
o["category"] = category;
sendJsonData({{ "msg", "enforce_current_category" },
{"data", o}});
}

void WSClient::sendNiMHReconditioning()
{
sendJsonData({{ "msg", "nimh_reconditioning" }});
Expand Down
2 changes: 2 additions & 0 deletions src/WSClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class WSClient: public QObject
void sendUserSettingsRequest();
void sendLoadParams();
void sendBatteryRequest();
void sendCurrentCategory(int category);
void sendNiMHReconditioning();
void sendSecurityChallenge(QString str);

Expand Down Expand Up @@ -156,6 +157,7 @@ class WSClient: public QObject
void displayUploadBundleResult(bool success);
void displayAvailableUsers(const QString& num);
void displayUserCategories(const QString& cat1, const QString& cat2, const QString& cat3, const QString& cat4);
void updateCurrentCategories(const QString& cat1, const QString& cat2, const QString& cat3, const QString& cat4);
void updateUserSettingsOnUI(const QJsonObject& userSettings);
void deviceConnected();
void deviceDisconnected();
Expand Down
11 changes: 11 additions & 0 deletions src/WSServerCon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,17 @@ void WSServerCon::processMessageBLE(QJsonObject root, const MPDeviceProgressCb &
sendJsonMessage(oroot);
});
}
else if (root["msg"] == "enforce_current_category")
{
static constexpr int ENFORCE_CATEGORY_SUPPORT_FW = 9;
if (bleImpl->get_bundleVersion() < ENFORCE_CATEGORY_SUPPORT_FW)
{
qWarning() << "Enforce category is not supported in the current bundle version";
return;
}
QJsonObject o = root["data"].toObject();
bleImpl->enforceCategory(o["category"].toInt());
}
else
{
qDebug() << root["msg"] << " message have not implemented yet for BLE";
Expand Down

0 comments on commit 1196ffc

Please sign in to comment.