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 "model/devices/hci_device.h"
18
19 #include <cstdint>
20 #include <memory>
21 #include <vector>
22
23 #include "log.h"
24 #include "model/controller/controller_properties.h"
25 #include "model/controller/dual_mode_controller.h"
26 #include "model/hci/hci_transport.h"
27 #include "packets/link_layer_packets.h"
28
29 namespace rootcanal {
30
HciDevice(std::shared_ptr<HciTransport> transport,ControllerProperties const & properties)31 HciDevice::HciDevice(std::shared_ptr<HciTransport> transport,
32 ControllerProperties const& properties)
33 : DualModeController(ControllerProperties(properties)),
34 transport_(transport) {
35 link_layer_controller_.SetLocalName(std::vector<uint8_t>({
36 'g',
37 'D',
38 'e',
39 'v',
40 'i',
41 'c',
42 'e',
43 '-',
44 'H',
45 'C',
46 'I',
47 }));
48 link_layer_controller_.SetExtendedInquiryResponse(std::vector<uint8_t>({
49 12, // Length
50 9, // Type: Device Name
51 'g',
52 'D',
53 'e',
54 'v',
55 'i',
56 'c',
57 'e',
58 '-',
59 'h',
60 'c',
61 'i',
62 }));
63
64 RegisterEventChannel([this](std::shared_ptr<std::vector<uint8_t>> packet) {
65 transport_->Send(PacketType::EVENT, *packet);
66 });
67 RegisterAclChannel([this](std::shared_ptr<std::vector<uint8_t>> packet) {
68 transport_->Send(PacketType::ACL, *packet);
69 });
70 RegisterScoChannel([this](std::shared_ptr<std::vector<uint8_t>> packet) {
71 transport_->Send(PacketType::SCO, *packet);
72 });
73 RegisterIsoChannel([this](std::shared_ptr<std::vector<uint8_t>> packet) {
74 transport_->Send(PacketType::ISO, *packet);
75 });
76
77 transport_->RegisterCallbacks(
78 [this](PacketType packet_type,
79 const std::shared_ptr<std::vector<uint8_t>> packet) {
80 switch (packet_type) {
81 case PacketType::COMMAND:
82 HandleCommand(packet);
83 break;
84 case PacketType::ACL:
85 HandleAcl(packet);
86 break;
87 case PacketType::SCO:
88 HandleSco(packet);
89 break;
90 case PacketType::ISO:
91 HandleIso(packet);
92 break;
93 default:
94 ASSERT(false);
95 break;
96 }
97 },
98 [this]() {
99 INFO(id_, "HCI transport closed");
100 Close();
101 });
102 }
103
Tick()104 void HciDevice::Tick() {
105 transport_->Tick();
106 DualModeController::Tick();
107 }
108
Close()109 void HciDevice::Close() {
110 transport_->Close();
111 DualModeController::Close();
112 }
113
114 } // namespace rootcanal
115