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 = nullptr;
45 static uint64_t last_timestamp_ms = 0;
46 
nfcsnoop_cb(const uint8_t * data,const size_t length,bool is_received,const uint64_t timestamp_us)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);
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 
nfcsnoop_compress(ringbuffer_t * rb_dst,ringbuffer_t * rb_src)76 static bool nfcsnoop_compress(ringbuffer_t* rb_dst, ringbuffer_t* rb_src) {
77   CHECK(rb_dst != nullptr);
78   CHECK(rb_src != nullptr);
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 
nfcsnoop_capture(const NFC_HDR * packet,bool is_received)117 void nfcsnoop_capture(const NFC_HDR* packet, bool is_received) {
118   struct timeval tv;
119   gettimeofday(&tv, nullptr);
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 
debug_nfcsnoop_init(void)132 void debug_nfcsnoop_init(void) {
133   if (buffer == nullptr) buffer = ringbuffer_init(NFCSNOOP_MEM_BUFFER_SIZE);
134 }
135 
debug_nfcsnoop_dump(int fd)136 void debug_nfcsnoop_dump(int fd) {
137   if (buffer == nullptr) {
138     dprintf(fd, "%s Nfcsnoop is not ready\n", __func__);
139     return;
140   }
141   ringbuffer_t* ringbuffer = ringbuffer_init(NFCSNOOP_MEM_BUFFER_SIZE);
142   if (ringbuffer == nullptr) {
143     dprintf(fd, "%s Unable to allocate memory for compression", __func__);
144     return;
145   }
146 
147   // Prepend preamble
148 
149   nfcsnooz_preamble_t preamble;
150   preamble.version = NFCSNOOZ_CURRENT_VERSION;
151   preamble.last_timestamp_ms = last_timestamp_ms;
152   ringbuffer_insert(ringbuffer, (uint8_t*)&preamble,
153                     sizeof(nfcsnooz_preamble_t));
154 
155   // Compress data
156 
157   uint8_t b64_in[3] = {0};
158   char b64_out[5] = {0};
159 
160   size_t line_length = 0;
161 
162   bool rc;
163   {
164     std::lock_guard<std::mutex> lock(buffer_mutex);
165     dprintf(fd, "--- BEGIN:NFCSNOOP_LOG_SUMMARY (%zu bytes in) ---\n",
166             ringbuffer_size(buffer));
167     rc = nfcsnoop_compress(ringbuffer, buffer);
168   }
169 
170   if (rc == false) {
171     dprintf(fd, "%s Log compression failed", __func__);
172     goto error;
173   }
174 
175   // Base64 encode & output
176 
177   while (ringbuffer_size(ringbuffer) > 0) {
178     size_t read = ringbuffer_pop(ringbuffer, b64_in, 3);
179     if (line_length >= MAX_LINE_LENGTH) {
180       dprintf(fd, "\n");
181       line_length = 0;
182     }
183     line_length += b64_ntop(b64_in, read, b64_out, 5);
184     dprintf(fd, "%s", b64_out);
185   }
186 
187   dprintf(fd, "\n--- END:NFCSNOOP_LOG_SUMMARY ---\n");
188 
189 error:
190   ringbuffer_free(ringbuffer);
191 }
192