1 /******************************************************************************
3 * Copyright (C) 2014 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 ******************************************************************************/
19 #define LOG_TAG "bt_snoop"
21 #include <arpa/inet.h>
26 #include <netinet/in.h>
36 #include "hci/include/btsnoop.h"
37 #include "hci/include/btsnoop_mem.h"
38 #include "hci_layer.h"
39 #include "osi/include/log.h"
40 #include "stack_config.h"
49 // Epoch in microseconds since 01/01/0000.
50 static const uint64_t BTSNOOP_EPOCH_DELTA = 0x00dcddb30f2f8000ULL;
52 static const stack_config_t *stack_config;
54 static int logfile_fd = INVALID_FD;
55 static bool module_started;
56 static bool is_logging;
57 static bool logging_enabled_via_api;
59 // TODO(zachoverflow): merge btsnoop and btsnoop_net together
60 void btsnoop_net_open();
61 void btsnoop_net_close();
62 void btsnoop_net_write(const void *data, size_t length);
64 static void btsnoop_write_packet(packet_type_t type, const uint8_t *packet, bool is_received);
65 static void update_logging();
67 // Module lifecycle functions
69 static future_t *start_up(void) {
70 module_started = true;
76 static future_t *shut_down(void) {
77 module_started = false;
83 EXPORT_SYMBOL const module_t btsnoop_module = {
84 .name = BTSNOOP_MODULE,
87 .shut_down = shut_down,
95 // Interface functions
97 static void set_api_wants_to_log(bool value) {
98 logging_enabled_via_api = value;
102 static void capture(const BT_HDR *buffer, bool is_received) {
103 const uint8_t *p = buffer->data + buffer->offset;
105 btsnoop_mem_capture(buffer);
107 if (logfile_fd == INVALID_FD)
110 switch (buffer->event & MSG_EVT_MASK) {
111 case MSG_HC_TO_STACK_HCI_EVT:
112 btsnoop_write_packet(kEventPacket, p, false);
114 case MSG_HC_TO_STACK_HCI_ACL:
115 case MSG_STACK_TO_HC_HCI_ACL:
116 btsnoop_write_packet(kAclPacket, p, is_received);
118 case MSG_HC_TO_STACK_HCI_SCO:
119 case MSG_STACK_TO_HC_HCI_SCO:
120 btsnoop_write_packet(kScoPacket, p, is_received);
122 case MSG_STACK_TO_HC_HCI_CMD:
123 btsnoop_write_packet(kCommandPacket, p, true);
128 static const btsnoop_t interface = {
129 set_api_wants_to_log,
133 const btsnoop_t *btsnoop_get_interface() {
134 stack_config = stack_config_get_interface();
138 // Internal functions
140 static uint64_t btsnoop_timestamp(void) {
142 gettimeofday(&tv, NULL);
144 // Timestamp is in microseconds.
145 uint64_t timestamp = tv.tv_sec * 1000 * 1000LL;
146 timestamp += tv.tv_usec;
147 timestamp += BTSNOOP_EPOCH_DELTA;
151 static void update_logging() {
152 bool should_log = module_started &&
153 (logging_enabled_via_api || stack_config->get_btsnoop_turned_on());
155 if (should_log == is_logging)
158 is_logging = should_log;
162 const char *log_path = stack_config->get_btsnoop_log_path();
164 // Save the old log if configured to do so
165 if (stack_config->get_btsnoop_should_save_last()) {
166 char last_log_path[PATH_MAX];
167 snprintf(last_log_path, PATH_MAX, "%s.%llu", log_path, btsnoop_timestamp());
168 if (!rename(log_path, last_log_path) && errno != ENOENT)
169 LOG_ERROR(LOG_TAG, "%s unable to rename '%s' to '%s': %s", __func__, log_path, last_log_path, strerror(errno));
172 logfile_fd = open(log_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
173 if (logfile_fd == INVALID_FD) {
174 LOG_ERROR(LOG_TAG, "%s unable to open '%s': %s", __func__, log_path, strerror(errno));
179 write(logfile_fd, "btsnoop\0\0\0\0\1\0\0\x3\xea", 16);
181 if (logfile_fd != INVALID_FD)
184 logfile_fd = INVALID_FD;
189 static void btsnoop_write(const void *data, size_t length) {
190 if (logfile_fd != INVALID_FD)
191 write(logfile_fd, data, length);
193 btsnoop_net_write(data, length);
196 static void btsnoop_write_packet(packet_type_t type, const uint8_t *packet, bool is_received) {
203 length_he = packet[2] + 4;
207 length_he = (packet[3] << 8) + packet[2] + 5;
211 length_he = packet[2] + 4;
215 length_he = packet[1] + 3;
220 uint64_t timestamp = btsnoop_timestamp();
221 uint32_t time_hi = timestamp >> 32;
222 uint32_t time_lo = timestamp & 0xFFFFFFFF;
224 length = htonl(length_he);
225 flags = htonl(flags);
226 drops = htonl(drops);
227 time_hi = htonl(time_hi);
228 time_lo = htonl(time_lo);
230 btsnoop_write(&length, 4);
231 btsnoop_write(&length, 4);
232 btsnoop_write(&flags, 4);
233 btsnoop_write(&drops, 4);
234 btsnoop_write(&time_hi, 4);
235 btsnoop_write(&time_lo, 4);
236 btsnoop_write(&type, 1);
237 btsnoop_write(packet, length_he - 1);