195489c19Sopenharmony_ci/* 295489c19Sopenharmony_ci * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 395489c19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 495489c19Sopenharmony_ci * you may not use this file except in compliance with the License. 595489c19Sopenharmony_ci * You may obtain a copy of the License at 695489c19Sopenharmony_ci * 795489c19Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 895489c19Sopenharmony_ci * 995489c19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1095489c19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1195489c19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1295489c19Sopenharmony_ci * See the License for the specific language governing permissions and 1395489c19Sopenharmony_ci * limitations under the License. 1495489c19Sopenharmony_ci */ 1595489c19Sopenharmony_ci 1695489c19Sopenharmony_ci#include <unistd.h> 1795489c19Sopenharmony_ci#include <cerrno> 1895489c19Sopenharmony_ci#include <cinttypes> 1995489c19Sopenharmony_ci#include "bluetooth_socket_outputstream.h" 2095489c19Sopenharmony_ci#include "bluetooth_log.h" 2195489c19Sopenharmony_ci#include "sys/socket.h" 2295489c19Sopenharmony_ci 2395489c19Sopenharmony_cinamespace OHOS { 2495489c19Sopenharmony_cinamespace Bluetooth { 2595489c19Sopenharmony_ci 2695489c19Sopenharmony_cistatic constexpr int32_t SOCKET_SEND_TIME_THRESHOLD = 1000; // 1000ms 2795489c19Sopenharmony_cistatic int64_t GetNowTimestamp(void) 2895489c19Sopenharmony_ci{ 2995489c19Sopenharmony_ci struct timeval tv; 3095489c19Sopenharmony_ci gettimeofday(&tv, nullptr); 3195489c19Sopenharmony_ci int64_t timestamp = tv.tv_sec * 1000 + tv.tv_usec / 1000; 3295489c19Sopenharmony_ci return timestamp; 3395489c19Sopenharmony_ci} 3495489c19Sopenharmony_ci 3595489c19Sopenharmony_ciOutputStream::OutputStream(int socketFd) : socketFd_(socketFd) 3695489c19Sopenharmony_ci{} 3795489c19Sopenharmony_ci 3895489c19Sopenharmony_ciOutputStream::~OutputStream() 3995489c19Sopenharmony_ci{} 4095489c19Sopenharmony_ci 4195489c19Sopenharmony_ciint OutputStream::Write(const uint8_t *buf, size_t length) 4295489c19Sopenharmony_ci{ 4395489c19Sopenharmony_ci if (socketFd_ == -1) { 4495489c19Sopenharmony_ci HILOGE("socket closed."); 4595489c19Sopenharmony_ci return -1; 4695489c19Sopenharmony_ci } 4795489c19Sopenharmony_ci 4895489c19Sopenharmony_ci int64_t beginTimestamp = GetNowTimestamp(); 4995489c19Sopenharmony_ci auto ret = send(socketFd_, buf, length, MSG_NOSIGNAL); 5095489c19Sopenharmony_ci int64_t endTimestamp = GetNowTimestamp(); 5195489c19Sopenharmony_ci if (endTimestamp - beginTimestamp > SOCKET_SEND_TIME_THRESHOLD) { 5295489c19Sopenharmony_ci HILOGE("socket send time %{public}" PRId64, endTimestamp - beginTimestamp); 5395489c19Sopenharmony_ci } 5495489c19Sopenharmony_ci 5595489c19Sopenharmony_ci HILOGD("ret: %{public}zu", ret); 5695489c19Sopenharmony_ci 5795489c19Sopenharmony_ci if (ret == -1) { 5895489c19Sopenharmony_ci HILOGE("Error."); 5995489c19Sopenharmony_ci return -1; 6095489c19Sopenharmony_ci } 6195489c19Sopenharmony_ci return ret; 6295489c19Sopenharmony_ci} 6395489c19Sopenharmony_ci} // namespace Bluetooth 6495489c19Sopenharmony_ci} // namespace OHOS