1 /******************************************************************************
2  *
3  *  Copyright 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 <base/logging.h>
20 
21 #include "hci/include/btsnoop_mem.h"
22 
23 static btsnoop_data_cb data_callback = NULL;
24 
btsnoop_mem_set_callback(btsnoop_data_cb cb)25 void btsnoop_mem_set_callback(btsnoop_data_cb cb) { data_callback = cb; }
26 
btsnoop_mem_capture(const BT_HDR * packet,uint64_t timestamp_us)27 void btsnoop_mem_capture(const BT_HDR* packet, uint64_t timestamp_us) {
28   if (!data_callback) return;
29 
30   CHECK(packet);
31 
32   const uint8_t* data = &packet->data[packet->offset];
33   const uint16_t type = packet->event & BT_EVT_MASK;
34   size_t length = 0;
35 
36   switch (type) {
37     case BT_EVT_TO_LM_HCI_CMD:
38       if (packet->len > 2) length = data[2] + 3;
39       break;
40 
41     case BT_EVT_TO_BTU_HCI_EVT:
42       if (packet->len > 1) length = data[1] + 2;
43       break;
44 
45     case BT_EVT_TO_LM_HCI_ACL:
46     case BT_EVT_TO_BTU_HCI_ACL:
47       if (packet->len > 3) length = (data[2] | (data[3] << 8)) + 4;
48       break;
49 
50     case BT_EVT_TO_LM_HCI_SCO:
51     case BT_EVT_TO_BTU_HCI_SCO:
52       if (packet->len > 2) length = data[2] + 3;
53       break;
54   }
55 
56   if (length) (*data_callback)(type, data, length, timestamp_us);
57 }
58