From 5519283cb31392eb03d542928a29701b2465d7df Mon Sep 17 00:00:00 2001 From: ndeadly <24677491+ndeadly@users.noreply.github.com> Date: Thu, 30 Jul 2020 22:10:37 +0200 Subject: [PATCH] btdrv-mitm: minor improvements to bluetooth::CircularBuffer --- .../bluetooth/bluetooth_circularbuffer.cpp | 34 +++++++++---------- .../bluetooth/bluetooth_circularbuffer.hpp | 15 ++++---- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/btdrv-mitm/source/bluetooth/bluetooth_circularbuffer.cpp b/btdrv-mitm/source/bluetooth/bluetooth_circularbuffer.cpp index 110e830..a18e44c 100644 --- a/btdrv-mitm/source/bluetooth/bluetooth_circularbuffer.cpp +++ b/btdrv-mitm/source/bluetooth/bluetooth_circularbuffer.cpp @@ -22,7 +22,6 @@ namespace ams::bluetooth { std::strncpy(this->name, name, sizeof(this->name) - 1); this->_unk1 = 0; this->size = BLUETOOTH_CIRCBUFFER_SIZE; - //os::InitializeSdkMutex(&this->mutex); this->isInitialized = true; } @@ -67,22 +66,21 @@ namespace ams::bluetooth { return -1; std::scoped_lock lk(this->mutex); - { - ON_SCOPE_EXIT { - if (this->event) - os::SignalEvent(this->event); - }; - if (size + sizeof(CircularBufferPacketHeader) <= this->GetWriteableSize()) { - if (size + 2*sizeof(CircularBufferPacketHeader) > BLUETOOTH_CIRCBUFFER_SIZE - this->writeOffset) { - R_TRY(this->_write(0xff, nullptr, (BLUETOOTH_CIRCBUFFER_SIZE - this->writeOffset) - sizeof(CircularBufferPacketHeader))); - } + ON_SCOPE_EXIT { + if (this->event) + os::SignalEvent(this->event); + }; - R_TRY(this->_write(type, data, size)); - this->_updateUtilization(); - - return 0; + if (size + sizeof(CircularBufferPacketHeader) <= this->GetWriteableSize()) { + if (size + 2*sizeof(CircularBufferPacketHeader) > BLUETOOTH_CIRCBUFFER_SIZE - this->writeOffset) { + R_TRY(this->_write(0xff, nullptr, (BLUETOOTH_CIRCBUFFER_SIZE - this->writeOffset) - sizeof(CircularBufferPacketHeader))); } + + R_TRY(this->_write(type, data, size)); + this->_updateUtilization(); + + return 0; } return -1; @@ -113,7 +111,7 @@ namespace ams::bluetooth { } } - void *CircularBuffer::Read(void) { + CircularBufferPacket *CircularBuffer::Read(void) { return this->_read(); } @@ -124,7 +122,7 @@ namespace ams::bluetooth { if (this->readOffset == this->writeOffset) return -1; - CircularBufferPacket *packet = reinterpret_cast(&this->data[this->readOffset]); + auto packet = reinterpret_cast(&this->data[this->readOffset]); u32 newOffset = this->readOffset + packet->header.size + sizeof(packet->header); if (newOffset >= BLUETOOTH_CIRCBUFFER_SIZE) @@ -161,7 +159,7 @@ namespace ams::bluetooth { } u64 CircularBuffer::_write(u8 type, void *data, size_t size) { - CircularBufferPacket *packet = reinterpret_cast(&this->data[this->writeOffset]); + auto packet = reinterpret_cast(&this->data[this->writeOffset]); packet->header.type = type; packet->header.timestamp = os::GetSystemTick(); packet->header.size = size; @@ -192,7 +190,7 @@ namespace ams::bluetooth { this->size = newCapacity; } - void *CircularBuffer::_read(void) { + CircularBufferPacket *CircularBuffer::_read(void) { if (this->isInitialized) { CircularBufferPacket *packet; u32 newOffset; diff --git a/btdrv-mitm/source/bluetooth/bluetooth_circularbuffer.hpp b/btdrv-mitm/source/bluetooth/bluetooth_circularbuffer.hpp index c991c80..b3a3f37 100644 --- a/btdrv-mitm/source/bluetooth/bluetooth_circularbuffer.hpp +++ b/btdrv-mitm/source/bluetooth/bluetooth_circularbuffer.hpp @@ -7,7 +7,8 @@ namespace ams::bluetooth { enum CircularBufferType { - CircularBufferType_HidReport = 0x1, + CircularBufferType_Other, + CircularBufferType_HidReport, CircularBufferType_Bluetooth, CircularBufferType_Ble, CircularBufferType_BleCore, @@ -33,12 +34,12 @@ namespace ams::bluetooth { void Initialize(const char *name); // 10.0.0+, previously took event argument void Finalize(void); bool IsInitialized(void); - u64 GetWriteableSize(void); + u64 GetWriteableSize(void); void SetWriteCompleteEvent(os::EventType *event); - u64 Write(u8 type, void *data, size_t size); - void DiscardOldPackets(u8 a1, u32 a2); - void *Read(void); - u64 Free(void); + u64 Write(u8 type, void *data, size_t size); + void DiscardOldPackets(u8 type, u32 ageLimit); + CircularBufferPacket *Read(void); + u64 Free(void); private: void _setReadOffset(u32 offset); @@ -47,7 +48,7 @@ namespace ams::bluetooth { u32 _getReadOffset(void); u64 _write(u8 type, void *data, size_t size); void _updateUtilization(void); - void *_read(void); + CircularBufferPacket *_read(void); os::SdkMutex mutex; os::EventType *event;