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