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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_fwk_socket_outputstream"
17 #endif
18
19 #include <unistd.h>
20 #include <cerrno>
21 #include "bluetooth_socket_outputstream.h"
22 #include "bluetooth_log.h"
23 #include "sys/socket.h"
24 #include "sys/ioctl.h"
25
26 namespace OHOS {
27 namespace Bluetooth {
28
29 static constexpr int32_t SOCKET_PACKET_HEAD_LENGTH = 1512;
30
OutputStream(int socketFd)31 OutputStream::OutputStream(int socketFd) : socketFd_(socketFd)
32 {}
33
~OutputStream()34 OutputStream::~OutputStream()
35 {}
36
Write(const uint8_t * buf,size_t length)37 int OutputStream::Write(const uint8_t *buf, size_t length)
38 {
39 if (socketFd_ == -1) {
40 HILOGE("socket closed.");
41 return -1;
42 }
43
44 int32_t bufSize;
45 socklen_t optlen = sizeof(bufSize);
46 int sockOptRet = getsockopt(socketFd_, SOL_SOCKET, SO_SNDBUF, &bufSize, &optlen);
47 unsigned long bytesInBuffer;
48 int ioctlRet = ioctl(socketFd_, TIOCOUTQ, &bytesInBuffer);
49 if (sockOptRet != -1 && ioctlRet != -1 && static_cast<unsigned long>(bufSize) > bytesInBuffer) { // -1代表无权限获取发送队列大小
50 // 该方法是跟踪send前socket发送通道是否占满导致发包阻塞
51 unsigned long availableLength = static_cast<unsigned long>(bufSize) - bytesInBuffer;
52 int32_t sendLength = static_cast<int32_t>(length) + SOCKET_PACKET_HEAD_LENGTH;
53 if (availableLength < static_cast<unsigned long>(sendLength)) {
54 HILOGW("send queue is full, availableLength is %{public}lu, sendlength is %{public}d",
55 availableLength, sendLength);
56 }
57 }
58 auto ret = send(socketFd_, buf, length, MSG_NOSIGNAL);
59
60 HILOGD("ret: %{public}zd", ret);
61
62 if (ret <= 0) {
63 HILOGE("socket write exception! ret:%{public}zd errno:%{public}d", ret, errno);
64 }
65 return ret;
66 }
67 } // namespace Bluetooth
68 } // namespace OHOS