195489c19Sopenharmony_ci/*
295489c19Sopenharmony_ci * Copyright (C) 2021-2023 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 "bluetooth_socket.h"
1795489c19Sopenharmony_ci
1895489c19Sopenharmony_ci#include <sys/socket.h>
1995489c19Sopenharmony_ci#include <string>
2095489c19Sopenharmony_ci#include <unistd.h>
2195489c19Sopenharmony_ci#include <atomic>
2295489c19Sopenharmony_ci#include "bluetooth_log.h"
2395489c19Sopenharmony_ci#include "bluetooth_host.h"
2495489c19Sopenharmony_ci#include "bluetooth_host_proxy.h"
2595489c19Sopenharmony_ci#include "bluetooth_utils.h"
2695489c19Sopenharmony_ci#include "bluetooth_socket_proxy.h"
2795489c19Sopenharmony_ci#include "hisysevent.h"
2895489c19Sopenharmony_ci#include "ipc_skeleton.h"
2995489c19Sopenharmony_ci#include "iservice_registry.h"
3095489c19Sopenharmony_ci#include "securec.h"
3195489c19Sopenharmony_ci#include "system_ability_definition.h"
3295489c19Sopenharmony_ci#include "raw_address.h"
3395489c19Sopenharmony_ci#include "bluetooth_socket_observer_stub.h"
3495489c19Sopenharmony_ci#include "bluetooth_profile_manager.h"
3595489c19Sopenharmony_ci#ifdef RES_SCHED_SUPPORT
3695489c19Sopenharmony_ci#include "res_type.h"
3795489c19Sopenharmony_ci#include "res_sched_client.h"
3895489c19Sopenharmony_ci#endif
3995489c19Sopenharmony_ci
4095489c19Sopenharmony_cinamespace OHOS {
4195489c19Sopenharmony_cinamespace Bluetooth {
4295489c19Sopenharmony_ciconst int LENGTH = 18;
4395489c19Sopenharmony_ciconst int MIN_BUFFER_SIZE_TO_SET = 4 * 1024; // 4KB
4495489c19Sopenharmony_ciconst int MAX_BUFFER_SIZE_TO_SET = 50 * 1024; // 50KB
4595489c19Sopenharmony_ciconst int ADDR_OFFSET = 1; // state(1)
4695489c19Sopenharmony_ciconst int TX_OFFSET = 7; // state(1)+addr(6)
4795489c19Sopenharmony_ciconst int RX_OFFSET = 9; // state(1)+addr(6)+tx(2)
4895489c19Sopenharmony_ciconst int SOCKET_RECV_ADDR_SIZE = 6;
4995489c19Sopenharmony_ciconst int SOCKET_RECV_TXRX_SIZE = 2;
5095489c19Sopenharmony_ciconst int SOCKET_RECV_CHANNEL_SIZE = 4;
5195489c19Sopenharmony_ciconst int SOCKET_RECV_FD_SIZE = 14;
5295489c19Sopenharmony_ciconst int SOCKET_RECV_FD_SIGNAL = 11; // state(1)+addr(6)+tx(2)+rx(2)
5395489c19Sopenharmony_ci
5495489c19Sopenharmony_ciconstexpr char BLUETOOTH_UE_DOMAIN[] = "BLUETOOTH_UE";
5595489c19Sopenharmony_cistd::mutex g_socketProxyMutex;
5695489c19Sopenharmony_ci
5795489c19Sopenharmony_ci#define SPTR_SOCKET_CBACK(cbSptr, func, ...)      \
5895489c19Sopenharmony_cido {                                            \
5995489c19Sopenharmony_ci    if (cbSptr) {                                \
6095489c19Sopenharmony_ci        cbSptr->func(__VA_ARGS__);               \
6195489c19Sopenharmony_ci    } else {                                     \
6295489c19Sopenharmony_ci        HILOGE(#cbSptr ": callback is nullptr"); \
6395489c19Sopenharmony_ci    }                                            \
6495489c19Sopenharmony_ci} while (0)
6595489c19Sopenharmony_ci
6695489c19Sopenharmony_cistatic void ReportDataToRss(const std::string &action, int id, const std::string &address, int pid, int uid)
6795489c19Sopenharmony_ci{
6895489c19Sopenharmony_ci#ifdef RES_SCHED_SUPPORT
6995489c19Sopenharmony_ci    HILOGD("report SPP_CONNECT_STATE");
7095489c19Sopenharmony_ci    std::unordered_map<std::string, std::string> payload;
7195489c19Sopenharmony_ci    payload["ACTION"] = action;
7295489c19Sopenharmony_ci    payload["ID"] = std::to_string(id);
7395489c19Sopenharmony_ci    payload["ADDRESS"] = address;
7495489c19Sopenharmony_ci    payload["PID"] = std::to_string(pid);
7595489c19Sopenharmony_ci    payload["UID"] = std::to_string(uid);
7695489c19Sopenharmony_ci    ResourceSchedule::ResSchedClient::GetInstance().ReportData(
7795489c19Sopenharmony_ci        OHOS::ResourceSchedule::ResType::RES_TYPE_BT_SERVICE_EVENT,
7895489c19Sopenharmony_ci        OHOS::ResourceSchedule::ResType::BtServiceEvent::SPP_CONNECT_STATE,
7995489c19Sopenharmony_ci        payload);
8095489c19Sopenharmony_ci#endif
8195489c19Sopenharmony_ci}
8295489c19Sopenharmony_ci
8395489c19Sopenharmony_cistruct ClientSocket::impl {
8495489c19Sopenharmony_ci    impl(const BluetoothRemoteDevice &addr, UUID uuid, BtSocketType type, bool auth);
8595489c19Sopenharmony_ci    impl(int fd, std::string address, BtSocketType type);
8695489c19Sopenharmony_ci    impl(const BluetoothRemoteDevice &addr, UUID uuid, BtSocketType type, bool auth,
8795489c19Sopenharmony_ci        std::shared_ptr<BluetoothConnectionObserver> observer);
8895489c19Sopenharmony_ci    ~impl()
8995489c19Sopenharmony_ci    {
9095489c19Sopenharmony_ci        HILOGI("ClientSocket::impl ~impl");
9195489c19Sopenharmony_ci        if (fd_ > 0) {
9295489c19Sopenharmony_ci            shutdown(fd_, SHUT_RD);
9395489c19Sopenharmony_ci            shutdown(fd_, SHUT_WR);
9495489c19Sopenharmony_ci            HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BLUETOOTH, "SPP_CONNECT_STATE",
9595489c19Sopenharmony_ci                HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", "close", "ID", fd_, "ADDRESS", "empty",
9695489c19Sopenharmony_ci                "PID", IPCSkeleton::GetCallingPid(), "UID", IPCSkeleton::GetCallingUid());
9795489c19Sopenharmony_ci            HiSysEventWrite(BLUETOOTH_UE_DOMAIN, "SOCKET_DISCONN", HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
9895489c19Sopenharmony_ci                "PNAMEID", "Bluetooth", "PVERSIONID", "1.0", "DEV_ADDRESS", GetEncryptAddr(address_),
9995489c19Sopenharmony_ci                "SCENE_CODE", fd_);
10095489c19Sopenharmony_ci            ReportDataToRss("close", fd_, "empty", IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
10195489c19Sopenharmony_ci            HILOGI("fd closed, fd_: %{public}d", fd_);
10295489c19Sopenharmony_ci            close(fd_);
10395489c19Sopenharmony_ci            fd_ = -1;
10495489c19Sopenharmony_ci        }
10595489c19Sopenharmony_ci    }
10695489c19Sopenharmony_ci
10795489c19Sopenharmony_ci    __attribute__((no_sanitize("cfi")))
10895489c19Sopenharmony_ci    void Close()
10995489c19Sopenharmony_ci    {
11095489c19Sopenharmony_ci        HILOGD("enter");
11195489c19Sopenharmony_ci        if (socketStatus_ == SOCKET_CLOSED) {
11295489c19Sopenharmony_ci            HILOGD("The socketStatus_ is already SOCKET_CLOSED");
11395489c19Sopenharmony_ci            return;
11495489c19Sopenharmony_ci        } else {
11595489c19Sopenharmony_ci            socketStatus_ = SOCKET_CLOSED;
11695489c19Sopenharmony_ci            if (fd_ > 0) {
11795489c19Sopenharmony_ci                shutdown(fd_, SHUT_RD);
11895489c19Sopenharmony_ci                shutdown(fd_, SHUT_WR);
11995489c19Sopenharmony_ci                HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BLUETOOTH, "SPP_CONNECT_STATE",
12095489c19Sopenharmony_ci                    HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", "close", "ID", fd_, "ADDRESS", "empty",
12195489c19Sopenharmony_ci                    "PID", IPCSkeleton::GetCallingPid(), "UID", IPCSkeleton::GetCallingUid());
12295489c19Sopenharmony_ci                HiSysEventWrite(BLUETOOTH_UE_DOMAIN, "SOCKET_DISCONN", HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
12395489c19Sopenharmony_ci                    "PNAMEID", "Bluetooth", "PVERSIONID", "1.0", "DEV_ADDRESS", GetEncryptAddr(address_),
12495489c19Sopenharmony_ci                    "SCENE_CODE", fd_);
12595489c19Sopenharmony_ci                ReportDataToRss("close", fd_, "empty", IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
12695489c19Sopenharmony_ci                HILOGI("fd closed, fd_: %{public}d", fd_);
12795489c19Sopenharmony_ci                close(fd_);
12895489c19Sopenharmony_ci                fd_ = -1;
12995489c19Sopenharmony_ci            } else {
13095489c19Sopenharmony_ci                HILOGE("socket not created");
13195489c19Sopenharmony_ci            }
13295489c19Sopenharmony_ci            sptr<IBluetoothSocket> proxy = GetRemoteProxy<IBluetoothSocket>(PROFILE_SOCKET);
13395489c19Sopenharmony_ci            CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
13495489c19Sopenharmony_ci            bluetooth::Uuid tempUuid = bluetooth::Uuid::ConvertFrom128Bits(uuid_.ConvertTo128Bits());
13595489c19Sopenharmony_ci            if (!observerImp_) {
13695489c19Sopenharmony_ci                HILOGD("observerImp_ is nullptr");
13795489c19Sopenharmony_ci                return;
13895489c19Sopenharmony_ci            }
13995489c19Sopenharmony_ci            proxy->DeregisterClientObserver(BluetoothRawAddress(remoteDevice_.GetDeviceAddr()), tempUuid,
14095489c19Sopenharmony_ci                observerImp_);
14195489c19Sopenharmony_ci        }
14295489c19Sopenharmony_ci    }
14395489c19Sopenharmony_ci
14495489c19Sopenharmony_ci    uint16_t GetPacketSizeFromBuf(const uint8_t recvBuf[], int recvBufLen) const
14595489c19Sopenharmony_ci    {
14695489c19Sopenharmony_ci        uint16_t shortBuf;
14795489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(recvBuf, 0, "getpacketsize fail, invalid recvBuf");
14895489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(recvBufLen >= SOCKET_RECV_TXRX_SIZE, 0, "getpacketsize fail, invalid recvBufLen");
14995489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(memcpy_s(&shortBuf, sizeof(shortBuf), &recvBuf[0], sizeof(shortBuf)) == EOK, 0,
15095489c19Sopenharmony_ci            "getpacketsize failed, memcpy_s fail");
15195489c19Sopenharmony_ci        return shortBuf;
15295489c19Sopenharmony_ci    }
15395489c19Sopenharmony_ci
15495489c19Sopenharmony_ci    bool RecvSocketSignal()
15595489c19Sopenharmony_ci    {
15695489c19Sopenharmony_ci        uint8_t signalBuf[SOCKET_RECV_FD_SIGNAL] = {0};
15795489c19Sopenharmony_ci#ifdef DARWIN_PLATFORM
15895489c19Sopenharmony_ci        int recvBufSize = recv(fd_, signalBuf, sizeof(signalBuf), 0);
15995489c19Sopenharmony_ci#else
16095489c19Sopenharmony_ci        int recvBufSize = recv(fd_, signalBuf, sizeof(signalBuf), MSG_WAITALL);
16195489c19Sopenharmony_ci#endif
16295489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(recvBufSize == SOCKET_RECV_FD_SIGNAL, false, "recv signal error, service closed");
16395489c19Sopenharmony_ci        bool state = signalBuf[0];
16495489c19Sopenharmony_ci        // remote addr has been obtained, no need obtain again
16595489c19Sopenharmony_ci        maxTxPacketSize_ = GetPacketSizeFromBuf(signalBuf + TX_OFFSET, SOCKET_RECV_FD_SIGNAL - TX_OFFSET);
16695489c19Sopenharmony_ci        maxRxPacketSize_ = GetPacketSizeFromBuf(signalBuf + RX_OFFSET, SOCKET_RECV_FD_SIGNAL - RX_OFFSET);
16795489c19Sopenharmony_ci
16895489c19Sopenharmony_ci        return state;
16995489c19Sopenharmony_ci    }
17095489c19Sopenharmony_ci
17195489c19Sopenharmony_ci    int getSecurityFlags()
17295489c19Sopenharmony_ci    {
17395489c19Sopenharmony_ci        int flags = 0;
17495489c19Sopenharmony_ci        if (auth_) {
17595489c19Sopenharmony_ci            flags |= FLAG_AUTH;
17695489c19Sopenharmony_ci            flags |= FLAG_ENCRYPT;
17795489c19Sopenharmony_ci        }
17895489c19Sopenharmony_ci        return flags;
17995489c19Sopenharmony_ci    }
18095489c19Sopenharmony_ci
18195489c19Sopenharmony_ci    std::shared_ptr<InputStream> GetInputStream()
18295489c19Sopenharmony_ci    {
18395489c19Sopenharmony_ci        HILOGD("enter");
18495489c19Sopenharmony_ci        if (inputStream_ == nullptr) {
18595489c19Sopenharmony_ci            HILOGE("inputStream is NULL, failed. please Connect");
18695489c19Sopenharmony_ci        }
18795489c19Sopenharmony_ci        return inputStream_;
18895489c19Sopenharmony_ci    }
18995489c19Sopenharmony_ci
19095489c19Sopenharmony_ci    std::shared_ptr<OutputStream> GetOutputStream()
19195489c19Sopenharmony_ci    {
19295489c19Sopenharmony_ci        HILOGD("enter");
19395489c19Sopenharmony_ci        if (outputStream_ == nullptr) {
19495489c19Sopenharmony_ci            HILOGE("outputStream is NULL, failed. please Connect");
19595489c19Sopenharmony_ci        }
19695489c19Sopenharmony_ci        return outputStream_;
19795489c19Sopenharmony_ci    }
19895489c19Sopenharmony_ci
19995489c19Sopenharmony_ci    BluetoothRemoteDevice &GetRemoteDevice()
20095489c19Sopenharmony_ci    {
20195489c19Sopenharmony_ci        HILOGD("enter");
20295489c19Sopenharmony_ci        return remoteDevice_;
20395489c19Sopenharmony_ci    }
20495489c19Sopenharmony_ci
20595489c19Sopenharmony_ci    bool IsConnected()
20695489c19Sopenharmony_ci    {
20795489c19Sopenharmony_ci        HILOGD("enter");
20895489c19Sopenharmony_ci        return socketStatus_ == SOCKET_CONNECTED;
20995489c19Sopenharmony_ci    }
21095489c19Sopenharmony_ci
21195489c19Sopenharmony_ci    int SetBufferSize(int bufferSize)
21295489c19Sopenharmony_ci    {
21395489c19Sopenharmony_ci        HILOGI("SetBufferSize bufferSize is %{public}d.", bufferSize);
21495489c19Sopenharmony_ci        if (bufferSize < MIN_BUFFER_SIZE_TO_SET || bufferSize > MAX_BUFFER_SIZE_TO_SET) {
21595489c19Sopenharmony_ci            HILOGE("SetBufferSize param is invalid.");
21695489c19Sopenharmony_ci            return RET_BAD_PARAM;
21795489c19Sopenharmony_ci        }
21895489c19Sopenharmony_ci
21995489c19Sopenharmony_ci        if (fd_ <= 0) {
22095489c19Sopenharmony_ci            HILOGE("SetBufferSize socket fd invalid.");
22195489c19Sopenharmony_ci            return RET_BAD_STATUS;
22295489c19Sopenharmony_ci        }
22395489c19Sopenharmony_ci
22495489c19Sopenharmony_ci        const std::pair<const char*, int> sockOpts[] = {
22595489c19Sopenharmony_ci            {"recvBuffer", SO_RCVBUF},
22695489c19Sopenharmony_ci            {"sendBuffer", SO_SNDBUF},
22795489c19Sopenharmony_ci        };
22895489c19Sopenharmony_ci        for (auto opt : sockOpts) {
22995489c19Sopenharmony_ci            int curSize = 0;
23095489c19Sopenharmony_ci            socklen_t optlen = sizeof(curSize);
23195489c19Sopenharmony_ci            if (getsockopt(fd_, SOL_SOCKET, opt.second, &curSize, &optlen) != 0) {
23295489c19Sopenharmony_ci                HILOGE("SetBufferSize getsockopt %{public}s failed.", opt.first);
23395489c19Sopenharmony_ci                return RET_BAD_STATUS;
23495489c19Sopenharmony_ci            }
23595489c19Sopenharmony_ci            HILOGI("SetBufferSize %{public}s before set size is %{public}d.", opt.first, curSize);
23695489c19Sopenharmony_ci
23795489c19Sopenharmony_ci            if (curSize != bufferSize) {
23895489c19Sopenharmony_ci                int setSize = bufferSize / 2;
23995489c19Sopenharmony_ci                if (setsockopt(fd_, SOL_SOCKET, opt.second, &setSize, sizeof(setSize)) != 0) {
24095489c19Sopenharmony_ci                    HILOGE("SetBufferSize setsockopt  %{public}s failed.", opt.first);
24195489c19Sopenharmony_ci                    return RET_BAD_STATUS;
24295489c19Sopenharmony_ci                }
24395489c19Sopenharmony_ci
24495489c19Sopenharmony_ci                curSize = 0;
24595489c19Sopenharmony_ci                if (getsockopt(fd_, SOL_SOCKET, opt.second, &curSize, &optlen) != 0) {
24695489c19Sopenharmony_ci                    HILOGE("SetBufferSize after getsockopt %{public}s failed.", opt.first);
24795489c19Sopenharmony_ci                    return RET_BAD_STATUS;
24895489c19Sopenharmony_ci                }
24995489c19Sopenharmony_ci                HILOGI("SetBufferSize %{public}s after set size is %{public}d.", opt.first, curSize);
25095489c19Sopenharmony_ci            }
25195489c19Sopenharmony_ci        }
25295489c19Sopenharmony_ci
25395489c19Sopenharmony_ci        return RET_NO_ERROR;
25495489c19Sopenharmony_ci    }
25595489c19Sopenharmony_ci
25695489c19Sopenharmony_ci    bool RecvSocketPsmOrScn()
25795489c19Sopenharmony_ci    {
25895489c19Sopenharmony_ci        int channel = 0;
25995489c19Sopenharmony_ci#ifdef DARWIN_PLATFORM
26095489c19Sopenharmony_ci        int recvBufSize = recv(fd_, &channel, sizeof(channel), 0);
26195489c19Sopenharmony_ci#else
26295489c19Sopenharmony_ci        int recvBufSize = recv(fd_, &channel, sizeof(channel), MSG_WAITALL);
26395489c19Sopenharmony_ci#endif
26495489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(recvBufSize == SOCKET_RECV_CHANNEL_SIZE, false,
26595489c19Sopenharmony_ci            "recv psm or scn error, errno:%{public}d, fd_:%{public}d", errno, fd_);
26695489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(channel > 0, false, "recv channel error, invalid channel:%{public}d", channel);
26795489c19Sopenharmony_ci        HILOGI("psm or scn = %{public}d, type = %{public}d", channel, type_);
26895489c19Sopenharmony_ci        socketChannel_ = channel;
26995489c19Sopenharmony_ci        return true;
27095489c19Sopenharmony_ci    }
27195489c19Sopenharmony_ci
27295489c19Sopenharmony_ci    // user register observer
27395489c19Sopenharmony_ci    std::shared_ptr<BluetoothConnectionObserver> observer_;
27495489c19Sopenharmony_ci    class BluetoothSocketObserverImp;
27595489c19Sopenharmony_ci
27695489c19Sopenharmony_ci    // socket observer
27795489c19Sopenharmony_ci    sptr<BluetoothSocketObserverImp> observerImp_ = nullptr;
27895489c19Sopenharmony_ci    std::shared_ptr<InputStream> inputStream_ {
27995489c19Sopenharmony_ci        nullptr
28095489c19Sopenharmony_ci    };
28195489c19Sopenharmony_ci    std::shared_ptr<OutputStream> outputStream_ {
28295489c19Sopenharmony_ci        nullptr
28395489c19Sopenharmony_ci    };
28495489c19Sopenharmony_ci    bool Init(std::weak_ptr<ClientSocket> client);
28595489c19Sopenharmony_ci    BluetoothRemoteDevice remoteDevice_;
28695489c19Sopenharmony_ci    UUID uuid_;
28795489c19Sopenharmony_ci    BtSocketType type_;
28895489c19Sopenharmony_ci    std::string address_;
28995489c19Sopenharmony_ci    int fd_;
29095489c19Sopenharmony_ci    bool auth_;
29195489c19Sopenharmony_ci    int socketStatus_;
29295489c19Sopenharmony_ci    std::atomic<int> socketChannel_{ -1 };
29395489c19Sopenharmony_ci    std::atomic<uint32_t> maxTxPacketSize_{ 0 };
29495489c19Sopenharmony_ci    std::atomic<uint32_t> maxRxPacketSize_{ 0 };
29595489c19Sopenharmony_ci};
29695489c19Sopenharmony_ci
29795489c19Sopenharmony_ciclass ClientSocket::impl::BluetoothSocketObserverImp : public BluetoothClientSocketObserverStub {
29895489c19Sopenharmony_cipublic:
29995489c19Sopenharmony_ci    inline std::shared_ptr<ClientSocket> GetClientSocketSptr(void)
30095489c19Sopenharmony_ci    {
30195489c19Sopenharmony_ci        auto clientSptr = clientSocket_.lock();
30295489c19Sopenharmony_ci        if (!clientSptr) {
30395489c19Sopenharmony_ci            HILOGE("clientSocket_ is nullptr");
30495489c19Sopenharmony_ci            return nullptr;
30595489c19Sopenharmony_ci        }
30695489c19Sopenharmony_ci        return clientSptr;
30795489c19Sopenharmony_ci    }
30895489c19Sopenharmony_ci
30995489c19Sopenharmony_ci    explicit BluetoothSocketObserverImp(std::weak_ptr<ClientSocket> clientSocket) : clientSocket_(clientSocket)
31095489c19Sopenharmony_ci    {
31195489c19Sopenharmony_ci        HILOGD("enter");
31295489c19Sopenharmony_ci    }
31395489c19Sopenharmony_ci    ~BluetoothSocketObserverImp()
31495489c19Sopenharmony_ci    {
31595489c19Sopenharmony_ci        HILOGD("enter");
31695489c19Sopenharmony_ci    }
31795489c19Sopenharmony_ci
31895489c19Sopenharmony_ci    __attribute__((no_sanitize("cfi")))
31995489c19Sopenharmony_ci    void OnConnectionStateChanged(const CallbackParam &callbackParam) override
32095489c19Sopenharmony_ci    {
32195489c19Sopenharmony_ci        HILOGD("dev: %{public}s, uuid:%{public}s, status: %{public}d, psm: %{public}d, result: %{public}d",
32295489c19Sopenharmony_ci            GetEncryptAddr((callbackParam.dev).GetAddress()).c_str(), callbackParam.uuid.ToString().c_str(),
32395489c19Sopenharmony_ci            callbackParam.status, callbackParam.psm, callbackParam.result);
32495489c19Sopenharmony_ci        BluetoothRemoteDevice device(callbackParam.dev.GetAddress(), BTTransport::ADAPTER_BREDR);
32595489c19Sopenharmony_ci        UUID btUuid = UUID::ConvertFrom128Bits(callbackParam.uuid.ConvertTo128Bits());
32695489c19Sopenharmony_ci        auto clientSptr = GetClientSocketSptr();
32795489c19Sopenharmony_ci        if (!clientSptr) {
32895489c19Sopenharmony_ci            HILOGE("clientSptr is nullptr");
32995489c19Sopenharmony_ci            return;
33095489c19Sopenharmony_ci        }
33195489c19Sopenharmony_ci        if (!clientSptr->pimpl) {
33295489c19Sopenharmony_ci            HILOGE("impl is nullptr");
33395489c19Sopenharmony_ci            return;
33495489c19Sopenharmony_ci        }
33595489c19Sopenharmony_ci        CallbackConnectParam callbackConnectParam = {
33695489c19Sopenharmony_ci            .addr = device,
33795489c19Sopenharmony_ci            .uuid = btUuid,
33895489c19Sopenharmony_ci            .status = callbackParam.status,
33995489c19Sopenharmony_ci            .result = callbackParam.result,
34095489c19Sopenharmony_ci            .type = callbackParam.type,
34195489c19Sopenharmony_ci            .psm = callbackParam.psm,
34295489c19Sopenharmony_ci        };
34395489c19Sopenharmony_ci        SPTR_SOCKET_CBACK(clientSptr->pimpl->observer_, OnConnectionStateChanged, callbackConnectParam);
34495489c19Sopenharmony_ci    }
34595489c19Sopenharmony_ci
34695489c19Sopenharmony_ciprivate:
34795489c19Sopenharmony_ci    std::weak_ptr<ClientSocket> clientSocket_;
34895489c19Sopenharmony_ci};
34995489c19Sopenharmony_ci
35095489c19Sopenharmony_cibool ClientSocket::impl::Init(std::weak_ptr<ClientSocket> client)
35195489c19Sopenharmony_ci{
35295489c19Sopenharmony_ci    if (observerImp_ != nullptr) {
35395489c19Sopenharmony_ci        return true;
35495489c19Sopenharmony_ci    }
35595489c19Sopenharmony_ci    observerImp_ = new(std::nothrow) BluetoothSocketObserverImp(client);
35695489c19Sopenharmony_ci    if (observerImp_ == nullptr) {
35795489c19Sopenharmony_ci        return false;
35895489c19Sopenharmony_ci    }
35995489c19Sopenharmony_ci    return true;
36095489c19Sopenharmony_ci}
36195489c19Sopenharmony_ci
36295489c19Sopenharmony_ciClientSocket::impl::impl(const BluetoothRemoteDevice &addr, UUID uuid, BtSocketType type, bool auth)
36395489c19Sopenharmony_ci    : inputStream_(nullptr),
36495489c19Sopenharmony_ci      outputStream_(nullptr),
36595489c19Sopenharmony_ci      remoteDevice_(addr),
36695489c19Sopenharmony_ci      uuid_(uuid),
36795489c19Sopenharmony_ci      type_(type),
36895489c19Sopenharmony_ci      fd_(-1),
36995489c19Sopenharmony_ci      auth_(auth),
37095489c19Sopenharmony_ci      socketStatus_(SOCKET_INIT)
37195489c19Sopenharmony_ci{
37295489c19Sopenharmony_ci    HILOGD("enter 4 parameters");
37395489c19Sopenharmony_ci}
37495489c19Sopenharmony_ci
37595489c19Sopenharmony_ciClientSocket::impl::impl(int fd, std::string address, BtSocketType type)
37695489c19Sopenharmony_ci    : inputStream_(std::make_unique<InputStream>(fd)),
37795489c19Sopenharmony_ci      outputStream_(std::make_unique<OutputStream>(fd)),
37895489c19Sopenharmony_ci      remoteDevice_(BluetoothRemoteDevice(address, 0)),
37995489c19Sopenharmony_ci      type_(type),
38095489c19Sopenharmony_ci      address_(address),
38195489c19Sopenharmony_ci      fd_(fd),
38295489c19Sopenharmony_ci      auth_(false),
38395489c19Sopenharmony_ci      socketStatus_(SOCKET_CONNECTED)
38495489c19Sopenharmony_ci{
38595489c19Sopenharmony_ci    HILOGD("enter 3 parameters");
38695489c19Sopenharmony_ci}
38795489c19Sopenharmony_ci
38895489c19Sopenharmony_ciClientSocket::impl::impl(const BluetoothRemoteDevice &addr, UUID uuid, BtSocketType type, bool auth,
38995489c19Sopenharmony_ci    std::shared_ptr<BluetoothConnectionObserver> observer)
39095489c19Sopenharmony_ci    : observer_(observer),
39195489c19Sopenharmony_ci      inputStream_(nullptr),
39295489c19Sopenharmony_ci      outputStream_(nullptr),
39395489c19Sopenharmony_ci      remoteDevice_(addr),
39495489c19Sopenharmony_ci      uuid_(uuid),
39595489c19Sopenharmony_ci      type_(type),
39695489c19Sopenharmony_ci      fd_(-1),
39795489c19Sopenharmony_ci      auth_(auth),
39895489c19Sopenharmony_ci      socketStatus_(SOCKET_INIT)
39995489c19Sopenharmony_ci{
40095489c19Sopenharmony_ci    HILOGD("enter 5 parameters");
40195489c19Sopenharmony_ci}
40295489c19Sopenharmony_ci
40395489c19Sopenharmony_ciClientSocket::ClientSocket(const BluetoothRemoteDevice &bda, UUID uuid, BtSocketType type, bool auth)
40495489c19Sopenharmony_ci    : pimpl(new ClientSocket::impl(bda, uuid, type, auth))
40595489c19Sopenharmony_ci{}
40695489c19Sopenharmony_ci
40795489c19Sopenharmony_ciClientSocket::ClientSocket(int fd, std::string address, BtSocketType type)
40895489c19Sopenharmony_ci    : pimpl(new ClientSocket::impl(fd, address, type))
40995489c19Sopenharmony_ci{}
41095489c19Sopenharmony_ci
41195489c19Sopenharmony_ciClientSocket::ClientSocket(const BluetoothRemoteDevice &bda, UUID uuid, BtSocketType type, bool auth,
41295489c19Sopenharmony_ci    std::shared_ptr<BluetoothConnectionObserver> observer)
41395489c19Sopenharmony_ci    : pimpl(new ClientSocket::impl(bda, uuid, type, auth, observer))
41495489c19Sopenharmony_ci{}
41595489c19Sopenharmony_ci
41695489c19Sopenharmony_ciClientSocket::~ClientSocket()
41795489c19Sopenharmony_ci{}
41895489c19Sopenharmony_ci
41995489c19Sopenharmony_cibool ClientSocket::Init()
42095489c19Sopenharmony_ci{
42195489c19Sopenharmony_ci    HILOGI("ClientSocket Init");
42295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, false, "pimpl is nullptr!");
42395489c19Sopenharmony_ci    return pimpl->Init(weak_from_this());
42495489c19Sopenharmony_ci}
42595489c19Sopenharmony_ci
42695489c19Sopenharmony_ciint ClientSocket::Connect(int psm)
42795489c19Sopenharmony_ci{
42895489c19Sopenharmony_ci    HILOGD("enter");
42995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
43095489c19Sopenharmony_ci    if (pimpl->type_ == TYPE_L2CAP_LE) {
43195489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(IS_BLE_ENABLED(), BT_ERR_INVALID_STATE, "BLE is not TURN_ON");
43295489c19Sopenharmony_ci    } else {
43395489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "BR is not TURN_ON");
43495489c19Sopenharmony_ci    }
43595489c19Sopenharmony_ci
43695489c19Sopenharmony_ci    if (!pimpl->Init(weak_from_this())) {
43795489c19Sopenharmony_ci        HILOGE("clientSocket proxy is nullptr");
43895489c19Sopenharmony_ci        return BT_ERR_INTERNAL_ERROR;
43995489c19Sopenharmony_ci    }
44095489c19Sopenharmony_ci
44195489c19Sopenharmony_ci    pimpl->address_ = pimpl->remoteDevice_.GetDeviceAddr();
44295489c19Sopenharmony_ci    std::string tempAddress = pimpl->address_;
44395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(tempAddress.size(), BtStatus::BT_FAILURE, "address size error");
44495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl->socketStatus_ != SOCKET_CLOSED, BT_ERR_INVALID_STATE, "socket closed");
44595489c19Sopenharmony_ci    sptr<IBluetoothSocket> proxy = GetRemoteProxy<IBluetoothSocket>(PROFILE_SOCKET);
44695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_SERVICE_DISCONNECTED, "proxy is nullptr");
44795489c19Sopenharmony_ci
44895489c19Sopenharmony_ci    bluetooth::Uuid tempUuid = bluetooth::Uuid::ConvertFrom128Bits(pimpl->uuid_.ConvertTo128Bits());
44995489c19Sopenharmony_ci    int ret = proxy->RegisterClientObserver(BluetoothRawAddress(pimpl->address_), tempUuid,
45095489c19Sopenharmony_ci        pimpl->observerImp_);
45195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(ret == BT_NO_ERROR, ret, "regitser observer fail, ret = %d", ret);
45295489c19Sopenharmony_ci
45395489c19Sopenharmony_ci    ConnectSocketParam param {
45495489c19Sopenharmony_ci        .addr = tempAddress,
45595489c19Sopenharmony_ci        .uuid = tempUuid,
45695489c19Sopenharmony_ci        .securityFlag = (int32_t)pimpl->getSecurityFlags(),
45795489c19Sopenharmony_ci        .type = (int32_t)pimpl->type_,
45895489c19Sopenharmony_ci        .psm = psm
45995489c19Sopenharmony_ci    };
46095489c19Sopenharmony_ci    ret = proxy->Connect(param, pimpl->fd_);
46195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(ret == BT_NO_ERROR, ret, "Connect error %{public}d", ret);
46295489c19Sopenharmony_ci
46395489c19Sopenharmony_ci    HILOGI("fd_: %{public}d", pimpl->fd_);
46495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl->fd_ != -1, BtStatus::BT_FAILURE, "connect failed!");
46595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl->RecvSocketPsmOrScn(), BT_ERR_INVALID_STATE, "recv psm or scn failed");
46695489c19Sopenharmony_ci
46795489c19Sopenharmony_ci    bool recvret = pimpl->RecvSocketSignal();
46895489c19Sopenharmony_ci    HILOGI("recvret: %{public}d", recvret);
46995489c19Sopenharmony_ci    pimpl->inputStream_ = std::make_unique<InputStream>(pimpl->fd_);
47095489c19Sopenharmony_ci    pimpl->outputStream_ = std::make_unique<OutputStream>(pimpl->fd_);
47195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(recvret, BtStatus::BT_FAILURE, "recvSocketSignal connect failed!");
47295489c19Sopenharmony_ci    pimpl->socketStatus_ = SOCKET_CONNECTED;
47395489c19Sopenharmony_ci    HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BLUETOOTH, "SPP_CONNECT_STATE",
47495489c19Sopenharmony_ci        HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", "connect", "ID", pimpl->fd_, "ADDRESS",
47595489c19Sopenharmony_ci        GetEncryptAddr(tempAddress), "PID", IPCSkeleton::GetCallingPid(), "UID", IPCSkeleton::GetCallingUid());
47695489c19Sopenharmony_ci    ReportDataToRss("connect", pimpl->fd_, GetEncryptAddr(tempAddress), IPCSkeleton::GetCallingPid(),
47795489c19Sopenharmony_ci        IPCSkeleton::GetCallingUid());
47895489c19Sopenharmony_ci    return BtStatus::BT_SUCCESS;
47995489c19Sopenharmony_ci}
48095489c19Sopenharmony_ci
48195489c19Sopenharmony_civoid ClientSocket::Close()
48295489c19Sopenharmony_ci{
48395489c19Sopenharmony_ci    HILOGD("enter");
48495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is nullptr!");
48595489c19Sopenharmony_ci    return pimpl->Close();
48695489c19Sopenharmony_ci}
48795489c19Sopenharmony_ci
48895489c19Sopenharmony_cistd::shared_ptr<InputStream> ClientSocket::GetInputStream()
48995489c19Sopenharmony_ci{
49095489c19Sopenharmony_ci    HILOGD("enter");
49195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, nullptr, "pimpl is nullptr!");
49295489c19Sopenharmony_ci    return pimpl->GetInputStream();
49395489c19Sopenharmony_ci}
49495489c19Sopenharmony_ci
49595489c19Sopenharmony_cistd::shared_ptr<OutputStream> ClientSocket::GetOutputStream()
49695489c19Sopenharmony_ci{
49795489c19Sopenharmony_ci    HILOGD("enter");
49895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, nullptr, "pimpl is nullptr!");
49995489c19Sopenharmony_ci    return pimpl->GetOutputStream();
50095489c19Sopenharmony_ci}
50195489c19Sopenharmony_ci
50295489c19Sopenharmony_ciBluetoothRemoteDevice &ClientSocket::GetRemoteDevice()
50395489c19Sopenharmony_ci{
50495489c19Sopenharmony_ci    HILOGD("enter");
50595489c19Sopenharmony_ci    return pimpl->GetRemoteDevice();
50695489c19Sopenharmony_ci}
50795489c19Sopenharmony_ci
50895489c19Sopenharmony_cibool ClientSocket::IsConnected() const
50995489c19Sopenharmony_ci{
51095489c19Sopenharmony_ci    HILOGD("enter");
51195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, false, "pimpl is nullptr!");
51295489c19Sopenharmony_ci    return pimpl->IsConnected();
51395489c19Sopenharmony_ci}
51495489c19Sopenharmony_ci
51595489c19Sopenharmony_ciint ClientSocket::SetBufferSize(int bufferSize)
51695489c19Sopenharmony_ci{
51795489c19Sopenharmony_ci    HILOGD("enter");
51895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, false, "pimpl is nullptr!");
51995489c19Sopenharmony_ci    return pimpl->SetBufferSize(bufferSize);
52095489c19Sopenharmony_ci}
52195489c19Sopenharmony_ci
52295489c19Sopenharmony_ciint ClientSocket::GetSocketFd()
52395489c19Sopenharmony_ci{
52495489c19Sopenharmony_ci    HILOGD("enter");
52595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
52695489c19Sopenharmony_ci    return pimpl->fd_;
52795489c19Sopenharmony_ci}
52895489c19Sopenharmony_ci
52995489c19Sopenharmony_ciint ClientSocket::GetL2capPsm()
53095489c19Sopenharmony_ci{
53195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
53295489c19Sopenharmony_ci    HILOGI("psm:%{public}d", pimpl->socketChannel_.load());
53395489c19Sopenharmony_ci    return pimpl->socketChannel_;
53495489c19Sopenharmony_ci}
53595489c19Sopenharmony_ci
53695489c19Sopenharmony_ciint ClientSocket::GetRfcommScn()
53795489c19Sopenharmony_ci{
53895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
53995489c19Sopenharmony_ci    HILOGI("scn:%{public}d", pimpl->socketChannel_.load());
54095489c19Sopenharmony_ci    return pimpl->socketChannel_;
54195489c19Sopenharmony_ci}
54295489c19Sopenharmony_ci
54395489c19Sopenharmony_ciuint32_t ClientSocket::GetMaxTransmitPacketSize()
54495489c19Sopenharmony_ci{
54595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
54695489c19Sopenharmony_ci    HILOGI("MaxTransmitPacketSize:%{public}d", pimpl->maxTxPacketSize_.load());
54795489c19Sopenharmony_ci    return pimpl->maxTxPacketSize_;
54895489c19Sopenharmony_ci}
54995489c19Sopenharmony_ci
55095489c19Sopenharmony_ciuint32_t ClientSocket::GetMaxReceivePacketSize()
55195489c19Sopenharmony_ci{
55295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
55395489c19Sopenharmony_ci    HILOGI("MaxReceivePacketSize:%{public}d", pimpl->maxRxPacketSize_.load());
55495489c19Sopenharmony_ci    return pimpl->maxRxPacketSize_;
55595489c19Sopenharmony_ci}
55695489c19Sopenharmony_ci
55795489c19Sopenharmony_cistruct ServerSocket::impl {
55895489c19Sopenharmony_ci    impl(const std::string &name, UUID uuid, BtSocketType type, bool encrypt);
55995489c19Sopenharmony_ci    ~impl()
56095489c19Sopenharmony_ci    {
56195489c19Sopenharmony_ci        if (fd_ > 0) {
56295489c19Sopenharmony_ci            shutdown(fd_, SHUT_RD);
56395489c19Sopenharmony_ci            shutdown(fd_, SHUT_WR);
56495489c19Sopenharmony_ci            close(fd_);
56595489c19Sopenharmony_ci            HILOGI("fd closed, fd_: %{public}d", fd_);
56695489c19Sopenharmony_ci            fd_ = -1;
56795489c19Sopenharmony_ci        }
56895489c19Sopenharmony_ci    }
56995489c19Sopenharmony_ci
57095489c19Sopenharmony_ci    int Listen()
57195489c19Sopenharmony_ci    {
57295489c19Sopenharmony_ci        HILOGD("enter");
57395489c19Sopenharmony_ci        if (type_ == TYPE_L2CAP_LE) {
57495489c19Sopenharmony_ci            CHECK_AND_RETURN_LOG_RET(IS_BLE_ENABLED(), BT_ERR_INVALID_STATE, "BLE is not TURN_ON");
57595489c19Sopenharmony_ci        } else {
57695489c19Sopenharmony_ci            CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "BR is not TURN_ON");
57795489c19Sopenharmony_ci        }
57895489c19Sopenharmony_ci
57995489c19Sopenharmony_ci        sptr<IBluetoothSocket> proxy = GetRemoteProxy<IBluetoothSocket>(PROFILE_SOCKET);
58095489c19Sopenharmony_ci        if (!proxy) {
58195489c19Sopenharmony_ci            HILOGE("failed, proxy is nullptr");
58295489c19Sopenharmony_ci            socketStatus_ = SOCKET_CLOSED;
58395489c19Sopenharmony_ci            return BT_ERR_SERVICE_DISCONNECTED;
58495489c19Sopenharmony_ci        }
58595489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(socketStatus_ != SOCKET_CLOSED, BT_ERR_INVALID_STATE,
58695489c19Sopenharmony_ci            "failed, socketStatus_ is SOCKET_CLOSED");
58795489c19Sopenharmony_ci
58895489c19Sopenharmony_ci        ListenSocketParam param {
58995489c19Sopenharmony_ci            .name = name_,
59095489c19Sopenharmony_ci            .uuid = bluetooth::Uuid::ConvertFrom128Bits(uuid_.ConvertTo128Bits()),
59195489c19Sopenharmony_ci            .securityFlag = (int32_t)getSecurityFlags(),
59295489c19Sopenharmony_ci            .type = (int32_t)type_,
59395489c19Sopenharmony_ci            .observer = observer_
59495489c19Sopenharmony_ci        };
59595489c19Sopenharmony_ci        int ret = proxy->Listen(param, fd_);
59695489c19Sopenharmony_ci        if (ret != BT_NO_ERROR) {
59795489c19Sopenharmony_ci            HILOGE("Listen error %{public}d.", ret);
59895489c19Sopenharmony_ci            socketStatus_ = SOCKET_CLOSED;
59995489c19Sopenharmony_ci            return ret;
60095489c19Sopenharmony_ci        }
60195489c19Sopenharmony_ci
60295489c19Sopenharmony_ci        if (fd_ == BT_INVALID_SOCKET_FD) {
60395489c19Sopenharmony_ci            HILOGE("listen socket failed");
60495489c19Sopenharmony_ci            socketStatus_ = SOCKET_CLOSED;
60595489c19Sopenharmony_ci            return BT_ERR_INVALID_STATE;
60695489c19Sopenharmony_ci        }
60795489c19Sopenharmony_ci
60895489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(RecvSocketPsmOrScn(), BT_ERR_INVALID_STATE, "recv psm or scn failed");
60995489c19Sopenharmony_ci
61095489c19Sopenharmony_ci        if (socketStatus_ == SOCKET_INIT) {
61195489c19Sopenharmony_ci            socketStatus_ = SOCKET_LISTENING;
61295489c19Sopenharmony_ci        } else {
61395489c19Sopenharmony_ci            HILOGE("failed, socketStatus_: %{public}d is not SOCKET_INIT", socketStatus_);
61495489c19Sopenharmony_ci            close(fd_);
61595489c19Sopenharmony_ci            socketStatus_ = SOCKET_CLOSED;
61695489c19Sopenharmony_ci            return BT_ERR_INVALID_STATE;
61795489c19Sopenharmony_ci        }
61895489c19Sopenharmony_ci
61995489c19Sopenharmony_ci        return BT_NO_ERROR;
62095489c19Sopenharmony_ci    }
62195489c19Sopenharmony_ci
62295489c19Sopenharmony_ci    int getSecurityFlags()
62395489c19Sopenharmony_ci    {
62495489c19Sopenharmony_ci        int flags = 0;
62595489c19Sopenharmony_ci        if (encrypt_) {
62695489c19Sopenharmony_ci            flags |= FLAG_AUTH;
62795489c19Sopenharmony_ci            flags |= FLAG_ENCRYPT;
62895489c19Sopenharmony_ci        }
62995489c19Sopenharmony_ci        return flags;
63095489c19Sopenharmony_ci    }
63195489c19Sopenharmony_ci
63295489c19Sopenharmony_ci    std::shared_ptr<ClientSocket> Accept(int timeout)
63395489c19Sopenharmony_ci    {
63495489c19Sopenharmony_ci        HILOGD("enter");
63595489c19Sopenharmony_ci        if (socketStatus_ != SOCKET_LISTENING) {
63695489c19Sopenharmony_ci            HILOGE("socket is not in listen state");
63795489c19Sopenharmony_ci            return nullptr;
63895489c19Sopenharmony_ci        }
63995489c19Sopenharmony_ci        struct timeval time = {timeout, 0};
64095489c19Sopenharmony_ci        setsockopt(fd_, SOL_SOCKET, SO_SNDTIMEO, (const char *)&time, sizeof(time));
64195489c19Sopenharmony_ci        setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, (const char *)&time, sizeof(time));
64295489c19Sopenharmony_ci
64395489c19Sopenharmony_ci        acceptFd_ = RecvSocketFd();
64495489c19Sopenharmony_ci        HILOGI("RecvSocketFd acceptFd: %{public}d", acceptFd_);
64595489c19Sopenharmony_ci        if (acceptFd_ <= 0) {
64695489c19Sopenharmony_ci            return nullptr;
64795489c19Sopenharmony_ci        }
64895489c19Sopenharmony_ci        if (timeout > 0) {
64995489c19Sopenharmony_ci            time = {0, 0};
65095489c19Sopenharmony_ci            setsockopt(fd_, SOL_SOCKET, SO_SNDTIMEO, (const char *)&time, sizeof(time));
65195489c19Sopenharmony_ci            setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, (const char *)&time, sizeof(time));
65295489c19Sopenharmony_ci        }
65395489c19Sopenharmony_ci
65495489c19Sopenharmony_ci        std::shared_ptr<ClientSocket> clientSocket = std::make_shared<ClientSocket>(acceptFd_, acceptAddress_, type_);
65595489c19Sopenharmony_ci
65695489c19Sopenharmony_ci        HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BLUETOOTH, "SPP_CONNECT_STATE",
65795489c19Sopenharmony_ci            HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", "connect", "ID", acceptFd_, "ADDRESS",
65895489c19Sopenharmony_ci            GetEncryptAddr(acceptAddress_), "PID", IPCSkeleton::GetCallingPid(), "UID", IPCSkeleton::GetCallingUid());
65995489c19Sopenharmony_ci        ReportDataToRss("connect", acceptFd_, GetEncryptAddr(acceptAddress_), IPCSkeleton::GetCallingPid(),
66095489c19Sopenharmony_ci            IPCSkeleton::GetCallingUid());
66195489c19Sopenharmony_ci        return clientSocket;
66295489c19Sopenharmony_ci    }
66395489c19Sopenharmony_ci
66495489c19Sopenharmony_ci    int RecvSocketFd()
66595489c19Sopenharmony_ci    {
66695489c19Sopenharmony_ci        HILOGD("enter");
66795489c19Sopenharmony_ci        char ccmsg[CMSG_SPACE(sizeof(int))];
66895489c19Sopenharmony_ci        char buffer[SOCKET_RECV_FD_SIZE];
66995489c19Sopenharmony_ci        struct iovec io = {.iov_base = buffer, .iov_len = sizeof(buffer)};
67095489c19Sopenharmony_ci        struct msghdr msg;
67195489c19Sopenharmony_ci        (void)memset_s(&msg, sizeof(msg), 0, sizeof(msg));
67295489c19Sopenharmony_ci        msg.msg_control = ccmsg;
67395489c19Sopenharmony_ci        msg.msg_controllen = sizeof(ccmsg);
67495489c19Sopenharmony_ci        msg.msg_iov = &io;
67595489c19Sopenharmony_ci        msg.msg_iovlen = 1;
67695489c19Sopenharmony_ci
67795489c19Sopenharmony_ci#ifdef DARWIN_PLATFORM
67895489c19Sopenharmony_ci        int rv = recvmsg(fd_, &msg, 0);
67995489c19Sopenharmony_ci#else
68095489c19Sopenharmony_ci        int rv = recvmsg(fd_, &msg, MSG_NOSIGNAL);
68195489c19Sopenharmony_ci#endif
68295489c19Sopenharmony_ci        if (rv == -1) {
68395489c19Sopenharmony_ci            HILOGE("[sock] recvmsg error  %{public}d, fd: %{public}d", errno, fd_);
68495489c19Sopenharmony_ci            return BtStatus::BT_FAILURE;
68595489c19Sopenharmony_ci        }
68695489c19Sopenharmony_ci        struct cmsghdr *cmptr = CMSG_FIRSTHDR(&msg);
68795489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(cmptr != nullptr, BtStatus::BT_FAILURE, "cmptr error");
68895489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(cmptr->cmsg_len == CMSG_LEN(sizeof(int)) && cmptr->cmsg_level == SOL_SOCKET
68995489c19Sopenharmony_ci            && cmptr->cmsg_type == SCM_RIGHTS, BtStatus::BT_FAILURE,
69095489c19Sopenharmony_ci            "recvmsg error, len:%{public}d level:%{public}d type:%{public}d",
69195489c19Sopenharmony_ci            cmptr->cmsg_len, cmptr->cmsg_level, cmptr->cmsg_type);
69295489c19Sopenharmony_ci        int clientFd = *(reinterpret_cast<int *>(CMSG_DATA(cmptr)));
69395489c19Sopenharmony_ci
69495489c19Sopenharmony_ci        uint8_t recvBuf[rv];
69595489c19Sopenharmony_ci        (void)memset_s(&recvBuf, sizeof(recvBuf), 0, sizeof(recvBuf));
69695489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(memcpy_s(recvBuf, sizeof(recvBuf), (uint8_t *)msg.msg_iov[0].iov_base, rv) == EOK,
69795489c19Sopenharmony_ci            BtStatus::BT_FAILURE, "RecvSocketFd, recvBuf memcpy_s fail");
69895489c19Sopenharmony_ci
69995489c19Sopenharmony_ci        uint8_t buf[SOCKET_RECV_ADDR_SIZE] = {0};
70095489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(memcpy_s(buf, sizeof(buf), &recvBuf[ADDR_OFFSET], sizeof(buf)) == EOK,
70195489c19Sopenharmony_ci            BtStatus::BT_FAILURE, "RecvSocketFd, buf memcpy_s fail");
70295489c19Sopenharmony_ci
70395489c19Sopenharmony_ci        char token[LENGTH] = {0};
70495489c19Sopenharmony_ci        (void)sprintf_s(token, sizeof(token), "%02X:%02X:%02X:%02X:%02X:%02X",
70595489c19Sopenharmony_ci            buf[0x05], buf[0x04], buf[0x03], buf[0x02], buf[0x01], buf[0x00]);
70695489c19Sopenharmony_ci        BluetoothRawAddress rawAddr {token};
70795489c19Sopenharmony_ci        acceptAddress_ = rawAddr.GetAddress().c_str();
70895489c19Sopenharmony_ci
70995489c19Sopenharmony_ci        maxTxPacketSize_ = GetPacketSizeFromBuf(recvBuf + TX_OFFSET, rv - TX_OFFSET);
71095489c19Sopenharmony_ci        maxRxPacketSize_ = GetPacketSizeFromBuf(recvBuf + RX_OFFSET, rv - RX_OFFSET);
71195489c19Sopenharmony_ci        return clientFd;
71295489c19Sopenharmony_ci    }
71395489c19Sopenharmony_ci
71495489c19Sopenharmony_ci    uint16_t GetPacketSizeFromBuf(uint8_t recvBuf[], int recvBufLen)
71595489c19Sopenharmony_ci    {
71695489c19Sopenharmony_ci        uint16_t shortBuf;
71795489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(recvBuf, 0, "getpacketsize fail, invalid recvBuf");
71895489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(recvBufLen >= SOCKET_RECV_TXRX_SIZE, 0, "getpacketsize fail, invalid recvBufLen");
71995489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(memcpy_s(&shortBuf, sizeof(shortBuf), &recvBuf[0], sizeof(shortBuf)) == EOK, 0,
72095489c19Sopenharmony_ci            "getpacketsize failed, memcpy_s fail");
72195489c19Sopenharmony_ci        return shortBuf;
72295489c19Sopenharmony_ci    }
72395489c19Sopenharmony_ci
72495489c19Sopenharmony_ci    bool RecvSocketPsmOrScn()
72595489c19Sopenharmony_ci    {
72695489c19Sopenharmony_ci        int channel = 0;
72795489c19Sopenharmony_ci#ifdef DARWIN_PLATFORM
72895489c19Sopenharmony_ci        int recvBufSize = recv(fd_, &channel, sizeof(channel), 0);
72995489c19Sopenharmony_ci#else
73095489c19Sopenharmony_ci        int recvBufSize = recv(fd_, &channel, sizeof(channel), MSG_WAITALL);
73195489c19Sopenharmony_ci#endif
73295489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(recvBufSize == SOCKET_RECV_CHANNEL_SIZE, false,
73395489c19Sopenharmony_ci            "recv psm or scn error, errno:%{public}d, fd_:%{public}d", errno, fd_);
73495489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(channel > 0, false,
73595489c19Sopenharmony_ci            "recv channel error, errno:%{public}d, fd_:%{public}d", errno, fd_);
73695489c19Sopenharmony_ci        HILOGI("psm or scn = %{public}d, type = %{public}d", channel, type_);
73795489c19Sopenharmony_ci        socketChannel_ = channel;
73895489c19Sopenharmony_ci        return true;
73995489c19Sopenharmony_ci    }
74095489c19Sopenharmony_ci
74195489c19Sopenharmony_ci    void Close()
74295489c19Sopenharmony_ci    {
74395489c19Sopenharmony_ci        HILOGD("enter");
74495489c19Sopenharmony_ci        if (socketStatus_ == SOCKET_CLOSED) {
74595489c19Sopenharmony_ci            HILOGD("The socketStatus_ is already SOCKET_CLOSED");
74695489c19Sopenharmony_ci            return;
74795489c19Sopenharmony_ci        } else {
74895489c19Sopenharmony_ci            socketStatus_ = SOCKET_CLOSED;
74995489c19Sopenharmony_ci            if (fd_ > 0) {
75095489c19Sopenharmony_ci                shutdown(fd_, SHUT_RD);
75195489c19Sopenharmony_ci                shutdown(fd_, SHUT_WR);
75295489c19Sopenharmony_ci                HILOGI("fd closed, fd_: %{public}d", fd_);
75395489c19Sopenharmony_ci                close(fd_);
75495489c19Sopenharmony_ci                fd_ = -1;
75595489c19Sopenharmony_ci                sptr<IBluetoothSocket> proxy = GetRemoteProxy<IBluetoothSocket>(PROFILE_SOCKET);
75695489c19Sopenharmony_ci                CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
75795489c19Sopenharmony_ci                proxy->DeregisterServerObserver(observer_);
75895489c19Sopenharmony_ci                return;
75995489c19Sopenharmony_ci            } else {
76095489c19Sopenharmony_ci                HILOGE("socket not created");
76195489c19Sopenharmony_ci                return;
76295489c19Sopenharmony_ci            }
76395489c19Sopenharmony_ci        }
76495489c19Sopenharmony_ci    }
76595489c19Sopenharmony_ci
76695489c19Sopenharmony_ci    const std::string &GetStringTag()
76795489c19Sopenharmony_ci    {
76895489c19Sopenharmony_ci        HILOGD("enter");
76995489c19Sopenharmony_ci        if (socketStatus_ == SOCKET_CLOSED) {
77095489c19Sopenharmony_ci            HILOGE("socketStatus_ is SOCKET_CLOSED");
77195489c19Sopenharmony_ci            socketServiceType_ = "";
77295489c19Sopenharmony_ci        } else {
77395489c19Sopenharmony_ci            socketServiceType_ = "ServerSocket:";
77495489c19Sopenharmony_ci            socketServiceType_.append(" Type: ").append(ConvertTypeToString(type_))
77595489c19Sopenharmony_ci                .append(" ServerName: ").append(name_);
77695489c19Sopenharmony_ci        }
77795489c19Sopenharmony_ci        return socketServiceType_;
77895489c19Sopenharmony_ci    }
77995489c19Sopenharmony_ci
78095489c19Sopenharmony_ci    static std::string ConvertTypeToString(BtSocketType type)
78195489c19Sopenharmony_ci    {
78295489c19Sopenharmony_ci        std::string retStr;
78395489c19Sopenharmony_ci        if (type == TYPE_RFCOMM) {
78495489c19Sopenharmony_ci            retStr = "TYPE_RFCOMM";
78595489c19Sopenharmony_ci        } else if (type == TYPE_L2CAP) {
78695489c19Sopenharmony_ci            retStr = "TYPE_L2CAP";
78795489c19Sopenharmony_ci        } else if (type == TYPE_L2CAP_LE) {
78895489c19Sopenharmony_ci            retStr = "TYPE_L2CAP_LE";
78995489c19Sopenharmony_ci        } else {
79095489c19Sopenharmony_ci            retStr = "TYPE_UNKNOW";
79195489c19Sopenharmony_ci        }
79295489c19Sopenharmony_ci        return retStr;
79395489c19Sopenharmony_ci    }
79495489c19Sopenharmony_ci
79595489c19Sopenharmony_ci    sptr<BluetoothServerSocketObserverStub> observer_ = nullptr;
79695489c19Sopenharmony_ci
79795489c19Sopenharmony_ci    sptr<IBluetoothSocket> proxy;
79895489c19Sopenharmony_ci    UUID uuid_;
79995489c19Sopenharmony_ci    BtSocketType type_;
80095489c19Sopenharmony_ci    bool encrypt_;
80195489c19Sopenharmony_ci    int fd_;
80295489c19Sopenharmony_ci    int socketStatus_;
80395489c19Sopenharmony_ci    std::string name_ {
80495489c19Sopenharmony_ci        ""
80595489c19Sopenharmony_ci        };
80695489c19Sopenharmony_ci    int acceptFd_ = 0;
80795489c19Sopenharmony_ci    std::string acceptAddress_;
80895489c19Sopenharmony_ci    std::string socketServiceType_ {
80995489c19Sopenharmony_ci        ""
81095489c19Sopenharmony_ci    };
81195489c19Sopenharmony_ci    std::atomic<int> socketChannel_{ -1 };
81295489c19Sopenharmony_ci    std::atomic<uint32_t> maxTxPacketSize_{ 0 };
81395489c19Sopenharmony_ci    std::atomic<uint32_t> maxRxPacketSize_{ 0 };
81495489c19Sopenharmony_ci};
81595489c19Sopenharmony_ci
81695489c19Sopenharmony_ciServerSocket::impl::impl(const std::string &name, UUID uuid, BtSocketType type, bool encrypt)
81795489c19Sopenharmony_ci    : uuid_(uuid), type_(type), encrypt_(encrypt), fd_(-1), socketStatus_(SOCKET_INIT), name_(name)
81895489c19Sopenharmony_ci{
81995489c19Sopenharmony_ci    HILOGD("(4 parameters) starts");
82095489c19Sopenharmony_ci    observer_ = new BluetoothServerSocketObserverStub();
82195489c19Sopenharmony_ci}
82295489c19Sopenharmony_ci
82395489c19Sopenharmony_ciServerSocket::ServerSocket(const std::string &name, UUID uuid, BtSocketType type, bool encrypt)
82495489c19Sopenharmony_ci    : pimpl(new ServerSocket::impl(name, uuid, type, encrypt))
82595489c19Sopenharmony_ci{
82695489c19Sopenharmony_ci    HILOGD("type:%{public}d encrypt:%{public}d", type, encrypt);
82795489c19Sopenharmony_ci}
82895489c19Sopenharmony_ci
82995489c19Sopenharmony_ciServerSocket::~ServerSocket()
83095489c19Sopenharmony_ci{}
83195489c19Sopenharmony_ci
83295489c19Sopenharmony_ciint ServerSocket::Listen()
83395489c19Sopenharmony_ci{
83495489c19Sopenharmony_ci    HILOGD("enter");
83595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
83695489c19Sopenharmony_ci    return pimpl->Listen();
83795489c19Sopenharmony_ci}
83895489c19Sopenharmony_ci
83995489c19Sopenharmony_cistd::shared_ptr<ClientSocket> ServerSocket::Accept(int timeout)
84095489c19Sopenharmony_ci{
84195489c19Sopenharmony_ci    HILOGD("enter");
84295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, nullptr, "pimpl is nullptr!");
84395489c19Sopenharmony_ci    return pimpl->Accept(timeout);
84495489c19Sopenharmony_ci}
84595489c19Sopenharmony_ci
84695489c19Sopenharmony_civoid ServerSocket::Close()
84795489c19Sopenharmony_ci{
84895489c19Sopenharmony_ci    HILOGD("enter");
84995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is nullptr!");
85095489c19Sopenharmony_ci    return pimpl->Close();
85195489c19Sopenharmony_ci}
85295489c19Sopenharmony_ci
85395489c19Sopenharmony_ciconst std::string &ServerSocket::GetStringTag()
85495489c19Sopenharmony_ci{
85595489c19Sopenharmony_ci    HILOGD("enter");
85695489c19Sopenharmony_ci    return pimpl->GetStringTag();
85795489c19Sopenharmony_ci}
85895489c19Sopenharmony_ci
85995489c19Sopenharmony_ciint ServerSocket::GetL2capPsm()
86095489c19Sopenharmony_ci{
86195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
86295489c19Sopenharmony_ci    HILOGI("psm:%{public}d", pimpl->socketChannel_.load());
86395489c19Sopenharmony_ci    return pimpl->socketChannel_;
86495489c19Sopenharmony_ci}
86595489c19Sopenharmony_ci
86695489c19Sopenharmony_ciint ServerSocket::GetRfcommScn()
86795489c19Sopenharmony_ci{
86895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
86995489c19Sopenharmony_ci    HILOGI("scn:%{public}d", pimpl->socketChannel_.load());
87095489c19Sopenharmony_ci    return pimpl->socketChannel_;
87195489c19Sopenharmony_ci}
87295489c19Sopenharmony_ci
87395489c19Sopenharmony_ciuint32_t ServerSocket::GetMaxTransmitPacketSize()
87495489c19Sopenharmony_ci{
87595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
87695489c19Sopenharmony_ci    HILOGI("MaxTransmitPacketSize:%{public}d", pimpl->maxTxPacketSize_.load());
87795489c19Sopenharmony_ci    return pimpl->maxTxPacketSize_;
87895489c19Sopenharmony_ci}
87995489c19Sopenharmony_ci
88095489c19Sopenharmony_ciuint32_t ServerSocket::GetMaxReceivePacketSize()
88195489c19Sopenharmony_ci{
88295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
88395489c19Sopenharmony_ci    HILOGI("MaxReceivePacketSize:%{public}d", pimpl->maxRxPacketSize_.load());
88495489c19Sopenharmony_ci    return pimpl->maxRxPacketSize_;
88595489c19Sopenharmony_ci}
88695489c19Sopenharmony_ci
88795489c19Sopenharmony_ciint ServerSocket::GetSocketFd()
88895489c19Sopenharmony_ci{
88995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_DEVICE_DISCONNECTED, "pimpl is nullptr!");
89095489c19Sopenharmony_ci    return pimpl->fd_;
89195489c19Sopenharmony_ci}
89295489c19Sopenharmony_ci
89395489c19Sopenharmony_cistd::shared_ptr<ClientSocket> SocketFactory::BuildInsecureRfcommDataSocketByServiceRecord(
89495489c19Sopenharmony_ci    const BluetoothRemoteDevice &device, const UUID &uuid)
89595489c19Sopenharmony_ci{
89695489c19Sopenharmony_ci    HILOGD("enter");
89795489c19Sopenharmony_ci    if (device.IsValidBluetoothRemoteDevice()) {
89895489c19Sopenharmony_ci        return std::make_shared<ClientSocket>(device, uuid, TYPE_RFCOMM, false);
89995489c19Sopenharmony_ci    } else {
90095489c19Sopenharmony_ci        HILOGE("[sock] Device is not valid.");
90195489c19Sopenharmony_ci        return nullptr;
90295489c19Sopenharmony_ci    }
90395489c19Sopenharmony_ci}
90495489c19Sopenharmony_ci
90595489c19Sopenharmony_cistd::shared_ptr<ClientSocket> SocketFactory::BuildRfcommDataSocketByServiceRecord(
90695489c19Sopenharmony_ci    const BluetoothRemoteDevice &device, const UUID &uuid)
90795489c19Sopenharmony_ci{
90895489c19Sopenharmony_ci    HILOGD("enter");
90995489c19Sopenharmony_ci    if (device.IsValidBluetoothRemoteDevice()) {
91095489c19Sopenharmony_ci        return std::make_shared<ClientSocket>(device, uuid, TYPE_RFCOMM, true);
91195489c19Sopenharmony_ci    } else {
91295489c19Sopenharmony_ci        HILOGE("[sock] Device is not valid.");
91395489c19Sopenharmony_ci        return nullptr;
91495489c19Sopenharmony_ci    }
91595489c19Sopenharmony_ci}
91695489c19Sopenharmony_ci
91795489c19Sopenharmony_cistd::shared_ptr<ServerSocket> SocketFactory::DataListenInsecureRfcommByServiceRecord(
91895489c19Sopenharmony_ci    const std::string &name, const UUID &uuid)
91995489c19Sopenharmony_ci{
92095489c19Sopenharmony_ci    HILOGD("enter");
92195489c19Sopenharmony_ci    return std::make_shared<ServerSocket>(name, uuid, TYPE_RFCOMM, false);
92295489c19Sopenharmony_ci}
92395489c19Sopenharmony_ci
92495489c19Sopenharmony_cistd::shared_ptr<ServerSocket> SocketFactory::DataListenRfcommByServiceRecord(const std::string &name, const UUID &uuid)
92595489c19Sopenharmony_ci{
92695489c19Sopenharmony_ci    HILOGD("enter");
92795489c19Sopenharmony_ci    return std::make_shared<ServerSocket>(name, uuid, TYPE_RFCOMM, true);
92895489c19Sopenharmony_ci}
92995489c19Sopenharmony_ci
93095489c19Sopenharmony_ciint ClientSocket::UpdateCocConnectionParams(CocUpdateSocketParam &param)
93195489c19Sopenharmony_ci{
93295489c19Sopenharmony_ci    HILOGI("UpdateCocConnectionParams enter");
93395489c19Sopenharmony_ci    BluetoothSocketCocInfo info;
93495489c19Sopenharmony_ci
93595489c19Sopenharmony_ci    info.addr = param.addr;
93695489c19Sopenharmony_ci    info.minInterval = param.minInterval;
93795489c19Sopenharmony_ci    info.maxInterval = param.maxInterval;
93895489c19Sopenharmony_ci    info.peripheralLatency = param.peripheralLatency;
93995489c19Sopenharmony_ci    info.supervisionTimeout = param.supervisionTimeout;
94095489c19Sopenharmony_ci    info.minConnEventLen = param.minConnEventLen;
94195489c19Sopenharmony_ci    info.maxConnEventLen = param.maxConnEventLen;
94295489c19Sopenharmony_ci    sptr<IBluetoothSocket> proxy = GetRemoteProxy<IBluetoothSocket>(PROFILE_SOCKET);
94395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "proxy is null");
94495489c19Sopenharmony_ci    return proxy->UpdateCocConnectionParams(info);
94595489c19Sopenharmony_ci}
94695489c19Sopenharmony_ci}  // namespace Bluetooth
94795489c19Sopenharmony_ci}  // namespace OHOS