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