180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
480922886Sopenharmony_ci * you may not use this file except in compliance with the License.
580922886Sopenharmony_ci * You may obtain a copy of the License at
680922886Sopenharmony_ci *
780922886Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
880922886Sopenharmony_ci *
980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1280922886Sopenharmony_ci * See the License for the specific language governing permissions and
1380922886Sopenharmony_ci * limitations under the License.
1480922886Sopenharmony_ci */
1580922886Sopenharmony_ci
1680922886Sopenharmony_ci#include "softbus_session.h"
1780922886Sopenharmony_ci
1880922886Sopenharmony_ci#include "avsession_log.h"
1980922886Sopenharmony_ci#include "avsession_errors.h"
2080922886Sopenharmony_ci#include "softbus_session_manager.h"
2180922886Sopenharmony_ci
2280922886Sopenharmony_cinamespace OHOS::AVSession {
2380922886Sopenharmony_civoid SoftbusSession::OnConnectSession(int32_t sessionId)
2480922886Sopenharmony_ci{
2580922886Sopenharmony_ci    std::string deviceId;
2680922886Sopenharmony_ci    int ret = SoftbusSessionManager::GetInstance().ObtainPeerDeviceId(sessionId, deviceId);
2780922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(ret == AVSESSION_SUCCESS, "obtain peer device id failed");
2880922886Sopenharmony_ci    std::lock_guard lockGuard(deviceMapLock_);
2980922886Sopenharmony_ci    deviceToSessionMap_.insert({ deviceId, sessionId });
3080922886Sopenharmony_ci}
3180922886Sopenharmony_ci
3280922886Sopenharmony_ci// LCOV_EXCL_START
3380922886Sopenharmony_civoid SoftbusSession::OnDisConnectSession(int32_t sessionId)
3480922886Sopenharmony_ci{
3580922886Sopenharmony_ci    std::string deviceId;
3680922886Sopenharmony_ci    int ret = SoftbusSessionManager::GetInstance().ObtainPeerDeviceId(sessionId, deviceId);
3780922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(ret == AVSESSION_SUCCESS, "obtain peer device id failed");
3880922886Sopenharmony_ci    std::lock_guard lockGuard(deviceMapLock_);
3980922886Sopenharmony_ci    deviceToSessionMap_.erase(deviceId);
4080922886Sopenharmony_ci}
4180922886Sopenharmony_ci
4280922886Sopenharmony_civoid SoftbusSession::SendByteToAll(const std::string &data)
4380922886Sopenharmony_ci{
4480922886Sopenharmony_ci    SLOGI("SendByteToAll: %{public}s", data.c_str());
4580922886Sopenharmony_ci    std::lock_guard lockGuard(deviceMapLock_);
4680922886Sopenharmony_ci    for (auto it = deviceToSessionMap_.begin(); it != deviceToSessionMap_.end(); it++) {
4780922886Sopenharmony_ci        SLOGI("SendByteToAll : %{public}s", data.c_str());
4880922886Sopenharmony_ci        SoftbusSessionManager::GetInstance().SendBytes(it->second, data);
4980922886Sopenharmony_ci    }
5080922886Sopenharmony_ci}
5180922886Sopenharmony_ci
5280922886Sopenharmony_civoid SoftbusSession::SendByte(const std::string &deviceId, const std::string &data)
5380922886Sopenharmony_ci{
5480922886Sopenharmony_ci    SLOGI("SendByte: %{public}s", data.c_str());
5580922886Sopenharmony_ci    std::lock_guard lockGuard(deviceMapLock_);
5680922886Sopenharmony_ci    auto iter = deviceToSessionMap_.find(deviceId);
5780922886Sopenharmony_ci    if (iter != deviceToSessionMap_.end()) {
5880922886Sopenharmony_ci        SoftbusSessionManager::GetInstance().SendBytes(iter->second, data);
5980922886Sopenharmony_ci    }
6080922886Sopenharmony_ci}
6180922886Sopenharmony_ci
6280922886Sopenharmony_civoid SoftbusSession::SendByte(int32_t sessionId, const std::string &data)
6380922886Sopenharmony_ci{
6480922886Sopenharmony_ci    SLOGI("SendByte: %{public}s", data.c_str());
6580922886Sopenharmony_ci    SoftbusSessionManager::GetInstance().SendBytes(sessionId, data);
6680922886Sopenharmony_ci}
6780922886Sopenharmony_ci
6880922886Sopenharmony_civoid SoftbusSession::SendMessage(const std::string &deviceId, const std::string &data)
6980922886Sopenharmony_ci{
7080922886Sopenharmony_ci    SLOGI("SendMessage: %{public}s", data.c_str());
7180922886Sopenharmony_ci    std::lock_guard lockGuard(deviceMapLock_);
7280922886Sopenharmony_ci    auto iter = deviceToSessionMap_.find(deviceId);
7380922886Sopenharmony_ci    if (iter != deviceToSessionMap_.end()) {
7480922886Sopenharmony_ci        SoftbusSessionManager::GetInstance().SendMessage(iter->second, data);
7580922886Sopenharmony_ci    }
7680922886Sopenharmony_ci}
7780922886Sopenharmony_ci
7880922886Sopenharmony_civoid SoftbusSession::SendMessage(int32_t sessionId, const std::string &data)
7980922886Sopenharmony_ci{
8080922886Sopenharmony_ci    SLOGI("SendMessage: %{public}s", data.c_str());
8180922886Sopenharmony_ci    SoftbusSessionManager::GetInstance().SendMessage(sessionId, data);
8280922886Sopenharmony_ci}
8380922886Sopenharmony_ci// LCOV_EXCL_STOP
8480922886Sopenharmony_ci} // namespace OHOS::AVSession