• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "distributed_camera_source_stub.h"
17 
18 #include "dcamera_source_callback_proxy.h"
19 #include "distributed_camera_errno.h"
20 #include "distributed_hardware_log.h"
21 
22 namespace OHOS {
23 namespace DistributedHardware {
DistributedCameraSourceStub()24 DistributedCameraSourceStub::DistributedCameraSourceStub()
25 {
26     memberFuncMap_[INIT_SOURCE] = &DistributedCameraSourceStub::InitSourceInner;
27     memberFuncMap_[RELEASE_SOURCE] = &DistributedCameraSourceStub::ReleaseSourceInner;
28     memberFuncMap_[REGISTER_DISTRIBUTED_HARDWARE] = &DistributedCameraSourceStub::RegisterDistributedHardwareInner;
29     memberFuncMap_[UNREGISTER_DISTRIBUTED_HARDWARE] = &DistributedCameraSourceStub::UnregisterDistributedHardwareInner;
30     memberFuncMap_[CAMERA_NOTIFY] = &DistributedCameraSourceStub::DCameraNotifyInner;
31 }
32 
~DistributedCameraSourceStub()33 DistributedCameraSourceStub::~DistributedCameraSourceStub()
34 {}
35 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int32_t DistributedCameraSourceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
37     MessageOption &option)
38 {
39     DHLOGD("DistributedCameraSourceStub OnRemoteRequest code: %u", code);
40     std::u16string desc = DistributedCameraSourceStub::GetDescriptor();
41     std::u16string remoteDesc = data.ReadInterfaceToken();
42     if (desc != remoteDesc) {
43         DHLOGE("DistributedCameraSourceStub::OnRemoteRequest remoteDesc is invalid!");
44         return ERR_INVALID_DATA;
45     }
46     auto itFunc = memberFuncMap_.find(code);
47     if (itFunc == memberFuncMap_.end()) {
48         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
49     }
50 
51     auto memberFunc = itFunc->second;
52     return (this->*memberFunc)(data, reply);
53 }
54 
InitSourceInner(MessageParcel & data,MessageParcel & reply)55 int32_t DistributedCameraSourceStub::InitSourceInner(MessageParcel &data, MessageParcel &reply)
56 {
57     DHLOGD("DistributedCameraSourceStub InitSourceInner");
58     int32_t ret = DCAMERA_OK;
59     do {
60         std::string params = data.ReadString();
61         if (params.empty() || params.size() > PARAM_MAX_SIZE) {
62             DHLOGE("DistributedCameraSourceStub InitSourceInner input params is invalid");
63             ret = DCAMERA_BAD_VALUE;
64             break;
65         }
66         sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
67         if (remoteObj == nullptr) {
68             DHLOGE("DistributedCameraSourceStub initSource read object failed");
69             ret = DCAMERA_BAD_VALUE;
70             break;
71         }
72 
73         sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObj));
74         if (callbackProxy == nullptr) {
75             DHLOGE("DistributedCameraSourceStub initSource get proxy failed");
76             ret = DCAMERA_BAD_VALUE;
77             break;
78         }
79 
80         ret = InitSource(params, callbackProxy);
81     } while (0);
82     reply.WriteInt32(ret);
83     return DCAMERA_OK;
84 }
85 
ReleaseSourceInner(MessageParcel & data,MessageParcel & reply)86 int32_t DistributedCameraSourceStub::ReleaseSourceInner(MessageParcel &data, MessageParcel &reply)
87 {
88     DHLOGD("DistributedCameraSourceStub ReleaseSourceInner");
89     (void)data;
90     int32_t ret = ReleaseSource();
91     reply.WriteInt32(ret);
92     return DCAMERA_OK;
93 }
94 
RegisterDistributedHardwareInner(MessageParcel & data,MessageParcel & reply)95 int32_t DistributedCameraSourceStub::RegisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply)
96 {
97     DHLOGD("DistributedCameraSourceStub RegisterDistributedHardwareInner");
98     int32_t ret = DCAMERA_OK;
99     do {
100         std::string devId = data.ReadString();
101         std::string dhId = data.ReadString();
102         std::string reqId = data.ReadString();
103         EnableParam params;
104         params.version = data.ReadString();
105         params.attrs = data.ReadString();
106         if (!CheckRegParams(devId, dhId, reqId, params)) {
107             DHLOGE("DistributedCameraSourceStub RegisterDistributedHardwareInner input is invalid");
108             ret = DCAMERA_BAD_VALUE;
109             break;
110         }
111         ret = RegisterDistributedHardware(devId, dhId, reqId, params);
112         DHLOGI("DistributedCameraSourceStub RegisterDistributedHardware %d", ret);
113     } while (0);
114     reply.WriteInt32(ret);
115     return DCAMERA_OK;
116 }
117 
CheckRegParams(const std::string & devId,const std::string & dhId,const std::string & reqId,const EnableParam & param)118 bool DistributedCameraSourceStub::CheckRegParams(const std::string& devId, const std::string& dhId,
119     const std::string& reqId, const EnableParam& param)
120 {
121     if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
122         DHLOGE("DistributedCameraSourceStub CheckRegParams devId or dhId is invalid");
123         return false;
124     }
125 
126     if (reqId.empty() || reqId.size() > DID_MAX_SIZE) {
127         DHLOGE("DistributedCameraSourceStub CheckRegParams reqId is invalid");
128         return false;
129     }
130 
131     if (param.version.empty() || param.version.size() > PARAM_MAX_SIZE ||
132         param.attrs.empty() || param.attrs.size() > PARAM_MAX_SIZE) {
133         DHLOGE("DistributedCameraSourceStub CheckRegParams param is invalid");
134         return false;
135     }
136     return true;
137 }
138 
UnregisterDistributedHardwareInner(MessageParcel & data,MessageParcel & reply)139 int32_t DistributedCameraSourceStub::UnregisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply)
140 {
141     DHLOGD("DistributedCameraSourceStub UnregisterDistributedHardwareInner");
142     int32_t ret = DCAMERA_OK;
143     do {
144         std::string devId = data.ReadString();
145         std::string dhId = data.ReadString();
146         std::string reqId = data.ReadString();
147         if (!CheckUnregParams(devId, dhId, reqId)) {
148             DHLOGE("DistributedCameraSourceService UnregisterDistributedHardware input is invalid");
149             ret = DCAMERA_BAD_VALUE;
150             break;
151         }
152         ret = UnregisterDistributedHardware(devId, dhId, reqId);
153     } while (0);
154     reply.WriteInt32(ret);
155     return DCAMERA_OK;
156 }
157 
CheckUnregParams(const std::string & devId,const std::string & dhId,const std::string & reqId)158 bool DistributedCameraSourceStub::CheckUnregParams(const std::string& devId, const std::string& dhId,
159     const std::string& reqId)
160 {
161     if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
162         DHLOGE("DistributedCameraSourceStub CheckUnregParams devId or dhId is invalid");
163         return false;
164     }
165 
166     if (reqId.empty() || reqId.size() > DID_MAX_SIZE) {
167         DHLOGE("DistributedCameraSourceStub CheckUnregParams reqId is invalid");
168         return false;
169     }
170     return true;
171 }
172 
DCameraNotifyInner(MessageParcel & data,MessageParcel & reply)173 int32_t DistributedCameraSourceStub::DCameraNotifyInner(MessageParcel &data, MessageParcel &reply)
174 {
175     int32_t ret = DCAMERA_OK;
176     do {
177         std::string devId = data.ReadString();
178         std::string dhId = data.ReadString();
179         std::string events = data.ReadString();
180         if (!CheckNotifyParams(devId, dhId, events)) {
181             DHLOGE("DistributedCameraSourceStub DCameraNotifyInner input is invalid");
182             ret = DCAMERA_BAD_VALUE;
183             break;
184         }
185         ret = DCameraNotify(devId, dhId, events);
186     } while (0);
187     reply.WriteInt32(ret);
188     return DCAMERA_OK;
189 }
190 
CheckNotifyParams(const std::string & devId,const std::string & dhId,std::string & events)191 bool DistributedCameraSourceStub::CheckNotifyParams(const std::string& devId, const std::string& dhId,
192     std::string& events)
193 {
194     if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
195         DHLOGE("DistributedCameraSourceStub CheckNotifyParams devId or dhId is invalid");
196         return false;
197     }
198 
199     if (events.empty() || events.size() > PARAM_MAX_SIZE) {
200         DHLOGE("DistributedCameraSourceStub CheckNotifyParams events is invalid");
201         return false;
202     }
203     return true;
204 }
205 } // namespace DistributedHardware
206 } // namespace OHOS
207