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