From 149091351d8880f5a64d8a98fa632089fe584622 Mon Sep 17 00:00:00 2001 From: ndeadly <24677491+ndeadly@users.noreply.github.com> Date: Thu, 11 Jun 2020 16:03:41 +0200 Subject: [PATCH] btdrv-mitm: clean up CircularBuffer class --- btdrv-mitm/source/circularbuffer.cpp | 177 +++------------------------ btdrv-mitm/source/circularbuffer.hpp | 10 +- 2 files changed, 24 insertions(+), 163 deletions(-) diff --git a/btdrv-mitm/source/circularbuffer.cpp b/btdrv-mitm/source/circularbuffer.cpp index be648ae..1c5e200 100644 --- a/btdrv-mitm/source/circularbuffer.cpp +++ b/btdrv-mitm/source/circularbuffer.cpp @@ -1,3 +1,4 @@ +#include #include #include "circularbuffer.hpp" @@ -5,14 +6,12 @@ namespace ams::bluetooth { - // done CircularBuffer::CircularBuffer(void) { this->readOffset = 0; this->writeOffset = 0; this->isInitialized = false; } - // done except unknowns void CircularBuffer::Initialize(const char *name) { if (!name || this->isInitialized) fatalThrow(-1); @@ -22,11 +21,10 @@ namespace ams::bluetooth { std::strncpy(this->name, name, 0x10); this->_unk1 = 0; this->size = BLUETOOTH_CIRCBUFFER_SIZE; - os::InitializeSdkMutex(&this->mutex); + //os::InitializeSdkMutex(&this->mutex); this->isInitialized = true; } - // done void CircularBuffer::Finalize(void) { if (!this->isInitialized) fatalThrow(-1); @@ -35,12 +33,10 @@ namespace ams::bluetooth { this->event = nullptr; } - // done bool CircularBuffer::IsInitialized(void) { return this->isInitialized; } - // done u64 CircularBuffer::GetWriteableSize(void) { u32 readOffset = this->readOffset; u32 writeOffset = this->writeOffset; @@ -52,7 +48,7 @@ namespace ams::bluetooth { if (readOffset <= writeOffset) size = (BLUETOOTH_CIRCBUFFER_SIZE - 1) - writeOffset + readOffset; else - size = readOffset + ~writeOffset; + size = readOffset - writeOffset - 1; if (size > BLUETOOTH_CIRCBUFFER_SIZE) size = 0; @@ -60,43 +56,39 @@ namespace ams::bluetooth { return size; } - // done void CircularBuffer::AttachEvent(os::EventType *event) { this->event = event; } - // *** WIP *** - u64 CircularBuffer::Write(u8 type, void *data, size_t size) { - if (this->IsInitialized()) { - os::LockSdkMutex(&this->mutex); + u64 CircularBuffer::Write(u8 type, void *data, size_t size) { - u32 writeableSize = this->GetWriteableSize(); - u32 packetSize = size + sizeof(CircularBufferPacketHeader); + if (!this->isInitialized) + return -1; - if (packetSize > writeableSize) { + std::scoped_lock lk(this->mutex); + { + ON_SCOPE_EXIT { if (this->event) os::SignalEvent(this->event); + }; - os::UnlockSdkMutex(&this->mutex); - return -1; - } - else { - if (packetSize > 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; } - - - // Not sure how to connec this logic just yet - } - // Todo: finish this return -1; } - // possibly done void CircularBuffer::DiscardOldPackets(u8 type, u32 ageLimit) { if (this->isInitialized) { @@ -106,7 +98,7 @@ namespace ams::bluetooth { if (this->readOffset == this->writeOffset) return; - packet = reinterpret_cast(this->data[this->readOffset]); + packet = reinterpret_cast(&this->data[this->readOffset]); if (packet->header.type != 0xff) { if (packet->header.type != type) @@ -122,38 +114,10 @@ namespace ams::bluetooth { } } - // done void *CircularBuffer::Read(void) { return this->_read(); } - // done - u64 CircularBuffer::Free(void) { - if (this->isInitialized) { - - u32 readOffset = this->readOffset; - if (readOffset != this->writeOffset) { - u32 newOffset = readOffset + this->data[readOffset + 16] + sizeof(CircularBufferPacketHeader); - if (newOffset >= BLUETOOTH_CIRCBUFFER_SIZE) { - newOffset = 0; - } - - if (newOffset < BLUETOOTH_CIRCBUFFER_SIZE) { - this->readOffset = newOffset; - return 0; - } - - // Maybe fatalThrow here? - //fatalThrow(-1); - } - } - - return -1; - } - - - // done (needs testing) <- new one - /* u64 CircularBuffer::Free(void) { if (!this->isInitialized) return -1; @@ -161,7 +125,7 @@ namespace ams::bluetooth { if (this->readOffset == this->writeOffset) return -1; - CircularBufferPacket *packet = reinterpret_cast(this->data[this->readOffset]); + CircularBufferPacket *packet = reinterpret_cast(&this->data[this->readOffset]); u32 newOffset = this->readOffset + packet->header.size + sizeof(packet->header); if (newOffset >= BLUETOOTH_CIRCBUFFER_SIZE) @@ -174,55 +138,7 @@ namespace ams::bluetooth { fatalThrow(-1); } - */ - - - - /* - // hid_report_buffer_write + modifications - u64 CircularBuffer::WritePacket(const CircularBufferPacket *srcPacket) { - if (!this->_unk3 || !this->isInitialized) - return -1; - - this->DiscardOldPackets(0x27, 0x64); - - if () {} - - if (size > BLUETOOTH_CIRCBUFFER_SIZE) - return -1; - - - BTDRV_LOG_FMT("write offset: %d", this->writeOffset); - BTDRV_LOG_FMT("buffer address: 0x%p", (void *)this); - BTDRV_LOG_FMT("data address: 0x%p", (void *)&this->data); - CircularBufferPacket *packet = reinterpret_cast(&this->data[this->writeOffset]); - BTDRV_LOG_FMT("packet address: 0x%p", (void *)packet); - packet->header.type = srcPacket->header.type; - packet->header.timestamp = srcPacket->header.timestamp; - packet->header.size = srcPacket->header.size; - - if (srcPacket->header.type != -1) { - if (&srcPacket->data && (srcPacket->header.size > 0)) - memcpy(&packet->data, &srcPacket->data, srcPacket->header.size); - else - return -1; - } - - u32 newOffset = this->writeOffset + srcPacket->header.size + sizeof(packet->header); - if (newOffset >= BLUETOOTH_CIRCBUFFER_SIZE) - return -1; - - if (newOffset == BLUETOOTH_CIRCBUFFER_SIZE) - this->writeOffset = 0; - else - this->writeOffset = newOffset; - - return 0; - } - */ - - // done void CircularBuffer::_setReadOffset(u32 offset) { if (offset >= BLUETOOTH_CIRCBUFFER_SIZE) fatalThrow(-1); @@ -230,7 +146,6 @@ namespace ams::bluetooth { this->readOffset = offset; } - // done void CircularBuffer::_setWriteOffset(u32 offset) { if (offset >= BLUETOOTH_CIRCBUFFER_SIZE) fatalThrow(-1); @@ -238,17 +153,14 @@ namespace ams::bluetooth { this->writeOffset = offset; } - // done u32 CircularBuffer::_getWriteOffset(void) { return this->writeOffset; } - // done u32 CircularBuffer::_getReadOffset(void) { return this->readOffset; } - // done u64 CircularBuffer::_write(u8 type, void *data, size_t size) { CircularBufferPacket *packet = reinterpret_cast(&this->data[this->writeOffset]); packet->header.type = type; @@ -274,21 +186,6 @@ namespace ams::bluetooth { return 0; } - // possibly done - /* - void CircularBuffer::_updateUtilization(void) { - if(this->isInitialized) { - u32 writeableSize = this->GetWriteableSize(); - if (writeableSize + 1000 < this->size) - this->size = writeableSize; - } - else if(this->size > 1000) { - this->size = 0; - } - } - */ - - // done void CircularBuffer::_updateUtilization(void) { u32 newCapacity = this->isInitialized ? this->GetWriteableSize() : 0; @@ -296,7 +193,6 @@ namespace ams::bluetooth { this->size = newCapacity; } - // done (needs checking) <-new one void *CircularBuffer::_read(void) { if (this->isInitialized) { CircularBufferPacket *packet; @@ -309,7 +205,6 @@ namespace ams::bluetooth { if (packet->header.type != 0xff) return packet; - //return &this->data[this->readOffset]; if (!this->isInitialized) return nullptr; @@ -328,36 +223,4 @@ namespace ams::bluetooth { return nullptr; } - /* - // done - void *CircularBuffer::_read(void) { - if (this->isInitialized) { - do { - u32 readOffset = this->readOffset; - if (readOffset == this->writeOffset) - break; - - if (this->data[readOffset] != 0xff) - return this->data + readOffset; - - if (!this->isInitialized) - break; - - readOffset = this->readOffset; - if (readOffset != this->writeOffset) { - u32 newOffset = readOffset + this->data[readOffset + 16] + sizeof(CircularBufferPacketHeader); - if (newOffset >= BLUETOOTH_CIRCBUFFER_SIZE) - newOffset = 0; - - this->_setReadOffset(newOffset); - } - - } while(this->isInitialized); - } - - return nullptr; - } - */ - - } diff --git a/btdrv-mitm/source/circularbuffer.hpp b/btdrv-mitm/source/circularbuffer.hpp index 9b908e7..b37ef5f 100644 --- a/btdrv-mitm/source/circularbuffer.hpp +++ b/btdrv-mitm/source/circularbuffer.hpp @@ -7,8 +7,7 @@ namespace ams::bluetooth { enum CircularBufferType { - CircularBufferType_Unknown, - CircularBufferType_HidReport, + CircularBufferType_HidReport = 0x1, CircularBufferType_Bluetooth, CircularBufferType_Ble, CircularBufferType_BleCore, @@ -41,8 +40,6 @@ namespace ams::bluetooth { void *Read(void); u64 Free(void); - u64 WritePacket(const CircularBufferPacket *packet); // Not a real function, used to write translated packet into buffer - //private: void _setReadOffset(u32 offset); void _setWriteOffset(u32 offset); @@ -53,8 +50,9 @@ namespace ams::bluetooth { void *_read(void); //private: - os::SdkMutexType mutex; - os::EventType *event; + //os::SdkMutexType mutex; + os::SdkMutex mutex; + os::EventType *event; u8 data[BLUETOOTH_CIRCBUFFER_SIZE]; u32 writeOffset;