• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_source_handler.h"
17 
18 #include "dinput_errcode.h"
19 #include "dinput_log.h"
20 #include "hisysevent_util.h"
21 #include "i_distributed_source_input.h"
22 #include "load_d_input_source_callback.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 namespace DistributedInput {
27 IMPLEMENT_SINGLE_INSTANCE(DistributedInputSourceHandler);
28 
DistributedInputSourceHandler()29 DistributedInputSourceHandler::DistributedInputSourceHandler()
30 {
31     DHLOGI("DInputSourceHandler construct.");
32     std::lock_guard<std::mutex> lock(proxyMutex_);
33     if (sourceSvrRecipient_ == nullptr) {
34         sourceSvrRecipient_ = new (std::nothrow) DInputSourceSvrRecipient();
35     }
36 
37     if (dInputSourceCallback_ == nullptr) {
38         dInputSourceCallback_ = new (std::nothrow) LoadDInputSourceCallback();
39     }
40 }
41 
InitSource(const std::string & params)42 int32_t DistributedInputSourceHandler::InitSource(const std::string &params)
43 {
44     DHLOGI("DistributedInputSourceHandler InitSource begin");
45     std::unique_lock<std::mutex> lock(proxyMutex_);
46     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
47     if (!samgr) {
48         DHLOGE("Failed to get system ability mgr.");
49         return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
50     }
51     sptr<LoadDInputSourceCallback> loadCallback(new LoadDInputSourceCallback(params));
52     HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_INIT,
53         "dinput init source sa start.");
54     int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, loadCallback);
55     if (ret != ERR_OK) {
56         DHLOGE("Failed to Load systemAbility, systemAbilityId:%d, ret code:%d",
57             DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, ret);
58         return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
59     }
60 
61     auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(INPUT_LOAD_SA_TIMEOUT_MS),
62         [this]() { return (DInputSAManager::GetInstance().HasDInputSourceProxy()); });
63     if (!waitStatus) {
64         DHLOGE("dinput load source sa timeout.");
65         return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
66     }
67 
68     DHLOGI("DistributedInputSourceHandler InitSource end");
69     return DH_SUCCESS;
70 }
71 
FinishStartSA(const std::string & params,const sptr<IRemoteObject> & remoteObject)72 void DistributedInputSourceHandler::FinishStartSA(const std::string &params, const sptr<IRemoteObject> &remoteObject)
73 {
74     DHLOGI("DInputSourceHandle FinishStartSA");
75     std::lock_guard<std::mutex> lock(proxyMutex_);
76     if (sourceSvrRecipient_ == nullptr) {
77         DHLOGE("sourceSvrRecipient is nullptr.");
78         return;
79     }
80     remoteObject->AddDeathRecipient(sourceSvrRecipient_);
81     dInputSourceProxy_ = iface_cast<IDistributedSourceInput>(remoteObject);
82     DInputSAManager::GetInstance().SetDInputSourceProxy(remoteObject);
83     if ((dInputSourceProxy_ == nullptr) || (dInputSourceProxy_->AsObject() == nullptr)) {
84         DHLOGE("Faild to get input source proxy.");
85         return;
86     }
87     DistributedInputClient::GetInstance().InitSource();
88     proxyConVar_.notify_all();
89 }
90 
ReleaseSource()91 int32_t DistributedInputSourceHandler::ReleaseSource()
92 {
93     return DistributedInputClient::GetInstance().ReleaseSource();
94 }
95 
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const EnableParam & param,std::shared_ptr<RegisterCallback> callback)96 int32_t DistributedInputSourceHandler::RegisterDistributedHardware(const std::string &devId,
97     const std::string &dhId, const EnableParam &param, std::shared_ptr<RegisterCallback> callback)
98 {
99     return DistributedInputClient::GetInstance().RegisterDistributedHardware(devId, dhId, param.sinkAttrs, callback);
100 }
101 
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,std::shared_ptr<UnregisterCallback> callback)102 int32_t DistributedInputSourceHandler::UnregisterDistributedHardware(const std::string &devId,
103     const std::string &dhId, std::shared_ptr<UnregisterCallback> callback)
104 {
105     return DistributedInputClient::GetInstance().UnregisterDistributedHardware(devId, dhId, callback);
106 }
107 
ConfigDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & key,const std::string & value)108 int32_t DistributedInputSourceHandler::ConfigDistributedHardware(const std::string &devId,
109     const std::string &dhId, const std::string &key, const std::string &value)
110 {
111     return DH_SUCCESS;
112 }
113 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const OHOS::sptr<IRemoteObject> & remoteObject)114 void DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
115     const OHOS::sptr<IRemoteObject> &remoteObject)
116 {
117     currSystemAbilityId = systemAbilityId;
118     currRemoteObject = remoteObject;
119     DHLOGI("DistributedInputSourceHandler OnLoadSystemAbilitySuccess. systemAbilityId=%d", systemAbilityId);
120 }
121 
OnLoadSystemAbilityFail(int32_t systemAbilityId)122 void DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilityFail(int32_t systemAbilityId)
123 {
124     currSystemAbilityId = systemAbilityId;
125     DHLOGE("DistributedInputSourceHandler OnLoadSystemAbilityFail. systemAbilityId=%d", systemAbilityId);
126 }
127 
OnRemoteDied(const wptr<IRemoteObject> & remote)128 void DistributedInputSourceHandler::DInputSourceSvrRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
129 {
130     if (remote == nullptr) {
131         DHLOGE("OnRemoteDied remote is nullptr.");
132         return;
133     }
134     DHLOGI("DInputSourceSvrRecipient OnRemoteDied.");
135     DistributedInputSourceHandler::GetInstance().OnRemoteSourceSvrDied(remote);
136 }
137 
OnRemoteSourceSvrDied(const wptr<IRemoteObject> & remote)138 void DistributedInputSourceHandler::OnRemoteSourceSvrDied(const wptr<IRemoteObject> &remote)
139 {
140     DHLOGI("OnRemoteSourceSvrDied.");
141     std::lock_guard<std::mutex> lock(proxyMutex_);
142     if (dInputSourceProxy_ == nullptr) {
143         DHLOGE("dInputSourceProxy is nullptr.");
144         return;
145     }
146     if (dInputSourceProxy_->AsObject() == nullptr) {
147         DHLOGE("AsObject is nullptr.");
148         return;
149     }
150     sptr<IRemoteObject> remoteObject = remote.promote();
151     if (remoteObject == nullptr) {
152         DHLOGE("OnRemoteDied remote promoted failed");
153         return;
154     }
155 
156     if (dInputSourceProxy_->AsObject() != remoteObject) {
157         DHLOGE("OnRemoteSourceSvrDied not found remote object.");
158         return;
159     }
160     dInputSourceProxy_->AsObject()->RemoveDeathRecipient(sourceSvrRecipient_);
161     dInputSourceProxy_ = nullptr;
162 }
163 
GetSourceHardwareHandler()164 IDistributedHardwareSource *GetSourceHardwareHandler()
165 {
166     return &DistributedInputSourceHandler::GetInstance();
167 }
168 } // namespace DistributedInput
169 } // namespace DistributedHardware
170 } // namespace OHOS
171