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