• 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 "distributed_input_adapter.h"
17 
18 #include <algorithm>
19 #include <map>
20 #include <mutex>
21 
22 #include "coordination_event_manager.h"
23 #include "devicestatus_define.h"
24 
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 using namespace DistributedHardware::DistributedInput;
29 namespace {
30 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DistributedInputAdapter" };
31 constexpr int32_t DEFAULT_DELAY_TIME { 4000 };
32 constexpr int32_t RETRY_TIME { 2 };
33 } // namespace
34 DistributedInputAdapter::DistributedInputAdapter() = default;
35 
~DistributedInputAdapter()36 DistributedInputAdapter::~DistributedInputAdapter()
37 {
38     CALL_INFO_TRACE;
39     std::lock_guard<std::mutex> guard(adapterLock_);
40     callbackMap_.clear();
41 }
42 
IsNeedFilterOut(const std::string & deviceId,const BusinessEvent & event)43 bool DistributedInputAdapter::IsNeedFilterOut(const std::string &deviceId, const BusinessEvent &event)
44 {
45     CALL_INFO_TRACE;
46     return DistributedInputKit::IsNeedFilterOut(deviceId, event);
47 }
48 
StartRemoteInput(const std::string & remoteNetworkId,const std::string & originNetworkId,const std::vector<std::string> & inputDeviceDhids,DInputCallback callback)49 int32_t DistributedInputAdapter::StartRemoteInput(const std::string &remoteNetworkId,
50     const std::string &originNetworkId, const std::vector<std::string> &inputDeviceDhids, DInputCallback callback)
51 {
52     CALL_INFO_TRACE;
53     sptr<IStartStopDInputsCallback> cb = new (std::nothrow) StartDInputCallbackSink();
54     CHKPR(cb, ERROR_NULL_POINTER);
55     SaveCallback(CallbackType::StartDInputCallbackSink, callback);
56     return DistributedInputKit::StartRemoteInput(remoteNetworkId, originNetworkId, inputDeviceDhids, cb);
57 }
58 
StopRemoteInput(const std::string & originNetworkId,const std::vector<std::string> & inputDeviceDhids,DInputCallback callback)59 int32_t DistributedInputAdapter::StopRemoteInput(const std::string &originNetworkId,
60     const std::vector<std::string> &inputDeviceDhids, DInputCallback callback)
61 {
62     CALL_INFO_TRACE;
63     sptr<IStartStopDInputsCallback> cb = new (std::nothrow) StopDInputCallbackDHIds();
64     CHKPR(cb, ERROR_NULL_POINTER);
65     SaveCallback(CallbackType::StopDInputCallbackDHIds, callback);
66     return DistributedInputKit::StopRemoteInput(originNetworkId, inputDeviceDhids, cb);
67 }
68 
StopRemoteInput(const std::string & remoteNetworkId,const std::string & originNetworkId,const std::vector<std::string> & inputDeviceDhids,DInputCallback callback)69 int32_t DistributedInputAdapter::StopRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId,
70     const std::vector<std::string> &inputDeviceDhids, DInputCallback callback)
71 {
72     CALL_INFO_TRACE;
73     sptr<IStartStopDInputsCallback> cb = new (std::nothrow) StopDInputCallbackSink();
74     CHKPR(cb, ERROR_NULL_POINTER);
75     SaveCallback(CallbackType::StopDInputCallbackSink, callback);
76     return DistributedInputKit::StopRemoteInput(remoteNetworkId, originNetworkId, inputDeviceDhids, cb);
77 }
78 
PrepareRemoteInput(const std::string & remoteNetworkId,const std::string & originNetworkId,DInputCallback callback)79 int32_t DistributedInputAdapter::PrepareRemoteInput(const std::string &remoteNetworkId,
80                                                     const std::string &originNetworkId, DInputCallback callback)
81 {
82     CALL_INFO_TRACE;
83     sptr<IPrepareDInputCallback> cb = new (std::nothrow) PrepareStartDInputCallbackSink();
84     CHKPR(cb, ERROR_NULL_POINTER);
85     SaveCallback(CallbackType::PrepareStartDInputCallbackSink, callback);
86     return DistributedInputKit::PrepareRemoteInput(remoteNetworkId, originNetworkId, cb);
87 }
88 
UnPrepareRemoteInput(const std::string & remoteNetworkId,const std::string & originNetworkId,DInputCallback callback)89 int32_t DistributedInputAdapter::UnPrepareRemoteInput(const std::string &remoteNetworkId,
90                                                       const std::string &originNetworkId, DInputCallback callback)
91 {
92     CALL_INFO_TRACE;
93     sptr<IUnprepareDInputCallback> cb = new (std::nothrow) UnPrepareStopDInputCallbackSink();
94     CHKPR(cb, ERROR_NULL_POINTER);
95     SaveCallback(CallbackType::UnPrepareStopDInputCallbackSink, callback);
96     return DistributedInputKit::UnprepareRemoteInput(remoteNetworkId, originNetworkId, cb);
97 }
98 
PrepareRemoteInput(const std::string & deviceId,DInputCallback callback)99 int32_t DistributedInputAdapter::PrepareRemoteInput(const std::string &deviceId, DInputCallback callback)
100 {
101     CALL_INFO_TRACE;
102     sptr<IPrepareDInputCallback> cb = new (std::nothrow) PrepareStartDInputCallback();
103     CHKPR(cb, ERROR_NULL_POINTER);
104     SaveCallback(CallbackType::PrepareStartDInputCallback, callback);
105     return DistributedInputKit::PrepareRemoteInput(deviceId, cb);
106 }
107 
UnPrepareRemoteInput(const std::string & deviceId,DInputCallback callback)108 int32_t DistributedInputAdapter::UnPrepareRemoteInput(const std::string &deviceId, DInputCallback callback)
109 {
110     CALL_INFO_TRACE;
111     sptr<IUnprepareDInputCallback> cb = new (std::nothrow) UnPrepareStopDInputCallback();
112     CHKPR(cb, ERROR_NULL_POINTER);
113     SaveCallback(CallbackType::UnPrepareStopDInputCallback, callback);
114     return DistributedInputKit::UnprepareRemoteInput(deviceId, cb);
115 }
116 
SaveCallback(CallbackType type,DInputCallback callback)117 void DistributedInputAdapter::SaveCallback(CallbackType type, DInputCallback callback)
118 {
119     std::lock_guard<std::mutex> guard(adapterLock_);
120     CHKPV(callback);
121     callbackMap_[type] = callback;
122     AddTimer(type);
123 }
124 
AddTimer(const CallbackType & type)125 void DistributedInputAdapter::AddTimer(const CallbackType &type)
126 {
127     FI_HILOGD("AddTimer type:%{public}d", type);
128     auto context = COOR_EVENT_MGR->GetIContext();
129     CHKPV(context);
130     int32_t timerId = context->GetTimerManager().AddTimer(DEFAULT_DELAY_TIME, RETRY_TIME, [this, type]() {
131         if ((callbackMap_.find(type) == callbackMap_.end()) || (watchingMap_.find(type) == watchingMap_.end())) {
132             FI_HILOGE("Callback or watching is not exist");
133             return;
134         }
135         if (watchingMap_[type].times == 0) {
136             FI_HILOGI("It will be retry to call callback next time");
137             watchingMap_[type].times++;
138             return;
139         }
140         callbackMap_[type](false);
141         callbackMap_.erase(type);
142     });
143     if (timerId < 0) {
144         FI_HILOGE("Add timer failed timeId:%{public}d", timerId);
145         return;
146     }
147     watchingMap_[type].timerId = timerId;
148     watchingMap_[type].times = 0;
149 }
150 
RemoveTimer(const CallbackType & type)151 void DistributedInputAdapter::RemoveTimer(const CallbackType &type)
152 {
153     FI_HILOGD("Remove timer type:%{public}d", type);
154     if (watchingMap_.find(type) != watchingMap_.end()) {
155         auto context = COOR_EVENT_MGR->GetIContext();
156         CHKPV(context);
157         context->GetTimerManager().RemoveTimer(watchingMap_[type].timerId);
158         watchingMap_.erase(type);
159     }
160 }
161 
ProcessDInputCallback(CallbackType type,int32_t status)162 void DistributedInputAdapter::ProcessDInputCallback(CallbackType type, int32_t status)
163 {
164     CALL_INFO_TRACE;
165     std::lock_guard<std::mutex> guard(adapterLock_);
166     RemoveTimer(type);
167     auto it = callbackMap_.find(type);
168     if (it == callbackMap_.end()) {
169         FI_HILOGI("Dinput callback not exist");
170         return;
171     }
172     it->second(status == RET_OK);
173     callbackMap_.erase(it);
174 }
175 
OnResult(const std::string & devId,const uint32_t & inputTypes,const int32_t & status)176 void DistributedInputAdapter::StartDInputCallback::OnResult(const std::string &devId, const uint32_t &inputTypes,
177                                                             const int32_t &status)
178 {
179     D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StartDInputCallback, status);
180 }
181 
OnResult(const std::string & devId,const uint32_t & inputTypes,const int32_t & status)182 void DistributedInputAdapter::StopDInputCallback::OnResult(const std::string &devId, const uint32_t &inputTypes,
183                                                            const int32_t &status)
184 {
185     D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StopDInputCallback, status);
186 }
187 
OnResultDhids(const std::string & devId,const int32_t & status)188 void DistributedInputAdapter::StartDInputCallbackDHIds::OnResultDhids(const std::string &devId, const int32_t &status)
189 {
190     D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StartDInputCallbackDHIds, status);
191 }
192 
OnResultDhids(const std::string & devId,const int32_t & status)193 void DistributedInputAdapter::StopDInputCallbackDHIds::OnResultDhids(const std::string &devId, const int32_t &status)
194 {
195     D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StopDInputCallbackDHIds, status);
196 }
197 
OnResultDhids(const std::string & devId,const int32_t & status)198 void DistributedInputAdapter::StartDInputCallbackSink::OnResultDhids(const std::string &devId, const int32_t &status)
199 {
200     D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StartDInputCallbackSink, status);
201 }
202 
OnResultDhids(const std::string & devId,const int32_t & status)203 void DistributedInputAdapter::StopDInputCallbackSink::OnResultDhids(const std::string &devId, const int32_t &status)
204 {
205     D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StopDInputCallbackSink, status);
206 }
207 
OnResult(const std::string & devId,const int32_t & status)208 void DistributedInputAdapter::PrepareStartDInputCallback::OnResult(const std::string &devId, const int32_t &status)
209 {
210     D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::PrepareStartDInputCallback, status);
211 }
212 
OnResult(const std::string & devId,const int32_t & status)213 void DistributedInputAdapter::UnPrepareStopDInputCallback::OnResult(const std::string &devId, const int32_t &status)
214 {
215     D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::UnPrepareStopDInputCallback, status);
216 }
217 
OnResult(const std::string & devId,const int32_t & status)218 void DistributedInputAdapter::PrepareStartDInputCallbackSink::OnResult(const std::string &devId, const int32_t &status)
219 {
220     D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::PrepareStartDInputCallbackSink, status);
221 }
222 
OnResult(const std::string & devId,const int32_t & status)223 void DistributedInputAdapter::UnPrepareStopDInputCallbackSink::OnResult(const std::string &devId, const int32_t &status)
224 {
225     D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::UnPrepareStopDInputCallbackSink, status);
226 }
227 } // namespace DeviceStatus
228 } // namespace Msdp
229 } // namespace OHOS
230