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 "accesstoken_kit.h"
17 #include "distributed_camera_source_stub.h"
18
19 #include "dcamera_ipc_interface_code.h"
20 #include "dcamera_radar.h"
21 #include "dcamera_source_callback_proxy.h"
22 #include "distributed_camera_errno.h"
23 #include "distributed_hardware_log.h"
24 #include "ipc_skeleton.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
DistributedCameraSourceStub()28 DistributedCameraSourceStub::DistributedCameraSourceStub() : IRemoteStub(true)
29 {
30 memberFuncMap_[static_cast<uint32_t>(IDCameraSourceInterfaceCode::INIT_SOURCE)] =
31 &DistributedCameraSourceStub::InitSourceInner;
32 memberFuncMap_[static_cast<uint32_t>(IDCameraSourceInterfaceCode::RELEASE_SOURCE)] =
33 &DistributedCameraSourceStub::ReleaseSourceInner;
34 memberFuncMap_[static_cast<uint32_t>(IDCameraSourceInterfaceCode::REGISTER_DISTRIBUTED_HARDWARE)] =
35 &DistributedCameraSourceStub::RegisterDistributedHardwareInner;
36 memberFuncMap_[static_cast<uint32_t>(IDCameraSourceInterfaceCode::UNREGISTER_DISTRIBUTED_HARDWARE)] =
37 &DistributedCameraSourceStub::UnregisterDistributedHardwareInner;
38 memberFuncMap_[static_cast<uint32_t>(IDCameraSourceInterfaceCode::CAMERA_NOTIFY)] =
39 &DistributedCameraSourceStub::DCameraNotifyInner;
40 }
41
~DistributedCameraSourceStub()42 DistributedCameraSourceStub::~DistributedCameraSourceStub()
43 {}
44
HasEnableDHPermission()45 bool DistributedCameraSourceStub::HasEnableDHPermission()
46 {
47 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
48 const std::string permissionName = "ohos.permission.ENABLE_DISTRIBUTED_HARDWARE";
49 int32_t result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken,
50 permissionName);
51 return (result == Security::AccessToken::PERMISSION_GRANTED);
52 }
53
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)54 int32_t DistributedCameraSourceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
55 MessageOption &option)
56 {
57 DHLOGD("OnRemoteRequest code: %{public}u", code);
58 std::u16string desc = DistributedCameraSourceStub::GetDescriptor();
59 std::u16string remoteDesc = data.ReadInterfaceToken();
60 if (desc != remoteDesc) {
61 DHLOGE("remoteDesc is invalid!");
62 return ERR_INVALID_DATA;
63 }
64
65 switch (code) {
66 case static_cast<uint32_t>(IDCameraSourceInterfaceCode::INIT_SOURCE):
67 return InitSourceInner(data, reply);
68 case static_cast<uint32_t>(IDCameraSourceInterfaceCode::RELEASE_SOURCE):
69 return ReleaseSourceInner(data, reply);
70 case static_cast<uint32_t>(IDCameraSourceInterfaceCode::REGISTER_DISTRIBUTED_HARDWARE):
71 return RegisterDistributedHardwareInner(data, reply);
72 case static_cast<uint32_t>(IDCameraSourceInterfaceCode::UNREGISTER_DISTRIBUTED_HARDWARE):
73 return UnregisterDistributedHardwareInner(data, reply);
74 case static_cast<uint32_t>(IDCameraSourceInterfaceCode::CAMERA_NOTIFY):
75 return DCameraNotifyInner(data, reply);
76 default:
77 DHLOGE("Invalid OnRemoteRequest code=%{public}d", code);
78 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
79 }
80 return DCAMERA_NOT_FOUND;
81 }
82
InitSourceInner(MessageParcel & data,MessageParcel & reply)83 int32_t DistributedCameraSourceStub::InitSourceInner(MessageParcel &data, MessageParcel &reply)
84 {
85 DHLOGD("enter");
86 int32_t ret = DCAMERA_OK;
87 do {
88 if (!HasEnableDHPermission()) {
89 DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
90 ret = DCAMERA_BAD_VALUE;
91 break;
92 }
93 std::string params = data.ReadString();
94 if (params.empty() || params.size() > PARAM_MAX_SIZE) {
95 DHLOGE("input params is invalid");
96 ret = DCAMERA_BAD_VALUE;
97 break;
98 }
99 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
100 if (remoteObj == nullptr) {
101 DHLOGE("read object failed");
102 ret = DCAMERA_BAD_VALUE;
103 break;
104 }
105
106 sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObj));
107 if (callbackProxy == nullptr) {
108 DHLOGE("get proxy failed");
109 ret = DCAMERA_BAD_VALUE;
110 break;
111 }
112
113 ret = InitSource(params, callbackProxy);
114 } while (0);
115 reply.WriteInt32(ret);
116 return DCAMERA_OK;
117 }
118
ReleaseSourceInner(MessageParcel & data,MessageParcel & reply)119 int32_t DistributedCameraSourceStub::ReleaseSourceInner(MessageParcel &data, MessageParcel &reply)
120 {
121 DHLOGD("enter");
122 (void)data;
123 int32_t ret = DCAMERA_OK;
124 do {
125 if (!HasEnableDHPermission()) {
126 DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
127 ret = DCAMERA_BAD_VALUE;
128 break;
129 }
130 ret = ReleaseSource();
131 } while (0);
132 reply.WriteInt32(ret);
133 return DCAMERA_OK;
134 }
135
RegisterDistributedHardwareInner(MessageParcel & data,MessageParcel & reply)136 int32_t DistributedCameraSourceStub::RegisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply)
137 {
138 DHLOGD("enter");
139 int32_t ret = DCAMERA_OK;
140 do {
141 if (!HasEnableDHPermission()) {
142 DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
143 ret = DCAMERA_BAD_VALUE;
144 break;
145 }
146 std::string devId = data.ReadString();
147 std::string dhId = data.ReadString();
148 std::string reqId = data.ReadString();
149 EnableParam params;
150 params.sinkVersion = data.ReadString();
151 params.sinkAttrs = data.ReadString();
152 if (!CheckRegParams(devId, dhId, reqId, params)) {
153 DHLOGE("input is invalid");
154 ret = DCAMERA_BAD_VALUE;
155 break;
156 }
157 ret = RegisterDistributedHardware(devId, dhId, reqId, params);
158 DHLOGI("DistributedCameraSourceStub RegisterDistributedHardware %{public}d", ret);
159 } while (0);
160 reply.WriteInt32(ret);
161 return DCAMERA_OK;
162 }
163
CheckRegParams(const std::string & devId,const std::string & dhId,const std::string & reqId,const EnableParam & param)164 bool DistributedCameraSourceStub::CheckRegParams(const std::string& devId, const std::string& dhId,
165 const std::string& reqId, const EnableParam& param)
166 {
167 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
168 DHLOGE("devId or dhId is invalid");
169 return false;
170 }
171
172 if (reqId.empty() || reqId.size() > DID_MAX_SIZE) {
173 DHLOGE("reqId is invalid");
174 return false;
175 }
176
177 if (param.sinkVersion.empty() || param.sinkVersion.size() > PARAM_MAX_SIZE ||
178 param.sinkAttrs.empty() || param.sinkAttrs.size() > PARAM_MAX_SIZE) {
179 DHLOGE("param is invalid");
180 return false;
181 }
182 return true;
183 }
184
UnregisterDistributedHardwareInner(MessageParcel & data,MessageParcel & reply)185 int32_t DistributedCameraSourceStub::UnregisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply)
186 {
187 DHLOGD("enter");
188 int32_t ret = DCAMERA_OK;
189 do {
190 if (!HasEnableDHPermission()) {
191 DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
192 ret = DCAMERA_BAD_VALUE;
193 break;
194 }
195 std::string devId = data.ReadString();
196 std::string dhId = data.ReadString();
197 std::string reqId = data.ReadString();
198 if (!CheckUnregParams(devId, dhId, reqId)) {
199 DHLOGE("input is invalid");
200 ret = DCAMERA_BAD_VALUE;
201 break;
202 }
203 ret = UnregisterDistributedHardware(devId, dhId, reqId);
204 } while (0);
205 reply.WriteInt32(ret);
206 return DCAMERA_OK;
207 }
208
CheckUnregParams(const std::string & devId,const std::string & dhId,const std::string & reqId)209 bool DistributedCameraSourceStub::CheckUnregParams(const std::string& devId, const std::string& dhId,
210 const std::string& reqId)
211 {
212 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
213 DHLOGE("devId or dhId is invalid");
214 return false;
215 }
216
217 if (reqId.empty() || reqId.size() > DID_MAX_SIZE) {
218 DHLOGE("reqId is invalid");
219 return false;
220 }
221 return true;
222 }
223
DCameraNotifyInner(MessageParcel & data,MessageParcel & reply)224 int32_t DistributedCameraSourceStub::DCameraNotifyInner(MessageParcel &data, MessageParcel &reply)
225 {
226 int32_t ret = DCAMERA_OK;
227 do {
228 std::string devId = data.ReadString();
229 std::string dhId = data.ReadString();
230 std::string events = data.ReadString();
231 if (!CheckNotifyParams(devId, dhId, events)) {
232 DHLOGE("input is invalid");
233 ret = DCAMERA_BAD_VALUE;
234 break;
235 }
236 ret = DCameraNotify(devId, dhId, events);
237 } while (0);
238 reply.WriteInt32(ret);
239 return DCAMERA_OK;
240 }
241
CheckNotifyParams(const std::string & devId,const std::string & dhId,std::string & events)242 bool DistributedCameraSourceStub::CheckNotifyParams(const std::string& devId, const std::string& dhId,
243 std::string& events)
244 {
245 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
246 DHLOGE("devId or dhId is invalid");
247 return false;
248 }
249
250 if (events.empty() || events.size() > PARAM_MAX_SIZE) {
251 DHLOGE("events is invalid");
252 return false;
253 }
254 return true;
255 }
256 } // namespace DistributedHardware
257 } // namespace OHOS
258