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