1 /*
2 * Copyright (c) 2021 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 "dcamera_source_callback_proxy.h"
17
18 #include "parcel.h"
19
20 #include "anonymous_string.h"
21 #include "distributed_camera_errno.h"
22 #include "distributed_hardware_log.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
OnNotifyRegResult(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,std::string & data)26 int32_t DCameraSourceCallbackProxy::OnNotifyRegResult(const std::string& devId, const std::string& dhId,
27 const std::string& reqId, int32_t status, std::string& data)
28 {
29 if (!CheckParams(devId, dhId, reqId, data)) {
30 DHLOGE("DCameraSourceCallbackProxy OnNotifyRegResult input is invalid");
31 return DCAMERA_BAD_VALUE;
32 }
33 sptr<IRemoteObject> remote = Remote();
34 if (remote == nullptr) {
35 DHLOGE("DCameraSourceCallbackProxy remote service null");
36 return DCAMERA_BAD_VALUE;
37 }
38 MessageParcel req;
39 MessageParcel reply;
40 MessageOption option;
41 if (!req.WriteInterfaceToken(DCameraSourceCallbackProxy::GetDescriptor())) {
42 DHLOGE("DCameraSourceCallbackProxy OnNotifyRegResult write token failed");
43 return DCAMERA_BAD_VALUE;
44 }
45
46 if (!req.WriteString(devId) || !req.WriteString(dhId) || !req.WriteString(reqId) ||
47 !req.WriteInt32(status) || !req.WriteString(data)) {
48 DHLOGE("DistributedCameraSourceProxy InitSource write params failed");
49 return DCAMERA_BAD_VALUE;
50 }
51 remote->SendRequest(NOTIFY_REG_RESULT, req, reply, option);
52 int32_t result = reply.ReadInt32();
53 return result;
54 }
55
OnNotifyUnregResult(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,std::string & data)56 int32_t DCameraSourceCallbackProxy::OnNotifyUnregResult(const std::string& devId, const std::string& dhId,
57 const std::string& reqId, int32_t status, std::string& data)
58 {
59 if (!CheckParams(devId, dhId, reqId, data)) {
60 DHLOGE("DCameraSourceCallbackProxy OnNotifyUnregResult input is invalid");
61 return DCAMERA_BAD_VALUE;
62 }
63 sptr<IRemoteObject> remote = Remote();
64 if (remote == nullptr) {
65 DHLOGE("DCameraSourceCallbackProxy remote service null");
66 return DCAMERA_BAD_VALUE;
67 }
68 MessageParcel req;
69 MessageParcel reply;
70 MessageOption option;
71 if (!req.WriteInterfaceToken(DCameraSourceCallbackProxy::GetDescriptor())) {
72 DHLOGE("DCameraSourceCallbackProxy OnNotifyRegResult write token failed");
73 return DCAMERA_BAD_VALUE;
74 }
75
76 if (!req.WriteString(devId) || !req.WriteString(dhId) || !req.WriteString(reqId) ||
77 !req.WriteInt32(status) || !req.WriteString(data)) {
78 DHLOGE("DistributedCameraSourceProxy InitSource write params failed");
79 return DCAMERA_BAD_VALUE;
80 }
81 remote->SendRequest(NOTIFY_UNREG_RESULT, req, reply, option);
82 int32_t result = reply.ReadInt32();
83 return result;
84 }
85
CheckParams(const std::string & devId,const std::string & dhId,const std::string & reqId,std::string & data)86 bool DCameraSourceCallbackProxy::CheckParams(const std::string& devId, const std::string& dhId,
87 const std::string& reqId, std::string& data)
88 {
89 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
90 DHLOGE("DCameraSourceCallbackProxy CheckParams devId or dhId is invalid");
91 return false;
92 }
93
94 if (reqId.empty() || reqId.size() > DID_MAX_SIZE || data.size() > PARAM_MAX_SIZE) {
95 DHLOGE("DCameraSourceCallbackProxy CheckParams reqId or data is invalid");
96 return false;
97 }
98 return true;
99 }
100 } // namespace DistributedHardware
101 } // namespace OHOS
102