• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "dm_discovery_manager.h"
17 
18 #include "dm_anonymous.h"
19 #include "dm_constants.h"
20 #include "dm_log.h"
21 
22 namespace OHOS {
23 namespace DistributedHardware {
24 const std::string DISCOVERY_TIMEOUT_TASK = TIMER_PREFIX + "discovery";
25 const int32_t DISCOVERY_TIMEOUT = 120;
26 
DmDiscoveryManager(std::shared_ptr<SoftbusConnector> softbusConnector,std::shared_ptr<DeviceManagerServiceListener> listener)27 DmDiscoveryManager::DmDiscoveryManager(std::shared_ptr<SoftbusConnector> softbusConnector,
28                                        std::shared_ptr<DeviceManagerServiceListener> listener)
29     : softbusConnector_(softbusConnector), listener_(listener)
30 {
31     LOGI("DmDiscoveryManager constructor");
32 }
33 
~DmDiscoveryManager()34 DmDiscoveryManager::~DmDiscoveryManager()
35 {
36     LOGI("DmDiscoveryManager destructor");
37 }
38 
StartDeviceDiscovery(const std::string & pkgName,const DmSubscribeInfo & subscribeInfo,const std::string & extra)39 int32_t DmDiscoveryManager::StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
40                                                  const std::string &extra)
41 {
42     if (!discoveryQueue_.empty()) {
43         if (pkgName == discoveryQueue_.front()) {
44             LOGE("DmDiscoveryManager::StartDeviceDiscovery repeated, pkgName:%s", pkgName.c_str());
45             return DM_DISCOVERY_REPEATED;
46         } else {
47             LOGI("DmDiscoveryManager::StartDeviceDiscovery stop preview discovery first, the preview pkgName is %s",
48                  discoveryQueue_.front().c_str());
49             StopDeviceDiscovery(discoveryQueue_.front(), discoveryContextMap_[discoveryQueue_.front()].subscribeId);
50         }
51     }
52     std::lock_guard<std::mutex> autoLock(locks_);
53     discoveryQueue_.push(pkgName);
54     DmDiscoveryContext context = {pkgName, extra, subscribeInfo.subscribeId};
55     discoveryContextMap_.emplace(pkgName, context);
56     softbusConnector_->RegisterSoftbusDiscoveryCallback(pkgName,
57                                                         std::shared_ptr<ISoftbusDiscoveryCallback>(shared_from_this()));
58     if (timer_ == nullptr) {
59         timer_ = std::make_shared<DmTimer>();
60     }
61     timer_->StartTimer(DISCOVERY_TIMEOUT_TASK, DISCOVERY_TIMEOUT,
62         [this] (std::string name) {
63             DmDiscoveryManager::HandleDiscoveryTimeout(name);
64         });
65     return softbusConnector_->StartDiscovery(subscribeInfo);
66 }
67 
StopDeviceDiscovery(const std::string & pkgName,uint16_t subscribeId)68 int32_t DmDiscoveryManager::StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId)
69 {
70     if (pkgName.empty()) {
71         LOGE("DmDiscoveryManager::StopDeviceDiscovery error: Invalid para, pkgName: %s", pkgName.c_str());
72         return ERR_DM_INPUT_PARA_INVALID;
73     }
74     std::lock_guard<std::mutex> autoLock(locks_);
75     if (!discoveryQueue_.empty()) {
76         discoveryQueue_.pop();
77     }
78     if (!discoveryContextMap_.empty()) {
79         discoveryContextMap_.erase(pkgName);
80         softbusConnector_->UnRegisterSoftbusDiscoveryCallback(pkgName);
81         timer_->DeleteTimer(DISCOVERY_TIMEOUT_TASK);
82     }
83     return softbusConnector_->StopDiscovery(subscribeId);
84 }
85 
OnDeviceFound(const std::string & pkgName,const DmDeviceInfo & info)86 void DmDiscoveryManager::OnDeviceFound(const std::string &pkgName, const DmDeviceInfo &info)
87 {
88     LOGI("DmDiscoveryManager::OnDeviceFound deviceId=%s", GetAnonyString(info.deviceId).c_str());
89     auto iter = discoveryContextMap_.find(pkgName);
90     if (iter == discoveryContextMap_.end()) {
91         LOGE("subscribeId not found by pkgName %s", GetAnonyString(pkgName).c_str());
92         return;
93     }
94     listener_->OnDeviceFound(pkgName, iter->second.subscribeId, info);
95 }
96 
OnDiscoveryFailed(const std::string & pkgName,int32_t subscribeId,int32_t failedReason)97 void DmDiscoveryManager::OnDiscoveryFailed(const std::string &pkgName, int32_t subscribeId, int32_t failedReason)
98 {
99     LOGI("DmDiscoveryManager::OnDiscoveryFailed subscribeId=%d reason=%d", subscribeId, failedReason);
100     StopDeviceDiscovery(pkgName, (uint32_t)subscribeId);
101     listener_->OnDiscoveryFailed(pkgName, (uint32_t)subscribeId, failedReason);
102 }
103 
OnDiscoverySuccess(const std::string & pkgName,int32_t subscribeId)104 void DmDiscoveryManager::OnDiscoverySuccess(const std::string &pkgName, int32_t subscribeId)
105 {
106     LOGI("DmDiscoveryManager::OnDiscoverySuccess subscribeId=%d", subscribeId);
107     discoveryContextMap_[pkgName].subscribeId = (uint32_t)subscribeId;
108     listener_->OnDiscoverySuccess(pkgName, subscribeId);
109 }
110 
HandleDiscoveryTimeout(std::string name)111 void DmDiscoveryManager::HandleDiscoveryTimeout(std::string name)
112 {
113     LOGI("DmDiscoveryManager::HandleDiscoveryTimeout");
114     if (discoveryQueue_.empty()) {
115         LOGE("HandleDiscoveryTimeout: discovery queue is empty.");
116         return;
117     }
118 
119     std::string pkgName = discoveryQueue_.front();
120     auto iter = discoveryContextMap_.find(pkgName);
121     if (iter == discoveryContextMap_.end()) {
122         LOGE("HandleDiscoveryTimeout: subscribeId not found by pkgName %s", GetAnonyString(pkgName).c_str());
123         return;
124     }
125     StopDeviceDiscovery(pkgName, discoveryContextMap_[pkgName].subscribeId);
126 }
127 } // namespace DistributedHardware
128 } // namespace OHOS
129