• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "device_cooperate_softbus_adapter.h"
17 
18 #include <chrono>
19 #include <thread>
20 
21 #include "softbus_bus_center.h"
22 #include "softbus_common.h"
23 
24 #include "config_multimodal.h"
25 #include "device_cooperate_softbus_define.h"
26 #include "input_device_cooperate_sm.h"
27 #include "input_device_cooperate_util.h"
28 #include "mmi_log.h"
29 #include "util.h"
30 
31 namespace OHOS {
32 namespace MMI {
33 namespace {
34 std::shared_ptr<DeviceCooperateSoftbusAdapter> g_instance = nullptr;
35 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "DeviceCooperateSoftbusAdapter" };
36 constexpr int32_t DINPUT_LINK_TYPE_MAX = 4;
37 const SessionAttribute g_sessionAttr = {
38     .dataType = SessionType::TYPE_BYTES,
39     .linkTypeNum = DINPUT_LINK_TYPE_MAX,
40     .linkType = {
41         LINK_TYPE_WIFI_WLAN_2G,
42         LINK_TYPE_WIFI_WLAN_5G,
43         LINK_TYPE_WIFI_P2P,
44         LINK_TYPE_BR
45     }
46 };
47 
ResponseStartRemoteCooperate(int32_t sessionId,const JsonParser & parser)48 void ResponseStartRemoteCooperate(int32_t sessionId, const JsonParser& parser)
49 {
50     CALL_DEBUG_ENTER;
51     cJSON* deviceId = cJSON_GetObjectItemCaseSensitive(parser.json_, MMI_SOFTBUS_KEY_LOCAL_DEVICE_ID);
52     if (!cJSON_IsString(deviceId)) {
53         MMI_HILOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONPREPARE, data type is error");
54         return;
55     }
56     InputDevCooSM->StartRemoteCooperate(deviceId->valuestring);
57 }
58 
ResponseStartRemoteCooperateResult(int32_t sessionId,const JsonParser & parser)59 void ResponseStartRemoteCooperateResult(int32_t sessionId, const JsonParser& parser)
60 {
61     CALL_DEBUG_ENTER;
62     cJSON* result = cJSON_GetObjectItemCaseSensitive(parser.json_, MMI_SOFTBUS_KEY_RESULT);
63     cJSON* dhid = cJSON_GetObjectItemCaseSensitive(parser.json_, MMI_SOFTBUS_KEY_START_DHID);
64     cJSON* x = cJSON_GetObjectItemCaseSensitive(parser.json_, MMI_SOFTBUS_KEY_POINTER_X);
65     cJSON* y = cJSON_GetObjectItemCaseSensitive(parser.json_, MMI_SOFTBUS_KEY_POINTER_Y);
66     if (!cJSON_IsBool(result) || !cJSON_IsString(dhid) || !cJSON_IsNumber(x) || !cJSON_IsNumber(y)) {
67         MMI_HILOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONPREPARE, data type is error");
68         return;
69     }
70     InputDevCooSM->StartRemoteCooperateResult(cJSON_IsTrue(result), dhid->valuestring, x->valueint, y->valueint);
71 }
72 
ResponseStopRemoteCooperate(int32_t sessionId,const JsonParser & parser)73 void ResponseStopRemoteCooperate(int32_t sessionId, const JsonParser& parser)
74 {
75     InputDevCooSM->StopRemoteCooperate();
76 }
77 
ResponseStopRemoteCooperateResult(int32_t sessionId,const JsonParser & parser)78 void ResponseStopRemoteCooperateResult(int32_t sessionId, const JsonParser& parser)
79 {
80     CALL_DEBUG_ENTER;
81     cJSON* result = cJSON_GetObjectItemCaseSensitive(parser.json_, MMI_SOFTBUS_KEY_RESULT);
82 
83     if (!cJSON_IsBool(result)) {
84         MMI_HILOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONPREPARE, data type is error");
85         return;
86     }
87     InputDevCooSM->StopRemoteCooperateResult(cJSON_IsTrue(result));
88 }
89 
ResponseStartCooperateOtherResult(int32_t sessionId,const JsonParser & parser)90 void ResponseStartCooperateOtherResult(int32_t sessionId, const JsonParser& parser)
91 {
92     CALL_DEBUG_ENTER;
93     cJSON* deviceId = cJSON_GetObjectItemCaseSensitive(parser.json_, MMI_SOFTBUS_KEY_OTHER_DEVICE_ID);
94 
95     if (!cJSON_IsString(deviceId)) {
96         MMI_HILOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONPREPARE, data type is error");
97         return;
98     }
99     InputDevCooSM->StartCooperateOtherResult(deviceId->valuestring);
100 }
101 } // namespace
102 
SessionOpened(int32_t sessionId,int32_t result)103 static int32_t SessionOpened(int32_t sessionId, int32_t result)
104 {
105     return DevCooperateSoftbusAdapter->OnSessionOpened(sessionId, result);
106 }
107 
SessionClosed(int32_t sessionId)108 static void SessionClosed(int32_t sessionId)
109 {
110     DevCooperateSoftbusAdapter->OnSessionClosed(sessionId);
111 }
112 
BytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)113 static void BytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
114 {
115     DevCooperateSoftbusAdapter->OnBytesReceived(sessionId, data, dataLen);
116 }
117 
MessageReceived(int32_t sessionId,const void * data,uint32_t dataLen)118 static void MessageReceived(int32_t sessionId, const void *data, uint32_t dataLen)
119 {
120     (void)sessionId;
121     (void)data;
122     (void)dataLen;
123 }
124 
StreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)125 static void StreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
126     const StreamFrameInfo *param)
127 {
128     (void)sessionId;
129     (void)data;
130     (void)ext;
131     (void)param;
132 }
133 
Init()134 int32_t DeviceCooperateSoftbusAdapter::Init()
135 {
136     CALL_INFO_TRACE;
137     sessListener_ = {
138         .OnSessionOpened = SessionOpened,
139         .OnSessionClosed = SessionClosed,
140         .OnBytesReceived = BytesReceived,
141         .OnMessageReceived = MessageReceived,
142         .OnStreamReceived = StreamReceived
143     };
144     std::string networkId = GetLocalDeviceId();
145     if (networkId.empty()) {
146         MMI_HILOGE("Local networkid is empty");
147         return RET_ERR;
148     }
149     localSessionName_ = SESSION_NAME + networkId.substr(0, INTERCEPT_STRING_LENGTH);
150     int32_t ret = CreateSessionServer(MMI_DINPUT_PKG_NAME, localSessionName_.c_str(), &sessListener_);
151     if (ret != RET_OK) {
152         MMI_HILOGE("Create session server failed, error code:%{public}d", ret);
153         return RET_ERR;
154     }
155     return RET_OK;
156 }
157 
~DeviceCooperateSoftbusAdapter()158 DeviceCooperateSoftbusAdapter::~DeviceCooperateSoftbusAdapter()
159 {
160     Release();
161 }
162 
Release()163 void DeviceCooperateSoftbusAdapter::Release()
164 {
165     CALL_INFO_TRACE;
166     std::unique_lock<std::mutex> sessionLock(operationMutex_);
167     std::for_each(sessionDevMap_.begin(), sessionDevMap_.end(), [](auto item) {
168         CloseSession(item.second);
169         MMI_HILOGD("Close session success");
170     });
171     int32_t ret = RemoveSessionServer(MMI_DINPUT_PKG_NAME, localSessionName_.c_str());
172     MMI_HILOGD("RemoveSessionServer ret:%{public}d", ret);
173     sessionDevMap_.clear();
174     channelStatusMap_.clear();
175 }
176 
CheckDeviceSessionState(const std::string & devId)177 bool DeviceCooperateSoftbusAdapter::CheckDeviceSessionState(const std::string &devId)
178 {
179     std::unique_lock<std::mutex> sessionLock(operationMutex_);
180     if (sessionDevMap_.find(devId) == sessionDevMap_.end()) {
181         MMI_HILOGE("Check session state error");
182         return false;
183     }
184     return true;
185 }
186 
OpenInputSoftbus(const std::string & remoteDevId)187 int32_t DeviceCooperateSoftbusAdapter::OpenInputSoftbus(const std::string &remoteDevId)
188 {
189     CALL_INFO_TRACE;
190     if (CheckDeviceSessionState(remoteDevId)) {
191         MMI_HILOGD("Softbus session has already  opened");
192         return RET_OK;
193     }
194 
195     int32_t ret = Init();
196     if (ret != RET_OK) {
197         MMI_HILOGE("Init failed");
198         return RET_ERR;
199     }
200 
201     std::string peerSessionName = SESSION_NAME + remoteDevId.substr(0, INTERCEPT_STRING_LENGTH);
202     int32_t sessionId = OpenSession(localSessionName_.c_str(), peerSessionName.c_str(), remoteDevId.c_str(),
203         GROUP_ID.c_str(), &g_sessionAttr);
204     if (sessionId < 0) {
205         MMI_HILOGE("OpenSession failed");
206         return RET_ERR;
207     }
208     return WaitSessionOpend(remoteDevId, sessionId);
209 }
210 
WaitSessionOpend(const std::string & remoteDevId,int32_t sessionId)211 int32_t DeviceCooperateSoftbusAdapter::WaitSessionOpend(const std::string &remoteDevId, int32_t sessionId)
212 {
213     CALL_INFO_TRACE;
214     std::unique_lock<std::mutex> waitLock(operationMutex_);
215     sessionDevMap_[remoteDevId] = sessionId;
216     auto status = openSessionWaitCond_.wait_for(waitLock, std::chrono::seconds(SESSION_WAIT_TIMEOUT_SECOND),
217         [this, remoteDevId] () { return channelStatusMap_[remoteDevId]; });
218     if (!status) {
219         MMI_HILOGE("OpenSession timeout");
220         return RET_ERR;
221     }
222     channelStatusMap_[remoteDevId] = false;
223     return RET_OK;
224 }
225 
CloseInputSoftbus(const std::string & remoteDevId)226 void DeviceCooperateSoftbusAdapter::CloseInputSoftbus(const std::string &remoteDevId)
227 {
228     CALL_INFO_TRACE;
229     std::unique_lock<std::mutex> sessionLock(operationMutex_);
230     if (sessionDevMap_.find(remoteDevId) == sessionDevMap_.end()) {
231         MMI_HILOGI("SessionDevIdMap not find");
232         return;
233     }
234     int32_t sessionId = sessionDevMap_[remoteDevId];
235 
236     CloseSession(sessionId);
237     sessionDevMap_.erase(remoteDevId);
238     channelStatusMap_.erase(remoteDevId);
239 }
240 
GetInstance()241 std::shared_ptr<DeviceCooperateSoftbusAdapter> DeviceCooperateSoftbusAdapter::GetInstance()
242 {
243     static std::once_flag flag;
244     std::call_once(flag, [&]() {
245         g_instance.reset(new (std::nothrow) DeviceCooperateSoftbusAdapter());
246     });
247     return g_instance;
248 }
249 
250 
StartRemoteCooperate(const std::string & localDeviceId,const std::string & remoteDeviceId)251 int32_t DeviceCooperateSoftbusAdapter::StartRemoteCooperate(const std::string &localDeviceId,
252     const std::string &remoteDeviceId)
253 {
254     CALL_DEBUG_ENTER;
255     std::unique_lock<std::mutex> sessionLock(operationMutex_);
256     if (sessionDevMap_.find(remoteDeviceId) == sessionDevMap_.end()) {
257         MMI_HILOGE("Start remote cooperate error, not find this device");
258         return RET_ERR;
259     }
260     int32_t sessionId = sessionDevMap_[remoteDeviceId];
261     cJSON *jsonStr = cJSON_CreateObject();
262     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_CMD_TYPE, cJSON_CreateNumber(REMOTE_COOPERATE_START));
263     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_LOCAL_DEVICE_ID, cJSON_CreateString(localDeviceId.c_str()));
264     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_SESSION_ID, cJSON_CreateNumber(sessionId));
265     char *smsg = cJSON_Print(jsonStr);
266     cJSON_Delete(jsonStr);
267     int32_t ret = SendMsg(sessionId, smsg);
268     cJSON_free(smsg);
269     if (ret != RET_OK) {
270         MMI_HILOGE("Start remote cooperate send session msg failed, ret:%{public}d", ret);
271         return RET_ERR;
272     }
273     return RET_OK;
274 }
275 
StartRemoteCooperateResult(const std::string & remoteDeviceId,bool isSuccess,const std::string & startDhid,int32_t xPercent,int32_t yPercent)276 int32_t DeviceCooperateSoftbusAdapter::StartRemoteCooperateResult(const std::string &remoteDeviceId, bool isSuccess,
277     const std::string &startDhid, int32_t xPercent, int32_t yPercent)
278 {
279     CALL_DEBUG_ENTER;
280     std::unique_lock<std::mutex> sessionLock(operationMutex_);
281     if (sessionDevMap_.find(remoteDeviceId) == sessionDevMap_.end()) {
282         MMI_HILOGE("Stop remote cooperate error, not find this device");
283         return RET_ERR;
284     }
285     int32_t sessionId = sessionDevMap_[remoteDeviceId];
286     cJSON *jsonStr = cJSON_CreateObject();
287     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_CMD_TYPE, cJSON_CreateNumber(REMOTE_COOPERATE_START_RES));
288     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_RESULT, cJSON_CreateBool(isSuccess));
289     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_START_DHID, cJSON_CreateString(startDhid.c_str()));
290     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_POINTER_X, cJSON_CreateNumber(xPercent));
291     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_POINTER_Y, cJSON_CreateNumber(yPercent));
292     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_SESSION_ID, cJSON_CreateNumber(sessionId));
293     char *smsg = cJSON_Print(jsonStr);
294     cJSON_Delete(jsonStr);
295     int32_t ret = SendMsg(sessionId, smsg);
296     cJSON_free(smsg);
297     if (ret != RET_OK) {
298         MMI_HILOGE("Start remote cooperate result send session msg failed");
299         return RET_ERR;
300     }
301     return RET_OK;
302 }
303 
StopRemoteCooperate(const std::string & remoteDeviceId)304 int32_t DeviceCooperateSoftbusAdapter::StopRemoteCooperate(const std::string &remoteDeviceId)
305 {
306     CALL_DEBUG_ENTER;
307     std::unique_lock<std::mutex> sessionLock(operationMutex_);
308     if (sessionDevMap_.find(remoteDeviceId) == sessionDevMap_.end()) {
309         MMI_HILOGE("Stop remote cooperate error, not find this device");
310         return RET_ERR;
311     }
312     int32_t sessionId = sessionDevMap_[remoteDeviceId];
313     cJSON *jsonStr = cJSON_CreateObject();
314     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_CMD_TYPE, cJSON_CreateNumber(REMOTE_COOPERATE_STOP));
315     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_SESSION_ID, cJSON_CreateNumber(sessionId));
316     char *smsg = cJSON_Print(jsonStr);
317     cJSON_Delete(jsonStr);
318     int32_t ret = SendMsg(sessionId, smsg);
319     cJSON_free(smsg);
320     if (ret != RET_OK) {
321         MMI_HILOGE("Stop remote cooperate send session msg failed");
322         return RET_ERR;
323     }
324     return RET_OK;
325 }
326 
StopRemoteCooperateResult(const std::string & remoteDeviceId,bool isSuccess)327 int32_t DeviceCooperateSoftbusAdapter::StopRemoteCooperateResult(const std::string &remoteDeviceId, bool isSuccess)
328 {
329     CALL_DEBUG_ENTER;
330     std::unique_lock<std::mutex> sessionLock(operationMutex_);
331     if (sessionDevMap_.find(remoteDeviceId) == sessionDevMap_.end()) {
332         MMI_HILOGE("Stop remote cooperate result error, not find this device");
333         return RET_ERR;
334     }
335     int32_t sessionId = sessionDevMap_[remoteDeviceId];
336     cJSON *jsonStr = cJSON_CreateObject();
337     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_CMD_TYPE, cJSON_CreateNumber(REMOTE_COOPERATE_STOP_RES));
338     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_RESULT, cJSON_CreateBool(isSuccess));
339     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_SESSION_ID, cJSON_CreateNumber(sessionId));
340     char *smsg = cJSON_Print(jsonStr);
341     cJSON_Delete(jsonStr);
342     int32_t ret = SendMsg(sessionId, smsg);
343     cJSON_free(smsg);
344     if (ret != RET_OK) {
345         MMI_HILOGE("Stop remote cooperate result send session msg failed");
346         return RET_ERR;
347     }
348     return RET_OK;
349 }
350 
StartCooperateOtherResult(const std::string & remoteDeviceId,const std::string & srcNetworkId)351 int32_t DeviceCooperateSoftbusAdapter::StartCooperateOtherResult(const std::string &remoteDeviceId,
352     const std::string &srcNetworkId)
353 {
354     CALL_DEBUG_ENTER;
355     std::unique_lock<std::mutex> sessionLock(operationMutex_);
356     if (sessionDevMap_.find(remoteDeviceId) == sessionDevMap_.end()) {
357         MMI_HILOGE("Start cooperate other result error, not find this device");
358         return RET_ERR;
359     }
360     int32_t sessionId = sessionDevMap_[remoteDeviceId];
361     cJSON *jsonStr = cJSON_CreateObject();
362     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_CMD_TYPE, cJSON_CreateNumber(REMOTE_COOPERATE_STOP_OTHER_RES));
363     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_OTHER_DEVICE_ID, cJSON_CreateString(srcNetworkId.c_str()));
364     cJSON_AddItemToObject(jsonStr, MMI_SOFTBUS_KEY_SESSION_ID, cJSON_CreateNumber(sessionId));
365     char *smsg = cJSON_Print(jsonStr);
366     cJSON_Delete(jsonStr);
367     int32_t ret = SendMsg(sessionId, smsg);
368     cJSON_free(smsg);
369     if (ret != RET_OK) {
370         MMI_HILOGE("Start cooperate other result send session msg failed");
371         return RET_ERR;
372     }
373     return RET_OK;
374 }
375 
HandleSessionData(int32_t sessionId,const std::string & message)376 void DeviceCooperateSoftbusAdapter::HandleSessionData(int32_t sessionId, const std::string& message)
377 {
378     JsonParser parser;
379     parser.json_ = cJSON_Parse(message.c_str());
380     if (!cJSON_IsObject(parser.json_)) {
381         MMI_HILOGE("Parser.json_ is not object");
382         return;
383     }
384     cJSON* comType = cJSON_GetObjectItemCaseSensitive(parser.json_, MMI_SOFTBUS_KEY_CMD_TYPE);
385     if (!cJSON_IsNumber(comType)) {
386         MMI_HILOGE("OnBytesReceived cmdType is not number type");
387         return;
388     }
389     MMI_HILOGD("valueint: %{public}d", comType->valueint);
390     switch (comType->valueint) {
391         case REMOTE_COOPERATE_START: {
392             ResponseStartRemoteCooperate(sessionId, parser);
393             break;
394         }
395         case REMOTE_COOPERATE_START_RES: {
396             ResponseStartRemoteCooperateResult(sessionId, parser);
397             break;
398         }
399         case REMOTE_COOPERATE_STOP: {
400             ResponseStopRemoteCooperate(sessionId, parser);
401             break;
402         }
403         case REMOTE_COOPERATE_STOP_RES: {
404             ResponseStopRemoteCooperateResult(sessionId, parser);
405             break;
406         }
407         case REMOTE_COOPERATE_STOP_OTHER_RES: {
408             ResponseStartCooperateOtherResult(sessionId, parser);
409             break;
410         }
411         default: {
412             MMI_HILOGE("OnBytesReceived cmdType is undefined");
413             break;
414         }
415     }
416 }
417 
OnBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)418 void DeviceCooperateSoftbusAdapter::OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
419 {
420     MMI_HILOGD("dataLen:%{public}d", dataLen);
421     if (sessionId < 0 || data == nullptr || dataLen <= 0) {
422         MMI_HILOGE("Param check failed");
423         return;
424     }
425     std::string message = std::string(static_cast<const char *>(data), dataLen);
426     HandleSessionData(sessionId, message);
427 }
428 
SendMsg(int32_t sessionId,const std::string & message)429 int32_t DeviceCooperateSoftbusAdapter::SendMsg(int32_t sessionId, const std::string &message)
430 {
431     CALL_DEBUG_ENTER;
432     if (message.size() > MSG_MAX_SIZE) {
433         MMI_HILOGW("error:message.size() > MSG_MAX_SIZE msessage size:%{public}zu", message.size());
434         return RET_ERR;
435     }
436     return SendBytes(sessionId, message.c_str(), strlen(message.c_str()));
437 }
438 
FindDevice(int32_t sessionId)439 std::string DeviceCooperateSoftbusAdapter::FindDevice(int32_t sessionId)
440 {
441     std::unique_lock<std::mutex> sessionLock(operationMutex_);
442     auto find_item = std::find_if(sessionDevMap_.begin(), sessionDevMap_.end(),
443         [sessionId](const std::map<std::string, int>::value_type item) {
444         return item.second == sessionId;
445     });
446     if (find_item == sessionDevMap_.end()) {
447         MMI_HILOGE("FindDevice error");
448         return {};
449     }
450     return find_item->first;
451 }
452 
OnSessionOpened(int32_t sessionId,int32_t result)453 int32_t DeviceCooperateSoftbusAdapter::OnSessionOpened(int32_t sessionId, int32_t result)
454 {
455     CALL_INFO_TRACE;
456     char peerDevId[DEVICE_ID_SIZE_MAX] = {};
457     int32_t getPeerDeviceIdResult = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId));
458     MMI_HILOGD("Get peer device id ret:%{public}d", getPeerDeviceIdResult);
459     if (result != RET_OK) {
460         std::string deviceId = FindDevice(sessionId);
461         MMI_HILOGE("Session open failed result:%{public}d", result);
462         std::unique_lock<std::mutex> sessionLock(operationMutex_);
463         if (sessionDevMap_.find(deviceId) != sessionDevMap_.end()) {
464             sessionDevMap_.erase(deviceId);
465         }
466         if (getPeerDeviceIdResult == RET_OK) {
467             channelStatusMap_[peerDevId] = true;
468         }
469         openSessionWaitCond_.notify_all();
470         return RET_OK;
471     }
472 
473     int32_t sessionSide = GetSessionSide(sessionId);
474     MMI_HILOGI("session open succeed, sessionId:%{public}d, sessionSide:%{public}d(1 is client side)",
475         sessionId, sessionSide);
476     std::lock_guard<std::mutex> notifyLock(operationMutex_);
477     if (sessionSide == SESSION_SIDE_SERVER) {
478         if (getPeerDeviceIdResult == RET_OK) {
479             sessionDevMap_[peerDevId] = sessionId;
480         }
481     } else {
482         if (getPeerDeviceIdResult == RET_OK) {
483             channelStatusMap_[peerDevId] = true;
484         }
485         openSessionWaitCond_.notify_all();
486     }
487     return RET_OK;
488 }
489 
OnSessionClosed(int32_t sessionId)490 void DeviceCooperateSoftbusAdapter::OnSessionClosed(int32_t sessionId)
491 {
492     CALL_DEBUG_ENTER;
493     std::string deviceId = FindDevice(sessionId);
494     std::unique_lock<std::mutex> sessionLock(operationMutex_);
495     if (sessionDevMap_.find(deviceId) != sessionDevMap_.end()) {
496         sessionDevMap_.erase(deviceId);
497     }
498     if (GetSessionSide(sessionId) != 0) {
499         channelStatusMap_.erase(deviceId);
500     }
501     InputDevCooSM->Reset(deviceId);
502 }
503 } // namespace MMI
504 } // namespace OHOS
505