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 <cinttypes>
22 #include "bluetooth_socket_outputstream.h"
23 #include "bluetooth_log.h"
24 #include "sys/socket.h"
25 #include "sys/ioctl.h"
26
27 namespace OHOS {
28 namespace Bluetooth {
29
30 static constexpr int32_t SOCKET_SEND_TIME_THRESHOLD = 1000; // 1000ms
31 static constexpr int32_t SOCKET_PACKET_HEAD_LENGTH = 1512;
GetNowTimestamp(void)32 static int64_t GetNowTimestamp(void)
33 {
34 struct timeval tv;
35 gettimeofday(&tv, nullptr);
36 int64_t timestamp = tv.tv_sec * 1000 + tv.tv_usec / 1000;
37 return timestamp;
38 }
39 static constexpr int32_t AAM_UID = 7878;
40 static constexpr int32_t AAM_BAD_RET = -978974;
41 static constexpr int32_t SOFTBUS_UID = 1024;
42 static constexpr int32_t SOCKET_WRITE_TIMEOUT_50_SEC = 50;
OutputStream(int socketFd)43 OutputStream::OutputStream(int socketFd) : socketFd_(socketFd)
44 {}
45
~OutputStream()46 OutputStream::~OutputStream()
47 {}
48
Write(const uint8_t * buf,size_t length)49 int OutputStream::Write(const uint8_t *buf, size_t length)
50 {
51 if (socketFd_ == -1) {
52 HILOGE("socket closed.");
53 return -1;
54 }
55
56 int32_t bufSize;
57 socklen_t optlen = sizeof(bufSize);
58 int sockOptRet = getsockopt(socketFd_, SOL_SOCKET, SO_SNDBUF, &bufSize, &optlen);
59 unsigned long bytesInBuffer;
60 int ioctlRet = ioctl(socketFd_, TIOCOUTQ, &bytesInBuffer);
61 if (sockOptRet != -1 && ioctlRet != -1 && static_cast<unsigned long>(bufSize) > bytesInBuffer) { // -1代表无权限获取发送队列大小
62 // 该方法是跟踪send前socket发送通道是否占满导致发包阻塞
63 unsigned long availableLength = static_cast<unsigned long>(bufSize) - bytesInBuffer;
64 int32_t sendLength = static_cast<int32_t>(length) + SOCKET_PACKET_HEAD_LENGTH;
65 if (availableLength < static_cast<unsigned long>(sendLength)) {
66 HILOGW("send queue is full, availableLength is %{public}lu, sendlength is %{public}d",
67 availableLength, sendLength);
68 if (getuid() == AAM_UID) {
69 HILOGE("AAM close socket");
70 return AAM_BAD_RET;
71 }
72 }
73 }
74 if (getuid() == SOFTBUS_UID && setTimeoutFlag_ == false) {
75 HILOGD("SOFTBUS write data set 50s tiemout.");
76 timeval timeout;
77 timeout.tv_sec = SOCKET_WRITE_TIMEOUT_50_SEC;
78 timeout.tv_usec = 0;
79 if (setsockopt(socketFd_, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0) {
80 HILOGW("Failed to set send timeout");
81 return -1;
82 }
83 setTimeoutFlag_ = true;
84 }
85 int64_t beginTimestamp = GetNowTimestamp();
86 auto ret = send(socketFd_, buf, length, MSG_NOSIGNAL);
87 int64_t endTimestamp = GetNowTimestamp();
88 if (endTimestamp - beginTimestamp > SOCKET_SEND_TIME_THRESHOLD) {
89 HILOGE("socket send time %{public}" PRId64, endTimestamp - beginTimestamp);
90 }
91
92 HILOGD("ret: %{public}zd", ret);
93
94 if (ret <= 0) {
95 setTimeoutFlag_ = false;
96 HILOGE("socket write exception! ret:%{public}zd errno:%{public}d", ret, errno);
97 return errno;
98 }
99 return ret;
100 }
101 } // namespace Bluetooth
102 } // namespace OHOS