1 /*
2  * Copyright (c) 2023 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 
16 #include "softbus_session.h"
17 
18 #include "avsession_log.h"
19 #include "avsession_errors.h"
20 #include "softbus_session_manager.h"
21 
22 namespace OHOS::AVSession {
OnConnectSession(int32_t sessionId)23 void SoftbusSession::OnConnectSession(int32_t sessionId)
24 {
25     std::string deviceId;
26     int ret = SoftbusSessionManager::GetInstance().ObtainPeerDeviceId(sessionId, deviceId);
27     CHECK_AND_RETURN_LOG(ret == AVSESSION_SUCCESS, "obtain peer device id failed");
28     std::lock_guard lockGuard(deviceMapLock_);
29     deviceToSessionMap_.insert({ deviceId, sessionId });
30 }
31 
32 // LCOV_EXCL_START
OnDisConnectSession(int32_t sessionId)33 void SoftbusSession::OnDisConnectSession(int32_t sessionId)
34 {
35     std::string deviceId;
36     int ret = SoftbusSessionManager::GetInstance().ObtainPeerDeviceId(sessionId, deviceId);
37     CHECK_AND_RETURN_LOG(ret == AVSESSION_SUCCESS, "obtain peer device id failed");
38     std::lock_guard lockGuard(deviceMapLock_);
39     deviceToSessionMap_.erase(deviceId);
40 }
41 
SendByteToAll(const std::string & data)42 void SoftbusSession::SendByteToAll(const std::string &data)
43 {
44     SLOGI("SendByteToAll: %{public}s", data.c_str());
45     std::lock_guard lockGuard(deviceMapLock_);
46     for (auto it = deviceToSessionMap_.begin(); it != deviceToSessionMap_.end(); it++) {
47         SLOGI("SendByteToAll : %{public}s", data.c_str());
48         SoftbusSessionManager::GetInstance().SendBytes(it->second, data);
49     }
50 }
51 
SendByte(const std::string & deviceId,const std::string & data)52 void SoftbusSession::SendByte(const std::string &deviceId, const std::string &data)
53 {
54     SLOGI("SendByte: %{public}s", data.c_str());
55     std::lock_guard lockGuard(deviceMapLock_);
56     auto iter = deviceToSessionMap_.find(deviceId);
57     if (iter != deviceToSessionMap_.end()) {
58         SoftbusSessionManager::GetInstance().SendBytes(iter->second, data);
59     }
60 }
61 
SendByte(int32_t sessionId,const std::string & data)62 void SoftbusSession::SendByte(int32_t sessionId, const std::string &data)
63 {
64     SLOGI("SendByte: %{public}s", data.c_str());
65     SoftbusSessionManager::GetInstance().SendBytes(sessionId, data);
66 }
67 
SendMessage(const std::string & deviceId,const std::string & data)68 void SoftbusSession::SendMessage(const std::string &deviceId, const std::string &data)
69 {
70     SLOGI("SendMessage: %{public}s", data.c_str());
71     std::lock_guard lockGuard(deviceMapLock_);
72     auto iter = deviceToSessionMap_.find(deviceId);
73     if (iter != deviceToSessionMap_.end()) {
74         SoftbusSessionManager::GetInstance().SendMessage(iter->second, data);
75     }
76 }
77 
SendMessage(int32_t sessionId,const std::string & data)78 void SoftbusSession::SendMessage(int32_t sessionId, const std::string &data)
79 {
80     SLOGI("SendMessage: %{public}s", data.c_str());
81     SoftbusSessionManager::GetInstance().SendMessage(sessionId, data);
82 }
83 // LCOV_EXCL_STOP
84 } // namespace OHOS::AVSession