• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "coordination_event_manager.h"
17 #include "devicestatus_define.h"
18 
19 namespace OHOS {
20 namespace Msdp {
21 namespace DeviceStatus {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "CoordinationEventManager" };
24 } // namespace
25 
CoordinationEventManager()26 CoordinationEventManager::CoordinationEventManager() {}
~CoordinationEventManager()27 CoordinationEventManager::~CoordinationEventManager() {}
28 
AddCoordinationEvent(sptr<EventInfo> event)29 void CoordinationEventManager::AddCoordinationEvent(sptr<EventInfo> event)
30 {
31     CALL_DEBUG_ENTER;
32     CHKPV(event);
33     std::lock_guard<std::mutex> guard(lock_);
34     if (event->type == EventType::LISTENER) {
35         auto it = std::find_if(remoteCoordinationCallbacks_.begin(), remoteCoordinationCallbacks_.end(),
36             [event] (auto info) {
37                 return (*info).sess == event->sess;
38             });
39         if (it != remoteCoordinationCallbacks_.end()) {
40             *it = event;
41         } else {
42             remoteCoordinationCallbacks_.emplace_back(event);
43         }
44     } else {
45         coordinationCallbacks_[event->type] = event;
46     }
47 }
48 
RemoveCoordinationEvent(sptr<EventInfo> event)49 void CoordinationEventManager::RemoveCoordinationEvent(sptr<EventInfo> event)
50 {
51     CALL_DEBUG_ENTER;
52     std::lock_guard<std::mutex> guard(lock_);
53     if (remoteCoordinationCallbacks_.empty() || event == nullptr) {
54         FI_HILOGE("Remove listener failed");
55         return;
56     }
57     for (auto it = remoteCoordinationCallbacks_.begin(); it != remoteCoordinationCallbacks_.end(); ++it) {
58         if ((*it)->sess == event->sess) {
59             remoteCoordinationCallbacks_.erase(it);
60             return;
61         }
62     }
63 }
64 
OnCoordinationMessage(CoordinationMessage msg,const std::string & networkId)65 int32_t CoordinationEventManager::OnCoordinationMessage(CoordinationMessage msg, const std::string &networkId)
66 {
67     CALL_DEBUG_ENTER;
68     std::lock_guard<std::mutex> guard(lock_);
69     if (remoteCoordinationCallbacks_.empty()) {
70         FI_HILOGW("The current listener is empty, unable to invoke the listening interface");
71         return RET_ERR;
72     }
73     for (auto it = remoteCoordinationCallbacks_.begin(); it != remoteCoordinationCallbacks_.end(); ++it) {
74         sptr<EventInfo> info = *it;
75         CHKPC(info);
76         NotifyCoordinationMessage(info->sess, info->msgId, info->userData, networkId, msg);
77     }
78     return RET_OK;
79 }
80 
OnEnable(CoordinationMessage msg,const std::string & networkId)81 void CoordinationEventManager::OnEnable(CoordinationMessage msg, const std::string &networkId)
82 {
83     CALL_DEBUG_ENTER;
84     std::lock_guard<std::mutex> guard(lock_);
85     sptr<EventInfo> info = coordinationCallbacks_[EventType::ENABLE];
86     CHKPV(info);
87     NotifyCoordinationMessage(info->sess, info->msgId, info->userData, networkId, msg);
88     coordinationCallbacks_[EventType::ENABLE] = nullptr;
89 }
90 
OnStart(CoordinationMessage msg,const std::string & networkId)91 void CoordinationEventManager::OnStart(CoordinationMessage msg, const std::string &networkId)
92 {
93     CALL_DEBUG_ENTER;
94     std::lock_guard<std::mutex> guard(lock_);
95     sptr<EventInfo> info = coordinationCallbacks_[EventType::START];
96     CHKPV(info);
97     NotifyCoordinationMessage(info->sess, info->msgId, info->userData, networkId, msg);
98     coordinationCallbacks_[EventType::START] = nullptr;
99 }
100 
OnStop(CoordinationMessage msg,const std::string & networkId)101 void CoordinationEventManager::OnStop(CoordinationMessage msg, const std::string &networkId)
102 {
103     CALL_DEBUG_ENTER;
104     std::lock_guard<std::mutex> guard(lock_);
105     sptr<EventInfo> info = coordinationCallbacks_[EventType::STOP];
106     CHKPV(info);
107     NotifyCoordinationMessage(info->sess, info->msgId, info->userData, networkId, msg);
108     coordinationCallbacks_[EventType::STOP] = nullptr;
109 }
110 
OnGetCrossingSwitchState(bool state)111 void CoordinationEventManager::OnGetCrossingSwitchState(bool state)
112 {
113     CALL_DEBUG_ENTER;
114     std::lock_guard<std::mutex> guard(lock_);
115     sptr<EventInfo> info = coordinationCallbacks_[EventType::STATE];
116     CHKPV(info);
117     NotifyCoordinationState(info->sess, info->msgId, info->userData, state);
118     coordinationCallbacks_[EventType::STATE] = nullptr;
119 }
120 
OnErrorMessage(EventType type,CoordinationMessage msg)121 void CoordinationEventManager::OnErrorMessage(EventType type, CoordinationMessage msg)
122 {
123     std::lock_guard<std::mutex> guard(lock_);
124     sptr<EventInfo> info = coordinationCallbacks_[type];
125     CHKPV(info);
126     NotifyCoordinationMessage(info->sess, info->msgId, info->userData, "", msg);
127     coordinationCallbacks_[type] = nullptr;
128 }
129 
SetIContext(IContext * context)130 void CoordinationEventManager::SetIContext(IContext *context)
131 {
132     context_ = context;
133 }
134 
GetIContext() const135 IContext* CoordinationEventManager::GetIContext() const
136 {
137     return context_;
138 }
139 
NotifyCoordinationMessage(SessionPtr sess,MessageId msgId,int32_t userData,const std::string & networkId,CoordinationMessage msg)140 void CoordinationEventManager::NotifyCoordinationMessage(
141     SessionPtr sess, MessageId msgId, int32_t userData, const std::string &networkId, CoordinationMessage msg)
142 {
143     CALL_DEBUG_ENTER;
144     CHKPV(sess);
145     NetPacket pkt(msgId);
146     pkt << userData << networkId << static_cast<int32_t>(msg);
147     if (pkt.ChkRWError()) {
148         FI_HILOGE("Packet write data failed");
149         return;
150     }
151     if (!sess->SendMsg(pkt)) {
152         FI_HILOGE("Sending failed");
153         return;
154     }
155 }
156 
NotifyCoordinationState(SessionPtr sess,MessageId msgId,int32_t userData,bool state)157 void CoordinationEventManager::NotifyCoordinationState(SessionPtr sess, MessageId msgId, int32_t userData, bool state)
158 {
159     CALL_DEBUG_ENTER;
160     CHKPV(sess);
161     NetPacket pkt(msgId);
162     pkt << userData << state;
163     if (pkt.ChkRWError()) {
164         FI_HILOGE("Coordination state, packet write data failed");
165         return;
166     }
167     if (!sess->SendMsg(pkt)) {
168         FI_HILOGE("Coordination state, sending failed");
169         return;
170     }
171 }
172 } // namespace DeviceStatus
173 } // namespace Msdp
174 } // namespace OHOS
175