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 #include "event_manager.h"
17
18 #include "devicestatus_define.h"
19 #include "utility.h"
20
21 #undef LOG_TAG
22 #define LOG_TAG "EventManager"
23
24 namespace OHOS {
25 namespace Msdp {
26 namespace DeviceStatus {
27 namespace Cooperate {
28
EventManager(IContext * env)29 EventManager::EventManager(IContext *env)
30 : env_(env)
31 {}
32
RegisterListener(const RegisterListenerEvent & event)33 void EventManager::RegisterListener(const RegisterListenerEvent &event)
34 {
35 CALL_INFO_TRACE;
36 std::shared_ptr<EventInfo> eventInfo = std::make_shared<EventInfo>();
37 eventInfo->type = EventType::LISTENER;
38 eventInfo->msgId = MessageId::COORDINATION_ADD_LISTENER;
39 eventInfo->pid = event.pid;
40
41 FI_HILOGI("Add cooperate listener (%{public}d)", eventInfo->pid);
42 auto iter = std::find_if(listeners_.begin(), listeners_.end(),
43 [eventInfo](const auto &item) {
44 return ((item != nullptr) && (item->pid == eventInfo->pid));
45 });
46 if (iter != listeners_.end()) {
47 *iter = eventInfo;
48 } else {
49 listeners_.emplace_back(eventInfo);
50 }
51 }
52
UnregisterListener(const UnregisterListenerEvent & event)53 void EventManager::UnregisterListener(const UnregisterListenerEvent &event)
54 {
55 FI_HILOGI("Remove cooperate listener (%{public}d)", event.pid);
56 listeners_.erase(std::remove_if(listeners_.begin(), listeners_.end(),
57 [pid = event.pid](const auto &item) {
58 return ((item == nullptr) || (item->pid == pid));
59 }), listeners_.end());
60 }
61
EnableCooperate(const EnableCooperateEvent & event)62 void EventManager::EnableCooperate(const EnableCooperateEvent &event)
63 {
64 CALL_INFO_TRACE;
65 std::string networkId;
66 NotifyCooperateMessage(event.pid, MessageId::COORDINATION_MESSAGE,
67 event.userData, networkId, CoordinationMessage::PREPARE);
68 }
69
DisableCooperate(const DisableCooperateEvent & event)70 void EventManager::DisableCooperate(const DisableCooperateEvent &event)
71 {
72 CALL_INFO_TRACE;
73 std::string networkId;
74 NotifyCooperateMessage(event.pid, MessageId::COORDINATION_MESSAGE,
75 event.userData, networkId, CoordinationMessage::UNPREPARE);
76 }
77
StartCooperate(const StartCooperateEvent & event)78 void EventManager::StartCooperate(const StartCooperateEvent &event)
79 {
80 CALL_INFO_TRACE;
81 std::shared_ptr<EventInfo> eventInfo = std::make_shared<EventInfo>();
82 eventInfo->type = EventType::START;
83 eventInfo->msgId = MessageId::COORDINATION_MESSAGE;
84 eventInfo->pid = event.pid;
85 eventInfo->networkId = event.remoteNetworkId;
86 eventInfo->userData = event.userData;
87 calls_[EventType::START] = eventInfo;
88 }
89
StartCooperateFinish(const DSoftbusStartCooperateFinished & event)90 void EventManager::StartCooperateFinish(const DSoftbusStartCooperateFinished &event)
91 {
92 CALL_INFO_TRACE;
93 std::shared_ptr<EventInfo> eventInfo = calls_[EventType::START];
94 CHKPV(eventInfo);
95 CoordinationMessage msg = (event.success ?
96 CoordinationMessage::ACTIVATE_SUCCESS :
97 CoordinationMessage::ACTIVATE_FAIL);
98 NotifyCooperateMessage(eventInfo->pid, eventInfo->msgId, eventInfo->userData, eventInfo->networkId, msg);
99 calls_[EventType::START] = nullptr;
100 }
101
RemoteStart(const DSoftbusStartCooperate & event)102 void EventManager::RemoteStart(const DSoftbusStartCooperate &event)
103 {
104 CALL_INFO_TRACE;
105 OnCooperateMessage(CoordinationMessage::ACTIVATE, event.networkId);
106 }
107
RemoteStartFinish(const DSoftbusStartCooperateFinished & event)108 void EventManager::RemoteStartFinish(const DSoftbusStartCooperateFinished &event)
109 {
110 CALL_INFO_TRACE;
111 CoordinationMessage msg { event.success ?
112 CoordinationMessage::ACTIVATE_SUCCESS :
113 CoordinationMessage::ACTIVATE_FAIL };
114 OnCooperateMessage(msg, event.networkId);
115 }
116
OnUnchain(const StopCooperateEvent & event)117 void EventManager::OnUnchain(const StopCooperateEvent &event)
118 {
119 CALL_INFO_TRACE;
120 OnCooperateMessage(CoordinationMessage::SESSION_CLOSED, std::string());
121 }
122
StopCooperate(const StopCooperateEvent & event)123 void EventManager::StopCooperate(const StopCooperateEvent &event)
124 {
125 CALL_INFO_TRACE;
126 std::shared_ptr<EventInfo> eventInfo = std::make_shared<EventInfo>();
127 eventInfo->type = EventType::STOP;
128 eventInfo->msgId = MessageId::COORDINATION_MESSAGE;
129 eventInfo->pid = event.pid;
130 eventInfo->userData = event.userData;
131 calls_[EventType::STOP] = eventInfo;
132 }
133
StopCooperateFinish(const DSoftbusStopCooperateFinished & event)134 void EventManager::StopCooperateFinish(const DSoftbusStopCooperateFinished &event)
135 {
136 CALL_INFO_TRACE;
137 std::shared_ptr<EventInfo> eventInfo = calls_[EventType::STOP];
138 CHKPV(eventInfo);
139 CoordinationMessage msg = (event.normal ?
140 CoordinationMessage::DEACTIVATE_SUCCESS :
141 CoordinationMessage::DEACTIVATE_FAIL);
142 NotifyCooperateMessage(eventInfo->pid, eventInfo->msgId, eventInfo->userData, eventInfo->networkId, msg);
143 calls_[EventType::STOP] = nullptr;
144 }
145
RemoteStop(const DSoftbusStopCooperate & event)146 void EventManager::RemoteStop(const DSoftbusStopCooperate &event)
147 {
148 CALL_DEBUG_ENTER;
149 }
150
RemoteStopFinish(const DSoftbusStopCooperateFinished & event)151 void EventManager::RemoteStopFinish(const DSoftbusStopCooperateFinished &event)
152 {
153 CALL_DEBUG_ENTER;
154 }
155
OnProfileChanged(const DDPCooperateSwitchChanged & event)156 void EventManager::OnProfileChanged(const DDPCooperateSwitchChanged &event)
157 {
158 CALL_INFO_TRACE;
159 FI_HILOGI("Switch status of \'%{public}s\' has changed to %{public}d",
160 Utility::Anonymize(event.networkId).c_str(), event.normal);
161 CoordinationMessage msg = (event.normal ? CoordinationMessage::PREPARE : CoordinationMessage::UNPREPARE);
162 OnCooperateMessage(msg, event.networkId);
163 }
164
OnSoftbusSessionClosed(const DSoftbusSessionClosed & event)165 void EventManager::OnSoftbusSessionClosed(const DSoftbusSessionClosed &event)
166 {
167 FI_HILOGI("Connection with \'%{public}s\' is closed", Utility::Anonymize(event.networkId).c_str());
168 OnCooperateMessage(CoordinationMessage::SESSION_CLOSED, event.networkId);
169 }
170
OnCooperateMessage(CoordinationMessage msg,const std::string & networkId)171 void EventManager::OnCooperateMessage(CoordinationMessage msg, const std::string &networkId)
172 {
173 CALL_INFO_TRACE;
174 for (auto iter = listeners_.begin(); iter != listeners_.end(); ++iter) {
175 std::shared_ptr<EventInfo> listener = *iter;
176 CHKPC(listener);
177 FI_HILOGD("Notify cooperate listener (%{public}d, %{public}d)", listener->pid, listener->msgId);
178 NotifyCooperateMessage(listener->pid, listener->msgId, listener->userData, networkId, msg);
179 }
180 }
181
NotifyCooperateMessage(int32_t pid,MessageId msgId,int32_t userData,const std::string & networkId,CoordinationMessage msg)182 void EventManager::NotifyCooperateMessage(int32_t pid, MessageId msgId, int32_t userData,
183 const std::string &networkId, CoordinationMessage msg)
184 {
185 CALL_INFO_TRACE;
186 auto session = env_->GetSocketSessionManager().FindSessionByPid(pid);
187 CHKPV(session);
188 NetPacket pkt(msgId);
189 pkt << userData << networkId << static_cast<int32_t>(msg);
190 if (pkt.ChkRWError()) {
191 FI_HILOGE("Packet write data failed");
192 return;
193 }
194 if (!session->SendMsg(pkt)) {
195 FI_HILOGE("Sending failed");
196 }
197 }
198
NotifyCooperateState(int32_t pid,MessageId msgId,int32_t userData,bool state)199 void EventManager::NotifyCooperateState(int32_t pid, MessageId msgId, int32_t userData, bool state)
200 {
201 CALL_INFO_TRACE;
202 CHKPV(env_);
203 auto session = env_->GetSocketSessionManager().FindSessionByPid(pid);
204 CHKPV(session);
205 NetPacket pkt(msgId);
206 pkt << userData << state;
207 if (pkt.ChkRWError()) {
208 FI_HILOGE("Packet write data failed");
209 return;
210 }
211 if (!session->SendMsg(pkt)) {
212 FI_HILOGE("Sending failed");
213 return;
214 }
215 }
216 } // namespace Cooperate
217 } // namespace DeviceStatus
218 } // namespace Msdp
219 } // namespace OHOS
220