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 "hal/hci_hal.h"
25 #include "module.h"
26 
27 namespace bluetooth {
28 namespace hal {
29 
30 class SnoopLogger : public ::bluetooth::Module {
31  public:
32   static const ModuleFactory Factory;
33 
34   // Each transport using SnoopLogger should define its own DefaultFilepath
35   static const std::string DefaultFilePath;
36   // Set File Path before module is started to ensure all packets are written to the right file
37   static void SetFilePath(const std::string& filename);
38   // Flag to allow flush into persistent memory on every packet captured. This is enabled on host for debugging.
39   static const bool AlwaysFlush;
40 
41   enum class PacketType {
42     CMD = 1,
43     ACL = 2,
44     SCO = 3,
45     EVT = 4,
46   };
47 
48   enum class Direction {
49     INCOMING,
50     OUTGOING,
51   };
52 
53   void capture(const HciPacket& packet, Direction direction, PacketType type);
54 
55  protected:
56   void ListDependencies(ModuleList* list) override;
57   void Start() override;
58   void Stop() override;
59 
60  private:
61   SnoopLogger();
62   static std::string file_path;
63   std::ofstream btsnoop_ostream_;
64   std::mutex file_mutex_;
65 };
66 
67 }  // namespace hal
68 }  // namespace bluetooth
69