static void btsnoop_cb(const uint16_t type, const uint8_t *data, const size_t length) {
btsnooz_header_t header;
+ // Only log packet content for HCI commands and events (privacy).
+ size_t included_length = 0;
+ switch (type) {
+ case BT_EVT_TO_LM_HCI_CMD:
+ included_length = length;
+ break;
+ case BT_EVT_TO_BTU_HCI_EVT:
+ included_length = length;
+ break;
+ case BT_EVT_TO_LM_HCI_ACL:
+ case BT_EVT_TO_BTU_HCI_ACL:
+ included_length = 4;
+ break;
+ case BT_EVT_TO_LM_HCI_SCO:
+ case BT_EVT_TO_BTU_HCI_SCO:
+ included_length = 2;
+ break;
+ }
+
// Make room in the ring buffer
- while (ringbuffer_available(buffer) < (length + sizeof(btsnooz_header_t))) {
+ while (ringbuffer_available(buffer) < (included_length + sizeof(btsnooz_header_t))) {
ringbuffer_pop(buffer, (uint8_t *)&header, sizeof(btsnooz_header_t));
ringbuffer_delete(buffer, header.length - 1);
}
const uint64_t now = btif_debug_ts();
header.type = REDUCE_HCI_TYPE_TO_SIGNIFICANT_BITS(type);
- header.length = length;
+ header.length = included_length + 1; // +1 for type byte
+ header.packet_length = length + 1; // +1 for type byte.
header.delta_time_ms = last_timestamp_ms ? now - last_timestamp_ms : 0;
last_timestamp_ms = now;
ringbuffer_insert(buffer, (uint8_t *)&header, sizeof(btsnooz_header_t));
- ringbuffer_insert(buffer, data, length - 1);
+ ringbuffer_insert(buffer, data, included_length);
}
static bool btsnoop_compress(ringbuffer_t *rb_dst, ringbuffer_t *rb_src) {
assert(rb_dst != NULL);
assert(rb_src != NULL);
- z_stream zs = {0};
+ z_stream zs = {.zalloc = Z_NULL, .zfree = Z_NULL, .opaque = Z_NULL};
if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK)
return false;
uint8_t block_src[BLOCK_SIZE];
uint8_t block_dst[BLOCK_SIZE];
- while (ringbuffer_size(rb_src) > 0) {
- zs.avail_in = ringbuffer_pop(rb_src, block_src, BLOCK_SIZE);
+ const size_t num_blocks = (ringbuffer_size(rb_src) + BLOCK_SIZE - 1) / BLOCK_SIZE;
+ for (size_t i = 0; i < num_blocks; ++i) {
+ zs.avail_in = ringbuffer_peek(rb_src, i * BLOCK_SIZE, block_src, BLOCK_SIZE);
zs.next_in = block_src;
do {
zs.avail_out = BLOCK_SIZE;
zs.next_out = block_dst;
- int err = deflate(&zs, ringbuffer_size(rb_src) == 0 ? Z_FINISH : Z_NO_FLUSH);
+ int err = deflate(&zs, (i == num_blocks - 1) ? Z_FINISH : Z_NO_FLUSH);
if (err == Z_STREAM_ERROR) {
rc = false;
break;
}
void btif_debug_btsnoop_dump(int fd) {
- dprintf(fd, "\n--- BEGIN:BTSNOOP_LOG_SUMMARY (%zu bytes in) ---\n", ringbuffer_size(buffer));
+ dprintf(fd, "--- BEGIN:BTSNOOP_LOG_SUMMARY (%zu bytes in) ---\n", ringbuffer_size(buffer));
ringbuffer_t *ringbuffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
if (ringbuffer == NULL) {
uint8_t b64_in[3] = {0};
char b64_out[5] = {0};
- size_t i = sizeof(btsnooz_preamble_t);
+ size_t line_length = 0;
while (ringbuffer_size(ringbuffer) > 0) {
size_t read = ringbuffer_pop(ringbuffer, b64_in, 3);
- if (i > 0 && i % MAX_LINE_LENGTH == 0)
+ if (line_length >= MAX_LINE_LENGTH) {
dprintf(fd, "\n");
- i += b64_ntop(b64_in, read, b64_out, 5);
+ line_length = 0;
+ }
+ line_length += b64_ntop(b64_in, read, b64_out, 5);
dprintf(fd, "%s", b64_out);
}
- dprintf(fd, "\n--- END:BTSNOOP_LOG_SUMMARY (%zu bytes out) ---\n", i);
+ dprintf(fd, "\n--- END:BTSNOOP_LOG_SUMMARY ---\n");
error:
ringbuffer_free(ringbuffer);