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