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_EXECUTOR_H
16 #define WIFI_DIRECT_EXECUTOR_H
17 
18 #include <condition_variable>
19 #include <memory>
20 #include <queue>
21 #include <thread>
22 
23 #include "processor/wifi_direct_processor.h"
24 #include "event/wifi_direct_event_receiver.h"
25 #include "event/wifi_direct_event_sender.h"
26 #include "utils/wifi_direct_trace.h"
27 
28 namespace OHOS::SoftBus {
29 class WifiDirectScheduler;
30 class WifiDirectExecutor {
31 public:
32     explicit WifiDirectExecutor(const std::string &remoteDeviceId, WifiDirectScheduler &scheduler,
33                                 std::shared_ptr<WifiDirectProcessor> &processor, bool active);
34     virtual ~WifiDirectExecutor();
35 
36     void Start();
37     void Run(std::shared_ptr<WifiDirectProcessor> processor);
38     std::string GetRemoteDeviceId();
39     void SetRemoteDeviceId(const std::string &remoteDeviceId);
40     bool IsActive() const;
41     void SetActive(bool active);
42     bool CanAcceptNegotiateData(WifiDirectCommand &command);
43 
44     template<typename Content>
SendEvent(const Content & content)45     void SendEvent(const Content &content)
46     {
47         GetSender().Send(content);
48     }
49 
SetProcessor(std::shared_ptr<WifiDirectProcessor> processor)50     void SetProcessor(std::shared_ptr<WifiDirectProcessor> processor)
51     {
52         std::lock_guard lock(processorLock_);
53         processor_ = processor;
54     }
55 
56     WifiDirectEventDispatcher WaitEvent();
57 
58 protected:
GetSender()59     WifiDirectEventSender GetSender() { return receiver_; }
60 
61     virtual void ProcessUnHandleCommand();
62 
63     std::string remoteDeviceId_;
64     WifiDirectEventReceiver receiver_;
65     WifiDirectScheduler &scheduler_;
66     std::recursive_mutex processorLock_;
67     std::shared_ptr<WifiDirectProcessor> processor_;
68     std::thread thread_;
69 
70     bool active_;
71 
72     bool started_;
73 };
74 }
75 #endif
76