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_EVENT_MANAGER_H 17 #define COORDINATION_EVENT_MANAGER_H 18 19 #include <list> 20 #include <mutex> 21 #include <string> 22 23 #include "nocopyable.h" 24 #include "refbase.h" 25 #include "singleton.h" 26 #include "stream_session.h" 27 28 #include "coordination_message.h" 29 #include "fi_log.h" 30 #include "i_context.h" 31 32 namespace OHOS { 33 namespace Msdp { 34 namespace DeviceStatus { 35 class CoordinationEventManager final { 36 DECLARE_DELAYED_SINGLETON(CoordinationEventManager); 37 public: 38 enum EventType { LISTENER, ENABLE, START, STOP, STATE }; 39 struct EventInfo : public RefBase { 40 EventType type { LISTENER }; 41 SessionPtr sess { nullptr }; 42 MessageId msgId { MessageId::INVALID }; 43 int32_t userData { -1 }; 44 std::string networkId; 45 CoordinationMessage msg { CoordinationMessage::PREPARE }; 46 bool state { false }; 47 }; 48 49 DISALLOW_COPY_AND_MOVE(CoordinationEventManager); 50 51 void AddCoordinationEvent(sptr<EventInfo> event); 52 void RemoveCoordinationEvent(sptr<EventInfo> event); 53 int32_t OnCoordinationMessage(CoordinationMessage msg, const std::string &networkId = ""); 54 void OnEnable(CoordinationMessage msg, const std::string &networkId = ""); 55 void OnStart(CoordinationMessage msg, const std::string &networkId = ""); 56 void OnStop(CoordinationMessage msg, const std::string &networkId = ""); 57 void OnGetCrossingSwitchState(bool state); 58 void OnErrorMessage(EventType type, CoordinationMessage msg); 59 void SetIContext(IContext *context); 60 IContext* GetIContext() const; 61 62 private: 63 void NotifyCoordinationMessage(SessionPtr sess, MessageId msgId, int32_t userData, 64 const std::string &networkId, CoordinationMessage msg); 65 void NotifyCoordinationState(SessionPtr sess, MessageId msgId, int32_t userData, bool state); 66 67 private: 68 std::mutex lock_; 69 std::list<sptr<EventInfo>> remoteCoordinationCallbacks_; 70 std::map<EventType, sptr<EventInfo>> coordinationCallbacks_{ 71 { EventType::ENABLE, nullptr }, 72 { EventType::START, nullptr }, 73 { EventType::STOP, nullptr }, 74 { EventType::STATE, nullptr } 75 }; 76 IContext *context_ { nullptr }; 77 }; 78 79 #define COOR_EVENT_MGR OHOS::DelayedSingleton<CoordinationEventManager>::GetInstance() 80 } // namespace DeviceStatus 81 } // namespace Msdp 82 } // namespace OHOS 83 #endif // COORDINATION_EVENT_MANAGER_H 84