1 /*
2  * Copyright 2022 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 "hci_socket_transport.h"
18 
19 #include "log.h"
20 
21 namespace rootcanal {
22 
HciSocketTransport(std::shared_ptr<AsyncDataChannel> socket)23 HciSocketTransport::HciSocketTransport(std::shared_ptr<AsyncDataChannel> socket)
24     : socket_(socket) {}
25 
RegisterCallbacks(PacketCallback packet_callback,CloseCallback close_callback)26 void HciSocketTransport::RegisterCallbacks(PacketCallback packet_callback,
27                                            CloseCallback close_callback) {
28   // TODO: Avoid the copy here by using new buffer in H4DataChannel
29   h4_ = H4DataChannelPacketizer(
30       socket_,
31       [packet_callback](const std::vector<uint8_t>& raw_command) {
32         std::shared_ptr<std::vector<uint8_t>> packet_copy =
33             std::make_shared<std::vector<uint8_t>>(raw_command);
34         packet_callback(PacketType::COMMAND, packet_copy);
35       },
36       [](const std::vector<uint8_t>&) {
37         FATAL("Unexpected Event in HciSocketTransport!");
38       },
39       [packet_callback](const std::vector<uint8_t>& raw_acl) {
40         std::shared_ptr<std::vector<uint8_t>> packet_copy =
41             std::make_shared<std::vector<uint8_t>>(raw_acl);
42         packet_callback(PacketType::ACL, packet_copy);
43       },
44       [packet_callback](const std::vector<uint8_t>& raw_sco) {
45         std::shared_ptr<std::vector<uint8_t>> packet_copy =
46             std::make_shared<std::vector<uint8_t>>(raw_sco);
47         packet_callback(PacketType::SCO, packet_copy);
48       },
49       [packet_callback](const std::vector<uint8_t>& raw_iso) {
50         std::shared_ptr<std::vector<uint8_t>> packet_copy =
51             std::make_shared<std::vector<uint8_t>>(raw_iso);
52         packet_callback(PacketType::ISO, packet_copy);
53       },
54       close_callback);
55 }
56 
Tick()57 void HciSocketTransport::Tick() { h4_.OnDataReady(socket_); }
58 
Send(PacketType packet_type,const std::vector<uint8_t> & packet)59 void HciSocketTransport::Send(PacketType packet_type,
60                               const std::vector<uint8_t>& packet) {
61   if (!socket_ || !socket_->Connected()) {
62     INFO("Closed socket. Dropping packet of type {}",
63          fmt::underlying(packet_type));
64     return;
65   }
66   uint8_t type = static_cast<uint8_t>(packet_type);
67   h4_.Send(type, packet.data(), packet.size());
68 }
69 
Close()70 void HciSocketTransport::Close() { socket_->Close(); }
71 
72 }  // namespace rootcanal
73