1 /****************************************************************************** 2 * 3 * Copyright (C) 2017 Google Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #include <android-base/logging.h> 20 #include <resolv.h> 21 #include <zlib.h> 22 #include <mutex> 23 24 #include <ringbuffer.h> 25 26 #include "bt_types.h" 27 #include "include/debug_nfcsnoop.h" 28 #include "nfc_int.h" 29 30 #define USEC_PER_SEC 1000000ULL 31 32 // Total nfcsnoop memory log buffer size 33 #ifndef NFCSNOOP_MEM_BUFFER_SIZE 34 static const size_t NFCSNOOP_MEM_BUFFER_SIZE = (256 * 1024); 35 #endif 36 37 // Block size for copying buffers (for compression/encoding etc.) 38 static const size_t BLOCK_SIZE = 16384; 39 40 // Maximum line length in bugreport (should be multiple of 4 for base64 output) 41 static const uint8_t MAX_LINE_LENGTH = 128; 42 43 static std::mutex buffer_mutex; 44 static ringbuffer_t* buffer = NULL; 45 static uint64_t last_timestamp_ms = 0; 46 47 static void nfcsnoop_cb(const uint8_t* data, const size_t length, 48 bool is_received, const uint64_t timestamp_us) { 49 nfcsnooz_header_t header; 50 51 std::lock_guard<std::mutex> lock(buffer_mutex); 52 53 // Make room in the ring buffer 54 55 while (ringbuffer_available(buffer) < (length + sizeof(nfcsnooz_header_t))) { 56 ringbuffer_pop(buffer, (uint8_t*)&header, sizeof(nfcsnooz_header_t)); 57 ringbuffer_delete(buffer, header.length - 1); 58 } 59 60 // Insert data 61 header.length = length; 62 header.is_received = is_received ? 1 : 0; 63 64 uint64_t delta_time_ms = 0; 65 if (last_timestamp_ms) { 66 __builtin_sub_overflow(timestamp_us, last_timestamp_ms, &delta_time_ms); 67 } 68 header.delta_time_ms = delta_time_ms; 69 70 last_timestamp_ms = timestamp_us; 71 72 ringbuffer_insert(buffer, (uint8_t*)&header, sizeof(nfcsnooz_header_t)); 73 ringbuffer_insert(buffer, data, length); 74 } 75 76 static bool nfcsnoop_compress(ringbuffer_t* rb_dst, ringbuffer_t* rb_src) { 77 CHECK(rb_dst != NULL); 78 CHECK(rb_src != NULL); 79 80 z_stream zs; 81 zs.zalloc = Z_NULL; 82 zs.zfree = Z_NULL; 83 zs.opaque = Z_NULL; 84 85 if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) return false; 86 87 bool rc = true; 88 uint8_t block_src[BLOCK_SIZE]; 89 uint8_t block_dst[BLOCK_SIZE]; 90 91 const size_t num_blocks = 92 (ringbuffer_size(rb_src) + BLOCK_SIZE - 1) / BLOCK_SIZE; 93 for (size_t i = 0; i < num_blocks; ++i) { 94 zs.avail_in = 95 ringbuffer_peek(rb_src, i * BLOCK_SIZE, block_src, BLOCK_SIZE); 96 zs.next_in = block_src; 97 98 do { 99 zs.avail_out = BLOCK_SIZE; 100 zs.next_out = block_dst; 101 102 int err = deflate(&zs, (i == num_blocks - 1) ? Z_FINISH : Z_NO_FLUSH); 103 if (err == Z_STREAM_ERROR) { 104 rc = false; 105 break; 106 } 107 108 const size_t length = BLOCK_SIZE - zs.avail_out; 109 ringbuffer_insert(rb_dst, block_dst, length); 110 } while (zs.avail_out == 0); 111 } 112 113 deflateEnd(&zs); 114 return rc; 115 } 116 117 void nfcsnoop_capture(const NFC_HDR* packet, bool is_received) { 118 struct timeval tv; 119 gettimeofday(&tv, NULL); 120 uint64_t timestamp = static_cast<uint64_t>(tv.tv_sec) * USEC_PER_SEC + 121 static_cast<uint64_t>(tv.tv_usec); 122 uint8_t* p = (uint8_t*)(packet + 1) + packet->offset; 123 uint8_t mt = (*(p)&NCI_MT_MASK) >> NCI_MT_SHIFT; 124 125 if (mt == NCI_MT_DATA) { 126 nfcsnoop_cb(p, NCI_DATA_HDR_SIZE, is_received, timestamp); 127 } else if (packet->len > 2) { 128 nfcsnoop_cb(p, p[2] + NCI_MSG_HDR_SIZE, is_received, timestamp); 129 } 130 } 131 132 void debug_nfcsnoop_init(void) { 133 if (buffer == NULL) buffer = ringbuffer_init(NFCSNOOP_MEM_BUFFER_SIZE); 134 } 135 136 void debug_nfcsnoop_dump(int fd) { 137 ringbuffer_t* ringbuffer = ringbuffer_init(NFCSNOOP_MEM_BUFFER_SIZE); 138 if (ringbuffer == NULL) { 139 dprintf(fd, "%s Unable to allocate memory for compression", __func__); 140 return; 141 } 142 143 // Prepend preamble 144 145 nfcsnooz_preamble_t preamble; 146 preamble.version = NFCSNOOZ_CURRENT_VERSION; 147 preamble.last_timestamp_ms = last_timestamp_ms; 148 ringbuffer_insert(ringbuffer, (uint8_t*)&preamble, 149 sizeof(nfcsnooz_preamble_t)); 150 151 // Compress data 152 153 uint8_t b64_in[3] = {0}; 154 char b64_out[5] = {0}; 155 156 size_t line_length = 0; 157 158 bool rc; 159 { 160 std::lock_guard<std::mutex> lock(buffer_mutex); 161 dprintf(fd, "--- BEGIN:NFCSNOOP_LOG_SUMMARY (%zu bytes in) ---\n", 162 ringbuffer_size(buffer)); 163 rc = nfcsnoop_compress(ringbuffer, buffer); 164 } 165 166 if (rc == false) { 167 dprintf(fd, "%s Log compression failed", __func__); 168 goto error; 169 } 170 171 // Base64 encode & output 172 173 while (ringbuffer_size(ringbuffer) > 0) { 174 size_t read = ringbuffer_pop(ringbuffer, b64_in, 3); 175 if (line_length >= MAX_LINE_LENGTH) { 176 dprintf(fd, "\n"); 177 line_length = 0; 178 } 179 line_length += b64_ntop(b64_in, read, b64_out, 5); 180 dprintf(fd, "%s", b64_out); 181 } 182 183 dprintf(fd, "\n--- END:NFCSNOOP_LOG_SUMMARY ---\n"); 184 185 error: 186 ringbuffer_free(ringbuffer); 187 } 188