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 #ifndef WIFI_DIRECT_CONNECT_COMMAND_H
16 #define WIFI_DIRECT_CONNECT_COMMAND_H
17 
18 #include "wifi_direct_command.h"
19 #include <functional>
20 #include "wifi_direct_types.h"
21 #include "channel/negotiate_channel.h"
22 #include "data/wifi_config_info.h"
23 #include "conn_event.h"
24 
25 namespace OHOS::SoftBus {
26 enum class ConnectCommandRetryReason {
27     RETRY_FOR_NOTHING = 0,
28     RETRY_FOR_PASSIVE_SWITCH_CHANNEL,
29 };
30 
31 struct ConnectInfo {
32     WifiDirectConnectInfo info_;
33     std::shared_ptr<NegotiateChannel> channel_;
34     std::shared_ptr<WifiConfigInfo> wifiConfigInfo_;
35 };
36 
37 class ConnectCommand : public WifiDirectCommand {
38 public:
39     using SuccessCallback = std::function<void(const WifiDirectLink &link)>;
40     using FailureCallback = std::function<void(int32_t reason)>;
41 
42     ConnectCommand(const WifiDirectConnectInfo &info, const WifiDirectConnectCallback &callback);
43 
44     std::string GetRemoteDeviceId() const override;
45     std::shared_ptr<WifiDirectProcessor> GetProcessor() override;
GetType()46     CommandType GetType() const override
47     {
48         return CommandType::CONNECT_COMMAND;
49     }
50 
51     ConnectInfo& GetConnectInfo();
52     WifiDirectConnectCallback GetConnectCallback() const;
53     void SetSuccessCallback(const SuccessCallback &callback);
54     void SetFailureCallback(const FailureCallback &callback);
55     virtual void PreferNegotiateChannel();
SetRetried(bool retried)56     void SetRetried(bool retried) { hasRetried_ = retried; }
HasRetried()57     bool HasRetried() const { return hasRetried_; }
SetRetryReason(ConnectCommandRetryReason reason)58     void SetRetryReason(ConnectCommandRetryReason reason) { retryReason_ = reason; }
GetRetryReason()59     ConnectCommandRetryReason GetRetryReason() const { return retryReason_; }
60 
61     void OnSuccess(const WifiDirectLink &link) const;
62     void OnFailure(int32_t reason) const;
63     bool IsSameCommand(const WifiDirectConnectInfo &info) const;
64 
65 protected:
66     ConnectInfo info_;
67     WifiDirectConnectCallback callback_;
68     SuccessCallback successCallback_;
69     FailureCallback failureCallback_;
70     mutable std::string remoteDeviceId_;
71     bool hasRetried_ = false;
72     ConnectCommandRetryReason retryReason_ = ConnectCommandRetryReason::RETRY_FOR_NOTHING;
73 };
74 }
75 #endif
76