• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dinput_adapter.h"
17 
18 #include <algorithm>
19 
20 #include "distributed_input_kit.h"
21 
22 #include "devicestatus_define.h"
23 
24 namespace OHOS {
25 namespace Msdp {
26 namespace DeviceStatus {
27 namespace {
28 constexpr HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DInputAdapter" };
29 constexpr int32_t DEFAULT_DELAY_TIME { 4000 };
30 constexpr int32_t RETRY_TIME { 2 };
31 } // namespace
32 
33 using namespace DistributedHardware::DistributedInput;
34 
DInputAdapter(IContext * env)35 DInputAdapter::DInputAdapter(IContext *env)
36     : env_(env)
37 {}
38 
IsNeedFilterOut(const std::string & networkId,BusinessEvent && event)39 bool DInputAdapter::IsNeedFilterOut(const std::string &networkId, BusinessEvent &&event)
40 {
41     CALL_DEBUG_ENTER;
42     DistributedHardware::DistributedInput::BusinessEvent ev {
43         .pressedKeys = std::move(event.pressedKeys),
44         .keyCode = event.keyCode,
45         .keyAction = event.keyAction,
46     };
47     return DistributedInputKit::IsNeedFilterOut(networkId, ev);
48 }
49 
StartRemoteInput(const std::string & remoteNetworkId,const std::string & originNetworkId,const std::vector<std::string> & inputDeviceDhids,DInputCallback callback)50 int32_t DInputAdapter::StartRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId,
51     const std::vector<std::string> &inputDeviceDhids, DInputCallback callback)
52 {
53     CALL_DEBUG_ENTER;
54     sptr<IStartStopDInputsCallback> cb = sptr<StartDInputCallbackSink>::MakeSptr(shared_from_this());
55     CHKPR(cb, ERROR_NULL_POINTER);
56     SaveCallback(CallbackType::StartDInputCallbackSink, callback);
57     return DistributedInputKit::StartRemoteInput(remoteNetworkId, originNetworkId, inputDeviceDhids, cb);
58 }
59 
StopRemoteInput(const std::string & remoteNetworkId,const std::string & originNetworkId,const std::vector<std::string> & inputDeviceDhids,DInputCallback callback)60 int32_t DInputAdapter::StopRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId,
61     const std::vector<std::string> &inputDeviceDhids, DInputCallback callback)
62 {
63     CALL_DEBUG_ENTER;
64     sptr<IStartStopDInputsCallback> cb = sptr<StopDInputCallbackSink>::MakeSptr(shared_from_this());
65     CHKPR(cb, ERROR_NULL_POINTER);
66     SaveCallback(CallbackType::StopDInputCallbackSink, callback);
67     return DistributedInputKit::StopRemoteInput(remoteNetworkId, originNetworkId, inputDeviceDhids, cb);
68 }
69 
StopRemoteInput(const std::string & originNetworkId,const std::vector<std::string> & inputDeviceDhids,DInputCallback callback)70 int32_t DInputAdapter::StopRemoteInput(const std::string &originNetworkId,
71     const std::vector<std::string> &inputDeviceDhids, DInputCallback callback)
72 {
73     CALL_DEBUG_ENTER;
74     sptr<IStartStopDInputsCallback> cb = sptr<StopDInputCallbackDHIds>::MakeSptr(shared_from_this());
75     CHKPR(cb, ERROR_NULL_POINTER);
76     SaveCallback(CallbackType::StopDInputCallbackDHIds, callback);
77     return DistributedInputKit::StopRemoteInput(originNetworkId, inputDeviceDhids, cb);
78 }
79 
PrepareRemoteInput(const std::string & remoteNetworkId,const std::string & originNetworkId,DInputCallback callback)80 int32_t DInputAdapter::PrepareRemoteInput(const std::string &remoteNetworkId,
81     const std::string &originNetworkId, DInputCallback callback)
82 {
83     CALL_DEBUG_ENTER;
84     sptr<IPrepareDInputCallback> cb = sptr<PrepareStartDInputCallbackSink>::MakeSptr(shared_from_this());
85     CHKPR(cb, ERROR_NULL_POINTER);
86     SaveCallback(CallbackType::PrepareStartDInputCallbackSink, callback);
87     return DistributedInputKit::PrepareRemoteInput(remoteNetworkId, originNetworkId, cb);
88 }
89 
UnPrepareRemoteInput(const std::string & remoteNetworkId,const std::string & originNetworkId,DInputCallback callback)90 int32_t DInputAdapter::UnPrepareRemoteInput(const std::string &remoteNetworkId,
91     const std::string &originNetworkId, DInputCallback callback)
92 {
93     CALL_DEBUG_ENTER;
94     sptr<IUnprepareDInputCallback> cb = sptr<UnPrepareStopDInputCallbackSink>::MakeSptr(shared_from_this());
95     CHKPR(cb, ERROR_NULL_POINTER);
96     SaveCallback(CallbackType::UnPrepareStopDInputCallbackSink, callback);
97     return DistributedInputKit::UnprepareRemoteInput(remoteNetworkId, originNetworkId, cb);
98 }
99 
PrepareRemoteInput(const std::string & networkId,DInputCallback callback)100 int32_t DInputAdapter::PrepareRemoteInput(const std::string &networkId, DInputCallback callback)
101 {
102     CALL_DEBUG_ENTER;
103     sptr<IPrepareDInputCallback> cb = sptr<PrepareDInputCallback>::MakeSptr(shared_from_this());
104     CHKPR(cb, ERROR_NULL_POINTER);
105     SaveCallback(CallbackType::PrepareDInputCallback, callback);
106     return DistributedInputKit::PrepareRemoteInput(networkId, cb);
107 }
108 
UnPrepareRemoteInput(const std::string & networkId,DInputCallback callback)109 int32_t DInputAdapter::UnPrepareRemoteInput(const std::string &networkId, DInputCallback callback)
110 {
111     CALL_DEBUG_ENTER;
112     sptr<IUnprepareDInputCallback> cb = sptr<UnprepareDInputCallback>::MakeSptr(shared_from_this());
113     CHKPR(cb, ERROR_NULL_POINTER);
114     SaveCallback(CallbackType::UnprepareDInputCallback, callback);
115     return DistributedInputKit::UnprepareRemoteInput(networkId, cb);
116 }
117 
RegisterSessionStateCb(std::function<void (uint32_t)> callback)118 int32_t DInputAdapter::RegisterSessionStateCb(std::function<void(uint32_t)> callback)
119 {
120     CALL_DEBUG_ENTER;
121     sptr<SessionStateCallback> cb = sptr<SessionStateCallback>::MakeSptr(callback);
122     CHKPR(callback, ERROR_NULL_POINTER);
123     return DistributedInputKit::RegisterSessionStateCb(cb);
124 }
125 
UnregisterSessionStateCb()126 int32_t DInputAdapter::UnregisterSessionStateCb()
127 {
128     CALL_DEBUG_ENTER;
129     return DistributedInputKit::UnregisterSessionStateCb();
130 }
131 
SaveCallback(CallbackType type,DInputCallback callback)132 void DInputAdapter::SaveCallback(CallbackType type, DInputCallback callback)
133 {
134     std::lock_guard<std::mutex> guard(adapterLock_);
135     CHKPV(callback);
136     callbacks_[type] = callback;
137     AddTimer(type);
138 }
139 
AddTimer(const CallbackType & type)140 void DInputAdapter::AddTimer(const CallbackType &type)
141 {
142     FI_HILOGD("AddTimer type:%{public}d", type);
143     CHKPV(env_);
144     int32_t timerId = env_->GetTimerManager().AddTimer(DEFAULT_DELAY_TIME, RETRY_TIME, [this, type]() {
145         if ((callbacks_.find(type) == callbacks_.end()) || (watchings_.find(type) == watchings_.end())) {
146             FI_HILOGE("Callback or watching is not exist");
147             return;
148         }
149         if (watchings_[type].times == 0) {
150             FI_HILOGI("It will be retry to call callback next time");
151             watchings_[type].times++;
152             return;
153         }
154         callbacks_[type](false);
155         callbacks_.erase(type);
156     });
157     if (timerId < 0) {
158         FI_HILOGE("Add timer failed, timeId:%{public}d", timerId);
159         return;
160     }
161     watchings_[type].timerId = timerId;
162     watchings_[type].times = 0;
163 }
164 
RemoveTimer(const CallbackType & type)165 void DInputAdapter::RemoveTimer(const CallbackType &type)
166 {
167     FI_HILOGD("Remove timer type:%{public}d", type);
168     if (watchings_.find(type) != watchings_.end()) {
169         CHKPV(env_);
170         env_->GetTimerManager().RemoveTimer(watchings_[type].timerId);
171         watchings_.erase(type);
172     }
173 }
174 
ProcessDInputCallback(CallbackType type,int32_t status)175 void DInputAdapter::ProcessDInputCallback(CallbackType type, int32_t status)
176 {
177     CALL_DEBUG_ENTER;
178     std::lock_guard<std::mutex> guard(adapterLock_);
179     RemoveTimer(type);
180     auto it = callbacks_.find(type);
181     if (it == callbacks_.end()) {
182         FI_HILOGI("Dinput callback not exist");
183         return;
184     }
185     it->second(status == RET_OK);
186     callbacks_.erase(it);
187 }
188 
StopDInputCallbackDHIds(std::shared_ptr<DInputAdapter> dinput)189 DInputAdapter::StopDInputCallbackDHIds::StopDInputCallbackDHIds(std::shared_ptr<DInputAdapter> dinput)
190     : dinput_(dinput)
191 {}
192 
OnResultDhids(const std::string & devId,const int32_t & status)193 void DInputAdapter::StopDInputCallbackDHIds::OnResultDhids(const std::string &devId, const int32_t &status)
194 {
195     std::shared_ptr<DInputAdapter> dinput = dinput_.lock();
196     CHKPV(dinput);
197     dinput->ProcessDInputCallback(CallbackType::StopDInputCallbackDHIds, status);
198 }
199 
StartDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput)200 DInputAdapter::StartDInputCallbackSink::StartDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput)
201     : dinput_(dinput)
202 {}
203 
OnResultDhids(const std::string & devId,const int32_t & status)204 void DInputAdapter::StartDInputCallbackSink::OnResultDhids(const std::string &devId, const int32_t &status)
205 {
206     std::shared_ptr<DInputAdapter> dinput = dinput_.lock();
207     CHKPV(dinput);
208     dinput->ProcessDInputCallback(CallbackType::StartDInputCallbackSink, status);
209 }
210 
StopDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput)211 DInputAdapter::StopDInputCallbackSink::StopDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput)
212     : dinput_(dinput)
213 {}
214 
OnResultDhids(const std::string & devId,const int32_t & status)215 void DInputAdapter::StopDInputCallbackSink::OnResultDhids(const std::string &devId, const int32_t &status)
216 {
217     std::shared_ptr<DInputAdapter> dinput = dinput_.lock();
218     CHKPV(dinput);
219     dinput->ProcessDInputCallback(CallbackType::StopDInputCallbackSink, status);
220 }
221 
PrepareDInputCallback(std::shared_ptr<DInputAdapter> dinput)222 DInputAdapter::PrepareDInputCallback::PrepareDInputCallback(std::shared_ptr<DInputAdapter> dinput)
223     : dinput_(dinput)
224 {}
225 
OnResult(const std::string & devId,const int32_t & status)226 void DInputAdapter::PrepareDInputCallback::OnResult(const std::string &devId, const int32_t &status)
227 {
228     std::shared_ptr<DInputAdapter> dinput = dinput_.lock();
229     CHKPV(dinput);
230     dinput->ProcessDInputCallback(CallbackType::PrepareDInputCallback, status);
231 }
232 
UnprepareDInputCallback(std::shared_ptr<DInputAdapter> dinput)233 DInputAdapter::UnprepareDInputCallback::UnprepareDInputCallback(std::shared_ptr<DInputAdapter> dinput)
234     : dinput_(dinput)
235 {}
236 
OnResult(const std::string & devId,const int32_t & status)237 void DInputAdapter::UnprepareDInputCallback::OnResult(const std::string &devId, const int32_t &status)
238 {
239     std::shared_ptr<DInputAdapter> dinput = dinput_.lock();
240     CHKPV(dinput);
241     dinput->ProcessDInputCallback(CallbackType::UnprepareDInputCallback, status);
242 }
243 
PrepareStartDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput)244 DInputAdapter::PrepareStartDInputCallbackSink::PrepareStartDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput)
245     : dinput_(dinput)
246 {}
247 
OnResult(const std::string & devId,const int32_t & status)248 void DInputAdapter::PrepareStartDInputCallbackSink::OnResult(const std::string &devId, const int32_t &status)
249 {
250     std::shared_ptr<DInputAdapter> dinput = dinput_.lock();
251     CHKPV(dinput);
252     dinput->ProcessDInputCallback(CallbackType::PrepareStartDInputCallbackSink, status);
253 }
254 
UnPrepareStopDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput)255 DInputAdapter::UnPrepareStopDInputCallbackSink::UnPrepareStopDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput)
256     : dinput_(dinput)
257 {}
258 
OnResult(const std::string & devId,const int32_t & status)259 void DInputAdapter::UnPrepareStopDInputCallbackSink::OnResult(const std::string &devId, const int32_t &status)
260 {
261     std::shared_ptr<DInputAdapter> dinput = dinput_.lock();
262     CHKPV(dinput);
263     dinput->ProcessDInputCallback(CallbackType::UnPrepareStopDInputCallbackSink, status);
264 }
265 
SessionStateCallback(std::function<void (uint32_t)> callback)266 DInputAdapter::SessionStateCallback::SessionStateCallback(std::function<void(uint32_t)> callback)
267     : callback_(callback) {}
268 
OnResult(const std::string & devId,const uint32_t status)269 void DInputAdapter::SessionStateCallback::OnResult(const std::string &devId, const uint32_t status)
270 {
271     CHKPV(callback_);
272     callback_(status);
273 }
274 } // namespace DeviceStatus
275 } // namespace Msdp
276 } // namespace OHOS
277