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 "h4_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 {
H4Protocol(int fd,HciDataCallback onAclReceive,HciDataCallback onScoReceive,HciDataCallback onEventReceive)27 H4Protocol::H4Protocol(
28 int fd, HciDataCallback onAclReceive, HciDataCallback onScoReceive, HciDataCallback onEventReceive)
29 : hciFd_(fd), onAclReceive_(onAclReceive), onScoReceive_(onScoReceive), onEventReceive_(onEventReceive)
30 {}
31
SendPacket(HciPacketType packetType,const std::vector<uint8_t> & packetData)32 ssize_t H4Protocol::SendPacket(HciPacketType packetType, const std::vector<uint8_t> &packetData)
33 {
34 uint8_t type = packetType;
35 ssize_t writtenNumber = 0;
36
37 ssize_t ret = Write(hciFd_, &type, sizeof(type));
38 if (ret != sizeof(type)) {
39 return 0;
40 } else {
41 do {
42 ret = Write(hciFd_, packetData.data() + writtenNumber, packetData.size() - writtenNumber);
43 if (ret > 0) {
44 writtenNumber += ret;
45 } else if (ret < 0) {
46 return ret;
47 }
48 } while (static_cast<size_t>(writtenNumber) != packetData.size());
49 }
50
51 return writtenNumber;
52 }
53
ReadData(int fd)54 void H4Protocol::ReadData(int fd)
55 {
56 ssize_t readLen;
57 if (hciPacket_.size() == 0) {
58 readLen = Read(fd, &packetType_, sizeof(packetType_));
59 if (readLen < 0) {
60 HDF_LOGE("read fd[%d] err:%s", fd, strerror(errno));
61 return;
62 } else if (readLen == 0) {
63 HDF_LOGE("read fd[%d] readLen = 0.", fd);
64 return;
65 }
66
67 if (packetType_ > HCI_PACKET_TYPE_UNKNOWN && packetType_ < HCI_PACKET_TYPE_MAX) {
68 hciPacket_.resize(header_[packetType_].headerSize);
69 }
70 } else if (hciPacket_.size() == header_[packetType_].headerSize) {
71 readLen = Read(fd, hciPacket_.data() + readLength_, hciPacket_.size() - readLength_);
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 readLength_ += readLen;
81 if (readLength_ == hciPacket_.size()) {
82 size_t dataLen = 0;
83 for (int ii = 0; ii < header_[packetType_].dataLengthSize; ii++) {
84 dataLen += (hciPacket_[header_[packetType_].dataLengthOffset + ii] << (ii * 0x08));
85 }
86 hciPacket_.resize(hciPacket_.size() + dataLen);
87 }
88 } else {
89 readLen = Read(fd, hciPacket_.data() + readLength_, hciPacket_.size() - readLength_);
90 if (readLen < 0) {
91 HDF_LOGE("read fd[%d] err:%s", fd, strerror(errno));
92 return;
93 } else if (readLen == 0) {
94 HDF_LOGE("read fd[%d] readLen = 0.", fd);
95 return;
96 }
97
98 readLength_ += readLen;
99 if (readLength_ == hciPacket_.size()) {
100 PacketCallback();
101 hciPacket_.clear();
102 readLength_ = 0;
103 }
104 }
105 }
106
PacketCallback()107 void H4Protocol::PacketCallback()
108 {
109 switch (packetType_) {
110 case HCI_PACKET_TYPE_ACL_DATA:
111 if (onAclReceive_) {
112 onAclReceive_(hciPacket_);
113 }
114 break;
115 case HCI_PACKET_TYPE_SCO_DATA:
116 if (onScoReceive_) {
117 onScoReceive_(hciPacket_);
118 }
119 break;
120 case HCI_PACKET_TYPE_EVENT:
121 if (onEventReceive_) {
122 onEventReceive_(hciPacket_);
123 }
124 break;
125 default:
126 HDF_LOGE("PacketCallback type[%d] error.", packetType_);
127 break;
128 }
129 }
130 } // namespace Hci
131 } // namespace Bluetooth
132 } // namespace HDI
133 } // namespace OHOS