-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconsole_report_buffer.cc
61 lines (46 loc) · 1.68 KB
/
console_report_buffer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//---------------------------------------------------------------------------
#include "console_report_buffer.h"
#include "usb_descriptors.h"
#include <string.h>
//---------------------------------------------------------------------------
ConsoleReportBuffer ConsoleReportBuffer::instance;
//---------------------------------------------------------------------------
ConsoleReportBuffer::ConsoleReportBuffer() : reportBuffer(ITF_NUM_CONSOLE) {}
void ConsoleReportBuffer::SendData(const uint8_t *data, size_t length) {
// Fill up the previous buffer if it's not empty.
if (bufferSize != 0) {
const int remaining = MAX_BUFFER_SIZE - bufferSize;
const int bytesToAdd = length > remaining ? remaining : length;
memcpy(buffer + bufferSize, data, bytesToAdd);
bufferSize += bytesToAdd;
data += bytesToAdd;
length -= bytesToAdd;
if (bufferSize == MAX_BUFFER_SIZE) {
reportBuffer.SendReport(0, buffer, MAX_BUFFER_SIZE);
bufferSize = 0;
}
if (length == 0) {
return;
}
}
// Send all full length requests.
while (length >= MAX_BUFFER_SIZE) {
reportBuffer.SendReport(0, data, MAX_BUFFER_SIZE);
data += MAX_BUFFER_SIZE;
length -= MAX_BUFFER_SIZE;
}
if (length > 0) {
memcpy(buffer, data, length);
bufferSize = length;
}
}
//---------------------------------------------------------------------------
void ConsoleReportBuffer::Flush() {
if (bufferSize > 0) {
const int remaining = MAX_BUFFER_SIZE - bufferSize;
memset(buffer + bufferSize, 0, remaining);
reportBuffer.SendReport(0, buffer, MAX_BUFFER_SIZE);
bufferSize = 0;
}
}
//---------------------------------------------------------------------------