• 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 "dscreen_source_stub.h"
17 
18 #include "iservice_registry.h"
19 
20 #include "dscreen_constants.h"
21 #include "dscreen_errcode.h"
22 #include "dscreen_ipc_interface_code.h"
23 #include "dscreen_log.h"
24 #include "dscreen_source_callback_proxy.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
DScreenSourceStub()28 DScreenSourceStub::DScreenSourceStub()
29 {
30     memberFuncMap_[static_cast<uint32_t>(IDScreenSourceInterfaceCode::INIT_SOURCE)] =
31         &DScreenSourceStub::InitSourceInner;
32     memberFuncMap_[static_cast<uint32_t>(IDScreenSourceInterfaceCode::RELEASE_SOURCE)] =
33         &DScreenSourceStub::ReleaseSourceInner;
34     memberFuncMap_[static_cast<uint32_t>(IDScreenSourceInterfaceCode::REGISTER_DISTRIBUTED_HARDWARE)] =
35         &DScreenSourceStub::RegisterDistributedHardwareInner;
36     memberFuncMap_[static_cast<uint32_t>(IDScreenSourceInterfaceCode::UNREGISTER_DISTRIBUTED_HARDWARE)] =
37         &DScreenSourceStub::UnregisterDistributedHardwareInner;
38     memberFuncMap_[static_cast<uint32_t>(IDScreenSourceInterfaceCode::CONFIG_DISTRIBUTED_HARDWARE)] =
39         &DScreenSourceStub::ConfigDistributedHardwareInner;
40     memberFuncMap_[static_cast<uint32_t>(IDScreenSourceInterfaceCode::DSCREEN_NOTIFY)] =
41         &DScreenSourceStub::DScreenNotifyInner;
42 }
43 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int32_t DScreenSourceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
45     MessageOption &option)
46 {
47     std::u16string desc = DScreenSourceStub::GetDescriptor();
48     std::u16string remoteDesc = data.ReadInterfaceToken();
49     if (desc != remoteDesc) {
50         DHLOGE("DScreenSourceStub::OnRemoteRequest remoteDesc is invalid!");
51         return ERR_INVALID_DATA;
52     }
53 
54     std::map<int32_t, DScreenSourceFunc>::iterator iter = memberFuncMap_.find(code);
55     if (iter == memberFuncMap_.end()) {
56         DHLOGE("invalid request code.");
57         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
58     }
59     DScreenSourceFunc &func = iter->second;
60     return (this->*func)(data, reply, option);
61 }
62 
InitSourceInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)63 int32_t DScreenSourceStub::InitSourceInner(MessageParcel &data, MessageParcel &reply,
64     MessageOption &option)
65 {
66     (void)option;
67     std::string param = data.ReadString();
68     if (param.empty() || param.size() > PARAM_MAX_SIZE) {
69         DHLOGE("InitSourceInner error: invalid parameter");
70         return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
71     }
72     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
73     if (remoteObject == nullptr) {
74         DHLOGE("Read param failed.");
75         return ERR_DH_SCREEN_SA_READPARAM_FAILED;
76     }
77 
78     sptr<DScreenSourceCallbackProxy> dScreenSourceCallbackProxy(new DScreenSourceCallbackProxy(remoteObject));
79     if (dScreenSourceCallbackProxy == nullptr) {
80         DHLOGE("dScreenSourceCallbackProxy is nullptr.");
81         return ERR_DH_SCREEN_SA_READPARAM_FAILED;
82     }
83     int32_t ret = InitSource(param, dScreenSourceCallbackProxy);
84     reply.WriteInt32(ret);
85     return DH_SUCCESS;
86 }
87 
ReleaseSourceInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)88 int32_t DScreenSourceStub::ReleaseSourceInner(MessageParcel &data, MessageParcel &reply,
89     MessageOption &option)
90 {
91     (void)data;
92     (void)option;
93     int32_t ret = ReleaseSource();
94     reply.WriteInt32(ret);
95     return DH_SUCCESS;
96 }
97 
RegisterDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)98 int32_t DScreenSourceStub::RegisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
99     MessageOption &option)
100 {
101     (void)option;
102     std::string devId = data.ReadString();
103     std::string dhId = data.ReadString();
104     std::string version = data.ReadString();
105     std::string attrs = data.ReadString();
106     std::string reqId = data.ReadString();
107     if (!CheckRegParams(devId, dhId, version, attrs, reqId)) {
108         DHLOGE("RegisterDistributedHardwareInner error: invalid parameter");
109         return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
110     }
111     EnableParam enableParam;
112     enableParam.version = version;
113     enableParam.attrs = attrs;
114 
115     int32_t ret = RegisterDistributedHardware(devId, dhId, enableParam, reqId);
116     reply.WriteInt32(ret);
117     return DH_SUCCESS;
118 }
119 
UnregisterDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)120 int32_t DScreenSourceStub::UnregisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
121     MessageOption &option)
122 {
123     (void)option;
124     std::string devId = data.ReadString();
125     std::string dhId = data.ReadString();
126     std::string reqId = data.ReadString();
127     if (!CheckUnregParams(devId, dhId, reqId)) {
128         DHLOGE("UnregisterDistributedHardwareInner error: invalid parameter");
129         return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
130     }
131 
132     int32_t ret = UnregisterDistributedHardware(devId, dhId, reqId);
133     reply.WriteInt32(ret);
134     return DH_SUCCESS;
135 }
136 
ConfigDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)137 int32_t DScreenSourceStub::ConfigDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
138     MessageOption &option)
139 {
140     (void)option;
141     std::string devId = data.ReadString();
142     std::string dhId = data.ReadString();
143     std::string key = data.ReadString();
144     std::string value = data.ReadString();
145     if (!CheckConfigParams(devId, dhId, key, value)) {
146         DHLOGE("ConfigDistributedHardwareInner error: invalid parameter");
147         return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
148     }
149 
150     int32_t ret = ConfigDistributedHardware(devId, dhId, key, value);
151     reply.WriteInt32(ret);
152     return DH_SUCCESS;
153 }
154 
DScreenNotifyInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)155 int32_t DScreenSourceStub::DScreenNotifyInner(MessageParcel &data, MessageParcel &reply,
156     MessageOption &option)
157 {
158     (void)reply;
159     (void)option;
160     std::string devId = data.ReadString();
161     int32_t eventCode = data.ReadInt32();
162     std::string eventContent = data.ReadString();
163     if (devId.empty() || devId.size() > DID_MAX_SIZE || eventContent.empty() ||
164         eventContent.size() > PARAM_MAX_SIZE) {
165         DHLOGE("DScreenNotifyInner error: invalid parameter");
166         return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
167     }
168 
169     DScreenNotify(devId, eventCode, eventContent);
170     return DH_SUCCESS;
171 }
172 
CheckRegParams(const std::string & devId,const std::string & dhId,const std::string & version,const std::string & attrs,const std::string & reqId) const173 bool DScreenSourceStub::CheckRegParams(const std::string &devId, const std::string &dhId,
174     const std::string &version, const std::string &attrs, const std::string &reqId) const
175 {
176     if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
177         DHLOGE("DScreenSourceStub CheckRegParams devId or dhId is inlvalid.");
178         return false;
179     }
180     if (version.empty() || version.size() > PARAM_MAX_SIZE || attrs.empty() || attrs.size() > PARAM_MAX_SIZE) {
181         DHLOGE("DScreenSourceStub CheckRegParams version or attrs is inlvalid.");
182         return false;
183     }
184     if (reqId.empty() || reqId.size() > DID_MAX_SIZE) {
185         DHLOGE("DScreenSourceStub CheckRegParams reqId is inlvalid.");
186         return false;
187     }
188     return true;
189 }
190 
CheckUnregParams(const std::string & devId,const std::string & dhId,const std::string & reqId) const191 bool DScreenSourceStub::CheckUnregParams(const std::string &devId,
192     const std::string &dhId, const std::string &reqId) const
193 {
194     if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
195         DHLOGE("DScreenSourceStub CheckUnregParams devId or dhId is invalid.");
196         return false;
197     }
198     if (reqId.empty() || reqId.size() > DID_MAX_SIZE) {
199         DHLOGE("DScreenSourceStub CheckUnregParams reqId is invalid.");
200         return false;
201     }
202     return true;
203 }
204 
CheckConfigParams(const std::string & devId,const std::string & dhId,const std::string & key,const std::string & value) const205 bool DScreenSourceStub::CheckConfigParams(const std::string &devId, const std::string &dhId,
206     const std::string &key, const std::string &value) const
207 {
208     if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
209         DHLOGE("DScreenSourceStub CheckConfigParams devId or dhId is invalid.");
210         return false;
211     }
212     if (key.empty() || key.size() > PARAM_MAX_SIZE || value.empty() || value.size() > PARAM_MAX_SIZE) {
213         DHLOGE("DScreenSourceStub CheckConfigParams key or value is invalid.");
214         return false;
215     }
216     return true;
217 }
218 } // namespace DistributedHardware
219 } // namespace OHOS