btif_debug_conn.c revision 89f5e411d9ef31436741288a2267e46dd744e273
1/******************************************************************************
2 *
3 *  Copyright (C) 2015 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 <stdio.h>
20#include <string.h>
21#include <time.h>
22
23#include "btif/include/btif_debug.h"
24#include "btif/include/btif_debug_conn.h"
25
26#define NUM_CONNECTION_EVENTS  16
27
28typedef struct conn_event_t {
29  uint64_t ts;
30  btif_debug_conn_state_t state;
31  bt_bdaddr_t bda;
32  tGATT_DISCONN_REASON disconnect_reason;
33} conn_event_t;
34
35static conn_event_t connection_events[NUM_CONNECTION_EVENTS];
36static uint8_t current_event = 0;
37
38static char *format_ts(const uint64_t ts, char *buffer, int len) {
39  const uint64_t ms = ts / 1000;
40  const time_t secs = ms / 1000;
41  struct tm *ptm = localtime(&secs);
42
43  strftime(buffer, len, "%m-%d %H:%M:%S.%%03u", ptm);
44  snprintf(buffer, len, buffer, (uint16_t)(ms % 1000));
45
46  return buffer;
47}
48
49static char *format_state(const btif_debug_conn_state_t state) {
50  switch (state) {
51    case BTIF_DEBUG_CONNECTED:
52      return "CONNECTED   ";
53    case BTIF_DEBUG_DISCONNECTED:
54      return "DISCONNECTED";
55  }
56  return "UNKNOWN";
57}
58
59static void next_event() {
60  ++current_event;
61  if (current_event == NUM_CONNECTION_EVENTS)
62    current_event = 0;
63}
64
65void btif_debug_conn_state(const bt_bdaddr_t bda, const btif_debug_conn_state_t state,
66    const tGATT_DISCONN_REASON disconnect_reason) {
67  next_event();
68
69  conn_event_t *evt = &connection_events[current_event];
70  evt->ts = btif_debug_ts();
71  evt->state = state;
72  evt->disconnect_reason = disconnect_reason;
73  memcpy(&evt->bda, &bda, sizeof(bt_bdaddr_t));
74}
75
76void btif_debug_conn_dump(int fd) {
77  const uint8_t current_event_local = current_event; // Cache to avoid threading issues
78  uint8_t dump_event = current_event_local;
79  char buffer[30] = {0};
80
81  dprintf(fd, "\nConnection Events:\n");
82  if (connection_events[dump_event].ts == 0)
83    dprintf(fd, "  None\n");
84
85  while (connection_events[dump_event].ts) {
86    conn_event_t *evt = &connection_events[dump_event];
87    dprintf(fd, "  %s %s %%s",
88            format_ts(evt->ts, buffer, sizeof(buffer)),
89            format_state(evt->state),
90            bdaddr_to_string(&evt->bda, buffer, sizeof(buffer))
91        );
92    if (evt->state == BTIF_DEBUG_DISCONNECTED) {
93        dprintf(fd," reason=%d\n", evt->disconnect_reason);
94    } else {
95        dprintf(fd,"\n");
96    }
97
98    // Go to previous event; wrap if needed
99    if (dump_event > 0)
100      --dump_event;
101    else
102      dump_event = NUM_CONNECTION_EVENTS - 1;
103
104    // Check if we dumped all events
105    if (dump_event == current_event_local)
106      break;
107  }
108}
109