• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 #pragma once
18 
19 #include <fstream>
20 #include <iostream>
21 #include <mutex>
22 #include <string>
23 
24 #include "common/circular_buffer.h"
25 #include "hal/hci_hal.h"
26 #include "module.h"
27 #include "os/repeating_alarm.h"
28 
29 namespace bluetooth {
30 namespace hal {
31 
32 #ifdef USE_FAKE_TIMERS
33 static uint64_t file_creation_time;
34 #endif
35 
36 class SnoopLogger : public ::bluetooth::Module {
37  public:
38   static const ModuleFactory Factory;
39 
40   static const std::string kBtSnoopLogModeDisabled;
41   static const std::string kBtSnoopLogModeTruncated;
42   static const std::string kBtSnoopLogModeFiltered;
43   static const std::string kBtSnoopLogModeFull;
44   static const std::string kSoCManufacturerQualcomm;
45 
46   static const std::string kBtSnoopMaxPacketsPerFileProperty;
47   static const std::string kIsDebuggableProperty;
48   static const std::string kBtSnoopLogModeProperty;
49   static const std::string kBtSnoopDefaultLogModeProperty;
50   static const std::string kSoCManufacturerProperty;
51 
52   // Put in header for test
53   struct PacketHeaderType {
54     uint32_t length_original;
55     uint32_t length_captured;
56     uint32_t flags;
57     uint32_t dropped_packets;
58     uint64_t timestamp;
59     uint8_t type;
60   } __attribute__((__packed__));
61 
62   // Put in header for test
63   struct FileHeaderType {
64     uint8_t identification_pattern[8];
65     uint32_t version_number;
66     uint32_t datalink_type;
67   } __attribute__((__packed__));
68 
69   // Returns the maximum number of packets per file
70   // Changes to this value is only effective after restarting Bluetooth
71   static size_t GetMaxPacketsPerFile();
72 
73   static size_t GetMaxPacketsPerBuffer();
74 
75   // Get snoop logger mode based on current system setup
76   // Changes to this values is only effective after restarting Bluetooth
77   static std::string GetBtSnoopMode();
78 
79   // Returns whether the soc manufacturer is Qualcomm
80   // Changes to this value is only effective after restarting Bluetooth
81   static bool IsQualcommDebugLogEnabled();
82 
83   // Has to be defined from 1 to 4 per btsnoop format
84   enum PacketType {
85     CMD = 1,
86     ACL = 2,
87     SCO = 3,
88     EVT = 4,
89     ISO = 5,
90   };
91 
92   enum Direction {
93     INCOMING,
94     OUTGOING,
95   };
96 
97   void Capture(const HciPacket& packet, Direction direction, PacketType type);
98 
99  protected:
100   void ListDependencies(ModuleList* list) const override;
101   void Start() override;
102   void Stop() override;
103   DumpsysDataFinisher GetDumpsysData(flatbuffers::FlatBufferBuilder* builder) const override;
ToString()104   std::string ToString() const override {
105     return std::string("SnoopLogger");
106   }
107 
108   // Visible for testing
109   SnoopLogger(
110       std::string snoop_log_path,
111       std::string snooz_log_path,
112       size_t max_packets_per_file,
113       size_t max_packets_per_buffer,
114       const std::string& btsnoop_mode,
115       bool qualcomm_debug_log_enabled,
116       const std::chrono::milliseconds snooz_log_life_time,
117       const std::chrono::milliseconds snooz_log_delete_alarm_interval);
118   void CloseCurrentSnoopLogFile();
119   void OpenNextSnoopLogFile();
120   void DumpSnoozLogToFile(const std::vector<std::string>& data) const;
121 
122  private:
123   std::string snoop_log_path_;
124   std::string snooz_log_path_;
125   std::ofstream btsnoop_ostream_;
126   bool is_enabled_ = false;
127   bool is_filtered_ = false;
128   bool is_truncated_ = false;
129   size_t max_packets_per_file_;
130   common::CircularBuffer<std::string> btsnooz_buffer_;
131   bool qualcomm_debug_log_enabled_ = false;
132   size_t packet_counter_ = 0;
133   mutable std::recursive_mutex file_mutex_;
134   std::unique_ptr<os::RepeatingAlarm> alarm_;
135   std::chrono::milliseconds snooz_log_life_time_;
136   std::chrono::milliseconds snooz_log_delete_alarm_interval_;
137 };
138 
139 }  // namespace hal
140 }  // namespace bluetooth
141