btdrv-mitm: minor improvements to bluetooth::CircularBuffer

This commit is contained in:
ndeadly 2020-07-30 22:10:37 +02:00
parent c5a5b1f456
commit 5519283cb3
2 changed files with 24 additions and 25 deletions

View file

@ -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<CircularBufferPacket *>(&this->data[this->readOffset]);
auto packet = reinterpret_cast<CircularBufferPacket *>(&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<CircularBufferPacket *>(&this->data[this->writeOffset]);
auto packet = reinterpret_cast<CircularBufferPacket *>(&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;

View file

@ -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;