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_proxy.h"
17 #include <hdf_base.h>
18 #include <message_parcel.h>
19 #include "utils_data_stub.h"
20 #include "istream_operator.h"
21 #include "istream_operator_callback.h"
22
23 namespace OHOS::Camera {
GetStreamOperator(const OHOS::sptr<IStreamOperatorCallback> & callback,OHOS::sptr<IStreamOperator> & streamOperator)24 CamRetCode CameraDeviceProxy::GetStreamOperator(
25 const OHOS::sptr<IStreamOperatorCallback> &callback,
26 OHOS::sptr<IStreamOperator> &streamOperator)
27 {
28 MessageParcel data;
29 MessageParcel reply;
30 MessageOption option;
31
32 bool nullFlag = (callback != nullptr);
33 if (!data.WriteBool(nullFlag)) {
34 HDF_LOGE("%s: stream operator callback flag write failed!", __func__);
35 return INVALID_ARGUMENT;
36 }
37
38 if (nullFlag && !data.WriteRemoteObject(callback->AsObject())) {
39 HDF_LOGE("%s: write stream operator failed", __func__);
40 return INVALID_ARGUMENT;
41 }
42
43 int32_t ret = Remote()->SendRequest(
44 CMD_CAMERA_DEVICE_REMOTE_GET_STREAM_OPERATOR, data, reply, option);
45 if (ret != HDF_SUCCESS) {
46 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
47 return INVALID_ARGUMENT;
48 }
49
50 CamRetCode retCode = static_cast<CamRetCode>(reply.ReadInt32());
51 bool flag = reply.ReadBool();
52 if (flag) {
53 sptr<IRemoteObject> remoteStreamOperator = reply.ReadRemoteObject();
54 streamOperator = OHOS::iface_cast<IStreamOperator>(remoteStreamOperator);
55 }
56
57 return retCode;
58 }
59
UpdateSettings(const std::shared_ptr<CameraSetting> & settings)60 CamRetCode CameraDeviceProxy::UpdateSettings(const std::shared_ptr<CameraSetting> &settings)
61 {
62 MessageParcel data;
63 MessageParcel reply;
64 MessageOption option;
65
66 bool bRet = UtilsDataStub::EncodeCameraMetadata(settings, data);
67 if (!bRet) {
68 HDF_LOGE("%s: write metadata failed", __func__);
69 return INVALID_ARGUMENT;
70 }
71
72 int32_t ret = Remote()->SendRequest(
73 CMD_CAMERA_DEVICE_UPDATE_SETTINGS, data, reply, option);
74 if (ret != HDF_SUCCESS) {
75 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
76 return INVALID_ARGUMENT;
77 }
78
79 return static_cast<CamRetCode>(reply.ReadInt32());
80 }
81
SetResultMode(const ResultCallbackMode & mode)82 CamRetCode CameraDeviceProxy::SetResultMode(const ResultCallbackMode &mode)
83 {
84 MessageParcel data;
85 MessageParcel reply;
86 MessageOption option;
87
88 if (!data.WriteInt32(static_cast<int32_t>(mode))) {
89 HDF_LOGE("%s: write result callback mode failed", __func__);
90 return INVALID_ARGUMENT;
91 }
92
93 int32_t ret = Remote()->SendRequest(
94 CMD_CAMERA_DEVICE_REMOTE_SET_RESULT_MODE, data, reply, option);
95 if (ret != HDF_SUCCESS) {
96 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
97 return INVALID_ARGUMENT;
98 }
99
100 return static_cast<CamRetCode>(reply.ReadInt32());
101 }
102
GetEnabledResults(std::vector<MetaType> & results)103 CamRetCode CameraDeviceProxy::GetEnabledResults(std::vector<MetaType> &results)
104 {
105 MessageParcel data;
106 MessageParcel reply;
107 MessageOption option;
108
109 int32_t ret = Remote()->SendRequest(
110 CMD_CAMERA_DEVICE_REMOTE_GET_ENABLED_RESULTS, data, reply, option);
111 if (ret != HDF_SUCCESS) {
112 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
113 return INVALID_ARGUMENT;
114 }
115
116 CamRetCode retCode = static_cast<CamRetCode>(reply.ReadInt32());
117 if (!reply.ReadInt32Vector(&results)) {
118 HDF_LOGE("%s: read results failed", __func__);
119 return INVALID_ARGUMENT;
120 }
121
122 return retCode;
123 }
124
EnableResult(const std::vector<MetaType> & results)125 CamRetCode CameraDeviceProxy::EnableResult(const std::vector<MetaType> &results)
126 {
127 MessageParcel data;
128 MessageParcel reply;
129 MessageOption option;
130
131 if (!data.WriteInt32Vector(results)) {
132 HDF_LOGE("%s: write results failed", __func__);
133 return INVALID_ARGUMENT;
134 }
135
136 int32_t ret = Remote()->SendRequest(
137 CMD_CAMERA_DEVICE_REMOTE_ENABLE_RESULT, data, reply, option);
138 if (ret != HDF_SUCCESS) {
139 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
140 return INVALID_ARGUMENT;
141 }
142
143 return static_cast<CamRetCode>(reply.ReadInt32());
144 }
145
DisableResult(const std::vector<MetaType> & results)146 CamRetCode CameraDeviceProxy::DisableResult(const std::vector<MetaType> &results)
147 {
148 MessageParcel data;
149 MessageParcel reply;
150 MessageOption option;
151
152 if (!data.WriteInt32Vector(results)) {
153 HDF_LOGE("%s: write results failed", __func__);
154 return INVALID_ARGUMENT;
155 }
156
157 int32_t ret = Remote()->SendRequest(
158 CMD_CAMERA_DEVICE_REMOTE_DISABLE_RESULT, data, reply, option);
159 if (ret != HDF_SUCCESS) {
160 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
161 return INVALID_ARGUMENT;
162 }
163
164 return static_cast<CamRetCode>(reply.ReadInt32());
165 }
166
Close()167 void CameraDeviceProxy::Close()
168 {
169 MessageParcel data;
170 MessageParcel reply;
171 MessageOption option;
172
173 int32_t ret = Remote()->SendRequest(
174 CMD_CAMERA_DEVICE_REMOTE_CLOSE, data, reply, option);
175 if (ret != HDF_SUCCESS) {
176 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
177 }
178 }
179 }