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 "camera_device_stub.h"
17 #include <hdf_log.h>
18 #include <hdf_base.h>
19 #include <hdf_sbuf_ipc.h>
20 #include "metadata_utils.h"
21 #include "istream_operator.h"
22 #include "istream_operator_callback.h"
23 #include "cmd_common.h"
24
25 namespace OHOS::Camera {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)26 int32_t CameraDeviceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
27 MessageOption &option)
28 {
29 HDF_LOGE("%s: CameraDeviceStub::OnRemoteRequest entry!", __func__);
30 int32_t ret = HDF_SUCCESS;
31 switch (code) {
32 case CMD_CAMERA_DEVICE_GET_STREAM_OPERATOR: {
33 ret = CameraDeviceStubGetStreamOperator(data, reply, option);
34 break;
35 }
36 case CMD_CAMERA_DEVICE_UPDATE_SETTINGS: {
37 ret = CameraDeviceStubUpdateSettings(data, reply, option);
38 break;
39 }
40 case CMD_CAMERA_DEVICE_SET_RESULT_MODE: {
41 ret = CameraDeviceStubSetResultMode(data, reply, option);
42 break;
43 }
44 case CMD_CAMERA_DEVICE_GET_ENABLED_RESULTS: {
45 ret = CameraDeviceStubGetEnabledReuslts(data, reply, option);
46 break;
47 }
48 case CMD_CAMERA_DEVICE_ENABLE_RESULT: {
49 ret = CameraDeviceStubEnableResult(data, reply, option);
50 break;
51 }
52 case CMD_CAMERA_DEVICE_DISABLE_RESULT: {
53 ret = CameraDeviceStubDisableResult(data, reply, option);
54 break;
55 }
56 case CMD_CAMERA_DEVICE_CLOSE: {
57 ret = CameraDeviceStubClose(data, reply, option);
58 break;
59 }
60 default: {
61 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62 }
63 }
64 return ret;
65 }
66
CameraDeviceStubGetStreamOperator(MessageParcel & data,MessageParcel & reply,MessageOption & option)67 int32_t CameraDeviceStub::CameraDeviceStubGetStreamOperator(
68 MessageParcel& data, MessageParcel& reply, MessageOption& option)
69 {
70 if (data.ReadInterfaceToken() != CameraDeviceStub::GetDescriptor()) {
71 HDF_LOGE("%{public}s: invalid interface descriptor.", __func__);
72 return INVALID_ARGUMENT;
73 }
74
75 sptr<IStreamOperatorCallback> spStreamOperatorCallback = nullptr;
76 bool flag = data.ReadBool();
77 if (flag) {
78 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
79 spStreamOperatorCallback = OHOS::iface_cast<IStreamOperatorCallback>(remoteObj);
80 if (spStreamOperatorCallback == nullptr) {
81 HDF_LOGE("%s: read operator callback failed", __func__);
82 return HDF_FAILURE;
83 }
84 }
85
86 OHOS::sptr<IStreamOperator> streamOperator = nullptr;
87 CamRetCode ret = GetStreamOperator(spStreamOperatorCallback, streamOperator);
88 if (!reply.WriteInt32(static_cast<int32_t>(ret))) {
89 HDF_LOGE("%s: get stream operator failed", __func__);
90 return HDF_FAILURE;
91 }
92
93 bool nullFlag = (streamOperator != nullptr);
94 if (!reply.WriteBool(nullFlag)) {
95 HDF_LOGE("%s: stream operator flag write failed!", __func__);
96 return INVALID_ARGUMENT;
97 }
98
99 if (nullFlag && !reply.WriteRemoteObject(streamOperator->AsObject())) {
100 HDF_LOGE("%s: write stream operator failed", __func__);
101 return HDF_FAILURE;
102 }
103
104 return HDF_SUCCESS;
105 }
106
CameraDeviceStubUpdateSettings(MessageParcel & data,MessageParcel & reply,MessageOption & option)107 int32_t CameraDeviceStub::CameraDeviceStubUpdateSettings(
108 MessageParcel& data, MessageParcel& reply, MessageOption& option)
109 {
110 if (data.ReadInterfaceToken() != CameraDeviceStub::GetDescriptor()) {
111 HDF_LOGE("%{public}s: invalid interface descriptor.", __func__);
112 return INVALID_ARGUMENT;
113 }
114
115 std::shared_ptr<CameraMetadata> metadata = nullptr;
116 MetadataUtils::DecodeCameraMetadata(data, metadata);
117
118 CamRetCode ret = UpdateSettings(metadata);
119 if (!reply.WriteInt32(static_cast<int32_t>(ret))) {
120 HDF_LOGE("%s: get stream operator failed", __func__);
121 return HDF_FAILURE;
122 }
123
124 return HDF_SUCCESS;
125 }
126
CameraDeviceStubSetResultMode(MessageParcel & data,MessageParcel & reply,MessageOption & option)127 int32_t CameraDeviceStub::CameraDeviceStubSetResultMode(
128 MessageParcel& data, MessageParcel& reply, MessageOption& option)
129 {
130 if (data.ReadInterfaceToken() != CameraDeviceStub::GetDescriptor()) {
131 HDF_LOGE("%{public}s: invalid interface descriptor.", __func__);
132 return INVALID_ARGUMENT;
133 }
134
135 ResultCallbackMode mode = static_cast<ResultCallbackMode>(data.ReadInt32());
136 CamRetCode ret = SetResultMode(mode);
137 if (!reply.WriteInt32(static_cast<int32_t>(ret))) {
138 HDF_LOGE("%s: write retcode failed", __func__);
139 return HDF_FAILURE;
140 }
141
142 return HDF_SUCCESS;
143 }
144
CameraDeviceStubGetEnabledReuslts(MessageParcel & data,MessageParcel & reply,MessageOption & option)145 int32_t CameraDeviceStub::CameraDeviceStubGetEnabledReuslts(
146 MessageParcel& data, MessageParcel& reply, MessageOption& option)
147 {
148 if (data.ReadInterfaceToken() != CameraDeviceStub::GetDescriptor()) {
149 HDF_LOGE("%{public}s: invalid interface descriptor.", __func__);
150 return INVALID_ARGUMENT;
151 }
152
153 std::vector<int32_t> results;
154 CamRetCode ret = GetEnabledResults(results);
155 if (!reply.WriteInt32(static_cast<CamRetCode>(ret))) {
156 HDF_LOGE("%s: write retcode failed", __func__);
157 return HDF_FAILURE;
158 }
159
160 if (!reply.WriteInt32Vector(results)) {
161 HDF_LOGE("%s: write results failed", __func__);
162 return HDF_FAILURE;
163 }
164
165 return HDF_SUCCESS;
166 }
167
CameraDeviceStubEnableResult(MessageParcel & data,MessageParcel & reply,MessageOption & option)168 int32_t CameraDeviceStub::CameraDeviceStubEnableResult(
169 MessageParcel& data, MessageParcel& reply, MessageOption& option)
170 {
171 if (data.ReadInterfaceToken() != CameraDeviceStub::GetDescriptor()) {
172 HDF_LOGE("%{public}s: invalid interface descriptor.", __func__);
173 return INVALID_ARGUMENT;
174 }
175
176 std::vector<int32_t> results;
177 if (!data.ReadInt32Vector(&results)) {
178 HDF_LOGE("%s: read results failed", __func__);
179 return HDF_FAILURE;
180 }
181
182 CamRetCode ret = EnableResult(results);
183 if (!reply.WriteInt32(static_cast<CamRetCode>(ret))) {
184 HDF_LOGE("%s: write retcode failed", __func__);
185 return HDF_FAILURE;
186 }
187
188 if (!reply.WriteInt32Vector(results)) {
189 HDF_LOGE("%s: write results failed", __func__);
190 return HDF_FAILURE;
191 }
192
193 return HDF_SUCCESS;
194 }
195
CameraDeviceStubDisableResult(MessageParcel & data,MessageParcel & reply,MessageOption & option)196 int32_t CameraDeviceStub::CameraDeviceStubDisableResult(
197 MessageParcel& data, MessageParcel& reply, MessageOption& option)
198 {
199 if (data.ReadInterfaceToken() != CameraDeviceStub::GetDescriptor()) {
200 HDF_LOGE("%{public}s: invalid interface descriptor.", __func__);
201 return INVALID_ARGUMENT;
202 }
203
204 std::vector<int32_t> results;
205 if (!data.ReadInt32Vector(&results)) {
206 HDF_LOGE("%s: read results failed", __func__);
207 return HDF_FAILURE;
208 }
209
210 CamRetCode ret = DisableResult(results);
211 if (!reply.WriteInt32(static_cast<CamRetCode>(ret))) {
212 HDF_LOGE("%s: write retcode failed", __func__);
213 return HDF_FAILURE;
214 }
215
216 if (!reply.WriteInt32Vector(results)) {
217 HDF_LOGE("%s: write results failed", __func__);
218 return HDF_FAILURE;
219 }
220
221 return HDF_SUCCESS;
222 }
223
CameraDeviceStubClose(MessageParcel & data,MessageParcel & reply,MessageOption & option)224 int32_t CameraDeviceStub::CameraDeviceStubClose(
225 MessageParcel& data, MessageParcel& reply, MessageOption& option)
226 {
227 if (data.ReadInterfaceToken() != CameraDeviceStub::GetDescriptor()) {
228 HDF_LOGE("%{public}s: invalid interface descriptor.", __func__);
229 return INVALID_ARGUMENT;
230 }
231
232 Close();
233 return HDF_SUCCESS;
234 }
235 }
236