• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 #define LOG_TAG "hci_socket_device"
18 
19 #include "hci_socket_device.h"
20 
21 #include <fcntl.h>
22 #include <netinet/in.h>
23 #include <sys/socket.h>
24 
25 #include "osi/include/log.h"
26 
27 #include "model/setup/device_boutique.h"
28 
29 using std::vector;
30 
31 namespace test_vendor_lib {
32 
HciSocketDevice(int file_descriptor)33 HciSocketDevice::HciSocketDevice(int file_descriptor) : socket_file_descriptor_(file_descriptor) {
34   advertising_interval_ms_ = std::chrono::milliseconds(0);
35 
36   page_scan_delay_ms_ = std::chrono::milliseconds(600);
37 
38   properties_.SetPageScanRepetitionMode(0);
39   properties_.SetClassOfDevice(0x600420);
40   properties_.SetExtendedInquiryData({
41       16,  // length
42       9,   // Type: Device Name
43       'g',
44       'D',
45       'e',
46       'v',
47       'i',
48       'c',
49       'e',
50       '-',
51       'h',
52       'c',
53       'i',
54       '_',
55       'n',
56       'e',
57       't',
58   });
59   properties_.SetName({
60       'g',
61       'D',
62       'e',
63       'v',
64       'i',
65       'c',
66       'e',
67       '-',
68       'H',
69       'C',
70       'I',
71       '_',
72       'N',
73       'e',
74       't',
75   });
76 
77   h4_ = hci::H4Packetizer(
78       socket_file_descriptor_,
79       [this](const std::vector<uint8_t>& raw_command) {
80         LOG_INFO(LOG_TAG, "Rx Command");
81         std::shared_ptr<std::vector<uint8_t>> packet_copy = std::make_shared<std::vector<uint8_t>>(raw_command);
82         HandleCommand(packet_copy);
83       },
84       [](const std::vector<uint8_t>&) { CHECK(false) << "Unexpected Event in HciSocketDevice!"; },
85       [this](const std::vector<uint8_t>& raw_acl) {
86         LOG_INFO(LOG_TAG, "Rx ACL");
87         std::shared_ptr<std::vector<uint8_t>> packet_copy = std::make_shared<std::vector<uint8_t>>(raw_acl);
88         HandleAcl(packet_copy);
89       },
90       [this](const std::vector<uint8_t>& raw_sco) {
91         LOG_INFO(LOG_TAG, "Rx SCO");
92         std::shared_ptr<std::vector<uint8_t>> packet_copy = std::make_shared<std::vector<uint8_t>>(raw_sco);
93         HandleSco(packet_copy);
94       });
95 
96   RegisterEventChannel([this](std::shared_ptr<std::vector<uint8_t>> packet) {
97     LOG_INFO(LOG_TAG, "Tx Event");
98     SendHci(hci::PacketType::EVENT, packet);
99   });
100   RegisterAclChannel([this](std::shared_ptr<std::vector<uint8_t>> packet) {
101     LOG_INFO(LOG_TAG, "Tx ACL");
102     SendHci(hci::PacketType::ACL, packet);
103   });
104   RegisterScoChannel([this](std::shared_ptr<std::vector<uint8_t>> packet) {
105     LOG_INFO(LOG_TAG, "Tx SCO");
106     SendHci(hci::PacketType::SCO, packet);
107   });
108 }
109 
TimerTick()110 void HciSocketDevice::TimerTick() {
111   LOG_INFO(LOG_TAG, "TimerTick fd = %d", socket_file_descriptor_);
112   h4_.OnDataReady(socket_file_descriptor_);
113   DualModeController::TimerTick();
114 }
115 
SendHci(hci::PacketType packet_type,const std::shared_ptr<std::vector<uint8_t>> packet)116 void HciSocketDevice::SendHci(hci::PacketType packet_type, const std::shared_ptr<std::vector<uint8_t>> packet) {
117   if (socket_file_descriptor_ == -1) {
118     LOG_INFO(LOG_TAG, "socket_file_descriptor == -1");
119     return;
120   }
121   uint8_t type = static_cast<uint8_t>(packet_type);
122   int bytes_written;
123   bytes_written = write(socket_file_descriptor_, &type, sizeof(type));
124   if (bytes_written != sizeof(type)) {
125     LOG_INFO(LOG_TAG, "bytes_written %d != sizeof(type)", bytes_written);
126   }
127   bytes_written = write(socket_file_descriptor_, packet->data(), packet->size());
128   if (static_cast<size_t>(bytes_written) != packet->size()) {
129     LOG_INFO(LOG_TAG, "bytes_written %d != packet->size", bytes_written);
130   }
131 }
132 
133 }  // namespace test_vendor_lib
134