Skip to content

Commit

Permalink
Fix getting account id + logging messages
Browse files Browse the repository at this point in the history
Notable Changes:
- Fix getting account id on Windows and big-endian systems
- Add extra logging to users during PSN connection process
  • Loading branch information
streetpea committed May 7, 2024
1 parent 6dbcb1d commit 35e83ad
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 18 deletions.
23 changes: 18 additions & 5 deletions gui/include/psnaccountid.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,25 @@ class PSNAccountID : public QObject {
QString basicAuthHeader;
Settings *settings = {};

static QByteArray to_bytes(long number, int num_bytes) {
static QByteArray to_bytes_little_endian(long long number, int num_bytes) {
QByteArray byte_array;
for (int i = 0; i < num_bytes; i++) {
char result = number & 0xFF;
byte_array.append(result);
number >>= 8;
int n = 1;
if(*(char *)&n == 1) // little endian
{
for (int i = 0; i < num_bytes; i++)
{
char result = number & 0xFF;
byte_array.append(result);
number >>= 8;
}
}
else // big endian
{
for (int i = num_bytes - 1; i >= 0; i--)
{
char result = (number >> (8 * num_bytes)) & 0xFF;
byte_array.append(result);
}
}
return byte_array;
}
Expand Down
20 changes: 18 additions & 2 deletions gui/include/qmlbackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,20 @@ class QmlBackend : public QObject
Q_PROPERTY(bool discoveryEnabled READ discoveryEnabled WRITE setDiscoveryEnabled NOTIFY discoveryEnabledChanged)
Q_PROPERTY(QVariantList hosts READ hosts NOTIFY hostsChanged)
Q_PROPERTY(bool autoConnect READ autoConnect NOTIFY autoConnectChanged)
Q_PROPERTY(PsnConnectState connectState READ connectState WRITE setConnectState NOTIFY connectStateChanged)

public:

enum class PsnConnectState
{
NotStarted,
InitiatingConnection,
LinkingConsole,
DataConnectionStart,
DataConnectionFinished,
ConnectFailed
};
Q_ENUM(PsnConnectState);
QmlBackend(Settings *settings, QmlMainWindow *window);
~QmlBackend();

Expand All @@ -69,6 +81,9 @@ class QmlBackend : public QObject
void setDiscoveryEnabled(bool enabled);
void refreshAuth();

PsnConnectState connectState() const;
void setConnectState(PsnConnectState connect_state);

QVariantList hosts() const;

bool autoConnect() const;
Expand Down Expand Up @@ -106,7 +121,7 @@ class QmlBackend : public QObject
signals:
void sessionChanged(StreamSession *session);
void psnConnect(StreamSession *session, const QString &duid, const bool &ps5);
void psnConnectDone(bool connected);
void connectStateChanged();
void controllersChanged();
void discoveryEnabledChanged();
void hostsChanged();
Expand Down Expand Up @@ -154,6 +169,7 @@ class QmlBackend : public QObject
StreamSession *session = {};
QThread *frame_thread = {};
QThread psn_connection_thread;
PsnConnectState psn_connect_state;
DiscoveryManager discovery_manager;
QHash<int, QmlController*> controllers;
DisplayServer regist_dialog_server;
Expand All @@ -162,4 +178,4 @@ class QmlBackend : public QObject
bool resume_session = false;
HostMAC auto_connect_mac = {};
QMap<QString, PsnHost> psn_hosts;
};
};
1 change: 1 addition & 0 deletions gui/include/streamsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class StreamSession : public QObject
#endif
void SessionQuit(ChiakiQuitReason reason, const QString &reason_str);
void LoginPINRequested(bool incorrect);
void DataHolepunchProgress(bool finished);
void ConnectedChanged();
void MeasuredBitrateChanged();
void AveragePacketLossChanged();
Expand Down
4 changes: 2 additions & 2 deletions gui/src/psnaccountid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <qjsonobject.h>
#include <QObject>
#include <QDebug>
PSNAccountID::PSNAccountID(Settings *settings, QObject *parent)
: QObject(parent)
, settings(settings)
Expand Down Expand Up @@ -40,8 +41,7 @@ void PSNAccountID::handleAccessTokenResponse(const QString& url, const QJsonDocu
void PSNAccountID::handUserIDResponse(const QString& url, const QJsonDocument& jsonDocument) {
QJsonObject object = jsonDocument.object();
QString user_id = object.value("user_id").toString();

QByteArray byte_representation = to_bytes(std::stoll(user_id.toStdString()), 8);
QByteArray byte_representation = to_bytes_little_endian(std::stoll(user_id.toStdString()), 8);
settings->SetPsnAccountId(byte_representation.toBase64());
emit AccountIDResponse(byte_representation.toBase64());
}
Expand Down
7 changes: 0 additions & 7 deletions gui/src/qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,6 @@ Item {
root.showPSNTokenDialog(qsTr(""), true);
}

function onPsnConnectDone(connected) {
if (connected)
root.showStreamView();
else
root.showMainView();
}

function onError(title, text) {
errorTitleLabel.text = title;
errorTextLabel.text = text;
Expand Down
33 changes: 33 additions & 0 deletions gui/src/qml/PsnView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,41 @@ Rectangle {
}

Timer {
id: closeTimer
interval: 1500
running: true
onTriggered: view.allowClose = true
}

Timer {
id: failTimer
interval: 1500
running: false
onTriggered: root.showMainView()
}

Connections {
target: Chiaki

function onConnectStateChanged() {
switch(Chiaki.connectState)
{
case Chiaki.PsnConnectState.LinkingConsole:
infoLabel.text = qsTr("Linking Chiaki4deck with PlayStation console ...")
view.allowClose = false
break
case Chiaki.PsnConnectState.DataConnectionStart:
infoLabel.text = qsTr("Console Linked ... Establising data connection with console over PSN ...")
view.allowClose = true
break
case Chiaki.PsnConnectState.DataConnectionFinished:
root.showStreamView()
break
case Chiaki.PsnConnectState.ConnectFailed:
qsTr("Connection over PSN failed closing ...")
failTimer.running = true
break
}
}
}
}
28 changes: 26 additions & 2 deletions gui/src/qmlbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ QmlBackend::QmlBackend(Settings *settings, QmlMainWindow *window)
connect(worker, &PsnConnectionWorker::resultReady, this, &QmlBackend::checkPsnConnection);
psn_connection_thread.start();

setConnectState(PsnConnectState::NotStarted);
connect(settings, &Settings::RegisteredHostsUpdated, this, &QmlBackend::hostsChanged);
connect(settings, &Settings::ManualHostsUpdated, this, &QmlBackend::hostsChanged);
connect(&discovery_manager, &DiscoveryManager::HostsUpdated, this, &QmlBackend::updateDiscoveryHosts);
Expand Down Expand Up @@ -221,6 +222,17 @@ void QmlBackend::setDiscoveryEnabled(bool enabled)
emit discoveryEnabledChanged();
}

QmlBackend::PsnConnectState QmlBackend::connectState() const
{
return psn_connect_state;
}

void QmlBackend::setConnectState(PsnConnectState connect_state)
{
psn_connect_state = connect_state;
emit connectStateChanged();
}

QVariantList QmlBackend::hosts() const
{
QVariantList out;
Expand Down Expand Up @@ -293,11 +305,14 @@ void QmlBackend::psnCancel()

void QmlBackend::checkPsnConnection(const bool &connected)
{
emit psnConnectDone(connected);
if(connected)
{
setConnectState(PsnConnectState::LinkingConsole);
psnSessionStart();
}
else
{
setConnectState(PsnConnectState::ConnectFailed);
delete session;
session = NULL;
}
Expand All @@ -306,7 +321,6 @@ void QmlBackend::checkPsnConnection(const bool &connected)
void QmlBackend::psnSessionStart()
{
session->Start();
emit sessionChanged(session);

sleep_inhibit->inhibit();
}
Expand Down Expand Up @@ -396,6 +410,13 @@ void QmlBackend::createSession(const StreamSessionConnectInfo &connect_info)
emit sessionPinDialogRequested();
});

connect(session, &StreamSession::DataHolepunchProgress, this, [this](bool finished) {
if(finished)
setConnectState(PsnConnectState::DataConnectionFinished);
else
setConnectState(PsnConnectState::DataConnectionStart);
});

connect(session, &StreamSession::ConnectedChanged, this, [this]() {
if (session->IsConnected())
setDiscoveryEnabled(false);
Expand All @@ -416,7 +437,10 @@ void QmlBackend::createSession(const StreamSessionConnectInfo &connect_info)
sleep_inhibit->inhibit();
}
else
{
setConnectState(PsnConnectState::InitiatingConnection);
emit psnConnect(session, session_info.duid, chiaki_target_is_ps5(session_info.target));
}
}

bool QmlBackend::closeRequested()
Expand Down
3 changes: 3 additions & 0 deletions gui/src/streamsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,9 @@ void StreamSession::Event(ChiakiEvent *event)
case CHIAKI_EVENT_LOGIN_PIN_REQUEST:
emit LoginPINRequested(event->login_pin_request.pin_incorrect);
break;
case CHIAKI_EVENT_HOLEPUNCH:
emit DataHolepunchProgress(event->data_holepunch.finished);
break;
case CHIAKI_EVENT_RUMBLE: {
uint8_t left = event->rumble.left;
uint8_t right = event->rumble.right;
Expand Down
5 changes: 5 additions & 0 deletions lib/include/chiaki/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ typedef struct chiaki_trigger_effects_event_t
typedef enum {
CHIAKI_EVENT_CONNECTED,
CHIAKI_EVENT_LOGIN_PIN_REQUEST,
CHIAKI_EVENT_HOLEPUNCH,
CHIAKI_EVENT_KEYBOARD_OPEN,
CHIAKI_EVENT_KEYBOARD_TEXT_CHANGE,
CHIAKI_EVENT_KEYBOARD_REMOTE_CLOSE,
Expand All @@ -167,6 +168,10 @@ typedef struct chiaki_event_t
{
bool pin_incorrect; // false on first request, true if the pin entered before was incorrect
} login_pin_request;
struct
{
bool finished; // false when punching hole, true when finished
} data_holepunch;
};
} ChiakiEvent;

Expand Down
8 changes: 8 additions & 0 deletions lib/src/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ static void *session_thread_func(void *arg)
if(session->rudp)
{
CHIAKI_LOGI(session->log, "Punching hole for data connection");
ChiakiEvent event_start = { 0 };
event_start.type = CHIAKI_EVENT_HOLEPUNCH;
event_start.data_holepunch.finished = false;
chiaki_session_send_event(session, &event_start);
err = chiaki_holepunch_session_punch_hole(session->holepunch_session, CHIAKI_HOLEPUNCH_PORT_TYPE_DATA);
if (err != CHIAKI_ERR_SUCCESS)
{
Expand All @@ -542,6 +546,10 @@ static void *session_thread_func(void *arg)
}
CHIAKI_LOGI(session->log, ">> Punched hole for data connection!");
data_sock = chiaki_get_holepunch_sock(session->holepunch_session, CHIAKI_HOLEPUNCH_PORT_TYPE_DATA);
ChiakiEvent event_finish = { 0 };
event_finish.type = CHIAKI_EVENT_HOLEPUNCH;
event_finish.data_holepunch.finished = true;
chiaki_session_send_event(session, &event_finish);
err = chiaki_cond_timedwait_pred(&session->state_cond, &session->state_mutex, SESSION_EXPECT_TIMEOUT_MS, session_check_state_pred_ctrl_start, session);
CHECK_STOP(quit_ctrl);
}
Expand Down

0 comments on commit 35e83ad

Please sign in to comment.