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_COMMAND_H
16 #define WIFI_DIRECT_COMMAND_H
17 
18 #include <chrono>
19 #include <memory>
20 #include <string>
21 
22 namespace OHOS::SoftBus {
23 enum class CommandType {
24     NOT_COMMAND,
25     CONNECT_COMMAND,
26     DISCONNECT_COMMAND,
27     NEGOTIATE_COMMAND,
28     BLE_TRIGGER_COMMAND,
29     FORCE_DISCONNECT_COMMAND,
30 };
31 
32 class WifiDirectProcessor;
33 class WifiDirectCommand {
34 public:
WifiDirectCommand()35     WifiDirectCommand()
36     {
37         id_ = commandId_++;
38         startTime_ = std::chrono::steady_clock::now();
39     }
40 
41     virtual std::string GetRemoteDeviceId() const = 0;
42     virtual std::shared_ptr<WifiDirectProcessor> GetProcessor() = 0;
GetType()43     virtual CommandType GetType() const { return CommandType::NOT_COMMAND; }
44     virtual ~WifiDirectCommand() = default;
45 
GetId()46     uint32_t GetId() const
47     {
48         return id_;
49     }
50 
IsValid(int validDuration)51     bool IsValid(int validDuration)
52     {
53         auto duration =
54             std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - startTime_);
55         return duration.count() < validDuration;
56     }
57 
58 private:
59     uint32_t id_ = 0;
60     static inline std::atomic_uint32_t commandId_;
61     std::chrono::time_point<std::chrono::steady_clock> startTime_;
62 };
63 }
64 #endif
65