diff --git a/OmobiDisplayApp/omobidisplaybackend.cpp b/OmobiDisplayApp/omobidisplaybackend.cpp index ff3cc48..48602cd 100644 --- a/OmobiDisplayApp/omobidisplaybackend.cpp +++ b/OmobiDisplayApp/omobidisplaybackend.cpp @@ -24,10 +24,6 @@ OmobiDisplayBackend::OmobiDisplayBackend(QObject *parent) : QObject(parent) this->setState(Idle); this->ble->startScanningForDevices(); - - qDebug() << QCryptographicHash::hash("HalloTest", QCryptographicHash::Sha256).toHex(); - // dd86fcfda3a20cbb8fbb3026a84550e0d70c2c79e7e8e36d6ffa04b9eef0401f - // dd86fcfda3a20cbb8fbb3026a84550e0d70c2c79e7e8e36d6ffa04b9eef0401f } @@ -35,6 +31,15 @@ void OmobiDisplayBackend::startScanning() { this->ble->startScanningForDevices(); } +void OmobiDisplayBackend::authenticate(QString code) { + // tell display to send over existing model data + this->setState(Authenticating); + QString combinedCode = this->ble->getCurrentDevice()->getAddress().toUpper() + code; + QString secret = QCryptographicHash::hash(combinedCode.toUtf8(), QCryptographicHash::Sha256).toHex(); + + this->sendBluetoothCommand(AuthorizeSessionCommand, QVariantMap{{"secret", secret}}); +} + void OmobiDisplayBackend::handleBluetoothStateChange(QBluetoothLeUart::BluetoothLeUartState state){ switch(state){ case QBluetoothLeUart::Idle: { @@ -74,12 +79,8 @@ void OmobiDisplayBackend::handleBluetoothStateChange(QBluetoothLeUart::Bluetooth } void OmobiDisplayBackend::handleBluetoothDeviceConected() { - this->setState(Initing); - - // tell display to send over existing model data - this->sendBluetoothCommand(AuthorizeSessionCommand, QVariantMap{{"secret", QCryptographicHash::hash("1234", QCryptographicHash::Sha256).toHex()}}); - this->sendBluetoothCommand(GetAllTextSetsCommand); - this->sendBluetoothCommand(GetDisplayBrightnessCommand); + this->setState(AuthenticationRequired); + // TODO: stuff } void OmobiDisplayBackend::handleFoundNewDevice(QBluetoothLeUartDevice* device) { @@ -149,8 +150,16 @@ void OmobiDisplayBackend::handleBluetoothDataReceived(QString s){ switch (header) { case AuthorizeSessionCommand: { - // TODO: handle error - this->refreshLoadingState(); + if(status != Success) { + this->setState(AuthenticationRequired); + return; + } + + this->waitingCommands = 0; + this->setState(Initing); + this->sendBluetoothCommand(GetAllTextSetsCommand); + this->sendBluetoothCommand(GetDisplayBrightnessCommand); + break; } case KeepAliveCommand: { diff --git a/OmobiDisplayApp/omobidisplaybackend.h b/OmobiDisplayApp/omobidisplaybackend.h index f0bdfdb..f9a5fec 100644 --- a/OmobiDisplayApp/omobidisplaybackend.h +++ b/OmobiDisplayApp/omobidisplaybackend.h @@ -25,6 +25,8 @@ public: Scanning, ReadyToConnect, Connecting, + AuthenticationRequired, + Authenticating, Initing, Connected, Loading @@ -57,6 +59,7 @@ private: public slots: Q_INVOKABLE void startScanning(); + Q_INVOKABLE void authenticate(QString secret); Q_INVOKABLE QBluetoothLeUart* getBleController(); Q_INVOKABLE OmobiDisplayAppState getState(); Q_INVOKABLE OmobiDisplayTextModel* getDisplayTextModel(); diff --git a/OmobiDisplayApp/ressources/qml/ConnectPage.qml b/OmobiDisplayApp/ressources/qml/ConnectPage.qml index b765333..e28c844 100644 --- a/OmobiDisplayApp/ressources/qml/ConnectPage.qml +++ b/OmobiDisplayApp/ressources/qml/ConnectPage.qml @@ -126,6 +126,45 @@ Page { } } + Dialog { + id: authenticationDialog + + property bool shouldBeOpened: false + + parent: Overlay.overlay + x: (parent.width - width) / 2 + y: (parent.height - height) / 2 + + width: parent.width * 0.9 + + modal: true + closePolicy: Popup.NoAutoClose + standardButtons: Dialog.Ok | Dialog.Cancel + + title: qsTr("Input code") + + onShouldBeOpenedChanged: { + if(shouldBeOpened) + open() + else + close() + } + + onAccepted: { + backend.authenticate(secretTextInput.text) + } + + onRejected: { + backend.bleController.disconnectFromDevice() + } + + contentItem: TextField { + id: secretTextInput + placeholderText: qsTr("code") + Keys.onReturnPressed: authenticationDialog.accept() + } + } + states: [ State { name: OmobiDisplayBackend.Idle @@ -154,6 +193,41 @@ Page { working: false } }, + + State { + name: OmobiDisplayBackend.AuthenticationRequired + + PropertyChanges { + target: authenticationDialog + shouldBeOpened: true + } + + PropertyChanges { + target: availableDevicesListView + enabled: false + } + + PropertyChanges { + target: root + statusText: "trying to authenticate..." + working: true + } + }, + State { + name: OmobiDisplayBackend.Authenticating + + PropertyChanges { + target: availableDevicesListView + enabled: false + } + + PropertyChanges { + target: root + statusText: "trying to authenticate..." + working: true + } + }, + State { name: OmobiDisplayBackend.Connecting diff --git a/OmobiDisplayApp/ressources/qml/TextInputDelegate.qml b/OmobiDisplayApp/ressources/qml/TextInputDelegate.qml index e60d694..0e708ad 100644 --- a/OmobiDisplayApp/ressources/qml/TextInputDelegate.qml +++ b/OmobiDisplayApp/ressources/qml/TextInputDelegate.qml @@ -73,6 +73,8 @@ ItemDelegate { placeholderText: control.placeholderText text: control.value + + Keys.onReturnPressed: textEditDialog.accept() } onAccepted: { diff --git a/OmobiDisplayApp/ressources/qml/main.qml b/OmobiDisplayApp/ressources/qml/main.qml index 8cb292e..5d62b80 100644 --- a/OmobiDisplayApp/ressources/qml/main.qml +++ b/OmobiDisplayApp/ressources/qml/main.qml @@ -104,6 +104,22 @@ ApplicationWindow { } }, + State { + name: OmobiDisplayBackend.AuthenticationRequired + PropertyChanges { + target: mainStack + currentComponent: connectPageComp + } + }, + + State { + name: OmobiDisplayBackend.Authenticating + PropertyChanges { + target: mainStack + currentComponent: connectPageComp + } + }, + State { name: OmobiDisplayBackend.Initing PropertyChanges {