1 /*
2  * Copyright (c) 2024 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 "force_disconnect_command.h"
17 #include "conn_log.h"
18 #include "processor_selector_factory.h"
19 #include "channel/auth_negotiate_channel.h"
20 #include "channel/proxy_negotiate_channel.h"
21 #include "channel/dummy_negotiate_channel.h"
22 #include "data/link_manager.h"
23 
24 namespace OHOS::SoftBus {
ForceDisconnectCommand(const WifiDirectForceDisconnectInfo & info,const WifiDirectDisconnectCallback & callback)25 ForceDisconnectCommand::ForceDisconnectCommand(
26     const WifiDirectForceDisconnectInfo &info, const WifiDirectDisconnectCallback &callback)
27     : callback_(callback)
28 {
29     info_.info_ = info;
30     remoteDeviceId_ = info.remoteUuid;
31     auto innerLink = LinkManager::GetInstance().GetReuseLink(info.linkType, info.remoteUuid);
32     if (innerLink == nullptr) {
33         CONN_LOGE(CONN_WIFI_DIRECT, "not find inner link, prefer input null channel");
34         info_.channel_ = std::make_shared<DummyNegotiateChannel>();
35         return;
36     }
37 
38     if (innerLink->GetNegotiateChannel() == nullptr) {
39         if (info.negoChannel.type == NEGO_CHANNEL_AUTH) {
40             CONN_LOGI(CONN_WIFI_DIRECT, "prefer input auth channel");
41             info_.channel_ = std::make_shared<AuthNegotiateChannel>(info.negoChannel.handle.authHandle);
42         } else if (info.negoChannel.type == NEGO_CHANNEL_COC) {
43             CONN_LOGI(CONN_WIFI_DIRECT, "prefer input proxy channel");
44             info_.channel_ = std::make_shared<CoCProxyNegotiateChannel>(info.negoChannel.handle.channelId);
45         } else {
46             CONN_LOGI(CONN_WIFI_DIRECT, "prefer input null channel");
47             info_.channel_ = std::make_shared<DummyNegotiateChannel>();
48         }
49         return;
50     }
51 
52     CONN_LOGI(CONN_WIFI_DIRECT, "prefer inner channel");
53     info_.channel_ = innerLink->GetNegotiateChannel();
54 }
55 
GetRemoteDeviceId() const56 std::string ForceDisconnectCommand::GetRemoteDeviceId() const
57 {
58     return remoteDeviceId_;
59 }
60 
GetProcessor()61 std::shared_ptr<WifiDirectProcessor> ForceDisconnectCommand::GetProcessor()
62 {
63     auto selector = ProcessorSelectorFactory::GetInstance().NewSelector();
64     return (*selector)(info_.info_);
65 }
66 
GetDisconnectInfo() const67 ForceDisconnectInfo ForceDisconnectCommand::GetDisconnectInfo() const
68 {
69     return info_;
70 }
71 
GetNegotiateChannel() const72 std::shared_ptr<NegotiateChannel> ForceDisconnectCommand::GetNegotiateChannel() const
73 {
74     return info_.channel_;
75 }
76 
OnSuccess() const77 void ForceDisconnectCommand::OnSuccess() const
78 {
79     CONN_LOGI(CONN_WIFI_DIRECT, "requestId=%{public}u", info_.info_.requestId);
80     callback_.onDisconnectSuccess(info_.info_.requestId);
81 }
82 
OnFailure(int32_t reason) const83 void ForceDisconnectCommand::OnFailure(int32_t reason) const
84 {
85     CONN_LOGI(CONN_WIFI_DIRECT, "requestId=%{public}u, reason=%{public}d", info_.info_.requestId, reason);
86     callback_.onDisconnectFailure(info_.info_.requestId, reason);
87 }
88 }  // namespace OHOS::SoftBus