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>&) { FATAL("Unexpected Event in HciSocketTransport!"); },
37           [packet_callback](const std::vector<uint8_t>& raw_acl) {
38             std::shared_ptr<std::vector<uint8_t>> packet_copy =
39                     std::make_shared<std::vector<uint8_t>>(raw_acl);
40             packet_callback(PacketType::ACL, packet_copy);
41           },
42           [packet_callback](const std::vector<uint8_t>& raw_sco) {
43             std::shared_ptr<std::vector<uint8_t>> packet_copy =
44                     std::make_shared<std::vector<uint8_t>>(raw_sco);
45             packet_callback(PacketType::SCO, packet_copy);
46           },
47           [packet_callback](const std::vector<uint8_t>& raw_iso) {
48             std::shared_ptr<std::vector<uint8_t>> packet_copy =
49                     std::make_shared<std::vector<uint8_t>>(raw_iso);
50             packet_callback(PacketType::ISO, packet_copy);
51           },
52           close_callback);
53 }
54 
Tick()55 void HciSocketTransport::Tick() { h4_.OnDataReady(socket_); }
56 
Send(PacketType packet_type,const std::vector<uint8_t> & packet)57 void HciSocketTransport::Send(PacketType packet_type, const std::vector<uint8_t>& packet) {
58   if (!socket_ || !socket_->Connected()) {
59     INFO("Closed socket. Dropping packet of type {}", fmt::underlying(packet_type));
60     return;
61   }
62   uint8_t type = static_cast<uint8_t>(packet_type);
63   h4_.Send(type, packet.data(), packet.size());
64 }
65 
Close()66 void HciSocketTransport::Close() { socket_->Close(); }
67 
68 }  // namespace rootcanal
69