• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"  // for LOG_INFO, LOG_ALWAYS_FATAL
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 command_callback,PacketCallback acl_callback,PacketCallback sco_callback,PacketCallback iso_callback,CloseCallback close_callback)26 void HciSocketTransport::RegisterCallbacks(PacketCallback command_callback,
27                                            PacketCallback acl_callback,
28                                            PacketCallback sco_callback,
29                                            PacketCallback iso_callback,
30                                            CloseCallback close_callback) {
31   // TODO: Avoid the copy here by using new buffer in H4DataChannel
32   h4_ = H4DataChannelPacketizer(
33       socket_,
34       [command_callback](const std::vector<uint8_t>& raw_command) {
35         std::shared_ptr<std::vector<uint8_t>> packet_copy =
36             std::make_shared<std::vector<uint8_t>>(raw_command);
37         command_callback(packet_copy);
38       },
39       [](const std::vector<uint8_t>&) {
40         LOG_ALWAYS_FATAL("Unexpected Event in HciSocketTransport!");
41       },
42       [acl_callback](const std::vector<uint8_t>& raw_acl) {
43         std::shared_ptr<std::vector<uint8_t>> packet_copy =
44             std::make_shared<std::vector<uint8_t>>(raw_acl);
45         acl_callback(packet_copy);
46       },
47       [sco_callback](const std::vector<uint8_t>& raw_sco) {
48         std::shared_ptr<std::vector<uint8_t>> packet_copy =
49             std::make_shared<std::vector<uint8_t>>(raw_sco);
50         sco_callback(packet_copy);
51       },
52       [iso_callback](const std::vector<uint8_t>& raw_iso) {
53         std::shared_ptr<std::vector<uint8_t>> packet_copy =
54             std::make_shared<std::vector<uint8_t>>(raw_iso);
55         iso_callback(packet_copy);
56       },
57       close_callback);
58 }
59 
Tick()60 void HciSocketTransport::Tick() { h4_.OnDataReady(socket_); }
61 
SendHci(PacketType packet_type,const std::vector<uint8_t> & packet)62 void HciSocketTransport::SendHci(PacketType packet_type,
63                                  const std::vector<uint8_t>& packet) {
64   if (!socket_ || !socket_->Connected()) {
65     LOG_INFO("Closed socket. Dropping packet of type %d",
66              static_cast<int>(packet_type));
67     return;
68   }
69   uint8_t type = static_cast<uint8_t>(packet_type);
70   h4_.Send(type, packet.data(), packet.size());
71 }
72 
SendEvent(const std::vector<uint8_t> & packet)73 void HciSocketTransport::SendEvent(const std::vector<uint8_t>& packet) {
74   SendHci(PacketType::EVENT, packet);
75 }
76 
SendAcl(const std::vector<uint8_t> & packet)77 void HciSocketTransport::SendAcl(const std::vector<uint8_t>& packet) {
78   SendHci(PacketType::ACL, packet);
79 }
80 
SendSco(const std::vector<uint8_t> & packet)81 void HciSocketTransport::SendSco(const std::vector<uint8_t>& packet) {
82   SendHci(PacketType::SCO, packet);
83 }
84 
SendIso(const std::vector<uint8_t> & packet)85 void HciSocketTransport::SendIso(const std::vector<uint8_t>& packet) {
86   SendHci(PacketType::ISO, packet);
87 }
88 
Close()89 void HciSocketTransport::Close() { socket_->Close(); }
90 
91 }  // namespace rootcanal
92