Home | History | Annotate | Download | only in adaptation
      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 <assert.h>
     20 #include <resolv.h>
     21 #include <zlib.h>
     22 #include <mutex>
     23 
     24 #include "bt_types.h"
     25 #include "include/debug_nfcsnoop.h"
     26 #include "include/ringbuffer.h"
     27 #include "nfc_int.h"
     28 #include "nfc_types.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   header.delta_time_ms =
     64       last_timestamp_ms ? timestamp_us - last_timestamp_ms : 0;
     65   last_timestamp_ms = timestamp_us;
     66 
     67   ringbuffer_insert(buffer, (uint8_t*)&header, sizeof(nfcsnooz_header_t));
     68   ringbuffer_insert(buffer, data, length);
     69 }
     70 
     71 static bool nfcsnoop_compress(ringbuffer_t* rb_dst, ringbuffer_t* rb_src) {
     72   assert(rb_dst != NULL);
     73   assert(rb_src != NULL);
     74 
     75   z_stream zs;
     76   zs.zalloc = Z_NULL;
     77   zs.zfree = Z_NULL;
     78   zs.opaque = Z_NULL;
     79 
     80   if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) return false;
     81 
     82   bool rc = true;
     83   uint8_t block_src[BLOCK_SIZE];
     84   uint8_t block_dst[BLOCK_SIZE];
     85 
     86   const size_t num_blocks =
     87       (ringbuffer_size(rb_src) + BLOCK_SIZE - 1) / BLOCK_SIZE;
     88   for (size_t i = 0; i < num_blocks; ++i) {
     89     zs.avail_in =
     90         ringbuffer_peek(rb_src, i * BLOCK_SIZE, block_src, BLOCK_SIZE);
     91     zs.next_in = block_src;
     92 
     93     do {
     94       zs.avail_out = BLOCK_SIZE;
     95       zs.next_out = block_dst;
     96 
     97       int err = deflate(&zs, (i == num_blocks - 1) ? Z_FINISH : Z_NO_FLUSH);
     98       if (err == Z_STREAM_ERROR) {
     99         rc = false;
    100         break;
    101       }
    102 
    103       const size_t length = BLOCK_SIZE - zs.avail_out;
    104       ringbuffer_insert(rb_dst, block_dst, length);
    105     } while (zs.avail_out == 0);
    106   }
    107 
    108   deflateEnd(&zs);
    109   return rc;
    110 }
    111 
    112 void nfcsnoop_capture(const NFC_HDR* packet, bool is_received) {
    113   struct timeval tv;
    114   gettimeofday(&tv, NULL);
    115   uint64_t timestamp = static_cast<uint64_t>(tv.tv_sec) * USEC_PER_SEC +
    116                        static_cast<uint64_t>(tv.tv_usec);
    117   uint8_t* p = (uint8_t*)(packet + 1) + packet->offset;
    118   uint8_t mt = (*(p)&NCI_MT_MASK) >> NCI_MT_SHIFT;
    119 
    120   if (mt == NCI_MT_DATA) {
    121     nfcsnoop_cb(p, NCI_DATA_HDR_SIZE, is_received, timestamp);
    122   } else if (packet->len > 2) {
    123     nfcsnoop_cb(p, p[2] + NCI_MSG_HDR_SIZE, is_received, timestamp);
    124   }
    125 }
    126 
    127 void debug_nfcsnoop_init(void) {
    128   if (buffer == NULL) buffer = ringbuffer_init(NFCSNOOP_MEM_BUFFER_SIZE);
    129 }
    130 
    131 void debug_nfcsnoop_dump(int fd) {
    132   ringbuffer_t* ringbuffer = ringbuffer_init(NFCSNOOP_MEM_BUFFER_SIZE);
    133   if (ringbuffer == NULL) {
    134     dprintf(fd, "%s Unable to allocate memory for compression", __func__);
    135     return;
    136   }
    137 
    138   // Prepend preamble
    139 
    140   nfcsnooz_preamble_t preamble;
    141   preamble.version = NFCSNOOZ_CURRENT_VERSION;
    142   preamble.last_timestamp_ms = last_timestamp_ms;
    143   ringbuffer_insert(ringbuffer, (uint8_t*)&preamble,
    144                     sizeof(nfcsnooz_preamble_t));
    145 
    146   // Compress data
    147 
    148   uint8_t b64_in[3] = {0};
    149   char b64_out[5] = {0};
    150 
    151   size_t line_length = 0;
    152 
    153   bool rc;
    154   {
    155     std::lock_guard<std::mutex> lock(buffer_mutex);
    156     dprintf(fd, "--- BEGIN:NFCSNOOP_LOG_SUMMARY (%zu bytes in) ---\n",
    157             ringbuffer_size(buffer));
    158     rc = nfcsnoop_compress(ringbuffer, buffer);
    159   }
    160 
    161   if (rc == false) {
    162     dprintf(fd, "%s Log compression failed", __func__);
    163     goto error;
    164   }
    165 
    166   // Base64 encode & output
    167 
    168   while (ringbuffer_size(ringbuffer) > 0) {
    169     size_t read = ringbuffer_pop(ringbuffer, b64_in, 3);
    170     if (line_length >= MAX_LINE_LENGTH) {
    171       dprintf(fd, "\n");
    172       line_length = 0;
    173     }
    174     line_length += b64_ntop(b64_in, read, b64_out, 5);
    175     dprintf(fd, "%s", b64_out);
    176   }
    177 
    178   dprintf(fd, "\n--- END:NFCSNOOP_LOG_SUMMARY ---\n");
    179 
    180 error:
    181   ringbuffer_free(ringbuffer);
    182 }
    183