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("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("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("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("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 
OnHardwareStateChanged(const std::string & devId,const std::string & dhId,int32_t status)86 int32_t DCameraSourceCallbackProxy::OnHardwareStateChanged(const std::string &devId,
87     const std::string &dhId, int32_t status)
88 {
89     if (!CheckParams(devId, dhId, status)) {
90         DHLOGE("input is invalid");
91         return DCAMERA_BAD_VALUE;
92     }
93     sptr<IRemoteObject> remote = Remote();
94     if (remote == nullptr) {
95         DHLOGE("DCameraSourceCallbackProxy remote service null");
96         return DCAMERA_BAD_VALUE;
97     }
98 
99     MessageParcel req;
100     MessageParcel reply;
101     MessageOption option;
102     if (!req.WriteInterfaceToken(DCameraSourceCallbackProxy::GetDescriptor())) {
103         DHLOGE("write token failed");
104         return DCAMERA_BAD_VALUE;
105     }
106     if (!req.WriteString(devId) || !req.WriteString(dhId) || !req.WriteInt32(status)) {
107         return DCAMERA_BAD_VALUE;
108     }
109     remote->SendRequest(NOTIFY_STATE_CHANGED, req, reply, option);
110     return reply.ReadInt32();
111 }
112 
OnDataSyncTrigger(const std::string & devId)113 int32_t DCameraSourceCallbackProxy::OnDataSyncTrigger(const std::string &devId)
114 {
115     if (devId.empty() || devId.size() > DID_MAX_SIZE) {
116         DHLOGE("devId is invalid");
117         return DCAMERA_BAD_VALUE;
118     }
119     sptr<IRemoteObject> remote = Remote();
120     if (remote == nullptr) {
121         DHLOGE("DCameraSourceCallbackProxy remote service null");
122         return DCAMERA_BAD_VALUE;
123     }
124 
125     MessageParcel req;
126     MessageParcel reply;
127     MessageOption option;
128     if (!req.WriteInterfaceToken(DCameraSourceCallbackProxy::GetDescriptor())) {
129         DHLOGE("write token failed");
130         return DCAMERA_BAD_VALUE;
131     }
132     if (!req.WriteString(devId)) {
133         return DCAMERA_BAD_VALUE;
134     }
135     remote->SendRequest(NOTIFY_DATASYNC_TRIGGER, req, reply, option);
136     return reply.ReadInt32();
137 }
138 
CheckParams(const std::string & devId,const std::string & dhId,int32_t status)139 bool DCameraSourceCallbackProxy::CheckParams(const std::string& devId, const std::string& dhId, int32_t status)
140 {
141     if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
142         DHLOGE("devId or dhId is invalid");
143         return false;
144     }
145     if (status < 0) {
146         DHLOGE("status in invalid.");
147         return false;
148     }
149     return true;
150 }
151 
CheckParams(const std::string & devId,const std::string & dhId,const std::string & reqId,std::string & data)152 bool DCameraSourceCallbackProxy::CheckParams(const std::string& devId, const std::string& dhId,
153     const std::string& reqId, std::string& data)
154 {
155     if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
156         DHLOGE("devId or dhId is invalid");
157         return false;
158     }
159 
160     if (reqId.empty() || reqId.size() > DID_MAX_SIZE || data.size() > PARAM_MAX_SIZE) {
161         DHLOGE("reqId or data is invalid");
162         return false;
163     }
164     return true;
165 }
166 } // namespace DistributedHardware
167 } // namespace OHOS
168