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 #include <sys/types.h>
23 #include <syscall.h>
24 #include <unistd.h>
25
26 #ifdef LOG_DOMAIN
27 #undef LOG_DOMAIN
28 #endif
29 #define LOG_DOMAIN 0xD000105
30
31 namespace OHOS {
32 namespace HDI {
33 namespace Bluetooth {
34 namespace Hci {
35 const int32_t RT_PRIORITY = 1;
36
H4Protocol(int fd,HciDataCallback onAclReceive,HciDataCallback onScoReceive,HciDataCallback onEventReceive,HciDataCallback onIsoReceive)37 H4Protocol::H4Protocol(
38 int fd, HciDataCallback onAclReceive, HciDataCallback onScoReceive, HciDataCallback onEventReceive,
39 HciDataCallback onIsoReceive) : hciFd_(fd), onAclReceive_(onAclReceive), onScoReceive_(onScoReceive),
40 onEventReceive_(onEventReceive), onIsoReceive_(onIsoReceive)
41 {}
42
SetRTSchedule()43 void H4Protocol::SetRTSchedule()
44 {
45 std::lock_guard<std::mutex> lock(tidMutex_);
46 pid_t tid = gettid();
47 auto it = tidMap_.find(tid);
48 if (it == tidMap_.end()) {
49 struct sched_param rtParams = {.sched_priority = RT_PRIORITY};
50 int rc = sched_setscheduler(tid, SCHED_FIFO, &rtParams);
51 if (rc != 0) {
52 HDF_LOGE("set tid rt fail.");
53 tidMap_[tid] = false;
54 } else {
55 tidMap_[tid] = true;
56 }
57 }
58 }
59
SendPacket(HciPacketType packetType,const std::vector<uint8_t> & packetData)60 ssize_t H4Protocol::SendPacket(HciPacketType packetType, const std::vector<uint8_t> &packetData)
61 {
62 SetRTSchedule();
63 uint8_t type = packetType;
64 ssize_t writtenNumber = 0;
65
66 ssize_t ret = Write(hciFd_, &type, sizeof(type));
67 if (ret != sizeof(type)) {
68 return 0;
69 } else {
70 do {
71 ret = Write(hciFd_, packetData.data() + writtenNumber, packetData.size() - writtenNumber);
72 if (ret > 0) {
73 writtenNumber += ret;
74 } else if (ret < 0) {
75 return ret;
76 }
77 } while (static_cast<size_t>(writtenNumber) != packetData.size());
78 }
79
80 return writtenNumber;
81 }
82
ReadData(int fd)83 void H4Protocol::ReadData(int fd)
84 {
85 const int bufsize = 256;
86 char buf[bufsize] = {0};
87 ssize_t readLen;
88 if (hciPacket_.size() == 0) {
89 readLen = Read(fd, &packetType_, sizeof(packetType_));
90 if (readLen < 0) {
91 HDF_LOGE("read fd[%d]", fd);
92 return;
93 } else if (readLen == 0) {
94 HDF_LOGE("read fd[%d] readLen = 0.", fd);
95 return;
96 }
97
98 if (packetType_ > HCI_PACKET_TYPE_UNKNOWN && packetType_ < HCI_PACKET_TYPE_MAX) {
99 hciPacket_.resize(header_[packetType_].headerSize);
100 }
101 } else if (hciPacket_.size() == header_[packetType_].headerSize) {
102 readLen = Read(fd, hciPacket_.data() + readLength_, hciPacket_.size() - readLength_);
103 if (readLen < 0) {
104 strerror_r(errno, buf, sizeof(buf));
105 HDF_LOGE("read fd[%d] err:%s", fd, buf);
106 return;
107 } else if (readLen == 0) {
108 HDF_LOGE("read fd[%d] readLen = 0.", fd);
109 return;
110 }
111
112 readLength_ += readLen;
113 if (readLength_ == hciPacket_.size()) {
114 size_t dataLen = 0;
115 for (int ii = 0; ii < header_[packetType_].dataLengthSize; ii++) {
116 dataLen += (hciPacket_[header_[packetType_].dataLengthOffset + ii] << (ii * 0x08));
117 }
118 hciPacket_.resize(hciPacket_.size() + dataLen);
119 }
120 } else {
121 readLen = Read(fd, hciPacket_.data() + readLength_, hciPacket_.size() - readLength_);
122 if (readLen < 0) {
123 strerror_r(errno, buf, sizeof(buf));
124 HDF_LOGE("read fd[%d] err:%s", fd, buf);
125 return;
126 } else if (readLen == 0) {
127 HDF_LOGE("read fd[%d] readLen = 0.", fd);
128 return;
129 }
130
131 readLength_ += readLen;
132 if (readLength_ == hciPacket_.size()) {
133 PacketCallback();
134 hciPacket_.clear();
135 readLength_ = 0;
136 }
137 }
138 }
139
~H4Protocol()140 H4Protocol::~H4Protocol() {}
141
PacketCallback()142 void H4Protocol::PacketCallback()
143 {
144 SetRTSchedule();
145 switch (packetType_) {
146 case HCI_PACKET_TYPE_ACL_DATA:
147 if (onAclReceive_) {
148 onAclReceive_(hciPacket_);
149 }
150 break;
151 case HCI_PACKET_TYPE_SCO_DATA:
152 if (onScoReceive_) {
153 onScoReceive_(hciPacket_);
154 }
155 break;
156 case HCI_PACKET_TYPE_EVENT:
157 if (onEventReceive_) {
158 onEventReceive_(hciPacket_);
159 }
160 break;
161 case HCI_PACKET_TYPE_ISO_DATA:
162 if (onIsoReceive_) {
163 onIsoReceive_(hciPacket_);
164 }
165 break;
166 default:
167 HDF_LOGE("PacketCallback type[%{public}d] error.", packetType_);
168 break;
169 }
170 }
171 } // namespace Hci
172 } // namespace Bluetooth
173 } // namespace HDI
174 } // namespace OHOS