summaryrefslogtreecommitdiffstats
path: root/hci/src/btsnoop.c
blob: 4dc8192a4174eb1d61287577d4396afb3946d0f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/******************************************************************************
 *
 *  Copyright (C) 2014 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#define LOG_TAG "bt_snoop"

#include <arpa/inet.h>
#include <assert.h>
#include <cutils/properties.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <unistd.h>

#include "hci/include/btsnoop.h"
#include "hci/include/btsnoop_mem.h"
#include "bt_types.h"
#include "hci_layer.h"
#include "osi/include/log.h"
#include "stack_config.h"

typedef enum {
  kCommandPacket = 1,
  kAclPacket = 2,
  kScoPacket = 3,
  kEventPacket = 4
} packet_type_t;

// Epoch in microseconds since 01/01/0000.
static const uint64_t BTSNOOP_EPOCH_DELTA = 0x00dcddb30f2f8000ULL;

static const stack_config_t *stack_config;
extern int client_socket_btsnoop;
static long int gmt_offset;
#define USEC_PER_SEC 1000000L
bool hci_ext_dump_enabled = false;  /* External BT snoop */
/* snoop config from the config file, required for userdebug
   build where snoop is enabled by default.
   power/perf measurements need the snoop to be disabled.
*/
bool btsnoop_conf_from_file = false;

static int logfile_fd = INVALID_FD;
static bool module_started;
static bool is_logging;
static bool logging_enabled_via_api;

// TODO(zachoverflow): merge btsnoop and btsnoop_net together
void btsnoop_net_open();
void btsnoop_net_close();
void btsnoop_net_write(const void *data, size_t length);

static void btsnoop_write_packet(packet_type_t type, const uint8_t *packet, bool is_received);
static void update_logging();

// Module lifecycle functions

static future_t *start_up(void) {
  time_t t = time(NULL);
  struct tm tm_cur = {0};

  localtime_r (&t, &tm_cur);
  LOG_INFO("Time GMT offset %ld\n", tm_cur.tm_gmtoff);
  gmt_offset = tm_cur.tm_gmtoff;

  module_started = true;
  stack_config->get_btsnoop_ext_options(&hci_ext_dump_enabled, &btsnoop_conf_from_file);
#if (BTSNOOP_DEFAULT == TRUE)
  if (btsnoop_conf_from_file == false) {
    hci_ext_dump_enabled = true;
  }
#endif
  update_logging();

  return NULL;
}

static future_t *shut_down(void) {
  module_started = false;
  if (hci_ext_dump_enabled == true) {
    property_set("bluetooth.startbtsnoop", "false");
  }
  update_logging();

  return NULL;
}

const module_t btsnoop_module = {
  .name = BTSNOOP_MODULE,
  .init = NULL,
  .start_up = start_up,
  .shut_down = shut_down,
  .clean_up = NULL,
  .dependencies = {
    STACK_CONFIG_MODULE,
    NULL
  }
};

// Interface functions

static void set_api_wants_to_log(bool value) {
  logging_enabled_via_api = value;
  update_logging();
}

static void capture(const BT_HDR *buffer, bool is_received) {
  const uint8_t *p = buffer->data + buffer->offset;

  btsnoop_mem_capture(buffer);

  if (logfile_fd == INVALID_FD)
    return;

  switch (buffer->event & MSG_EVT_MASK) {
    case MSG_HC_TO_STACK_HCI_EVT:
      btsnoop_write_packet(kEventPacket, p, false);
      break;
    case MSG_HC_TO_STACK_HCI_ACL:
    case MSG_STACK_TO_HC_HCI_ACL:
      btsnoop_write_packet(kAclPacket, p, is_received);
      break;
    case MSG_HC_TO_STACK_HCI_SCO:
    case MSG_STACK_TO_HC_HCI_SCO:
      btsnoop_write_packet(kScoPacket, p, is_received);
      break;
    case MSG_STACK_TO_HC_HCI_CMD:
      btsnoop_write_packet(kCommandPacket, p, true);
      break;
  }
}

static const btsnoop_t interface = {
  set_api_wants_to_log,
  capture
};

const btsnoop_t *btsnoop_get_interface() {
  stack_config = stack_config_get_interface();
  return &interface;
}

// Internal functions

static uint64_t btsnoop_timestamp(void) {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  tv.tv_sec += gmt_offset;

  // Timestamp is in microseconds.
  uint64_t timestamp = tv.tv_sec * 1000 * 1000LL;
  timestamp += tv.tv_usec;
  timestamp += BTSNOOP_EPOCH_DELTA;
  return timestamp;
}

