• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "mct_protocol.h"
17 
18 #include <cerrno>
19 #include <cstring>
20 
21 #include <hdf_log.h>
22 
23 namespace OHOS {
24 namespace HDI {
25 namespace Bluetooth {
26 namespace Hci {
MctProtocol(const int fds[HCI_MAX_CHANNEL],HciDataCallback onAclReceive,HciDataCallback onScoReceive,HciDataCallback onEventReceive)27 MctProtocol::MctProtocol(const int fds[HCI_MAX_CHANNEL], HciDataCallback onAclReceive, HciDataCallback onScoReceive,
28     HciDataCallback onEventReceive)
29 {
30     for (int ii = 0; ii < HCI_MAX_CHANNEL; ii++) {
31         hciFds_[ii] = fds[ii];
32     }
33     onAclReceive_ = onAclReceive;
34     onScoReceive_ = onScoReceive;
35     onEventReceive_ = onEventReceive;
36 }
37 
SendPacket(HciPacketType packetType,const std::vector<uint8_t> & packetData)38 ssize_t MctProtocol::SendPacket(HciPacketType packetType, const std::vector<uint8_t> &packetData)
39 {
40     uint8_t type = packetType;
41 
42     if (packetType == HciPacketType::HCI_PACKET_TYPE_COMMAND) {
43         return Write(hciFds_[hci_channels_t::HCI_CMD], &type, sizeof(type));
44     } else if (packetType == HciPacketType::HCI_PACKET_TYPE_ACL_DATA) {
45         return Write(hciFds_[hci_channels_t::HCI_ACL_OUT], &type, sizeof(type));
46     }
47 
48     return 0;
49 }
50 
ReadEventData(int fd)51 void MctProtocol::ReadEventData(int fd)
52 {
53     ssize_t readLen;
54     if (eventPacket_.size() == header_[HCI_PACKET_TYPE_EVENT].headerSize) {
55         readLen = Read(fd, eventPacket_.data() + eventReadLength_, eventPacket_.size() - eventReadLength_);
56         if (readLen < 0) {
57             HDF_LOGE("read fd[%d] err:%s", fd, strerror(errno));
58             return;
59         } else if (readLen == 0) {
60             HDF_LOGE("read fd[%d] readLen = 0.", fd);
61             return;
62         }
63 
64         eventReadLength_ += readLen;
65         if (eventReadLength_ == eventPacket_.size()) {
66             size_t dataLen = 0;
67             dataLen += eventPacket_[header_[HCI_PACKET_TYPE_EVENT].dataLengthOffset];
68             eventPacket_.resize(eventPacket_.size() + dataLen);
69         }
70     } else {
71         readLen = Read(fd, eventPacket_.data() + eventReadLength_, eventPacket_.size() - eventReadLength_);
72         if (readLen < 0) {
73             HDF_LOGE("read fd[%d] err:%s", fd, strerror(errno));
74             return;
75         } else if (readLen == 0) {
76             HDF_LOGE("read fd[%d] readLen = 0.", fd);
77             return;
78         }
79 
80         eventReadLength_ += readLen;
81         if (eventReadLength_ == eventPacket_.size()) {
82             if (onEventReceive_) {
83                 onEventReceive_(eventPacket_);
84             }
85             eventPacket_.resize(header_[HCI_PACKET_TYPE_EVENT].headerSize);
86             eventReadLength_ = 0;
87         }
88     }
89 }
90 
ReadAclData(int fd)91 void MctProtocol::ReadAclData(int fd)
92 {
93     ssize_t readLen;
94     if (aclPacket_.size() == header_[HCI_PACKET_TYPE_ACL_DATA].headerSize) {
95         readLen = Read(fd, aclPacket_.data() + aclReadLength_, aclPacket_.size() - aclReadLength_);
96         if (readLen < 0) {
97             HDF_LOGE("read fd[%d] err:%s", fd, strerror(errno));
98             return;
99         } else if (readLen == 0) {
100             HDF_LOGE("read fd[%d] readLen = 0.", fd);
101             return;
102         }
103 
104         aclReadLength_ += readLen;
105         if (aclReadLength_ == aclPacket_.size()) {
106             size_t dataLen = 0;
107             dataLen += aclPacket_[header_[HCI_PACKET_TYPE_ACL_DATA].dataLengthOffset];
108             aclPacket_.resize(aclPacket_.size() + dataLen);
109         }
110     } else {
111         readLen = Read(fd, aclPacket_.data() + aclReadLength_, aclPacket_.size() - aclReadLength_);
112         if (readLen < 0) {
113             HDF_LOGE("read fd[%d] err:%s", fd, strerror(errno));
114             return;
115         } else if (readLen == 0) {
116             HDF_LOGE("read fd[%d] readLen = 0.", fd);
117             return;
118         }
119 
120         aclReadLength_ += readLen;
121         if (aclReadLength_ == aclPacket_.size()) {
122             if (onAclReceive_) {
123                 onAclReceive_(aclPacket_);
124             }
125             aclPacket_.resize(header_[HCI_PACKET_TYPE_ACL_DATA].headerSize);
126             aclReadLength_ = 0;
127         }
128     }
129 }
130 }  // namespace Hci
131 }  // namespace Bluetooth
132 }  // namespace HDI
133 }  // namespace OHOS