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 if (remoteCoordinationCallbacks_.empty() || event == nullptr) {
53 FI_HILOGE("Remove listener failed");
54 return;
55 }
56 for (auto it = remoteCoordinationCallbacks_.begin(); it != remoteCoordinationCallbacks_.end(); ++it) {
57 if ((*it)->sess == event->sess) {
58 remoteCoordinationCallbacks_.erase(it);
59 return;
60 }
61 }
62 }
63
OnCoordinationMessage(CoordinationMessage msg,const std::string & deviceId)64 int32_t CoordinationEventManager::OnCoordinationMessage(CoordinationMessage msg, const std::string &deviceId)
65 {
66 CALL_DEBUG_ENTER;
67 std::lock_guard<std::mutex> guard(lock_);
68 if (remoteCoordinationCallbacks_.empty()) {
69 FI_HILOGW("The current listener is empty, unable to invoke the listening interface");
70 return RET_ERR;
71 }
72 for (auto it = remoteCoordinationCallbacks_.begin(); it != remoteCoordinationCallbacks_.end(); ++it) {
73 sptr<EventInfo> info = *it;
74 CHKPC(info);
75 NotifyCoordinationMessage(info->sess, info->msgId, info->userData, deviceId, msg);
76 }
77 return RET_OK;
78 }
79
OnEnable(CoordinationMessage msg,const std::string & deviceId)80 void CoordinationEventManager::OnEnable(CoordinationMessage msg, const std::string &deviceId)
81 {
82 CALL_DEBUG_ENTER;
83 std::lock_guard<std::mutex> guard(lock_);
84 sptr<EventInfo> info = coordinationCallbacks_[EventType::ENABLE];
85 CHKPV(info);
86 NotifyCoordinationMessage(info->sess, info->msgId, info->userData, deviceId, msg);
87 coordinationCallbacks_[EventType::ENABLE] = nullptr;
88 }
89
OnStart(CoordinationMessage msg,const std::string & deviceId)90 void CoordinationEventManager::OnStart(CoordinationMessage msg, const std::string &deviceId)
91 {
92 CALL_DEBUG_ENTER;
93 std::lock_guard<std::mutex> guard(lock_);
94 sptr<EventInfo> info = coordinationCallbacks_[EventType::START];
95 CHKPV(info);
96 NotifyCoordinationMessage(info->sess, info->msgId, info->userData, deviceId, msg);
97 coordinationCallbacks_[EventType::START] = nullptr;
98 }
99
OnStop(CoordinationMessage msg,const std::string & deviceId)100 void CoordinationEventManager::OnStop(CoordinationMessage msg, const std::string &deviceId)
101 {
102 CALL_DEBUG_ENTER;
103 std::lock_guard<std::mutex> guard(lock_);
104 sptr<EventInfo> info = coordinationCallbacks_[EventType::STOP];
105 CHKPV(info);
106 NotifyCoordinationMessage(info->sess, info->msgId, info->userData, deviceId, msg);
107 coordinationCallbacks_[EventType::STOP] = nullptr;
108 }
109
OnGetCrossingSwitchState(bool state)110 void CoordinationEventManager::OnGetCrossingSwitchState(bool state)
111 {
112 CALL_DEBUG_ENTER;
113 std::lock_guard<std::mutex> guard(lock_);
114 sptr<EventInfo> info = coordinationCallbacks_[EventType::STATE];
115 CHKPV(info);
116 NotifyCoordinationState(info->sess, info->msgId, info->userData, state);
117 coordinationCallbacks_[EventType::STATE] = nullptr;
118 }
119
OnErrorMessage(EventType type,CoordinationMessage msg)120 void CoordinationEventManager::OnErrorMessage(EventType type, CoordinationMessage msg)
121 {
122 std::lock_guard<std::mutex> guard(lock_);
123 sptr<EventInfo> info = coordinationCallbacks_[type];
124 CHKPV(info);
125 NotifyCoordinationMessage(info->sess, info->msgId, info->userData, "", msg);
126 coordinationCallbacks_[type] = nullptr;
127 }
128
SetIContext(IContext * context)129 void CoordinationEventManager::SetIContext(IContext *context)
130 {
131 context_ = context;
132 }
133
GetIContext() const134 IContext* CoordinationEventManager::GetIContext() const
135 {
136 return context_;
137 }
138
NotifyCoordinationMessage(SessionPtr sess,MessageId msgId,int32_t userData,const std::string & deviceId,CoordinationMessage msg)139 void CoordinationEventManager::NotifyCoordinationMessage(
140 SessionPtr sess, MessageId msgId, int32_t userData, const std::string &deviceId, CoordinationMessage msg)
141 {
142 CALL_DEBUG_ENTER;
143 CHKPV(sess);
144 NetPacket pkt(msgId);
145 pkt << userData << deviceId << static_cast<int32_t>(msg);
146 if (pkt.ChkRWError()) {
147 FI_HILOGE("Packet write data failed");
148 return;
149 }
150 if (!sess->SendMsg(pkt)) {
151 FI_HILOGE("Sending failed");
152 return;
153 }
154 }
155
NotifyCoordinationState(SessionPtr sess,MessageId msgId,int32_t userData,bool state)156 void CoordinationEventManager::NotifyCoordinationState(SessionPtr sess, MessageId msgId, int32_t userData, bool state)
157 {
158 CALL_DEBUG_ENTER;
159 CHKPV(sess);
160 NetPacket pkt(msgId);
161 pkt << userData << state;
162 if (pkt.ChkRWError()) {
163 FI_HILOGE("Packet write data failed");
164 return;
165 }
166 if (!sess->SendMsg(pkt)) {
167 FI_HILOGE("Sending failed");
168 return;
169 }
170 }
171 } // namespace DeviceStatus
172 } // namespace Msdp
173 } // namespace OHOS
174