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 #include <optional> 24 25 #include "chre/platform/shared/bt_snoop_log.h" 26 27 namespace android { 28 namespace chre { 29 30 class BtSnoopLogParser { 31 public: 32 /** 33 * Add a BT event to the snoop log file. 34 * 35 * @param buffer Pointer to the buffer that contains the BT snoop log. 36 * @param maxLogMessageLen The max size allowed for the bt snoop log payload. 37 * @return Size of the bt snoop log payload, std::nullopt if the message 38 * format is invalid. Note that the size includes the 2 bytes header that 39 * includes the bt packet direction and size. 40 */ 41 std::optional<size_t> log(const char *buffer, size_t maxLogMessageLen); 42 43 private: 44 enum class PacketType : uint8_t { 45 CMD = 1, 46 ACL = 2, 47 SCO = 3, 48 EVT = 4, 49 ISO = 5, 50 }; 51 52 //! See host_messages.fbs for the definition of this struct. 53 struct BtSnoopLog { 54 uint8_t direction; 55 uint8_t packetSize; 56 uint8_t packet[]; 57 } __attribute__((packed)); 58 59 //! Header type used for the snoop log file. 60 struct PacketHeaderType { 61 uint32_t length_original; 62 uint32_t length_captured; 63 uint32_t flags; 64 uint32_t dropped_packets; 65 uint64_t timestamp; 66 PacketType type; 67 } __attribute__((packed)); 68 69 bool ensureSnoopLogFileIsOpen(); 70 71 void closeSnoopLogFile(); 72 73 bool openNextSnoopLogFile(); 74 75 /** 76 * Write BT event to the snoop log file. 77 * 78 * @param packet The BT event packet. 79 * @param packetSize Size of the packet. 80 * @param direction Direction of the packet. 81 */ 82 void capture(const uint8_t *packet, size_t packetSize, 83 BtSnoopDirection direction); 84 85 //! File stream used to write the log file. 86 std::ofstream mBtSnoopOstream; 87 88 //! Number of BT packtets in the log file. 89 uint32_t mPacketCounter = 0; 90 }; 91 92 } // namespace chre 93 } // namespace android 94 95 #endif // CHRE_BT_SNOOP_LOG_PARSER_H_ 96