1 /*
2  * Copyright (c) 2022-2023 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 #ifndef COORDINATION_MANAGER_IMPL_H
17 #define COORDINATION_MANAGER_IMPL_H
18 
19 #include <functional>
20 #include <list>
21 #include <map>
22 #include <mutex>
23 #include <optional>
24 
25 #include "client.h"
26 #include "i_coordination_listener.h"
27 #include "i_hotarea_listener.h"
28 
29 namespace OHOS {
30 namespace Msdp {
31 namespace DeviceStatus {
32 class CoordinationManagerImpl final {
33 public:
34     using FuncCoordinationMessage = std::function<void(const std::string&, const CoordinationMsgInfo&)>;
35     using FuncCoordinationState = std::function<void(bool)>;
36     using CoordinationMsg = FuncCoordinationMessage;
37     using CoordinationState = FuncCoordinationState;
38     using CoordinationListenerPtr = std::shared_ptr<ICoordinationListener>;
39     using HotAreaListenerPtr = std::shared_ptr<IHotAreaListener>;
40     struct CoordinationEvent {
41         CoordinationMsg msg;
42         CoordinationState state;
43     };
44 
45     enum class CallbackMessageId {
46         PREPARE,
47         UNPREPARE,
48         ACTIVATE,
49         DEACTIVATE,
50         GET_COORDINATION
51     };
52 
53     CoordinationManagerImpl() = default;
54     ~CoordinationManagerImpl() = default;
55 
56     int32_t RegisterCoordinationListener(CoordinationListenerPtr listener, bool isCompatible = false);
57     int32_t UnregisterCoordinationListener(CoordinationListenerPtr listener, bool isCompatible = false);
58     int32_t PrepareCoordination(FuncCoordinationMessage callback, bool isCompatible = false);
59     int32_t UnprepareCoordination(FuncCoordinationMessage callback, bool isCompatible = false);
60     int32_t ActivateCoordination(const std::string &remoteNetworkId, int32_t startDeviceId,
61         FuncCoordinationMessage callback, bool isCompatible = false);
62     int32_t DeactivateCoordination(bool isUnchained, FuncCoordinationMessage callback, bool isCompatible = false);
63     int32_t GetCoordinationState(const std::string &networkId,
64         FuncCoordinationState callback, bool isCompatible = false);
65     int32_t GetCoordinationState(const std::string &udId, bool &state);
66     void OnDevCoordinationListener(const std::string networkId, CoordinationMessage msg);
67     void OnCoordinationMessageEvent(int32_t userData, const std::string networkId, CoordinationMessage msg);
68     void OnCoordinationStateEvent(int32_t userData, bool state);
69     int32_t OnCoordinationListener(const StreamClient &client, NetPacket &pkt);
70     int32_t OnCoordinationMessage(const StreamClient &client, NetPacket &pkt);
71     int32_t OnCoordinationState(const StreamClient &client, NetPacket &pkt);
72 
73     int32_t AddHotAreaListener(HotAreaListenerPtr listener);
74     void OnDevHotAreaListener(int32_t displayX, int32_t displayY, HotAreaType type, bool isEdge);
75     int32_t OnHotAreaListener(const StreamClient &client, NetPacket &pkt);
76     int32_t RemoveHotAreaListener(HotAreaListenerPtr listener = nullptr);
77     void OnConnected();
78 private:
79     void SetMessageCallback(CallbackMessageId id, FuncCoordinationMessage callback);
80     void SetStateCallback(CallbackMessageId id, FuncCoordinationState callback);
81 private:
82     std::list<CoordinationListenerPtr> devCoordinationListener_;
83     std::map<int32_t, CoordinationEvent> devCoordinationEvent_;
84     std::list<HotAreaListenerPtr> devHotAreaListener_;
85     mutable std::mutex mtx_;
86     std::atomic_bool isListeningProcess_ { false };
87     bool isHotAreaListener_ { false };
88     IClientPtr client_ { nullptr };
89     std::function<void(std::string, CoordinationMessage)> prepareCooCallback_ { nullptr };
90     bool isPrepareCooIsCompatible_ { false };
91 };
92 } // namespace DeviceStatus
93 } // namespace Msdp
94 } // namespace OHOS
95 #endif // COORDINATION_MANAGER_IMPL_H
96