summaryrefslogtreecommitdiff
path: root/osi
diff options
context:
space:
mode:
authorSharvil Nanavati <sharvil@google.com>2016-02-29 15:50:49 -0800
committerAndre Eisenbach <eisenbach@google.com>2016-03-25 13:19:06 -0700
commitf3758bdc1f38dcd237f4a39c6af3d4faf0f27832 (patch)
treed60408417a30c172bf5f584489ea120ef0ad71b2 /osi
parent5034d67fde55ccb8ac790ca76e7563f756261db5 (diff)
downloadandroid-system-bt-f3758bdc1f38dcd237f4a39c6af3d4faf0f27832.tar.gz
android-system-bt-f3758bdc1f38dcd237f4a39c6af3d4faf0f27832.tar.xz
Don't clear the btsnoop log ringbuffer on compress.
Change-Id: I0bf4279104e87b151553eba913491d934f3cde41
Diffstat (limited to 'osi')
-rw-r--r--osi/include/ringbuffer.h9
-rw-r--r--osi/src/ringbuffer.c15
-rw-r--r--osi/test/ringbuffer_test.cpp8
3 files changed, 17 insertions, 15 deletions
diff --git a/osi/include/ringbuffer.h b/osi/include/ringbuffer.h
index 8446d6c..b81e742 100644
--- a/osi/include/ringbuffer.h
+++ b/osi/include/ringbuffer.h
@@ -47,10 +47,11 @@ size_t ringbuffer_size(const ringbuffer_t *rb);
// is full.
size_t ringbuffer_insert(ringbuffer_t *rb, const uint8_t *p, size_t length);
-// Peek |length| number of bytes from the ringbuffer into the buffer |p|
-// Return the actual number of bytes peeked. Can be less than |length| if
-// there is less than |length| data available.
-size_t ringbuffer_peek(const ringbuffer_t *rb, uint8_t *p, size_t length);
+// Peek |length| number of bytes from the ringbuffer, starting at |offset|,
+// into the buffer |p|. Return the actual number of bytes peeked. Can be less
+// than |length| if there is less than |length| data available. |offset| must
+// be non-negative.
+size_t ringbuffer_peek(const ringbuffer_t *rb, off_t offset, uint8_t *p, size_t length);
// Does the same as |ringbuffer_peek|, but also advances the ring buffer head
size_t ringbuffer_pop(ringbuffer_t *rb, uint8_t *p, size_t length);
diff --git a/osi/src/ringbuffer.c b/osi/src/ringbuffer.c
index aeb413c..84d6043 100644
--- a/osi/src/ringbuffer.c
+++ b/osi/src/ringbuffer.c
@@ -87,28 +87,29 @@ size_t ringbuffer_delete(ringbuffer_t *rb, size_t length) {
return length;
}
-size_t ringbuffer_peek(const ringbuffer_t *rb, uint8_t *p, size_t length) {
+size_t ringbuffer_peek(const ringbuffer_t *rb, off_t offset, uint8_t *p, size_t length) {
assert(rb);
assert(p);
+ assert(offset >= 0);
+ assert((size_t)offset <= ringbuffer_size(rb));
- uint8_t *b = rb->head;
- size_t copied = 0;
+ uint8_t *b = ((rb->head - rb->base + offset) % rb->total) + rb->base;
+ const size_t bytes_to_copy = (offset + length > ringbuffer_size(rb)) ? ringbuffer_size(rb) - offset : length;
- while (copied < length && copied < ringbuffer_size(rb)) {
+ for (size_t copied = 0; copied < bytes_to_copy; ++copied) {
*p++ = *b++;
if (b >= (rb->base + rb->total))
b = rb->base;
- ++copied;
}
- return copied;
+ return bytes_to_copy;
}
size_t ringbuffer_pop(ringbuffer_t *rb, uint8_t *p, size_t length) {
assert(rb);
assert(p);
- const size_t copied = ringbuffer_peek(rb, p, length);
+ const size_t copied = ringbuffer_peek(rb, 0, p, length);
rb->head += copied;
if (rb->head >= (rb->base + rb->total))
rb->head -= rb->total;
diff --git a/osi/test/ringbuffer_test.cpp b/osi/test/ringbuffer_test.cpp
index af007ac..8dd5bdc 100644
--- a/osi/test/ringbuffer_test.cpp
+++ b/osi/test/ringbuffer_test.cpp
@@ -22,7 +22,7 @@ TEST(RingbufferTest, test_insert_basic) {
EXPECT_EQ((size_t)6, ringbuffer_available(rb));
uint8_t peek[10] = {0};
- size_t peeked = ringbuffer_peek(rb, peek, 10);
+ size_t peeked = ringbuffer_peek(rb, 0, peek, 10);
EXPECT_EQ((size_t)10, ringbuffer_size(rb)); // Ensure size doesn't change
EXPECT_EQ((size_t)6, ringbuffer_available(rb));
EXPECT_EQ((size_t)10, peeked);
@@ -48,7 +48,7 @@ TEST(RingbufferTest, test_insert_full) {
EXPECT_EQ((size_t)0, ringbuffer_available(rb));
EXPECT_EQ((size_t)5, ringbuffer_size(rb));
- size_t peeked = ringbuffer_peek(rb, peek, 5);
+ size_t peeked = ringbuffer_peek(rb, 0, peek, 5);
EXPECT_EQ((size_t)5, peeked);
EXPECT_EQ((size_t)0, ringbuffer_available(rb));
EXPECT_EQ((size_t)5, ringbuffer_size(rb));
@@ -78,7 +78,7 @@ TEST(RingbufferTest, test_multi_insert_delete) {
uint8_t content[] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB};
uint8_t peek[16] = {0};
- size_t peeked = ringbuffer_peek(rb, peek, 16);
+ size_t peeked = ringbuffer_peek(rb, 0, peek, 16);
EXPECT_EQ((size_t)13, peeked);
ASSERT_TRUE(0 == memcmp(content, peek, peeked));
@@ -96,7 +96,7 @@ TEST(RingbufferTest, test_multi_insert_delete) {
EXPECT_EQ((size_t)14, ringbuffer_size(rb));
uint8_t content2[] = {0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xCC, 0xCC};
- peeked = ringbuffer_peek(rb, peek, 7);
+ peeked = ringbuffer_peek(rb, 0, peek, 7);
EXPECT_EQ((size_t)7, peeked);
ASSERT_TRUE(0 == memcmp(content2, peek, peeked));