1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef CHRE_BT_SNOOP_LOG_PARSER_H_ 18 #define CHRE_BT_SNOOP_LOG_PARSER_H_ 19 20 #include <cinttypes> 21 #include <fstream> 22 #include <memory> 23 24 #include "chre/platform/shared/bt_snoop_log.h" 25 26 namespace android { 27 namespace chre { 28 29 class BtSnoopLogParser { 30 public: 31 /** 32 * Add a BT event to the snoop log file. 33 * 34 * @param buffer Pointer to the buffer that contains the BT snoop log. 35 */ 36 size_t log(const char *buffer); 37 38 private: 39 enum class PacketType : uint8_t { 40 CMD = 1, 41 ACL = 2, 42 SCO = 3, 43 EVT = 4, 44 ISO = 5, 45 }; 46 47 //! See host_messages.fbs for the definition of this struct. 48 struct BtSnoopLog { 49 uint8_t direction; 50 uint8_t packetSize; 51 uint8_t packet[]; 52 } __attribute__((packed)); 53 54 //! Header type used for the snoop log file. 55 struct PacketHeaderType { 56 uint32_t length_original; 57 uint32_t length_captured; 58 uint32_t flags; 59 uint32_t dropped_packets; 60 uint64_t timestamp; 61 PacketType type; 62 } __attribute__((packed)); 63 64 bool ensureSnoopLogFileIsOpen(); 65 66 void closeSnoopLogFile(); 67 68 bool openNextSnoopLogFile(); 69 70 /** 71 * Write BT event to the snoop log file. 72 * 73 * @param packet The BT event packet. 74 * @param packetSize Size of the packet. 75 * @param direction Direction of the packet. 76 */ 77 void capture(const uint8_t *packet, size_t packetSize, 78 BtSnoopDirection direction); 79 80 //! File stream used to write the log file. 81 std::ofstream mBtSnoopOstream; 82 83 //! Number of BT packtets in the log file. 84 uint32_t mPacketCounter = 0; 85 }; 86 87 } // namespace chre 88 } // namespace android 89 90 #endif // CHRE_BT_SNOOP_LOG_PARSER_H_ 91