static void update_logging() {
  bool should_log = module_started &&
    (logging_enabled_via_api || stack_config->get_btsnoop_turned_on() || hci_ext_dump_enabled);

  if (should_log == is_logging)
    return;

  is_logging = should_log;
  if (should_log) {
    btsnoop_net_open();
    if (hci_ext_dump_enabled == true) {
      property_set("bluetooth.startbtsnoop", "true");
    }
    const char *log_path = stack_config->get_btsnoop_log_path();

    // Save the old log if configured to do so
    if (stack_config->get_btsnoop_should_save_last()) {
      char last_log_path[PATH_MAX];
      snprintf(last_log_path, PATH_MAX, "%s.%llu", log_path, btsnoop_timestamp());
      if (!rename(log_path, last_log_path) && errno != ENOENT)
        LOG_ERROR("%s unable to rename '%s' to '%s': %s", __func__, log_path, last_log_path, strerror(errno));
    }

    logfile_fd = TEMP_FAILURE_RETRY(open(log_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH));
    if (logfile_fd == INVALID_FD) {
      LOG_ERROR("%s unable to open '%s': %s", __func__, log_path, strerror(errno));
      btsnoop_net_close();
      is_logging = false;
      return;
    }

    TEMP_FAILURE_RETRY(write(logfile_fd, "btsnoop\0\0\0\0\1\0\0\x3\xea", 16));
  } else {
    if (logfile_fd != INVALID_FD)
      close(logfile_fd);

    logfile_fd = INVALID_FD;
    btsnoop_net_close();
  }
}

static void btsnoop_write(const void *data, size_t length) {
  if (client_socket_btsnoop != -1) {
    btsnoop_net_write(data, length);
    /* skip writing to file if external client is connected*/
    return;
  }

  if (logfile_fd != INVALID_FD)
    TEMP_FAILURE_RETRY(write(logfile_fd, data, length));
}

static uint64_t time_now_us() {
    struct timespec ts_now;
    clock_gettime(CLOCK_BOOTTIME, &ts_now);
    return ((uint64_t)ts_now.tv_sec * USEC_PER_SEC) + ((uint64_t)ts_now.tv_nsec / 1000);
}

static void btsnoop_write_packet(packet_type_t type, const uint8_t *packet, bool is_received) {
  int length_he = 0;
  int length;
  int flags;
  int drops = 0;
  struct pollfd pfd;
#ifdef DEBUG_SNOOP
  uint64_t ts_begin;
  uint64_t ts_end, ts_diff;
#endif
  uint8_t snoop_buf[1200] = {0};
  uint32_t offset = 0;

  switch (type) {
    case kCommandPacket:
      length_he = packet[2] + 4;
      flags = 2;
      break;
    case kAclPacket:
      length_he = (packet[3] << 8) + packet[2] + 5;
      flags = is_received;
      break;
    case kScoPacket:
      length_he = packet[2] + 4;
      flags = is_received;
      break;
    case kEventPacket:
      length_he = packet[1] + 3;
      flags = 3;
      break;
  }

  uint64_t timestamp = btsnoop_timestamp();
  uint32_t time_hi = timestamp >> 32;
  uint32_t time_lo = timestamp & 0xFFFFFFFF;

  length = htonl(length_he);
  flags = htonl(flags);
  drops = htonl(drops);
  time_hi = htonl(time_hi);
  time_lo = htonl(time_lo);

  /* store the length in both original and included fields */
  memcpy(snoop_buf + offset, &length, 4);
  offset += 4;
  memcpy(snoop_buf + offset, &length, 4);
  offset += 4;

  /* flags:  */
  memcpy(snoop_buf + offset, &flags, 4);
  offset += 4;

  /* drops: none */
  memcpy(snoop_buf + offset, &drops, 4);
  offset += 4;

  /* time */
  memcpy(snoop_buf + offset, &time_hi, 4);
  offset += 4;
  memcpy(snoop_buf + offset, &time_lo, 4);
  offset = offset + 4;

  snoop_buf[offset] = type;
  offset += 1;
  memcpy(snoop_buf + offset, packet, length_he - 1);

  if (client_socket_btsnoop != -1) {
    pfd.fd = client_socket_btsnoop;
    pfd.events = POLLOUT;
#ifdef DEBUG_SNOOP
    ts_begin = time_now_us();
#endif

    if (poll(&pfd, 1, 10) == 0) {
      LOG_ERROR("btsnoop poll : Taking more than 10 ms : skip dump");
#ifdef DEBUG_SNOOP
      ts_end = time_now_us();
      ts_diff = ts_end - ts_begin;
      if (ts_diff > 10000) {
        LOG_ERROR("btsnoop poll T/O : took more time %08lld us", ts_diff);
      }
#endif
      return;
    }

#ifdef DEBUG_SNOOP
    ts_end = time_now_us();
    ts_diff = ts_end - ts_begin;
    if (ts_diff > 10000) {
      LOG_ERROR("btsnoop poll : took more time %08lld us", ts_diff);
    }
#endif
  }
#ifdef DEBUG_SNOOP
  ts_begin = time_now_us();
#endif

  btsnoop_write(snoop_buf, offset + length_he - 1);

#ifdef DEBUG_SNOOP
  ts_end = time_now_us();
  ts_diff = ts_end - ts_begin;
  if (ts_diff > 10000) {
    LOG_ERROR("btsnoop write : Write took more time %08lld us", ts_diff);
  }
#endif
}