1 /*
2  * Copyright (C) 2021 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 #include "host/frontend/webrtc/bluetooth_handler.h"
18 
19 #include <unistd.h>
20 
21 #include <android-base/logging.h>
22 
23 using namespace android;
24 
25 namespace cuttlefish {
26 namespace webrtc_streaming {
27 
BluetoothHandler(const int rootCanalTestPort,std::function<void (const uint8_t *,size_t)> send_to_client)28 BluetoothHandler::BluetoothHandler(
29     const int rootCanalTestPort,
30     std::function<void(const uint8_t *, size_t)> send_to_client)
31     : send_to_client_(send_to_client),
32       rootcanal_socket_(
33           SharedFD::SocketLocalClient(rootCanalTestPort, SOCK_STREAM)),
34       shutdown_(SharedFD::Event(0, 0)) {
35   std::thread loop([this]() { ReadLoop(); });
36   read_thread_.swap(loop);
37 }
38 
~BluetoothHandler()39 BluetoothHandler::~BluetoothHandler() {
40   // Send a message to the looper to shut down.
41   uint64_t v = 1;
42   shutdown_->Write(&v, sizeof(v));
43   // Shut down the socket as well.  Not strictly necessary.
44   rootcanal_socket_->Shutdown(SHUT_RDWR);
45   read_thread_.join();
46 }
47 
ReadLoop()48 void BluetoothHandler::ReadLoop() {
49   while (1) {
50     uint8_t buffer[4096];
51 
52     read_set_.Set(shutdown_);
53     read_set_.Set(rootcanal_socket_);
54     Select(&read_set_, nullptr, nullptr, nullptr);
55 
56     if (read_set_.IsSet(rootcanal_socket_)) {
57       auto read = rootcanal_socket_->Read(buffer, sizeof(buffer));
58       if (read < 0) {
59         PLOG(ERROR) << "Error on reading from RootCanal socket.";
60         break;
61       }
62       if (read) {
63         send_to_client_(buffer, read);
64       }
65     }
66 
67     if (read_set_.IsSet(shutdown_)) {
68       LOG(INFO) << "BluetoothHandler is shutting down.";
69       break;
70     }
71   }
72 }
73 
handleMessage(const uint8_t * msg,size_t len)74 void BluetoothHandler::handleMessage(const uint8_t *msg, size_t len) {
75   size_t sent = 0;
76   while (sent < len) {
77     auto this_sent = rootcanal_socket_->Write(&msg[sent], len - sent);
78     if (this_sent < 0) {
79       PLOG(ERROR) << "Error writing to rootcanal socket.";
80       return;
81     }
82     sent += this_sent;
83   }
84 }
85 
86 }  // namespace webrtc_streaming
87 }  // namespace cuttlefish
88