1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include "hci_protocol.h" 17094332d3Sopenharmony_ci 18094332d3Sopenharmony_ci#include <cerrno> 19094332d3Sopenharmony_ci#include <cstring> 20094332d3Sopenharmony_ci 21094332d3Sopenharmony_ci#include <unistd.h> 22094332d3Sopenharmony_ci 23094332d3Sopenharmony_ci#include <hdf_log.h> 24094332d3Sopenharmony_ci 25094332d3Sopenharmony_ci#ifdef LOG_DOMAIN 26094332d3Sopenharmony_ci#undef LOG_DOMAIN 27094332d3Sopenharmony_ci#endif 28094332d3Sopenharmony_ci#define LOG_DOMAIN 0xD000105 29094332d3Sopenharmony_ci 30094332d3Sopenharmony_cinamespace OHOS { 31094332d3Sopenharmony_cinamespace HDI { 32094332d3Sopenharmony_cinamespace Bluetooth { 33094332d3Sopenharmony_cinamespace Hci { 34094332d3Sopenharmony_ciconst PacketHeader HciProtocol::header_[HCI_PACKET_TYPE_MAX] = { 35094332d3Sopenharmony_ci {.headerSize = 0, .dataLengthOffset = 0, .dataLengthSize = 0}, /* HCI_PACKET_TYPE_UNKNOWN */ 36094332d3Sopenharmony_ci {.headerSize = 3, .dataLengthOffset = 2, .dataLengthSize = 1}, /* HCI_PACKET_TYPE_COMMAND */ 37094332d3Sopenharmony_ci {.headerSize = 4, .dataLengthOffset = 2, .dataLengthSize = 2}, /* HCI_PACKET_TYPE_ACL_DATA */ 38094332d3Sopenharmony_ci {.headerSize = 3, .dataLengthOffset = 2, .dataLengthSize = 1}, /* HCI_PACKET_TYPE_SCO_DATA */ 39094332d3Sopenharmony_ci {.headerSize = 2, .dataLengthOffset = 1, .dataLengthSize = 1}, /* HCI_PACKET_TYPE_EVENT */ 40094332d3Sopenharmony_ci}; 41094332d3Sopenharmony_ci 42094332d3Sopenharmony_ciconst PacketHeader &HciProtocol::GetPacketHeaderInfo(HciPacketType packetType) 43094332d3Sopenharmony_ci{ 44094332d3Sopenharmony_ci if (packetType >= HCI_PACKET_TYPE_MAX) { 45094332d3Sopenharmony_ci return header_[HCI_PACKET_TYPE_UNKNOWN]; 46094332d3Sopenharmony_ci } 47094332d3Sopenharmony_ci return header_[packetType]; 48094332d3Sopenharmony_ci} 49094332d3Sopenharmony_ci 50094332d3Sopenharmony_cissize_t HciProtocol::Read(int fd, uint8_t *data, size_t length) 51094332d3Sopenharmony_ci{ 52094332d3Sopenharmony_ci const int bufsize = 256; 53094332d3Sopenharmony_ci char buf[bufsize] = {0}; 54094332d3Sopenharmony_ci ssize_t ret = TEMP_FAILURE_RETRY(read(fd, data, length)); 55094332d3Sopenharmony_ci if (ret == -1) { 56094332d3Sopenharmony_ci strerror_r(errno, buf, sizeof(buf)); 57094332d3Sopenharmony_ci HDF_LOGE("read failed err:%s", buf); 58094332d3Sopenharmony_ci ret = 0; 59094332d3Sopenharmony_ci } 60094332d3Sopenharmony_ci return ret; 61094332d3Sopenharmony_ci} 62094332d3Sopenharmony_ci 63094332d3Sopenharmony_cissize_t HciProtocol::Write(int fd, const uint8_t *data, size_t length) 64094332d3Sopenharmony_ci{ 65094332d3Sopenharmony_ci const int bufsize = 256; 66094332d3Sopenharmony_ci char buf[bufsize] = {0}; 67094332d3Sopenharmony_ci ssize_t ret = 0; 68094332d3Sopenharmony_ci do { 69094332d3Sopenharmony_ci ret = TEMP_FAILURE_RETRY(write(fd, data, length)); 70094332d3Sopenharmony_ci } while (ret == -1 && errno == EAGAIN); 71094332d3Sopenharmony_ci 72094332d3Sopenharmony_ci if (ret == -1) { 73094332d3Sopenharmony_ci strerror_r(errno, buf, sizeof(buf)); 74094332d3Sopenharmony_ci HDF_LOGE("write failed err:%s", buf); 75094332d3Sopenharmony_ci } else if (static_cast<size_t>(ret) != length) { 76094332d3Sopenharmony_ci HDF_LOGE("write data %zd less than %zu.", ret, length); 77094332d3Sopenharmony_ci } 78094332d3Sopenharmony_ci return ret; 79094332d3Sopenharmony_ci} 80094332d3Sopenharmony_ci} // namespace Hci 81094332d3Sopenharmony_ci} // namespace Bluetooth 82094332d3Sopenharmony_ci} // namespace HDI 83094332d3Sopenharmony_ci} // namespace OHOS