1 /******************************************************************************
3 * Copyright (C) 2016 Google Inc.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
24 #include "btcore/include/bdaddr.h"
25 #include "btif/include/btif_debug.h"
26 #include "btif/include/btif_debug_l2c.h"
28 #define NUM_UPDATE_REQUESTS 5
29 #define NUM_UPDATE_RESPONSES 5
31 #define INTERVAL_1_25_MS_MULTIPLIER 1.25f
32 #define TIMEOUT_10_MS_MULTIPLIER 10
35 BTIF_DEBUG_CONNECTION_UPDATE_REQUEST,
36 BTIF_DEBUG_CONNECTION_UPDATE_RESPONSE,
37 } btif_debug_ble_conn_update_t;
39 /* Shared Connection update record for both request and response. */
40 typedef struct ble_conn_update_t {
41 uint64_t timestamp_ms;
43 btif_debug_ble_conn_update_t type;
44 uint8_t status; /* Not populated for request. */
45 uint16_t min_interval; /* Not populated for response. */
46 uint16_t max_interval;
51 static int update_request_index;
52 static int update_response_index;
53 static ble_conn_update_t last_ble_conn_update_requests[NUM_UPDATE_REQUESTS];
54 static ble_conn_update_t last_ble_conn_update_responses[NUM_UPDATE_RESPONSES];
56 static int dump_connection_update(int fd, const ble_conn_update_t *update) {
57 if (!update || update->timestamp_ms == 0) {
61 /* Format timestamp */
62 const uint64_t msecs = update->timestamp_ms / 1000;
63 const time_t secs = msecs / 1000;
64 struct tm *ptm = localtime(&secs);
65 char time_buf[20] = {0};
66 strftime(time_buf, sizeof(time_buf), "%m-%d %H:%M:%S", ptm);
67 snprintf(time_buf, sizeof(time_buf), "%s.%03u", time_buf,
68 (uint16_t)(msecs % 1000));
71 char addr_buf[18] = {0};
72 bdaddr_to_string(&update->bda, addr_buf, sizeof(addr_buf));
74 if (update->type == BTIF_DEBUG_CONNECTION_UPDATE_REQUEST) {
76 " %s %s min interval=%d (%.2fms) max interval=%d (%.2fms) "
77 "latency parameter=%d timeout multiplier=%d (%dms)\n",
78 time_buf, addr_buf, update->min_interval,
79 (float)update->min_interval * INTERVAL_1_25_MS_MULTIPLIER,
81 (float)update->max_interval * INTERVAL_1_25_MS_MULTIPLIER,
82 update->latency, update->timeout,
83 update->timeout * TIMEOUT_10_MS_MULTIPLIER);
86 " %s %s status=%d interval=%d (%.2fms) latency parameter=%d "
87 "timeout multiplier=%d (%dms)\n", time_buf,
88 addr_buf, update->status, update->max_interval,
89 (float)update->max_interval * INTERVAL_1_25_MS_MULTIPLIER,
90 update->latency, update->timeout,
91 update->timeout * TIMEOUT_10_MS_MULTIPLIER);
97 static void record_connection_update(bt_bdaddr_t bda, uint8_t status,
98 uint16_t min_interval, uint16_t max_interval, uint16_t latency,
99 uint16_t timeout, btif_debug_ble_conn_update_t type,
100 ble_conn_update_t* update) {
102 memcpy(&update->bda, &bda, sizeof(bt_bdaddr_t));
104 update->timestamp_ms = btif_debug_ts();
105 update->min_interval = min_interval;
106 update->max_interval = max_interval;
107 update->latency = latency;
108 update->timeout = timeout;
112 void btif_debug_ble_connection_update_request(bt_bdaddr_t bda,
113 uint16_t min_interval, uint16_t max_interval, uint16_t slave_latency_param,
114 uint16_t timeout_multiplier) {
115 ble_conn_update_t *request =
116 &last_ble_conn_update_requests[update_request_index];
118 record_connection_update(bda, 0, min_interval, max_interval, slave_latency_param,
119 timeout_multiplier, BTIF_DEBUG_CONNECTION_UPDATE_REQUEST, request);
121 update_request_index = (update_request_index == NUM_UPDATE_REQUESTS - 1) ?
122 0 : update_request_index + 1;
125 void btif_debug_ble_connection_update_response(bt_bdaddr_t bda, uint8_t status,
126 uint16_t interval, uint16_t slave_latency_param,
127 uint16_t timeout_multiplier) {
128 ble_conn_update_t *response =
129 &last_ble_conn_update_responses[update_response_index];
131 record_connection_update(bda, status, 0, interval, slave_latency_param,
132 timeout_multiplier, BTIF_DEBUG_CONNECTION_UPDATE_RESPONSE, response);
134 update_response_index = (update_response_index == NUM_UPDATE_RESPONSES - 1) ?
135 0 : update_response_index + 1;
138 void btif_debug_l2c_dump(int fd) {
139 dprintf(fd, "\nLE Connection Parameter Updates:\n");
142 dprintf(fd, " Last %d Request(s):\n", NUM_UPDATE_REQUESTS);
143 for (i = 0; i < NUM_UPDATE_REQUESTS; ++i) {
144 if (dump_connection_update(fd, &last_ble_conn_update_requests[i]) < 0 &&
146 dprintf(fd, " None\n");
151 dprintf(fd, "\n Last %d Response(s):\n", NUM_UPDATE_RESPONSES);
152 for (i = 0; i < NUM_UPDATE_RESPONSES; ++i) {
153 if (dump_connection_update(fd, &last_ble_conn_update_responses[i]) < 0 &&
155 dprintf(fd, " None\n");