1 /****************************************************************************** 2 * 3 * Copyright (C) 2022 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 #pragma once 20 21 #include <atomic> 22 #include <condition_variable> 23 #include <future> 24 #include <memory> 25 #include <mutex> 26 #include <thread> 27 28 #include "hal/snoop_logger_socket.h" 29 30 namespace bluetooth { 31 namespace hal { 32 33 class SnoopLoggerSocketThread : public SnoopLoggerSocketInterface { 34 public: 35 SnoopLoggerSocketThread(std::unique_ptr<SnoopLoggerSocket>&& socket); 36 SnoopLoggerSocketThread(const SnoopLoggerSocket&) = delete; 37 SnoopLoggerSocketThread& operator=(const SnoopLoggerSocketThread&) = delete; 38 virtual ~SnoopLoggerSocketThread(); 39 40 std::future<bool> Start(); 41 void Stop(); 42 void Write(const void* data, size_t length) override; 43 bool ThreadIsRunning() const; 44 45 SnoopLoggerSocket* GetSocket(); 46 47 private: 48 void Run(std::promise<bool> thread_started); 49 50 std::unique_ptr<SnoopLoggerSocket> socket_; 51 52 // Socket thread for listening to incoming connections. 53 std::unique_ptr<std::thread> listen_thread_; 54 bool listen_thread_running_ = false; 55 56 std::condition_variable listen_thread_running_cv_; 57 std::mutex listen_thread_running_mutex_; 58 std::atomic<bool> stop_thread_; 59 }; 60 61 } // namespace hal 62 } // namespace bluetooth 63