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 const int bufsize = 256;
54 char buf[bufsize] = {0};
55 ssize_t readLen;
56 if (eventPacket_.size() == header_[HCI_PACKET_TYPE_EVENT].headerSize) {
57 readLen = Read(fd, eventPacket_.data() + eventReadLength_, eventPacket_.size() - eventReadLength_);
58 if (readLen < 0) {
59 strerror_r(errno, buf, sizeof(buf));
60 HDF_LOGE("read fd[%d] err:%s", fd, buf);
61 return;
62 } else if (readLen == 0) {
63 HDF_LOGE("read fd[%d] readLen = 0.", fd);
64 return;
65 }
66
67 eventReadLength_ += readLen;
68 if (eventReadLength_ == eventPacket_.size()) {
69 size_t dataLen = 0;
70 dataLen += eventPacket_[header_[HCI_PACKET_TYPE_EVENT].dataLengthOffset];
71 eventPacket_.resize(eventPacket_.size() + dataLen);
72 }
73 } else {
74 readLen = Read(fd, eventPacket_.data() + eventReadLength_, eventPacket_.size() - eventReadLength_);
75 if (readLen < 0) {
76 strerror_r(errno, buf, sizeof(buf));
77 HDF_LOGE("read fd[%d] err:%s", fd, buf);
78 return;
79 } else if (readLen == 0) {
80 HDF_LOGE("read fd[%d] readLen = 0.", fd);
81 return;
82 }
83
84 eventReadLength_ += readLen;
85 if (eventReadLength_ == eventPacket_.size()) {
86 if (onEventReceive_) {
87 onEventReceive_(eventPacket_);
88 }
89 eventPacket_.resize(header_[HCI_PACKET_TYPE_EVENT].headerSize);
90 eventReadLength_ = 0;
91 }
92 }
93 }
94
~MctProtocol()95 MctProtocol::~MctProtocol() {}
96
ReadAclData(int fd)97 void MctProtocol::ReadAclData(int fd)
98 {
99 const int bufsize = 256;
100 char buf[bufsize] = {0};
101 ssize_t readLen;
102 if (aclPacket_.size() == header_[HCI_PACKET_TYPE_ACL_DATA].headerSize) {
103 readLen = Read(fd, aclPacket_.data() + aclReadLength_, aclPacket_.size() - aclReadLength_);
104 if (readLen < 0) {
105 strerror_r(errno, buf, sizeof(buf));
106 HDF_LOGE("read fd[%d] err:%s", fd, buf);
107 return;
108 } else if (readLen == 0) {
109 HDF_LOGE("read fd[%d] readLen = 0.", fd);
110 return;
111 }
112
113 aclReadLength_ += readLen;
114 if (aclReadLength_ == aclPacket_.size()) {
115 size_t dataLen = 0;
116 dataLen += aclPacket_[header_[HCI_PACKET_TYPE_ACL_DATA].dataLengthOffset];
117 aclPacket_.resize(aclPacket_.size() + dataLen);
118 }
119 } else {
120 readLen = Read(fd, aclPacket_.data() + aclReadLength_, aclPacket_.size() - aclReadLength_);
121 if (readLen < 0) {
122 strerror_r(errno, buf, sizeof(buf));
123 HDF_LOGE("read fd[%d] err:%s", fd, buf);
124 return;
125 } else if (readLen == 0) {
126 HDF_LOGE("read fd[%d] readLen = 0.", fd);
127 return;
128 }
129
130 aclReadLength_ += readLen;
131 if (aclReadLength_ == aclPacket_.size()) {
132 if (onAclReceive_) {
133 onAclReceive_(aclPacket_);
134 }
135 aclPacket_.resize(header_[HCI_PACKET_TYPE_ACL_DATA].headerSize);
136 aclReadLength_ = 0;
137 }
138 }
139 }
140 } // namespace Hci
141 } // namespace Bluetooth
142 } // namespace HDI
143 } // namespace OHOS