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