From caa70ab55f0f261b7fde3633dd3747488fb8a406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sat, 29 Jun 2019 19:06:43 +0200 Subject: [PATCH] Dirty Feedback State working! --- gui/include/streamsession.h | 1 + gui/src/streamsession.cpp | 18 ++++++++++++++++++ lib/include/chiaki/feedback.h | 8 ++++---- lib/include/chiaki/streamconnection.h | 4 ++++ lib/include/chiaki/thread.h | 2 +- lib/src/audioreceiver.c | 2 +- lib/src/feedback.c | 10 +++++----- lib/src/session.c | 2 +- lib/src/streamconnection.c | 12 +++++++++++- lib/src/takion.c | 8 +++++--- lib/src/thread.c | 16 +++++++++++++--- 11 files changed, 64 insertions(+), 19 deletions(-) diff --git a/gui/include/streamsession.h b/gui/include/streamsession.h index 63e7286..ff52b02 100644 --- a/gui/include/streamsession.h +++ b/gui/include/streamsession.h @@ -60,6 +60,7 @@ class StreamSession : public QObject private slots: void UpdateGamepads(); + void SendFeedbackState(); }; #endif // CHIAKI_STREAMSESSION_H diff --git a/gui/src/streamsession.cpp b/gui/src/streamsession.cpp index 0778b7d..f0ad71e 100644 --- a/gui/src/streamsession.cpp +++ b/gui/src/streamsession.cpp @@ -113,8 +113,26 @@ void StreamSession::UpdateGamepads() { gamepad = new QGamepad(connected_pads[0], this); qDebug() << "gamepad" << connected_pads[0] << "connected: " << gamepad->name(); + connect(gamepad, &QGamepad::axisLeftXChanged, this, &StreamSession::SendFeedbackState); + connect(gamepad, &QGamepad::axisLeftYChanged, this, &StreamSession::SendFeedbackState); + connect(gamepad, &QGamepad::axisRightXChanged, this, &StreamSession::SendFeedbackState); + connect(gamepad, &QGamepad::axisRightYChanged, this, &StreamSession::SendFeedbackState); } } + + SendFeedbackState(); +} + +void StreamSession::SendFeedbackState() +{ + if(!gamepad) + return; + ChiakiFeedbackState state; + state.left_x = static_cast(gamepad->axisLeftX() * 0x7fff); + state.left_y = static_cast(gamepad->axisLeftX() * 0x7fff); + state.right_x = static_cast(gamepad->axisLeftX() * 0x7fff); + state.right_y = static_cast(gamepad->axisLeftX() * 0x7fff); + chiaki_stream_connection_send_feedback_state(&session.stream_connection, &state); } void StreamSession::PushAudioFrame(int16_t *buf, size_t samples_count) diff --git a/lib/include/chiaki/feedback.h b/lib/include/chiaki/feedback.h index 2464eec..4fa2a37 100644 --- a/lib/include/chiaki/feedback.h +++ b/lib/include/chiaki/feedback.h @@ -28,10 +28,10 @@ extern "C" { typedef struct chiaki_feedback_state_t { - uint16_t left_x; - uint16_t left_y; - uint16_t right_x; - uint16_t right_y; + int16_t left_x; + int16_t left_y; + int16_t right_x; + int16_t right_y; } ChiakiFeedbackState; #define CHIAKI_FEEDBACK_STATE_BUF_SIZE 0x19 diff --git a/lib/include/chiaki/streamconnection.h b/lib/include/chiaki/streamconnection.h index 6f630a6..1042a02 100644 --- a/lib/include/chiaki/streamconnection.h +++ b/lib/include/chiaki/streamconnection.h @@ -41,6 +41,8 @@ typedef struct chiaki_stream_connection_t ChiakiGKCrypt *gkcrypt_local; ChiakiGKCrypt *gkcrypt_remote; + ChiakiSeqNum16 feedback_state_seq_num; + /** * signaled on change of state_finished or should_stop */ @@ -70,6 +72,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_run(ChiakiStreamConnectio */ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_stop(ChiakiStreamConnection *stream_connection); +CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_send_feedback_state(ChiakiStreamConnection *stream_connection, ChiakiFeedbackState *state); + #ifdef __cplusplus } #endif diff --git a/lib/include/chiaki/thread.h b/lib/include/chiaki/thread.h index 29803c4..4e40a85 100644 --- a/lib/include/chiaki/thread.h +++ b/lib/include/chiaki/thread.h @@ -44,7 +44,7 @@ typedef struct chiaki_mutex_t pthread_mutex_t mutex; } ChiakiMutex; -CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_init(ChiakiMutex *mutex); +CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_init(ChiakiMutex *mutex, bool rec); CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_fini(ChiakiMutex *mutex); CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_lock(ChiakiMutex *mutex); CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_trylock(ChiakiMutex *mutex); diff --git a/lib/src/audioreceiver.c b/lib/src/audioreceiver.c index f0ed1a2..87626e2 100644 --- a/lib/src/audioreceiver.c +++ b/lib/src/audioreceiver.c @@ -30,7 +30,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_audio_receiver_init(ChiakiAudioReceiver *au audio_receiver->opus_decoder = NULL; memset(&audio_receiver->audio_header, 0, sizeof(audio_receiver->audio_header)); - ChiakiErrorCode err = chiaki_mutex_init(&audio_receiver->mutex); + ChiakiErrorCode err = chiaki_mutex_init(&audio_receiver->mutex, false); if(err != CHIAKI_ERR_SUCCESS) return err; diff --git a/lib/src/feedback.c b/lib/src/feedback.c index abe00b1..f71c9f2 100644 --- a/lib/src/feedback.c +++ b/lib/src/feedback.c @@ -38,8 +38,8 @@ CHIAKI_EXPORT void chiaki_feedback_state_format(uint8_t *buf, ChiakiFeedbackStat buf[0xe] = 0xf7; // TODO buf[0xf] = 0xef; // TODO buf[0x10] = 0x1f; // TODO - *((uint16_t *)(buf + 0x11)) = htons(state->left_x); - *((uint16_t *)(buf + 0x13)) = htons(state->left_y); - *((uint16_t *)(buf + 0x15)) = htons(state->right_x); - *((uint16_t *)(buf + 0x17)) = htons(state->right_y); -} \ No newline at end of file + *((uint16_t *)(buf + 0x11)) = htons((uint16_t)state->left_x); + *((uint16_t *)(buf + 0x13)) = htons((uint16_t)state->left_y); + *((uint16_t *)(buf + 0x15)) = htons((uint16_t)state->right_x); + *((uint16_t *)(buf + 0x17)) = htons((uint16_t)state->right_y); +} diff --git a/lib/src/session.c b/lib/src/session.c index cfb14e7..9684c29 100644 --- a/lib/src/session.c +++ b/lib/src/session.c @@ -56,7 +56,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_session_init(ChiakiSession *session, Chiaki return CHIAKI_ERR_UNKNOWN; } - if(chiaki_mutex_init(&session->ctrl_cond_mutex) != CHIAKI_ERR_SUCCESS) + if(chiaki_mutex_init(&session->ctrl_cond_mutex, false) != CHIAKI_ERR_SUCCESS) { chiaki_cond_fini(&session->ctrl_cond); return CHIAKI_ERR_UNKNOWN; diff --git a/lib/src/streamconnection.c b/lib/src/streamconnection.c index 58394cf..1297b90 100644 --- a/lib/src/streamconnection.c +++ b/lib/src/streamconnection.c @@ -72,7 +72,9 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_init(ChiakiStreamConnecti stream_connection->gkcrypt_remote = NULL; stream_connection->gkcrypt_local = NULL; - ChiakiErrorCode err = chiaki_mutex_init(&stream_connection->state_mutex); + stream_connection->feedback_state_seq_num = 0; + + ChiakiErrorCode err = chiaki_mutex_init(&stream_connection->state_mutex, false); if(err != CHIAKI_ERR_SUCCESS) goto error; @@ -726,4 +728,12 @@ static ChiakiErrorCode stream_connection_send_heartbeat(ChiakiStreamConnection * } return chiaki_takion_send_message_data(&stream_connection->takion, 1, 1, buf, stream.bytes_written); +} + +CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_send_feedback_state(ChiakiStreamConnection *stream_connection, ChiakiFeedbackState *state) +{ + ChiakiErrorCode err = chiaki_takion_send_feedback_state(&stream_connection->takion, stream_connection->feedback_state_seq_num++, state); + if(err != CHIAKI_ERR_SUCCESS) + CHIAKI_LOGE(stream_connection->log, "StreamConnection failed to send feedback state\n"); + return err; } \ No newline at end of file diff --git a/lib/src/takion.c b/lib/src/takion.c index 1d785d8..5282cb5 100644 --- a/lib/src/takion.c +++ b/lib/src/takion.c @@ -175,7 +175,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_connect(ChiakiTakion *takion, Chiaki takion->log = info->log; takion->gkcrypt_local = NULL; - ret = chiaki_mutex_init(&takion->gkcrypt_local_mutex); + ret = chiaki_mutex_init(&takion->gkcrypt_local_mutex, true); if(ret != CHIAKI_ERR_SUCCESS) return ret; takion->key_pos_local = 0; @@ -431,11 +431,13 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_feedback_state(ChiakiTakion *ta if(err != CHIAKI_ERR_SUCCESS) goto beach; - err = chiaki_gkcrypt_gmac(takion->gkcrypt_local, key_pos, buf, sizeof(buf), buf + 4); + *((uint32_t *)(buf + 4)) = htonl((uint32_t)key_pos); + + err = chiaki_gkcrypt_gmac(takion->gkcrypt_local, key_pos, buf, sizeof(buf), buf + 8); if(err != CHIAKI_ERR_SUCCESS) goto beach; - *((uint32_t *)(buf + 4)) = htonl((uint32_t)key_pos); + chiaki_takion_send_raw(takion, buf, sizeof(buf)); beach: chiaki_mutex_unlock(&takion->gkcrypt_local_mutex); diff --git a/lib/src/thread.c b/lib/src/thread.c index b727d08..ef6e78d 100644 --- a/lib/src/thread.c +++ b/lib/src/thread.c @@ -39,9 +39,19 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_thread_join(ChiakiThread *thread, void **re -CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_init(ChiakiMutex *mutex) +CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_init(ChiakiMutex *mutex, bool rec) { - int r = pthread_mutex_init(&mutex->mutex, NULL); + pthread_mutexattr_t attr; + int r = pthread_mutexattr_init(&attr); + if(r != 0) + return CHIAKI_ERR_UNKNOWN; + + pthread_mutexattr_settype(&attr, rec ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_DEFAULT); + + r = pthread_mutex_init(&mutex->mutex, &attr); + + pthread_mutexattr_destroy(&attr); + if(r != 0) return CHIAKI_ERR_UNKNOWN; return CHIAKI_ERR_SUCCESS; @@ -203,7 +213,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_init(ChiakiBoolPredCond *con { cond->pred = false; - ChiakiErrorCode err = chiaki_mutex_init(&cond->mutex); + ChiakiErrorCode err = chiaki_mutex_init(&cond->mutex, false); if(err != CHIAKI_ERR_SUCCESS) return err;