1 /*
2 * Copyright (c) 2022 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 "cooperate_event_manager.h"
17
18 #include "define_multimodal.h"
19
20 namespace OHOS {
21 namespace MMI {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "CooperateEventManager"};
24 } // namespace
25
CooperateEventManager()26 CooperateEventManager::CooperateEventManager() {}
~CooperateEventManager()27 CooperateEventManager::~CooperateEventManager() {}
28
AddCooperationEvent(sptr<EventInfo> event)29 void CooperateEventManager::AddCooperationEvent(sptr<EventInfo> event)
30 {
31 CALL_DEBUG_ENTER;
32 std::lock_guard<std::mutex> guard(lock_);
33 if (event->type == EventType::LISTENER) {
34 remoteCooperateCallbacks_.emplace_back(event);
35 } else {
36 cooperateCallbacks_[event->type] = event;
37 }
38 }
39
RemoveCooperationEvent(sptr<EventInfo> event)40 void CooperateEventManager::RemoveCooperationEvent(sptr<EventInfo> event)
41 {
42 CALL_DEBUG_ENTER;
43 if (remoteCooperateCallbacks_.empty() || event == nullptr) {
44 MMI_HILOGE("Remove listener failed");
45 return;
46 }
47 for (auto it = remoteCooperateCallbacks_.begin(); it != remoteCooperateCallbacks_.end(); ++it) {
48 if ((*it)->sess == event->sess) {
49 remoteCooperateCallbacks_.erase(it);
50 return;
51 }
52 }
53 }
54
OnCooperateMessage(CooperationMessage msg,const std::string & deviceId)55 int32_t CooperateEventManager::OnCooperateMessage(CooperationMessage msg, const std::string &deviceId)
56 {
57 CALL_DEBUG_ENTER;
58 std::lock_guard<std::mutex> guard(lock_);
59 if (remoteCooperateCallbacks_.empty()) {
60 MMI_HILOGE("No listener, send cooperate message failed");
61 return RET_ERR;
62 }
63 for (auto it = remoteCooperateCallbacks_.begin(); it != remoteCooperateCallbacks_.end(); ++it) {
64 sptr<EventInfo> info = *it;
65 CHKPC(info);
66 NotifyCooperateMessage(info->sess, info->msgId, info->userData, deviceId, msg);
67 }
68 return RET_OK;
69 }
70
OnEnable(CooperationMessage msg,const std::string & deviceId)71 void CooperateEventManager::OnEnable(CooperationMessage msg, const std::string &deviceId)
72 {
73 CALL_DEBUG_ENTER;
74 std::lock_guard<std::mutex> guard(lock_);
75 sptr<EventInfo> info = cooperateCallbacks_[EventType::ENABLE];
76 CHKPV(info);
77 NotifyCooperateMessage(info->sess, info->msgId, info->userData, deviceId, msg);
78 cooperateCallbacks_[EventType::ENABLE] = nullptr;
79 }
80
OnStart(CooperationMessage msg,const std::string & deviceId)81 void CooperateEventManager::OnStart(CooperationMessage msg, const std::string &deviceId)
82 {
83 CALL_DEBUG_ENTER;
84 std::lock_guard<std::mutex> guard(lock_);
85 sptr<EventInfo> info = cooperateCallbacks_[EventType::START];
86 CHKPV(info);
87 NotifyCooperateMessage(info->sess, info->msgId, info->userData, deviceId, msg);
88 cooperateCallbacks_[EventType::START] = nullptr;
89 }
90
OnStop(CooperationMessage msg,const std::string & deviceId)91 void CooperateEventManager::OnStop(CooperationMessage msg, const std::string &deviceId)
92 {
93 CALL_DEBUG_ENTER;
94 std::lock_guard<std::mutex> guard(lock_);
95 sptr<EventInfo> info = cooperateCallbacks_[EventType::STOP];
96 CHKPV(info);
97 NotifyCooperateMessage(info->sess, info->msgId, info->userData, deviceId, msg);
98 cooperateCallbacks_[EventType::STOP] = nullptr;
99 }
100
OnGetState(bool state)101 void CooperateEventManager::OnGetState(bool state)
102 {
103 CALL_DEBUG_ENTER;
104 std::lock_guard<std::mutex> guard(lock_);
105 sptr<EventInfo> info = cooperateCallbacks_[EventType::STATE];
106 CHKPV(info);
107 NotifyCooperateState(info->sess, info->msgId, info->userData, state);
108 cooperateCallbacks_[EventType::STATE] = nullptr;
109 }
110
OnErrorMessage(EventType type,CooperationMessage msg)111 void CooperateEventManager::OnErrorMessage(EventType type, CooperationMessage msg)
112 {
113 std::lock_guard<std::mutex> guard(lock_);
114 sptr<EventInfo> info = cooperateCallbacks_[type];
115 CHKPV(info);
116 NotifyCooperateMessage(info->sess, info->msgId, info->userData, "", msg);
117 cooperateCallbacks_[type] = nullptr;
118 }
119
NotifyCooperateMessage(SessionPtr sess,MmiMessageId msgId,int32_t userData,const std::string & deviceId,CooperationMessage msg)120 void CooperateEventManager::NotifyCooperateMessage(
121 SessionPtr sess, MmiMessageId msgId, int32_t userData, const std::string &deviceId, CooperationMessage msg)
122 {
123 CALL_DEBUG_ENTER;
124 CHKPV(sess);
125 NetPacket pkt(msgId);
126 pkt << userData << deviceId << static_cast<int32_t>(msg);
127 if (pkt.ChkRWError()) {
128 MMI_HILOGE("Packet write data failed");
129 return;
130 }
131 if (!sess->SendMsg(pkt)) {
132 MMI_HILOGE("Sending failed");
133 return;
134 }
135 }
136
NotifyCooperateState(SessionPtr sess,MmiMessageId msgId,int32_t userData,bool state)137 void CooperateEventManager::NotifyCooperateState(SessionPtr sess, MmiMessageId msgId, int32_t userData, bool state)
138 {
139 CALL_DEBUG_ENTER;
140 CHKPV(sess);
141 NetPacket pkt(msgId);
142 pkt << userData << state;
143 if (pkt.ChkRWError()) {
144 MMI_HILOGE("Packet write data failed");
145 return;
146 }
147 if (!sess->SendMsg(pkt)) {
148 MMI_HILOGE("Sending failed");
149 return;
150 }
151 }
152 } // namespace MMI
153 } // namespace OHOS
154