1 /*
2  * Copyright (c) 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 COOPERATE_EVENT_MANAGER_H
17 #define COOPERATE_EVENT_MANAGER_H
18 
19 #include <list>
20 #include <mutex>
21 
22 #include "nocopyable.h"
23 
24 #include "cooperate_events.h"
25 #include "coordination_message.h"
26 #include "i_context.h"
27 
28 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31 namespace Cooperate {
32 class EventManager final {
33 public:
34     enum EventType {
35         LISTENER,
36         ENABLE,
37         START,
38         STOP,
39         STATE,
40     };
41 
42     struct EventInfo {
43         EventType type { LISTENER };
44         MessageId msgId { MessageId::INVALID };
45         int32_t pid { -1 };
46         int32_t userData { -1 };
47         std::string networkId;
48         CoordinationMessage msg { CoordinationMessage::PREPARE };
49         bool state { false };
50     };
51 
52     struct CooperateNotice {
53         int32_t pid { -1 };
54         MessageId msgId { MessageId::INVALID };
55         int32_t userData { -1 };
56         std::string networkId;
57         CoordinationMessage msg { CoordinationMessage::PREPARE };
58         int32_t errCode { static_cast<int32_t>(CoordinationErrCode::COORDINATION_OK) };
59     };
60 
61     struct CooperateStateNotice {
62         int32_t pid { -1 };
63         MessageId msgId { MessageId::INVALID };
64         int32_t userData { -1 };
65         bool state{ false };
66         int32_t errCode { static_cast<int32_t>(CoordinationErrCode::COORDINATION_OK) };
67     };
68 
69     EventManager(IContext *env);
70     ~EventManager() = default;
71     DISALLOW_COPY_AND_MOVE(EventManager);
72 
73     void RegisterListener(const RegisterListenerEvent &event);
74     void UnregisterListener(const UnregisterListenerEvent &event);
75     void EnableCooperate(const EnableCooperateEvent &event);
76     void DisableCooperate(const DisableCooperateEvent &event);
77     void StartCooperate(const StartCooperateEvent &event);
78     void StartCooperateFinish(const DSoftbusStartCooperateFinished &event);
79     void RemoteStart(const DSoftbusStartCooperate &event);
80     void RemoteStartFinish(const DSoftbusStartCooperateFinished &event);
81     void OnUnchain(const StopCooperateEvent &event);
82     void StopCooperate(const StopCooperateEvent &event);
83     void StopCooperateFinish(const DSoftbusStopCooperateFinished &event);
84     void RemoteStop(const DSoftbusStopCooperate &event);
85     void RemoteStopFinish(const DSoftbusStopCooperateFinished &event);
86     void OnProfileChanged(const DDPCooperateSwitchChanged &event);
87     void OnSoftbusSessionClosed(const DSoftbusSessionClosed &event);
88     void GetCooperateState(const CooperateStateNotice &notice);
89     void OnClientDied(const ClientDiedEvent &event);
90 
91 private:
92     void OnCooperateMessage(CoordinationMessage msg, const std::string &networkId);
93     void NotifyCooperateMessage(const CooperateNotice &notice);
94     void NotifyCooperateState(const CooperateStateNotice &notice);
95 
96 private:
97     IContext *env_ { nullptr };
98     std::list<std::shared_ptr<EventInfo>> listeners_;
99     std::map<EventType, std::shared_ptr<EventInfo>> calls_ {
100         { EventType::ENABLE, nullptr },
101         { EventType::START, nullptr },
102         { EventType::STOP, nullptr },
103         { EventType::STATE, nullptr }
104     };
105 };
106 } // namespace Cooperate
107 } // namespace DeviceStatus
108 } // namespace Msdp
109 } // namespace OHOS
110 #endif // COOPERATE_EVENT_MANAGER_H
111