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 #include "disconnect_command.h"
16 #include "channel/dummy_negotiate_channel.h"
17 #include "channel/proxy_negotiate_channel.h"
18 #include "conn_log.h"
19 #include "data/link_manager.h"
20 #include "processor_selector_factory.h"
21 
22 namespace OHOS::SoftBus {
DisconnectCommand(const WifiDirectDisconnectInfo & info,const WifiDirectDisconnectCallback & callback)23 DisconnectCommand::DisconnectCommand(const WifiDirectDisconnectInfo &info, const WifiDirectDisconnectCallback &callback)
24     : callback_(callback)
25 {
26     info_.info_ = info;
27     auto innerLink = LinkManager::GetInstance().GetLinkById(info_.info_.linkId);
28     if (innerLink == nullptr) {
29         CONN_LOGE(CONN_WIFI_DIRECT, "not find inner link, prefer input null channel");
30         info_.channel_ = std::make_shared<DummyNegotiateChannel>();
31         return;
32     }
33 
34     if (innerLink->GetNegotiateChannel() == nullptr) {
35         if (info.negoChannel.type == NEGO_CHANNEL_AUTH) {
36             CONN_LOGI(CONN_WIFI_DIRECT, "prefer input auth channel");
37             info_.channel_ = std::make_shared<AuthNegotiateChannel>(info.negoChannel.handle.authHandle);
38         } else if (info.negoChannel.type == NEGO_CHANNEL_COC) {
39             CONN_LOGI(CONN_WIFI_DIRECT, "prefer input proxy channel");
40             info_.channel_ = std::make_shared<CoCProxyNegotiateChannel>(info.negoChannel.handle.channelId);
41         } else {
42             CONN_LOGI(CONN_WIFI_DIRECT, "prefer input null channel");
43             info_.channel_ = std::make_shared<DummyNegotiateChannel>();
44         }
45         return;
46     }
47 
48     CONN_LOGI(CONN_WIFI_DIRECT, "prefer inner channel");
49     info_.channel_ = innerLink->GetNegotiateChannel();
50 }
51 
GetRemoteDeviceId() const52 std::string DisconnectCommand::GetRemoteDeviceId() const
53 {
54     if (!remoteDeviceId_.empty()) {
55         return remoteDeviceId_;
56     }
57 
58     auto innerLink = LinkManager::GetInstance().GetLinkById(info_.info_.linkId);
59     if (innerLink == nullptr) {
60         CONN_LOGI(CONN_WIFI_DIRECT, "innerLink is nullptr");
61         return remoteDeviceId_;
62     }
63     remoteDeviceId_ = innerLink->GetRemoteDeviceId();
64     return remoteDeviceId_;
65 }
66 
GetProcessor()67 std::shared_ptr<WifiDirectProcessor> DisconnectCommand::GetProcessor()
68 {
69     auto selector = ProcessorSelectorFactory::GetInstance().NewSelector();
70     if (selector == nullptr) {
71         CONN_LOGE(CONN_WIFI_DIRECT, "selector is null");
72         return nullptr;
73     }
74     return (*selector)(info_.info_);
75 }
76 
GetDisconnectInfo() const77 DisconnectInfo DisconnectCommand::GetDisconnectInfo() const
78 {
79     return info_;
80 }
81 
GetNegotiateChannel() const82 std::shared_ptr<NegotiateChannel> DisconnectCommand::GetNegotiateChannel() const
83 {
84     return info_.channel_;
85 }
86 
OnSuccess() const87 void DisconnectCommand::OnSuccess() const
88 {
89     CONN_LOGI(CONN_WIFI_DIRECT, "requestId=%{public}u", info_.info_.requestId);
90     callback_.onDisconnectSuccess(info_.info_.requestId);
91 }
92 
OnFailure(int32_t reason) const93 void DisconnectCommand::OnFailure(int32_t reason) const
94 {
95     CONN_LOGI(CONN_WIFI_DIRECT, "requestId=%{public}u, reason=%{public}d", info_.info_.requestId, reason);
96     callback_.onDisconnectFailure(info_.info_.requestId, reason);
97 }
98 } // namespace OHOS::SoftBus
99