Skip to content

Commit b993d8e

Browse files
committed
mc.mitm: update bluetooth circular buffer code style and simplify logic
1 parent 2e67e80 commit b993d8e

File tree

3 files changed

+165
-175
lines changed

3 files changed

+165
-175
lines changed

mc_mitm/source/bluetooth_mitm/bluetooth/bluetooth_circular_buffer.cpp

Lines changed: 119 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -18,239 +18,225 @@
1818
namespace ams::bluetooth {
1919

2020
CircularBuffer::CircularBuffer() {
21-
this->readOffset = 0;
22-
this->writeOffset = 0;
23-
this->isInitialized = false;
24-
this->event = nullptr;
21+
m_read_offset = 0;
22+
m_write_offset = 0;
23+
m_initialized = false;
24+
m_event = nullptr;
2525
}
2626

2727
void CircularBuffer::Initialize(const char *name) {
28-
if (!name || this->isInitialized)
29-
fatalThrow(-1);
30-
31-
this->readOffset = 0;
32-
this->writeOffset = 0;
33-
std::strncpy(this->name, name, sizeof(this->name) - 1);
34-
this->_unk1 = 0;
35-
this->size = BLUETOOTH_BUFFER_SIZE;
36-
this->isInitialized = true;
28+
AMS_ABORT_UNLESS(!(m_initialized || name == nullptr));
29+
30+
m_read_offset = 0;
31+
m_write_offset = 0;
32+
std::strncpy(m_name, name, CircularBuffer::MaxNameLength);
33+
m_name[CircularBuffer::MaxNameLength] = '\0';
34+
m_size = CircularBuffer::BufferSize;
35+
m_initialized = true;
3736
}
3837

3938
void CircularBuffer::Finalize() {
40-
if (!this->isInitialized)
41-
fatalThrow(-1);
39+
AMS_ABORT_UNLESS(m_initialized);
4240

43-
this->isInitialized = false;
44-
this->event = nullptr;
41+
m_initialized = false;
42+
m_event = nullptr;
4543
}
4644

4745
bool CircularBuffer::IsInitialized() {
48-
return this->isInitialized;
46+
return m_initialized;
4947
}
5048

5149
u64 CircularBuffer::GetWriteableSize() {
52-
u32 readOffset = this->readOffset;
53-
u32 writeOffset = this->writeOffset;
54-
55-
if (!this->isInitialized) {
50+
if (!m_initialized) {
5651
return 0;
5752
}
5853

54+
u32 read_offset = this->_getReadOffset();
55+
u32 write_offset = this->_getWriteOffset();
56+
5957
u64 size;
60-
if (readOffset <= writeOffset) {
61-
size = (BLUETOOTH_BUFFER_SIZE - 1) - writeOffset + readOffset;
58+
if (read_offset <= write_offset) {
59+
size = (CircularBuffer::BufferSize - 1) - write_offset + read_offset;
6260
} else {
63-
size = readOffset - writeOffset - 1;
61+
size = read_offset - write_offset - 1;
6462
}
6563

66-
if (size > BLUETOOTH_BUFFER_SIZE) {
64+
if (size > CircularBuffer::BufferSize) {
6765
size = 0;
6866
}
6967

7068
return size;
7169
}
7270

7371
void CircularBuffer::SetWriteCompleteEvent(os::EventType *event) {
74-
this->event = event;
72+
m_event = event;
7573
}
7674

77-
u64 CircularBuffer::Write(u8 type, void *data, size_t size) {
78-
79-
if (!this->isInitialized) {
80-
return -1;
75+
Result CircularBuffer::Write(u8 type, const void *data, size_t size) {
76+
if (!m_initialized) {
77+
R_RETURN(-1);
8178
}
8279

83-
std::scoped_lock lk(this->mutex);
80+
std::scoped_lock lk(m_mutex);
8481

8582
ON_SCOPE_EXIT {
86-
if (this->event)
87-
os::SignalEvent(this->event);
88-
};
89-
90-
if (size + sizeof(CircularBufferPacketHeader) <= this->GetWriteableSize()) {
91-
if (size + 2*sizeof(CircularBufferPacketHeader) > BLUETOOTH_BUFFER_SIZE - this->writeOffset) {
92-
if (const auto res = this->_write(0xff, nullptr, (BLUETOOTH_BUFFER_SIZE - this->writeOffset) - sizeof(CircularBufferPacketHeader)); res != 0) {
93-
return res;
94-
}
83+
if (m_event) {
84+
os::SignalEvent(m_event);
9585
}
86+
};
9687

97-
if (const auto res = this->_write(type, data, size); res != 0) {
98-
return res;
99-
}
100-
this->_updateUtilization();
88+
if (size + sizeof(CircularBufferPacketHeader) > this->GetWriteableSize()) {
89+
R_RETURN(-1);
90+
}
10191

102-
return 0;
92+
u32 write_offset = this->_getWriteOffset();
93+
if (size + 2*sizeof(CircularBufferPacketHeader) > CircularBuffer::BufferSize - write_offset) {
94+
R_TRY(this->_write(0xff, nullptr, (CircularBuffer::BufferSize - write_offset) - sizeof(CircularBufferPacketHeader)));
10395
}
10496

105-
return -1;
97+
R_TRY(this->_write(type, data, size));
98+
99+
this->_updateUtilization();
100+
101+
R_SUCCEED();
106102
}
107103

108-
void CircularBuffer::DiscardOldPackets(u8 type, u32 ageLimit) {
109-
if (this->isInitialized) {
104+
void CircularBuffer::DiscardOldPackets(u8 type, u32 age_limit) {
105+
while (m_initialized) {
106+
u32 read_offset = this->_getReadOffset();
107+
u32 write_offset = this->_getWriteOffset();
110108

111-
CircularBufferPacket *packet;
112-
TimeSpan timespan;
113-
do {
114-
if (this->readOffset == this->writeOffset) {
115-
return;
116-
}
109+
if (read_offset == write_offset) {
110+
return;
111+
}
117112

118-
packet = reinterpret_cast<CircularBufferPacket *>(&this->data[this->readOffset]);
119-
if (packet->header.type != 0xff) {
113+
auto packet = reinterpret_cast<CircularBufferPacket *>(&m_data[read_offset]);
114+
if (packet->header.type != 0xff) {
120115

121-
if (packet->header.type != type) {
122-
return;
123-
}
116+
if (packet->header.type != type) {
117+
return;
118+
}
124119

125-
timespan = os::ConvertToTimeSpan(os::GetSystemTick() - packet->header.timestamp);
126-
if (timespan.GetMilliSeconds() <= ageLimit) {
127-
return;
128-
}
120+
TimeSpan timespan = os::ConvertToTimeSpan(os::GetSystemTick() - packet->header.timestamp);
121+
if (timespan.GetMilliSeconds() <= age_limit) {
122+
return;
129123
}
124+
}
125+
126+
u32 new_offset = read_offset + packet->header.size + sizeof(packet->header);
127+
if (new_offset >= CircularBuffer::BufferSize) {
128+
new_offset = 0;
129+
}
130130

131-
this->Free();
132-
} while (this->isInitialized);
131+
this->_setReadOffset(new_offset);
133132
}
134133
}
135134

136135
CircularBufferPacket *CircularBuffer::Read() {
137136
return this->_read();
138137
}
139138

140-
u64 CircularBuffer::Free() {
141-
if (!this->isInitialized) {
142-
return -1;
143-
}
144-
145-
if (this->readOffset == this->writeOffset) {
146-
return -1;
139+
Result CircularBuffer::Free() {
140+
if (!m_initialized) {
141+
R_RETURN(-1);
147142
}
148143

149-
auto packet = reinterpret_cast<CircularBufferPacket *>(&this->data[this->readOffset]);
150-
u32 newOffset = this->readOffset + packet->header.size + sizeof(packet->header);
144+
u32 read_offset = this->_getReadOffset();
145+
u32 write_offset = this->_getWriteOffset();
151146

152-
if (newOffset >= BLUETOOTH_BUFFER_SIZE) {
153-
newOffset = 0;
147+
if (read_offset == write_offset) {
148+
R_RETURN(-1);
154149
}
155150

156-
if (newOffset < BLUETOOTH_BUFFER_SIZE) {
157-
this->readOffset = newOffset;
158-
return 0;
151+
auto packet = reinterpret_cast<CircularBufferPacket *>(&m_data[read_offset]);
152+
u32 new_offset = read_offset + packet->header.size + sizeof(packet->header);
153+
if (new_offset >= CircularBuffer::BufferSize) {
154+
new_offset = 0;
159155
}
160156

161-
fatalThrow(-1);
157+
this->_setReadOffset(new_offset);
158+
159+
R_SUCCEED();
162160
}
163161

164162
void CircularBuffer::_setReadOffset(u32 offset) {
165-
if (offset >= BLUETOOTH_BUFFER_SIZE) {
166-
fatalThrow(-1);
167-
}
163+
AMS_ABORT_UNLESS(offset < CircularBuffer::BufferSize);
168164

169-
this->readOffset = offset;
165+
m_read_offset = offset;
170166
}
171167

172168
void CircularBuffer::_setWriteOffset(u32 offset) {
173-
if (offset >= BLUETOOTH_BUFFER_SIZE) {
174-
fatalThrow(-1);
175-
}
169+
AMS_ABORT_UNLESS(offset < CircularBuffer::BufferSize);
176170

177-
this->writeOffset = offset;
171+
m_write_offset = offset;
178172
}
179173

180174
u32 CircularBuffer::_getWriteOffset() {
181-
return this->writeOffset;
175+
return m_write_offset;
182176
}
183177

184178
u32 CircularBuffer::_getReadOffset() {
185-
return this->readOffset;
179+
return m_read_offset;
186180
}
187181

188-
u64 CircularBuffer::_write(u8 type, void *data, size_t size) {
189-
auto packet = reinterpret_cast<CircularBufferPacket *>(&this->data[this->writeOffset]);
182+
Result CircularBuffer::_write(u8 type, const void *data, size_t size) {
183+
u32 write_offset = this->_getWriteOffset();
184+
185+
auto packet = reinterpret_cast<CircularBufferPacket *>(&m_data[write_offset]);
190186
packet->header.type = type;
191187
packet->header.timestamp = os::GetSystemTick();
192188
packet->header.size = size;
193189

194190
if (type != 0xff) {
195-
if (data && (size > 0)) {
196-
memcpy(&packet->data, data, size);
197-
} else {
198-
return -1;
191+
if (!(data && size)) {
192+
R_RETURN(-1);
199193
}
200-
}
201194

202-
u32 newOffset = this->writeOffset + size + sizeof(CircularBufferPacketHeader);
203-
if (newOffset > BLUETOOTH_BUFFER_SIZE) {
204-
return -1;
195+
memcpy(&packet->data, data, size);
205196
}
206197

207-
if (newOffset == BLUETOOTH_BUFFER_SIZE) {
208-
this->writeOffset = 0;
209-
} else {
210-
this->writeOffset = newOffset;
198+
u32 new_offset = write_offset + size + sizeof(CircularBufferPacketHeader);
199+
if (new_offset == CircularBuffer::BufferSize) {
200+
new_offset = 0;
201+
} else if (new_offset > CircularBuffer::BufferSize) {
202+
R_RETURN(-1);
211203
}
212204

213-
return 0;
205+
this->_setWriteOffset(new_offset);
206+
207+
R_SUCCEED();
214208
}
215209

216210
void CircularBuffer::_updateUtilization() {
217-
u32 newCapacity = this->isInitialized ? this->GetWriteableSize() : 0;
211+
u32 new_capacity = m_initialized ? this->GetWriteableSize() : 0;
218212

219-
if (this->size > newCapacity + 1000) {
220-
this->size = newCapacity;
213+
if (m_size > new_capacity + 1000) {
214+
m_size = new_capacity;
221215
}
222216
}
223217

224218
CircularBufferPacket *CircularBuffer::_read() {
225-
if (this->isInitialized) {
226-
CircularBufferPacket *packet;
227-
u32 newOffset;
228-
do {
229-
if (this->readOffset == this->writeOffset) {
230-
return nullptr;
231-
}
232-
233-
packet = reinterpret_cast<CircularBufferPacket *>(&this->data[this->readOffset]);
219+
while (m_initialized) {
220+
u32 read_offset = this->_getReadOffset();
221+
u32 write_offset = this->_getWriteOffset();
234222

235-
if (packet->header.type != 0xff) {
236-
return packet;
237-
}
223+
if (read_offset == write_offset) {
224+
break;
225+
}
238226

239-
if (!this->isInitialized) {
240-
return nullptr;
241-
}
227+
auto packet = reinterpret_cast<CircularBufferPacket *>(&m_data[read_offset]);
242228

243-
if (this->readOffset != this->writeOffset) {
244-
newOffset = this->readOffset + packet->header.size + sizeof(packet->header);
245-
if (newOffset >= BLUETOOTH_BUFFER_SIZE) {
246-
newOffset = 0;
247-
}
229+
if (packet->header.type != 0xff) {
230+
return packet;
231+
}
248232

249-
this->_setReadOffset(newOffset);
250-
}
233+
u32 new_offset = read_offset + packet->header.size + sizeof(packet->header);
234+
if (new_offset >= CircularBuffer::BufferSize) {
235+
new_offset = 0;
236+
}
251237

252-
} while (this->isInitialized);
253-
}
238+
this->_setReadOffset(new_offset);
239+
};
254240

255241
return nullptr;
256242
}

0 commit comments

Comments
 (0)