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 "hci_protocol.h"
17
18 #include <cerrno>
19 #include <cstring>
20
21 #include <unistd.h>
22
23 #include <hdf_log.h>
24
25 namespace OHOS {
26 namespace HDI {
27 namespace Bluetooth {
28 namespace Hci {
29 const PacketHeader HciProtocol::header_[HCI_PACKET_TYPE_MAX] = {
30 {.headerSize = 0, .dataLengthOffset = 0, .dataLengthSize = 0}, /* HCI_PACKET_TYPE_UNKNOWN */
31 {.headerSize = 3, .dataLengthOffset = 2, .dataLengthSize = 1}, /* HCI_PACKET_TYPE_COMMAND */
32 {.headerSize = 4, .dataLengthOffset = 2, .dataLengthSize = 2}, /* HCI_PACKET_TYPE_ACL_DATA */
33 {.headerSize = 3, .dataLengthOffset = 2, .dataLengthSize = 1}, /* HCI_PACKET_TYPE_SCO_DATA */
34 {.headerSize = 2, .dataLengthOffset = 1, .dataLengthSize = 1}, /* HCI_PACKET_TYPE_EVENT */
35 };
36
GetPacketHeaderInfo(HciPacketType packetType)37 const PacketHeader &HciProtocol::GetPacketHeaderInfo(HciPacketType packetType)
38 {
39 if (packetType >= HCI_PACKET_TYPE_MAX) {
40 return header_[HCI_PACKET_TYPE_UNKNOWN];
41 }
42 return header_[packetType];
43 }
44
Read(int fd,uint8_t * data,size_t length)45 ssize_t HciProtocol::Read(int fd, uint8_t *data, size_t length)
46 {
47 ssize_t ret = TEMP_FAILURE_RETRY(read(fd, data, length));
48 if (ret == -1) {
49 HDF_LOGE("read failed err:%s", strerror(errno));
50 ret = 0;
51 }
52 return ret;
53 }
54
Write(int fd,const uint8_t * data,size_t length)55 ssize_t HciProtocol::Write(int fd, const uint8_t *data, size_t length)
56 {
57 ssize_t ret = 0;
58 do {
59 ret = TEMP_FAILURE_RETRY(write(fd, data, length));
60 } while (ret == -1 && errno == EAGAIN);
61
62 if (ret == -1) {
63 HDF_LOGE("write failed err:%s", strerror(errno));
64 } else if (static_cast<size_t>(ret) != length) {
65 HDF_LOGE("write data %zd less than %zu.", ret, length);
66 }
67 return ret;
68 }
69 } // namespace Hci
70 } // namespace Bluetooth
71 } // namespace HDI
72 } // namespace OHOS