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