• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hal/snoop_logger_socket_thread.h"
20 
21 #include <arpa/inet.h>
22 #include <base/logging.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <netinet/in.h>
26 #include <pthread.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/prctl.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <sys/un.h>
34 #include <unistd.h>
35 
36 #include <mutex>
37 
38 #include "common/init_flags.h"
39 #include "hal/snoop_logger_common.h"
40 #include "os/handler.h"
41 #include "os/log.h"
42 #include "os/thread.h"
43 #include "os/utils.h"
44 
45 namespace bluetooth {
46 namespace hal {
47 
SnoopLoggerSocketThread(std::unique_ptr<SnoopLoggerSocket> && socket)48 SnoopLoggerSocketThread::SnoopLoggerSocketThread(std::unique_ptr<SnoopLoggerSocket>&& socket) {
49   socket_ = std::move(socket);
50   stop_thread_ = false;
51   listen_thread_running_ = false;
52 }
53 
~SnoopLoggerSocketThread()54 SnoopLoggerSocketThread::~SnoopLoggerSocketThread() {
55   Stop();
56 }
57 
Start()58 std::future<bool> SnoopLoggerSocketThread::Start() {
59   LOG_DEBUG("");
60   std::promise<bool> thread_started;
61   auto future = thread_started.get_future();
62   listen_thread_ = std::make_unique<std::thread>(&SnoopLoggerSocketThread::Run, this, std::move(thread_started));
63   stop_thread_ = false;
64   return std::move(future);
65 }
66 
Stop()67 void SnoopLoggerSocketThread::Stop() {
68   LOG_DEBUG("");
69 
70   stop_thread_ = true;
71   socket_->NotifySocketListener();
72 
73   if (listen_thread_ && listen_thread_->joinable()) {
74     listen_thread_->join();
75     listen_thread_.reset();
76   }
77 }
78 
Write(const void * data,size_t length)79 void SnoopLoggerSocketThread::Write(const void* data, size_t length) {
80   socket_->Write(data, length);
81 }
82 
ThreadIsRunning() const83 bool SnoopLoggerSocketThread::ThreadIsRunning() const {
84   return listen_thread_running_;
85 }
86 
GetSocket()87 SnoopLoggerSocket* SnoopLoggerSocketThread::GetSocket() {
88   return socket_.get();
89 }
90 
Run(std::promise<bool> thread_started)91 void SnoopLoggerSocketThread::Run(std::promise<bool> thread_started) {
92   LOG_DEBUG("");
93 
94   if (socket_->InitializeCommunications() != 0) {
95     thread_started.set_value(false);
96     return;
97   }
98 
99   thread_started.set_value(true);
100 
101   while (!stop_thread_ && socket_->ProcessIncomingRequest()) {
102   }
103 
104   socket_->Cleanup();
105   listen_thread_running_ = false;
106 }
107 
108 }  // namespace hal
109 }  // namespace bluetooth
110