• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef LOG_DOMAIN
26 #undef LOG_DOMAIN
27 #endif
28 #define LOG_DOMAIN 0xD000105
29 
30 namespace OHOS {
31 namespace HDI {
32 namespace Bluetooth {
33 namespace Hci {
34 const PacketHeader HciProtocol::header_[HCI_PACKET_TYPE_MAX] = {
35     {.headerSize = 0, .dataLengthOffset = 0, .dataLengthSize = 0}, /* HCI_PACKET_TYPE_UNKNOWN */
36     {.headerSize = 3, .dataLengthOffset = 2, .dataLengthSize = 1}, /* HCI_PACKET_TYPE_COMMAND */
37     {.headerSize = 4, .dataLengthOffset = 2, .dataLengthSize = 2}, /* HCI_PACKET_TYPE_ACL_DATA */
38     {.headerSize = 3, .dataLengthOffset = 2, .dataLengthSize = 1}, /* HCI_PACKET_TYPE_SCO_DATA */
39     {.headerSize = 2, .dataLengthOffset = 1, .dataLengthSize = 1}, /* HCI_PACKET_TYPE_EVENT */
40     {.headerSize = 4, .dataLengthOffset = 2, .dataLengthSize = 2}, /* HCI_PACKET_TYPE_ISO_DATA */
41 };
42 
GetPacketHeaderInfo(HciPacketType packetType)43 const PacketHeader &HciProtocol::GetPacketHeaderInfo(HciPacketType packetType)
44 {
45     if (packetType >= HCI_PACKET_TYPE_MAX) {
46         return header_[HCI_PACKET_TYPE_UNKNOWN];
47     }
48     return header_[packetType];
49 }
50 
Read(int fd,uint8_t * data,size_t length)51 ssize_t HciProtocol::Read(int fd, uint8_t *data, size_t length)
52 {
53     const int bufsize = 256;
54     char buf[bufsize] = {0};
55     ssize_t ret = TEMP_FAILURE_RETRY(read(fd, data, length));
56     if (ret == -1) {
57         strerror_r(errno, buf, sizeof(buf));
58         HDF_LOGE("read failed err:%s", buf);
59         ret = 0;
60     }
61     return ret;
62 }
63 
Write(int fd,const uint8_t * data,size_t length)64 ssize_t HciProtocol::Write(int fd, const uint8_t *data, size_t length)
65 {
66     const int bufsize = 256;
67     char buf[bufsize] = {0};
68     ssize_t ret = 0;
69     do {
70         ret = TEMP_FAILURE_RETRY(write(fd, data, length));
71     } while (ret == -1 && errno == EAGAIN);
72 
73     if (ret == -1) {
74         strerror_r(errno, buf, sizeof(buf));
75         HDF_LOGE("write failed err:%s", buf);
76     } else if (static_cast<size_t>(ret) != length) {
77         HDF_LOGE("write data %zd less than %zu.", ret, length);
78     }
79     return ret;
80 }
81 }  // namespace Hci
82 }  // namespace Bluetooth
83 }  // namespace HDI
84 }  // namespace OHOS