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_manager.h"
17
18 #include "avsession_log.h"
19 #include "avsession_errors.h"
20 #include "migrate_avsession_constant.h"
21
22 namespace OHOS::AVSession {
GetInstance()23 SoftbusSessionManager& SoftbusSessionManager::GetInstance()
24 {
25 static SoftbusSessionManager softbusSessionListener;
26 return softbusSessionListener;
27 }
28
OnBind(int32_t socket,PeerSocketInfo info)29 static void OnBind(int32_t socket, PeerSocketInfo info)
30 {
31 SLOGI("OnBind sessionId[%{public}d] result[%{public}s]", socket, info.networkId);
32 SoftbusSessionManager::GetInstance().OnBind(socket, info);
33 }
34
OnShutdown(int32_t socket,ShutdownReason reason)35 static void OnShutdown(int32_t socket, ShutdownReason reason)
36 {
37 SLOGI("OnSessionClosed sessionId[%{public}d], reason[%{public}d]", socket, reason);
38 SoftbusSessionManager::GetInstance().OnShutdown(socket, reason);
39 }
40
OnBytes(int socket,const void * data,unsigned int dataLen)41 static void OnBytes(int socket, const void *data, unsigned int dataLen)
42 {
43 SLOGI("OnBytesReceived sessionId[%{public}d], datalen[%{public}d]", socket, dataLen);
44 std::string msg = std::string(static_cast<const char*>(data), dataLen);
45 SoftbusSessionManager::GetInstance().OnBytes(socket, msg.c_str(), dataLen);
46 }
47
OnMessage(int socket,const void * data,unsigned int dataLen)48 static void OnMessage(int socket, const void *data, unsigned int dataLen)
49 {
50 SLOGI("OnMessageReceived sessionId[%{public}d], datalen[%{public}d]", socket, dataLen);
51 std::string msg = std::string(static_cast<const char*>(data), dataLen);
52 SoftbusSessionManager::GetInstance().OnMessage(socket, msg.c_str(), dataLen);
53 }
54
55 static ISocketListener iSessionListener = {
56 .OnBind = OnBind,
57 .OnShutdown = OnShutdown,
58 .OnBytes = OnBytes,
59 .OnMessage = OnMessage
60 };
61
Socket(const std::string & pkgName)62 int32_t SoftbusSessionManager::Socket(const std::string &pkgName)
63 {
64 if (pkgName.c_str() == nullptr) {
65 SLOGE("pkg name is null");
66 return AVSESSION_ERROR;
67 }
68 SocketInfo info = {
69 .name = const_cast<char *>(CONFIG_SOFTBUS_SESSION_TAG.c_str()),
70 .pkgName = const_cast<char *>(pkgName.c_str()),
71 .dataType = DATA_TYPE_BYTES
72 };
73 int32_t socket = ::Socket(info);
74 QosTV serverQos[] = {
75 {.qos = QOS_TYPE_MIN_BW, .value = 64 * 1024 }, //最小带宽64k
76 {.qos = QOS_TYPE_MAX_LATENCY, .value = 19000 }, //最大建链时延19s
77 {.qos = QOS_TYPE_MIN_LATENCY, .value = 500 }, //最小建链时延0.5s
78 };
79 int32_t ret = ::Listen(socket, serverQos, QOS_COUNT, &iSessionListener);
80 if (ret == 0) {
81 SLOGI("service success ,socket[%{public}d]", socket);
82 //建立服务成功
83 } else {
84 SLOGI("service failed ,ret[%{public}d]", ret);
85 //建立服务失败,错误码
86 }
87 return socket;
88 }
89
Shutdown(int32_t socket)90 void SoftbusSessionManager::Shutdown(int32_t socket)
91 {
92 SLOGI("socket Shutdown");
93 ::Shutdown(socket);
94 }
95
SendMessage(int32_t socket,const std::string & data)96 int32_t SoftbusSessionManager::SendMessage(int32_t socket, const std::string &data)
97 {
98 if (socket <= 0 || data == "") {
99 SLOGE("the params invalid, unable to send message by session.");
100 return AVSESSION_ERROR;
101 }
102 int ret = ::SendMessage(socket, data.c_str(), data.length());
103 if (ret != 0) {
104 SLOGE("SendMessage error, ret = %{public}d", ret);
105 return AVSESSION_ERROR;
106 }
107 return ret;
108 }
109
SendBytes(int32_t socket,const std::string & data)110 int32_t SoftbusSessionManager::SendBytes(int32_t socket, const std::string &data)
111 {
112 if (socket <= 0 || data == "") {
113 SLOGE("the params invalid, unable to send sendBytes by session.");
114 return AVSESSION_ERROR;
115 }
116 int ret = ::SendBytes(socket, data.c_str(), data.length());
117 if (ret != 0) {
118 SLOGE("SendBytes error, ret = %{public}d", ret);
119 return AVSESSION_ERROR;
120 }
121 return ret;
122 }
123
ObtainPeerDeviceId(int32_t socket,std::string & deviceId)124 int32_t SoftbusSessionManager::ObtainPeerDeviceId(int32_t socket, std::string &deviceId)
125 {
126 CHECK_AND_RETURN_RET_LOG(
127 socket > 0, AVSESSION_ERROR, "the session is null, unable to obtain the peer device id.");
128 if (mMap_.find(socket) == mMap_.end()) {
129 SLOGE("no find deviceid.");
130 return AVSESSION_ERROR;
131 } else {
132 deviceId = mMap_[socket];
133 return AVSESSION_SUCCESS;
134 }
135 }
136
AddSessionListener(std::shared_ptr<SoftbusSessionListener> softbusSessionListener)137 void SoftbusSessionManager::AddSessionListener(std::shared_ptr<SoftbusSessionListener> softbusSessionListener)
138 {
139 if (softbusSessionListener == nullptr) {
140 SLOGE("the session listener is null, unable to add to session listener list.");
141 return;
142 }
143 std::lock_guard lockGuard(socketLock_);
144 sessionListeners_.clear();
145 sessionListeners_.emplace_back(softbusSessionListener);
146 }
147
OnBind(int32_t socket,PeerSocketInfo info)148 void SoftbusSessionManager::OnBind(int32_t socket, PeerSocketInfo info)
149 {
150 if (info.networkId == nullptr) {
151 SLOGE("PeerSocketInfo is nullptr");
152 return;
153 }
154 std::lock_guard lockGuard(socketLock_);
155 for (auto listener : sessionListeners_) {
156 listener->OnBind(socket, info);
157 mMap_.insert({socket, info.networkId});
158 }
159 }
160
OnShutdown(int32_t socket,ShutdownReason reason)161 void SoftbusSessionManager::OnShutdown(int32_t socket, ShutdownReason reason)
162 {
163 SLOGI("ShutdownReason = %{public}d", reason);
164 std::lock_guard lockGuard(socketLock_);
165 for (auto listener : sessionListeners_) {
166 listener->OnShutdown(socket, reason);
167 mMap_.erase(socket);
168 }
169 }
170
OnMessage(int32_t socket,const void * data,int32_t dataLen)171 void SoftbusSessionManager::OnMessage(int32_t socket, const void *data, int32_t dataLen)
172 {
173 if (data == nullptr) {
174 SLOGE("message data is nullptr");
175 return;
176 }
177 std::lock_guard lockGuard(socketLock_);
178 for (auto listener : sessionListeners_) {
179 listener->OnMessage(socket, data, dataLen);
180 }
181 }
182
OnBytes(int32_t socket,const void * data,int32_t dataLen)183 void SoftbusSessionManager::OnBytes(int32_t socket, const void *data, int32_t dataLen)
184 {
185 if (data == nullptr) {
186 SLOGE("bytes data is nullptr");
187 return;
188 }
189 std::lock_guard lockGuard(socketLock_);
190 for (auto listener : sessionListeners_) {
191 listener->OnBytes(socket, data, dataLen);
192 }
193 }
194 } // namespace OHOS::AVSession
195