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